📄 editor_plugin_src.js
字号:
/**
* $RCSfile: editor_plugin_src.js,v $
* $Revision: 1.38 $
* $Date: 2006/02/11 18:53:51 $
*
* @author Moxiecode
* @copyright Copyright ?2004-2006, Moxiecode Systems AB, All rights reserved.
*/
/* Import plugin specific language pack */
tinyMCE.importPluginLanguagePack('table', 'en,tr,ar,cs,da,de,el,es,fi,fr_ca,hu,it,ja,ko,nl,nb,pl,pt,pt_br,sv,tw,zh_cn,zh_cn_utf8,fr,de,he,nb,ru,ru_KOI8-R,ru_UTF-8,nn,cy,is,zh_tw,zh_tw_utf8,sk');
var TinyMCE_TablePlugin = {
getInfo : function() {
return {
longname : 'Tables',
author : 'Moxiecode Systems',
authorurl : 'http://tinymce.moxiecode.com',
infourl : 'http://tinymce.moxiecode.com/tinymce/docs/plugin_table.html',
version : tinyMCE.majorVersion + "." + tinyMCE.minorVersion
};
},
initInstance : function(inst) {
if (tinyMCE.isGecko) {
var doc = inst.getDoc();
tinyMCE.addEvent(doc, "mouseup", TinyMCE_TablePlugin._mouseDownHandler);
}
inst.tableRowClipboard = null;
},
/**
* Returns the HTML contents of the table control.
*/
getControlHTML : function(control_name) {
var controls = new Array(
['table', 'table.gif', 'lang_table_desc', 'mceInsertTable', true],
['delete_col', 'table_delete_col.gif', 'lang_table_delete_col_desc', 'mceTableDeleteCol'],
['delete_row', 'table_delete_row.gif', 'lang_table_delete_row_desc', 'mceTableDeleteRow'],
['col_after', 'table_insert_col_after.gif', 'lang_table_col_after_desc', 'mceTableInsertColAfter'],
['col_before', 'table_insert_col_before.gif', 'lang_table_col_before_desc', 'mceTableInsertColBefore'],
['row_after', 'table_insert_row_after.gif', 'lang_table_row_after_desc', 'mceTableInsertRowAfter'],
['row_before', 'table_insert_row_before.gif', 'lang_table_row_before_desc', 'mceTableInsertRowBefore'],
['row_props', 'table_row_props.gif', 'lang_table_row_desc', 'mceTableRowProps', true],
['cell_props', 'table_cell_props.gif', 'lang_table_cell_desc', 'mceTableCellProps', true],
['split_cells', 'table_split_cells.gif', 'lang_table_split_cells_desc', 'mceTableSplitCells', true],
['merge_cells', 'table_merge_cells.gif', 'lang_table_merge_cells_desc', 'mceTableMergeCells', true]);
// Render table control
for (var i=0; i<controls.length; i++) {
var but = controls[i];
var cmd = 'tinyMCE.execInstanceCommand(\'{$editor_id}\',\'' + but[3] + '\', ' + (but.length > 4 ? but[4] : false) + (but.length > 5 ? ', \'' + but[5] + '\'' : '') + ');return false;';
if (but[0] == control_name)
return tinyMCE.getButtonHTML(control_name, but[2], '{$pluginurl}/images/'+ but[1], but[3], (but.length > 4 ? but[4] : false));
}
// Special tablecontrols
if (control_name == "tablecontrols") {
var html = "";
html += tinyMCE.getControlHTML("table");
html += tinyMCE.getControlHTML("separator");
html += tinyMCE.getControlHTML("row_props");
html += tinyMCE.getControlHTML("cell_props");
html += tinyMCE.getControlHTML("separator");
html += tinyMCE.getControlHTML("row_before");
html += tinyMCE.getControlHTML("row_after");
html += tinyMCE.getControlHTML("delete_row");
html += tinyMCE.getControlHTML("separator");
html += tinyMCE.getControlHTML("col_before");
html += tinyMCE.getControlHTML("col_after");
html += tinyMCE.getControlHTML("delete_col");
html += tinyMCE.getControlHTML("separator");
html += tinyMCE.getControlHTML("split_cells");
html += tinyMCE.getControlHTML("merge_cells");
return html;
}
return "";
},
/**
* Executes the table commands.
*/
execCommand : function(editor_id, element, command, user_interface, value) {
// Is table command
switch (command) {
case "mceInsertTable":
case "mceTableRowProps":
case "mceTableCellProps":
case "mceTableSplitCells":
case "mceTableMergeCells":
case "mceTableInsertRowBefore":
case "mceTableInsertRowAfter":
case "mceTableDeleteRow":
case "mceTableInsertColBefore":
case "mceTableInsertColAfter":
case "mceTableDeleteCol":
case "mceTableCutRow":
case "mceTableCopyRow":
case "mceTablePasteRowBefore":
case "mceTablePasteRowAfter":
case "mceTableDelete":
var inst = tinyMCE.getInstanceById(editor_id);
inst.execCommand('mceBeginUndoLevel');
TinyMCE_TablePlugin._doExecCommand(editor_id, element, command, user_interface, value);
inst.execCommand('mceEndUndoLevel');
return true;
}
// Pass to next handler in chain
return false;
},
handleNodeChange : function(editor_id, node, undo_index, undo_levels, visual_aid, any_selection) {
var colspan = "1", rowspan = "1";
var inst = tinyMCE.getInstanceById(editor_id);
// Reset table controls
tinyMCE.switchClass(editor_id + '_table', 'mceButtonNormal');
tinyMCE.switchClass(editor_id + '_row_props', 'mceButtonDisabled');
tinyMCE.switchClass(editor_id + '_cell_props', 'mceButtonDisabled');
tinyMCE.switchClass(editor_id + '_row_before', 'mceButtonDisabled');
tinyMCE.switchClass(editor_id + '_row_after', 'mceButtonDisabled');
tinyMCE.switchClass(editor_id + '_delete_row', 'mceButtonDisabled');
tinyMCE.switchClass(editor_id + '_col_before', 'mceButtonDisabled');
tinyMCE.switchClass(editor_id + '_col_after', 'mceButtonDisabled');
tinyMCE.switchClass(editor_id + '_delete_col', 'mceButtonDisabled');
tinyMCE.switchClass(editor_id + '_split_cells', 'mceButtonDisabled');
tinyMCE.switchClass(editor_id + '_merge_cells', 'mceButtonDisabled');
// Within a td element
if (tdElm = tinyMCE.getParentElement(node, "td,th")) {
tinyMCE.switchClass(editor_id + '_cell_props', 'mceButtonSelected');
tinyMCE.switchClass(editor_id + '_row_before', 'mceButtonNormal');
tinyMCE.switchClass(editor_id + '_row_after', 'mceButtonNormal');
tinyMCE.switchClass(editor_id + '_delete_row', 'mceButtonNormal');
tinyMCE.switchClass(editor_id + '_col_before', 'mceButtonNormal');
tinyMCE.switchClass(editor_id + '_col_after', 'mceButtonNormal');
tinyMCE.switchClass(editor_id + '_delete_col', 'mceButtonNormal');
colspan = tinyMCE.getAttrib(tdElm, "colspan");
rowspan = tinyMCE.getAttrib(tdElm, "rowspan");
colspan = colspan == "" ? "1" : colspan;
rowspan = rowspan == "" ? "1" : rowspan;
if (colspan != "1" || rowspan != "1")
tinyMCE.switchClass(editor_id + '_split_cells', 'mceButtonNormal');
}
// Within a tr element
if (tinyMCE.getParentElement(node, "tr"))
tinyMCE.switchClass(editor_id + '_row_props', 'mceButtonSelected');
// Within table
if (tinyMCE.getParentElement(node, "table")) {
tinyMCE.switchClass(editor_id + '_table', 'mceButtonSelected');
tinyMCE.switchClass(editor_id + '_merge_cells', 'mceButtonNormal');
}
},
// Private plugin internal methods
_mouseDownHandler : function(e) {
var elm = tinyMCE.isMSIE ? event.srcElement : e.target;
var focusElm = tinyMCE.selectedInstance.getFocusElement();
// If press on special Mozilla create TD/TR thingie
if (elm.nodeName == "BODY" && (focusElm.nodeName == "TD" || focusElm.nodeName == "TH" || (focusElm.parentNode && focusElm.parentNode.nodeName == "TD") ||(focusElm.parentNode && focusElm.parentNode.nodeName == "TH") )) {
window.setTimeout(function() {
var tableElm = tinyMCE.getParentElement(focusElm, "table");
tinyMCE.handleVisualAid(tableElm, true, tinyMCE.settings['visual'], tinyMCE.selectedInstance);
}, 10);
}
},
/**
* Executes the table commands.
*/
_doExecCommand : function(editor_id, element, command, user_interface, value) {
var inst = tinyMCE.getInstanceById(editor_id);
var focusElm = inst.getFocusElement();
var trElm = tinyMCE.getParentElement(focusElm, "tr");
var tdElm = tinyMCE.getParentElement(focusElm, "td,th");
var tableElm = tinyMCE.getParentElement(focusElm, "table");
var doc = inst.contentWindow.document;
var tableBorder = tableElm ? tableElm.getAttribute("border") : "";
// Get first TD if no TD found
if (trElm && tdElm == null)
tdElm = trElm.cells[0];
// ------- Inner functions ---------
function inArray(ar, v) {
for (var i=0; i<ar.length; i++) {
// Is array
if (ar[i].length > 0 && inArray(ar[i], v))
return true;
// Found value
if (ar[i] == v)
return true;
}
return false;
}
function makeTD() {
var newTD = doc.createElement("td");
newTD.innerHTML = " ";
}
function getColRowSpan(td) {
var colspan = tinyMCE.getAttrib(td, "colspan");
var rowspan = tinyMCE.getAttrib(td, "rowspan");
colspan = colspan == "" ? 1 : parseInt(colspan);
rowspan = rowspan == "" ? 1 : parseInt(rowspan);
return {colspan : colspan, rowspan : rowspan};
}
function getCellPos(grid, td) {
for (var y=0; y<grid.length; y++) {
for (var x=0; x<grid[y].length; x++) {
if (grid[y][x] == td)
return {cellindex : x, rowindex : y};
}
}
return null;
}
function getCell(grid, row, col) {
if (grid[row] && grid[row][col])
return grid[row][col];
return null;
}
function getTableGrid(table) {
var grid = new Array();
var rows = table.rows;
for (var y=0; y<rows.length; y++) {
for (var x=0; x<rows[y].cells.length; x++) {
var td = rows[y].cells[x];
var sd = getColRowSpan(td);
// All ready filled
for (xstart = x; grid[y] && grid[y][xstart]; xstart++) ;
// Fill box
for (var y2=y; y2<y+sd['rowspan']; y2++) {
if (!grid[y2])
grid[y2] = new Array();
for (var x2=xstart; x2<xstart+sd['colspan']; x2++) {
grid[y2][x2] = td;
}
}
}
}
return grid;
}
function trimRow(table, tr, td, new_tr) {
var grid = getTableGrid(table);
var cpos = getCellPos(grid, td);
// Time to crop away some
if (new_tr.cells.length != tr.childNodes.length) {
var cells = tr.childNodes;
var lastElm = null;
for (var x=0; td = getCell(grid, cpos.rowindex, x); x++) {
var remove = true;
var sd = getColRowSpan(td);
// Remove due to rowspan
if (inArray(cells, td)) {
new_tr.childNodes[x]._delete = true;
} else if ((lastElm == null || td != lastElm) && sd.colspan > 1) { // Remove due to colspan
for (var i=x; i<x+td.colSpan; i++)
new_tr.childNodes[i]._delete = true;
}
if ((lastElm == null || td != lastElm) && sd.rowspan > 1)
td.rowSpan = sd.rowspan + 1;
lastElm = td;
}
deleteMarked(tableElm);
}
}
function prevElm(node, name) {
while ((node = node.previousSibling) != null) {
if (node.nodeName == name)
return node;
}
return null;
}
function nextElm(node, names) {
var namesAr = names.split(',');
while ((node = node.nextSibling) != null) {
for (var i=0; i<namesAr.length; i++) {
if (node.nodeName.toLowerCase() == namesAr[i].toLowerCase() )
return node;
}
}
return null;
}
function deleteMarked(tbl) {
if (tbl.rows == 0)
return;
var tr = tbl.rows[0];
do {
var next = nextElm(tr, "TR");
// Delete row
if (tr._delete) {
tr.parentNode.removeChild(tr);
continue;
}
// Delete cells
var td = tr.cells[0];
if (td.cells > 1) {
do {
var nexttd = nextElm(td, "TD,TH");
if (td._delete)
td.parentNode.removeChild(td);
} while ((td = nexttd) != null);
}
} while ((tr = next) != null);
}
function addRows(td_elm, tr_elm, rowspan) {
// Add rows
td_elm.rowSpan = 1;
var trNext = nextElm(tr_elm, "TR");
for (var i=1; i<rowspan && trNext; i++) {
var newTD = doc.createElement("td");
newTD.innerHTML = " ";
if (tinyMCE.isMSIE)
trNext.insertBefore(newTD, trNext.cells(td_elm.cellIndex));
else
trNext.insertBefore(newTD, trNext.cells[td_elm.cellIndex]);
trNext = nextElm(trNext, "TR");
}
}
function copyRow(doc, table, tr) {
var grid = getTableGrid(table);
var newTR = tr.cloneNode(false);
var cpos = getCellPos(grid, tr.cells[0]);
var lastCell = null;
var tableBorder = tinyMCE.getAttrib(table, "border");
var tdElm = null;
for (var x=0; tdElm = getCell(grid, cpos.rowindex, x); x++) {
var newTD = null;
if (lastCell != tdElm) {
for (var i=0; i<tr.cells.length; i++) {
if (tdElm == tr.cells[i]) {
newTD = tdElm.cloneNode(true);
break;
}
}
}
if (newTD == null) {
newTD = doc.createElement("td");
newTD.innerHTML = " ";
}
// Reset col/row span
newTD.colSpan = 1;
newTD.rowSpan = 1;
newTR.appendChild(newTD);
lastCell = tdElm;
}
return newTR;
}
// ---- Commands -----
// Handle commands
switch (command) {
case "mceTableRowProps":
if (trElm == null)
return true;
if (user_interface) {
// Setup template
var template = new Array();
template['file'] = '../../plugins/table/row.htm';
template['width'] = 380;
template['height'] = 295;
// Language specific width and height addons
template['width'] += tinyMCE.getLang('lang_table_rowprops_delta_width', 0);
template['height'] += tinyMCE.getLang('lang_table_rowprops_delta_height', 0);
// Open window
tinyMCE.openWindow(template, {editor_id : inst.editorId, inline : "yes"});
}
return true;
case "mceTableCellProps":
if (tdElm == null)
return true;
if (user_interface) {
// Setup template
var template = new Array();
template['file'] = '../../plugins/table/cell.htm';
template['width'] = 380;
template['height'] = 295;
// Language specific width and height addons
template['width'] += tinyMCE.getLang('lang_table_cellprops_delta_width', 0);
template['height'] += tinyMCE.getLang('lang_table_cellprops_delta_height', 0);
// Open window
tinyMCE.openWindow(template, {editor_id : inst.editorId, inline : "yes"});
}
return true;
case "mceInsertTable":
if (user_interface) {
// Setup template
var template = new Array();
template['file'] = '../../plugins/table/table.htm';
template['width'] = 380;
template['height'] = 295;
// Language specific width and height addons
template['width'] += tinyMCE.getLang('lang_table_table_delta_width', 0);
template['height'] += tinyMCE.getLang('lang_table_table_delta_height', 0);
// Open window
tinyMCE.openWindow(template, {editor_id : inst.editorId, inline : "yes", action : value});
}
return true;
case "mceTableDelete":
var table = tinyMCE.getParentElement(inst.getFocusElement(), "table");
if (table) {
table.parentNode.removeChild(table);
inst.repaint();
}
return true;
case "mceTableSplitCells":
case "mceTableMergeCells":
case "mceTableInsertRowBefore":
case "mceTableInsertRowAfter":
case "mceTableDeleteRow":
case "mceTableInsertColBefore":
case "mceTableInsertColAfter":
case "mceTableDeleteCol":
case "mceTableCutRow":
case "mceTableCopyRow":
case "mceTablePasteRowBefore":
case "mceTablePasteRowAfter":
// No table just return (invalid command)
if (!tableElm)
return true;
// Table has a tbody use that reference
// Changed logic by ApTest 2005.07.12 (www.aptest.com)
// Now lookk at the focused element and take its parentNode. That will be a tbody or a table.
if (tableElm != trElm.parentNode)
tableElm = trElm.parentNode;
if (tableElm && trElm) {
switch (command) {
case "mceTableInsertRowBefore":
if (!trElm || !tdElm)
return true;
var grid = getTableGrid(tableElm);
var cpos = getCellPos(grid, tdElm);
var newTR = doc.createElement("tr");
var lastTDElm = null;
cpos.rowindex--;
if (cpos.rowindex < 0)
cpos.rowindex = 0;
// Create cells
for (var x=0; tdElm = getCell(grid, cpos.rowindex, x); x++) {
if (tdElm != lastTDElm) {
var sd = getColRowSpan(tdElm);
if (sd['rowspan'] == 1) {
var newTD = doc.createElement("td");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -