UNPKG

reactjs-tiptap-editor

Version:

A modern WYSIWYG rich text editor based on tiptap and shadcn ui for React

1,690 lines (1,688 loc) 411 kB
function F(r) { this.content = r; } F.prototype = { constructor: F, find: function(r) { for (var e = 0; e < this.content.length; e += 2) if (this.content[e] === r) return e; return -1; }, // :: (string) → ?any // Retrieve the value stored under `key`, or return undefined when // no such key exists. get: function(r) { var e = this.find(r); return e == -1 ? void 0 : this.content[e + 1]; }, // :: (string, any, ?string) → OrderedMap // Create a new map by replacing the value of `key` with a new // value, or adding a binding to the end of the map. If `newKey` is // given, the key of the binding will be replaced with that key. update: function(r, e, t) { var n = t && t != r ? this.remove(t) : this, i = n.find(r), s = n.content.slice(); return i == -1 ? s.push(t || r, e) : (s[i + 1] = e, t && (s[i] = t)), new F(s); }, // :: (string) → OrderedMap // Return a map with the given key removed, if it existed. remove: function(r) { var e = this.find(r); if (e == -1) return this; var t = this.content.slice(); return t.splice(e, 2), new F(t); }, // :: (string, any) → OrderedMap // Add a new key to the start of the map. addToStart: function(r, e) { return new F([r, e].concat(this.remove(r).content)); }, // :: (string, any) → OrderedMap // Add a new key to the end of the map. addToEnd: function(r, e) { var t = this.remove(r).content.slice(); return t.push(r, e), new F(t); }, // :: (string, string, any) → OrderedMap // Add a key after the given key. If `place` is not found, the new // key is added to the end. addBefore: function(r, e, t) { var n = this.remove(e), i = n.content.slice(), s = n.find(r); return i.splice(s == -1 ? i.length : s, 0, e, t), new F(i); }, // :: ((key: string, value: any)) // Call the given function for each key/value pair in the map, in // order. forEach: function(r) { for (var e = 0; e < this.content.length; e += 2) r(this.content[e], this.content[e + 1]); }, // :: (union<Object, OrderedMap>) → OrderedMap // Create a new map by prepending the keys in this map that don't // appear in `map` before the keys in `map`. prepend: function(r) { return r = F.from(r), r.size ? new F(r.content.concat(this.subtract(r).content)) : this; }, // :: (union<Object, OrderedMap>) → OrderedMap // Create a new map by appending the keys in this map that don't // appear in `map` after the keys in `map`. append: function(r) { return r = F.from(r), r.size ? new F(this.subtract(r).content.concat(r.content)) : this; }, // :: (union<Object, OrderedMap>) → OrderedMap // Create a map containing all the keys in this map that don't // appear in `map`. subtract: function(r) { var e = this; r = F.from(r); for (var t = 0; t < r.content.length; t += 2) e = e.remove(r.content[t]); return e; }, // :: () → Object // Turn ordered map into a plain object. toObject: function() { var r = {}; return this.forEach(function(e, t) { r[e] = t; }), r; }, // :: number // The amount of keys in this map. get size() { return this.content.length >> 1; } }; F.from = function(r) { if (r instanceof F) return r; var e = []; if (r) for (var t in r) e.push(t, r[t]); return new F(e); }; function xi(r, e, t) { for (let n = 0; ; n++) { if (n == r.childCount || n == e.childCount) return r.childCount == e.childCount ? null : t; let i = r.child(n), s = e.child(n); if (i == s) { t += i.nodeSize; continue; } if (!i.sameMarkup(s)) return t; if (i.isText && i.text != s.text) { for (let o = 0; i.text[o] == s.text[o]; o++) t++; return t; } if (i.content.size || s.content.size) { let o = xi(i.content, s.content, t + 1); if (o != null) return o; } t += i.nodeSize; } } function Si(r, e, t, n) { for (let i = r.childCount, s = e.childCount; ; ) { if (i == 0 || s == 0) return i == s ? null : { a: t, b: n }; let o = r.child(--i), l = e.child(--s), a = o.nodeSize; if (o == l) { t -= a, n -= a; continue; } if (!o.sameMarkup(l)) return { a: t, b: n }; if (o.isText && o.text != l.text) { let c = 0, f = Math.min(o.text.length, l.text.length); for (; c < f && o.text[o.text.length - c - 1] == l.text[l.text.length - c - 1]; ) c++, t--, n--; return { a: t, b: n }; } if (o.content.size || l.content.size) { let c = Si(o.content, l.content, t - 1, n - 1); if (c) return c; } t -= a, n -= a; } } class b { /** @internal */ constructor(e, t) { if (this.content = e, this.size = t || 0, t == null) for (let n = 0; n < e.length; n++) this.size += e[n].nodeSize; } /** Invoke a callback for all descendant nodes between the given two positions (relative to start of this fragment). Doesn't descend into a node when the callback returns `false`. */ nodesBetween(e, t, n, i = 0, s) { for (let o = 0, l = 0; l < t; o++) { let a = this.content[o], c = l + a.nodeSize; if (c > e && n(a, i + l, s || null, o) !== !1 && a.content.size) { let f = l + 1; a.nodesBetween(Math.max(0, e - f), Math.min(a.content.size, t - f), n, i + f); } l = c; } } /** Call the given callback for every descendant node. `pos` will be relative to the start of the fragment. The callback may return `false` to prevent traversal of a given node's children. */ descendants(e) { this.nodesBetween(0, this.size, e); } /** Extract the text between `from` and `to`. See the same method on [`Node`](https://prosemirror.net/docs/ref/#model.Node.textBetween). */ textBetween(e, t, n, i) { let s = "", o = !0; return this.nodesBetween(e, t, (l, a) => { let c = l.isText ? l.text.slice(Math.max(e, a) - a, t - a) : l.isLeaf ? i ? typeof i == "function" ? i(l) : i : l.type.spec.leafText ? l.type.spec.leafText(l) : "" : ""; l.isBlock && (l.isLeaf && c || l.isTextblock) && n && (o ? o = !1 : s += n), s += c; }, 0), s; } /** Create a new fragment containing the combined content of this fragment and the other. */ append(e) { if (!e.size) return this; if (!this.size) return e; let t = this.lastChild, n = e.firstChild, i = this.content.slice(), s = 0; for (t.isText && t.sameMarkup(n) && (i[i.length - 1] = t.withText(t.text + n.text), s = 1); s < e.content.length; s++) i.push(e.content[s]); return new b(i, this.size + e.size); } /** Cut out the sub-fragment between the two given positions. */ cut(e, t = this.size) { if (e == 0 && t == this.size) return this; let n = [], i = 0; if (t > e) for (let s = 0, o = 0; o < t; s++) { let l = this.content[s], a = o + l.nodeSize; a > e && ((o < e || a > t) && (l.isText ? l = l.cut(Math.max(0, e - o), Math.min(l.text.length, t - o)) : l = l.cut(Math.max(0, e - o - 1), Math.min(l.content.size, t - o - 1))), n.push(l), i += l.nodeSize), o = a; } return new b(n, i); } /** @internal */ cutByIndex(e, t) { return e == t ? b.empty : e == 0 && t == this.content.length ? this : new b(this.content.slice(e, t)); } /** Create a new fragment in which the node at the given index is replaced by the given node. */ replaceChild(e, t) { let n = this.content[e]; if (n == t) return this; let i = this.content.slice(), s = this.size + t.nodeSize - n.nodeSize; return i[e] = t, new b(i, s); } /** Create a new fragment by prepending the given node to this fragment. */ addToStart(e) { return new b([e].concat(this.content), this.size + e.nodeSize); } /** Create a new fragment by appending the given node to this fragment. */ addToEnd(e) { return new b(this.content.concat(e), this.size + e.nodeSize); } /** Compare this fragment to another one. */ eq(e) { if (this.content.length != e.content.length) return !1; for (let t = 0; t < this.content.length; t++) if (!this.content[t].eq(e.content[t])) return !1; return !0; } /** The first child of the fragment, or `null` if it is empty. */ get firstChild() { return this.content.length ? this.content[0] : null; } /** The last child of the fragment, or `null` if it is empty. */ get lastChild() { return this.content.length ? this.content[this.content.length - 1] : null; } /** The number of child nodes in this fragment. */ get childCount() { return this.content.length; } /** Get the child node at the given index. Raise an error when the index is out of range. */ child(e) { let t = this.content[e]; if (!t) throw new RangeError("Index " + e + " out of range for " + this); return t; } /** Get the child node at the given index, if it exists. */ maybeChild(e) { return this.content[e] || null; } /** Call `f` for every child node, passing the node, its offset into this parent node, and its index. */ forEach(e) { for (let t = 0, n = 0; t < this.content.length; t++) { let i = this.content[t]; e(i, n, t), n += i.nodeSize; } } /** Find the first position at which this fragment and another fragment differ, or `null` if they are the same. */ findDiffStart(e, t = 0) { return xi(this, e, t); } /** Find the first position, searching from the end, at which this fragment and the given fragment differ, or `null` if they are the same. Since this position will not be the same in both nodes, an object with two separate positions is returned. */ findDiffEnd(e, t = this.size, n = e.size) { return Si(this, e, t, n); } /** Find the index and inner offset corresponding to a given relative position in this fragment. The result object will be reused (overwritten) the next time the function is called. @internal */ findIndex(e, t = -1) { if (e == 0) return Mt(0, e); if (e == this.size) return Mt(this.content.length, e); if (e > this.size || e < 0) throw new RangeError(`Position ${e} outside of fragment (${this})`); for (let n = 0, i = 0; ; n++) { let s = this.child(n), o = i + s.nodeSize; if (o >= e) return o == e || t > 0 ? Mt(n + 1, o) : Mt(n, i); i = o; } } /** Return a debugging string that describes this fragment. */ toString() { return "<" + this.toStringInner() + ">"; } /** @internal */ toStringInner() { return this.content.join(", "); } /** Create a JSON-serializeable representation of this fragment. */ toJSON() { return this.content.length ? this.content.map((e) => e.toJSON()) : null; } /** Deserialize a fragment from its JSON representation. */ static fromJSON(e, t) { if (!t) return b.empty; if (!Array.isArray(t)) throw new RangeError("Invalid input for Fragment.fromJSON"); return new b(t.map(e.nodeFromJSON)); } /** Build a fragment from an array of nodes. Ensures that adjacent text nodes with the same marks are joined together. */ static fromArray(e) { if (!e.length) return b.empty; let t, n = 0; for (let i = 0; i < e.length; i++) { let s = e[i]; n += s.nodeSize, i && s.isText && e[i - 1].sameMarkup(s) ? (t || (t = e.slice(0, i)), t[t.length - 1] = s.withText(t[t.length - 1].text + s.text)) : t && t.push(s); } return new b(t || e, n); } /** Create a fragment from something that can be interpreted as a set of nodes. For `null`, it returns the empty fragment. For a fragment, the fragment itself. For a node or array of nodes, a fragment containing those nodes. */ static from(e) { if (!e) return b.empty; if (e instanceof b) return e; if (Array.isArray(e)) return this.fromArray(e); if (e.attrs) return new b([e], e.nodeSize); throw new RangeError("Can not convert " + e + " to a Fragment" + (e.nodesBetween ? " (looks like multiple versions of prosemirror-model were loaded)" : "")); } } b.empty = new b([], 0); const ln = { index: 0, offset: 0 }; function Mt(r, e) { return ln.index = r, ln.offset = e, ln; } function vt(r, e) { if (r === e) return !0; if (!(r && typeof r == "object") || !(e && typeof e == "object")) return !1; let t = Array.isArray(r); if (Array.isArray(e) != t) return !1; if (t) { if (r.length != e.length) return !1; for (let n = 0; n < r.length; n++) if (!vt(r[n], e[n])) return !1; } else { for (let n in r) if (!(n in e) || !vt(r[n], e[n])) return !1; for (let n in e) if (!(n in r)) return !1; } return !0; } let I = class On { /** @internal */ constructor(e, t) { this.type = e, this.attrs = t; } /** Given a set of marks, create a new set which contains this one as well, in the right position. If this mark is already in the set, the set itself is returned. If any marks that are set to be [exclusive](https://prosemirror.net/docs/ref/#model.MarkSpec.excludes) with this mark are present, those are replaced by this one. */ addToSet(e) { let t, n = !1; for (let i = 0; i < e.length; i++) { let s = e[i]; if (this.eq(s)) return e; if (this.type.excludes(s.type)) t || (t = e.slice(0, i)); else { if (s.type.excludes(this.type)) return e; !n && s.type.rank > this.type.rank && (t || (t = e.slice(0, i)), t.push(this), n = !0), t && t.push(s); } } return t || (t = e.slice()), n || t.push(this), t; } /** Remove this mark from the given set, returning a new set. If this mark is not in the set, the set itself is returned. */ removeFromSet(e) { for (let t = 0; t < e.length; t++) if (this.eq(e[t])) return e.slice(0, t).concat(e.slice(t + 1)); return e; } /** Test whether this mark is in the given set of marks. */ isInSet(e) { for (let t = 0; t < e.length; t++) if (this.eq(e[t])) return !0; return !1; } /** Test whether this mark has the same type and attributes as another mark. */ eq(e) { return this == e || this.type == e.type && vt(this.attrs, e.attrs); } /** Convert this mark to a JSON-serializeable representation. */ toJSON() { let e = { type: this.type.name }; for (let t in this.attrs) { e.attrs = this.attrs; break; } return e; } /** Deserialize a mark from JSON. */ static fromJSON(e, t) { if (!t) throw new RangeError("Invalid input for Mark.fromJSON"); let n = e.marks[t.type]; if (!n) throw new RangeError(`There is no mark type ${t.type} in this schema`); let i = n.create(t.attrs); return n.checkAttrs(i.attrs), i; } /** Test whether two sets of marks are identical. */ static sameSet(e, t) { if (e == t) return !0; if (e.length != t.length) return !1; for (let n = 0; n < e.length; n++) if (!e[n].eq(t[n])) return !1; return !0; } /** Create a properly sorted mark set from null, a single mark, or an unsorted array of marks. */ static setFrom(e) { if (!e || Array.isArray(e) && e.length == 0) return On.none; if (e instanceof On) return [e]; let t = e.slice(); return t.sort((n, i) => n.type.rank - i.type.rank), t; } }; I.none = []; class Pt extends Error { } class k { /** Create a slice. When specifying a non-zero open depth, you must make sure that there are nodes of at least that depth at the appropriate side of the fragment—i.e. if the fragment is an empty paragraph node, `openStart` and `openEnd` can't be greater than 1. It is not necessary for the content of open nodes to conform to the schema's content constraints, though it should be a valid start/end/middle for such a node, depending on which sides are open. */ constructor(e, t, n) { this.content = e, this.openStart = t, this.openEnd = n; } /** The size this slice would add when inserted into a document. */ get size() { return this.content.size - this.openStart - this.openEnd; } /** @internal */ insertAt(e, t) { let n = Mi(this.content, e + this.openStart, t); return n && new k(n, this.openStart, this.openEnd); } /** @internal */ removeBetween(e, t) { return new k(Ci(this.content, e + this.openStart, t + this.openStart), this.openStart, this.openEnd); } /** Tests whether this slice is equal to another slice. */ eq(e) { return this.content.eq(e.content) && this.openStart == e.openStart && this.openEnd == e.openEnd; } /** @internal */ toString() { return this.content + "(" + this.openStart + "," + this.openEnd + ")"; } /** Convert a slice to a JSON-serializable representation. */ toJSON() { if (!this.content.size) return null; let e = { content: this.content.toJSON() }; return this.openStart > 0 && (e.openStart = this.openStart), this.openEnd > 0 && (e.openEnd = this.openEnd), e; } /** Deserialize a slice from its JSON representation. */ static fromJSON(e, t) { if (!t) return k.empty; let n = t.openStart || 0, i = t.openEnd || 0; if (typeof n != "number" || typeof i != "number") throw new RangeError("Invalid input for Slice.fromJSON"); return new k(b.fromJSON(e, t.content), n, i); } /** Create a slice from a fragment by taking the maximum possible open value on both side of the fragment. */ static maxOpen(e, t = !0) { let n = 0, i = 0; for (let s = e.firstChild; s && !s.isLeaf && (t || !s.type.spec.isolating); s = s.firstChild) n++; for (let s = e.lastChild; s && !s.isLeaf && (t || !s.type.spec.isolating); s = s.lastChild) i++; return new k(e, n, i); } } k.empty = new k(b.empty, 0, 0); function Ci(r, e, t) { let { index: n, offset: i } = r.findIndex(e), s = r.maybeChild(n), { index: o, offset: l } = r.findIndex(t); if (i == e || s.isText) { if (l != t && !r.child(o).isText) throw new RangeError("Removing non-flat range"); return r.cut(0, e).append(r.cut(t)); } if (n != o) throw new RangeError("Removing non-flat range"); return r.replaceChild(n, s.copy(Ci(s.content, e - i - 1, t - i - 1))); } function Mi(r, e, t, n) { let { index: i, offset: s } = r.findIndex(e), o = r.maybeChild(i); if (s == e || o.isText) return r.cut(0, e).append(t).append(r.cut(e)); let l = Mi(o.content, e - s - 1, t); return l && r.replaceChild(i, o.copy(l)); } function so(r, e, t) { if (t.openStart > r.depth) throw new Pt("Inserted content deeper than insertion position"); if (r.depth - t.openStart != e.depth - t.openEnd) throw new Pt("Inconsistent open depths"); return wi(r, e, t, 0); } function wi(r, e, t, n) { let i = r.index(n), s = r.node(n); if (i == e.index(n) && n < r.depth - t.openStart) { let o = wi(r, e, t, n + 1); return s.copy(s.content.replaceChild(i, o)); } else if (t.content.size) if (!t.openStart && !t.openEnd && r.depth == n && e.depth == n) { let o = r.parent, l = o.content; return Pe(o, l.cut(0, r.parentOffset).append(t.content).append(l.cut(e.parentOffset))); } else { let { start: o, end: l } = oo(t, r); return Pe(s, Ni(r, o, l, e, n)); } else return Pe(s, Bt(r, e, n)); } function Oi(r, e) { if (!e.type.compatibleContent(r.type)) throw new Pt("Cannot join " + e.type.name + " onto " + r.type.name); } function Nn(r, e, t) { let n = r.node(t); return Oi(n, e.node(t)), n; } function ve(r, e) { let t = e.length - 1; t >= 0 && r.isText && r.sameMarkup(e[t]) ? e[t] = r.withText(e[t].text + r.text) : e.push(r); } function st(r, e, t, n) { let i = (e || r).node(t), s = 0, o = e ? e.index(t) : i.childCount; r && (s = r.index(t), r.depth > t ? s++ : r.textOffset && (ve(r.nodeAfter, n), s++)); for (let l = s; l < o; l++) ve(i.child(l), n); e && e.depth == t && e.textOffset && ve(e.nodeBefore, n); } function Pe(r, e) { return r.type.checkContent(e), r.copy(e); } function Ni(r, e, t, n, i) { let s = r.depth > i && Nn(r, e, i + 1), o = n.depth > i && Nn(t, n, i + 1), l = []; return st(null, r, i, l), s && o && e.index(i) == t.index(i) ? (Oi(s, o), ve(Pe(s, Ni(r, e, t, n, i + 1)), l)) : (s && ve(Pe(s, Bt(r, e, i + 1)), l), st(e, t, i, l), o && ve(Pe(o, Bt(t, n, i + 1)), l)), st(n, null, i, l), new b(l); } function Bt(r, e, t) { let n = []; if (st(null, r, t, n), r.depth > t) { let i = Nn(r, e, t + 1); ve(Pe(i, Bt(r, e, t + 1)), n); } return st(e, null, t, n), new b(n); } function oo(r, e) { let t = e.depth - r.openStart, i = e.node(t).copy(r.content); for (let s = t - 1; s >= 0; s--) i = e.node(s).copy(b.from(i)); return { start: i.resolveNoCache(r.openStart + t), end: i.resolveNoCache(i.content.size - r.openEnd - t) }; } class ft { /** @internal */ constructor(e, t, n) { this.pos = e, this.path = t, this.parentOffset = n, this.depth = t.length / 3 - 1; } /** @internal */ resolveDepth(e) { return e == null ? this.depth : e < 0 ? this.depth + e : e; } /** The parent node that the position points into. Note that even if a position points into a text node, that node is not considered the parent—text nodes are ‘flat’ in this model, and have no content. */ get parent() { return this.node(this.depth); } /** The root node in which the position was resolved. */ get doc() { return this.node(0); } /** The ancestor node at the given level. `p.node(p.depth)` is the same as `p.parent`. */ node(e) { return this.path[this.resolveDepth(e) * 3]; } /** The index into the ancestor at the given level. If this points at the 3rd node in the 2nd paragraph on the top level, for example, `p.index(0)` is 1 and `p.index(1)` is 2. */ index(e) { return this.path[this.resolveDepth(e) * 3 + 1]; } /** The index pointing after this position into the ancestor at the given level. */ indexAfter(e) { return e = this.resolveDepth(e), this.index(e) + (e == this.depth && !this.textOffset ? 0 : 1); } /** The (absolute) position at the start of the node at the given level. */ start(e) { return e = this.resolveDepth(e), e == 0 ? 0 : this.path[e * 3 - 1] + 1; } /** The (absolute) position at the end of the node at the given level. */ end(e) { return e = this.resolveDepth(e), this.start(e) + this.node(e).content.size; } /** The (absolute) position directly before the wrapping node at the given level, or, when `depth` is `this.depth + 1`, the original position. */ before(e) { if (e = this.resolveDepth(e), !e) throw new RangeError("There is no position before the top-level node"); return e == this.depth + 1 ? this.pos : this.path[e * 3 - 1]; } /** The (absolute) position directly after the wrapping node at the given level, or the original position when `depth` is `this.depth + 1`. */ after(e) { if (e = this.resolveDepth(e), !e) throw new RangeError("There is no position after the top-level node"); return e == this.depth + 1 ? this.pos : this.path[e * 3 - 1] + this.path[e * 3].nodeSize; } /** When this position points into a text node, this returns the distance between the position and the start of the text node. Will be zero for positions that point between nodes. */ get textOffset() { return this.pos - this.path[this.path.length - 1]; } /** Get the node directly after the position, if any. If the position points into a text node, only the part of that node after the position is returned. */ get nodeAfter() { let e = this.parent, t = this.index(this.depth); if (t == e.childCount) return null; let n = this.pos - this.path[this.path.length - 1], i = e.child(t); return n ? e.child(t).cut(n) : i; } /** Get the node directly before the position, if any. If the position points into a text node, only the part of that node before the position is returned. */ get nodeBefore() { let e = this.index(this.depth), t = this.pos - this.path[this.path.length - 1]; return t ? this.parent.child(e).cut(0, t) : e == 0 ? null : this.parent.child(e - 1); } /** Get the position at the given index in the parent node at the given depth (which defaults to `this.depth`). */ posAtIndex(e, t) { t = this.resolveDepth(t); let n = this.path[t * 3], i = t == 0 ? 0 : this.path[t * 3 - 1] + 1; for (let s = 0; s < e; s++) i += n.child(s).nodeSize; return i; } /** Get the marks at this position, factoring in the surrounding marks' [`inclusive`](https://prosemirror.net/docs/ref/#model.MarkSpec.inclusive) property. If the position is at the start of a non-empty node, the marks of the node after it (if any) are returned. */ marks() { let e = this.parent, t = this.index(); if (e.content.size == 0) return I.none; if (this.textOffset) return e.child(t).marks; let n = e.maybeChild(t - 1), i = e.maybeChild(t); if (!n) { let l = n; n = i, i = l; } let s = n.marks; for (var o = 0; o < s.length; o++) s[o].type.spec.inclusive === !1 && (!i || !s[o].isInSet(i.marks)) && (s = s[o--].removeFromSet(s)); return s; } /** Get the marks after the current position, if any, except those that are non-inclusive and not present at position `$end`. This is mostly useful for getting the set of marks to preserve after a deletion. Will return `null` if this position is at the end of its parent node or its parent node isn't a textblock (in which case no marks should be preserved). */ marksAcross(e) { let t = this.parent.maybeChild(this.index()); if (!t || !t.isInline) return null; let n = t.marks, i = e.parent.maybeChild(e.index()); for (var s = 0; s < n.length; s++) n[s].type.spec.inclusive === !1 && (!i || !n[s].isInSet(i.marks)) && (n = n[s--].removeFromSet(n)); return n; } /** The depth up to which this position and the given (non-resolved) position share the same parent nodes. */ sharedDepth(e) { for (let t = this.depth; t > 0; t--) if (this.start(t) <= e && this.end(t) >= e) return t; return 0; } /** Returns a range based on the place where this position and the given position diverge around block content. If both point into the same textblock, for example, a range around that textblock will be returned. If they point into different blocks, the range around those blocks in their shared ancestor is returned. You can pass in an optional predicate that will be called with a parent node to see if a range into that parent is acceptable. */ blockRange(e = this, t) { if (e.pos < this.pos) return e.blockRange(this); for (let n = this.depth - (this.parent.inlineContent || this.pos == e.pos ? 1 : 0); n >= 0; n--) if (e.pos <= this.end(n) && (!t || t(this.node(n)))) return new zt(this, e, n); return null; } /** Query whether the given position shares the same parent node. */ sameParent(e) { return this.pos - this.parentOffset == e.pos - e.parentOffset; } /** Return the greater of this and the given position. */ max(e) { return e.pos > this.pos ? e : this; } /** Return the smaller of this and the given position. */ min(e) { return e.pos < this.pos ? e : this; } /** @internal */ toString() { let e = ""; for (let t = 1; t <= this.depth; t++) e += (e ? "/" : "") + this.node(t).type.name + "_" + this.index(t - 1); return e + ":" + this.parentOffset; } /** @internal */ static resolve(e, t) { if (!(t >= 0 && t <= e.content.size)) throw new RangeError("Position " + t + " out of range"); let n = [], i = 0, s = t; for (let o = e; ; ) { let { index: l, offset: a } = o.content.findIndex(s), c = s - a; if (n.push(o, l, i + a), !c || (o = o.child(l), o.isText)) break; s = c - 1, i += a + 1; } return new ft(t, n, s); } /** @internal */ static resolveCached(e, t) { let n = pr.get(e); if (n) for (let s = 0; s < n.elts.length; s++) { let o = n.elts[s]; if (o.pos == t) return o; } else pr.set(e, n = new lo()); let i = n.elts[n.i] = ft.resolve(e, t); return n.i = (n.i + 1) % ao, i; } } class lo { constructor() { this.elts = [], this.i = 0; } } const ao = 12, pr = /* @__PURE__ */ new WeakMap(); class zt { /** Construct a node range. `$from` and `$to` should point into the same node until at least the given `depth`, since a node range denotes an adjacent set of nodes in a single parent node. */ constructor(e, t, n) { this.$from = e, this.$to = t, this.depth = n; } /** The position at the start of the range. */ get start() { return this.$from.before(this.depth + 1); } /** The position at the end of the range. */ get end() { return this.$to.after(this.depth + 1); } /** The parent node that the range points into. */ get parent() { return this.$from.node(this.depth); } /** The start index of the range in the parent node. */ get startIndex() { return this.$from.index(this.depth); } /** The end index of the range in the parent node. */ get endIndex() { return this.$to.indexAfter(this.depth); } } const co = /* @__PURE__ */ Object.create(null); let xe = class Tn { /** @internal */ constructor(e, t, n, i = I.none) { this.type = e, this.attrs = t, this.marks = i, this.content = n || b.empty; } /** The array of this node's child nodes. */ get children() { return this.content.content; } /** The size of this node, as defined by the integer-based [indexing scheme](/docs/guide/#doc.indexing). For text nodes, this is the amount of characters. For other leaf nodes, it is one. For non-leaf nodes, it is the size of the content plus two (the start and end token). */ get nodeSize() { return this.isLeaf ? 1 : 2 + this.content.size; } /** The number of children that the node has. */ get childCount() { return this.content.childCount; } /** Get the child node at the given index. Raises an error when the index is out of range. */ child(e) { return this.content.child(e); } /** Get the child node at the given index, if it exists. */ maybeChild(e) { return this.content.maybeChild(e); } /** Call `f` for every child node, passing the node, its offset into this parent node, and its index. */ forEach(e) { this.content.forEach(e); } /** Invoke a callback for all descendant nodes recursively between the given two positions that are relative to start of this node's content. The callback is invoked with the node, its position relative to the original node (method receiver), its parent node, and its child index. When the callback returns false for a given node, that node's children will not be recursed over. The last parameter can be used to specify a starting position to count from. */ nodesBetween(e, t, n, i = 0) { this.content.nodesBetween(e, t, n, i, this); } /** Call the given callback for every descendant node. Doesn't descend into a node when the callback returns `false`. */ descendants(e) { this.nodesBetween(0, this.content.size, e); } /** Concatenates all the text nodes found in this fragment and its children. */ get textContent() { return this.isLeaf && this.type.spec.leafText ? this.type.spec.leafText(this) : this.textBetween(0, this.content.size, ""); } /** Get all text between positions `from` and `to`. When `blockSeparator` is given, it will be inserted to separate text from different block nodes. If `leafText` is given, it'll be inserted for every non-text leaf node encountered, otherwise [`leafText`](https://prosemirror.net/docs/ref/#model.NodeSpec^leafText) will be used. */ textBetween(e, t, n, i) { return this.content.textBetween(e, t, n, i); } /** Returns this node's first child, or `null` if there are no children. */ get firstChild() { return this.content.firstChild; } /** Returns this node's last child, or `null` if there are no children. */ get lastChild() { return this.content.lastChild; } /** Test whether two nodes represent the same piece of document. */ eq(e) { return this == e || this.sameMarkup(e) && this.content.eq(e.content); } /** Compare the markup (type, attributes, and marks) of this node to those of another. Returns `true` if both have the same markup. */ sameMarkup(e) { return this.hasMarkup(e.type, e.attrs, e.marks); } /** Check whether this node's markup correspond to the given type, attributes, and marks. */ hasMarkup(e, t, n) { return this.type == e && vt(this.attrs, t || e.defaultAttrs || co) && I.sameSet(this.marks, n || I.none); } /** Create a new node with the same markup as this node, containing the given content (or empty, if no content is given). */ copy(e = null) { return e == this.content ? this : new Tn(this.type, this.attrs, e, this.marks); } /** Create a copy of this node, with the given set of marks instead of the node's own marks. */ mark(e) { return e == this.marks ? this : new Tn(this.type, this.attrs, this.content, e); } /** Create a copy of this node with only the content between the given positions. If `to` is not given, it defaults to the end of the node. */ cut(e, t = this.content.size) { return e == 0 && t == this.content.size ? this : this.copy(this.content.cut(e, t)); } /** Cut out the part of the document between the given positions, and return it as a `Slice` object. */ slice(e, t = this.content.size, n = !1) { if (e == t) return k.empty; let i = this.resolve(e), s = this.resolve(t), o = n ? 0 : i.sharedDepth(t), l = i.start(o), c = i.node(o).content.cut(i.pos - l, s.pos - l); return new k(c, i.depth - o, s.depth - o); } /** Replace the part of the document between the given positions with the given slice. The slice must 'fit', meaning its open sides must be able to connect to the surrounding content, and its content nodes must be valid children for the node they are placed into. If any of this is violated, an error of type [`ReplaceError`](https://prosemirror.net/docs/ref/#model.ReplaceError) is thrown. */ replace(e, t, n) { return so(this.resolve(e), this.resolve(t), n); } /** Find the node directly after the given position. */ nodeAt(e) { for (let t = this; ; ) { let { index: n, offset: i } = t.content.findIndex(e); if (t = t.maybeChild(n), !t) return null; if (i == e || t.isText) return t; e -= i + 1; } } /** Find the (direct) child node after the given offset, if any, and return it along with its index and offset relative to this node. */ childAfter(e) { let { index: t, offset: n } = this.content.findIndex(e); return { node: this.content.maybeChild(t), index: t, offset: n }; } /** Find the (direct) child node before the given offset, if any, and return it along with its index and offset relative to this node. */ childBefore(e) { if (e == 0) return { node: null, index: 0, offset: 0 }; let { index: t, offset: n } = this.content.findIndex(e); if (n < e) return { node: this.content.child(t), index: t, offset: n }; let i = this.content.child(t - 1); return { node: i, index: t - 1, offset: n - i.nodeSize }; } /** Resolve the given position in the document, returning an [object](https://prosemirror.net/docs/ref/#model.ResolvedPos) with information about its context. */ resolve(e) { return ft.resolveCached(this, e); } /** @internal */ resolveNoCache(e) { return ft.resolve(this, e); } /** Test whether a given mark or mark type occurs in this document between the two given positions. */ rangeHasMark(e, t, n) { let i = !1; return t > e && this.nodesBetween(e, t, (s) => (n.isInSet(s.marks) && (i = !0), !i)), i; } /** True when this is a block (non-inline node) */ get isBlock() { return this.type.isBlock; } /** True when this is a textblock node, a block node with inline content. */ get isTextblock() { return this.type.isTextblock; } /** True when this node allows inline content. */ get inlineContent() { return this.type.inlineContent; } /** True when this is an inline node (a text node or a node that can appear among text). */ get isInline() { return this.type.isInline; } /** True when this is a text node. */ get isText() { return this.type.isText; } /** True when this is a leaf node. */ get isLeaf() { return this.type.isLeaf; } /** True when this is an atom, i.e. when it does not have directly editable content. This is usually the same as `isLeaf`, but can be configured with the [`atom` property](https://prosemirror.net/docs/ref/#model.NodeSpec.atom) on a node's spec (typically used when the node is displayed as an uneditable [node view](https://prosemirror.net/docs/ref/#view.NodeView)). */ get isAtom() { return this.type.isAtom; } /** Return a string representation of this node for debugging purposes. */ toString() { if (this.type.spec.toDebugString) return this.type.spec.toDebugString(this); let e = this.type.name; return this.content.size && (e += "(" + this.content.toStringInner() + ")"), Ti(this.marks, e); } /** Get the content match in this node at the given index. */ contentMatchAt(e) { let t = this.type.contentMatch.matchFragment(this.content, 0, e); if (!t) throw new Error("Called contentMatchAt on a node with invalid content"); return t; } /** Test whether replacing the range between `from` and `to` (by child index) with the given replacement fragment (which defaults to the empty fragment) would leave the node's content valid. You can optionally pass `start` and `end` indices into the replacement fragment. */ canReplace(e, t, n = b.empty, i = 0, s = n.childCount) { let o = this.contentMatchAt(e).matchFragment(n, i, s), l = o && o.matchFragment(this.content, t); if (!l || !l.validEnd) return !1; for (let a = i; a < s; a++) if (!this.type.allowsMarks(n.child(a).marks)) return !1; return !0; } /** Test whether replacing the range `from` to `to` (by index) with a node of the given type would leave the node's content valid. */ canReplaceWith(e, t, n, i) { if (i && !this.type.allowsMarks(i)) return !1; let s = this.contentMatchAt(e).matchType(n), o = s && s.matchFragment(this.content, t); return o ? o.validEnd : !1; } /** Test whether the given node's content could be appended to this node. If that node is empty, this will only return true if there is at least one node type that can appear in both nodes (to avoid merging completely incompatible nodes). */ canAppend(e) { return e.content.size ? this.canReplace(this.childCount, this.childCount, e.content) : this.type.compatibleContent(e.type); } /** Check whether this node and its descendants conform to the schema, and raise an exception when they do not. */ check() { this.type.checkContent(this.content), this.type.checkAttrs(this.attrs); let e = I.none; for (let t = 0; t < this.marks.length; t++) { let n = this.marks[t]; n.type.checkAttrs(n.attrs), e = n.addToSet(e); } if (!I.sameSet(e, this.marks)) throw new RangeError(`Invalid collection of marks for node ${this.type.name}: ${this.marks.map((t) => t.type.name)}`); this.content.forEach((t) => t.check()); } /** Return a JSON-serializeable representation of this node. */ toJSON() { let e = { type: this.type.name }; for (let t in this.attrs) { e.attrs = this.attrs; break; } return this.content.size && (e.content = this.content.toJSON()), this.marks.length && (e.marks = this.marks.map((t) => t.toJSON())), e; } /** Deserialize a node from its JSON representation. */ static fromJSON(e, t) { if (!t) throw new RangeError("Invalid input for Node.fromJSON"); let n; if (t.marks) { if (!Array.isArray(t.marks)) throw new RangeError("Invalid mark data for Node.fromJSON"); n = t.marks.map(e.markFromJSON); } if (t.type == "text") { if (typeof t.text != "string") throw new RangeError("Invalid text node in JSON"); return e.text(t.text, n); } let i = b.fromJSON(e, t.content), s = e.nodeType(t.type).create(t.attrs, i, n); return s.type.checkAttrs(s.attrs), s; } }; xe.prototype.text = void 0; class Ft extends xe { /** @internal */ constructor(e, t, n, i) { if (super(e, t, null, i), !n) throw new RangeError("Empty text nodes are not allowed"); this.text = n; } toString() { return this.type.spec.toDebugString ? this.type.spec.toDebugString(this) : Ti(this.marks, JSON.stringify(this.text)); } get textContent() { return this.text; } textBetween(e, t) { return this.text.slice(e, t); } get nodeSize() { return this.text.length; } mark(e) { return e == this.marks ? this : new Ft(this.type, this.attrs, this.text, e); } withText(e) { return e == this.text ? this : new Ft(this.type, this.attrs, e, this.marks); } cut(e = 0, t = this.text.length) { return e == 0 && t == this.text.length ? this : this.withText(this.text.slice(e, t)); } eq(e) { return this.sameMarkup(e) && this.text == e.text; } toJSON() { let e = super.toJSON(); return e.text = this.text, e; } } function Ti(r, e) { for (let t = r.length - 1; t >= 0; t--) e = r[t].type.name + "(" + e + ")"; return e; } class Fe { /** @internal */ constructor(e) { this.validEnd = e, this.next = [], this.wrapCache = []; } /** @internal */ static parse(e, t) { let n = new fo(e, t); if (n.next == null) return Fe.empty; let i = Ei(n); n.next && n.err("Unexpected trailing text"); let s = bo(yo(i)); return ko(s, n), s; } /** Match a node type, returning a match after that node if successful. */ matchType(e) { for (let t = 0; t < this.next.length; t++) if (this.next[t].type == e) return this.next[t].next; return null; } /** Try to match a fragment. Returns the resulting match when successful. */ matchFragment(e, t = 0, n = e.childCount) { let i = this; for (let s = t; i && s < n; s++) i = i.matchType(e.child(s).type); return i; } /** @internal */ get inlineContent() { return this.next.length != 0 && this.next[0].type.isInline; } /** Get the first matching node type at this match position that can be generated. */ get defaultType() { for (let e = 0; e < this.next.length; e++) { let { type: t } = this.next[e]; if (!(t.isText || t.hasRequiredAttrs())) return t; } return null; } /** @internal */ compatible(e) { for (let t = 0; t < this.next.length; t++) for (let n = 0; n < e.next.length; n++) if (this.next[t].type == e.next[n].type) return !0; return !1; } /** Try to match the given fragment, and if that fails, see if it can be made to match by inserting nodes in front of it. When successful, return a fragment of inserted nodes (which may be empty if nothing had to be inserted). When `toEnd` is true, only return a fragment if the resulting match goes to the end of the content expression. */ fillBefore(e, t = !1, n = 0) { let i = [this]; function s(o, l) { let a = o.matchFragment(e, n); if (a && (!t || a.validEnd)) return b.from(l.map((c) => c.createAndFill())); for (let c = 0; c < o.next.length; c++) { let { type: f, next: d } = o.next[c]; if (!(f.isText || f.hasRequiredAttrs()) && i.indexOf(d) == -1) { i.push(d); let u = s(d, l.concat(f)); if (u) return u; } } return null; } return s(this, []); } /** Find a set of wrapping node types that would allow a node of the given type to appear at this position. The result may be empty (when it fits directly) and will be null when no such wrapping exists. */ findWrapping(e) { for (let n = 0; n < this.wrapCache.length; n += 2) if (this.wrapCache[n] == e) return this.wrapCache[n + 1]; let t = this.computeWrapping(e); return this.wrapCache.push(e, t), t; } /** @internal */ computeWrapping(e) { let t = /* @__PURE__ */ Object.create(null), n = [{ match: this, type: null, via: null }]; for (; n.length; ) { let i = n.shift(), s = i.match; if (s.matchType(e)) { let o = []; for (let l = i; l.type; l = l.via) o.push(l.type); return o.reverse(); } for (let o = 0; o < s.next.length; o++) { let { type: l, next: a } = s.next[o]; !l.isLeaf && !l.hasRequiredAttrs() && !(l.name in t) && (!i.type || a.validEnd) && (n.push({ match: l.contentMatch, type: l, via: i }), t[l.name] = !0); } } return null; } /** The number of outgoing edges this node has in the finite automaton that describes the content expression. */ get edgeCount() { return this.next.length; } /** Get the _n_​th outgoing edge from this node in the finite automaton that describes the content expression. */ edge(e) { if (e >= this.next.length) throw new RangeError(`There's no ${e}th edge in this content match`); return this.next[e]; } /** @internal */ toString() { let e = []; function t(n) { e.push(n); for (let i = 0; i < n.next.length; i++) e.indexOf(n.next[i].next) == -1 && t(n.next[i].next); } return t(this), e.map((n, i) => { let s = i + (n.validEnd ? "*" : " ") + " "; for (let o = 0; o < n.next.length; o++) s += (o ? ", " : "") + n.next[o].type.name + "->" + e.indexOf(n.next[o].next); return s; }).join(` `); } } Fe.empty = new Fe(!0); class fo { constructor(e, t) { this.string = e, this.nodeTypes = t, this.inline = null, this.pos = 0, this.tokens = e.split(/\s*(?=\b|\W|$)/), this.tokens[this.tokens.length - 1] == "" && this.tokens.pop(), this.tokens[0] == "" && this.tokens.shift(); } get next() { return this.tokens[this.pos]; } eat(e) { return this.next == e && (this.pos++ || !0); } err(e) { throw new SyntaxError(e + " (in content expression '" + this.string + "')"); } } function Ei(r) { let e = []; do e.push(uo(r)); while (r.eat("|")); return e.length == 1 ? e[0] : { type: "choice", exprs: e }; } function uo(r) { let e = []; do e.push(ho(r)); while (r.next && r.next != ")" && r.next != "|"); return e.length == 1 ? e[0] : { type: "seq", exprs: e }; } function ho(r) { let e = go(r); for (; ; ) if (r.eat("+")) e = { type: "plus", expr: e }; else if (r.eat("*")) e = { type: "star", expr: e }; else if (r.eat("?")) e = { type: "opt", expr: e }; else if (r.eat("{")) e = po(r, e); else break; return e; } function mr(r) { /\D/.test(r.next) && r.err("Expected number, got '" + r.next + "'"); let e = Number(r.next); return r.pos++, e; } function po(r, e) { let t = mr(r), n = t; return r.eat(",") && (r.next != "}" ? n = mr(r) : n = -1), r.eat("}") || r.err("Unclosed braced range"), { type: "range", min: t, max: n, expr: e }; } function mo(r, e) { let t = r.nodeTypes, n = t[e]; if (n) return [n]; let i = []; for (let s in t) { let o = t[s]; o.isInGroup(e) && i.push(o); } return i.length == 0 && r.err("No node type or group '" + e + "' found"), i; } function go(r) { if (r.eat("(")) { let e = Ei(r); return r.eat(")") || r.err("Missing closing paren"), e; } else if (/\W/.test(r.next)) r.err("Unexpected token '" + r.next + "'"); else { let e = mo(r, r.next).map((t) => (r.inline == null ? r.inline = t.isInline : r.inline != t.isInline && r.err("Mixing inline and block content"), { type: "name", value: t })); return r.pos++, e.length == 1 ? e[0] : { type: "choice", exprs: e }; } } function yo(r) { let e = [[]]; return i(s(r, 0), t()), e; function t() { return e.push([]) - 1; } function n(o, l, a) { let c = { term: a, to: l }; return e[o].push(c), c; } function i(o, l) { o.forEach((a) => a.to = l); } function s(o, l) { if (o.type == "choice") return o.exprs.reduce((a, c) => a.concat(s(c, l)), []); if (o.type == "seq") for (let a = 0; ; a++) { let c = s(o.exprs[a], l); if (a == o.exprs.length - 1) return c; i(c, l = t()); } else if (o.type == "star") { let a = t(); return n(l, a), i(s(o.expr, a), a), [n(a)]; } else if (o.type == "plus") { let a = t(); return i(s(o.expr, l), a), i(s(o.expr, a), a), [n(a)]; } else { if (o.type == "opt") return [n(l)].concat(s(o.expr, l)); if (o.type == "range") { let a = l; for (let c = 0; c < o.min; c++) { let f = t(); i(s(o.expr, a), f), a = f; } if (o.max == -1) i(s(o.expr, a), a); else for (let c = o.min; c < o.max; c++) { let f = t();