(props: {
isLast: boolean
parent: Element
text: SlateText
renderPlaceholder?: PlaceholderRender
})
| 14 | * Text. |
| 15 | */ |
| 16 | const Text = (props: { |
| 17 | isLast: boolean |
| 18 | parent: Element |
| 19 | text: SlateText |
| 20 | renderPlaceholder?: PlaceholderRender |
| 21 | }) => { |
| 22 | const { isLast, parent, text, renderPlaceholder } = props |
| 23 | const editor = useEditableStatic() |
| 24 | const ref = React.useRef<HTMLSpanElement>(null) |
| 25 | const key = Editable.findKey(editor, text) |
| 26 | const path = Editable.findPath(editor, text) |
| 27 | const decorates = useTextDecorations(text, path).map((d, index) => ({ |
| 28 | ...d, |
| 29 | key: `__decorate__${index}`, |
| 30 | })) |
| 31 | |
| 32 | const ranges = decorates |
| 33 | .map(({ ranges, key }) => ranges.map(range => ({ ...range, [key]: true }))) |
| 34 | .flat() |
| 35 | const leaves = SlateText.decorations(text, ranges) |
| 36 | |
| 37 | const decorateKeys = decorates.map(d => d.key) |
| 38 | const children = [] |
| 39 | for (let i = 0; i < leaves.length; i++) { |
| 40 | const leaf = leaves[i] |
| 41 | let content = ( |
| 42 | <Leaf |
| 43 | renderPlaceholder={renderPlaceholder} |
| 44 | isLast={isLast && i === leaves.length - 1} |
| 45 | key={`${key.id}-${i}`} |
| 46 | text={text} |
| 47 | leaf={leaf} |
| 48 | parent={parent} |
| 49 | /> |
| 50 | ) |
| 51 | for (const key of decorateKeys) { |
| 52 | if (key in leaf) { |
| 53 | const dec = decorates[decorateKeys.indexOf(key)].decorate.renderText({ |
| 54 | node: text, |
| 55 | path, |
| 56 | children: content, |
| 57 | }) |
| 58 | content = React.cloneElement(dec, { key }) |
| 59 | } |
| 60 | } |
| 61 | children.push(content) |
| 62 | } |
| 63 | // Update element-related weak maps with the DOM element ref. |
| 64 | useIsomorphicLayoutEffect(() => { |
| 65 | const KEY_TO_ELEMENT = EDITOR_TO_KEY_TO_ELEMENT.get(editor) |
| 66 | if (ref.current) { |
| 67 | KEY_TO_ELEMENT?.set(key, ref.current) |
| 68 | NODE_TO_ELEMENT.set(text, ref.current) |
| 69 | ELEMENT_TO_NODE.set(ref.current, text) |
| 70 | } else { |
| 71 | KEY_TO_ELEMENT?.delete(key) |
| 72 | NODE_TO_ELEMENT.delete(text) |
| 73 | } |
nothing calls this directly
no test coverage detected