📄 editor.js
字号:
}
}
this.fontoptions[''].style.display = '';
}
if(this.sizeoptions) {
for(var i in this.sizeoptions) {
if(i != '') {
this.sizeoptions[i].parentNode.removeChild(this.sizeoptions[i]);
}
}
this.sizeoptions[''].style.display = '';
}
};
this.insertSmiley = function(e, smilietext, smiliepath, smilieid) {
this.checkFocus();
if(wysiwyg && this.parsesmilies && !fetchCheckbox("smileyoff")) {
if(is_moz) {
this.applyFormat('InsertImage', false, smiliepath);
var smilies = findtags(this.editdoc.body, 'img');
for(var i = 0; i < smilies.length; i++)
if(smilies[i].src == smiliepath && smilies[i].getAttribute('smilieid') < 1) {
smilies[i].setAttribute('smilieid', smilieid);
smilies[i].setAttribute('border', "0");
}
} else {
this.insertText('<img src="' + smiliepath + '" border="0" smilieid="' + smilieid + '" alt="" /> ', false);
}
} else {
smilietext += ' ';
return this.insertText(smilietext, smilietext.length, 0);
}
};
this.setEditorContents = function(initialtext) {
if(wysiwyg) {
if($(editorid + '_iframe')) {
this.editbox = $(editorid + '_iframe');
} else {
var iframe = document.createElement('iframe')
this.editbox = textobj.parentNode.appendChild(iframe);
this.editbox.id = editorid + '_iframe';
this.editbox.tabIndex = 1;
}
if(!is_ie) {
this.editbox.style.border = '1px #DDDDDD solid';
}
this.editbox.style.width = textobj.style.width;
this.editbox.style.height = textobj.style.height;
textobj.style.display = 'none';
this.editwin = this.editbox.contentWindow;
this.editdoc = this.editwin.document;
this.writeEditorContents((undefined(initialtext) ? textobj.value : initialtext), true);
} else {
var iframe = textobj.parentNode.getElementsByTagName('iframe')[0];
if(iframe) {
textobj.style.display = '';
textobj.style.width = iframe.style.width;
textobj.style.height = iframe.style.height;
iframe.style.width = '0px';
iframe.style.height = '0px';
iframe.style.border = 'none';
}
this.editwin = textobj;
this.editdoc = textobj;
this.editbox = textobj;
if(typeof initialtext != 'undefined') {
this.writeEditorContents(initialtext);
}
history.add_snapshot(this.getEditorContents());
}
this.editdoc.editorid = editorid;
this.editwin.editorid = editorid;
};
this.applyFormat = function(cmd, dialog, argument) {
if(wysiwyg) {
this.editdoc.execCommand(cmd, (undefined(dialog) ? false : dialog), (undefined(argument) ? true : argument));
return false;
}
if(cmd=='bold' || cmd=='italic' || cmd=='underline') {
this.wrapTags(cmd.substr(0, 1), false);
} else if(cmd=='justifyleft') {
this.wrapTags('align', 'left');
} else if(cmd=='justifycenter') {
this.wrapTags('align', 'center');
} else if(cmd=='justifyright') {
this.wrapTags('align', 'right');
} else if(cmd=='indent') {
this.wrapTags(cmd, false);
} else if(cmd=='fontname') {
this.wrapTags('font', argument);
} else if(cmd=='fontsize') {
this.wrapTags('size', argument);
} else if(cmd=='forecolor') {
this.wrapTags('color', argument);
} else if(cmd=='createlink') {
var sel = this.getSel();
if(sel) {
this.wrapTags('url', argument);
} else {
this.wrapTags('url', argument, argument);
}
} else if(cmd=='insertimage') {
this.wrapTags('img', false, argument);
}
};
this.setContext = function(cmd) {
if(wysiwyg) {
for(var i in contextcontrols) {
var obj = $(editorid + '_cmd_' + contextcontrols[i]);
if(obj != null) {
state = this.editdoc.queryCommandState(contextcontrols[i]);
if(obj.state != state) {
obj.state = state;
this.button_context(obj, (obj.cmd == cmd ? 'mouseover' : 'mouseout'));
}
}
}
this.setFontContext();
this.setSizeContext();
this.setColorContext();
}
};
this.setEditorStyle = function() {
if(wysiwyg) {
if(is_moz) {
for(var ss = 0; ss < document.styleSheets.length; ss++) {
if(document.styleSheets[ss].cssRules.length <= 0) {
continue;
}
for(var i = 0; i < document.styleSheets[ss].cssRules.length; i++) {
if(document.styleSheets[ss].cssRules[i].selectorText == '.wysiwyg') {
newss = this.editdoc.createElement('style');
newss.type = 'text/css';
newss.innerHTML = document.styleSheets[ss].cssRules[i].cssText + ' p { margin: 0px; }';
this.editdoc.documentElement.childNodes[0].appendChild(newss);
this.editdoc.body.style.fontSize = document.styleSheets[ss].cssRules[i].style.fontSize;
this.editdoc.body.style.fontFamily = document.styleSheets[ss].cssRules[i].style.fontFamily;
this.editdoc.body.style.backgroundColor = typeof altbg1 == 'undefined' ? "#FFFFFF" : altbg1;
}
}
}
} else if(is_ie || is_opera) {
if(document.styleSheets['css']) {
this.editdoc.createStyleSheet().cssText = document.styleSheets['css'].cssText + ' p { margin: 0px; }';
this.editdoc.body.className = 'wysiwyg';
this.editdoc.body.style.backgroundColor = typeof altbg1 == 'undefined' ? "#FFFFFF" : altbg1;
}
}
}
};
this.writeEditorContents = function(text, doinit) {
if(wysiwyg) {
if(text == '' && is_moz) {
text = '<br /><br /><br /><br /><br /><br /><br /><br /><br /><br />';
}
if(this.editdoc && this.editdoc.initialized) {
this.editdoc.body.innerHTML = text;
} else {
if(doinit) {
this.editdoc.designMode = 'on';
}
this.editdoc = this.editwin.document;
this.editdoc.open('text/html', 'replace');
this.editdoc.write(text);
this.editdoc.close();
if(doinit) {
this.editdoc.body.contentEditable = true;
}
this.editdoc.initialized = true;
this.setEditorStyle();
}
} else {
textobj.value = text;
}
};
this.setEditorEvents = function() {
if(wysiwyg) {
if(is_moz || is_opera) {
this.editdoc.addEventListener('mouseup', EVENT.editdoc_onmouseup, true);
this.editdoc.addEventListener('keyup', EVENT.editdoc_onkeyup, true);
this.editwin.addEventListener('focus', EVENT.editwin_onfocus, true);
this.editwin.addEventListener('blur', EVENT.editwin_onblur, true);
this.editwin.addEventListener('keydown', function(e) {ctlent(e);}, true);
} else {
this.editdoc.onmouseup = EVENT.editdoc_onmouseup;
this.editdoc.onkeyup = EVENT.editdoc_onkeyup;
if(this.editdoc.attachEvent) {
this.editdoc.body.attachEvent("onkeydown", ctlent);
}
}
}
this.editwin.onfocus = EVENT.editwin_onfocus;
this.editwin.onblur = EVENT.editwin_onblur;
};
this.getEditorContents = function() {
return wysiwyg ? this.editdoc.body.innerHTML : this.editdoc.value;
};
this.insertText = function(text, movestart, moveend) {
if(wysiwyg) {
if(is_moz || is_opera) {
fragment = this.editdoc.createDocumentFragment();
holder = this.editdoc.createElement('span');
holder.innerHTML = text;
while(holder.firstChild) {
fragment.appendChild(holder.firstChild);
}
this.insert_node_at_selection(fragment);
} else {
this.checkFocus();
if(typeof(this.editdoc.selection) != 'undefined' && this.editdoc.selection.type != 'Text' && this.editdoc.selection.type != 'None') {
movestart = false;
this.editdoc.selection.clear();
}
var sel = this.editdoc.selection.createRange();
sel.pasteHTML(text);
if(text.indexOf('\n') == -1) {
if(typeof movestart != 'undefined') {
sel.moveStart('character', -mb_strlen(text) +movestart);
sel.moveEnd('character', -moveend);
} else if(movestart != false) {
sel.moveStart('character', -mb_strlen(text));
}
}
}
} else {
this.checkFocus();
if(typeof(this.editdoc.selectionStart) != 'undefined') {
var opn = this.editdoc.selectionStart + 0;
this.editdoc.value = this.editdoc.value.substr(0, this.editdoc.selectionStart) + text + this.editdoc.value.substr(this.editdoc.selectionEnd);
if(typeof movestart != 'undefined') {
this.editdoc.selectionStart = opn + movestart;
this.editdoc.selectionEnd = opn + mb_strlen(text) - moveend;
} else if(movestart !== false) {
this.editdoc.selectionStart = opn;
this.editdoc.selectionEnd = opn + mb_strlen(text);
}
} else if(document.selection && document.selection.createRange) {
var sel = document.selection.createRange();
sel.text = text.replace(/\r?\n/g, '\r\n');
if(typeof movestart != 'undefined') {
sel.moveStart('character', -mb_strlen(text) +movestart);
sel.moveEnd('character', -moveend);
} else if(movestart !== false) {
sel.moveStart('character', -mb_strlen(text));
}
sel.select();
} else {
this.editdoc.value += text;
}
}
};
this.createlink = function(e, url) {
if(wysiwyg) {
if(is_moz || is_opera) {
if(typeof url == 'undefined') {
url = this.showPrompt(lang['enter_link_url'], 'http://');
}
if((url = this.verifyPrompt(url)) !== false) {
if(this.getSel()) {
this.applyFormat('unlink');
this.applyFormat('createlink', is_ie, (typeof url == 'undefined' ? true : url));
} else {
this.insertText('<a href="' + url + '">' + url + '</a>');
}
}
} else {
this.applyFormat('createlink', is_ie, (typeof url == 'undefined' ? true : url));
}
} else {
this.prompt_link('url', url, lang['enter_link_url'], 'http://');
}
};
this.email = function(e, email) {
if(wysiwyg) {
if(undefined(email)) {
email = this.showPrompt(lang['enter_email_link'], '');
}
email = this.verifyPrompt(email);
if(email === false) {
this.applyFormat('unlink');
} else {
var selection = this.getSel();
this.insertText('<a href="mailto:' + email + '">' + (selection ? selection : email) + '</a>', (selection ? true : false));
}
} else {
this.prompt_link('email', email, lang['enter_email_link'], '');
}
};
this.table = function(e, rows, columns) {
if(wysiwyg) {
if(typeof rows == 'undefined') {
rows = this.showPrompt(lang['enter_table_rows'], '2');
}
rows = /^[-\+]?\d+$/.test(rows) && rows > 0 && rows <= 30 ? rows : 2;
if(typeof columns == 'undefined') {
columns = this.showPrompt(lang['enter_table_columns'], '2');
}
columns = /^[-\+]?\d+$/.test(columns) && columns > 0 && columns <= 30 ? columns : 2;
var html = '<table cellspacing="1" cellpadding="4" width="50%" align="center" style="' + tableborder + '">';
for (var row = 0; row < rows; row++) {
html += "<tr>\n";
for (col = 0; col < columns; col++) {
html += "<td style=\"" + altbg2 + "\"> </td>\n";
}
html+= "</tr>\n";
}
html += "</table>\n";
this.insertText(html);
} else {
return false;
}
};
this.getSel = function() {
if(wysiwyg) {
if(is_moz || is_opera) {
selection = this.editwin.getSelection();
this.checkFocus();
range = selection ? selection.getRangeAt(0) : this.editdoc.createRange();
return this.read_nodes(range.cloneContents(), false);
} else {
var range = this.editdoc.selection.createRange();
if(range.htmlText && range.text) {
return range.htmlText;
} else {
var do_not_steal_this_code_html = '';
for(var i = 0; i < range.length; i++) {
do_not_steal_this_code_html += range.item(i).outerHTML;
}
return do_not_steal_this_code_html;
}
}
} else {
if(typeof(this.editdoc.selectionStart) != 'undefined') {
return this.editdoc.value.substr(this.editdoc.selectionStart, this.editdoc.selectionEnd - this.editdoc.selectionStart);
} else if(document.selection && document.selection.createRange) {
return document.selection.createRange().text;
} else if(window.getSelection) {
return window.getSelection() + '';
} else {
return false;
}
}
};
if(wysiwyg) {
this.setFontContext = function(fs) {
if(eButtons['fontname']) {
if(undefined(fs)) {
fs = this.editdoc.queryCommandValue('fontname');
}
if(fs == '') {
if(!is_ie && window.getComputedStyle) {
fs = this.editdoc.body.style.fontFamily;
}
} else if(fs == null) {
fs = '';
}
if(fs != fontstate) {
thingy = fs.indexOf(',') > 0 ? fs.substr(0, fs.indexOf(',')) : fs;
for(var i in this.fontoptions) {
this.fontoptions[i].style.display = (i == thingy ? '' : 'none');
}
fontstate = fs;
}
}
};
this.setSizeContext = function(ss) {
if(eButtons['fontsize']) {
if(undefined(ss)) {
ss = this.editdoc.queryCommandValue('fontsize');
}
if(ss == null || ss == '') {
ss = this.formatFontsize(this.editdoc.body.style.fontSize);
}
if(ss != sizestate) {
if(sizestate == null) {
sizestate = '';
}
if(sizestate == '' || sizeoptions[sizestate-1]) {
this.sizeoptions[sizestate].style.display = 'none';
}
if(ss == '' || sizeoptions[ss-1]) {
this.sizeoptions[ss].style.display = '';
}
sizestate = ss;
}
}
};
this.formatFontsize = function(csssize) {
switch(csssize) {
case '7.5pt':
case '10px': return 1;
case '10pt': return 2;
case '12pt': return 3;
case '14pt': return 4;
case '18pt': return 5;
case '24pt': return 6;
case '36pt': return 7;
default: return '';
}
};
this.setColorContext = function(cs) {
if(eButtons['forecolor']) {
if(undefined(cs)) {
cs = this.editdoc.queryCommandValue('forecolor');
}
$(editorid + '_color_bar').style.backgroundColor = this.rgbToColor(cs);
}
};
this.rgbToColor = function(forecolor) {
return this.rgbhexToColor((forecolor & 0xFF).toString(16), ((forecolor >> 8) & 0xFF).toString(16), ((forecolor >> 16) & 0xFF).toString(16));
};
this.rgbhexToColor = function(r, g, b) {
return coloroptions['#' + (PHP.str_pad(r, 2, 0) + PHP.str_pad(g, 2, 0) + PHP.str_pad(b, 2, 0))];
};
if(is_moz || is_opera) {
this.rgbToColor = function(forecolor) {
if(forecolor == '' || forecolor == null) {
forecolor = window.getComputedStyle(this.editdoc.body, null).getPropertyValue('color');
}
if(forecolor.toLowerCase().indexOf('rgb') == 0) {
var matches = forecolor.match(/^rgb\s*\(([0-9]+),\s*([0-9]+),\s*([0-9]+)\)$/);
if(matches) {
return this.rgbhexToColor((matches[1] & 0xFF).toString(16), (matches[2] & 0xFF).toString(16), (matches[3] & 0xFF).toString(16));
} else {
return this.rgbToColor(null);
}
} else {
return forecolor;
}
};
this.add_range = function(node) {
this.checkFocus();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -