Skip to content

Commit

Permalink
Move inner table outside the parent table (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
luborepka committed Jul 21, 2023
1 parent e49c265 commit 914533f
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
3 changes: 3 additions & 0 deletions demo/script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ window.onload = () => {
imageResize: true,
table: false,
"better-table": {
table: {
tableInTableError: "This inner table was placed outside the parent table",
},
operationMenu: {
items: {
unmergeCells: {
Expand Down
2 changes: 1 addition & 1 deletion src/modules/better-table/quill-better-table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ export default class BetterTable extends Module {
// or pasting
quill.clipboard.addMatcher("td", matchTableCell);
quill.clipboard.addMatcher("th", matchTableHeader);
quill.clipboard.addMatcher("table", matchTable);
quill.clipboard.addMatcher("table", (node, delta) => matchTable(node, delta, quill, options));
quill.clipboard.addMatcher(Node.ELEMENT_NODE, (node, delta) => matchElement(quill, node, delta));
// quill.clipboard.addMatcher('h1, h2, h3, h4, h5, h6', matchHeader)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// @ts-nocheck

import Quill from "quill";
import { _omit, convertToHex } from "./index";
import { TableCellLine } from "../formats/table";
Expand Down Expand Up @@ -162,7 +164,20 @@ export function matchTableHeader(node, delta, scroll) {
}

// supplement colgroup and col
export function matchTable(node, delta, scroll) {
export function matchTable(node, delta, quill: Quill, options) {
const isBrokenTable = delta.ops.find((op) => "row" in op.attributes && "table-col" in op.attributes);
if (isBrokenTable) {
// delete broken tables from main table
const innerTables = Array.from(node.getElementsByTagName("table"));
const innerTablesHtml = innerTables
.map((table) => `<br /> ${options?.table?.tableInTableError ?? ""}` + table.outerHTML)
.join("");
innerTables.forEach((table) => table.parentNode.removeChild(table));

// append inner tables after main table
return quill.clipboard.convert({ html: node.outerHTML + innerTablesHtml });
}

let newColDelta = new Delta();
const topRow = node.querySelector("tr");

Expand Down

0 comments on commit 914533f

Please sign in to comment.