(
parent: Element,
yOffset: number,
opts: { assoc?: number; insert?: boolean } = {},
)
| 59 | } |
| 60 | |
| 61 | export function yOffsetToEditorOffsets( |
| 62 | parent: Element, |
| 63 | yOffset: number, |
| 64 | opts: { assoc?: number; insert?: boolean } = {}, |
| 65 | ): [number, number] { |
| 66 | const { assoc = 0, insert = false } = opts |
| 67 | |
| 68 | let currentOffset = 0 |
| 69 | let lastNonEmptyPathOffset = 0 |
| 70 | for (let pathOffset = 0; pathOffset < parent.children.length; pathOffset++) { |
| 71 | const child = parent.children[pathOffset] |
| 72 | const nodeLength = Text.isText(child) ? child.text.length : 1 |
| 73 | |
| 74 | if (nodeLength > 0) { |
| 75 | lastNonEmptyPathOffset = pathOffset |
| 76 | } |
| 77 | |
| 78 | const endOffset = currentOffset + nodeLength |
| 79 | if (nodeLength > 0 && (assoc >= 0 ? endOffset > yOffset : endOffset >= yOffset)) { |
| 80 | return [pathOffset, yOffset - currentOffset] |
| 81 | } |
| 82 | |
| 83 | currentOffset += nodeLength |
| 84 | } |
| 85 | |
| 86 | if (yOffset > currentOffset + (insert ? 1 : 0)) { |
| 87 | throw new Error('yOffset out of bounds') |
| 88 | } |
| 89 | |
| 90 | if (insert) { |
| 91 | return [parent.children.length, 0] |
| 92 | } |
| 93 | |
| 94 | const child = parent.children[lastNonEmptyPathOffset] |
| 95 | const textOffset = Text.isText(child) ? child.text.length : 1 |
| 96 | return [lastNonEmptyPathOffset, textOffset] |
| 97 | } |
| 98 | |
| 99 | export function getEditorPath(sharedRoot: Y.XmlText, editorRoot: Node, yText: Y.XmlText): Path { |
| 100 | const yNodePath = [yText] |
no outgoing calls
no test coverage detected
searching dependent graphs…