From 0b3a084c81d7dfdf16d3dec4f81056b07d6a91b8 Mon Sep 17 00:00:00 2001 From: Ruben Fiszel Date: Fri, 13 Jun 2025 14:10:55 +0200 Subject: [PATCH] fix handle default values --- frontend/src/lib/components/ArgInput.svelte | 18 ++++++------- .../lib/components/EditableSchemaForm.svelte | 1 - frontend/src/lib/components/SchemaForm.svelte | 27 +++++++++---------- .../schema/EditableSchemaDrawer.svelte | 20 +++++++------- .../components/schema/SchemaFormDND.svelte | 1 + 5 files changed, 34 insertions(+), 33 deletions(-) diff --git a/frontend/src/lib/components/ArgInput.svelte b/frontend/src/lib/components/ArgInput.svelte index 20c9efdb0d..0670de684f 100644 --- a/frontend/src/lib/components/ArgInput.svelte +++ b/frontend/src/lib/components/ArgInput.svelte @@ -235,6 +235,8 @@ let rawValue: string | undefined = $state(undefined) + let lastValue: any = null + function computeDefaultValue(inputCat?: string, defaultValue?: any, nnullable?: boolean) { let nvalue: any = null if (label == 'toString' && typeof value == 'function') { @@ -257,16 +259,14 @@ } } - if (nnullable && type === 'string' && value === '' && nvalue != null) { + if (nnullable && type === 'string' && value === '' && nvalue == null) { value = null } else if (nvalue != null && !deepEqual(nvalue, value)) { - console.log('set value 2', label, nvalue, value) value = nvalue + lastValue = value } } - let lastValue: any = $state(undefined) - // By setting isListJson to true, we can render inputs even if the value is not an array of the correct type // This avoids the issue of the input being rendered as a string with value: [object Object], or as a number with value: NaN function checkArrayValueType() { @@ -353,7 +353,6 @@ } onMount(() => { - computeDefaultValue() evalValueToRaw() }) @@ -457,7 +456,8 @@ $effect(() => { oneOf && value && untrack(() => onOneOfChange()) }) - $effect(() => { + + $effect.pre(() => { value let args = [inputCat, defaultValue, nullable] @@ -466,7 +466,7 @@ computeDefaultValue(...args) }) }) - $effect(() => { + $effect.pre(() => { !isListJson && inputCat === 'list' && value != lastValue && @@ -474,10 +474,10 @@ !hasIsListJsonChanged && untrack(() => checkArrayValueType()) }) - $effect(() => { + $effect.pre(() => { defaultValue != undefined && untrack(() => handleDefaultValueChange()) }) - $effect(() => { + $effect.pre(() => { ;(inputCat && (isObjectCat(inputCat) || isRawStringEditor(inputCat)) && !oneOf && diff --git a/frontend/src/lib/components/EditableSchemaForm.svelte b/frontend/src/lib/components/EditableSchemaForm.svelte index a8cd14920c..b19325f45b 100644 --- a/frontend/src/lib/components/EditableSchemaForm.svelte +++ b/frontend/src/lib/components/EditableSchemaForm.svelte @@ -337,7 +337,6 @@ bind:schema={ () => (previewSchema ? previewSchema : schema), (newSchema) => { - console.log('schemaChange set', $state.snapshot(newSchema)) schema = newSchema tick().then(() => dispatch('change', schema)) } diff --git a/frontend/src/lib/components/SchemaForm.svelte b/frontend/src/lib/components/SchemaForm.svelte index 977aaf2ff5..32d5e67d6c 100644 --- a/frontend/src/lib/components/SchemaForm.svelte +++ b/frontend/src/lib/components/SchemaForm.svelte @@ -114,12 +114,6 @@ actions }: Props = $props() - $effect.pre(() => { - if (args == undefined) { - args = {} - } - }) - const dispatch = createEventDispatcher() let inputCheck: { [id: string]: boolean } = $state({}) @@ -222,27 +216,32 @@ } } - $effect(() => { + $effect.pre(() => { if (args == undefined || typeof args !== 'object') { args = {} } }) - $effect(() => { - const newKeys = Array.isArray(schema?.order) - ? schema?.order - : Object.keys(schema?.properties ?? {}) + $effect.pre(() => { + const newKeys = [ + ...new Set( + (Array.isArray(schema?.order) + ? schema?.order + : Object.keys(schema?.properties ?? {})) as string[] + ) + ] + if (!deepEqual(keys, newKeys)) { keys = newKeys } }) - $effect(() => { + $effect.pre(() => { schema && (untrack(() => reorder()), (hidden = {})) }) - $effect(() => { + $effect.pre(() => { ;[schema, args] untrack(() => handleHiddenFields(schema, args ?? {})) }) - $effect(() => { + $effect.pre(() => { isValid = allTrue(inputCheck ?? {}) }) const actions_render = $derived(actions) diff --git a/frontend/src/lib/components/schema/EditableSchemaDrawer.svelte b/frontend/src/lib/components/schema/EditableSchemaDrawer.svelte index 83adba49af..76aa5892f9 100644 --- a/frontend/src/lib/components/schema/EditableSchemaDrawer.svelte +++ b/frontend/src/lib/components/schema/EditableSchemaDrawer.svelte @@ -61,14 +61,16 @@ let schemaString: string = $state(JSON.stringify(schema, null, '\t')) let editor: SimpleEditor | undefined = $state(undefined) let error: string | undefined = $state(undefined) - let items = $derived( - ((schema?.order ?? Object.keys(schema?.properties ?? {}))?.map((item, index) => { - return { value: item, id: item } - }) ?? []) as Array<{ - value: string - id: string - }> - ) + let items = $derived([ + ...new Set( + (schema?.order ?? Object.keys(schema?.properties ?? {}))?.map((item, index) => { + return { value: item, id: item } + }) ?? [] + ) + ]) as Array<{ + value: string + id: string + }>
@@ -163,7 +165,7 @@