editate 0.0.0 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +1 -1
- package/README.md +251 -1
- package/lib/commands.d.ts +42 -0
- package/lib/doc/edit.d.ts +49 -0
- package/lib/doc/position.d.ts +1 -0
- package/lib/doc/types.d.ts +31 -0
- package/lib/doc/utils.d.ts +1 -0
- package/lib/dom/default.d.ts +1 -0
- package/lib/dom/index.d.ts +2 -0
- package/lib/dom/mutation.d.ts +1 -0
- package/lib/dom/parser.d.ts +1 -0
- package/lib/editor.d.ts +107 -0
- package/lib/hooks/copy/html.d.ts +5 -0
- package/lib/hooks/copy/index.d.ts +4 -0
- package/lib/hooks/copy/internal.d.ts +7 -0
- package/lib/hooks/copy/plain.d.ts +6 -0
- package/lib/hooks/copy/types.d.ts +2 -0
- package/lib/hooks/index.d.ts +3 -0
- package/lib/hooks/keyboard.d.ts +14 -0
- package/lib/hooks/paste/file.d.ts +6 -0
- package/lib/hooks/paste/html.d.ts +6 -0
- package/lib/hooks/paste/index.d.ts +5 -0
- package/lib/hooks/paste/internal.d.ts +7 -0
- package/lib/hooks/paste/plain.d.ts +5 -0
- package/lib/hooks/paste/types.d.ts +3 -0
- package/lib/hooks/utils.d.ts +1 -0
- package/lib/index.cjs +811 -0
- package/lib/index.cjs.map +1 -0
- package/lib/index.d.ts +7 -0
- package/lib/index.js +828 -3
- package/lib/index.js.map +1 -0
- package/lib/plugins/debug.d.ts +2 -0
- package/lib/plugins/history.d.ts +1 -0
- package/lib/plugins/index.d.ts +2 -0
- package/lib/plugins/singleline.d.ts +2 -0
- package/lib/presets/index.d.ts +2 -0
- package/lib/presets/plain.d.ts +27 -0
- package/lib/utils.d.ts +1 -0
- package/package.json +85 -11
- package/lib/index.mjs +0 -3
package/lib/index.js
CHANGED
|
@@ -1,5 +1,830 @@
|
|
|
1
|
-
|
|
1
|
+
const t = Math.min, {keys: e, is: n} = Object, r = t => "string" == typeof t, o = t => "function" == typeof t, s = o(queueMicrotask) ? queueMicrotask : t => {
|
|
2
|
+
Promise.resolve().then(t);
|
|
3
|
+
}, c = (t, e = t => y(t) ? t.text : "") => t.children.reduce((t, n, r) => {
|
|
4
|
+
const o = g(n);
|
|
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 => ({
|
|
7
|
+
...n,
|
|
8
|
+
children: [ {
|
|
9
|
+
...e,
|
|
10
|
+
text: t
|
|
11
|
+
} ]
|
|
12
|
+
})), l = "delete", a = "insert_text", f = "insert_node", u = "set_attr", d = "set_node_attr", h = ({type: t}) => t === f || t === u || t === d;
|
|
2
13
|
|
|
3
|
-
|
|
14
|
+
class p {
|
|
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
|
+
}
|
|
4
64
|
|
|
5
|
-
|
|
65
|
+
const y = t => "text" in t, g = t => "children" in t, k = (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
|
+
}, m = new WeakMap, b = ({children: t}, e = 0, n = t.length) => {
|
|
69
|
+
let r = 0, o = 0;
|
|
70
|
+
for (let s = e; s < n; s++) {
|
|
71
|
+
const e = t[s];
|
|
72
|
+
r += w(e), 0 !== o && g(e) && r++, o++;
|
|
73
|
+
}
|
|
74
|
+
return r;
|
|
75
|
+
}, w = t => {
|
|
76
|
+
if (g(t)) {
|
|
77
|
+
let e = m.get(t);
|
|
78
|
+
return null == e && m.set(t, e = b(t)), e;
|
|
79
|
+
}
|
|
80
|
+
return y(t) ? t.text.length : 1;
|
|
81
|
+
}, x = (t, [e, n]) => {
|
|
82
|
+
let r = 0;
|
|
83
|
+
for (const n of e) r += b(t, 0, n), 0 !== n && r++, t = t.children[n];
|
|
84
|
+
return r + n;
|
|
85
|
+
}, _ = (t, e) => {
|
|
86
|
+
const n = [];
|
|
87
|
+
for (;t; ) {
|
|
88
|
+
const r = C(t, e);
|
|
89
|
+
if (!r) break;
|
|
90
|
+
const o = r.o, s = r.i;
|
|
91
|
+
if (!g(s)) break;
|
|
92
|
+
e = r.l, n.push(o), t = s;
|
|
93
|
+
}
|
|
94
|
+
return [ n, e ];
|
|
95
|
+
}, v = (t, [e, n]) => [ x(t, e), x(t, n) ], M = (t, [e, n]) => [ _(t, e), _(t, n) ], D = (t, e = 0, n = t.length - 1) => {
|
|
96
|
+
let r = e + 1;
|
|
97
|
+
for (;r <= n; ) {
|
|
98
|
+
const e = t[r - 1], o = t[r];
|
|
99
|
+
y(e) && y(o) && k(e, o) ? (t[r - 1] = {
|
|
100
|
+
...e,
|
|
101
|
+
text: e.text + o.text
|
|
102
|
+
}, t.splice(r, 1), n--) : r++;
|
|
103
|
+
}
|
|
104
|
+
for (r = e; r <= n; ) {
|
|
105
|
+
const e = t[r];
|
|
106
|
+
y(e) && !e.text && t.length > 1 ? (t.splice(r, 1), n--) : r++;
|
|
107
|
+
}
|
|
108
|
+
}, E = (t, e = 0, n = t.length - 1) => {
|
|
109
|
+
let r = e + 1;
|
|
110
|
+
for (;r <= n; ) {
|
|
111
|
+
const e = t[r - 1], o = t[r];
|
|
112
|
+
t[r - 1] = S(e, o), t.splice(r, 1), n--;
|
|
113
|
+
}
|
|
114
|
+
}, O = (t, e, n) => {
|
|
115
|
+
if (e.length) {
|
|
116
|
+
const r = t.length;
|
|
117
|
+
t.push(...e), r && n(t, r - 1, r);
|
|
118
|
+
}
|
|
119
|
+
}, S = (...t) => ({
|
|
120
|
+
...t[0],
|
|
121
|
+
children: t.reduce((t, e) => (O(t, e.children, D), t), [])
|
|
122
|
+
}), C = ({children: t}, e) => {
|
|
123
|
+
for (let n = 0; n < t.length; n++) {
|
|
124
|
+
const r = t[n];
|
|
125
|
+
let o = w(r);
|
|
126
|
+
if (g(r) && o++, o > e) return {
|
|
127
|
+
i: r,
|
|
128
|
+
o: n,
|
|
129
|
+
l: e
|
|
130
|
+
};
|
|
131
|
+
e -= o;
|
|
132
|
+
}
|
|
133
|
+
return null;
|
|
134
|
+
}, I = (t, e) => {
|
|
135
|
+
const n = t.children, r = C(t, e);
|
|
136
|
+
if (r) {
|
|
137
|
+
const {i: e, l: o, o: s} = r;
|
|
138
|
+
if (g(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), [ {
|
|
141
|
+
...t,
|
|
142
|
+
children: i
|
|
143
|
+
}, {
|
|
144
|
+
...t,
|
|
145
|
+
children: l
|
|
146
|
+
} ];
|
|
147
|
+
}
|
|
148
|
+
{
|
|
149
|
+
const r = n.slice(0, s), c = n.slice(s + 1);
|
|
150
|
+
if (y(e)) {
|
|
151
|
+
const t = e.text.slice(0, o), n = e.text.slice(o);
|
|
152
|
+
!t && r.length || r.push({
|
|
153
|
+
...e,
|
|
154
|
+
text: t
|
|
155
|
+
}), !n && c.length || c.unshift({
|
|
156
|
+
...e,
|
|
157
|
+
text: n
|
|
158
|
+
});
|
|
159
|
+
} else c.unshift(e);
|
|
160
|
+
return [ {
|
|
161
|
+
...t,
|
|
162
|
+
children: r
|
|
163
|
+
}, {
|
|
164
|
+
...t,
|
|
165
|
+
children: c
|
|
166
|
+
} ];
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
return [ t, {
|
|
170
|
+
...t,
|
|
171
|
+
children: []
|
|
172
|
+
} ];
|
|
173
|
+
}, A = (t, e) => {
|
|
174
|
+
for (let n = 0; n < e.length; n++) t = t.children[e[n]];
|
|
175
|
+
return t;
|
|
176
|
+
}, H = (t, e, n, r = 0) => {
|
|
177
|
+
if (r < e.length) {
|
|
178
|
+
const o = e[r];
|
|
179
|
+
return ((t, e, n, r) => {
|
|
180
|
+
const o = t.children.slice();
|
|
181
|
+
return o.splice(e, n - e + 1, ...r), {
|
|
182
|
+
...t,
|
|
183
|
+
children: o
|
|
184
|
+
};
|
|
185
|
+
})(t, o, o, [ H(t.children[o], e, n, r + 1) ]);
|
|
186
|
+
}
|
|
187
|
+
return n;
|
|
188
|
+
}, P = (t, e, n, r) => {
|
|
189
|
+
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), {
|
|
191
|
+
...t,
|
|
192
|
+
children: i
|
|
193
|
+
};
|
|
194
|
+
}, R = (t, e, n) => e >= n ? [] : I(I(t, n)[0], e)[1].children, B = (t, e) => e >= 0 && e <= w(t), F = (t, e) => {
|
|
195
|
+
switch (e.type) {
|
|
196
|
+
case l:
|
|
197
|
+
{
|
|
198
|
+
const {start: n, end: r} = e;
|
|
199
|
+
if (t >= n) return r >= t ? n : t + n - r;
|
|
200
|
+
break;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
case a:
|
|
204
|
+
{
|
|
205
|
+
const {at: n, text: r} = e;
|
|
206
|
+
if (t >= n) return t + r.length;
|
|
207
|
+
break;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
case f:
|
|
211
|
+
{
|
|
212
|
+
const {at: n, fragment: r} = e;
|
|
213
|
+
if (t >= n) return t + w({
|
|
214
|
+
children: r
|
|
215
|
+
});
|
|
216
|
+
break;
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
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
|
+
y(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
|
+
}
|
|
250
|
+
|
|
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 => y(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 = L(t.selection)) {
|
|
278
|
+
t.apply((new p).delete(...e));
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
function z(t, e, n = t.selection[0]) {
|
|
282
|
+
t.apply((new p).insertText(n, e));
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
function J(t, e, n = t.selection[0]) {
|
|
286
|
+
t.apply((new p).insertFragment(n, [ {
|
|
287
|
+
children: [ e ]
|
|
288
|
+
} ]));
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
function U(t, e) {
|
|
292
|
+
const [n, r] = L(t.selection);
|
|
293
|
+
t.apply((new p).delete(n, r).insertText(n, e));
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
function G(t, e) {
|
|
297
|
+
t.apply((new p).delete(0, w(t.doc)).insertFragment(0, e));
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
function W(t, e, n, r = L(t.selection)) {
|
|
301
|
+
t.apply((new p).format(...r, e, n));
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
function j(t, e, n = L(t.selection)) {
|
|
305
|
+
const r = R(t.doc, ...n).flatMap(t => t.children.filter(y));
|
|
306
|
+
r.length && t.apply((new p).format(...n, e, !!r.some(t => !t[e])));
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
function X(t, e, n, r = _(t.doc, t.selection[0])[0]) {
|
|
310
|
+
t.apply((new p).attr(r, e, n));
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
function Y(t, e, n, r, o = _(t.doc, t.selection[0])[0]) {
|
|
314
|
+
const s = A(t.doc, o);
|
|
315
|
+
t.apply((new p).attr(o, e, s[e] === n ? r : n));
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
let K = null, Q = null, Z = null, $ = null, tt = null;
|
|
319
|
+
|
|
320
|
+
const et = t => 1 === t.nodeType, nt = t => 8 === t.nodeType, rt = () => Q, ot = () => {
|
|
321
|
+
const t = st();
|
|
322
|
+
return 1 === t ? Q.data.length : 2 === t ? 1 : 0;
|
|
323
|
+
}, st = () => {
|
|
324
|
+
if (null != Z) return Z;
|
|
325
|
+
if (Q) if ((t => 3 === t.nodeType)(Q)) {
|
|
326
|
+
const t = Q.data;
|
|
327
|
+
if (t) return Z = "\n" === t ? at() ? 3 : 6 : 1;
|
|
328
|
+
} else if (et(Q)) {
|
|
329
|
+
if ("BR" === Q.tagName) return Z = at() ? 3 : 5;
|
|
330
|
+
if ($.u(Q)) return Z = 2;
|
|
331
|
+
if ($.h(Q)) return Z = 4;
|
|
332
|
+
}
|
|
333
|
+
return Z = 0;
|
|
334
|
+
}, ct = () => (Z = null, Q = K.nextNode()), it = () => {
|
|
335
|
+
for (;(Z = null) || (Q = K.nextSibling()); ) if (4 === st()) return;
|
|
336
|
+
}, lt = () => {
|
|
337
|
+
for (;(Z = null) || (Q = K.parentNode()); ) if (4 === st()) return;
|
|
338
|
+
}, at = () => {
|
|
339
|
+
const t = Q.parentNode;
|
|
340
|
+
return tt(() => {
|
|
341
|
+
for (;ct(); ) {
|
|
342
|
+
if (st()) return !0;
|
|
343
|
+
if (!t.contains(Q)) break;
|
|
344
|
+
}
|
|
345
|
+
return !1;
|
|
346
|
+
});
|
|
347
|
+
}, ft = () => {
|
|
348
|
+
for (;;) {
|
|
349
|
+
if (2 === st()) {
|
|
350
|
+
const t = Q;
|
|
351
|
+
for (;ct() && t.contains(Q); ) ;
|
|
352
|
+
} else ct();
|
|
353
|
+
if (!Q) break;
|
|
354
|
+
const t = st();
|
|
355
|
+
if (t) return t;
|
|
356
|
+
}
|
|
357
|
+
}, ut = new Set([ "DIV", "H1", "H2", "H3", "H4", "H5", "H6", "P", "PRE", "LI", "DT", "DD", "TR" ]), dt = t => ut.has(t.tagName), ht = new Set([ "EMBED", "IMG", "PICTURE", "AUDIO", "VIDEO", "SVG", "CANVAS", "MATH", "IFRAME", "OBJECT" ]), pt = t => "false" === t.contentEditable || ht.has(t.tagName), yt = (t, e) => t.compareDocumentPosition(e), gt = t => t.ownerDocument, kt = t => gt(t).getSelection(), mt = (t, e) => {
|
|
358
|
+
if (t.rangeCount) {
|
|
359
|
+
const n = t.getRangeAt(0);
|
|
360
|
+
if (e.contains(n.commonAncestorContainer)) return n;
|
|
361
|
+
}
|
|
362
|
+
}, bt = (t, e, n, r) => {
|
|
363
|
+
const o = kt(t);
|
|
364
|
+
(n || mt(o, t)) && (o.removeAllRanges(), o.addRange(e), r && (o.collapseToEnd(),
|
|
365
|
+
o.extend(e.startContainer, e.startOffset)));
|
|
366
|
+
}, wt = (t, e, [n, r], o, s, c) => {
|
|
367
|
+
const i = 0 === o, l = o > 0, a = l ? r : n, f = l ? n : r;
|
|
368
|
+
if (0 === a[0].length && 0 === a[1] && i && !e.hasChildNodes()) {
|
|
369
|
+
const n = t.createRange();
|
|
370
|
+
return n.setStart(e, 0), n.setEnd(e, 0), bt(e, n, c);
|
|
371
|
+
}
|
|
372
|
+
const u = xt(e, a, s);
|
|
373
|
+
if (!u) return;
|
|
374
|
+
const d = i ? u : xt(e, f, s);
|
|
375
|
+
if (!d) return;
|
|
376
|
+
const h = t.createRange(), [p, y] = u, [g, k] = d;
|
|
377
|
+
et(p) ? y < 1 ? h.setStartBefore(p) : h.setStartAfter(p) : h.setStart(p, y), et(g) ? k < 1 ? h.setEndBefore(g) : h.setEndAfter(g) : h.setEnd(g, k),
|
|
378
|
+
bt(e, h, c, l);
|
|
379
|
+
}, xt = (t, [e, n], r) => r(() => {
|
|
380
|
+
let t, r = 0;
|
|
381
|
+
for (;t = ft(); ) if (4 === t) {
|
|
382
|
+
if (r < e.length) for (let t = e[r++]; t > 0; t--) it();
|
|
383
|
+
} else {
|
|
384
|
+
const t = ot();
|
|
385
|
+
if (n <= t) return [ rt(), n ];
|
|
386
|
+
n -= t;
|
|
387
|
+
}
|
|
388
|
+
}, t), _t = (e, n, r, o) => {
|
|
389
|
+
let s = !0;
|
|
390
|
+
if (e === n && !n.hasChildNodes()) return [ [ 0 ], 0 ];
|
|
391
|
+
if (et(n) && n.hasChildNodes()) {
|
|
392
|
+
const e = t(r, n.childNodes.length - 1);
|
|
393
|
+
n = n.childNodes[e], s = e === r, r = 0;
|
|
394
|
+
}
|
|
395
|
+
return o(() => {
|
|
396
|
+
4 !== st() && lt();
|
|
397
|
+
const t = o(() => {
|
|
398
|
+
const t = [];
|
|
399
|
+
let n;
|
|
400
|
+
for (;(n = rt()) && n !== e; ) t.unshift(n), lt();
|
|
401
|
+
if (!t.length) return [ 0 ];
|
|
402
|
+
let r = 0, o = t[t.length - 1];
|
|
403
|
+
for (;o = o.previousElementSibling; ) r++;
|
|
404
|
+
return [ r ];
|
|
405
|
+
});
|
|
406
|
+
let c = 0;
|
|
407
|
+
for (;ft(); ) {
|
|
408
|
+
const t = yt(n, rt());
|
|
409
|
+
if (0 === t || 16 & t) {
|
|
410
|
+
if (s) break;
|
|
411
|
+
} else if (4 & t) break;
|
|
412
|
+
c += ot();
|
|
413
|
+
}
|
|
414
|
+
return [ t, c + r ];
|
|
415
|
+
}, e, n);
|
|
416
|
+
}, vt = (t, e, {startOffset: n, startContainer: r, endOffset: o, endContainer: s}) => {
|
|
417
|
+
const c = _t(t, r, n, e);
|
|
418
|
+
return [ c, r === s && n === o ? c : _t(t, s, o, e) ];
|
|
419
|
+
}, Mt = t => (e, n) => {
|
|
420
|
+
e.setData("text/plain", c({
|
|
421
|
+
children: n
|
|
422
|
+
}, t));
|
|
423
|
+
}, Dt = () => (t, e, n) => {
|
|
424
|
+
const r = document.createElement("div");
|
|
425
|
+
r.appendChild(mt(kt(n), n).cloneContents()), t.setData("text/html", r.innerHTML);
|
|
426
|
+
}, Et = "application/x-editate-editor", Ot = ({key: t = Et} = {}) => (e, n) => {
|
|
427
|
+
e.setData(t, JSON.stringify(n));
|
|
428
|
+
}, St = t => e => {
|
|
429
|
+
for (const n of e.items) if ("file" === n.kind) {
|
|
430
|
+
const e = t[n.type];
|
|
431
|
+
if (e) {
|
|
432
|
+
const t = n.getAsFile();
|
|
433
|
+
if (t) return [ {
|
|
434
|
+
children: [ e(t) ]
|
|
435
|
+
} ];
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
return null;
|
|
439
|
+
}, Ct = () => t => t.getData("text/plain"), It = (t, e = []) => (n, r) => {
|
|
440
|
+
const o = n.getData("text/html");
|
|
441
|
+
if (o) {
|
|
442
|
+
let n = (new DOMParser).parseFromString(o, "text/html").body, s = !1;
|
|
443
|
+
for (const t of [ ...n.childNodes ]) nt(t) ? "StartFragment" === t.data ? (s = !0,
|
|
444
|
+
n = new DocumentFragment) : "EndFragment" === t.data && (s = !1) : s && n.appendChild(t);
|
|
445
|
+
return ((t, e, n, r) => e(() => {
|
|
446
|
+
let t, e = null, o = "", s = !1;
|
|
447
|
+
const c = [], i = () => {
|
|
448
|
+
o && (e || (e = []), e.push(n(o)), o = "");
|
|
449
|
+
}, l = () => {
|
|
450
|
+
i(), !e && s && (e = []), e && c.push({
|
|
451
|
+
children: e
|
|
452
|
+
}), e = null, s = !1;
|
|
453
|
+
};
|
|
454
|
+
for (;t = ft(); ) if (4 === t) l(); else if (s = !0, 1 === t) o += rt().data; else if (2 === t) {
|
|
455
|
+
i();
|
|
456
|
+
const t = r(rt());
|
|
457
|
+
t && e.push(t);
|
|
458
|
+
} else 3 === t && l();
|
|
459
|
+
return l(), c.length || c.push({
|
|
460
|
+
children: []
|
|
461
|
+
}), c;
|
|
462
|
+
}, t))(n, r, t, t => {
|
|
463
|
+
for (const n of e) {
|
|
464
|
+
const e = n(t);
|
|
465
|
+
if (e) return e;
|
|
466
|
+
}
|
|
467
|
+
});
|
|
468
|
+
}
|
|
469
|
+
return null;
|
|
470
|
+
}, At = ({key: t = Et} = {}) => e => {
|
|
471
|
+
try {
|
|
472
|
+
return JSON.parse(e.getData(t));
|
|
473
|
+
} catch (t) {
|
|
474
|
+
return null;
|
|
475
|
+
}
|
|
476
|
+
}, Ht = (t, e, {mod: n, shift: r = !1, alt: o = !1} = {}) => (t = t.toLowerCase(),
|
|
477
|
+
s => {
|
|
478
|
+
if (s.key.toLowerCase() === t && (!n || s.ctrlKey || s.metaKey) && r === s.shiftKey && o === s.altKey) return e(s),
|
|
479
|
+
!0;
|
|
480
|
+
});
|
|
481
|
+
|
|
482
|
+
function Pt(t) {
|
|
483
|
+
let e = 0, r = 0, o = !1;
|
|
484
|
+
const s = Date.now, c = [ [ t.doc, t.selection, [] ] ], i = () => c[e];
|
|
485
|
+
t.hook("apply", (i, l) => {
|
|
486
|
+
if (o) return;
|
|
487
|
+
const a = t.doc, f = t.selection;
|
|
488
|
+
l(i);
|
|
489
|
+
const u = t.doc;
|
|
490
|
+
if (!n(a, u)) {
|
|
491
|
+
const t = s();
|
|
492
|
+
if (0 === e || t - r >= 500) {
|
|
493
|
+
e++;
|
|
494
|
+
const t = [ a, f, [] ];
|
|
495
|
+
e >= c.length ? c.push(t) : c[e] = t;
|
|
496
|
+
}
|
|
497
|
+
r = t, c[e][0] = u, c[e][2].push(i), c.splice(e + 1), e > 500 && (e--, c.shift());
|
|
498
|
+
}
|
|
499
|
+
}), t.hook("keyboard", Ht("z", () => {
|
|
500
|
+
if (e > 0) {
|
|
501
|
+
const r = i()[1];
|
|
502
|
+
e--;
|
|
503
|
+
const s = t.doc;
|
|
504
|
+
o = !0, t.exec(G, i()[0].children), o = !1, n(s, t.doc) || (t.selection = r);
|
|
505
|
+
}
|
|
506
|
+
}, {
|
|
507
|
+
mod: !0
|
|
508
|
+
})), t.hook("keyboard", Ht("z", () => {
|
|
509
|
+
if (e < c.length - 1) {
|
|
510
|
+
e++;
|
|
511
|
+
const [r, s, c] = i(), l = t.doc;
|
|
512
|
+
o = !0, t.exec(G, r.children), o = !1, n(l, t.doc) || (t.selection = c.reduce((t, e) => T(t, e), s));
|
|
513
|
+
}
|
|
514
|
+
}, {
|
|
515
|
+
mod: !0,
|
|
516
|
+
shift: !0
|
|
517
|
+
}));
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
const Rt = [], Bt = () => {}, Ft = ({doc: t, readonly: e = !1, schema: c, keyboard: i, copy: l = [ Mt() ], paste: a = [ t => t.getData("text/plain") ], isBlock: f = dt, onChange: u, onError: d = console.error}) => {
|
|
521
|
+
let y = [ 0, 0 ];
|
|
522
|
+
const g = (t, e) => {
|
|
523
|
+
if (!c) return e("An unsafe operation was detected. We recommend using schema option."),
|
|
524
|
+
!0;
|
|
525
|
+
const n = c["~standard"].validate(t);
|
|
526
|
+
if (n instanceof Promise) e("async validate is not supported."); else {
|
|
527
|
+
if (!n.issues) return !0;
|
|
528
|
+
e(n.issues.map(t => t.message).join("\n"));
|
|
529
|
+
}
|
|
530
|
+
return !1;
|
|
531
|
+
};
|
|
532
|
+
let k;
|
|
533
|
+
if (!g(t, t => {
|
|
534
|
+
k = t;
|
|
535
|
+
}) && k) throw Error(k);
|
|
536
|
+
const m = new Map, b = t => m.get(t) || Rt, w = new Map, _ = t => {
|
|
537
|
+
const e = w.get(t);
|
|
538
|
+
e && !e[1] && (e[1] = !0, s(() => {
|
|
539
|
+
e[1] = !1, e[0].forEach(t => {
|
|
540
|
+
t();
|
|
541
|
+
});
|
|
542
|
+
}));
|
|
543
|
+
}, D = r => {
|
|
544
|
+
if (!e) {
|
|
545
|
+
const e = t, o = b("apply"), s = o.length;
|
|
546
|
+
for (let e of r.ops) {
|
|
547
|
+
let n = 0;
|
|
548
|
+
const r = () => {
|
|
549
|
+
if (n < s) {
|
|
550
|
+
const t = n;
|
|
551
|
+
o[n](e, c), t === n && c();
|
|
552
|
+
} else if (n === s) {
|
|
553
|
+
n++;
|
|
554
|
+
try {
|
|
555
|
+
const [n, r] = V(t, y, e);
|
|
556
|
+
h(e) && !g(n, d) || (t = n, y = r);
|
|
557
|
+
} catch (t) {
|
|
558
|
+
d("rollback operation: " + t);
|
|
559
|
+
}
|
|
560
|
+
}
|
|
561
|
+
}, c = t => {
|
|
562
|
+
t && (e = t), n++, r();
|
|
563
|
+
};
|
|
564
|
+
r();
|
|
565
|
+
}
|
|
566
|
+
n(e, t) || _("change");
|
|
567
|
+
}
|
|
568
|
+
}, E = e => {
|
|
569
|
+
!((t, [e, n]) => B(t, e) && B(t, n))(t, e) || y[0] === e[0] && y[1] === e[1] || (y = e,
|
|
570
|
+
_("selectionchange"));
|
|
571
|
+
}, O = {
|
|
572
|
+
get doc() {
|
|
573
|
+
return t;
|
|
574
|
+
},
|
|
575
|
+
get selection() {
|
|
576
|
+
return y;
|
|
577
|
+
},
|
|
578
|
+
set selection(t) {
|
|
579
|
+
E(t);
|
|
580
|
+
},
|
|
581
|
+
get readonly() {
|
|
582
|
+
return e;
|
|
583
|
+
},
|
|
584
|
+
set readonly(t) {
|
|
585
|
+
e = t, _("readonly");
|
|
586
|
+
},
|
|
587
|
+
apply: t => (D(t), O),
|
|
588
|
+
on: (t, e) => {
|
|
589
|
+
let n = w.get(t);
|
|
590
|
+
n || w.set(t, n = [ new Set, !1 ]);
|
|
591
|
+
const r = n[0];
|
|
592
|
+
return r.add(e), () => {
|
|
593
|
+
r.delete(e);
|
|
594
|
+
};
|
|
595
|
+
},
|
|
596
|
+
hook: (t, e) => {
|
|
597
|
+
let n = m.get(t);
|
|
598
|
+
return n || m.set(t, n = []), n.push(e), () => {
|
|
599
|
+
const t = n.indexOf(e);
|
|
600
|
+
-1 !== t && n.splice(t, 1);
|
|
601
|
+
};
|
|
602
|
+
},
|
|
603
|
+
exec: (t, ...e) => {
|
|
604
|
+
const n = t(O, ...e);
|
|
605
|
+
return void 0 === n ? O : n;
|
|
606
|
+
},
|
|
607
|
+
input: n => {
|
|
608
|
+
if (!window.InputEvent || !o(InputEvent.prototype.getTargetRanges)) return d("beforeinput event is not supported."),
|
|
609
|
+
Bt;
|
|
610
|
+
const {contentEditable: s, role: c, ariaMultiLine: i, ariaReadOnly: u} = n, h = n.style.whiteSpace;
|
|
611
|
+
n.role = "textbox", n.style.whiteSpace = "pre-wrap", n.ariaMultiLine = "true";
|
|
612
|
+
let g = !1, k = null, m = !1, w = !1, _ = !1;
|
|
613
|
+
const S = gt(n), C = (t => {
|
|
614
|
+
const e = (n, r, o) => {
|
|
615
|
+
const s = $, c = tt, i = K, l = Q, a = Z;
|
|
616
|
+
try {
|
|
617
|
+
return K || ($ = t, tt = e, K = $.p.createTreeWalker(r, 5)), o && (K.currentNode = Q = o),
|
|
618
|
+
n();
|
|
619
|
+
} finally {
|
|
620
|
+
$ = s, tt = c, K = i, Q = l, Z = a, K && l && (K.currentNode = l);
|
|
621
|
+
}
|
|
622
|
+
};
|
|
623
|
+
return e;
|
|
624
|
+
})({
|
|
625
|
+
p: S,
|
|
626
|
+
h: f,
|
|
627
|
+
u: pt
|
|
628
|
+
}), I = () => {
|
|
629
|
+
n.contentEditable = e ? "false" : "true", n.ariaReadOnly = e ? "true" : null;
|
|
630
|
+
};
|
|
631
|
+
I();
|
|
632
|
+
const A = O.on("change", () => {
|
|
633
|
+
w || requestAnimationFrame(() => {
|
|
634
|
+
w || n.focus({
|
|
635
|
+
preventScroll: !0
|
|
636
|
+
});
|
|
637
|
+
});
|
|
638
|
+
}), H = O.on("readonly", I), P = t => {
|
|
639
|
+
for (const e of a) {
|
|
640
|
+
const n = e(t, C);
|
|
641
|
+
if (n) return n;
|
|
642
|
+
}
|
|
643
|
+
d("failed to serialize pasted data");
|
|
644
|
+
}, B = ((t, e) => {
|
|
645
|
+
let n = !1;
|
|
646
|
+
const r = [], o = t => {
|
|
647
|
+
n && r.push(...t);
|
|
648
|
+
}, s = new MutationObserver(t => {
|
|
649
|
+
o(t), n || e();
|
|
650
|
+
}), c = () => {
|
|
651
|
+
o(s.takeRecords());
|
|
652
|
+
}, i = () => (c(), r.splice(0));
|
|
653
|
+
return s.observe(t, {
|
|
654
|
+
characterData: !0,
|
|
655
|
+
characterDataOldValue: !0,
|
|
656
|
+
childList: !0,
|
|
657
|
+
subtree: !0
|
|
658
|
+
}), {
|
|
659
|
+
k: t => {
|
|
660
|
+
let e;
|
|
661
|
+
for (;e = t.pop(); ) if ("childList" === e.type) {
|
|
662
|
+
const {target: t, removedNodes: n, addedNodes: r, nextSibling: o} = e;
|
|
663
|
+
for (let e = n.length - 1; e >= 0; e--) t.insertBefore(n[e], o);
|
|
664
|
+
for (let e = r.length - 1; e >= 0; e--) t.removeChild(r[e]);
|
|
665
|
+
} else e.target.data = e.oldValue;
|
|
666
|
+
i();
|
|
667
|
+
},
|
|
668
|
+
m: t => {
|
|
669
|
+
!n && t && c(), n = t;
|
|
670
|
+
},
|
|
671
|
+
_: i,
|
|
672
|
+
v: () => {
|
|
673
|
+
r.splice(0), s.disconnect();
|
|
674
|
+
}
|
|
675
|
+
};
|
|
676
|
+
})(n, () => {
|
|
677
|
+
wt(S, n, M(t, y), y[0] - y[1], C);
|
|
678
|
+
}), F = () => {
|
|
679
|
+
E(v(t, ((t, e) => {
|
|
680
|
+
const n = kt(t), r = mt(n, t);
|
|
681
|
+
if (!r) return [ [ [ 0 ], 0 ], [ [ 0 ], 0 ] ];
|
|
682
|
+
const o = vt(t, e, r), s = yt(n.anchorNode, n.focusNode);
|
|
683
|
+
return (0 === s ? n.anchorOffset > n.focusOffset : 2 & s) ? [ o[1], o[0] ] : o;
|
|
684
|
+
})(n, C)));
|
|
685
|
+
}, T = () => {
|
|
686
|
+
const e = B._();
|
|
687
|
+
B.m(!1), e.length && (B.k(e), S.removeEventListener("selectionchange", j), wt(S, n, M(t, y), y[0] - y[1], C, !0),
|
|
688
|
+
S.addEventListener("selectionchange", j)), k && (E(k[1]), D(k[0]), k = null), m = !1;
|
|
689
|
+
}, V = t => {
|
|
690
|
+
if (!m) for (const e of b("keyboard")) if (e(t)) return t.preventDefault(), void B.m(!1);
|
|
691
|
+
}, q = () => {
|
|
692
|
+
m || T();
|
|
693
|
+
}, z = e => {
|
|
694
|
+
e.preventDefault();
|
|
695
|
+
const r = e.inputType;
|
|
696
|
+
if (r.startsWith("format")) return;
|
|
697
|
+
if ("historyUndo" === r || "historyRedo" === r) return;
|
|
698
|
+
m ? B.m(!0) : F();
|
|
699
|
+
const o = e.getTargetRanges()[0];
|
|
700
|
+
if (o) {
|
|
701
|
+
const s = v(t, vt(n, C, o));
|
|
702
|
+
let c, i = "insertParagraph" === r || "insertLineBreak" === r ? "\n" : e.data;
|
|
703
|
+
if (null == i) {
|
|
704
|
+
const t = e.dataTransfer;
|
|
705
|
+
t && (i = t.getData("text/plain"));
|
|
706
|
+
}
|
|
707
|
+
k || (k = [ new p, y ]), c = k[0], N(s) || c.delete(...s), i && c.insertText(s[0], i);
|
|
708
|
+
}
|
|
709
|
+
m || T();
|
|
710
|
+
}, J = () => {
|
|
711
|
+
m || F(), m = !0;
|
|
712
|
+
}, U = () => {
|
|
713
|
+
T();
|
|
714
|
+
}, G = () => {
|
|
715
|
+
w = !0, F();
|
|
716
|
+
}, W = () => {
|
|
717
|
+
w = !1;
|
|
718
|
+
}, j = () => {
|
|
719
|
+
!w || m || _ || F();
|
|
720
|
+
}, X = e => {
|
|
721
|
+
if (F(), !N(y)) {
|
|
722
|
+
const r = R(t, ...L(y));
|
|
723
|
+
for (const t of l) t(e, r, n);
|
|
724
|
+
}
|
|
725
|
+
}, Y = t => {
|
|
726
|
+
t.preventDefault(), X(t.clipboardData);
|
|
727
|
+
}, et = t => {
|
|
728
|
+
t.preventDefault(), e || (X(t.clipboardData), D((new p).delete(...L(y))));
|
|
729
|
+
}, nt = t => {
|
|
730
|
+
t.preventDefault();
|
|
731
|
+
const e = P(t.clipboardData);
|
|
732
|
+
if (e) {
|
|
733
|
+
const [t, n] = L(y), o = (new p).delete(t, n);
|
|
734
|
+
r(e) ? o.insertText(t, e) : o.insertFragment(t, e), D(o);
|
|
735
|
+
}
|
|
736
|
+
}, rt = e => {
|
|
737
|
+
e.preventDefault();
|
|
738
|
+
const o = e.dataTransfer, s = ((t, e, {clientX: n, clientY: r}, o) => {
|
|
739
|
+
if (t.caretPositionFromPoint) {
|
|
740
|
+
const s = t.caretPositionFromPoint(n, r);
|
|
741
|
+
if (s) return _t(e, s.offsetNode, s.offset, o);
|
|
742
|
+
} else if (t.caretRangeFromPoint) {
|
|
743
|
+
const s = t.caretRangeFromPoint(n, r);
|
|
744
|
+
if (s) return _t(e, s.startContainer, s.startOffset, o);
|
|
745
|
+
}
|
|
746
|
+
})(S, n, e, C);
|
|
747
|
+
if (o && s) {
|
|
748
|
+
let e;
|
|
749
|
+
const n = new p;
|
|
750
|
+
_ && n.delete(...L(y));
|
|
751
|
+
const c = P(o);
|
|
752
|
+
if (c) {
|
|
753
|
+
const o = x(t, s), i = n.transform(o);
|
|
754
|
+
r(c) ? n.insertText(i, c) : n.insertFragment(i, c), e = [ i, n.transform(o) ];
|
|
755
|
+
}
|
|
756
|
+
D(n), e && E(e);
|
|
757
|
+
}
|
|
758
|
+
}, ot = t => {
|
|
759
|
+
_ = !0, X(t.dataTransfer);
|
|
760
|
+
}, st = () => {
|
|
761
|
+
_ = !1;
|
|
762
|
+
};
|
|
763
|
+
S.addEventListener("selectionchange", j), n.addEventListener("keydown", V), n.addEventListener("input", q),
|
|
764
|
+
n.addEventListener("beforeinput", z), n.addEventListener("compositionstart", J),
|
|
765
|
+
n.addEventListener("compositionend", U), n.addEventListener("focus", G), n.addEventListener("blur", W),
|
|
766
|
+
n.addEventListener("copy", Y), n.addEventListener("cut", et), n.addEventListener("paste", nt),
|
|
767
|
+
n.addEventListener("drop", rt), n.addEventListener("dragstart", ot), n.addEventListener("dragend", st);
|
|
768
|
+
const ct = b("mount"), it = [];
|
|
769
|
+
return ct.forEach(t => {
|
|
770
|
+
const e = t(n);
|
|
771
|
+
e && it.push(e);
|
|
772
|
+
}), () => {
|
|
773
|
+
g || (g = !0, A(), H(), n.contentEditable = s, n.role = c, n.ariaMultiLine = i,
|
|
774
|
+
n.ariaReadOnly = u, n.style.whiteSpace = h, B.v(), S.removeEventListener("selectionchange", j),
|
|
775
|
+
n.removeEventListener("keydown", V), n.removeEventListener("input", q), n.removeEventListener("beforeinput", z),
|
|
776
|
+
n.removeEventListener("compositionstart", J), n.removeEventListener("compositionend", U),
|
|
777
|
+
n.removeEventListener("focus", G), n.removeEventListener("blur", W), n.removeEventListener("copy", Y),
|
|
778
|
+
n.removeEventListener("cut", et), n.removeEventListener("paste", nt), n.removeEventListener("drop", rt),
|
|
779
|
+
n.removeEventListener("dragstart", ot), n.removeEventListener("dragend", st), it.forEach(t => {
|
|
780
|
+
t();
|
|
781
|
+
}));
|
|
782
|
+
};
|
|
783
|
+
}
|
|
784
|
+
};
|
|
785
|
+
return O.exec(Pt), O.on("change", () => {
|
|
786
|
+
u(t);
|
|
787
|
+
}), i && i.forEach(t => {
|
|
788
|
+
O.hook("keyboard", t);
|
|
789
|
+
}), O;
|
|
790
|
+
};
|
|
791
|
+
|
|
792
|
+
function Tt(t) {
|
|
793
|
+
t.hook("apply", t => {
|
|
794
|
+
console.log("apply", t);
|
|
795
|
+
}), t.on("change", () => {
|
|
796
|
+
console.log("change", t.doc);
|
|
797
|
+
}), t.on("selectionchange", () => {
|
|
798
|
+
console.log("selectionchange", t.selection);
|
|
799
|
+
});
|
|
800
|
+
}
|
|
801
|
+
|
|
802
|
+
function Vt(t) {
|
|
803
|
+
t.hook("mount", t => {
|
|
804
|
+
t.ariaMultiLine = null;
|
|
805
|
+
}), t.hook("apply", (t, e) => {
|
|
806
|
+
"insert_text" === t.type ? t = {
|
|
807
|
+
...t,
|
|
808
|
+
text: t.text.replaceAll("\n", "")
|
|
809
|
+
} : "insert_node" === t.type && (t = {
|
|
810
|
+
...t,
|
|
811
|
+
fragment: [ S(...t.fragment) ]
|
|
812
|
+
}), e(t);
|
|
813
|
+
});
|
|
814
|
+
}
|
|
815
|
+
|
|
816
|
+
const Lt = ({text: t, singleline: e, onChange: n, ...r}) => {
|
|
817
|
+
const o = Ft({
|
|
818
|
+
...r,
|
|
819
|
+
doc: {
|
|
820
|
+
children: i(t)
|
|
821
|
+
},
|
|
822
|
+
onChange: t => {
|
|
823
|
+
n(c(t));
|
|
824
|
+
}
|
|
825
|
+
});
|
|
826
|
+
return e && o.exec(Vt), o;
|
|
827
|
+
};
|
|
828
|
+
|
|
829
|
+
export { q as Delete, W as Format, J as InsertNode, z as InsertText, G as ReplaceDoc, U as ReplaceText, X as SetBlockAttr, Y as ToggleBlockAttr, j as ToggleFormat, p as Transaction, Ft as createEditor, Lt as createPlainEditor, Tt as debugPlugin, St as filePaste, Ht as hotkey, Dt as htmlCopy, It as htmlPaste, Ot as internalCopy, At as internalPaste, Mt as plainCopy, Ct as plainPaste, Vt as singlelinePlugin };
|
|
830
|
+
//# sourceMappingURL=index.js.map
|