@lofcz/edix 0.3.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.
package/lib/index.cjs ADDED
@@ -0,0 +1,720 @@
1
+ const t = Math.min, {keys: e, is: n} = Object, r = t => "string" == typeof t, o = t => "function" == typeof t, s = o(queueMicrotask) ? queueMicrotask : t => {
2
+ Promise.resolve().then(t);
3
+ }, i = (e, n) => {
4
+ const r = t(e.length, n.length);
5
+ for (let t = 0; t < r; t++) {
6
+ const r = e[t], o = n[t];
7
+ if (r < o) return -1;
8
+ if (r > o) return 1;
9
+ }
10
+ return 0;
11
+ }, c = ([t, e], [n, r]) => {
12
+ const o = i(t, n);
13
+ return 0 === o ? e === r ? 0 : e < r ? -1 : 1 : o;
14
+ }, u = ([t, e]) => 1 === c(t, e) ? [ e, t ] : [ t, e ], f = (t, e = t => y(t) ? t.text : "") => t.children.reduce((t, n, r) => (0 !== r && (t += "\n"),
15
+ t + n.reduce((t, n) => t + e(n), "")), ""), l = (t, e) => t.split("\n").map(t => [ {
16
+ ...e,
17
+ text: t
18
+ } ]), a = "delete", p = "insert_text", d = "insert_node", h = "set_attr", x = ({type: t}) => t === d || t === h;
19
+
20
+ class m {
21
+ t;
22
+ selection;
23
+ constructor(t) {
24
+ this.t = t ? t.slice() : [];
25
+ }
26
+ get ops() {
27
+ return this.t;
28
+ }
29
+ insertText(t, e) {
30
+ return this.t.push({
31
+ type: p,
32
+ at: t,
33
+ text: e
34
+ }), this;
35
+ }
36
+ insertFragment(t, e) {
37
+ return this.t.push({
38
+ type: d,
39
+ at: t,
40
+ fragment: e
41
+ }), this;
42
+ }
43
+ delete(t, e) {
44
+ return this.t.push({
45
+ type: a,
46
+ start: t,
47
+ end: e
48
+ }), this;
49
+ }
50
+ attr(t, e, n, r) {
51
+ return this.t.push({
52
+ type: h,
53
+ start: t,
54
+ end: e,
55
+ key: n,
56
+ value: r
57
+ }), this;
58
+ }
59
+ transform(t) {
60
+ return this.t.reduce((t, e) => C(t, e), t);
61
+ }
62
+ }
63
+
64
+ const y = t => "text" in t, g = (t, r) => {
65
+ const o = e(t);
66
+ return o.length === e(r).length && o.every(e => e in r && ("text" === e || n(t[e], r[e])));
67
+ }, w = t => y(t) ? t.text.length : 1, k = t => t.reduce((t, e) => t + w(e), 0), b = (...t) => t.reduce((t, e) => (((t, e) => {
68
+ if (e.length) {
69
+ const n = t.length;
70
+ t.push(...e), n && ((t, e = 0, n = t.length - 1) => {
71
+ let r = e + 1;
72
+ for (;r <= n; ) {
73
+ const e = t[r - 1], o = t[r];
74
+ y(e) && y(o) && g(e, o) ? (t[r - 1] = {
75
+ ...e,
76
+ text: e.text + o.text
77
+ }, t.splice(r, 1), n--) : r++;
78
+ }
79
+ for (r = e; r <= n; ) {
80
+ const e = t[r];
81
+ y(e) && !e.text && t.length > 1 ? (t.splice(r, 1), n--) : r++;
82
+ }
83
+ })(t, n - 1, n);
84
+ }
85
+ })(t, e), t), []), E = (t, e) => {
86
+ for (let n = 0; n < t.length; n++) {
87
+ const r = t[n], o = w(r);
88
+ if (o > e) {
89
+ const o = t.slice(0, n), s = t.slice(n + 1);
90
+ if (y(r)) {
91
+ const t = r.text.slice(0, e), n = r.text.slice(e);
92
+ !t && o.length || o.push({
93
+ ...r,
94
+ text: t
95
+ }), !n && s.length || s.unshift({
96
+ ...r,
97
+ text: n
98
+ });
99
+ } else s.unshift(r);
100
+ return [ o, s ];
101
+ }
102
+ e -= o;
103
+ }
104
+ return [ t, [] ];
105
+ }, D = t => t.length ? t[0] : 0, O = (t, e) => t.children[D(e)], S = (t, e) => [ D(t) + e ], _ = (t, e, n, o) => {
106
+ const [s, i] = e, [u, f] = n, [a, p] = E(O(t, s), i), d = -1 === c(e, n) ? E(O(t, u), f)[1] : p;
107
+ if (r(o)) {
108
+ const t = a.length;
109
+ let e;
110
+ if (t) {
111
+ const n = a[t - 1];
112
+ y(n) && (e = n);
113
+ }
114
+ o = l(o, e);
115
+ }
116
+ let h;
117
+ o.length ? (h = o.slice(), h[h.length - 1] = b(h[h.length - 1], d)) : h = [ d ],
118
+ h[0] = b(a, h[0]);
119
+ const x = t.children.slice();
120
+ return x.splice(D(s), D(u) - D(s) + 1, ...h), {
121
+ ...t,
122
+ children: x
123
+ };
124
+ }, v = (t, e, n) => {
125
+ if (-1 !== c(e, n)) return [];
126
+ const r = t.children.slice(D(e[0]), D(n[0]) + 1), o = r.length - 1;
127
+ return r[o] = E(r[o], n[1])[0], r[0] = E(r[0], e[1])[1], r;
128
+ }, M = (t, [e, n]) => (!e.length || e[0] >= 0 && e[0] < t.children.length) && n >= 0 && n <= k(O(t, e)), C = (t, e) => {
129
+ switch (e.type) {
130
+ case a:
131
+ {
132
+ const {start: n, end: r} = e;
133
+ if (-1 !== c(t, n)) return -1 === c(r, t) ? [ S(t[0], D(n[0]) - D(r[0])), t[1] + (0 === i(r[0], t[0]) ? n[1] - r[1] : 0) ] : n;
134
+ break;
135
+ }
136
+
137
+ case p:
138
+ case d:
139
+ {
140
+ const n = e.at, r = e.type === p ? l(e.text) : e.fragment, o = r.length, s = o - 1;
141
+ if (-1 !== c(t, n)) return [ S(t[0], s), t[1] + (0 === i(t[0], n[0]) ? k(r[o - 1]) - (0 === s ? 0 : n[1]) : 0) ];
142
+ break;
143
+ }
144
+ }
145
+ return t;
146
+ }, A = (t, e) => [ C(t[0], e), C(t[1], e) ], I = (t, e, n) => {
147
+ switch (n.type) {
148
+ case a:
149
+ {
150
+ const {start: r, end: o} = n;
151
+ M(t, r) && M(t, o) && -1 === c(r, o) && (t = _(t, r, o, []), e = A(e, n));
152
+ break;
153
+ }
154
+
155
+ case p:
156
+ {
157
+ const {at: r, text: o} = n;
158
+ M(t, r) && o && (t = _(t, r, r, o), e = A(e, n));
159
+ break;
160
+ }
161
+
162
+ case d:
163
+ {
164
+ const {at: r, fragment: o} = n;
165
+ M(t, r) && o.length && (t = _(t, r, r, o), e = A(e, n));
166
+ break;
167
+ }
168
+
169
+ case h:
170
+ {
171
+ const {start: e, end: r, key: o, value: s} = n;
172
+ M(t, e) && M(t, r) && -1 === c(e, r) && (t = _(t, e, r, v(t, e, r).map(t => t.map(t => y(t) ? {
173
+ ...t,
174
+ [o]: s
175
+ } : t))));
176
+ break;
177
+ }
178
+ }
179
+ return [ t, e ];
180
+ }, F = t => "at" in t ? [ t.at, t.at ] : [ t.start, t.end ];
181
+
182
+ let H = null, P = null, R = null, B = null;
183
+
184
+ const T = t => 1 === t.nodeType, V = t => 8 === t.nodeType, L = () => P, N = () => {
185
+ const t = q();
186
+ return 1 === t ? P.data.length : 2 === t ? 1 : 0;
187
+ }, q = () => {
188
+ if (null != R) return R;
189
+ if (P) if ((t => 3 === t.nodeType)(P)) {
190
+ const t = P.data;
191
+ if (t) return R = "\n" === t ? G() ? 3 : 6 : 1;
192
+ } else if (T(P)) {
193
+ if ("BR" === P.tagName) return R = G() ? 3 : 5;
194
+ if (B.o(P)) return R = 2;
195
+ if (B.i(P)) return R = 4;
196
+ }
197
+ return R = 0;
198
+ }, z = () => (R = null, P = H.nextNode()), J = () => {
199
+ for (;(R = null) || (P = H.nextSibling()); ) if (4 === q()) return;
200
+ }, U = () => {
201
+ for (;(R = null) || (P = H.parentNode()); ) if (4 === q()) return;
202
+ }, G = () => {
203
+ const t = P.parentNode;
204
+ return W(() => {
205
+ for (;z(); ) {
206
+ if (q()) return !0;
207
+ if (!t.contains(P)) break;
208
+ }
209
+ return !1;
210
+ });
211
+ }, j = () => {
212
+ for (;;) {
213
+ if (2 === q()) {
214
+ const t = P;
215
+ for (;z() && t.contains(P); ) ;
216
+ } else z();
217
+ if (!P) break;
218
+ const t = q();
219
+ if (t) return t;
220
+ }
221
+ }, W = (t, e, n, r) => {
222
+ const o = B, s = H, i = P, c = R;
223
+ try {
224
+ return n && (B = n, H = B.u.createTreeWalker(e, 5)), r && (H.currentNode = P = r),
225
+ t();
226
+ } finally {
227
+ B = o, H = s, P = i, R = c, H && i && (H.currentNode = i);
228
+ }
229
+ }, X = new Set([ "DIV", "H1", "H2", "H3", "H4", "H5", "H6", "P", "PRE", "LI", "DT", "DD", "TR" ]), Y = t => X.has(t.tagName), K = new Set([ "EMBED", "IMG", "PICTURE", "AUDIO", "VIDEO", "SVG", "CANVAS", "MATH", "IFRAME", "OBJECT" ]), Q = t => "false" === t.contentEditable || K.has(t.tagName), Z = (t, e) => t.compareDocumentPosition(e), $ = t => t.ownerDocument, tt = t => $(t).getSelection(), et = (t, e) => {
230
+ if (t.rangeCount) {
231
+ const n = t.getRangeAt(0);
232
+ if (e.contains(n.commonAncestorContainer)) return n;
233
+ }
234
+ }, nt = (t, e, n, r) => {
235
+ const o = tt(t);
236
+ return !(!n && !et(o, t) || (o.removeAllRanges(), o.addRange(e), r && (o.collapseToEnd(),
237
+ o.extend(e.startContainer, e.startOffset)), 0));
238
+ }, rt = (t, e, [n, r], o, s) => {
239
+ const i = c(n, r), u = 0 === i, f = 1 === i, l = f ? r : n, a = f ? n : r;
240
+ if (0 === l[0].length && 0 === l[1] && u && !e.hasChildNodes()) {
241
+ const n = t.createRange();
242
+ return n.setStart(e, 0), n.setEnd(e, 0), nt(e, n, s);
243
+ }
244
+ const p = ot(e, l, o);
245
+ if (!p) return !1;
246
+ const d = u ? p : ot(e, a, o);
247
+ if (!d) return !1;
248
+ const h = t.createRange(), [x, m] = p, [y, g] = d;
249
+ return T(x) ? m < 1 ? h.setStartBefore(x) : h.setStartAfter(x) : h.setStart(x, m),
250
+ T(y) ? g < 1 ? h.setEndBefore(y) : h.setEndAfter(y) : h.setEnd(y, g), nt(e, h, s, f);
251
+ }, ot = (t, [e, n], r) => W(() => {
252
+ let t, r = 0;
253
+ for (;t = j(); ) if (4 === t) {
254
+ if (r < e.length) for (let t = e[r++]; t > 0; t--) J();
255
+ } else {
256
+ const t = N();
257
+ if (n <= t) return [ L(), n ];
258
+ n -= t;
259
+ }
260
+ }, t, r), st = (e, n, r, o) => {
261
+ let s = !0;
262
+ if (e === n && !n.hasChildNodes()) return [ [], 0 ];
263
+ if (T(n) && !o.o(n) && n.hasChildNodes()) {
264
+ const e = t(r, n.childNodes.length - 1);
265
+ n = n.childNodes[e], s = e === r, r = 0;
266
+ }
267
+ return W(() => {
268
+ 4 !== q() && U();
269
+ const t = W(() => {
270
+ const t = [];
271
+ let n;
272
+ for (;(n = L()) && n !== e; ) t.unshift(n), U();
273
+ if (!t.length) return [];
274
+ let r = 0, o = t[t.length - 1];
275
+ for (;o = o.previousElementSibling; ) r++;
276
+ return [ r ];
277
+ });
278
+ let o = 0;
279
+ for (;j(); ) {
280
+ const t = Z(n, L());
281
+ if (0 === t || 16 & t) {
282
+ if (s) break;
283
+ } else if (4 & t) break;
284
+ o += N();
285
+ }
286
+ return [ t, o + r ];
287
+ }, e, o, n);
288
+ }, it = (t, e, {startOffset: n, startContainer: r, endOffset: o, endContainer: s}) => {
289
+ const i = st(t, r, n, e);
290
+ return [ i, r === s && n === o ? i : st(t, s, o, e) ];
291
+ }, ct = t => (e, n) => {
292
+ e.setData("text/plain", f({
293
+ children: n
294
+ }, t));
295
+ }, ut = "application/x-edix-editor", ft = (t, e, {mod: n, shift: r = !1, alt: o = !1} = {}) => (t = t.toLowerCase(),
296
+ s => {
297
+ if (s.key.toLowerCase() === t && (!n || s.ctrlKey || s.metaKey) && r === s.shiftKey && o === s.altKey) return e(s),
298
+ !0;
299
+ }), lt = () => {}, at = ({doc: t, readonly: e = !1, schema: i, plugins: f, keyboard: l, copy: a = [ ct() ], paste: p = [ t => t.getData("text/plain") ], isBlock: d = Y, autoScroll: h = !1, onChange: g, onError: w = console.error}) => {
300
+ let k = [ [ [], 0 ], [ [], 0 ] ], b = lt, E = null, D = 0;
301
+ const O = () => {
302
+ if (E && !D) {
303
+ const t = E;
304
+ D = requestAnimationFrame(() => {
305
+ D = 0, t.scrollTop = t.scrollHeight;
306
+ });
307
+ }
308
+ }, S = t => {
309
+ for (const e of t.children) for (const t of e) if (!y(t) || t.text.length > 0) return !1;
310
+ return !0;
311
+ };
312
+ let _ = S(t);
313
+ const A = (t, e) => {
314
+ if (!i) return e("An unsafe operation was detected. We recommend using schema option."),
315
+ !0;
316
+ const n = i["~standard"].validate(t);
317
+ if (n instanceof Promise) e("async validate is not supported."); else {
318
+ if (!n.issues) return !0;
319
+ e(n.issues.map(t => t.message).join("\n"));
320
+ }
321
+ return !1;
322
+ };
323
+ let H;
324
+ if (!A(t, t => {
325
+ H = t;
326
+ }) && H) throw Error(H);
327
+ const P = [ ft("z", () => {
328
+ if (!e) {
329
+ const e = z.undo();
330
+ e && (t = e[0], _ = S(t), N(e[1]), g(t), h && O());
331
+ }
332
+ }, {
333
+ mod: !0
334
+ }), ft("z", () => {
335
+ if (!e) {
336
+ const e = z.redo();
337
+ e && (t = e[0], _ = S(t), N(e[1]), g(t), h && O());
338
+ }
339
+ }, {
340
+ mod: !0,
341
+ shift: !0
342
+ }) ];
343
+ l && P.push(...l);
344
+ const R = [], B = [];
345
+ f && (f.forEach(({apply: t, mount: e}) => {
346
+ t && R.push(t), e && B.push(e);
347
+ }), f = void 0);
348
+ const T = [], V = (t, n) => {
349
+ if (!e) {
350
+ const e = !T.length;
351
+ T.unshift(t), n ? L() : e && s(L);
352
+ }
353
+ }, L = () => {
354
+ if (T.length) {
355
+ const e = t, r = [], o = R.length;
356
+ let s;
357
+ for (;s = T.pop(); ) {
358
+ for (let e of s.ops) {
359
+ let n = 0;
360
+ const s = () => {
361
+ if (n < o) {
362
+ const t = n;
363
+ R[n](e, i), t === n && i();
364
+ } else if (n === o) {
365
+ n++;
366
+ try {
367
+ const [n, o] = I(t, k, e);
368
+ x(e) && !A(n, w) || (t = n, k = o, r.push(e));
369
+ } catch (t) {
370
+ w("rollback operation: " + t);
371
+ }
372
+ }
373
+ }, i = t => {
374
+ t && (e = t), n++, s();
375
+ };
376
+ s();
377
+ }
378
+ s.selection && N(s.selection);
379
+ }
380
+ n(e, t) || (_ = S(t), z.change(t, r), g(t), h && O());
381
+ }
382
+ }, N = e => {
383
+ ((t, [e, n]) => M(t, e) && M(t, n))(t, e) && (k = e);
384
+ }, q = {
385
+ get doc() {
386
+ return t;
387
+ },
388
+ get isEmpty() {
389
+ return _;
390
+ },
391
+ get selection() {
392
+ return k;
393
+ },
394
+ set selection(t) {
395
+ N(t);
396
+ },
397
+ get readonly() {
398
+ return e;
399
+ },
400
+ set readonly(t) {
401
+ e = t, b();
402
+ },
403
+ get autoScroll() {
404
+ return h;
405
+ },
406
+ set autoScroll(t) {
407
+ h = t;
408
+ },
409
+ apply: (t, ...e) => (o(t) ? t.call(q, ...e) : V(t, e[0]), q),
410
+ input: n => {
411
+ if (E = n, !window.InputEvent || !o(InputEvent.prototype.getTargetRanges)) return w("beforeinput event is not supported."),
412
+ lt;
413
+ const {contentEditable: s, role: i, ariaMultiLine: f, ariaReadOnly: l} = n, h = n.style.whiteSpace;
414
+ n.role = "textbox", n.style.whiteSpace = "pre-wrap", n.ariaMultiLine = "true";
415
+ let x = !1, y = !1, g = null, O = !1, S = !1, _ = !1;
416
+ const M = $(n), C = {
417
+ u: M,
418
+ i: d,
419
+ o: Q
420
+ };
421
+ b = () => {
422
+ n.contentEditable = e ? "false" : "true", n.ariaReadOnly = e ? "true" : null;
423
+ }, b();
424
+ const A = t => {
425
+ for (const e of p) {
426
+ const n = e(t, C);
427
+ if (n) return n;
428
+ }
429
+ w("failed to serialize pasted data");
430
+ }, I = ((t, e) => {
431
+ let n = !1;
432
+ const r = [], o = t => {
433
+ n && r.push(...t);
434
+ }, s = new MutationObserver(t => {
435
+ o(t), n || e();
436
+ }), i = () => {
437
+ o(s.takeRecords());
438
+ };
439
+ return s.observe(t, {
440
+ characterData: !0,
441
+ characterDataOldValue: !0,
442
+ childList: !0,
443
+ subtree: !0
444
+ }), {
445
+ l(t) {
446
+ !n && t && i(), n = t;
447
+ },
448
+ p: () => (i(), r.splice(0)),
449
+ h() {
450
+ r.splice(0), s.disconnect();
451
+ }
452
+ };
453
+ })(n, () => {
454
+ rt(M, n, k, C);
455
+ }), F = () => {
456
+ N(((t, e) => {
457
+ const n = tt(t), r = et(n, t);
458
+ if (!r) return [ [ [], 0 ], [ [], 0 ] ];
459
+ const o = it(t, e, r), s = Z(n.anchorNode, n.focusNode);
460
+ return (0 === s ? n.anchorOffset > n.focusOffset : 2 & s) ? [ o[1], o[0] ] : o;
461
+ })(n, C));
462
+ }, H = () => {
463
+ const t = I.p();
464
+ if (I.l(!1), t.length) {
465
+ let e;
466
+ for (;e = t.pop(); ) if ("childList" === e.type) {
467
+ const {target: t, removedNodes: n, addedNodes: r, nextSibling: o} = e;
468
+ for (let e = n.length - 1; e >= 0; e--) t.insertBefore(n[e], o);
469
+ for (let e = r.length - 1; e >= 0; e--) t.removeChild(r[e]);
470
+ } else e.target.nodeValue = e.oldValue;
471
+ I.p(), y = rt(M, n, k, C, !0);
472
+ }
473
+ g && (N(g[1]), V(g[0]), g = null), O = !1;
474
+ }, R = t => {
475
+ if (!O) for (const e of P) if (e(t)) return t.preventDefault(), void I.l(!1);
476
+ }, T = () => {
477
+ O || H();
478
+ }, L = t => {
479
+ t.preventDefault();
480
+ const e = t.inputType;
481
+ if (e.startsWith("format")) return;
482
+ if ("historyUndo" === e || "historyRedo" === e) return;
483
+ O ? I.l(!0) : F();
484
+ const r = t.getTargetRanges()[0];
485
+ if (r) {
486
+ const o = it(n, C, r);
487
+ let s, i = "insertParagraph" === e || "insertLineBreak" === e ? "\n" : t.data;
488
+ if (null == i) {
489
+ const e = t.dataTransfer;
490
+ e && (i = e.getData("text/plain"));
491
+ }
492
+ g || (g = [ new m, k ]), s = g[0], 0 !== c(...o) && s.delete(...o), i && s.insertText(o[0], i);
493
+ }
494
+ O || H();
495
+ }, q = () => {
496
+ O || F(), O = !0;
497
+ }, z = () => {
498
+ H();
499
+ }, J = () => {
500
+ S = !0, F();
501
+ }, U = () => {
502
+ S = !1;
503
+ }, G = () => {
504
+ y ? y = !1 : !S || O || _ || F();
505
+ }, j = e => {
506
+ F(), 0 !== c(...k) && ((t, e) => {
507
+ for (const r of a) r(t, e, n);
508
+ })(e, v(t, ...u(k)));
509
+ }, W = t => {
510
+ t.preventDefault(), j(t.clipboardData);
511
+ }, X = t => {
512
+ t.preventDefault(), e || (j(t.clipboardData), V((new m).delete(...u(k))));
513
+ }, Y = t => {
514
+ t.preventDefault();
515
+ const e = A(t.clipboardData);
516
+ if (e) {
517
+ const [t, n] = u(k), o = (new m).delete(t, n);
518
+ r(e) ? o.insertText(t, e) : o.insertFragment(t, e), V(o);
519
+ }
520
+ }, K = t => {
521
+ t.preventDefault();
522
+ const e = t.dataTransfer, o = ((t, e, {clientX: n, clientY: r}, o) => {
523
+ if (t.caretPositionFromPoint) {
524
+ const s = t.caretPositionFromPoint(n, r);
525
+ if (s) return st(e, s.offsetNode, s.offset, o);
526
+ } else if (t.caretRangeFromPoint) {
527
+ const s = t.caretRangeFromPoint(n, r);
528
+ if (s) return st(e, s.startContainer, s.startOffset, o);
529
+ }
530
+ })(M, n, t, C);
531
+ if (e && o) {
532
+ const t = new m;
533
+ _ && t.delete(...u(k));
534
+ const s = A(e);
535
+ if (s) {
536
+ const e = t.transform(o);
537
+ r(s) ? t.insertText(e, s) : t.insertFragment(e, s), t.selection = [ e, t.transform(o) ];
538
+ }
539
+ V(t), n.focus({
540
+ preventScroll: !0
541
+ });
542
+ }
543
+ }, nt = t => {
544
+ _ = !0, j(t.dataTransfer);
545
+ }, ot = () => {
546
+ _ = !1;
547
+ };
548
+ M.addEventListener("selectionchange", G), n.addEventListener("keydown", R), n.addEventListener("input", T),
549
+ n.addEventListener("beforeinput", L), n.addEventListener("compositionstart", q),
550
+ n.addEventListener("compositionend", z), n.addEventListener("focus", J), n.addEventListener("blur", U),
551
+ n.addEventListener("copy", W), n.addEventListener("cut", X), n.addEventListener("paste", Y),
552
+ n.addEventListener("drop", K), n.addEventListener("dragstart", nt), n.addEventListener("dragend", ot);
553
+ const ct = [];
554
+ return B.forEach(t => {
555
+ const e = t(n);
556
+ e && ct.push(e);
557
+ }), () => {
558
+ x || (x = !0, D && (cancelAnimationFrame(D), D = 0), E = null, b = lt, n.contentEditable = s,
559
+ n.role = i, n.ariaMultiLine = f, n.ariaReadOnly = l, n.style.whiteSpace = h, I.h(),
560
+ M.removeEventListener("selectionchange", G), n.removeEventListener("keydown", R),
561
+ n.removeEventListener("input", T), n.removeEventListener("beforeinput", L), n.removeEventListener("compositionstart", q),
562
+ n.removeEventListener("compositionend", z), n.removeEventListener("focus", J), n.removeEventListener("blur", U),
563
+ n.removeEventListener("copy", W), n.removeEventListener("cut", X), n.removeEventListener("paste", Y),
564
+ n.removeEventListener("drop", K), n.removeEventListener("dragstart", nt), n.removeEventListener("dragend", ot),
565
+ ct.forEach(t => {
566
+ t();
567
+ }));
568
+ };
569
+ }
570
+ }, z = (t => {
571
+ let e = 0, n = 0;
572
+ const r = Date.now, o = [ [ t, [] ] ], s = () => o[e];
573
+ return {
574
+ change: (t, s) => {
575
+ const i = r();
576
+ (0 === e || i - n >= 500) && (e++, e >= o.length ? o.push([ t, [] ]) : o[e][1].splice(0)),
577
+ n = i, o[e][0] = t, o[e][1].push(...s), o.splice(e + 1), e > 500 && (e--, o.shift());
578
+ },
579
+ undo: () => {
580
+ if (e > 0) {
581
+ const t = s()[1];
582
+ return e--, [ s()[0], F(t[0]) ];
583
+ }
584
+ },
585
+ redo: () => {
586
+ if (e < o.length - 1) {
587
+ e++;
588
+ const [t, n] = s(), r = n[n.length - 1], o = F(r);
589
+ return [ t, [ C(o[0], r), C(o[1], r) ] ];
590
+ }
591
+ }
592
+ };
593
+ })(t);
594
+ return q;
595
+ }, pt = () => ({
596
+ mount: t => {
597
+ t.ariaMultiLine = null;
598
+ },
599
+ apply: (t, e) => {
600
+ "insert_text" === t.type ? t = {
601
+ ...t,
602
+ text: t.text.replaceAll("\n", "")
603
+ } : "insert_node" === t.type && (t = {
604
+ ...t,
605
+ fragment: [ b(...t.fragment) ]
606
+ }), e(t);
607
+ }
608
+ }), dt = t => {
609
+ let e = "";
610
+ for (let n = 0; n < t.length; n++) {
611
+ const r = t[n];
612
+ y(r) && (e += r.text);
613
+ }
614
+ return e;
615
+ };
616
+
617
+ exports.Delete = function(t = u(this.selection)) {
618
+ this.apply((new m).delete(...t));
619
+ }, exports.Format = function(t, e, n = u(this.selection)) {
620
+ this.apply((new m).attr(...n, t, e));
621
+ }, exports.InsertNode = function(t, e = this.selection[0]) {
622
+ this.apply((new m).insertFragment(e, [ [ t ] ]));
623
+ }, exports.InsertNodes = function(t, e = this.selection[0], n = !0) {
624
+ const r = (new m).insertFragment(e, [ t ]);
625
+ if (n) {
626
+ const t = r.transform(e);
627
+ r.selection = [ t, t ];
628
+ }
629
+ this.apply(r);
630
+ }, exports.InsertText = function(t, e = this.selection[0]) {
631
+ this.apply((new m).insertText(e, t));
632
+ }, exports.ReplaceAll = function(t) {
633
+ const e = this.doc;
634
+ this.apply((new m).delete([ [], 0 ], [ [ e.children.length - 1 ], k(e.children[e.children.length - 1]) ]).insertText([ [], 0 ], t));
635
+ }, exports.ReplaceText = function(t) {
636
+ const [e, n] = u(this.selection);
637
+ this.apply((new m).delete(e, n).insertText(e, t));
638
+ }, exports.ToggleFormat = function(t, e = u(this.selection)) {
639
+ const n = v(this.doc, ...e).flatMap(t => t.filter(y));
640
+ n.length && this.apply((new m).attr(...e, t, !!n.some(e => !e[t])));
641
+ }, exports.Transaction = m, exports.createEditor = at, exports.createPlainEditor = ({text: t, singleline: e, plugins: n = [], onChange: r, ...o}) => {
642
+ const s = [ ...n ];
643
+ e && s.unshift(pt());
644
+ const i = l(t);
645
+ let c = i;
646
+ return at({
647
+ ...o,
648
+ doc: {
649
+ children: i
650
+ },
651
+ plugins: s,
652
+ onChange: t => {
653
+ const e = ((t, e) => {
654
+ const n = t.length, r = e.length, o = Math.min(n, r);
655
+ let s = 0;
656
+ for (;s < o && t[s] === e[s]; ) s++;
657
+ let i = n, c = r;
658
+ for (;i > s && c > s && t[i - 1] === e[c - 1]; ) i--, c--;
659
+ const u = c - s, f = Array(u);
660
+ for (let t = 0; t < u; t++) f[t] = dt(e[s + t]);
661
+ return {
662
+ start: s,
663
+ oldCount: i - s,
664
+ newCount: u,
665
+ lines: f
666
+ };
667
+ })(c, t.children);
668
+ c = t.children, r(f(t), e);
669
+ }
670
+ });
671
+ }, exports.filePaste = t => e => {
672
+ for (const n of e.items) if ("file" === n.kind) {
673
+ const e = t[n.type];
674
+ if (e) {
675
+ const t = n.getAsFile();
676
+ if (t) return [ [ e(t) ] ];
677
+ }
678
+ }
679
+ return null;
680
+ }, exports.hotkey = ft, exports.htmlCopy = () => (t, e, n) => {
681
+ const r = document.createElement("div");
682
+ r.appendChild(et(tt(n), n).cloneContents()), t.setData("text/html", r.innerHTML);
683
+ }, exports.htmlPaste = (t, e = []) => (n, r) => {
684
+ const o = n.getData("text/html");
685
+ if (o) {
686
+ let n = (new DOMParser).parseFromString(o, "text/html").body, s = !1;
687
+ for (const t of [ ...n.childNodes ]) V(t) ? "StartFragment" === t.data ? (s = !0,
688
+ n = new DocumentFragment) : "EndFragment" === t.data && (s = !1) : s && n.appendChild(t);
689
+ return ((t, e, n, r) => W(() => {
690
+ let t, e = null, o = "", s = !1;
691
+ const i = [], c = () => {
692
+ o && (e || (e = []), e.push(n(o)), o = "");
693
+ }, u = () => {
694
+ c(), !e && s && (e = []), e && i.push(e), e = null, s = !1;
695
+ };
696
+ for (;t = j(); ) if (4 === t) u(); else if (s = !0, 1 === t) o += L().data; else if (2 === t) {
697
+ c();
698
+ const t = r(L());
699
+ t && e.push(t);
700
+ } else 3 === t && u();
701
+ return u(), i.length || i.push([]), i;
702
+ }, t, e))(n, r, t, t => {
703
+ for (const n of e) {
704
+ const e = n(t);
705
+ if (e) return e;
706
+ }
707
+ });
708
+ }
709
+ return null;
710
+ }, exports.internalCopy = ({key: t = ut} = {}) => (e, n) => {
711
+ e.setData(t, JSON.stringify(n));
712
+ }, exports.internalPaste = ({key: t = ut} = {}) => e => {
713
+ try {
714
+ return JSON.parse(e.getData(t));
715
+ } catch (t) {
716
+ return null;
717
+ }
718
+ }, exports.plainCopy = ct, exports.plainPaste = () => t => t.getData("text/plain"),
719
+ exports.singlelinePlugin = pt;
720
+ //# sourceMappingURL=index.cjs.map