diff --git a/dist/css/medium-editor-tables.css b/dist/css/medium-editor-tables.css index 20a7f54..d054756 100644 --- a/dist/css/medium-editor-tables.css +++ b/dist/css/medium-editor-tables.css @@ -32,5 +32,6 @@ resize: both; table-layout: fixed; } -.medium-editor-table, .medium-editor-table td { +.medium-editor-table, +.medium-editor-table td { border: 1px dashed #e3e3e3; } diff --git a/dist/js/medium-editor-tables.js b/dist/js/medium-editor-tables.js index c945fd3..851b5a3 100644 --- a/dist/js/medium-editor-tables.js +++ b/dist/js/medium-editor-tables.js @@ -383,7 +383,7 @@ MediumEditorTable.prototype = { this.table.insert(rows, columns); this.hideForm(); }.bind(this), - ownerDocument: this.base.options.ownerDocument, + ownerDocument: this.document, rows: this.options.rows, columns: this.options.columns }); diff --git a/dist/js/medium-editor-tables.min.js b/dist/js/medium-editor-tables.min.js index 8c6898f..9b15443 100644 --- a/dist/js/medium-editor-tables.min.js +++ b/dist/js/medium-editor-tables.min.js @@ -1 +1 @@ -!function(a,b){"use strict";"object"==typeof module?module.exports=b:"function"==typeof define&&define.amd?define(b):a.MediumEditorTable=b}(this,function(){"use strict";function a(a,b){var c;a=a||{};for(c in b)b.hasOwnProperty(c)&&!a.hasOwnProperty(c)&&(a[c]=b[c]);return a}function b(a){return a.getSelection?a.getSelection().toString():a.selection&&"Control"!==a.selection.type?a.selection.createRange().text:""}function c(a){var b=a.getSelection().anchorNode,c=b&&3===b.nodeType?b.parentNode:b;return c}function d(a,b,c){if(void 0!==a.getSelection&&b){var d=a.createRange(),e=a.getSelection();c?d.setStartBefore(b):d.setStartAfter(b),d.collapse(!0),e.removeAllRanges(),e.addRange(d)}}function e(a,b){if(!a)return!1;for(var c=a.parentNode,d=c.tagName.toLowerCase();"body"!==d;){if(d===b)return!0;if(c=c.parentNode,!c||!c.tagName)return!1;d=c.tagName.toLowerCase()}return!1}function f(a,b){var c=a&&a.tagName?a.tagName.toLowerCase():!1;if(!c)return!1;for(;c&&"body"!==c;){if(c===b)return a;a=a.parentNode,c=a&&a.tagName?a.tagName.toLowerCase():!1}}function g(a,b,c,d){return this.init(a,b,c,d)}function h(a){return this.init(a)}function i(a){return this.init(a)}function j(b){this.options=a(b,{columns:10,rows:10}),this.parent=!0,this.hasForm=!0,this.isFormVisible=!1,this.createButton()}g.prototype={init:function(a,b,c,d){return this._root=a,this._callback=b,this.rows=c,this.columns=d,this._render()},setCurrentCell:function(a){this._currentCell=a},markCells:function(){[].forEach.call(this._cellsElements,function(a){var b={column:parseInt(a.dataset.column,10),row:parseInt(a.dataset.row,10)},c=this._currentCell&&b.row<=this._currentCell.row&&b.column<=this._currentCell.column;c===!0?a.classList.add("active"):a.classList.remove("active")}.bind(this))},_generateCells:function(){this._cells=[];for(var a=0;a"},_cellsHTML:function(){var a="";return this._generateCells(),this._cells.map(function(b){a+='',a+=""}),a},_render:function(){this._root.innerHTML=this._html(),this._cellsElements=this._root.querySelectorAll("a"),this._bindEvents()},_bindEvents:function(){[].forEach.call(this._cellsElements,function(a){this._onMouseEnter(a),this._onClick(a)}.bind(this))},_onMouseEnter:function(a){var b,c=this;a.addEventListener("mouseenter",function(){clearTimeout(b);var a=this.dataset;b=setTimeout(function(){c._currentCell={column:parseInt(a.column,10),row:parseInt(a.row,10)},c.markCells()},50)})},_onClick:function(a){var b=this;a.addEventListener("click",function(a){a.preventDefault(),b._callback(this.dataset.row,this.dataset.column)})}},h.prototype={init:function(a){this.options=a,this._doc=a.ownerDocument||document,this._root=this._doc.createElement("div"),this._root.className="medium-editor-table-builder",this.grid=new g(this._root,this.options.onClick,this.options.rows,this.options.columns)},getElement:function(){return this._root},hide:function(){this._root.style.display="",this.grid.setCurrentCell({column:-1,row:-1}),this.grid.markCells()},show:function(a){this._root.style.display="block",this._root.style.left=a+"px"}};var k=9;return i.prototype={init:function(a){this._editor=a,this._doc=this._editor.options.ownerDocument,this._bindTabBehavior()},insert:function(a,b){var c=this._html(a,b);this._editor.pasteHTML(''+c+"
",{cleanAttrs:[],cleanTags:[]});var e=this._doc.getElementById("medium-editor-table");e.removeAttribute("id"),d(this._doc,e.querySelector("td"),!0)},_html:function(a,c){var d,e,f="",g=b(this._doc);for(d=0;a>=d;d++){for(f+="",e=0;c>=e;e++)f+=""+(0===d&&0===e?g:"
")+"";f+=""}return f},_bindTabBehavior:function(){var a=this;[].forEach.call(this._editor.elements,function(b){b.addEventListener("keydown",function(b){a._onKeyDown(b)})})},_onKeyDown:function(a){var b,g=c(this._doc);a.which===k&&e(g,"table")&&(a.preventDefault(),a.stopPropagation(),b=this._getTableElements(g),a.shiftKey?this._tabBackwards(g.previousSibling,b.row):(this._isLastCell(g,b.row,b.root)&&this._insertRow(f(g,"tbody"),b.row.cells.length),d(this._doc,g)))},_getTableElements:function(a){return{cell:f(a,"td"),row:f(a,"tr"),root:f(a,"table")}},_tabBackwards:function(a,b){a=a||this._getPreviousRowLastCell(b),d(this._doc,a,!0)},_insertRow:function(a,b){var c,d=document.createElement("tr"),e="";for(c=0;b>c;c+=1)e+="
";d.innerHTML=e,a.appendChild(d)},_isLastCell:function(a,b,c){return--b.cells.length==a.cellIndex&&--c.rows.length==b.rowIndex},_getPreviousRowLastCell:function(a){return a=a.previousSibling,a?a.cells[a.cells.length-1]:void 0}},j.prototype={createButton:function(){this._createButtonElement(),this._bindButtonClick()},isDisplayed:function(){return this.isFormVisible},getForm:function(){return this.builder||(this.builder=new h({onClick:function(a,b){this.table.insert(a,b),this.hideForm()}.bind(this),ownerDocument:this.base.options.ownerDocument,rows:this.options.rows,columns:this.options.columns}),this.table=new i(this.base)),this.builder.getElement()},getButton:function(){return"fontawesome"===this.base.options.buttonLabels&&(this.button.innerHTML=''),this.button},onHide:function(){this.hideForm()},hideForm:function(){this.isFormVisible=!1,this.builder.hide(),this.button.classList.remove("medium-editor-button-active")},show:function(){this.isFormVisible=!0,this.builder.show(this.button.offsetLeft),this.button.classList.add("medium-editor-button-active");for(var a=document.getElementsByClassName("medium-editor-table-builder-grid"),b=0;b"},_cellsHTML:function(){var a="";return this._generateCells(),this._cells.map(function(b){a+='',a+=""}),a},_render:function(){this._root.innerHTML=this._html(),this._cellsElements=this._root.querySelectorAll("a"),this._bindEvents()},_bindEvents:function(){[].forEach.call(this._cellsElements,function(a){this._onMouseEnter(a),this._onClick(a)}.bind(this))},_onMouseEnter:function(a){var b,c=this;a.addEventListener("mouseenter",function(){clearTimeout(b);var a=this.dataset;b=setTimeout(function(){c._currentCell={column:parseInt(a.column,10),row:parseInt(a.row,10)},c.markCells()},50)})},_onClick:function(a){var b=this;a.addEventListener("click",function(a){a.preventDefault(),b._callback(this.dataset.row,this.dataset.column)})}},h.prototype={init:function(a){this.options=a,this._doc=a.ownerDocument||document,this._root=this._doc.createElement("div"),this._root.className="medium-editor-table-builder",this.grid=new g(this._root,this.options.onClick,this.options.rows,this.options.columns)},getElement:function(){return this._root},hide:function(){this._root.style.display="",this.grid.setCurrentCell({column:-1,row:-1}),this.grid.markCells()},show:function(a){this._root.style.display="block",this._root.style.left=a+"px"}};var k=9;return i.prototype={init:function(a){this._editor=a,this._doc=this._editor.options.ownerDocument,this._bindTabBehavior()},insert:function(a,b){var c=this._html(a,b);this._editor.pasteHTML(''+c+"
",{cleanAttrs:[],cleanTags:[]});var e=this._doc.getElementById("medium-editor-table");e.removeAttribute("id"),d(this._doc,e.querySelector("td"),!0)},_html:function(a,c){var d,e,f="",g=b(this._doc);for(d=0;a>=d;d++){for(f+="",e=0;c>=e;e++)f+=""+(0===d&&0===e?g:"
")+"";f+=""}return f},_bindTabBehavior:function(){var a=this;[].forEach.call(this._editor.elements,function(b){b.addEventListener("keydown",function(b){a._onKeyDown(b)})})},_onKeyDown:function(a){var b,g=c(this._doc);a.which===k&&e(g,"table")&&(a.preventDefault(),a.stopPropagation(),b=this._getTableElements(g),a.shiftKey?this._tabBackwards(g.previousSibling,b.row):(this._isLastCell(g,b.row,b.root)&&this._insertRow(f(g,"tbody"),b.row.cells.length),d(this._doc,g)))},_getTableElements:function(a){return{cell:f(a,"td"),row:f(a,"tr"),root:f(a,"table")}},_tabBackwards:function(a,b){a=a||this._getPreviousRowLastCell(b),d(this._doc,a,!0)},_insertRow:function(a,b){var c,d=document.createElement("tr"),e="";for(c=0;b>c;c+=1)e+="
";d.innerHTML=e,a.appendChild(d)},_isLastCell:function(a,b,c){return--b.cells.length==a.cellIndex&&--c.rows.length==b.rowIndex},_getPreviousRowLastCell:function(a){return a=a.previousSibling,a?a.cells[a.cells.length-1]:void 0}},j.prototype={createButton:function(){this._createButtonElement(),this._bindButtonClick()},isDisplayed:function(){return this.isFormVisible},getForm:function(){return this.builder||(this.builder=new h({onClick:function(a,b){this.table.insert(a,b),this.hideForm()}.bind(this),ownerDocument:this.document,rows:this.options.rows,columns:this.options.columns}),this.table=new i(this.base)),this.builder.getElement()},getButton:function(){return"fontawesome"===this.base.options.buttonLabels&&(this.button.innerHTML=''),this.button},onHide:function(){this.hideForm()},hideForm:function(){this.isFormVisible=!1,this.builder.hide(),this.button.classList.remove("medium-editor-button-active")},show:function(){this.isFormVisible=!0,this.builder.show(this.button.offsetLeft),this.button.classList.add("medium-editor-button-active");for(var a=document.getElementsByClassName("medium-editor-table-builder-grid"),b=0;b