editate 0.5.0 → 0.6.1

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