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