📄 ftb-freetextbox.js
字号:
if (this.timerToolbar)
this.timerToolbar = null;
};
FTB_FreeTextBox.prototype.UpdateAncestorTrail = function() {
if (this.ancestorArea) {
if (this.hasFocus) {
ancestors = this.GetAllAncestors();
this.ancestorArea.innerHTML = "Path(" + ancestors.length + "): ";
for (var i = ancestors.length-1; i>-1; i--) {
var el = ancestors[i];
if (!el) {
continue;
}
var a = document.createElement("a");
a.href = "javascript:void();";
a.el = el;
a.ftb = this;
a.onclick = function() {
this.blur();
this.ftb.SelectNodeContents(this.el);
this.ftb.UpdateToolbars();
return false;
};
a.oncontextmenu = function () {
this.ftb.EditElementStyle(this.el);
return false;
}
var txt = el.tagName.toLowerCase();
if (txt == "input") txt = el.type;
a.title = el.style.cssText;
if (el.id) {
txt += "#" + el.id;
}
if (el.className) {
txt += "." + el.className;
}
a.appendChild(document.createTextNode("<" + txt + ">"));
this.ancestorArea.appendChild(a);
//if (i != 0)
// this.ancestorArea.appendChild(document.createTextNode(String.fromCharCode(0xbb)));
}
} else {
this.ancestorArea.innerHTML = "";
}
}
};
FTB_FreeTextBox.prototype.SetToolbarItemsEnabledState = function() {
if (!this.enableToolbars) return;
if (this.hasFocus || !this.initialized) {
if (this.mode == FTB_MODE_DESIGN ) {
for (i=0; i<this.buttons.length; i++) {
button = this.buttons[i];
if (button.customEnabled)
button.customEnabled();
else
button.disabled = false;
if (button.disabled)
this.DisableButton(button);
else
this.EnableButton(button);
}
for (i=0; i<this.dropdownlists.length; i++) {
this.dropdownlists[i].list.disabled=false;
}
} else {
for (i=0; i<this.buttons.length; i++) {
button = this.buttons[i];
if (button.htmlModeEnabled)
button.disabled=false
else
button.disabled = true;
if (button.disabled)
this.DisableButton(button);
else
this.EnableButton(button);
}
for (i=0; i<this.dropdownlists.length; i++) {
this.dropdownlists[i].list.selectedIndex=0;
this.dropdownlists[i].list.disabled=true;
}
}
} else {
// do nothing: uncomment code to disable buttons when the editor does not have focus
/*
for (i=0; i<this.buttons.length; i++)
this.DisableButton(this.buttons[i]);
for (i=0; i<this.dropdownlists.length; i++)
this.dropdownlists[i].list.disabled=true;
*/
}
};
FTB_FreeTextBox.prototype.DisableAllToolbarItems = function() {
if (this.enableToolbars) {
for (i=0; i<this.buttons.length; i++) {
this.DisableButton(this.buttons[i]);
}
for (i=0; i<this.dropdownlists.length; i++) {
this.dropdownlists[i].list.disabled=true;
}
}
};
FTB_FreeTextBox.prototype.EnableButton = function(button) {
if (FTB_Browser.isIE)
button.buttonImage.style.filter = "alpha(opacity = 100);";
//button.td.style.filters.alpha.opacity = 100;
else
button.buttonImage.style.MozOpacity = 1;
};
FTB_FreeTextBox.prototype.DisableButton = function(button) {
button.state = FTB_BUTTON_OFF;
button.SetButtonStyle("Out");
if (FTB_Browser.isIE)
button.buttonImage.style.filter = "alpha(opacity = 25);";
//button.td.style.filters.alpha.opacity = 25;
else
button.buttonImage.style.MozOpacity = 0.25;
};
FTB_FreeTextBox.prototype.CopyHtmlToIframe = function(iframe) {
if (this.initialized) {
html = this.htmlEditor.value;
iframe.document.body.innerHTML = html;
//this.Debug(html.replace('\r','<R>').replace('\t','<T>').replace('\n','<N>'));
} else {
iframe.document.open();
iframe.document.write("<html>" +
"<head>" +
((this.designModeCss != '' && FTB_Browser.isGecko) ? "<style type='text/css'>@import url(" + this.designModeCss + ");</style>" : "") +
((this.baseUrl != '') ? "<base href='" + this.baseUrl + "' />" : "") +
"</head>" +
"<body>" + this.htmlEditor.value + "</body>" +
"</html>");
//iframe.document.write(this.htmlEditor.value);
iframe.document.close();
}
};
FTB_FreeTextBox.prototype.CopyDesignToHtml = function() {
this.htmlEditor.value = this.designEditor.document.body.innerHTML;
// clear out default moz & ie properties
if (this.htmlEditor.value == '<br>' || this.htmlEditor.value == '<br>\r\n' || // Moz
this.htmlEditor.value == '<P> </P>') { // IE
this.htmlEditor.value = '';
}
};
FTB_FreeTextBox.prototype.GoToHtmlMode = function() {
if (this.mode == FTB_MODE_DESIGN) this.CopyDesignToHtml();
if (FTB_Browser.isGecko)
this.designEditor.document.designMode = 'Off';
this.designEditorArea.style.display = 'none';
this.htmlEditorArea.style.display = '';
this.previewPaneArea.style.display = 'none';
if (this.ancestorArea) this.ancestorArea.innerHTML = "";
this.SetActiveTab(this.htmlModeTab);
this.mode = FTB_MODE_HTML;
//this.Focus();
return true;
};
FTB_FreeTextBox.prototype.GoToDesignMode = function() {
if (this.mode == FTB_MODE_DESIGN) return false;
this.CopyHtmlToIframe(this.designEditor);
this.designEditorArea.style.display = '';
this.htmlEditorArea.style.display = 'none';
this.previewPaneArea.style.display = 'none';
// reset for Gecko
if (FTB_Browser.isGecko) {
this.designEditor.document.designMode = 'On';
this.designEditor.document.execCommand("useCSS", false, true);
}
if (this.ancestorArea) this.ancestorArea.innerHTML = "";
this.SetActiveTab(this.designModeTab);
//this.SetToolbarItemsEnabledState();
this.mode = FTB_MODE_DESIGN;
//this.Focus();
return true;
};
FTB_FreeTextBox.prototype.GoToPreviewMode = function() {
if (this.mode == FTB_MODE_DESIGN) this.CopyDesignToHtml();
this.CopyHtmlToIframe(this.previewPane);
this.designEditorArea.style.display = 'none';
this.htmlEditorArea.style.display = 'none';
this.previewPaneArea.style.display = '';
this.SetActiveTab(this.previewModeTab);
if (this.ancestorArea) this.ancestorArea.innerHTML = "";
this.mode = FTB_MODE_PREVIEW;
return true;
};
FTB_FreeTextBox.prototype.HtmlEncode = function( text ) {
if ( typeof( text ) != "string" )
text = text.toString() ;
text = text.replace(/&/g, "&") ;
text = text.replace(/"/g, """) ;
text = text.replace(/</g, "<") ;
text = text.replace(/>/g, ">") ;
text = text.replace(/'/g, "’") ;
return text ;
};
FTB_FreeTextBox.prototype.ExecuteCommand = function(commandName, middle, commandValue) {
if (this.mode != FTB_MODE_DESIGN) return;
this.designEditor.focus();
if (commandName == 'backcolor' && !FTB_Browser.isIE) commandName = 'hilitecolor';
this.designEditor.document.execCommand(commandName,middle,commandValue);
if (this.clientSideTextChanged)
this.clientSideTextChanged(this);
};
FTB_FreeTextBox.prototype.QueryCommandState = function(commandName) {
if (this.mode != FTB_MODE_DESIGN) return false;
try {
if (this.designEditor.document.queryCommandState(commandName)) {
return FTB_BUTTON_ON;
} else {
// special case for paragraph on IE
if (commandName == 'justifyleft') {
if (this.designEditor.document.queryCommandState('justifyright') == false &&
this.designEditor.document.queryCommandState('justifycenter') == false &&
this.designEditor.document.queryCommandState('justifyfull') == false ) {
return FTB_BUTTON_ON;
} else {
return FTB_BUTTON_OFF;
}
} else {
return FTB_BUTTON_OFF;
}
}
} catch(exp) {
return FTB_BUTTON_OFF;
}
};
FTB_FreeTextBox.prototype.QueryCommandValue = function(commandName) {
if (this.mode != FTB_MODE_DESIGN) return false;
value = this.designEditor.document.queryCommandValue(commandName);
switch (commandName) {
case "backcolor":
if (FTB_Browser.isIE) {
value = FTB_IntToHexColor(value);
} else {
if (value == "") value = "#FFFFFF";
}
break;
case "forecolor":
if (FTB_Browser.isIE) {
value = FTB_IntToHexColor(value);
} else {
if (value == "") value = "#000000";
}
break;
case "formatBlock":
if (!FTB_Browser.isIE) {
if (value == "" || value == "<x>")
value = "<p>";
else
value = "<" + value + ">";
}
break;
}
if (value == '' || value == null) {
if (commandName == 'fontsize') return '3';
if (commandName == 'fontname') return 'Times New Roman';
if (commandName == 'forecolor') return '#000000';
if (commandName == 'backcolor') return '#ffffff';
}
return value;
};
FTB_FreeTextBox.prototype.SurroundHtml = function(start,end) {
if (this.mode == FTB_MODE_HTML) return;
this.designEditor.focus();
if (FTB_Browser.isIE) {
var sel = this.designEditor.document.selection.createRange();
html = start + sel.htmlText + end;
sel.pasteHTML(html);
} else {
selection = this.designEditor.window.getSelection();
if (selection) {
range = selection.getRangeAt(0);
} else {
range = this.designEditor.document.createRange();
}
this.InsertHtml(start + selection + end);
}
};
FTB_FreeTextBox.prototype.InsertHtml = function(html) {
if (this.mode != FTB_MODE_DESIGN) return;
this.designEditor.focus();
if (FTB_Browser.isIE) {
sel = this.designEditor.document.selection.createRange();
sel.pasteHTML(html);
} else {
selection = this.designEditor.window.getSelection();
if (selection) {
range = selection.getRangeAt(0);
} else {
range = editor.document.createRange();
}
var fragment = this.designEditor.document.createDocumentFragment();
var div = this.designEditor.document.createElement("div");
div.innerHTML = html;
while (div.firstChild) {
fragment.appendChild(div.firstChild);
}
selection.removeAllRanges();
range.deleteContents();
var node = range.startContainer;
var pos = range.startOffset;
switch (node.nodeType) {
case 3:
if (fragment.nodeType == 3) {
node.insertData(pos, fragment.data);
range.setEnd(node, pos + fragment.length);
range.setStart(node, pos + fragment.length);
} else {
node = node.splitText(pos);
node.parentNode.insertBefore(fragment, node);
range.setEnd(node, pos + fragment.length);
range.setStart(node, pos + fragment.length);
}
break;
case 1:
node = node.childNodes[pos];
node.parentNode.insertBefore(fragment, node);
range.setEnd(node, pos + fragment.length);
range.setStart(node, pos + fragment.length);
break;
}
selection.addRange(range);
}
};
/* ------------------------------------------------
START: Node and Selection Methods */
FTB_FreeTextBox.prototype.CheckTag = function(item,tagName) {
if (!item) return null;
if (item.tagName.search(tagName)!=-1) {
return item;
}
if (item.tagName=='BODY') {
return false;
}
item=item.parentElement;
return this.CheckTag(item,tagName);
};
FTB_FreeTextBox.prototype.GetParentElement = function() {
var sel = this.GetSelection();
var range = this.CreateRange(sel);
if (FTB_Browser.isIE) {
switch (sel.type) {
case "Text":
case "None":
return range.parentElement();
case "Control":
return range.item(0);
default:
return this.designEditor.document.body;
}
} else try {
var p = range.commonAncestorContainer;
if (!range.collapsed && range.startContainer == range.endContainer &&
range.startOffset - range.endOffset <= 1 && range.startContainer.hasChildNodes())
p = range.startContainer.childNodes[range.startOffset];
/*
alert(range.startContainer + ":" + range.startOffset + "\n" +
range.endContainer + ":" + range.endOffset);
*/
while (p.nodeType == 3) {
p = p.parentNode;
}
return p;
} catch (e) {
return null;
}
};
FTB_FreeTextBox.prototype.InsertNodeAtSelection = function(toBeInserted) {
if (!FTB_Browser.isIE) {
var sel = this.GetSelection();
var range = this.CreateRange(sel);
// remove the current selection
sel.removeAllRanges();
range.deleteContents();
var node = range.startContainer;
var pos = range.startOffset;
switch (node.nodeType) {
case 3: // Node.TEXT_NODE
// we have to split it at the caret position.
if (toBeInserted.nodeType == 3) {
// do optimized insertion
node.insertData(pos, toBeInserted.data);
range = this._createRange();
range.setEnd(node, pos + toBeInserted.length);
range.setStart(node, pos + toBeInserted.length);
sel.addRange(range);
} else {
node = node.splitText(pos);
var selnode = toBeInserted;
if (toBeInserted.nodeType == 11 /* Node.DOCUMENT_FRAGMENT_NODE */) {
selnode = selnode.firstChild;
}
node.parentNode.insertBefore(toBeInserted, node);
this.SelectNodeContents(selnode);
}
break;
case 1: // Node.ELEMENT_NODE
var selnode = toBeInserted;
if (toBeInserted.nodeType == 11 /* Node.DOCUMENT_FRAGMENT_NODE */) {
selnode = selnode.firstChild;
}
node.insertBefore(toBeInserted, node.childNodes[pos]);
this.SelectNodeContents(selnode);
break;
}
}
};
FTB_FreeTextBox.prototype.SelectNodeContents = function(node, pos) {
var range;
var collapsed = (typeof pos != "undefined");
if (isIE) {
range = this.designEditor.document.body.createTextRange();
range.moveToElementText(node);
(collapsed) && range.collapse(pos);
range.select();
} else {
var sel = this.GetSelection();
range = this.designEditor.document.createRange();
range.selectNodeContents(node);
(collapsed) && range.collapse(pos);
sel.removeAllRanges();
sel.addRange(range);
}
};
FTB_FreeTextBox.prototype.SelectNextNode = function(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) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -