Skip to content

Commit

Permalink
0.6.1
Browse files Browse the repository at this point in the history
  • Loading branch information
j0k3r committed Oct 14, 2016
1 parent f9d752c commit cedc228
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 15 deletions.
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@

0.6.1 / 2016-10-14
==================
* Table UI get distorted when Row or Column added

0.6.0 / 2016-10-13
==================
* Add restrict table inside table
Expand Down
49 changes: 36 additions & 13 deletions dist/js/medium-editor-tables.js
Original file line number Diff line number Diff line change
Expand Up @@ -325,21 +325,36 @@ Builder.prototype = {
}
},

getParentType: function (el, targetNode) {
var nodeName = el && el.nodeName ? el.nodeName.toLowerCase() : false;
if (!nodeName) {
return false;
}
while (nodeName && nodeName !== 'body') {
if (nodeName === targetNode) {
return el;
}
el = el.parentNode;
nodeName = el && el.nodeName ? el.nodeName.toLowerCase() : false;
}
},

addRow: function (before, e) {
e.preventDefault();
e.stopPropagation();
var tbody = this._range.parentNode.parentNode,
var tbody = this.getParentType(this._range, 'tbody'),
selectedTR = this.getParentType(this._range, 'tr'),
tr = this._doc.createElement('tr'),
td;
for (var i = 0; i < this._range.parentNode.childNodes.length; i++) {
for (var i = 0; i < selectedTR.childNodes.length; i++) {
td = this._doc.createElement('td');
td.appendChild(this._doc.createElement('br'));
tr.appendChild(td);
}
if (before !== true && this._range.parentNode.nextSibling) {
tbody.insertBefore(tr, this._range.parentNode.nextSibling);
if (before !== true && selectedTR.nextSibling) {
tbody.insertBefore(tr, selectedTR.nextSibling);
} else if (before === true) {
tbody.insertBefore(tr, this._range.parentNode);
tbody.insertBefore(tr, selectedTR);
} else {
tbody.appendChild(tr);
}
Expand All @@ -349,23 +364,27 @@ Builder.prototype = {
removeRow: function (e) {
e.preventDefault();
e.stopPropagation();
this._range.parentNode.parentNode.removeChild(this._range.parentNode);
var tbody = this.getParentType(this._range, 'tbody'),
selectedTR = this.getParentType(this._range, 'tr');
tbody.removeChild(selectedTR);
this.options.onClick(0, 0);
},

addColumn: function (before, e) {
e.preventDefault();
e.stopPropagation();
var cell = Array.prototype.indexOf.call(this._range.parentNode.childNodes, this._range),
tbody = this._range.parentNode.parentNode,
var selectedTR = this.getParentType(this._range, 'tr'),
selectedTD = this.getParentType(this._range, 'td'),
cell = Array.prototype.indexOf.call(selectedTR.childNodes, selectedTD),
tbody = this.getParentType(this._range, 'tbody'),
td;

for (var i = 0; i < tbody.childNodes.length; i++) {
td = this._doc.createElement('td');
td.appendChild(this._doc.createElement('br'));
if (before === true) {
tbody.childNodes[i].insertBefore(td, tbody.childNodes[i].childNodes[cell]);
} else if (this._range.parentNode.parentNode.childNodes[i].childNodes[cell].nextSibling) {
} else if (tbody.childNodes[i].childNodes[cell].nextSibling) {
tbody.childNodes[i].insertBefore(td, tbody.childNodes[i].childNodes[cell].nextSibling);
} else {
tbody.childNodes[i].appendChild(td);
Expand All @@ -378,8 +397,10 @@ Builder.prototype = {
removeColumn: function (e) {
e.preventDefault();
e.stopPropagation();
var cell = Array.prototype.indexOf.call(this._range.parentNode.childNodes, this._range),
tbody = this._range.parentNode.parentNode,
var selectedTR = this.getParentType(this._range, 'tr'),
selectedTD = this.getParentType(this._range, 'td'),
cell = Array.prototype.indexOf.call(selectedTR.childNodes, selectedTD),
tbody = this.getParentType(this._range, 'tbody'),
rows = tbody.childNodes.length;

for (var i = 0; i < rows; i++) {
Expand All @@ -391,8 +412,10 @@ Builder.prototype = {
removeTable: function (e) {
e.preventDefault();
e.stopPropagation();
var cell = Array.prototype.indexOf.call(this._range.parentNode.childNodes, this._range),
table = this._range.parentNode.parentNode.parentNode;
var selectedTR = this.getParentType(this._range, 'tr'),
selectedTD = this.getParentType(this._range, 'td'),
cell = Array.prototype.indexOf.call(selectedTR.childNodes, selectedTD),
table = this.getParentType(this._range, 'table');

table.parentNode.removeChild(table);
this.options.onClick(0, 0);
Expand Down
Loading

0 comments on commit cedc228

Please sign in to comment.