Skip to content

Commit a3e9f50

Browse files
authored
Merge pull request #733 from contember/fix/editor-initial-state
editor: assign "key" to initial element
2 parents 105f669 + e788630 commit a3e9f50

File tree

3 files changed

+5
-3
lines changed

3 files changed

+5
-3
lines changed

packages/react-slate-editor/src/components/BlockEditor.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { Fragment, ReactNode, useEffect, useState } from 'react'
44
import { Slate, useSlate } from 'slate-react'
55
import { createEditor, EditorPlugin, paragraphElementType } from '@contember/react-slate-editor-base'
66
import { Descendant, insertNodes, Node, removeNodes, withoutNormalizing } from 'slate'
7+
import { createElementKey } from '../internal/helpers/createElementKey'
78

89
export interface BlockEditorProps {
910
field: SugaredRelativeSingleField['field']
@@ -37,7 +38,7 @@ export const BlockEditor = Component<BlockEditorProps>(
3738
contentField.updateValue(contentJson)
3839
})
3940

40-
const [emptyValue] = useState(() => [editor.createDefaultElement([{ text: '' }])])
41+
const [emptyValue] = useState(() => [{ ...editor.createDefaultElement([{ text: '' }]), key: createElementKey() }])
4142

4243
const nodes = contentField.value && typeof contentField.value === 'object' && 'children' in contentField.value
4344
? (contentField.value.children as Descendant[])
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const createElementKey = () => Math.random().toString(36).slice(6)

packages/react-slate-editor/src/plugins/behaviour/sortable/withSortable.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import { EditorElement, EditorPlugin } from '@contember/react-slate-editor-base'
22
import { ReactEditor } from 'slate-react'
33
import { ReactNode } from 'react'
44
import { Element, Transforms } from 'slate'
5+
import { createElementKey } from '../../../internal/helpers/createElementKey'
56

6-
const createKey = () => Math.random().toString(36).slice(6)
77

88
export const withSortable = ({ render: Sortable }: {
99
render: (props: { element: EditorElement, children: ReactNode }) => ReactNode
@@ -22,7 +22,7 @@ export const withSortable = ({ render: Sortable }: {
2222
const [node, path] = entry
2323
if (Element.isElement(node) && path.length === 1 && (!node.key || editor.children.findIndex(it => it !== node && it.key === node.key) !== -1)) {
2424
Transforms.setNodes(editor, {
25-
key: createKey(),
25+
key: createElementKey(),
2626
}, { at: path })
2727
}
2828
normalizeNode(entry)

0 commit comments

Comments
 (0)