📄 yqweditor.js
字号:
}
// destroy popups
for (var menu in this.popups){
this.destroyPopup(menu);
}
if (this.fontoptions){
for (var i in this.fontoptions){
if (i != ''){
this.fontoptions[i].parentNode.removeChild(this.fontoptions[i]);
}
}
this.fontoptions[''].style.display = '';
}
if (this.sizeoptions){
for(var i=0; i<this.sizeoptions.length; i++){
if (i != ''){
this.sizeoptions[i].parentNode.removeChild(this.sizeoptions[i]);
}
}
this.sizeoptions[''].style.display = '';
}
};
/**
* Collapse the current selection and place the cursor where the end of the
* selection was.
*/
this.collapseSelectionEnd = function()
{
if (this.editDoc.selection)
{
var range = this.editDoc.selection.createRange();
// fix for horribly confusing IE bug where it randomly makes text white for funsies
// see 3.5 bug #279
eval("range." + "move" + "('character', -1);");
range.collapse(false);
range.select();
}
else if (document.selection && document.selection.createRange)
{
var range = document.selection.createRange();
range.collapse(false);
range.select();
}
else if (typeof(this.editDoc.selectionStart) != 'undefined')
{
var opn = this.editDoc.selectionStart + 0;
var sel_text = this.editDoc.value.substr(this.editDoc.selectionStart, this.editDoc.selectionEnd - this.editDoc.selectionStart);
this.editDoc.selectionStart = this.editDoc.selectionStart + sel_text.vBlength();
}
else if (window.getSelection)
{
// don't think I can do anything for this
}
}
/************************************************/
/**
* WYSIWYG editor start
* What You See Is What You Get ^_^
*/
if(this.wysiwygMode){
/**
* Enables the editor
* @param string Initial text for the editor
*/
this.enableEditor = function(text)
{
if (typeof text != 'undefined'){this.setEditorContents(text);}
var hider = c.o(this.editorId + '_hider');
if (hider){hider.parentNode.removeChild(hider);}
this.disabled = false;
};
/**
* Disables the editor
* @param string Initial text for the editor
*/
this.disableEditor = function(text)
{
if (!this.disabled)
{
this.disabled = true;
var hider = c.o(this.editorId + '_hider');
if (hider)
{
hider.parentNode.removeChild(hider);
}
var div = document.createElement('div');
div.id = this.editorId + '_hider';
div.className = 'wysiwyg';
div.style.border = '2px inset';
div.style.width = this.editbox.style.width;
div.style.height = this.editbox.style.height;
var childdiv = document.createElement('div');
childdiv.style.margin = '8px';
childdiv.innerHTML = text;
div.appendChild(childdiv);
this.editbox.parentNode.appendChild(div);
this.editbox.style.width = '0px';
this.editbox.style.height = '0px';
this.editbox.style.border = 'none';
}
};
/* WYSIWYG mode */
this.writeEditorContents = function(text, doinit)
{
if (text == ''){
if (is_ie){
text = '<p></p>';
}else if (is_moz){
text = '<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();
}
this.setDirection();
};
/* WYSIWYG mode */
this.setEditorContents = function(initial_text)
{
if (c.o(this.editorId + '_iframe'))
{
this.editBox = c.o(this.editorId + '_iframe');
}
else
{
var iframe = document.createElement('iframe');
if (is_ie && window.location.protocol == 'https:')
{
// workaround for IE throwing insecure page warnings
iframe.src = 'javascript:false;';
}
this.editBox = this.textObj.parentNode.appendChild(iframe);
this.editBox.id = this.editorId + '_iframe';
this.editBox.tabIndex = 1;
}
if (!is_ie)
{
this.editBox.style.border = '2px inset';
}
this.editBox.style.width = this.textObj.style.width;
this.editBox.style.height = this.textObj.style.height;
this.textObj.style.display = 'none';
this.editBox.className = "editBox";
this.editWin = this.editBox.contentWindow;
this.editDoc = this.editWin.document;
this.writeEditorContents((typeof initial_text == 'undefined' ? this.textObj.value : initial_text), true);
if (this.editDoc.dir == 'rtl')
{
// this.editDoc.execCommand('justifyright', false, null);
}
this.spellObj = this.editDoc.body;
this.editDoc.editorId = this.editorId;
this.editWin.editorId = this.editorId;
this.editDoc.body.className="editorCore";
};
/**
* 设置编辑器样式
* WYSIWYG mode
*/
this.setEditorStyle = function()
{
if (document.styleSheets['YeQiangWeiEditor'])
{
this.editDoc.createStyleSheet().cssText = document.styleSheets['YeQiangWeiEditor'].cssText + ' p { margin: 1px; }';
this.editDoc.body.className = 'editor';
}
};
/**
* Sets the text direction for the editor
* WYSIWYG mode
*/
this.setDirection = function()
{
this.editDoc.dir = this.textObj.dir;
};
/**
* Init Editor Functions
* WYSIWYG mode
*/
this.setEditorFunctions = function()
{
this.editDoc.onmouseup = YeQiangWeiEditor_Events.prototype.editDoc_onmouseup;
this.editDoc.onkeyup = YeQiangWeiEditor_Events.prototype.editDoc_onkeyup;
if (this.editDoc.attachEvent){
this.editDoc.body.attachEvent("onresizestart", YeQiangWeiEditor_Events.prototype.editDoc_onresizestart);
}
this.editDoc.onkeydown = YeQiangWeiEditor_Events.prototype.editDoc_onkeydown;
this.editWin.onfocus = YeQiangWeiEditor_Events.prototype.editWin_onfocus;
this.editWin.onblur = YeQiangWeiEditor_Events.prototype.editWin_onblur;
};
/**
* Set Context
* WYSIWYG mode
*/
this.setContext = function(cmd)
{
for (var i in contextcontrols)
{
var obj = c.o(this.editorId + '_cmd_' + contextcontrols[i]);
if (obj != null)
{
state = this.editDoc.queryCommandState(contextcontrols[i]);
if (obj.state != state)
{
obj.state = state;
this.buttonContext(obj, (obj.cmd == cmd ? 'mouseover' : 'mouseout'));
}
}
}
this.setColorContext();
this.setFontContext();
this.setSizeContext();
};
/**
* Set Font Context
* WYSIWYG mode
*/
this.setFontContext = function(fontstate)
{
if (this.buttons['fontname'])
{
if (typeof fontstate == 'undefined')
{
fontstate = this.editDoc.queryCommandValue('fontname');
}
switch (fontstate)
{
case '':
{
if (!is_ie && window.getComputedStyle)
{
fontstate = this.editDoc.body.style.fontFamily;
}
}
break;
case null:
{
fontstate = '';
}
break;
}
if (fontstate != this.fontstate)
{
this.fontstate = fontstate;
var thingy = JAVA.ucfirst(this.fontstate, ',');
if (this.popupmode)
{
for (var i in this.fontoptions)
{
this.fontoptions[i].style.display = (i == thingy ? '' : 'none');
}
}
else
{
for (var i = 0; i < this.buttons['fontname'].options.length; i++)
{
if (this.buttons['fontname'].options[i].value == thingy)
{
this.buttons['fontname'].selectedIndex = i;
break;
}
}
}
}
}
};
/**
* Set Size Context
* WYSIWYG mode
*/
this.setSizeContext = function(sizestate){
if (this.buttons['fontsize']){
if (typeof sizestate == 'undefined'){
sizestate = this.editDoc.queryCommandValue('fontsize');
}
switch (sizestate){
case null:
case '':
{
if (is_moz)
{
sizestate = this.translateFontsize(this.editDoc.body.style.fontSize);
}
}
break;
}
if (sizestate != this.sizestate)
{
this.sizestate = sizestate;
if (this.popupmode)
{
for(var i=0; i<this.sizeoptions.length; i++)
{
this.sizeoptions[i].style.display = (i == this.sizestate ? '' : 'none');
}
}
else
{
for (var i = 0; i < this.buttons['fontsize'].options.length; i++)
{
if (this.buttons['fontsize'].options[i].value == this.sizestate)
{
this.buttons['fontsize'].selectedIndex = i;
break;
}
}
}
}
}
};
/**
* Set Color Context
* WYSIWYG mode
*/
this.setColorContext = function(colorstate)
{
if (this.buttons['forecolor'])
{
if (typeof colorstate == 'undefined')
{
colorstate = this.editDoc.queryCommandValue('forecolor');
}
if (colorstate != this.colorstate){
if (this.popupmode){
var obj = c.o(this.editorId + '_color_' + this.translate_color_commandvalue(this.colorstate));
if (obj != null){
obj.state = false;
this.buttonContext(obj, 'mouseout', 'menu');
}
this.colorstate = colorstate;
elmid = this.editorId + '_color_' + this.translate_color_commandvalue(colorstate);
var obj = c.o(elmid);
if (obj != null){
obj.state = true;
this.buttonContext(obj, 'mouseout', 'menu');
}
}else{
this.colorstate = colorstate;
colorstate = this.translate_color_commandvalue(this.colorstate);
for (var i = 0; i < this.buttons['forecolor'].options.length; i++){
if (this.buttons['forecolor'].options[i].value == colorstate){
this.buttons['forecolor'].selectedIndex = i;
break;
}
}
}
}
}
};
/**
* Function to translate the output from queryCommandState('forecolor') into something useful
* @param string Output from queryCommandState('forecolor')
* @returnstring
* WYSIWYG mode
*/
this.translate_color_commandvalue = function(forecolor)
{
return this.translate_silly_hex((forecolor & 0xFF).toString(16), ((forecolor >> 8) & 0xFF).toString(16), ((forecolor >> 16) & 0xFF).toString(16));
};
/**
* Function to translate a hex like F AB 9 to #0FAB09 and then to coloroptions['#0FAB09']
* @param string Red value
* @param string Green value
* @param string Blue value
* @return string Option from coloroptions array
* WYSIWYG mode
*/
this.translate_silly_hex = function(r, g, b)
{
return coloroptions['#' + (JAVA.strPad(r, 2, 0) + JAVA.strPad(g, 2, 0) + JAVA.strPad(b, 2, 0)).toUpperCase()];
};
/**
* Apply Formatting
* WYSIWYG mode
*/
this.applyFormat = function(cmd, dialog, argument)
{
this.editDoc.execCommand(cmd, (typeof dialog == 'undefined' ? false : dialog), (typeof argument == 'undefined' ? true : argument));
return false;
};
/**
* Insert Link
* WYSIWYG mode
*/
this.createlink = function(e, url)
{
return this.applyFormat('createlink', is_ie, (typeof url == 'undefined' ? true : url));
};
/**
* Insert Email Link
* WYSIWYG mode
*/
this.email = function(e, email)
{
if (typeof email == 'undefined')
{
email = this.showPrompt(vbphrase['enter_email_link'], '');
}
email = this.verifyPrompt(email);
if (email == false)
{
return this.applyFormat('unlink');
}
else
{
var selection = this.get_selection();
return this.insertText('<a href="mailto:' + email + '">' + (selection ? selection : email) + '</a>', (selection ? true : false));
}
};
/**
* Get Editor Contents
* WYSIWYG mode
*/
this.getEditorContents = function()
{
return this.editDoc.body.innerHTML;
};
/**
* Get Selected Text
* WYSIWYG mode
*/
this.get_selection = function()
{
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 =
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -