advanced-editor-react
Version:
A modern WYSIWYG rich text editor based on tiptap and shadcn ui for React
1,685 lines (1,684 loc) • 727 kB
JavaScript
var qb = Object.defineProperty;
var Yb = (n, e, t) => e in n ? qb(n, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : n[e] = t;
var Ai = (n, e, t) => Yb(n, typeof e != "symbol" ? e + "" : e, t);
import Xb from "react";
import Qb from "react-dom";
import Zb from "tippy.js";
function ae(n) {
this.content = n;
}
ae.prototype = {
constructor: ae,
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 ae(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 ae(t);
},
// :: (string, any) → OrderedMap
// Add a new key to the start of the map.
addToStart: function(n, e) {
return new ae([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 ae(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 ae(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 = ae.from(n), n.size ? new ae(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 = ae.from(n), n.size ? new ae(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 = ae.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;
}
};
ae.from = function(n) {
if (n instanceof ae) return n;
var e = [];
if (n) for (var t in n) e.push(t, n[t]);
return new ae(e);
};
function kp(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 s = 0; i.text[s] == o.text[s]; s++)
t++;
return t;
}
if (i.content.size || o.content.size) {
let s = kp(i.content, o.content, t + 1);
if (s != null)
return s;
}
t += i.nodeSize;
}
}
function Ep(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 s = n.child(--i), l = e.child(--o), a = s.nodeSize;
if (s == l) {
t -= a, r -= a;
continue;
}
if (!s.sameMarkup(l))
return { a: t, b: r };
if (s.isText && s.text != l.text) {
let c = 0, f = Math.min(s.text.length, l.text.length);
for (; c < f && s.text[s.text.length - c - 1] == l.text[l.text.length - c - 1]; )
c++, t--, r--;
return { a: t, b: r };
}
if (s.content.size || l.content.size) {
let c = Ep(s.content, l.content, t - 1, r - 1);
if (c)
return c;
}
t -= a, r -= a;
}
}
class b {
/**
@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 s = 0, l = 0; l < t; s++) {
let a = this.content[s], c = l + a.nodeSize;
if (c > e && r(a, i + l, o || null, s) !== !1 && a.content.size) {
let f = l + 1;
a.nodesBetween(Math.max(0, e - f), Math.min(a.content.size, t - f), r, 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, r, i) {
let o = "", s = !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) && r && (s ? s = !1 : o += r), o += c;
}, 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 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 r = [], i = 0;
if (t > e)
for (let o = 0, s = 0; s < t; o++) {
let l = this.content[o], a = s + l.nodeSize;
a > e && ((s < e || a > t) && (l.isText ? l = l.cut(Math.max(0, e - s), Math.min(l.text.length, t - s)) : l = l.cut(Math.max(0, e - s - 1), Math.min(l.content.size, t - s - 1))), r.push(l), i += l.nodeSize), s = a;
}
return new b(r, 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 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 b(i, o);
}
/**
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, 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 kp(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 Ep(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 Ti(0, e);
if (e == this.size)
return Ti(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), s = i + o.nodeSize;
if (s >= e)
return s == e || t > 0 ? Ti(r + 1, s) : Ti(r, i);
i = s;
}
}
/**
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, 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 b(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 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 Js = { index: 0, offset: 0 };
function Ti(n, e) {
return Js.index = n, Js.offset = e, Js;
}
function po(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 (!po(n[r], e[r]))
return !1;
} else {
for (let r in n)
if (!(r in e) || !po(n[r], e[r]))
return !1;
for (let r in e)
if (!(r in n))
return !1;
}
return !0;
}
class F {
/**
@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 && po(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 F.none;
if (e instanceof F)
return [e];
let t = e.slice();
return t.sort((r, i) => r.type.rank - i.type.rank), t;
}
}
F.none = [];
class go extends Error {
}
class C {
/**
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 = Ap(this.content, e + this.openStart, t);
return r && new C(r, this.openStart, this.openEnd);
}
/**
@internal
*/
removeBetween(e, t) {
return new C(Op(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 C.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 C(b.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 C(e, r, i);
}
}
C.empty = new C(b.empty, 0, 0);
function Op(n, e, t) {
let { index: r, offset: i } = n.findIndex(e), o = n.maybeChild(r), { index: s, offset: l } = n.findIndex(t);
if (i == e || o.isText) {
if (l != t && !n.child(s).isText)
throw new RangeError("Removing non-flat range");
return n.cut(0, e).append(n.cut(t));
}
if (r != s)
throw new RangeError("Removing non-flat range");
return n.replaceChild(r, o.copy(Op(o.content, e - i - 1, t - i - 1)));
}
function Ap(n, e, t, r) {
let { index: i, offset: o } = n.findIndex(e), s = n.maybeChild(i);
if (o == e || s.isText)
return n.cut(0, e).append(t).append(n.cut(e));
let l = Ap(s.content, e - o - 1, t);
return l && n.replaceChild(i, s.copy(l));
}
function e0(n, e, t) {
if (t.openStart > n.depth)
throw new go("Inserted content deeper than insertion position");
if (n.depth - t.openStart != e.depth - t.openEnd)
throw new go("Inconsistent open depths");
return Tp(n, e, t, 0);
}
function Tp(n, e, t, r) {
let i = n.index(r), o = n.node(r);
if (i == e.index(r) && r < n.depth - t.openStart) {
let s = Tp(n, e, t, r + 1);
return o.copy(o.content.replaceChild(i, s));
} else if (t.content.size)
if (!t.openStart && !t.openEnd && n.depth == r && e.depth == r) {
let s = n.parent, l = s.content;
return un(s, l.cut(0, n.parentOffset).append(t.content).append(l.cut(e.parentOffset)));
} else {
let { start: s, end: l } = t0(t, n);
return un(o, Np(n, s, l, e, r));
}
else return un(o, mo(n, e, r));
}
function Mp(n, e) {
if (!e.type.compatibleContent(n.type))
throw new go("Cannot join " + e.type.name + " onto " + n.type.name);
}
function ra(n, e, t) {
let r = n.node(t);
return Mp(r, e.node(t)), r;
}
function fn(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 Tr(n, e, t, r) {
let i = (e || n).node(t), o = 0, s = e ? e.index(t) : i.childCount;
n && (o = n.index(t), n.depth > t ? o++ : n.textOffset && (fn(n.nodeAfter, r), o++));
for (let l = o; l < s; l++)
fn(i.child(l), r);
e && e.depth == t && e.textOffset && fn(e.nodeBefore, r);
}
function un(n, e) {
return n.type.checkContent(e), n.copy(e);
}
function Np(n, e, t, r, i) {
let o = n.depth > i && ra(n, e, i + 1), s = r.depth > i && ra(t, r, i + 1), l = [];
return Tr(null, n, i, l), o && s && e.index(i) == t.index(i) ? (Mp(o, s), fn(un(o, Np(n, e, t, r, i + 1)), l)) : (o && fn(un(o, mo(n, e, i + 1)), l), Tr(e, t, i, l), s && fn(un(s, mo(t, r, i + 1)), l)), Tr(r, null, i, l), new b(l);
}
function mo(n, e, t) {
let r = [];
if (Tr(null, n, t, r), n.depth > t) {
let i = ra(n, e, t + 1);
fn(un(i, mo(n, e, t + 1)), r);
}
return Tr(e, null, t, r), new b(r);
}
function t0(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(b.from(i));
return {
start: i.resolveNoCache(n.openStart + t),
end: i.resolveNoCache(i.content.size - n.openEnd - t)
};
}
class Hr {
/**
@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 F.none;
if (this.textOffset)
return e.child(t).marks;
let r = e.maybeChild(t - 1), i = e.maybeChild(t);
if (!r) {
let l = r;
r = i, i = l;
}
let o = r.marks;
for (var s = 0; s < o.length; s++)
o[s].type.spec.inclusive === !1 && (!i || !o[s].isInSet(i.marks)) && (o = o[s--].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 Jr(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 s = e; ; ) {
let { index: l, offset: a } = s.content.findIndex(o), c = o - a;
if (r.push(s, l, i + a), !c || (s = s.child(l), s.isText))
break;
o = c - 1, i += a + 1;
}
return new Hr(t, r, o);
}
/**
@internal
*/
static resolveCached(e, t) {
let r = Mf.get(e);
if (r)
for (let o = 0; o < r.elts.length; o++) {
let s = r.elts[o];
if (s.pos == t)
return s;
}
else
Mf.set(e, r = new n0());
let i = r.elts[r.i] = Hr.resolve(e, t);
return r.i = (r.i + 1) % r0, i;
}
}
class n0 {
constructor() {
this.elts = [], this.i = 0;
}
}
const r0 = 12, Mf = /* @__PURE__ */ new WeakMap();
class Jr {
/**
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 i0 = /* @__PURE__ */ Object.create(null);
class Xe {
/**
@internal
*/
constructor(e, t, r, i = F.none) {
this.type = e, this.attrs = t, this.marks = i, this.content = r || 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, 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 && po(this.attrs, t || e.defaultAttrs || i0) && F.sameSet(this.marks, r || F.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 Xe(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 Xe(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 C.empty;
let i = this.resolve(e), o = this.resolve(t), s = r ? 0 : i.sharedDepth(t), l = i.start(s), c = i.node(s).content.cut(i.pos - l, o.pos - l);
return new C(c, i.depth - s, o.depth - s);
}
/**
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 e0(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 Hr.resolveCached(this, e);
}
/**
@internal
*/
resolveNoCache(e) {
return Hr.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() + ")"), Ip(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 = b.empty, i = 0, o = r.childCount) {
let s = this.contentMatchAt(e).matchFragment(r, i, o), l = s && s.matchFragment(this.content, t);
if (!l || !l.validEnd)
return !1;
for (let a = i; a < o; a++)
if (!this.type.allowsMarks(r.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, r, i) {
if (i && !this.type.allowsMarks(i))
return !1;
let o = this.contentMatchAt(e).matchType(r), s = o && o.matchFragment(this.content, t);
return s ? s.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 = F.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 (!F.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 = b.fromJSON(e, t.content), o = e.nodeType(t.type).create(t.attrs, i, r);
return o.type.checkAttrs(o.attrs), o;
}
}
Xe.prototype.text = void 0;
class yo extends Xe {
/**
@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) : Ip(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 yo(this.type, this.attrs, this.text, e);
}
withText(e) {
return e == this.text ? this : new yo(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 Ip(n, e) {
for (let t = n.length - 1; t >= 0; t--)
e = n[t].type.name + "(" + e + ")";
return e;
}
class mn {
/**
@internal
*/
constructor(e) {
this.validEnd = e, this.next = [], this.wrapCache = [];
}
/**
@internal
*/
static parse(e, t) {
let r = new o0(e, t);
if (r.next == null)
return mn.empty;
let i = Dp(r);
r.next && r.err("Unexpected trailing text");
let o = h0(u0(i));
return d0(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(s, l) {
let a = s.matchFragment(e, r);
if (a && (!t || a.validEnd))
return b.from(l.map((c) => c.createAndFill()));
for (let c = 0; c < s.next.length; c++) {
let { type: f, next: u } = s.next[c];
if (!(f.isText || f.hasRequiredAttrs()) && i.indexOf(u) == -1) {
i.push(u);
let h = o(u, l.concat(f));
if (h)
return h;
}
}
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 s = [];
for (let l = i; l.type; l = l.via)
s.push(l.type);
return s.reverse();
}
for (let s = 0; s < o.next.length; s++) {
let { type: l, next: a } = o.next[s];
!l.isLeaf && !l.hasRequiredAttrs() && !(l.name in t) && (!i.type || a.validEnd) && (r.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(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 s = 0; s < r.next.length; s++)
o += (s ? ", " : "") + r.next[s].type.name + "->" + e.indexOf(r.next[s].next);
return o;
}).join(`
`);
}
}
mn.empty = new mn(!0);
class o0 {
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 Dp(n) {
let e = [];
do
e.push(s0(n));
while (n.eat("|"));
return e.length == 1 ? e[0] : { type: "choice", exprs: e };
}
function s0(n) {
let e = [];
do
e.push(l0(n));
while (n.next && n.next != ")" && n.next != "|");
return e.length == 1 ? e[0] : { type: "seq", exprs: e };
}
function l0(n) {
let e = f0(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 = a0(n, e);
else
break;
return e;
}
function Nf(n) {
/\D/.test(n.next) && n.err("Expected number, got '" + n.next + "'");
let e = Number(n.next);
return n.pos++, e;
}
function a0(n, e) {
let t = Nf(n), r = t;
return n.eat(",") && (n.next != "}" ? r = Nf(n) : r = -1), n.eat("}") || n.err("Unclosed braced range"), { type: "range", min: t, max: r, expr: e };
}
function c0(n, e) {
let t = n.nodeTypes, r = t[e];
if (r)
return [r];
let i = [];
for (let o in t) {
let s = t[o];
s.isInGroup(e) && i.push(s);
}
return i.length == 0 && n.err("No node type or group '" + e + "' found"), i;
}
function f0(n) {
if (n.eat("(")) {
let e = Dp(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 = c0(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 u0(n) {
let e = [[]];
return i(o(n, 0), t()), e;
function t() {
return e.push([]) - 1;
}
function r(s, l, a) {
let c = { term: a, to: l };
return e[s].push(c), c;
}
function i(s, l) {
s.forEach((a) => a.to = l);
}
function o(s, l) {
if (s.type == "choice")
return s.exprs.reduce((a, c) => a.concat(o(c, l)), []);
if (s.type == "seq")
for (let a = 0; ; a++) {
let c = o(s.exprs[a], l);
if (a == s.exprs.length - 1)
return c;
i(c, l = t());
}
else if (s.type == "star") {
let a = t();
return r(l, a), i(o(s.expr, a), a), [r(a)];
} else if (s.type == "plus") {
let a = t();
return i(o(s.expr, l), a), i(o(s.expr, a), a), [r(a)];
} else {
if (s.type == "opt")
return [r(l)].concat(o(s.expr, l));
if (s.type == "