From 237edc6ea616c9171611e632e146872a245bdb0e Mon Sep 17 00:00:00 2001 From: Jake Donham Date: Mon, 9 Aug 2021 19:59:59 -0700 Subject: [PATCH] fix decorate bug (#4277) without adding extra layers of render tree (#4421) * fix #4394 without adding extra layers of render tree * oops unused import * Add changeset Co-authored-by: Dylan Schiemann --- .changeset/real-jobs-bow.md | 6 + .../slate-react/src/components/editable.tsx | 20 +-- .../slate-react/src/hooks/use-children.tsx | 114 ++++++------------ 3 files changed, 56 insertions(+), 84 deletions(-) create mode 100644 .changeset/real-jobs-bow.md diff --git a/.changeset/real-jobs-bow.md b/.changeset/real-jobs-bow.md new file mode 100644 index 000000000..b0d7b7bef --- /dev/null +++ b/.changeset/real-jobs-bow.md @@ -0,0 +1,6 @@ +--- +'slate-react': patch +'slate': patch +--- + +fix decorate bug (#4277) without adding extra layers of render tree diff --git a/packages/slate-react/src/components/editable.tsx b/packages/slate-react/src/components/editable.tsx index 78cc5bf9b..ffd689955 100644 --- a/packages/slate-react/src/components/editable.tsx +++ b/packages/slate-react/src/components/editable.tsx @@ -48,6 +48,10 @@ import { EDITOR_TO_WINDOW, } from '../utils/weak-maps' +const Children = (props: Parameters[0]) => ( + {useChildren(props)} +) + /** * `RenderElementProps` are passed to the `renderElement` handler. */ @@ -1120,14 +1124,14 @@ export const Editable = (props: EditableProps) => { [readOnly, attributes.onPaste] )} > - {useChildren({ - decorations, - node: editor, - renderElement, - renderPlaceholder, - renderLeaf, - selection: editor.selection, - })} + diff --git a/packages/slate-react/src/hooks/use-children.tsx b/packages/slate-react/src/hooks/use-children.tsx index f9c51981c..812c1ae0a 100644 --- a/packages/slate-react/src/hooks/use-children.tsx +++ b/packages/slate-react/src/hooks/use-children.tsx @@ -1,5 +1,5 @@ import React from 'react' -import { Editor, Range, Element, Path, Ancestor, Descendant } from 'slate' +import { Editor, Range, Element, NodeEntry, Ancestor, Descendant } from 'slate' import ElementComponent from '../components/element' import TextComponent from '../components/text' @@ -17,67 +17,6 @@ import { * Children. */ -const Child = (props: { - decorations: Range[] - parent: Ancestor - path: Path - child: Descendant - isLast: boolean - renderElement?: (props: RenderElementProps) => JSX.Element - renderPlaceholder: (props: RenderPlaceholderProps) => JSX.Element - renderLeaf?: (props: RenderLeafProps) => JSX.Element - selection: Range | null -}) => { - const { - decorations, - parent, - path, - child, - isLast, - renderElement, - renderPlaceholder, - renderLeaf, - selection, - } = props - const decorate = useDecorate() - const editor = useSlateStatic() - - const range = Editor.range(editor, path) - const sel = selection && Range.intersection(range, selection) - const ds = decorate([child, path]) - - for (const dec of decorations) { - const d = Range.intersection(dec, range) - - if (d) { - ds.push(d) - } - } - - if (Element.isElement(child)) { - return ( - - ) - } - return ( - - ) -} - const useChildren = (props: { decorations: Range[] node: Ancestor @@ -94,6 +33,7 @@ const useChildren = (props: { renderLeaf, selection, } = props + const decorate = useDecorate() const editor = useSlateStatic() const path = ReactEditor.findPath(editor, node) const children = [] @@ -106,21 +46,43 @@ const useChildren = (props: { const p = path.concat(i) const n = node.children[i] as Descendant const key = ReactEditor.findKey(editor, n) + const range = Editor.range(editor, p) + const sel = selection && Range.intersection(range, selection) + const ds = decorate([n, p]) - children.push( - - ) + for (const dec of decorations) { + const d = Range.intersection(dec, range) + + if (d) { + ds.push(d) + } + } + + if (Element.isElement(n)) { + children.push( + + ) + } else { + children.push( + + ) + } NODE_TO_INDEX.set(n, i) NODE_TO_PARENT.set(n, node)