Skip to content

Commit

Permalink
refactor(portable-text-editor): memo editor instance spesific placeho…
Browse files Browse the repository at this point in the history
…lder block

This will enable tests to better predict keys and it should not be a problem to reuse key if the value is deleted and reintroduced.
  • Loading branch information
skogsmaskin committed Sep 8, 2021
1 parent 9ae43a8 commit 25585b9
Showing 1 changed file with 19 additions and 16 deletions.
35 changes: 19 additions & 16 deletions packages/@sanity/portable-text-editor/src/editor/Editable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,20 +77,23 @@ export const PortableTextEditable = (props: Props) => {
readOnly,
} = portableTextEditor

const createPlaceHolderBlock = () => ({
_type: portableTextFeatures.types.block.name,
_key: keyGenerator(),
style: 'normal',
markDefs: [],
children: [
{
_type: 'span',
_key: keyGenerator(),
text: '',
marks: [],
},
],
})
const placeHolderBlock = useMemo(
() => ({
_type: portableTextFeatures.types.block.name,
_key: keyGenerator(),
style: 'normal',
markDefs: [],
children: [
{
_type: 'span',
_key: keyGenerator(),
text: '',
marks: [],
},
],
}),
[portableTextFeatures.types.block.name, keyGenerator]
)

// React/UI-spesific plugins
const withInsertData = useMemo(
Expand Down Expand Up @@ -134,7 +137,7 @@ export const PortableTextEditable = (props: Props) => {
const [stateValue, setStateValue] = useState(
// Default value
toSlateValue(
getValueOrIntitialValue(value, [createPlaceHolderBlock()]),
getValueOrIntitialValue(value, [placeHolderBlock]),
portableTextFeatures.types.block.name,
KEY_TO_SLATE_ELEMENT.get(editor)
)
Expand Down Expand Up @@ -474,7 +477,7 @@ export const PortableTextEditable = (props: Props) => {
onChange={handleChange}
editor={editor}
selection={selection}
value={getValueOrIntitialValue(stateValue, [createPlaceHolderBlock()])}
value={getValueOrIntitialValue(stateValue, [placeHolderBlock])}
>
<SlateEditable
autoFocus={false}
Expand Down

0 comments on commit 25585b9

Please sign in to comment.