From 34249143e772cf886ddccc4f0cb1e4b5be69905c Mon Sep 17 00:00:00 2001 From: Clement Zhang Date: Thu, 7 Nov 2024 14:18:14 +0100 Subject: [PATCH] every app components: switching from static to eval preserves the user data input --- .../ArrayStaticInputEditor.svelte | 4 +- .../inputEditor/StaticInputEditor.svelte | 39 ++++++++++--------- 2 files changed, 22 insertions(+), 21 deletions(-) diff --git a/frontend/src/lib/components/apps/editor/settingsPanel/ArrayStaticInputEditor.svelte b/frontend/src/lib/components/apps/editor/settingsPanel/ArrayStaticInputEditor.svelte index 89bb7f2ad7..29bd8f9e5d 100644 --- a/frontend/src/lib/components/apps/editor/settingsPanel/ArrayStaticInputEditor.svelte +++ b/frontend/src/lib/components/apps/editor/settingsPanel/ArrayStaticInputEditor.svelte @@ -229,9 +229,7 @@ newComponentInput.expr = JSON.stringify(newComponentInput.value) newComponentInput.isLabeled = labeled - const newSubFieldType = labeled ? 'labeledselect' : 'simplestringselect' - - dispatch('componentInputChange', { newComponentInput, newSubFieldType }) + dispatch('componentInputChange', newComponentInput) } /** Transforms items string[] to ObjectOption[ and vice versa. Mutating items will fire handleItemsChange. */ diff --git a/frontend/src/lib/components/apps/editor/settingsPanel/inputEditor/StaticInputEditor.svelte b/frontend/src/lib/components/apps/editor/settingsPanel/inputEditor/StaticInputEditor.svelte index ac68595dcf..65c9722583 100644 --- a/frontend/src/lib/components/apps/editor/settingsPanel/inputEditor/StaticInputEditor.svelte +++ b/frontend/src/lib/components/apps/editor/settingsPanel/inputEditor/StaticInputEditor.svelte @@ -39,30 +39,33 @@ onMount(() => { /** preserve the `labeled` toggling when switching from eval to static */ - if ( - componentInput?.isLabeled && - fieldType === 'array' && - subFieldType === 'simplestringselect' - ) { - subFieldType = 'labeledselect' + if (componentInput !== undefined) { + handleComponentInputChange(componentInput) } }) /** - * Workaround for MultiSelect + * Every components: switching from `static` to `eval` preserves the user data * - * Changing fieldType in needs to mutate this component data and this component data type. + * Multiselect: toggling `labeled` needs to mutate input value and input fieldType. * */ - function handleComponentInputChange(newData: { - newComponentInput: StaticInput - newSubFieldType: InputType - }) { - if (!newData) { - console.error('fired a componentInputChange with no data?') - return - } - const { newComponentInput, newSubFieldType } = newData + function handleComponentInputChange(newComponentInput: StaticInput) { componentInput = { ...componentInput, ...newComponentInput } - subFieldType = newSubFieldType + + if (componentInput?.isLabeled) { + if (fieldType === 'array' && subFieldType === 'simplestringselect') { + subFieldType = 'labeledselect' + } + if (fieldType === 'simplestringselect') { + fieldType = 'labeledselect' + } + } else if (componentInput?.isLabeled === false) { + if (fieldType === 'array' && subFieldType === 'labeledselect') { + subFieldType = 'simplestringselect' + } + if (fieldType === 'labeledselect') { + fieldType = 'simplestringselect' + } + } }