@etrepum/lexical-builder-table
Version:
[EXPERIMENTAL] Lexical Builder @etrepum/lexical-builder-table
159 lines (158 loc) • 5.33 kB
JavaScript
import { TableNode as N, TableRowNode as x, 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 $, $isTableCellNode as H, applyTableHandlers as K } from "@lexical/table";
import { mergeRegister as E, $insertNodeToNearestRoot as L, $insertFirst as O } from "@lexical/utils";
import { $isTextNode as v, COMMAND_PRIORITY_EDITOR as P, $createParagraphNode as F, $getNodeByKey as j } from "lexical";
import { definePlan 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.35", U = z({
name: "@lexical/table",
nodes: [N, x, 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: k, hasTabHandler: R } = f;
return G(
f,
E(
a.registerCommand(
y,
({ columns: t, rows: o, includeHeaders: r }) => {
const e = B(
Number(o),
Number(t),
r
);
L(e);
const l = e.getFirstDescendant();
return v(l) && l.select(), !0;
},
P
),
a.registerNodeTransform(N, (t) => {
const [o] = _(t, null, null);
let r = 0;
for (const e of o)
r = Math.max(r, e.length);
for (const e of o) {
const l = e.length;
if (l === r || l === 0)
continue;
const n = e[l - 1].cell;
for (let s = l; s < r; ++s) {
const d = M(0);
d.append(F()), n.insertAfter(d);
}
}
}),
m(
R,
// Returning true mimics useEffect, we will call the register
// function any time the value changes
() => !0,
() => {
const t = R.get(), o = /* @__PURE__ */ new Map(), r = (e) => {
const l = e.getKey(), i = a.getElementByKey(
l
);
if (i && !o.has(l)) {
const n = K(
e,
i,
a,
t
);
o.set(l, n);
}
};
return E(
a.registerMutationListener(
N,
(e) => {
for (const [l, i] of e)
if (i === "created")
a.getEditorState().read(() => {
const n = j(l);
A(n) && r(n);
});
else if (i === "destroyed") {
const n = o.get(l);
n !== void 0 && (n.removeListeners(), o.delete(l));
}
},
{ 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 o)
e.removeListeners();
}
);
}
),
m(
p,
(t) => t,
() => a.registerNodeTransform(T, (t) => {
if (t.getColSpan() > 1 || t.getRowSpan() > 1) {
const [, , o] = I(t), [r] = D(o, t, t);
let e = o.getFirstChild();
w(
$(e),
"Expected TableNode first child to be a RowNode"
);
const l = r.length, i = r[0].length, n = [];
for (let s = 0; s < l; s++) {
s !== 0 && (e = e.getNextSibling(), w(
$(e),
"Expected TableNode first child to be a RowNode"
));
let d = null;
for (let u = 0; u < i; u++) {
const b = r[s][u], g = b.cell;
if (b.startRow === s && b.startColumn === u)
d = g, n.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 n)
s.setColSpan(1), s.setRowSpan(1);
}
})
),
m(
k,
(t) => t,
() => a.registerNodeTransform(T, (t) => {
t.getBackgroundColor() !== null && t.setBackgroundColor(null);
})
)
)
);
}
});
export {
Q as PACKAGE_VERSION,
U as TablePlan
};