Files
freedit/static/js/overtype.min.js
2025-08-19 18:17:07 +08:00

546 lines
59 KiB
JavaScript

/**
* OverType v1.1.4
* A lightweight markdown editor library with perfect WYSIWYG alignment
* @license MIT
* @author Demo User
* https://github.com/demo/overtype
*/
var OverType=(()=>{var N=Object.defineProperty;var Ie=Object.getOwnPropertyDescriptor;var je=Object.getOwnPropertyNames;var Oe=Object.prototype.hasOwnProperty;var Be=(n,e,t)=>e in n?N(n,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):n[e]=t;var Pe=(n,e)=>{for(var t in e)N(n,t,{get:e[t],enumerable:!0})},Ne=(n,e,t,o)=>{if(e&&typeof e=="object"||typeof e=="function")for(let i of je(e))!Oe.call(n,i)&&i!==t&&N(n,i,{get:()=>e[i],enumerable:!(o=Ie(e,i))||o.enumerable});return n};var ze=n=>Ne(N({},"__esModule",{value:!0}),n);var M=(n,e,t)=>(Be(n,typeof e!="symbol"?e+"":e,t),t);var Xe={};Pe(Xe,{OverType:()=>A,default:()=>Ge});var I=class{static resetLinkIndex(){this.linkIndex=0}static escapeHtml(e){let t={"&":"&amp;","<":"&lt;",">":"&gt;",'"':"&quot;","'":"&#39;"};return e.replace(/[&<>"']/g,o=>t[o])}static preserveIndentation(e,t){let i=t.match(/^(\s*)/)[1].replace(/ /g,"&nbsp;");return e.replace(/^\s*/,i)}static parseHeader(e){return e.replace(/^(#{1,3})\s(.+)$/,(t,o,i)=>{let r=o.length;return`<span class="header ${["h1","h2","h3"][r-1]}"><span class="syntax-marker">${o}</span> ${i}</span>`})}static parseHorizontalRule(e){return e.match(/^(-{3,}|\*{3,}|_{3,})$/)?`<div><span class="hr-marker">${e}</span></div>`:null}static parseBlockquote(e){return e.replace(/^&gt; (.+)$/,(t,o)=>`<span class="blockquote"><span class="syntax-marker">&gt;</span> ${o}</span>`)}static parseBulletList(e){return e.replace(/^((?:&nbsp;)*)([-*])\s(.+)$/,(t,o,i,r)=>`${o}<span class="syntax-marker">${i}</span> ${r}`)}static parseNumberedList(e){return e.replace(/^((?:&nbsp;)*)(\d+\.)\s(.+)$/,(t,o,i,r)=>`${o}<span class="syntax-marker">${i}</span> ${r}`)}static parseCodeBlock(e){return e.match(/^```(\s*|\w*)$/)?`<div><span class="code-fence">${e}</span></div>`:null}static parseBold(e){return e=e.replace(/\*\*(.+?)\*\*/g,'<strong><span class="syntax-marker">**</span>$1<span class="syntax-marker">**</span></strong>'),e=e.replace(/__(.+?)__/g,'<strong><span class="syntax-marker">__</span>$1<span class="syntax-marker">__</span></strong>'),e}static parseItalic(e){return e=e.replace(new RegExp("(?<!\\*)\\*(?!\\*)(.+?)(?<!\\*)\\*(?!\\*)","g"),'<em><span class="syntax-marker">*</span>$1<span class="syntax-marker">*</span></em>'),e=e.replace(new RegExp("(?<!_)_(?!_)(.+?)(?<!_)_(?!_)","g"),'<em><span class="syntax-marker">_</span>$1<span class="syntax-marker">_</span></em>'),e}static parseInlineCode(e){return e.replace(/`(.+?)`/g,'<code><span class="syntax-marker">`</span>$1<span class="syntax-marker">`</span></code>')}static parseLinks(e){return e.replace(/\[(.+?)\]\((.+?)\)/g,(t,o,i)=>{let r=`--link-${this.linkIndex++}`;return`<a href="${i}" style="anchor-name: ${r}"><span class="syntax-marker">[</span>${o}<span class="syntax-marker">](</span><span class="syntax-marker">${i}</span><span class="syntax-marker">)</span></a>`})}static parseInlineElements(e){let t=e;t=this.parseInlineCode(t);let o=new Map;return t=t.replace(/(<code>.*?<\/code>)/g,i=>{let r=`\uE000${o.size}\uE001`;return o.set(r,i),r}),t=this.parseLinks(t),t=this.parseBold(t),t=this.parseItalic(t),o.forEach((i,r)=>{t=t.replace(r,i)}),t}static parseLine(e){let t=this.escapeHtml(e);t=this.preserveIndentation(t,e);let o=this.parseHorizontalRule(t);if(o)return o;let i=this.parseCodeBlock(t);return i||(t=this.parseHeader(t),t=this.parseBlockquote(t),t=this.parseBulletList(t),t=this.parseNumberedList(t),t=this.parseInlineElements(t),t.trim()===""?"<div>&nbsp;</div>":`<div>${t}</div>`)}static parse(e,t=-1,o=!1){return this.resetLinkIndex(),e.split(`
`).map((l,s)=>o&&s===t?`<div class="raw-line">${this.escapeHtml(l)||"&nbsp;"}</div>`:this.parseLine(l)).join("")}};M(I,"linkIndex",0);var _e=Object.defineProperty,Y=Object.getOwnPropertySymbols,Fe=Object.prototype.hasOwnProperty,Ve=Object.prototype.propertyIsEnumerable,ee=(n,e,t)=>e in n?_e(n,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):n[e]=t,te=(n,e)=>{for(var t in e||(e={}))Fe.call(e,t)&&ee(n,t,e[t]);if(Y)for(var t of Y(e))Ve.call(e,t)&&ee(n,t,e[t]);return n},L={bold:{prefix:"**",suffix:"**",trimFirst:!0},italic:{prefix:"_",suffix:"_",trimFirst:!0},code:{prefix:"`",suffix:"`",blockPrefix:"```",blockSuffix:"```"},link:{prefix:"[",suffix:"](url)",replaceNext:"url",scanFor:"https?://"},bulletList:{prefix:"- ",multiline:!0,unorderedList:!0},numberedList:{prefix:"1. ",multiline:!0,orderedList:!0},quote:{prefix:"> ",multiline:!0,surroundWithNewlines:!0},taskList:{prefix:"- [ ] ",multiline:!0,surroundWithNewlines:!0},header1:{prefix:"# "},header2:{prefix:"## "},header3:{prefix:"### "},header4:{prefix:"#### "},header5:{prefix:"##### "},header6:{prefix:"###### "}};function Re(){return{prefix:"",suffix:"",blockPrefix:"",blockSuffix:"",multiline:!1,replaceNext:"",prefixSpace:!1,scanFor:"",surroundWithNewlines:!1,orderedList:!1,unorderedList:!1,trimFirst:!1}}function E(n){return te(te({},Re()),n)}var _=!1;function qe(){return _}function g(n,e,t){_&&(console.group(`\u{1F50D} ${n}`),console.log(e),t&&console.log("Data:",t),console.groupEnd())}function z(n,e){if(!_)return;let t=n.value.slice(n.selectionStart,n.selectionEnd);console.group(`\u{1F4CD} Selection: ${e}`),console.log("Position:",`${n.selectionStart}-${n.selectionEnd}`),console.log("Selected text:",JSON.stringify(t)),console.log("Length:",t.length);let o=n.value.slice(Math.max(0,n.selectionStart-10),n.selectionStart),i=n.value.slice(n.selectionEnd,Math.min(n.value.length,n.selectionEnd+10));console.log("Context:",JSON.stringify(o)+"[SELECTION]"+JSON.stringify(i)),console.groupEnd()}function re(n){_&&(console.group("\u{1F4DD} Result"),console.log("Text to insert:",JSON.stringify(n.text)),console.log("New selection:",`${n.selectionStart}-${n.selectionEnd}`),console.groupEnd())}var C=null;function H(n,{text:e,selectionStart:t,selectionEnd:o}){let i=qe();i&&(console.group("\u{1F527} insertText"),console.log("Current selection:",`${n.selectionStart}-${n.selectionEnd}`),console.log("Text to insert:",JSON.stringify(e)),console.log("New selection to set:",t,"-",o)),n.focus();let r=n.selectionStart,l=n.selectionEnd,s=n.value.slice(0,r),a=n.value.slice(l);i&&(console.log("Before text (last 20):",JSON.stringify(s.slice(-20))),console.log("After text (first 20):",JSON.stringify(a.slice(0,20))),console.log("Selected text being replaced:",JSON.stringify(n.value.slice(r,l))));let d=n.value,p=r!==l;if(C===null||C===!0){n.contentEditable="true";try{C=document.execCommand("insertText",!1,e),i&&console.log("execCommand returned:",C,"for text with",e.split(`
`).length,"lines")}catch(c){C=!1,i&&console.log("execCommand threw error:",c)}n.contentEditable="false"}if(i&&(console.log("canInsertText before:",C),console.log("execCommand result:",C)),C){let c=s+e+a,f=n.value;i&&(console.log("Expected length:",c.length),console.log("Actual length:",f.length)),f!==c&&i&&(console.log("execCommand changed the value but not as expected"),console.log("Expected:",JSON.stringify(c.slice(0,100))),console.log("Actual:",JSON.stringify(f.slice(0,100))))}if(!C)if(i&&console.log("Using manual insertion"),n.value===d){i&&console.log("Value unchanged, doing manual replacement");try{document.execCommand("ms-beginUndoUnit")}catch(c){}n.value=s+e+a;try{document.execCommand("ms-endUndoUnit")}catch(c){}n.dispatchEvent(new CustomEvent("input",{bubbles:!0,cancelable:!0}))}else i&&console.log("Value was changed by execCommand, skipping manual insertion");i&&console.log("Setting selection range:",t,o),t!=null&&o!=null?n.setSelectionRange(t,o):n.setSelectionRange(r,n.selectionEnd),i&&(console.log("Final value length:",n.value.length),console.groupEnd())}function ne(n){return n.trim().split(`
`).length>1}function De(n,e){let t=e;for(;n[t]&&n[t-1]!=null&&!n[t-1].match(/\s/);)t--;return t}function We(n,e,t){let o=e,i=t?/\n/:/\s/;for(;n[o]&&!n[o].match(i);)o++;return o}function se(n){let e=n.value.split(`
`),t=0;for(let o=0;o<e.length;o++){let i=e[o].length+1;n.selectionStart>=t&&n.selectionStart<t+i&&(n.selectionStart=t),n.selectionEnd>=t&&n.selectionEnd<t+i&&(o===e.length-1?n.selectionEnd=Math.min(t+e[o].length,n.value.length):n.selectionEnd=t+i-1),t+=i}}function Ke(n,e,t,o=!1){if(n.selectionStart===n.selectionEnd)n.selectionStart=De(n.value,n.selectionStart),n.selectionEnd=We(n.value,n.selectionEnd,o);else{let i=n.selectionStart-e.length,r=n.selectionEnd+t.length,l=n.value.slice(i,n.selectionStart)===e,s=n.value.slice(n.selectionEnd,r)===t;l&&s&&(n.selectionStart=i,n.selectionEnd=r)}return n.value.slice(n.selectionStart,n.selectionEnd)}function J(n){let e=n.value.slice(0,n.selectionStart),t=n.value.slice(n.selectionEnd),o=e.match(/\n*$/),i=t.match(/^\n*/),r=o?o[0].length:0,l=i?i[0].length:0,s="",a="";return e.match(/\S/)&&r<2&&(s=`
`.repeat(2-r)),t.match(/\S/)&&l<2&&(a=`
`.repeat(2-l)),{newlinesToAppend:s,newlinesToPrepend:a}}function F(n,e,t={}){let o=n.selectionStart,i=n.selectionEnd,r=o===i,l=n.value,s=o;for(;s>0&&l[s-1]!==`
`;)s--;if(r){let d=o;for(;d<l.length&&l[d]!==`
`;)d++;n.selectionStart=s,n.selectionEnd=d}else se(n);let a=e(n);if(t.adjustSelection){let p=n.value.slice(n.selectionStart,n.selectionEnd).startsWith(t.prefix),c=t.adjustSelection(p,o,i,s);a.selectionStart=c.start,a.selectionEnd=c.end}else if(t.prefix){let p=n.value.slice(n.selectionStart,n.selectionEnd).startsWith(t.prefix);r?p?(a.selectionStart=Math.max(o-t.prefix.length,s),a.selectionEnd=a.selectionStart):(a.selectionStart=o+t.prefix.length,a.selectionEnd=a.selectionStart):p?(a.selectionStart=Math.max(o-t.prefix.length,s),a.selectionEnd=Math.max(i-t.prefix.length,s)):(a.selectionStart=o+t.prefix.length,a.selectionEnd=i+t.prefix.length)}return a}function V(n,e){let t,o,{prefix:i,suffix:r,blockPrefix:l,blockSuffix:s,replaceNext:a,prefixSpace:d,scanFor:p,surroundWithNewlines:c,trimFirst:f}=e,u=n.selectionStart,v=n.selectionEnd,m=n.value.slice(n.selectionStart,n.selectionEnd),h=ne(m)&&l&&l.length>0?`${l}
`:i,b=ne(m)&&s&&s.length>0?`
${s}`:r;if(d){let x=n.value[n.selectionStart-1];n.selectionStart!==0&&x!=null&&!x.match(/\s/)&&(h=` ${h}`)}m=Ke(n,h,b,e.multiline);let y=n.selectionStart,w=n.selectionEnd,T=a&&a.length>0&&b.indexOf(a)>-1&&m.length>0;if(c){let x=J(n);t=x.newlinesToAppend,o=x.newlinesToPrepend,h=t+i,b+=o}if(m.startsWith(h)&&m.endsWith(b)){let x=m.slice(h.length,m.length-b.length);if(u===v){let S=u-h.length;S=Math.max(S,y),S=Math.min(S,y+x.length),y=w=S}else w=y+x.length;return{text:x,selectionStart:y,selectionEnd:w}}else if(T)if(p&&p.length>0&&m.match(p)){b=b.replace(a,m);let x=h+b;return y=w=y+h.length,{text:x,selectionStart:y,selectionEnd:w}}else{let x=h+m+b;return y=y+h.length+m.length+b.indexOf(a),w=y+a.length,{text:x,selectionStart:y,selectionEnd:w}}else{let x=h+m+b;y=u+h.length,w=v+h.length;let S=m.match(/^\s*|\s*$/g);if(f&&S){let G=S[0]||"",X=S[1]||"";x=G+h+m.trim()+b+X,y+=G.length,w-=X.length}return{text:x,selectionStart:y,selectionEnd:w}}}function le(n,e){let{prefix:t,suffix:o,surroundWithNewlines:i}=e,r=n.value.slice(n.selectionStart,n.selectionEnd),l=n.selectionStart,s=n.selectionEnd,a=r.split(`
`);if(a.every(p=>p.startsWith(t)&&(!o||p.endsWith(o))))r=a.map(p=>{let c=p.slice(t.length);return o&&(c=c.slice(0,c.length-o.length)),c}).join(`
`),s=l+r.length;else if(r=a.map(p=>t+p+(o||"")).join(`
`),i){let{newlinesToAppend:p,newlinesToPrepend:c}=J(n);l+=p.length,s=l+r.length,r=p+r+c}return{text:r,selectionStart:l,selectionEnd:s}}function oe(n){let e=n.split(`
`),t=/^\d+\.\s+/,o=e.every(r=>t.test(r)),i=e;return o&&(i=e.map(r=>r.replace(t,""))),{text:i.join(`
`),processed:o}}function ie(n){let e=n.split(`
`),t="- ",o=e.every(r=>r.startsWith(t)),i=e;return o&&(i=e.map(r=>r.slice(t.length))),{text:i.join(`
`),processed:o}}function O(n,e){return e?"- ":`${n+1}. `}function Ue(n,e){let t,o,i;return n.orderedList?(t=oe(e),o=ie(t.text),i=o.text):(t=ie(e),o=oe(t.text),i=o.text),[t,o,i]}function Ze(n,e){let t=n.selectionStart===n.selectionEnd,o=n.selectionStart,i=n.selectionEnd;se(n);let r=n.value.slice(n.selectionStart,n.selectionEnd),[l,s,a]=Ue(e,r),d=a.split(`
`).map((m,h)=>`${O(h,e.unorderedList)}${m}`),p=d.reduce((m,h,b)=>m+O(b,e.unorderedList).length,0),c=d.reduce((m,h,b)=>m+O(b,!e.unorderedList).length,0);if(l.processed)return t?(o=Math.max(o-O(0,e.unorderedList).length,0),i=o):(o=n.selectionStart,i=n.selectionEnd-p),{text:a,selectionStart:o,selectionEnd:i};let{newlinesToAppend:f,newlinesToPrepend:u}=J(n),v=f+d.join(`
`)+u;return t?(o=Math.max(o+O(0,e.unorderedList).length+f.length,0),i=o):s.processed?(o=Math.max(n.selectionStart+f.length,0),i=n.selectionEnd+f.length+p-c):(o=Math.max(n.selectionStart+f.length,0),i=n.selectionEnd+f.length+p),{text:v,selectionStart:o,selectionEnd:i}}function ae(n,e){let t=F(n,o=>Ze(o,e),{adjustSelection:(o,i,r,l)=>{let s=n.value.slice(l,n.selectionEnd),a=/^\d+\.\s+/,d=/^- /,p=a.test(s),c=d.test(s),f=e.orderedList&&p||e.unorderedList&&c;if(i===r)if(f){let u=s.match(e.orderedList?a:d),v=u?u[0].length:0;return{start:Math.max(i-v,l),end:Math.max(i-v,l)}}else if(p||c){let u=s.match(p?a:d),v=u?u[0].length:0,h=(e.unorderedList?2:3)-v;return{start:i+h,end:i+h}}else{let u=e.unorderedList?2:3;return{start:i+u,end:i+u}}else if(f){let u=s.match(e.orderedList?a:d),v=u?u[0].length:0;return{start:Math.max(i-v,l),end:Math.max(r-v,l)}}else if(p||c){let u=s.match(p?a:d),v=u?u[0].length:0,h=(e.unorderedList?2:3)-v;return{start:i+h,end:r+h}}else{let u=e.unorderedList?2:3;return{start:i+u,end:r+u}}}});H(n,t)}function Je(n){if(!n)return[];let e=[],{selectionStart:t,selectionEnd:o,value:i}=n,r=i.split(`
`),l=0,s="";for(let c of r){if(t>=l&&t<=l+c.length){s=c;break}l+=c.length+1}s.startsWith("- ")&&(s.startsWith("- [ ] ")||s.startsWith("- [x] ")?e.push("task-list"):e.push("bullet-list")),/^\d+\.\s/.test(s)&&e.push("numbered-list"),s.startsWith("> ")&&e.push("quote"),s.startsWith("# ")&&e.push("header"),s.startsWith("## ")&&e.push("header-2"),s.startsWith("### ")&&e.push("header-3");let a=Math.max(0,t-10),d=Math.min(i.length,o+10),p=i.slice(a,d);if(p.includes("**")){let c=i.slice(Math.max(0,t-100),t),f=i.slice(o,Math.min(i.length,o+100)),u=c.lastIndexOf("**"),v=f.indexOf("**");u!==-1&&v!==-1&&e.push("bold")}if(p.includes("_")){let c=i.slice(Math.max(0,t-100),t),f=i.slice(o,Math.min(i.length,o+100)),u=c.lastIndexOf("_"),v=f.indexOf("_");u!==-1&&v!==-1&&e.push("italic")}if(p.includes("`")){let c=i.slice(Math.max(0,t-100),t),f=i.slice(o,Math.min(i.length,o+100));c.includes("`")&&f.includes("`")&&e.push("code")}if(p.includes("[")&&p.includes("]")){let c=i.slice(Math.max(0,t-100),t),f=i.slice(o,Math.min(i.length,o+100)),u=c.lastIndexOf("["),v=f.indexOf("]");u!==-1&&v!==-1&&i.slice(o+v+1,o+v+10).startsWith("(")&&e.push("link")}return e}function R(n){if(!n||n.disabled||n.readOnly)return;g("toggleBold","Starting"),z(n,"Before");let e=E(L.bold),t=V(n,e);re(t),H(n,t),z(n,"After")}function q(n){if(!n||n.disabled||n.readOnly)return;let e=E(L.italic),t=V(n,e);H(n,t)}function ce(n){if(!n||n.disabled||n.readOnly)return;let e=E(L.code),t=V(n,e);H(n,t)}function D(n,e={}){if(!n||n.disabled||n.readOnly)return;let t=n.value.slice(n.selectionStart,n.selectionEnd),o=E(L.link);if(t&&t.match(/^https?:\/\//)&&!e.url?(o.suffix=`](${t})`,o.replaceNext=""):e.url&&(o.suffix=`](${e.url})`,o.replaceNext=""),e.text&&!t){let l=n.selectionStart;n.value=n.value.slice(0,l)+e.text+n.value.slice(l),n.selectionStart=l,n.selectionEnd=l+e.text.length}let r=V(n,o);H(n,r)}function W(n){if(!n||n.disabled||n.readOnly)return;let e=E(L.bulletList);ae(n,e)}function K(n){if(!n||n.disabled||n.readOnly)return;let e=E(L.numberedList);ae(n,e)}function pe(n){if(!n||n.disabled||n.readOnly)return;g("toggleQuote","Starting"),z(n,"Initial");let e=E(L.quote),t=F(n,o=>le(o,e),{prefix:e.prefix});re(t),H(n,t),z(n,"Final")}function de(n){if(!n||n.disabled||n.readOnly)return;let e=E(L.taskList),t=F(n,o=>le(o,e),{prefix:e.prefix});H(n,t)}function Q(n,e=1,t=!1){if(!n||n.disabled||n.readOnly)return;(e<1||e>6)&&(e=1),g("insertHeader","============ START ============"),g("insertHeader",`Level: ${e}, Toggle: ${t}`),g("insertHeader",`Initial cursor: ${n.selectionStart}-${n.selectionEnd}`);let o=`header${e===1?"1":e}`,i=E(L[o]||L.header1);g("insertHeader",`Style prefix: "${i.prefix}"`);let r=n.value,l=n.selectionStart,s=n.selectionEnd,a=l;for(;a>0&&r[a-1]!==`
`;)a--;let d=s;for(;d<r.length&&r[d]!==`
`;)d++;let p=r.slice(a,d);g("insertHeader",`Current line (before): "${p}"`);let c=p.match(/^(#{1,6})\s*/),f=c?c[1].length:0,u=c?c[0].length:0;g("insertHeader","Existing header check:"),g("insertHeader",` - Match: ${c?`"${c[0]}"`:"none"}`),g("insertHeader",` - Existing level: ${f}`),g("insertHeader",` - Existing prefix length: ${u}`),g("insertHeader",` - Target level: ${e}`);let v=t&&f===e;g("insertHeader",`Should toggle OFF: ${v} (toggle=${t}, existingLevel=${f}, level=${e})`);let m=F(n,h=>{let b=h.value.slice(h.selectionStart,h.selectionEnd);g("insertHeader",`Line in operation: "${b}"`);let y=b.replace(/^#{1,6}\s*/,"");g("insertHeader",`Cleaned line: "${y}"`);let w;return v?(g("insertHeader","ACTION: Toggling OFF - removing header"),w=y):f>0?(g("insertHeader",`ACTION: Replacing H${f} with H${e}`),w=i.prefix+y):(g("insertHeader","ACTION: Adding new header"),w=i.prefix+y),g("insertHeader",`New line: "${w}"`),{text:w,selectionStart:h.selectionStart,selectionEnd:h.selectionEnd}},{prefix:i.prefix,adjustSelection:(h,b,y,w)=>{if(g("insertHeader","Adjusting selection:"),g("insertHeader",` - isRemoving param: ${h}`),g("insertHeader",` - shouldToggleOff: ${v}`),g("insertHeader",` - selStart: ${b}, selEnd: ${y}`),g("insertHeader",` - lineStartPos: ${w}`),v){let T=Math.max(b-u,w);return g("insertHeader",` - Removing header, adjusting by -${u}`),{start:T,end:b===y?T:Math.max(y-u,w)}}else if(u>0){let T=i.prefix.length-u;return g("insertHeader",` - Replacing header, adjusting by ${T}`),{start:b+T,end:y+T}}else return g("insertHeader",` - Adding header, adjusting by +${i.prefix.length}`),{start:b+i.prefix.length,end:y+i.prefix.length}}});g("insertHeader",`Final result: text="${m.text}", cursor=${m.selectionStart}-${m.selectionEnd}`),g("insertHeader","============ END ============"),H(n,m)}function ue(n){Q(n,1,!0)}function he(n){Q(n,2,!0)}function fe(n){Q(n,3,!0)}function me(n){return Je(n)}var B=class{constructor(e){this.editor=e,this.textarea=e.textarea}handleKeydown(e){if(!(navigator.platform.toLowerCase().includes("mac")?e.metaKey:e.ctrlKey))return!1;let i=null;switch(e.key.toLowerCase()){case"b":e.shiftKey||(i="toggleBold");break;case"i":e.shiftKey||(i="toggleItalic");break;case"k":e.shiftKey||(i="insertLink");break;case"7":e.shiftKey&&(i="toggleNumberedList");break;case"8":e.shiftKey&&(i="toggleBulletList");break}return i?(e.preventDefault(),this.editor.toolbar?this.editor.toolbar.handleAction(i):this.handleAction(i),!0):!1}async handleAction(e){let t=this.textarea;if(t){t.focus();try{switch(e){case"toggleBold":R(t);break;case"toggleItalic":q(t);break;case"insertLink":D(t);break;case"toggleBulletList":W(t);break;case"toggleNumberedList":K(t);break}t.dispatchEvent(new Event("input",{bubbles:!0}))}catch(o){console.error("Error in markdown action:",o)}}}destroy(){}};var $={name:"solar",colors:{bgPrimary:"#faf0ca",bgSecondary:"#ffffff",text:"#0d3b66",h1:"#f95738",h2:"#ee964b",h3:"#3d8a51",strong:"#ee964b",em:"#f95738",link:"#0d3b66",code:"#0d3b66",codeBg:"rgba(244, 211, 94, 0.4)",blockquote:"#5a7a9b",hr:"#5a7a9b",syntaxMarker:"rgba(13, 59, 102, 0.52)",cursor:"#f95738",selection:"rgba(244, 211, 94, 0.4)",listMarker:"#ee964b",toolbarBg:"#ffffff",toolbarBorder:"rgba(13, 59, 102, 0.15)",toolbarIcon:"#0d3b66",toolbarHover:"#f5f5f5",toolbarActive:"#faf0ca"}},ve={name:"cave",colors:{bgPrimary:"#141E26",bgSecondary:"#1D2D3E",text:"#c5dde8",h1:"#d4a5ff",h2:"#f6ae2d",h3:"#9fcfec",strong:"#f6ae2d",em:"#9fcfec",link:"#9fcfec",code:"#c5dde8",codeBg:"#1a232b",blockquote:"#9fcfec",hr:"#c5dde8",syntaxMarker:"rgba(159, 207, 236, 0.73)",cursor:"#f26419",selection:"rgba(51, 101, 138, 0.4)",listMarker:"#f6ae2d",toolbarBg:"#1D2D3E",toolbarBorder:"rgba(197, 221, 232, 0.1)",toolbarIcon:"#c5dde8",toolbarHover:"#243546",toolbarActive:"#2a3f52"}},ye={solar:$,cave:ve,light:$,dark:ve};function j(n){return typeof n=="string"?{...ye[n]||ye.solar,name:n}:n}function P(n){let e=[];for(let[t,o]of Object.entries(n)){let i=t.replace(/([A-Z])/g,"-$1").toLowerCase();e.push(`--${i}: ${o};`)}return e.join(`
`)}function be(n,e={}){return{...n,colors:{...n.colors,...e}}}function ke(n={}){let{fontSize:e="14px",lineHeight:t=1.6,fontFamily:o='"SF Mono", SFMono-Regular, Menlo, Monaco, "Cascadia Code", Consolas, "Roboto Mono", "Noto Sans Mono", "Droid Sans Mono", "Ubuntu Mono", "DejaVu Sans Mono", "Liberation Mono", "Courier New", Courier, monospace',padding:i="20px",theme:r=null,mobile:l={}}=n,s=Object.keys(l).length>0?`
@media (max-width: 640px) {
.overtype-wrapper .overtype-input,
.overtype-wrapper .overtype-preview {
${Object.entries(l).map(([d,p])=>`${d.replace(/([A-Z])/g,"-$1").toLowerCase()}: ${p} !important;`).join(`
`)}
}
}
`:"",a=r&&r.colors?P(r.colors):"";return`
/* OverType Editor Styles */
.overtype-container {
display: grid !important;
grid-template-rows: auto 1fr auto !important;
width: 100% !important;
height: 100% !important;
${a?`
/* Theme Variables */
${a}`:""}
}
/* Auto-resize mode styles */
.overtype-container.overtype-auto-resize {
height: auto !important;
grid-template-rows: auto auto auto !important;
}
.overtype-container.overtype-auto-resize .overtype-wrapper {
height: auto !important;
min-height: 60px !important;
overflow: visible !important;
}
.overtype-wrapper {
position: relative !important;
width: 100% !important;
height: 100% !important; /* Take full height of grid cell */
min-height: 60px !important; /* Minimum usable height */
overflow: hidden !important;
background: var(--bg-secondary, #ffffff) !important;
grid-row: 2 !important; /* Always second row in grid */
}
/* Critical alignment styles - must be identical for both layers */
.overtype-wrapper .overtype-input,
.overtype-wrapper .overtype-preview {
/* Positioning - must be identical */
position: absolute !important;
top: 0 !important;
left: 0 !important;
width: 100% !important;
height: 100% !important;
/* Font properties - any difference breaks alignment */
font-family: ${o} !important;
font-synthesis: none !important; /* no faux bold/italic width drift */
font-variant-ligatures: none !important; /* keep metrics stable for code */
font-size: var(--instance-font-size, ${e}) !important;
line-height: var(--instance-line-height, ${t}) !important;
font-weight: normal !important;
font-style: normal !important;
font-variant: normal !important;
font-stretch: normal !important;
font-kerning: none !important;
font-feature-settings: normal !important;
/* Box model - must match exactly */
padding: var(--instance-padding, ${i}) !important;
margin: 0 !important;
border: none !important;
outline: none !important;
box-sizing: border-box !important;
/* Text layout - critical for character positioning */
white-space: pre-wrap !important;
word-wrap: break-word !important;
word-break: normal !important;
overflow-wrap: break-word !important;
tab-size: 2 !important;
-moz-tab-size: 2 !important;
text-align: left !important;
text-indent: 0 !important;
letter-spacing: normal !important;
word-spacing: normal !important;
/* Text rendering */
text-transform: none !important;
text-rendering: auto !important;
-webkit-font-smoothing: auto !important;
-webkit-text-size-adjust: 100% !important;
/* Direction and writing */
direction: ltr !important;
writing-mode: horizontal-tb !important;
unicode-bidi: normal !important;
text-orientation: mixed !important;
/* Visual effects that could shift perception */
text-shadow: none !important;
filter: none !important;
transform: none !important;
zoom: 1 !important;
/* Vertical alignment */
vertical-align: baseline !important;
/* Size constraints */
min-width: 0 !important;
min-height: 0 !important;
max-width: none !important;
max-height: none !important;
/* Overflow */
overflow-y: auto !important;
overflow-x: auto !important;
scrollbar-width: auto !important;
scrollbar-gutter: auto !important;
/* Animation/transition - disabled to prevent movement */
animation: none !important;
transition: none !important;
}
/* Input layer styles */
.overtype-wrapper .overtype-input {
/* Layer positioning */
z-index: 1 !important;
/* Text visibility */
color: transparent !important;
caret-color: var(--cursor, #f95738) !important;
background-color: transparent !important;
/* Textarea-specific */
resize: none !important;
appearance: none !important;
-webkit-appearance: none !important;
-moz-appearance: none !important;
/* Prevent mobile zoom on focus */
touch-action: manipulation !important;
/* Disable autofill and spellcheck */
autocomplete: off !important;
autocorrect: off !important;
autocapitalize: off !important;
spellcheck: false !important;
}
.overtype-wrapper .overtype-input::selection {
background-color: var(--selection, rgba(244, 211, 94, 0.4));
}
/* Preview layer styles */
.overtype-wrapper .overtype-preview {
/* Layer positioning */
z-index: 0 !important;
pointer-events: none !important;
color: var(--text, #0d3b66) !important;
background-color: transparent !important;
/* Prevent text selection */
user-select: none !important;
-webkit-user-select: none !important;
-moz-user-select: none !important;
-ms-user-select: none !important;
}
/* Defensive styles for preview child divs */
.overtype-wrapper .overtype-preview div {
/* Reset any inherited styles */
margin: 0 !important;
padding: 0 !important;
border: none !important;
text-align: left !important;
text-indent: 0 !important;
display: block !important;
position: static !important;
transform: none !important;
min-height: 0 !important;
max-height: none !important;
line-height: inherit !important;
font-size: inherit !important;
font-family: inherit !important;
}
/* Markdown element styling - NO SIZE CHANGES */
.overtype-wrapper .overtype-preview .header {
font-weight: bold !important;
}
/* Header colors */
.overtype-wrapper .overtype-preview .h1 {
color: var(--h1, #f95738) !important;
}
.overtype-wrapper .overtype-preview .h2 {
color: var(--h2, #ee964b) !important;
}
.overtype-wrapper .overtype-preview .h3 {
color: var(--h3, #3d8a51) !important;
}
/* Bold text */
.overtype-wrapper .overtype-preview strong {
color: var(--strong, #ee964b) !important;
font-weight: bold !important;
}
/* Italic text */
.overtype-wrapper .overtype-preview em {
color: var(--em, #f95738) !important;
text-decoration-color: var(--em, #f95738) !important;
text-decoration-thickness: 1px !important;
font-style: italic !important;
}
/* Inline code */
.overtype-wrapper .overtype-preview code {
background: var(--code-bg, rgba(244, 211, 94, 0.4)) !important;
color: var(--code, #0d3b66) !important;
padding: 0 !important;
border-radius: 2px !important;
font-family: inherit !important;
font-size: inherit !important;
line-height: inherit !important;
font-weight: normal !important;
}
/* Code blocks */
.overtype-wrapper .overtype-preview pre {
background: #1e1e1e !important;
padding: 0 !important;
margin: 0 !important;
border-radius: 4px !important;
overflow-x: auto !important;
}
.overtype-wrapper .overtype-preview pre code {
background: none !important;
}
/* Blockquotes */
.overtype-wrapper .overtype-preview .blockquote {
color: var(--blockquote, #5a7a9b) !important;
padding: 0 !important;
margin: 0 !important;
border: none !important;
}
/* Links */
.overtype-wrapper .overtype-preview a {
color: var(--link, #0d3b66) !important;
text-decoration: underline !important;
font-weight: normal !important;
}
.overtype-wrapper .overtype-preview a:hover {
text-decoration: underline !important;
color: var(--link, #0d3b66) !important;
}
/* Lists - no list styling */
.overtype-wrapper .overtype-preview ul,
.overtype-wrapper .overtype-preview ol {
list-style: none !important;
margin: 0 !important;
padding: 0 !important;
}
.overtype-wrapper .overtype-preview li {
margin: 0 !important;
padding: 0 !important;
list-style: none !important;
}
/* Horizontal rules */
.overtype-wrapper .overtype-preview hr {
border: none !important;
color: var(--hr, #5a7a9b) !important;
margin: 0 !important;
padding: 0 !important;
}
.overtype-wrapper .overtype-preview .hr-marker {
color: var(--hr, #5a7a9b) !important;
opacity: 0.6 !important;
}
/* Code fence markers - with background when not in code block */
.overtype-wrapper .overtype-preview .code-fence {
color: var(--code, #0d3b66) !important;
background: var(--code-bg, rgba(244, 211, 94, 0.4)) !important;
}
/* Code block lines - background for entire code block */
.overtype-wrapper .overtype-preview .code-block-line {
background: var(--code-bg, rgba(244, 211, 94, 0.4)) !important;
}
/* Remove background from code fence when inside code block line */
.overtype-wrapper .overtype-preview .code-block-line .code-fence {
background: transparent !important;
}
/* Raw markdown line */
.overtype-wrapper .overtype-preview .raw-line {
color: var(--raw-line, #5a7a9b) !important;
font-style: normal !important;
font-weight: normal !important;
}
/* Syntax markers */
.overtype-wrapper .overtype-preview .syntax-marker {
color: var(--syntax-marker, rgba(13, 59, 102, 0.52)) !important;
opacity: 0.7 !important;
}
/* List markers */
.overtype-wrapper .overtype-preview .list-marker {
color: var(--list-marker, #ee964b) !important;
}
/* Toolbar Styles */
.overtype-toolbar {
display: flex;
align-items: center;
gap: 4px;
padding: 8px;
background: var(--toolbar-bg, var(--bg-primary, #f8f9fa));
overflow-x: auto;
-webkit-overflow-scrolling: touch;
flex-shrink: 0;
height: auto !important;
grid-row: 1 !important; /* Always first row in grid */
}
.overtype-toolbar-button {
display: flex;
align-items: center;
justify-content: center;
width: 32px;
height: 32px;
padding: 0;
border: none;
border-radius: 6px;
background: transparent;
color: var(--toolbar-icon, var(--text-secondary, #666));
cursor: pointer;
transition: all 0.2s ease;
flex-shrink: 0;
}
.overtype-toolbar-button svg {
width: 20px;
height: 20px;
fill: currentColor;
}
.overtype-toolbar-button:hover {
background: var(--toolbar-hover, var(--bg-secondary, #e9ecef));
color: var(--toolbar-icon, var(--text-primary, #333));
}
.overtype-toolbar-button:active {
transform: scale(0.95);
}
.overtype-toolbar-button.active {
background: var(--toolbar-active, var(--primary, #007bff));
color: var(--toolbar-icon, var(--text-primary, #333));
}
.overtype-toolbar-button:disabled {
opacity: 0.5;
cursor: not-allowed;
}
.overtype-toolbar-separator {
width: 1px;
height: 24px;
background: var(--border, #e0e0e0);
margin: 0 4px;
flex-shrink: 0;
}
/* Adjust wrapper when toolbar is present */
.overtype-container .overtype-toolbar + .overtype-wrapper {
}
/* Mobile toolbar adjustments */
@media (max-width: 640px) {
.overtype-toolbar {
padding: 6px;
gap: 2px;
}
.overtype-toolbar-button {
width: 36px;
height: 36px;
}
.overtype-toolbar-separator {
margin: 0 2px;
}
}
${s}
`}var we=`<svg viewBox="0 0 18 18">
<path stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5,4H9.5A2.5,2.5,0,0,1,12,6.5v0A2.5,2.5,0,0,1,9.5,9H5A0,0,0,0,1,5,9V4A0,0,0,0,1,5,4Z"></path>
<path stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5,9h5.5A2.5,2.5,0,0,1,13,11.5v0A2.5,2.5,0,0,1,10.5,14H5a0,0,0,0,1,0,0V9A0,0,0,0,1,5,9Z"></path>
</svg>`,xe=`<svg viewBox="0 0 18 18">
<line stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" x1="7" x2="13" y1="4" y2="4"></line>
<line stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" x1="5" x2="11" y1="14" y2="14"></line>
<line stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" x1="8" x2="10" y1="14" y2="4"></line>
</svg>`,Le=`<svg viewBox="0 0 18 18">
<path fill="currentColor" d="M10,4V14a1,1,0,0,1-2,0V10H3v4a1,1,0,0,1-2,0V4A1,1,0,0,1,3,4V8H8V4a1,1,0,0,1,2,0Zm6.06787,9.209H14.98975V7.59863a.54085.54085,0,0,0-.605-.60547h-.62744a1.01119,1.01119,0,0,0-.748.29688L11.645,8.56641a.5435.5435,0,0,0-.022.8584l.28613.30762a.53861.53861,0,0,0,.84717.0332l.09912-.08789a1.2137,1.2137,0,0,0,.2417-.35254h.02246s-.01123.30859-.01123.60547V13.209H12.041a.54085.54085,0,0,0-.605.60547v.43945a.54085.54085,0,0,0,.605.60547h4.02686a.54085.54085,0,0,0,.605-.60547v-.43945A.54085.54085,0,0,0,16.06787,13.209Z"></path>
</svg>`,Se=`<svg viewBox="0 0 18 18">
<path fill="currentColor" d="M16.73975,13.81445v.43945a.54085.54085,0,0,1-.605.60547H11.855a.58392.58392,0,0,1-.64893-.60547V14.0127c0-2.90527,3.39941-3.42187,3.39941-4.55469a.77675.77675,0,0,0-.84717-.78125,1.17684,1.17684,0,0,0-.83594.38477c-.2749.26367-.561.374-.85791.13184l-.4292-.34082c-.30811-.24219-.38525-.51758-.1543-.81445a2.97155,2.97155,0,0,1,2.45361-1.17676,2.45393,2.45393,0,0,1,2.68408,2.40918c0,2.45312-3.1792,2.92676-3.27832,3.93848h2.79443A.54085.54085,0,0,1,16.73975,13.81445ZM9,3A.99974.99974,0,0,0,8,4V8H3V4A1,1,0,0,0,1,4V14a1,1,0,0,0,2,0V10H8v4a1,1,0,0,0,2,0V4A.99974.99974,0,0,0,9,3Z"></path>
</svg>`,Ce=`<svg viewBox="0 0 18 18">
<path fill="currentColor" d="M16.65186,12.30664a2.6742,2.6742,0,0,1-2.915,2.68457,3.96592,3.96592,0,0,1-2.25537-.6709.56007.56007,0,0,1-.13232-.83594L11.64648,13c.209-.34082.48389-.36328.82471-.1543a2.32654,2.32654,0,0,0,1.12256.33008c.71484,0,1.12207-.35156,1.12207-.78125,0-.61523-.61621-.86816-1.46338-.86816H13.2085a.65159.65159,0,0,1-.68213-.41895l-.05518-.10937a.67114.67114,0,0,1,.14307-.78125l.71533-.86914a8.55289,8.55289,0,0,1,.68213-.7373V8.58887a3.93913,3.93913,0,0,1-.748.05469H11.9873a.54085.54085,0,0,1-.605-.60547V7.59863a.54085.54085,0,0,1,.605-.60547h3.75146a.53773.53773,0,0,1,.60547.59375v.17676a1.03723,1.03723,0,0,1-.27539.748L14.74854,10.0293A2.31132,2.31132,0,0,1,16.65186,12.30664ZM9,3A.99974.99974,0,0,0,8,4V8H3V4A1,1,0,0,0,1,4V14a1,1,0,0,0,2,0V10H8v4a1,1,0,0,0,2,0V4A.99974.99974,0,0,0,9,3Z"></path>
</svg>`,Ee=`<svg viewBox="0 0 18 18">
<line stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" x1="7" x2="11" y1="7" y2="11"></line>
<path stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8.9,4.577a3.476,3.476,0,0,1,.36,4.679A3.476,3.476,0,0,1,4.577,8.9C3.185,7.5,2.035,6.4,4.217,4.217S7.5,3.185,8.9,4.577Z"></path>
<path stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M13.423,9.1a3.476,3.476,0,0,0-4.679-.36,3.476,3.476,0,0,0,.36,4.679c1.392,1.392,2.5,2.542,4.679.36S14.815,10.5,13.423,9.1Z"></path>
</svg>`,Ae=`<svg viewBox="0 0 18 18">
<polyline stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" points="5 7 3 9 5 11"></polyline>
<polyline stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" points="13 7 15 9 13 11"></polyline>
<line stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" x1="10" x2="8" y1="5" y2="13"></line>
</svg>`,Te=`<svg viewBox="0 0 18 18">
<line stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" x1="6" x2="15" y1="4" y2="4"></line>
<line stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" x1="6" x2="15" y1="9" y2="9"></line>
<line stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" x1="6" x2="15" y1="14" y2="14"></line>
<line stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" x1="3" x2="3" y1="4" y2="4"></line>
<line stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" x1="3" x2="3" y1="9" y2="9"></line>
<line stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" x1="3" x2="3" y1="14" y2="14"></line>
</svg>`,He=`<svg viewBox="0 0 18 18">
<line stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" x1="7" x2="15" y1="4" y2="4"></line>
<line stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" x1="7" x2="15" y1="9" y2="9"></line>
<line stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" x1="7" x2="15" y1="14" y2="14"></line>
<line stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round" stroke-width="1" x1="2.5" x2="4.5" y1="5.5" y2="5.5"></line>
<path fill="currentColor" d="M3.5,6A0.5,0.5,0,0,1,3,5.5V3.085l-0.276.138A0.5,0.5,0,0,1,2.053,3c-0.124-.247-0.023-0.324.224-0.447l1-.5A0.5,0.5,0,0,1,4,2.5v3A0.5,0.5,0,0,1,3.5,6Z"></path>
<path stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round" stroke-width="1" d="M4.5,10.5h-2c0-.234,1.85-1.076,1.85-2.234A0.959,0.959,0,0,0,2.5,8.156"></path>
<path stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round" stroke-width="1" d="M2.5,14.846a0.959,0.959,0,0,0,1.85-.109A0.7,0.7,0,0,0,3.75,14a0.688,0.688,0,0,0,.6-0.736,0.959,0.959,0,0,0-1.85-.109"></path>
</svg>`,$e=`<svg viewBox="2 2 20 20">
<path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M10 10.8182L9 10.8182C8.80222 10.8182 8.60888 10.7649 8.44443 10.665C8.27998 10.5651 8.15181 10.4231 8.07612 10.257C8.00043 10.0909 7.98063 9.90808 8.01922 9.73174C8.0578 9.55539 8.15304 9.39341 8.29289 9.26627C8.43275 9.13913 8.61093 9.05255 8.80491 9.01747C8.99889 8.98239 9.19996 9.00039 9.38268 9.0692C9.56541 9.13801 9.72159 9.25453 9.83147 9.40403C9.94135 9.55353 10 9.72929 10 9.90909L10 12.1818C10 12.664 9.78929 13.1265 9.41421 13.4675C9.03914 13.8084 8.53043 14 8 14"></path>
<path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M16 10.8182L15 10.8182C14.8022 10.8182 14.6089 10.7649 14.4444 10.665C14.28 10.5651 14.1518 10.4231 14.0761 10.257C14.0004 10.0909 13.9806 9.90808 14.0192 9.73174C14.0578 9.55539 14.153 9.39341 14.2929 9.26627C14.4327 9.13913 14.6109 9.05255 14.8049 9.01747C14.9989 8.98239 15.2 9.00039 15.3827 9.0692C15.5654 9.13801 15.7216 9.25453 15.8315 9.40403C15.9414 9.55353 16 9.72929 16 9.90909L16 12.1818C16 12.664 15.7893 13.1265 15.4142 13.4675C15.0391 13.8084 14.5304 14 14 14"></path>
</svg>`,Me=`<svg viewBox="0 0 18 18">
<line stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" x1="8" x2="16" y1="4" y2="4"></line>
<line stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" x1="8" x2="16" y1="9" y2="9"></line>
<line stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" x1="8" x2="16" y1="14" y2="14"></line>
<rect stroke="currentColor" fill="none" stroke-width="1.5" x="2" y="3" width="3" height="3" rx="0.5"></rect>
<rect stroke="currentColor" fill="none" stroke-width="1.5" x="2" y="13" width="3" height="3" rx="0.5"></rect>
<polyline stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5" points="2.65 9.5 3.5 10.5 5 8.5"></polyline>
</svg>`;var U=class{constructor(e){this.editor=e,this.container=null,this.buttons={}}create(){this.container=document.createElement("div"),this.container.className="overtype-toolbar",this.container.setAttribute("role","toolbar"),this.container.setAttribute("aria-label","Text formatting"),[{name:"bold",icon:we,title:"Bold (Ctrl+B)",action:"toggleBold"},{name:"italic",icon:xe,title:"Italic (Ctrl+I)",action:"toggleItalic"},{separator:!0},{name:"h1",icon:Le,title:"Heading 1",action:"insertH1"},{name:"h2",icon:Se,title:"Heading 2",action:"insertH2"},{name:"h3",icon:Ce,title:"Heading 3",action:"insertH3"},{separator:!0},{name:"link",icon:Ee,title:"Insert Link (Ctrl+K)",action:"insertLink"},{name:"code",icon:Ae,title:"Code (Ctrl+`)",action:"toggleCode"},{separator:!0},{name:"quote",icon:$e,title:"Quote",action:"toggleQuote"},{separator:!0},{name:"bulletList",icon:Te,title:"Bullet List",action:"toggleBulletList"},{name:"orderedList",icon:He,title:"Numbered List",action:"toggleNumberedList"},{name:"taskList",icon:Me,title:"Task List",action:"toggleTaskList"}].forEach(i=>{if(i.separator){let r=document.createElement("div");r.className="overtype-toolbar-separator",r.setAttribute("role","separator"),this.container.appendChild(r)}else{let r=this.createButton(i);this.buttons[i.name]=r,this.container.appendChild(r)}});let t=this.editor.element.querySelector(".overtype-container"),o=this.editor.element.querySelector(".overtype-wrapper");return t&&o&&t.insertBefore(this.container,o),this.container}createButton(e){let t=document.createElement("button");return t.className="overtype-toolbar-button",t.type="button",t.title=e.title,t.setAttribute("aria-label",e.title),t.setAttribute("data-action",e.action),t.innerHTML=e.icon,t.addEventListener("click",o=>{o.preventDefault(),this.handleAction(e.action)}),t}async handleAction(e){let t=this.editor.textarea;if(t){t.focus();try{switch(e){case"toggleBold":R(t);break;case"toggleItalic":q(t);break;case"insertH1":ue(t);break;case"insertH2":he(t);break;case"insertH3":fe(t);break;case"insertLink":D(t);break;case"toggleCode":ce(t);break;case"toggleBulletList":W(t);break;case"toggleNumberedList":K(t);break;case"toggleQuote":pe(t);break;case"toggleTaskList":de(t);break}t.dispatchEvent(new Event("input",{bubbles:!0}))}catch(o){console.error("Error loading markdown-actions:",o)}}}async updateButtonStates(){let e=this.editor.textarea;if(e)try{let t=me(e);Object.entries(this.buttons).forEach(([o,i])=>{let r=!1;switch(o){case"bold":r=t.includes("bold");break;case"italic":r=t.includes("italic");break;case"code":r=!1;break;case"bulletList":r=t.includes("bullet-list");break;case"orderedList":r=t.includes("numbered-list");break;case"quote":r=t.includes("quote");break;case"taskList":r=t.includes("task-list");break;case"h1":r=t.includes("header");break;case"h2":r=t.includes("header-2");break;case"h3":r=t.includes("header-3");break}i.classList.toggle("active",r),i.setAttribute("aria-pressed",r.toString())})}catch(t){}}destroy(){this.container&&(this.container.remove(),this.container=null,this.buttons={})}};var Z=class{constructor(e){this.editor=e,this.tooltip=null,this.currentLink=null,this.hideTimeout=null,this.init()}init(){CSS.supports("position-anchor: --x")&&CSS.supports("position-area: center")&&(this.createTooltip(),this.editor.textarea.addEventListener("selectionchange",()=>this.checkCursorPosition()),this.editor.textarea.addEventListener("keyup",t=>{(t.key.includes("Arrow")||t.key==="Home"||t.key==="End")&&this.checkCursorPosition()}),this.editor.textarea.addEventListener("input",()=>this.hide()),this.editor.textarea.addEventListener("scroll",()=>this.hide()),this.tooltip.addEventListener("mouseenter",()=>this.cancelHide()),this.tooltip.addEventListener("mouseleave",()=>this.scheduleHide()))}createTooltip(){this.tooltip=document.createElement("div"),this.tooltip.className="overtype-link-tooltip";let e=document.createElement("style");e.textContent=`
@supports (position-anchor: --x) and (position-area: center) {
.overtype-link-tooltip {
position: absolute;
position-anchor: var(--target-anchor, --link-0);
position-area: block-end center;
margin-top: 8px;
background: #333;
color: white;
padding: 6px 10px;
border-radius: 16px;
font-size: 12px;
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
display: none;
z-index: 10000;
cursor: pointer;
box-shadow: 0 2px 8px rgba(0,0,0,0.3);
max-width: 300px;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
position-try: most-width block-end inline-end, flip-inline, block-start center;
position-visibility: anchors-visible;
}
.overtype-link-tooltip.visible {
display: flex;
}
}
`,document.head.appendChild(e),this.tooltip.innerHTML=`
<span style="display: flex; align-items: center; gap: 6px;">
<svg width="12" height="12" viewBox="0 0 20 20" fill="currentColor" style="flex-shrink: 0;">
<path d="M11 3a1 1 0 100 2h2.586l-6.293 6.293a1 1 0 101.414 1.414L15 6.414V9a1 1 0 102 0V4a1 1 0 00-1-1h-5z"></path>
<path d="M5 5a2 2 0 00-2 2v8a2 2 0 002 2h8a2 2 0 002-2v-3a1 1 0 10-2 0v3H5V7h3a1 1 0 000-2H5z"></path>
</svg>
<span class="overtype-link-tooltip-url"></span>
</span>
`,this.tooltip.addEventListener("click",t=>{t.preventDefault(),t.stopPropagation(),this.currentLink&&(window.open(this.currentLink.url,"_blank"),this.hide())}),this.editor.container.appendChild(this.tooltip)}checkCursorPosition(){let e=this.editor.textarea.selectionStart,t=this.editor.textarea.value,o=this.findLinkAtPosition(t,e);o?(!this.currentLink||this.currentLink.url!==o.url||this.currentLink.index!==o.index)&&this.show(o):this.scheduleHide()}findLinkAtPosition(e,t){let o=/\[([^\]]+)\]\(([^)]+)\)/g,i,r=0;for(;(i=o.exec(e))!==null;){let l=i.index,s=i.index+i[0].length;if(t>=l&&t<=s)return{text:i[1],url:i[2],index:r,start:l,end:s};r++}return null}show(e){this.currentLink=e,this.cancelHide();let t=this.tooltip.querySelector(".overtype-link-tooltip-url");t.textContent=e.url,this.tooltip.style.setProperty("--target-anchor",`--link-${e.index}`),this.tooltip.classList.add("visible")}hide(){this.tooltip.classList.remove("visible"),this.currentLink=null}scheduleHide(){this.cancelHide(),this.hideTimeout=setTimeout(()=>this.hide(),300)}cancelHide(){this.hideTimeout&&(clearTimeout(this.hideTimeout),this.hideTimeout=null)}destroy(){this.cancelHide(),this.tooltip&&this.tooltip.parentNode&&this.tooltip.parentNode.removeChild(this.tooltip),this.tooltip=null,this.currentLink=null}};var k=class k{constructor(e,t={}){let o;if(typeof e=="string"){if(o=document.querySelectorAll(e),o.length===0)throw new Error(`No elements found for selector: ${e}`);o=Array.from(o)}else if(e instanceof Element)o=[e];else if(e instanceof NodeList)o=Array.from(e);else if(Array.isArray(e))o=e;else throw new Error("Invalid target: must be selector string, Element, NodeList, or Array");return o.map(r=>{if(r.overTypeInstance)return r.overTypeInstance.reinit(t),r.overTypeInstance;let l=Object.create(k.prototype);return l._init(r,t),r.overTypeInstance=l,k.instances.set(r,l),l})}_init(e,t={}){this.element=e,this.instanceTheme=t.theme||null,this.options=this._mergeOptions(t),this.instanceId=++k.instanceCount,this.initialized=!1,k.injectStyles(),k.initGlobalListeners();let o=e.querySelector(".overtype-container"),i=e.querySelector(".overtype-wrapper");o||i?this._recoverFromDOM(o,i):this._buildFromScratch(),this.shortcuts=new B(this),this.linkTooltip=new Z(this),this.options.toolbar&&(this.toolbar=new U(this),this.toolbar.create(),this.textarea.addEventListener("selectionchange",()=>{this.toolbar.updateButtonStates()}),this.textarea.addEventListener("input",()=>{this.toolbar.updateButtonStates()})),this.initialized=!0,this.options.onChange&&this.options.onChange(this.getValue(),this)}_mergeOptions(e){let t={fontSize:"14px",lineHeight:1.6,fontFamily:"ui-monospace, 'SFMono-Regular', 'Menlo', 'Consolas', 'Liberation Mono', monospace",padding:"16px",mobile:{fontSize:"16px",padding:"12px",lineHeight:1.5},textareaProps:{},autofocus:!1,autoResize:!1,minHeight:"100px",maxHeight:null,placeholder:"Start typing...",value:"",onChange:null,onKeydown:null,showActiveLineRaw:!1,toolbar:!1},{theme:o,colors:i,...r}=e;return{...t,...r}}_recoverFromDOM(e,t){if(e&&e.classList.contains("overtype-container"))this.container=e,this.wrapper=e.querySelector(".overtype-wrapper");else if(t){this.wrapper=t,this.container=document.createElement("div"),this.container.className="overtype-container";let o=this.instanceTheme||k.currentTheme||$,i=typeof o=="string"?o:o.name;if(i&&this.container.setAttribute("data-theme",i),this.instanceTheme){let r=typeof this.instanceTheme=="string"?j(this.instanceTheme):this.instanceTheme;if(r&&r.colors){let l=P(r.colors);this.container.style.cssText+=l}}t.parentNode.insertBefore(this.container,t),this.container.appendChild(t)}if(!this.wrapper){e&&e.remove(),t&&t.remove(),this._buildFromScratch();return}if(this.textarea=this.wrapper.querySelector(".overtype-input"),this.preview=this.wrapper.querySelector(".overtype-preview"),!this.textarea||!this.preview){this.container.remove(),this._buildFromScratch();return}this.wrapper._instance=this,this.options.fontSize&&this.wrapper.style.setProperty("--instance-font-size",this.options.fontSize),this.options.lineHeight&&this.wrapper.style.setProperty("--instance-line-height",String(this.options.lineHeight)),this.options.padding&&this.wrapper.style.setProperty("--instance-padding",this.options.padding),this._configureTextarea(),this._applyOptions()}_buildFromScratch(){let e=this._extractContent();this.element.innerHTML="",this._createDOM(),(e||this.options.value)&&this.setValue(e||this.options.value),this._applyOptions()}_extractContent(){let e=this.element.querySelector(".overtype-input");return e?e.value:this.element.textContent||""}_createDOM(){this.container=document.createElement("div"),this.container.className="overtype-container";let e=this.instanceTheme||k.currentTheme||$,t=typeof e=="string"?e:e.name;if(t&&this.container.setAttribute("data-theme",t),this.instanceTheme){let o=typeof this.instanceTheme=="string"?j(this.instanceTheme):this.instanceTheme;if(o&&o.colors){let i=P(o.colors);this.container.style.cssText+=i}}this.wrapper=document.createElement("div"),this.wrapper.className="overtype-wrapper",this.options.fontSize&&this.wrapper.style.setProperty("--instance-font-size",this.options.fontSize),this.options.lineHeight&&this.wrapper.style.setProperty("--instance-line-height",String(this.options.lineHeight)),this.options.padding&&this.wrapper.style.setProperty("--instance-padding",this.options.padding),this.wrapper._instance=this,this.textarea=document.createElement("textarea"),this.textarea.className="overtype-input",this.textarea.placeholder=this.options.placeholder,this._configureTextarea(),this.options.textareaProps&&Object.entries(this.options.textareaProps).forEach(([o,i])=>{o==="className"||o==="class"?this.textarea.className+=" "+i:o==="style"&&typeof i=="object"?Object.assign(this.textarea.style,i):this.textarea.setAttribute(o,i)}),this.preview=document.createElement("div"),this.preview.className="overtype-preview",this.preview.setAttribute("aria-hidden","true"),this.wrapper.appendChild(this.textarea),this.wrapper.appendChild(this.preview),this.container.appendChild(this.wrapper),this.element.appendChild(this.container),this.options.autoResize?this._setupAutoResize():(this.container.classList.remove("overtype-auto-resize"),window.location.pathname.includes("demo.html")&&console.log("Removed auto-resize class from:",this.element.id))}_configureTextarea(){this.textarea.setAttribute("autocomplete","off"),this.textarea.setAttribute("autocorrect","off"),this.textarea.setAttribute("autocapitalize","off"),this.textarea.setAttribute("spellcheck","false"),this.textarea.setAttribute("data-gramm","false"),this.textarea.setAttribute("data-gramm_editor","false"),this.textarea.setAttribute("data-enable-grammarly","false")}_applyOptions(){this.options.autofocus&&this.textarea.focus(),this.options.autoResize?this.container.classList.contains("overtype-auto-resize")||this._setupAutoResize():this.container.classList.remove("overtype-auto-resize"),this.updatePreview()}updatePreview(){let e=this.textarea.value,t=this.textarea.selectionStart,o=this._getCurrentLine(e,t),i=I.parse(e,o,this.options.showActiveLineRaw);this.preview.innerHTML=i||'<span style="color: #808080;">Start typing...</span>',this._applyCodeBlockBackgrounds(),this.options.onChange&&this.initialized&&this.options.onChange(e,this)}_applyCodeBlockBackgrounds(){let e=this.preview.querySelectorAll(".code-fence");for(let t=0;t<e.length-1;t+=2){let o=e[t],i=e[t+1],r=o.parentElement,l=i.parentElement;if(!r||!l)continue;o.style.display="block",i.style.display="block",r.classList.add("code-block-line"),l.classList.add("code-block-line");let s=r.nextElementSibling;for(;s&&s!==l;){if(s.tagName==="DIV"){s.classList.add("code-block-line");let a=s.textContent;s.textContent=a}if(s=s.nextElementSibling,!s)break}}}_getCurrentLine(e,t){return e.substring(0,t).split(`
`).length-1}handleInput(e){this.updatePreview()}handleKeydown(e){if(e.key==="Tab"){e.preventDefault();let o=this.textarea.selectionStart,i=this.textarea.selectionEnd,r=this.textarea.value;if(o!==i&&e.shiftKey){let l=r.substring(0,o),s=r.substring(o,i),a=r.substring(i),p=s.split(`
`).map(c=>c.replace(/^ /,"")).join(`
`);this.textarea.value=l+p+a,this.textarea.selectionStart=o,this.textarea.selectionEnd=o+p.length}else if(o!==i){let l=r.substring(0,o),s=r.substring(o,i),a=r.substring(i),p=s.split(`
`).map(c=>" "+c).join(`
`);this.textarea.value=l+p+a,this.textarea.selectionStart=o,this.textarea.selectionEnd=o+p.length}else this.textarea.value=r.substring(0,o)+" "+r.substring(i),this.textarea.selectionStart=this.textarea.selectionEnd=o+2;this.textarea.dispatchEvent(new Event("input",{bubbles:!0}));return}!this.shortcuts.handleKeydown(e)&&this.options.onKeydown&&this.options.onKeydown(e,this)}handleScroll(e){this.preview.scrollTop=this.textarea.scrollTop,this.preview.scrollLeft=this.textarea.scrollLeft}getValue(){return this.textarea.value}setValue(e){this.textarea.value=e,this.updatePreview(),this.options.autoResize&&this._updateAutoHeight()}focus(){this.textarea.focus()}blur(){this.textarea.blur()}isInitialized(){return this.initialized}reinit(e={}){this.options=this._mergeOptions({...this.options,...e}),this._applyOptions(),this.updatePreview()}_setupAutoResize(){this.container.classList.add("overtype-auto-resize"),this.previousHeight=null,this._updateAutoHeight(),this.textarea.addEventListener("input",()=>this._updateAutoHeight()),window.addEventListener("resize",()=>this._updateAutoHeight())}_updateAutoHeight(){if(!this.options.autoResize)return;let e=this.textarea,t=this.preview,o=this.wrapper,i=window.getComputedStyle(e),r=parseFloat(i.paddingTop),l=parseFloat(i.paddingBottom),s=e.scrollTop;e.style.setProperty("height","auto","important");let a=e.scrollHeight;if(this.options.minHeight){let c=parseInt(this.options.minHeight);a=Math.max(a,c)}let d="hidden";if(this.options.maxHeight){let c=parseInt(this.options.maxHeight);a>c&&(a=c,d="auto")}let p=a+"px";e.style.setProperty("height",p,"important"),e.style.setProperty("overflow-y",d,"important"),t.style.setProperty("height",p,"important"),t.style.setProperty("overflow-y",d,"important"),o.style.setProperty("height",p,"important"),e.scrollTop=s,t.scrollTop=s,this.previousHeight!==a&&(this.previousHeight=a)}destroy(){if(this.element.overTypeInstance=null,k.instances.delete(this.element),this.shortcuts&&this.shortcuts.destroy(),this.wrapper){let e=this.getValue();this.wrapper.remove(),this.element.textContent=e}this.initialized=!1}static init(e,t={}){return new k(e,t)}static getInstance(e){return e.overTypeInstance||k.instances.get(e)||null}static destroyAll(){document.querySelectorAll("[data-overtype-instance]").forEach(t=>{let o=k.getInstance(t);o&&o.destroy()})}static injectStyles(e=!1){if(k.stylesInjected&&!e)return;let t=document.querySelector("style.overtype-styles");t&&t.remove();let o=k.currentTheme||$,i=ke({theme:o}),r=document.createElement("style");r.className="overtype-styles",r.textContent=i,document.head.appendChild(r),k.stylesInjected=!0}static setTheme(e,t=null){let o=typeof e=="string"?j(e):e;t&&(o=be(o,t)),k.currentTheme=o,k.injectStyles(!0),document.querySelectorAll(".overtype-container").forEach(i=>{let r=typeof o=="string"?o:o.name;r&&i.setAttribute("data-theme",r)}),document.querySelectorAll(".overtype-wrapper").forEach(i=>{if(!i.closest(".overtype-container")){let l=typeof o=="string"?o:o.name;l&&i.setAttribute("data-theme",l)}let r=i._instance;r&&r.updatePreview()})}static initGlobalListeners(){k.globalListenersInitialized||(document.addEventListener("input",e=>{if(e.target&&e.target.classList&&e.target.classList.contains("overtype-input")){let t=e.target.closest(".overtype-wrapper"),o=t==null?void 0:t._instance;o&&o.handleInput(e)}}),document.addEventListener("keydown",e=>{if(e.target&&e.target.classList&&e.target.classList.contains("overtype-input")){let t=e.target.closest(".overtype-wrapper"),o=t==null?void 0:t._instance;o&&o.handleKeydown(e)}}),document.addEventListener("scroll",e=>{if(e.target&&e.target.classList&&e.target.classList.contains("overtype-input")){let t=e.target.closest(".overtype-wrapper"),o=t==null?void 0:t._instance;o&&o.handleScroll(e)}},!0),document.addEventListener("selectionchange",e=>{let t=document.activeElement;if(t&&t.classList.contains("overtype-input")){let o=t.closest(".overtype-wrapper"),i=o==null?void 0:o._instance;i&&(clearTimeout(i._selectionTimeout),i._selectionTimeout=setTimeout(()=>{i.updatePreview()},50))}}),k.globalListenersInitialized=!0)}};M(k,"instances",new WeakMap),M(k,"stylesInjected",!1),M(k,"globalListenersInitialized",!1),M(k,"instanceCount",0);var A=k;A.MarkdownParser=I;A.ShortcutsManager=B;A.themes={solar:$,cave:j("cave")};A.getTheme=j;A.currentTheme=$;var Ge=A;return ze(Xe);})();
/**
* OverType - A lightweight markdown editor library with perfect WYSIWYG alignment
* @version 1.0.0
* @license MIT
*/
window.OverType = OverType.OverType || OverType.default || OverType;
// Initialize the editor
const [editor] = new OverType('#editor', {
placeholder: 'Start typing markdown...',
toolbar: true,
textareaProps: {
required: true,
maxLength: 65535,
name: 'content',
rows: 15,
}
});