Skip to content

Commit

Permalink
add and use table helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
jhchen committed Jul 10, 2018
1 parent 9a65a80 commit 1cf476d
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 11 deletions.
45 changes: 40 additions & 5 deletions formats/table.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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';
Expand Down Expand Up @@ -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';
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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';
Expand Down
11 changes: 5 additions & 6 deletions modules/table.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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;
}
Expand All @@ -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) {
Expand Down

0 comments on commit 1cf476d

Please sign in to comment.