Skip to content

Commit

Permalink
feature/yabwe#19-add-table-head-support: adding thead to the table
Browse files Browse the repository at this point in the history
  • Loading branch information
antoniomm committed Sep 27, 2018
1 parent 6b72b23 commit 3843578
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions src/js/table.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,14 @@ Table.prototype = {
},

insert: function (rows, cols) {
var html = this._html(rows, cols);
var htmlHeader, html;
htmlHeader = this._html(0, cols, "header");
html = this._html(rows, cols);

this._editor.pasteHTML(
'<table class="medium-editor-table" id="medium-editor-table"' +
' width="100%">' +
'<thead>' + htmlHeader + '</thead>' +
'<tbody id="medium-editor-table-tbody">' +
html +
'</tbody>' +
Expand All @@ -39,10 +42,10 @@ Table.prototype = {
this._editor.checkSelection();
},

_html: function (rows, cols) {
_html: function (rows, cols, defaultText) {
var html = '',
x, y,
text = getSelectionText(this._doc);
text = (defaultText)? defaultText: getSelectionText(this._doc);

for (x = 0; x <= rows; x++) {
html += '<tr>';
Expand Down Expand Up @@ -71,17 +74,22 @@ Table.prototype = {
e.preventDefault();
e.stopPropagation();
table = this._getTableElements(el);
var tbody = this._getTBody(getParentOf(el, 'table'));
if (e.shiftKey) {
this._tabBackwards(el.previousSibling, table.row);
} else {
if (this._isLastCell(el, table.row, table.root)) {
this._insertRow(getParentOf(el, 'tbody'), table.row.cells.length);
this._insertRow(tbody, table.row.cells.length);
}
placeCaretAtNode(this._doc, el);
}
}
},

_getTBody: function (table) {
return table.querySelector('tbody');
},

_getTableElements: function (el) {
return {
cell: getParentOf(el, 'td'),
Expand Down Expand Up @@ -109,8 +117,8 @@ Table.prototype = {

_isLastCell: function (el, row, table) {
return (
(row.cells.length - 1) === el.cellIndex &&
(table.rows.length - 1) === row.rowIndex
(row.cells.length - 1) === el.cellIndex &&
(table.rows.length - 1) === row.rowIndex
);
},

Expand Down

0 comments on commit 3843578

Please sign in to comment.