@aiquants/markdown-explorer 0.20.0 → 0.21.0
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/CHANGELOG.md +24 -0
- package/README.md +5 -4
- package/dist/EditSurface-Bwl4S1vQ.cjs +7 -0
- package/dist/EditSurface-rKoy_ndP.js +880 -0
- package/dist/{ExplorerSplit-BAYa3WO3.js → ExplorerSplit-3WMRDA7G.js} +1 -1
- package/dist/{ExplorerSplit-DkE46pS0.cjs → ExplorerSplit-CdqEKz-I.cjs} +1 -1
- package/dist/{MultiLayout-DsxYURra.cjs → MultiLayout-BEg1X8Jx.cjs} +1 -1
- package/dist/{MultiLayout-BkhnkQs5.js → MultiLayout-Gf1xT-Le.js} +2 -2
- package/dist/{NativeScrollBar-CcNqDlO6.js → NativeScrollBar-0cpp2OIu.js} +1 -1
- package/dist/{NativeScrollBar-B57yWQVG.cjs → NativeScrollBar-idIQsvCv.cjs} +1 -1
- package/dist/{TreeLayout-DCGqVwt9.cjs → TreeLayout-DJY_WqzP.cjs} +1 -1
- package/dist/{TreeLayout-Cf2PzZEI.js → TreeLayout-DzZCgMTt.js} +2 -2
- package/dist/{index-DiLkl4dC.js → index-CJgHGeZs.js} +4 -4
- package/dist/{index-Bdve5EFS.cjs → index-bMaSoQTe.cjs} +3 -3
- package/dist/index.cjs +1 -1
- package/dist/index.js +1 -1
- package/dist/{merge-BIXNcrea.js → merge-BOcofJN5.js} +1 -1
- package/dist/{merge-DSfJceoQ.cjs → merge-BVk6pvO_.cjs} +1 -1
- package/docs/behavior/security.md +17 -7
- package/package.json +3 -3
- package/dist/EditSurface-D1yklbWu.js +0 -833
- package/dist/EditSurface-GxmyUeGE.cjs +0 -3
|
@@ -1,3 +0,0 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const R=require("react/jsx-runtime"),f=require("react"),S=require("./index-Bdve5EFS.cjs"),W=require("@codemirror/commands"),ce=require("@codemirror/lang-markdown"),Te=require("@codemirror/language"),ue=require("@codemirror/search"),$=require("@codemirror/state"),_=require("@codemirror/view"),P=require("@lezer/highlight"),_e=require("@aiquants/markdown"),oe=require("@aiquants/markdown/source-lines"),Y=require("./dataRoute-BONacTPi.cjs"),ie=require("./sanitizePolicy-gEXeavb-.cjs"),Pe=require("isomorphic-dompurify"),ee=require("@aiquants/resize-panels"),U=require("./location-BVEN_Ihk.cjs"),de=require("./useSplitPersistence-Ba-v64n-.cjs"),ke={history:W.historyField},Ne=Te.HighlightStyle.define([{tag:[P.tags.heading1,P.tags.heading2,P.tags.heading3,P.tags.heading4,P.tags.heading5,P.tags.heading6,P.tags.heading],color:"var(--aqmx-syntax-heading)",fontWeight:"700"},{tag:P.tags.strong,fontWeight:"700"},{tag:P.tags.emphasis,fontStyle:"italic"},{tag:P.tags.strikethrough,textDecoration:"line-through"},{tag:P.tags.link,color:"var(--aqmx-syntax-link)",textDecoration:"underline"},{tag:P.tags.url,color:"var(--aqmx-syntax-url)"},{tag:P.tags.monospace,color:"var(--aqmx-syntax-code)"},{tag:P.tags.quote,color:"var(--aqmx-syntax-quote)"},{tag:[P.tags.processingInstruction,P.tags.contentSeparator,P.tags.labelName,P.tags.meta],color:"var(--aqmx-syntax-mark)"}]),De=_.EditorView.theme({"&":{height:"100%",color:"var(--aqmx-text)",backgroundColor:"var(--aqmx-surface)",fontSize:"var(--aqmx-font-size-title)"},"&.cm-focused":{outline:"none"},".cm-scroller":{fontFamily:'ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, "Liberation Mono", monospace',lineHeight:"var(--aqmx-line-height-prose)"},".cm-content":{caretColor:"var(--aqmx-text)",paddingBlock:"var(--aqmx-space-6)"},".cm-line":{paddingInline:"var(--aqmx-space-6)"},"&.cm-focused .cm-cursor":{borderLeftColor:"var(--aqmx-text)"},"&.cm-focused > .cm-scroller > .cm-selectionLayer .cm-selectionBackground, .cm-selectionBackground, .cm-content ::selection":{backgroundColor:"var(--aqmx-editor-selection)"},".cm-activeLine":{backgroundColor:"var(--aqmx-editor-active-line)"},".cm-gutters":{backgroundColor:"var(--aqmx-surface-raised)",color:"var(--aqmx-editor-gutter)",borderInlineEnd:"var(--aqmx-space-1) solid var(--aqmx-border)"},".cm-activeLineGutter":{backgroundColor:"var(--aqmx-editor-active-line)"},".cm-searchMatch":{outline:"var(--aqmx-space-1) solid var(--aqmx-border-strong)"},".cm-panels":{backgroundColor:"var(--aqmx-surface-raised)",color:"var(--aqmx-text)"}}),Le="data-aqmx-conflict-actions",Ve=_.Decoration.line({attributes:{"data-aqmx-conflict-line":"marker"}}),Be=_.Decoration.line({attributes:{"data-aqmx-conflict-line":"ours"}}),je=_.Decoration.line({attributes:{"data-aqmx-conflict-line":"theirs"}});class He extends _.WidgetType{constructor(t,r){super(),this.index=t,this.labels=r}index;labels;eq(t){return t.index===this.index&&t.labels===this.labels}toDOM(t){const r=document.createElement("div");r.setAttribute(Le,String(this.index));for(const[n,l]of[["ours",this.labels.useOurs],["theirs",this.labels.useTheirs],["both",this.labels.keepBoth]]){const i=document.createElement("button");i.type="button",i.textContent=l,i.setAttribute("data-aqmx-conflict-choice",n),i.addEventListener("mousedown",a=>a.preventDefault()),i.addEventListener("click",()=>{const a=Q(t.state.doc)[this.index];a!==void 0&&(Re(t,a,n),t.focus())}),r.append(i)}return r}ignoreEvent(){return!0}}const Q=e=>S.findConflictsInLines(e.iterLines()),fe=(e,t)=>{const r=new $.RangeSetBuilder;return Q(e).forEach((n,l)=>{const i=e.line(n.start);r.add(i.from,i.from,_.Decoration.widget({widget:new He(l,t),block:!0,side:-1}));for(let a=n.start;a<=n.end;a+=1){const o=e.line(a).from,m=a===n.start||a===n.separator||a===n.end?Ve:a<n.separator?Be:je;r.add(o,o,m)}}),r.finish()},ze=e=>$.StateField.define({create:t=>fe(t.doc,e),update:(t,r)=>r.docChanged?fe(r.state.doc,e):t,provide:t=>_.EditorView.decorations.from(t)}),Re=(e,t,r)=>{const{doc:n}=e.state,l=(s,x)=>{const c=[];for(let h=s;h<=x;h+=1)c.push(n.line(h).text);return c},i=l(t.start+1,t.separator-1),a=l(t.separator+1,t.end-1),o=r==="ours"?i:r==="theirs"?a:[...i,...a];let m=n.line(t.start).from,d=n.line(t.end).to;o.length===0&&(t.end<n.lines?d+=1:t.start>1&&(m-=1));const g=o.join(`
|
|
2
|
-
`);e.dispatch({changes:{from:m,to:d,insert:g},selection:$.EditorSelection.cursor(m),scrollIntoView:!0,userEvent:"input.resolve"})},Fe="input.preview",Ge=/^ {0,3}(`{3,}|~{3,})/u,Ue=/^ {0,3}(`{3,}|~{3,})[ \t]*$/u,We=(e,t,r)=>{if(!Number.isInteger(t)||t<1||t>e.lines)return null;const n=Ge.exec(e.line(t).text)?.[1];if(n===void 0)return null;if(t===e.lines)return r===""?{from:e.length,to:e.length,lineBreak:!0}:null;const l=e.line(t+1).from,i=l+r.length;if(i>e.length||e.sliceString(l,i)!==r)return null;if(i===e.length)return{from:l,to:i,lineBreak:!1};const a=e.lineAt(i),o=a.from===i?Ue.exec(a.text)?.[1]:void 0;return o!==void 0&&o[0]===n[0]&&o.length>=n.length?{from:l,to:i,lineBreak:!1}:null},Xe=({parent:e,scroll:t,text:r,saved:n,labels:l,onChange:i,onGeometry:a,onSave:o})=>{let m=!1;const d=[_.EditorView.scrollHandler.of((c,h,E)=>($e(c,h.head,E,t),!0)),_.lineNumbers(),_.highlightActiveLineGutter(),W.history(),_.drawSelection(),_.highlightActiveLine(),ue.highlightSelectionMatches(),_.EditorView.lineWrapping,ce.markdown({base:ce.markdownLanguage}),Te.syntaxHighlighting(Ne),ze(l),_.keymap.of([{key:"Mod-s",preventDefault:!0,run:()=>(o(),!0)},...ue.searchKeymap,...W.historyKeymap,...W.defaultKeymap]),_.EditorView.contentAttributes.of({"aria-label":l.editor,"aria-multiline":"true"}),_.EditorView.updateListener.of(c=>{c.docChanged&&i(),(c.docChanged||c.heightChanged||c.geometryChanged)&&a(c.docChanged&&!m)}),De],g=Je(n,d)??$.EditorState.create({doc:r,extensions:d}),s=new _.EditorView({state:g,parent:e}),x=c=>{m=!0;try{c()}finally{m=!1}};return{text:()=>s.state.doc.toString(),replaceText:c=>{const h=s.state.doc.toString();if(h===c)return;let E=0;const p=Math.min(h.length,c.length);for(;E<p&&h.charCodeAt(E)===c.charCodeAt(E);)E+=1;let b=0;for(;b<p-E&&h.charCodeAt(h.length-1-b)===c.charCodeAt(c.length-1-b);)b+=1;s.dispatch({changes:{from:E,to:h.length-b,insert:c.slice(E,c.length-b)},userEvent:"input.replace"})},replaceBody:(c,h,E)=>{const p=We(s.state.doc,c,h);if(p===null)return!1;const b=p.lineBreak?`
|
|
3
|
-
${E}`:E;return x(()=>s.dispatch({changes:{from:p.from,to:p.to,insert:b},userEvent:Fe,annotations:W.isolateHistory.of("full")})),!0},undo:()=>x(()=>W.undo(s)),redo:()=>x(()=>W.redo(s)),reveal:({line:c,column:h},E)=>{const{doc:p}=s.state,b=p.line(Math.min(Math.max(c,1),p.lines)),k=Math.min(b.from+h,b.to);s.dispatch({selection:$.EditorSelection.cursor(k),effects:me(s,k,E,t)})},showLine:c=>{const{doc:h}=s.state,E=h.line(Math.min(Math.max(c,1),h.lines)).from;s.dispatch({effects:me(s,E,null,t)})},topLine:()=>{const c=t.scrollerOf(s.scrollDOM),h=S.visibleTopOf(c,s.contentDOM),E=S.frameTopOf(c)+c.clientHeight,{documentTop:p}=s,{doc:b}=s.state;let k=s.lineBlockAtHeight(Math.max(h-p,0));for(;b.lineAt(k.from).text.trim()===""&&k.to<b.length;){const C=s.lineBlockAt(k.to+1);if(p+C.top>=E)break;k=C}const I=s.coordsAtPos(k.from),N=I===null?p+k.top:I.top;return{line:s.state.doc.lineAt(k.from).number,clientTop:N,offset:N-h}},lineSpans:c=>{const h=s.scrollDOM,E=h.scrollTop+s.documentTop-S.visibleTopOf(h,s.contentDOM),{doc:p}=s.state,b=k=>p.line(Math.min(Math.max(k,1),p.lines)).from;return c.map(({first:k,last:I})=>({top:E+s.lineBlockAt(b(k)).top,bottom:E+s.lineBlockAt(b(I)).bottom}))},scrollElement:s.scrollDOM,focus:()=>s.focus(),conflicts:()=>Q(s.state.doc),resolveAtCursor:c=>{const h=Q(s.state.doc),E=s.state.doc.lineAt(s.state.selection.main.head).number,p=h.find(b=>b.end>=E);return p===void 0?!1:(Re(s,p,c),!0)},nextConflict:()=>{const c=Q(s.state.doc);if(c[0]===void 0)return!1;const E=s.state.doc.lineAt(s.state.selection.main.head).number,p=c.findIndex(N=>N.start>E),b=p<0?0:p,k=s.state.doc.line(c[b].start).from;s.dispatch({selection:$.EditorSelection.cursor(k),effects:_.EditorView.scrollIntoView(k,{y:"center"})});const I=s.contentDOM.querySelector(`[${Le}="${String(b)}"] button`);return I instanceof HTMLElement?I.focus({preventScroll:!0}):s.focus(),!0},save:()=>s.state.toJSON(ke),destroy:()=>s.destroy()}},me=(e,t,r,n)=>{const l=n.scrollerOf(e.scrollDOM),i=(r===null?S.visibleTopOf(l,e.contentDOM):r)-S.frameTopOf(l);return _.EditorView.scrollIntoView(t,{y:"start",yMargin:i})},$e=(e,t,r,n)=>{const l=Ke(e,t),i=n.scrollerOf(e.scrollDOM),a=S.frameTopOf(i),o=S.visibleTopOf(i,e.contentDOM),m=a+i.clientHeight;let d=0;switch(r.y){case"start":d=l.top-(a+r.yMargin);break;case"center":d=(l.top+l.bottom)/2-(o+m)/2;break;case"end":d=l.bottom-(m-r.yMargin);break;default:l.top<o+r.yMargin?d=l.top-(o+r.yMargin):l.bottom>m-r.yMargin&&(d=Math.min(l.bottom-(m-r.yMargin),l.top-(o+r.yMargin)))}if(d!==0&&(i.scrollTop+=d),r.y==="start")return;const g=Ye(e,t);g!==null&&n.reveal(g)},Ke=(e,t)=>{const{node:r,offset:n}=e.domAtPos(t);if(r.nodeType===Node.TEXT_NODE){const a=r.ownerDocument?.createRange();if(a!==void 0){a.setStart(r,n),a.setEnd(r,n);const[o]=a.getClientRects();if(o!==void 0&&o.height>0)return{top:o.top,bottom:o.bottom}}}const l=e.lineBlockAt(t),i=e.documentTop+l.top;return{top:i,bottom:i+l.height}},Ye=(e,t)=>{const{node:r}=e.domAtPos(t);for(let n=r instanceof Element?r:r.parentElement;n!==null;n=n.parentElement)if(n.parentElement===e.contentDOM)return n;return null},Je=(e,t)=>{if(e==null)return null;try{return $.EditorState.fromJSON(e,{extensions:t},ke)}catch{return null}},Qe=610,Ze=987,pe=Object.freeze({kind:"unavailable"}),et="true",tt="false",rt=e=>e===et?!0:e===tt?!1:null,nt=e=>{const t=new DOMParser().parseFromString(e,"text/html"),r=[];for(const n of t.querySelectorAll("img[data-asset-src], a[data-doc-path]")){const l=n.localName==="img",i=n.getAttribute(l?"data-asset-src":"data-doc-path");i!==null&&r.push({target:{kind:l?"asset":"doc",path:i},exists:rt(n.getAttribute(l?"data-asset-exists":"data-doc-exists"))})}return r},ot=({path:e,instant:t,preview:r,initial:n,onView:l})=>{const i=new AbortController,a={asset:new Map,doc:new Map};let o=n,m=null,d=!1,g=!1,s=null,x=null,c=[],h,E=!1,p,b=null;const k=y=>{y.parsed===o.parsed&&y.failure===o.failure&&y.busy===o.busy&&y.tier===o.tier||(o=y,l(y))},I=(y,T)=>{d||(d=!0,console.error(`[markdown-explorer] ${y}`,...T===void 0?[]:[T]))},N=y=>{if(t!==null)for(const{target:T,exists:u}of t.marksOf(y))a[T.kind].set(T.path,u)},C=()=>{clearTimeout(p),p=void 0,b?.abort(),b=null},j=()=>{x=null,c=[],clearTimeout(h),h=void 0},B=(y,T)=>{k({parsed:o.parsed,failure:y,busy:!1,tier:T})},G=()=>{clearTimeout(h),h=c.length===0?void 0:setTimeout(q,Ze)},w=()=>{if(t===null||x===null)return;const y=x,T=[],u={asset:new Set,doc:new Set};let L;try{L=t.sanitize(y.html,A=>{const z=a[A.kind].get(A.path);return z===void 0&&!u[A.kind].has(A.path)&&(u[A.kind].add(A.path),T.push(A)),z??void 0})}catch(A){j(),A instanceof ie.HtmlNestingLimitError?B(Y.failureOf("too-large"),"instant"):(I("the draft sanitizer failed",A),B(Y.failureOf("failed"),"instant"));return}c=T;const M=o.parsed!==null&&o.parsed.htmlContent===L&&o.parsed.headings===y.headings?o.parsed:{htmlContent:L,headings:y.headings};k({parsed:M,failure:null,busy:!1,tier:"instant"}),G()},q=()=>{if(h=void 0,E||x===null||c.length===0)return;E=!0;const y=c;r(e,x.text,i.signal).then(T=>{if(E=!1,!(i.signal.aborted||!("kind"in T))){T.kind==="preview"&&N(T.htmlContent);for(const u of y)a[u.kind].has(u.path)||a[u.kind].set(u.path,null);w()}})},O=y=>{C(),j(),k({...o,busy:!0,tier:"server"});const T=new AbortController;b=T,p=setTimeout(()=>{p=void 0,r(e,y,T.signal).then(u=>{if(!(T.signal.aborted||!("kind"in u))){if(b=null,u.kind==="failure"){B(u.failure,"server");return}N(u.htmlContent),k({parsed:{htmlContent:u.htmlContent,headings:u.headings},failure:null,busy:!1,tier:"server"})}})},Qe)},v=async(y,T)=>{let u;try{u=await y.convert({path:e,text:T},i.signal)}catch(M){return i.signal.aborted||I("the draft converter rejected a conversion, which its contract allows only once the signal aborted",M),pe}const L=Y.draftAnswerViolation(u);return L===null?u:(I(`the draft converter answered outside its contract: ${L}`),pe)},D=(y,T)=>{switch(T.kind){case"preview":if(C(),T.headings.length>Y.EXPLORER_MAX_HEADINGS){j(),B(Y.failureOf("too-large"),"instant");return}x={text:y,html:T.htmlContent,headings:T.headings.map(({text:u,depth:L,id:M})=>({text:u,depth:L,id:M}))},w();return;case"failure":C(),j(),B(Y.failureOf(T.failure.reason,T.failure.code),"instant");return;case"unavailable":O(y);return}},H=async y=>{g=!0;try{for(;s!==null&&!i.signal.aborted;){const T=s;s=null;const u=await v(y,T);if(i.signal.aborted)return;D(T,u)}}finally{g=!1}};return t!==null&&t.seed!==null&&N(t.seed),{update:y=>{if(!(i.signal.aborted||y===m)){if(m=y,clearTimeout(h),h=void 0,t===null){O(y);return}s=y,g||H(t.converter)}},dispose:()=>{i.abort(),s=null,clearTimeout(h),h=void 0,C()}}},it=(e,t)=>ie.createHtmlSanitizer(Pe(window),e,t,ie.MAX_HTML_NESTING_DEPTH),st=(e,t)=>e.flatMap((r,n)=>{const l=t[n];return l===void 0?[]:[{editor:l.top,preview:r.top},{editor:l.bottom,preview:r.bottom}]}),X=(e,t)=>{const r=e[t];if(r===void 0)throw new RangeError(`[markdown-explorer] a scroll map index must be in 0..${String(e.length-1)}; got ${String(t)}`);return r},lt=e=>{const t=[];for(const n of e){let l=0,i=t.length;for(;l<i;){const a=l+i>>1;X(t,a).pair.preview<=n.preview?l=a+1:i=a}t[l]={pair:n,before:l===0?void 0:X(t,l-1)}}const r=[];for(let n=t.at(-1);n!==void 0;n=n.before)r.push(n.pair);return r.reverse()},at=(e,t)=>{const r=Math.max(t.editorMax,0),n=Math.max(t.previewMax,0),l=r-t.editorViewport,i=n-t.previewViewport,a=e.filter(d=>d.editor>0&&d.editor<l&&d.preview>0&&d.preview<i).sort((d,g)=>d.editor-g.editor||d.preview-g.preview);let o={editor:0,preview:0};const m=[o];for(const d of lt(a))d.editor>o.editor&&d.preview>o.preview&&(m.push(d),o=d);return m.push({editor:r,preview:n}),{toPreview:d=>he(m,"editor","preview",d),toEditor:d=>he(m,"preview","editor",d)}},he=(e,t,r,n)=>{const l=X(e,0),i=X(e,e.length-1);if(Number.isNaN(n)||n<=l[t])return l[r];if(n>=i[t])return i[r];let a=0,o=e.length-1;for(;o-a>1;){const g=a+o>>1;X(e,g)[t]<=n?a=g:o=g}const m=X(e,a),d=X(e,o);return m[r]+(d[r]-m[r])*(n-m[t])/(d[t]-m[t])},ct=610,ge=89,ve=.5,ut=({editor:e,preview:t,previewBlocks:r,editorSpans:n,requestFrame:l,cancelFrame:i,now:a})=>{let o="editor",m=0,d=null,g=null,s=!1,x=null,c=null;const h={editor:0,preview:0},E={editor:null,preview:null};let p=null,b=()=>{};const k=v=>{d!==null&&v!==d||(o=v,m=a()+ct)},I=(v,D)=>{v!==o&&d===null&&a()>=m&&!N(v,D)&&(o=v),w()},N=(v,D)=>{const H=E[v];return a()<h[v]||H!==null&&Math.abs(D.position()-H)<ve},C=e.onScroll(()=>I("editor",e)),j=e.onIntent(()=>k("editor")),B=()=>{const v=t();return v!==p&&(b(),p=v,b=v===null?()=>{}:G(v)),v},G=v=>{const D=v.onScroll(()=>I("preview",v)),H=v.onIntent(()=>k("preview"));return()=>{D(),H()}},w=()=>{!s&&g===null&&(g=l(q))},q=()=>{g=null;const v=B();v!==null&&(x??=r(),c??=at(st(x,n(x)),{editorMax:e.max(),previewMax:v.max(),editorViewport:e.viewport(),previewViewport:v.viewport()}),o==="editor"?O(v,"preview",c.toPreview(e.position())):O(e,"editor",c.toEditor(v.position())))},O=(v,D,H)=>{if(Math.abs(v.position()-H)<ve){E[D]=v.position();return}h[D]=a()+ge,E[D]=v.scrollTo(H)};return w(),{editorChanged:v=>{c=null,v&&k("editor"),w()},previewChanged:()=>{x=null,c=null,h.preview=a()+ge,w()},showAlone:v=>{d=v,!(v===null||v===o)&&(o=v,m=0,w())},dispose:()=>{s=!0,g!==null&&i(g),g=null,C(),j(),b()}}},Ee=Object.freeze(["wheel","pointerdown","keydown","focusin"]),qe=e=>({position:()=>e.scrollTop,max:()=>Math.max(e.scrollHeight-e.clientHeight,0),viewport:()=>e.clientHeight,scrollTo:t=>(e.scrollTo({top:t,behavior:"instant"}),e.scrollTop),onScroll:t=>(e.addEventListener("scroll",t,{passive:!0}),()=>e.removeEventListener("scroll",t)),onIntent:t=>{for(const r of Ee)e.addEventListener(r,t,{capture:!0,passive:!0});return()=>{for(const r of Ee)e.removeEventListener(r,t,{capture:!0})}}}),J="[data-aqmd-scroll-container]",dt="[data-aqmd-content]",ft=`[${oe.SOURCE_LINES_ATTRIBUTE}]`,mt=Object.freeze(["p1","p2","p3","p4","p5","p6","p7","p8"]),Oe=(e,t)=>{const r=S.loadedOf(e.peek(t));return r!==null&&r.document.kind==="markdown"?{htmlContent:r.document.htmlContent,headings:r.document.headings}:null},pt=(e,t,r,n,l)=>{if(r===void 0)return null;let i;try{i=it(e,t)}catch(a){return console.error("[markdown-explorer] the draft sanitizer cannot run in this browser, so the server renders the preview",a),null}return{converter:r,sanitize:i,marksOf:nt,seed:Oe(n,l)?.htmlContent??null}},ht=e=>{const t=[];let r=null;for(const n of e.querySelectorAll(ft)){const l=oe.parseSourceLines(n.getAttribute(oe.SOURCE_LINES_ATTRIBUTE));if(l===null||n.getClientRects().length===0)continue;r??=e.scrollTop-S.visibleTopOf(e,n);const{top:i,bottom:a}=n.getBoundingClientRect();t.push({first:l.first,last:l.last,top:r+i,bottom:r+a})}return t},gt=({path:e,readDraft:t,viewer:r,spreadsheetEditor:n,concealed:l,onEditAt:i,onLayout:a,handleRef:o})=>{const{config:m,data:d,documents:g,draftConverter:s,labels:x}=S.useExplorer(),[c,h]=f.useState(()=>({parsed:Oe(g,e),failure:null,busy:!0,tier:s===void 0?"server":"instant"})),E=f.useRef(null),p=f.useRef(null),b=f.useRef(c);f.useLayoutEffect(()=>{b.current=c});const k=f.useEffectEvent(()=>t());f.useEffect(()=>{const w=pt(m.iframeHosts,m.dataPath,s,g,e),q=ot({path:e,instant:w,preview:d.preview,initial:b.current,onView:O=>f.startTransition(()=>h(O))});return p.current=q,q.update(k()),()=>{q.dispose(),p.current=null}},[m,d,g,s,e]);const I=f.useEffectEvent(()=>a());f.useLayoutEffect(()=>{c.parsed!==null&&I()},[c.parsed]);const N=c.parsed!==null;f.useLayoutEffect(()=>{const w=N?E.current?.querySelector(J):null;if(!(w instanceof HTMLElement))return;w.tabIndex=0;const q=new ResizeObserver(()=>I());q.observe(w);const O=w.querySelector(dt);return O!==null&&q.observe(O),()=>q.disconnect()},[N]);const C=f.useRef(null);f.useImperativeHandle(o,()=>({draft:w=>{p.current?.update(w)},scrollPort:()=>{const w=E.current?.querySelector(J);return w instanceof HTMLElement?(C.current?.scroller!==w&&(C.current={scroller:w,port:qe(w)}),C.current.port):null},blocks:()=>{const w=E.current?.querySelector(J);return w instanceof HTMLElement?ht(w):[]},focus:()=>{const w=E.current?.querySelector(J);w instanceof HTMLElement&&w.focus({preventScroll:!0})},anchor:w=>{const q=E.current,O=q?.querySelector(J);return q===null||!(O instanceof HTMLElement)?null:S.anchorOfView(q,w(O))},place:({line:w,clientTop:q},O)=>{const v=E.current,D=v===null?null:S.blockAtLine(v,w);D!==null&&(O(D).scrollTop+=S.lineTopIn(D,w)-q)}}),[]);const j=w=>{const q=E.current,{target:O}=w;if(i===void 0||q===null||!(O instanceof Element)||!S.isEditingDoubleClick(O))return;const v=S.anchorOfDoubleClick(O,q,q.ownerDocument.getSelection());v!==null&&i(v)},B=f.useMemo(()=>({...r,showToc:!1,showStyle:!1,showAlign:!1,renderScrollBar:void 0,spreadsheetEditor:n}),[r,n]),G=f.useMemo(()=>c.parsed===null?null:R.jsx("div",{className:"aqmx-document-viewer",children:R.jsx(_e.MarkdownViewer,{...B,parseResult:c.parsed,content:null})}),[B,c.parsed]);return R.jsxs("section",{ref:E,className:"aqmx-edit-preview","aria-label":x.previewRegion,"aria-busy":c.busy,inert:l!==null,hidden:l==="removed","data-aqmx-preview-tier":c.tier,"data-testid":"markdown-explorer-edit-preview",onDoubleClick:i===void 0?void 0:j,children:[c.failure===null?null:R.jsx("p",{className:"aqmx-callout aqmx-document-notice","data-aqmx-tone":"warning","data-testid":"markdown-explorer-edit-preview-failure",children:x.previewFailed(S.failureTitleOf(x,c.failure.reason))}),G??(c.failure===null?R.jsx("div",{className:"aqmx-skeleton","aria-hidden":"true","data-testid":"markdown-explorer-edit-preview-skeleton",children:mt.map(w=>R.jsx("span",{},w))}):null),c.busy?R.jsx("p",{className:"aqmx-sr-only",children:x.previewUpdating}):null]})},we="data-aqmx-edit-split",ne="--aqmx-edit-share",be="--aqmx-edit-gutter",vt="data-aqmx-edit-split-probe",Et="aqmx-edit-split",wt="aqmx-edit-editor",bt="aqmx-edit-preview",xe={value:U.EDIT_SPLIT_MIN_PERCENT,unit:"percentage"},Se={value:U.EDIT_SPLIT_MAX_PERCENT,unit:"percentage"},ye=e=>{if(e.length!==2)return null;const[t=0,r=0]=e,n=t+r;return n>0?t/n*100:null},te=e=>{const t=e.current?.parentElement;if(t==null)throw new Error("[markdown-explorer] the edit split is not mounted in its surface");return t},xt=(e,t,r)=>{const[n,l]=f.useState(null);return f.useLayoutEffect(()=>{const i=e.current,a=t.current;if(i===null||a===null)throw new Error("[markdown-explorer] the edit split is not mounted");const o={frame:i.getBoundingClientRect().width,measure:a.getBoundingClientRect().width},m=()=>{o.frame>0&&o.measure>0&&l(U.editSplitOrientation(o.frame,o.measure,r))};m();const d=new ResizeObserver(g=>{for(const s of g){const[x]=s.borderBoxSize;x!==void 0&&(s.target===i&&(o.frame=x.inlineSize),s.target===a&&(o.measure=x.inlineSize))}m()});return d.observe(i),d.observe(a),()=>d.disconnect()},[e,t,r]),n},St=({editorId:e})=>{const{edits:t,labels:r}=S.useExplorer(),n=f.useRef(null),l=f.useRef(null),i=de.useCoarsePointer()?U.COARSE_TARGET_PIXELS:U.FINE_TARGET_PIXELS,a=xt(n,l,i),[o]=f.useState(()=>t.editorShare());f.useLayoutEffect(()=>{const g=te(n);return g.style.setProperty(be,`${String(i)}px`),()=>{g.style.removeProperty(be)}},[i]),f.useLayoutEffect(()=>{const g=te(n);return g.style.setProperty(ne,`${String(o)}%`),()=>{g.style.removeProperty(ne)}},[o]),f.useLayoutEffect(()=>{if(a===null)return;const g=te(n);return g.setAttribute(we,a),()=>g.removeAttribute(we)},[a]);const m=de.useSplitPersistence(f.useCallback(g=>{const s=ye(g);s!==null&&t.setEditorShare(s)},[t])),d=f.useCallback((g,s)=>{const x=ye(g);x!==null&&te(n).style.setProperty(ne,`${String(x)}%`),m.layoutChanged(g,s)},[m]);return R.jsxs("div",{ref:n,className:"aqmx-edit-split","data-testid":"markdown-explorer-edit-split",children:[R.jsx("span",{ref:l,className:"aqmx-edit-split-probe","aria-hidden":"true",[vt]:""}),a===null?null:R.jsxs(ee.PanelGroup,{id:Et,direction:a==="beside"?"horizontal":"vertical",onLayout:d,className:"aqmx-edit-split-group",children:[R.jsx(ee.Panel,{id:wt,defaultSize:{value:o,unit:"percentage"},minSize:xe,maxSize:Se,contentOverflow:"hidden"}),R.jsx(ee.PanelResizeHandle,{thickness:i,indicator:{visible:!0,thickness:U.SPLIT_HANDLE_PIXELS.gripThickness,length:U.SPLIT_HANDLE_PIXELS.gripLength,barThickness:U.SPLIT_HANDLE_PIXELS.barThickness,barLength:U.SPLIT_HANDLE_PIXELS.barLength},title:r.editViewSplitResize,"aria-label":r.editViewSplitResize,"aria-controls":e,onDragging:m.draggingChanged,className:"aqmx-split-handle aqmx-edit-split-handle"}),R.jsx(ee.Panel,{id:bt,defaultSize:{value:100-o,unit:"percentage"},minSize:xe,maxSize:Se,contentOverflow:"hidden"})]})]})},Ie=144,yt="[data-aqmd-spreadsheet-fullscreen]",Tt=Object.freeze(["e1","e2","e3","e4","e5","e6","e7","e8"]),kt=e=>{if(typeof e!="object"||e===null)return!1;const{generation:t,topLine:r}=e;return"state"in e&&typeof t=="number"&&typeof r=="number"},Lt=e=>{e.stopPropagation()},Rt=e=>{const{path:t,name:r,session:n}=e,{edits:l,labels:i,announcer:a}=S.useExplorer(),{phase:o}=n,m=f.useCallback(()=>l.retry(t),[l,t]);switch(f.useEffect(()=>{o.kind==="failed"&&a.announce(i.documentFailed(r,S.failureTitleOf(i,o.failure.reason)))},[o,a,i,r]),o.kind){case"opening":return R.jsxs(R.Fragment,{children:[R.jsx("div",{className:"aqmx-skeleton","aria-hidden":"true","data-testid":"markdown-explorer-edit-skeleton",children:Tt.map(d=>R.jsx("span",{},d))}),R.jsx("p",{className:"aqmx-sr-only",children:i.editLoading})]});case"grant":return R.jsx("div",{className:"aqmx-edit-message",children:R.jsx(S.EditGrantNotice,{href:o.href,onRetry:m,testId:"markdown-explorer-edit-grant"})});case"failed":return R.jsx("div",{className:"aqmx-edit-message",children:R.jsx(S.FailureView,{failure:o.failure,alert:!1,onRetry:m,testId:"markdown-explorer-edit-failure"})});case"editing":return R.jsx(qt,{...e})}},qt=({path:e,name:t,session:r,layout:n,viewer:l,region:i,paneScroll:a})=>{const{edits:o,labels:m}=S.useExplorer(),d=f.useRef(null),g=f.useId(),s=f.useRef(null),x=f.useRef(null),c=f.useRef(null),h=n==="fill",E=S.useSideBySide(),p=S.editViewOf(r,E,h),b=p!=="editor"||r.previewPrepared,k=b&&h,I=f.useRef({layout:n,region:i,paneScroll:a});f.useLayoutEffect(()=>{I.current={layout:n,region:i,paneScroll:a}});const N=f.useRef(r.generation);f.useLayoutEffect(()=>{N.current=r.generation});const C=f.useRef({view:p,previewMounted:b});f.useLayoutEffect(()=>{C.current={view:p,previewMounted:b}});const j=f.useRef(null),B=f.useRef(null),G=f.useRef(!1),w=f.useMemo(()=>({editor:m.editorLabel(t),useOurs:m.conflictUseOurs,useTheirs:m.conflictUseTheirs,keepBoth:m.conflictKeepBoth}),[m,t]),q=f.useCallback(u=>{const{region:L,paneScroll:M}=I.current,A=L();return A===null?u:M.scrollerOf(A,u)},[]);f.useLayoutEffect(()=>{const u=d.current,L=o.draft(e);if(u===null||L===null)return;const M=o.editorState.get(e),A=kt(M)&&M.generation===N.current?M:null;let z=null,K=null;const re=()=>{z!==null&&clearTimeout(z),z=null,K!==null&&o.setDraft(e,K.text())},F=Xe({parent:u,scroll:{scrollerOf:V=>{const{layout:Z,region:Me,paneScroll:Ce}=I.current,ae=Me();return Z==="fill"||ae===null?V:Ce.scrollerOf(ae,V)},reveal:V=>I.current.paneScroll.reveal(V)},text:L,saved:A===null?void 0:A.state,labels:w,onGeometry:V=>c.current?.editorChanged(V),onChange:()=>{z??=setTimeout(re,Ie),C.current.previewMounted&&K!==null&&x.current?.draft(K.text())},onSave:()=>{o.save(e)}});K=F,s.current=F;const se=()=>{const V=F.topLine();return{line:V.line,clientTop:V.clientTop}},le=()=>x.current?.anchor(q)??null,Ae=o.bindEditor(e,{flush:re,adopt:V=>F.replaceText(V),returnPoint:()=>{const V=C.current.view==="preview"&&I.current.layout==="content"?le():null;return V===null?se():{line:V.lines.first,clientTop:V.clientTop}},nextConflict:()=>C.current.view!=="preview"?F.nextConflict():F.conflicts().length===0?!1:(G.current=!0,o.setView(e,"editor"),!0),beforeView:V=>{if(I.current.layout==="fill")return;if(V==="preview"){j.current={to:"preview",point:se()};return}const Z=le();j.current=Z===null?null:{to:"editor",anchor:Z}}});return A!==null&&F.showLine(A.topLine),()=>{re(),o.editorState.set(e,{state:F.save(),generation:N.current,topLine:F.topLine().line}),Ae(),F.destroy(),K=null,s.current=null}},[o,e,w,q]);const{request:O}=r;f.useLayoutEffect(()=>{const u=s.current,L=o.draft(e);u===null||L===null||O===null||(O.anchor!==null&&u.reveal(S.positionInSource(L,O.anchor),O.anchor.clientTop),u.focus(),o.consumeRequest(e))},[o,e,O,w]),f.useLayoutEffect(()=>{p!=="editor"&&!r.previewPrepared&&o.preparePreview(e)},[o,e,p,r.previewPrepared]),f.useLayoutEffect(()=>{const u=j.current,L=B.current,M=G.current;j.current=null,B.current=null,G.current=!1;const A=s.current;if(u?.to==="preview")x.current?.place(u.point,q);else if(u?.to==="editor"&&A!==null){const z=o.draft(e);z!==null&&A.reveal(S.positionInSource(z,u.anchor),u.anchor.clientTop)}M?A?.nextConflict():L==="editor"?A?.focus():L==="preview"&&x.current?.focus()},[o,e,p,q]);const v=f.useMemo(()=>({write:({fenceLine:u,expected:L,text:M})=>s.current?.replaceBody(u,L,M)===!0,undo:()=>s.current?.undo(),redo:()=>s.current?.redo()}),[]),D=f.useCallback(()=>{const u=s.current;if(u===null)throw new Error("[markdown-explorer] the preview read the draft before the editor was mounted");return u.text()},[]);f.useLayoutEffect(()=>{const u=s.current;if(!k||u===null)return;const L=ut({editor:qe(u.scrollElement),preview:()=>x.current?.scrollPort()??null,previewBlocks:()=>x.current?.blocks()??[],editorSpans:M=>u.lineSpans(M),requestFrame:M=>requestAnimationFrame(M),cancelFrame:M=>cancelAnimationFrame(M),now:()=>performance.now()});return L.showAlone(S.paneShownAlone(C.current.view)),c.current=L,()=>{L.dispose(),c.current=null}},[k,o,e,w]),f.useLayoutEffect(()=>{c.current?.showAlone(S.paneShownAlone(p))},[p]);const H=f.useCallback(()=>c.current?.previewChanged(),[]),y=f.useCallback(u=>o.start(e,u),[o,e]),T=u=>{if(u.target instanceof Element&&u.target.closest(yt)!==null)return;if(u.key==="Escape"){S.consumeKey(u);return}if(!S.isEditViewShortcut(u.nativeEvent,S.isApplePlatform(navigator.platform)))return;S.consumeKey(u);const L=S.toggledEditView(p);B.current=L,S.chooseEditView(o,e,L,h)};return R.jsxs("div",{className:"aqmx-edit","data-aqmx-edit-layout":n,"data-aqmx-edit-view":p,"data-testid":"markdown-explorer-edit",onKeyDown:T,onDoubleClick:Lt,children:[R.jsx("div",{ref:d,id:g,className:"aqmx-edit-editor","data-testid":"markdown-explorer-editor",inert:p==="preview",hidden:p==="preview"&&!h,...S.ALLOW_KEY_REPEAT}),p==="split"?R.jsx(St,{editorId:g}):null,b?R.jsx(gt,{path:e,readDraft:D,viewer:l,spreadsheetEditor:v,concealed:p!=="editor"?null:h?"overlay":"removed",onEditAt:p==="preview"?y:void 0,onLayout:H,handleRef:x}):null]})};exports.DRAFT_WRITE_INTERVAL_MS=Ie;exports.default=Rt;
|