diff --git a/formats/table.js b/formats/table.js index 38d1d8a48e..2f8586c3cd 100644 --- a/formats/table.js +++ b/formats/table.js @@ -19,6 +19,13 @@ class TableCell extends Block { return undefined; } + cellOffset() { + if (this.parent) { + return this.parent.children.indexOf(this); + } + return -1; + } + format(name, value) { if (name === TableCell.blotName && value) { this.domNode.setAttribute('data-row', value); @@ -27,12 +34,19 @@ class TableCell extends Block { } } - table() { - let cur = this.parent; - while (cur != null && cur.statics.blotName !== 'table-container') { - cur = cur.parent; + row() { + return this.parent; + } + + rowOffset() { + if (this.row()) { + return this.row().rowOffset(); } - return cur; + return -1; + } + + table() { + return this.row() && this.row().table(); } } TableCell.blotName = 'table'; @@ -72,6 +86,17 @@ class TableRow extends Container { } }); } + + rowOffset() { + if (this.parent) { + return this.parent.children.indexOf(this); + } + return -1; + } + + table() { + return this.parent && this.parent.parent; + } } TableRow.blotName = 'table-row'; TableRow.tagName = 'TR'; @@ -99,6 +124,10 @@ class TableContainer extends Container { }); } + cells(column) { + return this.rows().map(row => row.children.at(column)); + } + deleteColumn(index) { const [body] = this.descendant(TableBody); if (body == null || body.children.head == null) return; @@ -133,6 +162,12 @@ class TableContainer extends Container { const ref = body.children.at(index); body.insertBefore(row, ref); } + + rows() { + const body = this.children.head; + if (body == null) return []; + return body.children.map(row => row); + } } TableContainer.blotName = 'table-container'; TableContainer.tagName = 'TABLE'; diff --git a/modules/table.js b/modules/table.js index 98510db64e..7eded863f4 100644 --- a/modules/table.js +++ b/modules/table.js @@ -29,10 +29,9 @@ class Table extends Module { } deleteColumn() { - const [table, row, cell] = this.getTable(); + const [table, , cell] = this.getTable(); if (cell == null) return; - const column = row.children.indexOf(cell); - table.deleteColumn(column); + table.deleteColumn(cell.cellOffset()); this.quill.update(Quill.sources.USER); } @@ -67,10 +66,10 @@ class Table extends Module { const range = this.quill.getSelection(); const [table, row, cell] = this.getTable(range); if (cell == null) return; - const column = row.children.offset(cell); + const column = cell.cellOffset(); table.insertColumn(column + offset); this.quill.update(Quill.sources.USER); - let shift = row.parent.children.indexOf(row); + let shift = row.rowOffset(); if (offset === 0) { shift += 1; } @@ -93,7 +92,7 @@ class Table extends Module { const range = this.quill.getSelection(); const [table, row, cell] = this.getTable(range); if (cell == null) return; - const index = row.parent.children.indexOf(row); + const index = row.rowOffset(); table.insertRow(index + offset); this.quill.update(Quill.sources.USER); if (offset > 0) {