editate 0.6.0 → 0.6.2

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 CHANGED
@@ -12,7 +12,7 @@ const t = ([t, e]) => t > e ? [ e, t ] : [ t, e ], e = ([t, e]) => t === e, n =
12
12
  })), f = "delete", u = "insert_text", d = "insert_node", p = "set_attr", y = "set_node_attr", h = t => "text" in t, x = t => "children" in t, g = (t, e) => {
13
13
  const n = r(t);
14
14
  return n.length === r(e).length && n.every(n => n in e && ("text" === n || o(t[n], e[n])));
15
- }, k = new WeakMap, _ = ({children: t}, e = 0, n = t.length) => {
15
+ }, _ = new WeakMap, k = ({children: t}, e = 0, n = t.length) => {
16
16
  let r = 0, o = 0;
17
17
  for (let s = e; s < n; s++) {
18
18
  const e = t[s];
@@ -21,25 +21,18 @@ const t = ([t, e]) => t > e ? [ e, t ] : [ t, e ], e = ([t, e]) => t === e, n =
21
21
  return r;
22
22
  }, b = t => {
23
23
  if (x(t)) {
24
- let e = k.get(t);
25
- return null == e && k.set(t, e = _(t)), e;
24
+ let e = _.get(t);
25
+ return null == e && _.set(t, e = k(t)), e;
26
26
  }
27
27
  return h(t) ? t.text.length : 1;
28
28
  }, m = (t, [e, n]) => {
29
29
  let r = 0;
30
- for (const n of e) r += _(t, 0, n), 0 !== n && r++, t = t.children[n];
30
+ for (const n of e) r += k(t, 0, n), 0 !== n && r++, t = t.children[n];
31
31
  return r + n;
32
- }, w = (t, e) => {
33
- const n = [];
34
- for (;t; ) {
35
- const r = A(t, e);
36
- if (!r) break;
37
- const o = r.t, s = r.o;
38
- if (!x(s)) break;
39
- e = r.i, n.push(o), t = s;
40
- }
41
- return [ n, e ];
42
- }, v = (t, [e, n]) => [ m(t, e), m(t, n) ], M = (t, [e, n]) => [ w(t, e), w(t, n) ], S = (t, e = 0, n = t.length - 1) => {
32
+ }, v = (t, e) => {
33
+ const n = I(t, e);
34
+ return [ n.t, n.o ];
35
+ }, w = (t, [e, n]) => [ m(t, e), m(t, n) ], M = (t, [e, n]) => [ v(t, e), v(t, n) ], S = (t, e = 0, n = t.length - 1) => {
43
36
  let r = e + 1;
44
37
  for (;r <= n; ) {
45
38
  const e = t[r - 1], o = t[r];
@@ -71,19 +64,33 @@ const t = ([t, e]) => t > e ? [ e, t ] : [ t, e ], e = ([t, e]) => t === e, n =
71
64
  const r = t[n];
72
65
  let o = b(r);
73
66
  if (x(r) && o++, o > e) return {
74
- o: r,
75
- t: n,
76
- i: e
67
+ i: r,
68
+ l: n,
69
+ o: e
77
70
  };
78
71
  e -= o;
79
72
  }
80
73
  return null;
81
74
  }, I = (t, e) => {
75
+ const n = [];
76
+ for (;t; ) {
77
+ const r = A(t, e);
78
+ if (!r) break;
79
+ const o = r.i;
80
+ if (!x(o)) break;
81
+ e = r.o, t = o, n.push(r.l);
82
+ }
83
+ return {
84
+ i: t,
85
+ t: n,
86
+ o: e
87
+ };
88
+ }, P = (t, e) => {
82
89
  const n = t.children, r = A(t, e);
83
90
  if (r) {
84
- const {o: e, i: o, t: s} = r;
91
+ const {i: e, o, l: s} = r;
85
92
  if (x(e)) {
86
- const [r, c] = I(e, o), i = n.slice(0, s), a = n.slice(s + 1);
93
+ const [r, c] = P(e, o), i = n.slice(0, s), a = n.slice(s + 1);
87
94
  return i.push(r), a.unshift(c), [ {
88
95
  ...t,
89
96
  children: i
@@ -117,9 +124,6 @@ const t = ([t, e]) => t > e ? [ e, t ] : [ t, e ], e = ([t, e]) => t === e, n =
117
124
  ...t,
118
125
  children: []
119
126
  } ];
120
- }, P = (t, e) => {
121
- for (let n = 0; n < e.length; n++) t = t.children[e[n]];
122
- return t;
123
127
  }, C = (t, e, n, r = 0) => {
124
128
  if (r < e.length) {
125
129
  const o = e[r];
@@ -133,12 +137,12 @@ const t = ([t, e]) => t > e ? [ e, t ] : [ t, e ], e = ([t, e]) => t === e, n =
133
137
  }
134
138
  return n;
135
139
  }, H = (t, e, n, r) => {
136
- const [o, s] = I(t, e), c = e < n ? I(t, n)[1] : s, i = o.children.slice();
140
+ const [o, s] = P(t, e), c = e < n ? P(t, n)[1] : s, i = o.children.slice();
137
141
  return E(i, r, D), E(i, c.children, D), {
138
142
  ...t,
139
143
  children: i
140
144
  };
141
- }, R = (t, e, n) => e >= n ? [] : I(I(t, n)[0], e)[1].children, B = (t, e) => e >= 0 && e <= b(t), V = (t, e) => e.reduce((t, e) => F(t, e), t), F = (t, e) => {
145
+ }, R = (t, e, n) => e >= n ? [] : P(P(t, n)[0], e)[1].children, B = (t, e) => e >= 0 && e <= b(t), N = (t, e) => e.reduce((t, e) => V(t, e), t), V = (t, e) => {
142
146
  switch (e.type) {
143
147
  case f:
144
148
  {
@@ -164,9 +168,9 @@ const t = ([t, e]) => t > e ? [ e, t ] : [ t, e ], e = ([t, e]) => t === e, n =
164
168
  }
165
169
  }
166
170
  return t;
167
- }, L = ([t, e], n) => [ F(t, n), F(e, n) ];
171
+ }, F = ([t, e], n) => [ V(t, n), V(e, n) ];
168
172
 
169
- function N(t, e) {
173
+ function L(t, e) {
170
174
  t.apply({
171
175
  type: "delete",
172
176
  range: [ 0, b(t.doc) ]
@@ -177,42 +181,42 @@ function N(t, e) {
177
181
  });
178
182
  }
179
183
 
180
- let T = null, q = null, J = null, U = null, G = null;
184
+ let T = null, W = null, q = null, J = null, U = null;
181
185
 
182
- const W = t => 1 === t.nodeType, Z = t => 8 === t.nodeType, j = () => q, z = () => {
186
+ const G = t => 1 === t.nodeType, Z = t => 8 === t.nodeType, j = () => W, z = () => {
183
187
  const t = X();
184
- return 1 === t ? q.data.length : 2 === t ? 1 : 0;
188
+ return 1 === t ? W.data.length : 2 === t ? 1 : 0;
185
189
  }, X = () => {
186
- if (null != J) return J;
187
- if (q) if ((t => 3 === t.nodeType)(q)) {
188
- const t = q.data;
189
- if (t) return J = "\n" === t ? $() ? 3 : 6 : 1;
190
- } else if (W(q)) {
191
- if ("BR" === q.tagName) return J = $() ? 3 : 5;
192
- if (U.l(q)) return J = 2;
193
- if (U.u(q)) return J = 4;
190
+ if (null != q) return q;
191
+ if (W) if ((t => 3 === t.nodeType)(W)) {
192
+ const t = W.data;
193
+ if (t) return q = "\n" === t ? $() ? 3 : 6 : 1;
194
+ } else if (G(W)) {
195
+ if ("BR" === W.tagName) return q = $() ? 3 : 5;
196
+ if (J.u(W)) return q = 2;
197
+ if (J.p(W)) return q = 4;
194
198
  }
195
- return J = 0;
196
- }, Y = () => (J = null, q = T.nextNode()), K = () => {
197
- for (;(J = null) || (q = T.nextSibling()); ) if (4 === X()) return;
199
+ return q = 0;
200
+ }, Y = () => (q = null, W = T.nextNode()), K = () => {
201
+ for (;(q = null) || (W = T.nextSibling()); ) if (4 === X()) return;
198
202
  }, Q = () => {
199
- for (;(J = null) || (q = T.parentNode()); ) if (4 === X()) return;
203
+ for (;(q = null) || (W = T.parentNode()); ) if (4 === X()) return;
200
204
  }, $ = () => {
201
- const t = q.parentNode;
202
- return G(() => {
205
+ const t = W.parentNode;
206
+ return U(() => {
203
207
  for (;Y(); ) {
204
208
  if (X()) return !0;
205
- if (!t.contains(q)) break;
209
+ if (!t.contains(W)) break;
206
210
  }
207
211
  return !1;
208
212
  });
209
213
  }, tt = () => {
210
214
  for (;;) {
211
215
  if (2 === X()) {
212
- const t = q;
213
- for (;Y() && t.contains(q); ) ;
216
+ const t = W;
217
+ for (;Y() && t.contains(W); ) ;
214
218
  } else Y();
215
- if (!q) break;
219
+ if (!W) break;
216
220
  const t = X();
217
221
  if (t) return t;
218
222
  }
@@ -236,7 +240,7 @@ const W = t => 1 === t.nodeType, Z = t => 8 === t.nodeType, j = () => q, z = ()
236
240
  const d = i ? u : ut(e, f, s);
237
241
  if (!d) return;
238
242
  const p = t.createRange(), [y, h] = u, [x, g] = d;
239
- W(y) ? h < 1 ? p.setStartBefore(y) : p.setStartAfter(y) : p.setStart(y, h), W(x) ? g < 1 ? p.setEndBefore(x) : p.setEndAfter(x) : p.setEnd(x, g),
243
+ G(y) ? h < 1 ? p.setStartBefore(y) : p.setStartAfter(y) : p.setStart(y, h), G(x) ? g < 1 ? p.setEndBefore(x) : p.setEndAfter(x) : p.setEnd(x, g),
240
244
  lt(e, p, c, a);
241
245
  }, ut = (t, [e, n], r) => r(() => {
242
246
  let t, r = 0;
@@ -250,7 +254,7 @@ const W = t => 1 === t.nodeType, Z = t => 8 === t.nodeType, j = () => q, z = ()
250
254
  }, t), dt = (t, e, r, o) => {
251
255
  let s = !0;
252
256
  if (t === e && !e.hasChildNodes()) return [ [ 0 ], 0 ];
253
- if (W(e) && e.hasChildNodes()) {
257
+ if (G(e) && e.hasChildNodes()) {
254
258
  const t = n(r, e.childNodes.length - 1);
255
259
  e = e.childNodes[t], s = t === r, r = 0;
256
260
  }
@@ -310,7 +314,20 @@ const W = t => 1 === t.nodeType, Z = t => 8 === t.nodeType, j = () => q, z = ()
310
314
 
311
315
  function xt(t) {
312
316
  let e = 0, n = 0, r = !1;
313
- const s = Date.now, c = [ [ t.doc, t.selection, [] ] ], i = () => c[e];
317
+ const s = Date.now, c = [ [ t.doc, t.selection, [] ] ], i = () => c[e], a = () => e > 0, l = () => e < c.length - 1, f = () => {
318
+ if (a()) {
319
+ const n = i()[1];
320
+ e--;
321
+ const s = t.doc;
322
+ r = !0, t.exec(L, i()[0].children), r = !1, o(s, t.doc) || (t.selection = n);
323
+ }
324
+ }, u = () => {
325
+ if (l()) {
326
+ e++;
327
+ const [n, s, c] = i(), a = t.doc;
328
+ r = !0, t.exec(L, n.children), r = !1, o(a, t.doc) || (t.selection = [ N(s[0], c), N(s[1], c) ]);
329
+ }
330
+ };
314
331
  t.hook("apply", (i, a) => {
315
332
  if (r) return;
316
333
  const l = t.doc, f = t.selection;
@@ -325,49 +342,41 @@ function xt(t) {
325
342
  }
326
343
  n = t, c[e][0] = u, c[e][2].push(i), c.splice(e + 1), e > 500 && (e--, c.shift());
327
344
  }
328
- }), t.hook("keyboard", ht("Mod+Z", () => {
329
- if (e > 0) {
330
- const n = i()[1];
331
- e--;
332
- const s = t.doc;
333
- r = !0, t.exec(N, i()[0].children), r = !1, o(s, t.doc) || (t.selection = n);
334
- }
335
- })), t.hook("keyboard", ht("Shift+Mod+Z", () => {
336
- if (e < c.length - 1) {
337
- e++;
338
- const [n, s, c] = i(), a = t.doc;
339
- r = !0, t.exec(N, n.children), r = !1, o(a, t.doc) || (t.selection = [ V(s[0], c), V(s[1], c) ]);
340
- }
341
- }));
345
+ }), t.hook("keyboard", ht("Mod+Z", f)), t.hook("keyboard", ht("Shift+Mod+Z", u)),
346
+ t.set(xt, {
347
+ undo: f,
348
+ redo: u,
349
+ undoable: a,
350
+ redoable: l
351
+ });
342
352
  }
343
353
 
344
- const gt = [], kt = () => {}, _t = ({doc: n, readonly: r = !1, schema: a, isBlock: x = nt, onError: g = console.error}) => {
354
+ const gt = [], _t = () => {}, kt = t => {
355
+ throw Error(t);
356
+ }, bt = ({doc: n, readonly: r = !1, schema: a, isBlock: x = nt, onWarn: g = console.warn, onError: _ = kt}) => {
345
357
  let k = [ 0, 0 ];
346
- const _ = (t, e) => {
347
- if (!a) return e("An unsafe operation was detected. We recommend using schema option."),
358
+ const b = t => {
359
+ if (!a) return g("An unsafe operation was detected. We recommend using schema option."),
348
360
  !0;
349
- const n = a["~standard"].validate(t);
350
- if (n instanceof Promise) e("async validate is not supported."); else {
351
- if (!n.issues) return !0;
352
- e(n.issues.map(t => t.message).join("\n"));
361
+ const e = a["~standard"].validate(t);
362
+ if (e instanceof Promise) _("async validate is not supported"); else {
363
+ if (!e.issues) return !0;
364
+ g(e.issues.map(t => t.message).join("\n"));
353
365
  }
354
366
  return !1;
355
367
  };
356
- let b;
357
- if (!_(n, t => {
358
- b = t;
359
- }) && b) throw Error(b);
360
- const S = new Map, D = t => S.get(t) || gt, E = new Map, O = t => {
368
+ a && !b(n) && _("Invalid document");
369
+ const v = new Map, S = new WeakMap, D = t => v.get(t) || gt, E = new Map, O = t => {
361
370
  const e = E.get(t);
362
371
  e && !e[1] && (e[1] = !0, i(() => {
363
372
  e[1] = !1, e[0].forEach(t => {
364
373
  t();
365
374
  });
366
375
  }));
367
- }, I = t => {
368
- if (!r) if (Array.isArray(t)) for (const e of t) F(e); else F(t);
369
- return W;
370
- }, F = t => {
376
+ }, P = t => {
377
+ if (!r) if (Array.isArray(t)) for (const e of t) V(e); else V(t);
378
+ return G;
379
+ }, V = t => {
371
380
  const e = n, r = D("apply"), s = r.length;
372
381
  let c = 0;
373
382
  const i = () => {
@@ -382,7 +391,7 @@ const gt = [], kt = () => {}, _t = ({doc: n, readonly: r = !1, schema: a, isBloc
382
391
  case f:
383
392
  {
384
393
  const [r, o] = n.range;
385
- B(t, r) && B(t, o) && r < o && (t = H(t, r, o, []), e = L(e, n));
394
+ B(t, r) && B(t, o) && r < o && (t = H(t, r, o, []), e = F(e, n));
386
395
  break;
387
396
  }
388
397
 
@@ -390,13 +399,13 @@ const gt = [], kt = () => {}, _t = ({doc: n, readonly: r = !1, schema: a, isBloc
390
399
  {
391
400
  const {at: r, text: o} = n;
392
401
  if (B(t, r) && o) {
393
- const [s, c] = w(t, r), i = P(t, s), a = A(i, c - 1);
394
- let f;
395
- if (a) {
396
- const t = a.o;
397
- h(t) && (f = t);
402
+ const {i: s, o: c} = I(t, r), i = A(s, c - 1);
403
+ let a;
404
+ if (i) {
405
+ const t = i.i;
406
+ h(t) && (a = t);
398
407
  }
399
- t = H(t, r, r, l(o, f, i)), e = L(e, n);
408
+ t = H(t, r, r, l(o, a, s)), e = F(e, n);
400
409
  }
401
410
  break;
402
411
  }
@@ -404,7 +413,7 @@ const gt = [], kt = () => {}, _t = ({doc: n, readonly: r = !1, schema: a, isBloc
404
413
  case d:
405
414
  {
406
415
  const {at: r, fragment: o} = n;
407
- B(t, r) && o.length && (t = H(t, r, r, o), e = L(e, n));
416
+ B(t, r) && o.length && (t = H(t, r, r, o), e = F(e, n));
408
417
  break;
409
418
  }
410
419
 
@@ -423,7 +432,10 @@ const gt = [], kt = () => {}, _t = ({doc: n, readonly: r = !1, schema: a, isBloc
423
432
 
424
433
  case y:
425
434
  {
426
- const {path: e, key: r, value: o} = n, s = P(t, e);
435
+ const {path: e, key: r, value: o} = n, s = ((t, e) => {
436
+ for (let n = 0; n < e.length; n++) t = t.children[e[n]];
437
+ return t;
438
+ })(t, e);
427
439
  s && (t = C(t, e, {
428
440
  ...s,
429
441
  [r]: o
@@ -433,7 +445,7 @@ const gt = [], kt = () => {}, _t = ({doc: n, readonly: r = !1, schema: a, isBloc
433
445
  }
434
446
  return [ t, e ];
435
447
  })(n, k, t);
436
- (({type: t}) => t === d || t === p || t === y)(t) && !_(e, g) || (n = e, k = r);
448
+ (({type: t}) => t === d || t === p || t === y)(t) && !b(e) || (n = e, k = r);
437
449
  } catch (t) {
438
450
  g("rollback operation: " + t);
439
451
  }
@@ -442,10 +454,10 @@ const gt = [], kt = () => {}, _t = ({doc: n, readonly: r = !1, schema: a, isBloc
442
454
  e && (t = e), c++, i();
443
455
  };
444
456
  i(), o(e, n) || O("change");
445
- }, N = t => {
457
+ }, L = t => {
446
458
  !((t, [e, n]) => B(t, e) && B(t, n))(n, t) || k[0] === t[0] && k[1] === t[1] || (k = t,
447
459
  O("selectionchange"));
448
- }, W = {
460
+ }, G = {
449
461
  get doc() {
450
462
  return n;
451
463
  },
@@ -453,7 +465,7 @@ const gt = [], kt = () => {}, _t = ({doc: n, readonly: r = !1, schema: a, isBloc
453
465
  return k;
454
466
  },
455
467
  set selection(t) {
456
- N(t);
468
+ L(t);
457
469
  },
458
470
  get readonly() {
459
471
  return r;
@@ -461,7 +473,7 @@ const gt = [], kt = () => {}, _t = ({doc: n, readonly: r = !1, schema: a, isBloc
461
473
  set readonly(t) {
462
474
  r = t, O("readonly");
463
475
  },
464
- apply: I,
476
+ apply: P,
465
477
  on: (t, e) => {
466
478
  let n = E.get(t);
467
479
  n || E.set(t, n = [ new Set, !1 ]);
@@ -471,54 +483,56 @@ const gt = [], kt = () => {}, _t = ({doc: n, readonly: r = !1, schema: a, isBloc
471
483
  };
472
484
  },
473
485
  hook: (t, e) => {
474
- let n = S.get(t);
475
- return n || S.set(t, n = []), n.push(e), () => {
486
+ let n = v.get(t);
487
+ return n || v.set(t, n = []), n.push(e), () => {
476
488
  const t = n.indexOf(e);
477
489
  -1 !== t && n.splice(t, 1);
478
490
  };
479
491
  },
480
492
  exec: (t, ...e) => {
481
- const n = t(W, ...e);
482
- return void 0 === n ? W : n;
493
+ const n = t(G, ...e);
494
+ return void 0 === n ? G : n;
483
495
  },
496
+ get: t => (S.has(t) || _("No value found for key"), S.get(t)),
497
+ set: (t, e) => (S.set(t, e), G),
484
498
  input: o => {
485
499
  if (!window.InputEvent || !c(InputEvent.prototype.getTargetRanges)) return g("beforeinput event is not supported."),
486
- kt;
500
+ _t;
487
501
  const {contentEditable: i, role: a, ariaMultiLine: l, ariaReadOnly: f} = o, u = o.style.whiteSpace;
488
502
  o.role = "textbox", o.style.whiteSpace = "pre-wrap", o.ariaMultiLine = "true";
489
503
  let d = !1, p = null, y = !1, h = !1, _ = !1;
490
- const b = ct(o), w = (t => {
504
+ const b = ct(o), v = (t => {
491
505
  const e = (n, r, o) => {
492
- const s = U, c = G, i = T, a = q, l = J;
506
+ const s = J, c = U, i = T, a = W, l = q;
493
507
  try {
494
- return T || (U = t, G = e, T = U.p.createTreeWalker(r, 5)), o && (T.currentNode = q = o),
508
+ return T || (J = t, U = e, T = J.h.createTreeWalker(r, 5)), o && (T.currentNode = W = o),
495
509
  n();
496
510
  } finally {
497
- U = s, G = c, T = i, q = a, J = l, T && a && (T.currentNode = a);
511
+ J = s, U = c, T = i, W = a, q = l, T && a && (T.currentNode = a);
498
512
  }
499
513
  };
500
514
  return e;
501
515
  })({
502
- p: b,
503
- u: x,
504
- l: ot
516
+ h: b,
517
+ p: x,
518
+ u: ot
505
519
  }), S = () => {
506
520
  o.contentEditable = r ? "false" : "true", o.ariaReadOnly = r ? "true" : null;
507
521
  };
508
522
  S();
509
- const E = W.on("change", () => {
523
+ const E = G.on("change", () => {
510
524
  h || requestAnimationFrame(() => {
511
525
  h || o.focus({
512
526
  preventScroll: !0
513
527
  });
514
528
  });
515
- }), O = W.on("readonly", S), A = t => {
529
+ }), O = G.on("readonly", S), A = t => {
516
530
  for (const e of D("paste")) {
517
- const n = e(t, w);
531
+ const n = e(t, v);
518
532
  if (n) return n;
519
533
  }
520
534
  g("failed to serialize pasted data");
521
- }, P = ((t, e) => {
535
+ }, I = ((t, e) => {
522
536
  let n = !1;
523
537
  const r = [], o = t => {
524
538
  n && r.push(...t);
@@ -533,7 +547,7 @@ const gt = [], kt = () => {}, _t = ({doc: n, readonly: r = !1, schema: a, isBloc
533
547
  childList: !0,
534
548
  subtree: !0
535
549
  }), {
536
- h: t => {
550
+ _: t => {
537
551
  let e;
538
552
  for (;e = t.pop(); ) if ("childList" === e.type) {
539
553
  const {target: t, removedNodes: n, addedNodes: r, nextSibling: o} = e;
@@ -545,37 +559,37 @@ const gt = [], kt = () => {}, _t = ({doc: n, readonly: r = !1, schema: a, isBloc
545
559
  k: t => {
546
560
  !n && t && c(), n = t;
547
561
  },
548
- _: i,
549
- m: () => {
562
+ m: i,
563
+ v: () => {
550
564
  r.splice(0), s.disconnect();
551
565
  }
552
566
  };
553
567
  })(o, () => {
554
- ft(b, o, M(n, k), k[0] - k[1], w);
568
+ ft(b, o, M(n, k), k[0] - k[1], v);
555
569
  }), C = () => {
556
- N(v(n, ((t, e) => {
570
+ L(w(n, ((t, e) => {
557
571
  const n = it(t), r = at(n, t);
558
572
  if (!r) return [ [ [ 0 ], 0 ], [ [ 0 ], 0 ] ];
559
573
  const o = pt(t, e, r), s = st(n.anchorNode, n.focusNode);
560
574
  return (0 === s ? n.anchorOffset > n.focusOffset : 2 & s) ? [ o[1], o[0] ] : o;
561
- })(o, w)));
575
+ })(o, v)));
562
576
  }, H = () => {
563
- const t = P._();
564
- P.k(!1), t.length && (P.h(t), b.removeEventListener("selectionchange", Y), ft(b, o, M(n, k), k[0] - k[1], w, !0),
565
- b.addEventListener("selectionchange", Y)), p && (N(p[1]), I(p[0]), p = null), y = !1;
566
- }, B = t => {
567
- if (!y) for (const e of D("keyboard")) if (e(t)) return t.preventDefault(), void P.k(!1);
568
- }, F = () => {
577
+ const t = I.m();
578
+ I.k(!1), t.length && (I._(t), b.removeEventListener("selectionchange", X), ft(b, o, M(n, k), k[0] - k[1], v, !0),
579
+ b.addEventListener("selectionchange", X)), p && (L(p[1]), P(p[0]), p = null), y = !1;
580
+ }, R = t => {
581
+ if (!y) for (const e of D("keyboard")) if (e(t)) return t.preventDefault(), void I.k(!1);
582
+ }, B = () => {
569
583
  y || H();
570
- }, L = t => {
584
+ }, V = t => {
571
585
  t.preventDefault();
572
586
  const r = t.inputType;
573
587
  if (r.startsWith("format")) return;
574
588
  if ("historyUndo" === r || "historyRedo" === r) return;
575
- y ? P.k(!0) : C();
589
+ y ? I.k(!0) : C();
576
590
  const s = t.getTargetRanges()[0];
577
591
  if (s) {
578
- const c = v(n, pt(o, w, s));
592
+ const c = w(n, pt(o, v, s));
579
593
  let i, a = "insertParagraph" === r || "insertLineBreak" === r ? "\n" : t.data;
580
594
  if (null == a) {
581
595
  const e = t.dataTransfer;
@@ -591,29 +605,26 @@ const gt = [], kt = () => {}, _t = ({doc: n, readonly: r = !1, schema: a, isBloc
591
605
  });
592
606
  }
593
607
  y || H();
594
- }, Z = () => {
608
+ }, F = () => {
595
609
  y || C(), y = !0;
596
- }, j = () => {
610
+ }, Z = () => {
597
611
  H();
598
- }, z = () => {
612
+ }, j = () => {
599
613
  h = !0, C();
600
- }, X = () => {
614
+ }, z = () => {
601
615
  h = !1;
602
- }, Y = () => {
616
+ }, X = () => {
603
617
  !h || y || _ || C();
604
- }, K = r => {
605
- if (C(), !e(k)) {
606
- const e = R(n, ...t(k));
607
- for (const t of D("copy")) t(r, e, o);
608
- }
609
- }, Q = t => {
610
- t.preventDefault(), K(t.clipboardData);
611
- }, $ = e => {
612
- e.preventDefault(), r || (K(e.clipboardData), I({
618
+ }, Y = t => {
619
+ if (C(), !e(k)) for (const e of D("copy")) e(t);
620
+ }, K = t => {
621
+ t.preventDefault(), Y(t.clipboardData);
622
+ }, Q = e => {
623
+ e.preventDefault(), r || (Y(e.clipboardData), P({
613
624
  type: "delete",
614
625
  range: t(k)
615
626
  }));
616
- }, tt = e => {
627
+ }, $ = e => {
617
628
  e.preventDefault();
618
629
  const n = A(e.clipboardData);
619
630
  if (n) {
@@ -629,9 +640,9 @@ const gt = [], kt = () => {}, _t = ({doc: n, readonly: r = !1, schema: a, isBloc
629
640
  type: "insert_node",
630
641
  at: r,
631
642
  fragment: n
632
- }), I(o);
643
+ }), P(o);
633
644
  }
634
- }, et = e => {
645
+ }, tt = e => {
635
646
  e.preventDefault();
636
647
  const r = e.dataTransfer, c = ((t, e, {clientX: n, clientY: r}, o) => {
637
648
  if (t.caretPositionFromPoint) {
@@ -641,7 +652,7 @@ const gt = [], kt = () => {}, _t = ({doc: n, readonly: r = !1, schema: a, isBloc
641
652
  const s = t.caretRangeFromPoint(n, r);
642
653
  if (s) return dt(e, s.startContainer, s.startOffset, o);
643
654
  }
644
- })(b, o, e, w);
655
+ })(b, o, e, v);
645
656
  if (r && c) {
646
657
  let e;
647
658
  const o = [];
@@ -651,7 +662,7 @@ const gt = [], kt = () => {}, _t = ({doc: n, readonly: r = !1, schema: a, isBloc
651
662
  });
652
663
  const i = A(r);
653
664
  if (i) {
654
- const t = m(n, c), r = V(t, o);
665
+ const t = m(n, c), r = N(t, o);
655
666
  o.push(s(i) ? {
656
667
  type: "insert_text",
657
668
  at: r,
@@ -660,50 +671,50 @@ const gt = [], kt = () => {}, _t = ({doc: n, readonly: r = !1, schema: a, isBloc
660
671
  type: "insert_node",
661
672
  at: r,
662
673
  fragment: i
663
- }), e = [ r, V(t, o) ];
674
+ }), e = [ r, N(t, o) ];
664
675
  }
665
- I(o), e && N(e);
676
+ P(o), e && L(e);
666
677
  }
667
- }, nt = t => {
668
- _ = !0, K(t.dataTransfer);
669
- }, rt = () => {
678
+ }, et = t => {
679
+ _ = !0, Y(t.dataTransfer);
680
+ }, nt = () => {
670
681
  _ = !1;
671
682
  };
672
- b.addEventListener("selectionchange", Y), o.addEventListener("keydown", B), o.addEventListener("input", F),
673
- o.addEventListener("beforeinput", L), o.addEventListener("compositionstart", Z),
674
- o.addEventListener("compositionend", j), o.addEventListener("focus", z), o.addEventListener("blur", X),
675
- o.addEventListener("copy", Q), o.addEventListener("cut", $), o.addEventListener("paste", tt),
676
- o.addEventListener("drop", et), o.addEventListener("dragstart", nt), o.addEventListener("dragend", rt);
677
- const lt = [];
683
+ b.addEventListener("selectionchange", X), o.addEventListener("keydown", R), o.addEventListener("input", B),
684
+ o.addEventListener("beforeinput", V), o.addEventListener("compositionstart", F),
685
+ o.addEventListener("compositionend", Z), o.addEventListener("focus", j), o.addEventListener("blur", z),
686
+ o.addEventListener("copy", K), o.addEventListener("cut", Q), o.addEventListener("paste", $),
687
+ o.addEventListener("drop", tt), o.addEventListener("dragstart", et), o.addEventListener("dragend", nt);
688
+ const rt = [];
678
689
  return D("mount").forEach(t => {
679
690
  const e = t(o);
680
- e && lt.push(e);
691
+ e && rt.push(e);
681
692
  }), () => {
682
693
  d || (d = !0, E(), O(), o.contentEditable = i, o.role = a, o.ariaMultiLine = l,
683
- o.ariaReadOnly = f, o.style.whiteSpace = u, P.m(), b.removeEventListener("selectionchange", Y),
684
- o.removeEventListener("keydown", B), o.removeEventListener("input", F), o.removeEventListener("beforeinput", L),
685
- o.removeEventListener("compositionstart", Z), o.removeEventListener("compositionend", j),
686
- o.removeEventListener("focus", z), o.removeEventListener("blur", X), o.removeEventListener("copy", Q),
687
- o.removeEventListener("cut", $), o.removeEventListener("paste", tt), o.removeEventListener("drop", et),
688
- o.removeEventListener("dragstart", nt), o.removeEventListener("dragend", rt), lt.forEach(t => {
694
+ o.ariaReadOnly = f, o.style.whiteSpace = u, I.v(), b.removeEventListener("selectionchange", X),
695
+ o.removeEventListener("keydown", R), o.removeEventListener("input", B), o.removeEventListener("beforeinput", V),
696
+ o.removeEventListener("compositionstart", F), o.removeEventListener("compositionend", Z),
697
+ o.removeEventListener("focus", j), o.removeEventListener("blur", z), o.removeEventListener("copy", K),
698
+ o.removeEventListener("cut", Q), o.removeEventListener("paste", $), o.removeEventListener("drop", tt),
699
+ o.removeEventListener("dragstart", et), o.removeEventListener("dragend", nt), rt.forEach(t => {
689
700
  t();
690
701
  }));
691
702
  };
692
703
  }
693
704
  };
694
- return W.exec(xt), W;
705
+ return G.exec(xt), G;
695
706
  };
696
707
 
697
- function bt(t, e) {
698
- const n = e && e.serializer;
699
- t.hook("copy", (t, e) => {
700
- t.setData("text/plain", a({
701
- children: e
702
- }, n));
703
- }), t.hook("paste", t => t.getData("text/plain"));
708
+ function mt(e, n) {
709
+ const r = n && n.serializer;
710
+ e.hook("copy", n => {
711
+ n.setData("text/plain", a({
712
+ children: R(e.doc, ...t(e.selection))
713
+ }, r));
714
+ }), e.hook("paste", t => t.getData("text/plain"));
704
715
  }
705
716
 
706
- const mt = "application/x-editate-editor";
717
+ const vt = "application/x-editate-editor";
707
718
 
708
719
  function wt(t) {
709
720
  t.hook("mount", t => {
@@ -745,7 +756,11 @@ exports.Delete = function(e, n = t(e.selection)) {
745
756
  at: n,
746
757
  text: e
747
758
  });
748
- }, exports.ReplaceDoc = N, exports.ReplaceText = function(e, n) {
759
+ }, exports.Redo = function(t) {
760
+ t.get(xt).redo();
761
+ }, exports.Redoable = function(t) {
762
+ return t.get(xt).redoable();
763
+ }, exports.ReplaceDoc = L, exports.ReplaceText = function(e, n) {
749
764
  const r = t(e.selection);
750
765
  e.apply({
751
766
  type: "delete",
@@ -755,18 +770,19 @@ exports.Delete = function(e, n = t(e.selection)) {
755
770
  at: r[0],
756
771
  text: n
757
772
  });
758
- }, exports.SetBlockAttr = function(t, e, n, r = w(t.doc, t.selection[0])[0]) {
773
+ }, exports.SetBlockAttr = function(t, e, n, r = t.selection[0]) {
774
+ const {t: o} = I(t.doc, r);
759
775
  t.apply({
760
776
  type: "set_node_attr",
761
- path: r,
777
+ path: o,
762
778
  key: e,
763
779
  value: n
764
780
  });
765
- }, exports.ToggleBlockAttr = function(t, e, n, r, o = w(t.doc, t.selection[0])[0]) {
766
- const s = P(t.doc, o);
781
+ }, exports.ToggleBlockAttr = function(t, e, n, r, o = t.selection[0]) {
782
+ const {i: s, t: c} = I(t.doc, o);
767
783
  t.apply({
768
784
  type: "set_node_attr",
769
- path: o,
785
+ path: c,
770
786
  key: e,
771
787
  value: s[e] === n ? r : n
772
788
  });
@@ -778,13 +794,17 @@ exports.Delete = function(e, n = t(e.selection)) {
778
794
  key: n,
779
795
  value: !!o.some(t => !t[n])
780
796
  });
781
- }, exports.createEditor = _t, exports.createPlainEditor = ({text: t, singleline: e, onChange: n, ...r}) => {
782
- const o = _t({
797
+ }, exports.Undo = function(t) {
798
+ t.get(xt).undo();
799
+ }, exports.Undoable = function(t) {
800
+ return t.get(xt).undoable();
801
+ }, exports.createEditor = bt, exports.createPlainEditor = ({text: t, singleline: e, onChange: n, ...r}) => {
802
+ const o = bt({
783
803
  ...r,
784
804
  doc: {
785
805
  children: l(t)
786
806
  }
787
- }).exec(bt);
807
+ }).exec(mt);
788
808
  return o.on("change", () => {
789
809
  n(a(o.doc));
790
810
  }), e && o.exec(wt), o;
@@ -810,9 +830,13 @@ exports.Delete = function(e, n = t(e.selection)) {
810
830
  return null;
811
831
  });
812
832
  }, exports.htmlTransferPlugin = function(t, e) {
813
- t.hook("copy", (t, e, n) => {
814
- const r = document.createElement("div");
815
- r.appendChild(at(it(n), n).cloneContents()), t.setData("text/html", r.innerHTML);
833
+ let n = null;
834
+ t.hook("mount", t => (n = t, () => {
835
+ n = null;
836
+ })), t.hook("copy", t => {
837
+ if (!n) return;
838
+ const e = document.createElement("div");
839
+ e.appendChild(at(it(n), n).cloneContents()), t.setData("text/html", e.innerHTML);
816
840
  }), t.hook("paste", ((t, e = []) => (n, r) => {
817
841
  const o = n.getData("text/html");
818
842
  if (o) {
@@ -845,12 +869,12 @@ exports.Delete = function(e, n = t(e.selection)) {
845
869
  }
846
870
  return null;
847
871
  })(e.serializeText, e.serializers));
848
- }, exports.internalTranferPlugin = function(t) {
849
- t.hook("copy", (t, e) => {
850
- t.setData(mt, JSON.stringify(e));
851
- }), t.hook("paste", t => {
872
+ }, exports.internalTranferPlugin = function(e) {
873
+ e.hook("copy", n => {
874
+ n.setData(vt, JSON.stringify(R(e.doc, ...t(e.selection))));
875
+ }), e.hook("paste", t => {
852
876
  try {
853
- return JSON.parse(t.getData(mt));
877
+ return JSON.parse(t.getData(vt));
854
878
  } catch (t) {
855
879
  return null;
856
880
  }
@@ -862,5 +886,5 @@ exports.Delete = function(e, n = t(e.selection)) {
862
886
  r();
863
887
  }));
864
888
  });
865
- }, exports.plainTransferPlugin = bt, exports.singlelinePlugin = wt;
889
+ }, exports.plainTransferPlugin = mt, exports.singlelinePlugin = wt;
866
890
  //# sourceMappingURL=index.cjs.map