1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-08-28 17:39:57 +02:00

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 <dylan@dojotoolkit.org>
This commit is contained in:
Jake Donham
2021-08-09 19:59:59 -07:00
committed by GitHub
parent 748bf75005
commit 237edc6ea6
3 changed files with 56 additions and 84 deletions

View File

@@ -0,0 +1,6 @@
---
'slate-react': patch
'slate': patch
---
fix decorate bug (#4277) without adding extra layers of render tree

View File

@@ -48,6 +48,10 @@ import {
EDITOR_TO_WINDOW, EDITOR_TO_WINDOW,
} from '../utils/weak-maps' } from '../utils/weak-maps'
const Children = (props: Parameters<typeof useChildren>[0]) => (
<React.Fragment>{useChildren(props)}</React.Fragment>
)
/** /**
* `RenderElementProps` are passed to the `renderElement` handler. * `RenderElementProps` are passed to the `renderElement` handler.
*/ */
@@ -1120,14 +1124,14 @@ export const Editable = (props: EditableProps) => {
[readOnly, attributes.onPaste] [readOnly, attributes.onPaste]
)} )}
> >
{useChildren({ <Children
decorations, decorations={decorations}
node: editor, node={editor}
renderElement, renderElement={renderElement}
renderPlaceholder, renderPlaceholder={renderPlaceholder}
renderLeaf, renderLeaf={renderLeaf}
selection: editor.selection, selection={editor.selection}
})} />
</Component> </Component>
</DecorateContext.Provider> </DecorateContext.Provider>
</ReadOnlyContext.Provider> </ReadOnlyContext.Provider>

View File

@@ -1,5 +1,5 @@
import React from 'react' 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 ElementComponent from '../components/element'
import TextComponent from '../components/text' import TextComponent from '../components/text'
@@ -17,67 +17,6 @@ import {
* Children. * 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: { const useChildren = (props: {
decorations: Range[] decorations: Range[]
node: Ancestor node: Ancestor
@@ -94,6 +33,7 @@ const useChildren = (props: {
renderLeaf, renderLeaf,
selection, selection,
} = props } = props
const decorate = useDecorate()
const editor = useSlateStatic() const editor = useSlateStatic()
const path = ReactEditor.findPath(editor, node) const path = ReactEditor.findPath(editor, node)
const children = [] const children = []
@@ -106,21 +46,43 @@ const useChildren = (props: {
const p = path.concat(i) const p = path.concat(i)
const n = node.children[i] as Descendant const n = node.children[i] as Descendant
const key = ReactEditor.findKey(editor, n) 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)
if (d) {
ds.push(d)
}
}
if (Element.isElement(n)) {
children.push( children.push(
<Child <ElementComponent
decorations={decorations} decorations={ds}
parent={node} element={n}
path={p}
child={n}
key={key.id} key={key.id}
isLast={isLeafBlock && i === node.children.length - 1}
renderElement={renderElement} renderElement={renderElement}
renderPlaceholder={renderPlaceholder} renderPlaceholder={renderPlaceholder}
renderLeaf={renderLeaf} renderLeaf={renderLeaf}
selection={selection} 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_INDEX.set(n, i)
NODE_TO_PARENT.set(n, node) NODE_TO_PARENT.set(n, node)