Only save textContent of editable fields to fix HTML being inserted, and fully remount component when toggling editing to fix chrome issues

This commit is contained in:
Andrew Kingston 2022-03-30 09:32:19 +01:00
parent ca5bc790f5
commit a304aae95c
6 changed files with 75 additions and 70 deletions

View File

@ -2529,7 +2529,6 @@
"name": "Embedded Map", "name": "Embedded Map",
"icon": "Location", "icon": "Location",
"styles": ["size"], "styles": ["size"],
"editable": true,
"draggable": false, "draggable": false,
"illegalChildren": ["section"], "illegalChildren": ["section"],
"settings": [ "settings": [
@ -3631,7 +3630,6 @@
"name": "Markdown Viewer", "name": "Markdown Viewer",
"icon": "TaskList", "icon": "TaskList",
"styles": ["size"], "styles": ["size"],
"editable": true,
"settings": [ "settings": [
{ {
"type": "text", "type": "text",

View File

@ -22,41 +22,44 @@
$: componentText = getComponentText(text, $builderStore, $component) $: componentText = getComponentText(text, $builderStore, $component)
const getComponentText = (text, builderState, componentState) => { const getComponentText = (text, builderState, componentState) => {
if (!builderState.inBuilder || componentState.editing) { if (componentState.editing) {
return text || " " return text || " "
} }
return text || componentState.name || "Placeholder text" return text || componentState.name || "Placeholder text"
} }
const updateText = e => { const updateText = e => {
builderStore.actions.updateProp("text", e.target.textContent.trim()) builderStore.actions.updateProp("text", e.target.textContent)
} }
</script> </script>
<button {#key $component.editing}
class={`spectrum-Button spectrum-Button--size${size} spectrum-Button--${type}`} <button
class:spectrum-Button--quiet={quiet} class={`spectrum-Button spectrum-Button--size${size} spectrum-Button--${type}`}
{disabled} class:spectrum-Button--quiet={quiet}
use:styleable={$component.styles} class:editing={$component.editing}
on:click={onClick} {disabled}
contenteditable={$component.editing && !icon} use:styleable={$component.styles}
on:blur={$component.editing ? updateText : null} on:click={onClick}
bind:this={node} contenteditable={$component.editing && !icon}
class:active on:blur={$component.editing ? updateText : null}
> bind:this={node}
{#if icon} class:active
<svg >
class:hasText={componentText?.length > 0} {#if icon}
class="spectrum-Icon spectrum-Icon--size{size.toUpperCase()}" <svg
focusable="false" class:hasText={componentText?.length > 0}
aria-hidden="true" class="spectrum-Icon spectrum-Icon--size{size.toUpperCase()}"
aria-label={icon} focusable="false"
> aria-hidden="true"
<use xlink:href="#spectrum-icon-18-{icon}" /> aria-label={icon}
</svg> >
{/if} <use xlink:href="#spectrum-icon-18-{icon}" />
{componentText} </svg>
</button> {/if}
{componentText}
</button>
{/key}
<style> <style>
button { button {

View File

@ -47,24 +47,25 @@
// Convert contenteditable HTML to text and save // Convert contenteditable HTML to text and save
const updateText = e => { const updateText = e => {
const sanitized = e.target.innerHTML.replace(/<br>/gi, "\n").trim() builderStore.actions.updateProp("text", e.target.textContent)
builderStore.actions.updateProp("text", sanitized)
} }
</script> </script>
<h1 {#key $component.editing}
bind:this={node} <h1
contenteditable={$component.editing} bind:this={node}
use:styleable={styles} contenteditable={$component.editing}
class:placeholder use:styleable={styles}
class:bold class:placeholder
class:italic class:bold
class:underline class:italic
class="spectrum-Heading {sizeClass} {alignClass}" class:underline
on:blur={$component.editing ? updateText : null} class="spectrum-Heading {sizeClass} {alignClass}"
> on:blur={$component.editing ? updateText : null}
{componentText} >
</h1> {componentText}
</h1>
{/key}
<style> <style>
h1 { h1 {

View File

@ -61,7 +61,7 @@
} }
const updateText = e => { const updateText = e => {
builderStore.actions.updateProp("text", e.target.textContent.trim()) builderStore.actions.updateProp("text", e.target.textContent)
} }
</script> </script>

View File

@ -46,24 +46,25 @@
// Convert contenteditable HTML to text and save // Convert contenteditable HTML to text and save
const updateText = e => { const updateText = e => {
const sanitized = e.target.innerHTML.replace(/<br>/gi, "\n").trim() builderStore.actions.updateProp("text", e.target.textContent)
builderStore.actions.updateProp("text", sanitized)
} }
</script> </script>
<p {#key $component.editing}
bind:this={node} <p
contenteditable={$component.editing} bind:this={node}
use:styleable={styles} contenteditable={$component.editing}
class:placeholder use:styleable={styles}
class:bold class:placeholder
class:italic class:bold
class:underline class:italic
class="spectrum-Body {sizeClass} {alignClass}" class:underline
on:blur={$component.editing ? updateText : null} class="spectrum-Body {sizeClass} {alignClass}"
> on:blur={$component.editing ? updateText : null}
{componentText} >
</p> {componentText}
</p>
{/key}
<style> <style>
p { p {

View File

@ -50,22 +50,24 @@
$: labelClass = labelPos === "above" ? "" : `spectrum-FieldLabel--${labelPos}` $: labelClass = labelPos === "above" ? "" : `spectrum-FieldLabel--${labelPos}`
const updateLabel = e => { const updateLabel = e => {
builderStore.actions.updateProp("label", e.target.textContent.trim()) builderStore.actions.updateProp("label", e.target.textContent)
} }
</script> </script>
<FieldGroupFallback> <FieldGroupFallback>
<div class="spectrum-Form-item" use:styleable={$component.styles}> <div class="spectrum-Form-item" use:styleable={$component.styles}>
<label {#key $component.editing}
bind:this={labelNode} <label
contenteditable={$component.editing} bind:this={labelNode}
on:blur={$component.editing ? updateLabel : null} contenteditable={$component.editing}
class:hidden={!label} on:blur={$component.editing ? updateLabel : null}
for={fieldState?.fieldId} class:hidden={!label}
class={`spectrum-FieldLabel spectrum-FieldLabel--sizeM spectrum-Form-itemLabel ${labelClass}`} for={fieldState?.fieldId}
> class={`spectrum-FieldLabel spectrum-FieldLabel--sizeM spectrum-Form-itemLabel ${labelClass}`}
{label || " "} >
</label> {label || " "}
</label>
{/key}
<div class="spectrum-Form-itemField"> <div class="spectrum-Form-itemField">
{#if !formContext} {#if !formContext}
<Placeholder text="Form components need to be wrapped in a form" /> <Placeholder text="Form components need to be wrapped in a form" />