dsh-tiddlywiki 0.16.1 → 0.16.2

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.
@@ -565,7 +565,7 @@ html[data-dsh-tw-active] .dshDesktopConversationSurface > :not([data-dsh-tw-view
565
565
  .dsh-tw-toolcard-row-tags { flex: none; font-size: 10px; color: color-mix(in srgb, var(--dsw-alias-label-primary, #222) 55%, transparent); }
566
566
  .dsh-tw-toolcard-row-meta { flex: none; font-size: 10px; color: color-mix(in srgb, var(--dsw-alias-label-primary, #222) 40%, transparent); }
567
567
  .dsh-tw-toolcard-tags-wrap { display: flex; flex-wrap: wrap; gap: 4px; }
568
- `,document.head.append(e))}var d=class{open=!1;listeners=new Set;isOpen(){return this.open}toggle(){this.set(!this.open)}openPanel(){this.set(!0)}closePanel(){this.set(!1)}set(e){if(this.open!==e){this.open=e;for(let t of[...this.listeners])t(e)}}subscribe(e){return this.listeners.add(e),()=>{this.listeners.delete(e)}}};function f(){let e=document.querySelector(`[data-pane="sidebar"], [class*="sidebarCol"], .dshDesktopUpstreamSidebar, .dshDesktopSidebarSurface`);if(e!==null)return e.querySelector(`[class*="logoRow"]`)?.parentElement??e.firstElementChild}function p(e){let t=e.querySelector(`button[class*="newSession"]`);if(t!==null)return t;for(let t of e.children)if(t instanceof HTMLButtonElement&&!t.matches(`[data-dsh-tw-entry]`))return t;let n=e.querySelector(`button[aria-label="新建会话"], button[aria-label="New Session"], button[aria-label*="新会话"], button[aria-label*="new session" i]`);return n===null?Array.from(e.querySelectorAll(`button`)).find(e=>!e.matches(`[data-dsh-tw-entry]`)&&/新会话|新建会话|new session/i.test(e.textContent??``)):n}function m(e){let t=document.createElement(`button`);return t.type=`button`,t.dataset.dshTwEntry=``,t.className=`dsh-tw-entry`,t.setAttribute(`aria-label`,`TiddlyWiki 知识库`),t.innerHTML=`<span class="dsh-tw-entry-icon"><svg viewBox="0 0 16 16" width="14" height="14" fill="none" stroke="currentColor" stroke-width="1.3" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M4 2.5h8a1 1 0 0 1 1 1v9a1 1 0 0 1-1 1H4a1 1 0 0 1-1-1v-9a1 1 0 0 1 1-1z"/><path d="M6 6h4M6 8.5h2.5"/></svg></span><span class="dsh-tw-entry-label">TiddlyWiki</span>`,t.addEventListener(`click`,()=>{e.toggle()}),t}function h(e,t){let n=p(e);if(n===void 0)return!1;if(t.parentElement!==e){let r=n.closest(`[class*="logoRow"]`),i=r!==null&&r.parentElement===e?r:n,a=Array.from(e.children).filter(e=>e instanceof HTMLElement&&e.matches(`[data-dsh-tw-entry], [data-dsh-atb-entry], [data-dsh-taskboard-entry], [data-dsh-ssh-entry]`)),o=a.length>0?a[0]??null:i.nextElementSibling??null;e.insertBefore(t,o)}return!0}function g(e){let t=m(e),n={attempts:0,found:!1,placed:!1},r=globalThis.location?.hostname;(r===`localhost`||r===`127.0.0.1`)&&(window.__twDebug=n);let i,a=!1,o=()=>{if(n.attempts++,i!==void 0&&!i.isConnected&&(c.disconnect(),i=void 0,a=!1),a){if(document.body.contains(t))return;c.disconnect(),i=void 0,a=!1}i??=f(),i!==void 0&&(n.found=p(i)!==void 0,a=h(i,t),n.placed=a,a&&c.observe(i,{childList:!0,subtree:!0}))},s=new MutationObserver(()=>{o()});s.observe(document.body,{childList:!0,subtree:!0});let c=new MutationObserver(()=>{if(i===void 0||!i.isConnected){a=!1,o();return}i.contains(t)||(a=h(i,t))}),l=setInterval(()=>{o()},2e3),u=()=>{e.isOpen()?t.dataset.active=`true`:delete t.dataset.active},d=e.subscribe(u);return u(),o(),()=>{clearInterval(l),s.disconnect(),c.disconnect(),d(),t.remove()}}const _=`$:/palettes/CupertinoDark`,v=`$:/palettes/Vanilla`,y=`data-ds-dark-theme`,b=`$:/palette`,x=new Set,S=new WeakSet;let C=``,w=!0,T=_;function ee(){try{let e=document.documentElement,t=document.body;if(t!==null&&t.hasAttribute(y))return!0;if(e!==null){let t=e.style.getPropertyValue(`color-scheme`).trim().toLowerCase();if(t===`dark`)return!0;if(t===`light`)return!1}}catch{}try{return window.matchMedia(`(prefers-color-scheme: dark)`).matches}catch{return!1}}function E(e){try{let t=e.wiki.getTiddlerText(b);return typeof t==`string`&&t.trim().length>0?t.trim():v}catch{return v}}function D(e){try{let t=e.wiki.getTiddler(E(e))?.fields;return t!==void 0&&t[`color-scheme`]===`dark`}catch{return!1}}function te(e){try{let t=e.syncer;if(t?.tiddlerInfo===void 0)return;let n=e.wiki.getChangeCount(b),r=t.tiddlerInfo[b];r===void 0?t.tiddlerInfo[b]={changeCount:n}:r.changeCount=n}catch{}}function ne(e){try{let t=e.syncer,n=t?.syncadaptor;if(t===void 0||n===void 0||S.has(t))return;let r=n.saveTiddler.bind(n);n.saveTiddler=(e,t)=>{if(w&&C.length>0&&e?.fields?.title===b&&e?.fields?.text===T){t(null,{},0);return}r(e,t)},S.add(t)}catch{}}function re(e,t){try{if(E(e)===t)return;e.wiki.setText(b,`text`,void 0,t),te(e)}catch{}}function ie(e,t){let n=E(e);if(t){if(D(e))return;n!==T&&(C=n),re(e,T)}else D(e)&&n===T&&C.length>0&&re(e,C)}function ae(e,t){try{let n=t.contentDocument?.documentElement;if(n==null)return;let r=D(e)?`dark`:`light`;n.style.getPropertyValue(`color-scheme`)!==r&&n.style.setProperty(`color-scheme`,r)}catch{}}function oe(e){let t=e.contentWindow?.$tw;t===void 0||t.wiki===void 0||(ne(t),w?ie(t,ee()):ie(t,!1),ae(t,e))}function se(e){let t=e.enabled!==!1,n=typeof e.darkPalette==`string`&&e.darkPalette.trim().length>0?e.darkPalette.trim():_,r=t!==w||n!==T;if(w=t,T=n,r)for(let e of[...x])e()}function ce(e){if(e===null)return()=>{};let t=()=>oe(e);x.add(t);let n=()=>t();e.addEventListener(`load`,n);let r=window.matchMedia(`(prefers-color-scheme: dark)`),i=()=>t();try{r.addEventListener(`change`,i)}catch{r.addListener?.(i)}let a;try{a=new MutationObserver(()=>t()),document.body!==null&&a.observe(document.body,{attributes:!0,attributeFilter:[y]}),document.documentElement!==null&&a.observe(document.documentElement,{attributes:!0,attributeFilter:[`style`]})}catch{a=void 0}let o=[150,600,1500,3e3].map(e=>window.setTimeout(t,e));return t(),()=>{x.delete(t),e.removeEventListener(`load`,n);for(let e of o)window.clearTimeout(e);try{r.removeEventListener(`change`,i)}catch{r.removeListener?.(i)}try{a?.disconnect()}catch{}}}const le=[`[class*="centerCol"]`,`[data-pane="conversation"]`,`.dshDesktopConversationSurface`],ue=`data-dsh-tw-active`,de=[`data-dsh-atb-active`,`data-dsh-taskboard-active`,`data-dsh-ssh-active`],fe=`dsh-panel-activate`,pe=`dsh-tiddlywiki`,me=`[data-dsh-panel-host]`,he=`dsh-tw-open-tiddler`;function ge(e){typeof document>`u`||typeof e!=`string`||e.length===0||document.dispatchEvent(new CustomEvent(he,{detail:{title:e}}))}function _e(){let e=document.querySelector(me);if(e===null)return 40;let t=parseInt(getComputedStyle(e).zIndex,10);return Number.isFinite(t)?Math.max(20,Math.min(40,t-1)):40}function ve(){for(let e of le){let t=document.querySelector(e);if(t!==null)return t}}async function ye(){try{let e=await fetch(`/dsh-tiddlywiki/status`,{signal:AbortSignal.timeout(8e3)});return e.ok?await e.json():null}catch{return null}}async function be(){try{return(await fetch(`/dsh-tiddlywiki/restart`,{method:`POST`,signal:AbortSignal.timeout(8e3)})).ok}catch{return!1}}function xe(e){let t,n,r,i,a,o,s=0,c,l=!1,u=null,d=()=>{let e=document.createElement(`div`);return e.dataset.dshTwView=``,e.className=`dsh-tw-view`,i=document.createElement(`div`),i.className=`dsh-tw-panel-frame-wrap`,i.style.cssText=`flex:1;min-height:0;display:flex;flex-direction:column`,r=document.createElement(`iframe`),r.className=`dsh-tw-panel-frame`,r.title=`TiddlyWiki`,r.hidden=!0,i.append(r),r.addEventListener(`load`,()=>{l=!0,v()}),c=ce(r),a=document.createElement(`div`),a.className=`dsh-tw-panel-error`,a.hidden=!0,e.append(i,a),e},f=()=>{if(t===void 0||n===void 0)return;let e=n.getBoundingClientRect();if(e.width===0&&e.height===0)return;let r=`${e.left}px`,i=`${e.top}px`,a=`${e.width}px`,o=`${e.height}px`;t.style.left!==r&&(t.style.left=r),t.style.top!==i&&(t.style.top=i),t.style.width!==a&&(t.style.width=a),t.style.height!==o&&(t.style.height=o)},p=()=>{t!==void 0&&(t.style.zIndex=String(_e()),document.querySelector(me)===null?delete t.dataset.sidebarHost:t.dataset.sidebarHost=`1`)},m=()=>{t===void 0&&(n=ve(),n!==void 0&&(t=d(),t.style.position=`fixed`,p(),document.body.append(t),f()))},h=e=>{if(r===void 0||a===void 0||i===void 0)return;r.hidden=!0,a.hidden=!1,a.textContent=``;let t=document.createElement(`div`);t.textContent=`TiddlyWiki 服务不可用`;let n=document.createElement(`code`);n.textContent=e;let o=document.createElement(`button`);o.type=`button`,o.textContent=`重试`,o.addEventListener(`click`,()=>{o.disabled=!0,o.textContent=`重启中…`,be().finally(()=>{b()})}),a.append(t,n,o)},g=()=>{if(r===void 0||a===void 0||i===void 0)return;r.hidden=!0,a.hidden=!1,a.textContent=``;let e=document.createElement(`div`);e.textContent=`TiddlyWiki 服务正在启动…`,a.append(e)},_=e=>{r===void 0||a===void 0||(a.hidden=!0,r.hidden=!1,r.dataset.loaded!==e&&(r.dataset.loaded=e,l=!1,r.src=e))},v=()=>{if(u===null||r===void 0||!l)return;let e=u;u=null;try{let t=r.contentWindow;if(t===null)throw Error(`iframe window unavailable`);if(t.location.hash===e)return;t.location.hash=e}catch{let t=r.src.split(`#`)[0];if(r.src===`${t}${e}`)return;r.src=`${t}${e}`}},y=t=>{let n=t.detail,r=typeof n?.title==`string`&&n.title.length>0?n.title:``;r.length!==0&&(u=`#${encodeURIComponent(r)}`,e.openPanel(),v())},b=async()=>{o!==void 0&&(window.clearTimeout(o),o=void 0);let e=await ye();if(e===null){h(`无法访问 /dsh-tiddlywiki/status`);return}if(e.status===`running`){s=0,se({enabled:e.ui?.followDshTheme!==!1,darkPalette:e.ui?.darkPalette}),typeof e.twProxy==`string`?_(new URL(e.twProxy,window.location.origin).href):typeof e.url==`string`?_(e.url):h(`服务未返回编辑器地址`);return}if(e.status===`starting`){g(),s<30&&(s++,o=window.setTimeout(()=>{b()},1500));return}s=0,h(e.error??`服务状态:${e.status}`)},x=()=>{if(e.isOpen()){for(let e of de)document.documentElement.removeAttribute(e);document.documentElement.setAttribute(ue,``),document.dispatchEvent(new CustomEvent(fe,{detail:pe})),b()}else document.documentElement.removeAttribute(ue),o!==void 0&&(window.clearTimeout(o),o=void 0),s=0},S=t=>{t.detail!==pe&&e.isOpen()&&e.closePanel()},C=t=>{if(!e.isOpen())return;let n=t.target;n!==null&&n.closest(`[data-dsh-tw-entry]`)===null&&n.closest(`[class*="sessionRow"], [class*="projectRow"], [class*="searchResultRow"], [class*="searchResultWorkspace"], [class*="newSession"]`)!==null&&e.closePanel()},w=new MutationObserver(()=>{m(),p()});w.observe(document.body,{childList:!0,subtree:!0});let T=new ResizeObserver(()=>f());T.observe(document.body);let ee=window.setInterval(f,2e3),E=()=>f();window.addEventListener(`resize`,E);let D=()=>f();window.addEventListener(`scroll`,D,!0),document.addEventListener(`click`,C,!0),document.addEventListener(fe,S);let te=e.subscribe(x);m(),x();let ne=()=>{r!==void 0&&!r.hidden&&(r.src=r.src)};return document.addEventListener(`dsh-tw-panel-reload`,ne),document.addEventListener(he,y),()=>{o!==void 0&&window.clearTimeout(o),window.clearInterval(ee),window.removeEventListener(`resize`,E),window.removeEventListener(`scroll`,D,!0),T.disconnect(),document.removeEventListener(`click`,C,!0),document.removeEventListener(fe,S),document.removeEventListener(`dsh-tw-panel-reload`,ne),document.removeEventListener(he,y),w.disconnect(),te(),c?.(),document.documentElement.removeAttribute(ue),t?.remove()}}function O(e){let t=document.querySelector(`.dsh-tw-toast`);t===null&&(t=document.createElement(`div`),t.className=`dsh-tw-toast`,document.body.append(t)),t.textContent=e,t.classList.add(`dsh-tw-toast-show`),clearTimeout(t.__twToastTimer),t.__twToastTimer=window.setTimeout(()=>{t?.classList.remove(`dsh-tw-toast-show`)},2500)}let k,Se,Ce,we;function Te(e,t){De(),!(k===void 0||Se===void 0)&&(Ce!==void 0&&(Ce.textContent=`TiddlyWiki 编辑器 · ${t}`),k.style.display=``,Se.src=e)}function Ee(){we?.(),we=void 0,k?.remove(),k=void 0,Se=void 0,Ce=void 0}function De(){if(k!==void 0&&Se!==void 0)return;k=document.createElement(`div`),k.className=`dsh-tw-editor-popup`,k.style.display=`none`;let e=document.createElement(`div`);e.className=`dsh-tw-editor-bar`,Ce=document.createElement(`span`),Ce.className=`dsh-tw-editor-title`,Ce.textContent=`TiddlyWiki 编辑器`;let t=document.createElement(`button`);t.type=`button`,t.className=`dsh-tw-editor-close`,t.textContent=`✕`,t.title=`关闭`,e.append(Ce,t),Se=document.createElement(`iframe`),Se.className=`dsh-tw-editor-frame`,Se.setAttribute(`sandbox`,`allow-scripts allow-same-origin allow-forms allow-popups`),Se.title=`TiddlyWiki 编辑器`,we=ce(Se);let n=document.createElement(`div`);n.className=`dsh-tw-editor-resize`,n.title=`拖拽调整大小`,k.append(e,Se,n),document.body.append(k),t.addEventListener(`click`,()=>{k!==void 0&&(k.style.display=`none`)}),e.addEventListener(`mousedown`,e=>{if(e.button!==0||k===void 0)return;e.preventDefault();let t=k.getBoundingClientRect(),n=e.clientX,r=e.clientY,i=t.left,a=t.top,o=e=>{k!==void 0&&(k.style.left=`${i+e.clientX-n}px`,k.style.top=`${a+e.clientY-r}px`,k.style.margin=`0`,k.style.right=`auto`,k.style.bottom=`auto`)},s=()=>{window.removeEventListener(`mousemove`,o),window.removeEventListener(`mouseup`,s)};window.addEventListener(`mousemove`,o),window.addEventListener(`mouseup`,s)}),n.addEventListener(`mousedown`,e=>{if(e.button!==0||k===void 0)return;e.preventDefault(),e.stopPropagation();let t=k.getBoundingClientRect(),n=e.clientX,r=e.clientY,i=t.width,a=t.height,o=e=>{k!==void 0&&(k.style.width=`${Math.max(360,i+e.clientX-n)}px`,k.style.height=`${Math.max(260,a+e.clientY-r)}px`,k.style.right=`auto`,k.style.bottom=`auto`)},s=()=>{window.removeEventListener(`mousemove`,o),window.removeEventListener(`mouseup`,s)};window.addEventListener(`mousemove`,o),window.addEventListener(`mouseup`,s)})}let Oe=[],ke=[];(()=>{let e=`lc,34,7n,7,7b,19,,,,2,,2,,,20,b,1c,l,g,,2t,7,2,6,2,2,,4,z,,u,r,2j,b,1m,9,9,,o,4,,9,,3,,5,17,3,1n,9,16,o,,x,1i,3,,i,,7,a,2,t,3,1k,,,7,2,2,2,3,9,,a,2,q,,2,3,1k,,,5,4,2,2,3,3,,u,2,3,,b,3,1k,,,8,,3,,3,k,2,m,6,,3,1k,,,7,2,2,2,3,7,3,a,2,u,,1n,5,3,3,,4,9,,14,5,1j,,,7,,3,,4,7,2,b,2,t,3,1k,,,7,,3,,4,7,2,b,2,f,,c,4,1j,2,,7,,3,,4,9,,a,2,t,3,1y,,4,6,,,,8,i,2,1p,,,8,c,8,2q,,,a,b,7,21,2,r,,,,,,4,2,1d,k,,2,5,b,,10,9,,2u,b,,6,n,4,4,3,g,4,d,,,3,6,,f,,jj,3,qa,4,s,3,t,2,u,2,1s,w,9,,19,3,,,39,2,y,,3a,c,4,c,63,5,1l,a,,,,,2,o,2,,1c,1a,2,c,k,5,1b,h,12,9,c,3,u,d,1k,e,1c,k,48,3,,l,4,,6,,2,3,5i,1s,ek,,5f,x,2da,3,3x,,2o,w,fe,6,2x,2,n9w,4,,a,w,2,28,2,7k,,3,,4,,n,5,4,,2b,2,1e,i,q,i,d,,12,8,p,d,18,4,1b,e,10,,1v,e,c,,8,2,1a,,1f,,,3,2,2,5,2,,,15,5,5,2,6k,8,,2,fn4,,kh,g,g,g,a6,2,gt,,6a,,45,5,1ae,3,,2,5,4,14,3,4,,4l,2,fx,4,1t,5,8t,2,25,6,1y,b,1d,4,3e,3,1h,f,15,,2,2,a,4,19,b,7,,1p,3,10,e,g,2,18,,c,3,1c,e,8,4,,2,2k,c,6,,2,,4d,c,l,4,1j,2,,7,2,2,2,3,9,,a,2,2,7,3,5,1v,9,,,2,,,4,,5,,,e,2,2a,i,n,,29,k,6j,7,2,9,r,2,2a,h,2y,d,2t,3,2,a,74,f,6t,6,,2,2,4,,,,2,3x,7,2,7,3,,s,a,14,7,,4,8,,9,b,1a,g,5i,8,5j,8,,8,2a,m,,e,3e,6,3,,,2,,7,,,1u,5,,2,,5,9n,4,9,2,,,1c,7,3,5,n,,44l,,6,f,8ug,i,1xc,5,1n,7,t4,,,1j,7,4,29,,b,2,f57,2,3mp,1a,2,n,f2,5,3,6,8,8,2,7,u,4,44,3,1iz,1j,4,1e,8,,e,,m,5,,f,11s,7,,h,2,7,,2,,5,2s,,4g,7,af,,1p,4,e4,4,72,2,6r,,2,,7,2,5,,d6,7,31,7,240,5`.split(`,`).map(e=>e?parseInt(e,36):1);for(let t=0,n=0;t<e.length;t++)(t%2?ke:Oe).push(n+=e[t])})();function Ae(e){if(e<768)return!1;for(let t=0,n=Oe.length;;){let r=t+n>>1;if(e<Oe[r])n=r;else if(e>=ke[r])t=r+1;else return!0;if(t==n)return!1}}function je(e){return e>=127462&&e<=127487}const Me=8205;function Ne(e,t,n=!0,r=!0){return(n?Pe:Fe)(e,t,r)}function Pe(e,t,n){if(t==e.length)return t;t&&Le(e.charCodeAt(t))&&Re(e.charCodeAt(t-1))&&t--;let r=Ie(e,t);for(t+=ze(r);t<e.length;){let i=Ie(e,t);if(r==Me||i==Me||n&&Ae(i))t+=ze(i),r=i;else if(je(i)){let n=0,r=t-2;for(;r>=0&&je(Ie(e,r));)n++,r-=2;if(n%2==0)break;t+=2}else break}return t}function Fe(e,t,n){for(;t>1;){let r=Pe(e,t-2,n);if(r<t)return r;t--}return 0}function Ie(e,t){let n=e.charCodeAt(t);if(!Re(n)||t+1==e.length)return n;let r=e.charCodeAt(t+1);return Le(r)?(n-55296<<10)+(r-56320)+65536:n}function Le(e){return e>=56320&&e<57344}function Re(e){return e>=55296&&e<56320}function ze(e){return e<65536?1:2}var A=class e{lineAt(e){if(e<0||e>this.length)throw RangeError(`Invalid position ${e} in document of length ${this.length}`);return this.lineInner(e,!1,1,0)}line(e){if(e<1||e>this.lines)throw RangeError(`Invalid line number ${e} in ${this.lines}-line document`);return this.lineInner(e,!0,1,0)}replace(e,t,n){[e,t]=Ye(this,e,t);let r=[];return this.decompose(0,e,r,2),n.length&&n.decompose(0,n.length,r,3),this.decompose(t,this.length,r,1),Ve.from(r,this.length-(t-e)+n.length)}append(e){return this.replace(this.length,this.length,e)}slice(e,t=this.length){[e,t]=Ye(this,e,t);let n=[];return this.decompose(e,t,n,0),Ve.from(n,t-e)}eq(e){if(e==this)return!0;if(e.length!=this.length||e.lines!=this.lines)return!1;let t=this.scanIdentical(e,1),n=this.length-this.scanIdentical(e,-1),r=new Ge(this),i=new Ge(e);for(let e=t,a=t;;){if(r.next(e),i.next(e),e=0,r.lineBreak!=i.lineBreak||r.done!=i.done||r.value!=i.value)return!1;if(a+=r.value.length,r.done||a>=n)return!0}}iter(e=1){return new Ge(this,e)}iterRange(e,t=this.length){return new Ke(this,e,t)}iterLines(e,t){let n;if(e==null)n=this.iter();else{t??=this.lines+1;let r=this.line(e).from;n=this.iterRange(r,Math.max(r,t==this.lines+1?this.length:t<=1?0:this.line(t-1).to))}return new qe(n)}toString(){return this.sliceString(0)}toJSON(){let e=[];return this.flatten(e),e}constructor(){}static of(t){if(t.length==0)throw RangeError(`A document must have at least one line`);return t.length==1&&!t[0]?e.empty:t.length<=32?new Be(t):Ve.from(Be.split(t,[]))}},Be=class e extends A{constructor(e,t=He(e)){super(),this.text=e,this.length=t}get lines(){return this.text.length}get children(){return null}lineInner(e,t,n,r){for(let i=0;;i++){let a=this.text[i],o=r+a.length;if((t?n:o)>=e)return new Je(r,o,n,a);r=o+1,n++}}decompose(t,n,r,i){let a=t<=0&&n>=this.length?this:new e(We(this.text,t,n),Math.min(n,this.length)-Math.max(0,t));if(i&1){let t=r.pop(),n=Ue(a.text,t.text.slice(),0,a.length);if(n.length<=32)r.push(new e(n,t.length+a.length));else{let t=n.length>>1;r.push(new e(n.slice(0,t)),new e(n.slice(t)))}}else r.push(a)}replace(t,n,r){if(!(r instanceof e))return super.replace(t,n,r);[t,n]=Ye(this,t,n);let i=Ue(this.text,Ue(r.text,We(this.text,0,t)),n),a=this.length+r.length-(n-t);return i.length<=32?new e(i,a):Ve.from(e.split(i,[]),a)}sliceString(e,t=this.length,n=`
568
+ `,document.head.append(e))}var d=class{open=!1;listeners=new Set;isOpen(){return this.open}toggle(){this.set(!this.open)}openPanel(){this.set(!0)}closePanel(){this.set(!1)}set(e){if(this.open!==e){this.open=e;for(let t of[...this.listeners])t(e)}}subscribe(e){return this.listeners.add(e),()=>{this.listeners.delete(e)}}};function f(){let e=document.querySelector(`[data-pane="sidebar"], [class*="sidebarCol"], .dshDesktopUpstreamSidebar, .dshDesktopSidebarSurface`);if(e!==null)return e.querySelector(`[class*="logoRow"]`)?.parentElement??e.firstElementChild}function p(e){let t=e.querySelector(`button[class*="newSession"]`);if(t!==null)return t;for(let t of e.children)if(t instanceof HTMLButtonElement&&!t.matches(`[data-dsh-tw-entry]`))return t;let n=e.querySelector(`button[aria-label="新建会话"], button[aria-label="New Session"], button[aria-label*="新会话"], button[aria-label*="new session" i]`);return n===null?Array.from(e.querySelectorAll(`button`)).find(e=>!e.matches(`[data-dsh-tw-entry]`)&&/新会话|新建会话|new session/i.test(e.textContent??``)):n}function m(e){let t=document.createElement(`button`);return t.type=`button`,t.dataset.dshTwEntry=``,t.className=`dsh-tw-entry`,t.setAttribute(`aria-label`,`TiddlyWiki 知识库`),t.innerHTML=`<span class="dsh-tw-entry-icon"><svg viewBox="0 0 16 16" width="14" height="14" fill="none" stroke="currentColor" stroke-width="1.3" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M4 2.5h8a1 1 0 0 1 1 1v9a1 1 0 0 1-1 1H4a1 1 0 0 1-1-1v-9a1 1 0 0 1 1-1z"/><path d="M6 6h4M6 8.5h2.5"/></svg></span><span class="dsh-tw-entry-label">TiddlyWiki</span>`,t.addEventListener(`click`,()=>{e.toggle()}),t}function h(e,t){let n=p(e);if(n===void 0)return!1;if(t.parentElement!==e){let r=n.closest(`[class*="logoRow"]`),i=r!==null&&r.parentElement===e?r:n,a=Array.from(e.children).filter(e=>e instanceof HTMLElement&&e.matches(`[data-dsh-tw-entry], [data-dsh-atb-entry], [data-dsh-taskboard-entry], [data-dsh-ssh-entry]`)),o=a.length>0?a[0]??null:i.nextElementSibling??null;e.insertBefore(t,o)}return!0}function g(e){let t=m(e),n={attempts:0,found:!1,placed:!1},r=globalThis.location?.hostname;(r===`localhost`||r===`127.0.0.1`)&&(window.__twDebug=n);let i,a=!1,o=()=>{if(n.attempts++,i!==void 0&&!i.isConnected&&(c.disconnect(),i=void 0,a=!1),a){if(document.body.contains(t))return;c.disconnect(),i=void 0,a=!1}i??=f(),i!==void 0&&(n.found=p(i)!==void 0,a=h(i,t),n.placed=a,a&&c.observe(i,{childList:!0,subtree:!0}))},s=new MutationObserver(()=>{o()});s.observe(document.body,{childList:!0,subtree:!0});let c=new MutationObserver(()=>{if(i===void 0||!i.isConnected){a=!1,o();return}i.contains(t)||(a=h(i,t))}),l=setInterval(()=>{o()},2e3),u=()=>{e.isOpen()?t.dataset.active=`true`:delete t.dataset.active},d=e.subscribe(u);return u(),o(),()=>{clearInterval(l),s.disconnect(),c.disconnect(),d(),t.remove()}}const _=`$:/palettes/CupertinoDark`,v=`$:/palettes/Vanilla`,y=`data-ds-dark-theme`,b=`$:/palette`,x=new Set,S=new WeakSet;let C=``,w=!0,T=_;function ee(){try{let e=document.documentElement,t=document.body;if(t!==null&&t.hasAttribute(y))return!0;if(e!==null){let t=e.style.getPropertyValue(`color-scheme`).trim().toLowerCase();if(t===`dark`)return!0;if(t===`light`)return!1}}catch{}try{return window.matchMedia(`(prefers-color-scheme: dark)`).matches}catch{return!1}}function E(e){try{let t=e.wiki.getTiddlerText(b);return typeof t==`string`&&t.trim().length>0?t.trim():v}catch{return v}}function D(e){try{let t=e.wiki.getTiddler(E(e))?.fields;return t!==void 0&&t[`color-scheme`]===`dark`}catch{return!1}}function te(e){try{let t=e.syncer;if(t?.tiddlerInfo===void 0)return;let n=e.wiki.getChangeCount(b),r=t.tiddlerInfo[b];r===void 0?t.tiddlerInfo[b]={changeCount:n}:r.changeCount=n}catch{}}function ne(e){try{let t=e.syncer,n=t?.syncadaptor;if(t===void 0||n===void 0||S.has(t))return;let r=n.saveTiddler.bind(n);n.saveTiddler=(e,t)=>{if(w&&C.length>0&&e?.fields?.title===b&&e?.fields?.text===T){t(null,{},0);return}r(e,t)},S.add(t)}catch{}}function re(e,t){try{if(E(e)===t)return;e.wiki.setText(b,`text`,void 0,t),te(e)}catch{}}function ie(e,t){let n=E(e);if(t){if(D(e))return;n!==T&&(C=n),re(e,T)}else D(e)&&n===T&&C.length>0&&re(e,C)}function ae(e,t){try{let n=t.contentDocument?.documentElement;if(n==null)return;let r=D(e)?`dark`:`light`;n.style.getPropertyValue(`color-scheme`)!==r&&n.style.setProperty(`color-scheme`,r)}catch{}}function oe(e){let t=e.contentWindow?.$tw;t===void 0||t.wiki===void 0||(ne(t),w?ie(t,ee()):ie(t,!1),ae(t,e))}function se(e){let t=e.enabled!==!1,n=typeof e.darkPalette==`string`&&e.darkPalette.trim().length>0?e.darkPalette.trim():_,r=t!==w||n!==T;if(w=t,T=n,r)for(let e of[...x])e()}function ce(e){if(e===null)return()=>{};let t=()=>oe(e);x.add(t);let n=()=>t();e.addEventListener(`load`,n);let r=window.matchMedia(`(prefers-color-scheme: dark)`),i=()=>t();try{r.addEventListener(`change`,i)}catch{r.addListener?.(i)}let a;try{a=new MutationObserver(()=>t()),document.body!==null&&a.observe(document.body,{attributes:!0,attributeFilter:[y]}),document.documentElement!==null&&a.observe(document.documentElement,{attributes:!0,attributeFilter:[`style`]})}catch{a=void 0}let o=[150,600,1500,3e3].map(e=>window.setTimeout(t,e));return t(),()=>{x.delete(t),e.removeEventListener(`load`,n);for(let e of o)window.clearTimeout(e);try{r.removeEventListener(`change`,i)}catch{r.removeListener?.(i)}try{a?.disconnect()}catch{}}}const le=[`[class*="centerCol"]`,`[data-pane="conversation"]`,`.dshDesktopConversationSurface`],ue=`data-dsh-tw-active`,de=[`data-dsh-atb-active`,`data-dsh-taskboard-active`,`data-dsh-ssh-active`],fe=`dsh-panel-activate`,pe=`dsh-tiddlywiki`,me=`[data-dsh-panel-host]`,he=`dsh-tw-open-tiddler`;function ge(e){typeof document>`u`||typeof e!=`string`||e.length===0||document.dispatchEvent(new CustomEvent(he,{detail:{title:e}}))}function _e(){let e=document.querySelector(me);if(e===null)return 40;let t=parseInt(getComputedStyle(e).zIndex,10);return Number.isFinite(t)?Math.max(20,Math.min(40,t-1)):40}function ve(){for(let e of le){let t=document.querySelector(e);if(t!==null)return t}}async function ye(){try{let e=await fetch(`/dsh-tiddlywiki/status`,{signal:AbortSignal.timeout(8e3)});return e.ok?await e.json():null}catch{return null}}async function be(){try{return(await fetch(`/dsh-tiddlywiki/restart`,{method:`POST`,signal:AbortSignal.timeout(8e3)})).ok}catch{return!1}}function xe(e){let t,n,r,i,a,o,s=0,c,l=!1,u=null,d=()=>{let e=document.createElement(`div`);return e.dataset.dshTwView=``,e.className=`dsh-tw-view`,i=document.createElement(`div`),i.className=`dsh-tw-panel-frame-wrap`,i.style.cssText=`flex:1;min-height:0;display:flex;flex-direction:column`,r=document.createElement(`iframe`),r.className=`dsh-tw-panel-frame`,r.title=`TiddlyWiki`,r.hidden=!0,i.append(r),r.addEventListener(`load`,()=>{l=!0,v()}),c=ce(r),a=document.createElement(`div`),a.className=`dsh-tw-panel-error`,a.hidden=!0,e.append(i,a),e},f=()=>{if(t===void 0||n===void 0)return;let e=n.getBoundingClientRect();if(e.width===0&&e.height===0)return;let r=`${e.left}px`,i=`${e.top}px`,a=`${e.width}px`,o=`${e.height}px`;t.style.left!==r&&(t.style.left=r),t.style.top!==i&&(t.style.top=i),t.style.width!==a&&(t.style.width=a),t.style.height!==o&&(t.style.height=o)},p=()=>{t!==void 0&&(t.style.zIndex=String(_e()),document.querySelector(me)===null?delete t.dataset.sidebarHost:t.dataset.sidebarHost=`1`)},m=()=>{t===void 0&&(n=ve(),n!==void 0&&(t=d(),t.style.position=`fixed`,p(),document.body.append(t),f()))},h=e=>{if(r===void 0||a===void 0||i===void 0)return;r.hidden=!0,a.hidden=!1,a.textContent=``;let t=document.createElement(`div`);t.textContent=`TiddlyWiki 服务不可用`;let n=document.createElement(`code`);n.textContent=e;let o=document.createElement(`button`);o.type=`button`,o.textContent=`重试`,o.addEventListener(`click`,()=>{o.disabled=!0,o.textContent=`重启中…`,be().finally(()=>{x()})}),a.append(t,n,o)},g=()=>{if(r===void 0||a===void 0||i===void 0)return;r.hidden=!0,a.hidden=!1,a.textContent=``;let e=document.createElement(`div`);e.textContent=`TiddlyWiki 服务正在启动…`,a.append(e)},_=e=>{r===void 0||a===void 0||(a.hidden=!0,r.hidden=!1,r.dataset.loaded!==e&&(r.dataset.loaded=e,l=!1,r.src=e))},v=()=>{if(u===null||r===void 0||!l)return;let e=u,t=r.contentWindow;if(t===null){y(e);return}let n=r=>{if(u!==e)return;let i=t;if(typeof i.$tw!=`object`||i.$tw===null){if(r<40){window.setTimeout(()=>n(r+1),150);return}y(e);return}u=null;try{if(t.location.hash===e)return;t.location.hash=e}catch{y(e)}};n(0)},y=e=>{if(r===void 0)return;u===e&&(u=null);let t=r.src.split(`#`)[0];r.src!==`${t}${e}`&&(r.src=`${t}${e}`)},b=t=>{let n=t.detail,r=typeof n?.title==`string`&&n.title.length>0?n.title:``;r.length!==0&&(u=`#${encodeURIComponent(r)}`,e.openPanel(),v())},x=async()=>{o!==void 0&&(window.clearTimeout(o),o=void 0);let e=await ye();if(e===null){h(`无法访问 /dsh-tiddlywiki/status`);return}if(e.status===`running`){s=0,se({enabled:e.ui?.followDshTheme!==!1,darkPalette:e.ui?.darkPalette}),typeof e.twProxy==`string`?_(new URL(e.twProxy,window.location.origin).href):typeof e.url==`string`?_(e.url):h(`服务未返回编辑器地址`);return}if(e.status===`starting`){g(),s<30&&(s++,o=window.setTimeout(()=>{x()},1500));return}s=0,h(e.error??`服务状态:${e.status}`)},S=()=>{if(e.isOpen()){for(let e of de)document.documentElement.removeAttribute(e);document.documentElement.setAttribute(ue,``),document.dispatchEvent(new CustomEvent(fe,{detail:pe})),x()}else document.documentElement.removeAttribute(ue),o!==void 0&&(window.clearTimeout(o),o=void 0),s=0},C=t=>{t.detail!==pe&&e.isOpen()&&e.closePanel()},w=t=>{if(!e.isOpen())return;let n=t.target;n!==null&&n.closest(`[data-dsh-tw-entry]`)===null&&n.closest(`[class*="sessionRow"], [class*="projectRow"], [class*="searchResultRow"], [class*="searchResultWorkspace"], [class*="newSession"]`)!==null&&e.closePanel()},T=new MutationObserver(()=>{m(),p()});T.observe(document.body,{childList:!0,subtree:!0});let ee=new ResizeObserver(()=>f());ee.observe(document.body);let E=window.setInterval(f,2e3),D=()=>f();window.addEventListener(`resize`,D);let te=()=>f();window.addEventListener(`scroll`,te,!0),document.addEventListener(`click`,w,!0),document.addEventListener(fe,C);let ne=e.subscribe(S);m(),S();let re=()=>{r!==void 0&&!r.hidden&&(r.src=r.src)};return document.addEventListener(`dsh-tw-panel-reload`,re),document.addEventListener(he,b),()=>{o!==void 0&&window.clearTimeout(o),window.clearInterval(E),window.removeEventListener(`resize`,D),window.removeEventListener(`scroll`,te,!0),ee.disconnect(),document.removeEventListener(`click`,w,!0),document.removeEventListener(fe,C),document.removeEventListener(`dsh-tw-panel-reload`,re),document.removeEventListener(he,b),T.disconnect(),ne(),c?.(),document.documentElement.removeAttribute(ue),t?.remove()}}function O(e){let t=document.querySelector(`.dsh-tw-toast`);t===null&&(t=document.createElement(`div`),t.className=`dsh-tw-toast`,document.body.append(t)),t.textContent=e,t.classList.add(`dsh-tw-toast-show`),clearTimeout(t.__twToastTimer),t.__twToastTimer=window.setTimeout(()=>{t?.classList.remove(`dsh-tw-toast-show`)},2500)}let k,Se,Ce,we;function Te(e,t){De(),!(k===void 0||Se===void 0)&&(Ce!==void 0&&(Ce.textContent=`TiddlyWiki 编辑器 · ${t}`),k.style.display=``,Se.src=e)}function Ee(){we?.(),we=void 0,k?.remove(),k=void 0,Se=void 0,Ce=void 0}function De(){if(k!==void 0&&Se!==void 0)return;k=document.createElement(`div`),k.className=`dsh-tw-editor-popup`,k.style.display=`none`;let e=document.createElement(`div`);e.className=`dsh-tw-editor-bar`,Ce=document.createElement(`span`),Ce.className=`dsh-tw-editor-title`,Ce.textContent=`TiddlyWiki 编辑器`;let t=document.createElement(`button`);t.type=`button`,t.className=`dsh-tw-editor-close`,t.textContent=`✕`,t.title=`关闭`,e.append(Ce,t),Se=document.createElement(`iframe`),Se.className=`dsh-tw-editor-frame`,Se.setAttribute(`sandbox`,`allow-scripts allow-same-origin allow-forms allow-popups`),Se.title=`TiddlyWiki 编辑器`,we=ce(Se);let n=document.createElement(`div`);n.className=`dsh-tw-editor-resize`,n.title=`拖拽调整大小`,k.append(e,Se,n),document.body.append(k),t.addEventListener(`click`,()=>{k!==void 0&&(k.style.display=`none`)}),e.addEventListener(`mousedown`,e=>{if(e.button!==0||k===void 0)return;e.preventDefault();let t=k.getBoundingClientRect(),n=e.clientX,r=e.clientY,i=t.left,a=t.top,o=e=>{k!==void 0&&(k.style.left=`${i+e.clientX-n}px`,k.style.top=`${a+e.clientY-r}px`,k.style.margin=`0`,k.style.right=`auto`,k.style.bottom=`auto`)},s=()=>{window.removeEventListener(`mousemove`,o),window.removeEventListener(`mouseup`,s)};window.addEventListener(`mousemove`,o),window.addEventListener(`mouseup`,s)}),n.addEventListener(`mousedown`,e=>{if(e.button!==0||k===void 0)return;e.preventDefault(),e.stopPropagation();let t=k.getBoundingClientRect(),n=e.clientX,r=e.clientY,i=t.width,a=t.height,o=e=>{k!==void 0&&(k.style.width=`${Math.max(360,i+e.clientX-n)}px`,k.style.height=`${Math.max(260,a+e.clientY-r)}px`,k.style.right=`auto`,k.style.bottom=`auto`)},s=()=>{window.removeEventListener(`mousemove`,o),window.removeEventListener(`mouseup`,s)};window.addEventListener(`mousemove`,o),window.addEventListener(`mouseup`,s)})}let Oe=[],ke=[];(()=>{let e=`lc,34,7n,7,7b,19,,,,2,,2,,,20,b,1c,l,g,,2t,7,2,6,2,2,,4,z,,u,r,2j,b,1m,9,9,,o,4,,9,,3,,5,17,3,1n,9,16,o,,x,1i,3,,i,,7,a,2,t,3,1k,,,7,2,2,2,3,9,,a,2,q,,2,3,1k,,,5,4,2,2,3,3,,u,2,3,,b,3,1k,,,8,,3,,3,k,2,m,6,,3,1k,,,7,2,2,2,3,7,3,a,2,u,,1n,5,3,3,,4,9,,14,5,1j,,,7,,3,,4,7,2,b,2,t,3,1k,,,7,,3,,4,7,2,b,2,f,,c,4,1j,2,,7,,3,,4,9,,a,2,t,3,1y,,4,6,,,,8,i,2,1p,,,8,c,8,2q,,,a,b,7,21,2,r,,,,,,4,2,1d,k,,2,5,b,,10,9,,2u,b,,6,n,4,4,3,g,4,d,,,3,6,,f,,jj,3,qa,4,s,3,t,2,u,2,1s,w,9,,19,3,,,39,2,y,,3a,c,4,c,63,5,1l,a,,,,,2,o,2,,1c,1a,2,c,k,5,1b,h,12,9,c,3,u,d,1k,e,1c,k,48,3,,l,4,,6,,2,3,5i,1s,ek,,5f,x,2da,3,3x,,2o,w,fe,6,2x,2,n9w,4,,a,w,2,28,2,7k,,3,,4,,n,5,4,,2b,2,1e,i,q,i,d,,12,8,p,d,18,4,1b,e,10,,1v,e,c,,8,2,1a,,1f,,,3,2,2,5,2,,,15,5,5,2,6k,8,,2,fn4,,kh,g,g,g,a6,2,gt,,6a,,45,5,1ae,3,,2,5,4,14,3,4,,4l,2,fx,4,1t,5,8t,2,25,6,1y,b,1d,4,3e,3,1h,f,15,,2,2,a,4,19,b,7,,1p,3,10,e,g,2,18,,c,3,1c,e,8,4,,2,2k,c,6,,2,,4d,c,l,4,1j,2,,7,2,2,2,3,9,,a,2,2,7,3,5,1v,9,,,2,,,4,,5,,,e,2,2a,i,n,,29,k,6j,7,2,9,r,2,2a,h,2y,d,2t,3,2,a,74,f,6t,6,,2,2,4,,,,2,3x,7,2,7,3,,s,a,14,7,,4,8,,9,b,1a,g,5i,8,5j,8,,8,2a,m,,e,3e,6,3,,,2,,7,,,1u,5,,2,,5,9n,4,9,2,,,1c,7,3,5,n,,44l,,6,f,8ug,i,1xc,5,1n,7,t4,,,1j,7,4,29,,b,2,f57,2,3mp,1a,2,n,f2,5,3,6,8,8,2,7,u,4,44,3,1iz,1j,4,1e,8,,e,,m,5,,f,11s,7,,h,2,7,,2,,5,2s,,4g,7,af,,1p,4,e4,4,72,2,6r,,2,,7,2,5,,d6,7,31,7,240,5`.split(`,`).map(e=>e?parseInt(e,36):1);for(let t=0,n=0;t<e.length;t++)(t%2?ke:Oe).push(n+=e[t])})();function Ae(e){if(e<768)return!1;for(let t=0,n=Oe.length;;){let r=t+n>>1;if(e<Oe[r])n=r;else if(e>=ke[r])t=r+1;else return!0;if(t==n)return!1}}function je(e){return e>=127462&&e<=127487}const Me=8205;function Ne(e,t,n=!0,r=!0){return(n?Pe:Fe)(e,t,r)}function Pe(e,t,n){if(t==e.length)return t;t&&Le(e.charCodeAt(t))&&Re(e.charCodeAt(t-1))&&t--;let r=Ie(e,t);for(t+=ze(r);t<e.length;){let i=Ie(e,t);if(r==Me||i==Me||n&&Ae(i))t+=ze(i),r=i;else if(je(i)){let n=0,r=t-2;for(;r>=0&&je(Ie(e,r));)n++,r-=2;if(n%2==0)break;t+=2}else break}return t}function Fe(e,t,n){for(;t>1;){let r=Pe(e,t-2,n);if(r<t)return r;t--}return 0}function Ie(e,t){let n=e.charCodeAt(t);if(!Re(n)||t+1==e.length)return n;let r=e.charCodeAt(t+1);return Le(r)?(n-55296<<10)+(r-56320)+65536:n}function Le(e){return e>=56320&&e<57344}function Re(e){return e>=55296&&e<56320}function ze(e){return e<65536?1:2}var A=class e{lineAt(e){if(e<0||e>this.length)throw RangeError(`Invalid position ${e} in document of length ${this.length}`);return this.lineInner(e,!1,1,0)}line(e){if(e<1||e>this.lines)throw RangeError(`Invalid line number ${e} in ${this.lines}-line document`);return this.lineInner(e,!0,1,0)}replace(e,t,n){[e,t]=Ye(this,e,t);let r=[];return this.decompose(0,e,r,2),n.length&&n.decompose(0,n.length,r,3),this.decompose(t,this.length,r,1),Ve.from(r,this.length-(t-e)+n.length)}append(e){return this.replace(this.length,this.length,e)}slice(e,t=this.length){[e,t]=Ye(this,e,t);let n=[];return this.decompose(e,t,n,0),Ve.from(n,t-e)}eq(e){if(e==this)return!0;if(e.length!=this.length||e.lines!=this.lines)return!1;let t=this.scanIdentical(e,1),n=this.length-this.scanIdentical(e,-1),r=new Ge(this),i=new Ge(e);for(let e=t,a=t;;){if(r.next(e),i.next(e),e=0,r.lineBreak!=i.lineBreak||r.done!=i.done||r.value!=i.value)return!1;if(a+=r.value.length,r.done||a>=n)return!0}}iter(e=1){return new Ge(this,e)}iterRange(e,t=this.length){return new Ke(this,e,t)}iterLines(e,t){let n;if(e==null)n=this.iter();else{t??=this.lines+1;let r=this.line(e).from;n=this.iterRange(r,Math.max(r,t==this.lines+1?this.length:t<=1?0:this.line(t-1).to))}return new qe(n)}toString(){return this.sliceString(0)}toJSON(){let e=[];return this.flatten(e),e}constructor(){}static of(t){if(t.length==0)throw RangeError(`A document must have at least one line`);return t.length==1&&!t[0]?e.empty:t.length<=32?new Be(t):Ve.from(Be.split(t,[]))}},Be=class e extends A{constructor(e,t=He(e)){super(),this.text=e,this.length=t}get lines(){return this.text.length}get children(){return null}lineInner(e,t,n,r){for(let i=0;;i++){let a=this.text[i],o=r+a.length;if((t?n:o)>=e)return new Je(r,o,n,a);r=o+1,n++}}decompose(t,n,r,i){let a=t<=0&&n>=this.length?this:new e(We(this.text,t,n),Math.min(n,this.length)-Math.max(0,t));if(i&1){let t=r.pop(),n=Ue(a.text,t.text.slice(),0,a.length);if(n.length<=32)r.push(new e(n,t.length+a.length));else{let t=n.length>>1;r.push(new e(n.slice(0,t)),new e(n.slice(t)))}}else r.push(a)}replace(t,n,r){if(!(r instanceof e))return super.replace(t,n,r);[t,n]=Ye(this,t,n);let i=Ue(this.text,Ue(r.text,We(this.text,0,t)),n),a=this.length+r.length-(n-t);return i.length<=32?new e(i,a):Ve.from(e.split(i,[]),a)}sliceString(e,t=this.length,n=`
569
569
  `){[e,t]=Ye(this,e,t);let r=``;for(let i=0,a=0;i<=t&&a<this.text.length;a++){let o=this.text[a],s=i+o.length;i>e&&a&&(r+=n),e<s&&t>i&&(r+=o.slice(Math.max(0,e-i),t-i)),i=s+1}return r}flatten(e){for(let t of this.text)e.push(t)}scanIdentical(){return 0}static split(t,n){let r=[],i=-1;for(let a of t)r.push(a),i+=a.length+1,r.length==32&&(n.push(new e(r,i)),r=[],i=-1);return i>-1&&n.push(new e(r,i)),n}},Ve=class e extends A{constructor(e,t){super(),this.children=e,this.length=t,this.lines=0;for(let t of e)this.lines+=t.lines}lineInner(e,t,n,r){for(let i=0;;i++){let a=this.children[i],o=r+a.length,s=n+a.lines-1;if((t?s:o)>=e)return a.lineInner(e,t,n,r);r=o+1,n=s+1}}decompose(e,t,n,r){for(let i=0,a=0;a<=t&&i<this.children.length;i++){let o=this.children[i],s=a+o.length;if(e<=s&&t>=a){let i=r&(a<=e|(s>=t?2:0));a>=e&&s<=t&&!i?n.push(o):o.decompose(e-a,t-a,n,i)}a=s+1}}replace(t,n,r){if([t,n]=Ye(this,t,n),r.lines<this.lines)for(let i=0,a=0;i<this.children.length;i++){let o=this.children[i],s=a+o.length;if(t>=a&&n<=s){let c=o.replace(t-a,n-a,r),l=this.lines-o.lines+c.lines;if(c.lines<l>>4&&c.lines>l>>6){let a=this.children.slice();return a[i]=c,new e(a,this.length-(n-t)+r.length)}return super.replace(a,s,c)}a=s+1}return super.replace(t,n,r)}sliceString(e,t=this.length,n=`
570
570
  `){[e,t]=Ye(this,e,t);let r=``;for(let i=0,a=0;i<this.children.length&&a<=t;i++){let o=this.children[i],s=a+o.length;a>e&&i&&(r+=n),e<s&&t>a&&(r+=o.sliceString(e-a,t-a,n)),a=s+1}return r}flatten(e){for(let t of this.children)t.flatten(e)}scanIdentical(t,n){if(!(t instanceof e))return 0;let r=0,[i,a,o,s]=n>0?[0,0,this.children.length,t.children.length]:[this.children.length-1,t.children.length-1,-1,-1];for(;;i+=n,a+=n){if(i==o||a==s)return r;let e=this.children[i],c=t.children[a];if(e!=c)return r+e.scanIdentical(c,n);r+=e.length+1}}static from(t,n=t.reduce((e,t)=>e+t.length+1,-1)){let r=0;for(let e of t)r+=e.lines;if(r<32){let e=[];for(let n of t)n.flatten(e);return new Be(e,n)}let i=Math.max(32,r>>5),a=i<<1,o=i>>1,s=[],c=0,l=-1,u=[];function d(t){let n;if(t.lines>a&&t instanceof e)for(let e of t.children)d(e);else t.lines>o&&(c>o||!c)?(f(),s.push(t)):t instanceof Be&&c&&(n=u[u.length-1])instanceof Be&&t.lines+n.lines<=32?(c+=t.lines,l+=t.length+1,u[u.length-1]=new Be(n.text.concat(t.text),n.length+1+t.length)):(c+t.lines>i&&f(),c+=t.lines,l+=t.length+1,u.push(t))}function f(){c!=0&&(s.push(u.length==1?u[0]:e.from(u,l)),l=-1,c=u.length=0)}for(let e of t)d(e);return f(),s.length==1?s[0]:new e(s,n)}};A.empty=new Be([``],0);function He(e){let t=-1;for(let n of e)t+=n.length+1;return t}function Ue(e,t,n=0,r=1e9){for(let i=0,a=0,o=!0;a<e.length&&i<=r;a++){let s=e[a],c=i+s.length;c>=n&&(c>r&&(s=s.slice(0,r-i)),i<n&&(s=s.slice(n-i)),o?(t[t.length-1]+=s,o=!1):t.push(s)),i=c+1}return t}function We(e,t,n){return Ue(e,[``],t,n)}var Ge=class{constructor(e,t=1){this.dir=t,this.done=!1,this.lineBreak=!1,this.value=``,this.nodes=[e],this.offsets=[t>0?1:(e instanceof Be?e.text.length:e.children.length)<<1]}nextInner(e,t){for(this.done=this.lineBreak=!1;;){let n=this.nodes.length-1,r=this.nodes[n],i=this.offsets[n],a=i>>1,o=r instanceof Be?r.text.length:r.children.length;if(a==(t>0?o:0)){if(n==0)return this.done=!0,this.value=``,this;t>0&&this.offsets[n-1]++,this.nodes.pop(),this.offsets.pop()}else if((i&1)==(t>0?0:1)){if(this.offsets[n]+=t,e==0)return this.lineBreak=!0,this.value=`
571
571
  `,this;e--}else if(r instanceof Be){let i=r.text[a+(t<0?-1:0)];if(this.offsets[n]+=t,i.length>Math.max(0,e))return this.value=e==0?i:t>0?i.slice(e):i.slice(0,i.length-e),this;e-=i.length}else{let i=r.children[a+(t<0?-1:0)];e>i.length?(e-=i.length,this.offsets[n]+=t):(t<0&&this.offsets[n]--,this.nodes.push(i),this.offsets.push(t>0?1:(i instanceof Be?i.text.length:i.children.length)<<1))}}}next(e=0){return e<0&&(this.nextInner(-e,-this.dir),e=this.value.length),this.nextInner(e,this.dir)}},Ke=class{constructor(e,t,n){this.value=``,this.done=!1,this.cursor=new Ge(e,t>n?-1:1),this.pos=t>n?e.length:0,this.from=Math.min(t,n),this.to=Math.max(t,n)}nextInner(e,t){if(t<0?this.pos<=this.from:this.pos>=this.to)return this.value=``,this.done=!0,this;e+=Math.max(0,t<0?this.pos-this.to:this.from-this.pos);let n=t<0?this.pos-this.from:this.to-this.pos;e>n&&(e=n),n-=e;let{value:r}=this.cursor.next(e);return this.pos+=(r.length+e)*t,this.value=r.length<=n?r:t<0?r.slice(r.length-n):r.slice(0,n),this.done=!this.value,this}next(e=0){return e<0?e=Math.max(e,this.from-this.pos):e>0&&(e=Math.min(e,this.to-this.pos)),this.nextInner(e,this.cursor.dir)}get lineBreak(){return this.cursor.lineBreak&&this.value!=``}},qe=class{constructor(e){this.inner=e,this.afterBreak=!0,this.value=``,this.done=!1}next(e=0){let{done:t,lineBreak:n,value:r}=this.inner.next(e);return t&&this.afterBreak?(this.value=``,this.afterBreak=!1):t?(this.done=!0,this.value=``):n?this.afterBreak?this.value=``:(this.afterBreak=!0,this.next()):(this.value=r,this.afterBreak=!1),this}get lineBreak(){return!1}};typeof Symbol<`u`&&(A.prototype[Symbol.iterator]=function(){return this.iter()},Ge.prototype[Symbol.iterator]=Ke.prototype[Symbol.iterator]=qe.prototype[Symbol.iterator]=function(){return this});var Je=class{constructor(e,t,n,r){this.from=e,this.to=t,this.number=n,this.text=r}get length(){return this.to-this.from}};function Ye(e,t,n){return t=Math.max(0,Math.min(e.length,t)),[t,Math.max(t,Math.min(e.length,n))]}function Xe(e,t,n=!0,r=!0){return Ne(e,t,n,r)}function Ze(e){return e>=56320&&e<57344}function Qe(e){return e>=55296&&e<56320}function $e(e,t){let n=e.charCodeAt(t);if(!Qe(n)||t+1==e.length)return n;let r=e.charCodeAt(t+1);return Ze(r)?(n-55296<<10)+(r-56320)+65536:n}function et(e){return e<65536?1:2}const tt=/\r\n?|\n/;var nt=(function(e){return e[e.Simple=0]=`Simple`,e[e.TrackDel=1]=`TrackDel`,e[e.TrackBefore=2]=`TrackBefore`,e[e.TrackAfter=3]=`TrackAfter`,e})(nt||={}),rt=class e{constructor(e){this.sections=e}get length(){let e=0;for(let t=0;t<this.sections.length;t+=2)e+=this.sections[t];return e}get newLength(){let e=0;for(let t=0;t<this.sections.length;t+=2){let n=this.sections[t+1];e+=n<0?this.sections[t]:n}return e}get empty(){return this.sections.length==0||this.sections.length==2&&this.sections[1]<0}iterGaps(e){for(let t=0,n=0,r=0;t<this.sections.length;){let i=this.sections[t++],a=this.sections[t++];a<0?(e(n,r,i),r+=i):r+=a,n+=i}}iterChangedRanges(e,t=!1){st(this,e,t)}get invertedDesc(){let t=[];for(let e=0;e<this.sections.length;){let n=this.sections[e++],r=this.sections[e++];r<0?t.push(n,r):t.push(r,n)}return new e(t)}composeDesc(e){return this.empty?e:e.empty?this:lt(this,e)}mapDesc(e,t=!1){return e.empty?this:ct(this,e,t)}mapPos(e,t=-1,n=nt.Simple){let r=0,i=0;for(let a=0;a<this.sections.length;){let o=this.sections[a++],s=this.sections[a++],c=r+o;if(s<0){if(c>e)return i+(e-r);i+=o}else{if(n!=nt.Simple&&c>=e&&(n==nt.TrackDel&&r<e&&c>e||n==nt.TrackBefore&&r<e||n==nt.TrackAfter&&c>e))return null;if(c>e||c==e&&t<0&&!o)return e==r||t<0?i:i+s;i+=s}r=c}if(e>r)throw RangeError(`Position ${e} is out of range for changeset of length ${r}`);return i}touchesRange(e,t=e){for(let n=0,r=0;n<this.sections.length&&r<=t;){let i=this.sections[n++],a=this.sections[n++],o=r+i;if(a>=0&&r<=t&&o>=e)return r<e&&o>t?`cover`:!0;r=o}return!1}toString(){let e=``;for(let t=0;t<this.sections.length;){let n=this.sections[t++],r=this.sections[t++];e+=(e?` `:``)+n+(r>=0?`:`+r:``)}return e}toJSON(){return this.sections}static fromJSON(t){if(!Array.isArray(t)||t.length%2||t.some(e=>typeof e!=`number`))throw RangeError(`Invalid JSON representation of ChangeDesc`);return new e(t)}static create(t){return new e(t)}},it=class e extends rt{constructor(e,t){super(e),this.inserted=t}apply(e){if(this.length!=e.length)throw RangeError(`Applying change set to a document with the wrong length`);return st(this,(t,n,r,i,a)=>e=e.replace(r,r+(n-t),a),!1),e}mapDesc(e,t=!1){return ct(this,e,t,!0)}invert(t){let n=this.sections.slice(),r=[];for(let e=0,i=0;e<n.length;e+=2){let a=n[e],o=n[e+1];if(o>=0){n[e]=o,n[e+1]=a;let s=e>>1;for(;r.length<s;)r.push(A.empty);r.push(a?t.slice(i,i+a):A.empty)}i+=a}return new e(n,r)}compose(e){return this.empty?e:e.empty?this:lt(this,e,!0)}map(e,t=!1){return e.empty?this:ct(this,e,t,!0)}iterChanges(e,t=!1){st(this,e,t)}get desc(){return rt.create(this.sections)}filter(t){let n=[],r=[],i=[],a=new ut(this);done:for(let e=0,o=0;;){let s=e==t.length?1e9:t[e++];for(;o<s||o==s&&a.len==0;){if(a.done)break done;let e=Math.min(a.len,s-o);at(i,e,-1);let t=a.ins==-1?-1:a.off==0?a.ins:0;at(n,e,t),t>0&&ot(r,n,a.text),a.forward(e),o+=e}let c=t[e++];for(;o<c;){if(a.done)break done;let e=Math.min(a.len,c-o);at(n,e,-1),at(i,e,a.ins==-1?-1:a.off==0?a.ins:0),a.forward(e),o+=e}}return{changes:new e(n,r),filtered:rt.create(i)}}toJSON(){let e=[];for(let t=0;t<this.sections.length;t+=2){let n=this.sections[t],r=this.sections[t+1];r<0?e.push(n):r==0?e.push([n]):e.push([n].concat(this.inserted[t>>1].toJSON()))}return e}static of(t,n,r){let i=[],a=[],o=0,s=null;function c(t=!1){if(!t&&!i.length)return;o<n&&at(i,n-o,-1);let r=new e(i,a);s=s?s.compose(r.map(s)):r,i=[],a=[],o=0}function l(t){if(Array.isArray(t))for(let e of t)l(e);else if(t instanceof e){if(t.length!=n)throw RangeError(`Mismatched change set length (got ${t.length}, expected ${n})`);c(),s=s?s.compose(t.map(s)):t}else{let{from:e,to:s=e,insert:l}=t;if(e>s||e<0||s>n)throw RangeError(`Invalid change range ${e} to ${s} (in doc of length ${n})`);let u=l?typeof l==`string`?A.of(l.split(r||tt)):l:A.empty,d=u.length;if(e==s&&d==0)return;e<o&&c(),e>o&&at(i,e-o,-1),at(i,s-e,d),ot(a,i,u),o=s}}return l(t),c(!s),s}static empty(t){return new e(t?[t,-1]:[],[])}static fromJSON(t){if(!Array.isArray(t))throw RangeError(`Invalid JSON representation of ChangeSet`);let n=[],r=[];for(let e=0;e<t.length;e++){let i=t[e];if(typeof i==`number`)n.push(i,-1);else if(!Array.isArray(i)||typeof i[0]!=`number`||i.some((e,t)=>t&&typeof e!=`string`))throw RangeError(`Invalid JSON representation of ChangeSet`);else if(i.length==1)n.push(i[0],0);else{for(;r.length<e;)r.push(A.empty);r[e]=A.of(i.slice(1)),n.push(i[0],r[e].length)}}return new e(n,r)}static createSet(t,n){return new e(t,n)}};function at(e,t,n,r=!1){if(t==0&&n<=0)return;let i=e.length-2;i>=0&&n<=0&&n==e[i+1]?e[i]+=t:i>=0&&t==0&&e[i]==0?e[i+1]+=n:r?(e[i]+=t,e[i+1]+=n):e.push(t,n)}function ot(e,t,n){if(n.length==0)return;let r=t.length-2>>1;if(r<e.length)e[e.length-1]=e[e.length-1].append(n);else{for(;e.length<r;)e.push(A.empty);e.push(n)}}function st(e,t,n){let r=e.inserted;for(let i=0,a=0,o=0;o<e.sections.length;){let s=e.sections[o++],c=e.sections[o++];if(c<0)i+=s,a+=s;else{let l=i,u=a,d=A.empty;for(;l+=s,u+=c,c&&r&&(d=d.append(r[o-2>>1])),!(n||o==e.sections.length||e.sections[o+1]<0);)s=e.sections[o++],c=e.sections[o++];t(i,l,a,u,d),i=l,a=u}}}function ct(e,t,n,r=!1){let i=[],a=r?[]:null,o=new ut(e),s=new ut(t);for(let e=-1;;)if(o.done&&s.len||s.done&&o.len)throw Error(`Mismatched change set lengths`);else if(o.ins==-1&&s.ins==-1){let e=Math.min(o.len,s.len);at(i,e,-1),o.forward(e),s.forward(e)}else if(s.ins>=0&&(o.ins<0||e==o.i||o.off==0&&(s.len<o.len||s.len==o.len&&!n))){let t=s.len;for(at(i,s.ins,-1);t;){let n=Math.min(o.len,t);o.ins>=0&&e<o.i&&o.len<=n&&(at(i,0,o.ins),a&&ot(a,i,o.text),e=o.i),o.forward(n),t-=n}s.next()}else if(o.ins>=0){let t=0,n=o.len;for(;n;)if(s.ins==-1){let e=Math.min(n,s.len);t+=e,n-=e,s.forward(e)}else if(s.ins==0&&s.len<n)n-=s.len,s.next();else break;at(i,t,e<o.i?o.ins:0),a&&e<o.i&&ot(a,i,o.text),e=o.i,o.forward(o.len-n)}else if(o.done&&s.done)return a?it.createSet(i,a):rt.create(i);else throw Error(`Mismatched change set lengths`)}function lt(e,t,n=!1){let r=[],i=n?[]:null,a=new ut(e),o=new ut(t);for(let e=!1;;)if(a.done&&o.done)return i?it.createSet(r,i):rt.create(r);else if(a.ins==0)at(r,a.len,0,e),a.next();else if(o.len==0&&!o.done)at(r,0,o.ins,e),i&&ot(i,r,o.text),o.next();else if(a.done||o.done)throw Error(`Mismatched change set lengths`);else{let t=Math.min(a.len2,o.len),n=r.length;if(a.ins==-1){let n=o.ins==-1?-1:o.off?0:o.ins;at(r,t,n,e),i&&n&&ot(i,r,o.text)}else o.ins==-1?(at(r,a.off?0:a.len,t,e),i&&ot(i,r,a.textBit(t))):(at(r,a.off?0:a.len,o.off?0:o.ins,e),i&&!o.off&&ot(i,r,o.text));e=(a.ins>t||o.ins>=0&&o.len>t)&&(e||r.length>n),a.forward2(t),o.forward(t)}}var ut=class{constructor(e){this.set=e,this.i=0,this.next()}next(){let{sections:e}=this.set;this.i<e.length?(this.len=e[this.i++],this.ins=e[this.i++]):(this.len=0,this.ins=-2),this.off=0}get done(){return this.ins==-2}get len2(){return this.ins<0?this.len:this.ins}get text(){let{inserted:e}=this.set,t=this.i-2>>1;return t>=e.length?A.empty:e[t]}textBit(e){let{inserted:t}=this.set,n=this.i-2>>1;return n>=t.length&&!e?A.empty:t[n].slice(this.off,e==null?void 0:this.off+e)}forward(e){e==this.len?this.next():(this.len-=e,this.off+=e)}forward2(e){this.ins==-1?this.forward(e):e==this.ins?this.next():(this.ins-=e,this.off+=e)}},dt=class e{constructor(e,t,n,r){this.from=e,this.to=t,this.flags=n,this.goalColumn=r}get anchor(){return this.flags&32?this.to:this.from}get head(){return this.flags&32?this.from:this.to}get empty(){return this.from==this.to}get assoc(){return this.flags&8?-1:this.flags&16?1:0}get undirectional(){return(this.flags&64)>0}get bidiLevel(){let e=this.flags&7;return e==7?null:e}map(t,n=-1){let r,i;return this.empty?r=i=t.mapPos(this.from,n):(r=t.mapPos(this.from,1),i=t.mapPos(this.to,-1)),r==this.from&&i==this.to?this:new e(r,i,this.flags,this.goalColumn)}extend(e,t=e,n=0){if(e<=this.anchor&&t>=this.anchor)return j.range(e,t,void 0,void 0,n);let r=Math.abs(e-this.anchor)>Math.abs(t-this.anchor)?e:t;return j.range(this.anchor,r,void 0,void 0,n)}eq(e,t=!1){return this.anchor==e.anchor&&this.head==e.head&&this.goalColumn==e.goalColumn&&(!t||!this.empty||this.assoc==e.assoc)}toJSON(){return{anchor:this.anchor,head:this.head}}static fromJSON(e){if(!e||typeof e.anchor!=`number`||typeof e.head!=`number`)throw RangeError(`Invalid JSON representation for SelectionRange`);return j.range(e.anchor,e.head)}static create(t,n,r,i){return new e(t,n,r,i)}},j=class e{constructor(e,t){this.ranges=e,this.mainIndex=t}map(t,n=-1){return t.empty?this:e.create(this.ranges.map(e=>e.map(t,n)),this.mainIndex)}eq(e,t=!1){if(this.ranges.length!=e.ranges.length||this.mainIndex!=e.mainIndex)return!1;for(let n=0;n<this.ranges.length;n++)if(!this.ranges[n].eq(e.ranges[n],t))return!1;return!0}get main(){return this.ranges[this.mainIndex]}asSingle(){return this.ranges.length==1?this:new e([this.main],0)}addRange(t,n=!0){return e.create([t].concat(this.ranges),n?0:this.mainIndex+1)}replaceRange(t,n=this.mainIndex){let r=this.ranges.slice();return r[n]=t,e.create(r,this.mainIndex)}toJSON(){return{ranges:this.ranges.map(e=>e.toJSON()),main:this.mainIndex}}static fromJSON(t){if(!t||!Array.isArray(t.ranges)||typeof t.main!=`number`||t.main>=t.ranges.length)throw RangeError(`Invalid JSON representation for EditorSelection`);return new e(t.ranges.map(e=>dt.fromJSON(e)),t.main)}static single(t,n=t){return new e([e.range(t,n)],0)}static create(t,n=0){if(t.length==0)throw RangeError(`A selection needs at least one range`);for(let r=0,i=0;i<t.length;i++){let a=t[i];if(a.empty?a.from<=r:a.from<r)return e.normalized(t.slice(),n);r=a.to}return new e(t,n)}static cursor(e,t=0,n,r){return dt.create(e,e,(t==0?0:t<0?8:16)|(n==null?7:Math.min(6,n)),r)}static range(e,t,n,r,i){let a=r==null?7:Math.min(6,r);return!i&&e!=t&&(i=t<e?1:-1),i&&(a|=i<0?8:16),t<e?dt.create(t,e,a|32,n):dt.create(e,t,a,n)}static undirectionalRange(e,t){return dt.create(e,t,64,void 0)}static normalized(t,n=0){let r=t[n];t.sort((e,t)=>e.from-t.from),n=t.indexOf(r);for(let r=1;r<t.length;r++){let i=t[r],a=t[r-1];if(i.empty?i.from<=a.to:i.from<a.to){let o=a.from,s=Math.max(i.to,a.to);r<=n&&n--,t.splice(--r,2,i.anchor>i.head?e.range(s,o):e.range(o,s))}}return new e(t,n)}};function ft(e,t){for(let n of e.ranges)if(n.to>t)throw RangeError(`Selection points outside of document`)}let pt=0;var M=class e{constructor(e,t,n,r,i){this.combine=e,this.compareInput=t,this.compare=n,this.isStatic=r,this.id=pt++,this.default=e([]),this.extensions=typeof i==`function`?i(this):i}get reader(){return this}static define(t={}){return new e(t.combine||(e=>e),t.compareInput||((e,t)=>e===t),t.compare||(t.combine?(e,t)=>e===t:mt),!!t.static,t.enables)}of(e){return new ht([],this,0,e)}compute(e,t){if(this.isStatic)throw Error(`Can't compute a static facet`);return new ht(e,this,1,t)}computeN(e,t){if(this.isStatic)throw Error(`Can't compute a static facet`);return new ht(e,this,2,t)}from(e,t){return t||=e=>e,this.compute([e],n=>t(n.field(e)))}};function mt(e,t){return e==t||e.length==t.length&&e.every((e,n)=>e===t[n])}var ht=class{constructor(e,t,n,r){this.dependencies=e,this.facet=t,this.type=n,this.value=r,this.id=pt++}dynamicSlot(e){let t=this.value,n=this.facet.compareInput,r=this.id,i=e[r]>>1,a=this.type==2,o=!1,s=!1,c=[];for(let t of this.dependencies)t==`doc`?o=!0:t==`selection`?s=!0:(e[t.id]??1)&1||c.push(e[t.id]);return{create(e){return e.values[i]=t(e),1},update(e,r){if(o&&r.docChanged||s&&(r.docChanged||r.selection)||_t(e,c)){let r=t(e);if(a?!gt(r,e.values[i],n):!n(r,e.values[i]))return e.values[i]=r,1}return 0},reconfigure:(e,o)=>{let s,c=o.config.address[r];if(c!=null){let r=At(o,c);if(this.dependencies.every(t=>t instanceof M?o.facet(t)===e.facet(t):t instanceof bt?o.field(t,!1)==e.field(t,!1):!0)||(a?gt(s=t(e),r,n):n(s=t(e),r)))return e.values[i]=r,0}else s=t(e);return e.values[i]=s,1}}}get extension(){return this}};function gt(e,t,n){if(e.length!=t.length)return!1;for(let r=0;r<e.length;r++)if(!n(e[r],t[r]))return!1;return!0}function _t(e,t){let n=!1;for(let r of t)kt(e,r)&1&&(n=!0);return n}function vt(e,t,n){let r=n.map(t=>e[t.id]),i=n.map(e=>e.type),a=r.filter(e=>!(e&1)),o=e[t.id]>>1;function s(e){let n=[];for(let t=0;t<r.length;t++){let a=At(e,r[t]);if(i[t]==2)for(let e of a)n.push(e);else n.push(a)}return t.combine(n)}return{create(e){for(let t of r)kt(e,t);return e.values[o]=s(e),1},update(e,n){if(!_t(e,a))return 0;let r=s(e);return t.compare(r,e.values[o])?0:(e.values[o]=r,1)},reconfigure(e,i){let a=_t(e,r),c=i.config.facets[t.id],l=i.facet(t);if(c&&!a&&mt(n,c))return e.values[o]=l,0;let u=s(e);return t.compare(u,l)?(e.values[o]=l,0):(e.values[o]=u,1)}}}const yt=M.define({static:!0});var bt=class e{constructor(e,t,n,r,i){this.id=e,this.createF=t,this.updateF=n,this.compareF=r,this.spec=i,this.provides=void 0}static define(t){let n=new e(pt++,t.create,t.update,t.compare||((e,t)=>e===t),t);return t.provide&&(n.provides=t.provide(n)),n}create(e){return(e.facet(yt).find(e=>e.field==this)?.create||this.createF)(e)}slot(e){let t=e[this.id]>>1;return{create:e=>(e.values[t]=this.create(e),1),update:(e,n)=>{let r=e.values[t],i=this.updateF(r,n);return this.compareF(r,i)?0:(e.values[t]=i,1)},reconfigure:(e,n)=>{let r=e.facet(yt),i=n.facet(yt),a;return(a=r.find(e=>e.field==this))&&a!=i.find(e=>e.field==this)?(e.values[t]=a.create(e),1):n.config.address[this.id]==null?(e.values[t]=this.create(e),1):(e.values[t]=n.field(this),0)}}}init(e){return[this,yt.of({field:this,create:e})]}get extension(){return this}};const xt={lowest:4,low:3,default:2,high:1,highest:0};function St(e){return t=>new wt(t,e)}const Ct={highest:St(xt.highest),high:St(xt.high),default:St(xt.default),low:St(xt.low),lowest:St(xt.lowest)};var wt=class{constructor(e,t){this.inner=e,this.prec=t}get extension(){return this}},Tt=class e{of(e){return new Et(this,e)}reconfigure(t){return e.reconfigure.of({compartment:this,extension:t})}get(e){return e.config.compartments.get(this)}},Et=class{constructor(e,t){this.compartment=e,this.inner=t}get extension(){return this}},Dt=class e{constructor(e,t,n,r,i,a){for(this.base=e,this.compartments=t,this.dynamicSlots=n,this.address=r,this.staticValues=i,this.facets=a,this.statusTemplate=[];this.statusTemplate.length<n.length;)this.statusTemplate.push(0)}staticFacet(e){let t=this.address[e.id];return t==null?e.default:this.staticValues[t>>1]}static resolve(t,n,r){let i=[],a=Object.create(null),o=new Map;for(let e of Ot(t,n,o))e instanceof bt?i.push(e):(a[e.facet.id]||(a[e.facet.id]=[])).push(e);let s=Object.create(null),c=[],l=[];for(let e of i)s[e.id]=l.length<<1,l.push(t=>e.slot(t));let u=r?.config.facets;for(let e in a){let t=a[e],n=t[0].facet,i=u&&u[e]||[];if(t.every(e=>e.type==0))if(s[n.id]=c.length<<1|1,mt(i,t))c.push(r.facet(n));else{let e=n.combine(t.map(e=>e.value));c.push(r&&n.compare(e,r.facet(n))?r.facet(n):e)}else{for(let e of t)e.type==0?(s[e.id]=c.length<<1|1,c.push(e.value)):(s[e.id]=l.length<<1,l.push(t=>e.dynamicSlot(t)));s[n.id]=l.length<<1,l.push(e=>vt(e,n,t))}}let d=l.map(e=>e(s));return new e(t,o,d,s,c,a)}};function Ot(e,t,n){let r=[[],[],[],[],[]],i=new Map;function a(e,o){let s=i.get(e);if(s!=null){if(s<=o)return;let t=r[s].indexOf(e);t>-1&&r[s].splice(t,1),e instanceof Et&&n.delete(e.compartment)}if(i.set(e,o),Array.isArray(e))for(let t of e)a(t,o);else if(e instanceof Et){if(n.has(e.compartment))throw RangeError(`Duplicate use of compartment in extensions`);let r=t.get(e.compartment)||e.inner;n.set(e.compartment,r),a(r,o)}else if(e instanceof wt)a(e.inner,e.prec);else if(e instanceof bt)r[o].push(e),e.provides&&a(e.provides,o);else if(e instanceof ht)r[o].push(e),e.facet.extensions&&a(e.facet.extensions,xt.default);else{let t=e.extension;if(!t)throw Error(`Unrecognized extension value in extension set (${e}).`);if(t==e)throw Error(`Unrecognized extension value in extension set (${e}). This sometimes happens because multiple instances of @codemirror/state are loaded, breaking instanceof checks.`);a(t,o)}}return a(e,xt.default),r.reduce((e,t)=>e.concat(t))}function kt(e,t){if(t&1)return 2;let n=t>>1,r=e.status[n];if(r==4)throw Error(`Cyclic dependency between fields and/or facets`);if(r&2)return r;e.status[n]=4;let i=e.computeSlot(e,e.config.dynamicSlots[n]);return e.status[n]=2|i}function At(e,t){return t&1?e.config.staticValues[t>>1]:e.values[t>>1]}const jt=M.define(),Mt=M.define({combine:e=>e.some(e=>e),static:!0}),Nt=M.define({combine:e=>e.length?e[0]:void 0,static:!0}),Pt=M.define(),Ft=M.define(),It=M.define(),Lt=M.define({combine:e=>e.length?e[0]:!1});var Rt=class{constructor(e,t){this.type=e,this.value=t}static define(){return new zt}},zt=class{of(e){return new Rt(this,e)}},Bt=class{constructor(e){this.map=e}of(e){return new N(this,e)}},N=class e{constructor(e,t){this.type=e,this.value=t}map(t){let n=this.type.map(this.value,t);return n===void 0?void 0:n==this.value?this:new e(this.type,n)}is(e){return this.type==e}static define(e={}){return new Bt(e.map||(e=>e))}static mapEffects(e,t){if(!e.length)return e;let n=[];for(let r of e){let e=r.map(t);e&&n.push(e)}return n}};N.reconfigure=N.define(),N.appendConfig=N.define();var Vt=class e{constructor(t,n,r,i,a,o){this.startState=t,this.changes=n,this.selection=r,this.effects=i,this.annotations=a,this.scrollIntoView=o,this._doc=null,this._state=null,r&&ft(r,n.newLength),a.some(t=>t.type==e.time)||(this.annotations=a.concat(e.time.of(Date.now())))}static create(t,n,r,i,a,o){return new e(t,n,r,i,a,o)}get newDoc(){return this._doc||=this.changes.apply(this.startState.doc)}get newSelection(){return this.selection||this.startState.selection.map(this.changes)}get state(){return this._state||this.startState.applyTransaction(this),this._state}annotation(e){for(let t of this.annotations)if(t.type==e)return t.value}get docChanged(){return!this.changes.empty}get reconfigured(){return this.startState.config!=this.state.config}isUserEvent(t){let n=this.annotation(e.userEvent);return!!(n&&(n==t||n.length>t.length&&n.slice(0,t.length)==t&&n[t.length]==`.`))}};Vt.time=Rt.define(),Vt.userEvent=Rt.define(),Vt.addToHistory=Rt.define(),Vt.remote=Rt.define();function Ht(e,t){let n=[];for(let r=0,i=0;;){let a,o;if(r<e.length&&(i==t.length||t[i]>=e[r]))a=e[r++],o=e[r++];else if(i<t.length)a=t[i++],o=t[i++];else return n;!n.length||n[n.length-1]<a?n.push(a,o):n[n.length-1]<o&&(n[n.length-1]=o)}}function Ut(e,t,n){let r,i,a;return n?(r=t.changes,i=it.empty(t.changes.length),a=e.changes.compose(t.changes)):(r=t.changes.map(e.changes),i=e.changes.mapDesc(t.changes,!0),a=e.changes.compose(r)),{changes:a,selection:t.selection?t.selection.map(i):e.selection?.map(r),effects:N.mapEffects(e.effects,r).concat(N.mapEffects(t.effects,i)),annotations:e.annotations.length?e.annotations.concat(t.annotations):t.annotations,scrollIntoView:e.scrollIntoView||t.scrollIntoView}}function Wt(e,t,n){let r=t.selection,i=Yt(t.annotations);return t.userEvent&&(i=i.concat(Vt.userEvent.of(t.userEvent))),{changes:t.changes instanceof it?t.changes:it.of(t.changes||[],n,e.facet(Nt)),selection:r&&(r instanceof j?r:j.single(r.anchor,r.head)),effects:Yt(t.effects),annotations:i,scrollIntoView:!!t.scrollIntoView}}function Gt(e,t,n){let r=Wt(e,t.length?t[0]:{},e.doc.length);t.length&&t[0].filter===!1&&(n=!1);for(let i=1;i<t.length;i++){t[i].filter===!1&&(n=!1);let a=!!t[i].sequential;r=Ut(r,Wt(e,t[i],a?r.changes.newLength:e.doc.length),a)}let i=Vt.create(e,r.changes,r.selection,r.effects,r.annotations,r.scrollIntoView);return qt(n?Kt(i):i)}function Kt(e){let t=e.startState,n=!0;for(let r of t.facet(Pt)){let t=r(e);if(t===!1){n=!1;break}Array.isArray(t)&&(n=n===!0?t:Ht(n,t))}if(n!==!0){let r,i;if(n===!1)i=e.changes.invertedDesc,r=it.empty(t.doc.length);else{let t=e.changes.filter(n);r=t.changes,i=t.filtered.mapDesc(t.changes).invertedDesc}e=Vt.create(t,r,e.selection&&e.selection.map(i),N.mapEffects(e.effects,i),e.annotations,e.scrollIntoView)}let r=t.facet(Ft);for(let n=r.length-1;n>=0;n--){let i=r[n](e);e=i instanceof Vt?i:Array.isArray(i)&&i.length==1&&i[0]instanceof Vt?i[0]:Gt(t,Yt(i),!1)}return e}function qt(e){let t=e.startState,n=t.facet(It),r=e;for(let i=n.length-1;i>=0;i--){let a=n[i](e);a&&Object.keys(a).length&&(r=Ut(r,Wt(t,a,e.changes.newLength),!0))}return r==e?e:Vt.create(t,e.changes,e.selection,r.effects,r.annotations,r.scrollIntoView)}const Jt=[];function Yt(e){return e==null?Jt:Array.isArray(e)?e:[e]}var Xt=(function(e){return e[e.Word=0]=`Word`,e[e.Space=1]=`Space`,e[e.Other=2]=`Other`,e})(Xt||={});const Zt=/[\u00df\u0587\u0590-\u05f4\u0600-\u06ff\u3040-\u309f\u30a0-\u30ff\u3400-\u4db5\u4e00-\u9fcc\uac00-\ud7af]/;let Qt;try{Qt=RegExp(`[\\p{Alphabetic}\\p{Number}_]`,`u`)}catch{}function $t(e){if(Qt)return Qt.test(e);for(let t=0;t<e.length;t++){let n=e[t];if(/\w/.test(n)||n>`€`&&(n.toUpperCase()!=n.toLowerCase()||Zt.test(n)))return!0}return!1}function en(e){return t=>{if(!/\S/.test(t))return Xt.Space;if($t(t))return Xt.Word;for(let n=0;n<e.length;n++)if(t.indexOf(e[n])>-1)return Xt.Word;return Xt.Other}}var P=class e{constructor(e,t,n,r,i,a){this.config=e,this.doc=t,this.selection=n,this.values=r,this.status=e.statusTemplate.slice(),this.computeSlot=i,a&&(a._state=this);for(let e=0;e<this.config.dynamicSlots.length;e++)kt(this,e<<1);this.computeSlot=null}field(e,t=!0){let n=this.config.address[e.id];if(n==null){if(t)throw RangeError(`Field is not present in this state`);return}return kt(this,n),At(this,n)}update(...e){return Gt(this,e,!0)}applyTransaction(t){let n=this.config,{base:r,compartments:i}=n;for(let e of t.effects)e.is(Tt.reconfigure)?(n&&=(i=new Map,n.compartments.forEach((e,t)=>i.set(t,e)),null),i.set(e.value.compartment,e.value.extension)):e.is(N.reconfigure)?(n=null,r=e.value):e.is(N.appendConfig)&&(n=null,r=Yt(r).concat(e.value));let a;n?a=t.startState.values.slice():(n=Dt.resolve(r,i,this),a=new e(n,this.doc,this.selection,n.dynamicSlots.map(()=>null),(e,t)=>t.reconfigure(e,this),null).values);let o=t.startState.facet(Mt)?t.newSelection:t.newSelection.asSingle();new e(n,t.newDoc,o,a,(e,n)=>n.update(e,t),t)}replaceSelection(e){return typeof e==`string`&&(e=this.toText(e)),this.changeByRange(t=>({changes:{from:t.from,to:t.to,insert:e},range:j.cursor(t.from+e.length)}))}changeByRange(e){let t=this.selection,n=e(t.ranges[0]),r=this.changes(n.changes),i=[n.range],a=Yt(n.effects);for(let n=1;n<t.ranges.length;n++){let o=e(t.ranges[n]),s=this.changes(o.changes),c=s.map(r);for(let e=0;e<n;e++)i[e]=i[e].map(c);let l=r.mapDesc(s,!0);i.push(o.range.map(l)),r=r.compose(c),a=N.mapEffects(a,c).concat(N.mapEffects(Yt(o.effects),l))}return{changes:r,selection:j.create(i,t.mainIndex),effects:a}}changes(t=[]){return t instanceof it?t:it.of(t,this.doc.length,this.facet(e.lineSeparator))}toText(t){return A.of(t.split(this.facet(e.lineSeparator)||tt))}sliceDoc(e=0,t=this.doc.length){return this.doc.sliceString(e,t,this.lineBreak)}facet(e){let t=this.config.address[e.id];return t==null?e.default:(kt(this,t),At(this,t))}toJSON(e){let t={doc:this.sliceDoc(),selection:this.selection.toJSON()};if(e)for(let n in e){let r=e[n];r instanceof bt&&this.config.address[r.id]!=null&&(t[n]=r.spec.toJSON(this.field(e[n]),this))}return t}static fromJSON(t,n={},r){if(!t||typeof t.doc!=`string`)throw RangeError(`Invalid JSON representation for EditorState`);let i=[];if(r){for(let e in r)if(Object.prototype.hasOwnProperty.call(t,e)){let n=r[e],a=t[e];i.push(n.init(e=>n.spec.fromJSON(a,e)))}}return e.create({doc:t.doc,selection:j.fromJSON(t.selection),extensions:n.extensions?i.concat([n.extensions]):i})}static create(t={}){let n=Dt.resolve(t.extensions||[],new Map),r=t.doc instanceof A?t.doc:A.of((t.doc||``).split(n.staticFacet(e.lineSeparator)||tt)),i=t.selection?t.selection instanceof j?t.selection:j.single(t.selection.anchor,t.selection.head):j.single(0);return ft(i,r.length),n.staticFacet(Mt)||(i=i.asSingle()),new e(n,r,i,n.dynamicSlots.map(()=>null),(e,t)=>t.create(e),null)}get tabSize(){return this.facet(e.tabSize)}get lineBreak(){return this.facet(e.lineSeparator)||`
package/lib/client.js CHANGED
@@ -571,7 +571,7 @@ html[data-dsh-tw-active] .dshDesktopConversationSurface > :not([data-dsh-tw-view
571
571
  .dsh-tw-toolcard-row-tags { flex: none; font-size: 10px; color: color-mix(in srgb, var(--dsw-alias-label-primary, #222) 55%, transparent); }
572
572
  .dsh-tw-toolcard-row-meta { flex: none; font-size: 10px; color: color-mix(in srgb, var(--dsw-alias-label-primary, #222) 40%, transparent); }
573
573
  .dsh-tw-toolcard-tags-wrap { display: flex; flex-wrap: wrap; gap: 4px; }
574
- `,document.head.append(e))}var d=class{open=!1;listeners=new Set;isOpen(){return this.open}toggle(){this.set(!this.open)}openPanel(){this.set(!0)}closePanel(){this.set(!1)}set(e){if(this.open!==e){this.open=e;for(let t of[...this.listeners])t(e)}}subscribe(e){return this.listeners.add(e),()=>{this.listeners.delete(e)}}};function f(){let e=document.querySelector(`[data-pane="sidebar"], [class*="sidebarCol"], .dshDesktopUpstreamSidebar, .dshDesktopSidebarSurface`);if(e!==null)return e.querySelector(`[class*="logoRow"]`)?.parentElement??e.firstElementChild}function p(e){let t=e.querySelector(`button[class*="newSession"]`);if(t!==null)return t;for(let t of e.children)if(t instanceof HTMLButtonElement&&!t.matches(`[data-dsh-tw-entry]`))return t;let n=e.querySelector(`button[aria-label="新建会话"], button[aria-label="New Session"], button[aria-label*="新会话"], button[aria-label*="new session" i]`);return n===null?Array.from(e.querySelectorAll(`button`)).find(e=>!e.matches(`[data-dsh-tw-entry]`)&&/新会话|新建会话|new session/i.test(e.textContent??``)):n}function m(e){let t=document.createElement(`button`);return t.type=`button`,t.dataset.dshTwEntry=``,t.className=`dsh-tw-entry`,t.setAttribute(`aria-label`,`TiddlyWiki 知识库`),t.innerHTML=`<span class="dsh-tw-entry-icon"><svg viewBox="0 0 16 16" width="14" height="14" fill="none" stroke="currentColor" stroke-width="1.3" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M4 2.5h8a1 1 0 0 1 1 1v9a1 1 0 0 1-1 1H4a1 1 0 0 1-1-1v-9a1 1 0 0 1 1-1z"/><path d="M6 6h4M6 8.5h2.5"/></svg></span><span class="dsh-tw-entry-label">TiddlyWiki</span>`,t.addEventListener(`click`,()=>{e.toggle()}),t}function h(e,t){let n=p(e);if(n===void 0)return!1;if(t.parentElement!==e){let r=n.closest(`[class*="logoRow"]`),i=r!==null&&r.parentElement===e?r:n,a=Array.from(e.children).filter(e=>e instanceof HTMLElement&&e.matches(`[data-dsh-tw-entry], [data-dsh-atb-entry], [data-dsh-taskboard-entry], [data-dsh-ssh-entry]`)),o=a.length>0?a[0]??null:i.nextElementSibling??null;e.insertBefore(t,o)}return!0}function g(e){let t=m(e),n={attempts:0,found:!1,placed:!1},r=globalThis.location?.hostname;(r===`localhost`||r===`127.0.0.1`)&&(window.__twDebug=n);let i,a=!1,o=()=>{if(n.attempts++,i!==void 0&&!i.isConnected&&(c.disconnect(),i=void 0,a=!1),a){if(document.body.contains(t))return;c.disconnect(),i=void 0,a=!1}i??=f(),i!==void 0&&(n.found=p(i)!==void 0,a=h(i,t),n.placed=a,a&&c.observe(i,{childList:!0,subtree:!0}))},s=new MutationObserver(()=>{o()});s.observe(document.body,{childList:!0,subtree:!0});let c=new MutationObserver(()=>{if(i===void 0||!i.isConnected){a=!1,o();return}i.contains(t)||(a=h(i,t))}),l=setInterval(()=>{o()},2e3),u=()=>{e.isOpen()?t.dataset.active=`true`:delete t.dataset.active},d=e.subscribe(u);return u(),o(),()=>{clearInterval(l),s.disconnect(),c.disconnect(),d(),t.remove()}}const _=`$:/palettes/CupertinoDark`,v=`$:/palettes/Vanilla`,y=`data-ds-dark-theme`,b=`$:/palette`,x=new Set,S=new WeakSet;let C=``,w=!0,T=_;function ee(){try{let e=document.documentElement,t=document.body;if(t!==null&&t.hasAttribute(y))return!0;if(e!==null){let t=e.style.getPropertyValue(`color-scheme`).trim().toLowerCase();if(t===`dark`)return!0;if(t===`light`)return!1}}catch{}try{return window.matchMedia(`(prefers-color-scheme: dark)`).matches}catch{return!1}}function E(e){try{let t=e.wiki.getTiddlerText(b);return typeof t==`string`&&t.trim().length>0?t.trim():v}catch{return v}}function D(e){try{let t=e.wiki.getTiddler(E(e))?.fields;return t!==void 0&&t[`color-scheme`]===`dark`}catch{return!1}}function te(e){try{let t=e.syncer;if(t?.tiddlerInfo===void 0)return;let n=e.wiki.getChangeCount(b),r=t.tiddlerInfo[b];r===void 0?t.tiddlerInfo[b]={changeCount:n}:r.changeCount=n}catch{}}function ne(e){try{let t=e.syncer,n=t?.syncadaptor;if(t===void 0||n===void 0||S.has(t))return;let r=n.saveTiddler.bind(n);n.saveTiddler=(e,t)=>{if(w&&C.length>0&&e?.fields?.title===b&&e?.fields?.text===T){t(null,{},0);return}r(e,t)},S.add(t)}catch{}}function re(e,t){try{if(E(e)===t)return;e.wiki.setText(b,`text`,void 0,t),te(e)}catch{}}function ie(e,t){let n=E(e);if(t){if(D(e))return;n!==T&&(C=n),re(e,T)}else D(e)&&n===T&&C.length>0&&re(e,C)}function ae(e,t){try{let n=t.contentDocument?.documentElement;if(n==null)return;let r=D(e)?`dark`:`light`;n.style.getPropertyValue(`color-scheme`)!==r&&n.style.setProperty(`color-scheme`,r)}catch{}}function oe(e){let t=e.contentWindow?.$tw;t===void 0||t.wiki===void 0||(ne(t),w?ie(t,ee()):ie(t,!1),ae(t,e))}function se(e){let t=e.enabled!==!1,n=typeof e.darkPalette==`string`&&e.darkPalette.trim().length>0?e.darkPalette.trim():_,r=t!==w||n!==T;if(w=t,T=n,r)for(let e of[...x])e()}function ce(e){if(e===null)return()=>{};let t=()=>oe(e);x.add(t);let n=()=>t();e.addEventListener(`load`,n);let r=window.matchMedia(`(prefers-color-scheme: dark)`),i=()=>t();try{r.addEventListener(`change`,i)}catch{r.addListener?.(i)}let a;try{a=new MutationObserver(()=>t()),document.body!==null&&a.observe(document.body,{attributes:!0,attributeFilter:[y]}),document.documentElement!==null&&a.observe(document.documentElement,{attributes:!0,attributeFilter:[`style`]})}catch{a=void 0}let o=[150,600,1500,3e3].map(e=>window.setTimeout(t,e));return t(),()=>{x.delete(t),e.removeEventListener(`load`,n);for(let e of o)window.clearTimeout(e);try{r.removeEventListener(`change`,i)}catch{r.removeListener?.(i)}try{a?.disconnect()}catch{}}}const le=[`[class*="centerCol"]`,`[data-pane="conversation"]`,`.dshDesktopConversationSurface`],ue=`data-dsh-tw-active`,de=[`data-dsh-atb-active`,`data-dsh-taskboard-active`,`data-dsh-ssh-active`],fe=`dsh-panel-activate`,pe=`dsh-tiddlywiki`,me=`[data-dsh-panel-host]`,he=`dsh-tw-open-tiddler`;function ge(e){typeof document>`u`||typeof e!=`string`||e.length===0||document.dispatchEvent(new CustomEvent(he,{detail:{title:e}}))}function _e(){let e=document.querySelector(me);if(e===null)return 40;let t=parseInt(getComputedStyle(e).zIndex,10);return Number.isFinite(t)?Math.max(20,Math.min(40,t-1)):40}function ve(){for(let e of le){let t=document.querySelector(e);if(t!==null)return t}}async function ye(){try{let e=await fetch(`/dsh-tiddlywiki/status`,{signal:AbortSignal.timeout(8e3)});return e.ok?await e.json():null}catch{return null}}async function be(){try{return(await fetch(`/dsh-tiddlywiki/restart`,{method:`POST`,signal:AbortSignal.timeout(8e3)})).ok}catch{return!1}}function xe(e){let t,n,r,i,a,o,s=0,c,l=!1,u=null,d=()=>{let e=document.createElement(`div`);return e.dataset.dshTwView=``,e.className=`dsh-tw-view`,i=document.createElement(`div`),i.className=`dsh-tw-panel-frame-wrap`,i.style.cssText=`flex:1;min-height:0;display:flex;flex-direction:column`,r=document.createElement(`iframe`),r.className=`dsh-tw-panel-frame`,r.title=`TiddlyWiki`,r.hidden=!0,i.append(r),r.addEventListener(`load`,()=>{l=!0,v()}),c=ce(r),a=document.createElement(`div`),a.className=`dsh-tw-panel-error`,a.hidden=!0,e.append(i,a),e},f=()=>{if(t===void 0||n===void 0)return;let e=n.getBoundingClientRect();if(e.width===0&&e.height===0)return;let r=`${e.left}px`,i=`${e.top}px`,a=`${e.width}px`,o=`${e.height}px`;t.style.left!==r&&(t.style.left=r),t.style.top!==i&&(t.style.top=i),t.style.width!==a&&(t.style.width=a),t.style.height!==o&&(t.style.height=o)},p=()=>{t!==void 0&&(t.style.zIndex=String(_e()),document.querySelector(me)===null?delete t.dataset.sidebarHost:t.dataset.sidebarHost=`1`)},m=()=>{t===void 0&&(n=ve(),n!==void 0&&(t=d(),t.style.position=`fixed`,p(),document.body.append(t),f()))},h=e=>{if(r===void 0||a===void 0||i===void 0)return;r.hidden=!0,a.hidden=!1,a.textContent=``;let t=document.createElement(`div`);t.textContent=`TiddlyWiki 服务不可用`;let n=document.createElement(`code`);n.textContent=e;let o=document.createElement(`button`);o.type=`button`,o.textContent=`重试`,o.addEventListener(`click`,()=>{o.disabled=!0,o.textContent=`重启中…`,be().finally(()=>{b()})}),a.append(t,n,o)},g=()=>{if(r===void 0||a===void 0||i===void 0)return;r.hidden=!0,a.hidden=!1,a.textContent=``;let e=document.createElement(`div`);e.textContent=`TiddlyWiki 服务正在启动…`,a.append(e)},_=e=>{r===void 0||a===void 0||(a.hidden=!0,r.hidden=!1,r.dataset.loaded!==e&&(r.dataset.loaded=e,l=!1,r.src=e))},v=()=>{if(u===null||r===void 0||!l)return;let e=u;u=null;try{let t=r.contentWindow;if(t===null)throw Error(`iframe window unavailable`);if(t.location.hash===e)return;t.location.hash=e}catch{let t=r.src.split(`#`)[0];if(r.src===`${t}${e}`)return;r.src=`${t}${e}`}},y=t=>{let n=t.detail,r=typeof n?.title==`string`&&n.title.length>0?n.title:``;r.length!==0&&(u=`#${encodeURIComponent(r)}`,e.openPanel(),v())},b=async()=>{o!==void 0&&(window.clearTimeout(o),o=void 0);let e=await ye();if(e===null){h(`无法访问 /dsh-tiddlywiki/status`);return}if(e.status===`running`){s=0,se({enabled:e.ui?.followDshTheme!==!1,darkPalette:e.ui?.darkPalette}),typeof e.twProxy==`string`?_(new URL(e.twProxy,window.location.origin).href):typeof e.url==`string`?_(e.url):h(`服务未返回编辑器地址`);return}if(e.status===`starting`){g(),s<30&&(s++,o=window.setTimeout(()=>{b()},1500));return}s=0,h(e.error??`服务状态:${e.status}`)},x=()=>{if(e.isOpen()){for(let e of de)document.documentElement.removeAttribute(e);document.documentElement.setAttribute(ue,``),document.dispatchEvent(new CustomEvent(fe,{detail:pe})),b()}else document.documentElement.removeAttribute(ue),o!==void 0&&(window.clearTimeout(o),o=void 0),s=0},S=t=>{t.detail!==pe&&e.isOpen()&&e.closePanel()},C=t=>{if(!e.isOpen())return;let n=t.target;n!==null&&n.closest(`[data-dsh-tw-entry]`)===null&&n.closest(`[class*="sessionRow"], [class*="projectRow"], [class*="searchResultRow"], [class*="searchResultWorkspace"], [class*="newSession"]`)!==null&&e.closePanel()},w=new MutationObserver(()=>{m(),p()});w.observe(document.body,{childList:!0,subtree:!0});let T=new ResizeObserver(()=>f());T.observe(document.body);let ee=window.setInterval(f,2e3),E=()=>f();window.addEventListener(`resize`,E);let D=()=>f();window.addEventListener(`scroll`,D,!0),document.addEventListener(`click`,C,!0),document.addEventListener(fe,S);let te=e.subscribe(x);m(),x();let ne=()=>{r!==void 0&&!r.hidden&&(r.src=r.src)};return document.addEventListener(`dsh-tw-panel-reload`,ne),document.addEventListener(he,y),()=>{o!==void 0&&window.clearTimeout(o),window.clearInterval(ee),window.removeEventListener(`resize`,E),window.removeEventListener(`scroll`,D,!0),T.disconnect(),document.removeEventListener(`click`,C,!0),document.removeEventListener(fe,S),document.removeEventListener(`dsh-tw-panel-reload`,ne),document.removeEventListener(he,y),w.disconnect(),te(),c?.(),document.documentElement.removeAttribute(ue),t?.remove()}}function O(e){let t=document.querySelector(`.dsh-tw-toast`);t===null&&(t=document.createElement(`div`),t.className=`dsh-tw-toast`,document.body.append(t)),t.textContent=e,t.classList.add(`dsh-tw-toast-show`),clearTimeout(t.__twToastTimer),t.__twToastTimer=window.setTimeout(()=>{t?.classList.remove(`dsh-tw-toast-show`)},2500)}let k,Se,Ce,we;function Te(e,t){De(),!(k===void 0||Se===void 0)&&(Ce!==void 0&&(Ce.textContent=`TiddlyWiki 编辑器 · ${t}`),k.style.display=``,Se.src=e)}function Ee(){we?.(),we=void 0,k?.remove(),k=void 0,Se=void 0,Ce=void 0}function De(){if(k!==void 0&&Se!==void 0)return;k=document.createElement(`div`),k.className=`dsh-tw-editor-popup`,k.style.display=`none`;let e=document.createElement(`div`);e.className=`dsh-tw-editor-bar`,Ce=document.createElement(`span`),Ce.className=`dsh-tw-editor-title`,Ce.textContent=`TiddlyWiki 编辑器`;let t=document.createElement(`button`);t.type=`button`,t.className=`dsh-tw-editor-close`,t.textContent=`✕`,t.title=`关闭`,e.append(Ce,t),Se=document.createElement(`iframe`),Se.className=`dsh-tw-editor-frame`,Se.setAttribute(`sandbox`,`allow-scripts allow-same-origin allow-forms allow-popups`),Se.title=`TiddlyWiki 编辑器`,we=ce(Se);let n=document.createElement(`div`);n.className=`dsh-tw-editor-resize`,n.title=`拖拽调整大小`,k.append(e,Se,n),document.body.append(k),t.addEventListener(`click`,()=>{k!==void 0&&(k.style.display=`none`)}),e.addEventListener(`mousedown`,e=>{if(e.button!==0||k===void 0)return;e.preventDefault();let t=k.getBoundingClientRect(),n=e.clientX,r=e.clientY,i=t.left,a=t.top,o=e=>{k!==void 0&&(k.style.left=`${i+e.clientX-n}px`,k.style.top=`${a+e.clientY-r}px`,k.style.margin=`0`,k.style.right=`auto`,k.style.bottom=`auto`)},s=()=>{window.removeEventListener(`mousemove`,o),window.removeEventListener(`mouseup`,s)};window.addEventListener(`mousemove`,o),window.addEventListener(`mouseup`,s)}),n.addEventListener(`mousedown`,e=>{if(e.button!==0||k===void 0)return;e.preventDefault(),e.stopPropagation();let t=k.getBoundingClientRect(),n=e.clientX,r=e.clientY,i=t.width,a=t.height,o=e=>{k!==void 0&&(k.style.width=`${Math.max(360,i+e.clientX-n)}px`,k.style.height=`${Math.max(260,a+e.clientY-r)}px`,k.style.right=`auto`,k.style.bottom=`auto`)},s=()=>{window.removeEventListener(`mousemove`,o),window.removeEventListener(`mouseup`,s)};window.addEventListener(`mousemove`,o),window.addEventListener(`mouseup`,s)})}let Oe=[],ke=[];(()=>{let e=`lc,34,7n,7,7b,19,,,,2,,2,,,20,b,1c,l,g,,2t,7,2,6,2,2,,4,z,,u,r,2j,b,1m,9,9,,o,4,,9,,3,,5,17,3,1n,9,16,o,,x,1i,3,,i,,7,a,2,t,3,1k,,,7,2,2,2,3,9,,a,2,q,,2,3,1k,,,5,4,2,2,3,3,,u,2,3,,b,3,1k,,,8,,3,,3,k,2,m,6,,3,1k,,,7,2,2,2,3,7,3,a,2,u,,1n,5,3,3,,4,9,,14,5,1j,,,7,,3,,4,7,2,b,2,t,3,1k,,,7,,3,,4,7,2,b,2,f,,c,4,1j,2,,7,,3,,4,9,,a,2,t,3,1y,,4,6,,,,8,i,2,1p,,,8,c,8,2q,,,a,b,7,21,2,r,,,,,,4,2,1d,k,,2,5,b,,10,9,,2u,b,,6,n,4,4,3,g,4,d,,,3,6,,f,,jj,3,qa,4,s,3,t,2,u,2,1s,w,9,,19,3,,,39,2,y,,3a,c,4,c,63,5,1l,a,,,,,2,o,2,,1c,1a,2,c,k,5,1b,h,12,9,c,3,u,d,1k,e,1c,k,48,3,,l,4,,6,,2,3,5i,1s,ek,,5f,x,2da,3,3x,,2o,w,fe,6,2x,2,n9w,4,,a,w,2,28,2,7k,,3,,4,,n,5,4,,2b,2,1e,i,q,i,d,,12,8,p,d,18,4,1b,e,10,,1v,e,c,,8,2,1a,,1f,,,3,2,2,5,2,,,15,5,5,2,6k,8,,2,fn4,,kh,g,g,g,a6,2,gt,,6a,,45,5,1ae,3,,2,5,4,14,3,4,,4l,2,fx,4,1t,5,8t,2,25,6,1y,b,1d,4,3e,3,1h,f,15,,2,2,a,4,19,b,7,,1p,3,10,e,g,2,18,,c,3,1c,e,8,4,,2,2k,c,6,,2,,4d,c,l,4,1j,2,,7,2,2,2,3,9,,a,2,2,7,3,5,1v,9,,,2,,,4,,5,,,e,2,2a,i,n,,29,k,6j,7,2,9,r,2,2a,h,2y,d,2t,3,2,a,74,f,6t,6,,2,2,4,,,,2,3x,7,2,7,3,,s,a,14,7,,4,8,,9,b,1a,g,5i,8,5j,8,,8,2a,m,,e,3e,6,3,,,2,,7,,,1u,5,,2,,5,9n,4,9,2,,,1c,7,3,5,n,,44l,,6,f,8ug,i,1xc,5,1n,7,t4,,,1j,7,4,29,,b,2,f57,2,3mp,1a,2,n,f2,5,3,6,8,8,2,7,u,4,44,3,1iz,1j,4,1e,8,,e,,m,5,,f,11s,7,,h,2,7,,2,,5,2s,,4g,7,af,,1p,4,e4,4,72,2,6r,,2,,7,2,5,,d6,7,31,7,240,5`.split(`,`).map(e=>e?parseInt(e,36):1);for(let t=0,n=0;t<e.length;t++)(t%2?ke:Oe).push(n+=e[t])})();function Ae(e){if(e<768)return!1;for(let t=0,n=Oe.length;;){let r=t+n>>1;if(e<Oe[r])n=r;else if(e>=ke[r])t=r+1;else return!0;if(t==n)return!1}}function je(e){return e>=127462&&e<=127487}const Me=8205;function Ne(e,t,n=!0,r=!0){return(n?Pe:Fe)(e,t,r)}function Pe(e,t,n){if(t==e.length)return t;t&&Le(e.charCodeAt(t))&&Re(e.charCodeAt(t-1))&&t--;let r=Ie(e,t);for(t+=ze(r);t<e.length;){let i=Ie(e,t);if(r==Me||i==Me||n&&Ae(i))t+=ze(i),r=i;else if(je(i)){let n=0,r=t-2;for(;r>=0&&je(Ie(e,r));)n++,r-=2;if(n%2==0)break;t+=2}else break}return t}function Fe(e,t,n){for(;t>1;){let r=Pe(e,t-2,n);if(r<t)return r;t--}return 0}function Ie(e,t){let n=e.charCodeAt(t);if(!Re(n)||t+1==e.length)return n;let r=e.charCodeAt(t+1);return Le(r)?(n-55296<<10)+(r-56320)+65536:n}function Le(e){return e>=56320&&e<57344}function Re(e){return e>=55296&&e<56320}function ze(e){return e<65536?1:2}var A=class e{lineAt(e){if(e<0||e>this.length)throw RangeError(`Invalid position ${e} in document of length ${this.length}`);return this.lineInner(e,!1,1,0)}line(e){if(e<1||e>this.lines)throw RangeError(`Invalid line number ${e} in ${this.lines}-line document`);return this.lineInner(e,!0,1,0)}replace(e,t,n){[e,t]=Ye(this,e,t);let r=[];return this.decompose(0,e,r,2),n.length&&n.decompose(0,n.length,r,3),this.decompose(t,this.length,r,1),Ve.from(r,this.length-(t-e)+n.length)}append(e){return this.replace(this.length,this.length,e)}slice(e,t=this.length){[e,t]=Ye(this,e,t);let n=[];return this.decompose(e,t,n,0),Ve.from(n,t-e)}eq(e){if(e==this)return!0;if(e.length!=this.length||e.lines!=this.lines)return!1;let t=this.scanIdentical(e,1),n=this.length-this.scanIdentical(e,-1),r=new Ge(this),i=new Ge(e);for(let e=t,a=t;;){if(r.next(e),i.next(e),e=0,r.lineBreak!=i.lineBreak||r.done!=i.done||r.value!=i.value)return!1;if(a+=r.value.length,r.done||a>=n)return!0}}iter(e=1){return new Ge(this,e)}iterRange(e,t=this.length){return new Ke(this,e,t)}iterLines(e,t){let n;if(e==null)n=this.iter();else{t??=this.lines+1;let r=this.line(e).from;n=this.iterRange(r,Math.max(r,t==this.lines+1?this.length:t<=1?0:this.line(t-1).to))}return new qe(n)}toString(){return this.sliceString(0)}toJSON(){let e=[];return this.flatten(e),e}constructor(){}static of(t){if(t.length==0)throw RangeError(`A document must have at least one line`);return t.length==1&&!t[0]?e.empty:t.length<=32?new Be(t):Ve.from(Be.split(t,[]))}},Be=class e extends A{constructor(e,t=He(e)){super(),this.text=e,this.length=t}get lines(){return this.text.length}get children(){return null}lineInner(e,t,n,r){for(let i=0;;i++){let a=this.text[i],o=r+a.length;if((t?n:o)>=e)return new Je(r,o,n,a);r=o+1,n++}}decompose(t,n,r,i){let a=t<=0&&n>=this.length?this:new e(We(this.text,t,n),Math.min(n,this.length)-Math.max(0,t));if(i&1){let t=r.pop(),n=Ue(a.text,t.text.slice(),0,a.length);if(n.length<=32)r.push(new e(n,t.length+a.length));else{let t=n.length>>1;r.push(new e(n.slice(0,t)),new e(n.slice(t)))}}else r.push(a)}replace(t,n,r){if(!(r instanceof e))return super.replace(t,n,r);[t,n]=Ye(this,t,n);let i=Ue(this.text,Ue(r.text,We(this.text,0,t)),n),a=this.length+r.length-(n-t);return i.length<=32?new e(i,a):Ve.from(e.split(i,[]),a)}sliceString(e,t=this.length,n=`
574
+ `,document.head.append(e))}var d=class{open=!1;listeners=new Set;isOpen(){return this.open}toggle(){this.set(!this.open)}openPanel(){this.set(!0)}closePanel(){this.set(!1)}set(e){if(this.open!==e){this.open=e;for(let t of[...this.listeners])t(e)}}subscribe(e){return this.listeners.add(e),()=>{this.listeners.delete(e)}}};function f(){let e=document.querySelector(`[data-pane="sidebar"], [class*="sidebarCol"], .dshDesktopUpstreamSidebar, .dshDesktopSidebarSurface`);if(e!==null)return e.querySelector(`[class*="logoRow"]`)?.parentElement??e.firstElementChild}function p(e){let t=e.querySelector(`button[class*="newSession"]`);if(t!==null)return t;for(let t of e.children)if(t instanceof HTMLButtonElement&&!t.matches(`[data-dsh-tw-entry]`))return t;let n=e.querySelector(`button[aria-label="新建会话"], button[aria-label="New Session"], button[aria-label*="新会话"], button[aria-label*="new session" i]`);return n===null?Array.from(e.querySelectorAll(`button`)).find(e=>!e.matches(`[data-dsh-tw-entry]`)&&/新会话|新建会话|new session/i.test(e.textContent??``)):n}function m(e){let t=document.createElement(`button`);return t.type=`button`,t.dataset.dshTwEntry=``,t.className=`dsh-tw-entry`,t.setAttribute(`aria-label`,`TiddlyWiki 知识库`),t.innerHTML=`<span class="dsh-tw-entry-icon"><svg viewBox="0 0 16 16" width="14" height="14" fill="none" stroke="currentColor" stroke-width="1.3" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M4 2.5h8a1 1 0 0 1 1 1v9a1 1 0 0 1-1 1H4a1 1 0 0 1-1-1v-9a1 1 0 0 1 1-1z"/><path d="M6 6h4M6 8.5h2.5"/></svg></span><span class="dsh-tw-entry-label">TiddlyWiki</span>`,t.addEventListener(`click`,()=>{e.toggle()}),t}function h(e,t){let n=p(e);if(n===void 0)return!1;if(t.parentElement!==e){let r=n.closest(`[class*="logoRow"]`),i=r!==null&&r.parentElement===e?r:n,a=Array.from(e.children).filter(e=>e instanceof HTMLElement&&e.matches(`[data-dsh-tw-entry], [data-dsh-atb-entry], [data-dsh-taskboard-entry], [data-dsh-ssh-entry]`)),o=a.length>0?a[0]??null:i.nextElementSibling??null;e.insertBefore(t,o)}return!0}function g(e){let t=m(e),n={attempts:0,found:!1,placed:!1},r=globalThis.location?.hostname;(r===`localhost`||r===`127.0.0.1`)&&(window.__twDebug=n);let i,a=!1,o=()=>{if(n.attempts++,i!==void 0&&!i.isConnected&&(c.disconnect(),i=void 0,a=!1),a){if(document.body.contains(t))return;c.disconnect(),i=void 0,a=!1}i??=f(),i!==void 0&&(n.found=p(i)!==void 0,a=h(i,t),n.placed=a,a&&c.observe(i,{childList:!0,subtree:!0}))},s=new MutationObserver(()=>{o()});s.observe(document.body,{childList:!0,subtree:!0});let c=new MutationObserver(()=>{if(i===void 0||!i.isConnected){a=!1,o();return}i.contains(t)||(a=h(i,t))}),l=setInterval(()=>{o()},2e3),u=()=>{e.isOpen()?t.dataset.active=`true`:delete t.dataset.active},d=e.subscribe(u);return u(),o(),()=>{clearInterval(l),s.disconnect(),c.disconnect(),d(),t.remove()}}const _=`$:/palettes/CupertinoDark`,v=`$:/palettes/Vanilla`,y=`data-ds-dark-theme`,b=`$:/palette`,x=new Set,S=new WeakSet;let C=``,w=!0,T=_;function ee(){try{let e=document.documentElement,t=document.body;if(t!==null&&t.hasAttribute(y))return!0;if(e!==null){let t=e.style.getPropertyValue(`color-scheme`).trim().toLowerCase();if(t===`dark`)return!0;if(t===`light`)return!1}}catch{}try{return window.matchMedia(`(prefers-color-scheme: dark)`).matches}catch{return!1}}function E(e){try{let t=e.wiki.getTiddlerText(b);return typeof t==`string`&&t.trim().length>0?t.trim():v}catch{return v}}function D(e){try{let t=e.wiki.getTiddler(E(e))?.fields;return t!==void 0&&t[`color-scheme`]===`dark`}catch{return!1}}function te(e){try{let t=e.syncer;if(t?.tiddlerInfo===void 0)return;let n=e.wiki.getChangeCount(b),r=t.tiddlerInfo[b];r===void 0?t.tiddlerInfo[b]={changeCount:n}:r.changeCount=n}catch{}}function ne(e){try{let t=e.syncer,n=t?.syncadaptor;if(t===void 0||n===void 0||S.has(t))return;let r=n.saveTiddler.bind(n);n.saveTiddler=(e,t)=>{if(w&&C.length>0&&e?.fields?.title===b&&e?.fields?.text===T){t(null,{},0);return}r(e,t)},S.add(t)}catch{}}function re(e,t){try{if(E(e)===t)return;e.wiki.setText(b,`text`,void 0,t),te(e)}catch{}}function ie(e,t){let n=E(e);if(t){if(D(e))return;n!==T&&(C=n),re(e,T)}else D(e)&&n===T&&C.length>0&&re(e,C)}function ae(e,t){try{let n=t.contentDocument?.documentElement;if(n==null)return;let r=D(e)?`dark`:`light`;n.style.getPropertyValue(`color-scheme`)!==r&&n.style.setProperty(`color-scheme`,r)}catch{}}function oe(e){let t=e.contentWindow?.$tw;t===void 0||t.wiki===void 0||(ne(t),w?ie(t,ee()):ie(t,!1),ae(t,e))}function se(e){let t=e.enabled!==!1,n=typeof e.darkPalette==`string`&&e.darkPalette.trim().length>0?e.darkPalette.trim():_,r=t!==w||n!==T;if(w=t,T=n,r)for(let e of[...x])e()}function ce(e){if(e===null)return()=>{};let t=()=>oe(e);x.add(t);let n=()=>t();e.addEventListener(`load`,n);let r=window.matchMedia(`(prefers-color-scheme: dark)`),i=()=>t();try{r.addEventListener(`change`,i)}catch{r.addListener?.(i)}let a;try{a=new MutationObserver(()=>t()),document.body!==null&&a.observe(document.body,{attributes:!0,attributeFilter:[y]}),document.documentElement!==null&&a.observe(document.documentElement,{attributes:!0,attributeFilter:[`style`]})}catch{a=void 0}let o=[150,600,1500,3e3].map(e=>window.setTimeout(t,e));return t(),()=>{x.delete(t),e.removeEventListener(`load`,n);for(let e of o)window.clearTimeout(e);try{r.removeEventListener(`change`,i)}catch{r.removeListener?.(i)}try{a?.disconnect()}catch{}}}const le=[`[class*="centerCol"]`,`[data-pane="conversation"]`,`.dshDesktopConversationSurface`],ue=`data-dsh-tw-active`,de=[`data-dsh-atb-active`,`data-dsh-taskboard-active`,`data-dsh-ssh-active`],fe=`dsh-panel-activate`,pe=`dsh-tiddlywiki`,me=`[data-dsh-panel-host]`,he=`dsh-tw-open-tiddler`;function ge(e){typeof document>`u`||typeof e!=`string`||e.length===0||document.dispatchEvent(new CustomEvent(he,{detail:{title:e}}))}function _e(){let e=document.querySelector(me);if(e===null)return 40;let t=parseInt(getComputedStyle(e).zIndex,10);return Number.isFinite(t)?Math.max(20,Math.min(40,t-1)):40}function ve(){for(let e of le){let t=document.querySelector(e);if(t!==null)return t}}async function ye(){try{let e=await fetch(`/dsh-tiddlywiki/status`,{signal:AbortSignal.timeout(8e3)});return e.ok?await e.json():null}catch{return null}}async function be(){try{return(await fetch(`/dsh-tiddlywiki/restart`,{method:`POST`,signal:AbortSignal.timeout(8e3)})).ok}catch{return!1}}function xe(e){let t,n,r,i,a,o,s=0,c,l=!1,u=null,d=()=>{let e=document.createElement(`div`);return e.dataset.dshTwView=``,e.className=`dsh-tw-view`,i=document.createElement(`div`),i.className=`dsh-tw-panel-frame-wrap`,i.style.cssText=`flex:1;min-height:0;display:flex;flex-direction:column`,r=document.createElement(`iframe`),r.className=`dsh-tw-panel-frame`,r.title=`TiddlyWiki`,r.hidden=!0,i.append(r),r.addEventListener(`load`,()=>{l=!0,v()}),c=ce(r),a=document.createElement(`div`),a.className=`dsh-tw-panel-error`,a.hidden=!0,e.append(i,a),e},f=()=>{if(t===void 0||n===void 0)return;let e=n.getBoundingClientRect();if(e.width===0&&e.height===0)return;let r=`${e.left}px`,i=`${e.top}px`,a=`${e.width}px`,o=`${e.height}px`;t.style.left!==r&&(t.style.left=r),t.style.top!==i&&(t.style.top=i),t.style.width!==a&&(t.style.width=a),t.style.height!==o&&(t.style.height=o)},p=()=>{t!==void 0&&(t.style.zIndex=String(_e()),document.querySelector(me)===null?delete t.dataset.sidebarHost:t.dataset.sidebarHost=`1`)},m=()=>{t===void 0&&(n=ve(),n!==void 0&&(t=d(),t.style.position=`fixed`,p(),document.body.append(t),f()))},h=e=>{if(r===void 0||a===void 0||i===void 0)return;r.hidden=!0,a.hidden=!1,a.textContent=``;let t=document.createElement(`div`);t.textContent=`TiddlyWiki 服务不可用`;let n=document.createElement(`code`);n.textContent=e;let o=document.createElement(`button`);o.type=`button`,o.textContent=`重试`,o.addEventListener(`click`,()=>{o.disabled=!0,o.textContent=`重启中…`,be().finally(()=>{x()})}),a.append(t,n,o)},g=()=>{if(r===void 0||a===void 0||i===void 0)return;r.hidden=!0,a.hidden=!1,a.textContent=``;let e=document.createElement(`div`);e.textContent=`TiddlyWiki 服务正在启动…`,a.append(e)},_=e=>{r===void 0||a===void 0||(a.hidden=!0,r.hidden=!1,r.dataset.loaded!==e&&(r.dataset.loaded=e,l=!1,r.src=e))},v=()=>{if(u===null||r===void 0||!l)return;let e=u,t=r.contentWindow;if(t===null){y(e);return}let n=r=>{if(u!==e)return;let i=t;if(typeof i.$tw!=`object`||i.$tw===null){if(r<40){window.setTimeout(()=>n(r+1),150);return}y(e);return}u=null;try{if(t.location.hash===e)return;t.location.hash=e}catch{y(e)}};n(0)},y=e=>{if(r===void 0)return;u===e&&(u=null);let t=r.src.split(`#`)[0];r.src!==`${t}${e}`&&(r.src=`${t}${e}`)},b=t=>{let n=t.detail,r=typeof n?.title==`string`&&n.title.length>0?n.title:``;r.length!==0&&(u=`#${encodeURIComponent(r)}`,e.openPanel(),v())},x=async()=>{o!==void 0&&(window.clearTimeout(o),o=void 0);let e=await ye();if(e===null){h(`无法访问 /dsh-tiddlywiki/status`);return}if(e.status===`running`){s=0,se({enabled:e.ui?.followDshTheme!==!1,darkPalette:e.ui?.darkPalette}),typeof e.twProxy==`string`?_(new URL(e.twProxy,window.location.origin).href):typeof e.url==`string`?_(e.url):h(`服务未返回编辑器地址`);return}if(e.status===`starting`){g(),s<30&&(s++,o=window.setTimeout(()=>{x()},1500));return}s=0,h(e.error??`服务状态:${e.status}`)},S=()=>{if(e.isOpen()){for(let e of de)document.documentElement.removeAttribute(e);document.documentElement.setAttribute(ue,``),document.dispatchEvent(new CustomEvent(fe,{detail:pe})),x()}else document.documentElement.removeAttribute(ue),o!==void 0&&(window.clearTimeout(o),o=void 0),s=0},C=t=>{t.detail!==pe&&e.isOpen()&&e.closePanel()},w=t=>{if(!e.isOpen())return;let n=t.target;n!==null&&n.closest(`[data-dsh-tw-entry]`)===null&&n.closest(`[class*="sessionRow"], [class*="projectRow"], [class*="searchResultRow"], [class*="searchResultWorkspace"], [class*="newSession"]`)!==null&&e.closePanel()},T=new MutationObserver(()=>{m(),p()});T.observe(document.body,{childList:!0,subtree:!0});let ee=new ResizeObserver(()=>f());ee.observe(document.body);let E=window.setInterval(f,2e3),D=()=>f();window.addEventListener(`resize`,D);let te=()=>f();window.addEventListener(`scroll`,te,!0),document.addEventListener(`click`,w,!0),document.addEventListener(fe,C);let ne=e.subscribe(S);m(),S();let re=()=>{r!==void 0&&!r.hidden&&(r.src=r.src)};return document.addEventListener(`dsh-tw-panel-reload`,re),document.addEventListener(he,b),()=>{o!==void 0&&window.clearTimeout(o),window.clearInterval(E),window.removeEventListener(`resize`,D),window.removeEventListener(`scroll`,te,!0),ee.disconnect(),document.removeEventListener(`click`,w,!0),document.removeEventListener(fe,C),document.removeEventListener(`dsh-tw-panel-reload`,re),document.removeEventListener(he,b),T.disconnect(),ne(),c?.(),document.documentElement.removeAttribute(ue),t?.remove()}}function O(e){let t=document.querySelector(`.dsh-tw-toast`);t===null&&(t=document.createElement(`div`),t.className=`dsh-tw-toast`,document.body.append(t)),t.textContent=e,t.classList.add(`dsh-tw-toast-show`),clearTimeout(t.__twToastTimer),t.__twToastTimer=window.setTimeout(()=>{t?.classList.remove(`dsh-tw-toast-show`)},2500)}let k,Se,Ce,we;function Te(e,t){De(),!(k===void 0||Se===void 0)&&(Ce!==void 0&&(Ce.textContent=`TiddlyWiki 编辑器 · ${t}`),k.style.display=``,Se.src=e)}function Ee(){we?.(),we=void 0,k?.remove(),k=void 0,Se=void 0,Ce=void 0}function De(){if(k!==void 0&&Se!==void 0)return;k=document.createElement(`div`),k.className=`dsh-tw-editor-popup`,k.style.display=`none`;let e=document.createElement(`div`);e.className=`dsh-tw-editor-bar`,Ce=document.createElement(`span`),Ce.className=`dsh-tw-editor-title`,Ce.textContent=`TiddlyWiki 编辑器`;let t=document.createElement(`button`);t.type=`button`,t.className=`dsh-tw-editor-close`,t.textContent=`✕`,t.title=`关闭`,e.append(Ce,t),Se=document.createElement(`iframe`),Se.className=`dsh-tw-editor-frame`,Se.setAttribute(`sandbox`,`allow-scripts allow-same-origin allow-forms allow-popups`),Se.title=`TiddlyWiki 编辑器`,we=ce(Se);let n=document.createElement(`div`);n.className=`dsh-tw-editor-resize`,n.title=`拖拽调整大小`,k.append(e,Se,n),document.body.append(k),t.addEventListener(`click`,()=>{k!==void 0&&(k.style.display=`none`)}),e.addEventListener(`mousedown`,e=>{if(e.button!==0||k===void 0)return;e.preventDefault();let t=k.getBoundingClientRect(),n=e.clientX,r=e.clientY,i=t.left,a=t.top,o=e=>{k!==void 0&&(k.style.left=`${i+e.clientX-n}px`,k.style.top=`${a+e.clientY-r}px`,k.style.margin=`0`,k.style.right=`auto`,k.style.bottom=`auto`)},s=()=>{window.removeEventListener(`mousemove`,o),window.removeEventListener(`mouseup`,s)};window.addEventListener(`mousemove`,o),window.addEventListener(`mouseup`,s)}),n.addEventListener(`mousedown`,e=>{if(e.button!==0||k===void 0)return;e.preventDefault(),e.stopPropagation();let t=k.getBoundingClientRect(),n=e.clientX,r=e.clientY,i=t.width,a=t.height,o=e=>{k!==void 0&&(k.style.width=`${Math.max(360,i+e.clientX-n)}px`,k.style.height=`${Math.max(260,a+e.clientY-r)}px`,k.style.right=`auto`,k.style.bottom=`auto`)},s=()=>{window.removeEventListener(`mousemove`,o),window.removeEventListener(`mouseup`,s)};window.addEventListener(`mousemove`,o),window.addEventListener(`mouseup`,s)})}let Oe=[],ke=[];(()=>{let e=`lc,34,7n,7,7b,19,,,,2,,2,,,20,b,1c,l,g,,2t,7,2,6,2,2,,4,z,,u,r,2j,b,1m,9,9,,o,4,,9,,3,,5,17,3,1n,9,16,o,,x,1i,3,,i,,7,a,2,t,3,1k,,,7,2,2,2,3,9,,a,2,q,,2,3,1k,,,5,4,2,2,3,3,,u,2,3,,b,3,1k,,,8,,3,,3,k,2,m,6,,3,1k,,,7,2,2,2,3,7,3,a,2,u,,1n,5,3,3,,4,9,,14,5,1j,,,7,,3,,4,7,2,b,2,t,3,1k,,,7,,3,,4,7,2,b,2,f,,c,4,1j,2,,7,,3,,4,9,,a,2,t,3,1y,,4,6,,,,8,i,2,1p,,,8,c,8,2q,,,a,b,7,21,2,r,,,,,,4,2,1d,k,,2,5,b,,10,9,,2u,b,,6,n,4,4,3,g,4,d,,,3,6,,f,,jj,3,qa,4,s,3,t,2,u,2,1s,w,9,,19,3,,,39,2,y,,3a,c,4,c,63,5,1l,a,,,,,2,o,2,,1c,1a,2,c,k,5,1b,h,12,9,c,3,u,d,1k,e,1c,k,48,3,,l,4,,6,,2,3,5i,1s,ek,,5f,x,2da,3,3x,,2o,w,fe,6,2x,2,n9w,4,,a,w,2,28,2,7k,,3,,4,,n,5,4,,2b,2,1e,i,q,i,d,,12,8,p,d,18,4,1b,e,10,,1v,e,c,,8,2,1a,,1f,,,3,2,2,5,2,,,15,5,5,2,6k,8,,2,fn4,,kh,g,g,g,a6,2,gt,,6a,,45,5,1ae,3,,2,5,4,14,3,4,,4l,2,fx,4,1t,5,8t,2,25,6,1y,b,1d,4,3e,3,1h,f,15,,2,2,a,4,19,b,7,,1p,3,10,e,g,2,18,,c,3,1c,e,8,4,,2,2k,c,6,,2,,4d,c,l,4,1j,2,,7,2,2,2,3,9,,a,2,2,7,3,5,1v,9,,,2,,,4,,5,,,e,2,2a,i,n,,29,k,6j,7,2,9,r,2,2a,h,2y,d,2t,3,2,a,74,f,6t,6,,2,2,4,,,,2,3x,7,2,7,3,,s,a,14,7,,4,8,,9,b,1a,g,5i,8,5j,8,,8,2a,m,,e,3e,6,3,,,2,,7,,,1u,5,,2,,5,9n,4,9,2,,,1c,7,3,5,n,,44l,,6,f,8ug,i,1xc,5,1n,7,t4,,,1j,7,4,29,,b,2,f57,2,3mp,1a,2,n,f2,5,3,6,8,8,2,7,u,4,44,3,1iz,1j,4,1e,8,,e,,m,5,,f,11s,7,,h,2,7,,2,,5,2s,,4g,7,af,,1p,4,e4,4,72,2,6r,,2,,7,2,5,,d6,7,31,7,240,5`.split(`,`).map(e=>e?parseInt(e,36):1);for(let t=0,n=0;t<e.length;t++)(t%2?ke:Oe).push(n+=e[t])})();function Ae(e){if(e<768)return!1;for(let t=0,n=Oe.length;;){let r=t+n>>1;if(e<Oe[r])n=r;else if(e>=ke[r])t=r+1;else return!0;if(t==n)return!1}}function je(e){return e>=127462&&e<=127487}const Me=8205;function Ne(e,t,n=!0,r=!0){return(n?Pe:Fe)(e,t,r)}function Pe(e,t,n){if(t==e.length)return t;t&&Le(e.charCodeAt(t))&&Re(e.charCodeAt(t-1))&&t--;let r=Ie(e,t);for(t+=ze(r);t<e.length;){let i=Ie(e,t);if(r==Me||i==Me||n&&Ae(i))t+=ze(i),r=i;else if(je(i)){let n=0,r=t-2;for(;r>=0&&je(Ie(e,r));)n++,r-=2;if(n%2==0)break;t+=2}else break}return t}function Fe(e,t,n){for(;t>1;){let r=Pe(e,t-2,n);if(r<t)return r;t--}return 0}function Ie(e,t){let n=e.charCodeAt(t);if(!Re(n)||t+1==e.length)return n;let r=e.charCodeAt(t+1);return Le(r)?(n-55296<<10)+(r-56320)+65536:n}function Le(e){return e>=56320&&e<57344}function Re(e){return e>=55296&&e<56320}function ze(e){return e<65536?1:2}var A=class e{lineAt(e){if(e<0||e>this.length)throw RangeError(`Invalid position ${e} in document of length ${this.length}`);return this.lineInner(e,!1,1,0)}line(e){if(e<1||e>this.lines)throw RangeError(`Invalid line number ${e} in ${this.lines}-line document`);return this.lineInner(e,!0,1,0)}replace(e,t,n){[e,t]=Ye(this,e,t);let r=[];return this.decompose(0,e,r,2),n.length&&n.decompose(0,n.length,r,3),this.decompose(t,this.length,r,1),Ve.from(r,this.length-(t-e)+n.length)}append(e){return this.replace(this.length,this.length,e)}slice(e,t=this.length){[e,t]=Ye(this,e,t);let n=[];return this.decompose(e,t,n,0),Ve.from(n,t-e)}eq(e){if(e==this)return!0;if(e.length!=this.length||e.lines!=this.lines)return!1;let t=this.scanIdentical(e,1),n=this.length-this.scanIdentical(e,-1),r=new Ge(this),i=new Ge(e);for(let e=t,a=t;;){if(r.next(e),i.next(e),e=0,r.lineBreak!=i.lineBreak||r.done!=i.done||r.value!=i.value)return!1;if(a+=r.value.length,r.done||a>=n)return!0}}iter(e=1){return new Ge(this,e)}iterRange(e,t=this.length){return new Ke(this,e,t)}iterLines(e,t){let n;if(e==null)n=this.iter();else{t??=this.lines+1;let r=this.line(e).from;n=this.iterRange(r,Math.max(r,t==this.lines+1?this.length:t<=1?0:this.line(t-1).to))}return new qe(n)}toString(){return this.sliceString(0)}toJSON(){let e=[];return this.flatten(e),e}constructor(){}static of(t){if(t.length==0)throw RangeError(`A document must have at least one line`);return t.length==1&&!t[0]?e.empty:t.length<=32?new Be(t):Ve.from(Be.split(t,[]))}},Be=class e extends A{constructor(e,t=He(e)){super(),this.text=e,this.length=t}get lines(){return this.text.length}get children(){return null}lineInner(e,t,n,r){for(let i=0;;i++){let a=this.text[i],o=r+a.length;if((t?n:o)>=e)return new Je(r,o,n,a);r=o+1,n++}}decompose(t,n,r,i){let a=t<=0&&n>=this.length?this:new e(We(this.text,t,n),Math.min(n,this.length)-Math.max(0,t));if(i&1){let t=r.pop(),n=Ue(a.text,t.text.slice(),0,a.length);if(n.length<=32)r.push(new e(n,t.length+a.length));else{let t=n.length>>1;r.push(new e(n.slice(0,t)),new e(n.slice(t)))}}else r.push(a)}replace(t,n,r){if(!(r instanceof e))return super.replace(t,n,r);[t,n]=Ye(this,t,n);let i=Ue(this.text,Ue(r.text,We(this.text,0,t)),n),a=this.length+r.length-(n-t);return i.length<=32?new e(i,a):Ve.from(e.split(i,[]),a)}sliceString(e,t=this.length,n=`
575
575
  `){[e,t]=Ye(this,e,t);let r=``;for(let i=0,a=0;i<=t&&a<this.text.length;a++){let o=this.text[a],s=i+o.length;i>e&&a&&(r+=n),e<s&&t>i&&(r+=o.slice(Math.max(0,e-i),t-i)),i=s+1}return r}flatten(e){for(let t of this.text)e.push(t)}scanIdentical(){return 0}static split(t,n){let r=[],i=-1;for(let a of t)r.push(a),i+=a.length+1,r.length==32&&(n.push(new e(r,i)),r=[],i=-1);return i>-1&&n.push(new e(r,i)),n}},Ve=class e extends A{constructor(e,t){super(),this.children=e,this.length=t,this.lines=0;for(let t of e)this.lines+=t.lines}lineInner(e,t,n,r){for(let i=0;;i++){let a=this.children[i],o=r+a.length,s=n+a.lines-1;if((t?s:o)>=e)return a.lineInner(e,t,n,r);r=o+1,n=s+1}}decompose(e,t,n,r){for(let i=0,a=0;a<=t&&i<this.children.length;i++){let o=this.children[i],s=a+o.length;if(e<=s&&t>=a){let i=r&(a<=e|(s>=t?2:0));a>=e&&s<=t&&!i?n.push(o):o.decompose(e-a,t-a,n,i)}a=s+1}}replace(t,n,r){if([t,n]=Ye(this,t,n),r.lines<this.lines)for(let i=0,a=0;i<this.children.length;i++){let o=this.children[i],s=a+o.length;if(t>=a&&n<=s){let c=o.replace(t-a,n-a,r),l=this.lines-o.lines+c.lines;if(c.lines<l>>4&&c.lines>l>>6){let a=this.children.slice();return a[i]=c,new e(a,this.length-(n-t)+r.length)}return super.replace(a,s,c)}a=s+1}return super.replace(t,n,r)}sliceString(e,t=this.length,n=`
576
576
  `){[e,t]=Ye(this,e,t);let r=``;for(let i=0,a=0;i<this.children.length&&a<=t;i++){let o=this.children[i],s=a+o.length;a>e&&i&&(r+=n),e<s&&t>a&&(r+=o.sliceString(e-a,t-a,n)),a=s+1}return r}flatten(e){for(let t of this.children)t.flatten(e)}scanIdentical(t,n){if(!(t instanceof e))return 0;let r=0,[i,a,o,s]=n>0?[0,0,this.children.length,t.children.length]:[this.children.length-1,t.children.length-1,-1,-1];for(;;i+=n,a+=n){if(i==o||a==s)return r;let e=this.children[i],c=t.children[a];if(e!=c)return r+e.scanIdentical(c,n);r+=e.length+1}}static from(t,n=t.reduce((e,t)=>e+t.length+1,-1)){let r=0;for(let e of t)r+=e.lines;if(r<32){let e=[];for(let n of t)n.flatten(e);return new Be(e,n)}let i=Math.max(32,r>>5),a=i<<1,o=i>>1,s=[],c=0,l=-1,u=[];function d(t){let n;if(t.lines>a&&t instanceof e)for(let e of t.children)d(e);else t.lines>o&&(c>o||!c)?(f(),s.push(t)):t instanceof Be&&c&&(n=u[u.length-1])instanceof Be&&t.lines+n.lines<=32?(c+=t.lines,l+=t.length+1,u[u.length-1]=new Be(n.text.concat(t.text),n.length+1+t.length)):(c+t.lines>i&&f(),c+=t.lines,l+=t.length+1,u.push(t))}function f(){c!=0&&(s.push(u.length==1?u[0]:e.from(u,l)),l=-1,c=u.length=0)}for(let e of t)d(e);return f(),s.length==1?s[0]:new e(s,n)}};A.empty=new Be([``],0);function He(e){let t=-1;for(let n of e)t+=n.length+1;return t}function Ue(e,t,n=0,r=1e9){for(let i=0,a=0,o=!0;a<e.length&&i<=r;a++){let s=e[a],c=i+s.length;c>=n&&(c>r&&(s=s.slice(0,r-i)),i<n&&(s=s.slice(n-i)),o?(t[t.length-1]+=s,o=!1):t.push(s)),i=c+1}return t}function We(e,t,n){return Ue(e,[``],t,n)}var Ge=class{constructor(e,t=1){this.dir=t,this.done=!1,this.lineBreak=!1,this.value=``,this.nodes=[e],this.offsets=[t>0?1:(e instanceof Be?e.text.length:e.children.length)<<1]}nextInner(e,t){for(this.done=this.lineBreak=!1;;){let n=this.nodes.length-1,r=this.nodes[n],i=this.offsets[n],a=i>>1,o=r instanceof Be?r.text.length:r.children.length;if(a==(t>0?o:0)){if(n==0)return this.done=!0,this.value=``,this;t>0&&this.offsets[n-1]++,this.nodes.pop(),this.offsets.pop()}else if((i&1)==(t>0?0:1)){if(this.offsets[n]+=t,e==0)return this.lineBreak=!0,this.value=`
577
577
  `,this;e--}else if(r instanceof Be){let i=r.text[a+(t<0?-1:0)];if(this.offsets[n]+=t,i.length>Math.max(0,e))return this.value=e==0?i:t>0?i.slice(e):i.slice(0,i.length-e),this;e-=i.length}else{let i=r.children[a+(t<0?-1:0)];e>i.length?(e-=i.length,this.offsets[n]+=t):(t<0&&this.offsets[n]--,this.nodes.push(i),this.offsets.push(t>0?1:(i instanceof Be?i.text.length:i.children.length)<<1))}}}next(e=0){return e<0&&(this.nextInner(-e,-this.dir),e=this.value.length),this.nextInner(e,this.dir)}},Ke=class{constructor(e,t,n){this.value=``,this.done=!1,this.cursor=new Ge(e,t>n?-1:1),this.pos=t>n?e.length:0,this.from=Math.min(t,n),this.to=Math.max(t,n)}nextInner(e,t){if(t<0?this.pos<=this.from:this.pos>=this.to)return this.value=``,this.done=!0,this;e+=Math.max(0,t<0?this.pos-this.to:this.from-this.pos);let n=t<0?this.pos-this.from:this.to-this.pos;e>n&&(e=n),n-=e;let{value:r}=this.cursor.next(e);return this.pos+=(r.length+e)*t,this.value=r.length<=n?r:t<0?r.slice(r.length-n):r.slice(0,n),this.done=!this.value,this}next(e=0){return e<0?e=Math.max(e,this.from-this.pos):e>0&&(e=Math.min(e,this.to-this.pos)),this.nextInner(e,this.cursor.dir)}get lineBreak(){return this.cursor.lineBreak&&this.value!=``}},qe=class{constructor(e){this.inner=e,this.afterBreak=!0,this.value=``,this.done=!1}next(e=0){let{done:t,lineBreak:n,value:r}=this.inner.next(e);return t&&this.afterBreak?(this.value=``,this.afterBreak=!1):t?(this.done=!0,this.value=``):n?this.afterBreak?this.value=``:(this.afterBreak=!0,this.next()):(this.value=r,this.afterBreak=!1),this}get lineBreak(){return!1}};typeof Symbol<`u`&&(A.prototype[Symbol.iterator]=function(){return this.iter()},Ge.prototype[Symbol.iterator]=Ke.prototype[Symbol.iterator]=qe.prototype[Symbol.iterator]=function(){return this});var Je=class{constructor(e,t,n,r){this.from=e,this.to=t,this.number=n,this.text=r}get length(){return this.to-this.from}};function Ye(e,t,n){return t=Math.max(0,Math.min(e.length,t)),[t,Math.max(t,Math.min(e.length,n))]}function Xe(e,t,n=!0,r=!0){return Ne(e,t,n,r)}function Ze(e){return e>=56320&&e<57344}function Qe(e){return e>=55296&&e<56320}function $e(e,t){let n=e.charCodeAt(t);if(!Qe(n)||t+1==e.length)return n;let r=e.charCodeAt(t+1);return Ze(r)?(n-55296<<10)+(r-56320)+65536:n}function et(e){return e<65536?1:2}const tt=/\r\n?|\n/;var nt=(function(e){return e[e.Simple=0]=`Simple`,e[e.TrackDel=1]=`TrackDel`,e[e.TrackBefore=2]=`TrackBefore`,e[e.TrackAfter=3]=`TrackAfter`,e})(nt||={}),rt=class e{constructor(e){this.sections=e}get length(){let e=0;for(let t=0;t<this.sections.length;t+=2)e+=this.sections[t];return e}get newLength(){let e=0;for(let t=0;t<this.sections.length;t+=2){let n=this.sections[t+1];e+=n<0?this.sections[t]:n}return e}get empty(){return this.sections.length==0||this.sections.length==2&&this.sections[1]<0}iterGaps(e){for(let t=0,n=0,r=0;t<this.sections.length;){let i=this.sections[t++],a=this.sections[t++];a<0?(e(n,r,i),r+=i):r+=a,n+=i}}iterChangedRanges(e,t=!1){st(this,e,t)}get invertedDesc(){let t=[];for(let e=0;e<this.sections.length;){let n=this.sections[e++],r=this.sections[e++];r<0?t.push(n,r):t.push(r,n)}return new e(t)}composeDesc(e){return this.empty?e:e.empty?this:lt(this,e)}mapDesc(e,t=!1){return e.empty?this:ct(this,e,t)}mapPos(e,t=-1,n=nt.Simple){let r=0,i=0;for(let a=0;a<this.sections.length;){let o=this.sections[a++],s=this.sections[a++],c=r+o;if(s<0){if(c>e)return i+(e-r);i+=o}else{if(n!=nt.Simple&&c>=e&&(n==nt.TrackDel&&r<e&&c>e||n==nt.TrackBefore&&r<e||n==nt.TrackAfter&&c>e))return null;if(c>e||c==e&&t<0&&!o)return e==r||t<0?i:i+s;i+=s}r=c}if(e>r)throw RangeError(`Position ${e} is out of range for changeset of length ${r}`);return i}touchesRange(e,t=e){for(let n=0,r=0;n<this.sections.length&&r<=t;){let i=this.sections[n++],a=this.sections[n++],o=r+i;if(a>=0&&r<=t&&o>=e)return r<e&&o>t?`cover`:!0;r=o}return!1}toString(){let e=``;for(let t=0;t<this.sections.length;){let n=this.sections[t++],r=this.sections[t++];e+=(e?` `:``)+n+(r>=0?`:`+r:``)}return e}toJSON(){return this.sections}static fromJSON(t){if(!Array.isArray(t)||t.length%2||t.some(e=>typeof e!=`number`))throw RangeError(`Invalid JSON representation of ChangeDesc`);return new e(t)}static create(t){return new e(t)}},it=class e extends rt{constructor(e,t){super(e),this.inserted=t}apply(e){if(this.length!=e.length)throw RangeError(`Applying change set to a document with the wrong length`);return st(this,(t,n,r,i,a)=>e=e.replace(r,r+(n-t),a),!1),e}mapDesc(e,t=!1){return ct(this,e,t,!0)}invert(t){let n=this.sections.slice(),r=[];for(let e=0,i=0;e<n.length;e+=2){let a=n[e],o=n[e+1];if(o>=0){n[e]=o,n[e+1]=a;let s=e>>1;for(;r.length<s;)r.push(A.empty);r.push(a?t.slice(i,i+a):A.empty)}i+=a}return new e(n,r)}compose(e){return this.empty?e:e.empty?this:lt(this,e,!0)}map(e,t=!1){return e.empty?this:ct(this,e,t,!0)}iterChanges(e,t=!1){st(this,e,t)}get desc(){return rt.create(this.sections)}filter(t){let n=[],r=[],i=[],a=new ut(this);done:for(let e=0,o=0;;){let s=e==t.length?1e9:t[e++];for(;o<s||o==s&&a.len==0;){if(a.done)break done;let e=Math.min(a.len,s-o);at(i,e,-1);let t=a.ins==-1?-1:a.off==0?a.ins:0;at(n,e,t),t>0&&ot(r,n,a.text),a.forward(e),o+=e}let c=t[e++];for(;o<c;){if(a.done)break done;let e=Math.min(a.len,c-o);at(n,e,-1),at(i,e,a.ins==-1?-1:a.off==0?a.ins:0),a.forward(e),o+=e}}return{changes:new e(n,r),filtered:rt.create(i)}}toJSON(){let e=[];for(let t=0;t<this.sections.length;t+=2){let n=this.sections[t],r=this.sections[t+1];r<0?e.push(n):r==0?e.push([n]):e.push([n].concat(this.inserted[t>>1].toJSON()))}return e}static of(t,n,r){let i=[],a=[],o=0,s=null;function c(t=!1){if(!t&&!i.length)return;o<n&&at(i,n-o,-1);let r=new e(i,a);s=s?s.compose(r.map(s)):r,i=[],a=[],o=0}function l(t){if(Array.isArray(t))for(let e of t)l(e);else if(t instanceof e){if(t.length!=n)throw RangeError(`Mismatched change set length (got ${t.length}, expected ${n})`);c(),s=s?s.compose(t.map(s)):t}else{let{from:e,to:s=e,insert:l}=t;if(e>s||e<0||s>n)throw RangeError(`Invalid change range ${e} to ${s} (in doc of length ${n})`);let u=l?typeof l==`string`?A.of(l.split(r||tt)):l:A.empty,d=u.length;if(e==s&&d==0)return;e<o&&c(),e>o&&at(i,e-o,-1),at(i,s-e,d),ot(a,i,u),o=s}}return l(t),c(!s),s}static empty(t){return new e(t?[t,-1]:[],[])}static fromJSON(t){if(!Array.isArray(t))throw RangeError(`Invalid JSON representation of ChangeSet`);let n=[],r=[];for(let e=0;e<t.length;e++){let i=t[e];if(typeof i==`number`)n.push(i,-1);else if(!Array.isArray(i)||typeof i[0]!=`number`||i.some((e,t)=>t&&typeof e!=`string`))throw RangeError(`Invalid JSON representation of ChangeSet`);else if(i.length==1)n.push(i[0],0);else{for(;r.length<e;)r.push(A.empty);r[e]=A.of(i.slice(1)),n.push(i[0],r[e].length)}}return new e(n,r)}static createSet(t,n){return new e(t,n)}};function at(e,t,n,r=!1){if(t==0&&n<=0)return;let i=e.length-2;i>=0&&n<=0&&n==e[i+1]?e[i]+=t:i>=0&&t==0&&e[i]==0?e[i+1]+=n:r?(e[i]+=t,e[i+1]+=n):e.push(t,n)}function ot(e,t,n){if(n.length==0)return;let r=t.length-2>>1;if(r<e.length)e[e.length-1]=e[e.length-1].append(n);else{for(;e.length<r;)e.push(A.empty);e.push(n)}}function st(e,t,n){let r=e.inserted;for(let i=0,a=0,o=0;o<e.sections.length;){let s=e.sections[o++],c=e.sections[o++];if(c<0)i+=s,a+=s;else{let l=i,u=a,d=A.empty;for(;l+=s,u+=c,c&&r&&(d=d.append(r[o-2>>1])),!(n||o==e.sections.length||e.sections[o+1]<0);)s=e.sections[o++],c=e.sections[o++];t(i,l,a,u,d),i=l,a=u}}}function ct(e,t,n,r=!1){let i=[],a=r?[]:null,o=new ut(e),s=new ut(t);for(let e=-1;;)if(o.done&&s.len||s.done&&o.len)throw Error(`Mismatched change set lengths`);else if(o.ins==-1&&s.ins==-1){let e=Math.min(o.len,s.len);at(i,e,-1),o.forward(e),s.forward(e)}else if(s.ins>=0&&(o.ins<0||e==o.i||o.off==0&&(s.len<o.len||s.len==o.len&&!n))){let t=s.len;for(at(i,s.ins,-1);t;){let n=Math.min(o.len,t);o.ins>=0&&e<o.i&&o.len<=n&&(at(i,0,o.ins),a&&ot(a,i,o.text),e=o.i),o.forward(n),t-=n}s.next()}else if(o.ins>=0){let t=0,n=o.len;for(;n;)if(s.ins==-1){let e=Math.min(n,s.len);t+=e,n-=e,s.forward(e)}else if(s.ins==0&&s.len<n)n-=s.len,s.next();else break;at(i,t,e<o.i?o.ins:0),a&&e<o.i&&ot(a,i,o.text),e=o.i,o.forward(o.len-n)}else if(o.done&&s.done)return a?it.createSet(i,a):rt.create(i);else throw Error(`Mismatched change set lengths`)}function lt(e,t,n=!1){let r=[],i=n?[]:null,a=new ut(e),o=new ut(t);for(let e=!1;;)if(a.done&&o.done)return i?it.createSet(r,i):rt.create(r);else if(a.ins==0)at(r,a.len,0,e),a.next();else if(o.len==0&&!o.done)at(r,0,o.ins,e),i&&ot(i,r,o.text),o.next();else if(a.done||o.done)throw Error(`Mismatched change set lengths`);else{let t=Math.min(a.len2,o.len),n=r.length;if(a.ins==-1){let n=o.ins==-1?-1:o.off?0:o.ins;at(r,t,n,e),i&&n&&ot(i,r,o.text)}else o.ins==-1?(at(r,a.off?0:a.len,t,e),i&&ot(i,r,a.textBit(t))):(at(r,a.off?0:a.len,o.off?0:o.ins,e),i&&!o.off&&ot(i,r,o.text));e=(a.ins>t||o.ins>=0&&o.len>t)&&(e||r.length>n),a.forward2(t),o.forward(t)}}var ut=class{constructor(e){this.set=e,this.i=0,this.next()}next(){let{sections:e}=this.set;this.i<e.length?(this.len=e[this.i++],this.ins=e[this.i++]):(this.len=0,this.ins=-2),this.off=0}get done(){return this.ins==-2}get len2(){return this.ins<0?this.len:this.ins}get text(){let{inserted:e}=this.set,t=this.i-2>>1;return t>=e.length?A.empty:e[t]}textBit(e){let{inserted:t}=this.set,n=this.i-2>>1;return n>=t.length&&!e?A.empty:t[n].slice(this.off,e==null?void 0:this.off+e)}forward(e){e==this.len?this.next():(this.len-=e,this.off+=e)}forward2(e){this.ins==-1?this.forward(e):e==this.ins?this.next():(this.ins-=e,this.off+=e)}},dt=class e{constructor(e,t,n,r){this.from=e,this.to=t,this.flags=n,this.goalColumn=r}get anchor(){return this.flags&32?this.to:this.from}get head(){return this.flags&32?this.from:this.to}get empty(){return this.from==this.to}get assoc(){return this.flags&8?-1:this.flags&16?1:0}get undirectional(){return(this.flags&64)>0}get bidiLevel(){let e=this.flags&7;return e==7?null:e}map(t,n=-1){let r,i;return this.empty?r=i=t.mapPos(this.from,n):(r=t.mapPos(this.from,1),i=t.mapPos(this.to,-1)),r==this.from&&i==this.to?this:new e(r,i,this.flags,this.goalColumn)}extend(e,t=e,n=0){if(e<=this.anchor&&t>=this.anchor)return j.range(e,t,void 0,void 0,n);let r=Math.abs(e-this.anchor)>Math.abs(t-this.anchor)?e:t;return j.range(this.anchor,r,void 0,void 0,n)}eq(e,t=!1){return this.anchor==e.anchor&&this.head==e.head&&this.goalColumn==e.goalColumn&&(!t||!this.empty||this.assoc==e.assoc)}toJSON(){return{anchor:this.anchor,head:this.head}}static fromJSON(e){if(!e||typeof e.anchor!=`number`||typeof e.head!=`number`)throw RangeError(`Invalid JSON representation for SelectionRange`);return j.range(e.anchor,e.head)}static create(t,n,r,i){return new e(t,n,r,i)}},j=class e{constructor(e,t){this.ranges=e,this.mainIndex=t}map(t,n=-1){return t.empty?this:e.create(this.ranges.map(e=>e.map(t,n)),this.mainIndex)}eq(e,t=!1){if(this.ranges.length!=e.ranges.length||this.mainIndex!=e.mainIndex)return!1;for(let n=0;n<this.ranges.length;n++)if(!this.ranges[n].eq(e.ranges[n],t))return!1;return!0}get main(){return this.ranges[this.mainIndex]}asSingle(){return this.ranges.length==1?this:new e([this.main],0)}addRange(t,n=!0){return e.create([t].concat(this.ranges),n?0:this.mainIndex+1)}replaceRange(t,n=this.mainIndex){let r=this.ranges.slice();return r[n]=t,e.create(r,this.mainIndex)}toJSON(){return{ranges:this.ranges.map(e=>e.toJSON()),main:this.mainIndex}}static fromJSON(t){if(!t||!Array.isArray(t.ranges)||typeof t.main!=`number`||t.main>=t.ranges.length)throw RangeError(`Invalid JSON representation for EditorSelection`);return new e(t.ranges.map(e=>dt.fromJSON(e)),t.main)}static single(t,n=t){return new e([e.range(t,n)],0)}static create(t,n=0){if(t.length==0)throw RangeError(`A selection needs at least one range`);for(let r=0,i=0;i<t.length;i++){let a=t[i];if(a.empty?a.from<=r:a.from<r)return e.normalized(t.slice(),n);r=a.to}return new e(t,n)}static cursor(e,t=0,n,r){return dt.create(e,e,(t==0?0:t<0?8:16)|(n==null?7:Math.min(6,n)),r)}static range(e,t,n,r,i){let a=r==null?7:Math.min(6,r);return!i&&e!=t&&(i=t<e?1:-1),i&&(a|=i<0?8:16),t<e?dt.create(t,e,a|32,n):dt.create(e,t,a,n)}static undirectionalRange(e,t){return dt.create(e,t,64,void 0)}static normalized(t,n=0){let r=t[n];t.sort((e,t)=>e.from-t.from),n=t.indexOf(r);for(let r=1;r<t.length;r++){let i=t[r],a=t[r-1];if(i.empty?i.from<=a.to:i.from<a.to){let o=a.from,s=Math.max(i.to,a.to);r<=n&&n--,t.splice(--r,2,i.anchor>i.head?e.range(s,o):e.range(o,s))}}return new e(t,n)}};function ft(e,t){for(let n of e.ranges)if(n.to>t)throw RangeError(`Selection points outside of document`)}let pt=0;var M=class e{constructor(e,t,n,r,i){this.combine=e,this.compareInput=t,this.compare=n,this.isStatic=r,this.id=pt++,this.default=e([]),this.extensions=typeof i==`function`?i(this):i}get reader(){return this}static define(t={}){return new e(t.combine||(e=>e),t.compareInput||((e,t)=>e===t),t.compare||(t.combine?(e,t)=>e===t:mt),!!t.static,t.enables)}of(e){return new ht([],this,0,e)}compute(e,t){if(this.isStatic)throw Error(`Can't compute a static facet`);return new ht(e,this,1,t)}computeN(e,t){if(this.isStatic)throw Error(`Can't compute a static facet`);return new ht(e,this,2,t)}from(e,t){return t||=e=>e,this.compute([e],n=>t(n.field(e)))}};function mt(e,t){return e==t||e.length==t.length&&e.every((e,n)=>e===t[n])}var ht=class{constructor(e,t,n,r){this.dependencies=e,this.facet=t,this.type=n,this.value=r,this.id=pt++}dynamicSlot(e){let t=this.value,n=this.facet.compareInput,r=this.id,i=e[r]>>1,a=this.type==2,o=!1,s=!1,c=[];for(let t of this.dependencies)t==`doc`?o=!0:t==`selection`?s=!0:(e[t.id]??1)&1||c.push(e[t.id]);return{create(e){return e.values[i]=t(e),1},update(e,r){if(o&&r.docChanged||s&&(r.docChanged||r.selection)||_t(e,c)){let r=t(e);if(a?!gt(r,e.values[i],n):!n(r,e.values[i]))return e.values[i]=r,1}return 0},reconfigure:(e,o)=>{let s,c=o.config.address[r];if(c!=null){let r=At(o,c);if(this.dependencies.every(t=>t instanceof M?o.facet(t)===e.facet(t):t instanceof bt?o.field(t,!1)==e.field(t,!1):!0)||(a?gt(s=t(e),r,n):n(s=t(e),r)))return e.values[i]=r,0}else s=t(e);return e.values[i]=s,1}}}get extension(){return this}};function gt(e,t,n){if(e.length!=t.length)return!1;for(let r=0;r<e.length;r++)if(!n(e[r],t[r]))return!1;return!0}function _t(e,t){let n=!1;for(let r of t)kt(e,r)&1&&(n=!0);return n}function vt(e,t,n){let r=n.map(t=>e[t.id]),i=n.map(e=>e.type),a=r.filter(e=>!(e&1)),o=e[t.id]>>1;function s(e){let n=[];for(let t=0;t<r.length;t++){let a=At(e,r[t]);if(i[t]==2)for(let e of a)n.push(e);else n.push(a)}return t.combine(n)}return{create(e){for(let t of r)kt(e,t);return e.values[o]=s(e),1},update(e,n){if(!_t(e,a))return 0;let r=s(e);return t.compare(r,e.values[o])?0:(e.values[o]=r,1)},reconfigure(e,i){let a=_t(e,r),c=i.config.facets[t.id],l=i.facet(t);if(c&&!a&&mt(n,c))return e.values[o]=l,0;let u=s(e);return t.compare(u,l)?(e.values[o]=l,0):(e.values[o]=u,1)}}}const yt=M.define({static:!0});var bt=class e{constructor(e,t,n,r,i){this.id=e,this.createF=t,this.updateF=n,this.compareF=r,this.spec=i,this.provides=void 0}static define(t){let n=new e(pt++,t.create,t.update,t.compare||((e,t)=>e===t),t);return t.provide&&(n.provides=t.provide(n)),n}create(e){return(e.facet(yt).find(e=>e.field==this)?.create||this.createF)(e)}slot(e){let t=e[this.id]>>1;return{create:e=>(e.values[t]=this.create(e),1),update:(e,n)=>{let r=e.values[t],i=this.updateF(r,n);return this.compareF(r,i)?0:(e.values[t]=i,1)},reconfigure:(e,n)=>{let r=e.facet(yt),i=n.facet(yt),a;return(a=r.find(e=>e.field==this))&&a!=i.find(e=>e.field==this)?(e.values[t]=a.create(e),1):n.config.address[this.id]==null?(e.values[t]=this.create(e),1):(e.values[t]=n.field(this),0)}}}init(e){return[this,yt.of({field:this,create:e})]}get extension(){return this}};const xt={lowest:4,low:3,default:2,high:1,highest:0};function St(e){return t=>new wt(t,e)}const Ct={highest:St(xt.highest),high:St(xt.high),default:St(xt.default),low:St(xt.low),lowest:St(xt.lowest)};var wt=class{constructor(e,t){this.inner=e,this.prec=t}get extension(){return this}},Tt=class e{of(e){return new Et(this,e)}reconfigure(t){return e.reconfigure.of({compartment:this,extension:t})}get(e){return e.config.compartments.get(this)}},Et=class{constructor(e,t){this.compartment=e,this.inner=t}get extension(){return this}},Dt=class e{constructor(e,t,n,r,i,a){for(this.base=e,this.compartments=t,this.dynamicSlots=n,this.address=r,this.staticValues=i,this.facets=a,this.statusTemplate=[];this.statusTemplate.length<n.length;)this.statusTemplate.push(0)}staticFacet(e){let t=this.address[e.id];return t==null?e.default:this.staticValues[t>>1]}static resolve(t,n,r){let i=[],a=Object.create(null),o=new Map;for(let e of Ot(t,n,o))e instanceof bt?i.push(e):(a[e.facet.id]||(a[e.facet.id]=[])).push(e);let s=Object.create(null),c=[],l=[];for(let e of i)s[e.id]=l.length<<1,l.push(t=>e.slot(t));let u=r?.config.facets;for(let e in a){let t=a[e],n=t[0].facet,i=u&&u[e]||[];if(t.every(e=>e.type==0))if(s[n.id]=c.length<<1|1,mt(i,t))c.push(r.facet(n));else{let e=n.combine(t.map(e=>e.value));c.push(r&&n.compare(e,r.facet(n))?r.facet(n):e)}else{for(let e of t)e.type==0?(s[e.id]=c.length<<1|1,c.push(e.value)):(s[e.id]=l.length<<1,l.push(t=>e.dynamicSlot(t)));s[n.id]=l.length<<1,l.push(e=>vt(e,n,t))}}let d=l.map(e=>e(s));return new e(t,o,d,s,c,a)}};function Ot(e,t,n){let r=[[],[],[],[],[]],i=new Map;function a(e,o){let s=i.get(e);if(s!=null){if(s<=o)return;let t=r[s].indexOf(e);t>-1&&r[s].splice(t,1),e instanceof Et&&n.delete(e.compartment)}if(i.set(e,o),Array.isArray(e))for(let t of e)a(t,o);else if(e instanceof Et){if(n.has(e.compartment))throw RangeError(`Duplicate use of compartment in extensions`);let r=t.get(e.compartment)||e.inner;n.set(e.compartment,r),a(r,o)}else if(e instanceof wt)a(e.inner,e.prec);else if(e instanceof bt)r[o].push(e),e.provides&&a(e.provides,o);else if(e instanceof ht)r[o].push(e),e.facet.extensions&&a(e.facet.extensions,xt.default);else{let t=e.extension;if(!t)throw Error(`Unrecognized extension value in extension set (${e}).`);if(t==e)throw Error(`Unrecognized extension value in extension set (${e}). This sometimes happens because multiple instances of @codemirror/state are loaded, breaking instanceof checks.`);a(t,o)}}return a(e,xt.default),r.reduce((e,t)=>e.concat(t))}function kt(e,t){if(t&1)return 2;let n=t>>1,r=e.status[n];if(r==4)throw Error(`Cyclic dependency between fields and/or facets`);if(r&2)return r;e.status[n]=4;let i=e.computeSlot(e,e.config.dynamicSlots[n]);return e.status[n]=2|i}function At(e,t){return t&1?e.config.staticValues[t>>1]:e.values[t>>1]}const jt=M.define(),Mt=M.define({combine:e=>e.some(e=>e),static:!0}),Nt=M.define({combine:e=>e.length?e[0]:void 0,static:!0}),Pt=M.define(),Ft=M.define(),It=M.define(),Lt=M.define({combine:e=>e.length?e[0]:!1});var Rt=class{constructor(e,t){this.type=e,this.value=t}static define(){return new zt}},zt=class{of(e){return new Rt(this,e)}},Bt=class{constructor(e){this.map=e}of(e){return new N(this,e)}},N=class e{constructor(e,t){this.type=e,this.value=t}map(t){let n=this.type.map(this.value,t);return n===void 0?void 0:n==this.value?this:new e(this.type,n)}is(e){return this.type==e}static define(e={}){return new Bt(e.map||(e=>e))}static mapEffects(e,t){if(!e.length)return e;let n=[];for(let r of e){let e=r.map(t);e&&n.push(e)}return n}};N.reconfigure=N.define(),N.appendConfig=N.define();var Vt=class e{constructor(t,n,r,i,a,o){this.startState=t,this.changes=n,this.selection=r,this.effects=i,this.annotations=a,this.scrollIntoView=o,this._doc=null,this._state=null,r&&ft(r,n.newLength),a.some(t=>t.type==e.time)||(this.annotations=a.concat(e.time.of(Date.now())))}static create(t,n,r,i,a,o){return new e(t,n,r,i,a,o)}get newDoc(){return this._doc||=this.changes.apply(this.startState.doc)}get newSelection(){return this.selection||this.startState.selection.map(this.changes)}get state(){return this._state||this.startState.applyTransaction(this),this._state}annotation(e){for(let t of this.annotations)if(t.type==e)return t.value}get docChanged(){return!this.changes.empty}get reconfigured(){return this.startState.config!=this.state.config}isUserEvent(t){let n=this.annotation(e.userEvent);return!!(n&&(n==t||n.length>t.length&&n.slice(0,t.length)==t&&n[t.length]==`.`))}};Vt.time=Rt.define(),Vt.userEvent=Rt.define(),Vt.addToHistory=Rt.define(),Vt.remote=Rt.define();function Ht(e,t){let n=[];for(let r=0,i=0;;){let a,o;if(r<e.length&&(i==t.length||t[i]>=e[r]))a=e[r++],o=e[r++];else if(i<t.length)a=t[i++],o=t[i++];else return n;!n.length||n[n.length-1]<a?n.push(a,o):n[n.length-1]<o&&(n[n.length-1]=o)}}function Ut(e,t,n){let r,i,a;return n?(r=t.changes,i=it.empty(t.changes.length),a=e.changes.compose(t.changes)):(r=t.changes.map(e.changes),i=e.changes.mapDesc(t.changes,!0),a=e.changes.compose(r)),{changes:a,selection:t.selection?t.selection.map(i):e.selection?.map(r),effects:N.mapEffects(e.effects,r).concat(N.mapEffects(t.effects,i)),annotations:e.annotations.length?e.annotations.concat(t.annotations):t.annotations,scrollIntoView:e.scrollIntoView||t.scrollIntoView}}function Wt(e,t,n){let r=t.selection,i=Yt(t.annotations);return t.userEvent&&(i=i.concat(Vt.userEvent.of(t.userEvent))),{changes:t.changes instanceof it?t.changes:it.of(t.changes||[],n,e.facet(Nt)),selection:r&&(r instanceof j?r:j.single(r.anchor,r.head)),effects:Yt(t.effects),annotations:i,scrollIntoView:!!t.scrollIntoView}}function Gt(e,t,n){let r=Wt(e,t.length?t[0]:{},e.doc.length);t.length&&t[0].filter===!1&&(n=!1);for(let i=1;i<t.length;i++){t[i].filter===!1&&(n=!1);let a=!!t[i].sequential;r=Ut(r,Wt(e,t[i],a?r.changes.newLength:e.doc.length),a)}let i=Vt.create(e,r.changes,r.selection,r.effects,r.annotations,r.scrollIntoView);return qt(n?Kt(i):i)}function Kt(e){let t=e.startState,n=!0;for(let r of t.facet(Pt)){let t=r(e);if(t===!1){n=!1;break}Array.isArray(t)&&(n=n===!0?t:Ht(n,t))}if(n!==!0){let r,i;if(n===!1)i=e.changes.invertedDesc,r=it.empty(t.doc.length);else{let t=e.changes.filter(n);r=t.changes,i=t.filtered.mapDesc(t.changes).invertedDesc}e=Vt.create(t,r,e.selection&&e.selection.map(i),N.mapEffects(e.effects,i),e.annotations,e.scrollIntoView)}let r=t.facet(Ft);for(let n=r.length-1;n>=0;n--){let i=r[n](e);e=i instanceof Vt?i:Array.isArray(i)&&i.length==1&&i[0]instanceof Vt?i[0]:Gt(t,Yt(i),!1)}return e}function qt(e){let t=e.startState,n=t.facet(It),r=e;for(let i=n.length-1;i>=0;i--){let a=n[i](e);a&&Object.keys(a).length&&(r=Ut(r,Wt(t,a,e.changes.newLength),!0))}return r==e?e:Vt.create(t,e.changes,e.selection,r.effects,r.annotations,r.scrollIntoView)}const Jt=[];function Yt(e){return e==null?Jt:Array.isArray(e)?e:[e]}var Xt=(function(e){return e[e.Word=0]=`Word`,e[e.Space=1]=`Space`,e[e.Other=2]=`Other`,e})(Xt||={});const Zt=/[\u00df\u0587\u0590-\u05f4\u0600-\u06ff\u3040-\u309f\u30a0-\u30ff\u3400-\u4db5\u4e00-\u9fcc\uac00-\ud7af]/;let Qt;try{Qt=RegExp(`[\\p{Alphabetic}\\p{Number}_]`,`u`)}catch{}function $t(e){if(Qt)return Qt.test(e);for(let t=0;t<e.length;t++){let n=e[t];if(/\w/.test(n)||n>`€`&&(n.toUpperCase()!=n.toLowerCase()||Zt.test(n)))return!0}return!1}function en(e){return t=>{if(!/\S/.test(t))return Xt.Space;if($t(t))return Xt.Word;for(let n=0;n<e.length;n++)if(t.indexOf(e[n])>-1)return Xt.Word;return Xt.Other}}var P=class e{constructor(e,t,n,r,i,a){this.config=e,this.doc=t,this.selection=n,this.values=r,this.status=e.statusTemplate.slice(),this.computeSlot=i,a&&(a._state=this);for(let e=0;e<this.config.dynamicSlots.length;e++)kt(this,e<<1);this.computeSlot=null}field(e,t=!0){let n=this.config.address[e.id];if(n==null){if(t)throw RangeError(`Field is not present in this state`);return}return kt(this,n),At(this,n)}update(...e){return Gt(this,e,!0)}applyTransaction(t){let n=this.config,{base:r,compartments:i}=n;for(let e of t.effects)e.is(Tt.reconfigure)?(n&&=(i=new Map,n.compartments.forEach((e,t)=>i.set(t,e)),null),i.set(e.value.compartment,e.value.extension)):e.is(N.reconfigure)?(n=null,r=e.value):e.is(N.appendConfig)&&(n=null,r=Yt(r).concat(e.value));let a;n?a=t.startState.values.slice():(n=Dt.resolve(r,i,this),a=new e(n,this.doc,this.selection,n.dynamicSlots.map(()=>null),(e,t)=>t.reconfigure(e,this),null).values);let o=t.startState.facet(Mt)?t.newSelection:t.newSelection.asSingle();new e(n,t.newDoc,o,a,(e,n)=>n.update(e,t),t)}replaceSelection(e){return typeof e==`string`&&(e=this.toText(e)),this.changeByRange(t=>({changes:{from:t.from,to:t.to,insert:e},range:j.cursor(t.from+e.length)}))}changeByRange(e){let t=this.selection,n=e(t.ranges[0]),r=this.changes(n.changes),i=[n.range],a=Yt(n.effects);for(let n=1;n<t.ranges.length;n++){let o=e(t.ranges[n]),s=this.changes(o.changes),c=s.map(r);for(let e=0;e<n;e++)i[e]=i[e].map(c);let l=r.mapDesc(s,!0);i.push(o.range.map(l)),r=r.compose(c),a=N.mapEffects(a,c).concat(N.mapEffects(Yt(o.effects),l))}return{changes:r,selection:j.create(i,t.mainIndex),effects:a}}changes(t=[]){return t instanceof it?t:it.of(t,this.doc.length,this.facet(e.lineSeparator))}toText(t){return A.of(t.split(this.facet(e.lineSeparator)||tt))}sliceDoc(e=0,t=this.doc.length){return this.doc.sliceString(e,t,this.lineBreak)}facet(e){let t=this.config.address[e.id];return t==null?e.default:(kt(this,t),At(this,t))}toJSON(e){let t={doc:this.sliceDoc(),selection:this.selection.toJSON()};if(e)for(let n in e){let r=e[n];r instanceof bt&&this.config.address[r.id]!=null&&(t[n]=r.spec.toJSON(this.field(e[n]),this))}return t}static fromJSON(t,n={},r){if(!t||typeof t.doc!=`string`)throw RangeError(`Invalid JSON representation for EditorState`);let i=[];if(r){for(let e in r)if(Object.prototype.hasOwnProperty.call(t,e)){let n=r[e],a=t[e];i.push(n.init(e=>n.spec.fromJSON(a,e)))}}return e.create({doc:t.doc,selection:j.fromJSON(t.selection),extensions:n.extensions?i.concat([n.extensions]):i})}static create(t={}){let n=Dt.resolve(t.extensions||[],new Map),r=t.doc instanceof A?t.doc:A.of((t.doc||``).split(n.staticFacet(e.lineSeparator)||tt)),i=t.selection?t.selection instanceof j?t.selection:j.single(t.selection.anchor,t.selection.head):j.single(0);return ft(i,r.length),n.staticFacet(Mt)||(i=i.asSingle()),new e(n,r,i,n.dynamicSlots.map(()=>null),(e,t)=>t.create(e),null)}get tabSize(){return this.facet(e.tabSize)}get lineBreak(){return this.facet(e.lineSeparator)||`
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "dsh-tiddlywiki",
3
3
  "description": "TiddlyWiki 5 as the DSH persistent knowledge base: tiddlywiki_* agent tools (search with filters/recent/tags/batch/rename + git sync & conflict resolve), a full TiddlyWiki editor embedded in the GUI center column (same-origin proxy, works over Tailscale/LAN/domain), a quick-note card with draft auto-save & recent-notes picker behind a single knowledge FAB, and git-based sync with auto-commit. Mounts via the official dsh plugin system — no DSH source changes.",
4
- "version": "0.16.1",
4
+ "version": "0.16.2",
5
5
  "type": "module",
6
6
  "keywords": [
7
7
  "dsh",
@@ -263,21 +263,50 @@ export function mountPanel(state: PanelState): () => void {
263
263
  * Preferred: same-origin `contentWindow.location.hash` — a hashchange INSIDE
264
264
  * the frame (no reload), TW's story handler opens the tiddler. Fallback: a
265
265
  * full `iframe.src` reload with the hash (TW auto-saves drafts, acceptable).
266
+ *
267
+ * TW registers its hashchange listener in a STARTUP module that runs after
268
+ * the iframe's `load` event, so setting `location.hash` right on load is a
269
+ * no-op (hash lost → TW stays on its home page). We therefore wait for TW to
270
+ * become ready inside the frame (the `$tw` global appears once boot settles)
271
+ * before setting the hash; if it never does (service down / slow boot) we
272
+ * fall back to a full reload with the hash, which TW processes at startup.
273
+ * A newer open request supersedes an in-flight wait.
266
274
  */
267
275
  const applyPendingHash = (): void => {
268
276
  if (pendingHash === null || iframe === undefined || !frameLoaded) return
269
277
  const hash = pendingHash
270
- pendingHash = null
271
- try {
272
- const win = iframe.contentWindow
273
- if (win === null) throw new Error('iframe window unavailable')
274
- if (win.location.hash === hash) return
275
- win.location.hash = hash
276
- } catch {
277
- const base = iframe.src.split('#')[0]
278
- if (iframe.src === `${base}${hash}`) return
279
- iframe.src = `${base}${hash}`
278
+ const win = iframe.contentWindow
279
+ if (win === null) {
280
+ fallbackLoad(hash)
281
+ return
282
+ }
283
+ const tryOnce = (attempt: number): void => {
284
+ if (pendingHash !== hash) return // superseded by a newer request
285
+ const frameTw = win as { $tw?: unknown }
286
+ if (typeof frameTw.$tw !== 'object' || frameTw.$tw === null) {
287
+ if (attempt < 40) {
288
+ window.setTimeout(() => tryOnce(attempt + 1), 150)
289
+ return
290
+ }
291
+ fallbackLoad(hash)
292
+ return
293
+ }
294
+ pendingHash = null
295
+ try {
296
+ if (win.location.hash === hash) return
297
+ win.location.hash = hash
298
+ } catch {
299
+ fallbackLoad(hash)
300
+ }
280
301
  }
302
+ tryOnce(0)
303
+ }
304
+
305
+ const fallbackLoad = (hash: string): void => {
306
+ if (iframe === undefined) return
307
+ if (pendingHash === hash) pendingHash = null
308
+ const base = iframe.src.split('#')[0]
309
+ if (iframe.src !== `${base}${hash}`) iframe.src = `${base}${hash}`
281
310
  }
282
311
 
283
312
  const onOpenTiddler = (event: Event): void => {