📄 editor.js
字号:
setCaret(pos);
}
if((params == 1 && first) || (params == 2 && first && (haveSel || second)) || (params == 3 && first && second && (haveSel || third))) {
var text;
if(params == 1) {
text = first;
} else if(params == 2) {
text = haveSel ? selection : second;
opentag = '[' + tagname + '=' + first + ']';
} else {
text = haveSel ? selection : third;
opentag = '[' + tagname + '=' + first + ',' + second + ']';
}
insertText((opentag + text + closetag), strlen(opentag), strlen(closetag), true, sel);
}
hideMenu();
document.body.removeChild(div);
};
}
function editorMenu(ctrlid, str) {
var div = document.createElement('div');
div.id = ctrlid + '_menu';
div.style.display = 'none';
div.className = 'popupmenu_popup';
document.body.appendChild(div);
div.innerHTML = '<div class="popupmenu_option" unselectable="on">' + str + '<br /><center><input type="button" id="' + ctrlid + '_submit" value="' + lang['submit'] + '" /> <input type="button" onClick="hideMenu();try{document.body.removeChild(' + div.id + ')}catch(e){}" value="' + lang['cancel'] + '" /></center></div>';
showMenu(ctrlid, true, 0, 3);
return div;
}
function discuzcode(cmd, arg) {
if(cmd != 'redo') {
addSnapshot(getEditorContents());
}
checkFocus();
if(in_array(cmd, ['quote', 'code', 'free', 'hide'])) {
var sel;
if(is_ie) {
sel = wysiwyg ? editdoc.selection.createRange() : document.selection.createRange();
var pos = getCaret();
}
var selection = sel ? (wysiwyg ? sel.htmlText : sel.text) : getSel();
var opentag = '[' + cmd + ']';
var closetag = '[/' + cmd + ']';
if(cmd != 'hide' && selection) {
return insertText((opentag + selection + closetag), strlen(opentag), strlen(closetag), true, sel);
}
var ctrlid = editorid + '_cmd_' + cmd;
var str = '';
if(cmd != 'hide' || !selection) {
str += lang['post_discuzcode_' + cmd] + ':<br /><textarea id="' + ctrlid + '_param_1" cols="50" rows="5"></textarea>';
}
str += cmd == 'hide' && selection ? '' : '<br />';
str += cmd == 'hide' ? '<input type="radio" name="' + ctrlid + '_radio" id="' + ctrlid + '_radio_1" checked="checked">只有当浏览者回复本帖时才显示<br /><input type="radio" name="' + ctrlid + '_radio" id="' + ctrlid + '_radio_2">只有当浏览者积分高于 <input type="text" size="3" id="' + ctrlid + '_param_2"> 时才显示' : '';
var div = editorMenu(ctrlid, str);
$(ctrlid + '_param_' + (cmd == 'hide' && selection ? 2 : 1)).focus();
$(ctrlid + '_param_' + (cmd == 'hide' && selection ? 2 : 1)).onkeydown = editorMenuEvent_onkeydown;
$(ctrlid + '_submit').onclick = function() {
checkFocus();
if(is_ie) {
setCaret(pos);
}
if(cmd == 'hide' && $(ctrlid + '_radio_2').checked) {
var mincredits = parseInt($(ctrlid + '_param_2').value);
opentag = mincredits > 0 ? '[hide=' + mincredits + ']' : '[hide]';
}
var text = selection ? selection : $(ctrlid + '_param_1').value;
if(wysiwyg) {
if(cmd == 'code') {
text = preg_replace(['<', '>'], ['<', '>'], text);
}
text = text.replace(/\r?\n/g, '<br />');
}
text = opentag + text + closetag;
insertText(text, strlen(opentag), strlen(closetag), false, sel);
hideMenu();
document.body.removeChild(div);
}
return;
} else if(cmd.substr(0, 6) == 'custom') {
var ret = customTags(cmd.substr(8), cmd.substr(6, 1));
} else if(!wysiwyg && cmd == 'removeformat') {
var simplestrip = new Array('b', 'i', 'u');
var complexstrip = new Array('font', 'color', 'size');
var str = getSel();
if(str === false) {
return;
}
for(var tag in simplestrip) {
str = stripSimple(simplestrip[tag], str);
}
for(var tag in complexstrip) {
str = stripComplex(complexstrip[tag], str);
}
insertText(str);
} else if(!wysiwyg && cmd == 'undo') {
addSnapshot(getEditorContents());
moveCursor(-1);
if((str = getSnapshot()) !== false) {
editdoc.value = str;
}
} else if(!wysiwyg && cmd == 'redo') {
moveCursor(1);
if((str = getSnapshot()) !== false) {
editdoc.value = str;
}
} else if(!wysiwyg && in_array(cmd, ['insertorderedlist', 'insertunorderedlist'])) {
var listtype = cmd == 'insertorderedlist' ? '1' : '';
var opentag = '[list' + (listtype ? ('=' + listtype) : '') + ']\n';
var closetag = '[/list]';
if(txt = getSel()) {
var regex = new RegExp('([\r\n]+|^[\r\n]*)(?!\\[\\*\\]|\\[\\/?list)(?=[^\r\n])', 'gi');
txt = opentag + trim(txt).replace(regex, '$1[*]') + '\n' + closetag;
insertText(txt, strlen(txt), 0);
} else {
insertText(opentag + closetag, opentag.length, closetag.length);
while(listvalue = prompt(lang['enter_list_item'], '')) {
if(is_opera > 8) {
listvalue = '\n' + '[*]' + listvalue;
insertText(listvalue, strlen(listvalue) + 1, 0);
} else {
listvalue = '[*]' + listvalue + '\n';
insertText(listvalue, strlen(listvalue), 0);
}
}
}
} else if(!wysiwyg && cmd == 'outdent') {
var sel = getSel();
sel = stripSimple('indent', sel, 1);
insertText(sel);
} else if(cmd == 'createlink') {
insertlink('createlink');
} else if(!wysiwyg && cmd == 'unlink') {
var sel = getSel();
sel = stripSimple('url', sel);
sel = stripComplex('url', sel);
insertText(sel);
} else if(cmd == 'email') {
insertlink('email');
} else if(cmd == 'insertimage') {
insertlink('insertimage');
} else if(cmd == 'table') {
if(wysiwyg) {
var selection = getSel();
if(is_ie) {
var pos = getCaret();
}
var ctrlid = editorid + '_cmd_table';
var str = '表格行数: <input type="text" id="' + ctrlid + '_param_rows" size="10" value="2" /> 表格列数: <input type="text" id="' + ctrlid + '_param_columns" size="10" value="2" /><br />表格宽度: <input type="text" id="' + ctrlid + '_param_width" size="10" value="" /> 背景颜色: <input type="text" id="' + ctrlid + '_param_bgcolor" size="10" />';
var div = editorMenu(ctrlid, str);
$(ctrlid + '_param_rows').focus();
var params = ['rows', 'columns', 'width', 'bgcolor'];
for(var i = 0; i < 4; i++) {$(ctrlid + '_param_' + params[i]).onkeydown = editorMenuEvent_onkeydown;}
$(ctrlid + '_submit').onclick = function() {
var rows = $(ctrlid + '_param_rows').value;
var columns = $(ctrlid + '_param_columns').value;
var width = $(ctrlid + '_param_width').value;
var bgcolor = $(ctrlid + '_param_bgcolor').value;
rows = /^[-\+]?\d+$/.test(rows) && rows > 0 && rows <= 30 ? rows : 2;
columns = /^[-\+]?\d+$/.test(columns) && columns > 0 && columns <= 30 ? columns : 2;
width = width.substr(width.length - 1, width.length) == '%' ? (width.substr(0, width.length - 1) <= 98 ? width : '98%') : (width <= 560 ? width : '98%');
bgcolor = /[\(\)%,#\w]+/.test(bgcolor) ? bgcolor : '';
var html = '<table cellspacing="0" cellpadding="0" width="' + (width ? width : '50%') + '" class="t_table"' + (bgcolor ? ' bgcolor="' + bgcolor + '"' : '') + '>';
for (var row = 0; row < rows; row++) {
html += '<tr>\n';
for (col = 0; col < columns; col++) {
html += '<td> </td>\n';
}
html+= '</tr>\n';
}
html += '</table>\n';
insertText(html);
hideMenu();
document.body.removeChild(div);
}
}
return false;
} else if(cmd == 'floatleft' || cmd == 'floatright') {
if(wysiwyg) {
var selection = getSel();
if(selection) {
insertText('<br style="clear: both"><span style="float: ' + cmd.substr(5) + '">' + selection + '</span>', true);
}
} else {
return applyFormat(cmd, false);
}
} else {
try {
var ret = applyFormat(cmd, false, (isUndefined(arg) ? true : arg));
} catch(e) {
var ret = false;
}
}
if(cmd != 'undo') {
addSnapshot(getEditorContents());
}
if(wysiwyg) {
setContext(cmd);
if(cmd == 'forecolor') {
$(editorid + '_color_bar').style.backgroundColor = arg;
}
}
//checkFocus();
return ret;
}
function setContext(cmd) {
var contextcontrols = new Array('bold', 'italic', 'underline', 'justifyleft', 'justifycenter', 'justifyright', 'insertorderedlist', 'insertunorderedlist');
for(var i in contextcontrols) {
var obj = $(editorid + '_cmd_' + contextcontrols[i]);
if(obj != null) {
try {
var state = editdoc.queryCommandState(contextcontrols[i]);
} catch(e) {
var state = false;
}
if(isUndefined(obj.state)) {
obj.state = false;
}
if(obj.state != state) {
obj.state = state;
buttonContext(obj, (obj.id.substr(obj.id.indexOf('_cmd_') + 5) == cmd ? 'mouseover' : 'mouseout'));
}
}
}
var fs = editdoc.queryCommandValue('fontname');
if(fs == '' && !is_ie && window.getComputedStyle) {
fs = editdoc.body.style.fontFamily;
} else if(fs == null) {
fs = '';
}
fs = fs ? fs : '字体';
if(fs != $(editorid + '_font_out').fontstate) {
thingy = fs.indexOf(',') > 0 ? fs.substr(0, fs.indexOf(',')) : fs;
$(editorid + '_font_out').innerHTML = thingy;
$(editorid + '_font_out').fontstate = fs;
}
var ss = editdoc.queryCommandValue('fontsize');
if(ss == null || ss == '') {
ss = formatFontsize(editdoc.body.style.fontSize);
}
if(ss != $(editorid + '_size_out').sizestate) {
if($(editorid + '_size_out').sizestate == null) {
$(editorid + '_size_out').sizestate = '';
}
$(editorid + '_size_out').innerHTML = ss;
$(editorid + '_size_out').sizestate = ss;
}
var cs = editdoc.queryCommandValue('forecolor');
$(editorid + '_color_bar').style.backgroundColor = rgbToColor(cs);
}
function buttonContext(obj, state) {
if(state == 'mouseover') {
obj.style.cursor = 'pointer';
var mode = obj.state ? 'down' : 'hover';
if(obj.mode != mode) {
obj.mode = mode;
obj.className = 'hover';
}
} else {
var mode = obj.state ? 'selected' : 'normal';
if(obj.mode != mode) {
obj.mode = mode;
obj.className = mode == 'selected' ? 'hover' : '';
}
}
}
function menuContext(obj, state) {
obj.style.cursor = 'pointer';
var mode = state == 'mouseover' ? 'hover' : 'normal';
obj.className = 'editor_button' + mode;
var tds = obj.getElementsByTagName('td');
for(var i = 0; i < tds.length; i++) {
if(tds[i].id.substr(0, tds[i].id.lastIndexOf('_')) == editorid + '_menu') {
tds[i].className = 'editor_menu' + mode;
} else if(tds[i].id == editorid + '_colormenu') {
tds[i].className = 'editor_colormenu' + mode;
}
}
}
function colorContext(obj, state) {
obj.style.cursor = 'pointer';
var mode = state == 'mouseover' ? 'hover' : 'normal';
obj.className = 'editor_color' + mode;
}
function getSel() {
if(wysiwyg) {
if(is_moz || is_opera) {
selection = editwin.getSelection();
checkFocus();
range = selection ? selection.getRangeAt(0) : editdoc.createRange();
return readNodes(range.cloneContents(), false);
} else {
var range = editdoc.selection.createRange();
if(range.htmlText && range.text) {
return range.htmlText;
} else {
var htmltext = '';
for(var i = 0; i < range.length; i++) {
htmltext += range.item(i).outerHTML;
}
return htmltext;
}
}
} else {
if(!isUndefined(editdoc.selectionStart)) {
return editdoc.value.substr(editdoc.selectionStart, editdoc.selectionEnd - editdoc.selectionStart);
} else if(document.selection && document.selection.createRange) {
return document.selection.createRange().text;
} else if(window.getSelection) {
return window.getSelection() + '';
} else {
return false;
}
}
}
function insertText(text, movestart, moveend, select, sel) {
if(wysiwyg) {
if(is_moz || is_opera) {
applyFormat('removeformat');
var fragment = editdoc.createDocumentFragment();
var holder = editdoc.createElement('span');
holder.innerHTML = text;
while(holder.firstChild) {
fragment.appendChild(holder.firstChild);
}
insertNodeAtSelection(fragment);
} else {
checkFocus();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -