From 5768cae94d8d2b19b78797f14c2e9adb1c77d68e Mon Sep 17 00:00:00 2001 From: Javier Martinez Date: Thu, 26 Sep 2024 14:30:20 +0000 Subject: [PATCH 1/2] Add new layout for simplfied arrays (strings) --- src/lib/editor/form/AbstractField.svelte | 80 +++++++++++++---------- src/lib/editor/form/FieldButtons.svelte | 12 ++-- src/lib/editor/form/FieldTitle.svelte | 25 +++++-- src/lib/editor/form/RootField.svelte | 15 ++++- src/lib/editor/form/SectionWrapper.svelte | 33 ++++++---- src/lib/editor/form/utils/model.ts | 2 +- 6 files changed, 106 insertions(+), 61 deletions(-) diff --git a/src/lib/editor/form/AbstractField.svelte b/src/lib/editor/form/AbstractField.svelte index 91c0ecf..24eebfe 100644 --- a/src/lib/editor/form/AbstractField.svelte +++ b/src/lib/editor/form/AbstractField.svelte @@ -12,6 +12,7 @@ import BooleanField from "./BooleanField.svelte"; import clsx from "clsx"; import { getBuilderContext } from "$lib/store/builder"; + import type { Schema } from "./utils/schema"; const dispatch = createEventDispatcher(); @@ -34,6 +35,7 @@ export let readOnly = false; + $: childrenType = (field.schema.items as Schema)?.type; $: isHover = $activeItem === field.id; $: highlight = $highlightedItem === field.id; $: showContextMenu = !readOnly && isHover; @@ -42,12 +44,12 @@ const offset = top + height - window.innerHeight; contextMenuOffset = offset > 0 ? offset : 0; } - $: isParent = ["object", "array"].includes(field.type); + $: isParent = ["object", "array"].includes(field.type) && childrenType !== "string"; $: isSection = isParent && field.parent?.is.root; $: wrapperClasses = clsx({ "bg-neutral-50 border-neutral-100": isHover && !isParent, "border-transparent": !isHover, - "border-l border-t border-b": !isParent, + "border-l border-t border-b": !["object", "array"].includes(field.type), "border-r rounded-r": readOnly && !isParent, "pl-2": isParent && !isSection, }); @@ -55,6 +57,7 @@ "bg-neutral-50": isHover, "border-workspace-accent-500": highlight, "border-neutral-200": !highlight, + "border-l border-t border-b": childrenType !== "string", }); $: contextMenuClasses = clsx({ "mt-1": isParent && !isSection && !field.is.root, @@ -78,20 +81,29 @@ function handleAddField() { showAddMenu = false; + // Children of string arrays can add items directly from the row + if (field.type === "string" && field.parent?.type === "array") { + addFieldToArray(field.parent); + return; + } + // @note: Add field directly instead of showing the dropdown option list if (field.type === "array" || field.controlType === "dictionary") { - const [childOption] = field.options || []; - - const newField = field.addChildField(childOption); - newField?.tryFocus(); - dispatch("fieldAdded", newField); - + addFieldToArray(field); return; } showAddMenu = true; } + function addFieldToArray(f: UIModelField) { + const [childOption] = f.options || []; + + const newField = f.addChildField(childOption); + newField?.tryFocus(); + dispatch("fieldAdded", newField); + } + function focusNextField(nextField = field.getNextFocusableField()) { if (!nextField) return; nextField.tryFocus(5, 0); @@ -156,7 +168,7 @@ >
{#if isParent && !isSection && !field.is.root} -
+
{/if}
-
- - - - {#if showAddMenu} -
+ - (showAddMenu = false)} /> -
- {/if} -
+ + + {#if showAddMenu} +
+ (showAddMenu = false)} /> +
+ {/if} + + {/if}