📄 table-operations.js
字号:
} else { newCell.className = element.className; newCell.removeAttribute("className"); } } while (element.firstChild) newCell.appendChild(element.firstChild); p.insertBefore(newCell, element); p.removeChild(element); dialog.editor.selectNodeContents(newCell, false); } dialog.editor.updateToolbar(); });};/* * this function gets called when some button from the TableOperations toolbar was pressed. */TableOperations.prototype.buttonPress = function(editor,button_id) { this.editor = editor; var mozbr = HTMLArea.is_gecko ? "<br />" : ""; var tableParts = ["tfoot", "thead", "tbody"]; var tablePartsIndex = { tfoot : 0, thead : 1, tbody : 2 }; // helper function that clears the content in a table row function clearRow(tr) { var tds = tr.getElementsByTagName("td"); for (var i = tds.length; --i >= 0;) { var td = tds[i]; td.rowSpan = 1; td.innerHTML = mozbr; } var tds = tr.getElementsByTagName("th"); for (var i = tds.length; --i >= 0;) { var td = tds[i]; td.rowSpan = 1; td.innerHTML = mozbr; } }; function splitRow(td) { var n = parseInt("" + td.rowSpan); var colSpan = td.colSpan; var tagName = td.tagName.toLowerCase(); td.rowSpan = 1; var tr = td.parentNode; var sectionRowIndex = tr.sectionRowIndex; var rows = tr.parentNode.rows; var index = td.cellIndex; while (--n > 0) { tr = rows[++sectionRowIndex]; // Last row if (!tr) tr = td.parentNode.parentNode.appendChild(editor._doc.createElement("tr")); var otd = editor._doc.createElement(tagName); otd.colSpan = colSpan; otd.innerHTML = mozbr; tr.insertBefore(otd, tr.cells[index]); } }; function splitCol(td) { var nc = parseInt("" + td.colSpan); var tagName = td.tagName.toLowerCase(); td.colSpan = 1; var tr = td.parentNode; var ref = td.nextSibling; while (--nc > 0) { var otd = editor._doc.createElement(tagName); otd.rowSpan = td.rowSpan; otd.innerHTML = mozbr; tr.insertBefore(otd, ref); } }; function splitCell(td) { var nc = parseInt("" + td.colSpan); splitCol(td); var cells = td.parentNode.cells; var index = td.cellIndex; while (nc-- > 0) { splitRow(cells[index++]); } }; function selectNextNode(el) { var node = el.nextSibling; while (node && node.nodeType != 1) { node = node.nextSibling; } if (!node) { node = el.previousSibling; while (node && node.nodeType != 1) { node = node.previousSibling; } } if (!node) node = el.parentNode; editor.selectNodeContents(node); }; function getSelectedCells(sel) { var cell, range, i = 0, cells = []; try { while (range = sel.getRangeAt(i++)) { cell = range.startContainer.childNodes[range.startOffset]; while (!/^(td|th|body)$/.test(cell.tagName.toLowerCase())) cell = cell.parentNode; if (/^(td|th)$/.test(cell.tagName.toLowerCase())) cells.push(cell); } } catch(e) { /* finished walking through selection */ } return cells; }; function deleteEmptyTable(table) { var lastPart = true; for (var j = tableParts.length; --j >= 0;) { var tablePart = table.getElementsByTagName(tableParts[j])[0]; if (tablePart) lastPart = false; } if (lastPart) { selectNextNode(table); table.parentNode.removeChild(table); } }; switch (button_id) { // ROWS case "TO-row-insert-above": case "TO-row-insert-under": var tr = this.getClosest("tr"); if (!tr) break; var otr = tr.cloneNode(true); clearRow(otr); tr.parentNode.insertBefore(otr, (/under/.test(button_id) ? tr.nextSibling : tr)); editor.forceRedraw(); editor.focusEditor(); break; case "TO-row-delete": var tr = this.getClosest("tr"); if (!tr) break; var part = tr.parentNode; var table = part.parentNode; if(part.rows.length == 1) { // this the last row, delete the whole table part selectNextNode(part); table.removeChild(part); deleteEmptyTable(table); } else { // set the caret first to a position that doesn't disappear. selectNextNode(tr); part.removeChild(tr); } editor.forceRedraw(); editor.focusEditor(); editor.updateToolbar(); break; case "TO-row-split": var cell = this.getClosest("td"); if (!cell) var cell = this.getClosest("th"); if (!cell) break; var sel = editor._getSelection(); if (HTMLArea.is_gecko && !sel.isCollapsed && !HTMLArea.is_safari && !HTMLArea.is_opera) { var cells = getSelectedCells(sel); for (i = 0; i < cells.length; ++i) splitRow(cells[i]); } else { splitRow(cell); } editor.forceRedraw(); editor.updateToolbar(); break; // COLUMNS case "TO-col-insert-before": case "TO-col-insert-after": var cell = this.getClosest("td"); if (!cell) var cell = this.getClosest("th"); if (!cell) break; var index = cell.cellIndex; var table = cell.parentNode.parentNode.parentNode; for (var j = tableParts.length; --j >= 0;) { var tablePart = table.getElementsByTagName(tableParts[j])[0]; if (tablePart) { var rows = tablePart.rows; for (var i = rows.length; --i >= 0;) { var tr = rows[i]; var ref = tr.cells[index + (/after/.test(button_id) ? 1 : 0)]; if (!ref) { var otd = editor._doc.createElement(tr.lastChild.tagName.toLowerCase()); otd.innerHTML = mozbr; tr.appendChild(otd); } else { var otd = editor._doc.createElement(ref.tagName.toLowerCase()); otd.innerHTML = mozbr; tr.insertBefore(otd, ref); } } } } editor.focusEditor(); break; case "TO-col-split": var cell = this.getClosest("td"); if (!cell) var cell = this.getClosest("th"); if (!cell) break; var sel = editor._getSelection(); if (HTMLArea.is_gecko && !sel.isCollapsed && !HTMLArea.is_safari && !HTMLArea.is_opera) { var cells = getSelectedCells(sel); for (i = 0; i < cells.length; ++i) splitCol(cells[i]); } else { splitCol(cell); } editor.forceRedraw(); editor.updateToolbar(); break; case "TO-col-delete": var cell = this.getClosest("td"); if (!cell) var cell = this.getClosest("th"); if (!cell) break; var index = cell.cellIndex; var part = cell.parentNode.parentNode; var table = part.parentNode; var lastPart = true; for (var j = tableParts.length; --j >= 0;) { var tablePart = table.getElementsByTagName(tableParts[j])[0]; if (tablePart) { var rows = tablePart.rows; var lastColumn = true; for (var i = rows.length; --i >= 0;) { if(rows[i].cells.length > 1) lastColumn = false; } if (lastColumn) { // this is the last column, delete the whole tablepart // set the caret first to a position that doesn't disappear selectNextNode(tablePart); table.removeChild(tablePart); } else { // set the caret first to a position that doesn't disappear if (part == tablePart) selectNextNode(cell); for (var i = rows.length; --i >= 0;) { if(rows[i].cells[index]) rows[i].removeChild(rows[i].cells[index]); } lastPart = false; } } } if (lastPart) { // the last table section was deleted: delete the whole table // set the caret first to a position that doesn't disappear selectNextNode(table); table.parentNode.removeChild(table); } editor.forceRedraw(); editor.focusEditor(); editor.updateToolbar(); break; // CELLS case "TO-cell-split": var cell = this.getClosest("td"); if (!cell) var cell = this.getClosest("th"); if (!cell) break; var sel = editor._getSelection(); if (HTMLArea.is_gecko && !sel.isCollapsed && !HTMLArea.is_safari && !HTMLArea.is_opera) { var cells = getSelectedCells(sel); for (i = 0; i < cells.length; ++i) splitCell(cells[i]); } else { splitCell(cell); } editor.forceRedraw(); editor.updateToolbar(); break; case "TO-cell-insert-before": case "TO-cell-insert-after": var cell = this.getClosest("td"); if (!cell) var cell = this.getClosest("th"); if (!cell) break; var tr = cell.parentNode; var otd = editor._doc.createElement(cell.tagName.toLowerCase()); otd.innerHTML = mozbr; tr.insertBefore(otd, (/after/.test(button_id) ? cell.nextSibling : cell)); editor.forceRedraw(); editor.focusEditor(); break; case "TO-cell-delete": var cell = this.getClosest("td"); if (!cell) var cell = this.getClosest("th"); if (!cell) break; var row = cell.parentNode; if(row.cells.length == 1) { // this is the only cell in the row, delete the row var part = row.parentNode; var table = part.parentNode; if (part.rows.length == 1) { // this the last row, delete the whole table part selectNextNode(part); table.removeChild(part); deleteEmptyTable(table); } else { selectNextNode(row); part.removeChild(row); } } else { // set the caret first to a position that doesn't disappear selectNextNode(cell); row.removeChild(cell); } editor.forceRedraw(); editor.focusEditor(); editor.updateToolbar(); break; case "TO-cell-merge": var sel = editor._getSelection(); var range, i = 0; var rows = new Array(); for (var k = tableParts.length; --k >= 0;) rows[k] = []; var row = null; var cells = null; if (HTMLArea.is_gecko && !HTMLArea.is_safari && !HTMLArea.is_opera) { try { while (range = sel.getRangeAt(i++)) { var td = range.startContainer.childNodes[range.startOffset]; if (td.parentNode != row) { (cells) && rows[tablePartsIndex[row.parentNode.tagName.toLowerCase()]].push(cells); row = td.parentNode; cells = []; } cells.push(td); } } catch(e) { /* finished walking through selection */ } rows[tablePartsIndex[row.parentNode.tagName.toLowerCase()]].push(cells); } else { // Internet Explorer, Safari and Opera var cell = this.getClosest("td"); if (!cell) var cell = this.getClosest("th"); if (!cell) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -