📄 editor.js
字号:
var format = "HTML";
var initHTML = "";
var edit;
var RangeType;
var bEditMode = null;
// 浏览器版本检测
var BrowserInfo = new Object() ;
BrowserInfo.MajorVer = navigator.appVersion.match(/MSIE (.)/)[1] ;
BrowserInfo.MinorVer = navigator.appVersion.match(/MSIE .\.(.)/)[1] ;
BrowserInfo.IsIE55OrMore = BrowserInfo.MajorVer >= 6 || ( BrowserInfo.MajorVer >= 5 && BrowserInfo.MinorVer >= 5 ) ;
function setFocus() {
textEdit.focus();
}
function selectRange(){
editr = textEdit.document.body.createTextRange()
edit = textEdit.document.selection.createRange();
RangeType = textEdit.document.selection.type;
}
//007
// 修正Undo/Redo
var history = new Object;
history.data = [];
history.position = 0;
history.bookmark = [];
// 保存历史
function saveHistory() {
if (history.data[history.position] != textEdit.document.body.innerHTML){
var nBeginLen = history.data.length;
var nPopLen = history.data.length - history.position;
for (var i=1; i<nPopLen; i++){
history.data.pop();
history.bookmark.pop();
}
history.data[history.data.length] = textEdit.document.body.innerHTML;
if (textEdit.document.selection.type != "Control"){
history.bookmark[history.bookmark.length] = textEdit.document.selection.createRange().getBookmark();
} else {
var oControl = textEdit.document.selection.createRange();
history.bookmark[history.bookmark.length] = oControl(0);
}
if (nBeginLen!=0){
history.position++;
}
}
}
// 初始历史
function initHistory() {
history.data.length = 0;
history.bookmark.length = 0;
history.position = 0;
}
// 返回历史
function goHistory(value) {
saveHistory();
// undo
if (value == "Undo"){
if (history.position > 0){
textEdit.document.body.innerHTML = history.data[--history.position];
setHistoryCursor();
}
// redo
} else {
if (history.position < history.data.length -1){
textEdit.document.body.innerHTML = history.data[++history.position];
setHistoryCursor();
}
}
}
// 设置当前书签
function setHistoryCursor() {
if (history.bookmark[history.position]){
r = textEdit.document.body.createTextRange()
if (history.bookmark[history.position] != "[object]"){
if (r.moveToBookmark(history.bookmark[history.position])){
r.collapse(false);
r.select();
}
}
}
}
// end 007
function execCommand(command) {
if (format == "HTML"){
setFocus();
selectRange();
if ((command == "Undo") || (command == "Redo"))
goHistory(command)
//document.execCommand(command);
else{
if (arguments[1]==null)
edit.execCommand(command);
else
edit.execCommand(command, false, arguments[1]);}
textEdit.focus();
if (RangeType != "Control") edit.select();
}else{
if(format == "ABC"){
alert("操作无效,现在是HTML状态!请切换到普通状态,才能进行编辑操作");
}
if(format == "PREVIEW"){
alert("操作无效,现在是预览状态!请切换到普通状态,才能进行编辑操作");
}
}
}
//***辅助线
var borderShown = 0
function sBorders(){
allTables = textEdit.document.body.getElementsByTagName("TABLE");
for(i=0; i < allTables.length; i++){
if(!borderShown)
allTables[i].runtimeStyle.borderTop = allTables[i].runtimeStyle.borderLeft = "1px dotted #BFBFBF";
else
allTables[i].runtimeStyle.cssText = '';
allRows = allTables[i].rows;
for(y=0; y < allRows.length; y++){
allCells = allRows[y].cells;
for(x=0; x < allCells.length; x++)
if(!borderShown)
allCells[x].runtimeStyle.borderRight = allCells[x].runtimeStyle.borderBottom = "1px dotted #BFBFBF";
else
allCells[x].runtimeStyle.cssText = '';
}
}
borderShown = borderShown ? 0 : 1;
if(!borderShown)
textEdit.document.body.innerHTML = textEdit.document.body.innerHTML;
}
//判断表格属性
function isTableSelected(){
textEdit.focus()
if(textEdit.document.selection.type == "Control"){
var oControlRange = textEdit.document.selection.createRange();
if(oControlRange(0).tagName.toUpperCase() == "TABLE"){
selectedTable = textEdit.document.selection.createRange()(0);
return true;
}
}
}
function isCursorInTableCell(){
textEdit.focus();
if(document.selection.type != "Control"){
var elem = document.selection.createRange().parentElement();
while(elem.tagName.toUpperCase() != "TD" && elem.tagName.toUpperCase() != "TH"){
elem = elem.parentElement
if(elem == null)
break
}
if(elem){
selectedTD = elem
selectedTR = selectedTD.parentElement
selectedTBODY = selectedTR.parentElement
selectedTable = selectedTBODY.parentElement
return true
}
}
}
//修改表格
function ModifyTable(sNewTable){
if(isTableSelected() || isCursorInTableCell()){
res = showModalDialog('tablemod.html', selectedTable, 'dialogWidth: 360px; dialogHeight: 200px; center: yes; resizable: no; scroll: no; status: no;');
if(res){
if(res.width)
selectedTable.width = res.width;
else
selectedTable.removeAttribute('width',0);
if(res.cellPadding)
selectedTable.cellPadding = res.cellPadding;
else
selectedTable.removeAttribute('cellPadding',0);
if(res.bgColor)
selectedTable.bgColor = res.bgColor;
else
selectedTable.removeAttribute('bgColor',0);
if(res.background)
selectedTable.background = res.background;
else
selectedTable.removeAttribute('background',0);
if(res.cellSpacing)
selectedTable.cellSpacing = res.cellSpacing;
else
selectedTable.removeAttribute('cellSpacing',0);
if(res.border)
selectedTable.border = res.border;
else
selectedTable.removeAttribute('border',0);
}
}
}
//修改单元格
function ModifyCell(){
if(isCursorInTableCell()){
res = showModalDialog('Cell.html', selectedTD, 'dialogWidth: 360px; dialogHeight: 225px; center: yes; resizable: no; scroll: no; status: no;');
if(res){
selectedTD.colSpan = res.colSpan;
selectedTD.rowSpan = res.rowSpan;
if(res.width)
selectedTD.width = res.width;
else
selectedTD.removeAttribute('width',0);
if(res.height)
selectedTD.height = res.height;
else
selectedTD.removeAttribute('height',0);
if(res.bgColor)
selectedTD.bgColor = res.bgColor;
else
selectedTD.removeAttribute('bgColor',0);
if(res.background)
selectedTD.background = res.background;
else
selectedTD.removeAttribute('background',0);
if(res.align && !res.align.match(/^None$/i))
selectedTD.align = res.align;
else
selectedTD.removeAttribute('align',0);
if(res.vAlign && !res.vAlign.match(/^None$/i))
selectedTD.vAlign = res.vAlign;
else
selectedTD.removeAttribute('vAlign',0);
}
}else{
alert('修改单元格属性前,需将光标停留在需要修改的单元格中');
}
}
//表格修改功能
function insrowabove(){
if(isCursorInTableCell()){
var numCols = 0
allCells = selectedTR.cells
for(var i=0;i<allCells.length;i++)
numCols = numCols + allCells[i].getAttribute('colSpan')
var newTR = selectedTable.insertRow(selectedTR.rowIndex)
for(i = 0; i < numCols; i++){
newTD = newTR.insertCell()
newTD.innerHTML = " "
}
}
}
function insrowbelow(){
if(isCursorInTableCell()){
var numCols = 0
allCells = selectedTR.cells
for(var i=0;i<allCells.length;i++)
numCols = numCols + allCells[i].getAttribute('colSpan')
var newTR = selectedTable.insertRow(selectedTR.rowIndex+1)
for (i = 0; i < numCols; i++){
newTD = newTR.insertCell()
newTD.innerHTML = " "
}
}
}
function deleterow(){
if(isCursorInTableCell())
selectedTable.deleteRow(selectedTR.rowIndex)
}
function deletecol(){
if(isCursorInTableCell()){
moveFromEnd = (selectedTR.cells.length-1) - (selectedTD.cellIndex)
allRows = selectedTable.rows
for(var i=0;i<allRows.length;i++){
endOfRow = allRows[i].cells.length - 1
position = endOfRow - moveFromEnd
if(position < 0)
position = 0
allCellsInRow = allRows[i].cells
if(allCellsInRow[position].colSpan > 1)
allCellsInRow[position].colSpan = allCellsInRow[position].colSpan - 1
else
allRows[i].deleteCell(position)
}
}
}
function inscolafter(){
if (isCursorInTableCell()) {
moveFromEnd = (selectedTR.cells.length-1) - (selectedTD.cellIndex)
allRows = selectedTable.rows
for(i=0;i<allRows.length;i++){
rowCount = allRows[i].cells.length - 1
position = rowCount - moveFromEnd
if(position < 0)
position = 0
newCell = allRows[i].insertCell(position+1)
newCell.innerHTML = " "
}
}
}
function inscolbefore(){
if (isCursorInTableCell()){
moveFromEnd = (selectedTR.cells.length-1) - (selectedTD.cellIndex)
allRows = selectedTable.rows
for(i=0;i<allRows.length;i++){
rowCount = allRows[i].cells.length - 1
position = rowCount - moveFromEnd
if(position < 0)
position = 0
newCell = allRows[i].insertCell(position)
newCell.innerHTML = " "
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -