5a740a4251
Update Trix to a compatible version https://github.com/basecamp/house/tree/morphing-2
44 lines
24 KiB
JavaScript
44 lines
24 KiB
JavaScript
function w(s){return typeof s=="string"}function T(s){return s instanceof RegExp}function l(s,t,e=null,n=!1){return s.dispatchEvent(new CustomEvent(t,{bubbles:!0,detail:e,cancelable:n}))}function k(s){return new Promise(t=>setTimeout(t,s))}var p=class extends HTMLElement{constructor(){super()}connectedCallback(){this.#e(),this.addEventListener("click",this.#s),this.#t?.addEventListener("change",this.#n)}disconnectedCallback(){this.#t?.removeEventListener("change",this.#n),this.removeEventListener("click",this.#s)}get#t(){return this.querySelector("[data-house-md-toolbar-file-picker]")}#e(){this.#i()&&(this.innerHTML=this.#c())}#s=t=>{let e=t.target.closest("[data-house-md-action]")?.dataset?.houseMdAction;e&&(t.preventDefault(),l(this,"house-md:toolbar-action",{houseMdAction:e}))};#n=t=>{for(let e of t.target.files)l(this,"house-md:toolbar-action",{houseMdAction:"uploadFile",file:e})};#i(){return this.children.length===0}#c(){return`
|
|
<button data-house-md-action="bold" title="Bold">
|
|
<svg viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path d="m4.1 23c-.5 0-.7-.4-.7-.7v-20.6c0-.4.4-.7.7-.7h8.9c2 0 3.8.6 4.9 1.5 1.2 1 1.8 2.4 1.8 4.1s-.9 3.2-2.3 4.1c-.2 0-.3.3-.3.5s0 .4.3.5c1.9.8 3.2 2.7 3.2 5s-.7 3.6-2.1 4.7-3.3 1.7-5.6 1.7h-8.8zm4.2-18.1v5.1h3c1.2 0 2-.3 2.7-.7.6-.5.9-1.1.9-1.9s-.3-1.4-.8-1.8-1.3-.6-2.3-.6-2.4 0-3.5 0zm0 8.5v5.8h3.7c1.3 0 2.2-.3 2.8-.7s.9-1.2.9-2.2-.4-1.7-1-2.1-1.7-.7-2.9-.7-2.4 0-3.5 0z" fill-rule="evenodd"/></svg>
|
|
</button>
|
|
<button data-house-md-action="italic" title="Italic">
|
|
<svg viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path d="m9.3 1h10.2v3.1h-3.5l-3.7 15.7h3.2v3.2h-11v-3.1h3.8l3.7-15.7h-2.8v-3.2z" fill-rule="evenodd"/></svg>
|
|
</button>
|
|
<button data-house-md-action="quote" title="Quote">
|
|
<svg viewBox="0 0 24 22" xmlns="http://www.w3.org/2000/svg"><path d="m1.1 5.2c.6-.7 1.4-1.3 2.4-1.4 2.6-.4 4.2.4 5.3 1.9 2 2.3 1.9 5.1.6 7.6-1.3 2.4-4 4.6-7.2 5.1-.4 0-.7-.1-1-.4-.1-.3-.1-.7.3-1.1l1.1-1.1c.3-.4.6-.7.7-1.1s.3-.9 0-1.3c0-.4-.6-.7-1-1-1.2-.8-2.3-2.2-2.3-4.1.1-1.4.4-2.4 1.1-3.1z"/><path d="m14.6 5.2c.6-.7 1.6-1.1 2.6-1.4 2.4-.4 4.2.4 5.3 1.9 2 2.3 1.9 5.1.6 7.6-1.3 2.4-4 4.6-7.2 5.1-.4 0-.7-.1-1-.4-.1-.3-.1-.7.3-1.1l1.1-1.1c.3-.4.6-.7.7-1.1s.3-.9 0-1.3c-.1-.4-.6-.7-1-1-1.3-.6-2.4-2-2.4-3.9s.4-2.6 1-3.3z"/></svg>
|
|
</button>
|
|
<button data-house-md-action="code" title="Code">
|
|
<svg viewBox="0 0 24 22" xmlns="http://www.w3.org/2000/svg"><path d="m.4 10.1c-.5.5-.5 1.4 0 1.9l5.3 5.3c.5.5 1.4.5 1.9 0s.5-1.4 0-1.9l-4.4-4.4 4.4-4.4c.5-.5.5-1.4 0-1.9s-1.3-.5-1.9 0c0 0-5.3 5.4-5.3 5.4zm17.9 7.2 5.3-5.3c.5-.5.5-1.4 0-1.9l-5.3-5.3c-.5-.5-1.4-.5-1.9 0s-.5 1.4 0 1.9l4.4 4.4-4.4 4.4c-.5.5-.5 1.4 0 1.9.5.4 1.4.4 1.9-.1z" fill-rule="evenodd"/></svg>
|
|
</button>
|
|
<button data-house-md-action="link" title="Link">
|
|
<svg viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path d="m9.4 14.5c-2.3-2.3-2.3-5.9 0-8.2l4.6-4.6c1.1-1 2.6-1.7 4.2-1.7s3 .5 4.1 1.7c2.3 2.3 2.3 5.9 0 8.2l-2.7 2.3c-.5.5-1.2.5-1.8 0-.5-.5-.5-1.2 0-1.7l2.7-2.3c1.4-1.3 1.4-3.4 0-4.7-.7-.7-1.5-.9-2.3-.9s-1.8.4-2.5.9l-4.7 4.5c-1.4 1.3-1.4 3.4 0 4.7.5.5.5 1.2 0 1.7-.1.3-.4.4-.8.4s-.5-.1-.8-.3z"/><path d="m1.7 22.3c-2.3-2.3-2.3-5.9 0-8.2l2.6-2.5c.5-.5 1.2-.5 1.8 0 .5.5.5 1.2 0 1.7l-2.6 2.5c-1.4 1.3-1.4 3.4 0 4.7.7.7 1.5.9 2.3.9s1.8-.4 2.3-.9l4.6-4.6c1.4-1.3 1.4-3.4 0-4.7-.5-.4-.5-1.2 0-1.7s1.2-.5 1.8 0c2.3 2.3 2.3 5.9 0 8.2l-4.6 4.6c-1 1-2.5 1.7-4.1 1.7s-3-.7-4.1-1.7z"/></svg>
|
|
</button>
|
|
<button data-house-md-action="bulletList" title="Bullet list">
|
|
<svg viewBox="0 0 24 22" xmlns="http://www.w3.org/2000/svg"><path d="m2.1 4.8c1.1 0 2.1-.9 2.1-2.1s-1-2-2.1-2-2.1.9-2.1 2.1.9 2 2.1 2zm4.1-2c0-.8.6-1.4 1.4-1.4h15.1c.7 0 1.3.6 1.3 1.4s-.6 1.4-1.4 1.4h-15.1c-.7 0-1.3-.7-1.3-1.4zm1.3 6.8c-.8 0-1.4.6-1.4 1.4s.6 1.4 1.4 1.4h15.1c.8 0 1.4-.6 1.4-1.4s-.6-1.4-1.4-1.4zm0 8.3c-.8 0-1.4.6-1.4 1.4s.6 1.4 1.4 1.4h15.1c.8 0 1.4-.6 1.4-1.4s-.6-1.4-1.4-1.4zm-3.4-6.9c0 1.1-.9 2.1-2.1 2.1s-2-1-2-2.1.9-2.1 2.1-2.1 2 1 2 2.1zm-2 10.3c1.1 0 2.1-.9 2.1-2.1s-.9-2.1-2.1-2.1-2.1 1-2.1 2.1.9 2.1 2.1 2.1z" fill-rule="evenodd"/></svg>
|
|
</button>
|
|
<button data-house-md-action="numberList" title="Numbered list">
|
|
<svg viewBox="0 0 24 22" xmlns="http://www.w3.org/2000/svg"><path d="m0 .3h2.7v5.3h-1.4v-4h-1.3zm6.7 2.7c0-.7.6-1.3 1.3-1.3h14.7c.7 0 1.3.6 1.3 1.3s-.6 1.3-1.3 1.3h-14.7c-.7 0-1.3-.6-1.3-1.3zm1.3 6.7c-.7 0-1.3.6-1.3 1.3s.6 1.3 1.3 1.3h14.7c.7 0 1.3-.6 1.3-1.3s-.6-1.3-1.3-1.3zm0 8c-.7 0-1.3.6-1.3 1.3s.6 1.3 1.3 1.3h14.7c.7 0 1.3-.6 1.3-1.3s-.6-1.3-1.3-1.3zm-4.7-9.4h.7v1.3l-2 2.7h2v1.3h-4v-1.3l2.2-2.7h-2.2v-1.3zm-3.3 9.4v-1.3h4v5.3h-4v-1.3h2.7v-.7h-1.4v-1.3h1.3v-.7z" fill-rule="evenodd"/></svg>
|
|
</button>
|
|
<label title="Add Image">
|
|
<svg viewBox="0 0 24 20" xmlns="http://www.w3.org/2000/svg"><path d="m22 20h-20c-1.1 0-2-.9-2-2.1v-15.8c0-1.2.9-2.1 2-2.1h20c1.1 0 2 .9 2 2.1v15.8c0 1.1-.9 2.1-2 2.1zm0-2.9v-14.5c0-.3-.2-.5-.5-.5h-19c-.3 0-.5.2-.5.5v14.5c0 .1.1.2.2.2s.2 0 .2-.1l2.2-3.3c.1-.2.3-.3.5-.3h.7l2.6-4c.1-.2.3-.3.5-.3h.7c.2 0 .4.1.5.3l5.3 8c0 .1.2.2.3.2h.3c.2 0 .4-.2.4-.4s0-.2 0-.2l-1.3-1.9c-.2-.2-.2-.6 0-.8l1.2-1.6c.1-.2.3-.3.5-.3h1.1c.2 0 .4 0 .5.3l3.2 4.4c0 .1.3.2.4 0 .2 0 .2 0 .2-.2zm-5.5-7.6c-1.4 0-2.5-1.2-2.5-2.6s1.1-2.6 2.5-2.6 2.5 1.2 2.5 2.6-1.1 2.6-2.5 2.6z" fill-rule="evenodd"/></svg>
|
|
<input type="file" data-house-md-toolbar-file-picker multiple style="display: none;">
|
|
</label>`}};customElements.define("house-md-toolbar",p);var a=class s{start=0;end=0;static fromDOMRange(t,e){let{startOffset:n,startContainer:i,endOffset:r,endContainer:o}=t,m=E(e,i,n);if(t.collapsed)return new s(e,m,m);let C=E(e,o,r);return new s(e,m,C)}constructor(t,e,n){this.container=t,this.start=e,this.end=n}toDOMRange(){let t=document.createRange();t.setStart(this.container,1),t.setEnd(this.container,this.container.childNodes.length);let e=document.createTreeWalker(this.container,NodeFilter.SHOW_TEXT),n=0,i=!1,r=!1;for(;e.nextNode()&&!(i&&r);){let o=e.currentNode.textContent.length;n+o>=this.start&&!i&&(i=!0,t.setStart(e.currentNode,this.start-n)),n+o>=this.end&&!r&&(r=!0,t.setEnd(e.currentNode,this.end-n)),n+=o}return t}isEqual(t){return this.container===t.container&&this.start===t.start&&this.end===t.end}};function E(s,t,e){return t.nodeType===Node.ELEMENT_NODE?$(s,t,e):y(s,t,e)}function $(s,t,e){let n=t.childNodes[e],i=0,r=document.createTreeWalker(s);for(;r.nextNode();){if(r.currentNode===n)return i;r.currentNode.nodeType===Node.TEXT_NODE&&(i+=r.currentNode.textContent.length)}return i}function y(s,t,e){let n=document.createTreeWalker(s,NodeFilter.SHOW_TEXT);for(;n.nextNode();){if(n.currentNode===t)return e;e+=n.currentNode.textContent.length}return e}var d=class{#t;#e;#s;constructor(t){this.#t=t,this.#e=new a(this.contentElement,0,0),this.#s=""}current(){return{start:this.start,end:this.end,selection:this.#s}}currentLine(){return this.lineAt(this.end)}previousLine(){let{content:t}=this.#t,e=Math.max(t.lastIndexOf(`
|
|
`,this.start-1),0);return this.lineAt(e)}lineAt(t){let{content:e}=this.#t,n=Math.max(e.lastIndexOf(`
|
|
`,t-1)+1,0),i=e.indexOf(`
|
|
`,t),r=i===-1?e.length:i+1,o=e.slice(n,r);return{start:n,end:r,content:o}}select({start:t,end:e}){this.documentRange=new a(this.contentElement,t,e);let n=this.documentRange.toDOMRange();if(n){let i=window.getSelection();i.removeAllRanges(),i.addRange(n),this.update()}else console.error("Failed to create range",{start:t,end:e})}update=()=>{let t=document.getSelection()?.getRangeAt(0),e=this.#e;t.intersectsNode(this.contentElement)&&(this.#e=a.fromDOMRange(t,this.contentElement),this.#s=this.#t.content.slice(this.start,this.end)),this.#e.isEqual(e)||l(this.element,"house-md:selectionchange",{start:this.start,end:this.end})};get start(){return this.#e.start}get end(){return this.#e.end}get element(){return this.#t.element}get contentElement(){return this.#t.contentElement}get isEmpty(){return this.start===this.end}get isMultiline(){return this.current().selection.includes(`
|
|
`)}get isBold(){return this.#n("strong")}get isItalic(){return this.#n("em")}get isStrikethrough(){return this.#n("s")}get isLink(){return this.#n(".link")}get isCode(){return this.#n(".code")}get isQuote(){return this.#n(".quote")}get isBulletList(){return this.#n(".ul-li")}get isNumberList(){return this.#n(".ol-li")}#n(t){let i=window.getSelection().getRangeAt(0).endContainer;if(i.nodeType===Node.TEXT_NODE&&(i=i.parentElement),this.contentElement.contains(i))return!!i.closest(t)}};var c=class{static saveInterval=1e3;constructor(t=500){this.maxSize=t,this.undoStack=[],this.redoStack=[]}undo(t){let e=this.undoStack.pop();return e?(this.redoStack.push(t),e):{}}redo(t){let e=this.redoStack.pop();return e?(this.undoStack.push(t),e):{}}add=H(t=>{this.undoStack.push(t),this.redoStack=[],this.undoStack.length>this.maxSize&&this.undoStack.shift()})};function H(s){let t;return(...e)=>{t||(s(...e),t=setTimeout(()=>{t=null},c.saveInterval))}}function A(s){let t=s;return t=_(t),t=N(t),t=B(t),t=D(t),t=R(t),t=F(t),t=z(t),t=I(t),t}function _(s){return s=s.replaceAll(/&/g,"&"),s=s.replaceAll(/</g,"<"),s=s.replaceAll(/>/g,">"),s=s.replaceAll(/"/g,"""),s=s.replaceAll(/'/g,"'"),s}function N(s){return s=M(s),s=O(s),s=W(s),s=q(s),s=P(s),s}function B(s){return s=U(s),s=K(s),s=Q(s),s=X(s),s}function D(s){return V(s)}function R(s){return s.replaceAll(/(?<!!)\[(.*?)\]\((.*?)\)/gm,'<span class="link">[$1]($2)</span>')}function F(s){return s.replaceAll(/!\[(.*?)\]\((.*?)\)/gm,'<span class="img">$&</span>')}function z(s){return s=s.replaceAll(/^```(.*?)```$/gms,'<span class="code">```$1```</span>'),s=s.replaceAll(/([^`])`([^`]+)`([^`])/gm,'$1<span class="code">`$2`</span>$3'),s}function I(s){return s.replaceAll(/<!--(.*?)-->/gm,'<span class="comment"><!--$1--></span>')}function M(s){return s=s.replaceAll(/(?<!\*)\*\*\*([^*]+)\*\*\*(?!\*)/gm,"<strong><em>***$1***</em></strong>"),s=s.replaceAll(/(?<!_)___([^_]+)___(?!_)/gm,"<strong><em>___$1___</em></strong>"),s}function O(s){return s=s.replaceAll(/(?<!\*)\*\*([^*]+)\*\*(?!\*)/gm,"<strong>**$1**</strong>"),s=s.replaceAll(/(?<!_)__([^_]+)__(?!_)/gm,"<strong>__$1__</strong>"),s}function W(s){return s=s.replaceAll(/(?<!\*)\*([^*]+)\*(?!\*)/gm,"<em>*$1*</em>"),s=s.replaceAll(/(?<!_)_([^_]+)_(?!_)/gm,"<em>_$1_</em>"),s}function q(s){return s.replaceAll(/~~(.*)?~~/gm,"<s>~~$1~~</s>")}function P(s){return s.replaceAll(/==(.*?)==/gm,"<mark>==$1==</mark>")}function U(s){return s=s.replaceAll(/^# (.*)$/gm,'<span class="h1"># $1</span>'),s=s.replaceAll(/^## (.*)$/gm,'<span class="h2">## $1</span>'),s=s.replaceAll(/^### (.*)$/gm,'<span class="h3">### $1</span>'),s=s.replaceAll(/^#### (.*)$/gm,'<span class="h4">#### $1</span>'),s=s.replaceAll(/^##### (.*)$/gm,'<span class="h5">##### $1</span>'),s=s.replaceAll(/^###### (.*)$/gm,'<span class="h6">###### $1</span>'),s}function K(s){return s.replaceAll(/^(\d+\.\s.*)$/gm,'<span class="ol-li">$1</span>')}function Q(s){return s.replaceAll(/^(-\s.*)$/gm,'<span class="ul-li">$1</span>')}function X(s){return s.replaceAll(/^>(.*)$/gm,'<span class="quote">>$1</span>')}function V(s){return s.replaceAll(/\n---\n/gm,`
|
|
<span class="hr">---</span>
|
|
`)}var j="<wbr/>",u=class{constructor(t,e){this.content=t,this.element=e,this.selection=new d(this),this.history=new c}select(t,e){this.selection.select({start:t,end:e})}insertText(t){this.replaceText(t,this.currentSelection.start,this.currentSelection.end)}replaceText(t,e,n){let i=this.content.slice(0,e),r=this.content.slice(n),o=`${i}${t}${r}`,m={start:i.length+t.length,end:i.length+t.length};this.#d(),this.#l(o,m)}insertParagraph(t,e){this.select(t,e),this.selection.isBulletList?this.#c():this.selection.isNumberList?this.#h():this.replaceText(`
|
|
`,t,e)}insertLink(t,e,n=""){let{start:i,end:r}=this.currentSelection,o=` ${n}[${t}](${e}) `;this.replaceText(o,i,r),this.selection.select({start:i+o.length,end:i+o.length})}insertFile(t,e,n){n.startsWith("image/")?this.insertImage(t,e):this.insertLink(t,e)}insertImage(t,e){this.insertLink(t,e,"!")}deleteText(t,e){this.replaceText("",Math.max(t,0),e)}deleteCurrentLine(){let{start:t,end:e}=this.currentLine;this.deleteText(t,e)}toggleBold(){this.#t(this.selection.isBold,"**")}toggleItalic(){this.#t(this.selection.isItalic,"_")}toggleStrikethrough(){this.#t(this.selection.isStrikethrough,"~~")}toggleCode(){let[t,e]=this.selection.isMultiline?["```\n","\n```"]:["`","`"];this.#t(this.selection.isCode,t,e)}toggleLink(){this.selection.isLink?this.#i():this.#n()}toggleQuote(){this.selection.isQuote?this.#o("> "):this.#r("> ")}toggleBulletList(){this.selection.isBulletList?this.#o("- "):this.#r("- ")}toggleNumberList(){this.selection.isNumberList?this.#o(/\d+\. /):this.#r(`${this.#u+1}. `)}undo(){let{content:t,start:e,end:n}=this.history.undo(this.#a);w(t)&&this.#l(t,{start:e,end:n})}redo(){let{content:t,start:e,end:n}=this.history.redo(this.#a);w(t)&&this.#l(t,{start:e,end:n})}render(){this.contentElement.innerHTML=A(this.content)+j}get isEmpty(){return!this.content.trim()}get contentElement(){return this.element.contentWrapper}get currentSelection(){return this.selection.current()}get currentLine(){return this.selection.currentLine()}#t(t,e,n=e){t?this.#s(e,n):this.#e(e,n)}#e(t,e=t){let{start:n,end:i,selection:r}=this.currentSelection,o=`${t}${r}${e}`;this.replaceText(o,n,i),this.selection.select({start:n,end:n+r.length+t.length+e.length})}#s(t,e=t){let{start:n,end:i,selection:r}=this.currentSelection;r.startsWith(t)&&r.endsWith(e)&&(this.replaceText("",i-e.length,i),this.replaceText("",n,n+t.length),this.selection.select({start:n,end:i-t.length-e.length}))}#n(){let{start:t,end:e,selection:n}=this.currentSelection,i=this.selection.isEmpty,r=i?"title":n;this.replaceText(`[${r}](url)`,t,e),i?this.selection.select({start:t+1,end:t+r.length+1}):this.selection.select({start:t+n.length+3,end:t+n.length+6})}#i(){let{start:t,end:e,selection:n}=this.currentSelection,i=n.replace(/\[(.*)\]\(.*\)/,"$1");this.replaceText(i,t,e)}#c(){this.#m.match(/^-\s+$/)?this.deleteCurrentLine():(this.replaceText(`
|
|
`,this.currentSelection.end,this.currentSelection.end),this.replaceText("- ",this.currentSelection.end+1,this.currentSelection.end+1))}#h(){this.#m.match(/^\d+\.\s+$/)?this.deleteCurrentLine():(this.replaceText(`
|
|
`,this.currentSelection.end,this.currentSelection.end),this.replaceText(`${this.#u+1}. `,this.currentSelection.end+1,this.currentSelection.end+1))}#r(t){let{start:e,end:n}=this.currentSelection,i=this.currentLine;this.replaceText(t,i.start,i.start),this.selection.select({start:e+t.length,end:n+t.length})}#o(t){let{start:e,end:n}=this.currentSelection,i=this.currentLine;if(T(t)){let r=i.content.match(t);if(r)t=r[0];else return}i.content.startsWith(t)&&(this.deleteText(i.start,i.start+t.length),this.selection.select({start:e-t.length,end:n-t.length}))}#d(){this.history.add(this.#a)}#l(t,e){let n=this.content;this.content=t,this.element.value=t,this.selection.select(e),l(this.element,"house-md:change",{previousContent:n,newContent:t})}get#a(){let{start:t,end:e}=this.currentSelection;return{content:this.content,start:t,end:e}}get#m(){return this.currentLine.content}get#u(){let e=this.selection.previousLine().content.match(/^(\d+). /);return e?parseInt(e[1]):0}};var g=class{#t;constructor(t){this.#t=t}asPlainText(){let e=new window.DOMParser().parseFromString(this.#t,"text/html");return this.#e(e.body).trim()}#e(t){return t.nodeType===Node.TEXT_NODE?t.nodeValue:Array.from(t.childNodes).map((e,n)=>{let i=this.#s(e),r=n>0&&this.#s(t.childNodes[n-1]);return(i&&r?`
|
|
|
|
`:"")+this.#e(e)}).join("")}#s(t){return["BR","DIV","H1","H2","H3","H4","H5","H6","LI","P","TR"].includes(t.tagName)}};var f=class{constructor(t){this.document=t}handleInput(t,e){t.preventDefault();let n=this[`${t.inputType}Handler`];if(n){let i=t.getTargetRanges()[0],{start:r,end:o}=a.fromDOMRange(i,e);n.call(this,t,r,o)}else console.error("Not handling:",t.inputType)}insertTextHandler(t,e,n){this.#t(t,e,n)}insertCompositionTextHandler(t,e,n){this.#t(t,e,n)}insertParagraphHandler(t,e,n){this.document.insertParagraph(e,n)}insertLineBreakHandler(t,e,n){this.document.insertParagraph(e,n)}insertLinkHandler(t,e,n){this.#t(t,e,n)}insertFromYankHandler(t,e,n){this.#t(t,e,n)}insertFromDropHandler(t,e,n){this.#t(t,e,n)}insertFromPasteAsQuotationHandler(t,e,n){let i=t.dataTransfer.getData("text/plain");this.document.replaceText(`> ${i}`,e,n)}insertTransposeHandler(t,e,n){this.#t(t,e,n)}insertReplacementTextHandler(t,e,n){this.#t(t,e,n)}insertFromPasteHandler(t,e,n){this.#t(t,e,n)}deleteContentBackwardHandler(t,e,n){this.document.deleteText(e,n)}deleteByCutHandler(t,e,n){this.document.deleteText(e,n)}deleteWordBackwardHandler(t,e,n){this.document.deleteText(e,n)}deleteWordForwardHandler(t,e,n){this.document.deleteText(e,n)}deleteSoftLineBackwardHandler(t,e,n){this.document.deleteText(e,n)}deleteSoftLineForwardHandler(t,e,n){this.document.deleteText(e,n)}deleteEntireSoftLineHandler(t,e,n){this.document.deleteText(e,n)}deleteHardLineBackwardHandler(t,e,n){this.document.deleteText(e,n)}deleteHardLineForwardHandler(t,e,n){this.document.deleteText(e,n)}deleteByDragHandler(t,e,n){this.document.deleteText(e,n)}deleteContentHandler(t,e,n){this.document.deleteText(e,n)}deleteContentForwardHandler(t,e,n){this.document.deleteText(e,n)}#t(t,e,n){let i;if(t.data)i=t.data;else{let r=t.dataTransfer.getData("text/html");r?i=new g(r).asPlainText():i=t.dataTransfer.getData("text/plain")}this.document.replaceText(i,e,n)}};var h=class{constructor(t,e){this.element=t.closest("house-md"),this.file=e}upload(){if(l(this.element,"house-md:before-upload",{file:this.file})){let t=document.createElement("house-md-upload");t.file=this.file,t.uploadsURL=this.#t,this.element.appendChild(t)}}get#t(){return this.element.dataset.uploadsUrl||document.head.querySelector("meta[name=house-uploads-url]")?.content||"/uploads"}};var v=class{constructor(t){this.document=t}handleAction(t,e){let n=this[t];n?n.call(this,e):console.error("Not handling:",t)}bold(){this.document.toggleBold()}italic(){this.document.toggleItalic()}strikethrough(){this.document.toggleStrikethrough()}quote(){this.document.toggleQuote()}code(){this.document.toggleCode()}link(){this.document.toggleLink()}bulletList(){this.document.toggleBulletList()}numberList(){this.document.toggleNumberList()}undo(){this.document.undo()}redo(){this.document.redo()}uploadFile({file:t}){new h(this.document.element,t).upload()}};var b=class{constructor(t){this.element=t.closest("house-md")}handleDragOver(t){t.preventDefault()}async handleDrop(t){t.preventDefault();for(let e of t.dataTransfer.items)e.kind==="file"&&new h(this.element,e.getAsFile()).upload()}};var Y={"Ctrl+B":"bold","Ctrl+I":"italic","Ctrl+S":"strikethrough","Ctrl+Z":"undo","Ctrl+Shift+Z":"redo"};function S(s){return Y[Z(s)]}function Z(s){let t=[];return(s.metaKey||s.ctrlKey)&&t.push("Ctrl"),s.altKey&&t.push("Alt"),s.shiftKey&&t.push("Shift"),t.push(s.key.toUpperCase()),t.join("+")}var L=class extends HTMLElement{static formAssociated=!0;static observedAttributes=["initialized"];#t;constructor(){super(),this.internals=this.attachInternals(),this.internals.ariaRole="textbox"}attributeChangedCallback(t,e,n){t==="initialized"&&this.isConnected&&e!=null&&e!==n&&requestAnimationFrame(()=>this.#n())}connectedCallback(){let t=this.querySelector(".house-md-content")?.textContent||this.textContent;this.document=new u(t,this),this.#e(),this.#i(),this.inputHandler=new f(this.document),this.actionHandler=new v(this.document),this.dropAndDropHandler=new b(this),this.autofocus&&this.focus(),this.setAttribute("initialized",""),this.value=t}disconnectedCallback(){this.#c()}validate=t=>{this.required&&this.document.isEmpty?(t.preventDefault(),this.internals.setValidity({valueMissing:!0},"This field is required.",this.element),this.focus()):this.internals.setValidity({})};render(){this.document.render()}focus(){this.contentWrapper.focus()}get required(){return this.hasAttribute("required")}set required(t){t?this.setAttribute("required",""):this.removeAttribute("required")}get autofocus(){return this.hasAttribute("autofocus")}set autofocus(t){t?this.setAttribute("autofocus",""):this.removeAttribute("autofocus")}get value(){return this.document.content}set value(t){this.document.content=t,this.internals.setFormValue(t),this.internals.setValidity({}),this.render()}get form(){return this.internals.form}#e(){this.textContent="",this.toolbar=this.#s(),this.contentWrapper=document.createElement("div"),this.contentWrapper.classList.add("house-md-content"),this.contentWrapper.setAttribute("contenteditable",!0),this.contentWrapper.setAttribute("role","textbox"),this.contentWrapper.setAttribute("aria-multiline",!0),this.append(this.contentWrapper),this.getAttribute("tabindex")?(this.contentWrapper.setAttribute("tabindex",this.getAttribute("tabindex")),this.removeAttribute("tabindex")):this.contentWrapper.setAttribute("tabindex",0)}#s(){if(this.getAttribute("toolbar"))return document.getElementById(this.getAttribute("toolbar"));{let t=document.createElement("house-md-toolbar");return this.prepend(t),t}}#n(){this.disconnectedCallback(),this.getAttribute("toolbar")||this.toolbar.remove(),this.contentWrapper.remove(),this.connectedCallback()}#i(){this.addEventListener("beforeinput",this.#h),this.addEventListener("keydown",this.#r),this.addEventListener("dragover",this.#o),this.addEventListener("drop",this.#d),this.toolbar.addEventListener("house-md:toolbar-action",this.#l),document.addEventListener("selectionchange",this.#a),this.internals.form.addEventListener("submit",this.validate)}#c(){this.removeEventListener("beforeinput",this.#h),this.removeEventListener("keydown",this.#r),this.removeEventListener("dragover",this.#o),this.removeEventListener("drop",this.#d),this.toolbar.removeEventListener("house-md:toolbar-action",this.#l),document.removeEventListener("selectionchange",this.#a),this.internals.form.removeEventListener("submit",this.validate)}#h=t=>{this.inputHandler.handleInput(t,this.contentWrapper)};#r(t){let e=S(t);e&&(t.preventDefault(),this.actionHandler.handleAction(e))}#o=t=>{this.dropAndDropHandler.handleDragOver(t)};#d=t=>{this.dropAndDropHandler.handleDrop(t)};#l=({detail:t})=>{this.actionHandler.handleAction(t.houseMdAction,t)};#a=()=>{this.document.selection.update()}};customElements.define("house-md",L);var x=class extends HTMLElement{#t;#e;#s;constructor(){super(),this.percentComplete=0,this.status="pending"}connectedCallback(){this.#i(),this.#n(),this.addEventListener("click",this.#u)}disconnectedCallback(){this.removeEventListener("click",this.#u)}async#n(){let t=new FormData;t.append("file",this.file),this.csrfParamName&&t.append(this.csrfParamName,this.csrfToken),this.xhr=new XMLHttpRequest,this.xhr.open("POST",this.uploadsURL,!0),this.xhr.upload.onprogress=this.#d,this.xhr.onload=this.#l,this.xhr.onerror=this.#a,this.xhr.onabort=this.#m,this.xhr.send(t),this.status="uploading",this.#i()}#i(){this.setAttribute("status",this.status),this.innerHTML=`
|
|
${this.#c()}
|
|
${this.#h()}
|
|
${this.#r()}
|
|
${this.#o()}
|
|
`}#c(){return this.status==="failed"?"<button data-md-action='close' class='md-close' aria-label='Close'></button>":""}#h(){return`<div class="md-file">${this.file.name}</div>`}#r(){return`<progress aria-label="Uploading file\u2026" class="md-progress-bar" max="100" value="${this.percentComplete}">${this.percentComplete}%</progress>`}#o(){return this.#s?`<div class="md-error">${this.#s}</div>`:""}#d=t=>{t.lengthComputable&&(this.percentComplete=t.loaded/t.total*100),this.#i()};#l=async()=>{if(this.xhr.status>=400)this.status="failed",this.#s=this.xhr.responseText||"Upload failed";else{let t=JSON.parse(this.xhr.responseText);await this.document.insertFile(t.fileName,t.fileUrl,t.mimetype),this.status="complete",this.#p()}this.#i()};#a=()=>{this.status="failed",this.#i()};#m=()=>{this.status="aborted",this.#i()};#u=t=>{t.target.matches("[data-md-action=close]")&&this.remove()};async#p(t=500){await k(t),this.remove()}get document(){return this.closest("house-md").document}get file(){return this.#t}set file(t){this.#t=t}get uploadsURL(){return this.#e}set uploadsURL(t){this.#e=t}get csrfParamName(){return document.head.querySelector("meta[name=csrf-param]")?.content}get csrfToken(){return document.head.querySelector("meta[name=csrf-token]")?.content}};customElements.define("house-md-upload",x);export{u as Document,L as Editor,c as History,d as Selection,p as Toolbar,x as Upload};
|
|
//# sourceMappingURL=house.min.js.map
|