?? coreeditor.js
字號:
if (this.EditType == Config.UBB) {
content = this.GetXHtmlFromUBB(true);
}
else {
if (!this.JudgeNotAllowHtmlTag(this.Document.body.innerHTML)) return false;
content = UBB.imageHTMLUrlToSign(UBB.multimediaHtmlToUbb(this.GetXhtmlFromHtml(false)));
}
this.TextRange = null;
this.CreateTextArea(content, type);
this.SelectOneTextArea();
}
// to UBB
else if (type == Config.UBB && this.EditType != Config.UBB) {
if (!this.AllowUBB) return false;
if (this.EditType == Config.XHTML) {
content = this.GetUBBFromXHtml(true);
}
else {
if (!this.JudgeNotAllowHtmlTag(this.Document.body.innerHTML)) return false;
content = UBB.imageUBBUrlToSign(this.GetUBBFromXHtml(false));
}
this.TextRange = null;
this.CreateTextArea(content, type);
this.TextAreaBindEvent();
}
// 設(shè)置當(dāng)前狀態(tài)
this.EditType = type;
Toolbar.responseAll(type == Config.XHTML);
// 文本域更新調(diào)用
this.EditorObj.AfterChange({DMode:type,AllowHTML:this.AllowHTML,AllowUBB:this.AllowUBB});
// 設(shè)置文本選擇域?yàn)榭? //this.TextRange = null;
this.Resize();
return true;
},
ChangeStateAndButton : function (type) {
type = type.toLowerCase();
this.ChangeState(type);
switch (type) {
case Config.WYSIWYG:
Toolbar.wysiwygClick();
break;
case Config.XHTML:
this.AllowHTML && Toolbar.xhtmlClick();
break;
case Config.UBB:
this.AllowUBB && Toolbar.ubbClick();
break;
}
},
JudgeNotAllowHtmlTag : function (content) {
if (Config.NotAllowHtmlTag().test(content)) {
return confirm('內(nèi)容包含不允許的標(biāo)簽。\n是否繼續(xù)轉(zhuǎn)換?');
}
return true;
},
/*
content edit
*/
// 獲取文本選取對象
// 選取范圍
GetRange : function () {
// 設(shè)置選擇區(qū)對象
if (Browser.IsIE) {
this.SelectRange = this.Document.selection;
this.TextRange = this.SelectRange.createRange();
}
else {
this.SelectRange = this.Window.getSelection();
}
return this.SelectRange;
},
// 文本選擇區(qū)對象
GetTextRange : function () {
if (Browser.IsIE) {
this.TextRange = document.selection.createRange();
}
else {
this.TextRangeStart = this.TextArea.selectionStart;
this.TextRangeEnd = this.TextArea.selectionEnd;
}
return this.TextRange;
},
// [ie] 選擇內(nèi)容
SelectTextRange : function (isCollapse) {
if (!Browser.IsIE) return;
if (this.EditType != Config.WYSIWYG) return;
try {
if (this.TextRange) {
this.TextRange.collapse(false);
isCollapse && this.TextRange.select();
}
else {
this.Focus();
}
}
catch (e) {}
},
// 對編輯器內(nèi)容應(yīng)用樣式
SetStyle : function (type, value) {
var _status;
if (! Browser.IsIELike && type == 'HiliteColor') {
this.SetStyle('styleWithCSS', true);
}
try {
this.Focus();
_status = this.Document.execCommand(type, false, value);
}
catch (e) {}
if (! Browser.IsIELike && type == 'HiliteColor') {
this.SetStyle('styleWithCSS', false);
}
this.SelectTextRange();
return _status;
},
// ubb方式替換內(nèi)容
// contents : [content, content1, content2, ..., contentN]
// eg:
// [b] => ['[b]\x01[/b]', 'content1']
// [url] => ['[url=\x01]\x02[/url]', 'content1', 'content2']
// 'content1'是文本域選擇的文本,所以無需通過參數(shù)傳入,所以參數(shù)參數(shù)傳遞應(yīng)為:
// eg:
// [b] => ['[b]\x01[/b]']
// [url] => ['[url=\x01]\x02[/url]', 'content2']
// 'content1'在SetUbbStyle方法內(nèi)獲得
SetUBBStyle : function () {
var contents = arguments;
var l = contents.length;
var t = this.TextArea;
var content;
var _content = content = Browser.IsIE ? (this.TextRange ? this.TextRange.text : '')
: t.value.substring(this.TextRangeStart, this.TextRangeEnd)
var index = contents[0].indexOf('\x01');
content = contents[0].replace(/\x01/g, content || contents[1] || '');
for (var i = 1; i < l; i++) {
content = content.replace(new RegExp('\\x0' + (i + 1), 'g'), contents[i]);
}
if (_content != '') index = 0;
if (Browser.IsIE) {
if (this.TextRange) {
this.TextRange.text = content;
if (index > 0) this.TextRange.move('character', index - content.length);
this.TextRange.select();
}
else {
t.value += content;
}
}
else {
t.value = t.value.substring(0, this.TextRangeStart) + content + t.value.substring(this.TextRangeEnd);
var start = index > 0 ? this.TextRangeStart + index : this.TextRangeStart + content.length;
t.setSelectionRange(start, start);
this.TextRangeStart = this.TextRangeEnd = index > 0 ? this.TextRangeStart + index : start;
}
t.focus();
this.GetTextRange();
},
// 插入內(nèi)容
InsertContent : function (content) {
if (this.EditType != Config.WYSIWYG) return;
if (Browser.IsIE)
this.InsertContentIE(content);
else {
var c = this.Document.createElement('span');
this.Document.body.appendChild(c);
c.innerHTML = content;
try {
this.InsertContentGecko(c);
}
catch (e) {
this.Document.body.appendChild(c);
this.SelectOneEidtor();
}
while (c && c.firstChild) {
var node = c.removeChild(c.firstChild);
c.parentNode.insertBefore(node, c);
}
c.parentNode.removeChild(c);
}
// 提示一次,是否啟用要切換至全屏
if (this.FullScreenPrompt) {
this.FullScreenPrompt = false;
var sw = Global.GetClientWidth(this.Document);
var nodes = this.Document.getElementsByTagName('*');
for (var i = 0, len = nodes.length; i < len; i++) {
if (nodes[i].offsetWidth > sw) {
// 提示
this.EditorObj.SwitchToFullScreen();
/*
if (this.EditorObj.SwitchToFullScreen()) {
Toolbar.SwitchToFullScreen();
}
*/
break;
}
}
}
},
// [ie] insert content
InsertContentIE : function (content) {
try {
if (this.SelectRange.type == 'None') {
if (this.TextRange.parentElement().ownerDocument.body.className != 'maxcode-userenters')
throw new Error('parent document');
}
if ((this.SelectRange.type == 'Control' || this.TextRange.text != '') && this.SelectRange.clear)
this.SelectRange.clear();
this.TextRange.pasteHTML(content);
this.TextRange.select();
this.TextRange.collapse(false);
}
catch (e) {
var c = this.Document.createElement('span');
this.Document.body.appendChild(c);
c.innerHTML = content;
while (c.firstChild) {
var node = c.removeChild(c.firstChild);
c.parentNode.insertBefore(node, c);
}
c.parentNode.removeChild(c);
this.SelectOneEidtor();
}
this.GetRange();
},
// [gecko] insert content
InsertContentGecko : function (insertNode) {
// 獲得當(dāng)前選取對象
var sel = this.SelectRange;
// 獲取第一個(gè)選取對象,僅僅只有一個(gè)
var range = sel.getRangeAt(0);
// 取消所有選取
sel.removeAllRanges();
// 從文檔中刪除選區(qū)的內(nèi)容
range.deleteContents();
var container = range.startContainer;
var pos = range.startOffset;
// 創(chuàng)建新的Range
range = this.Document.createRange();
if (container.nodeType == 3 && insertNode.nodeType == 3) {
// 如果插入點(diǎn)是文本節(jié)點(diǎn),則只進(jìn)行插入事件
container.insertData(pos, insertNode.nodeValue);
// 移動(dòng)光標(biāo)到插入的內(nèi)容后面
range.setEnd(container, pos + insertNode.length);
range.setStart(container, pos + insertNode.length);
}
else {
var afterNode;
if (container.nodeType == 3) {
// 當(dāng)插入一個(gè)非文本節(jié)點(diǎn)到一個(gè)文本節(jié)點(diǎn)中,需要?jiǎng)?chuàng)建兩個(gè)文本節(jié)點(diǎn),然后在其間放入我們的節(jié)點(diǎn)
var textNode = container;
container = textNode.parentNode;
var text = textNode.nodeValue;
// 截取焦點(diǎn)之前的文本
var textBefore = text.substr(0,pos);
// 截取焦點(diǎn)之后的文本
var textAfter = text.substr(pos);
var beforeNode = this.Document.createTextNode(textBefore);
afterNode = this.Document.createTextNode(textAfter);
// insert the 3 new nodes before the old one
container.insertBefore(afterNode, textNode);
container.insertBefore(insertNode, afterNode);
container.insertBefore(beforeNode, insertNode);
// remove the old node
container.removeChild(textNode);
}
else {
// 否則僅僅插入節(jié)點(diǎn)
afterNode = container.childNodes[pos];
container.insertBefore(insertNode, afterNode);
}
try {range.setEnd(afterNode, 0);}catch (e) {}
try {range.setStart(afterNode, 0);}catch (e) {}
}
try {
sel.addRange(range);
range.collapse(false);
this.Focus();
}
catch (e) {}
},
// 菜單
Menu : function (targetWindow, width, height, content, callback) {
var targetDocument = targetWindow.document;
var menu = document.createElement('div');
menu.style.position = 'absolute';
width && (menu.style.width = width + 'px');
height && (menu.style.height = height + 'px');
CSS.AddClass(menu, 'menu');
if (typeof content == 'string') {
var link = targetDocument.createElement('a');
link.href = '#';
link.onclick = function () {cEditor.setStyle('Bold')};
link.appendChild(targetDocument.createTextNode(content));
content = link;
}
content && menu.appendChild(content);
targetDocument.body.appendChild(menu);
/// 點(diǎn)擊按鈕關(guān)閉
// 當(dāng)前窗口點(diǎn)擊
var tempMouseDown = targetDocument.onmousedown || new Function();
targetDocument.onmousedown = function (e) {
//tempMouseDown();
e = (e || targetWindow.event);
var p = e.srcElement || e.target;
if (p != menu) {
while (p) {
if (p == menu) {
return;
}
p = p.offsetParent;
}
}
menu.style.display = 'none';
callback && callback();
};
// 編輯器點(diǎn)擊
try {
var tempMouseDownEditor = this.Document.onmousedown || new Function();
this.Document.onmousedown = function () {
tempMouseDownEditor();
menu.style.display = 'none';
callback && callback();
mouse_down();
};
}
catch (e) {}
try {
this.Document.addEventListener('mousedown', function () {
menu.style.display = 'none';
callback && callback();
}, false);
}
catch (e) {}
// 父窗口點(diǎn)擊
var _parent = targetWindow.parent;
function mouse_down () {
menu.style.display = 'none';
callback && callback();
_parent.Events.RemoveEvent(_parent.document, 'mousedown', mouse_down);
}
_parent.Events.AttachEvent(_parent.document, 'mousedown', mouse_down);
return menu;
}
};
(function () {
// 獲取編輯文本框
var Document = window.parent.document;
cEditor.TextField = Document.getElementsByName(cEditor.Name)[0];
cEditor._TextField = Document.getElementsByName('_' + cEditor.Name)[0];
cEditor.EditorType = Document.getElementsByName(cEditor.Name + 'Type')[0];
cEditor.EditorObj.EditorWindow = window;
cEditor.EditorObj.FrameEditor = cEditor;
// 聚焦
Events.AttachEvent(window, 'focus', function () {
cEditor.Focus();
});
})();
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -