📄 kupubasetools.js
字号:
}; this.createImageHandler = function(event) { /* create an image according to a url entered in a popup */ var imageWindow = openPopup('kupupopups/image.html', 300, 200); imageWindow.imagetool = this; imageWindow.focus(); }; this.createImage = function(url, floatstyle) { var img = this.editor.getInnerDocument().createElement('img'); if (floatstyle) { img.style.cssFloat = floatstyle; }; img.setAttribute('src', url); img = this.editor.insertNodeAtSelection(img, 1); this.editor.logMessage('Image inserted'); this.editor.updateState(); return img; }; this.createContextMenuElements = function(selNode, event) { return new Array(new ContextMenuElement('Create image', this.createImageHandler, this)); };}ImageTool.prototype = new KupuTool;function ImageToolBox(inputfieldid, insertbuttonid, floatselectid, toolboxid, plainclass, activeclass) { /* toolbox for adding images */ this.inputfield = document.getElementById(inputfieldid); this.insertbutton = document.getElementById(insertbuttonid); this.floatselect = document.getElementById(floatselectid); this.toolboxel = document.getElementById(toolboxid); this.plainclass = plainclass; this.activeclass = activeclass; this.initialize = function(tool, editor) { this.tool = tool; this.editor = editor; addEventHandler(this.insertbutton, "click", this.addImage, this); }; this.updateState = function(selNode, event) { /* update the state of the toolbox element */ var imageel = this.editor.getNearestParentOfType(selNode, 'img'); if (imageel) { // check first before setting a class for backward compatibility if (this.toolboxel) { this.toolboxel.className = this.activeclass; this.inputfield.value = imageel.getAttribute('src'); var floatstyle = imageel.style.cssFloat ? imageel.style.cssFloat : 'left'; selectSelectItem(this.floatselect, floatstyle); }; } else { if (this.toolboxel) { this.toolboxel.className = this.plainclass; }; }; }; this.addImage = function() { /* add an image */ var url = this.inputfield.value; var floatstyle = this.floatselect.options[this.floatselect.selectedIndex].value; this.tool.createImage(url, floatstyle); };};ImageToolBox.prototype = new KupuToolBox;function TableTool() { /* The table tool */ // XXX There are some awfully long methods in here!! this.createContextMenuElements = function(selNode, event) { var table = this.editor.getNearestParentOfType(selNode, 'table'); if (!table) { ret = new Array(); var el = new ContextMenuElement('Add table', this.addPlainTable, this); ret.push(el); return ret; } else { var ret = new Array(); ret.push(new ContextMenuElement('Add row', this.addTableRow, this)); ret.push(new ContextMenuElement('Delete row', this.delTableRow, this)); ret.push(new ContextMenuElement('Add column', this.addTableColumn, this)); ret.push(new ContextMenuElement('Delete column', this.delTableColumn, this)); ret.push(new ContextMenuElement('Delete Table', this.delTable, this)); return ret; }; }; this.addPlainTable = function() { /* event handler for the context menu */ this.createTable(2, 3, 1, 'plain'); }; this.createTable = function(rows, cols, makeHeader, tableclass) { /* add a table */ var doc = this.editor.getInnerDocument(); table = doc.createElement("table"); table.setAttribute("border", "1"); table.setAttribute("cellpadding", "8"); table.setAttribute("cellspacing", "2"); table.setAttribute("class", tableclass); // If the user wants a row of headings, make them if (makeHeader) { var tr = doc.createElement("tr"); var thead = doc.createElement("thead"); for (i=0; i < cols; i++) { var th = doc.createElement("th"); th.appendChild(doc.createTextNode("Col " + i+1)); tr.appendChild(th); } thead.appendChild(tr); table.appendChild(thead); } tbody = doc.createElement("tbody"); for (var i=0; i < rows; i++) { var tr = doc.createElement("tr"); for (var j=0; j < cols; j++) { var td = doc.createElement("td"); var content = doc.createTextNode('\u00a0'); td.appendChild(content); tr.appendChild(td); } tbody.appendChild(tr); } table.appendChild(tbody); this.editor.insertNodeAtSelection(table); this._setTableCellHandlers(table); this.editor.logMessage('Table added'); return table; }; this._setTableCellHandlers = function(table) { // make each cell select its full contents if it's clicked var cells = table.getElementsByTagName('td'); for (var i=0; i < cells.length; i++) { addEventHandler(cells[i], 'click', this._selectContentIfEmpty, this); }; // select the nbsp in the first cell var firstcell = cells[0]; if (firstcell) { var children = firstcell.childNodes; if (children.length == 1 && children[0].nodeType == 3 && children[0].nodeValue == '\xa0') { var selection = this.editor.getSelection(); selection.selectNodeContents(firstcell); }; }; }; this._selectContentIfEmpty = function() { var selNode = this.editor.getSelectedNode(); var cell = this.editor.getNearestParentOfType(selNode, 'td'); if (!cell) { return; }; var children = cell.childNodes; if (children.length == 1 && children[0].nodeType == 3 && children[0].nodeValue == '\xa0') { var selection = this.editor.getSelection(); selection.selectNodeContents(cell); }; }; this.addTableRow = function() { /* Find the current row and add a row after it */ var currnode = this.editor.getSelectedNode(); var currtbody = this.editor.getNearestParentOfType(currnode, "TBODY"); var bodytype = "tbody"; if (!currtbody) { currtbody = this.editor.getNearestParentOfType(currnode, "THEAD"); bodytype = "thead"; } var parentrow = this.editor.getNearestParentOfType(currnode, "TR"); var nextrow = parentrow.nextSibling; // get the number of cells we should place var colcount = 0; for (var i=0; i < currtbody.childNodes.length; i++) { var el = currtbody.childNodes[i]; if (el.nodeType != 1) { continue; } if (el.nodeName.toLowerCase() == 'tr') { var cols = 0; for (var j=0; j < el.childNodes.length; j++) { if (el.childNodes[j].nodeType == 1) { cols++; } } if (cols > colcount) { colcount = cols; } } } var newrow = this.editor.getInnerDocument().createElement("TR"); for (var i = 0; i < colcount; i++) { var newcell; if (bodytype == 'tbody') { newcell = this.editor.getInnerDocument().createElement("TD"); } else { newcell = this.editor.getInnerDocument().createElement("TH"); } var newcellvalue = this.editor.getInnerDocument().createTextNode("\u00a0"); newcell.appendChild(newcellvalue); newrow.appendChild(newcell); } if (!nextrow) { currtbody.appendChild(newrow); } else { currtbody.insertBefore(newrow, nextrow); } this.editor.logMessage('Table row added'); }; this.delTableRow = function() { /* Find the current row and delete it */ var currnode = this.editor.getSelectedNode(); var parentrow = this.editor.getNearestParentOfType(currnode, "TR"); if (!parentrow) { this.editor.logMessage('No row to delete', 1); return; } // remove the row parentrow.parentNode.removeChild(parentrow); this.editor.logMessage('Table row removed'); }; this.addTableColumn = function() { /* Add a new column after the current column */ var currnode = this.editor.getSelectedNode(); var currtd = this.editor.getNearestParentOfType(currnode, 'TD'); if (!currtd) { currtd = this.editor.getNearestParentOfType(currnode, 'TH'); } if (!currtd) { this.editor.logMessage('No parentcolumn found!', 1); return; } var currtr = this.editor.getNearestParentOfType(currnode, 'TR'); var currtable = this.editor.getNearestParentOfType(currnode, 'TABLE'); // get the current index var tdindex = this._getColIndex(currtd); this.editor.logMessage('tdindex: ' + tdindex); // now add a column to all rows // first the thead var theads = currtable.getElementsByTagName('THEAD'); if (theads) { for (var i=0; i < theads.length; i++) { // let's assume table heads only have ths var currthead = theads[i]; for (var j=0; j < currthead.childNodes.length; j++) { var tr = currthead.childNodes[j]; if (tr.nodeType != 1) { continue; } var currindex = 0; for (var k=0; k < tr.childNodes.length; k++) { var th = tr.childNodes[k]; if (th.nodeType != 1) { continue; } if (currindex == tdindex) { var doc = this.editor.getInnerDocument(); var newth = doc.createElement('th'); var text = doc.createTextNode('\u00a0'); newth.appendChild(text); if (tr.childNodes.length == k+1) { // the column will be on the end of the row tr.appendChild(newth); } else { tr.insertBefore(newth, tr.childNodes[k + 1]); } break; } currindex++; } } } } // then the tbody var tbodies = currtable.getElementsByTagName('TBODY'); if (tbodies) { for (var i=0; i < tbodies.length; i++) { // let's assume table heads only have ths var currtbody = tbodies[i]; for (var j=0; j < currtbody.childNodes.length; j++) { var tr = currtbody.childNodes[j]; if (tr.nodeType != 1) { continue; } var currindex = 0; for (var k=0; k < tr.childNodes.length; k++) { var td = tr.childNodes[k]; if (td.nodeType != 1) { continue; } if (currindex == tdindex) { var doc = this.editor.getInnerDocument(); var newtd = doc.createElement('td'); var text = doc.createTextNode('\u00a0'); newtd.appendChild(text); if (tr.childNodes.length == k+1) { // the column will be on the end of the row tr.appendChild(newtd); } else { tr.insertBefore(newtd, tr.childNodes[k + 1]); } break; } currindex++; } } } } this.editor.logMessage('Table column added'); }; this.delTableColumn = function() { /* remove a column */ var currnode = this.editor.getSelectedNode(); var currtd = this.editor.getNearestParentOfType(currnode, 'TD'); if (!currtd) { currtd = this.editor.getNearestParentOfType(currnode, 'TH'); } var currcolindex = this._getColIndex(currtd); var currtable = this.editor.getNearestParentOfType(currnode, 'TABLE'); // remove the theaders var heads = currtable.getElementsByTagName('THEAD'); if (heads.length) { for (var i=0; i < heads.length; i++) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -