reactjs-tiptap-editor
Version:
A modern WYSIWYG rich text editor based on tiptap and shadcn ui for React
1,352 lines (1,351 loc) • 38.7 kB
JavaScript
import { PluginKey as Kt, Plugin as Ut } from "@tiptap/pm/state";
import { DecorationSet as Gt, Decoration as Jt } from "@tiptap/pm/view";
import { L as Qt } from "./clsx-DaPvp9ji.js";
const Bt = ["top", "right", "bottom", "left"], Rt = ["start", "end"], At = /* @__PURE__ */ Bt.reduce((t, e) => t.concat(e, e + "-" + Rt[0], e + "-" + Rt[1]), []), j = Math.min, M = Math.max, rt = Math.round, H = (t) => ({
x: t,
y: t
}), Zt = {
left: "right",
right: "left",
bottom: "top",
top: "bottom"
}, te = {
start: "end",
end: "start"
};
function ht(t, e, o) {
return M(t, j(e, o));
}
function Y(t, e) {
return typeof t == "function" ? t(e) : t;
}
function F(t) {
return t.split("-")[0];
}
function I(t) {
return t.split("-")[1];
}
function Mt(t) {
return t === "x" ? "y" : "x";
}
function wt(t) {
return t === "y" ? "height" : "width";
}
const ee = /* @__PURE__ */ new Set(["top", "bottom"]);
function V(t) {
return ee.has(F(t)) ? "y" : "x";
}
function xt(t) {
return Mt(V(t));
}
function _t(t, e, o) {
o === void 0 && (o = !1);
const n = I(t), i = xt(t), s = wt(i);
let r = i === "x" ? n === (o ? "end" : "start") ? "right" : "left" : n === "start" ? "bottom" : "top";
return e.reference[s] > e.floating[s] && (r = lt(r)), [r, lt(r)];
}
function ne(t) {
const e = lt(t);
return [ct(t), e, ct(e)];
}
function ct(t) {
return t.replace(/start|end/g, (e) => te[e]);
}
const Ot = ["left", "right"], Ct = ["right", "left"], oe = ["top", "bottom"], ie = ["bottom", "top"];
function se(t, e, o) {
switch (t) {
case "top":
case "bottom":
return o ? e ? Ct : Ot : e ? Ot : Ct;
case "left":
case "right":
return e ? oe : ie;
default:
return [];
}
}
function re(t, e, o, n) {
const i = I(t);
let s = se(F(t), o === "start", n);
return i && (s = s.map((r) => r + "-" + i), e && (s = s.concat(s.map(ct)))), s;
}
function lt(t) {
return t.replace(/left|right|bottom|top/g, (e) => Zt[e]);
}
function ce(t) {
return {
top: 0,
right: 0,
bottom: 0,
left: 0,
...t
};
}
function yt(t) {
return typeof t != "number" ? ce(t) : {
top: t,
right: t,
bottom: t,
left: t
};
}
function Q(t) {
const {
x: e,
y: o,
width: n,
height: i
} = t;
return {
width: n,
height: i,
top: o,
left: e,
right: e + n,
bottom: o + i,
x: e,
y: o
};
}
function Pt(t, e, o) {
let {
reference: n,
floating: i
} = t;
const s = V(e), r = xt(e), c = wt(r), f = F(e), a = s === "y", u = n.x + n.width / 2 - i.width / 2, l = n.y + n.height / 2 - i.height / 2, w = n[c] / 2 - i[c] / 2;
let g;
switch (f) {
case "top":
g = {
x: u,
y: n.y - i.height
};
break;
case "bottom":
g = {
x: u,
y: n.y + n.height
};
break;
case "right":
g = {
x: n.x + n.width,
y: l
};
break;
case "left":
g = {
x: n.x - i.width,
y: l
};
break;
default:
g = {
x: n.x,
y: n.y
};
}
switch (I(e)) {
case "start":
g[r] -= w * (o && a ? -1 : 1);
break;
case "end":
g[r] += w * (o && a ? -1 : 1);
break;
}
return g;
}
const le = async (t, e, o) => {
const {
placement: n = "bottom",
strategy: i = "absolute",
middleware: s = [],
platform: r
} = o, c = s.filter(Boolean), f = await (r.isRTL == null ? void 0 : r.isRTL(e));
let a = await r.getElementRects({
reference: t,
floating: e,
strategy: i
}), {
x: u,
y: l
} = Pt(a, n, f), w = n, g = {}, p = 0;
for (let v = 0; v < c.length; v++) {
const {
name: d,
fn: m
} = c[v], {
x: S,
y: A,
data: E,
reset: O
} = await m({
x: u,
y: l,
initialPlacement: n,
placement: w,
strategy: i,
middlewareData: g,
rects: a,
platform: r,
elements: {
reference: t,
floating: e
}
});
u = S ?? u, l = A ?? l, g = {
...g,
[d]: {
...g[d],
...E
}
}, O && p <= 50 && (p++, typeof O == "object" && (O.placement && (w = O.placement), O.rects && (a = O.rects === !0 ? await r.getElementRects({
reference: t,
floating: e,
strategy: i
}) : O.rects), {
x: u,
y: l
} = Pt(a, w, f)), v = -1);
}
return {
x: u,
y: l,
placement: w,
strategy: i,
middlewareData: g
};
};
async function Z(t, e) {
var o;
e === void 0 && (e = {});
const {
x: n,
y: i,
platform: s,
rects: r,
elements: c,
strategy: f
} = t, {
boundary: a = "clippingAncestors",
rootBoundary: u = "viewport",
elementContext: l = "floating",
altBoundary: w = !1,
padding: g = 0
} = Y(e, t), p = yt(g), d = c[w ? l === "floating" ? "reference" : "floating" : l], m = Q(await s.getClippingRect({
element: (o = await (s.isElement == null ? void 0 : s.isElement(d))) == null || o ? d : d.contextElement || await (s.getDocumentElement == null ? void 0 : s.getDocumentElement(c.floating)),
boundary: a,
rootBoundary: u,
strategy: f
})), S = l === "floating" ? {
x: n,
y: i,
width: r.floating.width,
height: r.floating.height
} : r.reference, A = await (s.getOffsetParent == null ? void 0 : s.getOffsetParent(c.floating)), E = await (s.isElement == null ? void 0 : s.isElement(A)) ? await (s.getScale == null ? void 0 : s.getScale(A)) || {
x: 1,
y: 1
} : {
x: 1,
y: 1
}, O = Q(s.convertOffsetParentRelativeRectToViewportRelativeRect ? await s.convertOffsetParentRelativeRectToViewportRelativeRect({
elements: c,
rect: S,
offsetParent: A,
strategy: f
}) : S);
return {
top: (m.top - O.top + p.top) / E.y,
bottom: (O.bottom - m.bottom + p.bottom) / E.y,
left: (m.left - O.left + p.left) / E.x,
right: (O.right - m.right + p.right) / E.x
};
}
const ae = (t) => ({
name: "arrow",
options: t,
async fn(e) {
const {
x: o,
y: n,
placement: i,
rects: s,
platform: r,
elements: c,
middlewareData: f
} = e, {
element: a,
padding: u = 0
} = Y(t, e) || {};
if (a == null)
return {};
const l = yt(u), w = {
x: o,
y: n
}, g = xt(i), p = wt(g), v = await r.getDimensions(a), d = g === "y", m = d ? "top" : "left", S = d ? "bottom" : "right", A = d ? "clientHeight" : "clientWidth", E = s.reference[p] + s.reference[g] - w[g] - s.floating[p], O = w[g] - s.reference[g], y = await (r.getOffsetParent == null ? void 0 : r.getOffsetParent(a));
let b = y ? y[A] : 0;
(!b || !await (r.isElement == null ? void 0 : r.isElement(y))) && (b = c.floating[A] || s.floating[p]);
const C = E / 2 - O / 2, P = b / 2 - v[p] / 2 - 1, h = j(l[m], P), R = j(l[S], P), D = h, k = b - v[p] - R, L = b / 2 - v[p] / 2 + C, x = ht(D, L, k), $ = !f.arrow && I(i) != null && L !== x && s.reference[p] / 2 - (L < D ? h : R) - v[p] / 2 < 0, T = $ ? L < D ? L - D : L - k : 0;
return {
[g]: w[g] + T,
data: {
[g]: x,
centerOffset: L - x - T,
...$ && {
alignmentOffset: T
}
},
reset: $
};
}
});
function fe(t, e, o) {
return (t ? [...o.filter((i) => I(i) === t), ...o.filter((i) => I(i) !== t)] : o.filter((i) => F(i) === i)).filter((i) => t ? I(i) === t || (e ? ct(i) !== i : !1) : !0);
}
const ue = function(t) {
return t === void 0 && (t = {}), {
name: "autoPlacement",
options: t,
async fn(e) {
var o, n, i;
const {
rects: s,
middlewareData: r,
placement: c,
platform: f,
elements: a
} = e, {
crossAxis: u = !1,
alignment: l,
allowedPlacements: w = At,
autoAlignment: g = !0,
...p
} = Y(t, e), v = l !== void 0 || w === At ? fe(l || null, g, w) : w, d = await Z(e, p), m = ((o = r.autoPlacement) == null ? void 0 : o.index) || 0, S = v[m];
if (S == null)
return {};
const A = _t(S, s, await (f.isRTL == null ? void 0 : f.isRTL(a.floating)));
if (c !== S)
return {
reset: {
placement: v[0]
}
};
const E = [d[F(S)], d[A[0]], d[A[1]]], O = [...((n = r.autoPlacement) == null ? void 0 : n.overflows) || [], {
placement: S,
overflows: E
}], y = v[m + 1];
if (y)
return {
data: {
index: m + 1,
overflows: O
},
reset: {
placement: y
}
};
const b = O.map((h) => {
const R = I(h.placement);
return [h.placement, R && u ? (
// Check along the mainAxis and main crossAxis side.
h.overflows.slice(0, 2).reduce((D, k) => D + k, 0)
) : (
// Check only the mainAxis.
h.overflows[0]
), h.overflows];
}).sort((h, R) => h[1] - R[1]), P = ((i = b.filter((h) => h[2].slice(
0,
// Aligned placements should not check their opposite crossAxis
// side.
I(h[0]) ? 2 : 3
).every((R) => R <= 0))[0]) == null ? void 0 : i[0]) || b[0][0];
return P !== c ? {
data: {
index: m + 1,
overflows: O
},
reset: {
placement: P
}
} : {};
}
};
}, de = function(t) {
return t === void 0 && (t = {}), {
name: "flip",
options: t,
async fn(e) {
var o, n;
const {
placement: i,
middlewareData: s,
rects: r,
initialPlacement: c,
platform: f,
elements: a
} = e, {
mainAxis: u = !0,
crossAxis: l = !0,
fallbackPlacements: w,
fallbackStrategy: g = "bestFit",
fallbackAxisSideDirection: p = "none",
flipAlignment: v = !0,
...d
} = Y(t, e);
if ((o = s.arrow) != null && o.alignmentOffset)
return {};
const m = F(i), S = V(c), A = F(c) === c, E = await (f.isRTL == null ? void 0 : f.isRTL(a.floating)), O = w || (A || !v ? [lt(c)] : ne(c)), y = p !== "none";
!w && y && O.push(...re(c, v, p, E));
const b = [c, ...O], C = await Z(e, d), P = [];
let h = ((n = s.flip) == null ? void 0 : n.overflows) || [];
if (u && P.push(C[m]), l) {
const L = _t(i, r, E);
P.push(C[L[0]], C[L[1]]);
}
if (h = [...h, {
placement: i,
overflows: P
}], !P.every((L) => L <= 0)) {
var R, D;
const L = (((R = s.flip) == null ? void 0 : R.index) || 0) + 1, x = b[L];
if (x && (!(l === "alignment" ? S !== V(x) : !1) || // We leave the current main axis only if every placement on that axis
// overflows the main axis.
h.every((B) => V(B.placement) === S ? B.overflows[0] > 0 : !0)))
return {
data: {
index: L,
overflows: h
},
reset: {
placement: x
}
};
let $ = (D = h.filter((T) => T.overflows[0] <= 0).sort((T, B) => T.overflows[1] - B.overflows[1])[0]) == null ? void 0 : D.placement;
if (!$)
switch (g) {
case "bestFit": {
var k;
const T = (k = h.filter((B) => {
if (y) {
const _ = V(B.placement);
return _ === S || // Create a bias to the `y` side axis due to horizontal
// reading directions favoring greater width.
_ === "y";
}
return !0;
}).map((B) => [B.placement, B.overflows.filter((_) => _ > 0).reduce((_, U) => _ + U, 0)]).sort((B, _) => B[1] - _[1])[0]) == null ? void 0 : k[0];
T && ($ = T);
break;
}
case "initialPlacement":
$ = c;
break;
}
if (i !== $)
return {
reset: {
placement: $
}
};
}
return {};
}
};
};
function Et(t, e) {
return {
top: t.top - e.height,
right: t.right - e.width,
bottom: t.bottom - e.height,
left: t.left - e.width
};
}
function Dt(t) {
return Bt.some((e) => t[e] >= 0);
}
const me = function(t) {
return t === void 0 && (t = {}), {
name: "hide",
options: t,
async fn(e) {
const {
rects: o
} = e, {
strategy: n = "referenceHidden",
...i
} = Y(t, e);
switch (n) {
case "referenceHidden": {
const s = await Z(e, {
...i,
elementContext: "reference"
}), r = Et(s, o.reference);
return {
data: {
referenceHiddenOffsets: r,
referenceHidden: Dt(r)
}
};
}
case "escaped": {
const s = await Z(e, {
...i,
altBoundary: !0
}), r = Et(s, o.floating);
return {
data: {
escapedOffsets: r,
escaped: Dt(r)
}
};
}
default:
return {};
}
}
};
};
function Nt(t) {
const e = j(...t.map((s) => s.left)), o = j(...t.map((s) => s.top)), n = M(...t.map((s) => s.right)), i = M(...t.map((s) => s.bottom));
return {
x: e,
y: o,
width: n - e,
height: i - o
};
}
function ge(t) {
const e = t.slice().sort((i, s) => i.y - s.y), o = [];
let n = null;
for (let i = 0; i < e.length; i++) {
const s = e[i];
!n || s.y - n.y > n.height / 2 ? o.push([s]) : o[o.length - 1].push(s), n = s;
}
return o.map((i) => Q(Nt(i)));
}
const he = function(t) {
return t === void 0 && (t = {}), {
name: "inline",
options: t,
async fn(e) {
const {
placement: o,
elements: n,
rects: i,
platform: s,
strategy: r
} = e, {
padding: c = 2,
x: f,
y: a
} = Y(t, e), u = Array.from(await (s.getClientRects == null ? void 0 : s.getClientRects(n.reference)) || []), l = ge(u), w = Q(Nt(u)), g = yt(c);
function p() {
if (l.length === 2 && l[0].left > l[1].right && f != null && a != null)
return l.find((d) => f > d.left - g.left && f < d.right + g.right && a > d.top - g.top && a < d.bottom + g.bottom) || w;
if (l.length >= 2) {
if (V(o) === "y") {
const h = l[0], R = l[l.length - 1], D = F(o) === "top", k = h.top, L = R.bottom, x = D ? h.left : R.left, $ = D ? h.right : R.right, T = $ - x, B = L - k;
return {
top: k,
bottom: L,
left: x,
right: $,
width: T,
height: B,
x,
y: k
};
}
const d = F(o) === "left", m = M(...l.map((h) => h.right)), S = j(...l.map((h) => h.left)), A = l.filter((h) => d ? h.left === S : h.right === m), E = A[0].top, O = A[A.length - 1].bottom, y = S, b = m, C = b - y, P = O - E;
return {
top: E,
bottom: O,
left: y,
right: b,
width: C,
height: P,
x: y,
y: E
};
}
return w;
}
const v = await s.getElementRects({
reference: {
getBoundingClientRect: p
},
floating: n.floating,
strategy: r
});
return i.reference.x !== v.reference.x || i.reference.y !== v.reference.y || i.reference.width !== v.reference.width || i.reference.height !== v.reference.height ? {
reset: {
rects: v
}
} : {};
}
};
}, pe = /* @__PURE__ */ new Set(["left", "top"]);
async function we(t, e) {
const {
placement: o,
platform: n,
elements: i
} = t, s = await (n.isRTL == null ? void 0 : n.isRTL(i.floating)), r = F(o), c = I(o), f = V(o) === "y", a = pe.has(r) ? -1 : 1, u = s && f ? -1 : 1, l = Y(e, t);
let {
mainAxis: w,
crossAxis: g,
alignmentAxis: p
} = typeof l == "number" ? {
mainAxis: l,
crossAxis: 0,
alignmentAxis: null
} : {
mainAxis: l.mainAxis || 0,
crossAxis: l.crossAxis || 0,
alignmentAxis: l.alignmentAxis
};
return c && typeof p == "number" && (g = c === "end" ? p * -1 : p), f ? {
x: g * u,
y: w * a
} : {
x: w * a,
y: g * u
};
}
const xe = function(t) {
return t === void 0 && (t = 0), {
name: "offset",
options: t,
async fn(e) {
var o, n;
const {
x: i,
y: s,
placement: r,
middlewareData: c
} = e, f = await we(e, t);
return r === ((o = c.offset) == null ? void 0 : o.placement) && (n = c.arrow) != null && n.alignmentOffset ? {} : {
x: i + f.x,
y: s + f.y,
data: {
...f,
placement: r
}
};
}
};
}, ye = function(t) {
return t === void 0 && (t = {}), {
name: "shift",
options: t,
async fn(e) {
const {
x: o,
y: n,
placement: i
} = e, {
mainAxis: s = !0,
crossAxis: r = !1,
limiter: c = {
fn: (d) => {
let {
x: m,
y: S
} = d;
return {
x: m,
y: S
};
}
},
...f
} = Y(t, e), a = {
x: o,
y: n
}, u = await Z(e, f), l = V(F(i)), w = Mt(l);
let g = a[w], p = a[l];
if (s) {
const d = w === "y" ? "top" : "left", m = w === "y" ? "bottom" : "right", S = g + u[d], A = g - u[m];
g = ht(S, g, A);
}
if (r) {
const d = l === "y" ? "top" : "left", m = l === "y" ? "bottom" : "right", S = p + u[d], A = p - u[m];
p = ht(S, p, A);
}
const v = c.fn({
...e,
[w]: g,
[l]: p
});
return {
...v,
data: {
x: v.x - o,
y: v.y - n,
enabled: {
[w]: s,
[l]: r
}
}
};
}
};
}, ve = function(t) {
return t === void 0 && (t = {}), {
name: "size",
options: t,
async fn(e) {
var o, n;
const {
placement: i,
rects: s,
platform: r,
elements: c
} = e, {
apply: f = () => {
},
...a
} = Y(t, e), u = await Z(e, a), l = F(i), w = I(i), g = V(i) === "y", {
width: p,
height: v
} = s.floating;
let d, m;
l === "top" || l === "bottom" ? (d = l, m = w === (await (r.isRTL == null ? void 0 : r.isRTL(c.floating)) ? "start" : "end") ? "left" : "right") : (m = l, d = w === "end" ? "top" : "bottom");
const S = v - u.top - u.bottom, A = p - u.left - u.right, E = j(v - u[d], S), O = j(p - u[m], A), y = !e.middlewareData.shift;
let b = E, C = O;
if ((o = e.middlewareData.shift) != null && o.enabled.x && (C = A), (n = e.middlewareData.shift) != null && n.enabled.y && (b = S), y && !w) {
const h = M(u.left, 0), R = M(u.right, 0), D = M(u.top, 0), k = M(u.bottom, 0);
g ? C = p - 2 * (h !== 0 || R !== 0 ? h + R : M(u.left, u.right)) : b = v - 2 * (D !== 0 || k !== 0 ? D + k : M(u.top, u.bottom));
}
await f({
...e,
availableWidth: C,
availableHeight: b
});
const P = await r.getDimensions(c.floating);
return p !== P.width || v !== P.height ? {
reset: {
rects: !0
}
} : {};
}
};
};
function at() {
return typeof window < "u";
}
function et(t) {
return Ft(t) ? (t.nodeName || "").toLowerCase() : "#document";
}
function N(t) {
var e;
return (t == null || (e = t.ownerDocument) == null ? void 0 : e.defaultView) || window;
}
function z(t) {
var e;
return (e = (Ft(t) ? t.ownerDocument : t.document) || window.document) == null ? void 0 : e.documentElement;
}
function Ft(t) {
return at() ? t instanceof Node || t instanceof N(t).Node : !1;
}
function W(t) {
return at() ? t instanceof Element || t instanceof N(t).Element : !1;
}
function X(t) {
return at() ? t instanceof HTMLElement || t instanceof N(t).HTMLElement : !1;
}
function Tt(t) {
return !at() || typeof ShadowRoot > "u" ? !1 : t instanceof ShadowRoot || t instanceof N(t).ShadowRoot;
}
const be = /* @__PURE__ */ new Set(["inline", "contents"]);
function it(t) {
const {
overflow: e,
overflowX: o,
overflowY: n,
display: i
} = q(t);
return /auto|scroll|overlay|hidden|clip/.test(e + n + o) && !be.has(i);
}
const Se = /* @__PURE__ */ new Set(["table", "td", "th"]);
function Re(t) {
return Se.has(et(t));
}
const Ae = [":popover-open", ":modal"];
function ft(t) {
return Ae.some((e) => {
try {
return t.matches(e);
} catch {
return !1;
}
});
}
const Oe = ["transform", "translate", "scale", "rotate", "perspective"], Ce = ["transform", "translate", "scale", "rotate", "perspective", "filter"], Pe = ["paint", "layout", "strict", "content"];
function vt(t) {
const e = bt(), o = W(t) ? q(t) : t;
return Oe.some((n) => o[n] ? o[n] !== "none" : !1) || (o.containerType ? o.containerType !== "normal" : !1) || !e && (o.backdropFilter ? o.backdropFilter !== "none" : !1) || !e && (o.filter ? o.filter !== "none" : !1) || Ce.some((n) => (o.willChange || "").includes(n)) || Pe.some((n) => (o.contain || "").includes(n));
}
function Ee(t) {
let e = K(t);
for (; X(e) && !tt(e); ) {
if (vt(e))
return e;
if (ft(e))
return null;
e = K(e);
}
return null;
}
function bt() {
return typeof CSS > "u" || !CSS.supports ? !1 : CSS.supports("-webkit-backdrop-filter", "none");
}
const De = /* @__PURE__ */ new Set(["html", "body", "#document"]);
function tt(t) {
return De.has(et(t));
}
function q(t) {
return N(t).getComputedStyle(t);
}
function ut(t) {
return W(t) ? {
scrollLeft: t.scrollLeft,
scrollTop: t.scrollTop
} : {
scrollLeft: t.scrollX,
scrollTop: t.scrollY
};
}
function K(t) {
if (et(t) === "html")
return t;
const e = (
// Step into the shadow DOM of the parent of a slotted node.
t.assignedSlot || // DOM Element detected.
t.parentNode || // ShadowRoot detected.
Tt(t) && t.host || // Fallback.
z(t)
);
return Tt(e) ? e.host : e;
}
function It(t) {
const e = K(t);
return tt(e) ? t.ownerDocument ? t.ownerDocument.body : t.body : X(e) && it(e) ? e : It(e);
}
function Wt(t, e, o) {
var n;
e === void 0 && (e = []);
const i = It(t), s = i === ((n = t.ownerDocument) == null ? void 0 : n.body), r = N(i);
return s ? (pt(r), e.concat(r, r.visualViewport || [], it(i) ? i : [], [])) : e.concat(i, Wt(i, []));
}
function pt(t) {
return t.parent && Object.getPrototypeOf(t.parent) ? t.frameElement : null;
}
function qt(t) {
const e = q(t);
let o = parseFloat(e.width) || 0, n = parseFloat(e.height) || 0;
const i = X(t), s = i ? t.offsetWidth : o, r = i ? t.offsetHeight : n, c = rt(o) !== s || rt(n) !== r;
return c && (o = s, n = r), {
width: o,
height: n,
$: c
};
}
function Vt(t) {
return W(t) ? t : t.contextElement;
}
function J(t) {
const e = Vt(t);
if (!X(e))
return H(1);
const o = e.getBoundingClientRect(), {
width: n,
height: i,
$: s
} = qt(e);
let r = (s ? rt(o.width) : o.width) / n, c = (s ? rt(o.height) : o.height) / i;
return (!r || !Number.isFinite(r)) && (r = 1), (!c || !Number.isFinite(c)) && (c = 1), {
x: r,
y: c
};
}
const Te = /* @__PURE__ */ H(0);
function Ht(t) {
const e = N(t);
return !bt() || !e.visualViewport ? Te : {
x: e.visualViewport.offsetLeft,
y: e.visualViewport.offsetTop
};
}
function Le(t, e, o) {
return e === void 0 && (e = !1), !o || e && o !== N(t) ? !1 : e;
}
function ot(t, e, o, n) {
e === void 0 && (e = !1), o === void 0 && (o = !1);
const i = t.getBoundingClientRect(), s = Vt(t);
let r = H(1);
e && (n ? W(n) && (r = J(n)) : r = J(t));
const c = Le(s, o, n) ? Ht(s) : H(0);
let f = (i.left + c.x) / r.x, a = (i.top + c.y) / r.y, u = i.width / r.x, l = i.height / r.y;
if (s) {
const w = N(s), g = n && W(n) ? N(n) : n;
let p = w, v = pt(p);
for (; v && n && g !== p; ) {
const d = J(v), m = v.getBoundingClientRect(), S = q(v), A = m.left + (v.clientLeft + parseFloat(S.paddingLeft)) * d.x, E = m.top + (v.clientTop + parseFloat(S.paddingTop)) * d.y;
f *= d.x, a *= d.y, u *= d.x, l *= d.y, f += A, a += E, p = N(v), v = pt(p);
}
}
return Q({
width: u,
height: l,
x: f,
y: a
});
}
function dt(t, e) {
const o = ut(t).scrollLeft;
return e ? e.left + o : ot(z(t)).left + o;
}
function jt(t, e) {
const o = t.getBoundingClientRect(), n = o.left + e.scrollLeft - dt(t, o), i = o.top + e.scrollTop;
return {
x: n,
y: i
};
}
function $e(t) {
let {
elements: e,
rect: o,
offsetParent: n,
strategy: i
} = t;
const s = i === "fixed", r = z(n), c = e ? ft(e.floating) : !1;
if (n === r || c && s)
return o;
let f = {
scrollLeft: 0,
scrollTop: 0
}, a = H(1);
const u = H(0), l = X(n);
if ((l || !l && !s) && ((et(n) !== "body" || it(r)) && (f = ut(n)), X(n))) {
const g = ot(n);
a = J(n), u.x = g.x + n.clientLeft, u.y = g.y + n.clientTop;
}
const w = r && !l && !s ? jt(r, f) : H(0);
return {
width: o.width * a.x,
height: o.height * a.y,
x: o.x * a.x - f.scrollLeft * a.x + u.x + w.x,
y: o.y * a.y - f.scrollTop * a.y + u.y + w.y
};
}
function ke(t) {
return Array.from(t.getClientRects());
}
function Be(t) {
const e = z(t), o = ut(t), n = t.ownerDocument.body, i = M(e.scrollWidth, e.clientWidth, n.scrollWidth, n.clientWidth), s = M(e.scrollHeight, e.clientHeight, n.scrollHeight, n.clientHeight);
let r = -o.scrollLeft + dt(t);
const c = -o.scrollTop;
return q(n).direction === "rtl" && (r += M(e.clientWidth, n.clientWidth) - i), {
width: i,
height: s,
x: r,
y: c
};
}
const Lt = 25;
function Me(t, e) {
const o = N(t), n = z(t), i = o.visualViewport;
let s = n.clientWidth, r = n.clientHeight, c = 0, f = 0;
if (i) {
s = i.width, r = i.height;
const u = bt();
(!u || u && e === "fixed") && (c = i.offsetLeft, f = i.offsetTop);
}
const a = dt(n);
if (a <= 0) {
const u = n.ownerDocument, l = u.body, w = getComputedStyle(l), g = u.compatMode === "CSS1Compat" && parseFloat(w.marginLeft) + parseFloat(w.marginRight) || 0, p = Math.abs(n.clientWidth - l.clientWidth - g);
p <= Lt && (s -= p);
} else a <= Lt && (s += a);
return {
width: s,
height: r,
x: c,
y: f
};
}
const _e = /* @__PURE__ */ new Set(["absolute", "fixed"]);
function Ne(t, e) {
const o = ot(t, !0, e === "fixed"), n = o.top + t.clientTop, i = o.left + t.clientLeft, s = X(t) ? J(t) : H(1), r = t.clientWidth * s.x, c = t.clientHeight * s.y, f = i * s.x, a = n * s.y;
return {
width: r,
height: c,
x: f,
y: a
};
}
function $t(t, e, o) {
let n;
if (e === "viewport")
n = Me(t, o);
else if (e === "document")
n = Be(z(t));
else if (W(e))
n = Ne(e, o);
else {
const i = Ht(t);
n = {
x: e.x - i.x,
y: e.y - i.y,
width: e.width,
height: e.height
};
}
return Q(n);
}
function Xt(t, e) {
const o = K(t);
return o === e || !W(o) || tt(o) ? !1 : q(o).position === "fixed" || Xt(o, e);
}
function Fe(t, e) {
const o = e.get(t);
if (o)
return o;
let n = Wt(t, []).filter((c) => W(c) && et(c) !== "body"), i = null;
const s = q(t).position === "fixed";
let r = s ? K(t) : t;
for (; W(r) && !tt(r); ) {
const c = q(r), f = vt(r);
!f && c.position === "fixed" && (i = null), (s ? !f && !i : !f && c.position === "static" && !!i && _e.has(i.position) || it(r) && !f && Xt(t, r)) ? n = n.filter((u) => u !== r) : i = c, r = K(r);
}
return e.set(t, n), n;
}
function Ie(t) {
let {
element: e,
boundary: o,
rootBoundary: n,
strategy: i
} = t;
const r = [...o === "clippingAncestors" ? ft(e) ? [] : Fe(e, this._c) : [].concat(o), n], c = r[0], f = r.reduce((a, u) => {
const l = $t(e, u, i);
return a.top = M(l.top, a.top), a.right = j(l.right, a.right), a.bottom = j(l.bottom, a.bottom), a.left = M(l.left, a.left), a;
}, $t(e, c, i));
return {
width: f.right - f.left,
height: f.bottom - f.top,
x: f.left,
y: f.top
};
}
function We(t) {
const {
width: e,
height: o
} = qt(t);
return {
width: e,
height: o
};
}
function qe(t, e, o) {
const n = X(e), i = z(e), s = o === "fixed", r = ot(t, !0, s, e);
let c = {
scrollLeft: 0,
scrollTop: 0
};
const f = H(0);
function a() {
f.x = dt(i);
}
if (n || !n && !s)
if ((et(e) !== "body" || it(i)) && (c = ut(e)), n) {
const g = ot(e, !0, s, e);
f.x = g.x + e.clientLeft, f.y = g.y + e.clientTop;
} else i && a();
s && !n && i && a();
const u = i && !n && !s ? jt(i, c) : H(0), l = r.left + c.scrollLeft - f.x - u.x, w = r.top + c.scrollTop - f.y - u.y;
return {
x: l,
y: w,
width: r.width,
height: r.height
};
}
function gt(t) {
return q(t).position === "static";
}
function kt(t, e) {
if (!X(t) || q(t).position === "fixed")
return null;
if (e)
return e(t);
let o = t.offsetParent;
return z(t) === o && (o = o.ownerDocument.body), o;
}
function Yt(t, e) {
const o = N(t);
if (ft(t))
return o;
if (!X(t)) {
let i = K(t);
for (; i && !tt(i); ) {
if (W(i) && !gt(i))
return i;
i = K(i);
}
return o;
}
let n = kt(t, e);
for (; n && Re(n) && gt(n); )
n = kt(n, e);
return n && tt(n) && gt(n) && !vt(n) ? o : n || Ee(t) || o;
}
const Ve = async function(t) {
const e = this.getOffsetParent || Yt, o = this.getDimensions, n = await o(t.floating);
return {
reference: qe(t.reference, await e(t.floating), t.strategy),
floating: {
x: 0,
y: 0,
width: n.width,
height: n.height
}
};
};
function He(t) {
return q(t).direction === "rtl";
}
const je = {
convertOffsetParentRelativeRectToViewportRelativeRect: $e,
getDocumentElement: z,
getClippingRect: Ie,
getOffsetParent: Yt,
getElementRects: Ve,
getClientRects: ke,
getDimensions: We,
getScale: J,
isElement: W,
isRTL: He
}, Je = xe, Qe = ue, Ze = ye, tn = de, en = ve, nn = me, on = ae, sn = he, rn = (t, e, o) => {
const n = /* @__PURE__ */ new Map(), i = {
platform: je,
...o
}, s = {
...i.platform,
_c: n
};
return le(t, e, {
...i,
platform: s
});
};
function Xe(t) {
var e;
const { char: o, allowSpaces: n, allowToIncludeChar: i, allowedPrefixes: s, startOfLine: r, $position: c } = t, f = n && !i, a = Qt(o), u = new RegExp(`\\s${a}$`), l = r ? "^" : "", w = i ? "" : a, g = f ? new RegExp(`${l}${a}.*?(?=\\s${w}|$)`, "gm") : new RegExp(`${l}(?:^)?${a}[^\\s${w}]*`, "gm"), p = ((e = c.nodeBefore) == null ? void 0 : e.isText) && c.nodeBefore.text;
if (!p)
return null;
const v = c.pos - p.length, d = Array.from(p.matchAll(g)).pop();
if (!d || d.input === void 0 || d.index === void 0)
return null;
const m = d.input.slice(Math.max(0, d.index - 1), d.index), S = new RegExp(`^[${s == null ? void 0 : s.join("")}\0]?$`).test(m);
if (s !== null && !S)
return null;
const A = v + d.index;
let E = A + d[0].length;
return f && u.test(p.slice(E - 1, E + 1)) && (d[0] += " ", E += 1), A < c.pos && E >= c.pos ? {
range: {
from: A,
to: E
},
query: d[0].slice(o.length),
text: d[0]
} : null;
}
var Ye = new Kt("suggestion");
function ze({
pluginKey: t = Ye,
editor: e,
char: o = "@",
allowSpaces: n = !1,
allowToIncludeChar: i = !1,
allowedPrefixes: s = [" "],
startOfLine: r = !1,
decorationTag: c = "span",
decorationClass: f = "suggestion",
decorationContent: a = "",
decorationEmptyClass: u = "is-empty",
command: l = () => null,
items: w = () => [],
render: g = () => ({}),
allow: p = () => !0,
findSuggestionMatch: v = Xe
}) {
let d;
const m = g == null ? void 0 : g(), S = () => {
const y = e.state.selection.$anchor.pos, b = e.view.coordsAtPos(y), { top: C, right: P, bottom: h, left: R } = b;
try {
return new DOMRect(R, C, P - R, h - C);
} catch {
return null;
}
}, A = (y, b) => b ? () => {
const C = t.getState(e.state), P = C == null ? void 0 : C.decorationId, h = y.dom.querySelector(`[data-decoration-id="${P}"]`);
return (h == null ? void 0 : h.getBoundingClientRect()) || null;
} : S;
function E(y, b) {
var C;
try {
const h = t.getState(y.state), R = h != null && h.decorationId ? y.dom.querySelector(`[data-decoration-id="${h.decorationId}"]`) : null, D = {
// @ts-ignore editor is available in closure
editor: e,
range: (h == null ? void 0 : h.range) || { from: 0, to: 0 },
query: (h == null ? void 0 : h.query) || null,
text: (h == null ? void 0 : h.text) || null,
items: [],
command: (k) => l({ editor: e, range: (h == null ? void 0 : h.range) || { from: 0, to: 0 }, props: k }),
decorationNode: R,
clientRect: A(y, R)
};
(C = m == null ? void 0 : m.onExit) == null || C.call(m, D);
} catch {
}
const P = y.state.tr.setMeta(b, { exit: !0 });
y.dispatch(P);
}
const O = new Ut({
key: t,
view() {
return {
update: async (y, b) => {
var C, P, h, R, D, k, L;
const x = (C = this.key) == null ? void 0 : C.getState(b), $ = (P = this.key) == null ? void 0 : P.getState(y.state), T = x.active && $.active && x.range.from !== $.range.from, B = !x.active && $.active, _ = x.active && !$.active, U = !B && !_ && x.query !== $.query, nt = B || T && U, st = U || T, mt = _ || T && U;
if (!nt && !st && !mt)
return;
const G = mt && !nt ? x : $, St = y.dom.querySelector(`[data-decoration-id="${G.decorationId}"]`);
d = {
editor: e,
range: G.range,
query: G.query,
text: G.text,
items: [],
command: (zt) => l({
editor: e,
range: G.range,
props: zt
}),
decorationNode: St,
clientRect: A(y, St)
}, nt && ((h = m == null ? void 0 : m.onBeforeStart) == null || h.call(m, d)), st && ((R = m == null ? void 0 : m.onBeforeUpdate) == null || R.call(m, d)), (st || nt) && (d.items = await w({
editor: e,
query: G.query
})), mt && ((D = m == null ? void 0 : m.onExit) == null || D.call(m, d)), st && ((k = m == null ? void 0 : m.onUpdate) == null || k.call(m, d)), nt && ((L = m == null ? void 0 : m.onStart) == null || L.call(m, d));
},
destroy: () => {
var y;
d && ((y = m == null ? void 0 : m.onExit) == null || y.call(m, d));
}
};
},
state: {
// Initialize the plugin's internal state.
init() {
return {
active: !1,
range: {
from: 0,
to: 0
},
query: null,
text: null,
composing: !1
};
},
// Apply changes to the plugin state from a view transaction.
apply(y, b, C, P) {
const { isEditable: h } = e, { composing: R } = e.view, { selection: D } = y, { empty: k, from: L } = D, x = { ...b }, $ = y.getMeta(t);
if ($ && $.exit)
return x.active = !1, x.decorationId = null, x.range = { from: 0, to: 0 }, x.query = null, x.text = null, x;
if (x.composing = R, h && (k || e.view.composing)) {
(L < b.range.from || L > b.range.to) && !R && !b.composing && (x.active = !1);
const T = v({
char: o,
allowSpaces: n,
allowToIncludeChar: i,
allowedPrefixes: s,
startOfLine: r,
$position: D.$from
}), B = `id_${Math.floor(Math.random() * 4294967295)}`;
T && p({
editor: e,
state: P,
range: T.range,
isActive: b.active
}) ? (x.active = !0, x.decorationId = b.decorationId ? b.decorationId : B, x.range = T.range, x.query = T.query, x.text = T.text) : x.active = !1;
} else
x.active = !1;
return x.active || (x.decorationId = null, x.range = { from: 0, to: 0 }, x.query = null, x.text = null), x;
}
},
props: {
// Call the keydown hook if suggestion is active.
handleKeyDown(y, b) {
var C, P, h, R;
const { active: D, range: k } = O.getState(y.state);
if (!D)
return !1;
if (b.key === "Escape" || b.key === "Esc") {
const x = O.getState(y.state), $ = (C = d == null ? void 0 : d.decorationNode) != null ? C : null, T = $ ?? (x != null && x.decorationId ? y.dom.querySelector(`[data-decoration-id="${x.decorationId}"]`) : null);
if (((P = m == null ? void 0 : m.onKeyDown) == null ? void 0 : P.call(m, { view: y, event: b, range: x.range })) || !1)
return !0;
const _ = {
editor: e,
range: x.range,
query: x.query,
text: x.text,
items: [],
command: (U) => l({ editor: e, range: x.range, props: U }),
decorationNode: T,
// If we have a cached decoration node, use it for the clientRect
// to avoid another DOM lookup. If not, leave clientRect null and
// let consumer decide if they want to query.
clientRect: T ? () => T.getBoundingClientRect() || null : null
};
return (h = m == null ? void 0 : m.onExit) == null || h.call(m, _), E(y, t), !0;
}
return ((R = m == null ? void 0 : m.onKeyDown) == null ? void 0 : R.call(m, { view: y, event: b, range: k })) || !1;
},
// Setup decorator on the currently active suggestion.
decorations(y) {
const { active: b, range: C, decorationId: P, query: h } = O.getState(y);
if (!b)
return null;
const R = !(h != null && h.length), D = [f];
return R && D.push(u), Gt.create(y.doc, [
Jt.inline(C.from, C.to, {
nodeName: c,
class: D.join(" "),
"data-decoration-id": P,
"data-decoration-content": a
})
]);
}
}
});
return O;
}
var cn = ze;
export {
ze as S,
on as a,
en as b,
rn as c,
Qe as d,
sn as e,
tn as f,
nn as h,
cn as i,
Je as o,
Ze as s
};