File size: 5,560 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
import React, { useCallback, useRef } from 'react'
import { Ancestor, Editor, Element, DecoratedRange, Text } from 'slate'
import { Key, isElementDecorationsEqual } from 'slate-dom'
import {
RenderChunkProps,
RenderElementProps,
RenderLeafProps,
RenderPlaceholderProps,
RenderTextProps,
} from '../components/editable'
import ElementComponent from '../components/element'
import TextComponent from '../components/text'
import { ReactEditor } from '../plugin/react-editor'
import {
IS_NODE_MAP_DIRTY,
NODE_TO_INDEX,
NODE_TO_PARENT,
splitDecorationsByChild,
} from 'slate-dom'
import { useSlateStatic } from './use-slate-static'
import { getChunkTreeForNode } from '../chunking'
import ChunkTree from '../components/chunk-tree'
import { ElementContext } from './use-element'
/**
* Children.
*/
const useChildren = (props: {
decorations: DecoratedRange[]
node: Ancestor
renderElement?: (props: RenderElementProps) => JSX.Element
renderChunk?: (props: RenderChunkProps) => JSX.Element
renderPlaceholder: (props: RenderPlaceholderProps) => JSX.Element
renderText?: (props: RenderTextProps) => JSX.Element
renderLeaf?: (props: RenderLeafProps) => JSX.Element
}) => {
const {
decorations,
node,
renderElement,
renderChunk,
renderPlaceholder,
renderText,
renderLeaf,
} = props
const editor = useSlateStatic()
IS_NODE_MAP_DIRTY.set(editor as ReactEditor, false)
const isEditor = Editor.isEditor(node)
const isBlock = !isEditor && Element.isElement(node) && !editor.isInline(node)
const isLeafBlock = isBlock && Editor.hasInlines(editor, node)
const chunkSize = isLeafBlock ? null : editor.getChunkSize(node)
const chunking = !!chunkSize
const { decorationsByChild, childrenToRedecorate } = useDecorationsByChild(
editor,
node,
decorations
)
// Update the index and parent of each child.
// PERF: If chunking is enabled, this is done while traversing the chunk tree
// instead to eliminate unnecessary weak map operations.
if (!chunking) {
node.children.forEach((n, i) => {
NODE_TO_INDEX.set(n, i)
NODE_TO_PARENT.set(n, node)
})
}
const renderElementComponent = useCallback(
(n: Element, i: number, cachedKey?: Key) => {
const key = cachedKey ?? ReactEditor.findKey(editor, n)
return (
<ElementContext.Provider key={`provider-${key.id}`} value={n}>
<ElementComponent
decorations={decorationsByChild[i]}
element={n}
key={key.id}
renderElement={renderElement}
renderChunk={renderChunk}
renderPlaceholder={renderPlaceholder}
renderLeaf={renderLeaf}
renderText={renderText}
/>
</ElementContext.Provider>
)
},
[
editor,
decorationsByChild,
renderElement,
renderChunk,
renderPlaceholder,
renderLeaf,
renderText,
]
)
const renderTextComponent = (n: Text, i: number) => {
const key = ReactEditor.findKey(editor, n)
return (
<TextComponent
decorations={decorationsByChild[i]}
key={key.id}
isLast={i === node.children.length - 1}
parent={node}
renderPlaceholder={renderPlaceholder}
renderLeaf={renderLeaf}
renderText={renderText}
text={n}
/>
)
}
if (!chunking) {
return node.children.map((n, i) =>
Text.isText(n) ? renderTextComponent(n, i) : renderElementComponent(n, i)
)
}
const chunkTree = getChunkTreeForNode(editor, node, {
reconcile: {
chunkSize,
rerenderChildren: childrenToRedecorate,
onInsert: (n, i) => {
NODE_TO_INDEX.set(n, i)
NODE_TO_PARENT.set(n, node)
},
onUpdate: (n, i) => {
NODE_TO_INDEX.set(n, i)
NODE_TO_PARENT.set(n, node)
},
onIndexChange: (n, i) => {
NODE_TO_INDEX.set(n, i)
},
},
})
return (
<ChunkTree
root={chunkTree}
ancestor={chunkTree}
renderElement={renderElementComponent}
renderChunk={renderChunk}
/>
)
}
const useDecorationsByChild = (
editor: Editor,
node: Ancestor,
decorations: DecoratedRange[]
) => {
const decorationsByChild = splitDecorationsByChild(editor, node, decorations)
// The value we return is a mutable array of `DecoratedRange[]` arrays. This
// lets us avoid passing an immutable array of decorations for each child into
// `ChunkTree` using props. Each `DecoratedRange[]` is only updated if the
// decorations at that index have changed, which speeds up the equality check
// for the `decorations` prop in the memoized `Element` and `Text` components.
const mutableDecorationsByChild = useRef(decorationsByChild).current
// Track the list of child indices whose decorations have changed, so that we
// can tell the chunk tree to re-render these children.
const childrenToRedecorate: number[] = []
// Resize the mutable array to match the latest result
mutableDecorationsByChild.length = decorationsByChild.length
for (let i = 0; i < decorationsByChild.length; i++) {
const decorations = decorationsByChild[i]
const previousDecorations: DecoratedRange[] | null =
mutableDecorationsByChild[i] ?? null
if (!isElementDecorationsEqual(previousDecorations, decorations)) {
mutableDecorationsByChild[i] = decorations
childrenToRedecorate.push(i)
}
}
return { decorationsByChild: mutableDecorationsByChild, childrenToRedecorate }
}
export default useChildren
|