diff --git a/editormd.js b/editormd.js index 67882e1..8db2b60 100644 --- a/editormd.js +++ b/editormd.js @@ -1,8 +1,8 @@ /* * Editor.md * - * @file editormd.js - * @version v1.4.4 + * @file editormd.js + * @version v1.4.4 * @description Open source online markdown editor. * @license MIT License * @author Pandao @@ -12,10 +12,10 @@ ;(function(factory) { "use strict"; - + // CommonJS/Node.js if (typeof require === "function" && typeof exports === "object" && typeof module === "object") - { + { module.exports = factory; } else if (typeof define === "function") // AMD/CMD/Sea.js @@ -23,60 +23,60 @@ if (define.amd) // for Require.js { /* Require.js define replace */ - } - else + } + else { define(["jquery"], factory); // for Sea.js } - } + } else - { + { window.editormd = factory(); } - -}(function() { + +}(function() { /* Require.js assignment replace */ - + "use strict"; - + var $ = (typeof (jQuery) !== "undefined") ? jQuery : Zepto; if (typeof ($) === "undefined") { return ; } - + /** * editormd - * + * * @param {String} id 编辑器的ID * @param {Object} options 配置选项 Key/Value * @returns {Object} editormd 返回editormd对象 */ - + var editormd = function (id, options) { return new editormd.fn.init(id, options); }; - + editormd.title = editormd.$name = "Editor.md"; editormd.version = "1.4.4"; editormd.homePage = "https://pandao.github.io/editor.md/"; editormd.classPrefix = "editormd-"; - + editormd.toolbarModes = { full : [ - "undo", "redo", "|", - "bold", "del", "italic", "quote", "ucwords", "uppercase", "lowercase", "|", - "h1", "h2", "h3", "h4", "h5", "h6", "|", + "undo", "redo", "|", + "bold", "del", "italic", "quote", "ucwords", "uppercase", "lowercase", "|", + "h1", "h2", "h3", "h4", "h5", "h6", "|", "list-ul", "list-ol", "hr", "|", "link", "reference-link", "image", "code", "preformatted-text", "code-block", "table", "datetime", "emoji", "html-entities", "pagebreak", "|", "goto-line", "watch", "preview", "fullscreen", "clear", "search", "|", "help", "info" ], simple : [ - "undo", "redo", "|", - "bold", "del", "italic", "quote", "uppercase", "lowercase", "|", - "h1", "h2", "h3", "h4", "h5", "h6", "|", + "undo", "redo", "|", + "bold", "del", "italic", "quote", "uppercase", "lowercase", "|", + "h1", "h2", "h3", "h4", "h5", "h6", "|", "list-ul", "list-ol", "hr", "|", "watch", "preview", "fullscreen", "|", "help", "info" @@ -87,7 +87,7 @@ "help", "info" ] }; - + editormd.defaults = { mode : "gfm", //gfm or markdown theme : "default", @@ -129,7 +129,7 @@ fontSize : "13px", saveHTMLToTextarea : false, disabledKeyMaps : [], - + onload : function() {}, onresize : function() {}, onchange : function() {}, @@ -141,20 +141,20 @@ onfullscreenExit : function() {}, onscroll : function() {}, onpreviewscroll : function() {}, - + imageUpload : false, imageFormats : ["jpg", "jpeg", "gif", "png", "bmp", "webp"], imageUploadURL : "", crossDomainUpload : false, uploadCallbackURL : "", - + toc : true, // Table of contents tocm : false, // Using [TOCM], auto create ToC dropdown menu tocTitle : "", // for ToC dropdown menu btn tocDropdown : false, tocContainer : "", tocStartLevel : 1, // Said from H1 to create ToC - htmlDecode : false, // Open the HTML tag identification + htmlDecode : false, // Open the HTML tag identification pageBreak : true, // Enable parse page break [========] atLink : true, // for @link emailLink : true, // for email address auto link @@ -166,7 +166,7 @@ flowChart : false, // flowChart.js only support IE9+ sequenceDiagram : false, // sequenceDiagram.js only support IE9+ previewCodeHighlight : true, - + toolbar : true, // show/hide toolbar toolbarAutoFixed : true, // on window scroll auto fixed position toolbarIcons : "full", @@ -182,7 +182,7 @@ toolbarCustomIcons : { // using html tag create toolbar icon, unused default tag. lowercase : "a", "ucwords" : "Aa" - }, + }, toolbarIconsClass : { undo : "fa-undo", redo : "fa-repeat", @@ -220,9 +220,9 @@ clear : "fa-eraser", help : "fa-question-circle", info : "fa-info-circle" - }, + }, toolbarIconTexts : {}, - + lang : { name : "zh-cn", description : "开源在线Markdown编辑器
Open source online Markdown editor.", @@ -300,11 +300,11 @@ formatNotAllowed : "错误:只允许上传图片文件,允许上传的图片文件格式有:" }, preformattedText : { - title : "添加预格式文本或代码块", + title : "添加预格式文本或代码块", emptyAlert : "错误:请填写预格式文本或代码的内容。" }, codeBlock : { - title : "添加代码块", + title : "添加代码块", selectLabel : "代码语言:", selectDefaultText : "请选择代码语言", otherLanguage : "其他语言", @@ -320,18 +320,18 @@ } } }; - + editormd.classNames = { tex : editormd.classPrefix + "tex" }; editormd.dialogZindex = 99999; - + editormd.$katex = null; editormd.$marked = null; editormd.$CodeMirror = null; editormd.$prettyPrint = null; - + var timer, flowchartTimer; editormd.prototype = editormd.fn = { @@ -341,71 +341,73 @@ preview : false, fullscreen : false }, - + /** * 构造函数/实例初始化 * Constructor / instance initialization - * + * * @param {String} id 编辑器的ID * @param {Object} [options={}] 配置选项 Key/Value * @returns {editormd} 返回editormd的实例对象 */ - + init : function (id, options) { - + options = options || {}; - + if (typeof id === "object") { options = id; } - + var _this = this; - var classPrefix = this.classPrefix = editormd.classPrefix; + var classPrefix = this.classPrefix = editormd.classPrefix; var settings = this.settings = $.extend(true, editormd.defaults, options); - + id = (typeof id === "object") ? settings.id : id; - + var editor = this.editor = $("#" + id); - + this.id = id; this.lang = settings.lang; - + var classNames = this.classNames = { textarea : { html : classPrefix + "html-textarea", markdown : classPrefix + "markdown-textarea" } }; - - settings.pluginPath = (settings.pluginPath === "") ? settings.path + "../plugins/" : settings.pluginPath; - + + settings.pluginPath = (settings.pluginPath === "") ? settings.path + "../plugins/" : settings.pluginPath; + this.state.watching = (settings.watch) ? true : false; - + if (!editor.hasClass("editormd")) { editor.addClass("editormd"); } - + editor.css({ width : (typeof settings.width === "number") ? settings.width + "px" : settings.width, height : (typeof settings.height === "number") ? settings.height + "px" : settings.height }); - + if (settings.autoHeight) { editor.css("height", "auto"); } - + var markdownTextarea = this.markdownTextarea = editor.children("textarea"); - + if (markdownTextarea.length < 1) { editor.append(""); markdownTextarea = this.markdownTextarea = editor.children("textarea"); } - - markdownTextarea.addClass(classNames.textarea.markdown).attr("name", id + "-markdown-doc").attr("placeholder", settings.placeholder); - + if(markdownTextarea.attr("name")=="undefined" || markdownTextarea.attr("name")==""){ + markdownTextarea.attr("name", id + "-markdown-doc"); + } + markdownTextarea.addClass(classNames.textarea.markdown).attr("placeholder", settings.placeholder); + var appendElements = [ (!settings.readOnly) ? "" : "", ( (settings.saveHTMLToTextarea) ? "" : "" ), @@ -413,104 +415,104 @@ "
", "
" ].join("\n"); - + editor.append(appendElements).addClass(classPrefix + "vertical"); - - this.mask = editor.children("." + classPrefix + "mask"); + + this.mask = editor.children("." + classPrefix + "mask"); this.containerMask = editor.children("." + classPrefix + "container-mask"); - + if (settings.markdown !== "") { markdownTextarea.val(settings.markdown); } - + if (settings.appendMarkdown !== "") { markdownTextarea.val(markdownTextarea.val() + settings.appendMarkdown); } - - this.htmlTextarea = editor.children("." + classNames.textarea.html); + + this.htmlTextarea = editor.children("." + classNames.textarea.html); this.preview = editor.children("." + classPrefix + "preview"); this.previewContainer = this.preview.children("." + classPrefix + "preview-container"); - + if (typeof define === "function" && define.amd) { - if (typeof katex !== "undefined") + if (typeof katex !== "undefined") { editormd.$katex = katex; } - - if (settings.searchReplace && !settings.readOnly) + + if (settings.searchReplace && !settings.readOnly) { editormd.loadCSS(settings.path + "codemirror/addon/dialog/dialog"); editormd.loadCSS(settings.path + "codemirror/addon/search/matchesonscrollbar"); } } - + if ((typeof define === "function" && define.amd) || !settings.autoLoadModules) { if (typeof CodeMirror !== "undefined") { editormd.$CodeMirror = CodeMirror; } - + if (typeof marked !== "undefined") { editormd.$marked = marked; } - + this.setCodeMirror().setToolbar().loadedDisplay(); - } - else + } + else { this.loadQueues(); } return this; }, - + /** * 所需组件加载队列 * Required components loading queue - * + * * @returns {editormd} 返回editormd的实例对象 */ - + loadQueues : function() { var _this = this; var settings = this.settings; var loadPath = settings.path; - + var loadFlowChartOrSequenceDiagram = function() { - - if (editormd.isIE8) + + if (editormd.isIE8) { _this.loadedDisplay(); - + return ; } - if (settings.flowChart || settings.sequenceDiagram) + if (settings.flowChart || settings.sequenceDiagram) { editormd.loadScript(loadPath + "raphael.min", function() { - editormd.loadScript(loadPath + "underscore.min", function() { + editormd.loadScript(loadPath + "underscore.min", function() { - if (!settings.flowChart && settings.sequenceDiagram) + if (!settings.flowChart && settings.sequenceDiagram) { editormd.loadScript(loadPath + "sequence-diagram.min", function() { _this.loadedDisplay(); }); } - else if (settings.flowChart && !settings.sequenceDiagram) - { - editormd.loadScript(loadPath + "flowchart.min", function() { + else if (settings.flowChart && !settings.sequenceDiagram) + { + editormd.loadScript(loadPath + "flowchart.min", function() { editormd.loadScript(loadPath + "jquery.flowchart.min", function() { _this.loadedDisplay(); }); }); } - else if (settings.flowChart && settings.sequenceDiagram) - { - editormd.loadScript(loadPath + "flowchart.min", function() { + else if (settings.flowChart && settings.sequenceDiagram) + { + editormd.loadScript(loadPath + "flowchart.min", function() { editormd.loadScript(loadPath + "jquery.flowchart.min", function() { editormd.loadScript(loadPath + "sequence-diagram.min", function() { _this.loadedDisplay(); @@ -521,106 +523,106 @@ }); }); - } + } else { _this.loadedDisplay(); } - }; + }; editormd.loadCSS(loadPath + "codemirror/codemirror.min"); - + if (settings.searchReplace && !settings.readOnly) { editormd.loadCSS(loadPath + "codemirror/addon/dialog/dialog"); editormd.loadCSS(loadPath + "codemirror/addon/search/matchesonscrollbar"); } - + if (settings.codeFold) { - editormd.loadCSS(loadPath + "codemirror/addon/fold/foldgutter"); + editormd.loadCSS(loadPath + "codemirror/addon/fold/foldgutter"); } - + editormd.loadScript(loadPath + "codemirror/codemirror.min", function() { editormd.$CodeMirror = CodeMirror; - + editormd.loadScript(loadPath + "codemirror/modes.min", function() { - + editormd.loadScript(loadPath + "codemirror/addons.min", function() { - + _this.setCodeMirror(); - - if (settings.mode !== "gfm" && settings.mode !== "markdown") + + if (settings.mode !== "gfm" && settings.mode !== "markdown") { _this.loadedDisplay(); - + return false; } - + _this.setToolbar(); editormd.loadScript(loadPath + "marked.min", function() { editormd.$marked = marked; - - if (settings.previewCodeHighlight) + + if (settings.previewCodeHighlight) { editormd.loadScript(loadPath + "prettify.min", function() { loadFlowChartOrSequenceDiagram(); }); - } + } else - { + { loadFlowChartOrSequenceDiagram(); } }); - + }); - + }); - + }); return this; }, - + /** * 设置CodeMirror的主题 * Setting CodeMirror theme - * + * * @returns {editormd} 返回editormd的实例对象 */ - - setTheme : function(theme) { - var settings = this.settings; - settings.theme = theme; - + + setTheme : function(theme) { + var settings = this.settings; + settings.theme = theme; + if (theme !== "default") { editormd.loadCSS(settings.path + "codemirror/theme/" + settings.theme); } - + this.cm.setOption("theme", theme); - + return this; }, - + /** * 配置和初始化CodeMirror组件 * CodeMirror initialization - * + * * @returns {editormd} 返回editormd的实例对象 */ - - setCodeMirror : function() { + + setCodeMirror : function() { var settings = this.settings; var editor = this.editor; - + if (settings.theme !== "default") { editormd.loadCSS(settings.path + "codemirror/theme/" + settings.theme); } - + var codeMirrorConfig = { mode : settings.mode, theme : settings.theme, @@ -633,8 +635,8 @@ lineNumbers : settings.lineNumbers, lineWrapping : settings.lineWrapping, extraKeys : { - "Ctrl-Q": function(cm) { - cm.foldCode(cm.getCursor()); + "Ctrl-Q": function(cm) { + cm.foldCode(cm.getCursor()); } }, foldGutter : settings.codeFold, @@ -647,10 +649,10 @@ showTrailingSpace : settings.showTrailingSpace, highlightSelectionMatches : ( (!settings.matchWordHighlight) ? false : { showToken: (settings.matchWordHighlight === "onselected") ? false : /\w/ } ) }; - + this.codeEditor = this.cm = editormd.$CodeMirror.fromTextArea(this.markdownTextarea[0], codeMirrorConfig); this.codeMirror = this.cmElement = editor.children(".CodeMirror"); - + if (settings.value !== "") { this.cm.setValue(settings.value); @@ -660,7 +662,7 @@ fontSize : settings.fontSize, width : (!settings.watch) ? "100%" : "50%" }); - + if (settings.autoHeight) { this.codeMirror.css("height", "auto"); @@ -669,149 +671,149 @@ return this; }, - + /** * 获取CodeMirror的配置选项 * Get CodeMirror setting options - * + * * @returns {Mixed} return CodeMirror setting option value */ - - getCodeMirrorOption : function(key) { + + getCodeMirrorOption : function(key) { return this.cm.getOption(key); }, - + /** * 配置和重配置CodeMirror的选项 * CodeMirror setting options / resettings - * + * * @returns {editormd} 返回editormd的实例对象 */ - + setCodeMirrorOption : function(key, value) { - + this.cm.setOption(key, value); - + return this; }, - + /** * 添加 CodeMirror 键盘快捷键 * Add CodeMirror keyboard shortcuts key map - * + * * @returns {editormd} 返回editormd的实例对象 */ - + addKeyMap : function(map, bottom) { this.cm.addKeyMap(map, bottom); - + return this; }, - + /** * 移除 CodeMirror 键盘快捷键 * Remove CodeMirror keyboard shortcuts key map - * + * * @returns {editormd} 返回editormd的实例对象 */ - + removeKeyMap : function(map) { this.cm.removeKeyMap(map); - + return this; }, - + /** * 跳转到指定的行 * Goto CodeMirror line - * + * * @param {String|Intiger} line line number or "first"|"last" * @returns {editormd} 返回editormd的实例对象 */ - + gotoLine : function (line) { - + var settings = this.settings; - + if (!settings.gotoLine) { return this; } - + var cm = this.cm; var editor = this.editor; var count = cm.lineCount(); var preview = this.preview; - + if (typeof line === "string") { if(line === "last") { line = count; } - + if (line === "first") { line = 1; } } - - if (typeof line !== "number") - { + + if (typeof line !== "number") + { alert("Error: The line number must be an integer."); return this; } - + line = parseInt(line) - 1; - + if (line > count) { alert("Error: The line number range 1-" + count); - + return this; } - + cm.setCursor( {line : line, ch : 0} ); - + var scrollInfo = cm.getScrollInfo(); - var clientHeight = scrollInfo.clientHeight; + var clientHeight = scrollInfo.clientHeight; var coords = cm.charCoords({line : line, ch : 0}, "local"); - + cm.scrollTo(null, (coords.top + coords.bottom - clientHeight) / 2); - + if (settings.watch) - { + { var cmScroll = this.codeMirror.find(".CodeMirror-scroll")[0]; - var height = $(cmScroll).height(); - var scrollTop = cmScroll.scrollTop; + var height = $(cmScroll).height(); + var scrollTop = cmScroll.scrollTop; var percent = (scrollTop / cmScroll.scrollHeight); if (scrollTop === 0) { preview.scrollTop(0); - } + } else if (scrollTop + height >= cmScroll.scrollHeight - 16) - { - preview.scrollTop(preview[0].scrollHeight); - } + { + preview.scrollTop(preview[0].scrollHeight); + } else - { + { preview.scrollTop(preview[0].scrollHeight * percent); } } cm.focus(); - + return this; }, - + /** * 扩展当前实例对象,可同时设置多个或者只设置一个 * Extend editormd instance object, can mutil setting. - * + * * @returns {editormd} this(editormd instance object.) */ - + extends : function() { if (typeof arguments[1] !== "undefined") { @@ -822,7 +824,7 @@ this[arguments[0]] = arguments[1]; } - + if (typeof arguments[0] === "object" && typeof arguments[0].length === "undefined") { $.extend(true, this, arguments[0]); @@ -830,168 +832,168 @@ return this; }, - + /** * 设置或扩展当前实例对象,单个设置 * Extend editormd instance object, one by one - * + * * @param {String|Object} key option key * @param {String|Object} value option value * @returns {editormd} this(editormd instance object.) */ - + set : function (key, value) { - + if (typeof value !== "undefined" && typeof value === "function") { value = $.proxy(value, this); } - + this[key] = value; return this; }, - + /** * 重新配置 * Resetting editor options - * + * * @param {String|Object} key option key * @param {String|Object} value option value * @returns {editormd} this(editormd instance object.) */ - + config : function(key, value) { var settings = this.settings; - + if (typeof key === "object") { settings = $.extend(true, settings, key); } - + if (typeof key === "string") { settings[key] = value; } - + this.settings = settings; this.recreate(); - + return this; }, - + /** * 注册事件处理方法 * Bind editor event handle - * + * * @param {String} eventType event type * @param {Function} callback 回调函数 * @returns {editormd} this(editormd instance object.) */ - + on : function(eventType, callback) { var settings = this.settings; - - if (typeof settings["on" + eventType] !== "undefined") - { - settings["on" + eventType] = $.proxy(callback, this); + + if (typeof settings["on" + eventType] !== "undefined") + { + settings["on" + eventType] = $.proxy(callback, this); } return this; }, - + /** * 解除事件处理方法 * Unbind editor event handle - * + * * @param {String} eventType event type * @returns {editormd} this(editormd instance object.) */ - + off : function(eventType) { var settings = this.settings; - - if (typeof settings["on" + eventType] !== "undefined") + + if (typeof settings["on" + eventType] !== "undefined") { settings["on" + eventType] = function(){}; } - + return this; }, - + /** * 显示工具栏 * Display toolbar - * + * * @param {Function} [callback=function(){}] 回调函数 * @returns {editormd} 返回editormd的实例对象 */ - + showToolbar : function(callback) { var settings = this.settings; - + if(settings.readOnly) { return this; } - + if (settings.toolbar && (this.toolbar.length < 1 || this.toolbar.find("." + this.classPrefix + "menu").html() === "") ) { this.setToolbar(); } - - settings.toolbar = true; - + + settings.toolbar = true; + this.toolbar.show(); this.resize(); - + $.proxy(callback || function(){}, this)(); return this; }, - + /** * 隐藏工具栏 * Hide toolbar - * + * * @param {Function} [callback=function(){}] 回调函数 * @returns {editormd} this(editormd instance object.) */ - - hideToolbar : function(callback) { + + hideToolbar : function(callback) { var settings = this.settings; - - settings.toolbar = false; + + settings.toolbar = false; this.toolbar.hide(); this.resize(); - + $.proxy(callback || function(){}, this)(); return this; }, - + /** * 页面滚动时工具栏的固定定位 * Set toolbar in window scroll auto fixed position - * + * * @returns {editormd} 返回editormd的实例对象 */ - + setToolbarAutoFixed : function(fixed) { - + var state = this.state; var editor = this.editor; var toolbar = this.toolbar; var settings = this.settings; - + if (typeof fixed !== "undefined") { settings.toolbarAutoFixed = fixed; } - + var autoFixedHandle = function(){ var $window = $(window); var top = $window.scrollTop(); - + if (!settings.toolbarAutoFixed) { return false; @@ -1014,7 +1016,7 @@ }); } }; - + if (!state.fullscreen && !state.preview && settings.toolbar && settings.toolbarAutoFixed) { $(window).bind("scroll", autoFixedHandle); @@ -1022,58 +1024,58 @@ return this; }, - + /** * 配置和初始化工具栏 * Set toolbar and Initialization - * + * * @returns {editormd} 返回editormd的实例对象 */ - + setToolbar : function() { - var settings = this.settings; - + var settings = this.settings; + if(settings.readOnly) { return this; } - + var editor = this.editor; var preview = this.preview; var classPrefix = this.classPrefix; - + var toolbar = this.toolbar = editor.children("." + classPrefix + "toolbar"); - + if (settings.toolbar && toolbar.length < 1) - { + { var toolbarHTML = "
"; - + editor.append(toolbarHTML); toolbar = this.toolbar = editor.children("." + classPrefix + "toolbar"); } - - if (!settings.toolbar) + + if (!settings.toolbar) { toolbar.hide(); - + return this; } - + toolbar.show(); - - var icons = (typeof settings.toolbarIcons === "function") ? settings.toolbarIcons() + + var icons = (typeof settings.toolbarIcons === "function") ? settings.toolbarIcons() : ((typeof settings.toolbarIcons === "string") ? editormd.toolbarModes[settings.toolbarIcons] : settings.toolbarIcons); - + var toolbarMenu = toolbar.find("." + this.classPrefix + "menu"), menu = ""; var pullRight = false; - + for (var i = 0, len = icons.length; i < len; i++) { var name = icons[i]; - if (name === "||") - { + if (name === "||") + { pullRight = true; - } + } else if (name === "|") { menu += "
  • |
  • "; @@ -1082,26 +1084,26 @@ { var isHeader = (/h(\d)/.test(name)); var index = name; - + if (name === "watch" && !settings.watch) { index = "unwatch"; } - + var title = settings.lang.toolbar[index]; var iconTexts = settings.toolbarIconTexts[index]; var iconClass = settings.toolbarIconsClass[index]; - + title = (typeof title === "undefined") ? "" : title; iconTexts = (typeof iconTexts === "undefined") ? "" : iconTexts; iconClass = (typeof iconClass === "undefined") ? "" : iconClass; var menuItem = pullRight ? "
  • " : "
  • "; - + if (typeof settings.toolbarCustomIcons[name] !== "undefined" && typeof settings.toolbarCustomIcons[name] !== "function") { menuItem += settings.toolbarCustomIcons[name]; } - else + else { menuItem += ""; menuItem += ""+((isHeader) ? name.toUpperCase() : ( (iconClass === "") ? iconTexts : "") ) + ""; @@ -1115,64 +1117,64 @@ } toolbarMenu.html(menu); - + toolbarMenu.find("[title=\"Lowercase\"]").attr("title", settings.lang.toolbar.lowercase); toolbarMenu.find("[title=\"ucwords\"]").attr("title", settings.lang.toolbar.ucwords); - + this.setToolbarHandler(); this.setToolbarAutoFixed(); return this; }, - + /** * 工具栏图标事件处理对象序列 * Get toolbar icons event handlers - * + * * @param {Object} cm CodeMirror的实例对象 * @param {String} name 要获取的事件处理器名称 * @returns {Object} 返回处理对象序列 */ - + dialogLockScreen : function() { $.proxy(editormd.dialogLockScreen, this)(); - + return this; }, dialogShowMask : function(dialog) { $.proxy(editormd.dialogShowMask, this)(dialog); - + return this; }, - - getToolbarHandles : function(name) { + + getToolbarHandles : function(name) { var toolbarHandlers = this.toolbarHandlers = editormd.toolbarHandlers; - + return (name && typeof toolbarIconHandlers[name] !== "undefined") ? toolbarHandlers[name] : toolbarHandlers; }, - + /** * 工具栏图标事件处理器 * Bind toolbar icons event handle - * + * * @returns {editormd} 返回editormd的实例对象 */ - + setToolbarHandler : function() { var _this = this; var settings = this.settings; - + if (!settings.toolbar || settings.readOnly) { return this; } - + var toolbar = this.toolbar; var cm = this.cm; - var classPrefix = this.classPrefix; - var toolbarIcons = this.toolbarIcons = toolbar.find("." + classPrefix + "menu > li > a"); - var toolbarIconHandlers = this.getToolbarHandles(); - + var classPrefix = this.classPrefix; + var toolbarIcons = this.toolbarIcons = toolbar.find("." + classPrefix + "menu > li > a"); + var toolbarIconHandlers = this.getToolbarHandles(); + toolbarIcons.bind(editormd.mouseOrTouch("click", "touchend"), function(event) { var icon = $(this).children(".fa"); @@ -1183,23 +1185,23 @@ if (name === "") { return ; } - + _this.activeIcon = icon; - if (typeof toolbarIconHandlers[name] !== "undefined") + if (typeof toolbarIconHandlers[name] !== "undefined") { $.proxy(toolbarIconHandlers[name], _this)(cm); } - else + else { - if (typeof settings.toolbarHandlers[name] !== "undefined") + if (typeof settings.toolbarHandlers[name] !== "undefined") { $.proxy(settings.toolbarHandlers[name], _this)(cm, icon, cursor, selection); } } - - if (name !== "link" && name !== "reference-link" && name !== "image" && name !== "code-block" && - name !== "preformatted-text" && name !== "watch" && name !== "preview" && name !== "search" && name !== "fullscreen" && name !== "info") + + if (name !== "link" && name !== "reference-link" && name !== "image" && name !== "code-block" && + name !== "preformatted-text" && name !== "watch" && name !== "preview" && name !== "search" && name !== "fullscreen" && name !== "info") { cm.focus(); } @@ -1210,31 +1212,31 @@ return this; }, - + /** * 动态创建对话框 * Creating custom dialogs - * + * * @param {Object} options 配置项键值对 Key/Value * @returns {dialog} 返回创建的dialog的jQuery实例对象 */ - - createDialog : function(options) { + + createDialog : function(options) { return $.proxy(editormd.createDialog, this)(options); }, - + /** * 创建关于Editor.md的对话框 * Create about Editor.md dialog - * + * * @returns {editormd} 返回editormd的实例对象 */ - + createInfoDialog : function() { var _this = this; var editor = this.editor; - var classPrefix = this.classPrefix; - + var classPrefix = this.classPrefix; + var infoDialogHTML = [ "
    ", "
    ", @@ -1248,30 +1250,30 @@ ].join("\n"); editor.append(infoDialogHTML); - + var infoDialog = this.infoDialog = editor.children("." + classPrefix + "dialog-info"); infoDialog.find("." + classPrefix + "dialog-close").bind(editormd.mouseOrTouch("click", "touchend"), function() { _this.hideInfoDialog(); }); - + infoDialog.css("border", (editormd.isIE8) ? "1px solid #ddd" : "").css("z-index", editormd.dialogZindex).show(); - + this.infoDialogPosition(); return this; }, - + /** * 关于Editor.md对话居中定位 * Editor.md dialog position handle - * + * * @returns {editormd} 返回editormd的实例对象 */ - + infoDialogPosition : function() { var infoDialog = this.infoDialog; - + var _infoDialogPosition = function() { infoDialog.css({ top : ($(window).height() - infoDialog.height()) / 2 + "px", @@ -1282,33 +1284,33 @@ _infoDialogPosition(); $(window).resize(_infoDialogPosition); - + return this; }, - + /** * 显示关于Editor.md * Display about Editor.md dialog - * + * * @returns {editormd} 返回editormd的实例对象 */ - + showInfoDialog : function() { $("html,body").css("overflow-x", "hidden"); - + var _this = this; var editor = this.editor; - var settings = this.settings; + var settings = this.settings; var infoDialog = this.infoDialog = editor.children("." + this.classPrefix + "dialog-info"); - + if (infoDialog.length < 1) { this.createInfoDialog(); } - + this.lockScreen(true); - + this.mask.css({ opacity : settings.dialogMaskOpacity, backgroundColor : settings.dialogMaskBgColor @@ -1320,15 +1322,15 @@ return this; }, - + /** * 隐藏关于Editor.md * Hide about Editor.md dialog - * + * * @returns {editormd} 返回editormd的实例对象 */ - - hideInfoDialog : function() { + + hideInfoDialog : function() { $("html,body").css("overflow-x", ""); this.infoDialog.hide(); this.mask.hide(); @@ -1336,113 +1338,113 @@ return this; }, - + /** * 锁屏 * lock screen - * + * * @param {Boolean} lock Boolean 布尔值,是否锁屏 * @returns {editormd} 返回editormd的实例对象 */ - + lockScreen : function(lock) { editormd.lockScreen(lock); return this; }, - + /** * 编辑器界面重建,用于动态语言包或模块加载等 * Recreate editor - * + * * @returns {editormd} 返回editormd的实例对象 */ - + recreate : function() { var _this = this; var editor = this.editor; var settings = this.settings; - + this.codeMirror.remove(); - + this.setCodeMirror(); - if (!settings.readOnly) + if (!settings.readOnly) { if (editor.find(".editormd-dialog").length > 0) { editor.find(".editormd-dialog").remove(); } - - if (settings.toolbar) - { - this.getToolbarHandles(); + + if (settings.toolbar) + { + this.getToolbarHandles(); this.setToolbar(); } } - + this.loadedDisplay(true); return this; }, - + /** * 高亮预览HTML的pre代码部分 * highlight of preview codes - * + * * @returns {editormd} 返回editormd的实例对象 */ - - previewCodeHighlight : function() { + + previewCodeHighlight : function() { var settings = this.settings; var previewContainer = this.previewContainer; - - if (settings.previewCodeHighlight) + + if (settings.previewCodeHighlight) { previewContainer.find("pre").addClass("prettyprint linenums"); - + if (typeof prettyPrint !== "undefined") - { + { prettyPrint(); } } return this; }, - + /** * 解析TeX(KaTeX)科学公式 * TeX(KaTeX) Renderer - * + * * @returns {editormd} 返回editormd的实例对象 */ - + katexRender : function() { - + if (timer === null) { return this; } - + this.previewContainer.find("." + editormd.classNames.tex).each(function(){ var tex = $(this); editormd.$katex.render(tex.text(), tex[0]); - }); + }); return this; }, - + /** * 解析和渲染流程图及时序图 * FlowChart and SequenceDiagram Renderer - * + * * @returns {editormd} 返回editormd的实例对象 */ - + flowChartAndSequenceDiagramRender : function() { - + var settings = this.settings; var previewContainer = this.previewContainer; - + if (editormd.isIE8) { return this; } @@ -1451,8 +1453,8 @@ if (flowchartTimer === null) { return this; } - - previewContainer.find(".flowchart").flowChart(); + + previewContainer.find(".flowchart").flowChart(); } if (settings.sequenceDiagram) { @@ -1461,25 +1463,25 @@ return this; }, - + /** * 注册键盘快捷键处理 * Register CodeMirror keyMaps (keyboard shortcuts). - * + * * @param {Object} keyMap KeyMap key/value {"(Ctrl/Shift/Alt)-Key" : function(){}} * @returns {editormd} return this */ - + registerKeyMaps : function(keyMap) { - + var _this = this; var cm = this.cm; var settings = this.settings; var toolbarHandlers = editormd.toolbarHandlers; var disabledKeyMaps = settings.disabledKeyMaps; - + keyMap = keyMap || null; - + if (keyMap) { for (var i in keyMap) @@ -1499,7 +1501,7 @@ { var _keyMap = editormd.keyMaps[k]; var handle = (typeof _keyMap === "string") ? $.proxy(toolbarHandlers[_keyMap], _this) : $.proxy(_keyMap, _this); - + if ($.inArray(k, ["F9", "F10", "F11"]) < 0 && $.inArray(k, disabledKeyMaps) < 0) { var _map = {}; @@ -1508,15 +1510,15 @@ cm.addKeyMap(_map); } } - + $(window).keydown(function(event) { - + var keymaps = { "120" : "F9", "121" : "F10", "122" : "F11" }; - + if ( $.inArray(keymaps[event.keyCode], disabledKeyMaps) < 0 ) { switch (event.keyCode) @@ -1525,17 +1527,17 @@ $.proxy(toolbarHandlers["watch"], _this)(); return false; break; - + case 121: $.proxy(toolbarHandlers["preview"], _this)(); return false; break; - + case 122: - $.proxy(toolbarHandlers["fullscreen"], _this)(); + $.proxy(toolbarHandlers["fullscreen"], _this)(); return false; break; - + default: break; } @@ -1545,38 +1547,38 @@ return this; }, - + bindScrollEvent : function() { - + var _this = this; var preview = this.preview; var settings = this.settings; var codeMirror = this.codeMirror; var mouseOrTouch = editormd.mouseOrTouch; - + if (!settings.syncScrolling) { return this; } - - var cmBindScroll = function() { + + var cmBindScroll = function() { codeMirror.find(".CodeMirror-scroll").bind(mouseOrTouch("scroll", "touchmove"), function(event) { var height = $(this).height(); - var scrollTop = $(this).scrollTop(); + var scrollTop = $(this).scrollTop(); var percent = (scrollTop / $(this)[0].scrollHeight); - if (scrollTop === 0) + if (scrollTop === 0) { preview.scrollTop(0); - } + } else if (scrollTop + height >= $(this)[0].scrollHeight - 16) - { - preview.scrollTop(preview[0].scrollHeight); - } + { + preview.scrollTop(preview[0].scrollHeight); + } else - { + { preview.scrollTop(preview[0].scrollHeight * percent); } - + $.proxy(settings.onscroll, _this)(event); }); }; @@ -1586,26 +1588,26 @@ }; var previewBindScroll = function() { - + preview.bind(mouseOrTouch("scroll", "touchmove"), function(event) { var height = $(this).height(); - var scrollTop = $(this).scrollTop(); + var scrollTop = $(this).scrollTop(); var percent = (scrollTop / $(this)[0].scrollHeight); var codeView = codeMirror.find(".CodeMirror-scroll"); - if(scrollTop === 0) + if(scrollTop === 0) { codeView.scrollTop(0); } else if (scrollTop + height >= $(this)[0].scrollHeight) { - codeView.scrollTop(codeView[0].scrollHeight); + codeView.scrollTop(codeView[0].scrollHeight); } - else + else { codeView.scrollTop(codeView[0].scrollHeight * percent); } - + $.proxy(settings.onpreviewscroll, _this)(event); }); @@ -1613,7 +1615,7 @@ var previewUnbindScroll = function() { preview.unbind(mouseOrTouch("scroll", "touchmove")); - }; + }; codeMirror.bind({ mouseover : cmBindScroll, @@ -1621,7 +1623,7 @@ touchstart : cmBindScroll, touchend : cmUnbindScroll }); - + preview.bind({ mouseover : previewBindScroll, mouseout : previewUnbindScroll, @@ -1631,24 +1633,24 @@ return this; }, - + bindChangeEvent : function() { - + var _this = this; var cm = this.cm; var settings = this.settings; - + if (!settings.syncScrolling) { return this; } - + cm.on("change", function(_cm, changeObj) { - + if (settings.watch) - { + { _this.previewContainer.css("padding", settings.autoHeight ? "20px 20px 50px 40px" : "20px"); } - + timer = setTimeout(function() { clearTimeout(timer); _this.save(); @@ -1658,208 +1660,208 @@ return this; }, - + /** * 加载队列完成之后的显示处理 * Display handle of the module queues loaded after. - * + * * @param {Boolean} recreate 是否为重建编辑器 * @returns {editormd} 返回editormd的实例对象 */ - + loadedDisplay : function(recreate) { - + recreate = recreate || false; - + var _this = this; var editor = this.editor; var preview = this.preview; var settings = this.settings; - + this.containerMask.hide(); - + this.save(); - + if (settings.watch) { preview.show(); } - + editor.data("oldWidth", editor.width()).data("oldHeight", editor.height()); // 为了兼容Zepto - + this.resize(); this.registerKeyMaps(); - + $(window).resize(function(){ _this.resize(); }); - + this.bindScrollEvent().bindChangeEvent(); - + if (!recreate) { $.proxy(settings.onload, this)(); } - + this.state.loaded = true; return this; }, - + /** * 设置编辑器的宽度 * Set editor width - * + * * @param {Number|String} width 编辑器宽度值 * @returns {editormd} 返回editormd的实例对象 */ - + width : function(width) { - - this.editor.css("width", (typeof width === "number") ? width + "px" : width); + + this.editor.css("width", (typeof width === "number") ? width + "px" : width); this.resize(); - + return this; }, - + /** * 设置编辑器的高度 * Set editor height - * + * * @param {Number|String} height 编辑器高度值 * @returns {editormd} 返回editormd的实例对象 */ - + height : function(height) { - - this.editor.css("height", (typeof height === "number") ? height + "px" : height); + + this.editor.css("height", (typeof height === "number") ? height + "px" : height); this.resize(); - + return this; }, - + /** * 调整编辑器的尺寸和布局 * Resize editor layout - * + * * @param {Number|String} [width=null] 编辑器宽度值 * @param {Number|String} [height=null] 编辑器高度值 * @returns {editormd} 返回editormd的实例对象 */ - + resize : function(width, height) { - + width = width || null; height = height || null; - + var state = this.state; var editor = this.editor; var preview = this.preview; var toolbar = this.toolbar; var settings = this.settings; var codeMirror = this.codeMirror; - + if (width) { editor.css("width", (typeof width === "number") ? width + "px" : width); } - + if (settings.autoHeight && !state.fullscreen && !state.preview) { editor.css("height", "auto"); codeMirror.css("height", "auto"); - } - else + } + else { - if (height) + if (height) { editor.css("height", (typeof height === "number") ? height + "px" : height); } - + if (state.fullscreen) { editor.height($(window).height()); } - if (settings.toolbar && !settings.readOnly) + if (settings.toolbar && !settings.readOnly) { codeMirror.css("margin-top", toolbar.height() + 1).height(editor.height() - toolbar.height()); - } + } else { codeMirror.css("margin-top", 0).height(editor.height()); } } - - if(settings.watch) + + if(settings.watch) { codeMirror.width(editor.width() / 2); preview.width((!state.preview) ? editor.width() / 2 : editor.width()); - + this.previewContainer.css("padding", settings.autoHeight ? "20px 20px 50px 40px" : "20px"); - - if (settings.toolbar && !settings.readOnly) + + if (settings.toolbar && !settings.readOnly) { preview.css("top", toolbar.height()); - } - else + } + else { preview.css("top", 0); } - + if (settings.autoHeight && !state.fullscreen && !state.preview) { preview.height(""); } else - { + { preview.height((settings.toolbar && !settings.readOnly) ? editor.height() - toolbar.height() : editor.height()); } - } - else + } + else { codeMirror.width(editor.width()); preview.hide(); } - - if (state.loaded) + + if (state.loaded) { $.proxy(settings.onresize, this)(); } return this; }, - + /** * 解析和保存Markdown代码 * Parse & Saving Markdown source code - * + * * @returns {editormd} 返回editormd的实例对象 */ - + save : function() { - + if (timer === null) { return this; } - + var _this = this; var state = this.state; var settings = this.settings; - var cm = this.cm; + var cm = this.cm; var cmValue = cm.getValue(); var previewContainer = this.previewContainer; - if (settings.mode !== "gfm" && settings.mode !== "markdown") + if (settings.mode !== "gfm" && settings.mode !== "markdown") { this.markdownTextarea.val(cmValue); - + return this; } - + var marked = editormd.$marked; - var markdownToC = this.markdownToC = []; - var rendererOptions = this.markedRendererOptions = { + var markdownToC = this.markdownToC = []; + var rendererOptions = this.markedRendererOptions = { toc : settings.toc, tocm : settings.tocm, tocStartLevel : settings.tocStartLevel, @@ -1873,7 +1875,7 @@ sequenceDiagram : settings.sequenceDiagram, previewCodeHighlight : settings.previewCodeHighlight, }; - + var markedOptions = this.markedOptions = { renderer : editormd.markedRenderer(markdownToC, rendererOptions), gfm : true, @@ -1884,70 +1886,70 @@ smartLists : true, smartypants : true }; - + marked.setOptions(markedOptions); - + cmValue = editormd.filterHTMLTags(cmValue, settings.htmlDecode); - + var newMarkdownDoc = editormd.$marked(cmValue, markedOptions); - + this.markdownTextarea.val(cmValue); - + cm.save(); - - if (settings.saveHTMLToTextarea) + + if (settings.saveHTMLToTextarea) { this.htmlTextarea.val(newMarkdownDoc); } - + if(settings.watch || (!settings.watch && state.preview)) { previewContainer.html(newMarkdownDoc); this.previewCodeHighlight(); - - if (settings.toc) + + if (settings.toc) { var tocContainer = (settings.tocContainer === "") ? previewContainer : $(settings.tocContainer); var tocMenu = tocContainer.find("." + this.classPrefix + "toc-menu"); - + tocContainer.attr("previewContainer", (settings.tocContainer === "") ? "true" : "false"); - + if (settings.tocContainer !== "" && tocMenu.length > 0) { tocMenu.remove(); } - + editormd.markdownToCRenderer(markdownToC, tocContainer, settings.tocDropdown, settings.tocStartLevel); - + if (settings.tocDropdown || tocContainer.find("." + this.classPrefix + "toc-menu").length > 0) { editormd.tocDropdownMenu(tocContainer, (settings.tocTitle !== "") ? settings.tocTitle : this.lang.tocTitle); } - + if (settings.tocContainer !== "") { previewContainer.find(".markdown-toc").css("border", "none"); } } - + if (settings.tex) { - if (!editormd.kaTeXLoaded && settings.autoLoadModules) + if (!editormd.kaTeXLoaded && settings.autoLoadModules) { editormd.loadKaTeX(function() { editormd.$katex = katex; editormd.kaTeXLoaded = true; _this.katexRender(); }); - } - else + } + else { editormd.$katex = katex; this.katexRender(); } - } - + } + if (settings.flowChart || settings.sequenceDiagram) { flowchartTimer = setTimeout(function(){ @@ -1957,7 +1959,7 @@ }, 10); } - if (state.loaded) + if (state.loaded) { $.proxy(settings.onchange, this)(); } @@ -1965,215 +1967,215 @@ return this; }, - + /** * 聚焦光标位置 * Focusing the cursor position - * + * * @returns {editormd} 返回editormd的实例对象 */ - + focus : function() { this.cm.focus(); return this; }, - + /** * 设置光标的位置 * Set cursor position - * + * * @param {Object} cursor 要设置的光标位置键值对象,例:{line:1, ch:0} * @returns {editormd} 返回editormd的实例对象 */ - + setCursor : function(cursor) { this.cm.setCursor(cursor); return this; }, - + /** * 获取当前光标的位置 * Get the current position of the cursor - * + * * @returns {Cursor} 返回一个光标Cursor对象 */ - + getCursor : function() { return this.cm.getCursor(); }, - + /** * 设置光标选中的范围 * Set cursor selected ranges - * + * * @param {Object} from 开始位置的光标键值对象,例:{line:1, ch:0} * @param {Object} to 结束位置的光标键值对象,例:{line:1, ch:0} * @returns {editormd} 返回editormd的实例对象 */ - + setSelection : function(from, to) { - + this.cm.setSelection(from, to); - + return this; }, - + /** * 获取光标选中的文本 * Get the texts from cursor selected - * + * * @returns {String} 返回选中文本的字符串形式 */ - + getSelection : function() { return this.cm.getSelection(); }, - + /** * 设置光标选中的文本范围 * Set the cursor selection ranges - * + * * @param {Array} ranges cursor selection ranges array * @returns {Array} return this */ - + setSelections : function(ranges) { this.cm.setSelections(ranges); - + return this; }, - + /** * 获取光标选中的文本范围 * Get the cursor selection ranges - * + * * @returns {Array} return selection ranges array */ - + getSelections : function() { return this.cm.getSelections(); }, - + /** * 替换当前光标选中的文本或在当前光标处插入新字符 * Replace the text at the current cursor selected or insert a new character at the current cursor position - * + * * @param {String} value 要插入的字符值 * @returns {editormd} 返回editormd的实例对象 */ - + replaceSelection : function(value) { this.cm.replaceSelection(value); return this; }, - + /** * 在当前光标处插入新字符 * Insert a new character at the current cursor position * * 同replaceSelection()方法 * With the replaceSelection() method - * + * * @param {String} value 要插入的字符值 * @returns {editormd} 返回editormd的实例对象 */ - + insertValue : function(value) { this.replaceSelection(value); return this; }, - + /** * 追加markdown * append Markdown to editor - * + * * @param {String} md 要追加的markdown源文档 * @returns {editormd} 返回editormd的实例对象 */ - + appendMarkdown : function(md) { var settings = this.settings; var cm = this.cm; - + cm.setValue(cm.getValue() + md); - + return this; }, - + /** * 设置和传入编辑器的markdown源文档 * Set Markdown source document - * + * * @param {String} md 要传入的markdown源文档 * @returns {editormd} 返回editormd的实例对象 */ - - setMarkdown : function(md) { + + setMarkdown : function(md) { this.cm.setValue(md || this.settings.markdown); - + return this; }, - + /** * 获取编辑器的markdown源文档 * Set Editor.md markdown/CodeMirror value - * + * * @returns {editormd} 返回editormd的实例对象 */ - + getMarkdown : function() { return this.cm.getValue(); }, - + /** * 获取编辑器的源文档 * Get CodeMirror value - * + * * @returns {editormd} 返回editormd的实例对象 */ - + getValue : function() { return this.cm.getValue(); }, - + /** * 设置编辑器的源文档 * Set CodeMirror value - * + * * @param {String} value set code/value/string/text * @returns {editormd} 返回editormd的实例对象 */ - + setValue : function(value) { this.cm.setValue(value); - + return this; }, - + /** * 清空编辑器 * Empty CodeMirror editor container - * + * * @returns {editormd} 返回editormd的实例对象 */ - + clear : function() { this.cm.setValue(""); - + return this; }, - + /** * 获取解析后存放在Textarea的HTML源码 * Get parsed html code from Textarea - * + * * @returns {String} 返回HTML源码 */ - + getHTML : function() { if (!this.settings.saveHTMLToTextarea) { @@ -2181,28 +2183,28 @@ return false; } - + return this.htmlTextarea.html(); }, - + /** * getHTML()的别名 * getHTML (alias) - * + * * @returns {String} Return html code 返回HTML源码 */ - + getTextareaSavedHTML : function() { return this.getHTML(); }, - + /** * 获取预览窗口的HTML源码 * Get html from preview container - * + * * @returns {editormd} 返回editormd的实例对象 */ - + getPreviewedHTML : function() { if (!this.settings.watch) { @@ -2210,155 +2212,155 @@ return false; } - + return this.previewContainer.html(); }, - + /** * 开启实时预览 * Enable real-time watching - * + * * @returns {editormd} 返回editormd的实例对象 */ - - watch : function(callback) { + + watch : function(callback) { var settings = this.settings; - + if ($.inArray(settings.mode, ["gfm", "markdown"]) < 0) { return this; } - + this.state.watching = settings.watch = true; this.preview.show(); - + if (this.toolbar) { var watchIcon = settings.toolbarIconsClass.watch; var unWatchIcon = settings.toolbarIconsClass.unwatch; - + var icon = this.toolbar.find(".fa[name=watch]"); icon.parent().attr("title", settings.lang.toolbar.watch); icon.removeClass(unWatchIcon).addClass(watchIcon); } - - this.codeMirror.css("border-right", "1px solid #ddd").width(this.editor.width() / 2); - + + this.codeMirror.css("border-right", "1px solid #ddd").width(this.editor.width() / 2); + timer = 0; - + this.save().resize(); - + if (!settings.onwatch) { settings.onwatch = callback || function() {}; } - + $.proxy(settings.onwatch, this)(); - + return this; }, - + /** * 关闭实时预览 * Disable real-time watching - * + * * @returns {editormd} 返回editormd的实例对象 */ - + unwatch : function(callback) { var settings = this.settings; this.state.watching = settings.watch = false; this.preview.hide(); - - if (this.toolbar) + + if (this.toolbar) { var watchIcon = settings.toolbarIconsClass.watch; var unWatchIcon = settings.toolbarIconsClass.unwatch; - + var icon = this.toolbar.find(".fa[name=watch]"); icon.parent().attr("title", settings.lang.toolbar.unwatch); icon.removeClass(watchIcon).addClass(unWatchIcon); } - + this.codeMirror.css("border-right", "none").width(this.editor.width()); - + this.resize(); - + if (!settings.onunwatch) { settings.onunwatch = callback || function() {}; } - + $.proxy(settings.onunwatch, this)(); - + return this; }, - + /** * 显示编辑器 * Show editor - * + * * @param {Function} [callback=function()] 回调函数 * @returns {editormd} 返回editormd的实例对象 */ - + show : function(callback) { callback = callback || function() {}; - + var _this = this; this.editor.show(0, function() { $.proxy(callback, _this)(); }); - + return this; }, - + /** * 隐藏编辑器 * Hide editor - * + * * @param {Function} [callback=function()] 回调函数 * @returns {editormd} 返回editormd的实例对象 */ - + hide : function(callback) { callback = callback || function() {}; - + var _this = this; this.editor.hide(0, function() { $.proxy(callback, _this)(); }); - + return this; }, - + /** * 隐藏编辑器部分,只预览HTML * Enter preview html state - * + * * @returns {editormd} 返回editormd的实例对象 */ - + previewing : function() { - + var _this = this; var editor = this.editor; var preview = this.preview; var toolbar = this.toolbar; var settings = this.settings; var codeMirror = this.codeMirror; - + if ($.inArray(settings.mode, ["gfm", "markdown"]) < 0) { return this; } - + if (settings.toolbar && toolbar) { toolbar.toggle(); toolbar.find(".fa[name=preview]").toggleClass("active"); } - + codeMirror.toggle(); - + var escHandle = function(event) { if (event.shiftKey && event.keyCode === 27) { _this.previewed(); @@ -2372,11 +2374,11 @@ if (this.state.fullscreen) { preview.css("background", "#fff"); } - + editor.find("." + this.classPrefix + "preview-close-btn").show().bind(editormd.mouseOrTouch("click", "touchend"), function(){ _this.previewed(); }); - + if (!settings.watch) { this.save(); @@ -2388,30 +2390,30 @@ width : editor.width(), height : (settings.autoHeight && !this.state.fullscreen) ? "auto" : editor.height() }); - + if (this.state.loaded) { $.proxy(settings.onpreviewing, this)(); } $(window).bind("keyup", escHandle); - } - else + } + else { $(window).unbind("keyup", escHandle); this.previewed(); } }, - + /** * 显示编辑器部分,退出只预览HTML * Exit preview html state - * + * * @returns {editormd} 返回editormd的实例对象 */ - + previewed : function() { - + var editor = this.editor; var preview = this.preview; var toolbar = this.toolbar; @@ -2419,18 +2421,18 @@ var previewCloseBtn = editor.find("." + this.classPrefix + "preview-close-btn"); this.state.preview = false; - + this.codeMirror.show(); - + if (settings.toolbar) { toolbar.show(); } - + preview[(settings.watch) ? "show" : "hide"](); - + previewCloseBtn.hide().unbind(editormd.mouseOrTouch("click", "touchend")); - - preview.css({ + + preview.css({ background : null, position : "absolute", width : editor.width() / 2, @@ -2442,19 +2444,19 @@ { $.proxy(settings.onpreviewed, this)(); } - + return this; }, - + /** * 编辑器全屏显示 * Fullscreen show - * + * * @returns {editormd} 返回editormd的实例对象 */ - + fullscreen : function() { - + var _this = this; var state = this.state; var editor = this.editor; @@ -2462,13 +2464,13 @@ var toolbar = this.toolbar; var settings = this.settings; var fullscreenClass = this.classPrefix + "fullscreen"; - + if (toolbar) { - toolbar.find(".fa[name=fullscreen]").parent().toggleClass("active"); + toolbar.find(".fa[name=fullscreen]").parent().toggleClass("active"); } - + var escHandle = function(event) { - if (!event.shiftKey && event.keyCode === 27) + if (!event.shiftKey && event.keyCode === 27) { if (state.fullscreen) { @@ -2477,107 +2479,107 @@ } }; - if (!editor.hasClass(fullscreenClass)) + if (!editor.hasClass(fullscreenClass)) { state.fullscreen = true; $("html,body").css("overflow", "hidden"); - + editor.css({ - position : "fixed", - top : 0, - left : 0, - margin : 0, + position : "fixed", + top : 0, + left : 0, + margin : 0, border : "none", width : $(window).width(), height : $(window).height() }).addClass(fullscreenClass); this.resize(); - + $.proxy(settings.onfullscreen, this)(); $(window).bind("keyup", escHandle); } else - { - $(window).unbind("keyup", escHandle); + { + $(window).unbind("keyup", escHandle); this.fullscreenExit(); } return this; }, - + /** * 编辑器退出全屏显示 * Exit fullscreen state - * + * * @returns {editormd} 返回editormd的实例对象 */ - + fullscreenExit : function() { - + var editor = this.editor; var settings = this.settings; var toolbar = this.toolbar; - var fullscreenClass = this.classPrefix + "fullscreen"; - + var fullscreenClass = this.classPrefix + "fullscreen"; + this.state.fullscreen = false; - + if (toolbar) { - toolbar.find(".fa[name=fullscreen]").parent().removeClass("active"); + toolbar.find(".fa[name=fullscreen]").parent().removeClass("active"); } $("html,body").css("overflow", ""); editor.css({ - position : "", + position : "", top : "", - left : "", - margin : "0 auto 15px", + left : "", + margin : "0 auto 15px", width : editor.data("oldWidth"), height : editor.data("oldHeight"), border : "1px solid #ddd" }).removeClass(fullscreenClass); this.resize(); - + $.proxy(settings.onfullscreenExit, this)(); return this; }, - + /** * 加载并执行插件 * Load and execute the plugin - * + * * @param {String} name plugin name / function name * @param {String} path plugin load path * @returns {editormd} 返回editormd的实例对象 */ - + executePlugin : function(name, path) { - + var _this = this; var cm = this.cm; var settings = this.settings; - + path = settings.pluginPath + path; - - if (typeof define === "function") - { + + if (typeof define === "function") + { if (typeof this[name] === "undefined") { alert("Error: " + name + " plugin is not found, you are not load this plugin."); - + return this; } - + this[name](cm); - + return this; } - + if ($.inArray(path, editormd.loadFiles.plugin) < 0) { editormd.loadPlugin(path, function() { @@ -2589,78 +2591,78 @@ { $.proxy(editormd.loadPlugins[name], this)(cm); } - + return this; }, - + /** * 搜索替换 * Search & replace - * + * * @param {String} command CodeMirror serach commands, "find, fintNext, fintPrev, clearSearch, replace, replaceAll" * @returns {editormd} return this */ - + search : function(command) { var settings = this.settings; - + if (!settings.searchReplace) { alert("Error: settings.searchReplace == false"); return this; } - + if (!settings.readOnly) { this.cm.execCommand(command || "find"); } - + return this; }, - - searchReplace : function() { + + searchReplace : function() { this.search("replace"); - + return this; }, - - searchReplaceAll : function() { + + searchReplaceAll : function() { this.search("replaceAll"); - + return this; } }; - - editormd.fn.init.prototype = editormd.fn; - + + editormd.fn.init.prototype = editormd.fn; + /** * 锁屏 * lock screen when dialog opening - * + * * @returns {void} */ editormd.dialogLockScreen = function() { var settings = this.settings || {dialogLockScreen : true}; - - if (settings.dialogLockScreen) + + if (settings.dialogLockScreen) { $("html,body").css("overflow", "hidden"); } }; - + /** * 显示透明背景层 * Display mask layer when dialog opening - * + * * @param {Object} dialog dialog jQuery object * @returns {void} */ - + editormd.dialogShowMask = function(dialog) { var editor = this.editor; var settings = this.settings || {dialogShowMask : true}; - + dialog.css({ top : ($(window).height() - dialog.height()) / 2 + "px", left : ($(window).width() - dialog.width()) / 2 + "px" @@ -2675,11 +2677,11 @@ undo : function() { this.cm.undo(); }, - + redo : function() { this.cm.redo(); }, - + bold : function() { var cm = this.cm; var cursor = cm.getCursor(); @@ -2691,7 +2693,7 @@ cm.setCursor(cursor.line, cursor.ch + 2); } }, - + del : function() { var cm = this.cm; var cursor = cm.getCursor(); @@ -2724,7 +2726,7 @@ cm.replaceSelection("> " + selection); cm.setCursor(cursor.line, (selection === "") ? cursor.ch + 2 : cursor.ch + selection.length + 2); }, - + ucfirst : function() { var cm = this.cm; var selection = cm.getSelection(); @@ -2733,7 +2735,7 @@ cm.replaceSelection(editormd.firstUpperCase(selection)); cm.setSelections(selections); }, - + ucwords : function() { var cm = this.cm; var selection = cm.getSelection(); @@ -2742,7 +2744,7 @@ cm.replaceSelection(editormd.wordsFirstUpperCase(selection)); cm.setSelections(selections); }, - + uppercase : function() { var cm = this.cm; var selection = cm.getSelection(); @@ -2751,13 +2753,13 @@ cm.replaceSelection(selection.toUpperCase()); cm.setSelections(selections); }, - + lowercase : function() { var cm = this.cm; var cursor = cm.getCursor(); var selection = cm.getSelection(); var selections = cm.listSelections(); - + cm.replaceSelection(selection.toLowerCase()); cm.setSelections(selections); }, @@ -2809,15 +2811,15 @@ var cursor = cm.getCursor(); var selection = cm.getSelection(); - if (selection === "") + if (selection === "") { cm.replaceSelection("- " + selection); - } - else + } + else { var selectionText = selection.split("\n"); - for (var i = 0, len = selectionText.length; i < len; i++) + for (var i = 0, len = selectionText.length; i < len; i++) { selectionText[i] = (selectionText[i] === "") ? "" : "- " + selectionText[i]; } @@ -2831,7 +2833,7 @@ var cursor = cm.getCursor(); var selection = cm.getSelection(); - if(selection === "") + if(selection === "") { cm.replaceSelection("1. " + selection); } @@ -2839,7 +2841,7 @@ { var selectionText = selection.split("\n"); - for (var i = 0, len = selectionText.length; i < len; i++) + for (var i = 0, len = selectionText.length; i < len; i++) { selectionText[i] = (selectionText[i] === "") ? "" : (i+1) + ". " + selectionText[i]; } @@ -2862,7 +2864,7 @@ alert("settings.tex === false"); return this; } - + var cm = this.cm; var cursor = cm.getCursor(); var selection = cm.getSelection(); @@ -2888,7 +2890,7 @@ alert("settings.pageBreak === false"); return this; } - + var cm = this.cm; var selection = cm.getSelection(); @@ -2898,7 +2900,7 @@ image : function() { this.executePlugin("imageDialog", "image-dialog/image-dialog"); }, - + code : function() { var cm = this.cm; var cursor = cm.getCursor(); @@ -2912,17 +2914,17 @@ }, "code-block" : function() { - this.executePlugin("codeBlockDialog", "code-block-dialog/code-block-dialog"); + this.executePlugin("codeBlockDialog", "code-block-dialog/code-block-dialog"); }, "preformatted-text" : function() { this.executePlugin("preformattedTextDialog", "preformatted-text-dialog/preformatted-text-dialog"); }, - + table : function() { - this.executePlugin("tableDialog", "table-dialog/table-dialog"); + this.executePlugin("tableDialog", "table-dialog/table-dialog"); }, - + datetime : function() { var cm = this.cm; var selection = cm.getSelection(); @@ -2932,20 +2934,20 @@ cm.replaceSelection(datefmt); }, - + emoji : function() { this.executePlugin("emojiDialog", "emoji-dialog/emoji-dialog"); }, - + "html-entities" : function() { this.executePlugin("htmlEntitiesDialog", "html-entities-dialog/html-entities-dialog"); }, - + "goto-line" : function() { this.executePlugin("gotoLineDialog", "goto-line-dialog/goto-line-dialog"); }, - watch : function() { + watch : function() { this[this.settings.watch ? "unwatch" : "watch"](); }, @@ -2960,7 +2962,7 @@ clear : function() { this.clear(); }, - + search : function() { this.search(); }, @@ -2973,7 +2975,7 @@ this.showInfoDialog(); } }; - + editormd.keyMaps = { "Ctrl-1" : "h1", "Ctrl-2" : "h2", @@ -2983,12 +2985,12 @@ "Ctrl-6" : "h6", "Ctrl-B" : "bold", // if this is string == editormd.toolbarHandlers.xxxx "Ctrl-D" : "datetime", - + "Ctrl-E" : function() { // emoji var cm = this.cm; var cursor = cm.getCursor(); var selection = cm.getSelection(); - + if (!this.settings.emoji) { alert("Error: settings.emoji == false"); @@ -3005,12 +3007,12 @@ "Ctrl-H" : "hr", "Ctrl-I" : "italic", "Ctrl-K" : "code", - + "Ctrl-L" : function() { var cm = this.cm; var cursor = cm.getCursor(); var selection = cm.getSelection(); - + var title = (selection === "") ? "" : " \""+selection+"\""; cm.replaceSelection("[" + selection + "]("+title+")"); @@ -3020,12 +3022,12 @@ } }, "Ctrl-U" : "list-ul", - + "Shift-Ctrl-A" : function() { var cm = this.cm; var cursor = cm.getCursor(); var selection = cm.getSelection(); - + if (!this.settings.atLink) { alert("Error: settings.atLink == false"); @@ -3038,24 +3040,24 @@ cm.setCursor(cursor.line, cursor.ch + 1); } }, - + "Shift-Ctrl-C" : "code", "Shift-Ctrl-Q" : "quote", "Shift-Ctrl-S" : "del", "Shift-Ctrl-K" : "tex", // KaTeX - + "Shift-Alt-C" : function() { var cm = this.cm; var cursor = cm.getCursor(); var selection = cm.getSelection(); - + cm.replaceSelection(["```", selection, "```"].join("\n")); if (selection === "") { cm.setCursor(cursor.line, cursor.ch + 3); - } + } }, - + "Shift-Ctrl-Alt-C" : "code-block", "Shift-Ctrl-H" : "html-entities", "Shift-Alt-H" : "help", @@ -3064,12 +3066,12 @@ "Shift-Alt-U" : "ucwords", "Shift-Ctrl-Alt-U" : "ucfirst", "Shift-Alt-L" : "lowercase", - + "Shift-Ctrl-I" : function() { var cm = this.cm; var cursor = cm.getCursor(); var selection = cm.getSelection(); - + var title = (selection === "") ? "" : " \""+selection+"\""; cm.replaceSelection("![" + selection + "]("+title+")"); @@ -3078,7 +3080,7 @@ cm.setCursor(cursor.line, cursor.ch + 4); } }, - + "Shift-Ctrl-Alt-I" : "image", "Shift-Ctrl-L" : "link", "Shift-Ctrl-O" : "list-ol", @@ -3089,59 +3091,59 @@ "F10" : "preview", "F11" : "fullscreen", }; - + /** * 清除字符串两边的空格 * Clear the space of strings both sides. - * + * * @param {String} str string - * @returns {String} trimed string + * @returns {String} trimed string */ - + var trim = function(str) { return (!String.prototype.trim) ? str.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, "") : str.trim(); }; - + editormd.trim = trim; - + /** * 所有单词首字母大写 * Words first to uppercase - * + * * @param {String} str string * @returns {String} string */ - + var ucwords = function (str) { - return str.toLowerCase().replace(/\b(\w)|\s(\w)/g, function($1) { + return str.toLowerCase().replace(/\b(\w)|\s(\w)/g, function($1) { return $1.toUpperCase(); }); }; - + editormd.ucwords = editormd.wordsFirstUpperCase = ucwords; - + /** * 字符串首字母大写 * Only string first char to uppercase - * + * * @param {String} str string * @returns {String} string */ - - var firstUpperCase = function(str) { + + var firstUpperCase = function(str) { return str.toLowerCase().replace(/\b(\w)/, function($1){ return $1.toUpperCase(); }); }; - + var ucfirst = firstUpperCase; - + editormd.firstUpperCase = editormd.ucfirst = firstUpperCase; - + editormd.urls = { atLinkBase : "https://github.com/" }; - + editormd.regexs = { atLink : /@(\w+)/g, email : /(\w+)@(\w+)\.(\w+)\.?(\w+)?/g, @@ -3160,7 +3162,7 @@ ext : ".png" }; - // Twitter Emoji (Twemoji) graphics files url path + // Twitter Emoji (Twemoji) graphics files url path editormd.twemoji = { path : "http://twemoji.maxcdn.com/36x36/", ext : ".png" @@ -3169,7 +3171,7 @@ /** * 自定义marked的解析器 * Custom Marked renderer rules - * + * * @param {Array} markdownToC 传入用于接收TOC的数组 * @returns {Renderer} markedRenderer 返回marked的Renderer自定义对象 */ @@ -3178,7 +3180,7 @@ var defaults = { toc : true, // Table of contents tocm : false, - tocStartLevel : 1, // Said from H1 to create ToC + tocStartLevel : 1, // Said from H1 to create ToC pageBreak : true, atLink : true, // for @link emailLink : true, // for mail address auto link @@ -3188,12 +3190,12 @@ flowChart : false, // flowChart.js only support IE9+ sequenceDiagram : false, // sequenceDiagram.js only support IE9+ }; - - var settings = $.extend(defaults, options || {}); + + var settings = $.extend(defaults, options || {}); var marked = editormd.$marked; var markedRenderer = new marked.Renderer(); - markdownToC = markdownToC || []; - + markdownToC = markdownToC || []; + var regexs = editormd.regexs; var atLinkReg = regexs.atLink; var emojiReg = regexs.emoji; @@ -3205,11 +3207,11 @@ var pageBreakReg = regexs.pageBreak; markedRenderer.emoji = function(text) { - - text = text.replace(editormd.regexs.emojiDatetime, function($1) { + + text = text.replace(editormd.regexs.emojiDatetime, function($1) { return $1.replace(/:/g, ":"); }); - + var matchs = text.match(emojiReg); if (!matchs || !settings.emoji) { @@ -3217,7 +3219,7 @@ } for (var i = 0, len = matchs.length; i < len; i++) - { + { if (matchs[i] === ":+1:") { matchs[i] = ":\\+1:"; } @@ -3227,11 +3229,11 @@ var name = $1.replace(/:/g, ""); if (faMatchs) - { + { for (var fa = 0, len1 = faMatchs.length; fa < len1; fa++) { var faName = faMatchs[fa].replace(/:/g, ""); - + return ""; } } @@ -3240,15 +3242,15 @@ var emdlogoMathcs = $1.match(editormdLogoReg); var twemojiMatchs = $1.match(twemojiReg); - if (emdlogoMathcs) - { + if (emdlogoMathcs) + { for (var x = 0, len2 = emdlogoMathcs.length; x < len2; x++) { var logoName = emdlogoMathcs[x].replace(/:/g, ""); return ""; } } - else if (twemojiMatchs) + else if (twemojiMatchs) { for (var t = 0, len3 = twemojiMatchs.length; t < len3; t++) { @@ -3273,8 +3275,8 @@ markedRenderer.atLink = function(text) { if (atLinkReg.test(text)) - { - if (settings.atLink) + { + if (settings.atLink) { text = text.replace(emailReg, function($1, $2, $3, $4) { return $1.replace(/@/g, "_#_@_#_"); @@ -3284,7 +3286,7 @@ return "" + $1 + ""; }).replace(/_#_@_#_/g, "@"); } - + if (settings.emailLink) { text = text.replace(emailLinkReg, function($1, $2, $3, $4, $5) { @@ -3297,7 +3299,7 @@ return text; }; - + markedRenderer.link = function (href, title, text) { if (this.options.sanitize) { @@ -3313,14 +3315,14 @@ } var out = "" + text.replace(/@/g, "@") + ""; } @@ -3332,14 +3334,14 @@ return out; }; - + markedRenderer.heading = function(text, level, raw) { - + var linkText = text; var hasLinkReg = /\s*\]*)\>(.*)\<\/a\>\s*/; var getLinkTextReg = /\s*\]+)\>([^\>]*)\<\/a\>\s*/g; - if (hasLinkReg.test(text)) + if (hasLinkReg.test(text)) { var tempText = []; text = text.split(/\]+)\>([^\>]*)\<\/a\>/); @@ -3351,23 +3353,23 @@ text = tempText.join(" "); } - + text = trim(text); - + var escapedText = text.toLowerCase().replace(/[^\w]+/g, "-"); var toc = { text : text, level : level, slug : escapedText }; - + var isChinese = /^[\u4e00-\u9fa5]+$/.test(text); var id = (isChinese) ? escape(text).replace(/\%/g, "") : text.toLowerCase().replace(/[^\w]+/g, "-"); markdownToC.push(toc); - + var headingHTML = ""; - + headingHTML += ""; headingHTML += ""; headingHTML += (hasLinkReg) ? this.atLink(this.emoji(linkText)) : this.atLink(this.emoji(text)); @@ -3375,13 +3377,13 @@ return headingHTML; }; - + markedRenderer.pageBreak = function(text) { if (pageBreakReg.test(text) && settings.pageBreak) { text = "
    "; } - + return text; }; @@ -3391,35 +3393,35 @@ var isTeXAddClass = (isTeXLine) ? " class=\"" + editormd.classNames.tex + "\"" : ""; var isToC = (settings.tocm) ? /^(\[TOC\]|\[TOCM\])$/.test(text) : /^\[TOC\]$/.test(text); var isToCMenu = /^\[TOCM\]$/.test(text); - - if (!isTeXLine && isTeXInline) + + if (!isTeXLine && isTeXInline) { text = text.replace(/(\$\$([^\$]*)\$\$)+/g, function($1, $2) { return "" + $2.replace(/\$/g, "") + ""; }); - } - else + } + else { text = (isTeXLine) ? text.replace(/\$/g, "") : text; } - + var tocHTML = "
    " + text + "
    "; - + return (isToC) ? ( (isToCMenu) ? "
    " + tocHTML + "

    " : tocHTML ) : ( (pageBreakReg.test(text)) ? this.pageBreak(text) : "" + this.atLink(this.emoji(text)) + "

    \n" ); }; - markedRenderer.code = function (code, lang, escaped) { + markedRenderer.code = function (code, lang, escaped) { if (lang === "seq" || lang === "sequence") { return "
    " + code + "
    "; - } + } else if ( lang === "flow") { return "
    " + code + "
    "; - } - else + } + else { return marked.Renderer.prototype.code.apply(this, arguments); @@ -3429,64 +3431,64 @@ markedRenderer.tablecell = function(content, flags) { var type = (flags.header) ? "th" : "td"; var tag = (flags.align) ? "<" + type +" style=\"text-align:" + flags.align + "\">" : "<" + type + ">"; - + return tag + this.atLink(this.emoji(content)) + "\n"; }; markedRenderer.listitem = function(text) { - if (settings.taskList && /^\s*\[[x\s]\]\s*/.test(text)) + if (settings.taskList && /^\s*\[[x\s]\]\s*/.test(text)) { text = text.replace(/^\s*\[\s\]\s*/, " ") .replace(/^\s*\[x\]\s*/, " "); return "
  • " + this.atLink(this.emoji(text)) + "
  • "; } - else + else { return "
  • " + this.atLink(this.emoji(text)) + "
  • "; } }; - + return markedRenderer; }; - + /** * * 生成TOC(Table of Contents) * Creating ToC (Table of Contents) - * + * * @param {Array} toc 从marked获取的TOC数组列表 * @param {Element} container 插入TOC的容器元素 * @param {Integer} startLevel Hx 起始层级 * @returns {Object} tocContainer 返回ToC列表容器层的jQuery对象元素 */ - + editormd.markdownToCRenderer = function(toc, container, tocDropdown, startLevel) { - - var html = ""; + + var html = ""; var lastLevel = 0; var classPrefix = this.classPrefix; - + startLevel = startLevel || 1; - - for (var i = 0, len = toc.length; i < len; i++) + + for (var i = 0, len = toc.length; i < len; i++) { var text = toc[i].text; var level = toc[i].level; - + if (level < startLevel) { continue; } - - if (level > lastLevel) + + if (level > lastLevel) { html += ""; } - else if (level < lastLevel) + else if (level < lastLevel) { html += (new Array(lastLevel - level + 2)).join(""); - } - else + } + else { html += ""; } @@ -3494,44 +3496,44 @@ html += "
  • " + text + "
  • "):"",r+='
  • '+c+"