MCPcopy Create free account
hub / github.com/editablejs/editable / yOffsetToEditorOffsets

Function yOffsetToEditorOffsets

packages/yjs-transform/src/location.ts:61–97  ·  view source on GitHub ↗
(
  parent: Element,
  yOffset: number,
  opts: { assoc?: number; insert?: boolean } = {},
)

Source from the content-addressed store, hash-verified

59}
60
61export 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
99export function getEditorPath(sharedRoot: Y.XmlText, editorRoot: Node, yText: Y.XmlText): Path {
100 const yNodePath = [yText]

Callers 3

applyDeltaFunction · 0.90
getEditorPathFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…