-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmdedit.min.js
1 lines (1 loc) · 15.4 KB
/
mdedit.min.js
1
!function(e,t){"function"==typeof define&&define.amd?define(["prismjs"],t):"object"==typeof exports?module.exports=t(require("prismjs")):e.mdEdit=t(e.Prism)}(this,function(e){function t(e,t,n,r){return n=+n||0,r=r||"",e.slice(0,t)+r+e.slice(t+n)}function n(e){this.elt=e}function r(e,t){var n=e.textContent,r=e.parentNode,i=n.slice(0,t),a=n.slice(t);e.textContent=a;var s=document.createTextNode(i);r.insertBefore(c,e),r.insertBefore(s,c);var o=c.offsetTop,d=c.offsetHeight,l=this.elt.offsetHeight,h=this.elt.scrollTop;e.textContent=n,r.removeChild(c),r.removeChild(s),0>o-h?this.elt.scrollTop=o:o-h+d>l&&(this.elt.scrollTop=o+d-l)}function i(e,t){if(!e)return null;var n=0,r=e;do{var i=r;if(r=r.firstChild)do{var a=r.textContent.length;if(t>=n&&n+a>t)break;n+=a}while(r=r.nextSibling);if(!r)break}while(r&&r.hasChildNodes()&&3!=r.nodeType);if(r)return{element:r,offset:t-n};if(i){for(r=i;r&&r.lastChild;)r=r.lastChild;return 3===r.nodeType?{element:r,offset:r.textContent.length}:{element:r,offset:0}}return{element:e,offset:0,error:!0}}function a(e){this.editor=e,this.undoStack=[],this.redoStack=[]}function s(e,t){if(!(this instanceof s))return new s(e,t);t=t||{},"PRE"===e.tagName?this.el=e:(this.el=document.createElement("pre"),e.appendChild(this.el));var r=t.className||"";this.el.className=this.el.className?this.el.className+" ":"",this.el.className+="mdedit"+(r?" "+r:""),this.el.setAttribute("contenteditable",!0);var i=this.inner=document.createElement("div");i.innerHTML=this.el.innerHTML,this.el.innerHTML="",this.el.appendChild(i),this.selMgr=new n(e),this.undoMgr=new a(this),l.bind(e,"cut",this.cut.bind(this)),l.bind(e,"paste",this.paste.bind(this)),l.bind(e,"keyup",this.keyup.bind(this)),l.bind(e,"input",this.changed.bind(this)),l.bind(e,"keydown",this.keydown.bind(this)),l.bind(e,"keypress",this.keypress.bind(this));var o=t.change;this.changeCb=o||function(){},this.changed()}var o={scalar:{pattern:/([\-:]\s*(![^\s]+)?[ \t]*[|>])[ \t]*(?:(\n[ \t]+)[^\r\n]+(?:\3[^\r\n]+)*)/,lookbehind:!0,alias:"string"},comment:/#[^\n]+/,key:{pattern:/(\s*[:\-,[{\n?][ \t]*(![^\s]+)?[ \t]*)[^\n{[\]},#]+?(?=\s*:\s)/,lookbehind:!0,alias:"atrule"},directive:{pattern:/((^|\n)[ \t]*)%[^\n]+/,lookbehind:!0,alias:"important"},datetime:{pattern:/([:\-,[{]\s*(![^\s]+)?[ \t]*)(\d{4}-\d\d?-\d\d?([tT]|[ \t]+)\d\d?:\d{2}:\d{2}(\.\d*)?[ \t]*(Z|[-+]\d\d?(:\d{2})?)?|\d{4}-\d{2}-\d{2}|\d\d?:\d{2}(:\d{2}(\.\d*)?)?)(?=[ \t]*(\n|$|,|]|}))/,lookbehind:!0,alias:"number"},"boolean":{pattern:/([:\-,[{]\s*(![^\s]+)?[ \t]*)(true|false)[ \t]*(?=\n|$|,|]|})/i,lookbehind:!0,alias:"important"},"null":{pattern:/([:\-,[{]\s*(![^\s]+)?[ \t]*)(null|~)[ \t]*(?=\n|$|,|]|})/i,lookbehind:!0,alias:"important"},string:{pattern:/([:\-,[{]\s*(![^\s]+)?[ \t]*)("(?:\\.|[^"\\])*"|'(?:\\.|[^'\\])*')(?=[ \t]*(\n|$|,|]|}))/,lookbehind:!0},number:{pattern:/([:\-,[{]\s*(![^\s]+)?[ \t]*)[+\-]?(0x[\dA-Fa-f]+|0o[0-7]+|(\d+\.?\d*|\.?\d+)(e[\+\-]?\d+)?|\.inf|\.nan)[ \t]*(?=\n|$|,|]|})/i,lookbehind:!0},tag:/![^\s]+/,important:/[&*][\w]+/,punctuation:/([:[\]{}\-,|>?]|---|\.\.\.)/},d=function(){function t(e,t){a[e]=i[e]=r[e]=t}function n(e,t){a[e]=r[e]=t}var r={comment:e.languages.markup.comment};r["front-matter"]={pattern:/^---\n[\s\S]*?\n---(?=\n|$)/,inside:{"marker front-matter-marker start":/^---/,"marker front-matter-marker end":/---$/,rest:o}};var i={},a={},s={markup:["markup","html","xml"],javascript:["javascript","js"]};for(var d in e.languages)if(e.languages.hasOwnProperty(d)){var l=e.languages[d];if("function"!=typeof l){var h=s[d],c=h?h.join("|"):d;n("code-block fenced "+d,{pattern:new RegExp("(^ {0,3}|\\n {0,3})(([`~])\\3\\3) *("+c+")( [^`\n]*)? *\\n(?:[\\s\\S]*?)\\n {0,3}(\\2\\3*(?= *\\n)|$)","gi"),lookbehind:!0,inside:{"code-language":{pattern:/(^([`~])\2+)((?!\2)[^\2\n])+/,lookbehind:!0},"marker code-fence start":/^([`~])\1+/,"marker code-fence end":/([`~])\1+$/,"code-inner":{pattern:/(^\n)[\s\S]*(?=\n$)/,lookbehind:!0,alias:"language-"+d,inside:l}}})}}n("code-block fenced untagged",{pattern:/(^ {0,3}|\n {0,3})(([`~])\3\3)[^`\n]*\n(?:[\s\S]*?)\n {0,3}(\2\3*(?= *\n)|$)/g,lookbehind:!0,inside:{"code-language":{pattern:/(^([`~])\2+)((?!\2)[^\2\n])+/,lookbehind:!0},"marker code-fence start":/^([`~])\1+/,"marker code-fence end":/([`~])\1+$/,"code-inner":{pattern:/(^\n)[\s\S]*(?=\n$)/,lookbehind:!0}}}),n("heading setext-heading heading-1",{pattern:/^ {0,3}[^\s].*\n {0,3}=+[ \t]*$/gm,inside:{"marker heading-setext-line":{pattern:/^( {0,3}[^\s].*\n) {0,3}=+[ \t]*$/gm,lookbehind:!0},rest:i}}),n("heading setext-heading heading-2",{pattern:/^ {0,3}[^\s].*\n {0,3}-+[ \t]*$/gm,inside:{"marker heading-setext-line":{pattern:/^( {0,3}[^\s].*\n) {0,3}-+[ \t]*$/gm,lookbehind:!0},rest:i}});for(var p={"marker heading-hash start":/^ *#+ */,"marker heading-hash end":/ +#+ *$/,rest:i},d=1;6>=d;d+=1)n("heading heading-"+d,{pattern:new RegExp("^ {0,3}#{"+d+"}(?![#\\S]).*$","gm"),inside:p});var g={pattern:/^\[(?:\\.|[^\[\]]|\[[^\[\]]*\])*\]/,inside:{"marker bracket start":/^\[/,"marker bracket end":/\]$/,"link-text-inner":{pattern:/[\w\W]+/,inside:i}}},u={pattern:/\[(?:\\.|[^\]])*\]/,inside:{"marker bracket start":/^\[/,"marker bracket end":/\]$/,"link-label-inner":/[\w\W]+/}},f={pattern:/^!\[(?:\\.|[^\[\]]|\[[^\[\]]*\])*\]/,inside:{"marker image-bang":/^!/,"marker bracket start":/^\[/,"marker bracket end":/\]$/,"image-text-inner":{pattern:/[\w\W]+/,inside:i}}},k={pattern:/^(\s*)(?!<)(?:\\.|[^\(\)\s]|\([^\(\)\s]*\))+/,lookbehind:!0},m={pattern:/^(\s*)<(?:\\.|[^<>\n])*>/,lookbehind:!0,inside:{"marker brace start":/^</,"marker brace end":/>$/,"braced-href-inner":/[\w\W]+/}},b={pattern:/('(?:\\'|[^'])+'|"(?:\\"|[^"])+")\s*$/,inside:{"marker quote start":/^['"]/,"marker quote end":/['"]$/,"title-inner":/[\w\W]+/}},v={pattern:/\( *(?:(?!<)(?:\\.|[^\(\)\s]|\([^\(\)\s]*\))*|<(?:[^<>\n]|\\.)*>)( +('(?:[^']|\\')+'|"(?:[^"]|\\")+"))? *\)/,inside:{"marker bracket start":/^\(/,"marker bracket end":/\)$/,"link-params-inner":{pattern:/[\w\W]+/,inside:{"link-title":b,href:k,"braced-href":m}}}};n("hr",{pattern:/^[\t ]*([*\-_])([\t ]*\1){2,}([\t ]*$)/gm,inside:{"marker hr-marker":/[*\-_]/g}}),n("urldef",{pattern:/^( {0,3})\[(?:\\.|[^\]])+]: *\n? *(?:(?!<)(?:\\.|[^\(\)\s]|\([^\(\)\s]*\))*|<(?:[^<>\n]|\\.)*>)( *\n? *('(?:\\'|[^'])+'|"(?:\\"|[^"])+"))?$/gm,lookbehind:!0,inside:{"link-label":u,"marker urldef-colon":/^:/,"link-title":b,href:k,"braced-href":m}}),n("blockquote",{pattern:/^[\t ]*>[\t ]?.+(?:\n(?!\n)|.)*/gm,inside:{"marker quote-marker":/^[\t ]*>[\t ]?/gm,"blockquote-content":{pattern:/[^>]+/,inside:{rest:a}}}}),n("list",{pattern:/^[\t ]*([*+\-]|\d+\.)[\t ].+(?:\n(?!\n)|.)*/gm,inside:{li:{pattern:/^[\t ]*([*+\-]|\d+\.)[\t ].+(?:\n|[ \t]+[^*+\- \t].*\n)*/gm,inside:{"marker list-item":/^[ \t]*([*+\-]|\d+\.)[ \t]/m,rest:a}}}}),n("code-block indented",{pattern:/(^|(?:^|(?:^|\n)(?![ \t]*([*+\-]|\d+\.)[ \t]).*\n)\s*?\n)((?: {4}|\t).*(?:\n|$))+/g,lookbehind:!0}),n("p",{pattern:/[^\n](?:\n(?!\n)|.)*[^\n]/g,inside:i}),t("image",{pattern:/(^|[^\\])!\[(?:\\.|[^\[\]]|\[[^\[\]]*\])*\]\(\s*(?:(?!<)(?:\\.|[^\(\)\s]|\([^\(\)\s]*\))*|<(?:[^<>\n]|\\.)*>)(\s+('(?:[^']|\\')+'|"(?:[^"]|\\")+"))?\s*\)/,lookbehind:!0,inside:{"link-text":f,"link-params":v}}),t("link",{pattern:/(^|[^\\])\[(?:\\.|[^\[\]]|\[[^\[\]]*\])*\]\(\s*(?:(?!<)(?:\\.|[^\(\)\s]|\([^\(\)\s]*\))*|<(?:[^<>\n]|\\.)*>)(\s+('(?:[^']|\\')+'|"(?:[^"]|\\")+"))?\s*\)/,lookbehind:!0,inside:{"link-text":g,"link-params":v}}),t("image image-ref",{pattern:/(^|[^\\])!\[(?:\\.|[^\[\]]|\[[^\[\]]*\])*\] ?\[(?:\\.|[^\]])*\]/,lookbehind:!0,inside:{"link-text":f,"link-label":u}}),t("link link-ref",{pattern:/(^|[^\\])\[(?:\\.|[^\[\]]|\[[^\[\]]*\])*\] ?\[(?:\\.|[^\]])*\]/,lookbehind:!0,inside:{"link-text":g,"link-label":u}}),t("image image-ref shortcut-ref",{pattern:/(^|[^\\])!\[(?:\\.|[^\[\]]|\[[^\[\]]*\])*\]/,lookbehind:!0,inside:{"marker image-bang":/^!/,"link-text":g}}),t("link link-ref shortcut-ref",{pattern:/(^|[^\\])\[(?:\\.|[^\[\]]|\[[^\[\]]*\])*\]/,lookbehind:!0,inside:{"link-text":g}}),t("code",{pattern:/(^|[^\\])(`+)([^\r]*?[^`])\2(?!`)/g,lookbehind:!0,inside:{"marker code-marker start":/^`/,"marker code-marker end":/`$/,"code-inner":/[\w\W]+/}}),t("strong",{pattern:/(^|[^\\*_]|\\[*_])([_\*])\2(?:\n(?!\n)|.)+?\2{2}(?!\2)/g,lookbehind:!0,inside:{"marker strong-marker start":/^(\*\*|__)/,"marker strong-marker end":/(\*\*|__)$/,"strong-inner":{pattern:/[\w\W]+/,inside:i}}}),t("em",{pattern:/(^|[^\\*_]|\\[*_])(\*|_)(?:\n(?!\n)|.)+?\2(?!\2)/g,lookbehind:!0,inside:{"marker em-marker start":/^(\*|_)/,"marker em-marker end":/(\*|_)$/,"em-inner":{pattern:/[\w\W]+/,inside:i}}}),t("strike",{pattern:/(^|\n|(?!\\)\W)(~~)(?=\S)([^\r]*?\S)\2/gm,lookbehind:!0,inside:{"marker strike-marker start":/^~~/,"marker strike-marker end":/~~$/,"strike-inner":{pattern:/[\w\W]+/,inside:i}}}),t("comment",e.languages.markup.comment);var y=e.languages.markup.tag,x=y.pattern;return t("tag",{pattern:new RegExp("(^|[^\\\\])"+x.source,"i"),lookbehind:!0,inside:y.inside}),t("entity",e.languages.markup.entity),r}(),l={bind:function(e,t,n){e.addEventListener(t,n,!1)}},h={newline:function(e,t){var n=e.start,r=e.before.lastIndexOf("\n")+1,i=e.before.slice(r),a=i.match(/^\s*/)[0],s=a,o=!1;if(/^ {0,3}$/.test(a)){var d=i.slice(a.length);/^[*+\-]\s+/.test(d)?(s+=d.match(/^[*+\-]\s+/)[0],o=/^[*+\-]\s+$/.test(d)):/^\d+\.\s+/.test(d)?(s+=d.match(/^\d+\.\s+/)[0].replace(/^\d+/,function(e){return+e+1}),o=/^\d+\.\s+$/.test(d)):/^>/.test(d)&&(s+=d.match(/^>\s*/)[0],o=/^>\s*$/.test(d))}s="\n"+s;var l=e.sel;return e.sel="",o&&(l=i+l,e.before=e.before.slice(0,r),e.start-=i.length,n-=i.length,s="\n"),e.before+=s,e.start+=s.length,e.end=e.start,{add:s,del:l,start:n}},indent:function(e,n){var r=e.before.lastIndexOf("\n")+1;if(n.inverse)/\s/.test(e.before.charAt(r))&&(e.before=t(e.before,r,1),e.start-=1),e.sel=e.sel.replace(/\r?\n(?!\r?\n)\s/,"\n");else{if(!e.sel&&!n.ctrl)return e.before+=" ",e.start+=1,e.end+=1,{add:" ",del:"",start:e.start-1};e.before=t(e.before,r,0," "),e.sel=e.sel.replace(/\r?\n/,"\n "),e.start+=1}return e.end=e.start+e.sel.length,{action:"indent",start:e.start,end:e.end,inverse:n.inverse}},wrap:function(e,t){var n={"(":")","[":"]","{":"}","<":">"}[t.bracket]||t.bracket;return e.before+=t.bracket,e.after=n+e.after,e.start+=1,e.end+=1,{add:t.bracket+e.sel+n,del:e.sel,start:e.start-1,end:e.end-1}}};n.prototype.getStart=function(){var e=getSelection();if(!e.rangeCount)return 0;var t=e.getRangeAt(0),n=t.startContainer,r=n,i=t.startOffset;if(!(16&this.elt.compareDocumentPosition(n)))return 0;do{for(;n=n.previousSibling;)n.textContent&&(i+=n.textContent.length);n=r=r.parentNode}while(n&&n!==this.elt);return i},n.prototype.getEnd=function(){var e=getSelection();return e.rangeCount?this.getStart()+String(e.getRangeAt(0)).length:0},n.prototype.setRange=function(e,t,n){var a=document.createRange(),s=i(this.elt,e),o=s;t&&t!==e?o=i(this.elt,t):n!==!1&&r.call(this,o.element,o.offset),a.setStart(s.element,s.offset),a.setEnd(o.element,o.offset);var d=getSelection();d.removeAllRanges(),d.addRange(a)};var c=document.createElement("span");return c.style.position="absolute",c.innerHTML="|",a.prototype.action=function(e){this.undoStack.length&&this.canCombine(this.undoStack[this.undoStack.length-1],e)?this.undoStack.push(this.combine(this.undoStack.pop(),e)):this.undoStack.push(e),this.redoStack=[]},a.prototype.canCombine=function(e,t){return!(e.action||t.action||Array.isArray(e)||Array.isArray(t)||e.del&&t.add||e.add&&t.del||e.add&&!t.add||!e.add&&t.add||e.add&&e.del||t.add&&t.del||e.start+e.add.length!==t.start+t.del.length)},a.prototype.combine=function(e,t){return{add:e.add+t.add,del:t.del+e.del,start:Math.min(e.start,t.start)}},a.prototype.undo=function(){if(this.undoStack.length){var e=this.undoStack.pop();this.redoStack.push(e),this.applyInverse(e)}},a.prototype.redo=function(){if(this.redoStack.length){var e=this.redoStack.pop();this.undoStack.push(e),this.apply(e)}},a.prototype.apply=function p(e){return Array.isArray(e)?void e.forEach(p.bind(this)):void(e.action?this.editor.action(e.action,{inverse:e.inverse,start:e.start,end:e.end,noHistory:!0}):this.editor.apply(e))},a.prototype.applyInverse=function g(e){return Array.isArray(e)?void e.forEach(g.bind(this)):void(e.action?this.editor.action(e.action,{inverse:!e.inverse,start:e.start,end:e.end,noHistory:!0}):this.editor.apply({start:e.start,end:e.end,del:e.add,add:e.del}))},s.prototype.fireChange=function(){var e=this._prevValue,t=this.getValue();e!==t&&(this.changeCb(t),this._prevValue=t)},s.prototype.setValue=function(e){this.setText(e),this.changed()},s.prototype.getValue=function(){return this.getText()},s.prototype.getText=function(){return this.inner.textContent},s.prototype.setText=function(e){this.inner.textContent=e},s.prototype.keyup=function(e){var t=e&&e.keyCode||0;this.getText();[9,91,93,16,17,18,20,13,112,113,114,115,116,117,118,119,120,121,122,123,27].indexOf(t)>-1||-1===[33,34,35,36,37,39,38,40].indexOf(t)&&this.changed()},s.prototype.changed=function(t){var n=this.getText(),r=this.selMgr.getStart(),i=this.selMgr.getEnd();this.saveScrollPos();var a;if(n===this._prevCode?this.inner.innerHTML!==this._prevHTML&&(a=this._prevHTML):this._prevHTML=a=e.highlight(n,d),this._prevCode=n,void 0!==a){/\n$/.test(n)||(a+="\n");var s=this.inner.cloneNode(!1);s.innerHTML=a,this.el.replaceChild(s,this.inner),this.inner=s}this.restoreScrollPos(),(null!==r||null!==i)&&this.selMgr.setRange(r,i),this.fireChange()},s.prototype.saveScrollPos=function(){void 0===this.st&&(this.st=this.el.scrollTop),setTimeout(function(){this.st=void 0}.bind(this),500)},s.prototype.restoreScrollPos=function(){this.el.scrollTop=this.st,this.st=void 0},s.prototype.keypress=function(e){var t=e.metaKey||e.ctrlKey;if(!t){var n=e.charCode;if(n){var r=this.selMgr.getStart(),i=this.selMgr.getEnd(),a=String.fromCharCode(n);return/[\[\{\(<"'~\*_]/.test(a)&&r!==i?(this.action("wrap",{bracket:a}),void e.preventDefault()):void this.undoMgr.action({add:a,del:r===i?"":this.getText().slice(r,i),start:r})}}},s.prototype.keydown=function(e){var t=e.metaKey||e.ctrlKey;switch(e.keyCode){case 8:case 46:var n=this.selMgr.getStart(),r=this.selMgr.getEnd(),i=n===r?1:Math.abs(r-n);n=8===e.keyCode?r-i:n,this.undoMgr.action({add:"",del:this.getText().slice(n,n+i),start:n});break;case 9:t||(this.action("indent",{inverse:e.shiftKey}),e.preventDefault());break;case 219:case 221:t&&!e.shiftKey&&(this.action("indent",{inverse:219===e.keyCode,ctrl:!0}),e.preventDefault());break;case 13:this.action("newline"),e.preventDefault();break;case 89:t&&(this.undoMgr.redo(),e.preventDefault());break;case 90:t&&(e.shiftKey?this.undoMgr.redo():this.undoMgr.undo(),e.preventDefault())}},s.prototype.apply=function(e){this.setText(t(this.getText(),e.start,e.del.length,e.add)),this.selMgr.setRange(e.start,e.start+e.add.length),this.changed()},s.prototype.action=function(e,t){t=t||{};var n=this.getText(),r=t.start||this.selMgr.getStart(),i=t.end||this.selMgr.getEnd(),a={start:r,end:i,before:n.slice(0,r),after:n.slice(i),sel:n.slice(r,i)},s=h[e](a,t);this.saveScrollPos(),this.setText(a.before+a.sel+a.after),s&&!t.noHistory&&this.undoMgr.action(s),this.selMgr.setRange(a.start,a.end,!1),this.changed()},s.prototype.cut=function(){var e=this.selMgr.getStart(),t=this.selMgr.getEnd();e!==t&&this.undoMgr.action({add:"",del:this.getText().slice(e,t),start:e})},s.prototype.paste=function(e){function t(e){a.undoMgr.action({add:e,del:i,start:n}),n+=e.length,a.selMgr.setRange(n,n),a.changed()}var n=this.selMgr.getStart(),r=this.selMgr.getEnd(),i=n===r?"":this.getText().slice(n,r),a=this;if(e.clipboardData){e.preventDefault();var s=e.clipboardData.getData("text/plain");this.apply({add:s,del:i,start:n}),t(s)}else setTimeout(function(){var e=a.selMgr.getEnd();t(a.getText().slice(n,e))},0)},s});