(yRoot: Y.XmlText, editorRoot: Node, path: Path)
| 18 | } |
| 19 | |
| 20 | export function getYTarget(yRoot: Y.XmlText, editorRoot: Node, path: Path): YTarget { |
| 21 | if (path.length === 0) { |
| 22 | throw new Error('Path has to a have a length >= 1') |
| 23 | } |
| 24 | |
| 25 | if (Text.isText(editorRoot)) { |
| 26 | throw new Error('Cannot descent into editor text') |
| 27 | } |
| 28 | |
| 29 | const [pathOffset, ...childPath] = path |
| 30 | |
| 31 | const yOffset = editorPathOffsetToYOffset(editorRoot, pathOffset) |
| 32 | const targetNode = editorRoot.children[pathOffset] |
| 33 | |
| 34 | const delta = yTextToInsertDelta(yRoot) |
| 35 | const targetLength = getEditorNodeYLength(targetNode) |
| 36 | |
| 37 | const targetDelta = sliceInsertDelta(delta, yOffset, targetLength) |
| 38 | if (targetDelta.length > 1) { |
| 39 | throw new Error("Path doesn't match yText, yTarget spans multiple nodes") |
| 40 | } |
| 41 | |
| 42 | const yTarget = targetDelta[0]?.insert |
| 43 | if (childPath.length > 0) { |
| 44 | if (!(yTarget instanceof Y.XmlText)) { |
| 45 | throw new Error("Path doesn't match yText, cannot descent into non-yText") |
| 46 | } |
| 47 | |
| 48 | return getYTarget(yTarget, targetNode, childPath) |
| 49 | } |
| 50 | |
| 51 | return { |
| 52 | yParent: yRoot, |
| 53 | textRange: { start: yOffset, end: yOffset + targetLength }, |
| 54 | yTarget: yTarget instanceof Y.XmlText ? yTarget : undefined, |
| 55 | editorParent: editorRoot, |
| 56 | editorTarget: targetNode, |
| 57 | targetDelta, |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | export function yOffsetToEditorOffsets( |
| 62 | parent: Element, |
no test coverage detected
searching dependent graphs…