(sharedRoot: Y.XmlText, editorRoot: Node, yText: Y.XmlText)
| 97 | } |
| 98 | |
| 99 | export function getEditorPath(sharedRoot: Y.XmlText, editorRoot: Node, yText: Y.XmlText): Path { |
| 100 | const yNodePath = [yText] |
| 101 | while (yNodePath[0] !== sharedRoot) { |
| 102 | const { parent: yParent } = yNodePath[0] |
| 103 | |
| 104 | if (!yParent) { |
| 105 | throw new Error("yText isn't a descendant of root element") |
| 106 | } |
| 107 | |
| 108 | if (!(yParent instanceof Y.XmlText)) { |
| 109 | throw new Error('Unexpected y parent type') |
| 110 | } |
| 111 | |
| 112 | yNodePath.unshift(yParent) |
| 113 | } |
| 114 | |
| 115 | if (yNodePath.length < 2) { |
| 116 | return [] |
| 117 | } |
| 118 | |
| 119 | let editorParent = editorRoot |
| 120 | return yNodePath.reduce<Path>((path, yParent, idx) => { |
| 121 | const yChild = yNodePath[idx + 1] |
| 122 | if (!yChild) { |
| 123 | return path |
| 124 | } |
| 125 | |
| 126 | let yOffset = 0 |
| 127 | const currentDelta = yTextToInsertDelta(yParent) |
| 128 | for (const element of currentDelta) { |
| 129 | if (element.insert === yChild) { |
| 130 | break |
| 131 | } |
| 132 | |
| 133 | yOffset += typeof element.insert === 'string' ? element.insert.length : 1 |
| 134 | } |
| 135 | |
| 136 | if (Text.isText(editorParent)) { |
| 137 | throw new Error('Cannot descent into editor text') |
| 138 | } |
| 139 | |
| 140 | const [pathOffset] = yOffsetToEditorOffsets(editorParent, yOffset) |
| 141 | editorParent = editorParent.children[pathOffset] |
| 142 | return path.concat(pathOffset) |
| 143 | }, []) |
| 144 | } |
no test coverage detected
searching dependent graphs…