@daasboard/editor 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (83) hide show
  1. package/LICENSE +5 -0
  2. package/README.md +6 -2
  3. package/dist/camera/CameraInput.d.ts +106 -0
  4. package/dist/camera/CameraInput.js +398 -0
  5. package/dist/clipboard/ObjectClipboard.d.ts +40 -0
  6. package/dist/clipboard/ObjectClipboard.js +243 -0
  7. package/dist/clipboard/PasteCommitError.d.ts +4 -0
  8. package/dist/clipboard/PasteCommitError.js +7 -0
  9. package/dist/clipboard/carrier.d.ts +5 -0
  10. package/dist/clipboard/carrier.js +59 -0
  11. package/dist/clipboard/objects.d.ts +21 -0
  12. package/dist/clipboard/objects.js +362 -0
  13. package/dist/clipboard/placement.d.ts +11 -0
  14. package/dist/clipboard/placement.js +21 -0
  15. package/dist/clipboard/systemImages.d.ts +1 -0
  16. package/dist/clipboard/systemImages.js +24 -0
  17. package/dist/index.d.ts +23 -0
  18. package/dist/index.js +22 -0
  19. package/dist/input/TouchInput.d.ts +159 -0
  20. package/dist/input/TouchInput.js +656 -0
  21. package/dist/input/visiblePart.d.ts +8 -0
  22. package/dist/input/visiblePart.js +15 -0
  23. package/dist/interaction/InteractionLayer.d.ts +83 -0
  24. package/dist/interaction/InteractionLayer.js +520 -0
  25. package/dist/props/PropertyController.d.ts +130 -0
  26. package/dist/props/PropertyController.js +1448 -0
  27. package/dist/props/PropertyRegistry.d.ts +38 -0
  28. package/dist/props/PropertyRegistry.js +21 -0
  29. package/dist/props/builtinProperties.d.ts +23 -0
  30. package/dist/props/builtinProperties.js +350 -0
  31. package/dist/props/defaults.d.ts +6 -0
  32. package/dist/props/defaults.js +29 -0
  33. package/dist/selection/SelectionModel.d.ts +25 -0
  34. package/dist/selection/SelectionModel.js +79 -0
  35. package/dist/text/CanvasEditorView.d.ts +99 -0
  36. package/dist/text/CanvasEditorView.js +308 -0
  37. package/dist/text/InputSink.d.ts +157 -0
  38. package/dist/text/InputSink.js +841 -0
  39. package/dist/text/TextEditSession.d.ts +173 -0
  40. package/dist/text/TextEditSession.js +1328 -0
  41. package/dist/text/clipboard.d.ts +11 -0
  42. package/dist/text/clipboard.js +489 -0
  43. package/dist/text/commands.d.ts +48 -0
  44. package/dist/text/commands.js +329 -0
  45. package/dist/text/contextMirror.d.ts +51 -0
  46. package/dist/text/contextMirror.js +292 -0
  47. package/dist/text/flat.d.ts +35 -0
  48. package/dist/text/flat.js +556 -0
  49. package/dist/text/graphemes.d.ts +16 -0
  50. package/dist/text/graphemes.js +219 -0
  51. package/dist/text/index.d.ts +13 -0
  52. package/dist/text/index.js +13 -0
  53. package/dist/text/inputReducer.d.ts +24 -0
  54. package/dist/text/inputReducer.js +44 -0
  55. package/dist/text/keymap.d.ts +17 -0
  56. package/dist/text/keymap.js +148 -0
  57. package/dist/text/schema.d.ts +12 -0
  58. package/dist/text/schema.js +44 -0
  59. package/dist/text/sessionFocus.d.ts +20 -0
  60. package/dist/text/sessionFocus.js +51 -0
  61. package/dist/text/types.d.ts +114 -0
  62. package/dist/text/types.js +1 -0
  63. package/dist/tools/CreateTool.d.ts +78 -0
  64. package/dist/tools/CreateTool.js +258 -0
  65. package/dist/tools/SelectTool.d.ts +155 -0
  66. package/dist/tools/SelectTool.js +808 -0
  67. package/dist/tools/ToolManager.d.ts +63 -0
  68. package/dist/tools/ToolManager.js +146 -0
  69. package/dist/tools/connectorEndpoints.d.ts +17 -0
  70. package/dist/tools/connectorEndpoints.js +53 -0
  71. package/dist/tools/frameRotation.d.ts +8 -0
  72. package/dist/tools/frameRotation.js +66 -0
  73. package/dist/tools/gradientHandles.d.ts +67 -0
  74. package/dist/tools/gradientHandles.js +110 -0
  75. package/dist/tools/presetPaths.d.ts +5 -0
  76. package/dist/tools/presetPaths.js +49 -0
  77. package/dist/tools/snapResize.d.ts +7 -0
  78. package/dist/tools/snapResize.js +65 -0
  79. package/dist/tools/snapping.d.ts +30 -0
  80. package/dist/tools/snapping.js +280 -0
  81. package/dist/tools/transform.d.ts +52 -0
  82. package/dist/tools/transform.js +277 -0
  83. package/package.json +36 -3
@@ -0,0 +1,329 @@
1
+ import { deleteSelection, joinBackward, joinForward, selectNodeBackward, selectNodeForward, splitBlockAs } from "prosemirror-commands";
2
+ import { TextSelection } from "prosemirror-state";
3
+ import { CARET_AFFINITY, VERTICAL_MOTION, lineGeometry } from "./CanvasEditorView.js";
4
+ import { offsetToPmPos, paragraphIndexAtPos, paragraphStarts, pmPosToOffset, snapDown } from "./flat.js";
5
+ import { nextGraphemeEnd, prevGraphemeStart, nextWordEnd, nextWordStart, prevWordStart } from "./graphemes.js";
6
+ import { schema } from "./schema.js";
7
+ export const fromPm = (c) => c;
8
+ export function chain(...commands) {
9
+ return (state, dispatch, view) => {
10
+ for (const c of commands)
11
+ if (c(state, dispatch, view))
12
+ return true;
13
+ return false;
14
+ };
15
+ }
16
+ export { nextGraphemeEnd, prevGraphemeStart, nextWordEnd, nextWordStart, prevWordStart, wordRangeAt } from "./graphemes.js";
17
+ function moveTo(state, dispatch, head, extend, meta, affinity = "downstream") {
18
+ if (dispatch) {
19
+ const anchor = extend ? state.selection.anchor : head;
20
+ const tr = state.tr.setSelection(TextSelection.create(state.doc, anchor, head));
21
+ if (meta)
22
+ tr.setMeta(meta, true);
23
+ tr.setMeta(CARET_AFFINITY, affinity);
24
+ dispatch(tr);
25
+ }
26
+ return true;
27
+ }
28
+ function moveHorizontal(dir, extend, back, forward) {
29
+ return (state, dispatch) => {
30
+ const sel = state.selection;
31
+ if (!extend && !sel.empty)
32
+ return moveTo(state, dispatch, dir === "left" ? sel.from : sel.to, false);
33
+ const $head = sel.$head;
34
+ const text = $head.parent.textContent;
35
+ const po = $head.parentOffset;
36
+ let head;
37
+ if (dir === "left") {
38
+ if (po === 0) {
39
+ if ($head.index(0) === 0)
40
+ return true;
41
+ head = $head.pos - 2;
42
+ }
43
+ else
44
+ head = $head.pos - (po - back(text, po));
45
+ }
46
+ else if (po === text.length) {
47
+ if ($head.index(0) === state.doc.childCount - 1)
48
+ return true;
49
+ head = $head.pos + 2;
50
+ }
51
+ else
52
+ head = $head.pos + (forward(text, po) - po);
53
+ return moveTo(state, dispatch, head, extend);
54
+ };
55
+ }
56
+ export function moveChar(dir, extend) {
57
+ return moveHorizontal(dir, extend, prevGraphemeStart, nextGraphemeEnd);
58
+ }
59
+ export function moveWord(dir, extend, windows = false) {
60
+ return moveHorizontal(dir, extend, prevWordStart, windows ? nextWordStart : nextWordEnd);
61
+ }
62
+ function headLine(state, geo) {
63
+ if (!geo)
64
+ return null;
65
+ const lines = geo.lines();
66
+ if (!lines || lines.length === 0)
67
+ return null;
68
+ const off = pmPosToOffset(state.doc, state.selection.head);
69
+ return { geo, index: geo.lineIndexAt(off), off };
70
+ }
71
+ export function lineStart(extend) {
72
+ return (state, dispatch, view) => {
73
+ const hl = headLine(state, lineGeometry(view));
74
+ const head = hl ? offsetToPmPos(state.doc, hl.geo.lines()[hl.index].start) : state.selection.$head.start();
75
+ return moveTo(state, dispatch, head, extend);
76
+ };
77
+ }
78
+ export function lineEnd(extend) {
79
+ return (state, dispatch, view) => {
80
+ const hl = headLine(state, lineGeometry(view));
81
+ const head = hl ? offsetToPmPos(state.doc, hl.geo.visualLineEnd(hl.index)) : state.selection.$head.end();
82
+ return moveTo(state, dispatch, head, extend, undefined, "upstream");
83
+ };
84
+ }
85
+ export function docStart(extend) {
86
+ return (state, dispatch) => moveTo(state, dispatch, 1, extend);
87
+ }
88
+ export function docEnd(extend) {
89
+ return (state, dispatch) => moveTo(state, dispatch, state.doc.content.size - 1, extend);
90
+ }
91
+ export function moveVertically(dir, extend) {
92
+ return (state, dispatch, view) => {
93
+ const doc = state.doc;
94
+ const $head = state.selection.$head;
95
+ const hl = headLine(state, lineGeometry(view));
96
+ if (!hl) {
97
+ const idx = $head.index(0);
98
+ const po = $head.parentOffset;
99
+ let head;
100
+ if (dir === "up") {
101
+ if (idx === 0)
102
+ return moveTo(state, dispatch, 1, extend);
103
+ const prev = doc.child(idx - 1);
104
+ head = $head.before() - prev.nodeSize + 1 + snapDown(prev.textContent, Math.min(po, prev.content.size));
105
+ }
106
+ else {
107
+ if (idx === doc.childCount - 1)
108
+ return moveTo(state, dispatch, doc.content.size - 1, extend);
109
+ const next = doc.child(idx + 1);
110
+ head = $head.after() + 1 + snapDown(next.textContent, Math.min(po, next.content.size));
111
+ }
112
+ return moveTo(state, dispatch, head, extend);
113
+ }
114
+ const { geo, index } = hl;
115
+ const lines = geo.lines();
116
+ const target = dir === "up" ? index - 1 : index + 1;
117
+ if (target < 0)
118
+ return moveTo(state, dispatch, 1, extend);
119
+ if (target >= lines.length)
120
+ return moveTo(state, dispatch, doc.content.size - 1, extend);
121
+ const x = geo.goalX ?? geo.caretLocal($head.pos).x;
122
+ const hit = geo.hitOnLine(target, x);
123
+ const head = offsetToPmPos(doc, hit.offset);
124
+ if (dispatch)
125
+ geo.goalX = x;
126
+ return moveTo(state, dispatch, head, extend, VERTICAL_MOTION, hit.affinity);
127
+ };
128
+ }
129
+ function deleteRange(state, dispatch, from, to) {
130
+ if (dispatch)
131
+ dispatch(state.tr.delete(from, to));
132
+ return true;
133
+ }
134
+ export const deleteCharBackward = (state, dispatch) => {
135
+ const { $head, empty } = state.selection;
136
+ const po = $head.parentOffset;
137
+ if (!empty || po === 0)
138
+ return false;
139
+ return deleteRange(state, dispatch, $head.pos - (po - prevGraphemeStart($head.parent.textContent, po)), $head.pos);
140
+ };
141
+ export const deleteCharForward = (state, dispatch) => {
142
+ const { $head, empty } = state.selection;
143
+ const text = $head.parent.textContent;
144
+ const po = $head.parentOffset;
145
+ if (!empty || po === text.length)
146
+ return false;
147
+ return deleteRange(state, dispatch, $head.pos, $head.pos + (nextGraphemeEnd(text, po) - po));
148
+ };
149
+ export const deleteWordBackward = (state, dispatch) => {
150
+ const { $head, empty } = state.selection;
151
+ const po = $head.parentOffset;
152
+ if (!empty || po === 0)
153
+ return false;
154
+ return deleteRange(state, dispatch, $head.pos - (po - prevWordStart($head.parent.textContent, po)), $head.pos);
155
+ };
156
+ function deleteWordForwardTo(boundary) {
157
+ return (state, dispatch) => {
158
+ const { $head, empty } = state.selection;
159
+ const text = $head.parent.textContent;
160
+ const po = $head.parentOffset;
161
+ if (!empty || po === text.length)
162
+ return false;
163
+ return deleteRange(state, dispatch, $head.pos, $head.pos + (boundary(text, po) - po));
164
+ };
165
+ }
166
+ export const deleteWordForward = deleteWordForwardTo(nextWordEnd);
167
+ export const deleteWordForwardWindows = deleteWordForwardTo(nextWordStart);
168
+ export const deleteToLineStart = (state, dispatch, view) => {
169
+ const { $head, empty } = state.selection;
170
+ if (!empty)
171
+ return false;
172
+ const hl = headLine(state, lineGeometry(view));
173
+ const from = hl ? offsetToPmPos(state.doc, hl.geo.lines()[hl.index].start) : $head.start();
174
+ if (from >= $head.pos)
175
+ return false;
176
+ return deleteRange(state, dispatch, from, $head.pos);
177
+ };
178
+ export const deleteToLineEnd = (state, dispatch, view) => {
179
+ const { $head, empty } = state.selection;
180
+ if (!empty)
181
+ return false;
182
+ const hl = headLine(state, lineGeometry(view));
183
+ const to = hl ? offsetToPmPos(state.doc, hl.geo.lines()[hl.index].end) : $head.end();
184
+ if (to <= $head.pos)
185
+ return false;
186
+ return deleteRange(state, dispatch, $head.pos, to);
187
+ };
188
+ export const selectAll = (state, dispatch) => {
189
+ if (dispatch)
190
+ dispatch(state.tr.setSelection(TextSelection.create(state.doc, 1, state.doc.content.size - 1)));
191
+ return true;
192
+ };
193
+ function markDefault(view, name) {
194
+ const ask = view?.someProp("markDefault");
195
+ const v = ask?.(name);
196
+ if (name === "weight")
197
+ return typeof v === "number" ? v : 400;
198
+ return typeof v === "boolean" ? v : false;
199
+ }
200
+ function effectiveValues(state, type, def) {
201
+ const { from, to, empty, $from } = state.selection;
202
+ const of = (marks) => type.isInSet(marks)?.attrs.value ?? def;
203
+ if (empty)
204
+ return [of(state.storedMarks ?? $from.marks())];
205
+ const out = [];
206
+ state.doc.nodesBetween(from, to, (node) => {
207
+ if (node.isInline)
208
+ out.push(of(node.marks));
209
+ });
210
+ return out.length ? out : [of($from.marks())];
211
+ }
212
+ export function applyMark(state, dispatch, type, mark) {
213
+ if (dispatch) {
214
+ const { from, to, empty } = state.selection;
215
+ const tr = state.tr;
216
+ if (empty) {
217
+ if (mark)
218
+ tr.addStoredMark(mark);
219
+ else
220
+ tr.removeStoredMark(type);
221
+ }
222
+ else if (mark)
223
+ tr.addMark(from, to, mark);
224
+ else
225
+ tr.removeMark(from, to, type);
226
+ dispatch(tr);
227
+ }
228
+ return true;
229
+ }
230
+ export function toggleMark(key) {
231
+ return (state, dispatch, view) => {
232
+ const type = schema.marks[key];
233
+ const def = markDefault(view, key);
234
+ const next = !effectiveValues(state, type, def).every((v) => v === true);
235
+ return applyMark(state, dispatch, type, next === def ? null : type.create({ value: next }));
236
+ };
237
+ }
238
+ export const toggleBold = (state, dispatch, view) => {
239
+ const type = schema.marks.weight;
240
+ const def = markDefault(view, "weight");
241
+ const next = effectiveValues(state, type, def).every((v) => typeof v === "number" && v >= 600) ? 400 : 700;
242
+ return applyMark(state, dispatch, type, next === def ? null : type.create({ value: next }));
243
+ };
244
+ function updateParagraphs(state, dispatch, update) {
245
+ const { from, to } = state.selection;
246
+ const doc = state.doc;
247
+ const starts = paragraphStarts(doc);
248
+ const first = paragraphIndexAtPos(doc, from);
249
+ const last = paragraphIndexAtPos(doc, to);
250
+ if (dispatch) {
251
+ const tr = state.tr;
252
+ let changed = false;
253
+ for (let i = first; i <= last; i++) {
254
+ const next = update(doc.child(i).attrs);
255
+ if (!next)
256
+ continue;
257
+ tr.setNodeMarkup(starts[i] + i, null, next);
258
+ changed = true;
259
+ }
260
+ if (changed)
261
+ dispatch(tr);
262
+ }
263
+ return true;
264
+ }
265
+ export function setParagraphAttr(name, value) {
266
+ return (state, dispatch) => updateParagraphs(state, dispatch, (attrs) => {
267
+ if (name === "checked" && attrs.list !== "task")
268
+ return null;
269
+ if (attrs[name] === value)
270
+ return null;
271
+ const next = { ...attrs, [name]: value };
272
+ if (name === "list" && value !== "task" && attrs.checked != null)
273
+ next.checked = null;
274
+ return next;
275
+ });
276
+ }
277
+ const leftList = (attrs) => ({ ...attrs, list: null, indent: 0, checked: null });
278
+ export const MAX_INDENT = 8;
279
+ export function indentListItem(delta) {
280
+ return (state, dispatch) => updateParagraphs(state, dispatch, (attrs) => {
281
+ if (attrs.list == null)
282
+ return null;
283
+ const indent = Math.max(0, Math.min(MAX_INDENT, (attrs.indent ?? 0) + delta));
284
+ return indent === attrs.indent ? null : { ...attrs, indent };
285
+ });
286
+ }
287
+ export const exitEmptyListItem = (state, dispatch) => {
288
+ const { $head, empty } = state.selection;
289
+ if (!empty || $head.parent.content.size !== 0 || $head.parent.attrs.list == null)
290
+ return false;
291
+ return updateParagraphs(state, dispatch, leftList);
292
+ };
293
+ export const exitListItemAtStart = (state, dispatch) => {
294
+ const { $head, empty } = state.selection;
295
+ if (!empty || $head.parentOffset !== 0 || $head.parent.attrs.list == null)
296
+ return false;
297
+ return updateParagraphs(state, dispatch, (attrs) => {
298
+ const indent = attrs.indent ?? 0;
299
+ return indent > 0 ? { ...attrs, indent: indent - 1 } : leftList(attrs);
300
+ });
301
+ };
302
+ const splitInheriting = splitBlockAs((node) => ({ type: node.type, attrs: node.attrs }));
303
+ export const splitParagraph = (state, dispatch, view) => fromPm(splitInheriting)(state, dispatch &&
304
+ ((tr) => {
305
+ const marks = state.storedMarks || (state.selection.$to.parentOffset && state.selection.$from.marks());
306
+ if (marks)
307
+ tr.ensureMarks(marks);
308
+ dispatch(tr);
309
+ }), view);
310
+ export const insertHardBreak = (state, dispatch) => {
311
+ if (dispatch)
312
+ dispatch(state.tr.replaceSelectionWith(schema.nodes.hard_break.create(), true));
313
+ return true;
314
+ };
315
+ export const pm = {
316
+ deleteSelection: fromPm(deleteSelection),
317
+ joinBackward: fromPm(joinBackward),
318
+ joinForward: fromPm(joinForward),
319
+ selectNodeBackward: fromPm(selectNodeBackward),
320
+ selectNodeForward: fromPm(selectNodeForward),
321
+ };
322
+ export const backspace = chain(pm.deleteSelection, deleteCharBackward, exitListItemAtStart, pm.joinBackward, pm.selectNodeBackward);
323
+ export const del = chain(pm.deleteSelection, deleteCharForward, pm.joinForward, pm.selectNodeForward);
324
+ export const wordBackspace = chain(pm.deleteSelection, deleteWordBackward, exitListItemAtStart, pm.joinBackward);
325
+ export const wordDelete = chain(pm.deleteSelection, deleteWordForward, pm.joinForward);
326
+ export const windowsWordDelete = chain(pm.deleteSelection, deleteWordForwardWindows, pm.joinForward);
327
+ export const enter = chain(exitEmptyListItem, splitParagraph);
328
+ export const lineBackspace = chain(pm.deleteSelection, deleteToLineStart, exitListItemAtStart, pm.joinBackward);
329
+ export const lineDelete = chain(pm.deleteSelection, deleteToLineEnd, pm.joinForward);
@@ -0,0 +1,51 @@
1
+ export declare const MIRROR_MAX_UNITS = 2048;
2
+ export declare const MIRROR_GAP = "\uFFFC";
3
+ export interface MirrorSegment {
4
+ readonly at: number;
5
+ readonly from: number;
6
+ readonly to: number;
7
+ }
8
+ export interface MirrorWindow {
9
+ readonly text: string;
10
+ readonly segments: readonly MirrorSegment[];
11
+ readonly anchor: number;
12
+ readonly head: number;
13
+ }
14
+ export interface MirrorText {
15
+ length(): number;
16
+ text(from: number, to: number): string;
17
+ }
18
+ export declare function mirrorWindow(source: MirrorText, anchor: number, head: number, max?: number): MirrorWindow;
19
+ export declare function toMirror(segments: readonly MirrorSegment[], flat: number): number | null;
20
+ export declare function toFlat(segments: readonly MirrorSegment[], m: number, side?: "start" | "end"): number;
21
+ export interface TextEdit {
22
+ from: number;
23
+ to: number;
24
+ text: string;
25
+ }
26
+ export declare function diffText(a: string, b: string, preferred?: number, caret?: number): TextEdit | null;
27
+ export declare function compositionRange(base: string, shown: string, composing: string, near: number): {
28
+ from: number;
29
+ to: number;
30
+ } | null;
31
+ export declare function selectionToFlat(segments: readonly MirrorSegment[], anchor: number, head: number): {
32
+ anchor: number;
33
+ head: number;
34
+ };
35
+ export interface SinkMirrorDom {
36
+ read(): string;
37
+ write(text: string, force?: boolean): void;
38
+ selection(): {
39
+ anchor: number;
40
+ head: number;
41
+ } | null;
42
+ select(anchor: number, head: number): void;
43
+ offsetOf(node: unknown, offset: number): number | null;
44
+ caret(): {
45
+ x: number;
46
+ y: number;
47
+ } | null;
48
+ scrollBy(dx: number, dy: number): void;
49
+ }
50
+ export declare const SINK_LINE_PX = 16;
51
+ export declare function domMirror(element: HTMLElement): SinkMirrorDom;
@@ -0,0 +1,292 @@
1
+ import { isCodePointBoundary } from "./flat.js";
2
+ export const MIRROR_MAX_UNITS = 2048;
3
+ export const MIRROR_GAP = "\uFFFC";
4
+ export function mirrorWindow(source, anchor, head, max = MIRROR_MAX_UNITS) {
5
+ const length = source.length();
6
+ const end = Math.max(0, length - 1);
7
+ const clamp = (x) => Math.max(0, Math.min(end, x));
8
+ const a = clamp(anchor);
9
+ const h = clamp(head);
10
+ const from = Math.min(a, h);
11
+ const to = Math.max(a, h);
12
+ const ranges = [];
13
+ if (to - from <= max / 2) {
14
+ const rest = max - (to - from);
15
+ let before = Math.min(from, Math.ceil(rest / 2));
16
+ const after = Math.min(end - to, rest - before);
17
+ before = Math.min(from, rest - after);
18
+ ranges.push([from - before, to + after]);
19
+ }
20
+ else {
21
+ const quarter = Math.floor(max / 4);
22
+ ranges.push([Math.max(0, from - quarter), from + quarter], [to - quarter, Math.min(end, to + quarter - 1)]);
23
+ }
24
+ const segments = [];
25
+ let text = "";
26
+ for (const [start, stop] of ranges) {
27
+ if (segments.length)
28
+ text += MIRROR_GAP;
29
+ const lo = Math.max(0, start - 1);
30
+ const t = source.text(lo, Math.min(length, stop + 1));
31
+ let s = start;
32
+ let e = stop;
33
+ if (!isCodePointBoundary(t, s - lo))
34
+ s++;
35
+ if (!isCodePointBoundary(t, e - lo))
36
+ e--;
37
+ if (e < s)
38
+ e = s;
39
+ segments.push({ at: text.length, from: s, to: e });
40
+ text += t.slice(s - lo, e - lo);
41
+ }
42
+ return { text, segments, anchor: toMirror(segments, a) ?? 0, head: toMirror(segments, h) ?? 0 };
43
+ }
44
+ export function toMirror(segments, flat) {
45
+ for (const s of segments)
46
+ if (flat >= s.from && flat <= s.to)
47
+ return s.at + (flat - s.from);
48
+ return null;
49
+ }
50
+ export function toFlat(segments, m, side = "end") {
51
+ let prev = null;
52
+ for (let i = 0; i < segments.length; i++) {
53
+ const s = segments[i];
54
+ const length = s.to - s.from;
55
+ if (m < s.at)
56
+ return prev ? prev.to : s.from;
57
+ if (m < s.at + length || (m === s.at + length && (side === "end" || i === segments.length - 1)))
58
+ return s.from + (m - s.at);
59
+ prev = s;
60
+ }
61
+ return segments[segments.length - 1]?.to ?? 0;
62
+ }
63
+ const isHigh = (c) => c >= 0xd800 && c <= 0xdbff;
64
+ const isLow = (c) => c >= 0xdc00 && c <= 0xdfff;
65
+ const parts = (s, i) => i > 0 && i < s.length && isHigh(s.charCodeAt(i - 1)) && isLow(s.charCodeAt(i));
66
+ export function diffText(a, b, preferred = 0, caret) {
67
+ const la = a.length;
68
+ const lb = b.length;
69
+ const min = Math.min(la, lb);
70
+ let prefix = 0;
71
+ while (prefix < min && a.charCodeAt(prefix) === b.charCodeAt(prefix))
72
+ prefix++;
73
+ if (prefix === la && prefix === lb)
74
+ return null;
75
+ let suffix = 0;
76
+ while (suffix < min && a.charCodeAt(la - 1 - suffix) === b.charCodeAt(lb - 1 - suffix))
77
+ suffix++;
78
+ let lenA;
79
+ let lenB;
80
+ let lo;
81
+ let hi;
82
+ if (prefix + suffix >= min) {
83
+ lenA = Math.max(0, la - lb);
84
+ lenB = Math.max(0, lb - la);
85
+ lo = Math.max(0, la - lenA - suffix);
86
+ hi = Math.min(prefix, la - lenA);
87
+ }
88
+ else {
89
+ lenA = la - prefix - suffix;
90
+ lenB = lb - prefix - suffix;
91
+ lo = hi = prefix;
92
+ }
93
+ let x = hi;
94
+ if (caret !== undefined && caret - lenB >= lo && caret - lenB <= hi)
95
+ x = caret - lenB;
96
+ else if (preferred >= lo && preferred <= hi)
97
+ x = preferred;
98
+ let start = x;
99
+ let endA = x + lenA;
100
+ let endB = x + lenB;
101
+ while (start > 0 && (parts(a, start) || parts(b, start)))
102
+ start--;
103
+ while (endA < la && endB < lb && (parts(a, endA) || parts(b, endB))) {
104
+ endA++;
105
+ endB++;
106
+ }
107
+ return { from: start, to: endA, text: b.slice(start, endB) };
108
+ }
109
+ export function compositionRange(base, shown, composing, near) {
110
+ const c = composing.length;
111
+ const replaced = base.length - shown.length + c;
112
+ if (replaced < 0)
113
+ return null;
114
+ const min = Math.min(base.length, shown.length);
115
+ let prefix = 0;
116
+ while (prefix < min && base.charCodeAt(prefix) === shown.charCodeAt(prefix))
117
+ prefix++;
118
+ let suffix = 0;
119
+ while (suffix < min && base.charCodeAt(base.length - 1 - suffix) === shown.charCodeAt(shown.length - 1 - suffix))
120
+ suffix++;
121
+ const lo = Math.max(0, base.length - replaced - suffix);
122
+ const hi = Math.min(prefix, base.length - replaced);
123
+ let best = null;
124
+ for (let s = lo; s <= hi; s++) {
125
+ if (shown.startsWith(composing, s) && (best === null || Math.abs(s - near) < Math.abs(best - near)))
126
+ best = s;
127
+ }
128
+ return best === null ? null : { from: best, to: best + replaced };
129
+ }
130
+ export function selectionToFlat(segments, anchor, head) {
131
+ const ranged = anchor !== head;
132
+ const map = (m, earlier) => toFlat(segments, m, ranged && earlier ? "start" : "end");
133
+ return { anchor: map(anchor, anchor < head), head: map(head, head < anchor) };
134
+ }
135
+ export const SINK_LINE_PX = 16;
136
+ function selectionPoints(element) {
137
+ const doc = element.ownerDocument;
138
+ const root = element.getRootNode();
139
+ const own = root.getSelection;
140
+ const sel = typeof own === "function" ? own.call(root) : doc.getSelection();
141
+ if (!sel)
142
+ return null;
143
+ if (sel.anchorNode && sel.focusNode && (element === sel.anchorNode || element.contains(sel.anchorNode))) {
144
+ return { anchorNode: sel.anchorNode, anchorOffset: sel.anchorOffset, focusNode: sel.focusNode, focusOffset: sel.focusOffset };
145
+ }
146
+ const composed = sel.getComposedRanges;
147
+ if (typeof composed !== "function" || root === doc)
148
+ return null;
149
+ let ranges;
150
+ try {
151
+ ranges = composed.call(sel, { shadowRoots: [root] });
152
+ }
153
+ catch {
154
+ ranges = composed.call(sel, root);
155
+ }
156
+ const r = ranges[0];
157
+ if (!r)
158
+ return null;
159
+ return { anchorNode: r.startContainer, anchorOffset: r.startOffset, focusNode: r.endContainer, focusOffset: r.endOffset };
160
+ }
161
+ function flatten(element) {
162
+ let text = "";
163
+ const spans = new Map();
164
+ const visit = (node, last) => {
165
+ const start = text.length;
166
+ if (node.nodeType === 3) {
167
+ text += node.data;
168
+ }
169
+ else if (node.nodeType === 1) {
170
+ const name = node.nodeName;
171
+ if (name === "BR") {
172
+ if (!last)
173
+ text += "\n";
174
+ }
175
+ else {
176
+ if ((name === "DIV" || name === "P") && text.length > 0 && !text.endsWith("\n"))
177
+ text += "\n";
178
+ const kids = node.childNodes;
179
+ for (let i = 0; i < kids.length; i++)
180
+ visit(kids[i], last && i === kids.length - 1);
181
+ }
182
+ }
183
+ spans.set(node, [start, text.length]);
184
+ };
185
+ const kids = element.childNodes;
186
+ for (let i = 0; i < kids.length; i++)
187
+ visit(kids[i], i === kids.length - 1);
188
+ spans.set(element, [0, text.length]);
189
+ return { text, spans };
190
+ }
191
+ export function domMirror(element) {
192
+ const offsetIn = (spans, node, offset) => {
193
+ const span = spans.get(node);
194
+ if (!span)
195
+ return null;
196
+ if (node.nodeType === 3)
197
+ return span[0] + Math.max(0, Math.min(offset, node.data.length));
198
+ const kids = node.childNodes;
199
+ if (offset < kids.length)
200
+ return spans.get(kids[offset])?.[0] ?? span[1];
201
+ return span[1];
202
+ };
203
+ const pointAt = (offset) => {
204
+ const kids = element.childNodes;
205
+ if (kids.length === 1 && kids[0].nodeType === 3)
206
+ return [kids[0], Math.max(0, Math.min(offset, kids[0].data.length))];
207
+ const { spans } = flatten(element);
208
+ let last = null;
209
+ for (const [node, [start, end]] of spans) {
210
+ if (node.nodeType !== 3)
211
+ continue;
212
+ if (offset >= start && offset <= end)
213
+ return [node, offset - start];
214
+ last = [node, end - start];
215
+ }
216
+ return last ?? [element, 0];
217
+ };
218
+ return {
219
+ read: () => flatten(element).text,
220
+ write(text, force = false) {
221
+ const kids = element.childNodes;
222
+ if (kids.length === 1 && kids[0].nodeType === 3) {
223
+ const node = kids[0];
224
+ const old = node.data;
225
+ if (force) {
226
+ node.replaceData(0, old.length, text);
227
+ return;
228
+ }
229
+ if (old === text)
230
+ return;
231
+ let p = 0;
232
+ const min = Math.min(old.length, text.length);
233
+ while (p < min && old.charCodeAt(p) === text.charCodeAt(p))
234
+ p++;
235
+ let s = 0;
236
+ while (s < min - p && old.charCodeAt(old.length - 1 - s) === text.charCodeAt(text.length - 1 - s))
237
+ s++;
238
+ node.replaceData(p, old.length - p - s, text.slice(p, text.length - s));
239
+ return;
240
+ }
241
+ if (!force && kids.length === 0 && text === "")
242
+ return;
243
+ element.textContent = text;
244
+ },
245
+ selection() {
246
+ const sel = selectionPoints(element);
247
+ if (!sel || !element.contains(sel.focusNode))
248
+ return null;
249
+ const { spans } = flatten(element);
250
+ const anchor = offsetIn(spans, sel.anchorNode, sel.anchorOffset);
251
+ const head = offsetIn(spans, sel.focusNode, sel.focusOffset);
252
+ return anchor === null || head === null ? null : { anchor, head };
253
+ },
254
+ select(anchor, head) {
255
+ const sel = element.ownerDocument.getSelection();
256
+ if (!sel)
257
+ return;
258
+ const [an, ao] = pointAt(anchor);
259
+ const [hn, ho] = pointAt(head);
260
+ sel.setBaseAndExtent(an, ao, hn, ho);
261
+ },
262
+ offsetOf(node, offset) {
263
+ if (!(node instanceof Node) || !(node === element || element.contains(node)))
264
+ return null;
265
+ return offsetIn(flatten(element).spans, node, offset);
266
+ },
267
+ caret() {
268
+ const sel = selectionPoints(element);
269
+ if (!sel || !element.contains(sel.focusNode))
270
+ return null;
271
+ const range = element.ownerDocument.createRange();
272
+ range.setStart(sel.focusNode, sel.focusOffset);
273
+ range.collapse(true);
274
+ const rect = range.getClientRects()[0];
275
+ if (rect)
276
+ return { x: rect.left + rect.width / 2, y: rect.top + rect.height / 2 };
277
+ const { text, spans } = flatten(element);
278
+ const at = offsetIn(spans, sel.focusNode, sel.focusOffset) ?? 0;
279
+ let lines = 0;
280
+ for (let i = 0; i < at; i++)
281
+ if (text.charCodeAt(i) === 10)
282
+ lines++;
283
+ const box = element.getBoundingClientRect();
284
+ const scale = element.offsetHeight ? box.height / element.offsetHeight : 1;
285
+ return { x: box.left - element.scrollLeft * scale, y: box.top + ((lines + 0.5) * SINK_LINE_PX - element.scrollTop) * scale };
286
+ },
287
+ scrollBy(dx, dy) {
288
+ element.scrollLeft += dx;
289
+ element.scrollTop += dy;
290
+ },
291
+ };
292
+ }