@powerduck/md-editor 0.10.5 → 0.10.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +4 -4
- package/dist/index.mjs +203 -146
- package/dist/plugins/doclink.d.ts +4 -0
- package/dist/plugins/mention.d.ts +4 -0
- package/dist/style.css +1 -1
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
"use strict";var tn=Object.create;var ze=Object.defineProperty;var nn=Object.getOwnPropertyDescriptor;var rn=Object.getOwnPropertyNames;var sn=Object.getPrototypeOf,on=Object.prototype.hasOwnProperty;var an=(n,e,t,i)=>{if(e&&typeof e=="object"||typeof e=="function")for(let r of rn(e))!on.call(n,r)&&r!==t&&ze(n,r,{get:()=>e[r],enumerable:!(i=nn(e,r))||i.enumerable});return n};var Ke=(n,e,t)=>(t=n!=null?tn(sn(n)):{},an(e||!n||!n.__esModule?ze(t,"default",{value:n,enumerable:!0}):t,n));Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const Te=require("@codemirror/state"),J=require("@codemirror/view"),Se=require("@codemirror/commands"),cn=require("@codemirror/lang-markdown"),ln=require("@codemirror/language-data"),dn=require("markdown-it"),Ge=require("katex");class un{constructor(e,t={}){this.dropdown=null,this.items=[],this.selectedIndex=0,this.trigger=null,this.activeHead=0,this.searchAbort=null,this.destroyed=!1,this.view=e,this.options={onMentionSearch:t.onMentionSearch??(()=>[]),onMentionSelect:t.onMentionSelect??(i=>`[@${i.label}](mention:${i.id})`),minChars:t.minChars??0,maxItems:t.maxItems??8}}update(){if(this.destroyed)return;const e=this.findTrigger();if(!e){this.hide();return}this.trigger=e,this.runSearch(e.query)}handleKey(e){if(!this.dropdown||this.items.length===0)return!1;switch(e.key){case"ArrowDown":return e.preventDefault(),this.selectedIndex=(this.selectedIndex+1)%this.items.length,this.updateActiveClass(),!0;case"ArrowUp":return e.preventDefault(),this.selectedIndex=(this.selectedIndex-1+this.items.length)%this.items.length,this.updateActiveClass(),!0;case"Enter":case"Tab":return e.preventDefault(),this.selectItem(this.selectedIndex),!0;case"Escape":return e.preventDefault(),this.hide(),!0}return!1}findTrigger(){const{head:e}=this.view.state.selection.main,t=this.view.state.doc.lineAt(e),i=t.text,r=e-t.from;for(let a=r-1;a>=0;a--){const s=i[a];if(s==="@"){if(a===0||/\s|[^\w@]/.test(i[a-1])){const o=i.slice(a+1,r);return/\s/.test(o)?null:{from:t.from+a,query:o}}return null}if(/\s/.test(s))break}return null}async runSearch(e){var i;(i=this.searchAbort)==null||i.call(this);let t=!1;if(this.searchAbort=()=>{t=!0},e.length<this.options.minChars){this.hide();return}try{const r=await this.options.onMentionSearch(e);if(t||this.destroyed)return;this.items=r.slice(0,this.options.maxItems),this.selectedIndex=0,this.items.length>0?this.show():this.hide()}catch{t||this.hide()}}show(){if(this.trigger){if(this.activeHead=this.view.state.selection.main.head,!this.dropdown){this.dropdown=document.createElement("div"),this.dropdown.className="md-editor-mention-dropdown",this.dropdown.setAttribute("role","listbox");const e=this.view.dom.closest(".md-editor-root"),t=(e==null?void 0:e.getAttribute("data-theme"))??"light";this.dropdown.setAttribute("data-theme",t),document.body.appendChild(this.dropdown),this.dropdown.addEventListener("mousedown",i=>{const a=i.target.closest(".md-editor-mention-item");if(!a)return;i.preventDefault(),i.stopPropagation();const s=Number(a.dataset.index);Number.isNaN(s)||this.selectItem(s)}),this.dropdown.addEventListener("mouseover",i=>{const a=i.target.closest(".md-editor-mention-item");if(!a)return;const s=Number(a.dataset.index);!Number.isNaN(s)&&s!==this.selectedIndex&&(this.selectedIndex=s,this.updateActiveClass())})}this.renderDropdown(),this.positionDropdown()}}renderDropdown(){this.dropdown&&(this.dropdown.innerHTML="",this.items.forEach((e,t)=>{const i=document.createElement("div");if(i.className="md-editor-mention-item"+(t===this.selectedIndex?" is-active":""),i.setAttribute("role","option"),i.setAttribute("aria-selected",t===this.selectedIndex?"true":"false"),i.dataset.index=String(t),e.avatar){const s=document.createElement("img");s.className="md-editor-mention-avatar",s.src=e.avatar,s.alt="",i.appendChild(s)}const r=document.createElement("div");r.className="md-editor-mention-text";const a=document.createElement("div");if(a.className="md-editor-mention-label",a.textContent=e.label,r.appendChild(a),e.description){const s=document.createElement("div");s.className="md-editor-mention-desc",s.textContent=e.description,r.appendChild(s)}i.appendChild(r),this.dropdown.appendChild(i)}))}updateActiveClass(){if(!this.dropdown)return;const e=this.dropdown.children;for(let t=0;t<e.length;t++){const i=e[t],r=t===this.selectedIndex;i.classList.toggle("is-active",r),i.setAttribute("aria-selected",r?"true":"false")}}positionDropdown(){if(!this.dropdown||!this.trigger)return;let e=null;try{e=this.view.coordsAtPos(this.trigger.from)}catch{e=null}if(!e){const s=this.view.dom.getBoundingClientRect();e={top:s.top,bottom:s.top+20,left:s.left}}const t=this.dropdown.getBoundingClientRect(),i=window.innerHeight;let r=e.bottom+4,a=e.left;r+t.height>i-8&&(r=e.top-t.height-4),a=Math.max(8,Math.min(a,window.innerWidth-t.width-8)),this.dropdown.style.top=`${r}px`,this.dropdown.style.left=`${a}px`}selectItem(e){const t=this.items[e],i=this.trigger;if(!t||!i)return;const r=this.options.onMentionSelect(t);this.options.onMentionSelect!==void 0&&!r.includes("](mention:")&&r.startsWith("@")&&console.warn("[md-editor] onMentionSelect returned plain '@"+t.label+"'. For badge rendering in the preview, return '[@"+t.label+"](mention:"+t.id+")' instead, or omit onMentionSelect to use the default wrapper.");const a=i.from,s=this.activeHead;this.view.focus(),this.view.dispatch({changes:{from:a,to:s,insert:r},selection:{anchor:a+r.length}}),this.hide()}hide(){var e;this.dropdown&&(this.dropdown.remove(),this.dropdown=null),this.items=[],this.trigger=null,(e=this.searchAbort)==null||e.call(this),this.searchAbort=null}destroy(){this.destroyed=!0,this.hide()}}function hn(n){const e=n.renderer.rules.link_open??((r,a,s,o,c)=>c.renderToken(r,a,s)),t=n.renderer.rules.link_close??((r,a,s,o,c)=>c.renderToken(r,a,s));let i=!1;n.renderer.rules.link_open=(r,a,s,o,c)=>{const l=r[a],d=(l==null?void 0:l.attrGet("href"))??"";if(typeof d=="string"&&d.startsWith("mention:")){i=!0;const h=d.slice(8);return`<span class="md-editor-mention" data-mention-id="${pn(h)}">`}return i=!1,e(r,a,s,o,c)},n.renderer.rules.link_close=(r,a,s,o,c)=>i?(i=!1,"</span>"):t(r,a,s,o,c)}function pn(n){return n.replace(/"/g,""").replace(/</g,"<").replace(/>/g,">")}async function gn(n){const e=await fetch(n,{mode:"cors"});if(!e.ok)throw new Error(`HTTP ${e.status}`);const t=await e.text();return mn(t,n)}function mn(n,e){var c,l,d,h,p,m,b;const t={},i=new DOMParser().parseFromString(n,"text/html"),a=((c=i.querySelector('meta[property="og:title"]'))==null?void 0:c.getAttribute("content"))??((l=i.querySelector("title"))==null?void 0:l.textContent)??void 0;a&&(t.title=a.trim());const s=((d=i.querySelector('meta[property="og:description"]'))==null?void 0:d.getAttribute("content"))??((h=i.querySelector('meta[name="description"]'))==null?void 0:h.getAttribute("content"));s&&(t.description=s.trim());const o=((p=i.querySelector('meta[property="og:image"]'))==null?void 0:p.getAttribute("content"))??((m=i.querySelector('meta[property="og:image:secure_url"]'))==null?void 0:m.getAttribute("content"))??((b=i.querySelector('meta[name="twitter:image"]'))==null?void 0:b.getAttribute("content"));return o&&(t.thumbnail=o.startsWith("http")?o:new URL(o,e).href),t}class fn{constructor(e,t={}){this.dropdown=null,this.items=[],this.selectedIndex=0,this.trigger=null,this.activeHead=0,this.searchAbort=null,this.destroyed=!1,this.view=e,this.options={onDocSearch:t.onDocSearch??(()=>[]),insertStyle:t.insertStyle??"auto",triggerChar:t.triggerChar??"/",minChars:t.minChars??0,maxItems:t.maxItems??8,onFetchDocMeta:t.onFetchDocMeta??gn}}update(){if(this.destroyed)return;const e=this.findTrigger();if(!e){this.hide();return}this.trigger=e,this.runSearch(e.query)}handleKey(e){if(!this.dropdown||this.items.length===0)return!1;switch(e.key){case"ArrowDown":return e.preventDefault(),this.selectedIndex=(this.selectedIndex+1)%this.items.length,this.updateActiveClass(),!0;case"ArrowUp":return e.preventDefault(),this.selectedIndex=(this.selectedIndex-1+this.items.length)%this.items.length,this.updateActiveClass(),!0;case"Enter":case"Tab":return e.preventDefault(),this.selectItem(this.selectedIndex),!0;case"Escape":return e.preventDefault(),this.hide(),!0}return!1}findTrigger(){const{head:e}=this.view.state.selection.main,t=this.view.state.doc.lineAt(e),i=t.text,r=e-t.from,a=this.options.triggerChar;for(let s=r-1;s>=0;s--){const o=i[s];if(o===a){if(s===0||/\s/.test(i[s-1])){const c=i.slice(s+1,r);return/\s/.test(c)?null:{from:t.from+s,query:c}}return null}if(/\s/.test(o))break}return null}async runSearch(e){var i;(i=this.searchAbort)==null||i.call(this);let t=!1;if(this.searchAbort=()=>{t=!0},e.length<this.options.minChars){this.hide();return}try{const r=await this.options.onDocSearch(e);if(t||this.destroyed)return;this.items=r.slice(0,this.options.maxItems),this.selectedIndex=0,this.items.length>0?this.show():this.hide()}catch{t||this.hide()}}show(){if(this.trigger){if(this.activeHead=this.view.state.selection.main.head,!this.dropdown){this.dropdown=document.createElement("div"),this.dropdown.className="md-editor-doclink-dropdown",this.dropdown.setAttribute("role","listbox");const e=this.view.dom.closest(".md-editor-root"),t=(e==null?void 0:e.getAttribute("data-theme"))??"light";this.dropdown.setAttribute("data-theme",t),document.body.appendChild(this.dropdown),this.dropdown.addEventListener("mousedown",i=>{const a=i.target.closest(".md-editor-doclink-item");if(!a)return;i.preventDefault(),i.stopPropagation();const s=Number(a.dataset.index);Number.isNaN(s)||this.selectItem(s)}),this.dropdown.addEventListener("mouseover",i=>{const a=i.target.closest(".md-editor-doclink-item");if(!a)return;const s=Number(a.dataset.index);!Number.isNaN(s)&&s!==this.selectedIndex&&(this.selectedIndex=s,this.updateActiveClass())})}this.renderDropdown(),this.positionDropdown()}}renderDropdown(){this.dropdown&&(this.dropdown.innerHTML="",this.items.forEach((e,t)=>{const i=document.createElement("div");if(i.className="md-editor-doclink-item"+(t===this.selectedIndex?" is-active":""),i.setAttribute("role","option"),i.setAttribute("aria-selected",t===this.selectedIndex?"true":"false"),i.dataset.index=String(t),e.thumbnail){const o=document.createElement("img");o.className="md-editor-doclink-thumb-img",o.src=e.thumbnail,o.alt="",i.appendChild(o)}const r=document.createElement("div");r.className="md-editor-doclink-text";const a=document.createElement("div");if(a.className="md-editor-doclink-label",a.textContent=e.title,r.appendChild(a),e.description){const o=document.createElement("div");o.className="md-editor-doclink-desc",o.textContent=e.description,r.appendChild(o)}const s=document.createElement("div");s.className="md-editor-doclink-url";try{s.textContent=new URL(e.url).hostname}catch{s.textContent=e.url}r.appendChild(s),i.appendChild(r),this.dropdown.appendChild(i)}))}updateActiveClass(){if(!this.dropdown)return;const e=this.dropdown.children;for(let t=0;t<e.length;t++){const i=e[t],r=t===this.selectedIndex;i.classList.toggle("is-active",r),i.setAttribute("aria-selected",r?"true":"false")}}positionDropdown(){if(!this.dropdown||!this.trigger)return;let e=null;try{e=this.view.coordsAtPos(this.trigger.from)}catch{e=null}if(!e){const s=this.view.dom.getBoundingClientRect();e={top:s.top,bottom:s.top+20,left:s.left}}const t=this.dropdown.getBoundingClientRect(),i=window.innerHeight;let r=e.bottom+4,a=e.left;r+t.height>i-8&&(r=e.top-t.height-4),a=Math.max(8,Math.min(a,window.innerWidth-t.width-8)),this.dropdown.style.top=`${r}px`,this.dropdown.style.left=`${a}px`}async selectItem(e){const t=this.items[e],i=this.trigger;if(!t||!i)return;const r=i.from,a=this.activeHead;this.view.focus(),this.view.dispatch({changes:{from:r,to:a,insert:""}});const s=await this.buildInsertion(t);this.view.dispatch({changes:{from:r,insert:s},selection:{anchor:r+s.length}}),this.hide()}async buildInsertion(e){const t=this.options.insertStyle;if(t==="link")return`[${e.title}](${e.url})`;let i={title:e.title,thumbnail:e.thumbnail,description:e.description};if(!i.thumbnail||!i.description)try{const o=await this.options.onFetchDocMeta(e.url);i={title:o.title??i.title,thumbnail:o.thumbnail??i.thumbnail,description:o.description??i.description}}catch{}const a=!!(i.thumbnail||i.description);if(t==="auto"&&!a)return`[${e.title}](${e.url})`;if(t==="card"&&!a)return`[${e.title}](${e.url})`;const s=[`:::doc-link ${e.url}`];return s.push(`# ${i.title??e.title}`),i.thumbnail&&s.push(``),i.description&&s.push(i.description),s.push(":::"),`
|
|
1
|
+
"use strict";var tn=Object.create;var ze=Object.defineProperty;var nn=Object.getOwnPropertyDescriptor;var rn=Object.getOwnPropertyNames;var sn=Object.getPrototypeOf,on=Object.prototype.hasOwnProperty;var an=(n,e,t,i)=>{if(e&&typeof e=="object"||typeof e=="function")for(let r of rn(e))!on.call(n,r)&&r!==t&&ze(n,r,{get:()=>e[r],enumerable:!(i=nn(e,r))||i.enumerable});return n};var Ke=(n,e,t)=>(t=n!=null?tn(sn(n)):{},an(e||!n||!n.__esModule?ze(t,"default",{value:n,enumerable:!0}):t,n));Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const Te=require("@codemirror/state"),J=require("@codemirror/view"),Se=require("@codemirror/commands"),cn=require("@codemirror/lang-markdown"),ln=require("@codemirror/language-data"),dn=require("markdown-it"),Ge=require("katex");class un{constructor(e,t={}){this.dropdown=null,this.items=[],this.selectedIndex=0,this.trigger=null,this.activeHead=0,this.searchAbort=null,this.destroyed=!1,this.view=e,this.options={onMentionSearch:t.onMentionSearch??(()=>[]),onMentionSelect:t.onMentionSelect??(i=>`[@${i.label}](mention:${i.id})`),minChars:t.minChars??0,maxItems:t.maxItems??8}}update(){if(this.destroyed)return;const e=this.findTrigger();if(!e){this.hide();return}this.trigger=e,this.runSearch(e.query)}isOpen(){return this.dropdown!==null&&this.items.length>0}selectCurrent(){this.isOpen()&&this.selectItem(this.selectedIndex)}handleKey(e){if(!this.dropdown||this.items.length===0)return!1;switch(e.key){case"ArrowDown":return e.preventDefault(),this.selectedIndex=(this.selectedIndex+1)%this.items.length,this.updateActiveClass(),!0;case"ArrowUp":return e.preventDefault(),this.selectedIndex=(this.selectedIndex-1+this.items.length)%this.items.length,this.updateActiveClass(),!0;case"Enter":case"Tab":return e.preventDefault(),this.selectItem(this.selectedIndex),!0;case"Escape":return e.preventDefault(),this.hide(),!0}return!1}findTrigger(){const{head:e}=this.view.state.selection.main,t=this.view.state.doc.lineAt(e),i=t.text,r=e-t.from;for(let a=r-1;a>=0;a--){const s=i[a];if(s==="@"){if(a===0||/\s|[^\w@]/.test(i[a-1])){const o=i.slice(a+1,r);return/\s/.test(o)?null:{from:t.from+a,query:o}}return null}if(/\s/.test(s))break}return null}async runSearch(e){var i;(i=this.searchAbort)==null||i.call(this);let t=!1;if(this.searchAbort=()=>{t=!0},e.length<this.options.minChars){this.hide();return}try{const r=await this.options.onMentionSearch(e);if(t||this.destroyed)return;this.items=r.slice(0,this.options.maxItems),this.selectedIndex=0,this.items.length>0?this.show():this.hide()}catch{t||this.hide()}}show(){if(this.trigger){if(this.activeHead=this.view.state.selection.main.head,!this.dropdown){this.dropdown=document.createElement("div"),this.dropdown.className="md-editor-mention-dropdown",this.dropdown.setAttribute("role","listbox");const e=this.view.dom.closest(".md-editor-root"),t=(e==null?void 0:e.getAttribute("data-theme"))??"light";this.dropdown.setAttribute("data-theme",t),document.body.appendChild(this.dropdown),this.dropdown.addEventListener("mousedown",i=>{const a=i.target.closest(".md-editor-mention-item");if(!a)return;i.preventDefault(),i.stopPropagation();const s=Number(a.dataset.index);Number.isNaN(s)||this.selectItem(s)}),this.dropdown.addEventListener("mouseover",i=>{const a=i.target.closest(".md-editor-mention-item");if(!a)return;const s=Number(a.dataset.index);!Number.isNaN(s)&&s!==this.selectedIndex&&(this.selectedIndex=s,this.updateActiveClass())})}this.renderDropdown(),this.positionDropdown()}}renderDropdown(){this.dropdown&&(this.dropdown.innerHTML="",this.items.forEach((e,t)=>{const i=document.createElement("div");if(i.className="md-editor-mention-item"+(t===this.selectedIndex?" is-active":""),i.setAttribute("role","option"),i.setAttribute("aria-selected",t===this.selectedIndex?"true":"false"),i.dataset.index=String(t),e.avatar){const s=document.createElement("img");s.className="md-editor-mention-avatar",s.src=e.avatar,s.alt="",i.appendChild(s)}const r=document.createElement("div");r.className="md-editor-mention-text";const a=document.createElement("div");if(a.className="md-editor-mention-label",a.textContent=e.label,r.appendChild(a),e.description){const s=document.createElement("div");s.className="md-editor-mention-desc",s.textContent=e.description,r.appendChild(s)}i.appendChild(r),this.dropdown.appendChild(i)}))}updateActiveClass(){if(!this.dropdown)return;const e=this.dropdown.children;for(let t=0;t<e.length;t++){const i=e[t],r=t===this.selectedIndex;i.classList.toggle("is-active",r),i.setAttribute("aria-selected",r?"true":"false")}}positionDropdown(){if(!this.dropdown||!this.trigger)return;let e=null;try{e=this.view.coordsAtPos(this.trigger.from)}catch{e=null}if(!e){const s=this.view.dom.getBoundingClientRect();e={top:s.top,bottom:s.top+20,left:s.left}}const t=this.dropdown.getBoundingClientRect(),i=window.innerHeight;let r=e.bottom+4,a=e.left;r+t.height>i-8&&(r=e.top-t.height-4),a=Math.max(8,Math.min(a,window.innerWidth-t.width-8)),this.dropdown.style.top=`${r}px`,this.dropdown.style.left=`${a}px`}selectItem(e){const t=this.items[e],i=this.trigger;if(!t||!i)return;const r=this.options.onMentionSelect(t);this.options.onMentionSelect!==void 0&&!r.includes("](mention:")&&r.startsWith("@")&&console.warn("[md-editor] onMentionSelect returned plain '@"+t.label+"'. For badge rendering in the preview, return '[@"+t.label+"](mention:"+t.id+")' instead, or omit onMentionSelect to use the default wrapper.");const a=i.from,s=this.activeHead;this.view.focus(),this.view.dispatch({changes:{from:a,to:s,insert:r},selection:{anchor:a+r.length}}),this.hide()}hide(){var e;this.dropdown&&(this.dropdown.remove(),this.dropdown=null),this.items=[],this.trigger=null,(e=this.searchAbort)==null||e.call(this),this.searchAbort=null}destroy(){this.destroyed=!0,this.hide()}}function hn(n){const e=n.renderer.rules.link_open??((r,a,s,o,c)=>c.renderToken(r,a,s)),t=n.renderer.rules.link_close??((r,a,s,o,c)=>c.renderToken(r,a,s));let i=!1;n.renderer.rules.link_open=(r,a,s,o,c)=>{const l=r[a],d=(l==null?void 0:l.attrGet("href"))??"";if(typeof d=="string"&&d.startsWith("mention:")){i=!0;const h=d.slice(8);return`<span class="md-editor-mention" data-mention-id="${pn(h)}">`}return i=!1,e(r,a,s,o,c)},n.renderer.rules.link_close=(r,a,s,o,c)=>i?(i=!1,"</span>"):t(r,a,s,o,c)}function pn(n){return n.replace(/"/g,""").replace(/</g,"<").replace(/>/g,">")}async function gn(n){const e=await fetch(n,{mode:"cors"});if(!e.ok)throw new Error(`HTTP ${e.status}`);const t=await e.text();return mn(t,n)}function mn(n,e){var c,l,d,h,p,m,b;const t={},i=new DOMParser().parseFromString(n,"text/html"),a=((c=i.querySelector('meta[property="og:title"]'))==null?void 0:c.getAttribute("content"))??((l=i.querySelector("title"))==null?void 0:l.textContent)??void 0;a&&(t.title=a.trim());const s=((d=i.querySelector('meta[property="og:description"]'))==null?void 0:d.getAttribute("content"))??((h=i.querySelector('meta[name="description"]'))==null?void 0:h.getAttribute("content"));s&&(t.description=s.trim());const o=((p=i.querySelector('meta[property="og:image"]'))==null?void 0:p.getAttribute("content"))??((m=i.querySelector('meta[property="og:image:secure_url"]'))==null?void 0:m.getAttribute("content"))??((b=i.querySelector('meta[name="twitter:image"]'))==null?void 0:b.getAttribute("content"));return o&&(t.thumbnail=o.startsWith("http")?o:new URL(o,e).href),t}class fn{constructor(e,t={}){this.dropdown=null,this.items=[],this.selectedIndex=0,this.trigger=null,this.activeHead=0,this.searchAbort=null,this.destroyed=!1,this.view=e,this.options={onDocSearch:t.onDocSearch??(()=>[]),insertStyle:t.insertStyle??"auto",triggerChar:t.triggerChar??"/",minChars:t.minChars??0,maxItems:t.maxItems??8,onFetchDocMeta:t.onFetchDocMeta??gn}}update(){if(this.destroyed)return;const e=this.findTrigger();if(!e){this.hide();return}this.trigger=e,this.runSearch(e.query)}isOpen(){return this.dropdown!==null&&this.items.length>0}selectCurrent(){this.isOpen()&&this.selectItem(this.selectedIndex)}handleKey(e){if(!this.dropdown||this.items.length===0)return!1;switch(e.key){case"ArrowDown":return e.preventDefault(),this.selectedIndex=(this.selectedIndex+1)%this.items.length,this.updateActiveClass(),!0;case"ArrowUp":return e.preventDefault(),this.selectedIndex=(this.selectedIndex-1+this.items.length)%this.items.length,this.updateActiveClass(),!0;case"Enter":case"Tab":return e.preventDefault(),this.selectItem(this.selectedIndex),!0;case"Escape":return e.preventDefault(),this.hide(),!0}return!1}findTrigger(){const{head:e}=this.view.state.selection.main,t=this.view.state.doc.lineAt(e),i=t.text,r=e-t.from,a=this.options.triggerChar;for(let s=r-1;s>=0;s--){const o=i[s];if(o===a){if(s===0||/\s/.test(i[s-1])){const c=i.slice(s+1,r);return/\s/.test(c)?null:{from:t.from+s,query:c}}return null}if(/\s/.test(o))break}return null}async runSearch(e){var i;(i=this.searchAbort)==null||i.call(this);let t=!1;if(this.searchAbort=()=>{t=!0},e.length<this.options.minChars){this.hide();return}try{const r=await this.options.onDocSearch(e);if(t||this.destroyed)return;this.items=r.slice(0,this.options.maxItems),this.selectedIndex=0,this.items.length>0?this.show():this.hide()}catch{t||this.hide()}}show(){if(this.trigger){if(this.activeHead=this.view.state.selection.main.head,!this.dropdown){this.dropdown=document.createElement("div"),this.dropdown.className="md-editor-doclink-dropdown",this.dropdown.setAttribute("role","listbox");const e=this.view.dom.closest(".md-editor-root"),t=(e==null?void 0:e.getAttribute("data-theme"))??"light";this.dropdown.setAttribute("data-theme",t),document.body.appendChild(this.dropdown),this.dropdown.addEventListener("mousedown",i=>{const a=i.target.closest(".md-editor-doclink-item");if(!a)return;i.preventDefault(),i.stopPropagation();const s=Number(a.dataset.index);Number.isNaN(s)||this.selectItem(s)}),this.dropdown.addEventListener("mouseover",i=>{const a=i.target.closest(".md-editor-doclink-item");if(!a)return;const s=Number(a.dataset.index);!Number.isNaN(s)&&s!==this.selectedIndex&&(this.selectedIndex=s,this.updateActiveClass())})}this.renderDropdown(),this.positionDropdown()}}renderDropdown(){this.dropdown&&(this.dropdown.innerHTML="",this.items.forEach((e,t)=>{const i=document.createElement("div");if(i.className="md-editor-doclink-item"+(t===this.selectedIndex?" is-active":""),i.setAttribute("role","option"),i.setAttribute("aria-selected",t===this.selectedIndex?"true":"false"),i.dataset.index=String(t),e.thumbnail){const o=document.createElement("img");o.className="md-editor-doclink-thumb-img",o.src=e.thumbnail,o.alt="",i.appendChild(o)}const r=document.createElement("div");r.className="md-editor-doclink-text";const a=document.createElement("div");if(a.className="md-editor-doclink-label",a.textContent=e.title,r.appendChild(a),e.description){const o=document.createElement("div");o.className="md-editor-doclink-desc",o.textContent=e.description,r.appendChild(o)}const s=document.createElement("div");s.className="md-editor-doclink-url";try{s.textContent=new URL(e.url).hostname}catch{s.textContent=e.url}r.appendChild(s),i.appendChild(r),this.dropdown.appendChild(i)}))}updateActiveClass(){if(!this.dropdown)return;const e=this.dropdown.children;for(let t=0;t<e.length;t++){const i=e[t],r=t===this.selectedIndex;i.classList.toggle("is-active",r),i.setAttribute("aria-selected",r?"true":"false")}}positionDropdown(){if(!this.dropdown||!this.trigger)return;let e=null;try{e=this.view.coordsAtPos(this.trigger.from)}catch{e=null}if(!e){const s=this.view.dom.getBoundingClientRect();e={top:s.top,bottom:s.top+20,left:s.left}}const t=this.dropdown.getBoundingClientRect(),i=window.innerHeight;let r=e.bottom+4,a=e.left;r+t.height>i-8&&(r=e.top-t.height-4),a=Math.max(8,Math.min(a,window.innerWidth-t.width-8)),this.dropdown.style.top=`${r}px`,this.dropdown.style.left=`${a}px`}async selectItem(e){const t=this.items[e],i=this.trigger;if(!t||!i)return;const r=i.from,a=this.activeHead;this.view.focus(),this.view.dispatch({changes:{from:r,to:a,insert:""}});const s=await this.buildInsertion(t);this.view.dispatch({changes:{from:r,insert:s},selection:{anchor:r+s.length}}),this.hide()}async buildInsertion(e){const t=this.options.insertStyle;if(t==="link")return`[${e.title}](${e.url})`;let i={title:e.title,thumbnail:e.thumbnail,description:e.description};if(!i.thumbnail||!i.description)try{const o=await this.options.onFetchDocMeta(e.url);i={title:o.title??i.title,thumbnail:o.thumbnail??i.thumbnail,description:o.description??i.description}}catch{}const a=!!(i.thumbnail||i.description);if(t==="auto"&&!a)return`[${e.title}](${e.url})`;if(t==="card"&&!a)return`[${e.title}](${e.url})`;const s=[`:::doc-link ${e.url}`];return s.push(`# ${i.title??e.title}`),i.thumbnail&&s.push(``),i.description&&s.push(i.description),s.push(":::"),`
|
|
2
2
|
`+s.join(`
|
|
3
3
|
`)+`
|
|
4
4
|
`}hide(){var e;this.dropdown&&(this.dropdown.remove(),this.dropdown=null),this.items=[],this.trigger=null,(e=this.searchAbort)==null||e.call(this),this.searchAbort=null}destroy(){this.destroyed=!0,this.hide()}}const bn=/^:::doc-link\s+(\S+)(?:\s+(.+))?$/,_n=/^:::\s*$/;function En(n){n.block.ruler.before("fence","doclink_block",(e,t,i,r)=>{const s=e.src.slice(e.bMarks[t],e.eMarks[t]).match(bn);if(!s)return!1;const o=s[1]??"";let c=t+1,l=!1;for(;c<i;){const E=e.src.slice(e.bMarks[c],e.eMarks[c]);if(_n.test(E.trim())){l=!0;break}c++}if(!l)return!1;if(r)return!0;const d=e.src.slice(e.bMarks[t+1],e.bMarks[c]).split(`
|
|
5
|
-
`).map(E=>E.trim()).filter(E=>E.length>0);let h="",p="";const m=[];for(const E of d)if(E.startsWith("# "))h=E.slice(2).trim();else if(E.startsWith("){const N=E.match(/^!\[thumbnail\]\((.+)\)$/);N&&(p=N[1]??"")}else m.push(E);const b=e.push("doclink_block","div",0);return b.content=JSON.stringify({url:o,title:h,thumbnail:p,description:m.join(" ")}),e.line=c+1,!0}),n.renderer.rules.doclink_block=(e,t)=>{const i=e[t];if(!i)return"";let r;try{r=JSON.parse(i.content)}catch{return""}const{url:a,title:s,thumbnail:o,description:c}=r;let l=a;try{l=new URL(a).hostname}catch{}const d=o?`<div class="md-editor-doclink-card-thumb"><img src="${qe(o)}" alt="" loading="lazy" /></div>`:"";return`<a class="md-editor-doclink-card" href="${qe(a)}" target="_blank" rel="noopener noreferrer">`+d+`<div class="md-editor-doclink-card-body"><div class="md-editor-doclink-card-title">${Ae(s)}</div>`+(c?`<div class="md-editor-doclink-card-desc">${Ae(c)}</div>`:"")+`<div class="md-editor-doclink-card-url">${Ae(l)}</div></div></a>`}}function Ae(n){return n.replace(/&/g,"&").replace(/</g,"<").replace(/>/g,">").replace(/"/g,""")}function qe(n){return n.replace(/"/g,""").replace(/</g,"<")}class st{constructor(e,t){var r,a;this.lineNumbersCompartment=new Te.Compartment,this.customKeymapCompartment=new Te.Compartment,this.mention=null,this.docLink=null,this.blurTimeout=null,this.destroyed=!1,this.onImageFile=t.onImageFile;const i=[Se.history(),J.keymap.of([
|
|
5
|
+
`).map(E=>E.trim()).filter(E=>E.length>0);let h="",p="";const m=[];for(const E of d)if(E.startsWith("# "))h=E.slice(2).trim();else if(E.startsWith("){const N=E.match(/^!\[thumbnail\]\((.+)\)$/);N&&(p=N[1]??"")}else m.push(E);const b=e.push("doclink_block","div",0);return b.content=JSON.stringify({url:o,title:h,thumbnail:p,description:m.join(" ")}),e.line=c+1,!0}),n.renderer.rules.doclink_block=(e,t)=>{const i=e[t];if(!i)return"";let r;try{r=JSON.parse(i.content)}catch{return""}const{url:a,title:s,thumbnail:o,description:c}=r;let l=a;try{l=new URL(a).hostname}catch{}const d=o?`<div class="md-editor-doclink-card-thumb"><img src="${qe(o)}" alt="" loading="lazy" /></div>`:"";return`<a class="md-editor-doclink-card" href="${qe(a)}" target="_blank" rel="noopener noreferrer">`+d+`<div class="md-editor-doclink-card-body"><div class="md-editor-doclink-card-title">${Ae(s)}</div>`+(c?`<div class="md-editor-doclink-card-desc">${Ae(c)}</div>`:"")+`<div class="md-editor-doclink-card-url">${Ae(l)}</div></div></a>`}}function Ae(n){return n.replace(/&/g,"&").replace(/</g,"<").replace(/>/g,">").replace(/"/g,""")}function qe(n){return n.replace(/"/g,""").replace(/</g,"<")}class st{constructor(e,t){var r,a;this.lineNumbersCompartment=new Te.Compartment,this.customKeymapCompartment=new Te.Compartment,this.mention=null,this.docLink=null,this.blurTimeout=null,this.destroyed=!1,this.onImageFile=t.onImageFile;const i=[Se.history(),J.keymap.of([{key:"Enter",run:()=>{var s,o;return(s=this.mention)!=null&&s.isOpen()?(this.mention.selectCurrent(),!0):(o=this.docLink)!=null&&o.isOpen()?(this.docLink.selectCurrent(),!0):!1}},{key:"ArrowDown",run:()=>{var s,o,c,l;if((s=this.mention)!=null&&s.isOpen()||(o=this.docLink)!=null&&o.isOpen()){const d=new KeyboardEvent("keydown",{key:"ArrowDown"});return(c=this.mention)==null||c.handleKey(d),(l=this.docLink)==null||l.handleKey(d),!0}return!1}},{key:"ArrowUp",run:()=>{var s,o,c,l;if((s=this.mention)!=null&&s.isOpen()||(o=this.docLink)!=null&&o.isOpen()){const d=new KeyboardEvent("keydown",{key:"ArrowUp"});return(c=this.mention)==null||c.handleKey(d),(l=this.docLink)==null||l.handleKey(d),!0}return!1}},{key:"Escape",run:()=>{var s,o;return(s=this.mention)!=null&&s.isOpen()?(this.mention.hide(),!0):(o=this.docLink)!=null&&o.isOpen()?(this.docLink.hide(),!0):!1}},...Se.defaultKeymap,...Se.historyKeymap]),this.customKeymapCompartment.of([]),cn.markdown({codeLanguages:ln.languages}),J.EditorView.lineWrapping,this.lineNumbersCompartment.of(t.lineNumbers?J.lineNumbers():[]),J.EditorView.updateListener.of(s=>{var o,c,l;s.docChanged&&((o=t.onChange)==null||o.call(t,s.state.doc.toString())),(s.docChanged||s.selectionSet)&&((c=this.mention)==null||c.update(),(l=this.docLink)==null||l.update())}),J.EditorView.domEventHandlers({paste:s=>this.handlePaste(s),drop:s=>this.handleDrop(s),blur:()=>{this.blurTimeout=window.setTimeout(()=>{var s,o;this.blurTimeout=null,!this.destroyed&&(this.view.dom.contains(document.activeElement)||((s=this.mention)==null||s.hide(),(o=this.docLink)==null||o.hide()))},150)},keydown:s=>{var o,c;return!!((o=this.mention)!=null&&o.handleKey(s)||(c=this.docLink)!=null&&c.handleKey(s))}})];this.view=new J.EditorView({state:Te.EditorState.create({doc:t.value??"",extensions:i}),parent:e}),(r=t.mention)!=null&&r.onMentionSearch&&(this.mention=new un(this.view,t.mention)),(a=t.docLink)!=null&&a.onDocSearch&&(this.docLink=new fn(this.view,t.docLink))}handlePaste(e){var i,r;const t=(i=e.clipboardData)==null?void 0:i.items;if(!t)return!1;for(const a of Array.from(t))if(a.type.startsWith("image/")){const s=a.getAsFile();if(s)return(r=this.onImageFile)==null||r.call(this,s),e.preventDefault(),!0}return!1}handleDrop(e){var i,r;const t=(i=e.dataTransfer)==null?void 0:i.files;if(!t||t.length===0)return!1;for(const a of Array.from(t))if(a.type.startsWith("image/"))return(r=this.onImageFile)==null||r.call(this,a),e.preventDefault(),!0;return!1}setKeymap(e){this.view.dispatch({effects:this.customKeymapCompartment.reconfigure(J.keymap.of(e))})}getValue(){return this.view.state.doc.toString()}setValue(e){this.view.dispatch({changes:{from:0,to:this.view.state.doc.length,insert:e}})}setLineNumbers(e){this.view.dispatch({effects:this.lineNumbersCompartment.reconfigure(e?J.lineNumbers():[])})}insertAtCursor(e){const{from:t,to:i}=this.view.state.selection.main;this.view.dispatch({changes:{from:t,to:i,insert:e},selection:{anchor:t+e.length}}),this.view.focus()}wrapSelection(e,t,i){const{from:r,to:a}=this.view.state.selection.main,o=this.view.state.doc.sliceString(r,a)||i,c=`${e}${o}${t}`;this.view.dispatch({changes:{from:r,to:a,insert:c},selection:{anchor:r+e.length,head:r+e.length+o.length}}),this.view.focus()}getSelection(){const{from:e,to:t}=this.view.state.selection.main;return this.view.state.doc.sliceString(e,t)}wrapLines(e,t){const{from:i,to:r}=this.view.state.selection.main;let a=this.view.state.doc.sliceString(i,r);if(a){const o=a.split(`
|
|
6
6
|
`).map(c=>`${e}${c}`).join(`
|
|
7
7
|
`);this.view.dispatch({changes:{from:i,to:r,insert:o},selection:{anchor:i,head:i+o.length}})}else{const s=this.view.state.doc.lineAt(i);a=s.text;const o=s.from,c=s.to,d=(a?a.split(`
|
|
8
8
|
`):[t]).map(h=>`${e}${h}`).join(`
|
|
@@ -30,13 +30,13 @@ ${r}
|
|
|
30
30
|
${s}
|
|
31
31
|
`:`${i}
|
|
32
32
|
${r}
|
|
33
|
-
`}showHelpPopup(e){var a;(a=this.activePopup)==null||a.destroy();const t=new V(e,{title:"
|
|
33
|
+
`}showHelpPopup(e){var a;(a=this.activePopup)==null||a.destroy();const t=new V(e,{title:"Help",theme:this.theme,fields:[],onSubmit:()=>{}}),i=t.el.querySelector(".md-editor-popup-body");if(i){const s=[{label:"Task list",hint:"- [ ] checked / - [x] unchecked"},{label:"@mention",hint:"Type @ then pick a user from the dropdown"},{label:"Document link",hint:"Type / then pick a document"},{label:"Math formula",hint:"$E=mc^2$ inline / $$ block $$"},{label:"Mindmap",hint:"```mindmap fenced block with # headings"},{label:"Callout",hint:":::warning Title ...content... :::"}];i.innerHTML='<div class="md-editor-help-section-title">Keyboard Shortcuts</div><div class="md-editor-help-list">'+this.shortcuts.map(o=>`<div class="md-editor-help-row">
|
|
34
34
|
<span class="md-editor-help-desc">${o.description}</span>
|
|
35
35
|
<span class="md-editor-help-keys">${Yt(o.key)}</span>
|
|
36
36
|
</div>`).join("")+'</div><div class="md-editor-help-section-title">Syntax</div><div class="md-editor-help-list">'+s.map(o=>`<div class="md-editor-help-row">
|
|
37
37
|
<span class="md-editor-help-desc">${o.label}</span>
|
|
38
38
|
<span class="md-editor-help-syntax">${o.hint}</span>
|
|
39
|
-
</div>`).join("")+"</div>"}const r=t.el.querySelector(".md-editor-popup-footer");r&&(r.innerHTML='<a href="https://www.powerduck.com" target="_blank" rel="noopener noreferrer" class="md-editor-help-link">powerduck.com</a>',r.style.justifyContent="center"),this.activePopup=t,this.activePopup.show()}async handleImageFile(e){if(this.onImageUpload)try{const t=await this.onImageUpload(e);t&&this.codeEditor.insertAtCursor(Ce(t,e.name.replace(/\.[^.]+$/,"")||"image"))}catch{}}showTipsPopup(e){var s;(s=this.activePopup)==null||s.destroy();const t=[{key:"notice",label:"Notice",color:"#0969da"},{key:"info",label:"Info",color:"#0969da"},{key:"tip",label:"Tip",color:"#1a7f37"},{key:"success",label:"Success",color:"#1a7f37"},{key:"warning",label:"Warning",color:"#9a6700"},{key:"danger",label:"Danger",color:"#cf222e"}],i=new V(e,{title:"Insert callout",theme:this.theme,fields:[],onSubmit:()=>{}}),r=i.el.querySelector(".md-editor-popup-body");if(r){const o=document.createElement("div");o.className="md-editor-tips-grid";for(const c of t){const l=document.createElement("button");l.type="button",l.className="md-editor-tips-option",l.innerHTML=`<span class="md-editor-tips-dot" style="background:${c.color}"></span><span>${c.label}</span>`,l.addEventListener("click",()=>{this.codeEditor.insertAtCursor(`:::${c.key} ${c.label}
|
|
39
|
+
</div>`).join("")+"</div>"}const r=t.el.querySelector(".md-editor-popup-footer");r&&(r.innerHTML='<span class="md-editor-help-brand-text">PowerDuck Markdown Editor</span><a href="https://www.powerduck.com" target="_blank" rel="noopener noreferrer" class="md-editor-help-link">powerduck.com</a>',r.style.justifyContent="space-between",r.style.alignItems="center"),this.activePopup=t,this.activePopup.show()}async handleImageFile(e){if(this.onImageUpload)try{const t=await this.onImageUpload(e);t&&this.codeEditor.insertAtCursor(Ce(t,e.name.replace(/\.[^.]+$/,"")||"image"))}catch{}}showTipsPopup(e){var s;(s=this.activePopup)==null||s.destroy();const t=[{key:"notice",label:"Notice",color:"#0969da"},{key:"info",label:"Info",color:"#0969da"},{key:"tip",label:"Tip",color:"#1a7f37"},{key:"success",label:"Success",color:"#1a7f37"},{key:"warning",label:"Warning",color:"#9a6700"},{key:"danger",label:"Danger",color:"#cf222e"}],i=new V(e,{title:"Insert callout",theme:this.theme,fields:[],onSubmit:()=>{}}),r=i.el.querySelector(".md-editor-popup-body");if(r){const o=document.createElement("div");o.className="md-editor-tips-grid";for(const c of t){const l=document.createElement("button");l.type="button",l.className="md-editor-tips-option",l.innerHTML=`<span class="md-editor-tips-dot" style="background:${c.color}"></span><span>${c.label}</span>`,l.addEventListener("click",()=>{this.codeEditor.insertAtCursor(`:::${c.key} ${c.label}
|
|
40
40
|
|
|
41
41
|
Your ${c.key.toLowerCase()} content here.
|
|
42
42
|
|