File
Blob: src/client/components/editor/extensions/table/normalization-extension.ts
| 1 | import { Extension } from "@tiptap/core"; |
| 2 | import { Plugin } from "@tiptap/pm/state"; |
| 3 | import { applyExplicitColumnWidths, deriveCanonicalColumnWidths, snapshotTableWidths } from "./widths"; |
| 4 | |
| 5 | export const TableWidthNormalization = Extension.create({ |
| 6 | name: "tableWidthNormalization", |
| 7 | |
| 8 | addProseMirrorPlugins() { |
| 9 | return [ |
| 10 | new Plugin({ |
| 11 | appendTransaction(transactions, _oldState, newState) { |
| 12 | if (!transactions.some((tr) => tr.docChanged)) return null; |
| 13 | |
| 14 | const nextTr = newState.tr; |
| 15 | let mutated = false; |
| 16 | |
| 17 | newState.doc.descendants((node, pos) => { |
| 18 | if (node.type.spec.tableRole !== "table") return true; |
| 19 | |
| 20 | const snap = snapshotTableWidths(node); |
| 21 | if (!snap.hasSome || snap.hasAll) return false; |
| 22 | |
| 23 | const widths = deriveCanonicalColumnWidths(newState.doc, node, pos); |
| 24 | if (!widths) return false; |
| 25 | |
| 26 | mutated = applyExplicitColumnWidths(newState, nextTr, pos, widths) || mutated; |
| 27 | return false; |
| 28 | }); |
| 29 | |
| 30 | if (!mutated) return null; |
| 31 | nextTr.setMeta("addToHistory", false); |
| 32 | return nextTr; |
| 33 | }, |
| 34 | }), |
| 35 | ]; |
| 36 | }, |
| 37 | }); |