framework/extensions/markdown/js/dist/forum.js
2021-02-27 21:06:27 +00:00

2 lines
16 KiB
JavaScript

module.exports=function(e){var t={};function n(i){if(t[i])return t[i].exports;var r=t[i]={i:i,l:!1,exports:{}};return e[i].call(r.exports,r,r.exports,n),r.l=!0,r.exports}return n.m=e,n.c=t,n.d=function(e,t,i){n.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:i})},n.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},n.t=function(e,t){if(1&t&&(e=n(e)),8&t)return e;if(4&t&&"object"==typeof e&&e&&e.__esModule)return e;var i=Object.create(null);if(n.r(i),Object.defineProperty(i,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var r in e)n.d(i,r,function(t){return e[t]}.bind(null,r));return i},n.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return n.d(t,"a",t),t},n.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},n.p="",n(n.s=6)}([function(e,t){e.exports=flarum.core.compat.extend},function(e,t){e.exports=flarum.core.compat["components/TextEditor"]},function(e,t){e.exports=flarum.core.compat.Component},function(e,t){e.exports=flarum.core.compat["helpers/icon"]},function(e,t,n){e.exports=function(){"use strict";const e=/^[ \t]*(?:(?:[-+*]|\d+\.)[ \t]+(?:\[[ x]][ \t]+)?|>[ \t]*)*(?::[ \t]*)?/,t=/(?:[-+*]|\d+\.)[ \t]+(?:\[[ x]][ \t]+)?$/,n=/[-+*\[\]x\d.]/g,i=/[^ \t]/g,r=/(\d+)\.(?=[ \t]+$)/,o=/(?:(?:^[ \t]+)?(?:[-+*]|\d+\.|[>:])(?:[ \t]+\[[ x]])?[ \t]*|^[ \t]+)$/,s=/[*_]/,l=/[-=\s"'`<>\[\](){}+*^$\\.|]$/,a=/^(?:\n|$)/,c=/^(?!$)/gm,u={"`":/^``$/m,"~":/^~~$/m},d={"[":"]","(":")","{":"}","<":">"},f={"]":"[",")":"(","}":"{",">":"<"},h={enter:(n,i,s,l,c)=>{const u=s?null:function(t){const n=t.lastIndexOf("\n")+1,i=t.substring(n),r=e.exec(i);return{line:i,offset:n,prefix:r&&r[0]}}(i);if(u)if(u.line&&u.line.charAt(u.line.length-1)in d){const e=u.prefix?y(u.prefix,!0):"";l="\n"+e+l,c.shiftKey||(i+="\n"+e+n.indent)}else u.prefix?!c.shiftKey&&u.prefix===u.line&&a.test(l)?i=i.substring(0,u.offset)+function(e){return e.replace(o,"")}(u.prefix):!c.shiftKey&&function(e){return t.test(e)}(u.prefix)?i+="\n"+function(e){return e.replace(r,(function(e,t){return parseInt(t)+1+"."}))}(u.prefix):i+="\n"+y(u.prefix,c.shiftKey):i+="\n";else i+="\n";return{v:i+l,s:i.length}},indent:(e,t,n,i)=>{let r=t.length,o=t.lastIndexOf("\n")+1;return o<r&&(n=t.substring(o)+n,t=t.substring(0,o)),(o<r||!n)&&(r+=e.indent.length),n?n=n.replace(c,e.indent):t+=e.indent,{v:t+n+i,s:r,e:n?o+n.length:r}},outdent:(e,t,n,i)=>{let r=t.length,o=t.lastIndexOf("\n")+1;return o<r&&(n=t.substring(o)+n,t=t.substring(0,o),n.substring(0,e.indent.length)===e.indent&&(r-=e.indent.length)),{v:t+(n=n.replace(e.reOutdent,""))+i,s:r,e:o+n.length}},inline:(e,t,n,i,r)=>n||r.key in d||i.charAt(0)!==r.key?n||("'"!==r.key||l.test(t))&&!(r.key in f)?!n&&r.key in u&&u[r.key].test(t)?{v:t+r.key+"language\n"+r.key+r.key+r.key+("\n"!==i.charAt(0)?"\n":"")+i,s:t.length+1,e:t.length+9}:r.key===t.slice(-1)&&r.key===i.slice(0,1)?{v:t.slice(0,-1)+n+i.slice(1),s:t.length-1,e:t.length-1+n.length}:{v:t+(f[r.key]||r.key)+n+(d[r.key]||r.key)+i,s:t.length+1,e:t.length+1+n.length}:{v:t+r.key+i,s:t.length+1}:{v:t+(s.test(r.key)?r.key+r.key:"")+i,s:t.length+1}};class p{constructor(e){this.options=e}handleKey(e,t,n,i){const r=this.options.keyMap.find(e=>function(e,t){for(const n in t)if(t.hasOwnProperty(n)&&e[n]!==t[n])return!1;return!0}(i,e.key));if(r&&h[r.action])return h[r.action](this.options,e,t,n,i)}}function y(e,t){return e.replace(t?i:n," ")}const m=/mac|iphone|ipad|ipod/i.test(navigator.platform),v=/firefox/i.test(navigator.userAgent),x=m?"metaKey":"ctrlKey",g={enter:["Enter","Shift+Enter"],indent:["Tab","Cmd+m"],outdent:["Shift+Tab","Cmd+Shift+m"],inline:['"',"'","`","*","_","[","]","(",")","{","}","<",">"]};function k(e={}){const t={},n=[];for(let i in g)if(g.hasOwnProperty(i)){let r=e[i]||g[i];Array.isArray(r)||(r=r.toString().trim().split(/\s*[|,]\s*/g)),n.push.apply(n,r.map(e=>{const n=b(e);return n.key in t||(t[n.key]=0),++t[n.key],{key:n,action:i}}))}return n.forEach(e=>{var n;t[e.key.key]>1&&("ctrlKey"in(n=e.key)||(n.ctrlKey=!1),"altKey"in n||(n.altKey=!1),"shiftKey"in n||(n.shiftKey=!1),"metaKey"in n||(n.metaKey=!1))}),n}function b(e){const t={};return e.trim().split(/\s*\+\s*/g).forEach((function(e){switch(e.toLowerCase()){case"cmd":t[x]=!0;break;case"ctrl":case"alt":case"shift":case"meta":t[e.toLowerCase()+"Key"]=!0;break;default:t.key=e}})),t}function E(){const e=document.createElement("div");return e.setAttribute("aria-hidden","true"),e.tabIndex=-1,e.contentEditable=!0,e.textContent="0",e.addEventListener("focus",()=>setTimeout(()=>e.blur(),0)),e.style.position="fixed",e.style.overflow="hidden",e.style.left="-1000px",e.style.top="50%",e.style.width=e.style.height="1px",e.style.opacity="0",e}function S(e,t=!1){return{s:e.selectionStart,e:e.selectionEnd,x:e.scrollLeft,y:e.scrollTop,v:e.value.replace(/\u00A0/g," "),c:t}}function w(e,t){e.lock=!0;const n=void 0===t.e?t.s:t.e,i=void 0===t.x?e.elem.scrollLeft:t.x,r=void 0===t.y?e.elem.scrollTop:t.y;T(e,{...t,e:n,x:i,y:r,c:!0},!0),e.lock=!1}function O(e,t){if(!e.elem||t.defaultPrevented)return;if(v&&t[x]&&"z"===t.key)return void e.elem.blur();const n=S(e.elem),i=n.v.substring(0,n.s),r=n.v.substring(n.s,n.e),o=n.v.substring(n.e);for(let n=0,s=e.options.extensions.length;n<s;n++){const s=e.options.extensions[n].handleKey(i,r,o,t);if(s){t.preventDefault(),w(e,s);break}}}function K(e,t){if(e.lock||"insertFromPaste"===t.type)return;e.lock=!0;const n=S(e.elem);document.execCommand("undo"),T(e,n),e.lock=!1}function j(e){e.lock||(e.lock=!0,e.idx=+(e.helper.textContent||"0"),e.state=e.history[e.idx],I(e.elem,e.state),e.lock=!1)}function L(e){e.helper.textContent="0",e.state=S(e.elem),e.history=[e.state],e.idx=0}function T(e,t,n=!1){(e.idx<1||n&&!e.state.c)&&C(e,S(e.elem,!0)),e.state.c?function(e,t){e.history.splice(++e.idx,e.history.length,t),e.state=t,e.helper.focus(),document.execCommand("selectAll"),document.execCommand("insertText",!1,e.idx.toString())}(e,t):C(e,t),I(e.elem,e.state)}function C(e,t){Object.assign(e.state,t)}function I(e,t){e.value=t.v,e.selectionStart=t.s,e.selectionEnd=t.e,e.scrollTo(t.x,t.y),e.focus()}class P{constructor(e,t){const n=function(e={}){const t=function(e=4){return"number"!=typeof e&&(e=(e+"").length),new Array(e+1).join(" ")}(e.indent),n={indent:t,reOutdent:new RegExp("^"+t,"mg"),keyMap:k(e.keyMap),extensions:e.extensions||[]};return n.extensions.push(new p(n)),n}(t),i=this.ed={elem:e,options:n,helper:E(),history:[],state:void 0,idx:-1,lock:!1};i.onKeyDown=O.bind(null,i),i.onInput=K.bind(null,i),i.onUndo=j.bind(null,i),i.helper.addEventListener("input",i.onUndo),document.body.appendChild(i.helper),this.setElement(e)}getElement(){return this.ed.elem}setElement(e){this.ed.elem&&(this.ed.options.extensions.forEach(e=>e.cleanup&&e.cleanup(this)),this.ed.elem.removeEventListener("input",this.ed.onInput),this.ed.elem.removeEventListener("keydown",this.ed.onKeyDown)),this.ed.elem=e,e.addEventListener("keydown",this.ed.onKeyDown),e.addEventListener("input",this.ed.onInput),L(this.ed),this.ed.options.extensions.forEach(e=>e.init&&e.init(this))}getValue(){return this.ed.elem.value.replace(/\u00A0/g," ")}setValue(e,t=!1){this.ed.elem.value=e,t||L(this.ed)}pushState(e){w(this.ed,e)}destroy(){return this.ed.options.extensions.forEach(e=>e.cleanup&&e.cleanup(this)),this.ed.elem.removeEventListener("keydown",this.ed.onKeyDown),this.ed.elem.removeEventListener("input",this.ed.onInput),this.ed.helper.removeEventListener("input",this.ed.onUndo),document.body.removeChild(this.ed.helper),this.ed.options.extensions.forEach(e=>e.destroy&&e.destroy()),Object.assign(this.ed,{elem:null,helper:null,options:null,reOutdent:null,onKeyDown:null,onInput:null,onUndo:null,history:null}),Object.assign(this,{ed:null}),null}}return function(e){e.version="v2.0.9"}(P||(P={})),P}()},function(e,t){e.exports=flarum.core.compat["utils/BasicEditorDriver"]},function(e,t,n){"use strict";n.r(t);var i=n(0),r=n(1),o=n.n(r);function s(e,t){e.prototype=Object.create(t.prototype),e.prototype.constructor=e,e.__proto__=t}var l=n(2),a=n.n(l),c=navigator.userAgent.match(/Macintosh/)?"Meta":"Control",u=function(e){function t(){return e.apply(this,arguments)||this}s(t,e);var n=t.prototype;return n.oncreate=function(t){e.prototype.oncreate.call(this,t),this.attrs.setShortcutHandler(this.shortcut.bind(this))},n.view=function(e){return m("div",{id:"MarkdownToolbar","data-for":this.attrs.for,style:{display:"inline-block"}},e.children)},n.shortcut=function(e){if(e.metaKey&&"Meta"===c||e.ctrlKey&&"Control"===c){var t=this.element.querySelector('[data-hotkey="'+e.key+'"]');t&&(t.click(),e.preventDefault())}},t}(a.a),d=n(3),f=n.n(d);function h(){return(h=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var n=arguments[t];for(var i in n)Object.prototype.hasOwnProperty.call(n,i)&&(e[i]=n[i])}return e}).apply(this,arguments)}var p=null;function y(e){return e.trim().split("\n").length>1}function v(e,t){return Array(t+1).join(e)}function x(e,t){for(var n=t;e[n]&&null!=e[n-1]&&!e[n-1].match(/\s/);)n--;return n}function g(e,t,n){for(var i=t,r=n?/\n/:/\s/;e[i]&&!e[i].match(r);)i++;return i}function k(e){var t,n,i=e.value.slice(0,e.selectionStart),r=e.value.slice(e.selectionEnd),o=i.match(/\n*$/),s=r.match(/^\n*/),l=o?o[0].length:0,a=s?s[0].length:0;return i.match(/\S/)&&l<2&&(t=v("\n",2-l)),r.match(/\S/)&&a<2&&(n=v("\n",2-a)),null==t&&(t=""),null==n&&(n=""),{newlinesToAppend:t,newlinesToPrepend:n}}var b=function(e,t){var n=t.prefix,i=t.suffix,r=t.blockPrefix,o=t.blockSuffix,s=t.replaceNext,l=t.prefixSpace,a=t.scanFor,c=t.surroundWithNewlines,u=e.selectionStart,d=e.selectionEnd,f=e.value.slice(e.selectionStart,e.selectionEnd),h=y(f)&&r.length>0?r+"\n":n,p=y(f)&&o.length>0?"\n"+o:i;if(l){var m=e.value[e.selectionStart-1];0===e.selectionStart||null==m||m.match(/\s/)||(h=" "+h)}f=function(e,t,n){var i=arguments.length>3&&void 0!==arguments[3]&&arguments[3];if(e.selectionStart===e.selectionEnd)e.selectionStart=x(e.value,e.selectionStart),e.selectionEnd=g(e.value,e.selectionEnd,i);else{var r=e.selectionStart-t.length,o=e.selectionEnd+n.length,s=e.value.slice(r,e.selectionStart)===t,l=e.value.slice(e.selectionEnd,o)===n;s&&l&&(e.selectionStart=r,e.selectionEnd=o)}return e.value.slice(e.selectionStart,e.selectionEnd)}(e,h,p,t.multiline);var v=e.selectionStart,b=e.selectionEnd,E=s.length>0&&p.indexOf(s)>-1&&f.length>0;if(c){var S=k(e);h=S.newlinesToAppend+n,p+=S.newlinesToPrepend}if(f.startsWith(h)&&f.endsWith(p)){var w=f.slice(h.length,f.length-p.length);if(u===d){var O=u-h.length;O=Math.max(O,v),v=b=O=Math.min(O,v+w.length)}else b=v+w.length;return{text:w,selectionStart:v,selectionEnd:b}}if(E)return a.length>0&&f.match(a)?{text:h+(p=p.replace(s,f)),selectionStart:v=b=v+h.length,selectionEnd:b}:{text:h+f+p,selectionStart:v=v+h.length+f.length+p.indexOf(s),selectionEnd:b=v+s.length};var K=h+f+p;v=u+h.length,b=d+h.length;var j=f.match(/^\s*|\s*$/g);if(t.trimFirst&&j){var L=j[0]||"",T=j[1]||"";K=L+h+f.trim()+p+T,v+=L.length,b-=T.length}return{text:K,selectionStart:v,selectionEnd:b}},E=function(e,t){var n,i=e.value.slice(e.selectionStart,e.selectionEnd);n=t.orderedList?function(e){var t,n,i,r,o=/^\d+\.\s+/,s=e.selectionStart===e.selectionEnd,l=e.value.slice(e.selectionStart,e.selectionEnd),a=l,c=l.split("\n");if(s){var u=e.value.slice(0,e.selectionStart).split(/\n/);i=e.selectionStart-u[u.length-1].length,r=g(e.value,e.selectionStart,!0),a=e.value.slice(i,r)}var d=a.split("\n");if(d.every((function(e){return o.test(e)}))){if(l=(c=d.map((function(e){return e.replace(o,"")}))).join("\n"),s&&i&&r){var f=d[0].length-c[0].length;n=t=e.selectionStart-f,e.selectionStart=i,e.selectionEnd=r}}else{l=(c=function(){var e,t,n,i=[];for(n=e=0,t=c.length;e<t;n=++e){var r=c[n];i.push(n+1+". "+r)}return i}()).join("\n");var h=k(e),p=h.newlinesToAppend,y=h.newlinesToPrepend;t=(n=e.selectionStart+p.length)+l.length,s&&(n=t),l=p+l+y}return{text:l,selectionStart:n,selectionEnd:t}}(e):t.multiline&&y(i)?function(e,t){var n=t.prefix,i=t.suffix,r=t.surroundWithNewlines,o=e.value.slice(e.selectionStart,e.selectionEnd),s=e.selectionStart,l=e.selectionEnd,a=o.split("\n");if(a.every((function(e){return e.startsWith(n)&&e.endsWith(i)})))l=s+(o=a.map((function(e){return e.slice(n.length,e.length-i.length)})).join("\n")).length;else if(o=a.map((function(e){return n+e+i})).join("\n"),r){var c=k(e),u=c.newlinesToAppend,d=c.newlinesToPrepend;l=(s+=u.length)+o.length,o=u+o+d}return{text:o,selectionStart:s,selectionEnd:l}}(e,t):b(e,t),function(e,t){var n=t.text,i=t.selectionStart,r=t.selectionEnd,o=e.selectionStart,s=e.value.slice(0,o),l=e.value.slice(e.selectionEnd);if(null===p||!0===p){e.contentEditable="true";try{p=document.execCommand("insertText",!1,n)}catch(e){p=!1}e.contentEditable="false"}if(p&&!e.value.slice(0,e.selectionStart).endsWith(n)&&(p=!1),!p){try{document.execCommand("ms-beginUndoUnit")}catch(e){}e.value=s+n+l;try{document.execCommand("ms-endUndoUnit")}catch(e){}var a=document.createEvent("Event");a.initEvent("input",!0,!0),e.dispatchEvent(a)}null!=i&&null!=r?e.setSelectionRange(i,r):e.setSelectionRange(o,e.selectionEnd)}(e,n)},S=navigator.userAgent.match(/Macintosh/)?"⌘":"ctrl",w=function(e){function t(){return e.apply(this,arguments)||this}s(t,e);var n=t.prototype;return n.oncreate=function(t){e.prototype.oncreate.call(this,t),this.$().tooltip()},n.view=function(){return m("button",{className:"Button Button--icon Button--link",title:this.title(),"data-hotkey":this.attrs.hotkey,onclick:this.click.bind(this),onkeydown:this.keydown.bind(this)},f()(this.attrs.icon))},n.keydown=function(e){" "!==e.key&&"Enter"!==e.key||(e.preventDefault(),this.click())},n.click=function(){return e=this.element,t=this.attrs.style,n=e.parentElement,i=h({},{prefix:"",suffix:"",blockPrefix:"",blockSuffix:"",multiline:!1,replaceNext:"",prefixSpace:!1,scanFor:"",surroundWithNewlines:!1,orderedList:!1,trimFirst:!1},t),void((r=document.getElementById(n.dataset.for))&&(r.focus(),E(r,i)));var e,t,n,i,r},n.title=function(){var e=this.attrs.title;return this.attrs.hotkey&&(e+=" <"+S+"-"+this.attrs.hotkey+">"),e},t}(a.a),O=n(4),K=n.n(O),j=n(5),L=n.n(j),T=function(){function e(e,t,n){this.oninput=e,this.callInputListeners=t,this.onsubmit=n}return e.prototype.handleKey=function(e,t,n,i){var r=this;setTimeout((function(){if(r.oninput(i.target.value),(i.metaKey||i.ctrlKey)&&"Enter"===i.key)return r.onsubmit();r.callInputListeners(i)}))},e}(),C=function(e){function t(){return e.apply(this,arguments)||this}s(t,e);var n=t.prototype;return n.build=function(e,t){this.el.className=t.classNames.join(" "),this.el.disabled=t.disabled,this.el.placeholder=t.placeholder,this.el.value=t.value,this.el.id=t.textareaId,e.append(this.el),this.el.addEventListener("keydown",(function(e){return t.shortcutHandler.apply(t,arguments)}));var n=function(e){t.inputListeners.forEach((function(e){e()})),e.redraw=!1};this.el.addEventListener("click",n),this.mdarea=new K.a(this.el,{keyMap:{indent:["Ctrl+m"],outdent:["Ctrl+M"],inline:[]},extensions:[new T(t.oninput,n,t.onsubmit)]})},n.destroy=function(){this.mdarea.destroy(),e.prototype.destroy.call(this)},t}(L.a),I=function(){};app.initializers.add("flarum-markdown",(function(e){var t=1;Object(i.extend)(o.a.prototype,"oninit",(function(){this.textareaId="textarea"+t++})),Object(i.override)(o.a.prototype,"buildEditor",(function(e,t){return new C(t,this.buildEditorParams())})),Object(i.extend)(o.a.prototype,"buildEditorParams",(function(e){e.textareaId=this.textareaId,e.shortcutHandler=I})),Object(i.extend)(o.a.prototype,"toolbarItems",(function(t){var n=function(t){return e.translator.trans("flarum-markdown.forum.composer."+t+"_tooltip")};t.add("markdown",m(u,{for:this.textareaId,setShortcutHandler:function(e){return I=e}},m(w,{title:n("header"),icon:"fas fa-heading",style:{prefix:"### "}}),m(w,{title:n("bold"),icon:"fas fa-bold",style:{prefix:"**",suffix:"**",trimFirst:!0},hotkey:"b"}),m(w,{title:n("italic"),icon:"fas fa-italic",style:{prefix:"_",suffix:"_",trimFirst:!0},hotkey:"i"}),m(w,{title:n("quote"),icon:"fas fa-quote-left",style:{prefix:"> ",multiline:!0,surroundWithNewlines:!0}}),m(w,{title:n("code"),icon:"fas fa-code",style:{prefix:"`",suffix:"`",blockPrefix:"```",blockSuffix:"```"}}),m(w,{title:n("link"),icon:"fas fa-link",style:{prefix:"[",suffix:"](https://)",replaceNext:"https://",scanFor:"https?://"}}),m(w,{title:n("image"),icon:"fas fa-image",style:{prefix:"![",suffix:"](https://)",replaceNext:"https://",scanFor:"https?://"}}),m(w,{title:n("unordered_list"),icon:"fas fa-list-ul",style:{prefix:"- ",multiline:!0,surroundWithNewlines:!0}}),m(w,{title:n("ordered_list"),icon:"fas fa-list-ol",style:{prefix:"1. ",multiline:!0,orderedList:!0}})),100)}))}))}]);
//# sourceMappingURL=forum.js.map