From 706f9b5d4abae8bf6f991c5c1180997b329a36e9 Mon Sep 17 00:00:00 2001 From: Andrew Kingston Date: Tue, 5 Mar 2024 19:11:34 +0000 Subject: [PATCH] Update snippets to be an array --- .../common/bindings/BindingPanel.svelte | 35 ++++----- .../common/bindings/ClientBindingPanel.svelte | 3 +- .../common/bindings/ServerBindingPanel.svelte | 2 + .../common/bindings/SnippetSidePanel.svelte | 37 ---------- .../src/components/common/bindings/utils.js | 6 +- packages/builder/src/stores/builder/index.js | 3 + .../builder/src/stores/builder/snippets.js | 71 +++++++++++++++++++ .../builder/src/stores/builder/websocket.js | 2 + .../jsRunner/bundles/snippets.ivm.bundle.js | 4 +- .../server/src/jsRunner/bundles/snippets.ts | 6 +- .../src/helpers/javascript.js | 4 +- 11 files changed, 110 insertions(+), 63 deletions(-) create mode 100644 packages/builder/src/stores/builder/snippets.js diff --git a/packages/builder/src/components/common/bindings/BindingPanel.svelte b/packages/builder/src/components/common/bindings/BindingPanel.svelte index 8951ba1207..9efab620af 100644 --- a/packages/builder/src/components/common/bindings/BindingPanel.svelte +++ b/packages/builder/src/components/common/bindings/BindingPanel.svelte @@ -41,6 +41,7 @@ export let allowHelpers = true export let allowSnippets = true export let context = null + export let snippets = null export let autofocusEditor = false const drawerContext = getContext("drawer") @@ -69,14 +70,14 @@ $: drawerContext?.modal.subscribe(val => (drawerIsModal = val)) $: editorModeOptions = allowJS ? [Modes.Text, Modes.JavaScript] : [Modes.Text] $: sidePanelOptions = getSidePanelOptions(context, allowSnippets, mode) - $: enrichedBindings = enrichBindings(bindings, context) + $: enrichedBindings = enrichBindings(bindings, context, snippets) $: usingJS = mode === Modes.JavaScript $: editorMode = mode === Modes.JavaScript ? EditorModes.JS : EditorModes.Handlebars $: editorValue = editorMode === EditorModes.JS ? jsValue : hbsValue $: bindingCompletions = bindingsToCompletions(enrichedBindings, editorMode) $: runtimeExpression = readableToRuntimeBinding(enrichedBindings, value) - $: requestUpdateEvaluation(runtimeExpression, context) + $: requestEval(runtimeExpression, context, snippets) $: bindingHelpers = new BindingHelpers(getCaretPosition, insertAtPos) $: { if (!sidePanelOptions.includes(sidePanel)) { @@ -95,20 +96,23 @@ return options } - const debouncedUpdateEvaluation = Utils.debounce((expression, context) => { - expressionResult = processStringSync(expression || "", context) + const debouncedEval = Utils.debounce((expression, context, snippets) => { + expressionResult = processStringSync(expression || "", { + ...context, + snippets, + }) evaluating = false }, 260) - const requestUpdateEvaluation = (expression, context) => { + const requestEval = (expression, context, snippets) => { evaluating = true - debouncedUpdateEvaluation(expression, context) + debouncedEval(expression, context, snippets) } - const getBindingValue = (binding, context) => { + const getBindingValue = (binding, context, snippets) => { const js = `return $("${binding.runtimeBinding}")` const hbs = encodeJSBinding(js) - const res = processStringSync(hbs, context) + const res = processStringSync(hbs, { ...context, snippets }) return JSON.stringify(res, null, 2) } @@ -123,9 +127,9 @@ }) } - const enrichBindings = (bindings, context) => { + const enrichBindings = (bindings, context, snippets) => { return bindings.map(binding => { - const value = getBindingValue(binding, context) + const value = getBindingValue(binding, context, snippets) return { ...binding, value, @@ -139,7 +143,7 @@ valid = isValid(runtimeExpression) if (valid) { dispatch("change", val) - requestUpdateEvaluation(runtimeExpression, context) + requestEval(runtimeExpression, context, snippets) } } @@ -152,10 +156,6 @@ bindingHelpers.onSelectBinding(js ? jsValue : hbsValue, binding, { js }) } - const onSelectSnippet = snippet => { - bindingHelpers.onSelectSnippet(jsValue, snippet) - } - const changeMode = newMode => { if (targetMode || newMode === mode) { return @@ -316,7 +316,10 @@ expression={editorValue} /> {:else if sidePanel === SidePanels.Snippets} - + bindingHelpers.onSelectSnippet(snippet)} + {snippets} + /> {/if} diff --git a/packages/builder/src/components/common/bindings/ClientBindingPanel.svelte b/packages/builder/src/components/common/bindings/ClientBindingPanel.svelte index 97cb097a51..d3e80cf696 100644 --- a/packages/builder/src/components/common/bindings/ClientBindingPanel.svelte +++ b/packages/builder/src/components/common/bindings/ClientBindingPanel.svelte @@ -1,6 +1,6 @@