@etrepum/lexical-builder-table
Version:
[EXPERIMENTAL] Lexical Builder @etrepum/lexical-builder-table
159 lines (158 loc) • 5.34 kB
JavaScript
import { TableNode as N, TableRowNode as k, TableCellNode as T, INSERT_TABLE_COMMAND as y, $createTableNodeWithDimensions as B, $computeTableMapSkipCellCheck as _, $createTableCellNode as M, $isTableNode as A, $getNodeTriplet as I, $computeTableMap as D, $isTableRowNode as E, $isTableCellNode as H, applyTableHandlers as K } from "@lexical/table";
import { mergeRegister as $, $insertNodeToNearestRoot as L, $insertFirst as O } from "@lexical/utils";
import { $isTextNode as v, COMMAND_PRIORITY_EDITOR as F, $createParagraphNode as P, $getNodeByKey as j } from "lexical";
import { defineExtension as z, safeCast as V, Store as h, provideOutput as G, registerStoreToggle as m } from "@etrepum/lexical-builder";
function w(a, c, ...C) {
if (!a)
throw new Error(
C.reduce((f, p) => f.replace("%s", String(p)), c || "")
);
}
const Q = "0.0.36", U = z({
name: "@lexical/table",
nodes: [N, k, T],
config: V({
hasCellMerge: !0,
hasCellBackgroundColor: !0,
hasTabHandler: !0
}),
init(a, c) {
return {
hasCellMerge: new h(c.hasCellMerge),
hasCellBackgroundColor: new h(c.hasCellBackgroundColor),
hasTabHandler: new h(c.hasTabHandler)
};
},
register(a, c, C) {
const f = C.getInitResult(), { hasCellMerge: p, hasCellBackgroundColor: x, hasTabHandler: R } = f;
return G(
f,
$(
a.registerCommand(
y,
({ columns: t, rows: l, includeHeaders: n }) => {
const e = B(
Number(l),
Number(t),
n
);
L(e);
const o = e.getFirstDescendant();
return v(o) && o.select(), !0;
},
F
),
a.registerNodeTransform(N, (t) => {
const [l] = _(t, null, null);
let n = 0;
for (const e of l)
n = Math.max(n, e.length);
for (const e of l) {
const o = e.length;
if (o === n || o === 0)
continue;
const r = e[o - 1].cell;
for (let s = o; s < n; ++s) {
const d = M(0);
d.append(P()), r.insertAfter(d);
}
}
}),
m(
R,
// Returning true mimics useEffect, we will call the register
// function any time the value changes
() => !0,
() => {
const t = R.get(), l = /* @__PURE__ */ new Map(), n = (e) => {
const o = e.getKey(), i = a.getElementByKey(
o
);
if (i && !l.has(o)) {
const r = K(
e,
i,
a,
t
);
l.set(o, r);
}
};
return $(
a.registerMutationListener(
N,
(e) => {
for (const [o, i] of e)
if (i === "created")
a.getEditorState().read(() => {
const r = j(o);
A(r) && n(r);
});
else if (i === "destroyed") {
const r = l.get(o);
r !== void 0 && (r.removeListeners(), l.delete(o));
}
},
{ skipInitialization: !1 }
),
// Hook might be called multiple times so cleaning up tables listeners as well,
// as it'll be reinitialized during recurring call
() => {
for (const [, e] of l)
e.removeListeners();
}
);
}
),
m(
p,
(t) => t,
() => a.registerNodeTransform(T, (t) => {
if (t.getColSpan() > 1 || t.getRowSpan() > 1) {
const [, , l] = I(t), [n] = D(l, t, t);
let e = l.getFirstChild();
w(
E(e),
"Expected TableNode first child to be a RowNode"
);
const o = n.length, i = n[0].length, r = [];
for (let s = 0; s < o; s++) {
s !== 0 && (e = e.getNextSibling(), w(
E(e),
"Expected TableNode first child to be a RowNode"
));
let d = null;
for (let u = 0; u < i; u++) {
const b = n[s][u], g = b.cell;
if (b.startRow === s && b.startColumn === u)
d = g, r.push(g);
else if (g.getColSpan() > 1 || g.getRowSpan() > 1) {
w(
H(g),
"Expected TableNode cell to be a TableCellNode"
);
const S = M(g.__headerState);
d !== null ? d.insertAfter(S) : O(e, S);
}
}
}
for (const s of r)
s.setColSpan(1), s.setRowSpan(1);
}
})
),
m(
x,
(t) => t,
() => a.registerNodeTransform(T, (t) => {
t.getBackgroundColor() !== null && t.setBackgroundColor(null);
})
)
)
);
}
});
export {
Q as PACKAGE_VERSION,
U as TableExtension
};