editate 0.6.2 → 0.6.4

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
@@ -1,6 +1,6 @@
1
1
  const t = ([t, e]) => t > e ? [ e, t ] : [ t, e ], e = ([t, e]) => t === e, n = Math.min, {keys: r, is: o} = Object, s = t => "string" == typeof t, c = t => "function" == typeof t, i = c(queueMicrotask) ? queueMicrotask : t => {
2
2
  Promise.resolve().then(t);
3
- }, a = (t, e = t => h(t) ? t.text : "") => t.children.reduce((t, n, r) => {
3
+ }, a = (t, e = t => y(t) ? t.text : "") => t.children.reduce((t, n, r) => {
4
4
  const o = x(n);
5
5
  return 0 !== r && o && (t += "\n"), t + (o ? n.children.reduce((t, n) => t + e(n), "") : "");
6
6
  }, ""), l = (t, e, n) => t.split("\n").map(t => ({
@@ -9,7 +9,7 @@ const t = ([t, e]) => t > e ? [ e, t ] : [ t, e ], e = ([t, e]) => t === e, n =
9
9
  ...e,
10
10
  text: t
11
11
  } ]
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) => {
12
+ })), f = "delete", u = "insert_text", d = "insert_node", p = "set_attr", h = "set_node_attr", y = 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
15
  }, _ = new WeakMap, k = ({children: t}, e = 0, n = t.length) => {
@@ -24,57 +24,58 @@ const t = ([t, e]) => t > e ? [ e, t ] : [ t, e ], e = ([t, e]) => t === e, n =
24
24
  let e = _.get(t);
25
25
  return null == e && _.set(t, e = k(t)), e;
26
26
  }
27
- return h(t) ? t.text.length : 1;
27
+ return y(t) ? t.text.length : 1;
28
28
  }, m = (t, [e, n]) => {
29
29
  let r = 0;
30
30
  for (const n of e) r += k(t, 0, n), 0 !== n && r++, t = t.children[n];
31
31
  return r + n;
32
- }, v = (t, e) => {
33
- const n = I(t, e);
32
+ }, w = (t, e) => {
33
+ const n = A(t, e);
34
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) => {
35
+ }, 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) => {
36
36
  let r = e + 1;
37
37
  for (;r <= n; ) {
38
38
  const e = t[r - 1], o = t[r];
39
- h(e) && h(o) && g(e, o) ? (t[r - 1] = {
39
+ y(e) && y(o) && g(e, o) ? (t[r - 1] = {
40
40
  ...e,
41
41
  text: e.text + o.text
42
42
  }, t.splice(r, 1), n--) : r++;
43
43
  }
44
44
  for (r = e; r <= n; ) {
45
45
  const e = t[r];
46
- h(e) && !e.text && t.length > 1 ? (t.splice(r, 1), n--) : r++;
46
+ y(e) && !e.text && t.length > 1 ? (t.splice(r, 1), n--) : r++;
47
47
  }
48
- }, D = (t, e = 0, n = t.length - 1) => {
48
+ }, E = (t, e = 0, n = t.length - 1) => {
49
49
  let r = e + 1;
50
50
  for (;r <= n; ) {
51
51
  const e = t[r - 1], o = t[r];
52
- t[r - 1] = O(e, o), t.splice(r, 1), n--;
52
+ t[r - 1] = D(e, o), t.splice(r, 1), n--;
53
53
  }
54
- }, E = (t, e, n) => {
54
+ }, O = (t, e, n) => {
55
55
  if (e.length) {
56
56
  const r = t.length;
57
57
  t.push(...e), r && n(t, r - 1, r);
58
58
  }
59
- }, O = (...t) => ({
59
+ }, D = (...t) => ({
60
60
  ...t[0],
61
- children: t.reduce((t, e) => (E(t, e.children, S), t), [])
62
- }), A = ({children: t}, e) => {
63
- for (let n = 0; n < t.length; n++) {
64
- const r = t[n];
65
- let o = b(r);
66
- if (x(r) && o++, o > e) return {
67
- i: r,
68
- l: n,
61
+ children: t.reduce((t, e) => (O(t, e.children, S), t), [])
62
+ }), P = ({children: t}, e) => {
63
+ const n = t.length;
64
+ for (let r = 0; r < n; r++) {
65
+ const n = t[r];
66
+ let o = b(n);
67
+ if (x(n) && o++, o > e || o === e && y(n) && !n.text) return {
68
+ i: n,
69
+ l: r,
69
70
  o: e
70
71
  };
71
72
  e -= o;
72
73
  }
73
74
  return null;
74
- }, I = (t, e) => {
75
+ }, A = (t, e) => {
75
76
  const n = [];
76
77
  for (;t; ) {
77
- const r = A(t, e);
78
+ const r = P(t, e);
78
79
  if (!r) break;
79
80
  const o = r.i;
80
81
  if (!x(o)) break;
@@ -85,12 +86,12 @@ const t = ([t, e]) => t > e ? [ e, t ] : [ t, e ], e = ([t, e]) => t === e, n =
85
86
  t: n,
86
87
  o: e
87
88
  };
88
- }, P = (t, e) => {
89
- const n = t.children, r = A(t, e);
89
+ }, I = (t, e) => {
90
+ const n = t.children, r = P(t, e);
90
91
  if (r) {
91
92
  const {i: e, o, l: s} = r;
92
93
  if (x(e)) {
93
- const [r, c] = P(e, o), i = n.slice(0, s), a = n.slice(s + 1);
94
+ const [r, c] = I(e, o), i = n.slice(0, s), a = n.slice(s + 1);
94
95
  return i.push(r), a.unshift(c), [ {
95
96
  ...t,
96
97
  children: i
@@ -101,7 +102,7 @@ const t = ([t, e]) => t > e ? [ e, t ] : [ t, e ], e = ([t, e]) => t === e, n =
101
102
  }
102
103
  {
103
104
  const r = n.slice(0, s), c = n.slice(s + 1);
104
- if (h(e)) {
105
+ if (y(e)) {
105
106
  const t = e.text.slice(0, o), n = e.text.slice(o);
106
107
  !t && r.length || r.push({
107
108
  ...e,
@@ -120,9 +121,13 @@ const t = ([t, e]) => t > e ? [ e, t ] : [ t, e ], e = ([t, e]) => t === e, n =
120
121
  } ];
121
122
  }
122
123
  }
124
+ const o = n[n.length - 1];
123
125
  return [ t, {
124
126
  ...t,
125
- children: []
127
+ children: y(o) ? [ {
128
+ ...o,
129
+ text: ""
130
+ } ] : []
126
131
  } ];
127
132
  }, C = (t, e, n, r = 0) => {
128
133
  if (r < e.length) {
@@ -136,13 +141,13 @@ const t = ([t, e]) => t > e ? [ e, t ] : [ t, e ], e = ([t, e]) => t === e, n =
136
141
  })(t, o, o, [ C(t.children[o], e, n, r + 1) ]);
137
142
  }
138
143
  return n;
139
- }, H = (t, e, n, r) => {
140
- const [o, s] = P(t, e), c = e < n ? P(t, n)[1] : s, i = o.children.slice();
141
- return E(i, r, D), E(i, c.children, D), {
144
+ }, R = (t, e, n, r) => {
145
+ const [o, s] = I(t, e), c = e < n ? I(t, n)[1] : s, i = o.children.slice();
146
+ return O(i, r, E), O(i, c.children, E), {
142
147
  ...t,
143
148
  children: i
144
149
  };
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) => {
150
+ }, T = (t, e, n) => e >= n ? [] : I(I(t, n)[0], e)[1].children, L = (t, e) => e >= 0 && e <= b(t), H = (t, e) => e.reduce((t, e) => B(t, e), t), B = (t, e) => {
146
151
  switch (e.type) {
147
152
  case f:
148
153
  {
@@ -168,9 +173,9 @@ const t = ([t, e]) => t > e ? [ e, t ] : [ t, e ], e = ([t, e]) => t === e, n =
168
173
  }
169
174
  }
170
175
  return t;
171
- }, F = ([t, e], n) => [ V(t, n), V(e, n) ];
176
+ }, N = ([t, e], n) => [ B(t, n), B(e, n) ];
172
177
 
173
- function L(t, e) {
178
+ function V(t, e) {
174
179
  t.apply({
175
180
  type: "delete",
176
181
  range: [ 0, b(t.doc) ]
@@ -181,108 +186,112 @@ function L(t, e) {
181
186
  });
182
187
  }
183
188
 
184
- let T = null, W = null, q = null, J = null, U = null;
189
+ let F = null, U = null, W = null, q = null, G = null;
185
190
 
186
- const G = t => 1 === t.nodeType, Z = t => 8 === t.nodeType, j = () => W, z = () => {
187
- const t = X();
188
- return 1 === t ? W.data.length : 2 === t ? 1 : 0;
191
+ const J = t => 1 === t.nodeType, Y = t => 8 === t.nodeType, Z = () => U, j = () => {
192
+ const t = z();
193
+ return 1 === t ? U.data.length : 2 === t ? 1 : 0;
194
+ }, z = () => {
195
+ if (null != W) return W;
196
+ if (U) if ((t => 3 === t.nodeType)(U)) {
197
+ const t = U.data;
198
+ if (t) return W = "\n" === t ? et() ? 3 : 5 : 1;
199
+ } else if (J(U)) {
200
+ if ("BR" === U.tagName) return W = et() ? 3 : 5;
201
+ if (tt(U)) return W = 6;
202
+ if (q.u(U)) return W = 2;
203
+ if (q.p(U)) return W = 4;
204
+ }
205
+ return W = 0;
189
206
  }, X = () => {
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;
207
+ const t = z();
208
+ if (W = null, 2 === t || 6 === t) {
209
+ const t = U;
210
+ if (U = F.nextSibling(), !U) for (;(U = F.nextNode()) && t.contains(U); ) ;
211
+ return U;
198
212
  }
199
- return q = 0;
200
- }, Y = () => (q = null, W = T.nextNode()), K = () => {
201
- for (;(q = null) || (W = T.nextSibling()); ) if (4 === X()) return;
213
+ return U = F.nextNode();
214
+ }, K = () => {
215
+ for (;(W = null) || (U = F.nextSibling()); ) if (4 === z()) return;
202
216
  }, Q = () => {
203
- for (;(q = null) || (W = T.parentNode()); ) if (4 === X()) return;
204
- }, $ = () => {
205
- const t = W.parentNode;
206
- return U(() => {
207
- for (;Y(); ) {
208
- if (X()) return !0;
209
- if (!t.contains(W)) break;
217
+ for (;(W = null) || (U = F.parentNode()); ) if (4 === z()) return;
218
+ }, $ = new Set([ "TEMPLATE", "STYLE", "SCRIPT", "COLGROUP" ]), tt = t => $.has(t.tagName), et = () => {
219
+ const t = U.parentNode;
220
+ return G(() => {
221
+ for (W = 0; X(); ) {
222
+ if (z()) return !0;
223
+ if (!t.contains(U)) break;
210
224
  }
211
225
  return !1;
212
226
  });
213
- }, tt = () => {
214
- for (;;) {
215
- if (2 === X()) {
216
- const t = W;
217
- for (;Y() && t.contains(W); ) ;
218
- } else Y();
219
- if (!W) break;
220
- const t = X();
221
- if (t) return t;
227
+ }, nt = () => {
228
+ for (;X(); ) {
229
+ const t = z();
230
+ if (t && 6 !== t) return t;
222
231
  }
223
- }, et = new Set([ "DIV", "H1", "H2", "H3", "H4", "H5", "H6", "P", "PRE", "LI", "DT", "DD", "TR" ]), nt = t => et.has(t.tagName), rt = new Set([ "EMBED", "IMG", "PICTURE", "AUDIO", "VIDEO", "SVG", "CANVAS", "MATH", "IFRAME", "OBJECT" ]), ot = t => "false" === t.contentEditable || rt.has(t.tagName), st = (t, e) => t.compareDocumentPosition(e), ct = t => t.ownerDocument, it = t => ct(t).getSelection(), at = (t, e) => {
232
+ }, rt = new Set([ "DIV", "H1", "H2", "H3", "H4", "H5", "H6", "P", "PRE", "LI", "DT", "DD", "TR" ]), ot = t => rt.has(t.tagName), st = new Set([ "EMBED", "IMG", "PICTURE", "AUDIO", "VIDEO", "SVG", "CANVAS", "MATH", "IFRAME", "OBJECT" ]), ct = t => "false" === t.contentEditable || st.has(t.tagName), it = (t, e) => t.compareDocumentPosition(e), at = t => t.ownerDocument, lt = t => at(t).getSelection(), ft = (t, e) => {
224
233
  if (t.rangeCount) {
225
234
  const n = t.getRangeAt(0);
226
235
  if (e.contains(n.commonAncestorContainer)) return n;
227
236
  }
228
- }, lt = (t, e, n, r) => {
229
- const o = it(t);
230
- (n || at(o, t)) && (o.removeAllRanges(), o.addRange(e), r && (o.collapseToEnd(),
237
+ }, ut = (t, e, n, r) => {
238
+ const o = lt(t);
239
+ (n || ft(o, t)) && (o.removeAllRanges(), o.addRange(e), r && (o.collapseToEnd(),
231
240
  o.extend(e.startContainer, e.startOffset)));
232
- }, ft = (t, e, [n, r], o, s, c) => {
241
+ }, dt = (t, e, [n, r], o, s, c) => {
233
242
  const i = 0 === o, a = o > 0, l = a ? r : n, f = a ? n : r;
234
243
  if (0 === l[0].length && 0 === l[1] && i && !e.hasChildNodes()) {
235
244
  const n = t.createRange();
236
- return n.setStart(e, 0), n.setEnd(e, 0), lt(e, n, c);
245
+ return n.setStart(e, 0), n.setEnd(e, 0), ut(e, n, c);
237
246
  }
238
- const u = ut(e, l, s);
247
+ const u = pt(e, l, s);
239
248
  if (!u) return;
240
- const d = i ? u : ut(e, f, s);
249
+ const d = i ? u : pt(e, f, s);
241
250
  if (!d) return;
242
- const p = t.createRange(), [y, h] = u, [x, g] = d;
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),
244
- lt(e, p, c, a);
245
- }, ut = (t, [e, n], r) => r(() => {
251
+ const p = t.createRange(), [h, y] = u, [x, g] = d;
252
+ J(h) ? y < 1 ? p.setStartBefore(h) : p.setStartAfter(h) : p.setStart(h, y), J(x) ? g < 1 ? p.setEndBefore(x) : p.setEndAfter(x) : p.setEnd(x, g),
253
+ ut(e, p, c, a);
254
+ }, pt = (t, [e, n], r) => r(() => {
246
255
  let t, r = 0;
247
- for (;t = tt(); ) if (4 === t) {
256
+ for (;t = nt(); ) if (4 === t) {
248
257
  if (r < e.length) for (let t = e[r++]; t > 0; t--) K();
249
258
  } else {
250
- const t = z();
251
- if (n <= t) return [ j(), n ];
259
+ const t = j();
260
+ if (n <= t) return [ Z(), n ];
252
261
  n -= t;
253
262
  }
254
- }, t), dt = (t, e, r, o) => {
263
+ }, t), ht = (t, e, r, o) => {
255
264
  let s = !0;
256
265
  if (t === e && !e.hasChildNodes()) return [ [ 0 ], 0 ];
257
- if (G(e) && e.hasChildNodes()) {
266
+ if (J(e) && e.hasChildNodes()) {
258
267
  const t = n(r, e.childNodes.length - 1);
259
268
  e = e.childNodes[t], s = t === r, r = 0;
260
269
  }
261
270
  return o(() => {
262
- 4 !== X() && Q();
271
+ 4 !== z() && Q();
263
272
  const n = o(() => {
264
273
  const e = [];
265
274
  let n;
266
- for (;(n = j()) && n !== t; ) e.unshift(n), Q();
275
+ for (;(n = Z()) && n !== t; ) e.unshift(n), Q();
267
276
  if (!e.length) return [ 0 ];
268
277
  let r = 0, o = e[e.length - 1];
269
- for (;o = o.previousElementSibling; ) r++;
278
+ for (;o = o.previousElementSibling; ) tt(o) || r++;
270
279
  return [ r ];
271
280
  });
272
281
  let c = 0;
273
- for (;tt(); ) {
274
- const t = st(e, j());
282
+ for (;nt(); ) {
283
+ const t = it(e, Z());
275
284
  if (0 === t || 16 & t) {
276
285
  if (s) break;
277
286
  } else if (4 & t) break;
278
- c += z();
287
+ c += j();
279
288
  }
280
289
  return [ n, c + r ];
281
290
  }, t, e);
282
- }, pt = (t, e, {startOffset: n, startContainer: r, endOffset: o, endContainer: s}) => {
283
- const c = dt(t, r, n, e);
284
- return [ c, r === s && n === o ? c : dt(t, s, o, e) ];
285
- }, yt = "undefined" != typeof navigator && /Mac|iP(hone|od|ad)/.test(navigator.platform), ht = (t, e) => {
291
+ }, yt = (t, e, {startOffset: n, startContainer: r, endOffset: o, endContainer: s}) => {
292
+ const c = ht(t, r, n, e);
293
+ return [ c, r === s && n === o ? c : ht(t, s, o, e) ];
294
+ }, xt = "undefined" != typeof navigator && /Mac|iP(hone|od|ad)/.test(navigator.platform), gt = (t, e) => {
286
295
  const n = t.endsWith("+"), r = (n ? t.slice(0, -2) : t).split("+"), o = n ? "+" : r.pop(), s = "Space" === o ? " " : o.toLowerCase();
287
296
  let c = !1, i = !1, a = !1, l = !1;
288
297
  return r.forEach(t => {
@@ -296,7 +305,7 @@ const G = t => 1 === t.nodeType, Z = t => 8 === t.nodeType, j = () => W, z = ()
296
305
  break;
297
306
 
298
307
  case "Mod":
299
- yt ? a = !0 : i = !0;
308
+ xt ? a = !0 : i = !0;
300
309
  break;
301
310
 
302
311
  case "Alt":
@@ -312,20 +321,20 @@ const G = t => 1 === t.nodeType, Z = t => 8 === t.nodeType, j = () => W, z = ()
312
321
  };
313
322
  };
314
323
 
315
- function xt(t) {
324
+ function _t(t) {
316
325
  let e = 0, n = 0, r = !1;
317
326
  const s = Date.now, c = [ [ t.doc, t.selection, [] ] ], i = () => c[e], a = () => e > 0, l = () => e < c.length - 1, f = () => {
318
327
  if (a()) {
319
328
  const n = i()[1];
320
329
  e--;
321
330
  const s = t.doc;
322
- r = !0, t.exec(L, i()[0].children), r = !1, o(s, t.doc) || (t.selection = n);
331
+ r = !0, t.exec(V, i()[0].children), r = !1, o(s, t.doc) || (t.selection = n);
323
332
  }
324
333
  }, u = () => {
325
334
  if (l()) {
326
335
  e++;
327
336
  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) ]);
337
+ r = !0, t.exec(V, n.children), r = !1, o(a, t.doc) || (t.selection = [ H(s[0], c), H(s[1], c) ]);
329
338
  }
330
339
  };
331
340
  t.hook("apply", (i, a) => {
@@ -342,8 +351,8 @@ function xt(t) {
342
351
  }
343
352
  n = t, c[e][0] = u, c[e][2].push(i), c.splice(e + 1), e > 500 && (e--, c.shift());
344
353
  }
345
- }), t.hook("keyboard", ht("Mod+Z", f)), t.hook("keyboard", ht("Shift+Mod+Z", u)),
346
- t.set(xt, {
354
+ }), t.hook("keyboard", gt("Mod+Z", f)), t.hook("keyboard", gt("Shift+Mod+Z", u)),
355
+ t.set(_t, {
347
356
  undo: f,
348
357
  redo: u,
349
358
  undoable: a,
@@ -351,9 +360,9 @@ function xt(t) {
351
360
  });
352
361
  }
353
362
 
354
- const gt = [], _t = () => {}, kt = t => {
363
+ const kt = [], bt = () => {}, mt = t => {
355
364
  throw Error(t);
356
- }, bt = ({doc: n, readonly: r = !1, schema: a, isBlock: x = nt, onWarn: g = console.warn, onError: _ = kt}) => {
365
+ }, wt = ({doc: n, readonly: r = !1, schema: a, isBlock: x = ot, onWarn: g = console.warn, onError: _ = mt}) => {
357
366
  let k = [ 0, 0 ];
358
367
  const b = t => {
359
368
  if (!a) return g("An unsafe operation was detected. We recommend using schema option."),
@@ -366,18 +375,18 @@ const gt = [], _t = () => {}, kt = t => {
366
375
  return !1;
367
376
  };
368
377
  a && !b(n) && _("Invalid document");
369
- const v = new Map, S = new WeakMap, D = t => v.get(t) || gt, E = new Map, O = t => {
370
- const e = E.get(t);
371
- e && !e[1] && (e[1] = !0, i(() => {
372
- e[1] = !1, e[0].forEach(t => {
378
+ const w = new Map, S = new WeakMap, E = t => w.get(t) || kt, O = new Map, D = [], I = t => {
379
+ const e = O.get(t);
380
+ e && (D.length || i(() => {
381
+ D.forEach(t => {
373
382
  t();
374
- });
375
- }));
376
- }, P = t => {
383
+ }), D.splice(0);
384
+ }), D.push(...e));
385
+ }, B = t => {
377
386
  if (!r) if (Array.isArray(t)) for (const e of t) V(e); else V(t);
378
- return G;
387
+ return Y;
379
388
  }, V = t => {
380
- const e = n, r = D("apply"), s = r.length;
389
+ const e = n, r = E("apply"), s = r.length;
381
390
  let c = 0;
382
391
  const i = () => {
383
392
  if (c < s) {
@@ -391,21 +400,21 @@ const gt = [], _t = () => {}, kt = t => {
391
400
  case f:
392
401
  {
393
402
  const [r, o] = n.range;
394
- B(t, r) && B(t, o) && r < o && (t = H(t, r, o, []), e = F(e, n));
403
+ L(t, r) && L(t, o) && r < o && (t = R(t, r, o, []), e = N(e, n));
395
404
  break;
396
405
  }
397
406
 
398
407
  case u:
399
408
  {
400
409
  const {at: r, text: o} = n;
401
- if (B(t, r) && o) {
402
- const {i: s, o: c} = I(t, r), i = A(s, c - 1);
410
+ if (L(t, r) && o) {
411
+ const {i: s, o: c} = A(t, r), i = P(s, c - 1);
403
412
  let a;
404
413
  if (i) {
405
414
  const t = i.i;
406
- h(t) && (a = t);
415
+ y(t) && (a = t);
407
416
  }
408
- t = H(t, r, r, l(o, a, s)), e = F(e, n);
417
+ t = R(t, r, r, l(o, a, s)), e = N(e, n);
409
418
  }
410
419
  break;
411
420
  }
@@ -413,24 +422,33 @@ const gt = [], _t = () => {}, kt = t => {
413
422
  case d:
414
423
  {
415
424
  const {at: r, fragment: o} = n;
416
- B(t, r) && o.length && (t = H(t, r, r, o), e = F(e, n));
425
+ L(t, r) && o.length && (t = R(t, r, r, o), e = N(e, n));
417
426
  break;
418
427
  }
419
428
 
420
429
  case p:
421
430
  {
422
431
  const {range: [e, r], key: o, value: s} = n;
423
- B(t, e) && B(t, r) && e < r && (t = H(t, e, r, R(t, e, r).map(t => ({
432
+ if (L(t, e) && L(t, r) && e <= r) if (e === r) {
433
+ const {i: {children: n}, t: r} = A(t, e);
434
+ if (1 === n.length) {
435
+ const e = n[0];
436
+ y(e) && !e.text && (t = C(t, [ ...r, 0 ], {
437
+ ...e,
438
+ [o]: s
439
+ }));
440
+ }
441
+ } else t = R(t, e, r, T(t, e, r).map(t => ({
424
442
  ...t,
425
- children: t.children.map(t => h(t) ? {
443
+ children: t.children.map(t => y(t) ? {
426
444
  ...t,
427
445
  [o]: s
428
446
  } : t)
429
- }))));
447
+ })));
430
448
  break;
431
449
  }
432
450
 
433
- case y:
451
+ case h:
434
452
  {
435
453
  const {path: e, key: r, value: o} = n, s = ((t, e) => {
436
454
  for (let n = 0; n < e.length; n++) t = t.children[e[n]];
@@ -445,7 +463,7 @@ const gt = [], _t = () => {}, kt = t => {
445
463
  }
446
464
  return [ t, e ];
447
465
  })(n, k, t);
448
- (({type: t}) => t === d || t === p || t === y)(t) && !b(e) || (n = e, k = r);
466
+ (({type: t}) => t === d || t === p || t === h)(t) && !b(e) || (n = e, k = r);
449
467
  } catch (t) {
450
468
  g("rollback operation: " + t);
451
469
  }
@@ -453,11 +471,11 @@ const gt = [], _t = () => {}, kt = t => {
453
471
  }, a = e => {
454
472
  e && (t = e), c++, i();
455
473
  };
456
- i(), o(e, n) || O("change");
457
- }, L = t => {
458
- !((t, [e, n]) => B(t, e) && B(t, n))(n, t) || k[0] === t[0] && k[1] === t[1] || (k = t,
459
- O("selectionchange"));
460
- }, G = {
474
+ i(), o(e, n) || I("change");
475
+ }, J = t => {
476
+ !((t, [e, n]) => L(t, e) && L(t, n))(n, t) || k[0] === t[0] && k[1] === t[1] || (k = t,
477
+ I("selectionchange"));
478
+ }, Y = {
461
479
  get doc() {
462
480
  return n;
463
481
  },
@@ -465,74 +483,72 @@ const gt = [], _t = () => {}, kt = t => {
465
483
  return k;
466
484
  },
467
485
  set selection(t) {
468
- L(t);
486
+ J(t);
469
487
  },
470
488
  get readonly() {
471
489
  return r;
472
490
  },
473
491
  set readonly(t) {
474
- r = t, O("readonly");
492
+ r = t, I("readonly");
475
493
  },
476
- apply: P,
494
+ apply: B,
477
495
  on: (t, e) => {
478
- let n = E.get(t);
479
- n || E.set(t, n = [ new Set, !1 ]);
480
- const r = n[0];
481
- return r.add(e), () => {
482
- r.delete(e);
496
+ let n = O.get(t);
497
+ return n || O.set(t, n = new Set), n.add(e), () => {
498
+ n.delete(e);
483
499
  };
484
500
  },
485
501
  hook: (t, e) => {
486
- let n = v.get(t);
487
- return n || v.set(t, n = []), n.push(e), () => {
502
+ let n = w.get(t);
503
+ return n || w.set(t, n = []), n.push(e), () => {
488
504
  const t = n.indexOf(e);
489
505
  -1 !== t && n.splice(t, 1);
490
506
  };
491
507
  },
492
508
  exec: (t, ...e) => {
493
- const n = t(G, ...e);
494
- return void 0 === n ? G : n;
509
+ const n = t(Y, ...e);
510
+ return void 0 === n ? Y : n;
495
511
  },
496
512
  get: t => (S.has(t) || _("No value found for key"), S.get(t)),
497
- set: (t, e) => (S.set(t, e), G),
513
+ set: (t, e) => (S.set(t, e), Y),
498
514
  input: o => {
499
515
  if (!window.InputEvent || !c(InputEvent.prototype.getTargetRanges)) return g("beforeinput event is not supported."),
500
- _t;
516
+ bt;
501
517
  const {contentEditable: i, role: a, ariaMultiLine: l, ariaReadOnly: f} = o, u = o.style.whiteSpace;
502
518
  o.role = "textbox", o.style.whiteSpace = "pre-wrap", o.ariaMultiLine = "true";
503
- let d = !1, p = null, y = !1, h = !1, _ = !1;
504
- const b = ct(o), v = (t => {
519
+ let d = !1, p = null, h = !1, y = !1, _ = !1;
520
+ const b = at(o), w = (t => {
505
521
  const e = (n, r, o) => {
506
- const s = J, c = U, i = T, a = W, l = q;
522
+ const s = q, c = G, i = F, a = U, l = W;
507
523
  try {
508
- return T || (J = t, U = e, T = J.h.createTreeWalker(r, 5)), o && (T.currentNode = W = o),
524
+ return F || (q = t, G = e, F = q.h.createTreeWalker(r, 5)), o && (F.currentNode = U = o),
509
525
  n();
510
526
  } finally {
511
- J = s, U = c, T = i, W = a, q = l, T && a && (T.currentNode = a);
527
+ q = s, G = c, F = i, U = a, W = l, F && a && (F.currentNode = a);
512
528
  }
513
529
  };
514
530
  return e;
515
531
  })({
516
532
  h: b,
517
533
  p: x,
518
- u: ot
534
+ u: ct
519
535
  }), S = () => {
520
536
  o.contentEditable = r ? "false" : "true", o.ariaReadOnly = r ? "true" : null;
521
537
  };
522
538
  S();
523
- const E = G.on("change", () => {
524
- h || requestAnimationFrame(() => {
525
- h || o.focus({
539
+ const O = Y.on("change", () => {
540
+ y || requestAnimationFrame(() => {
541
+ y || o.focus({
526
542
  preventScroll: !0
527
543
  });
528
544
  });
529
- }), O = G.on("readonly", S), A = t => {
530
- for (const e of D("paste")) {
531
- const n = e(t, v);
545
+ }), D = Y.on("readonly", S), P = t => {
546
+ for (const e of E("paste")) {
547
+ const n = e(t, w);
532
548
  if (n) return n;
533
549
  }
534
550
  g("failed to serialize pasted data");
535
- }, I = ((t, e) => {
551
+ }, A = ((t, e) => {
536
552
  let n = !1;
537
553
  const r = [], o = t => {
538
554
  n && r.push(...t);
@@ -565,31 +581,31 @@ const gt = [], _t = () => {}, kt = t => {
565
581
  }
566
582
  };
567
583
  })(o, () => {
568
- ft(b, o, M(n, k), k[0] - k[1], v);
569
- }), C = () => {
570
- L(w(n, ((t, e) => {
571
- const n = it(t), r = at(n, t);
584
+ dt(b, o, M(n, k), k[0] - k[1], w);
585
+ }), I = () => {
586
+ J(v(n, ((t, e) => {
587
+ const n = lt(t), r = ft(n, t);
572
588
  if (!r) return [ [ [ 0 ], 0 ], [ [ 0 ], 0 ] ];
573
- const o = pt(t, e, r), s = st(n.anchorNode, n.focusNode);
589
+ const o = yt(t, e, r), s = it(n.anchorNode, n.focusNode);
574
590
  return (0 === s ? n.anchorOffset > n.focusOffset : 2 & s) ? [ o[1], o[0] ] : o;
575
- })(o, v)));
576
- }, H = () => {
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;
591
+ })(o, w)));
592
+ }, C = () => {
593
+ const t = A.m();
594
+ A.k(!1), t.length && (A._(t), b.removeEventListener("selectionchange", z), dt(b, o, M(n, k), k[0] - k[1], w, !0),
595
+ b.addEventListener("selectionchange", z)), p && (J(p[1]), B(p[0]), p = null), h = !1;
580
596
  }, R = t => {
581
- if (!y) for (const e of D("keyboard")) if (e(t)) return t.preventDefault(), void I.k(!1);
582
- }, B = () => {
583
- y || H();
584
- }, V = t => {
597
+ if (!h) for (const e of E("keyboard")) if (e(t)) return t.preventDefault(), void A.k(!1);
598
+ }, T = () => {
599
+ h || C();
600
+ }, L = t => {
585
601
  t.preventDefault();
586
602
  const r = t.inputType;
587
603
  if (r.startsWith("format")) return;
588
604
  if ("historyUndo" === r || "historyRedo" === r) return;
589
- y ? I.k(!0) : C();
605
+ h ? A.k(!0) : I();
590
606
  const s = t.getTargetRanges()[0];
591
607
  if (s) {
592
- const c = w(n, pt(o, v, s));
608
+ const c = v(n, yt(o, w, s));
593
609
  let i, a = "insertParagraph" === r || "insertLineBreak" === r ? "\n" : t.data;
594
610
  if (null == a) {
595
611
  const e = t.dataTransfer;
@@ -604,29 +620,29 @@ const gt = [], _t = () => {}, kt = t => {
604
620
  text: a
605
621
  });
606
622
  }
607
- y || H();
608
- }, F = () => {
609
- y || C(), y = !0;
623
+ h || C();
624
+ }, N = () => {
625
+ h || I(), h = !0;
626
+ }, V = () => {
627
+ C();
610
628
  }, Z = () => {
611
- H();
629
+ y = !0, I();
612
630
  }, j = () => {
613
- h = !0, C();
631
+ y = !1;
614
632
  }, z = () => {
615
- h = !1;
616
- }, X = () => {
617
- !h || y || _ || C();
618
- }, Y = t => {
619
- if (C(), !e(k)) for (const e of D("copy")) e(t);
633
+ !y || h || _ || I();
634
+ }, X = t => {
635
+ if (I(), !e(k)) for (const e of E("copy")) e(t);
620
636
  }, K = t => {
621
- t.preventDefault(), Y(t.clipboardData);
637
+ t.preventDefault(), X(t.clipboardData);
622
638
  }, Q = e => {
623
- e.preventDefault(), r || (Y(e.clipboardData), P({
639
+ e.preventDefault(), r || (X(e.clipboardData), B({
624
640
  type: "delete",
625
641
  range: t(k)
626
642
  }));
627
643
  }, $ = e => {
628
644
  e.preventDefault();
629
- const n = A(e.clipboardData);
645
+ const n = P(e.clipboardData);
630
646
  if (n) {
631
647
  const e = t(k), r = e[0], o = [ {
632
648
  type: "delete",
@@ -640,19 +656,19 @@ const gt = [], _t = () => {}, kt = t => {
640
656
  type: "insert_node",
641
657
  at: r,
642
658
  fragment: n
643
- }), P(o);
659
+ }), B(o);
644
660
  }
645
661
  }, tt = e => {
646
662
  e.preventDefault();
647
663
  const r = e.dataTransfer, c = ((t, e, {clientX: n, clientY: r}, o) => {
648
664
  if (t.caretPositionFromPoint) {
649
665
  const s = t.caretPositionFromPoint(n, r);
650
- if (s) return dt(e, s.offsetNode, s.offset, o);
666
+ if (s) return ht(e, s.offsetNode, s.offset, o);
651
667
  } else if (t.caretRangeFromPoint) {
652
668
  const s = t.caretRangeFromPoint(n, r);
653
- if (s) return dt(e, s.startContainer, s.startOffset, o);
669
+ if (s) return ht(e, s.startContainer, s.startOffset, o);
654
670
  }
655
- })(b, o, e, v);
671
+ })(b, o, e, w);
656
672
  if (r && c) {
657
673
  let e;
658
674
  const o = [];
@@ -660,9 +676,9 @@ const gt = [], _t = () => {}, kt = t => {
660
676
  type: "delete",
661
677
  range: t(k)
662
678
  });
663
- const i = A(r);
679
+ const i = P(r);
664
680
  if (i) {
665
- const t = m(n, c), r = N(t, o);
681
+ const t = m(n, c), r = H(t, o);
666
682
  o.push(s(i) ? {
667
683
  type: "insert_text",
668
684
  at: r,
@@ -671,30 +687,30 @@ const gt = [], _t = () => {}, kt = t => {
671
687
  type: "insert_node",
672
688
  at: r,
673
689
  fragment: i
674
- }), e = [ r, N(t, o) ];
690
+ }), e = [ r, H(t, o) ];
675
691
  }
676
- P(o), e && L(e);
692
+ B(o), e && J(e);
677
693
  }
678
694
  }, et = t => {
679
- _ = !0, Y(t.dataTransfer);
695
+ _ = !0, X(t.dataTransfer);
680
696
  }, nt = () => {
681
697
  _ = !1;
682
698
  };
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),
699
+ b.addEventListener("selectionchange", z), o.addEventListener("keydown", R), o.addEventListener("input", T),
700
+ o.addEventListener("beforeinput", L), o.addEventListener("compositionstart", N),
701
+ o.addEventListener("compositionend", V), o.addEventListener("focus", Z), o.addEventListener("blur", j),
686
702
  o.addEventListener("copy", K), o.addEventListener("cut", Q), o.addEventListener("paste", $),
687
703
  o.addEventListener("drop", tt), o.addEventListener("dragstart", et), o.addEventListener("dragend", nt);
688
704
  const rt = [];
689
- return D("mount").forEach(t => {
705
+ return E("mount").forEach(t => {
690
706
  const e = t(o);
691
707
  e && rt.push(e);
692
708
  }), () => {
693
- d || (d = !0, E(), O(), o.contentEditable = i, o.role = a, o.ariaMultiLine = l,
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),
709
+ d || (d = !0, O(), D(), o.contentEditable = i, o.role = a, o.ariaMultiLine = l,
710
+ o.ariaReadOnly = f, o.style.whiteSpace = u, A.v(), b.removeEventListener("selectionchange", z),
711
+ o.removeEventListener("keydown", R), o.removeEventListener("input", T), o.removeEventListener("beforeinput", L),
712
+ o.removeEventListener("compositionstart", N), o.removeEventListener("compositionend", V),
713
+ o.removeEventListener("focus", Z), o.removeEventListener("blur", j), o.removeEventListener("copy", K),
698
714
  o.removeEventListener("cut", Q), o.removeEventListener("paste", $), o.removeEventListener("drop", tt),
699
715
  o.removeEventListener("dragstart", et), o.removeEventListener("dragend", nt), rt.forEach(t => {
700
716
  t();
@@ -702,21 +718,21 @@ const gt = [], _t = () => {}, kt = t => {
702
718
  };
703
719
  }
704
720
  };
705
- return G.exec(xt), G;
721
+ return Y.exec(_t), Y;
706
722
  };
707
723
 
708
- function mt(e, n) {
724
+ function vt(e, n) {
709
725
  const r = n && n.serializer;
710
726
  e.hook("copy", n => {
711
727
  n.setData("text/plain", a({
712
- children: R(e.doc, ...t(e.selection))
728
+ children: T(e.doc, ...t(e.selection))
713
729
  }, r));
714
730
  }), e.hook("paste", t => t.getData("text/plain"));
715
731
  }
716
732
 
717
- const vt = "application/x-editate-editor";
733
+ const Mt = "application/x-editate-editor";
718
734
 
719
- function wt(t) {
735
+ function St(t) {
720
736
  t.hook("mount", t => {
721
737
  t.ariaMultiLine = null;
722
738
  }), t.hook("apply", (t, e) => {
@@ -725,7 +741,7 @@ function wt(t) {
725
741
  text: t.text.replaceAll("\n", "")
726
742
  } : "insert_node" === t.type && (t = {
727
743
  ...t,
728
- fragment: [ O(...t.fragment) ]
744
+ fragment: [ D(...t.fragment) ]
729
745
  }), e(t);
730
746
  });
731
747
  }
@@ -757,10 +773,10 @@ exports.Delete = function(e, n = t(e.selection)) {
757
773
  text: e
758
774
  });
759
775
  }, exports.Redo = function(t) {
760
- t.get(xt).redo();
776
+ t.get(_t).redo();
761
777
  }, exports.Redoable = function(t) {
762
- return t.get(xt).redoable();
763
- }, exports.ReplaceDoc = L, exports.ReplaceText = function(e, n) {
778
+ return t.get(_t).redoable();
779
+ }, exports.ReplaceDoc = V, exports.ReplaceText = function(e, n) {
764
780
  const r = t(e.selection);
765
781
  e.apply({
766
782
  type: "delete",
@@ -771,7 +787,7 @@ exports.Delete = function(e, n = t(e.selection)) {
771
787
  text: n
772
788
  });
773
789
  }, exports.SetBlockAttr = function(t, e, n, r = t.selection[0]) {
774
- const {t: o} = I(t.doc, r);
790
+ const {t: o} = A(t.doc, r);
775
791
  t.apply({
776
792
  type: "set_node_attr",
777
793
  path: o,
@@ -779,35 +795,44 @@ exports.Delete = function(e, n = t(e.selection)) {
779
795
  value: n
780
796
  });
781
797
  }, exports.ToggleBlockAttr = function(t, e, n, r, o = t.selection[0]) {
782
- const {i: s, t: c} = I(t.doc, o);
798
+ const {i: s, t: c} = A(t.doc, o);
783
799
  t.apply({
784
800
  type: "set_node_attr",
785
801
  path: c,
786
802
  key: e,
787
803
  value: s[e] === n ? r : n
788
804
  });
789
- }, exports.ToggleFormat = function(e, n, r = t(e.selection)) {
790
- const o = R(e.doc, ...r).flatMap(t => t.children.filter(h));
791
- o.length && e.apply({
805
+ }, exports.ToggleFormat = function(n, r, o = t(n.selection)) {
806
+ let s;
807
+ if (e(o)) {
808
+ const t = ((t, e) => {
809
+ const n = A(t, e);
810
+ return P(n.i, n.o);
811
+ })(n.doc, o[0]);
812
+ if (!t) return;
813
+ s = [ t.i ];
814
+ } else s = T(n.doc, ...o).flatMap(t => t.children);
815
+ const c = s.filter(y);
816
+ c.length && n.apply({
792
817
  type: "set_attr",
793
- range: r,
794
- key: n,
795
- value: !!o.some(t => !t[n])
818
+ range: o,
819
+ key: r,
820
+ value: !!c.some(t => !t[r])
796
821
  });
797
822
  }, exports.Undo = function(t) {
798
- t.get(xt).undo();
823
+ t.get(_t).undo();
799
824
  }, 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({
825
+ return t.get(_t).undoable();
826
+ }, exports.createEditor = wt, exports.createPlainEditor = ({text: t, singleline: e, onChange: n, ...r}) => {
827
+ const o = wt({
803
828
  ...r,
804
829
  doc: {
805
830
  children: l(t)
806
831
  }
807
- }).exec(mt);
832
+ }).exec(vt);
808
833
  return o.on("change", () => {
809
834
  n(a(o.doc));
810
- }), e && o.exec(wt), o;
835
+ }), e && o.exec(St), o;
811
836
  }, exports.debugPlugin = function(t) {
812
837
  t.hook("apply", t => {
813
838
  console.log("apply", t);
@@ -836,12 +861,12 @@ exports.Delete = function(e, n = t(e.selection)) {
836
861
  })), t.hook("copy", t => {
837
862
  if (!n) return;
838
863
  const e = document.createElement("div");
839
- e.appendChild(at(it(n), n).cloneContents()), t.setData("text/html", e.innerHTML);
864
+ e.appendChild(ft(lt(n), n).cloneContents()), t.setData("text/html", e.innerHTML);
840
865
  }), t.hook("paste", ((t, e = []) => (n, r) => {
841
866
  const o = n.getData("text/html");
842
867
  if (o) {
843
868
  let n = (new DOMParser).parseFromString(o, "text/html").body, s = !1;
844
- for (const t of [ ...n.childNodes ]) Z(t) ? "StartFragment" === t.data ? (s = !0,
869
+ for (const t of [ ...n.childNodes ]) Y(t) ? "StartFragment" === t.data ? (s = !0,
845
870
  n = new DocumentFragment) : "EndFragment" === t.data && (s = !1) : s && n.appendChild(t);
846
871
  return ((t, e, n, r) => e(() => {
847
872
  let t, e = null, o = "", s = !1;
@@ -852,9 +877,9 @@ exports.Delete = function(e, n = t(e.selection)) {
852
877
  children: e
853
878
  }), e = null, s = !1;
854
879
  };
855
- for (;t = tt(); ) if (4 === t) a(); else if (s = !0, 1 === t) o += j().data; else if (2 === t) {
880
+ for (;t = nt(); ) if (4 === t) a(); else if (s = !0, 1 === t) o += Z().data; else if (2 === t) {
856
881
  i();
857
- const t = r(j());
882
+ const t = r(Z());
858
883
  t && e.push(t);
859
884
  } else 3 === t && a();
860
885
  return a(), c.length || c.push({
@@ -871,10 +896,10 @@ exports.Delete = function(e, n = t(e.selection)) {
871
896
  })(e.serializeText, e.serializers));
872
897
  }, exports.internalTranferPlugin = function(e) {
873
898
  e.hook("copy", n => {
874
- n.setData(vt, JSON.stringify(R(e.doc, ...t(e.selection))));
899
+ n.setData(Mt, JSON.stringify(T(e.doc, ...t(e.selection))));
875
900
  }), e.hook("paste", t => {
876
901
  try {
877
- return JSON.parse(t.getData(vt));
902
+ return JSON.parse(t.getData(Mt));
878
903
  } catch (t) {
879
904
  return null;
880
905
  }
@@ -882,9 +907,9 @@ exports.Delete = function(e, n = t(e.selection)) {
882
907
  }, exports.keymapPlugin = function(t, e) {
883
908
  r(e).forEach(n => {
884
909
  const r = e[n];
885
- t.hook("keyboard", ht(n, () => {
910
+ t.hook("keyboard", gt(n, () => {
886
911
  r();
887
912
  }));
888
913
  });
889
- }, exports.plainTransferPlugin = mt, exports.singlelinePlugin = wt;
914
+ }, exports.plainTransferPlugin = vt, exports.singlelinePlugin = St;
890
915
  //# sourceMappingURL=index.cjs.map