/* * The "codemirror" plugin. It's indented to enhance the * "sourcearea" editing mode, which displays the xhtml source code with * syntax highlight and line numbers. * Licensed under the MIT license * CodeMirror Plugin: http://codemirror.net/ (MIT License) */ (function() { CKEDITOR.plugins.add('codemirror', { icons: 'searchcode,autoformat,commentselectedrange,uncommentselectedrange,autocomplete', // %REMOVE_LINE_CORE% lang: 'af,ar,bg,bn,bs,ca,cs,cy,da,de,el,en-au,en-ca,en-gb,en,eo,es,et,eu,fa,fi,fo,fr-ca,fr,gl,gu,he,hi,hr,hu,is,it,ja,ka,km,ko,ku,lt,lv,mk,mn,ms,nb,nl,no,pl,pt-br,pt,ro,ru,sk,sl,sr-latn,sr,sv,th,tr,ug,uk,vi,zh-cn,zh', // %REMOVE_LINE_CORE% version: 1.15, init: function (editor) { var rootPath = this.path, defaultConfig = { extraKeys: { 'Ctrl-S': function () { AveDocs.documentSaveFunction(); }, 'Cmd-S': function () { AveDocs.documentSaveFunction(); }, }, autoCloseBrackets: true, autoCloseTags: true, autoFormatOnStart: false, autoFormatOnUncomment: true, continueComments: true, enableCodeFolding: true, enableCodeFormatting: true, enableSearchTools: true, enterMode: 'keep', highlightMatches: true, indentUnit: 4, indentWithTabs: true, lineNumbers: true, lineWrapping: true, mode: 'htmlmixed', matchBrackets: true, matchTags: true, showAutoCompleteButton: true, showCommentButton: true, showFormatButton: true, showSearchButton: true, showTrailingSpace: true, showUncommentButton: true, styleActiveLine: true, tabMode: 'shift', theme: 'default', useBeautify: false }; // Get Config & Lang var config = CKEDITOR.tools.extend(defaultConfig, editor.config.codemirror || {}, true), lang = editor.lang.codemirror; // check for old config settings for legacy support if (editor.config.codemirror_theme) { config.theme = editor.config.codemirror_theme; } if (editor.config.codemirror_autoFormatOnStart) { config.autoFormatOnStart = editor.config.codemirror_autoFormatOnStart; } // automatically switch to bbcode mode if bbcode plugin is enabled if (editor.plugins.bbcode && config.mode.indexOf("bbcode") <= 0) { config.mode = "bbcode"; } // Source mode isn't available in inline mode yet. if (editor.elementMode === CKEDITOR.ELEMENT_MODE_INLINE || editor.plugins.sourcedialog) { // Override Source Dialog CKEDITOR.dialog.add('sourcedialog', function (editor) { var size = CKEDITOR.document.getWindow().getViewPaneSize(), width = Math.min(size.width - 70, 800), height = size.height / 1.5, oldData; function loadCodeMirrorInline(editor, textarea) { window["codemirror_" + editor.id] = CodeMirror.fromTextArea(textarea, { mode: config.mode, matchBrackets: config.matchBrackets, matchTags: config.matchTags, workDelay: 300, workTime: 35, readOnly: editor.readOnly, lineNumbers: config.lineNumbers, lineWrapping: config.lineWrapping, autoCloseTags: config.autoCloseTags, autoCloseBrackets: config.autoCloseBrackets, highlightSelectionMatches: config.highlightMatches, continueComments: config.continueComments, indentWithTabs: config.indentWithTabs, theme: config.theme, showTrailingSpace: config.showTrailingSpace, showCursorWhenSelecting: true, styleActiveLine: config.styleActiveLine, viewportMargin: Infinity, extraKeys: { "Ctrl-Q": function (codeMirror_Editor) { if (config.enableCodeFolding) { window["foldFunc_" + editor.id](codeMirror_Editor, codeMirror_Editor.getCursor().line); } }, 'Ctrl-S': function (codeMirror_Editor) { AveDocs.documentSaveFunction(); }, 'Cmd-S': function (codeMirror_Editor) { AveDocs.documentSaveFunction(); }, }, foldGutter: true, gutters: ["CodeMirror-linenumbers", "CodeMirror-foldgutter"] }); var holderHeight = height + 'px'; var holderWidth = width + 'px'; // Store config so we can access it within commands etc. window["codemirror_" + editor.id].config = config; if (config.autoFormatOnStart) { if (config.useBeautify) { var indent_size = 4, indent_char = ' ', brace_style = 'collapse'; //collapse, expand, end-expand var source = window["codemirror_" + editor.id].getValue(); window["codemirror_" + editor.id].setValue(html_beautify(source, indent_size, indent_char, 120, brace_style)); } else { window["codemirror_" + editor.id].autoFormatAll({ line: 0, ch: 0 }, { line: window["codemirror_" + editor.id].lineCount(), ch: 0 }); } } function getSelectedRange() { return { from: window["codemirror_" + editor.id].getCursor(true), to: window["codemirror_" + editor.id].getCursor(false) }; } window["codemirror_" + editor.id].on("change", function () { window["codemirror_" + editor.id].save(); editor.fire('change', this); }); window["codemirror_" + editor.id].setSize(holderWidth, holderHeight); // Enable Code Folding (Requires 'lineNumbers' to be set to 'true') if (config.lineNumbers && config.enableCodeFolding) { window["codemirror_" + editor.id].on("gutterClick", window["foldFunc_" + editor.id]); } // Run config.onLoad callback, if present. if (typeof config.onLoad === 'function') { config.onLoad(window["codemirror_" + editor.id], editor); } // inherit blur event window["codemirror_" + editor.id].on("blur", function () { editor.fire('blur', this); }); window["codemirror_" + editor.id].on("keypress", function (codeMirror_Editor, evt) { if (config.enableCodeFormatting) { var range = getSelectedRange(); if (evt.type === "keydown" && evt.ctrlKey && evt.keyCode === 75 && !evt.shiftKey && !evt.altKey) { window["codemirror_" + editor.id].commentRange(true, range.from, range.to); } else if (evt.type === "keydown" && evt.ctrlKey && evt.keyCode === 75 && evt.shiftKey && !evt.altKey) { window["codemirror_" + editor.id].commentRange(false, range.from, range.to); if (config.autoFormatOnUncomment) { window["codemirror_" + editor.id].autoFormatRange(range.from, range.to); } } else if (evt.type === "keydown" && evt.ctrlKey && evt.keyCode === 75 && !evt.shiftKey && evt.altKey) { window["codemirror_" + editor.id].autoFormatRange(range.from, range.to); }/* else if (evt.type === "keydown") { CodeMirror.commands.newlineAndIndentContinueMarkdownList(window["codemirror_" + editor.id]); }*/ } }); } return { title: editor.lang.sourcedialog.title, minWidth: width, minHeight: height, resizable : CKEDITOR.DIALOG_RESIZE_NONE, onShow: function () { // Set Elements this.getContentElement('main', 'data').focus(); this.getContentElement('main', 'AutoComplete').setValue(config.autoCloseTags, true); var textArea = this.getContentElement('main', 'data').getInputElement().$; // Load the content this.setValueOf('main', 'data', oldData = editor.getData()); if (!IsStyleSheetAlreadyLoaded(rootPath + 'css/codemirror.min.css')) { CKEDITOR.document.appendStyleSheet(rootPath + 'css/codemirror.min.css'); } if (config.theme.length && config.theme != 'default' && !IsStyleSheetAlreadyLoaded(rootPath + 'theme/' + config.theme + '.css')) { CKEDITOR.document.appendStyleSheet(rootPath + 'theme/' + config.theme + '.css'); } if (typeof (CodeMirror) == 'undefined') { CKEDITOR.scriptLoader.load(rootPath + 'js/codemirror.js', function() { CKEDITOR.scriptLoader.load(getCodeMirrorScripts(), function() { loadCodeMirrorInline(editor, textArea); }); }); } else { //loadCodeMirrorInline(editor, textArea); if (CodeMirror.prototype['autoFormatAll']) { loadCodeMirrorInline(editor, textArea); } else { // loading the add-on scripts. CKEDITOR.scriptLoader.load(getCodeMirrorScripts(), function() { loadCodeMirrorInline(editor, textArea); }); } } }, onCancel: function (event) { if (event.data.hide) { window["codemirror_" + editor.id].toTextArea(); // Free Memory window["codemirror_" + editor.id] = null; editor.fire('blur', this); editor.fire('focus', this); } }, onOk: (function () { function setData(newData) { var that = this; editor.setData(newData, function () { that.hide(); // Ensure correct selection. var range = editor.createRange(); range.moveToElementEditStart(editor.editable()); range.select(); }); } return function () { window["codemirror_" + editor.id].toTextArea(); // Free Memory window["codemirror_" + editor.id] = null; // Remove CR from input data for reliable comparison with editor data. var newData = this.getValueOf('main', 'data').replace(/\r/g, ''); // Avoid unnecessary setData. Also preserve selection // when user changed his mind and goes back to wysiwyg editing. if (newData === oldData) return true; // Set data asynchronously to avoid errors in IE. CKEDITOR.env.ie ? CKEDITOR.tools.setTimeout(setData, 0, this, newData) : setData.call(this, newData); editor.fire('blur', this); editor.fire('focus', this); // Don't let the dialog close before setData is over. return false; }; })(), contents: [{ id: 'main', label: editor.lang.sourcedialog.title, elements: [ { type: 'hbox', style: 'width: 80px;margin:0;', widths: ['20px', '20px', '20px', '20px'], children: [ { type: 'button', id: 'searchCode', label: '', title: lang.searchCode, 'class': 'searchCodeButton', onClick: function() { CodeMirror.commands.find(window["codemirror_" + editor.id]); } }, { type: 'button', id: 'autoFormat', label: '', title: lang.autoFormat, 'class': 'autoFormat', onClick: function() { var range = { from: window["codemirror_" + editor.id].getCursor(true), to: window["codemirror_" + editor.id].getCursor(false) }; window["codemirror_" + editor.id].autoFormatRange(range.from, range.to); } }, { type: 'button', id: 'CommentSelectedRange', label: '', title: lang.commentSelectedRange, 'class': 'CommentSelectedRange', onClick: function () { var range = { from: window["codemirror_" + editor.id].getCursor(true), to: window["codemirror_" + editor.id].getCursor(false) }; window["codemirror_" + editor.id].commentRange(true, range.from, range.to); } }, { type: 'button', id: 'UncommentSelectedRange', label: '', title: lang.uncommentSelectedRange, 'class': 'UncommentSelectedRange', onClick: function () { var range = { from: window["codemirror_" + editor.id].getCursor(true), to: window["codemirror_" + editor.id].getCursor(false) }; window["codemirror_" + editor.id].commentRange(false, range.from, range.to); if (window["codemirror_" + editor.id].config.autoFormatOnUncomment) { window["codemirror_" + editor.id].autoFormatRange(range.from, range.to); } } }] }, { type: 'checkbox', id: 'AutoComplete', label: lang.autoCompleteToggle, title: lang.autoCompleteToggle, onChange: function () { window["codemirror_" + editor.id].setOption("autoCloseTags", this.getValue()); } }, { type: 'textarea', id: 'data', dir: 'ltr', inputStyle: 'cursor:auto;' + 'width:' + width + 'px;' + 'height:' + height + 'px;' + 'tab-size:4;' + 'text-align:left;', 'class': 'cke_source cke_enable_context_menu' } ] }] }; }); // return; } /* // Override Copy Button if (editor.commands.copy) { editor.commands.copy.modes = { wysiwyg: 1, source: 1 }; // TODO } // Override Paste Button if (editor.commands.paste) { editor.commands.paste.modes = { wysiwyg: 1, source: 1 }; // TODO } // Override Cut Button if (editor.commands.cut) { editor.commands.cut.modes = { wysiwyg: 1, source: 1 }; // TODO }*/ // Override Find Button if (editor.commands.find) { editor.commands.find.modes = { wysiwyg: 1, source: 1 }; editor.commands.find.exec = function() { if (editor.mode === 'wysiwyg') { editor.openDialog('find'); } else { CodeMirror.commands.find(window["codemirror_" + editor.id]); } }; } // Override Replace Button if (editor.commands.replace) { editor.commands.replace.modes = { wysiwyg: 1, source: 1 }; editor.commands.replace.exec = function () { if (editor.mode === 'wysiwyg') { editor.openDialog('replace'); } else { CodeMirror.commands.replace(window["codemirror_" + editor.id]); } }; } var sourcearea = CKEDITOR.plugins.sourcearea; // check if sourcearea plugin is overrriden if (!sourcearea.commands.searchCode) { CKEDITOR.plugins.sourcearea.commands = { source: { modes: { wysiwyg: 1, source: 1 }, editorFocus: false, readOnly: 1, exec: function(editorInstance) { if (editorInstance.mode === 'wysiwyg') { editorInstance.fire('saveSnapshot'); } editorInstance.getCommand('source').setState(CKEDITOR.TRISTATE_DISABLED); editorInstance.setMode(editorInstance.mode === 'source' ? 'wysiwyg' : 'source'); }, canUndo: false }, searchCode: { modes: { wysiwyg: 0, source: 1 }, editorFocus: false, readOnly: 1, exec: function (editorInstance) { CodeMirror.commands.find(window["codemirror_" + editorInstance.id]); }, canUndo: true }, autoFormat: { modes: { wysiwyg: 0, source: 1 }, editorFocus: false, readOnly: 1, exec: function (editorInstance) { var range = { from: window["codemirror_" + editorInstance.id].getCursor(true), to: window["codemirror_" + editorInstance.id].getCursor(false) }; window["codemirror_" + editorInstance.id].autoFormatRange(range.from, range.to); }, canUndo: true }, commentSelectedRange: { modes: { wysiwyg: 0, source: 1 }, editorFocus: false, readOnly: 1, exec: function (editorInstance) { var range = { from: window["codemirror_" + editorInstance.id].getCursor(true), to: window["codemirror_" + editorInstance.id].getCursor(false) }; window["codemirror_" + editorInstance.id].commentRange(true, range.from, range.to); }, canUndo: true }, uncommentSelectedRange: { modes: { wysiwyg: 0, source: 1 }, editorFocus: false, readOnly: 1, exec: function (editorInstance) { var range = { from: window["codemirror_" + editorInstance.id].getCursor(true), to: window["codemirror_" + editorInstance.id].getCursor(false) }; window["codemirror_" + editorInstance.id].commentRange(false, range.from, range.to); if (window["codemirror_" + editorInstance.id].config.autoFormatOnUncomment) { window["codemirror_" + editorInstance.id].autoFormatRange( range.from, range.to); } }, canUndo: true }, autoCompleteToggle: { modes: { wysiwyg: 0, source: 1 }, editorFocus: false, readOnly: 1, exec: function (editorInstance) { if (this.state == CKEDITOR.TRISTATE_ON) { window["codemirror_" + editorInstance.id].setOption("autoCloseTags", false); } else if (this.state == CKEDITOR.TRISTATE_OFF) { window["codemirror_" + editorInstance.id].setOption("autoCloseTags", true); } this.toggleState(); }, canUndo: true } }; } editor.addMode('source', function (callback) { if (!IsStyleSheetAlreadyLoaded(rootPath + 'css/codemirror.min.css')) { CKEDITOR.document.appendStyleSheet(rootPath + 'css/codemirror.min.css'); } if (config.theme.length && config.theme != 'default' && !IsStyleSheetAlreadyLoaded(rootPath + 'theme/' + config.theme + '.css')) { CKEDITOR.document.appendStyleSheet(rootPath + 'theme/' + config.theme + '.css'); } if (typeof (CodeMirror) == 'undefined') { CKEDITOR.scriptLoader.load(rootPath + 'js/codemirror.js', function() { CKEDITOR.scriptLoader.load(getCodeMirrorScripts(), function() { loadCodeMirror(editor); callback(); }); }); } else { if (CodeMirror.prototype['autoFormatAll']) { loadCodeMirror(editor); callback(); } else { // loading the add-on scripts. CKEDITOR.scriptLoader.load(getCodeMirrorScripts(), function() { loadCodeMirror(editor); callback(); }); } } }); function getCodeMirrorScripts() { var scriptFiles = [rootPath + 'js/codemirror.addons.min.js']; switch (config.mode) { case "bbcode": { scriptFiles.push(rootPath + 'js/codemirror.mode.bbcode.min.js'); } break; case "bbcodemixed": { scriptFiles.push(rootPath + 'js/codemirror.mode.bbcodemixed.min.js'); } break; case "htmlmixed": { scriptFiles.push(rootPath + 'js/codemirror.mode.htmlmixed.min.js'); } break; case "text/html": { scriptFiles.push(rootPath + 'js/codemirror.mode.htmlmixed.min.js'); } break; case "application/x-httpd-php": { scriptFiles.push(rootPath + 'js/codemirror.mode.php.min.js'); } break; case "text/javascript": { scriptFiles.push(rootPath + 'js/codemirror.mode.javascript.min.js'); } break; default: scriptFiles.push(rootPath + 'js/codemirror.mode.htmlmixed.min.js'); } if (config.useBeautify) { scriptFiles.push(rootPath + 'js/beautify.min.js'); } if (config.enableSearchTools) { scriptFiles.push(rootPath + 'js/codemirror.addons.search.min.js'); } return scriptFiles; } function loadCodeMirror(editor) { var contentsSpace = editor.ui.space('contents'), textarea = contentsSpace.getDocument().createElement('textarea'); textarea.setStyles( CKEDITOR.tools.extend({ // IE7 has overflow the