Fix existing issue of CodeEditor completions not being reactive and allow saving invalid HBS bindings

This commit is contained in:
Andrew Kingston 2024-03-11 13:18:53 +00:00
parent 07ea080ab8
commit a3b1062d09
7 changed files with 41 additions and 55 deletions

View File

@ -77,6 +77,18 @@
$: runtimeExpression = readableToRuntimeBinding(enrichedBindings, value) $: runtimeExpression = readableToRuntimeBinding(enrichedBindings, value)
$: requestUpdateEvaluation(runtimeExpression, context) $: requestUpdateEvaluation(runtimeExpression, context)
$: bindingHelpers = new BindingHelpers(getCaretPosition, insertAtPos) $: bindingHelpers = new BindingHelpers(getCaretPosition, insertAtPos)
$: hbsCompletions = [
hbAutocomplete([
...bindingCompletions,
...getHelperCompletions(EditorModes.Handlebars),
]),
]
$: jsCompletions = [
jsAutocomplete([
...bindingCompletions,
...getHelperCompletions(EditorModes.JS),
]),
]
const debouncedUpdateEvaluation = Utils.debounce((expression, context) => { const debouncedUpdateEvaluation = Utils.debounce((expression, context) => {
expressionResult = processStringSync(expression || "", context) expressionResult = processStringSync(expression || "", context)
@ -217,31 +229,24 @@
</div> </div>
<div class="editor"> <div class="editor">
{#if mode === Modes.Text} {#if mode === Modes.Text}
{#key hbsCompletions}
<CodeEditor <CodeEditor
value={hbsValue} value={hbsValue}
on:change={onChangeHBSValue} on:change={onChangeHBSValue}
bind:getCaretPosition bind:getCaretPosition
bind:insertAtPos bind:insertAtPos
completions={[ completions={hbsCompletions}
hbAutocomplete([
...bindingCompletions,
...getHelperCompletions(editorMode),
]),
]}
autofocus={autofocusEditor} autofocus={autofocusEditor}
placeholder="Add bindings by typing &#123;&#123; or use the menu on the right" placeholder="Add bindings by typing &#123;&#123; or use the menu on the right"
jsBindingWrapping={false} jsBindingWrapping={false}
/> />
{/key}
{:else if mode === Modes.JavaScript} {:else if mode === Modes.JavaScript}
{#key jsCompletions}
<CodeEditor <CodeEditor
value={decodeJSBinding(jsValue)} value={decodeJSBinding(jsValue)}
on:change={onChangeJSValue} on:change={onChangeJSValue}
completions={[ completions={jsCompletions}
jsAutocomplete([
...bindingCompletions,
...getHelperCompletions(editorMode),
]),
]}
mode={EditorModes.JS} mode={EditorModes.JS}
bind:getCaretPosition bind:getCaretPosition
bind:insertAtPos bind:insertAtPos
@ -249,6 +254,7 @@
placeholder="Add bindings by typing $ or use the menu on the right" placeholder="Add bindings by typing $ or use the menu on the right"
jsBindingWrapping jsBindingWrapping
/> />
{/key}
{/if} {/if}
{#if targetMode} {#if targetMode}
<div class="mode-overlay"> <div class="mode-overlay">

View File

@ -4,7 +4,6 @@
import { onMount } from "svelte" import { onMount } from "svelte"
export let bindings = [] export let bindings = []
export let valid
export let value = "" export let value = ""
export let allowJS = false export let allowJS = false
export let allowHelpers = true export let allowHelpers = true
@ -27,7 +26,6 @@
</script> </script>
<BindingPanel <BindingPanel
bind:valid
bindings={enrichedBindings} bindings={enrichedBindings}
context={$previewStore.selectedComponentContext} context={$previewStore.selectedComponentContext}
{value} {value}

View File

@ -22,7 +22,6 @@
const dispatch = createEventDispatcher() const dispatch = createEventDispatcher()
let bindingDrawer let bindingDrawer
let valid = true
$: readableValue = runtimeToReadableBinding(bindings, value) $: readableValue = runtimeToReadableBinding(bindings, value)
$: tempValue = readableValue $: tempValue = readableValue
@ -80,15 +79,12 @@
</div> </div>
<Drawer bind:this={bindingDrawer} title={title ?? placeholder ?? "Bindings"}> <Drawer bind:this={bindingDrawer} title={title ?? placeholder ?? "Bindings"}>
<Button cta slot="buttons" on:click={handleClose} disabled={!valid}> <Button cta slot="buttons" on:click={handleClose}>Save</Button>
Save
</Button>
<svelte:component <svelte:component
this={panel} this={panel}
slot="body" slot="body"
value={readableValue} value={readableValue}
close={handleClose} close={handleClose}
bind:valid
on:change={event => (tempValue = event.detail)} on:change={event => (tempValue = event.detail)}
{bindings} {bindings}
{allowJS} {allowJS}

View File

@ -28,7 +28,6 @@
const dispatch = createEventDispatcher() const dispatch = createEventDispatcher()
let bindingDrawer let bindingDrawer
let valid = true
let currentVal = value let currentVal = value
$: readableValue = runtimeToReadableBinding(bindings, value) $: readableValue = runtimeToReadableBinding(bindings, value)
@ -93,13 +92,10 @@
title={title ?? placeholder ?? "Bindings"} title={title ?? placeholder ?? "Bindings"}
{forceModal} {forceModal}
> >
<Button cta slot="buttons" disabled={!valid} on:click={saveBinding}> <Button cta slot="buttons" on:click={saveBinding}>Save</Button>
Save
</Button>
<svelte:component <svelte:component
this={panel} this={panel}
slot="body" slot="body"
bind:valid
value={readableValue} value={readableValue}
on:change={event => (tempValue = event.detail)} on:change={event => (tempValue = event.detail)}
{bindings} {bindings}

View File

@ -25,7 +25,6 @@
const dispatch = createEventDispatcher() const dispatch = createEventDispatcher()
let bindingDrawer let bindingDrawer
let valid = true
let currentVal = value let currentVal = value
$: readableValue = runtimeToReadableBinding(bindings, value) $: readableValue = runtimeToReadableBinding(bindings, value)
@ -176,13 +175,10 @@
title={title ?? placeholder ?? "Bindings"} title={title ?? placeholder ?? "Bindings"}
left={drawerLeft} left={drawerLeft}
> >
<Button cta slot="buttons" disabled={!valid} on:click={saveBinding}> <Button cta slot="buttons" on:click={saveBinding}>Save</Button>
Save
</Button>
<svelte:component <svelte:component
this={panel} this={panel}
slot="body" slot="body"
bind:valid
value={readableValue} value={readableValue}
on:change={event => (tempValue = event.detail)} on:change={event => (tempValue = event.detail)}
{bindings} {bindings}

View File

@ -2,7 +2,6 @@
import BindingPanel from "./BindingPanel.svelte" import BindingPanel from "./BindingPanel.svelte"
export let bindings = [] export let bindings = []
export let valid
export let value = "" export let value = ""
export let allowJS = false export let allowJS = false
export let context = null export let context = null
@ -20,7 +19,6 @@
</script> </script>
<BindingPanel <BindingPanel
bind:valid
bindings={enrichedBindings} bindings={enrichedBindings}
{value} {value}
{allowJS} {allowJS}

View File

@ -48,7 +48,6 @@
let drawer let drawer
let tmpQueryParams let tmpQueryParams
let tmpCustomData let tmpCustomData
let customDataValid = true
let modal let modal
$: text = value?.label ?? "Choose an option" $: text = value?.label ?? "Choose an option"
@ -267,14 +266,11 @@
<Drawer title="Custom data" bind:this={drawer}> <Drawer title="Custom data" bind:this={drawer}>
<div slot="buttons" style="display:contents"> <div slot="buttons" style="display:contents">
<Button primary on:click={promptForCSV}>Load CSV</Button> <Button primary on:click={promptForCSV}>Load CSV</Button>
<Button cta on:click={saveCustomData} disabled={!customDataValid}> <Button cta on:click={saveCustomData}>Save</Button>
Save
</Button>
</div> </div>
<div slot="description">Provide a JSON array to use as data</div> <div slot="description">Provide a JSON array to use as data</div>
<ClientBindingPanel <ClientBindingPanel
slot="body" slot="body"
bind:valid={customDataValid}
value={tmpCustomData} value={tmpCustomData}
on:change={event => (tmpCustomData = event.detail)} on:change={event => (tmpCustomData = event.detail)}
{bindings} {bindings}