Skip to content

Commit

Permalink
fix decorate bug (#4277) without adding extra layers of render tree (#…
Browse files Browse the repository at this point in the history
…4421)

* fix #4394 without adding extra layers of render tree

* oops unused import

* Add changeset

Co-authored-by: Dylan Schiemann <dylan@dojotoolkit.org>
  • Loading branch information
jaked and dylans authored Aug 10, 2021
1 parent 748bf75 commit 237edc6
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 84 deletions.
6 changes: 6 additions & 0 deletions .changeset/real-jobs-bow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'slate-react': patch
'slate': patch
---

fix decorate bug (#4277) without adding extra layers of render tree
20 changes: 12 additions & 8 deletions packages/slate-react/src/components/editable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ import {
EDITOR_TO_WINDOW,
} from '../utils/weak-maps'

const Children = (props: Parameters<typeof useChildren>[0]) => (
<React.Fragment>{useChildren(props)}</React.Fragment>
)

/**
* `RenderElementProps` are passed to the `renderElement` handler.
*/
Expand Down Expand Up @@ -1120,14 +1124,14 @@ export const Editable = (props: EditableProps) => {
[readOnly, attributes.onPaste]
)}
>
{useChildren({
decorations,
node: editor,
renderElement,
renderPlaceholder,
renderLeaf,
selection: editor.selection,
})}
<Children
decorations={decorations}
node={editor}
renderElement={renderElement}
renderPlaceholder={renderPlaceholder}
renderLeaf={renderLeaf}
selection={editor.selection}
/>
</Component>
</DecorateContext.Provider>
</ReadOnlyContext.Provider>
Expand Down
114 changes: 38 additions & 76 deletions packages/slate-react/src/hooks/use-children.tsx
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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 (
<ElementComponent
decorations={ds}
element={child}
renderElement={renderElement}
renderPlaceholder={renderPlaceholder}
renderLeaf={renderLeaf}
selection={sel}
/>
)
}
return (
<TextComponent
decorations={ds}
isLast={isLast}
parent={parent}
renderPlaceholder={renderPlaceholder}
renderLeaf={renderLeaf}
text={child}
/>
)
}

const useChildren = (props: {
decorations: Range[]
node: Ancestor
Expand All @@ -94,6 +33,7 @@ const useChildren = (props: {
renderLeaf,
selection,
} = props
const decorate = useDecorate()
const editor = useSlateStatic()
const path = ReactEditor.findPath(editor, node)
const children = []
Expand All @@ -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])

for (const dec of decorations) {
const d = Range.intersection(dec, range)

children.push(
<Child
decorations={decorations}
parent={node}
path={p}
child={n}
key={key.id}
isLast={isLeafBlock && i === node.children.length - 1}
renderElement={renderElement}
renderPlaceholder={renderPlaceholder}
renderLeaf={renderLeaf}
selection={selection}
/>
)
if (d) {
ds.push(d)
}
}

if (Element.isElement(n)) {
children.push(
<ElementComponent
decorations={ds}
element={n}
key={key.id}
renderElement={renderElement}
renderPlaceholder={renderPlaceholder}
renderLeaf={renderLeaf}
selection={sel}
/>
)
} else {
children.push(
<TextComponent
decorations={ds}
key={key.id}
isLast={isLeafBlock && i === node.children.length - 1}
parent={node}
renderPlaceholder={renderPlaceholder}
renderLeaf={renderLeaf}
text={n}
/>
)
}

NODE_TO_INDEX.set(n, i)
NODE_TO_PARENT.set(n, node)
Expand Down

0 comments on commit 237edc6

Please sign in to comment.