From b522726afcb3a9a519a513832bf75d86a37a006d Mon Sep 17 00:00:00 2001 From: Andrew Kingston Date: Thu, 28 Oct 2021 12:43:31 +0100 Subject: [PATCH] Allow in-preview editing of paragraphs and headings --- packages/client/manifest.json | 2 ++ .../client/src/components/Component.svelte | 25 ++++++++++++----- .../client/src/components/app/Heading.svelte | 19 ++++++++++--- .../client/src/components/app/Text.svelte | 19 ++++++++++--- .../src/components/preview/Indicator.svelte | 3 +-- .../preview/SelectionIndicator.svelte | 6 ++++- packages/client/src/stores/builder.js | 20 +++++++------- packages/client/src/utils/styleable.js | 27 +++++++++++++------ 8 files changed, 88 insertions(+), 33 deletions(-) diff --git a/packages/client/manifest.json b/packages/client/manifest.json index 2adfd96626..63ac7b1c7f 100644 --- a/packages/client/manifest.json +++ b/packages/client/manifest.json @@ -589,6 +589,7 @@ "icon": "TextParagraph", "illegalChildren": ["section"], "showSettingsBar": true, + "editable": true, "settings": [ { "type": "text", @@ -695,6 +696,7 @@ "description": "A component for displaying heading text", "illegalChildren": ["section"], "showSettingsBar": true, + "editable": true, "settings": [ { "type": "text", diff --git a/packages/client/src/components/Component.svelte b/packages/client/src/components/Component.svelte index faf0226604..8200812477 100644 --- a/packages/client/src/components/Component.svelte +++ b/packages/client/src/components/Component.svelte @@ -60,21 +60,32 @@ $: instanceKey = JSON.stringify(rawProps) $: updateComponentProps(rawProps, instanceKey, $context) $: selected = - $builderStore.inBuilder && - $builderStore.selectedComponentId === instance._id + $builderStore.inBuilder && $builderStore.selectedComponentId === id $: inSelectedPath = $builderStore.selectedComponentPath?.includes(id) $: evaluateConditions(enrichedSettings?._conditions) $: componentSettings = { ...enrichedSettings, ...conditionalSettings } $: renderKey = `${propsHash}-${emptyState}` + $: editable = definition.editable + $: editing = editable && selected && $builderStore.editMode + $: draggable = interactive && !isLayout && !isScreen && !editing + $: droppable = interactive && !isLayout && !isScreen // Update component context $: componentStore.set({ id, children: children.length, - styles: { ...instance._styles, id, empty: emptyState, interactive }, + styles: { + ...instance._styles, + id, + empty: emptyState, + interactive, + draggable, + editable, + }, empty: emptyState, selected, name, + editing, }) const getRawProps = instance => { @@ -171,10 +182,6 @@ conditionalSettings = result.settingUpdates visible = nextVisible } - - // Drag and drop helper tags - $: draggable = interactive && !isLayout && !isScreen - $: droppable = interactive && !isLayout && !isScreen {#key renderKey} @@ -187,6 +194,7 @@ class:droppable class:empty class:interactive + class:editing data-id={id} data-name={name} > @@ -213,4 +221,7 @@ .draggable :global(*:hover) { cursor: grab; } + .editing :global(*:hover) { + cursor: auto; + } diff --git a/packages/client/src/components/app/Heading.svelte b/packages/client/src/components/app/Heading.svelte index bf4d902017..f3e283cf97 100644 --- a/packages/client/src/components/app/Heading.svelte +++ b/packages/client/src/components/app/Heading.svelte @@ -36,21 +36,34 @@ }, } } + + // Convert contenteditable HTML to text and save + const updateText = e => { + const html = e.target.innerHTML + const sanitized = html + .replace(/<\/div>
/gi, "\n") + .replace(/
/gi, "") + .replace(/<\/div>/gi, "") + .replace(/
/gi, "") + builderStore.actions.updateProp("text", sanitized) + } -

{componentText} -

+