tiptap-extension-code-block-shiki 0.4.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.
@@ -1,1453 +0,0 @@
1
- import { Plugin as $, PluginKey as N, Selection as O, TextSelection as b, NodeSelection as ae } from "@tiptap/pm/state";
2
- import { DecorationSet as xe, Decoration as Se } from "@tiptap/pm/view";
3
- import "@tiptap/pm/keymap";
4
- import { Fragment as L, Slice as be, DOMParser as Me } from "@tiptap/pm/model";
5
- import { liftTarget as Ae, joinPoint as ce, canSplit as R, ReplaceStep as Te, ReplaceAroundStep as Ce, canJoin as le } from "@tiptap/pm/transform";
6
- import { createParagraphNear as Ee, deleteSelection as Be, exitCode as ve, joinUp as Ie, joinDown as Pe, joinBackward as $e, joinForward as Ne, joinTextblockBackward as Oe, joinTextblockForward as je, lift as Fe, liftEmptyBlock as Le, newlineInCode as Re, selectNodeBackward as De, selectNodeForward as ze, selectParentNode as We, selectTextblockEnd as He, selectTextblockStart as Ke, setBlockType as ee, wrapIn as Ve } from "@tiptap/pm/commands";
7
- import { liftListItem as _e, sinkListItem as Je, wrapInList as Ue } from "@tiptap/pm/schema-list";
8
- import { bundledThemes as de, bundledLanguages as ue, createHighlighter as Ge } from "shiki";
9
- function fe(e) {
10
- const { state: t, transaction: n } = e;
11
- let { selection: o } = n, { doc: r } = n, { storedMarks: s } = n;
12
- return {
13
- ...t,
14
- apply: t.apply.bind(t),
15
- applyTransaction: t.applyTransaction.bind(t),
16
- plugins: t.plugins,
17
- schema: t.schema,
18
- reconfigure: t.reconfigure.bind(t),
19
- toJSON: t.toJSON.bind(t),
20
- get storedMarks() {
21
- return s;
22
- },
23
- get selection() {
24
- return o;
25
- },
26
- get doc() {
27
- return r;
28
- },
29
- get tr() {
30
- return o = n.selection, r = n.doc, s = n.storedMarks, n;
31
- }
32
- };
33
- }
34
- class qe {
35
- constructor(t) {
36
- this.editor = t.editor, this.rawCommands = this.editor.extensionManager.commands, this.customState = t.state;
37
- }
38
- get hasCustomState() {
39
- return !!this.customState;
40
- }
41
- get state() {
42
- return this.customState || this.editor.state;
43
- }
44
- get commands() {
45
- const { rawCommands: t, editor: n, state: o } = this, { view: r } = n, { tr: s } = o, a = this.buildProps(s);
46
- return Object.fromEntries(Object.entries(t).map(([i, c]) => [i, (...u) => {
47
- const d = c(...u)(a);
48
- return !s.getMeta("preventDispatch") && !this.hasCustomState && r.dispatch(s), d;
49
- }]));
50
- }
51
- get chain() {
52
- return () => this.createChain();
53
- }
54
- get can() {
55
- return () => this.createCan();
56
- }
57
- createChain(t, n = !0) {
58
- const { rawCommands: o, editor: r, state: s } = this, { view: a } = r, i = [], c = !!t, l = t || s.tr, u = () => (!c && n && !l.getMeta("preventDispatch") && !this.hasCustomState && a.dispatch(l), i.every((f) => f === !0)), d = {
59
- ...Object.fromEntries(Object.entries(o).map(([f, p]) => [f, (...h) => {
60
- const g = this.buildProps(l, n), k = p(...h)(g);
61
- return i.push(k), d;
62
- }])),
63
- run: u
64
- };
65
- return d;
66
- }
67
- createCan(t) {
68
- const { rawCommands: n, state: o } = this, r = !1, s = t || o.tr, a = this.buildProps(s, r);
69
- return {
70
- ...Object.fromEntries(Object.entries(n).map(([c, l]) => [c, (...u) => l(...u)({ ...a, dispatch: void 0 })])),
71
- chain: () => this.createChain(s, r)
72
- };
73
- }
74
- buildProps(t, n = !0) {
75
- const { rawCommands: o, editor: r, state: s } = this, { view: a } = r, i = {
76
- tr: t,
77
- editor: r,
78
- view: a,
79
- state: fe({
80
- state: s,
81
- transaction: t
82
- }),
83
- dispatch: n ? () => {
84
- } : void 0,
85
- chain: () => this.createChain(t, n),
86
- can: () => this.createCan(t),
87
- get commands() {
88
- return Object.fromEntries(Object.entries(o).map(([c, l]) => [c, (...u) => l(...u)(i)]));
89
- }
90
- };
91
- return i;
92
- }
93
- }
94
- function w(e, t, n) {
95
- return e.config[t] === void 0 && e.parent ? w(e.parent, t, n) : typeof e.config[t] == "function" ? e.config[t].bind({
96
- ...n,
97
- parent: e.parent ? w(e.parent, t, n) : null
98
- }) : e.config[t];
99
- }
100
- function Qe(e) {
101
- const t = e.filter((r) => r.type === "extension"), n = e.filter((r) => r.type === "node"), o = e.filter((r) => r.type === "mark");
102
- return {
103
- baseExtensions: t,
104
- nodeExtensions: n,
105
- markExtensions: o
106
- };
107
- }
108
- function y(e, t) {
109
- if (typeof e == "string") {
110
- if (!t.nodes[e])
111
- throw Error(`There is no node type named '${e}'. Maybe you forgot to add the extension?`);
112
- return t.nodes[e];
113
- }
114
- return e;
115
- }
116
- function Xe(...e) {
117
- return e.filter((t) => !!t).reduce((t, n) => {
118
- const o = { ...t };
119
- return Object.entries(n).forEach(([r, s]) => {
120
- if (!o[r]) {
121
- o[r] = s;
122
- return;
123
- }
124
- if (r === "class") {
125
- const i = s ? s.split(" ") : [], c = o[r] ? o[r].split(" ") : [], l = i.filter((u) => !c.includes(u));
126
- o[r] = [...c, ...l].join(" ");
127
- } else
128
- r === "style" ? o[r] = [o[r], s].join("; ") : o[r] = s;
129
- }), o;
130
- }, {});
131
- }
132
- function Ye(e) {
133
- return typeof e == "function";
134
- }
135
- function x(e, t = void 0, ...n) {
136
- return Ye(e) ? t ? e.bind(t)(...n) : e(...n) : e;
137
- }
138
- function Ze(e) {
139
- return Object.prototype.toString.call(e) === "[object RegExp]";
140
- }
141
- class et {
142
- constructor(t) {
143
- this.find = t.find, this.handler = t.handler;
144
- }
145
- }
146
- function tt(e) {
147
- return Object.prototype.toString.call(e).slice(8, -1);
148
- }
149
- function V(e) {
150
- return tt(e) !== "Object" ? !1 : e.constructor === Object && Object.getPrototypeOf(e) === Object.prototype;
151
- }
152
- function Q(e, t) {
153
- const n = { ...e };
154
- return V(e) && V(t) && Object.keys(t).forEach((o) => {
155
- V(t[o]) ? o in e ? n[o] = Q(e[o], t[o]) : Object.assign(n, { [o]: t[o] }) : Object.assign(n, { [o]: t[o] });
156
- }), n;
157
- }
158
- class C {
159
- constructor(t = {}) {
160
- this.type = "extension", this.name = "extension", this.parent = null, this.child = null, this.config = {
161
- name: this.name,
162
- defaultOptions: {}
163
- }, this.config = {
164
- ...this.config,
165
- ...t
166
- }, this.name = this.config.name, t.defaultOptions && Object.keys(t.defaultOptions).length > 0 && console.warn(`[tiptap warn]: BREAKING CHANGE: "defaultOptions" is deprecated. Please use "addOptions" instead. Found in extension: "${this.name}".`), this.options = this.config.defaultOptions, this.config.addOptions && (this.options = x(w(this, "addOptions", {
167
- name: this.name
168
- }))), this.storage = x(w(this, "addStorage", {
169
- name: this.name,
170
- options: this.options
171
- })) || {};
172
- }
173
- static create(t = {}) {
174
- return new C(t);
175
- }
176
- configure(t = {}) {
177
- const n = this.extend();
178
- return n.options = Q(this.options, t), n.storage = x(w(n, "addStorage", {
179
- name: n.name,
180
- options: n.options
181
- })), n;
182
- }
183
- extend(t = {}) {
184
- const n = new C({ ...this.config, ...t });
185
- return n.parent = this, this.child = n, n.name = t.name ? t.name : n.parent.name, t.defaultOptions && console.warn(`[tiptap warn]: BREAKING CHANGE: "defaultOptions" is deprecated. Please use "addOptions" instead. Found in extension: "${n.name}".`), n.options = x(w(n, "addOptions", {
186
- name: n.name
187
- })), n.storage = x(w(n, "addStorage", {
188
- name: n.name,
189
- options: n.options
190
- })), n;
191
- }
192
- }
193
- function nt(e, t, n) {
194
- const { from: o, to: r } = t, { blockSeparator: s = `
195
-
196
- `, textSerializers: a = {} } = n || {};
197
- let i = "", c = !0;
198
- return e.nodesBetween(o, r, (l, u, d, f) => {
199
- var p;
200
- const m = a == null ? void 0 : a[l.type.name];
201
- if (m)
202
- return l.isBlock && !c && (i += s, c = !0), d && (i += m({
203
- node: l,
204
- pos: u,
205
- parent: d,
206
- index: f,
207
- range: t
208
- })), !1;
209
- l.isText ? (i += (p = l == null ? void 0 : l.text) === null || p === void 0 ? void 0 : p.slice(Math.max(o, u) - u, r - u), c = !1) : l.isBlock && !c && (i += s, c = !0);
210
- }), i;
211
- }
212
- function ot(e) {
213
- return Object.fromEntries(Object.entries(e.nodes).filter(([, t]) => t.spec.toText).map(([t, n]) => [t, n.spec.toText]));
214
- }
215
- C.create({
216
- name: "clipboardTextSerializer",
217
- addOptions() {
218
- return {
219
- blockSeparator: void 0
220
- };
221
- },
222
- addProseMirrorPlugins() {
223
- return [
224
- new $({
225
- key: new N("clipboardTextSerializer"),
226
- props: {
227
- clipboardTextSerializer: () => {
228
- const { editor: e } = this, { state: t, schema: n } = e, { doc: o, selection: r } = t, { ranges: s } = r, a = Math.min(...s.map((u) => u.$from.pos)), i = Math.max(...s.map((u) => u.$to.pos)), c = ot(n);
229
- return nt(o, { from: a, to: i }, {
230
- ...this.options.blockSeparator !== void 0 ? { blockSeparator: this.options.blockSeparator } : {},
231
- textSerializers: c
232
- });
233
- }
234
- }
235
- })
236
- ];
237
- }
238
- });
239
- const rt = () => ({ editor: e, view: t }) => (requestAnimationFrame(() => {
240
- var n;
241
- e.isDestroyed || (t.dom.blur(), (n = window == null ? void 0 : window.getSelection()) === null || n === void 0 || n.removeAllRanges());
242
- }), !0), st = (e = !1) => ({ commands: t }) => t.setContent("", e), it = () => ({ state: e, tr: t, dispatch: n }) => {
243
- const { selection: o } = t, { ranges: r } = o;
244
- return n && r.forEach(({ $from: s, $to: a }) => {
245
- e.doc.nodesBetween(s.pos, a.pos, (i, c) => {
246
- if (i.type.isText)
247
- return;
248
- const { doc: l, mapping: u } = t, d = l.resolve(u.map(c)), f = l.resolve(u.map(c + i.nodeSize)), p = d.blockRange(f);
249
- if (!p)
250
- return;
251
- const m = Ae(p);
252
- if (i.type.isTextblock) {
253
- const { defaultType: h } = d.parent.contentMatchAt(d.index());
254
- t.setNodeMarkup(p.start, h);
255
- }
256
- (m || m === 0) && t.lift(p, m);
257
- });
258
- }), !0;
259
- }, at = (e) => (t) => e(t), ct = () => ({ state: e, dispatch: t }) => Ee(e, t), lt = (e, t) => ({ editor: n, tr: o }) => {
260
- const { state: r } = n, s = r.doc.slice(e.from, e.to);
261
- o.deleteRange(e.from, e.to);
262
- const a = o.mapping.map(t);
263
- return o.insert(a, s.content), o.setSelection(new b(o.doc.resolve(a - 1))), !0;
264
- }, dt = () => ({ tr: e, dispatch: t }) => {
265
- const { selection: n } = e, o = n.$anchor.node();
266
- if (o.content.size > 0)
267
- return !1;
268
- const r = e.selection.$anchor;
269
- for (let s = r.depth; s > 0; s -= 1)
270
- if (r.node(s).type === o.type) {
271
- if (t) {
272
- const i = r.before(s), c = r.after(s);
273
- e.delete(i, c).scrollIntoView();
274
- }
275
- return !0;
276
- }
277
- return !1;
278
- }, ut = (e) => ({ tr: t, state: n, dispatch: o }) => {
279
- const r = y(e, n.schema), s = t.selection.$anchor;
280
- for (let a = s.depth; a > 0; a -= 1)
281
- if (s.node(a).type === r) {
282
- if (o) {
283
- const c = s.before(a), l = s.after(a);
284
- t.delete(c, l).scrollIntoView();
285
- }
286
- return !0;
287
- }
288
- return !1;
289
- }, ft = (e) => ({ tr: t, dispatch: n }) => {
290
- const { from: o, to: r } = e;
291
- return n && t.delete(o, r), !0;
292
- }, pt = () => ({ state: e, dispatch: t }) => Be(e, t), mt = () => ({ commands: e }) => e.keyboardShortcut("Enter"), ht = () => ({ state: e, dispatch: t }) => ve(e, t);
293
- function z(e, t, n = { strict: !0 }) {
294
- const o = Object.keys(t);
295
- return o.length ? o.every((r) => n.strict ? t[r] === e[r] : Ze(t[r]) ? t[r].test(e[r]) : t[r] === e[r]) : !0;
296
- }
297
- function q(e, t, n = {}) {
298
- return e.find((o) => o.type === t && z(o.attrs, n));
299
- }
300
- function gt(e, t, n = {}) {
301
- return !!q(e, t, n);
302
- }
303
- function pe(e, t, n = {}) {
304
- if (!e || !t)
305
- return;
306
- let o = e.parent.childAfter(e.parentOffset);
307
- if (e.parentOffset === o.offset && o.offset !== 0 && (o = e.parent.childBefore(e.parentOffset)), !o.node)
308
- return;
309
- const r = q([...o.node.marks], t, n);
310
- if (!r)
311
- return;
312
- let s = o.index, a = e.start() + o.offset, i = s + 1, c = a + o.node.nodeSize;
313
- for (q([...o.node.marks], t, n); s > 0 && r.isInSet(e.parent.child(s - 1).marks); )
314
- s -= 1, a -= e.parent.child(s).nodeSize;
315
- for (; i < e.parent.childCount && gt([...e.parent.child(i).marks], t, n); )
316
- c += e.parent.child(i).nodeSize, i += 1;
317
- return {
318
- from: a,
319
- to: c
320
- };
321
- }
322
- function I(e, t) {
323
- if (typeof e == "string") {
324
- if (!t.marks[e])
325
- throw Error(`There is no mark type named '${e}'. Maybe you forgot to add the extension?`);
326
- return t.marks[e];
327
- }
328
- return e;
329
- }
330
- const kt = (e, t = {}) => ({ tr: n, state: o, dispatch: r }) => {
331
- const s = I(e, o.schema), { doc: a, selection: i } = n, { $from: c, from: l, to: u } = i;
332
- if (r) {
333
- const d = pe(c, s, t);
334
- if (d && d.from <= l && d.to >= u) {
335
- const f = b.create(a, d.from, d.to);
336
- n.setSelection(f);
337
- }
338
- }
339
- return !0;
340
- }, yt = (e) => (t) => {
341
- const n = typeof e == "function" ? e(t) : e;
342
- for (let o = 0; o < n.length; o += 1)
343
- if (n[o](t))
344
- return !0;
345
- return !1;
346
- };
347
- function me(e) {
348
- return e instanceof b;
349
- }
350
- function P(e = 0, t = 0, n = 0) {
351
- return Math.min(Math.max(e, t), n);
352
- }
353
- function wt(e, t = null) {
354
- if (!t)
355
- return null;
356
- const n = O.atStart(e), o = O.atEnd(e);
357
- if (t === "start" || t === !0)
358
- return n;
359
- if (t === "end")
360
- return o;
361
- const r = n.from, s = o.to;
362
- return t === "all" ? b.create(e, P(0, r, s), P(e.content.size, r, s)) : b.create(e, P(t, r, s), P(t, r, s));
363
- }
364
- function X() {
365
- return [
366
- "iPad Simulator",
367
- "iPhone Simulator",
368
- "iPod Simulator",
369
- "iPad",
370
- "iPhone",
371
- "iPod"
372
- ].includes(navigator.platform) || navigator.userAgent.includes("Mac") && "ontouchend" in document;
373
- }
374
- const xt = (e = null, t = {}) => ({ editor: n, view: o, tr: r, dispatch: s }) => {
375
- t = {
376
- scrollIntoView: !0,
377
- ...t
378
- };
379
- const a = () => {
380
- X() && o.dom.focus(), requestAnimationFrame(() => {
381
- n.isDestroyed || (o.focus(), t != null && t.scrollIntoView && n.commands.scrollIntoView());
382
- });
383
- };
384
- if (o.hasFocus() && e === null || e === !1)
385
- return !0;
386
- if (s && e === null && !me(n.state.selection))
387
- return a(), !0;
388
- const i = wt(r.doc, e) || n.state.selection, c = n.state.selection.eq(i);
389
- return s && (c || r.setSelection(i), c && r.storedMarks && r.setStoredMarks(r.storedMarks), a()), !0;
390
- }, St = (e, t) => (n) => e.every((o, r) => t(o, { ...n, index: r })), bt = (e, t) => ({ tr: n, commands: o }) => o.insertContentAt({ from: n.selection.from, to: n.selection.to }, e, t), he = (e) => {
391
- const t = e.childNodes;
392
- for (let n = t.length - 1; n >= 0; n -= 1) {
393
- const o = t[n];
394
- o.nodeType === 3 && o.nodeValue && /^(\n\s\s|\n)$/.test(o.nodeValue) ? e.removeChild(o) : o.nodeType === 1 && he(o);
395
- }
396
- return e;
397
- };
398
- function te(e) {
399
- const t = `<body>${e}</body>`, n = new window.DOMParser().parseFromString(t, "text/html").body;
400
- return he(n);
401
- }
402
- function W(e, t, n) {
403
- if (n = {
404
- slice: !0,
405
- parseOptions: {},
406
- ...n
407
- }, typeof e == "object" && e !== null)
408
- try {
409
- return Array.isArray(e) && e.length > 0 ? L.fromArray(e.map((o) => t.nodeFromJSON(o))) : t.nodeFromJSON(e);
410
- } catch (o) {
411
- return console.warn("[tiptap warn]: Invalid content.", "Passed value:", e, "Error:", o), W("", t, n);
412
- }
413
- if (typeof e == "string") {
414
- const o = Me.fromSchema(t);
415
- return n.slice ? o.parseSlice(te(e), n.parseOptions).content : o.parse(te(e), n.parseOptions);
416
- }
417
- return W("", t, n);
418
- }
419
- function Mt(e, t, n) {
420
- const o = e.steps.length - 1;
421
- if (o < t)
422
- return;
423
- const r = e.steps[o];
424
- if (!(r instanceof Te || r instanceof Ce))
425
- return;
426
- const s = e.mapping.maps[o];
427
- let a = 0;
428
- s.forEach((i, c, l, u) => {
429
- a === 0 && (a = u);
430
- }), e.setSelection(O.near(e.doc.resolve(a), n));
431
- }
432
- const At = (e) => e.toString().startsWith("<"), Tt = (e, t, n) => ({ tr: o, dispatch: r, editor: s }) => {
433
- if (r) {
434
- n = {
435
- parseOptions: {},
436
- updateSelection: !0,
437
- applyInputRules: !1,
438
- applyPasteRules: !1,
439
- ...n
440
- };
441
- const a = W(t, s.schema, {
442
- parseOptions: {
443
- preserveWhitespace: "full",
444
- ...n.parseOptions
445
- }
446
- });
447
- if (a.toString() === "<>")
448
- return !0;
449
- let { from: i, to: c } = typeof e == "number" ? { from: e, to: e } : { from: e.from, to: e.to }, l = !0, u = !0;
450
- if ((At(a) ? a : [a]).forEach((p) => {
451
- p.check(), l = l ? p.isText && p.marks.length === 0 : !1, u = u ? p.isBlock : !1;
452
- }), i === c && u) {
453
- const { parent: p } = o.doc.resolve(i);
454
- p.isTextblock && !p.type.spec.code && !p.childCount && (i -= 1, c += 1);
455
- }
456
- let f;
457
- l ? (Array.isArray(t) ? f = t.map((p) => p.text || "").join("") : typeof t == "object" && t && t.text ? f = t.text : f = t, o.insertText(f, i, c)) : (f = a, o.replaceWith(i, c, f)), n.updateSelection && Mt(o, o.steps.length - 1, -1), n.applyInputRules && o.setMeta("applyInputRules", { from: i, text: f }), n.applyPasteRules && o.setMeta("applyPasteRules", { from: i, text: f });
458
- }
459
- return !0;
460
- }, Ct = () => ({ state: e, dispatch: t }) => Ie(e, t), Et = () => ({ state: e, dispatch: t }) => Pe(e, t), Bt = () => ({ state: e, dispatch: t }) => $e(e, t), vt = () => ({ state: e, dispatch: t }) => Ne(e, t), It = () => ({ tr: e, state: t, dispatch: n }) => {
461
- try {
462
- const o = ce(t.doc, t.selection.$from.pos, -1);
463
- return o == null ? !1 : (e.join(o, 2), n && n(e), !0);
464
- } catch {
465
- return !1;
466
- }
467
- }, Pt = () => ({ state: e, dispatch: t, tr: n }) => {
468
- try {
469
- const o = ce(e.doc, e.selection.$from.pos, 1);
470
- return o == null ? !1 : (n.join(o, 2), t && t(n), !0);
471
- } catch {
472
- return !1;
473
- }
474
- }, $t = () => ({ state: e, dispatch: t }) => Oe(e, t), Nt = () => ({ state: e, dispatch: t }) => je(e, t);
475
- function ge() {
476
- return typeof navigator < "u" ? /Mac/.test(navigator.platform) : !1;
477
- }
478
- function Ot(e) {
479
- const t = e.split(/-(?!$)/);
480
- let n = t[t.length - 1];
481
- n === "Space" && (n = " ");
482
- let o, r, s, a;
483
- for (let i = 0; i < t.length - 1; i += 1) {
484
- const c = t[i];
485
- if (/^(cmd|meta|m)$/i.test(c))
486
- a = !0;
487
- else if (/^a(lt)?$/i.test(c))
488
- o = !0;
489
- else if (/^(c|ctrl|control)$/i.test(c))
490
- r = !0;
491
- else if (/^s(hift)?$/i.test(c))
492
- s = !0;
493
- else if (/^mod$/i.test(c))
494
- X() || ge() ? a = !0 : r = !0;
495
- else
496
- throw new Error(`Unrecognized modifier name: ${c}`);
497
- }
498
- return o && (n = `Alt-${n}`), r && (n = `Ctrl-${n}`), a && (n = `Meta-${n}`), s && (n = `Shift-${n}`), n;
499
- }
500
- const jt = (e) => ({ editor: t, view: n, tr: o, dispatch: r }) => {
501
- const s = Ot(e).split(/-(?!$)/), a = s.find((l) => !["Alt", "Ctrl", "Meta", "Shift"].includes(l)), i = new KeyboardEvent("keydown", {
502
- key: a === "Space" ? " " : a,
503
- altKey: s.includes("Alt"),
504
- ctrlKey: s.includes("Ctrl"),
505
- metaKey: s.includes("Meta"),
506
- shiftKey: s.includes("Shift"),
507
- bubbles: !0,
508
- cancelable: !0
509
- }), c = t.captureTransaction(() => {
510
- n.someProp("handleKeyDown", (l) => l(n, i));
511
- });
512
- return c == null || c.steps.forEach((l) => {
513
- const u = l.map(o.mapping);
514
- u && r && o.maybeStep(u);
515
- }), !0;
516
- };
517
- function Y(e, t, n = {}) {
518
- const { from: o, to: r, empty: s } = e.selection, a = t ? y(t, e.schema) : null, i = [];
519
- e.doc.nodesBetween(o, r, (d, f) => {
520
- if (d.isText)
521
- return;
522
- const p = Math.max(o, f), m = Math.min(r, f + d.nodeSize);
523
- i.push({
524
- node: d,
525
- from: p,
526
- to: m
527
- });
528
- });
529
- const c = r - o, l = i.filter((d) => a ? a.name === d.node.type.name : !0).filter((d) => z(d.node.attrs, n, { strict: !1 }));
530
- return s ? !!l.length : l.reduce((d, f) => d + f.to - f.from, 0) >= c;
531
- }
532
- const Ft = (e, t = {}) => ({ state: n, dispatch: o }) => {
533
- const r = y(e, n.schema);
534
- return Y(n, r, t) ? Fe(n, o) : !1;
535
- }, Lt = () => ({ state: e, dispatch: t }) => Le(e, t), Rt = (e) => ({ state: t, dispatch: n }) => {
536
- const o = y(e, t.schema);
537
- return _e(o)(t, n);
538
- }, Dt = () => ({ state: e, dispatch: t }) => Re(e, t);
539
- function ke(e, t) {
540
- return t.nodes[e] ? "node" : t.marks[e] ? "mark" : null;
541
- }
542
- function ne(e, t) {
543
- const n = typeof t == "string" ? [t] : t;
544
- return Object.keys(e).reduce((o, r) => (n.includes(r) || (o[r] = e[r]), o), {});
545
- }
546
- const zt = (e, t) => ({ tr: n, state: o, dispatch: r }) => {
547
- let s = null, a = null;
548
- const i = ke(typeof e == "string" ? e : e.name, o.schema);
549
- return i ? (i === "node" && (s = y(e, o.schema)), i === "mark" && (a = I(e, o.schema)), r && n.selection.ranges.forEach((c) => {
550
- o.doc.nodesBetween(c.$from.pos, c.$to.pos, (l, u) => {
551
- s && s === l.type && n.setNodeMarkup(u, void 0, ne(l.attrs, t)), a && l.marks.length && l.marks.forEach((d) => {
552
- a === d.type && n.addMark(u, u + l.nodeSize, a.create(ne(d.attrs, t)));
553
- });
554
- });
555
- }), !0) : !1;
556
- }, Wt = () => ({ tr: e, dispatch: t }) => (t && e.scrollIntoView(), !0), Ht = () => ({ tr: e, commands: t }) => t.setTextSelection({
557
- from: 0,
558
- to: e.doc.content.size
559
- }), Kt = () => ({ state: e, dispatch: t }) => De(e, t), Vt = () => ({ state: e, dispatch: t }) => ze(e, t), _t = () => ({ state: e, dispatch: t }) => We(e, t), Jt = () => ({ state: e, dispatch: t }) => He(e, t), Ut = () => ({ state: e, dispatch: t }) => Ke(e, t);
560
- function Gt(e, t, n = {}) {
561
- return W(e, t, { slice: !1, parseOptions: n });
562
- }
563
- const qt = (e, t = !1, n = {}) => ({ tr: o, editor: r, dispatch: s }) => {
564
- const { doc: a } = o, i = Gt(e, r.schema, n);
565
- return s && o.replaceWith(0, a.content.size, i).setMeta("preventUpdate", !t), !0;
566
- };
567
- function Qt(e, t) {
568
- const n = I(t, e.schema), { from: o, to: r, empty: s } = e.selection, a = [];
569
- s ? (e.storedMarks && a.push(...e.storedMarks), a.push(...e.selection.$head.marks())) : e.doc.nodesBetween(o, r, (c) => {
570
- a.push(...c.marks);
571
- });
572
- const i = a.find((c) => c.type.name === n.name);
573
- return i ? { ...i.attrs } : {};
574
- }
575
- function Xt(e) {
576
- for (let t = 0; t < e.edgeCount; t += 1) {
577
- const { type: n } = e.edge(t);
578
- if (n.isTextblock && !n.hasRequiredAttrs())
579
- return n;
580
- }
581
- return null;
582
- }
583
- function F(e, t) {
584
- const n = [];
585
- return e.descendants((o, r) => {
586
- t(o) && n.push({
587
- node: o,
588
- pos: r
589
- });
590
- }), n;
591
- }
592
- function Yt(e, t) {
593
- for (let n = e.depth; n > 0; n -= 1) {
594
- const o = e.node(n);
595
- if (t(o))
596
- return {
597
- pos: n > 0 ? e.before(n) : 0,
598
- start: e.start(n),
599
- depth: n,
600
- node: o
601
- };
602
- }
603
- }
604
- function Z(e) {
605
- return (t) => Yt(t.$from, e);
606
- }
607
- function D(e, t, n) {
608
- return Object.fromEntries(Object.entries(n).filter(([o]) => {
609
- const r = e.find((s) => s.type === t && s.name === o);
610
- return r ? r.attribute.keepOnSplit : !1;
611
- }));
612
- }
613
- function Zt(e, t, n = {}) {
614
- const { empty: o, ranges: r } = e.selection, s = t ? I(t, e.schema) : null;
615
- if (o)
616
- return !!(e.storedMarks || e.selection.$from.marks()).filter((d) => s ? s.name === d.type.name : !0).find((d) => z(d.attrs, n, { strict: !1 }));
617
- let a = 0;
618
- const i = [];
619
- if (r.forEach(({ $from: d, $to: f }) => {
620
- const p = d.pos, m = f.pos;
621
- e.doc.nodesBetween(p, m, (h, g) => {
622
- if (!h.isText && !h.marks.length)
623
- return;
624
- const k = Math.max(p, g), E = Math.min(m, g + h.nodeSize), M = E - k;
625
- a += M, i.push(...h.marks.map((B) => ({
626
- mark: B,
627
- from: k,
628
- to: E
629
- })));
630
- });
631
- }), a === 0)
632
- return !1;
633
- const c = i.filter((d) => s ? s.name === d.mark.type.name : !0).filter((d) => z(d.mark.attrs, n, { strict: !1 })).reduce((d, f) => d + f.to - f.from, 0), l = i.filter((d) => s ? d.mark.type !== s && d.mark.type.excludes(s) : !0).reduce((d, f) => d + f.to - f.from, 0);
634
- return (c > 0 ? c + l : c) >= a;
635
- }
636
- function oe(e, t) {
637
- const { nodeExtensions: n } = Qe(t), o = n.find((a) => a.name === e);
638
- if (!o)
639
- return !1;
640
- const r = {
641
- name: o.name,
642
- options: o.options,
643
- storage: o.storage
644
- }, s = x(w(o, "group", r));
645
- return typeof s != "string" ? !1 : s.split(" ").includes("list");
646
- }
647
- function en(e, t, n) {
648
- var o;
649
- const { selection: r } = t;
650
- let s = null;
651
- if (me(r) && (s = r.$cursor), s) {
652
- const i = (o = e.storedMarks) !== null && o !== void 0 ? o : s.marks();
653
- return !!n.isInSet(i) || !i.some((c) => c.type.excludes(n));
654
- }
655
- const { ranges: a } = r;
656
- return a.some(({ $from: i, $to: c }) => {
657
- let l = i.depth === 0 ? e.doc.inlineContent && e.doc.type.allowsMarkType(n) : !1;
658
- return e.doc.nodesBetween(i.pos, c.pos, (u, d, f) => {
659
- if (l)
660
- return !1;
661
- if (u.isInline) {
662
- const p = !f || f.type.allowsMarkType(n), m = !!n.isInSet(u.marks) || !u.marks.some((h) => h.type.excludes(n));
663
- l = p && m;
664
- }
665
- return !l;
666
- }), l;
667
- });
668
- }
669
- const tn = (e, t = {}) => ({ tr: n, state: o, dispatch: r }) => {
670
- const { selection: s } = n, { empty: a, ranges: i } = s, c = I(e, o.schema);
671
- if (r)
672
- if (a) {
673
- const l = Qt(o, c);
674
- n.addStoredMark(c.create({
675
- ...l,
676
- ...t
677
- }));
678
- } else
679
- i.forEach((l) => {
680
- const u = l.$from.pos, d = l.$to.pos;
681
- o.doc.nodesBetween(u, d, (f, p) => {
682
- const m = Math.max(p, u), h = Math.min(p + f.nodeSize, d);
683
- f.marks.find((k) => k.type === c) ? f.marks.forEach((k) => {
684
- c === k.type && n.addMark(m, h, c.create({
685
- ...k.attrs,
686
- ...t
687
- }));
688
- }) : n.addMark(m, h, c.create(t));
689
- });
690
- });
691
- return en(o, n, c);
692
- }, nn = (e, t) => ({ tr: n }) => (n.setMeta(e, t), !0), on = (e, t = {}) => ({ state: n, dispatch: o, chain: r }) => {
693
- const s = y(e, n.schema);
694
- return s.isTextblock ? r().command(({ commands: a }) => ee(s, t)(n) ? !0 : a.clearNodes()).command(({ state: a }) => ee(s, t)(a, o)).run() : (console.warn('[tiptap warn]: Currently "setNode()" only supports text block nodes.'), !1);
695
- }, rn = (e) => ({ tr: t, dispatch: n }) => {
696
- if (n) {
697
- const { doc: o } = t, r = P(e, 0, o.content.size), s = ae.create(o, r);
698
- t.setSelection(s);
699
- }
700
- return !0;
701
- }, sn = (e) => ({ tr: t, dispatch: n }) => {
702
- if (n) {
703
- const { doc: o } = t, { from: r, to: s } = typeof e == "number" ? { from: e, to: e } : e, a = b.atStart(o).from, i = b.atEnd(o).to, c = P(r, a, i), l = P(s, a, i), u = b.create(o, c, l);
704
- t.setSelection(u);
705
- }
706
- return !0;
707
- }, an = (e) => ({ state: t, dispatch: n }) => {
708
- const o = y(e, t.schema);
709
- return Je(o)(t, n);
710
- };
711
- function re(e, t) {
712
- const n = e.storedMarks || e.selection.$to.parentOffset && e.selection.$from.marks();
713
- if (n) {
714
- const o = n.filter((r) => t == null ? void 0 : t.includes(r.type.name));
715
- e.tr.ensureMarks(o);
716
- }
717
- }
718
- const cn = ({ keepMarks: e = !0 } = {}) => ({ tr: t, state: n, dispatch: o, editor: r }) => {
719
- const { selection: s, doc: a } = t, { $from: i, $to: c } = s, l = r.extensionManager.attributes, u = D(l, i.node().type.name, i.node().attrs);
720
- if (s instanceof ae && s.node.isBlock)
721
- return !i.parentOffset || !R(a, i.pos) ? !1 : (o && (e && re(n, r.extensionManager.splittableMarks), t.split(i.pos).scrollIntoView()), !0);
722
- if (!i.parent.isBlock)
723
- return !1;
724
- if (o) {
725
- const d = c.parentOffset === c.parent.content.size;
726
- s instanceof b && t.deleteSelection();
727
- const f = i.depth === 0 ? void 0 : Xt(i.node(-1).contentMatchAt(i.indexAfter(-1)));
728
- let p = d && f ? [
729
- {
730
- type: f,
731
- attrs: u
732
- }
733
- ] : void 0, m = R(t.doc, t.mapping.map(i.pos), 1, p);
734
- if (!p && !m && R(t.doc, t.mapping.map(i.pos), 1, f ? [{ type: f }] : void 0) && (m = !0, p = f ? [
735
- {
736
- type: f,
737
- attrs: u
738
- }
739
- ] : void 0), m && (t.split(t.mapping.map(i.pos), 1, p), f && !d && !i.parentOffset && i.parent.type !== f)) {
740
- const h = t.mapping.map(i.before()), g = t.doc.resolve(h);
741
- i.node(-1).canReplaceWith(g.index(), g.index() + 1, f) && t.setNodeMarkup(t.mapping.map(i.before()), f);
742
- }
743
- e && re(n, r.extensionManager.splittableMarks), t.scrollIntoView();
744
- }
745
- return !0;
746
- }, ln = (e) => ({ tr: t, state: n, dispatch: o, editor: r }) => {
747
- var s;
748
- const a = y(e, n.schema), { $from: i, $to: c } = n.selection, l = n.selection.node;
749
- if (l && l.isBlock || i.depth < 2 || !i.sameParent(c))
750
- return !1;
751
- const u = i.node(-1);
752
- if (u.type !== a)
753
- return !1;
754
- const d = r.extensionManager.attributes;
755
- if (i.parent.content.size === 0 && i.node(-1).childCount === i.indexAfter(-1)) {
756
- if (i.depth === 2 || i.node(-3).type !== a || i.index(-2) !== i.node(-2).childCount - 1)
757
- return !1;
758
- if (o) {
759
- let g = L.empty;
760
- const k = i.index(-1) ? 1 : i.index(-2) ? 2 : 3;
761
- for (let v = i.depth - k; v >= i.depth - 3; v -= 1)
762
- g = L.from(i.node(v).copy(g));
763
- const E = i.indexAfter(-1) < i.node(-2).childCount ? 1 : i.indexAfter(-2) < i.node(-3).childCount ? 2 : 3, M = D(d, i.node().type.name, i.node().attrs), B = ((s = a.contentMatch.defaultType) === null || s === void 0 ? void 0 : s.createAndFill(M)) || void 0;
764
- g = g.append(L.from(a.createAndFill(null, B) || void 0));
765
- const S = i.before(i.depth - (k - 1));
766
- t.replace(S, i.after(-E), new be(g, 4 - k, 0));
767
- let T = -1;
768
- t.doc.nodesBetween(S, t.doc.content.size, (v, K) => {
769
- if (T > -1)
770
- return !1;
771
- v.isTextblock && v.content.size === 0 && (T = K + 1);
772
- }), T > -1 && t.setSelection(b.near(t.doc.resolve(T))), t.scrollIntoView();
773
- }
774
- return !0;
775
- }
776
- const f = c.pos === i.end() ? u.contentMatchAt(0).defaultType : null, p = D(d, u.type.name, u.attrs), m = D(d, i.node().type.name, i.node().attrs);
777
- t.delete(i.pos, c.pos);
778
- const h = f ? [
779
- { type: a, attrs: p },
780
- { type: f, attrs: m }
781
- ] : [{ type: a, attrs: p }];
782
- if (!R(t.doc, i.pos, 2))
783
- return !1;
784
- if (o) {
785
- const { selection: g, storedMarks: k } = n, { splittableMarks: E } = r.extensionManager, M = k || g.$to.parentOffset && g.$from.marks();
786
- if (t.split(i.pos, 2, h).scrollIntoView(), !M || !o)
787
- return !0;
788
- const B = M.filter((S) => E.includes(S.type.name));
789
- t.ensureMarks(B);
790
- }
791
- return !0;
792
- }, _ = (e, t) => {
793
- const n = Z((a) => a.type === t)(e.selection);
794
- if (!n)
795
- return !0;
796
- const o = e.doc.resolve(Math.max(0, n.pos - 1)).before(n.depth);
797
- if (o === void 0)
798
- return !0;
799
- const r = e.doc.nodeAt(o);
800
- return n.node.type === (r == null ? void 0 : r.type) && le(e.doc, n.pos) && e.join(n.pos), !0;
801
- }, J = (e, t) => {
802
- const n = Z((a) => a.type === t)(e.selection);
803
- if (!n)
804
- return !0;
805
- const o = e.doc.resolve(n.start).after(n.depth);
806
- if (o === void 0)
807
- return !0;
808
- const r = e.doc.nodeAt(o);
809
- return n.node.type === (r == null ? void 0 : r.type) && le(e.doc, o) && e.join(o), !0;
810
- }, dn = (e, t, n, o = {}) => ({ editor: r, tr: s, state: a, dispatch: i, chain: c, commands: l, can: u }) => {
811
- const { extensions: d, splittableMarks: f } = r.extensionManager, p = y(e, a.schema), m = y(t, a.schema), { selection: h, storedMarks: g } = a, { $from: k, $to: E } = h, M = k.blockRange(E), B = g || h.$to.parentOffset && h.$from.marks();
812
- if (!M)
813
- return !1;
814
- const S = Z((T) => oe(T.type.name, d))(h);
815
- if (M.depth >= 1 && S && M.depth - S.depth <= 1) {
816
- if (S.node.type === p)
817
- return l.liftListItem(m);
818
- if (oe(S.node.type.name, d) && p.validContent(S.node.content) && i)
819
- return c().command(() => (s.setNodeMarkup(S.pos, p), !0)).command(() => _(s, p)).command(() => J(s, p)).run();
820
- }
821
- return !n || !B || !i ? c().command(() => u().wrapInList(p, o) ? !0 : l.clearNodes()).wrapInList(p, o).command(() => _(s, p)).command(() => J(s, p)).run() : c().command(() => {
822
- const T = u().wrapInList(p, o), v = B.filter((K) => f.includes(K.type.name));
823
- return s.ensureMarks(v), T ? !0 : l.clearNodes();
824
- }).wrapInList(p, o).command(() => _(s, p)).command(() => J(s, p)).run();
825
- }, un = (e, t = {}, n = {}) => ({ state: o, commands: r }) => {
826
- const { extendEmptyMarkRange: s = !1 } = n, a = I(e, o.schema);
827
- return Zt(o, a, t) ? r.unsetMark(a, { extendEmptyMarkRange: s }) : r.setMark(a, t);
828
- }, fn = (e, t, n = {}) => ({ state: o, commands: r }) => {
829
- const s = y(e, o.schema), a = y(t, o.schema);
830
- return Y(o, s, n) ? r.setNode(a) : r.setNode(s, n);
831
- }, pn = (e, t = {}) => ({ state: n, commands: o }) => {
832
- const r = y(e, n.schema);
833
- return Y(n, r, t) ? o.lift(r) : o.wrapIn(r, t);
834
- }, mn = () => ({ state: e, dispatch: t }) => {
835
- const n = e.plugins;
836
- for (let o = 0; o < n.length; o += 1) {
837
- const r = n[o];
838
- let s;
839
- if (r.spec.isInputRules && (s = r.getState(e))) {
840
- if (t) {
841
- const a = e.tr, i = s.transform;
842
- for (let c = i.steps.length - 1; c >= 0; c -= 1)
843
- a.step(i.steps[c].invert(i.docs[c]));
844
- if (s.text) {
845
- const c = a.doc.resolve(s.from).marks();
846
- a.replaceWith(s.from, s.to, e.schema.text(s.text, c));
847
- } else
848
- a.delete(s.from, s.to);
849
- }
850
- return !0;
851
- }
852
- }
853
- return !1;
854
- }, hn = () => ({ tr: e, dispatch: t }) => {
855
- const { selection: n } = e, { empty: o, ranges: r } = n;
856
- return o || t && r.forEach((s) => {
857
- e.removeMark(s.$from.pos, s.$to.pos);
858
- }), !0;
859
- }, gn = (e, t = {}) => ({ tr: n, state: o, dispatch: r }) => {
860
- var s;
861
- const { extendEmptyMarkRange: a = !1 } = t, { selection: i } = n, c = I(e, o.schema), { $from: l, empty: u, ranges: d } = i;
862
- if (!r)
863
- return !0;
864
- if (u && a) {
865
- let { from: f, to: p } = i;
866
- const m = (s = l.marks().find((g) => g.type === c)) === null || s === void 0 ? void 0 : s.attrs, h = pe(l, c, m);
867
- h && (f = h.from, p = h.to), n.removeMark(f, p, c);
868
- } else
869
- d.forEach((f) => {
870
- n.removeMark(f.$from.pos, f.$to.pos, c);
871
- });
872
- return n.removeStoredMark(c), !0;
873
- }, kn = (e, t = {}) => ({ tr: n, state: o, dispatch: r }) => {
874
- let s = null, a = null;
875
- const i = ke(typeof e == "string" ? e : e.name, o.schema);
876
- return i ? (i === "node" && (s = y(e, o.schema)), i === "mark" && (a = I(e, o.schema)), r && n.selection.ranges.forEach((c) => {
877
- const l = c.$from.pos, u = c.$to.pos;
878
- o.doc.nodesBetween(l, u, (d, f) => {
879
- s && s === d.type && n.setNodeMarkup(f, void 0, {
880
- ...d.attrs,
881
- ...t
882
- }), a && d.marks.length && d.marks.forEach((p) => {
883
- if (a === p.type) {
884
- const m = Math.max(f, l), h = Math.min(f + d.nodeSize, u);
885
- n.addMark(m, h, a.create({
886
- ...p.attrs,
887
- ...t
888
- }));
889
- }
890
- });
891
- });
892
- }), !0) : !1;
893
- }, yn = (e, t = {}) => ({ state: n, dispatch: o }) => {
894
- const r = y(e, n.schema);
895
- return Ve(r, t)(n, o);
896
- }, wn = (e, t = {}) => ({ state: n, dispatch: o }) => {
897
- const r = y(e, n.schema);
898
- return Ue(r, t)(n, o);
899
- };
900
- var xn = /* @__PURE__ */ Object.freeze({
901
- __proto__: null,
902
- blur: rt,
903
- clearContent: st,
904
- clearNodes: it,
905
- command: at,
906
- createParagraphNear: ct,
907
- cut: lt,
908
- deleteCurrentNode: dt,
909
- deleteNode: ut,
910
- deleteRange: ft,
911
- deleteSelection: pt,
912
- enter: mt,
913
- exitCode: ht,
914
- extendMarkRange: kt,
915
- first: yt,
916
- focus: xt,
917
- forEach: St,
918
- insertContent: bt,
919
- insertContentAt: Tt,
920
- joinUp: Ct,
921
- joinDown: Et,
922
- joinBackward: Bt,
923
- joinForward: vt,
924
- joinItemBackward: It,
925
- joinItemForward: Pt,
926
- joinTextblockBackward: $t,
927
- joinTextblockForward: Nt,
928
- keyboardShortcut: jt,
929
- lift: Ft,
930
- liftEmptyBlock: Lt,
931
- liftListItem: Rt,
932
- newlineInCode: Dt,
933
- resetAttributes: zt,
934
- scrollIntoView: Wt,
935
- selectAll: Ht,
936
- selectNodeBackward: Kt,
937
- selectNodeForward: Vt,
938
- selectParentNode: _t,
939
- selectTextblockEnd: Jt,
940
- selectTextblockStart: Ut,
941
- setContent: qt,
942
- setMark: tn,
943
- setMeta: nn,
944
- setNode: on,
945
- setNodeSelection: rn,
946
- setTextSelection: sn,
947
- sinkListItem: an,
948
- splitBlock: cn,
949
- splitListItem: ln,
950
- toggleList: dn,
951
- toggleMark: un,
952
- toggleNode: fn,
953
- toggleWrap: pn,
954
- undoInputRule: mn,
955
- unsetAllMarks: hn,
956
- unsetMark: gn,
957
- updateAttributes: kn,
958
- wrapIn: yn,
959
- wrapInList: wn
960
- });
961
- C.create({
962
- name: "commands",
963
- addCommands() {
964
- return {
965
- ...xn
966
- };
967
- }
968
- });
969
- C.create({
970
- name: "editable",
971
- addProseMirrorPlugins() {
972
- return [
973
- new $({
974
- key: new N("editable"),
975
- props: {
976
- editable: () => this.editor.options.editable
977
- }
978
- })
979
- ];
980
- }
981
- });
982
- C.create({
983
- name: "focusEvents",
984
- addProseMirrorPlugins() {
985
- const { editor: e } = this;
986
- return [
987
- new $({
988
- key: new N("focusEvents"),
989
- props: {
990
- handleDOMEvents: {
991
- focus: (t, n) => {
992
- e.isFocused = !0;
993
- const o = e.state.tr.setMeta("focus", { event: n }).setMeta("addToHistory", !1);
994
- return t.dispatch(o), !1;
995
- },
996
- blur: (t, n) => {
997
- e.isFocused = !1;
998
- const o = e.state.tr.setMeta("blur", { event: n }).setMeta("addToHistory", !1);
999
- return t.dispatch(o), !1;
1000
- }
1001
- }
1002
- }
1003
- })
1004
- ];
1005
- }
1006
- });
1007
- C.create({
1008
- name: "keymap",
1009
- addKeyboardShortcuts() {
1010
- const e = () => this.editor.commands.first(({ commands: a }) => [
1011
- () => a.undoInputRule(),
1012
- // maybe convert first text block node to default node
1013
- () => a.command(({ tr: i }) => {
1014
- const { selection: c, doc: l } = i, { empty: u, $anchor: d } = c, { pos: f, parent: p } = d, m = d.parent.isTextblock && f > 0 ? i.doc.resolve(f - 1) : d, h = m.parent.type.spec.isolating, g = d.pos - d.parentOffset, k = h && m.parent.childCount === 1 ? g === d.pos : O.atStart(l).from === f;
1015
- return !u || !k || !p.type.isTextblock || p.textContent.length ? !1 : a.clearNodes();
1016
- }),
1017
- () => a.deleteSelection(),
1018
- () => a.joinBackward(),
1019
- () => a.selectNodeBackward()
1020
- ]), t = () => this.editor.commands.first(({ commands: a }) => [
1021
- () => a.deleteSelection(),
1022
- () => a.deleteCurrentNode(),
1023
- () => a.joinForward(),
1024
- () => a.selectNodeForward()
1025
- ]), o = {
1026
- Enter: () => this.editor.commands.first(({ commands: a }) => [
1027
- () => a.newlineInCode(),
1028
- () => a.createParagraphNear(),
1029
- () => a.liftEmptyBlock(),
1030
- () => a.splitBlock()
1031
- ]),
1032
- "Mod-Enter": () => this.editor.commands.exitCode(),
1033
- Backspace: e,
1034
- "Mod-Backspace": e,
1035
- "Shift-Backspace": e,
1036
- Delete: t,
1037
- "Mod-Delete": t,
1038
- "Mod-a": () => this.editor.commands.selectAll()
1039
- }, r = {
1040
- ...o
1041
- }, s = {
1042
- ...o,
1043
- "Ctrl-h": e,
1044
- "Alt-Backspace": e,
1045
- "Ctrl-d": t,
1046
- "Ctrl-Alt-Backspace": t,
1047
- "Alt-Delete": t,
1048
- "Alt-d": t,
1049
- "Ctrl-a": () => this.editor.commands.selectTextblockStart(),
1050
- "Ctrl-e": () => this.editor.commands.selectTextblockEnd()
1051
- };
1052
- return X() || ge() ? s : r;
1053
- },
1054
- addProseMirrorPlugins() {
1055
- return [
1056
- // With this plugin we check if the whole document was selected and deleted.
1057
- // In this case we will additionally call `clearNodes()` to convert e.g. a heading
1058
- // to a paragraph if necessary.
1059
- // This is an alternative to ProseMirror's `AllSelection`, which doesn’t work well
1060
- // with many other commands.
1061
- new $({
1062
- key: new N("clearDocument"),
1063
- appendTransaction: (e, t, n) => {
1064
- if (!(e.some((m) => m.docChanged) && !t.doc.eq(n.doc)))
1065
- return;
1066
- const { empty: r, from: s, to: a } = t.selection, i = O.atStart(t.doc).from, c = O.atEnd(t.doc).to;
1067
- if (r || !(s === i && a === c) || !(n.doc.textBetween(0, n.doc.content.size, " ", " ").length === 0))
1068
- return;
1069
- const d = n.tr, f = fe({
1070
- state: n,
1071
- transaction: d
1072
- }), { commands: p } = new qe({
1073
- editor: this.editor,
1074
- state: f
1075
- });
1076
- if (p.clearNodes(), !!d.steps.length)
1077
- return d;
1078
- }
1079
- })
1080
- ];
1081
- }
1082
- });
1083
- C.create({
1084
- name: "tabindex",
1085
- addProseMirrorPlugins() {
1086
- return [
1087
- new $({
1088
- key: new N("tabindex"),
1089
- props: {
1090
- attributes: this.editor.isEditable ? { tabindex: "0" } : {}
1091
- }
1092
- })
1093
- ];
1094
- }
1095
- });
1096
- function se(e) {
1097
- return new et({
1098
- find: e.find,
1099
- handler: ({ state: t, range: n, match: o }) => {
1100
- const r = t.doc.resolve(n.from), s = x(e.getAttributes, void 0, o) || {};
1101
- if (!r.node(-1).canReplaceWith(r.index(-1), r.indexAfter(-1), e.type))
1102
- return null;
1103
- t.tr.delete(n.from, n.to).setBlockType(n.from, n.from, e.type, s);
1104
- }
1105
- });
1106
- }
1107
- class H {
1108
- constructor(t = {}) {
1109
- this.type = "node", this.name = "node", this.parent = null, this.child = null, this.config = {
1110
- name: this.name,
1111
- defaultOptions: {}
1112
- }, this.config = {
1113
- ...this.config,
1114
- ...t
1115
- }, this.name = this.config.name, t.defaultOptions && Object.keys(t.defaultOptions).length > 0 && console.warn(`[tiptap warn]: BREAKING CHANGE: "defaultOptions" is deprecated. Please use "addOptions" instead. Found in extension: "${this.name}".`), this.options = this.config.defaultOptions, this.config.addOptions && (this.options = x(w(this, "addOptions", {
1116
- name: this.name
1117
- }))), this.storage = x(w(this, "addStorage", {
1118
- name: this.name,
1119
- options: this.options
1120
- })) || {};
1121
- }
1122
- static create(t = {}) {
1123
- return new H(t);
1124
- }
1125
- configure(t = {}) {
1126
- const n = this.extend();
1127
- return n.options = Q(this.options, t), n.storage = x(w(n, "addStorage", {
1128
- name: n.name,
1129
- options: n.options
1130
- })), n;
1131
- }
1132
- extend(t = {}) {
1133
- const n = new H({ ...this.config, ...t });
1134
- return n.parent = this, this.child = n, n.name = t.name ? t.name : n.parent.name, t.defaultOptions && console.warn(`[tiptap warn]: BREAKING CHANGE: "defaultOptions" is deprecated. Please use "addOptions" instead. Found in extension: "${n.name}".`), n.options = x(w(n, "addOptions", {
1135
- name: n.name
1136
- })), n.storage = x(w(n, "addStorage", {
1137
- name: n.name,
1138
- options: n.options
1139
- })), n;
1140
- }
1141
- }
1142
- const Sn = /^```([a-z]+)?[\s\n]$/, bn = /^~~~([a-z]+)?[\s\n]$/, Mn = H.create({
1143
- name: "codeBlock",
1144
- addOptions() {
1145
- return {
1146
- languageClassPrefix: "language-",
1147
- exitOnTripleEnter: !0,
1148
- exitOnArrowDown: !0,
1149
- HTMLAttributes: {}
1150
- };
1151
- },
1152
- content: "text*",
1153
- marks: "",
1154
- group: "block",
1155
- code: !0,
1156
- defining: !0,
1157
- addAttributes() {
1158
- return {
1159
- language: {
1160
- default: null,
1161
- parseHTML: (e) => {
1162
- var t;
1163
- const { languageClassPrefix: n } = this.options, s = [...((t = e.firstElementChild) === null || t === void 0 ? void 0 : t.classList) || []].filter((a) => a.startsWith(n)).map((a) => a.replace(n, ""))[0];
1164
- return s || null;
1165
- },
1166
- rendered: !1
1167
- }
1168
- };
1169
- },
1170
- parseHTML() {
1171
- return [
1172
- {
1173
- tag: "pre",
1174
- preserveWhitespace: "full"
1175
- }
1176
- ];
1177
- },
1178
- renderHTML({ node: e, HTMLAttributes: t }) {
1179
- return [
1180
- "pre",
1181
- Xe(this.options.HTMLAttributes, t),
1182
- [
1183
- "code",
1184
- {
1185
- class: e.attrs.language ? this.options.languageClassPrefix + e.attrs.language : null
1186
- },
1187
- 0
1188
- ]
1189
- ];
1190
- },
1191
- addCommands() {
1192
- return {
1193
- setCodeBlock: (e) => ({ commands: t }) => t.setNode(this.name, e),
1194
- toggleCodeBlock: (e) => ({ commands: t }) => t.toggleNode(this.name, "paragraph", e)
1195
- };
1196
- },
1197
- addKeyboardShortcuts() {
1198
- return {
1199
- "Mod-Alt-c": () => this.editor.commands.toggleCodeBlock(),
1200
- // remove code block when at start of document or code block is empty
1201
- Backspace: () => {
1202
- const { empty: e, $anchor: t } = this.editor.state.selection, n = t.pos === 1;
1203
- return !e || t.parent.type.name !== this.name ? !1 : n || !t.parent.textContent.length ? this.editor.commands.clearNodes() : !1;
1204
- },
1205
- // exit node on triple enter
1206
- Enter: ({ editor: e }) => {
1207
- if (!this.options.exitOnTripleEnter)
1208
- return !1;
1209
- const { state: t } = e, { selection: n } = t, { $from: o, empty: r } = n;
1210
- if (!r || o.parent.type !== this.type)
1211
- return !1;
1212
- const s = o.parentOffset === o.parent.nodeSize - 2, a = o.parent.textContent.endsWith(`
1213
-
1214
- `);
1215
- return !s || !a ? !1 : e.chain().command(({ tr: i }) => (i.delete(o.pos - 2, o.pos), !0)).exitCode().run();
1216
- },
1217
- // exit node on arrow down
1218
- ArrowDown: ({ editor: e }) => {
1219
- if (!this.options.exitOnArrowDown)
1220
- return !1;
1221
- const { state: t } = e, { selection: n, doc: o } = t, { $from: r, empty: s } = n;
1222
- if (!s || r.parent.type !== this.type || !(r.parentOffset === r.parent.nodeSize - 2))
1223
- return !1;
1224
- const i = r.after();
1225
- return i === void 0 || o.nodeAt(i) ? !1 : e.commands.exitCode();
1226
- }
1227
- };
1228
- },
1229
- addInputRules() {
1230
- return [
1231
- se({
1232
- find: Sn,
1233
- type: this.type,
1234
- getAttributes: (e) => ({
1235
- language: e[1]
1236
- })
1237
- }),
1238
- se({
1239
- find: bn,
1240
- type: this.type,
1241
- getAttributes: (e) => ({
1242
- language: e[1]
1243
- })
1244
- })
1245
- ];
1246
- },
1247
- addProseMirrorPlugins() {
1248
- return [
1249
- // this plugin creates a code block for pasted content from VS Code
1250
- // we can also detect the copied code language
1251
- new $({
1252
- key: new N("codeBlockVSCodeHandler"),
1253
- props: {
1254
- handlePaste: (e, t) => {
1255
- if (!t.clipboardData || this.editor.isActive(this.type.name))
1256
- return !1;
1257
- const n = t.clipboardData.getData("text/plain"), o = t.clipboardData.getData("vscode-editor-data"), r = o ? JSON.parse(o) : void 0, s = r == null ? void 0 : r.mode;
1258
- if (!n || !s)
1259
- return !1;
1260
- const { tr: a } = e.state;
1261
- return a.replaceSelectionWith(this.type.create({ language: s })), a.setSelection(b.near(a.doc.resolve(Math.max(0, a.selection.from - 2)))), a.insertText(n.replace(/\r\n?/g, `
1262
- `)), a.setMeta("paste", !0), e.dispatch(a), !0;
1263
- }
1264
- }
1265
- })
1266
- ];
1267
- }
1268
- });
1269
- let A, j;
1270
- const U = /* @__PURE__ */ new Set(), G = /* @__PURE__ */ new Set();
1271
- function An() {
1272
- return A;
1273
- }
1274
- function Tn(e) {
1275
- if (!A && !j) {
1276
- const t = e.themes.filter(
1277
- (o) => !!o && o in de
1278
- ), n = e.languages.filter(
1279
- (o) => !!o && o in ue
1280
- );
1281
- return j = Ge({ themes: t, langs: n }).then((o) => {
1282
- A = o;
1283
- }), j;
1284
- }
1285
- if (j)
1286
- return j;
1287
- }
1288
- async function ye(e) {
1289
- return A && !A.getLoadedThemes().includes(e) && !G.has(e) && e in de ? (G.add(e), await A.loadTheme(e), G.delete(e), !0) : !1;
1290
- }
1291
- async function we(e) {
1292
- return A && !A.getLoadedLanguages().includes(e) && !U.has(e) && e in ue ? (U.add(e), await A.loadLanguage(e), U.delete(e), !0) : !1;
1293
- }
1294
- async function Cn({
1295
- doc: e,
1296
- name: t,
1297
- defaultTheme: n,
1298
- defaultLanguage: o
1299
- }) {
1300
- const r = F(e, (i) => i.type.name === t), s = [
1301
- ...r.map((i) => i.node.attrs.theme),
1302
- n
1303
- ], a = [
1304
- ...r.map((i) => i.node.attrs.language),
1305
- o
1306
- ];
1307
- A ? await Promise.all([
1308
- ...s.flatMap((i) => ye(i)),
1309
- ...a.flatMap((i) => !!i && we(i))
1310
- ]) : await Tn({ languages: a, themes: s });
1311
- }
1312
- function ie({
1313
- doc: e,
1314
- name: t,
1315
- defaultTheme: n,
1316
- defaultLanguage: o
1317
- }) {
1318
- const r = [];
1319
- return F(e, (a) => a.type.name === t).forEach((a) => {
1320
- let i = a.pos + 1, c = a.node.attrs.language || o, l = a.node.attrs.theme || n;
1321
- const u = An();
1322
- if (!u)
1323
- return;
1324
- u.getLoadedLanguages().includes(c) || (c = "plaintext");
1325
- const d = u.getLoadedThemes().includes(l) ? l : u.getLoadedThemes()[0], f = u.codeToTokensBase(a.node.textContent, {
1326
- lang: c,
1327
- theme: d
1328
- });
1329
- for (const p of f) {
1330
- for (const m of p) {
1331
- const h = i + m.content.length, g = Se.inline(i, h, {
1332
- style: `color: ${m.color}`
1333
- });
1334
- r.push(g), i = h;
1335
- }
1336
- i += 1;
1337
- }
1338
- }), xe.create(e, r);
1339
- }
1340
- function En({
1341
- name: e,
1342
- defaultLanguage: t,
1343
- defaultTheme: n
1344
- }) {
1345
- const o = new $({
1346
- key: new N("shiki"),
1347
- view(r) {
1348
- class s {
1349
- constructor() {
1350
- this.initDecorations();
1351
- }
1352
- update() {
1353
- this.checkUndecoratedBlocks();
1354
- }
1355
- destroy() {
1356
- }
1357
- // Initialize shiki async, and then highlight initial document
1358
- async initDecorations() {
1359
- const i = r.state.doc;
1360
- await Cn({ doc: i, name: e, defaultLanguage: t, defaultTheme: n });
1361
- const c = r.state.tr.setMeta("shikiPluginForceDecoration", !0);
1362
- r.dispatch(c);
1363
- }
1364
- // When new codeblocks were added and they have missing themes or
1365
- // languages, load those and then add code decorations once again.
1366
- async checkUndecoratedBlocks() {
1367
- const i = F(
1368
- r.state.doc,
1369
- (u) => u.type.name === e
1370
- );
1371
- if ((await Promise.all(
1372
- i.flatMap((u) => [
1373
- ye(u.node.attrs.theme),
1374
- we(u.node.attrs.language)
1375
- ])
1376
- )).includes(!0)) {
1377
- const u = r.state.tr.setMeta("shikiPluginForceDecoration", !0);
1378
- r.dispatch(u);
1379
- }
1380
- }
1381
- }
1382
- return new s();
1383
- },
1384
- state: {
1385
- init: (r, { doc: s }) => ie({
1386
- doc: s,
1387
- name: e,
1388
- defaultLanguage: t,
1389
- defaultTheme: n
1390
- }),
1391
- apply: (r, s, a, i) => {
1392
- const c = a.selection.$head.parent.type.name, l = i.selection.$head.parent.type.name, u = F(
1393
- a.doc,
1394
- (p) => p.type.name === e
1395
- ), d = F(
1396
- i.doc,
1397
- (p) => p.type.name === e
1398
- ), f = r.docChanged && // Apply decorations if:
1399
- // selection includes named node,
1400
- ([c, l].includes(e) || // OR transaction adds/removes named node,
1401
- d.length !== u.length || // OR transaction has changes that completely encapsulte a node
1402
- // (for example, a transaction that affects the entire document).
1403
- // Such transactions can happen during collab syncing via y-prosemirror, for example.
1404
- r.steps.some((p) => (
1405
- // @ts-ignore
1406
- p.from !== void 0 && // @ts-ignore
1407
- p.to !== void 0 && u.some((m) => (
1408
- // @ts-ignore
1409
- m.pos >= p.from && // @ts-ignore
1410
- m.pos + m.node.nodeSize <= p.to
1411
- ))
1412
- )));
1413
- return r.getMeta("shikiPluginForceDecoration") || f ? ie({
1414
- doc: r.doc,
1415
- name: e,
1416
- defaultLanguage: t,
1417
- defaultTheme: n
1418
- }) : s.map(r.mapping, r.doc);
1419
- }
1420
- },
1421
- props: {
1422
- decorations(r) {
1423
- return o.getState(r);
1424
- }
1425
- }
1426
- });
1427
- return o;
1428
- }
1429
- const Fn = Mn.extend({
1430
- addOptions() {
1431
- var e;
1432
- return {
1433
- ...(e = this.parent) == null ? void 0 : e.call(this),
1434
- defaultLanguage: null,
1435
- defaultTheme: "github-dark"
1436
- };
1437
- },
1438
- addProseMirrorPlugins() {
1439
- var e;
1440
- return [
1441
- ...((e = this.parent) == null ? void 0 : e.call(this)) || [],
1442
- En({
1443
- name: this.name,
1444
- defaultLanguage: this.options.defaultLanguage,
1445
- defaultTheme: this.options.defaultTheme
1446
- })
1447
- ];
1448
- }
1449
- });
1450
- export {
1451
- Fn as CodeBlockShiki,
1452
- Fn as default
1453
- };