/** * 源码编辑插件 * @file * @since 1.2.6.1 */ (function (){ var sourceEditors = { textarea: function (editor, holder){ var textarea = holder.ownerDocument.createElement('textarea'); textarea.style.cssText = 'position:absolute;resize:none;width:100%;height:100%;border:0;padding:0;margin:0;overflow-y:auto;'; // todo: IE下只有onresize属性可用... 很纠结 if (browser.ie && browser.version < 8) { textarea.style.width = holder.offsetWidth + 'px'; textarea.style.height = holder.offsetHeight + 'px'; holder.onresize = function (){ textarea.style.width = holder.offsetWidth + 'px'; textarea.style.height = holder.offsetHeight + 'px'; }; } holder.appendChild(textarea); return { setContent: function (content){ textarea.value = content; }, getContent: function (){ return textarea.value; }, select: function (){ var range; if (browser.ie) { range = textarea.createTextRange(); range.collapse(true); range.select(); } else { //todo: chrome下无法设置焦点 textarea.setSelectionRange(0, 0); textarea.focus(); } }, dispose: function (){ holder.removeChild(textarea); // todo holder.onresize = null; textarea = null; holder = null; } }; }, codemirror: function (editor, holder){ var codeEditor = window.CodeMirror(holder, { mode: "text/html", tabMode: "indent", lineNumbers: true, lineWrapping:true }); var dom = codeEditor.getWrapperElement(); dom.style.cssText = 'position:absolute;left:0;top:0;width:100%;height:100%;font-family:consolas,"Courier new",monospace;font-size:13px;'; codeEditor.getScrollerElement().style.cssText = 'position:absolute;left:0;top:0;width:100%;height:100%;'; codeEditor.refresh(); return { getCodeMirror:function(){ return codeEditor; }, setContent: function (content){ codeEditor.setValue(content); }, getContent: function (){ return codeEditor.getValue(); }, select: function (){ codeEditor.focus(); }, dispose: function (){ holder.removeChild(dom); dom = null; codeEditor = null; } }; } }; UE.plugins['source'] = function (){ var me = this; var opt = this.options; var sourceMode = false; var sourceEditor; var orgSetContent; opt.sourceEditor = browser.ie ? 'textarea' : (opt.sourceEditor || 'codemirror'); me.setOpt({ sourceEditorFirst:false }); function createSourceEditor(holder){ return sourceEditors[opt.sourceEditor == 'codemirror' && window.CodeMirror ? 'codemirror' : 'textarea'](me, holder); } var bakCssText; //解决在源码模式下getContent不能得到最新的内容问题 var oldGetContent, bakAddress; /** * 切换源码模式和编辑模式 * @command source * @method execCommand * @param { String } cmd 命令字符串 * @example * ```javascript * editor.execCommand( 'source'); * ``` */ /** * 查询当前编辑区域的状态是源码模式还是可视化模式 * @command source * @method queryCommandState * @param { String } cmd 命令字符串 * @return { int } 如果当前是源码编辑模式,返回1,否则返回0 * @example * ```javascript * editor.queryCommandState( 'source' ); * ``` */ me.commands['source'] = { execCommand: function (){ sourceMode = !sourceMode; if (sourceMode) { bakAddress = me.selection.getRange().createAddress(false,true); me.undoManger && me.undoManger.save(true); if(browser.gecko){ me.body.contentEditable = false; } bakCssText = me.iframe.style.cssText; me.iframe.style.cssText += 'position:absolute;left:-32768px;top:-32768px;'; me.fireEvent('beforegetcontent'); var root = UE.htmlparser(me.body.innerHTML); me.filterOutputRule(root); root.traversal(function (node) { if (node.type == 'element') { switch (node.tagName) { case 'td': case 'th': case 'caption': if(node.children && node.children.length == 1){ if(node.firstChild().tagName == 'br' ){ node.removeChild(node.firstChild()) } }; break; case 'pre': node.innerText(node.innerText().replace(/ /g,' ')) } } }); me.fireEvent('aftergetcontent'); var content = root.toHtml(true); sourceEditor = createSourceEditor(me.iframe.parentNode); sourceEditor.setContent(content); orgSetContent = me.setContent; me.setContent = function(html){ //这里暂时不触发事件,防止报错 var root = UE.htmlparser(html); me.filterInputRule(root); html = root.toHtml(); sourceEditor.setContent(html); }; setTimeout(function (){ sourceEditor.select(); me.addListener('fullscreenchanged', function(){ try{ sourceEditor.getCodeMirror().refresh() }catch(e){} }); }); //重置getContent,源码模式下取值也能是最新的数据 oldGetContent = me.getContent; me.getContent = function (){ return sourceEditor.getContent() || '
' + (browser.ie ? '' : '
')+'
' + (browser.ie ? '' : '
')+'
'+(browser.ie?'':'
')+'