UNPKG

tiptap-editor-react

Version:

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

1,679 lines (1,678 loc) 1.48 MB
import no, { useSyncExternalStore as gD, useState as vs, Fragment as iC, useEffect as mD, useMemo as yD, createContext as oC, useLayoutEffect as aC, useContext as sC, useRef as Vr, useCallback as $l, useDebugValue as cC } from "react"; import lC from "react-dom"; import { jsx as le, jsxs as je, Fragment as uC } from "react/jsx-runtime"; function dt(n) { this.content = n; } dt.prototype = { constructor: dt, find: function(n) { for (var e = 0; e < this.content.length; e += 2) if (this.content[e] === n) return e; return -1; }, // :: (string) → ?any // Retrieve the value stored under `key`, or return undefined when // no such key exists. get: function(n) { var e = this.find(n); 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(n, e, t) { var r = t && t != n ? this.remove(t) : this, i = r.find(n), o = r.content.slice(); return i == -1 ? o.push(t || n, e) : (o[i + 1] = e, t && (o[i] = t)), new dt(o); }, // :: (string) → OrderedMap // Return a map with the given key removed, if it existed. remove: function(n) { var e = this.find(n); if (e == -1) return this; var t = this.content.slice(); return t.splice(e, 2), new dt(t); }, // :: (string, any) → OrderedMap // Add a new key to the start of the map. addToStart: function(n, e) { return new dt([n, e].concat(this.remove(n).content)); }, // :: (string, any) → OrderedMap // Add a new key to the end of the map. addToEnd: function(n, e) { var t = this.remove(n).content.slice(); return t.push(n, e), new dt(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(n, e, t) { var r = this.remove(e), i = r.content.slice(), o = r.find(n); return i.splice(o == -1 ? i.length : o, 0, e, t), new dt(i); }, // :: ((key: string, value: any)) // Call the given function for each key/value pair in the map, in // order. forEach: function(n) { for (var e = 0; e < this.content.length; e += 2) n(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(n) { return n = dt.from(n), n.size ? new dt(n.content.concat(this.subtract(n).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(n) { return n = dt.from(n), n.size ? new dt(this.subtract(n).content.concat(n.content)) : this; }, // :: (union<Object, OrderedMap>) → OrderedMap // Create a map containing all the keys in this map that don't // appear in `map`. subtract: function(n) { var e = this; n = dt.from(n); for (var t = 0; t < n.content.length; t += 2) e = e.remove(n.content[t]); return e; }, // :: () → Object // Turn ordered map into a plain object. toObject: function() { var n = {}; return this.forEach(function(e, t) { n[e] = t; }), n; }, // :: number // The amount of keys in this map. get size() { return this.content.length >> 1; } }; dt.from = function(n) { if (n instanceof dt) return n; var e = []; if (n) for (var t in n) e.push(t, n[t]); return new dt(e); }; function bD(n, e, t) { for (let r = 0; ; r++) { if (r == n.childCount || r == e.childCount) return n.childCount == e.childCount ? null : t; let i = n.child(r), o = e.child(r); if (i == o) { t += i.nodeSize; continue; } if (!i.sameMarkup(o)) return t; if (i.isText && i.text != o.text) { for (let a = 0; i.text[a] == o.text[a]; a++) t++; return t; } if (i.content.size || o.content.size) { let a = bD(i.content, o.content, t + 1); if (a != null) return a; } t += i.nodeSize; } } function vD(n, e, t, r) { for (let i = n.childCount, o = e.childCount; ; ) { if (i == 0 || o == 0) return i == o ? null : { a: t, b: r }; let a = n.child(--i), s = e.child(--o), c = a.nodeSize; if (a == s) { t -= c, r -= c; continue; } if (!a.sameMarkup(s)) return { a: t, b: r }; if (a.isText && a.text != s.text) { let l = 0, u = Math.min(a.text.length, s.text.length); for (; l < u && a.text[a.text.length - l - 1] == s.text[s.text.length - l - 1]; ) l++, t--, r--; return { a: t, b: r }; } if (a.content.size || s.content.size) { let l = vD(a.content, s.content, t - 1, r - 1); if (l) return l; } t -= c, r -= c; } } class ge { /** @internal */ constructor(e, t) { if (this.content = e, this.size = t || 0, t == null) for (let r = 0; r < e.length; r++) this.size += e[r].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, r, i = 0, o) { for (let a = 0, s = 0; s < t; a++) { let c = this.content[a], l = s + c.nodeSize; if (l > e && r(c, i + s, o || null, a) !== !1 && c.content.size) { let u = s + 1; c.nodesBetween(Math.max(0, e - u), Math.min(c.content.size, t - u), r, i + u); } s = l; } } /** 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, r, i) { let o = "", a = !0; return this.nodesBetween(e, t, (s, c) => { let l = s.isText ? s.text.slice(Math.max(e, c) - c, t - c) : s.isLeaf ? i ? typeof i == "function" ? i(s) : i : s.type.spec.leafText ? s.type.spec.leafText(s) : "" : ""; s.isBlock && (s.isLeaf && l || s.isTextblock) && r && (a ? a = !1 : o += r), o += l; }, 0), o; } /** 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, r = e.firstChild, i = this.content.slice(), o = 0; for (t.isText && t.sameMarkup(r) && (i[i.length - 1] = t.withText(t.text + r.text), o = 1); o < e.content.length; o++) i.push(e.content[o]); return new ge(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 r = [], i = 0; if (t > e) for (let o = 0, a = 0; a < t; o++) { let s = this.content[o], c = a + s.nodeSize; c > e && ((a < e || c > t) && (s.isText ? s = s.cut(Math.max(0, e - a), Math.min(s.text.length, t - a)) : s = s.cut(Math.max(0, e - a - 1), Math.min(s.content.size, t - a - 1))), r.push(s), i += s.nodeSize), a = c; } return new ge(r, i); } /** @internal */ cutByIndex(e, t) { return e == t ? ge.empty : e == 0 && t == this.content.length ? this : new ge(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 r = this.content[e]; if (r == t) return this; let i = this.content.slice(), o = this.size + t.nodeSize - r.nodeSize; return i[e] = t, new ge(i, o); } /** Create a new fragment by prepending the given node to this fragment. */ addToStart(e) { return new ge([e].concat(this.content), this.size + e.nodeSize); } /** Create a new fragment by appending the given node to this fragment. */ addToEnd(e) { return new ge(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, r = 0; t < this.content.length; t++) { let i = this.content[t]; e(i, r, t), r += 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 bD(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, r = e.size) { return vD(this, e, t, r); } /** 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 Ma(0, e); if (e == this.size) return Ma(this.content.length, e); if (e > this.size || e < 0) throw new RangeError(`Position ${e} outside of fragment (${this})`); for (let r = 0, i = 0; ; r++) { let o = this.child(r), a = i + o.nodeSize; if (a >= e) return a == e || t > 0 ? Ma(r + 1, a) : Ma(r, i); i = a; } } /** 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 ge.empty; if (!Array.isArray(t)) throw new RangeError("Invalid input for Fragment.fromJSON"); return new ge(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 ge.empty; let t, r = 0; for (let i = 0; i < e.length; i++) { let o = e[i]; r += o.nodeSize, i && o.isText && e[i - 1].sameMarkup(o) ? (t || (t = e.slice(0, i)), t[t.length - 1] = o.withText(t[t.length - 1].text + o.text)) : t && t.push(o); } return new ge(t || e, r); } /** 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 ge.empty; if (e instanceof ge) return e; if (Array.isArray(e)) return this.fromArray(e); if (e.attrs) return new ge([e], e.nodeSize); throw new RangeError("Can not convert " + e + " to a Fragment" + (e.nodesBetween ? " (looks like multiple versions of prosemirror-model were loaded)" : "")); } } ge.empty = new ge([], 0); const jl = { index: 0, offset: 0 }; function Ma(n, e) { return jl.index = n, jl.offset = e, jl; } function Qs(n, e) { if (n === e) return !0; if (!(n && typeof n == "object") || !(e && typeof e == "object")) return !1; let t = Array.isArray(n); if (Array.isArray(e) != t) return !1; if (t) { if (n.length != e.length) return !1; for (let r = 0; r < n.length; r++) if (!Qs(n[r], e[r])) return !1; } else { for (let r in n) if (!(r in e) || !Qs(n[r], e[r])) return !1; for (let r in e) if (!(r in n)) return !1; } return !0; } class qe { /** @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, r = !1; for (let i = 0; i < e.length; i++) { let o = e[i]; if (this.eq(o)) return e; if (this.type.excludes(o.type)) t || (t = e.slice(0, i)); else { if (o.type.excludes(this.type)) return e; !r && o.type.rank > this.type.rank && (t || (t = e.slice(0, i)), t.push(this), r = !0), t && t.push(o); } } return t || (t = e.slice()), r || 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 && Qs(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 r = e.marks[t.type]; if (!r) throw new RangeError(`There is no mark type ${t.type} in this schema`); let i = r.create(t.attrs); return r.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 r = 0; r < e.length; r++) if (!e[r].eq(t[r])) 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 qe.none; if (e instanceof qe) return [e]; let t = e.slice(); return t.sort((r, i) => r.type.rank - i.type.rank), t; } } qe.none = []; class ec extends Error { } class _e { /** 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, r) { this.content = e, this.openStart = t, this.openEnd = r; } /** 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 r = xD(this.content, e + this.openStart, t); return r && new _e(r, this.openStart, this.openEnd); } /** @internal */ removeBetween(e, t) { return new _e(DD(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 _e.empty; let r = t.openStart || 0, i = t.openEnd || 0; if (typeof r != "number" || typeof i != "number") throw new RangeError("Invalid input for Slice.fromJSON"); return new _e(ge.fromJSON(e, t.content), r, 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 r = 0, i = 0; for (let o = e.firstChild; o && !o.isLeaf && (t || !o.type.spec.isolating); o = o.firstChild) r++; for (let o = e.lastChild; o && !o.isLeaf && (t || !o.type.spec.isolating); o = o.lastChild) i++; return new _e(e, r, i); } } _e.empty = new _e(ge.empty, 0, 0); function DD(n, e, t) { let { index: r, offset: i } = n.findIndex(e), o = n.maybeChild(r), { index: a, offset: s } = n.findIndex(t); if (i == e || o.isText) { if (s != t && !n.child(a).isText) throw new RangeError("Removing non-flat range"); return n.cut(0, e).append(n.cut(t)); } if (r != a) throw new RangeError("Removing non-flat range"); return n.replaceChild(r, o.copy(DD(o.content, e - i - 1, t - i - 1))); } function xD(n, e, t, r) { let { index: i, offset: o } = n.findIndex(e), a = n.maybeChild(i); if (o == e || a.isText) return n.cut(0, e).append(t).append(n.cut(e)); let s = xD(a.content, e - o - 1, t); return s && n.replaceChild(i, a.copy(s)); } function fC(n, e, t) { if (t.openStart > n.depth) throw new ec("Inserted content deeper than insertion position"); if (n.depth - t.openStart != e.depth - t.openEnd) throw new ec("Inconsistent open depths"); return wD(n, e, t, 0); } function wD(n, e, t, r) { let i = n.index(r), o = n.node(r); if (i == e.index(r) && r < n.depth - t.openStart) { let a = wD(n, e, t, r + 1); return o.copy(o.content.replaceChild(i, a)); } else if (t.content.size) if (!t.openStart && !t.openEnd && n.depth == r && e.depth == r) { let a = n.parent, s = a.content; return ni(a, s.cut(0, n.parentOffset).append(t.content).append(s.cut(e.parentOffset))); } else { let { start: a, end: s } = dC(t, n); return ni(o, TD(n, a, s, e, r)); } else return ni(o, tc(n, e, r)); } function _D(n, e) { if (!e.type.compatibleContent(n.type)) throw new ec("Cannot join " + e.type.name + " onto " + n.type.name); } function kh(n, e, t) { let r = n.node(t); return _D(r, e.node(t)), r; } function ti(n, e) { let t = e.length - 1; t >= 0 && n.isText && n.sameMarkup(e[t]) ? e[t] = n.withText(e[t].text + n.text) : e.push(n); } function Mo(n, e, t, r) { let i = (e || n).node(t), o = 0, a = e ? e.index(t) : i.childCount; n && (o = n.index(t), n.depth > t ? o++ : n.textOffset && (ti(n.nodeAfter, r), o++)); for (let s = o; s < a; s++) ti(i.child(s), r); e && e.depth == t && e.textOffset && ti(e.nodeBefore, r); } function ni(n, e) { return n.type.checkContent(e), n.copy(e); } function TD(n, e, t, r, i) { let o = n.depth > i && kh(n, e, i + 1), a = r.depth > i && kh(t, r, i + 1), s = []; return Mo(null, n, i, s), o && a && e.index(i) == t.index(i) ? (_D(o, a), ti(ni(o, TD(n, e, t, r, i + 1)), s)) : (o && ti(ni(o, tc(n, e, i + 1)), s), Mo(e, t, i, s), a && ti(ni(a, tc(t, r, i + 1)), s)), Mo(r, null, i, s), new ge(s); } function tc(n, e, t) { let r = []; if (Mo(null, n, t, r), n.depth > t) { let i = kh(n, e, t + 1); ti(ni(i, tc(n, e, t + 1)), r); } return Mo(e, null, t, r), new ge(r); } function dC(n, e) { let t = e.depth - n.openStart, i = e.node(t).copy(n.content); for (let o = t - 1; o >= 0; o--) i = e.node(o).copy(ge.from(i)); return { start: i.resolveNoCache(n.openStart + t), end: i.resolveNoCache(i.content.size - n.openEnd - t) }; } class Jo { /** @internal */ constructor(e, t, r) { this.pos = e, this.path = t, this.parentOffset = r, 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 r = this.pos - this.path[this.path.length - 1], i = e.child(t); return r ? e.child(t).cut(r) : 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 r = this.path[t * 3], i = t == 0 ? 0 : this.path[t * 3 - 1] + 1; for (let o = 0; o < e; o++) i += r.child(o).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 qe.none; if (this.textOffset) return e.child(t).marks; let r = e.maybeChild(t - 1), i = e.maybeChild(t); if (!r) { let s = r; r = i, i = s; } let o = r.marks; for (var a = 0; a < o.length; a++) o[a].type.spec.inclusive === !1 && (!i || !o[a].isInSet(i.marks)) && (o = o[a--].removeFromSet(o)); return o; } /** 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 r = t.marks, i = e.parent.maybeChild(e.index()); for (var o = 0; o < r.length; o++) r[o].type.spec.inclusive === !1 && (!i || !r[o].isInSet(i.marks)) && (r = r[o--].removeFromSet(r)); return r; } /** 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 r = this.depth - (this.parent.inlineContent || this.pos == e.pos ? 1 : 0); r >= 0; r--) if (e.pos <= this.end(r) && (!t || t(this.node(r)))) return new nc(this, e, r); 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 r = [], i = 0, o = t; for (let a = e; ; ) { let { index: s, offset: c } = a.content.findIndex(o), l = o - c; if (r.push(a, s, i + c), !l || (a = a.child(s), a.isText)) break; o = l - 1, i += c + 1; } return new Jo(t, r, o); } /** @internal */ static resolveCached(e, t) { let r = Im.get(e); if (r) for (let o = 0; o < r.elts.length; o++) { let a = r.elts[o]; if (a.pos == t) return a; } else Im.set(e, r = new hC()); let i = r.elts[r.i] = Jo.resolve(e, t); return r.i = (r.i + 1) % pC, i; } } class hC { constructor() { this.elts = [], this.i = 0; } } const pC = 12, Im = /* @__PURE__ */ new WeakMap(); class nc { /** 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, r) { this.$from = e, this.$to = t, this.depth = r; } /** 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 gC = /* @__PURE__ */ Object.create(null); class _n { /** @internal */ constructor(e, t, r, i = qe.none) { this.type = e, this.attrs = t, this.marks = i, this.content = r || ge.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, r, i = 0) { this.content.nodesBetween(e, t, r, 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, r, i) { return this.content.textBetween(e, t, r, 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, r) { return this.type == e && Qs(this.attrs, t || e.defaultAttrs || gC) && qe.sameSet(this.marks, r || qe.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 _n(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 _n(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, r = !1) { if (e == t) return _e.empty; let i = this.resolve(e), o = this.resolve(t), a = r ? 0 : i.sharedDepth(t), s = i.start(a), l = i.node(a).content.cut(i.pos - s, o.pos - s); return new _e(l, i.depth - a, o.depth - a); } /** 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, r) { return fC(this.resolve(e), this.resolve(t), r); } /** Find the node directly after the given position. */ nodeAt(e) { for (let t = this; ; ) { let { index: r, offset: i } = t.content.findIndex(e); if (t = t.maybeChild(r), !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: r } = this.content.findIndex(e); return { node: this.content.maybeChild(t), index: t, offset: r }; } /** 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: r } = this.content.findIndex(e); if (r < e) return { node: this.content.child(t), index: t, offset: r }; let i = this.content.child(t - 1); return { node: i, index: t - 1, offset: r - 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 Jo.resolveCached(this, e); } /** @internal */ resolveNoCache(e) { return Jo.resolve(this, e); } /** Test whether a given mark or mark type occurs in this document between the two given positions. */ rangeHasMark(e, t, r) { let i = !1; return t > e && this.nodesBetween(e, t, (o) => (r.isInSet(o.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() + ")"), ED(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, r = ge.empty, i = 0, o = r.childCount) { let a = this.contentMatchAt(e).matchFragment(r, i, o), s = a && a.matchFragment(this.content, t); if (!s || !s.validEnd) return !1; for (let c = i; c < o; c++) if (!this.type.allowsMarks(r.child(c).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, r, i) { if (i && !this.type.allowsMarks(i)) return !1; let o = this.contentMatchAt(e).matchType(r), a = o && o.matchFragment(this.content, t); return a ? a.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 = qe.none; for (let t = 0; t < this.marks.length; t++) { let r = this.marks[t]; r.type.checkAttrs(r.attrs), e = r.addToSet(e); } if (!qe.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 r; if (t.marks) { if (!Array.isArray(t.marks)) throw new RangeError("Invalid mark data for Node.fromJSON"); r = 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, r); } let i = ge.fromJSON(e, t.content), o = e.nodeType(t.type).create(t.attrs, i, r); return o.type.checkAttrs(o.attrs), o; } } _n.prototype.text = void 0; class rc extends _n { /** @internal */ constructor(e, t, r, i) { if (super(e, t, null, i), !r) throw new RangeError("Empty text nodes are not allowed"); this.text = r; } toString() { return this.type.spec.toDebugString ? this.type.spec.toDebugString(this) : ED(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 rc(this.type, this.attrs, this.text, e); } withText(e) { return e == this.text ? this : new rc(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 ED(n, e) { for (let t = n.length - 1; t >= 0; t--) e = n[t].type.name + "(" + e + ")"; return e; } class si { /** @internal */ constructor(e) { this.validEnd = e, this.next = [], this.wrapCache = []; } /** @internal */ static parse(e, t) { let r = new mC(e, t); if (r.next == null) return si.empty; let i = CD(r); r.next && r.err("Unexpected trailing text"); let o = _C(wC(i)); return TC(o, r), o; } /** 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, r = e.childCount) { let i = this; for (let o = t; i && o < r; o++) i = i.matchType(e.child(o).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 r = 0; r < e.next.length; r++) if (this.next[t].type == e.next[r].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, r = 0) { let i = [this]; function o(a, s) { let c = a.matchFragment(e, r); if (c && (!t || c.validEnd)) return ge.from(s.map((l) => l.createAndFill())); for (let l = 0; l < a.next.length; l++) { let { type: u, next: f } = a.next[l]; if (!(u.isText || u.hasRequiredAttrs()) && i.indexOf(f) == -1) { i.push(f); let d = o(f, s.concat(u)); if (d) return d; } } return null; } return o(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 r = 0; r < this.wrapCache.length; r += 2) if (this.wrapCache[r] == e) return this.wrapCache[r + 1]; let t = this.computeWrapping(e); return this.wrapCache.push(e, t), t; } /** @internal */ computeWrapping(e) { let t = /* @__PURE__ */ Object.create(null), r = [{ match: this, type: null, via: null }]; for (; r.length; ) { let i = r.shift(), o = i.match; if (o.matchType(e)) { let a = []; for (let s = i; s.type; s = s.via) a.push(s.type); return a.reverse(); } for (let a = 0; a < o.next.length; a++) { let { type: s, next: c } = o.next[a]; !s.isLeaf && !s.hasRequiredAttrs() && !(s.name in t) && (!i.type || c.validEnd) && (r.push({ match: s.contentMatch, type: s, via: i }), t[s.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(r) { e.push(r); for (let i = 0; i < r.next.length; i++) e.indexOf(r.next[i].next) == -1 && t(r.next[i].next); } return t(this), e.map((r, i) => { let o = i + (r.validEnd ? "*" : " ") + " "; for (let a = 0; a < r.next.length; a++) o += (a ? ", " : "") + r.next[a].type.name + "->" + e.indexOf(r.next[a].next); return o; }).join(` `); } } si.empty = new si(!0); class mC { 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 CD(n) { let e = []; do e.push(yC(n)); while (n.eat("|")); return e.length == 1 ? e[0] : { type: "choice", exprs: e }; } function yC(n) { let e = []; do e.push(bC(n)); while (n.next && n.next != ")" && n.next != "|"); return e.length == 1 ? e[0] : { type: "seq", exprs: e }; } function bC(n) { let e = xC(n); for (; ; ) if (n.eat("+")) e = { type: "plus", expr: e }; else if (n.eat("*")) e = { type: "star", expr: e }; else if (n.eat("?")) e = { type: "opt", expr: e }; else if (n.eat("{")) e = vC(n, e); else break; return e; } function Bm(n) { /\D/.test(n.next) && n.err("Expected number, got '" + n.next + "'"); let e = Number(n.next); return n.pos++, e; } function vC(n, e) { let t = Bm(n), r = t; return n.eat(",") && (n.next != "}" ? r = Bm(n) : r = -1), n.eat("}") || n.err("Unclosed braced range"), { type: "range", min: t, max: r, expr: e }; } function DC(n, e) { let t = n.nodeTypes, r = t[e]; if (r) return [r]; let i = []; for (let o in t) { let a = t[o]; a.isInGroup(e) && i.push(a); } return i.length == 0 && n.err("No node type or group '" + e + "' found"), i; } function xC(n) { if (n.eat("(")) { let e = CD(n); return n.eat(")") || n.err("Missing closing paren"), e; } else if (/\W/.test(n.next)) n.err("Unexpected token '" + n.next + "'"); else { let e = DC(n, n.next).map((t) => (n.inline == null ? n.inline = t.isInline : n.inline != t.isInline && n.err("Mixing inline and block content"), { type: "name", value: t })); return n.pos++, e.length == 1 ? e[0] : { type: "choice", exprs: e }; } } function wC(n) { let e = [[]]; return i(o(n, 0), t()), e; function t() { return e.push([]) - 1; } function r(a, s, c) { let l = { term: c, to: s }; return e[a].push(l), l; } function i(a, s) { a.forEach((c) => c.to = s); } function o(a, s) { if (a.type == "choice") return a.exprs.reduce((c, l) => c.concat(o(l, s)), []); if (a.type == "seq") for (let c = 0; ; c++) { let l = o(a.exprs[c], s); if (c == a.exprs.length - 1) return l; i(l, s = t()); } else if (a.type == "star") { let c = t(); return r(s, c), i(o(a.expr, c), c), [r(c)]; } else if (a.type == "plus") { let c = t(); return i(o(a.expr, s), c), i(o(a.expr, c), c), [r(c)]; } else {