@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,841 @@
1
+ import { interpretBeforeInput, normalizePasteText } from "./inputReducer.js";
2
+ import { RICH_TEXT_MIME } from "./clipboard.js";
3
+ import { MIRROR_GAP, SINK_LINE_PX, compositionRange, diffText, domMirror, mirrorWindow, selectionToFlat, toFlat, toMirror } from "./contextMirror.js";
4
+ import { nextGraphemeEnd, prevGraphemeStart } from "./graphemes.js";
5
+ function defaultIsActiveElement(element) {
6
+ return typeof document !== "undefined" && document.activeElement === element;
7
+ }
8
+ const COMPOSITION_ENTER_WINDOW_MS = 500;
9
+ const SINK_STYLE = [
10
+ "position:fixed",
11
+ "left:0",
12
+ "top:0",
13
+ "min-width:1px",
14
+ "height:16px",
15
+ "margin:0",
16
+ "padding:0",
17
+ "border:0",
18
+ "white-space:pre",
19
+ "overflow:hidden",
20
+ "opacity:0.01",
21
+ "pointer-events:none",
22
+ "z-index:0",
23
+ "color:transparent",
24
+ "caret-color:transparent",
25
+ "outline:none",
26
+ "background:transparent",
27
+ "font:16px/1 sans-serif",
28
+ "resize:none",
29
+ ].join(";");
30
+ const MIRROR_STYLE = ";width:1px";
31
+ const SINK_ATTRIBUTES = [
32
+ ["contenteditable", "true"],
33
+ ["role", "textbox"],
34
+ ["aria-multiline", "true"],
35
+ ["spellcheck", "false"],
36
+ ["autocapitalize", "off"],
37
+ ["autocomplete", "off"],
38
+ ["autocorrect", "off"],
39
+ ["data-daas-input-sink", ""],
40
+ ];
41
+ function defaultCreateElement() {
42
+ if (typeof document === "undefined")
43
+ throw new Error("InputSink needs a DOM or an injected createElement");
44
+ return document.createElement("div");
45
+ }
46
+ function detectSafari() {
47
+ return typeof navigator !== "undefined" && /Apple Computer/.test(navigator.vendor);
48
+ }
49
+ function compositionOver(e) {
50
+ switch (e.type) {
51
+ case "compositionend":
52
+ case "compositionstart":
53
+ return true;
54
+ case "keydown":
55
+ case "beforeinput":
56
+ case "input":
57
+ return e.isComposing === false;
58
+ default:
59
+ return false;
60
+ }
61
+ }
62
+ function inputPayload(e) {
63
+ if (e.inputType.startsWith("delete"))
64
+ return "";
65
+ if (e.inputType === "insertParagraph" || e.inputType === "insertLineBreak")
66
+ return "\n";
67
+ if (!e.inputType.startsWith("insert"))
68
+ return null;
69
+ const data = e.data ?? e.dataTransfer?.getData("text/plain") ?? null;
70
+ return data === null ? null : data.replace(/\r\n?/g, "\n");
71
+ }
72
+ function editByText(old, shown, text, caret, preferred) {
73
+ const n = old.length - shown.length + text.length;
74
+ if (n < 0 || n > old.length)
75
+ return null;
76
+ const min = Math.min(old.length, shown.length);
77
+ let prefix = 0;
78
+ while (prefix < min && old.charCodeAt(prefix) === shown.charCodeAt(prefix))
79
+ prefix++;
80
+ let suffix = 0;
81
+ while (suffix < min && old.charCodeAt(old.length - 1 - suffix) === shown.charCodeAt(shown.length - 1 - suffix))
82
+ suffix++;
83
+ const lo = Math.max(0, old.length - n - suffix);
84
+ const hi = Math.min(prefix, old.length - n);
85
+ const fits = (x) => x >= lo && x <= hi && shown.startsWith(text, x);
86
+ let x = caret !== undefined && fits(caret - text.length) ? caret - text.length : null;
87
+ for (let d = 0; x === null && (preferred - d >= lo || preferred + d <= hi); d++) {
88
+ if (fits(preferred - d))
89
+ x = preferred - d;
90
+ else if (fits(preferred + d))
91
+ x = preferred + d;
92
+ }
93
+ return x === null ? null : { from: x, to: x + n, text };
94
+ }
95
+ function aroundGap(written, d) {
96
+ const second = written.segments[1];
97
+ const g = second ? second.at - 1 : -1;
98
+ const k = d.text.indexOf(MIRROR_GAP);
99
+ if (g < 0 || written.text[g] !== MIRROR_GAP || k < 0 || d.from > g || d.to <= g)
100
+ return [d];
101
+ return [{ from: d.from, to: g, text: d.text.slice(0, k) }, { from: g + 1, to: d.to, text: d.text.slice(k + 1) }].filter((e) => e.to > e.from || e.text);
102
+ }
103
+ function detectVirtualKeyboard() {
104
+ if (typeof matchMedia !== "function")
105
+ return false;
106
+ try {
107
+ return matchMedia("(any-pointer: coarse)").matches && !matchMedia("(any-pointer: fine)").matches;
108
+ }
109
+ catch {
110
+ return false;
111
+ }
112
+ }
113
+ export class InputSink {
114
+ host;
115
+ target;
116
+ element;
117
+ listeners;
118
+ safari;
119
+ isActiveElement;
120
+ keepsSession;
121
+ mirror;
122
+ dom;
123
+ attached = false;
124
+ composing = false;
125
+ compositionEndedAt = -Infinity;
126
+ suppressBlur = false;
127
+ pasteGuard = false;
128
+ pasteTimer = null;
129
+ written = null;
130
+ writtenVersion = undefined;
131
+ unaligned = true;
132
+ local;
133
+ bounds;
134
+ composition = null;
135
+ disconnect = null;
136
+ holding = 0;
137
+ syncing = false;
138
+ released = false;
139
+ releasedComposition = false;
140
+ releaseTimer = null;
141
+ pending = null;
142
+ frozen = false;
143
+ constructor(host, target, options = {}) {
144
+ this.host = host;
145
+ this.target = target;
146
+ this.element = (options.createElement ?? defaultCreateElement)();
147
+ this.safari = options.safari ?? detectSafari();
148
+ this.isActiveElement = options.isActiveElement ?? defaultIsActiveElement;
149
+ this.keepsSession = options.keepsSession ?? (() => false);
150
+ this.local = options.local ?? ((p) => p);
151
+ this.bounds = options.bounds;
152
+ this.mirror = target.mirror ?? null;
153
+ this.dom = this.mirror ? (options.mirrorDom ?? ((el) => domMirror(el)))(this.element) : null;
154
+ for (const [name, value] of SINK_ATTRIBUTES)
155
+ this.element.setAttribute(name, value);
156
+ if (this.mirror && (options.virtualKeyboard ?? detectVirtualKeyboard())) {
157
+ this.element.setAttribute("autocorrect", "on");
158
+ this.element.setAttribute("autocapitalize", "sentences");
159
+ }
160
+ this.element.style.cssText = this.mirror ? SINK_STYLE + MIRROR_STYLE : SINK_STYLE;
161
+ this.listeners = [
162
+ ["keydown", (e) => this.onKeyDown(e)],
163
+ ["beforeinput", (e) => this.onBeforeInput(e)],
164
+ ["input", (e) => this.onInput(e)],
165
+ ["compositionstart", (e) => this.onCompositionStart(e)],
166
+ ["compositionupdate", (e) => this.onCompositionUpdate(e)],
167
+ ["compositionend", (e) => this.onCompositionEnd(e)],
168
+ ["copy", (e) => this.onCopy(e)],
169
+ ["cut", (e) => this.onCut(e)],
170
+ ["paste", (e) => this.onPaste(e)],
171
+ ["blur", (e) => this.onBlur(e)],
172
+ ];
173
+ if (options.handleEvent) {
174
+ const allowed = options.handleEvent;
175
+ this.listeners = this.listeners.map(([name, handler]) => [name, (event) => {
176
+ if (this.releasedComposition && compositionOver(event))
177
+ this.endReleasedComposition();
178
+ if (allowed(event))
179
+ handler(event);
180
+ else
181
+ this.onReleased(event);
182
+ }]);
183
+ }
184
+ }
185
+ attach() {
186
+ if (this.attached)
187
+ return;
188
+ this.attached = true;
189
+ if (this.mirror) {
190
+ this.written = null;
191
+ this.unaligned = true;
192
+ this.released = this.releasedComposition = false;
193
+ this.pending = null;
194
+ this.disconnect = this.mirror.connect({ changed: () => this.rewrite(), sync: (commit) => this.sync(commit), focused: () => this.restoreSelection() });
195
+ this.rewrite();
196
+ }
197
+ else
198
+ this.element.textContent = "";
199
+ for (const [type, handler] of this.listeners)
200
+ this.element.addEventListener(type, handler);
201
+ this.host.appendChild(this.element);
202
+ }
203
+ detach() {
204
+ if (!this.attached)
205
+ return;
206
+ this.attached = false;
207
+ for (const [type, handler] of this.listeners)
208
+ this.element.removeEventListener(type, handler);
209
+ this.clearPasteGuard();
210
+ if (this.releaseTimer !== null)
211
+ clearTimeout(this.releaseTimer);
212
+ this.releaseTimer = null;
213
+ this.composing = false;
214
+ this.composition = null;
215
+ this.disconnect?.();
216
+ this.disconnect = null;
217
+ this.written = null;
218
+ this.element.remove();
219
+ }
220
+ focus() {
221
+ this.suppressBlur = true;
222
+ try {
223
+ this.element.focus({ preventScroll: true });
224
+ }
225
+ finally {
226
+ this.suppressBlur = false;
227
+ }
228
+ this.restoreSelection();
229
+ }
230
+ updatePosition() {
231
+ const rect = this.target.caretClientRect();
232
+ if (!rect)
233
+ return;
234
+ const height = Math.max(1, rect.height);
235
+ const b = this.bounds?.();
236
+ const left = b ? Math.max(b.x, Math.min(b.x + b.w - 1, rect.left)) : rect.left;
237
+ const top = b ? Math.max(b.y, Math.min(b.y + b.h - height, rect.top)) : rect.top;
238
+ const style = this.element.style;
239
+ style.left = `${left}px`;
240
+ style.top = `${top}px`;
241
+ style.height = `${height}px`;
242
+ if (!this.dom || !this.written || !this.unaligned || this.composing || !this.isActiveElement(this.element))
243
+ return;
244
+ this.unaligned = false;
245
+ const centre = this.dom.caret();
246
+ if (!centre)
247
+ return;
248
+ const at = this.local(centre);
249
+ this.dom.scrollBy(at.x - left, at.y - SINK_LINE_PX / 2 - top);
250
+ }
251
+ poll() {
252
+ this.readKeyboard(true);
253
+ }
254
+ commitComposition() {
255
+ if (!this.composing)
256
+ return false;
257
+ if (!this.mirror) {
258
+ this.composing = false;
259
+ this.target.compositionEnd(this.element.textContent ?? "");
260
+ this.element.textContent = "";
261
+ return true;
262
+ }
263
+ this.finishComposition(this.composition?.data ?? "", true);
264
+ return true;
265
+ }
266
+ onKeyDown(e) {
267
+ if (!this.composing)
268
+ this.readKeyboard(true);
269
+ if (this.composing || e.isComposing || e.keyCode === 229)
270
+ return;
271
+ if (this.safari && e.key === "Enter" && e.timeStamp - this.compositionEndedAt < COMPOSITION_ENTER_WINDOW_MS) {
272
+ this.compositionEndedAt = -Infinity;
273
+ return;
274
+ }
275
+ const handled = this.target.keydown({
276
+ key: e.key,
277
+ code: e.code,
278
+ shiftKey: e.shiftKey,
279
+ ctrlKey: e.ctrlKey,
280
+ altKey: e.altKey,
281
+ metaKey: e.metaKey,
282
+ });
283
+ if (handled)
284
+ e.preventDefault();
285
+ }
286
+ onBeforeInput(e) {
287
+ if (e.inputType === "insertCompositionText" || e.inputType === "deleteCompositionText") {
288
+ const at = this.composition ? this.targetOffsets(e) : null;
289
+ if (at)
290
+ this.composition.near = at.from;
291
+ return;
292
+ }
293
+ const target = this.mirror && !this.composing ? this.targetFlat(e) : null;
294
+ if (this.mirror) {
295
+ if (e.cancelable === false) {
296
+ if (!this.composing)
297
+ this.readBefore(e, target);
298
+ return;
299
+ }
300
+ if (!this.composing)
301
+ this.readKeyboard(true);
302
+ }
303
+ e.preventDefault();
304
+ if (e.inputType === "insertFromPaste") {
305
+ this.applyPaste(this.readClipboard(e.dataTransfer, e.data ?? ""));
306
+ return;
307
+ }
308
+ const data = e.data ?? e.dataTransfer?.getData("text/plain") ?? null;
309
+ if (this.mirror) {
310
+ if (e.inputType === "insertReplacementText")
311
+ return this.replaceByTarget(target, data ?? "");
312
+ if ((e.inputType === "deleteContentBackward" || e.inputType === "deleteContentForward") && this.deleteByTarget(target, e.inputType === "deleteContentBackward"))
313
+ return;
314
+ }
315
+ const action = interpretBeforeInput(e.inputType, data);
316
+ if (action)
317
+ this.apply(action);
318
+ }
319
+ onInput(e) {
320
+ if (!this.mirror) {
321
+ if (!this.composing && this.element.textContent)
322
+ this.element.textContent = "";
323
+ return;
324
+ }
325
+ if (this.composing)
326
+ return this.followComposition();
327
+ if (e.inputType === "insertFromComposition")
328
+ return this.rewrite();
329
+ this.readKeyboard(true, undefined, e);
330
+ }
331
+ onCompositionStart(e) {
332
+ if (this.composing)
333
+ return;
334
+ const prior = this.mirror?.selection();
335
+ if (this.mirror) {
336
+ this.frozen = true;
337
+ try {
338
+ this.readKeyboard(true);
339
+ }
340
+ finally {
341
+ this.frozen = false;
342
+ }
343
+ }
344
+ this.composing = true;
345
+ const mirror = this.mirror;
346
+ const base = this.written;
347
+ if (!mirror || !base) {
348
+ this.target.compositionStart();
349
+ return;
350
+ }
351
+ const sel = mirror.selection();
352
+ const start = { from: Math.min(sel.anchor, sel.head), to: Math.max(sel.anchor, sel.head) };
353
+ this.target.compositionStart();
354
+ const near = toMirror(base.segments, start.from) ?? 0;
355
+ const selected = !!prior && prior.anchor !== prior.head && Math.min(prior.anchor, prior.head) === start.from && Math.max(prior.anchor, prior.head) === start.to;
356
+ const c = { base, version: mirror.textVersion(), start, selected, range: { ...start }, text: "", data: "", near };
357
+ this.composition = c;
358
+ const data = e?.data ?? "";
359
+ const startM = toMirror(base.segments, start.to) ?? near;
360
+ if (data && base.text.slice(near, startM) !== data && this.dom) {
361
+ const r = compositionRange(base.text, this.dom.read(), data, near - data.length);
362
+ if (r)
363
+ this.composeAt(c, r, data);
364
+ }
365
+ }
366
+ onCompositionUpdate(e) {
367
+ if (!this.composing)
368
+ this.onCompositionStart();
369
+ const data = e.data ?? "";
370
+ if (this.composition) {
371
+ this.composition.data = data;
372
+ this.composition.text = data;
373
+ }
374
+ this.target.compositionUpdate(data);
375
+ }
376
+ onCompositionEnd(e) {
377
+ if (!this.composing)
378
+ return;
379
+ if (this.mirror) {
380
+ this.compositionEndedAt = e.timeStamp;
381
+ this.finishComposition(e.data ?? "", false);
382
+ return;
383
+ }
384
+ this.composing = false;
385
+ this.compositionEndedAt = e.timeStamp;
386
+ this.target.compositionEnd(e.data ?? "");
387
+ queueMicrotask(() => {
388
+ if (!this.composing)
389
+ this.element.textContent = "";
390
+ });
391
+ }
392
+ onCopy(e) {
393
+ if (!this.composing)
394
+ this.readKeyboard(true);
395
+ const content = this.target.copyClipboard?.() ?? { text: this.target.copyText() };
396
+ if (!content.text && !content.richText)
397
+ return;
398
+ e.preventDefault();
399
+ this.writeClipboard(e.clipboardData, content);
400
+ }
401
+ onCut(e) {
402
+ if (!this.composing)
403
+ this.readKeyboard(true);
404
+ const content = this.target.copyClipboard?.();
405
+ const text = this.target.cut();
406
+ if (!text && !content?.richText)
407
+ return;
408
+ e.preventDefault();
409
+ this.writeClipboard(e.clipboardData, content ?? { text });
410
+ }
411
+ onPaste(e) {
412
+ if (!this.composing)
413
+ this.readKeyboard(true);
414
+ e.preventDefault();
415
+ this.applyPaste(this.readClipboard(e.clipboardData));
416
+ }
417
+ onBlur(e) {
418
+ if (this.suppressBlur)
419
+ return;
420
+ if (this.isActiveElement(this.element))
421
+ return;
422
+ if (this.keepsSession(e.relatedTarget ?? null)) {
423
+ if (this.composing) {
424
+ if (this.mirror)
425
+ this.finishComposition(this.composition?.data ?? "", false);
426
+ else {
427
+ this.composing = false;
428
+ this.target.compositionEnd(this.element.textContent ?? "");
429
+ }
430
+ }
431
+ else if (this.mirror) {
432
+ this.readKeyboard(true, "blur");
433
+ }
434
+ if (!this.mirror)
435
+ this.element.textContent = "";
436
+ return;
437
+ }
438
+ this.composing = false;
439
+ this.composition = null;
440
+ if (!this.mirror)
441
+ this.element.textContent = "";
442
+ this.target.blur();
443
+ }
444
+ apply(action) {
445
+ switch (action.kind) {
446
+ case "insertText":
447
+ this.target.insertText(action.text);
448
+ break;
449
+ case "splitBlock":
450
+ this.target.splitBlock();
451
+ break;
452
+ case "insertLineBreak":
453
+ this.target.insertLineBreak();
454
+ break;
455
+ case "deleteBackward":
456
+ this.target.deleteBackward(action.unit);
457
+ break;
458
+ case "deleteForward":
459
+ this.target.deleteForward(action.unit);
460
+ break;
461
+ case "paste":
462
+ this.applyPaste({ text: action.text });
463
+ break;
464
+ case "undo":
465
+ this.target.undo();
466
+ break;
467
+ case "redo":
468
+ this.target.redo();
469
+ break;
470
+ }
471
+ }
472
+ applyPaste(content) {
473
+ if (this.pasteGuard)
474
+ return;
475
+ this.pasteGuard = true;
476
+ this.pasteTimer = setTimeout(() => this.clearPasteGuard(), 0);
477
+ if (this.target.pasteClipboard?.(content))
478
+ return;
479
+ const text = normalizePasteText(content.text);
480
+ if (text)
481
+ this.target.insertText(text);
482
+ }
483
+ readClipboard(data, fallback = "") {
484
+ return {
485
+ text: data?.getData("text/plain") || fallback,
486
+ html: data?.getData("text/html") ?? "",
487
+ richText: data?.getData(RICH_TEXT_MIME) ?? "",
488
+ };
489
+ }
490
+ writeClipboard(data, content) {
491
+ if (!data)
492
+ return;
493
+ data.setData("text/plain", content.text);
494
+ if (content.html)
495
+ data.setData("text/html", content.html);
496
+ if (content.richText) {
497
+ try {
498
+ data.setData(RICH_TEXT_MIME, content.richText);
499
+ }
500
+ catch { }
501
+ }
502
+ }
503
+ clearPasteGuard() {
504
+ if (this.pasteTimer !== null)
505
+ clearTimeout(this.pasteTimer);
506
+ this.pasteTimer = null;
507
+ this.pasteGuard = false;
508
+ }
509
+ rewrite(force = false) {
510
+ const mirror = this.mirror;
511
+ const dom = this.dom;
512
+ if (!mirror || !dom || !this.attached || this.composing || this.releasedComposition || this.holding > 0 || this.frozen)
513
+ return;
514
+ const sel = mirror.selection();
515
+ const held = mirrorWindow(mirror, sel.anchor, sel.head);
516
+ dom.write(held.text, force);
517
+ this.written = held;
518
+ this.writtenVersion = mirror.textVersion();
519
+ this.unaligned = true;
520
+ if (!this.isActiveElement(this.element))
521
+ return;
522
+ const shown = dom.selection();
523
+ if (force || !shown || shown.anchor !== held.anchor || shown.head !== held.head)
524
+ dom.select(held.anchor, held.head);
525
+ }
526
+ restoreSelection() {
527
+ const dom = this.dom;
528
+ const written = this.written;
529
+ if (!dom || !written || this.composing || this.releasedComposition || !this.attached || !this.isActiveElement(this.element))
530
+ return;
531
+ const shown = dom.selection();
532
+ if (!shown || shown.anchor !== written.anchor || shown.head !== written.head)
533
+ dom.select(written.anchor, written.head);
534
+ this.unaligned = true;
535
+ }
536
+ readKeyboard(text, at, input) {
537
+ const mirror = this.mirror;
538
+ const dom = this.dom;
539
+ const written = this.written;
540
+ if (!mirror || !dom || !written || this.composing || this.releasedComposition || !this.attached || this.holding > 0)
541
+ return;
542
+ if (this.released) {
543
+ this.released = false;
544
+ this.pending = null;
545
+ this.rewrite();
546
+ return;
547
+ }
548
+ if (mirror.textVersion() !== this.writtenVersion || (at !== "blur" && !this.isActiveElement(this.element)))
549
+ return;
550
+ const shown = dom.read();
551
+ const caret = dom.selection();
552
+ const pending = this.pending;
553
+ this.pending = null;
554
+ const edits = this.readBack(written, shown, caret, pending, input ? inputPayload(input) : null);
555
+ if (edits) {
556
+ if (!text)
557
+ return;
558
+ this.holding++;
559
+ try {
560
+ this.applyEdits(written, edits, caret);
561
+ }
562
+ finally {
563
+ this.holding--;
564
+ }
565
+ this.rewrite();
566
+ return;
567
+ }
568
+ if (shown !== written.text || !caret || (caret.anchor === written.anchor && caret.head === written.head))
569
+ return;
570
+ this.written = { ...written, anchor: caret.anchor, head: caret.head };
571
+ const flat = selectionToFlat(written.segments, caret.anchor, caret.head);
572
+ mirror.select(flat.anchor, flat.head);
573
+ }
574
+ readBack(written, shown, caret, pending, payload) {
575
+ const old = written.text;
576
+ const change = (e) => (e.to > e.from || e.text ? [e] : null);
577
+ if (pending && shown === old.slice(0, pending.from) + pending.text + old.slice(pending.to))
578
+ return change({ ...pending });
579
+ const from = Math.min(written.anchor, written.head);
580
+ const to = Math.max(written.anchor, written.head);
581
+ const at = caret && caret.anchor === caret.head ? caret.head : undefined;
582
+ if (payload !== null) {
583
+ const e = editByText(old, shown, payload, at, from);
584
+ if (e)
585
+ return change(e);
586
+ }
587
+ if (shown === old)
588
+ return null;
589
+ const end = shown.length - (old.length - to);
590
+ if (to > from && at === end && end >= from && shown.startsWith(old.slice(0, from)) && shown.endsWith(old.slice(to)) && !shown.slice(from, end).includes(MIRROR_GAP))
591
+ return [{ from, to, text: shown.slice(from, end) }];
592
+ const d = diffText(old, shown, from, at);
593
+ return d ? aroundGap(written, d) : null;
594
+ }
595
+ applyEdits(written, edits, caret) {
596
+ const mirror = this.mirror;
597
+ const segments = written.segments;
598
+ const flat = edits.map((e) => ({ e, from: toFlat(segments, e.from, e.to > e.from ? "start" : "end"), to: toFlat(segments, e.to, "end") }));
599
+ const after = (m) => {
600
+ let shown = 0;
601
+ let shift = 0;
602
+ for (const { e, from, to } of flat) {
603
+ const start = e.from + shown;
604
+ if (m <= start)
605
+ return toFlat(segments, m - shown, "end") + shift;
606
+ if (m < start + e.text.length)
607
+ return from + shift + (m - start);
608
+ shown += e.text.length - (e.to - e.from);
609
+ shift += e.text.length - (to - from);
610
+ }
611
+ return toFlat(segments, m - shown, "end") + shift;
612
+ };
613
+ const last = flat[flat.length - 1];
614
+ const end = last.from + last.e.text.length + flat.slice(0, -1).reduce((s, f) => s + f.e.text.length - (f.to - f.from), 0);
615
+ const anchor = caret ? after(caret.anchor) : end;
616
+ const head = caret ? after(caret.head) : end;
617
+ for (let i = flat.length - 1; i >= 0; i--) {
618
+ const { e, from, to } = flat[i];
619
+ if (i === 0)
620
+ mirror.replace(from, to, e.text, anchor, head);
621
+ else
622
+ mirror.replace(from, to, e.text, from + e.text.length, from + e.text.length);
623
+ }
624
+ }
625
+ readBefore(e, target) {
626
+ if (this.released) {
627
+ this.readKeyboard(true);
628
+ return;
629
+ }
630
+ this.frozen = true;
631
+ try {
632
+ this.readKeyboard(true);
633
+ }
634
+ finally {
635
+ this.frozen = false;
636
+ }
637
+ const deletes = e.inputType.startsWith("delete");
638
+ const data = deletes ? "" : e.data ?? e.dataTransfer?.getData("text/plain") ?? null;
639
+ this.pending = target && data !== null ? { from: target.mFrom, to: target.mTo, text: data.replace(/\r\n?/g, "\n") } : null;
640
+ }
641
+ onReleased(e) {
642
+ if (!this.mirror || !this.attached)
643
+ return;
644
+ switch (e.type) {
645
+ case "keydown":
646
+ case "beforeinput":
647
+ case "cut":
648
+ case "paste":
649
+ case "compositionstart":
650
+ this.readKeyboard(true);
651
+ this.released = true;
652
+ if (e.type === "compositionstart")
653
+ this.releasedComposition = true;
654
+ break;
655
+ case "input":
656
+ case "compositionupdate":
657
+ this.released = true;
658
+ break;
659
+ case "compositionend":
660
+ this.released = true;
661
+ this.releasedComposition = false;
662
+ break;
663
+ default:
664
+ return;
665
+ }
666
+ this.writeBackLater();
667
+ }
668
+ endReleasedComposition() {
669
+ this.releasedComposition = false;
670
+ this.released = true;
671
+ this.writeBackLater();
672
+ }
673
+ writeBackLater() {
674
+ if (this.releaseTimer !== null || this.releasedComposition)
675
+ return;
676
+ this.releaseTimer = setTimeout(() => {
677
+ this.releaseTimer = null;
678
+ this.readKeyboard(true);
679
+ }, 0);
680
+ }
681
+ sync(commit) {
682
+ if (this.syncing || !this.attached || !this.mirror)
683
+ return;
684
+ this.syncing = true;
685
+ try {
686
+ if (this.composing) {
687
+ if (commit)
688
+ this.finishComposition(this.composition?.data ?? "", true);
689
+ }
690
+ else
691
+ this.readKeyboard(false);
692
+ }
693
+ finally {
694
+ this.syncing = false;
695
+ }
696
+ }
697
+ targetOffsets(e) {
698
+ const r = e.getTargetRanges?.()[0];
699
+ if (!r || !this.dom)
700
+ return null;
701
+ const a = this.dom.offsetOf(r.startContainer, r.startOffset);
702
+ const b = this.dom.offsetOf(r.endContainer, r.endOffset);
703
+ return a === null || b === null ? null : { from: Math.min(a, b), to: Math.max(a, b) };
704
+ }
705
+ targetFlat(e) {
706
+ const written = this.written;
707
+ const at = this.targetOffsets(e);
708
+ if (!at || !written)
709
+ return null;
710
+ return { mFrom: at.from, mTo: at.to, from: toFlat(written.segments, at.from, at.to > at.from ? "start" : "end"), to: toFlat(written.segments, at.to, "end") };
711
+ }
712
+ replaceByTarget(target, data) {
713
+ const mirror = this.mirror;
714
+ if (target) {
715
+ const sel = mirror.selection();
716
+ if (target.from !== Math.min(sel.anchor, sel.head) || target.to !== Math.max(sel.anchor, sel.head)) {
717
+ const end = target.from + data.replace(/\r\n?/g, "\n").length;
718
+ mirror.replace(target.from, target.to, data, end, end);
719
+ return;
720
+ }
721
+ }
722
+ if (data)
723
+ this.target.insertText(data);
724
+ }
725
+ deleteByTarget(target, backward) {
726
+ const mirror = this.mirror;
727
+ const written = this.written;
728
+ if (!target || !written || target.to <= target.from)
729
+ return false;
730
+ const sel = mirror.selection();
731
+ if (sel.anchor !== sel.head)
732
+ return false;
733
+ const caret = written.head;
734
+ const ownM = backward ? { from: prevGraphemeStart(written.text, caret), to: caret } : { from: caret, to: nextGraphemeEnd(written.text, caret) };
735
+ const own = { from: toFlat(written.segments, ownM.from, "start"), to: toFlat(written.segments, ownM.to, "end") };
736
+ if (!(target.from <= own.from && target.to >= own.to && (target.from < own.from || target.to > own.to)))
737
+ return false;
738
+ mirror.replace(target.from, target.to, "", target.from, target.from);
739
+ return true;
740
+ }
741
+ composeAt(c, r, text) {
742
+ const from = toFlat(c.base.segments, r.from, r.to > r.from ? "start" : "end");
743
+ const to = toFlat(c.base.segments, r.to, "end");
744
+ if (from === c.range.from && to === c.range.to && text === c.text)
745
+ return;
746
+ c.range = { from, to };
747
+ c.text = text;
748
+ this.mirror.compose(from, to, text);
749
+ }
750
+ followComposition() {
751
+ const c = this.composition;
752
+ const mirror = this.mirror;
753
+ const dom = this.dom;
754
+ if (!c || !mirror || !dom || mirror.textVersion() !== c.version)
755
+ return;
756
+ const shown = dom.read();
757
+ const r = compositionRange(c.base.text, shown, c.data, c.near);
758
+ if (r)
759
+ return this.composeAt(c, r, c.data);
760
+ const d = diffText(c.base.text, shown, c.near);
761
+ if (d)
762
+ this.composeAt(c, d, d.text);
763
+ }
764
+ finishComposition(data, force) {
765
+ const c = this.composition;
766
+ this.composing = false;
767
+ this.composition = null;
768
+ this.holding++;
769
+ try {
770
+ this.commitShown(c, data, force);
771
+ }
772
+ finally {
773
+ this.holding--;
774
+ }
775
+ this.rewrite(force);
776
+ }
777
+ commitShown(c, data, force) {
778
+ const mirror = this.mirror;
779
+ const dom = this.dom;
780
+ if (!c || mirror.textVersion() !== c.version) {
781
+ const range = c ? mirror.composition() : null;
782
+ if (c && data && range && !(c.selected && c.range.from === c.start.from && c.range.to === c.start.to) && mirror.text(range.from, range.to) === data) {
783
+ this.target.compositionEnd("");
784
+ if (!force)
785
+ mirror.select(range.to, range.to);
786
+ return;
787
+ }
788
+ const r = c && !data ? compositionRange(c.base.text, dom.read(), "", c.near) : null;
789
+ const deleted = r && r.to > r.from ? range : null;
790
+ this.target.compositionEnd(data);
791
+ if (deleted && deleted.to > deleted.from)
792
+ mirror.replace(deleted.from, deleted.to, "", deleted.from, deleted.from);
793
+ return;
794
+ }
795
+ const shown = dom.read();
796
+ const caret = dom.selection();
797
+ const base = c.base;
798
+ let r = compositionRange(base.text, shown, data, c.near) ?? (data ? compositionRange(base.text, shown, "", c.near) : null);
799
+ let text = data;
800
+ if (!r) {
801
+ const d = diffText(base.text, shown, c.near);
802
+ r = d ? { from: d.from, to: d.to } : { from: c.near, to: c.near };
803
+ text = d?.text ?? "";
804
+ }
805
+ const from = toFlat(base.segments, r.from, r.to > r.from ? "start" : "end");
806
+ const to = toFlat(base.segments, r.to, "end");
807
+ if (base.text.slice(r.from, r.to) === text && !(c.selected && from === c.start.from && to === c.start.to)) {
808
+ if (c.range.from !== c.start.from || c.range.to !== c.start.to)
809
+ mirror.compose(c.start.from, c.start.to, c.text);
810
+ this.target.compositionEnd("");
811
+ if (force || !caret || shown !== base.text)
812
+ return;
813
+ const flat = selectionToFlat(base.segments, caret.anchor, caret.head);
814
+ const now = mirror.selection();
815
+ if (now.anchor !== flat.anchor || now.head !== flat.head)
816
+ mirror.select(flat.anchor, flat.head);
817
+ return;
818
+ }
819
+ if (!text) {
820
+ mirror.compose(from, to, "");
821
+ this.target.compositionEnd("");
822
+ mirror.replace(from, to, "", from, from);
823
+ return;
824
+ }
825
+ if (from !== c.range.from || to !== c.range.to || text !== c.text)
826
+ mirror.compose(from, to, text);
827
+ this.target.compositionEnd(text);
828
+ if (force || !caret || shown !== base.text.slice(0, r.from) + text + base.text.slice(r.to))
829
+ return;
830
+ const shift = text.length - (to - from);
831
+ const end = r.from + text.length;
832
+ const replaced = r.to - r.from;
833
+ const rFrom = r.from;
834
+ const after = (m) => (m <= rFrom ? toFlat(base.segments, m, "end") : m >= end ? toFlat(base.segments, m - text.length + replaced, "end") + shift : from + (m - rFrom));
835
+ const anchor = after(caret.anchor);
836
+ const head = after(caret.head);
837
+ const now = mirror.selection();
838
+ if (now.anchor !== anchor || now.head !== head)
839
+ mirror.select(anchor, head);
840
+ }
841
+ }