1
0
Fork 0
mirror of synced 2024-09-08 13:41:09 +12:00

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 f8f8d3c9cb
commit 444072ecac
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,20 +22,22 @@
$: 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}
<button
class={`spectrum-Button spectrum-Button--size${size} spectrum-Button--${type}`} class={`spectrum-Button spectrum-Button--size${size} spectrum-Button--${type}`}
class:spectrum-Button--quiet={quiet} class:spectrum-Button--quiet={quiet}
class:editing={$component.editing}
{disabled} {disabled}
use:styleable={$component.styles} use:styleable={$component.styles}
on:click={onClick} on:click={onClick}
@ -43,7 +45,7 @@
on:blur={$component.editing ? updateText : null} on:blur={$component.editing ? updateText : null}
bind:this={node} bind:this={node}
class:active class:active
> >
{#if icon} {#if icon}
<svg <svg
class:hasText={componentText?.length > 0} class:hasText={componentText?.length > 0}
@ -56,7 +58,8 @@
</svg> </svg>
{/if} {/if}
{componentText} {componentText}
</button> </button>
{/key}
<style> <style>
button { button {

View file

@ -47,12 +47,12 @@
// 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}
<h1
bind:this={node} bind:this={node}
contenteditable={$component.editing} contenteditable={$component.editing}
use:styleable={styles} use:styleable={styles}
@ -62,9 +62,10 @@
class:underline class:underline
class="spectrum-Heading {sizeClass} {alignClass}" class="spectrum-Heading {sizeClass} {alignClass}"
on:blur={$component.editing ? updateText : null} on:blur={$component.editing ? updateText : null}
> >
{componentText} {componentText}
</h1> </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,12 +46,12 @@
// 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}
<p
bind:this={node} bind:this={node}
contenteditable={$component.editing} contenteditable={$component.editing}
use:styleable={styles} use:styleable={styles}
@ -61,9 +61,10 @@
class:underline class:underline
class="spectrum-Body {sizeClass} {alignClass}" class="spectrum-Body {sizeClass} {alignClass}"
on:blur={$component.editing ? updateText : null} on:blur={$component.editing ? updateText : null}
> >
{componentText} {componentText}
</p> </p>
{/key}
<style> <style>
p { p {

View file

@ -50,12 +50,13 @@
$: 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}>
{#key $component.editing}
<label <label
bind:this={labelNode} bind:this={labelNode}
contenteditable={$component.editing} contenteditable={$component.editing}
@ -66,6 +67,7 @@
> >
{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" />