reactjs-tiptap-editor
Version:
A modern WYSIWYG rich text editor based on tiptap and shadcn ui for React
992 lines (991 loc) • 32.6 kB
JavaScript
import { Q as N, y as te, F as V, x as G, T as H, v as _, S as A, U as W, V as ne, E as C, W as re, X as ie, Y as oe, q as se } from "./clsx-DaPvp9ji.js";
import { Plugin as I, PluginKey as b } from "@tiptap/pm/state";
import { DecorationSet as O, Decoration as R } from "@tiptap/pm/view";
import { k as le, D as ae, a as de } from "./index-DqruLhsu.js";
import { jsx as T, Fragment as j } from "react/jsx-runtime";
import { u as U, e as X, A as Y, i as ce } from "./index-RcSPeQHn.js";
import "react";
import "./theme.js";
function ue(i = {}) {
return new N({
view(e) {
return new pe(e, i);
}
});
}
class pe {
constructor(e, t) {
var n;
this.editorView = e, this.cursorPos = null, this.element = null, this.timeout = -1, this.width = (n = t.width) !== null && n !== void 0 ? n : 1, this.color = t.color === !1 ? void 0 : t.color || "black", this.class = t.class, this.handlers = ["dragover", "dragend", "drop", "dragleave"].map((r) => {
let o = (s) => {
this[r](s);
};
return e.dom.addEventListener(r, o), { name: r, handler: o };
});
}
destroy() {
this.handlers.forEach(({ name: e, handler: t }) => this.editorView.dom.removeEventListener(e, t));
}
update(e, t) {
this.cursorPos != null && t.doc != e.state.doc && (this.cursorPos > e.state.doc.content.size ? this.setCursor(null) : this.updateOverlay());
}
setCursor(e) {
e != this.cursorPos && (this.cursorPos = e, e == null ? (this.element.parentNode.removeChild(this.element), this.element = null) : this.updateOverlay());
}
updateOverlay() {
let e = this.editorView.state.doc.resolve(this.cursorPos), t = !e.parent.inlineContent, n, r = this.editorView.dom, o = r.getBoundingClientRect(), s = o.width / r.offsetWidth, l = o.height / r.offsetHeight;
if (t) {
let c = e.nodeBefore, p = e.nodeAfter;
if (c || p) {
let h = this.editorView.nodeDOM(this.cursorPos - (c ? c.nodeSize : 0));
if (h) {
let g = h.getBoundingClientRect(), M = c ? g.bottom : g.top;
c && p && (M = (M + this.editorView.nodeDOM(this.cursorPos).getBoundingClientRect().top) / 2);
let P = this.width / 2 * l;
n = { left: g.left, right: g.right, top: M - P, bottom: M + P };
}
}
}
if (!n) {
let c = this.editorView.coordsAtPos(this.cursorPos), p = this.width / 2 * s;
n = { left: c.left - p, right: c.left + p, top: c.top, bottom: c.bottom };
}
let a = this.editorView.dom.offsetParent;
this.element || (this.element = a.appendChild(document.createElement("div")), this.class && (this.element.className = this.class), this.element.style.cssText = "position: absolute; z-index: 50; pointer-events: none;", this.color && (this.element.style.backgroundColor = this.color)), this.element.classList.toggle("prosemirror-dropcursor-block", t), this.element.classList.toggle("prosemirror-dropcursor-inline", !t);
let d, u;
if (!a || a == document.body && getComputedStyle(a).position == "static")
d = -pageXOffset, u = -pageYOffset;
else {
let c = a.getBoundingClientRect(), p = c.width / a.offsetWidth, h = c.height / a.offsetHeight;
d = c.left - a.scrollLeft * p, u = c.top - a.scrollTop * h;
}
this.element.style.left = (n.left - d) / s + "px", this.element.style.top = (n.top - u) / l + "px", this.element.style.width = (n.right - n.left) / s + "px", this.element.style.height = (n.bottom - n.top) / l + "px";
}
scheduleRemoval(e) {
clearTimeout(this.timeout), this.timeout = setTimeout(() => this.setCursor(null), e);
}
dragover(e) {
if (!this.editorView.editable)
return;
let t = this.editorView.posAtCoords({ left: e.clientX, top: e.clientY }), n = t && t.inside >= 0 && this.editorView.state.doc.nodeAt(t.inside), r = n && n.type.spec.disableDropCursor, o = typeof r == "function" ? r(this.editorView, t, e) : r;
if (t && !o) {
let s = t.pos;
if (this.editorView.dragging && this.editorView.dragging.slice) {
let l = te(this.editorView.state.doc, s, this.editorView.dragging.slice);
l != null && (s = l);
}
this.setCursor(s), this.scheduleRemoval(5e3);
}
}
dragend() {
this.scheduleRemoval(20);
}
drop() {
this.scheduleRemoval(20);
}
dragleave(e) {
this.editorView.dom.contains(e.relatedTarget) || this.setCursor(null);
}
}
class f extends A {
/**
Create a gap cursor.
*/
constructor(e) {
super(e, e);
}
map(e, t) {
let n = e.resolve(t.map(this.head));
return f.valid(n) ? new f(n) : A.near(n);
}
content() {
return G.empty;
}
eq(e) {
return e instanceof f && e.head == this.head;
}
toJSON() {
return { type: "gapcursor", pos: this.head };
}
/**
@internal
*/
static fromJSON(e, t) {
if (typeof t.pos != "number")
throw new RangeError("Invalid input for GapCursor.fromJSON");
return new f(e.resolve(t.pos));
}
/**
@internal
*/
getBookmark() {
return new F(this.anchor);
}
/**
@internal
*/
static valid(e) {
let t = e.parent;
if (t.isTextblock || !fe(e) || !he(e))
return !1;
let n = t.type.spec.allowGapCursor;
if (n != null)
return n;
let r = t.contentMatchAt(e.index()).defaultType;
return r && r.isTextblock;
}
/**
@internal
*/
static findGapCursorFrom(e, t, n = !1) {
e: for (; ; ) {
if (!n && f.valid(e))
return e;
let r = e.pos, o = null;
for (let s = e.depth; ; s--) {
let l = e.node(s);
if (t > 0 ? e.indexAfter(s) < l.childCount : e.index(s) > 0) {
o = l.child(t > 0 ? e.indexAfter(s) : e.index(s) - 1);
break;
} else if (s == 0)
return null;
r += t;
let a = e.doc.resolve(r);
if (f.valid(a))
return a;
}
for (; ; ) {
let s = t > 0 ? o.firstChild : o.lastChild;
if (!s) {
if (o.isAtom && !o.isText && !_.isSelectable(o)) {
e = e.doc.resolve(r + o.nodeSize * t), n = !1;
continue e;
}
break;
}
o = s, r += t;
let l = e.doc.resolve(r);
if (f.valid(l))
return l;
}
return null;
}
}
}
f.prototype.visible = !1;
f.findFrom = f.findGapCursorFrom;
A.jsonID("gapcursor", f);
class F {
constructor(e) {
this.pos = e;
}
map(e) {
return new F(e.map(this.pos));
}
resolve(e) {
let t = e.resolve(this.pos);
return f.valid(t) ? new f(t) : A.near(t);
}
}
function J(i) {
return i.isAtom || i.spec.isolating || i.spec.createGapCursor;
}
function fe(i) {
for (let e = i.depth; e >= 0; e--) {
let t = i.index(e), n = i.node(e);
if (t == 0) {
if (n.type.spec.isolating)
return !0;
continue;
}
for (let r = n.child(t - 1); ; r = r.lastChild) {
if (r.childCount == 0 && !r.inlineContent || J(r.type))
return !0;
if (r.inlineContent)
return !1;
}
}
return !0;
}
function he(i) {
for (let e = i.depth; e >= 0; e--) {
let t = i.indexAfter(e), n = i.node(e);
if (t == n.childCount) {
if (n.type.spec.isolating)
return !0;
continue;
}
for (let r = n.child(t); ; r = r.firstChild) {
if (r.childCount == 0 && !r.inlineContent || J(r.type))
return !0;
if (r.inlineContent)
return !1;
}
}
return !0;
}
function me() {
return new N({
props: {
decorations: ye,
createSelectionBetween(i, e, t) {
return e.pos == t.pos && f.valid(t) ? new f(t) : null;
},
handleClick: ve,
handleKeyDown: ge,
handleDOMEvents: { beforeinput: we }
}
});
}
const ge = le({
ArrowLeft: x("horiz", -1),
ArrowRight: x("horiz", 1),
ArrowUp: x("vert", -1),
ArrowDown: x("vert", 1)
});
function x(i, e) {
const t = i == "vert" ? e > 0 ? "down" : "up" : e > 0 ? "right" : "left";
return function(n, r, o) {
let s = n.selection, l = e > 0 ? s.$to : s.$from, a = s.empty;
if (s instanceof H) {
if (!o.endOfTextblock(t) || l.depth == 0)
return !1;
a = !1, l = n.doc.resolve(e > 0 ? l.after() : l.before());
}
let d = f.findGapCursorFrom(l, e, a);
return d ? (r && r(n.tr.setSelection(new f(d))), !0) : !1;
};
}
function ve(i, e, t) {
if (!i || !i.editable)
return !1;
let n = i.state.doc.resolve(e);
if (!f.valid(n))
return !1;
let r = i.posAtCoords({ left: t.clientX, top: t.clientY });
return r && r.inside > -1 && _.isSelectable(i.state.doc.nodeAt(r.inside)) ? !1 : (i.dispatch(i.state.tr.setSelection(new f(n))), !0);
}
function we(i, e) {
if (e.inputType != "insertCompositionText" || !(i.state.selection instanceof f))
return !1;
let { $from: t } = i.state.selection, n = t.parent.contentMatchAt(t.index()).findWrapping(i.state.schema.nodes.text);
if (!n)
return !1;
let r = V.empty;
for (let s = n.length - 1; s >= 0; s--)
r = V.from(n[s].createAndFill(null, r));
let o = i.state.tr.replace(t.pos, t.pos, new G(r, 0, 0));
return o.setSelection(H.near(o.doc.resolve(t.pos + 1))), i.dispatch(o), !1;
}
function ye(i) {
if (!(i.selection instanceof f))
return null;
let e = document.createElement("div");
return e.className = "ProseMirror-gapcursor", ae.create(i.doc, [de.widget(i.selection.head, e, { key: "gapcursor" })]);
}
var D = 200, m = function() {
};
m.prototype.append = function(e) {
return e.length ? (e = m.from(e), !this.length && e || e.length < D && this.leafAppend(e) || this.length < D && e.leafPrepend(this) || this.appendInner(e)) : this;
};
m.prototype.prepend = function(e) {
return e.length ? m.from(e).append(this) : this;
};
m.prototype.appendInner = function(e) {
return new Ce(this, e);
};
m.prototype.slice = function(e, t) {
return e === void 0 && (e = 0), t === void 0 && (t = this.length), e >= t ? m.empty : this.sliceInner(Math.max(0, e), Math.min(this.length, t));
};
m.prototype.get = function(e) {
if (!(e < 0 || e >= this.length))
return this.getInner(e);
};
m.prototype.forEach = function(e, t, n) {
t === void 0 && (t = 0), n === void 0 && (n = this.length), t <= n ? this.forEachInner(e, t, n, 0) : this.forEachInvertedInner(e, t, n, 0);
};
m.prototype.map = function(e, t, n) {
t === void 0 && (t = 0), n === void 0 && (n = this.length);
var r = [];
return this.forEach(function(o, s) {
return r.push(e(o, s));
}, t, n), r;
};
m.from = function(e) {
return e instanceof m ? e : e && e.length ? new Z(e) : m.empty;
};
var Z = /* @__PURE__ */ (function(i) {
function e(n) {
i.call(this), this.values = n;
}
i && (e.__proto__ = i), e.prototype = Object.create(i && i.prototype), e.prototype.constructor = e;
var t = { length: { configurable: !0 }, depth: { configurable: !0 } };
return e.prototype.flatten = function() {
return this.values;
}, e.prototype.sliceInner = function(r, o) {
return r == 0 && o == this.length ? this : new e(this.values.slice(r, o));
}, e.prototype.getInner = function(r) {
return this.values[r];
}, e.prototype.forEachInner = function(r, o, s, l) {
for (var a = o; a < s; a++)
if (r(this.values[a], l + a) === !1)
return !1;
}, e.prototype.forEachInvertedInner = function(r, o, s, l) {
for (var a = o - 1; a >= s; a--)
if (r(this.values[a], l + a) === !1)
return !1;
}, e.prototype.leafAppend = function(r) {
if (this.length + r.length <= D)
return new e(this.values.concat(r.flatten()));
}, e.prototype.leafPrepend = function(r) {
if (this.length + r.length <= D)
return new e(r.flatten().concat(this.values));
}, t.length.get = function() {
return this.values.length;
}, t.depth.get = function() {
return 0;
}, Object.defineProperties(e.prototype, t), e;
})(m);
m.empty = new Z([]);
var Ce = /* @__PURE__ */ (function(i) {
function e(t, n) {
i.call(this), this.left = t, this.right = n, this.length = t.length + n.length, this.depth = Math.max(t.depth, n.depth) + 1;
}
return i && (e.__proto__ = i), e.prototype = Object.create(i && i.prototype), e.prototype.constructor = e, e.prototype.flatten = function() {
return this.left.flatten().concat(this.right.flatten());
}, e.prototype.getInner = function(n) {
return n < this.left.length ? this.left.get(n) : this.right.get(n - this.left.length);
}, e.prototype.forEachInner = function(n, r, o, s) {
var l = this.left.length;
if (r < l && this.left.forEachInner(n, r, Math.min(o, l), s) === !1 || o > l && this.right.forEachInner(n, Math.max(r - l, 0), Math.min(this.length, o) - l, s + l) === !1)
return !1;
}, e.prototype.forEachInvertedInner = function(n, r, o, s) {
var l = this.left.length;
if (r > l && this.right.forEachInvertedInner(n, r - l, Math.max(o, l) - l, s + l) === !1 || o < l && this.left.forEachInvertedInner(n, Math.min(r, l), o, s) === !1)
return !1;
}, e.prototype.sliceInner = function(n, r) {
if (n == 0 && r == this.length)
return this;
var o = this.left.length;
return r <= o ? this.left.slice(n, r) : n >= o ? this.right.slice(n - o, r - o) : this.left.slice(n, o).append(this.right.slice(0, r - o));
}, e.prototype.leafAppend = function(n) {
var r = this.right.leafAppend(n);
if (r)
return new e(this.left, r);
}, e.prototype.leafPrepend = function(n) {
var r = this.left.leafPrepend(n);
if (r)
return new e(r, this.right);
}, e.prototype.appendInner = function(n) {
return this.left.depth >= Math.max(this.right.depth, n.depth) + 1 ? new e(this.left, new e(this.right, n)) : new e(this, n);
}, e;
})(m);
const Ee = 500;
class v {
constructor(e, t) {
this.items = e, this.eventCount = t;
}
// Pop the latest event off the branch's history and apply it
// to a document transform.
popEvent(e, t) {
if (this.eventCount == 0)
return null;
let n = this.items.length;
for (; ; n--)
if (this.items.get(n - 1).selection) {
--n;
break;
}
let r, o;
t && (r = this.remapping(n, this.items.length), o = r.maps.length);
let s = e.tr, l, a, d = [], u = [];
return this.items.forEach((c, p) => {
if (!c.step) {
r || (r = this.remapping(n, p + 1), o = r.maps.length), o--, u.push(c);
return;
}
if (r) {
u.push(new w(c.map));
let h = c.step.map(r.slice(o)), g;
h && s.maybeStep(h).doc && (g = s.mapping.maps[s.mapping.maps.length - 1], d.push(new w(g, void 0, void 0, d.length + u.length))), o--, g && r.appendMap(g, o);
} else
s.maybeStep(c.step);
if (c.selection)
return l = r ? c.selection.map(r.slice(o)) : c.selection, a = new v(this.items.slice(0, n).append(u.reverse().concat(d)), this.eventCount - 1), !1;
}, this.items.length, 0), { remaining: a, transform: s, selection: l };
}
// Create a new branch with the given transform added.
addTransform(e, t, n, r) {
let o = [], s = this.eventCount, l = this.items, a = !r && l.length ? l.get(l.length - 1) : null;
for (let u = 0; u < e.steps.length; u++) {
let c = e.steps[u].invert(e.docs[u]), p = new w(e.mapping.maps[u], c, t), h;
(h = a && a.merge(p)) && (p = h, u ? o.pop() : l = l.slice(0, l.length - 1)), o.push(p), t && (s++, t = void 0), r || (a = p);
}
let d = s - n.depth;
return d > Pe && (l = Me(l, d), s -= d), new v(l.append(o), s);
}
remapping(e, t) {
let n = new ne();
return this.items.forEach((r, o) => {
let s = r.mirrorOffset != null && o - r.mirrorOffset >= e ? n.maps.length - r.mirrorOffset : void 0;
n.appendMap(r.map, s);
}, e, t), n;
}
addMaps(e) {
return this.eventCount == 0 ? this : new v(this.items.append(e.map((t) => new w(t))), this.eventCount);
}
// When the collab module receives remote changes, the history has
// to know about those, so that it can adjust the steps that were
// rebased on top of the remote changes, and include the position
// maps for the remote changes in its array of items.
rebased(e, t) {
if (!this.eventCount)
return this;
let n = [], r = Math.max(0, this.items.length - t), o = e.mapping, s = e.steps.length, l = this.eventCount;
this.items.forEach((p) => {
p.selection && l--;
}, r);
let a = t;
this.items.forEach((p) => {
let h = o.getMirror(--a);
if (h == null)
return;
s = Math.min(s, h);
let g = o.maps[h];
if (p.step) {
let M = e.steps[h].invert(e.docs[h]), P = p.selection && p.selection.map(o.slice(a + 1, h));
P && l++, n.push(new w(g, M, P));
} else
n.push(new w(g));
}, r);
let d = [];
for (let p = t; p < s; p++)
d.push(new w(o.maps[p]));
let u = this.items.slice(0, r).append(d).append(n), c = new v(u, l);
return c.emptyItemCount() > Ee && (c = c.compress(this.items.length - n.length)), c;
}
emptyItemCount() {
let e = 0;
return this.items.forEach((t) => {
t.step || e++;
}), e;
}
// Compressing a branch means rewriting it to push the air (map-only
// items) out. During collaboration, these naturally accumulate
// because each remote change adds one. The `upto` argument is used
// to ensure that only the items below a given level are compressed,
// because `rebased` relies on a clean, untouched set of items in
// order to associate old items with rebased steps.
compress(e = this.items.length) {
let t = this.remapping(0, e), n = t.maps.length, r = [], o = 0;
return this.items.forEach((s, l) => {
if (l >= e)
r.push(s), s.selection && o++;
else if (s.step) {
let a = s.step.map(t.slice(n)), d = a && a.getMap();
if (n--, d && t.appendMap(d, n), a) {
let u = s.selection && s.selection.map(t.slice(n));
u && o++;
let c = new w(d.invert(), a, u), p, h = r.length - 1;
(p = r.length && r[h].merge(c)) ? r[h] = p : r.push(c);
}
} else s.map && n--;
}, this.items.length, 0), new v(m.from(r.reverse()), o);
}
}
v.empty = new v(m.empty, 0);
function Me(i, e) {
let t;
return i.forEach((n, r) => {
if (n.selection && e-- == 0)
return t = r, !1;
}), i.slice(t);
}
class w {
constructor(e, t, n, r) {
this.map = e, this.step = t, this.selection = n, this.mirrorOffset = r;
}
merge(e) {
if (this.step && e.step && !e.selection) {
let t = e.step.merge(this.step);
if (t)
return new w(t.getMap().invert(), t, this.selection);
}
}
}
class y {
constructor(e, t, n, r, o) {
this.done = e, this.undone = t, this.prevRanges = n, this.prevTime = r, this.prevComposition = o;
}
}
const Pe = 20;
function Ie(i, e, t, n) {
let r = t.getMeta(E), o;
if (r)
return r.historyState;
t.getMeta(Se) && (i = new y(i.done, i.undone, null, 0, -1));
let s = t.getMeta("appendedTransaction");
if (t.steps.length == 0)
return i;
if (s && s.getMeta(E))
return s.getMeta(E).redo ? new y(i.done.addTransform(t, void 0, n, S(e)), i.undone, L(t.mapping.maps), i.prevTime, i.prevComposition) : new y(i.done, i.undone.addTransform(t, void 0, n, S(e)), null, i.prevTime, i.prevComposition);
if (t.getMeta("addToHistory") !== !1 && !(s && s.getMeta("addToHistory") === !1)) {
let l = t.getMeta("composition"), a = i.prevTime == 0 || !s && i.prevComposition != l && (i.prevTime < (t.time || 0) - n.newGroupDelay || !be(t, i.prevRanges)), d = s ? k(i.prevRanges, t.mapping) : L(t.mapping.maps);
return new y(i.done.addTransform(t, a ? e.selection.getBookmark() : void 0, n, S(e)), v.empty, d, t.time, l ?? i.prevComposition);
} else return (o = t.getMeta("rebased")) ? new y(i.done.rebased(t, o), i.undone.rebased(t, o), k(i.prevRanges, t.mapping), i.prevTime, i.prevComposition) : new y(i.done.addMaps(t.mapping.maps), i.undone.addMaps(t.mapping.maps), k(i.prevRanges, t.mapping), i.prevTime, i.prevComposition);
}
function be(i, e) {
if (!e)
return !1;
if (!i.docChanged)
return !0;
let t = !1;
return i.mapping.maps[0].forEach((n, r) => {
for (let o = 0; o < e.length; o += 2)
n <= e[o + 1] && r >= e[o] && (t = !0);
}), t;
}
function L(i) {
let e = [];
for (let t = i.length - 1; t >= 0 && e.length == 0; t--)
i[t].forEach((n, r, o, s) => e.push(o, s));
return e;
}
function k(i, e) {
if (!i)
return null;
let t = [];
for (let n = 0; n < i.length; n += 2) {
let r = e.map(i[n], 1), o = e.map(i[n + 1], -1);
r <= o && t.push(r, o);
}
return t;
}
function xe(i, e, t) {
let n = S(e), r = E.get(e).spec.config, o = (t ? i.undone : i.done).popEvent(e, n);
if (!o)
return null;
let s = o.selection.resolve(o.transform.doc), l = (t ? i.done : i.undone).addTransform(o.transform, e.selection.getBookmark(), r, n), a = new y(t ? l : o.remaining, t ? o.remaining : l, null, 0, -1);
return o.transform.setSelection(s).setMeta(E, { redo: t, historyState: a });
}
let z = !1, B = null;
function S(i) {
let e = i.plugins;
if (B != e) {
z = !1, B = e;
for (let t = 0; t < e.length; t++)
if (e[t].spec.historyPreserveItems) {
z = !0;
break;
}
}
return z;
}
const E = new W("history"), Se = new W("closeHistory");
function Ae(i = {}) {
return i = {
depth: i.depth || 100,
newGroupDelay: i.newGroupDelay || 500
}, new N({
key: E,
state: {
init() {
return new y(v.empty, v.empty, null, 0, -1);
},
apply(e, t, n) {
return Ie(t, n, e, i);
}
},
config: i,
props: {
handleDOMEvents: {
beforeinput(e, t) {
let n = t.inputType, r = n == "historyUndo" ? q : n == "historyRedo" ? $ : null;
return !r || !e.editable ? !1 : (t.preventDefault(), r(e.state, e.dispatch));
}
}
}
});
}
function Q(i, e) {
return (t, n) => {
let r = E.getState(t);
if (!r || (i ? r.undone : r.done).eventCount == 0)
return !1;
if (n) {
let o = xe(r, t, i);
o && n(e ? o.scrollIntoView() : o);
}
return !0;
};
}
const q = Q(!1, !0), $ = Q(!0, !0);
C.create({
name: "characterCount",
addOptions() {
return {
limit: null,
mode: "textSize",
textCounter: (i) => i.length,
wordCounter: (i) => i.split(" ").filter((e) => e !== "").length
};
},
addStorage() {
return {
characters: () => 0,
words: () => 0
};
},
onBeforeCreate() {
this.storage.characters = (i) => {
const e = (i == null ? void 0 : i.node) || this.editor.state.doc;
if (((i == null ? void 0 : i.mode) || this.options.mode) === "textSize") {
const n = e.textBetween(0, e.content.size, void 0, " ");
return this.options.textCounter(n);
}
return e.nodeSize;
}, this.storage.words = (i) => {
const e = (i == null ? void 0 : i.node) || this.editor.state.doc, t = e.textBetween(0, e.content.size, " ", " ");
return this.options.wordCounter(t);
};
},
addProseMirrorPlugins() {
let i = !1;
return [
new I({
key: new b("characterCount"),
appendTransaction: (e, t, n) => {
if (i)
return;
const r = this.options.limit;
if (r == null || r === 0) {
i = !0;
return;
}
const o = this.storage.characters({ node: n.doc });
if (o > r) {
const s = o - r, l = 0, a = s;
console.warn(
`[CharacterCount] Initial content exceeded limit of ${r} characters. Content was automatically trimmed.`
);
const d = n.tr.deleteRange(l, a);
return i = !0, d;
}
i = !0;
},
filterTransaction: (e, t) => {
const n = this.options.limit;
if (!e.docChanged || n === 0 || n === null || n === void 0)
return !0;
const r = this.storage.characters({ node: t.doc }), o = this.storage.characters({ node: e.doc });
if (o <= n || r > n && o > n && o <= r)
return !0;
if (r > n && o > n && o > r || !e.getMeta("paste"))
return !1;
const l = e.selection.$head.pos, a = o - n, d = l - a, u = l;
return e.deleteRange(d, u), !(this.storage.characters({ node: e.doc }) > n);
}
})
];
}
});
C.create({
name: "dropCursor",
addOptions() {
return {
color: "currentColor",
width: 1,
class: void 0
};
},
addProseMirrorPlugins() {
return [ue(this.options)];
}
});
C.create({
name: "focus",
addOptions() {
return {
className: "has-focus",
mode: "all"
};
},
addProseMirrorPlugins() {
return [
new I({
key: new b("focus"),
props: {
decorations: ({ doc: i, selection: e }) => {
const { isEditable: t, isFocused: n } = this.editor, { anchor: r } = e, o = [];
if (!t || !n)
return O.create(i, []);
let s = 0;
this.options.mode === "deepest" && i.descendants((a, d) => {
if (a.isText)
return;
if (!(r >= d && r <= d + a.nodeSize - 1))
return !1;
s += 1;
});
let l = 0;
return i.descendants((a, d) => {
if (a.isText || !(r >= d && r <= d + a.nodeSize - 1))
return !1;
if (l += 1, this.options.mode === "deepest" && s - l > 0 || this.options.mode === "shallowest" && l > 1)
return this.options.mode === "deepest";
o.push(
R.node(d, d + a.nodeSize, {
class: this.options.className
})
);
}), O.create(i, o);
}
}
})
];
}
});
C.create({
name: "gapCursor",
addProseMirrorPlugins() {
return [me()];
},
extendNodeSchema(i) {
var e;
const t = {
name: i.name,
options: i.options,
storage: i.storage
};
return {
allowGapCursor: (e = re(ie(i, "allowGapCursor", t))) != null ? e : null
};
}
});
C.create({
name: "placeholder",
addOptions() {
return {
emptyEditorClass: "is-editor-empty",
emptyNodeClass: "is-empty",
placeholder: "Write something …",
showOnlyWhenEditable: !0,
showOnlyCurrent: !0,
includeChildren: !1
};
},
addProseMirrorPlugins() {
return [
new I({
key: new b("placeholder"),
props: {
decorations: ({ doc: i, selection: e }) => {
const t = this.editor.isEditable || !this.options.showOnlyWhenEditable, { anchor: n } = e, r = [];
if (!t)
return null;
const o = this.editor.isEmpty;
return i.descendants((s, l) => {
const a = n >= l && n <= l + s.nodeSize, d = !s.isLeaf && oe(s);
if ((a || !this.options.showOnlyCurrent) && d) {
const u = [this.options.emptyNodeClass];
o && u.push(this.options.emptyEditorClass);
const c = R.node(l, l + s.nodeSize, {
class: u.join(" "),
"data-placeholder": typeof this.options.placeholder == "function" ? this.options.placeholder({
editor: this.editor,
node: s,
pos: l,
hasAnchor: a
}) : this.options.placeholder
});
r.push(c);
}
return this.options.includeChildren;
}), O.create(i, r);
}
}
})
];
}
});
C.create({
name: "selection",
addOptions() {
return {
className: "selection"
};
},
addProseMirrorPlugins() {
const { editor: i, options: e } = this;
return [
new I({
key: new b("selection"),
props: {
decorations(t) {
return t.selection.empty || i.isFocused || !i.isEditable || se(t.selection) || i.view.dragging ? null : O.create(t.doc, [
R.inline(t.selection.from, t.selection.to, {
class: e.className
})
]);
}
}
})
];
}
});
function K({ types: i, node: e }) {
return e && Array.isArray(i) && i.includes(e.type) || (e == null ? void 0 : e.type) === i;
}
C.create({
name: "trailingNode",
addOptions() {
return {
node: void 0,
notAfter: []
};
},
addProseMirrorPlugins() {
var i;
const e = new b(this.name), t = this.options.node || ((i = this.editor.schema.topNodeType.contentMatch.defaultType) == null ? void 0 : i.name) || "paragraph", n = Object.entries(this.editor.schema.nodes).map(([, r]) => r).filter((r) => (this.options.notAfter || []).concat(t).includes(r.name));
return [
new I({
key: e,
appendTransaction: (r, o, s) => {
const { doc: l, tr: a, schema: d } = s, u = e.getState(s), c = l.content.size, p = d.nodes[t];
if (u)
return a.insert(c, p.create());
},
state: {
init: (r, o) => {
const s = o.tr.doc.lastChild;
return !K({ node: s, types: n });
},
apply: (r, o) => {
if (!r.docChanged || r.getMeta("__uniqueIDTransaction"))
return o;
const s = r.doc.lastChild;
return !K({ node: s, types: n });
}
}
})
];
}
});
var Oe = C.create({
name: "undoRedo",
addOptions() {
return {
depth: 100,
newGroupDelay: 500
};
},
addCommands() {
return {
undo: () => ({ state: i, dispatch: e }) => q(i, e),
redo: () => ({ state: i, dispatch: e }) => $(i, e)
};
},
addProseMirrorPlugins() {
return [Ae(this.options)];
},
addKeyboardShortcuts() {
return {
"Mod-z": () => this.editor.commands.undo(),
"Shift-Mod-z": () => this.editor.commands.redo(),
"Mod-y": () => this.editor.commands.redo(),
// Russian keyboard layouts
"Mod-я": () => this.editor.commands.undo(),
"Shift-Mod-я": () => this.editor.commands.redo()
};
}
});
function Le() {
var u;
const i = U(ee.name), {
icon: e = void 0,
tooltip: t = void 0,
shortcutKeys: n = void 0,
tooltipOptions: r = {},
action: o = void 0,
isActive: s = void 0
} = ((u = i == null ? void 0 : i.componentProps) == null ? void 0 : u.undo) ?? {}, { disabled: l } = X(s), a = ce[e], d = () => {
l || o && o();
};
return !i || !a ? /* @__PURE__ */ T(j, {}) : /* @__PURE__ */ T(
Y,
{
action: d,
disabled: l,
icon: e,
shortcutKeys: n,
tooltip: t,
tooltipOptions: r
}
);
}
function Be() {
var d;
const i = U(ee.name), {
icon: e = void 0,
tooltip: t = void 0,
shortcutKeys: n = void 0,
tooltipOptions: r = {},
action: o = void 0,
isActive: s = void 0
} = ((d = i == null ? void 0 : i.componentProps) == null ? void 0 : d.redo) ?? {}, { disabled: l } = X(s), a = () => {
l || o && o();
};
return i ? /* @__PURE__ */ T(
Y,
{
action: a,
disabled: l,
icon: e,
shortcutKeys: n,
tooltip: t,
tooltipOptions: r
}
) : /* @__PURE__ */ T(j, {});
}
const ee = /* @__PURE__ */ Oe.extend({
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
//@ts-expect-error
addOptions() {
var i;
return {
...(i = this.parent) == null ? void 0 : i.call(this),
depth: 100,
newGroupDelay: 500,
button: ({ editor: e, t, extension: n }) => {
var r, o;
return {
componentProps: {
undo: {
action: () => {
e.chain().focus().undo().run();
},
shortcutKeys: ((r = n.options.shortcutKeys) == null ? void 0 : r[0]) ?? ["mod", "Z"],
isActive: () => e.can().undo(),
icon: "Undo2",
tooltip: t("editor.undo.tooltip")
},
redo: {
action: () => {
e.chain().focus().redo().run();
},
shortcutKeys: ((o = n.options.shortcutKeys) == null ? void 0 : o[1]) ?? ["shift", "mod", "Z"],
isActive: () => e.can().redo(),
icon: "Redo2",
tooltip: t("editor.redo.tooltip")
}
}
};
}
};
}
});
export {
ee as History,
Be as RichTextRedo,
Le as RichTextUndo
};