| 20 | |
| 21 | leaf: Text |
| 22 | }> = props => { |
| 23 | const { isLast, parent, text, leaf } = props |
| 24 | const editor = useEditableStatic() |
| 25 | const path = Editable.findPath(editor, text) |
| 26 | const parentPath = Path.parent(path) |
| 27 | |
| 28 | // COMPAT: Render text inside void nodes with a zero-width space. |
| 29 | // So the node can contain selection but the text is not visible. |
| 30 | if (editor.isVoid(parent)) { |
| 31 | return <ZeroWidthString length={Node.string(parent).length} /> |
| 32 | } |
| 33 | |
| 34 | if (CompositionText.isCompositionText(text)) { |
| 35 | const { offset, text: compositionText } = text.composition |
| 36 | const content = text.text |
| 37 | const left = content.substring(0, offset) |
| 38 | const right = content.substring(offset) |
| 39 | return ( |
| 40 | <> |
| 41 | {left && <TextString text={left} />} |
| 42 | <CompositionString text={compositionText} /> |
| 43 | {right && <TextString text={right} />} |
| 44 | </> |
| 45 | ) |
| 46 | } |
| 47 | // COMPAT: If this is the last text node in an empty block, render a zero- |
| 48 | // width space that will convert into a line break when copying and pasting |
| 49 | // to support expected plain text. |
| 50 | if ( |
| 51 | leaf.text === '' && |
| 52 | parent.children[parent.children.length - 1] === text && |
| 53 | !editor.isInline(parent) && |
| 54 | Editor.string(editor, parentPath) === '' |
| 55 | ) { |
| 56 | return <ZeroWidthString isLineBreak /> |
| 57 | } |
| 58 | |
| 59 | // COMPAT: If the text is empty, it's because it's on the edge of an inline |
| 60 | // node, so we render a zero-width space so that the selection can be |
| 61 | // inserted next to it still. |
| 62 | if (leaf.text === '') { |
| 63 | return <ZeroWidthString /> |
| 64 | } |
| 65 | |
| 66 | // COMPAT: Browsers will collapse trailing new lines at the end of blocks, |
| 67 | // so we need to add an extra trailing new lines to prevent that. |
| 68 | if (isLast && leaf.text.slice(-1) === '\n') { |
| 69 | return <TextString isTrailing text={leaf.text} /> |
| 70 | } |
| 71 | return <TextString text={leaf.text} /> |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Leaf strings with text in them. |
no test coverage detected