@pretextbook/web-editor 0.14.0 → 0.15.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -434,8 +434,8 @@ $0`,documentation:`To add a watermark to the document`}},Qa=`alert.angles.articl
434
434
  `+e),e}let i=fs(n,t);return r&&(i=r+`
435
435
 
436
436
  `+i),i}function ms(e,t,n,r){switch(e.type){case`instruction`:t.push(`<?${e.name}${e.value?` `+e.value:``}?>`);break;case`doctype`:t.push(`<!DOCTYPE ${e.name}>`);break;case`comment`:t.push(`${r.ind.repeat(n)}<!--${e.value}-->`);break;case`cdata`:t.push(`${r.ind.repeat(n)}<![CDATA[${e.value}]]>`);break;case`text`:{let i=e.value.trim();i&&t.push(`${r.ind.repeat(n)}${Is(i)}`);break}case`element`:hs(e,t,n,r);break}}function hs(e,t,n,r){let i=e.name;as.includes(i)?gs(e,t,n,r):us.includes(i)?vs(e,t,n,r):ts.includes(i)?_s(e,t,n,r):ls.includes(i)&&!Fs(e)?ys(e,t,n,r):ls.includes(i)?bs(e,t,n,r):xs(e,t,n,r)}function gs(e,t,n,r){if(e.children.some(e=>e.type===`element`)){xs(e,t,n,r);return}let i=r.ind.repeat(n);if(Ms(e)){t.push(`${i}${Os(e)}`);return}let a=e.children.map(e=>e.type===`text`?Is(e.value):e.type===`cdata`?`<![CDATA[${e.value}]]>`:``).join(``);if(/\n[ \t]*$/.test(a)){let n=a.replace(/\n[ \t]*$/,`
437
- `);t.push(`${i}${Ds(e)}${n}${i}</${e.name}>`)}else t.push(`${i}${Ds(e)}${a}</${e.name}>`)}function _s(e,t,n,r){let i=r.ind.repeat(n);if(Ms(e)){t.push(`${i}${Os(e)}`);return}let a=Cs(e.children);t.push(`${i}${Ds(e)}${a}</${e.name}>`)}function vs(e,t,n,r){let i=r.ind.repeat(n);if(Ms(e)){t.push(`${i}${Os(e)}`);return}if(Fs(e)){bs(e,t,n,r);return}let a=Ts(e.children),o=r.ind.repeat(n+1),s=`${i}${Ds(e)}${a.join(` `)}</${e.name}>`;if(r.printWidth===0||s.length<=r.printWidth){t.push(s);return}t.push(`${i}${Ds(e)}`);for(let e of Es(a,r.printWidth,o.length,r.breakSentences))t.push(`${o}${e}`);t.push(`${i}</${e.name}>`)}function ys(e,t,n,r){let i=r.ind.repeat(n),a=r.ind.repeat(n+1);if(Ms(e)){t.push(`${i}${Os(e)}`);return}t.push(`${i}${Ds(e)}`);let o=Ts(e.children);for(let e of Es(o,r.printWidth,a.length,r.breakSentences))t.push(`${a}${e}`);t.push(`${i}</${e.name}>`)}function bs(e,t,n,r){let i=r.ind.repeat(n),a=r.ind.repeat(n+1);if(Ms(e)){t.push(`${i}${Os(e)}`);return}t.push(`${i}${Ds(e)}`);let o=Ns(e),s=0;for(;s<o.length;)if(Ps(o[s])){let e=o[s];s++;let i=``;if(s<o.length&&o[s].type===`text`){let e=o[s].value,t=/^(\s*)([.,;:!?])/.exec(e);if(t){i=t[2];let n=e.slice(t[0].length);o[s].value=n,n.trim()||s++}}let a=[];hs(e,a,n+1,r),i&&a.length>0&&(a[a.length-1]+=i),t.push(...a)}else{let e=[];for(;s<o.length&&!Ps(o[s]);)e.push(o[s]),s++;let n=Ts(e);if(n.length>0)for(let e of Es(n,r.printWidth,a.length,r.breakSentences))t.push(`${a}${e}`)}t.push(`${i}</${e.name}>`)}function xs(e,t,n,r){let i=r.ind.repeat(n);if(Ms(e)){t.push(...js(e,n,r,!0));return}let a=Ns(e);if(a.length===1&&a[0].type===`element`&&a[0].name===`xi:include`){let o=a[0],s=js(e,n,r);if(s.length===1){let n=Ms(o)?Os(o):`${Ds(o)}${Cs(o.children)}</${o.name}>`;t.push(`${s[0]}${n}</${e.name}>`);return}t.push(...s),ms(o,t,n+1,r),t.push(`${i}</${e.name}>`);return}t.push(...js(e,n,r));for(let i of Ns(e))ms(i,t,n+1,r);t.push(`${i}</${e.name}>`)}function Ss(e,t){let n=[];for(let r=0;r<e.length;r++){let i=e[r].trim(),a=r<e.length-1?e[r+1].trim():null;if(n.push(e[r]),r===0&&/^<\?xml(?:\s|\?>)/.test(i)){n.push(``);continue}if(t.blankLines!==`few`)if(t.blankLines===`some`)if(i.startsWith(`</`)&&a!==null&&a.startsWith(`<`)){let e=/^<([^\s>/]+)/.exec(a)?.[1];e&&ss.includes(e)&&n.push(``)}else /^<title[\s>]/.test(i)&&i.includes(`</title>`)&&n.push(``);else t.blankLines===`many`&&(i.startsWith(`</`)||i.startsWith(`<`)&&a!==null&&a.startsWith(`<`))&&n.push(``)}return n}function Cs(e){return e.map(e=>e.type===`text`?Is(e.value):e.type===`element`?ws(e):e.type===`comment`?`<!--${e.value}-->`:e.type===`cdata`?`<![CDATA[${e.value}]]>`:``).join(``)}function ws(e){return Ms(e)?Os(e):`${Ds(e)}${Cs(e.children)}</${e.name}>`}function Ts(e){let t=[];for(let n of e)if(n.type===`text`){let e=n.value.split(/\s+/).filter(e=>e.length>0);for(let n=0;n<e.length;n++){let r=Is(e[n]);n===0&&t.length>0&&/^[.,;:!?\-]/.test(r)?t[t.length-1]+=r:t.push(r)}}else n.type===`element`?t.push(ws(n)):n.type===`comment`&&t.push(`<!--${n.value}-->`);return t}function Es(e,t,n,r){if(e.length===0)return[];let i=t===0?1/0:Math.max(20,t-n),a=[],o=``;for(let t=0;t<e.length;t++){let n=e[t];o===``?o=n:o.length+1+n.length<=i?o+=` `+n:(a.push(o),o=n),r&&/[.!?]$/.test(n)&&t<e.length-1&&(a.push(o),o=``)}return o&&a.push(o),a}function Ds(e){let t=ks(e);return t?`<${e.name} ${t}>`:`<${e.name}>`}function Os(e){let t=ks(e);return t?`<${e.name} ${t}/>`:`<${e.name}/>`}function ks(e){return As(e).join(` `)}function As(e){return Object.entries(e.attributes||{}).map(([e,t])=>t==null?e:`${e}="${Ls(t)}"`)}function js(e,t,n,r=!1){let i=n.ind.repeat(t),a=As(e),o=r?`/>`:`>`;if(a.length===0)return[`${i}<${e.name}${o}`];let s=`${i}<${e.name} ${a.join(` `)}${o}`;if(!n.breakLongAttributes||n.printWidth===0||s.length<=n.printWidth)return[s];let c=`${i}${` `.repeat(e.name.length+2)}`,l=[`${i}<${e.name} ${a[0]}`];for(let e of a.slice(1))l.push(`${c}${e}`);return l[l.length-1]+=o,l}function Ms(e){return e.children.every(e=>e.type===`text`&&e.value.trim()===``)}function Ns(e){return e.children.filter(e=>!(e.type===`text`&&e.value.trim()===``))}function Ps(e){if(e.type!==`element`)return!1;let t=e.name;return t===`c`?!1:(cs.includes(t)||as.includes(t))&&!ts.includes(t)}function Fs(e){return e.children.some(e=>Ps(e))}function Is(e){return e.replace(/&/g,`&amp;`).replace(/</g,`&lt;`).replace(/>/g,`&gt;`)}function Ls(e){return e.replace(/&/g,`&amp;`).replace(/"/g,`&quot;`).replace(/</g,`&lt;`)}var Rs=6,zs=({actions:e})=>{let r=(0,n.useRef)(null),i=(0,n.useRef)(null),a=(0,n.useRef)(null),o=(0,n.useRef)([]),s=(0,n.useRef)(0),[c,l]=(0,n.useState)(e.length),[u,d]=(0,n.useState)(!1),f=(0,n.useCallback)(()=>{let e=r.current,t=o.current;if(!e||t.length===0)return;let n=e.clientWidth,i=t.length>0?Rs*(t.length-1):0;if(t.reduce((e,t)=>e+t,0)+i<=n){l(t.length);return}let a=s.current,c=0;for(let e=0;e<t.length;e++){let r=a+t[e]+Rs;if(r<=n)a=r,c++;else break}l(c)},[]);(0,n.useLayoutEffect)(()=>{let e=i.current;if(!e)return;o.current=Array.from(e.querySelectorAll(`[data-measure-btn]`)).map(e=>e.offsetWidth);let t=e.querySelector(`[data-measure-more]`);s.current=t?t.offsetWidth+Rs:0,f()},[e.map(e=>`${e.key}:${e.label}:${+!!e.disabled}`).join(`|`),f]),(0,n.useEffect)(()=>{let e=r.current;if(!e||typeof ResizeObserver>`u`)return;let t=new ResizeObserver(()=>f());return t.observe(e),()=>t.disconnect()},[f]),(0,n.useEffect)(()=>{if(!u)return;let e=e=>{a.current?.contains(e.target)||d(!1)},t=e=>{e.key===`Escape`&&d(!1)};return document.addEventListener(`mousedown`,e),document.addEventListener(`keydown`,t),()=>{document.removeEventListener(`mousedown`,e),document.removeEventListener(`keydown`,t)}},[u]);let p=e.slice(0,c),m=e.slice(c);return(0,t.jsxs)(`div`,{className:`pretext-plus-editor__menu-actions`,ref:r,children:[p.map(e=>(0,t.jsx)(`button`,{className:`pretext-plus-editor__menu-button ${e.className??``}`,onClick:e.onClick,disabled:e.disabled,title:e.title,children:e.label},e.key)),m.length>0&&(0,t.jsxs)(`div`,{className:`pretext-plus-editor__menu-dropdown`,ref:a,children:[(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__menu-button`,onClick:()=>d(e=>!e),"aria-haspopup":`menu`,"aria-expanded":u,title:`More actions`,children:`More ▾`}),u&&(0,t.jsx)(`div`,{className:`pretext-plus-editor__menu-dropdown-list`,role:`menu`,children:m.map(e=>(0,t.jsx)(`button`,{type:`button`,role:`menuitem`,className:`pretext-plus-editor__menu-dropdown-item`,onClick:()=>{d(!1),e.onClick()},disabled:e.disabled,title:e.title,children:e.label},e.key))})]}),(0,t.jsxs)(`div`,{className:`pretext-plus-editor__menu-measure`,ref:i,"aria-hidden":!0,children:[e.map(e=>(0,t.jsx)(`button`,{"data-measure-btn":!0,className:`pretext-plus-editor__menu-button ${e.className??``}`,tabIndex:-1,children:e.label},e.key)),(0,t.jsx)(`button`,{"data-measure-more":!0,className:`pretext-plus-editor__menu-button`,tabIndex:-1,children:`More ▾`})]})]})},Bs=({content:e,sourceFormat:n,onContentChange:r,onOpenLatexImport:i,onOpenDocinfoEditor:a,onConvertToPretext:o,canConvertToPretext:s,onOpenAssets:c,onShowFullSource:l})=>{let u=()=>{try{r(ps(e))}catch(e){console.error(`Error formatting:`,e),alert(`Error formatting XML`)}},d={key:`full-source`,label:`Display Full Source`,onClick:l,title:`Show the full assembled PreTeXt source for the project`},f;return f=n===`latex`?[...o?[{key:`convert`,label:`Convert to PreTeXt`,onClick:o,title:`Create a new project copy using the converted PreTeXt source`,className:`pretext-plus-editor__menu-button--convert`,disabled:s===!1}]:[],{key:`preamble`,label:`Edit Preamble`,onClick:a,title:`Edit Preamble`},d]:n===`markdown`?[...o?[{key:`convert`,label:`Convert to PreTeXt`,onClick:o,title:`Create a new project copy using the converted PreTeXt source`,className:`pretext-plus-editor__menu-button--convert`,disabled:s===!1}]:[],{key:`macros`,label:`Edit Macros`,onClick:a,title:`Edit Macros`},d]:[{key:`format`,label:`Format PreTeXt`,onClick:u,title:`Format the PreTeXt source`},{key:`import-latex`,label:`Import LaTeX`,onClick:i,title:`Import LaTeX`},{key:`macros`,label:`Edit Macros`,onClick:a,title:`Edit Macros`},...c?[{key:`assets`,label:`Assets`,onClick:c,title:`Manage assets`}]:[],d],(0,t.jsxs)(`div`,{className:`pretext-plus-editor__code-editor-menu`,children:[(0,t.jsx)(zs,{actions:f}),(0,t.jsx)(`span`,{className:`pretext-plus-editor__code-editor-source-badge pretext-plus-editor__code-editor-source-badge--right`,children:n===`latex`?`LaTeX`:n===`markdown`?`Markdown`:`PreTeXt`})]})};function Vs(e){let t=e.getLineCount();if(t<2||!/^\s*<title\b/.test(e.getLineContent(2)))return 1;let n=2;for(;n<=t&&!/<\/title\s*>/.test(e.getLineContent(n));)n++;return n<=t?n:1}function Hs(e,t){let n=e.getLineCount();if(t===`pretext`){if(n<3)return null;let t=Vs(e);if(t>=n-1)return null;let r=[];for(let e=1;e<=t;e++)r.push(e);return r.push(n),{editableRange:[t+1,1,n-1,e.getLineMaxColumn(n-1)],lockedLines:r,leadingLockedLines:t}}if(t===`markdown`){if(e.getLineContent(1).trim()!==`---`)return null;let t=-1;for(let r=2;r<=n;r++)if(e.getLineContent(r).trim()===`---`){t=r;break}if(t===-1||t>=n)return null;let r=[];for(let e=1;e<=t;e++)r.push(e);return{editableRange:[t+1,1,n,e.getLineMaxColumn(n)],lockedLines:r,leadingLockedLines:t}}return t===`latex`?n<2||!/^\s*\\(?!begin\b|end\b)[A-Za-z][A-Za-z-]*\*?\{/.test(e.getLineContent(1))?null:{editableRange:[2,1,n,e.getLineMaxColumn(n)],lockedLines:[1],leadingLockedLines:1}:null}var Us={automaticLayout:!0,minimap:{enabled:!1},acceptSuggestionOnCommitCharacter:!1,quickSuggestions:!1,wordWrap:`on`,insertSpaces:!0,tabSize:2,padding:{top:10,bottom:10}},Ws=(0,n.forwardRef)(({content:e,sourceFormat:r,onChange:i,onRebuild:a,onSave:o,onOpenLatexImport:s,onOpenDocinfoEditor:c,onOpenConvertToPretext:l,canConvertToPretext:u,onOpenAssets:d,onShowFullSource:f,onRequestWrapperEdit:p},m)=>{let h=(0,n.useRef)(null),g=(0,n.useRef)(null),_=(0,n.useRef)(null),v=(0,n.useRef)(null),y=(0,n.useRef)(!1),b=(0,n.useRef)(0),x=(0,n.useRef)(null),S=(0,n.useRef)(null),C=(0,n.useRef)(p),w=(0,n.useRef)(null),T=(0,n.useRef)(null),E=(0,n.useRef)(!1),[D,O]=(0,n.useState)(!1),[k,A]=(0,n.useState)(!1),ee=(0,n.useRef)(a),te=(0,n.useRef)(o);(0,n.useImperativeHandle)(m,()=>({insertAtCursor:e=>{let t=h.current;if(!t)return;let n=t.getSelection();n&&(t.executeEdits(`insert-asset`,[{range:n,text:e,forceMoveMarkers:!0}]),t.focus())},focus:()=>{h.current?.focus()}}),[]),(0,n.useEffect)(()=>{ee.current=a},[a]),(0,n.useEffect)(()=>{te.current=o},[o]),(0,n.useEffect)(()=>{C.current=p},[p]),(0,n.useEffect)(()=>{w.current?.dispose?.();let e=ko[r];w.current=g.current?e.registerMonacoExtensions?.(g.current)??null:null,ne()},[r]),(0,n.useEffect)(()=>()=>{x.current?.dispose?.(),w.current?.dispose?.(),S.current?.dispose?.(),_.current?.disposeConstrainer?.(),T.current&&clearTimeout(T.current)},[]);let j=(e,t,n)=>{if(r!==`pretext`)return;let i=t.getLineCount();if(i<2)return;let a=t.getLineContent(1).trim();if(!a.startsWith(`<`)||a.startsWith(`</`)||a.endsWith(`/>`))return;let o=Vs(t);if(i!==o+1)return;let s=t.getLineContent(i).trim();if(!/^<\/[A-Za-z][\w.:-]*\s*>$/.test(s))return;E.current=!0;let c=t.getLineMaxColumn(o);e.executeEdits(`ensure-body-line`,[{range:new n.Range(o,c,o,c),text:`
438
- `,forceMoveMarkers:!0}]),queueMicrotask(()=>{E.current=!1})},M=(e,t,n)=>{if(r!==`pretext`)return;let i=t.getLineCount();if(i<2||t.getLineContent(i).trim()!==``)return;let a=i-1;for(;a>1&&t.getLineContent(a).trim()===``;)a--;let o=t.getLineContent(a).trim();/^<\/[A-Za-z][\w.:-]*\s*>$/.test(o)&&(E.current=!0,e.executeEdits(`trim-trailing-blank-lines`,[{range:new n.Range(a,t.getLineMaxColumn(a),i,t.getLineMaxColumn(i)),text:``,forceMoveMarkers:!0}]),queueMicrotask(()=>{E.current=!1}))},ne=()=>{let e=h.current,t=g.current,n=_.current;if(!e||!t||!n)return;let i=e.getModel();if(!i)return;typeof i.disposeRestrictions==`function`&&i.disposeRestrictions(),M(e,i,t),j(e,i,t);let a=Hs(i,r);if(y.current=a!==null,b.current=a?.leadingLockedLines??0,a&&n.addRestrictionsTo(i,[{range:a.editableRange,allowMultiline:!0}]),typeof e.createDecorationsCollection==`function`)if(v.current||=e.createDecorationsCollection(),!a)v.current.clear();else{let e={isWholeLine:!0,className:`pretext-plus-editor__locked-line`,inlineClassName:`pretext-plus-editor__locked-line-text`,stickiness:t.editor.TrackedRangeStickiness.NeverGrowsWhenTypingAtEdges,hoverMessage:{value:`These properties are structural — edit them from the Table of Contents.`}};v.current.set(a.lockedLines.map(n=>({range:new t.Range(n,1,n,i.getLineMaxColumn(n)),options:e})))}},N=(e,t)=>{e.getValue()!==t&&(E.current=!0,typeof e.disposeRestrictions==`function`&&e.disposeRestrictions(),e.setValue(t),ne(),queueMicrotask(()=>{E.current=!1}))};(0,n.useEffect)(()=>{let t=h.current;if(!t)return;let n=t.getModel();if(n&&n.getValue()!==e){let r=t.getPosition(),i=t.getSelections();N(n,e),r&&t.setPosition(r),i&&t.setSelections(i)}},[e]);let re=(t,n)=>{h.current=t,g.current=n,_.current=tr(n),_.current.initializeIn(t);let i=t.getModel();i&&i.getValue()!==e&&N(i,e),x.current?.dispose?.(),x.current=t.onDidChangeModelContent(()=>{ie()}),ie(),S.current?.dispose?.(),S.current=t.onMouseDown(e=>{let t=e?.target?.position?.lineNumber;y.current&&typeof t==`number`&&t<=b.current&&C.current?.()}),t.addCommand(n.KeyMod.CtrlCmd|n.KeyCode.Enter,()=>{ee.current?.()}),t.addCommand(n.KeyMod.CtrlCmd|n.KeyCode.KeyS,()=>{te.current?.()}),w.current?.dispose?.(),w.current=ko[r].registerMonacoExtensions?.(n)??null,ne()},ie=()=>{let e=h.current?.getModel?.();if(e&&typeof e.canUndo==`function`&&typeof e.canRedo==`function`)try{O(e.canUndo()),A(e.canRedo());return}catch{}let t=!!h.current;O(t),A(t)},ae=e=>{i(e)};return(0,t.jsxs)(`div`,{style:{display:`flex`,flexDirection:`column`,height:`100%`},children:[(0,t.jsx)(Bs,{content:e,sourceFormat:r,onContentChange:ae,onOpenLatexImport:s,onOpenDocinfoEditor:c,onUndo:()=>{h.current?.trigger(``,`undo`),ie()},onRedo:()=>{h.current?.trigger(``,`redo`),ie()},canUndo:D,canRedo:k,onConvertToPretext:l,canConvertToPretext:u,onOpenAssets:d,onShowFullSource:f}),(0,t.jsx)(`div`,{style:{flex:1},children:(0,t.jsx)(Yn,{options:Us,height:`100%`,language:ko[r].language,defaultValue:e,onMount:re,onChange:(e,t)=>{t?.isFlush||E.current||(T.current&&clearTimeout(T.current),T.current=setTimeout(()=>{ae(e||``)},500))}})})]})});Ws.displayName=`CodeEditor`;function Gs(e){this.content=e}Gs.prototype={constructor:Gs,find:function(e){for(var t=0;t<this.content.length;t+=2)if(this.content[t]===e)return t;return-1},get:function(e){var t=this.find(e);return t==-1?void 0:this.content[t+1]},update:function(e,t,n){var r=n&&n!=e?this.remove(n):this,i=r.find(e),a=r.content.slice();return i==-1?a.push(n||e,t):(a[i+1]=t,n&&(a[i]=n)),new Gs(a)},remove:function(e){var t=this.find(e);if(t==-1)return this;var n=this.content.slice();return n.splice(t,2),new Gs(n)},addToStart:function(e,t){return new Gs([e,t].concat(this.remove(e).content))},addToEnd:function(e,t){var n=this.remove(e).content.slice();return n.push(e,t),new Gs(n)},addBefore:function(e,t,n){var r=this.remove(t),i=r.content.slice(),a=r.find(e);return i.splice(a==-1?i.length:a,0,t,n),new Gs(i)},forEach:function(e){for(var t=0;t<this.content.length;t+=2)e(this.content[t],this.content[t+1])},prepend:function(e){return e=Gs.from(e),e.size?new Gs(e.content.concat(this.subtract(e).content)):this},append:function(e){return e=Gs.from(e),e.size?new Gs(this.subtract(e).content.concat(e.content)):this},subtract:function(e){var t=this;e=Gs.from(e);for(var n=0;n<e.content.length;n+=2)t=t.remove(e.content[n]);return t},toObject:function(){var e={};return this.forEach(function(t,n){e[t]=n}),e},get size(){return this.content.length>>1}},Gs.from=function(e){if(e instanceof Gs)return e;var t=[];if(e)for(var n in e)t.push(n,e[n]);return new Gs(t)};function Ks(e,t,n){for(let r=0;;r++){if(r==e.childCount||r==t.childCount)return e.childCount==t.childCount?null:n;let i=e.child(r),a=t.child(r);if(i==a){n+=i.nodeSize;continue}if(!i.sameMarkup(a))return n;if(i.isText&&i.text!=a.text){let e=i.text,t=a.text,r=0;for(;e[r]==t[r];r++)n++;return r&&r<e.length&&r<t.length&&Ys(e.charCodeAt(r-1))&&Js(e.charCodeAt(r))&&n--,n}if(i.content.size||a.content.size){let e=Ks(i.content,a.content,n+1);if(e!=null)return e}n+=i.nodeSize}}function qs(e,t,n,r){for(let i=e.childCount,a=t.childCount;;){if(i==0||a==0)return i==a?null:{a:n,b:r};let o=e.child(--i),s=t.child(--a),c=o.nodeSize;if(o==s){n-=c,r-=c;continue}if(!o.sameMarkup(s))return{a:n,b:r};if(o.isText&&o.text!=s.text){let e=o.text,t=s.text,i=e.length,a=t.length;for(;i>0&&a>0&&e[i-1]==t[a-1];)i--,a--,n--,r--;return i&&a&&i<e.length&&Ys(e.charCodeAt(i-1))&&Js(e.charCodeAt(i))&&(n++,r++),{a:n,b:r}}if(o.content.size||s.content.size){let e=qs(o.content,s.content,n-1,r-1);if(e)return e}n-=c,r-=c}}function Js(e){return e>=56320&&e<57344}function Ys(e){return e>=55296&&e<56320}var Xs=class e{constructor(e,t){if(this.content=e,this.size=t||0,t==null)for(let t=0;t<e.length;t++)this.size+=e[t].nodeSize}nodesBetween(e,t,n,r=0,i){for(let a=0,o=0;o<t;a++){let s=this.content[a],c=o+s.nodeSize;if(c>e&&n(s,r+o,i||null,a)!==!1&&s.content.size){let i=o+1;s.nodesBetween(Math.max(0,e-i),Math.min(s.content.size,t-i),n,r+i)}o=c}}descendants(e){this.nodesBetween(0,this.size,e)}textBetween(e,t,n,r){let i=``,a=!0;return this.nodesBetween(e,t,(o,s)=>{let c=o.isText?o.text.slice(Math.max(e,s)-s,t-s):o.isLeaf?r?typeof r==`function`?r(o):r:o.type.spec.leafText?o.type.spec.leafText(o):``:``;o.isBlock&&(o.isLeaf&&c||o.isTextblock)&&n&&(a?a=!1:i+=n),i+=c},0),i}append(t){if(!t.size)return this;if(!this.size)return t;let n=this.lastChild,r=t.firstChild,i=this.content.slice(),a=0;for(n.isText&&n.sameMarkup(r)&&(i[i.length-1]=n.withText(n.text+r.text),a=1);a<t.content.length;a++)i.push(t.content[a]);return new e(i,this.size+t.size)}cut(t,n=this.size){if(t==0&&n==this.size)return this;let r=[],i=0;if(n>t)for(let e=0,a=0;a<n;e++){let o=this.content[e],s=a+o.nodeSize;s>t&&((a<t||s>n)&&(o=o.isText?o.cut(Math.max(0,t-a),Math.min(o.text.length,n-a)):o.cut(Math.max(0,t-a-1),Math.min(o.content.size,n-a-1))),r.push(o),i+=o.nodeSize),a=s}return new e(r,i)}cutByIndex(t,n){return t==n?e.empty:t==0&&n==this.content.length?this:new e(this.content.slice(t,n))}replaceChild(t,n){let r=this.content[t];if(r==n)return this;let i=this.content.slice(),a=this.size+n.nodeSize-r.nodeSize;return i[t]=n,new e(i,a)}addToStart(t){return new e([t].concat(this.content),this.size+t.nodeSize)}addToEnd(t){return new e(this.content.concat(t),this.size+t.nodeSize)}eq(e){if(this.content.length!=e.content.length)return!1;for(let t=0;t<this.content.length;t++)if(!this.content[t].eq(e.content[t]))return!1;return!0}get firstChild(){return this.content.length?this.content[0]:null}get lastChild(){return this.content.length?this.content[this.content.length-1]:null}get childCount(){return this.content.length}child(e){let t=this.content[e];if(!t)throw RangeError(`Index `+e+` out of range for `+this);return t}maybeChild(e){return this.content[e]||null}forEach(e){for(let t=0,n=0;t<this.content.length;t++){let r=this.content[t];e(r,n,t),n+=r.nodeSize}}findDiffStart(e,t=0){return Ks(this,e,t)}findDiffEnd(e,t=this.size,n=e.size){return qs(this,e,t,n)}findIndex(e){if(e==0)return Qs(0,e);if(e==this.size)return Qs(this.content.length,e);if(e>this.size||e<0)throw RangeError(`Position ${e} outside of fragment (${this})`);for(let t=0,n=0;;t++){let r=this.child(t),i=n+r.nodeSize;if(i>=e)return i==e?Qs(t+1,i):Qs(t,n);n=i}}toString(){return`<`+this.toStringInner()+`>`}toStringInner(){return this.content.join(`, `)}toJSON(){return this.content.length?this.content.map(e=>e.toJSON()):null}static fromJSON(t,n){if(!n)return e.empty;if(!Array.isArray(n))throw RangeError(`Invalid input for Fragment.fromJSON`);return e.fromArray(n.map(t.nodeFromJSON))}static fromArray(t){if(!t.length)return e.empty;let n,r=0;for(let e=0;e<t.length;e++){let i=t[e];r+=i.nodeSize,e&&i.isText&&t[e-1].sameMarkup(i)?(n||=t.slice(0,e),n[n.length-1]=i.withText(n[n.length-1].text+i.text)):n&&n.push(i)}return new e(n||t,r)}static from(t){if(!t)return e.empty;if(t instanceof e)return t;if(Array.isArray(t))return this.fromArray(t);if(t.attrs)return new e([t],t.nodeSize);throw RangeError(`Can not convert `+t+` to a Fragment`+(t.nodesBetween?` (looks like multiple versions of prosemirror-model were loaded)`:``))}};Xs.empty=new Xs([],0);var Zs={index:0,offset:0};function Qs(e,t){return Zs.index=e,Zs.offset=t,Zs}function $s(e,t){if(e===t)return!0;if(!(e&&typeof e==`object`)||!(t&&typeof t==`object`))return!1;let n=Array.isArray(e);if(Array.isArray(t)!=n)return!1;if(n){if(e.length!=t.length)return!1;for(let n=0;n<e.length;n++)if(!$s(e[n],t[n]))return!1}else{for(let n in e)if(!(n in t)||!$s(e[n],t[n]))return!1;for(let n in t)if(!(n in e))return!1}return!0}var ec=class e{constructor(e,t){this.type=e,this.attrs=t}addToSet(e){let t,n=!1;for(let r=0;r<e.length;r++){let i=e[r];if(this.eq(i))return e;if(this.type.excludes(i.type))t||=e.slice(0,r);else if(i.type.excludes(this.type))return e;else !n&&i.type.rank>this.type.rank&&(t||=e.slice(0,r),t.push(this),n=!0),t&&t.push(i)}return t||=e.slice(),n||t.push(this),t}removeFromSet(e){for(let t=0;t<e.length;t++)if(this.eq(e[t]))return e.slice(0,t).concat(e.slice(t+1));return e}isInSet(e){for(let t=0;t<e.length;t++)if(this.eq(e[t]))return!0;return!1}eq(e){return this==e||this.type==e.type&&$s(this.attrs,e.attrs)}toJSON(){let e={type:this.type.name};for(let t in this.attrs){e.attrs=this.attrs;break}return e}static fromJSON(e,t){if(!t)throw RangeError(`Invalid input for Mark.fromJSON`);let n=e.marks[t.type];if(!n)throw RangeError(`There is no mark type ${t.type} in this schema`);let r=n.create(t.attrs);return n.checkAttrs(r.attrs),r}static sameSet(e,t){if(e==t)return!0;if(e.length!=t.length)return!1;for(let n=0;n<e.length;n++)if(!e[n].eq(t[n]))return!1;return!0}static setFrom(t){if(!t||Array.isArray(t)&&t.length==0)return e.none;if(t instanceof e)return[t];let n=t.slice();return n.sort((e,t)=>e.type.rank-t.type.rank),n}};ec.none=[];var tc=class extends Error{},nc=class e{constructor(e,t,n){this.content=e,this.openStart=t,this.openEnd=n}get size(){return this.content.size-this.openStart-this.openEnd}insertAt(t,n){let r=ic(this.content,t+this.openStart,n,this.openStart+1,this.openEnd+1);return r&&new e(r,this.openStart,this.openEnd)}removeBetween(t,n){return new e(rc(this.content,t+this.openStart,n+this.openStart),this.openStart,this.openEnd)}eq(e){return this.content.eq(e.content)&&this.openStart==e.openStart&&this.openEnd==e.openEnd}toString(){return this.content+`(`+this.openStart+`,`+this.openEnd+`)`}toJSON(){if(!this.content.size)return null;let e={content:this.content.toJSON()};return this.openStart>0&&(e.openStart=this.openStart),this.openEnd>0&&(e.openEnd=this.openEnd),e}static fromJSON(t,n){if(!n)return e.empty;let r=n.openStart||0,i=n.openEnd||0;if(typeof r!=`number`||typeof i!=`number`)throw RangeError(`Invalid input for Slice.fromJSON`);return new e(Xs.fromJSON(t,n.content),r,i)}static maxOpen(t,n=!0){let r=0,i=0;for(let e=t.firstChild;e&&!e.isLeaf&&(n||!e.type.spec.isolating);e=e.firstChild)r++;for(let e=t.lastChild;e&&!e.isLeaf&&(n||!e.type.spec.isolating);e=e.lastChild)i++;return new e(t,r,i)}};nc.empty=new nc(Xs.empty,0,0);function rc(e,t,n){let{index:r,offset:i}=e.findIndex(t),a=e.maybeChild(r),{index:o,offset:s}=e.findIndex(n);if(i==t||a.isText){if(s!=n&&!e.child(o).isText)throw RangeError(`Removing non-flat range`);return e.cut(0,t).append(e.cut(n))}if(r!=o)throw RangeError(`Removing non-flat range`);return e.replaceChild(r,a.copy(rc(a.content,t-i-1,n-i-1)))}function ic(e,t,n,r,i,a){let{index:o,offset:s}=e.findIndex(t),c=e.maybeChild(o);if(s==t||c.isText)return a&&r<=0&&i<=0&&!a.canReplace(o,o,n)?null:e.cut(0,t).append(n).append(e.cut(t));let l=ic(c.content,t-s-1,n,o==0?r-1:0,o==e.childCount-1?i-1:0,c);return l&&e.replaceChild(o,c.copy(l))}function ac(e,t,n){if(n.openStart>e.depth)throw new tc(`Inserted content deeper than insertion position`);if(e.depth-n.openStart!=t.depth-n.openEnd)throw new tc(`Inconsistent open depths`);return oc(e,t,n,0)}function oc(e,t,n,r){let i=e.index(r),a=e.node(r);if(i==t.index(r)&&r<e.depth-n.openStart){let o=oc(e,t,n,r+1);return a.copy(a.content.replaceChild(i,o))}else if(!n.content.size)return dc(a,pc(e,t,r));else if(!n.openStart&&!n.openEnd&&e.depth==r&&t.depth==r){let r=e.parent,i=r.content;return dc(r,i.cut(0,e.parentOffset).append(n.content).append(i.cut(t.parentOffset)))}else{let{start:i,end:o}=mc(n,e);return dc(a,fc(e,i,o,t,r))}}function sc(e,t){if(!t.type.compatibleContent(e.type))throw new tc(`Cannot join `+t.type.name+` onto `+e.type.name)}function cc(e,t,n){let r=e.node(n);return sc(r,t.node(n)),r}function lc(e,t){let n=t.length-1;n>=0&&e.isText&&e.sameMarkup(t[n])?t[n]=e.withText(t[n].text+e.text):t.push(e)}function uc(e,t,n,r){let i=(t||e).node(n),a=0,o=t?t.index(n):i.childCount;e&&(a=e.index(n),e.depth>n?a++:e.textOffset&&(lc(e.nodeAfter,r),a++));for(let e=a;e<o;e++)lc(i.child(e),r);t&&t.depth==n&&t.textOffset&&lc(t.nodeBefore,r)}function dc(e,t){if(!e.type.validContent(t))throw new tc(`Invalid content for node `+e.type.name);return e.copy(t)}function fc(e,t,n,r,i){let a=e.depth>i&&cc(e,t,i+1),o=r.depth>i&&cc(n,r,i+1),s=[];return uc(null,e,i,s),a&&o&&t.index(i)==n.index(i)?(sc(a,o),lc(dc(a,fc(e,t,n,r,i+1)),s)):(a&&lc(dc(a,pc(e,t,i+1)),s),uc(t,n,i,s),o&&lc(dc(o,pc(n,r,i+1)),s)),uc(r,null,i,s),new Xs(s)}function pc(e,t,n){let r=[];return uc(null,e,n,r),e.depth>n&&lc(dc(cc(e,t,n+1),pc(e,t,n+1)),r),uc(t,null,n,r),new Xs(r)}function mc(e,t){let n=t.depth-e.openStart,r=t.node(n).copy(e.content);for(let e=n-1;e>=0;e--)r=t.node(e).copy(Xs.from(r));return{start:r.resolveNoCache(e.openStart+n),end:r.resolveNoCache(r.content.size-e.openEnd-n)}}var hc=class e{constructor(e,t,n){this.pos=e,this.path=t,this.parentOffset=n,this.depth=t.length/3-1}resolveDepth(e){return e==null?this.depth:e<0?this.depth+e:e}get parent(){return this.node(this.depth)}get doc(){return this.node(0)}node(e){return this.path[this.resolveDepth(e)*3]}index(e){return this.path[this.resolveDepth(e)*3+1]}indexAfter(e){return e=this.resolveDepth(e),this.index(e)+(e==this.depth&&!this.textOffset?0:1)}start(e){return e=this.resolveDepth(e),e==0?0:this.path[e*3-1]+1}end(e){return e=this.resolveDepth(e),this.start(e)+this.node(e).content.size}before(e){if(e=this.resolveDepth(e),!e)throw RangeError(`There is no position before the top-level node`);return e==this.depth+1?this.pos:this.path[e*3-1]}after(e){if(e=this.resolveDepth(e),!e)throw RangeError(`There is no position after the top-level node`);return e==this.depth+1?this.pos:this.path[e*3-1]+this.path[e*3].nodeSize}get textOffset(){return this.pos-this.path[this.path.length-1]}get nodeAfter(){let e=this.parent,t=this.index(this.depth);if(t==e.childCount)return null;let n=this.pos-this.path[this.path.length-1],r=e.child(t);return n?e.child(t).cut(n):r}get nodeBefore(){let e=this.index(this.depth),t=this.pos-this.path[this.path.length-1];return t?this.parent.child(e).cut(0,t):e==0?null:this.parent.child(e-1)}posAtIndex(e,t){t=this.resolveDepth(t);let n=this.path[t*3],r=t==0?0:this.path[t*3-1]+1;for(let t=0;t<e;t++)r+=n.child(t).nodeSize;return r}marks(){let e=this.parent,t=this.index();if(e.content.size==0)return ec.none;if(this.textOffset)return e.child(t).marks;let n=e.maybeChild(t-1),r=e.maybeChild(t);if(!n){let e=n;n=r,r=e}let i=n.marks;for(var a=0;a<i.length;a++)i[a].type.spec.inclusive===!1&&(!r||!i[a].isInSet(r.marks))&&(i=i[a--].removeFromSet(i));return i}marksAcross(e){let t=this.parent.maybeChild(this.index());if(!t||!t.isInline)return null;let n=t.marks,r=e.parent.maybeChild(e.index());for(var i=0;i<n.length;i++)n[i].type.spec.inclusive===!1&&(!r||!n[i].isInSet(r.marks))&&(n=n[i--].removeFromSet(n));return n}sharedDepth(e){for(let t=this.depth;t>0;t--)if(this.start(t)<=e&&this.end(t)>=e)return t;return 0}blockRange(e=this,t){if(e.pos<this.pos)return e.blockRange(this);for(let n=this.depth-(this.parent.inlineContent||this.pos==e.pos?1:0);n>=0;n--)if(e.pos<=this.end(n)&&(!t||t(this.node(n))))return new yc(this,e,n);return null}sameParent(e){return this.pos-this.parentOffset==e.pos-e.parentOffset}max(e){return e.pos>this.pos?e:this}min(e){return e.pos<this.pos?e:this}toString(){let e=``;for(let t=1;t<=this.depth;t++)e+=(e?`/`:``)+this.node(t).type.name+`_`+this.index(t-1);return e+`:`+this.parentOffset}static resolve(t,n){if(!(n>=0&&n<=t.content.size))throw RangeError(`Position `+n+` out of range`);let r=[],i=0,a=n;for(let e=t;;){let{index:t,offset:n}=e.content.findIndex(a),o=a-n;if(r.push(e,t,i+n),!o||(e=e.child(t),e.isText))break;a=o-1,i+=n+1}return new e(n,r,a)}static resolveCached(t,n){let r=vc.get(t);if(r)for(let e=0;e<r.elts.length;e++){let t=r.elts[e];if(t.pos==n)return t}else vc.set(t,r=new gc);let i=r.elts[r.i]=e.resolve(t,n);return r.i=(r.i+1)%_c,i}},gc=class{constructor(){this.elts=[],this.i=0}},_c=12,vc=new WeakMap,yc=class{constructor(e,t,n){this.$from=e,this.$to=t,this.depth=n}get start(){return this.$from.before(this.depth+1)}get end(){return this.$to.after(this.depth+1)}get parent(){return this.$from.node(this.depth)}get startIndex(){return this.$from.index(this.depth)}get endIndex(){return this.$to.indexAfter(this.depth)}},bc=Object.create(null),xc=class e{constructor(e,t,n,r=ec.none){this.type=e,this.attrs=t,this.marks=r,this.content=n||Xs.empty}get children(){return this.content.content}get nodeSize(){return this.isLeaf?1:2+this.content.size}get childCount(){return this.content.childCount}child(e){return this.content.child(e)}maybeChild(e){return this.content.maybeChild(e)}forEach(e){this.content.forEach(e)}nodesBetween(e,t,n,r=0){this.content.nodesBetween(e,t,n,r,this)}descendants(e){this.nodesBetween(0,this.content.size,e)}get textContent(){return this.isLeaf&&this.type.spec.leafText?this.type.spec.leafText(this):this.textBetween(0,this.content.size,``)}textBetween(e,t,n,r){return this.content.textBetween(e,t,n,r)}get firstChild(){return this.content.firstChild}get lastChild(){return this.content.lastChild}eq(e){return this==e||this.sameMarkup(e)&&this.content.eq(e.content)}sameMarkup(e){return this.hasMarkup(e.type,e.attrs,e.marks)}hasMarkup(e,t,n){return this.type==e&&$s(this.attrs,t||e.defaultAttrs||bc)&&ec.sameSet(this.marks,n||ec.none)}copy(t=null){return t==this.content?this:new e(this.type,this.attrs,t,this.marks)}mark(t){return t==this.marks?this:new e(this.type,this.attrs,this.content,t)}cut(e,t=this.content.size){return e==0&&t==this.content.size?this:this.copy(this.content.cut(e,t))}slice(e,t=this.content.size,n=!1){if(e==t)return nc.empty;let r=this.resolve(e),i=this.resolve(t),a=n?0:r.sharedDepth(t),o=r.start(a);return new nc(r.node(a).content.cut(r.pos-o,i.pos-o),r.depth-a,i.depth-a)}replace(e,t,n){return ac(this.resolve(e),this.resolve(t),n)}nodeAt(e){for(let t=this;;){let{index:n,offset:r}=t.content.findIndex(e);if(t=t.maybeChild(n),!t)return null;if(r==e||t.isText)return t;e-=r+1}}childAfter(e){let{index:t,offset:n}=this.content.findIndex(e);return{node:this.content.maybeChild(t),index:t,offset:n}}childBefore(e){if(e==0)return{node:null,index:0,offset:0};let{index:t,offset:n}=this.content.findIndex(e);if(n<e)return{node:this.content.child(t),index:t,offset:n};let r=this.content.child(t-1);return{node:r,index:t-1,offset:n-r.nodeSize}}resolve(e){return hc.resolveCached(this,e)}resolveNoCache(e){return hc.resolve(this,e)}rangeHasMark(e,t,n){let r=!1;return t>e&&this.nodesBetween(e,t,e=>(n.isInSet(e.marks)&&(r=!0),!r)),r}get isBlock(){return this.type.isBlock}get isTextblock(){return this.type.isTextblock}get inlineContent(){return this.type.inlineContent}get isInline(){return this.type.isInline}get isText(){return this.type.isText}get isLeaf(){return this.type.isLeaf}get isAtom(){return this.type.isAtom}toString(){if(this.type.spec.toDebugString)return this.type.spec.toDebugString(this);let e=this.type.name;return this.content.size&&(e+=`(`+this.content.toStringInner()+`)`),Cc(this.marks,e)}contentMatchAt(e){let t=this.type.contentMatch.matchFragment(this.content,0,e);if(!t)throw Error(`Called contentMatchAt on a node with invalid content`);return t}canReplace(e,t,n=Xs.empty,r=0,i=n.childCount){let a=this.contentMatchAt(e).matchFragment(n,r,i),o=a&&a.matchFragment(this.content,t);if(!o||!o.validEnd)return!1;for(let e=r;e<i;e++)if(!this.type.allowsMarks(n.child(e).marks))return!1;return!0}canReplaceWith(e,t,n,r){if(r&&!this.type.allowsMarks(r))return!1;let i=this.contentMatchAt(e).matchType(n),a=i&&i.matchFragment(this.content,t);return a?a.validEnd:!1}canAppend(e){return e.content.size?this.canReplace(this.childCount,this.childCount,e.content):this.type.compatibleContent(e.type)}check(){this.type.checkContent(this.content),this.type.checkAttrs(this.attrs);let e=ec.none;for(let t=0;t<this.marks.length;t++){let n=this.marks[t];n.type.checkAttrs(n.attrs),e=n.addToSet(e)}if(!ec.sameSet(e,this.marks))throw RangeError(`Invalid collection of marks for node ${this.type.name}: ${this.marks.map(e=>e.type.name)}`);this.content.forEach(e=>e.check())}toJSON(){let e={type:this.type.name};for(let t in this.attrs){e.attrs=this.attrs;break}return this.content.size&&(e.content=this.content.toJSON()),this.marks.length&&(e.marks=this.marks.map(e=>e.toJSON())),e}static fromJSON(e,t){if(!t)throw RangeError(`Invalid input for Node.fromJSON`);let n;if(t.marks){if(!Array.isArray(t.marks))throw RangeError(`Invalid mark data for Node.fromJSON`);n=t.marks.map(e.markFromJSON)}if(t.type==`text`){if(typeof t.text!=`string`)throw RangeError(`Invalid text node in JSON`);return e.text(t.text,n)}let r=Xs.fromJSON(e,t.content),i=e.nodeType(t.type).create(t.attrs,r,n);return i.type.checkAttrs(i.attrs),i}};xc.prototype.text=void 0;var Sc=class e extends xc{constructor(e,t,n,r){if(super(e,t,null,r),!n)throw RangeError(`Empty text nodes are not allowed`);this.text=n}toString(){return this.type.spec.toDebugString?this.type.spec.toDebugString(this):Cc(this.marks,JSON.stringify(this.text))}get textContent(){return this.text}textBetween(e,t){return this.text.slice(e,t)}get nodeSize(){return this.text.length}mark(t){return t==this.marks?this:new e(this.type,this.attrs,this.text,t)}withText(t){return t==this.text?this:new e(this.type,this.attrs,t,this.marks)}cut(e=0,t=this.text.length){return e==0&&t==this.text.length?this:this.withText(this.text.slice(e,t))}eq(e){return this.sameMarkup(e)&&this.text==e.text}toJSON(){let e=super.toJSON();return e.text=this.text,e}};function Cc(e,t){for(let n=e.length-1;n>=0;n--)t=e[n].type.name+`(`+t+`)`;return t}var wc=class e{constructor(e){this.validEnd=e,this.next=[],this.wrapCache=[]}static parse(t,n){let r=new Tc(t,n);if(r.next==null)return e.empty;let i=Ec(r);r.next&&r.err(`Unexpected trailing text`);let a=Ic(Nc(i));return Lc(a,r),a}matchType(e){for(let t=0;t<this.next.length;t++)if(this.next[t].type==e)return this.next[t].next;return null}matchFragment(e,t=0,n=e.childCount){let r=this;for(let i=t;r&&i<n;i++)r=r.matchType(e.child(i).type);return r}get inlineContent(){return this.next.length!=0&&this.next[0].type.isInline}get defaultType(){for(let e=0;e<this.next.length;e++){let{type:t}=this.next[e];if(!(t.isText||t.hasRequiredAttrs()))return t}return null}compatible(e){for(let t=0;t<this.next.length;t++)for(let n=0;n<e.next.length;n++)if(this.next[t].type==e.next[n].type)return!0;return!1}fillBefore(e,t=!1,n=0){let r=[this];function i(a,o){let s=a.matchFragment(e,n);if(s&&(!t||s.validEnd))return Xs.from(o.map(e=>e.createAndFill()));for(let e=0;e<a.next.length;e++){let{type:t,next:n}=a.next[e];if(!(t.isText||t.hasRequiredAttrs())&&r.indexOf(n)==-1){r.push(n);let e=i(n,o.concat(t));if(e)return e}}return null}return i(this,[])}findWrapping(e){for(let t=0;t<this.wrapCache.length;t+=2)if(this.wrapCache[t]==e)return this.wrapCache[t+1];let t=this.computeWrapping(e);return this.wrapCache.push(e,t),t}computeWrapping(e){let t=Object.create(null),n=[{match:this,type:null,via:null}];for(;n.length;){let r=n.shift(),i=r.match;if(i.matchType(e)){let e=[];for(let t=r;t.type;t=t.via)e.push(t.type);return e.reverse()}for(let e=0;e<i.next.length;e++){let{type:a,next:o}=i.next[e];!a.isLeaf&&!a.hasRequiredAttrs()&&!(a.name in t)&&(!r.type||o.validEnd)&&(n.push({match:a.contentMatch,type:a,via:r}),t[a.name]=!0)}}return null}get edgeCount(){return this.next.length}edge(e){if(e>=this.next.length)throw RangeError(`There's no ${e}th edge in this content match`);return this.next[e]}toString(){let e=[];function t(n){e.push(n);for(let r=0;r<n.next.length;r++)e.indexOf(n.next[r].next)==-1&&t(n.next[r].next)}return t(this),e.map((t,n)=>{let r=n+(t.validEnd?`*`:` `)+` `;for(let n=0;n<t.next.length;n++)r+=(n?`, `:``)+t.next[n].type.name+`->`+e.indexOf(t.next[n].next);return r}).join(`
437
+ `);t.push(`${i}${Ds(e)}${n}${i}</${e.name}>`)}else t.push(`${i}${Ds(e)}${a}</${e.name}>`)}function _s(e,t,n,r){let i=r.ind.repeat(n);if(Ms(e)){t.push(`${i}${Os(e)}`);return}let a=Cs(e.children);t.push(`${i}${Ds(e)}${a}</${e.name}>`)}function vs(e,t,n,r){let i=r.ind.repeat(n);if(Ms(e)){t.push(`${i}${Os(e)}`);return}if(Fs(e)){bs(e,t,n,r);return}let a=Ts(e.children),o=r.ind.repeat(n+1),s=`${i}${Ds(e)}${a.join(` `)}</${e.name}>`;if(r.printWidth===0||s.length<=r.printWidth){t.push(s);return}t.push(`${i}${Ds(e)}`);for(let e of Es(a,r.printWidth,o.length,r.breakSentences))t.push(`${o}${e}`);t.push(`${i}</${e.name}>`)}function ys(e,t,n,r){let i=r.ind.repeat(n),a=r.ind.repeat(n+1);if(Ms(e)){t.push(`${i}${Os(e)}`);return}t.push(`${i}${Ds(e)}`);let o=Ts(e.children);for(let e of Es(o,r.printWidth,a.length,r.breakSentences))t.push(`${a}${e}`);t.push(`${i}</${e.name}>`)}function bs(e,t,n,r){let i=r.ind.repeat(n),a=r.ind.repeat(n+1);if(Ms(e)){t.push(`${i}${Os(e)}`);return}t.push(`${i}${Ds(e)}`);let o=Ns(e),s=0;for(;s<o.length;)if(Ps(o[s])){let e=o[s];s++;let i=``;if(s<o.length&&o[s].type===`text`){let e=o[s].value,t=/^(\s*)([.,;:!?])/.exec(e);if(t){i=t[2];let n=e.slice(t[0].length);o[s].value=n,n.trim()||s++}}let a=[];hs(e,a,n+1,r),i&&a.length>0&&(a[a.length-1]+=i),t.push(...a)}else{let e=[];for(;s<o.length&&!Ps(o[s]);)e.push(o[s]),s++;let n=Ts(e);if(n.length>0)for(let e of Es(n,r.printWidth,a.length,r.breakSentences))t.push(`${a}${e}`)}t.push(`${i}</${e.name}>`)}function xs(e,t,n,r){let i=r.ind.repeat(n);if(Ms(e)){t.push(...js(e,n,r,!0));return}let a=Ns(e);if(a.length===1&&a[0].type===`element`&&a[0].name===`xi:include`){let o=a[0],s=js(e,n,r);if(s.length===1){let n=Ms(o)?Os(o):`${Ds(o)}${Cs(o.children)}</${o.name}>`;t.push(`${s[0]}${n}</${e.name}>`);return}t.push(...s),ms(o,t,n+1,r),t.push(`${i}</${e.name}>`);return}t.push(...js(e,n,r));for(let i of Ns(e))ms(i,t,n+1,r);t.push(`${i}</${e.name}>`)}function Ss(e,t){let n=[];for(let r=0;r<e.length;r++){let i=e[r].trim(),a=r<e.length-1?e[r+1].trim():null;if(n.push(e[r]),r===0&&/^<\?xml(?:\s|\?>)/.test(i)){n.push(``);continue}if(t.blankLines!==`few`)if(t.blankLines===`some`)if(i.startsWith(`</`)&&a!==null&&a.startsWith(`<`)){let e=/^<([^\s>/]+)/.exec(a)?.[1];e&&ss.includes(e)&&n.push(``)}else /^<title[\s>]/.test(i)&&i.includes(`</title>`)&&n.push(``);else t.blankLines===`many`&&(i.startsWith(`</`)||i.startsWith(`<`)&&a!==null&&a.startsWith(`<`))&&n.push(``)}return n}function Cs(e){return e.map(e=>e.type===`text`?Is(e.value):e.type===`element`?ws(e):e.type===`comment`?`<!--${e.value}-->`:e.type===`cdata`?`<![CDATA[${e.value}]]>`:``).join(``)}function ws(e){return Ms(e)?Os(e):`${Ds(e)}${Cs(e.children)}</${e.name}>`}function Ts(e){let t=[];for(let n of e)if(n.type===`text`){let e=n.value.split(/\s+/).filter(e=>e.length>0);for(let n=0;n<e.length;n++){let r=Is(e[n]);n===0&&t.length>0&&/^[.,;:!?\-]/.test(r)?t[t.length-1]+=r:t.push(r)}}else n.type===`element`?t.push(ws(n)):n.type===`comment`&&t.push(`<!--${n.value}-->`);return t}function Es(e,t,n,r){if(e.length===0)return[];let i=t===0?1/0:Math.max(20,t-n),a=[],o=``;for(let t=0;t<e.length;t++){let n=e[t];o===``?o=n:o.length+1+n.length<=i?o+=` `+n:(a.push(o),o=n),r&&/[.!?]$/.test(n)&&t<e.length-1&&(a.push(o),o=``)}return o&&a.push(o),a}function Ds(e){let t=ks(e);return t?`<${e.name} ${t}>`:`<${e.name}>`}function Os(e){let t=ks(e);return t?`<${e.name} ${t}/>`:`<${e.name}/>`}function ks(e){return As(e).join(` `)}function As(e){return Object.entries(e.attributes||{}).map(([e,t])=>t==null?e:`${e}="${Ls(t)}"`)}function js(e,t,n,r=!1){let i=n.ind.repeat(t),a=As(e),o=r?`/>`:`>`;if(a.length===0)return[`${i}<${e.name}${o}`];let s=`${i}<${e.name} ${a.join(` `)}${o}`;if(!n.breakLongAttributes||n.printWidth===0||s.length<=n.printWidth)return[s];let c=`${i}${` `.repeat(e.name.length+2)}`,l=[`${i}<${e.name} ${a[0]}`];for(let e of a.slice(1))l.push(`${c}${e}`);return l[l.length-1]+=o,l}function Ms(e){return e.children.every(e=>e.type===`text`&&e.value.trim()===``)}function Ns(e){return e.children.filter(e=>!(e.type===`text`&&e.value.trim()===``))}function Ps(e){if(e.type!==`element`)return!1;let t=e.name;return t===`c`?!1:(cs.includes(t)||as.includes(t))&&!ts.includes(t)}function Fs(e){return e.children.some(e=>Ps(e))}function Is(e){return e.replace(/&/g,`&amp;`).replace(/</g,`&lt;`).replace(/>/g,`&gt;`)}function Ls(e){return e.replace(/&/g,`&amp;`).replace(/"/g,`&quot;`).replace(/</g,`&lt;`)}var Rs=6,zs=({actions:e})=>{let r=(0,n.useRef)(null),i=(0,n.useRef)(null),a=(0,n.useRef)(null),o=(0,n.useRef)([]),s=(0,n.useRef)(0),[c,l]=(0,n.useState)(e.length),[u,d]=(0,n.useState)(!1),f=(0,n.useCallback)(()=>{let e=r.current,t=o.current;if(!e||t.length===0)return;let n=e.clientWidth,i=t.length>0?Rs*(t.length-1):0;if(t.reduce((e,t)=>e+t,0)+i<=n){l(t.length);return}let a=s.current,c=0;for(let e=0;e<t.length;e++){let r=a+t[e]+Rs;if(r<=n)a=r,c++;else break}l(c)},[]);(0,n.useLayoutEffect)(()=>{let e=i.current;if(!e)return;o.current=Array.from(e.querySelectorAll(`[data-measure-btn]`)).map(e=>e.offsetWidth);let t=e.querySelector(`[data-measure-more]`);s.current=t?t.offsetWidth+Rs:0,f()},[e.map(e=>`${e.key}:${e.label}:${+!!e.disabled}`).join(`|`),f]),(0,n.useEffect)(()=>{let e=r.current;if(!e||typeof ResizeObserver>`u`)return;let t=new ResizeObserver(()=>f());return t.observe(e),()=>t.disconnect()},[f]),(0,n.useEffect)(()=>{if(!u)return;let e=e=>{a.current?.contains(e.target)||d(!1)},t=e=>{e.key===`Escape`&&d(!1)};return document.addEventListener(`mousedown`,e),document.addEventListener(`keydown`,t),()=>{document.removeEventListener(`mousedown`,e),document.removeEventListener(`keydown`,t)}},[u]);let p=e.slice(0,c),m=e.slice(c);return(0,t.jsxs)(`div`,{className:`pretext-plus-editor__menu-actions`,ref:r,children:[p.map(e=>(0,t.jsx)(`button`,{className:`pretext-plus-editor__menu-button ${e.className??``}`,onClick:e.onClick,disabled:e.disabled,title:e.title,children:e.label},e.key)),m.length>0&&(0,t.jsxs)(`div`,{className:`pretext-plus-editor__menu-dropdown`,ref:a,children:[(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__menu-button`,onClick:()=>d(e=>!e),"aria-haspopup":`menu`,"aria-expanded":u,title:`More actions`,children:`More ▾`}),u&&(0,t.jsx)(`div`,{className:`pretext-plus-editor__menu-dropdown-list`,role:`menu`,children:m.map(e=>(0,t.jsx)(`button`,{type:`button`,role:`menuitem`,className:`pretext-plus-editor__menu-dropdown-item`,onClick:()=>{d(!1),e.onClick()},disabled:e.disabled,title:e.title,children:e.label},e.key))})]}),(0,t.jsxs)(`div`,{className:`pretext-plus-editor__menu-measure`,ref:i,"aria-hidden":!0,children:[e.map(e=>(0,t.jsx)(`button`,{"data-measure-btn":!0,className:`pretext-plus-editor__menu-button ${e.className??``}`,tabIndex:-1,children:e.label},e.key)),(0,t.jsx)(`button`,{"data-measure-more":!0,className:`pretext-plus-editor__menu-button`,tabIndex:-1,children:`More ▾`})]})]})},Bs=({content:e,sourceFormat:n,onContentChange:r,onOpenLatexImport:i,onOpenDocinfoEditor:a,onConvertToPretext:o,canConvertToPretext:s,onOpenAssets:c,hideAssets:l,onShowFullSource:u})=>{let d=()=>{try{r(ps(e))}catch(e){console.error(`Error formatting:`,e),alert(`Error formatting XML`)}},f={key:`full-source`,label:`Display Full Source`,onClick:u,title:`Show the full assembled PreTeXt source for the project`},p;return p=n===`latex`?[...o?[{key:`convert`,label:`Convert to PreTeXt`,onClick:o,title:`Create a new project copy using the converted PreTeXt source`,className:`pretext-plus-editor__menu-button--convert`,disabled:s===!1}]:[],{key:`preamble`,label:`Edit Preamble`,onClick:a,title:`Edit Preamble`},f]:n===`markdown`?[...o?[{key:`convert`,label:`Convert to PreTeXt`,onClick:o,title:`Create a new project copy using the converted PreTeXt source`,className:`pretext-plus-editor__menu-button--convert`,disabled:s===!1}]:[],{key:`macros`,label:`Edit Macros`,onClick:a,title:`Edit Macros`},f]:[{key:`format`,label:`Format PreTeXt`,onClick:d,title:`Format the PreTeXt source`},{key:`import-latex`,label:`Import LaTeX`,onClick:i,title:`Import LaTeX`},{key:`macros`,label:`Edit Macros`,onClick:a,title:`Edit Macros`},...c&&!l?[{key:`assets`,label:`Assets`,onClick:c,title:`Manage assets`}]:[],f],(0,t.jsxs)(`div`,{className:`pretext-plus-editor__code-editor-menu`,children:[(0,t.jsx)(zs,{actions:p}),(0,t.jsx)(`span`,{className:`pretext-plus-editor__code-editor-source-badge pretext-plus-editor__code-editor-source-badge--right`,children:n===`latex`?`LaTeX`:n===`markdown`?`Markdown`:`PreTeXt`})]})};function Vs(e){let t=e.getLineCount();if(t<2||!/^\s*<title\b/.test(e.getLineContent(2)))return 1;let n=2;for(;n<=t&&!/<\/title\s*>/.test(e.getLineContent(n));)n++;return n<=t?n:1}function Hs(e,t){let n=e.getLineCount();if(t===`pretext`){if(n<3)return null;let t=Vs(e);if(t>=n-1)return null;let r=[];for(let e=1;e<=t;e++)r.push(e);return r.push(n),{editableRange:[t+1,1,n-1,e.getLineMaxColumn(n-1)],lockedLines:r,leadingLockedLines:t}}if(t===`markdown`){if(e.getLineContent(1).trim()!==`---`)return null;let t=-1;for(let r=2;r<=n;r++)if(e.getLineContent(r).trim()===`---`){t=r;break}if(t===-1||t>=n)return null;let r=[];for(let e=1;e<=t;e++)r.push(e);return{editableRange:[t+1,1,n,e.getLineMaxColumn(n)],lockedLines:r,leadingLockedLines:t}}return t===`latex`?n<2||!/^\s*\\(?!begin\b|end\b)[A-Za-z][A-Za-z-]*\*?\{/.test(e.getLineContent(1))?null:{editableRange:[2,1,n,e.getLineMaxColumn(n)],lockedLines:[1],leadingLockedLines:1}:null}var Us={automaticLayout:!0,minimap:{enabled:!1},acceptSuggestionOnCommitCharacter:!1,quickSuggestions:!1,wordWrap:`on`,insertSpaces:!0,tabSize:2,padding:{top:10,bottom:10}},Ws=(0,n.forwardRef)(({content:e,sourceFormat:r,onChange:i,onRebuild:a,onSave:o,onOpenLatexImport:s,onOpenDocinfoEditor:c,onOpenConvertToPretext:l,canConvertToPretext:u,onOpenAssets:d,onShowFullSource:f,onRequestWrapperEdit:p,hideAssets:m},h)=>{let g=(0,n.useRef)(null),_=(0,n.useRef)(null),v=(0,n.useRef)(null),y=(0,n.useRef)(null),b=(0,n.useRef)(!1),x=(0,n.useRef)(0),S=(0,n.useRef)(null),C=(0,n.useRef)(null),w=(0,n.useRef)(p),T=(0,n.useRef)(null),E=(0,n.useRef)(null),D=(0,n.useRef)(!1),[O,k]=(0,n.useState)(!1),[A,ee]=(0,n.useState)(!1),te=(0,n.useRef)(a),j=(0,n.useRef)(o);(0,n.useImperativeHandle)(h,()=>({insertAtCursor:e=>{let t=g.current;if(!t)return;let n=t.getSelection();n&&(t.executeEdits(`insert-asset`,[{range:n,text:e,forceMoveMarkers:!0}]),t.focus())},focus:()=>{g.current?.focus()}}),[]),(0,n.useEffect)(()=>{te.current=a},[a]),(0,n.useEffect)(()=>{j.current=o},[o]),(0,n.useEffect)(()=>{w.current=p},[p]),(0,n.useEffect)(()=>{T.current?.dispose?.();let e=ko[r];T.current=_.current?e.registerMonacoExtensions?.(_.current)??null:null,N()},[r]),(0,n.useEffect)(()=>()=>{S.current?.dispose?.(),T.current?.dispose?.(),C.current?.dispose?.(),v.current?.disposeConstrainer?.(),E.current&&clearTimeout(E.current)},[]);let M=(e,t,n)=>{if(r!==`pretext`)return;let i=t.getLineCount();if(i<2)return;let a=t.getLineContent(1).trim();if(!a.startsWith(`<`)||a.startsWith(`</`)||a.endsWith(`/>`))return;let o=Vs(t);if(i!==o+1)return;let s=t.getLineContent(i).trim();if(!/^<\/[A-Za-z][\w.:-]*\s*>$/.test(s))return;D.current=!0;let c=t.getLineMaxColumn(o);e.executeEdits(`ensure-body-line`,[{range:new n.Range(o,c,o,c),text:`
438
+ `,forceMoveMarkers:!0}]),queueMicrotask(()=>{D.current=!1})},ne=(e,t,n)=>{if(r!==`pretext`)return;let i=t.getLineCount();if(i<2||t.getLineContent(i).trim()!==``)return;let a=i-1;for(;a>1&&t.getLineContent(a).trim()===``;)a--;let o=t.getLineContent(a).trim();/^<\/[A-Za-z][\w.:-]*\s*>$/.test(o)&&(D.current=!0,e.executeEdits(`trim-trailing-blank-lines`,[{range:new n.Range(a,t.getLineMaxColumn(a),i,t.getLineMaxColumn(i)),text:``,forceMoveMarkers:!0}]),queueMicrotask(()=>{D.current=!1}))},N=()=>{let e=g.current,t=_.current,n=v.current;if(!e||!t||!n)return;let i=e.getModel();if(!i)return;typeof i.disposeRestrictions==`function`&&i.disposeRestrictions(),ne(e,i,t),M(e,i,t);let a=Hs(i,r);if(b.current=a!==null,x.current=a?.leadingLockedLines??0,a&&n.addRestrictionsTo(i,[{range:a.editableRange,allowMultiline:!0}]),typeof e.createDecorationsCollection==`function`)if(y.current||=e.createDecorationsCollection(),!a)y.current.clear();else{let e={isWholeLine:!0,className:`pretext-plus-editor__locked-line`,inlineClassName:`pretext-plus-editor__locked-line-text`,stickiness:t.editor.TrackedRangeStickiness.NeverGrowsWhenTypingAtEdges,hoverMessage:{value:`These properties are structural — edit them from the Table of Contents.`}};y.current.set(a.lockedLines.map(n=>({range:new t.Range(n,1,n,i.getLineMaxColumn(n)),options:e})))}},re=(e,t)=>{e.getValue()!==t&&(D.current=!0,typeof e.disposeRestrictions==`function`&&e.disposeRestrictions(),e.setValue(t),N(),queueMicrotask(()=>{D.current=!1}))};(0,n.useEffect)(()=>{let t=g.current;if(!t)return;let n=t.getModel();if(n&&n.getValue()!==e){let r=t.getPosition(),i=t.getSelections();re(n,e),r&&t.setPosition(r),i&&t.setSelections(i)}},[e]);let ie=(t,n)=>{g.current=t,_.current=n,v.current=tr(n),v.current.initializeIn(t);let i=t.getModel();i&&i.getValue()!==e&&re(i,e),S.current?.dispose?.(),S.current=t.onDidChangeModelContent(()=>{ae()}),ae(),C.current?.dispose?.(),C.current=t.onMouseDown(e=>{let t=e?.target?.position?.lineNumber;b.current&&typeof t==`number`&&t<=x.current&&w.current?.()}),t.addCommand(n.KeyMod.CtrlCmd|n.KeyCode.Enter,()=>{te.current?.()}),t.addCommand(n.KeyMod.CtrlCmd|n.KeyCode.KeyS,()=>{j.current?.()}),T.current?.dispose?.(),T.current=ko[r].registerMonacoExtensions?.(n)??null,N()},ae=()=>{let e=g.current?.getModel?.();if(e&&typeof e.canUndo==`function`&&typeof e.canRedo==`function`)try{k(e.canUndo()),ee(e.canRedo());return}catch{}let t=!!g.current;k(t),ee(t)},oe=e=>{i(e)};return(0,t.jsxs)(`div`,{style:{display:`flex`,flexDirection:`column`,height:`100%`},children:[(0,t.jsx)(Bs,{content:e,sourceFormat:r,onContentChange:oe,onOpenLatexImport:s,onOpenDocinfoEditor:c,onUndo:()=>{g.current?.trigger(``,`undo`),ae()},onRedo:()=>{g.current?.trigger(``,`redo`),ae()},canUndo:O,canRedo:A,onConvertToPretext:l,canConvertToPretext:u,onOpenAssets:d,onShowFullSource:f,hideAssets:m}),(0,t.jsx)(`div`,{style:{flex:1},children:(0,t.jsx)(Yn,{options:Us,height:`100%`,language:ko[r].language,defaultValue:e,onMount:ie,onChange:(e,t)=>{t?.isFlush||D.current||(E.current&&clearTimeout(E.current),E.current=setTimeout(()=>{oe(e||``)},500))}})})]})});Ws.displayName=`CodeEditor`;function Gs(e){this.content=e}Gs.prototype={constructor:Gs,find:function(e){for(var t=0;t<this.content.length;t+=2)if(this.content[t]===e)return t;return-1},get:function(e){var t=this.find(e);return t==-1?void 0:this.content[t+1]},update:function(e,t,n){var r=n&&n!=e?this.remove(n):this,i=r.find(e),a=r.content.slice();return i==-1?a.push(n||e,t):(a[i+1]=t,n&&(a[i]=n)),new Gs(a)},remove:function(e){var t=this.find(e);if(t==-1)return this;var n=this.content.slice();return n.splice(t,2),new Gs(n)},addToStart:function(e,t){return new Gs([e,t].concat(this.remove(e).content))},addToEnd:function(e,t){var n=this.remove(e).content.slice();return n.push(e,t),new Gs(n)},addBefore:function(e,t,n){var r=this.remove(t),i=r.content.slice(),a=r.find(e);return i.splice(a==-1?i.length:a,0,t,n),new Gs(i)},forEach:function(e){for(var t=0;t<this.content.length;t+=2)e(this.content[t],this.content[t+1])},prepend:function(e){return e=Gs.from(e),e.size?new Gs(e.content.concat(this.subtract(e).content)):this},append:function(e){return e=Gs.from(e),e.size?new Gs(this.subtract(e).content.concat(e.content)):this},subtract:function(e){var t=this;e=Gs.from(e);for(var n=0;n<e.content.length;n+=2)t=t.remove(e.content[n]);return t},toObject:function(){var e={};return this.forEach(function(t,n){e[t]=n}),e},get size(){return this.content.length>>1}},Gs.from=function(e){if(e instanceof Gs)return e;var t=[];if(e)for(var n in e)t.push(n,e[n]);return new Gs(t)};function Ks(e,t,n){for(let r=0;;r++){if(r==e.childCount||r==t.childCount)return e.childCount==t.childCount?null:n;let i=e.child(r),a=t.child(r);if(i==a){n+=i.nodeSize;continue}if(!i.sameMarkup(a))return n;if(i.isText&&i.text!=a.text){let e=i.text,t=a.text,r=0;for(;e[r]==t[r];r++)n++;return r&&r<e.length&&r<t.length&&Ys(e.charCodeAt(r-1))&&Js(e.charCodeAt(r))&&n--,n}if(i.content.size||a.content.size){let e=Ks(i.content,a.content,n+1);if(e!=null)return e}n+=i.nodeSize}}function qs(e,t,n,r){for(let i=e.childCount,a=t.childCount;;){if(i==0||a==0)return i==a?null:{a:n,b:r};let o=e.child(--i),s=t.child(--a),c=o.nodeSize;if(o==s){n-=c,r-=c;continue}if(!o.sameMarkup(s))return{a:n,b:r};if(o.isText&&o.text!=s.text){let e=o.text,t=s.text,i=e.length,a=t.length;for(;i>0&&a>0&&e[i-1]==t[a-1];)i--,a--,n--,r--;return i&&a&&i<e.length&&Ys(e.charCodeAt(i-1))&&Js(e.charCodeAt(i))&&(n++,r++),{a:n,b:r}}if(o.content.size||s.content.size){let e=qs(o.content,s.content,n-1,r-1);if(e)return e}n-=c,r-=c}}function Js(e){return e>=56320&&e<57344}function Ys(e){return e>=55296&&e<56320}var Xs=class e{constructor(e,t){if(this.content=e,this.size=t||0,t==null)for(let t=0;t<e.length;t++)this.size+=e[t].nodeSize}nodesBetween(e,t,n,r=0,i){for(let a=0,o=0;o<t;a++){let s=this.content[a],c=o+s.nodeSize;if(c>e&&n(s,r+o,i||null,a)!==!1&&s.content.size){let i=o+1;s.nodesBetween(Math.max(0,e-i),Math.min(s.content.size,t-i),n,r+i)}o=c}}descendants(e){this.nodesBetween(0,this.size,e)}textBetween(e,t,n,r){let i=``,a=!0;return this.nodesBetween(e,t,(o,s)=>{let c=o.isText?o.text.slice(Math.max(e,s)-s,t-s):o.isLeaf?r?typeof r==`function`?r(o):r:o.type.spec.leafText?o.type.spec.leafText(o):``:``;o.isBlock&&(o.isLeaf&&c||o.isTextblock)&&n&&(a?a=!1:i+=n),i+=c},0),i}append(t){if(!t.size)return this;if(!this.size)return t;let n=this.lastChild,r=t.firstChild,i=this.content.slice(),a=0;for(n.isText&&n.sameMarkup(r)&&(i[i.length-1]=n.withText(n.text+r.text),a=1);a<t.content.length;a++)i.push(t.content[a]);return new e(i,this.size+t.size)}cut(t,n=this.size){if(t==0&&n==this.size)return this;let r=[],i=0;if(n>t)for(let e=0,a=0;a<n;e++){let o=this.content[e],s=a+o.nodeSize;s>t&&((a<t||s>n)&&(o=o.isText?o.cut(Math.max(0,t-a),Math.min(o.text.length,n-a)):o.cut(Math.max(0,t-a-1),Math.min(o.content.size,n-a-1))),r.push(o),i+=o.nodeSize),a=s}return new e(r,i)}cutByIndex(t,n){return t==n?e.empty:t==0&&n==this.content.length?this:new e(this.content.slice(t,n))}replaceChild(t,n){let r=this.content[t];if(r==n)return this;let i=this.content.slice(),a=this.size+n.nodeSize-r.nodeSize;return i[t]=n,new e(i,a)}addToStart(t){return new e([t].concat(this.content),this.size+t.nodeSize)}addToEnd(t){return new e(this.content.concat(t),this.size+t.nodeSize)}eq(e){if(this.content.length!=e.content.length)return!1;for(let t=0;t<this.content.length;t++)if(!this.content[t].eq(e.content[t]))return!1;return!0}get firstChild(){return this.content.length?this.content[0]:null}get lastChild(){return this.content.length?this.content[this.content.length-1]:null}get childCount(){return this.content.length}child(e){let t=this.content[e];if(!t)throw RangeError(`Index `+e+` out of range for `+this);return t}maybeChild(e){return this.content[e]||null}forEach(e){for(let t=0,n=0;t<this.content.length;t++){let r=this.content[t];e(r,n,t),n+=r.nodeSize}}findDiffStart(e,t=0){return Ks(this,e,t)}findDiffEnd(e,t=this.size,n=e.size){return qs(this,e,t,n)}findIndex(e){if(e==0)return Qs(0,e);if(e==this.size)return Qs(this.content.length,e);if(e>this.size||e<0)throw RangeError(`Position ${e} outside of fragment (${this})`);for(let t=0,n=0;;t++){let r=this.child(t),i=n+r.nodeSize;if(i>=e)return i==e?Qs(t+1,i):Qs(t,n);n=i}}toString(){return`<`+this.toStringInner()+`>`}toStringInner(){return this.content.join(`, `)}toJSON(){return this.content.length?this.content.map(e=>e.toJSON()):null}static fromJSON(t,n){if(!n)return e.empty;if(!Array.isArray(n))throw RangeError(`Invalid input for Fragment.fromJSON`);return e.fromArray(n.map(t.nodeFromJSON))}static fromArray(t){if(!t.length)return e.empty;let n,r=0;for(let e=0;e<t.length;e++){let i=t[e];r+=i.nodeSize,e&&i.isText&&t[e-1].sameMarkup(i)?(n||=t.slice(0,e),n[n.length-1]=i.withText(n[n.length-1].text+i.text)):n&&n.push(i)}return new e(n||t,r)}static from(t){if(!t)return e.empty;if(t instanceof e)return t;if(Array.isArray(t))return this.fromArray(t);if(t.attrs)return new e([t],t.nodeSize);throw RangeError(`Can not convert `+t+` to a Fragment`+(t.nodesBetween?` (looks like multiple versions of prosemirror-model were loaded)`:``))}};Xs.empty=new Xs([],0);var Zs={index:0,offset:0};function Qs(e,t){return Zs.index=e,Zs.offset=t,Zs}function $s(e,t){if(e===t)return!0;if(!(e&&typeof e==`object`)||!(t&&typeof t==`object`))return!1;let n=Array.isArray(e);if(Array.isArray(t)!=n)return!1;if(n){if(e.length!=t.length)return!1;for(let n=0;n<e.length;n++)if(!$s(e[n],t[n]))return!1}else{for(let n in e)if(!(n in t)||!$s(e[n],t[n]))return!1;for(let n in t)if(!(n in e))return!1}return!0}var ec=class e{constructor(e,t){this.type=e,this.attrs=t}addToSet(e){let t,n=!1;for(let r=0;r<e.length;r++){let i=e[r];if(this.eq(i))return e;if(this.type.excludes(i.type))t||=e.slice(0,r);else if(i.type.excludes(this.type))return e;else !n&&i.type.rank>this.type.rank&&(t||=e.slice(0,r),t.push(this),n=!0),t&&t.push(i)}return t||=e.slice(),n||t.push(this),t}removeFromSet(e){for(let t=0;t<e.length;t++)if(this.eq(e[t]))return e.slice(0,t).concat(e.slice(t+1));return e}isInSet(e){for(let t=0;t<e.length;t++)if(this.eq(e[t]))return!0;return!1}eq(e){return this==e||this.type==e.type&&$s(this.attrs,e.attrs)}toJSON(){let e={type:this.type.name};for(let t in this.attrs){e.attrs=this.attrs;break}return e}static fromJSON(e,t){if(!t)throw RangeError(`Invalid input for Mark.fromJSON`);let n=e.marks[t.type];if(!n)throw RangeError(`There is no mark type ${t.type} in this schema`);let r=n.create(t.attrs);return n.checkAttrs(r.attrs),r}static sameSet(e,t){if(e==t)return!0;if(e.length!=t.length)return!1;for(let n=0;n<e.length;n++)if(!e[n].eq(t[n]))return!1;return!0}static setFrom(t){if(!t||Array.isArray(t)&&t.length==0)return e.none;if(t instanceof e)return[t];let n=t.slice();return n.sort((e,t)=>e.type.rank-t.type.rank),n}};ec.none=[];var tc=class extends Error{},nc=class e{constructor(e,t,n){this.content=e,this.openStart=t,this.openEnd=n}get size(){return this.content.size-this.openStart-this.openEnd}insertAt(t,n){let r=ic(this.content,t+this.openStart,n,this.openStart+1,this.openEnd+1);return r&&new e(r,this.openStart,this.openEnd)}removeBetween(t,n){return new e(rc(this.content,t+this.openStart,n+this.openStart),this.openStart,this.openEnd)}eq(e){return this.content.eq(e.content)&&this.openStart==e.openStart&&this.openEnd==e.openEnd}toString(){return this.content+`(`+this.openStart+`,`+this.openEnd+`)`}toJSON(){if(!this.content.size)return null;let e={content:this.content.toJSON()};return this.openStart>0&&(e.openStart=this.openStart),this.openEnd>0&&(e.openEnd=this.openEnd),e}static fromJSON(t,n){if(!n)return e.empty;let r=n.openStart||0,i=n.openEnd||0;if(typeof r!=`number`||typeof i!=`number`)throw RangeError(`Invalid input for Slice.fromJSON`);return new e(Xs.fromJSON(t,n.content),r,i)}static maxOpen(t,n=!0){let r=0,i=0;for(let e=t.firstChild;e&&!e.isLeaf&&(n||!e.type.spec.isolating);e=e.firstChild)r++;for(let e=t.lastChild;e&&!e.isLeaf&&(n||!e.type.spec.isolating);e=e.lastChild)i++;return new e(t,r,i)}};nc.empty=new nc(Xs.empty,0,0);function rc(e,t,n){let{index:r,offset:i}=e.findIndex(t),a=e.maybeChild(r),{index:o,offset:s}=e.findIndex(n);if(i==t||a.isText){if(s!=n&&!e.child(o).isText)throw RangeError(`Removing non-flat range`);return e.cut(0,t).append(e.cut(n))}if(r!=o)throw RangeError(`Removing non-flat range`);return e.replaceChild(r,a.copy(rc(a.content,t-i-1,n-i-1)))}function ic(e,t,n,r,i,a){let{index:o,offset:s}=e.findIndex(t),c=e.maybeChild(o);if(s==t||c.isText)return a&&r<=0&&i<=0&&!a.canReplace(o,o,n)?null:e.cut(0,t).append(n).append(e.cut(t));let l=ic(c.content,t-s-1,n,o==0?r-1:0,o==e.childCount-1?i-1:0,c);return l&&e.replaceChild(o,c.copy(l))}function ac(e,t,n){if(n.openStart>e.depth)throw new tc(`Inserted content deeper than insertion position`);if(e.depth-n.openStart!=t.depth-n.openEnd)throw new tc(`Inconsistent open depths`);return oc(e,t,n,0)}function oc(e,t,n,r){let i=e.index(r),a=e.node(r);if(i==t.index(r)&&r<e.depth-n.openStart){let o=oc(e,t,n,r+1);return a.copy(a.content.replaceChild(i,o))}else if(!n.content.size)return dc(a,pc(e,t,r));else if(!n.openStart&&!n.openEnd&&e.depth==r&&t.depth==r){let r=e.parent,i=r.content;return dc(r,i.cut(0,e.parentOffset).append(n.content).append(i.cut(t.parentOffset)))}else{let{start:i,end:o}=mc(n,e);return dc(a,fc(e,i,o,t,r))}}function sc(e,t){if(!t.type.compatibleContent(e.type))throw new tc(`Cannot join `+t.type.name+` onto `+e.type.name)}function cc(e,t,n){let r=e.node(n);return sc(r,t.node(n)),r}function lc(e,t){let n=t.length-1;n>=0&&e.isText&&e.sameMarkup(t[n])?t[n]=e.withText(t[n].text+e.text):t.push(e)}function uc(e,t,n,r){let i=(t||e).node(n),a=0,o=t?t.index(n):i.childCount;e&&(a=e.index(n),e.depth>n?a++:e.textOffset&&(lc(e.nodeAfter,r),a++));for(let e=a;e<o;e++)lc(i.child(e),r);t&&t.depth==n&&t.textOffset&&lc(t.nodeBefore,r)}function dc(e,t){if(!e.type.validContent(t))throw new tc(`Invalid content for node `+e.type.name);return e.copy(t)}function fc(e,t,n,r,i){let a=e.depth>i&&cc(e,t,i+1),o=r.depth>i&&cc(n,r,i+1),s=[];return uc(null,e,i,s),a&&o&&t.index(i)==n.index(i)?(sc(a,o),lc(dc(a,fc(e,t,n,r,i+1)),s)):(a&&lc(dc(a,pc(e,t,i+1)),s),uc(t,n,i,s),o&&lc(dc(o,pc(n,r,i+1)),s)),uc(r,null,i,s),new Xs(s)}function pc(e,t,n){let r=[];return uc(null,e,n,r),e.depth>n&&lc(dc(cc(e,t,n+1),pc(e,t,n+1)),r),uc(t,null,n,r),new Xs(r)}function mc(e,t){let n=t.depth-e.openStart,r=t.node(n).copy(e.content);for(let e=n-1;e>=0;e--)r=t.node(e).copy(Xs.from(r));return{start:r.resolveNoCache(e.openStart+n),end:r.resolveNoCache(r.content.size-e.openEnd-n)}}var hc=class e{constructor(e,t,n){this.pos=e,this.path=t,this.parentOffset=n,this.depth=t.length/3-1}resolveDepth(e){return e==null?this.depth:e<0?this.depth+e:e}get parent(){return this.node(this.depth)}get doc(){return this.node(0)}node(e){return this.path[this.resolveDepth(e)*3]}index(e){return this.path[this.resolveDepth(e)*3+1]}indexAfter(e){return e=this.resolveDepth(e),this.index(e)+(e==this.depth&&!this.textOffset?0:1)}start(e){return e=this.resolveDepth(e),e==0?0:this.path[e*3-1]+1}end(e){return e=this.resolveDepth(e),this.start(e)+this.node(e).content.size}before(e){if(e=this.resolveDepth(e),!e)throw RangeError(`There is no position before the top-level node`);return e==this.depth+1?this.pos:this.path[e*3-1]}after(e){if(e=this.resolveDepth(e),!e)throw RangeError(`There is no position after the top-level node`);return e==this.depth+1?this.pos:this.path[e*3-1]+this.path[e*3].nodeSize}get textOffset(){return this.pos-this.path[this.path.length-1]}get nodeAfter(){let e=this.parent,t=this.index(this.depth);if(t==e.childCount)return null;let n=this.pos-this.path[this.path.length-1],r=e.child(t);return n?e.child(t).cut(n):r}get nodeBefore(){let e=this.index(this.depth),t=this.pos-this.path[this.path.length-1];return t?this.parent.child(e).cut(0,t):e==0?null:this.parent.child(e-1)}posAtIndex(e,t){t=this.resolveDepth(t);let n=this.path[t*3],r=t==0?0:this.path[t*3-1]+1;for(let t=0;t<e;t++)r+=n.child(t).nodeSize;return r}marks(){let e=this.parent,t=this.index();if(e.content.size==0)return ec.none;if(this.textOffset)return e.child(t).marks;let n=e.maybeChild(t-1),r=e.maybeChild(t);if(!n){let e=n;n=r,r=e}let i=n.marks;for(var a=0;a<i.length;a++)i[a].type.spec.inclusive===!1&&(!r||!i[a].isInSet(r.marks))&&(i=i[a--].removeFromSet(i));return i}marksAcross(e){let t=this.parent.maybeChild(this.index());if(!t||!t.isInline)return null;let n=t.marks,r=e.parent.maybeChild(e.index());for(var i=0;i<n.length;i++)n[i].type.spec.inclusive===!1&&(!r||!n[i].isInSet(r.marks))&&(n=n[i--].removeFromSet(n));return n}sharedDepth(e){for(let t=this.depth;t>0;t--)if(this.start(t)<=e&&this.end(t)>=e)return t;return 0}blockRange(e=this,t){if(e.pos<this.pos)return e.blockRange(this);for(let n=this.depth-(this.parent.inlineContent||this.pos==e.pos?1:0);n>=0;n--)if(e.pos<=this.end(n)&&(!t||t(this.node(n))))return new yc(this,e,n);return null}sameParent(e){return this.pos-this.parentOffset==e.pos-e.parentOffset}max(e){return e.pos>this.pos?e:this}min(e){return e.pos<this.pos?e:this}toString(){let e=``;for(let t=1;t<=this.depth;t++)e+=(e?`/`:``)+this.node(t).type.name+`_`+this.index(t-1);return e+`:`+this.parentOffset}static resolve(t,n){if(!(n>=0&&n<=t.content.size))throw RangeError(`Position `+n+` out of range`);let r=[],i=0,a=n;for(let e=t;;){let{index:t,offset:n}=e.content.findIndex(a),o=a-n;if(r.push(e,t,i+n),!o||(e=e.child(t),e.isText))break;a=o-1,i+=n+1}return new e(n,r,a)}static resolveCached(t,n){let r=vc.get(t);if(r)for(let e=0;e<r.elts.length;e++){let t=r.elts[e];if(t.pos==n)return t}else vc.set(t,r=new gc);let i=r.elts[r.i]=e.resolve(t,n);return r.i=(r.i+1)%_c,i}},gc=class{constructor(){this.elts=[],this.i=0}},_c=12,vc=new WeakMap,yc=class{constructor(e,t,n){this.$from=e,this.$to=t,this.depth=n}get start(){return this.$from.before(this.depth+1)}get end(){return this.$to.after(this.depth+1)}get parent(){return this.$from.node(this.depth)}get startIndex(){return this.$from.index(this.depth)}get endIndex(){return this.$to.indexAfter(this.depth)}},bc=Object.create(null),xc=class e{constructor(e,t,n,r=ec.none){this.type=e,this.attrs=t,this.marks=r,this.content=n||Xs.empty}get children(){return this.content.content}get nodeSize(){return this.isLeaf?1:2+this.content.size}get childCount(){return this.content.childCount}child(e){return this.content.child(e)}maybeChild(e){return this.content.maybeChild(e)}forEach(e){this.content.forEach(e)}nodesBetween(e,t,n,r=0){this.content.nodesBetween(e,t,n,r,this)}descendants(e){this.nodesBetween(0,this.content.size,e)}get textContent(){return this.isLeaf&&this.type.spec.leafText?this.type.spec.leafText(this):this.textBetween(0,this.content.size,``)}textBetween(e,t,n,r){return this.content.textBetween(e,t,n,r)}get firstChild(){return this.content.firstChild}get lastChild(){return this.content.lastChild}eq(e){return this==e||this.sameMarkup(e)&&this.content.eq(e.content)}sameMarkup(e){return this.hasMarkup(e.type,e.attrs,e.marks)}hasMarkup(e,t,n){return this.type==e&&$s(this.attrs,t||e.defaultAttrs||bc)&&ec.sameSet(this.marks,n||ec.none)}copy(t=null){return t==this.content?this:new e(this.type,this.attrs,t,this.marks)}mark(t){return t==this.marks?this:new e(this.type,this.attrs,this.content,t)}cut(e,t=this.content.size){return e==0&&t==this.content.size?this:this.copy(this.content.cut(e,t))}slice(e,t=this.content.size,n=!1){if(e==t)return nc.empty;let r=this.resolve(e),i=this.resolve(t),a=n?0:r.sharedDepth(t),o=r.start(a);return new nc(r.node(a).content.cut(r.pos-o,i.pos-o),r.depth-a,i.depth-a)}replace(e,t,n){return ac(this.resolve(e),this.resolve(t),n)}nodeAt(e){for(let t=this;;){let{index:n,offset:r}=t.content.findIndex(e);if(t=t.maybeChild(n),!t)return null;if(r==e||t.isText)return t;e-=r+1}}childAfter(e){let{index:t,offset:n}=this.content.findIndex(e);return{node:this.content.maybeChild(t),index:t,offset:n}}childBefore(e){if(e==0)return{node:null,index:0,offset:0};let{index:t,offset:n}=this.content.findIndex(e);if(n<e)return{node:this.content.child(t),index:t,offset:n};let r=this.content.child(t-1);return{node:r,index:t-1,offset:n-r.nodeSize}}resolve(e){return hc.resolveCached(this,e)}resolveNoCache(e){return hc.resolve(this,e)}rangeHasMark(e,t,n){let r=!1;return t>e&&this.nodesBetween(e,t,e=>(n.isInSet(e.marks)&&(r=!0),!r)),r}get isBlock(){return this.type.isBlock}get isTextblock(){return this.type.isTextblock}get inlineContent(){return this.type.inlineContent}get isInline(){return this.type.isInline}get isText(){return this.type.isText}get isLeaf(){return this.type.isLeaf}get isAtom(){return this.type.isAtom}toString(){if(this.type.spec.toDebugString)return this.type.spec.toDebugString(this);let e=this.type.name;return this.content.size&&(e+=`(`+this.content.toStringInner()+`)`),Cc(this.marks,e)}contentMatchAt(e){let t=this.type.contentMatch.matchFragment(this.content,0,e);if(!t)throw Error(`Called contentMatchAt on a node with invalid content`);return t}canReplace(e,t,n=Xs.empty,r=0,i=n.childCount){let a=this.contentMatchAt(e).matchFragment(n,r,i),o=a&&a.matchFragment(this.content,t);if(!o||!o.validEnd)return!1;for(let e=r;e<i;e++)if(!this.type.allowsMarks(n.child(e).marks))return!1;return!0}canReplaceWith(e,t,n,r){if(r&&!this.type.allowsMarks(r))return!1;let i=this.contentMatchAt(e).matchType(n),a=i&&i.matchFragment(this.content,t);return a?a.validEnd:!1}canAppend(e){return e.content.size?this.canReplace(this.childCount,this.childCount,e.content):this.type.compatibleContent(e.type)}check(){this.type.checkContent(this.content),this.type.checkAttrs(this.attrs);let e=ec.none;for(let t=0;t<this.marks.length;t++){let n=this.marks[t];n.type.checkAttrs(n.attrs),e=n.addToSet(e)}if(!ec.sameSet(e,this.marks))throw RangeError(`Invalid collection of marks for node ${this.type.name}: ${this.marks.map(e=>e.type.name)}`);this.content.forEach(e=>e.check())}toJSON(){let e={type:this.type.name};for(let t in this.attrs){e.attrs=this.attrs;break}return this.content.size&&(e.content=this.content.toJSON()),this.marks.length&&(e.marks=this.marks.map(e=>e.toJSON())),e}static fromJSON(e,t){if(!t)throw RangeError(`Invalid input for Node.fromJSON`);let n;if(t.marks){if(!Array.isArray(t.marks))throw RangeError(`Invalid mark data for Node.fromJSON`);n=t.marks.map(e.markFromJSON)}if(t.type==`text`){if(typeof t.text!=`string`)throw RangeError(`Invalid text node in JSON`);return e.text(t.text,n)}let r=Xs.fromJSON(e,t.content),i=e.nodeType(t.type).create(t.attrs,r,n);return i.type.checkAttrs(i.attrs),i}};xc.prototype.text=void 0;var Sc=class e extends xc{constructor(e,t,n,r){if(super(e,t,null,r),!n)throw RangeError(`Empty text nodes are not allowed`);this.text=n}toString(){return this.type.spec.toDebugString?this.type.spec.toDebugString(this):Cc(this.marks,JSON.stringify(this.text))}get textContent(){return this.text}textBetween(e,t){return this.text.slice(e,t)}get nodeSize(){return this.text.length}mark(t){return t==this.marks?this:new e(this.type,this.attrs,this.text,t)}withText(t){return t==this.text?this:new e(this.type,this.attrs,t,this.marks)}cut(e=0,t=this.text.length){return e==0&&t==this.text.length?this:this.withText(this.text.slice(e,t))}eq(e){return this.sameMarkup(e)&&this.text==e.text}toJSON(){let e=super.toJSON();return e.text=this.text,e}};function Cc(e,t){for(let n=e.length-1;n>=0;n--)t=e[n].type.name+`(`+t+`)`;return t}var wc=class e{constructor(e){this.validEnd=e,this.next=[],this.wrapCache=[]}static parse(t,n){let r=new Tc(t,n);if(r.next==null)return e.empty;let i=Ec(r);r.next&&r.err(`Unexpected trailing text`);let a=Ic(Nc(i));return Lc(a,r),a}matchType(e){for(let t=0;t<this.next.length;t++)if(this.next[t].type==e)return this.next[t].next;return null}matchFragment(e,t=0,n=e.childCount){let r=this;for(let i=t;r&&i<n;i++)r=r.matchType(e.child(i).type);return r}get inlineContent(){return this.next.length!=0&&this.next[0].type.isInline}get defaultType(){for(let e=0;e<this.next.length;e++){let{type:t}=this.next[e];if(!(t.isText||t.hasRequiredAttrs()))return t}return null}compatible(e){for(let t=0;t<this.next.length;t++)for(let n=0;n<e.next.length;n++)if(this.next[t].type==e.next[n].type)return!0;return!1}fillBefore(e,t=!1,n=0){let r=[this];function i(a,o){let s=a.matchFragment(e,n);if(s&&(!t||s.validEnd))return Xs.from(o.map(e=>e.createAndFill()));for(let e=0;e<a.next.length;e++){let{type:t,next:n}=a.next[e];if(!(t.isText||t.hasRequiredAttrs())&&r.indexOf(n)==-1){r.push(n);let e=i(n,o.concat(t));if(e)return e}}return null}return i(this,[])}findWrapping(e){for(let t=0;t<this.wrapCache.length;t+=2)if(this.wrapCache[t]==e)return this.wrapCache[t+1];let t=this.computeWrapping(e);return this.wrapCache.push(e,t),t}computeWrapping(e){let t=Object.create(null),n=[{match:this,type:null,via:null}];for(;n.length;){let r=n.shift(),i=r.match;if(i.matchType(e)){let e=[];for(let t=r;t.type;t=t.via)e.push(t.type);return e.reverse()}for(let e=0;e<i.next.length;e++){let{type:a,next:o}=i.next[e];!a.isLeaf&&!a.hasRequiredAttrs()&&!(a.name in t)&&(!r.type||o.validEnd)&&(n.push({match:a.contentMatch,type:a,via:r}),t[a.name]=!0)}}return null}get edgeCount(){return this.next.length}edge(e){if(e>=this.next.length)throw RangeError(`There's no ${e}th edge in this content match`);return this.next[e]}toString(){let e=[];function t(n){e.push(n);for(let r=0;r<n.next.length;r++)e.indexOf(n.next[r].next)==-1&&t(n.next[r].next)}return t(this),e.map((t,n)=>{let r=n+(t.validEnd?`*`:` `)+` `;for(let n=0;n<t.next.length;n++)r+=(n?`, `:``)+t.next[n].type.name+`->`+e.indexOf(t.next[n].next);return r}).join(`
439
439
  `)}};wc.empty=new wc(!0);var Tc=class{constructor(e,t){this.string=e,this.nodeTypes=t,this.inline=null,this.pos=0,this.tokens=e.split(/\s*(?=\b|\W|$)/),this.tokens[this.tokens.length-1]==``&&this.tokens.pop(),this.tokens[0]==``&&this.tokens.shift()}get next(){return this.tokens[this.pos]}eat(e){return this.next==e&&(this.pos++||!0)}err(e){throw SyntaxError(e+` (in content expression '`+this.string+`')`)}};function Ec(e){let t=[];do t.push(Dc(e));while(e.eat(`|`));return t.length==1?t[0]:{type:`choice`,exprs:t}}function Dc(e){let t=[];do t.push(Oc(e));while(e.next&&e.next!=`)`&&e.next!=`|`);return t.length==1?t[0]:{type:`seq`,exprs:t}}function Oc(e){let t=Mc(e);for(;;)if(e.eat(`+`))t={type:`plus`,expr:t};else if(e.eat(`*`))t={type:`star`,expr:t};else if(e.eat(`?`))t={type:`opt`,expr:t};else if(e.eat(`{`))t=Ac(e,t);else break;return t}function kc(e){/\D/.test(e.next)&&e.err(`Expected number, got '`+e.next+`'`);let t=Number(e.next);return e.pos++,t}function Ac(e,t){let n=kc(e),r=n;return e.eat(`,`)&&(r=e.next==`}`?-1:kc(e)),e.eat(`}`)||e.err(`Unclosed braced range`),{type:`range`,min:n,max:r,expr:t}}function jc(e,t){let n=e.nodeTypes,r=n[t];if(r)return[r];let i=[];for(let e in n){let r=n[e];r.isInGroup(t)&&i.push(r)}return i.length==0&&e.err(`No node type or group '`+t+`' found`),i}function Mc(e){if(e.eat(`(`)){let t=Ec(e);return e.eat(`)`)||e.err(`Missing closing paren`),t}else if(/\W/.test(e.next))e.err(`Unexpected token '`+e.next+`'`);else{let t=jc(e,e.next).map(t=>(e.inline==null?e.inline=t.isInline:e.inline!=t.isInline&&e.err(`Mixing inline and block content`),{type:`name`,value:t}));return e.pos++,t.length==1?t[0]:{type:`choice`,exprs:t}}}function Nc(e){let t=[[]];return i(a(e,0),n()),t;function n(){return t.push([])-1}function r(e,n,r){let i={term:r,to:n};return t[e].push(i),i}function i(e,t){e.forEach(e=>e.to=t)}function a(e,t){if(e.type==`choice`)return e.exprs.reduce((e,n)=>e.concat(a(n,t)),[]);if(e.type==`seq`)for(let r=0;;r++){let o=a(e.exprs[r],t);if(r==e.exprs.length-1)return o;i(o,t=n())}else if(e.type==`star`){let o=n();return r(t,o),i(a(e.expr,o),o),[r(o)]}else if(e.type==`plus`){let o=n();return i(a(e.expr,t),o),i(a(e.expr,o),o),[r(o)]}else if(e.type==`opt`)return[r(t)].concat(a(e.expr,t));else if(e.type==`range`){let o=t;for(let t=0;t<e.min;t++){let t=n();i(a(e.expr,o),t),o=t}if(e.max==-1)i(a(e.expr,o),o);else for(let t=e.min;t<e.max;t++){let t=n();r(o,t),i(a(e.expr,o),t),o=t}return[r(o)]}else if(e.type==`name`)return[r(t,void 0,e.value)];else throw Error(`Unknown expr type`)}}function Pc(e,t){return t-e}function Fc(e,t){let n=[];return r(t),n.sort(Pc);function r(t){let i=e[t];if(i.length==1&&!i[0].term)return r(i[0].to);n.push(t);for(let e=0;e<i.length;e++){let{term:t,to:a}=i[e];!t&&n.indexOf(a)==-1&&r(a)}}}function Ic(e){let t=Object.create(null);return n(Fc(e,0));function n(r){let i=[];r.forEach(t=>{e[t].forEach(({term:t,to:n})=>{if(!t)return;let r;for(let e=0;e<i.length;e++)i[e][0]==t&&(r=i[e][1]);Fc(e,n).forEach(e=>{r||i.push([t,r=[]]),r.indexOf(e)==-1&&r.push(e)})})});let a=t[r.join(`,`)]=new wc(r.indexOf(e.length-1)>-1);for(let e=0;e<i.length;e++){let r=i[e][1].sort(Pc);a.next.push({type:i[e][0],next:t[r.join(`,`)]||n(r)})}return a}}function Lc(e,t){for(let n=0,r=[e];n<r.length;n++){let e=r[n],i=!e.validEnd,a=[];for(let t=0;t<e.next.length;t++){let{type:n,next:o}=e.next[t];a.push(n.name),i&&!(n.isText||n.hasRequiredAttrs())&&(i=!1),r.indexOf(o)==-1&&r.push(o)}i&&t.err(`Only non-generatable nodes (`+a.join(`, `)+`) in a required position (see https://prosemirror.net/docs/guide/#generatable)`)}}function Rc(e){let t=Object.create(null);for(let n in e){let r=e[n];if(!r.hasDefault)return null;t[n]=r.default}return t}function zc(e,t){let n=Object.create(null);for(let r in e){let i=t&&t[r];if(i===void 0){let t=e[r];if(t.hasDefault)i=t.default;else throw RangeError(`No value supplied for attribute `+r)}n[r]=i}return n}function Bc(e,t,n,r){for(let i in t)if(!(i in e))throw RangeError(`Unsupported attribute ${i} for ${n} of type ${r}`);for(let n in e)e[n].validate&&e[n].validate(t[n])}function Vc(e,t){let n=Object.create(null);if(t)for(let r in t)n[r]=new Wc(e,r,t[r]);return n}var Hc=class e{constructor(e,t,n){this.name=e,this.schema=t,this.spec=n,this.markSet=null,this.groups=n.group?n.group.split(` `):[],this.attrs=Vc(e,n.attrs),this.defaultAttrs=Rc(this.attrs),this.contentMatch=null,this.inlineContent=null,this.isBlock=!(n.inline||e==`text`),this.isText=e==`text`}get isInline(){return!this.isBlock}get isTextblock(){return this.isBlock&&this.inlineContent}get isLeaf(){return this.contentMatch==wc.empty}get isAtom(){return this.isLeaf||!!this.spec.atom}isInGroup(e){return this.groups.indexOf(e)>-1}get whitespace(){return this.spec.whitespace||(this.spec.code?`pre`:`normal`)}hasRequiredAttrs(){for(let e in this.attrs)if(this.attrs[e].isRequired)return!0;return!1}compatibleContent(e){return this==e||this.contentMatch.compatible(e.contentMatch)}computeAttrs(e){return!e&&this.defaultAttrs?this.defaultAttrs:zc(this.attrs,e)}create(e=null,t,n){if(this.isText)throw Error(`NodeType.create can't construct text nodes`);return new xc(this,this.computeAttrs(e),Xs.from(t),ec.setFrom(n))}createChecked(e=null,t,n){return t=Xs.from(t),this.checkContent(t),new xc(this,this.computeAttrs(e),t,ec.setFrom(n))}createAndFill(e=null,t,n){if(e=this.computeAttrs(e),t=Xs.from(t),t.size){let e=this.contentMatch.fillBefore(t);if(!e)return null;t=e.append(t)}let r=this.contentMatch.matchFragment(t),i=r&&r.fillBefore(Xs.empty,!0);return i?new xc(this,e,t.append(i),ec.setFrom(n)):null}validContent(e){let t=this.contentMatch.matchFragment(e);if(!t||!t.validEnd)return!1;for(let t=0;t<e.childCount;t++)if(!this.allowsMarks(e.child(t).marks))return!1;return!0}checkContent(e){if(!this.validContent(e))throw RangeError(`Invalid content for node ${this.name}: ${e.toString().slice(0,50)}`)}checkAttrs(e){Bc(this.attrs,e,`node`,this.name)}allowsMarkType(e){return this.markSet==null||this.markSet.indexOf(e)>-1}allowsMarks(e){if(this.markSet==null)return!0;for(let t=0;t<e.length;t++)if(!this.allowsMarkType(e[t].type))return!1;return!0}allowedMarks(e){if(this.markSet==null)return e;let t;for(let n=0;n<e.length;n++)this.allowsMarkType(e[n].type)?t&&t.push(e[n]):t||=e.slice(0,n);return t?t.length?t:ec.none:e}static compile(t,n){let r=Object.create(null);t.forEach((t,i)=>r[t]=new e(t,n,i));let i=n.spec.topNode||`doc`;if(!r[i])throw RangeError(`Schema is missing its top node type ('`+i+`')`);if(!r.text)throw RangeError(`Every schema needs a 'text' type`);for(let e in r.text.attrs)throw RangeError(`The text node type should not have attributes`);return r}};function Uc(e,t,n){let r=n.split(`|`);return n=>{let i=n===null?`null`:typeof n;if(r.indexOf(i)<0)throw RangeError(`Expected value of type ${r} for attribute ${t} on type ${e}, got ${i}`)}}var Wc=class{constructor(e,t,n){this.hasDefault=Object.prototype.hasOwnProperty.call(n,`default`),this.default=n.default,this.validate=typeof n.validate==`string`?Uc(e,t,n.validate):n.validate}get isRequired(){return!this.hasDefault}},Gc=class e{constructor(e,t,n,r){this.name=e,this.rank=t,this.schema=n,this.spec=r,this.attrs=Vc(e,r.attrs),this.excluded=null;let i=Rc(this.attrs);this.instance=i?new ec(this,i):null}create(e=null){return!e&&this.instance?this.instance:new ec(this,zc(this.attrs,e))}static compile(t,n){let r=Object.create(null),i=0;return t.forEach((t,a)=>r[t]=new e(t,i++,n,a)),r}removeFromSet(e){for(var t=0;t<e.length;t++)e[t].type==this&&(e=e.slice(0,t).concat(e.slice(t+1)),t--);return e}isInSet(e){for(let t=0;t<e.length;t++)if(e[t].type==this)return e[t]}checkAttrs(e){Bc(this.attrs,e,`mark`,this.name)}excludes(e){return this.excluded.indexOf(e)>-1}},Kc=class{constructor(e){this.linebreakReplacement=null,this.cached=Object.create(null);let t=this.spec={};for(let n in e)t[n]=e[n];t.nodes=Gs.from(e.nodes),t.marks=Gs.from(e.marks||{}),this.nodes=Hc.compile(this.spec.nodes,this),this.marks=Gc.compile(this.spec.marks,this);let n=Object.create(null);for(let e in this.nodes){if(e in this.marks)throw RangeError(e+` can not be both a node and a mark`);let t=this.nodes[e],r=t.spec.content||``,i=t.spec.marks;if(t.contentMatch=n[r]||(n[r]=wc.parse(r,this.nodes)),t.inlineContent=t.contentMatch.inlineContent,t.spec.linebreakReplacement){if(this.linebreakReplacement)throw RangeError(`Multiple linebreak nodes defined`);if(!t.isInline||!t.isLeaf)throw RangeError(`Linebreak replacement nodes must be inline leaf nodes`);this.linebreakReplacement=t}t.markSet=i==`_`?null:i?qc(this,i.split(` `)):i==``||!t.inlineContent?[]:null}for(let e in this.marks){let t=this.marks[e],n=t.spec.excludes;t.excluded=n==null?[t]:n==``?[]:qc(this,n.split(` `))}this.nodeFromJSON=e=>xc.fromJSON(this,e),this.markFromJSON=e=>ec.fromJSON(this,e),this.topNodeType=this.nodes[this.spec.topNode||`doc`],this.cached.wrappings=Object.create(null)}node(e,t=null,n,r){if(typeof e==`string`)e=this.nodeType(e);else if(!(e instanceof Hc))throw RangeError(`Invalid node type: `+e);else if(e.schema!=this)throw RangeError(`Node type from different schema used (`+e.name+`)`);return e.createChecked(t,n,r)}text(e,t){let n=this.nodes.text;return new Sc(n,n.defaultAttrs,e,ec.setFrom(t))}mark(e,t){return typeof e==`string`&&(e=this.marks[e]),e.create(t)}nodeType(e){let t=this.nodes[e];if(!t)throw RangeError(`Unknown node type: `+e);return t}};function qc(e,t){let n=[];for(let r=0;r<t.length;r++){let i=t[r],a=e.marks[i],o=a;if(a)n.push(a);else for(let t in e.marks){let r=e.marks[t];(i==`_`||r.spec.group&&r.spec.group.split(` `).indexOf(i)>-1)&&n.push(o=r)}if(!o)throw SyntaxError(`Unknown mark type: '`+t[r]+`'`)}return n}function Jc(e){return e.tag!=null}function Yc(e){return e.style!=null}var Xc=class e{constructor(e,t){this.schema=e,this.rules=t,this.tags=[],this.styles=[];let n=this.matchedStyles=[];t.forEach(e=>{if(Jc(e))this.tags.push(e);else if(Yc(e)){let t=/[^=]*/.exec(e.style)[0];n.indexOf(t)<0&&n.push(t),this.styles.push(e)}}),this.normalizeLists=!this.tags.some(t=>{if(!/^(ul|ol)\b/.test(t.tag)||!t.node)return!1;let n=e.nodes[t.node];return n.contentMatch.matchType(n)})}parse(e,t={}){let n=new al(this,t,!1);return n.addAll(e,ec.none,t.from,t.to),n.finish()}parseSlice(e,t={}){let n=new al(this,t,!0);return n.addAll(e,ec.none,t.from,t.to),nc.maxOpen(n.finish())}matchTag(e,t,n){for(let r=n?this.tags.indexOf(n)+1:0;r<this.tags.length;r++){let n=this.tags[r];if(sl(e,n.tag)&&(n.namespace===void 0||e.namespaceURI==n.namespace)&&(!n.context||t.matchesContext(n.context))){if(n.getAttrs){let t=n.getAttrs(e);if(t===!1)continue;n.attrs=t||void 0}return n}}}matchStyle(e,t,n,r){for(let i=r?this.styles.indexOf(r)+1:0;i<this.styles.length;i++){let r=this.styles[i],a=r.style;if(!(a.indexOf(e)!=0||r.context&&!n.matchesContext(r.context)||a.length>e.length&&(a.charCodeAt(e.length)!=61||a.slice(e.length+1)!=t))){if(r.getAttrs){let e=r.getAttrs(t);if(e===!1)continue;r.attrs=e||void 0}return r}}}static schemaRules(e){let t=[];function n(e){let n=e.priority==null?50:e.priority,r=0;for(;r<t.length;r++){let e=t[r];if((e.priority==null?50:e.priority)<n)break}t.splice(r,0,e)}for(let t in e.marks){let r=e.marks[t].spec.parseDOM;r&&r.forEach(e=>{n(e=cl(e)),e.mark||e.ignore||e.clearMark||(e.mark=t)})}for(let t in e.nodes){let r=e.nodes[t].spec.parseDOM;r&&r.forEach(e=>{n(e=cl(e)),e.node||e.ignore||e.mark||(e.node=t)})}return t}static fromSchema(t){return t.cached.domParser||(t.cached.domParser=new e(t,e.schemaRules(t)))}},Zc={address:!0,article:!0,aside:!0,blockquote:!0,canvas:!0,dd:!0,div:!0,dl:!0,fieldset:!0,figcaption:!0,figure:!0,footer:!0,form:!0,h1:!0,h2:!0,h3:!0,h4:!0,h5:!0,h6:!0,header:!0,hgroup:!0,hr:!0,li:!0,noscript:!0,ol:!0,output:!0,p:!0,pre:!0,section:!0,table:!0,tfoot:!0,ul:!0},Qc={head:!0,noscript:!0,object:!0,script:!0,style:!0,title:!0},$c={ol:!0,ul:!0},el=1,tl=2,nl=4;function rl(e,t,n){return t==null?e&&e.whitespace==`pre`?3:n&-5:(t?el:0)|(t===`full`?tl:0)}var il=class{constructor(e,t,n,r,i,a){this.type=e,this.attrs=t,this.marks=n,this.solid=r,this.options=a,this.content=[],this.activeMarks=ec.none,this.match=i||(a&nl?null:e.contentMatch)}findWrapping(e){if(!this.match){if(!this.type)return[];let t=this.type.contentMatch.fillBefore(Xs.from(e));if(t)this.match=this.type.contentMatch.matchFragment(t);else{let t=this.type.contentMatch,n;return(n=t.findWrapping(e.type))?(this.match=t,n):null}}return this.match.findWrapping(e.type)}finish(e){if(!(this.options&el)){let e=this.content[this.content.length-1],t;if(e&&e.isText&&(t=/[ \t\r\n\u000c]+$/.exec(e.text))){let n=e;e.text.length==t[0].length?this.content.pop():this.content[this.content.length-1]=n.withText(n.text.slice(0,n.text.length-t[0].length))}}let t=Xs.from(this.content);return!e&&this.match&&(t=t.append(this.match.fillBefore(Xs.empty,!0))),this.type?this.type.create(this.attrs,t,this.marks):t}inlineContext(e){return this.type?this.type.inlineContent:this.content.length?this.content[0].isInline:e.parentNode&&!Zc.hasOwnProperty(e.parentNode.nodeName.toLowerCase())}},al=class{constructor(e,t,n){this.parser=e,this.options=t,this.isOpen=n,this.open=0,this.localPreserveWS=!1;let r=t.topNode,i,a=rl(null,t.preserveWhitespace,0)|(n?nl:0);i=r?new il(r.type,r.attrs,ec.none,!0,t.topMatch||r.type.contentMatch,a):n?new il(null,null,ec.none,!0,null,a):new il(e.schema.topNodeType,null,ec.none,!0,null,a),this.nodes=[i],this.find=t.findPositions,this.needsBlock=!1}get top(){return this.nodes[this.open]}addDOM(e,t){e.nodeType==3?this.addTextNode(e,t):e.nodeType==1&&this.addElement(e,t)}addTextNode(e,t){let n=e.nodeValue,r=this.top,i=r.options&tl?`full`:this.localPreserveWS||(r.options&el)>0,{schema:a}=this.parser;if(i===`full`||r.inlineContext(e)||/[^ \t\r\n\u000c]/.test(n)){if(!i){if(n=n.replace(/[ \t\r\n\u000c]+/g,` `),/^[ \t\r\n\u000c]/.test(n)&&this.open==this.nodes.length-1){let t=r.content[r.content.length-1],i=e.previousSibling;(!t||i&&i.nodeName==`BR`||t.isText&&/[ \t\r\n\u000c]$/.test(t.text))&&(n=n.slice(1))}}else if(i===`full`)n=n.replace(/\r\n?/g,`
440
440
  `);else if(a.linebreakReplacement&&/[\r\n]/.test(n)&&this.top.findWrapping(a.linebreakReplacement.create())){let e=n.split(/\r?\n|\r/);for(let n=0;n<e.length;n++)n&&this.insertNode(a.linebreakReplacement.create(),t,!0),e[n]&&this.insertNode(a.text(e[n]),t,!/\S/.test(e[n]));n=``}else n=n.replace(/\r?\n|\r/g,` `);n&&this.insertNode(a.text(n),t,!/\S/.test(n)),this.findInText(e)}else this.findInside(e)}addElement(e,t,n){let r=this.localPreserveWS,i=this.top;(e.tagName==`PRE`||/pre/.test(e.style&&e.style.whiteSpace))&&(this.localPreserveWS=!0);let a=e.nodeName.toLowerCase(),o;$c.hasOwnProperty(a)&&this.parser.normalizeLists&&ol(e);let s=this.options.ruleFromNode&&this.options.ruleFromNode(e)||(o=this.parser.matchTag(e,this,n));out:if(s?s.ignore:Qc.hasOwnProperty(a))this.findInside(e),this.ignoreFallback(e,t);else if(!s||s.skip||s.closeParent){s&&s.closeParent?this.open=Math.max(0,this.open-1):s&&s.skip.nodeType&&(e=s.skip);let n,r=this.needsBlock;if(Zc.hasOwnProperty(a))i.content.length&&i.content[0].isInline&&this.open&&(this.open--,i=this.top),n=!0,i.type||(this.needsBlock=!0);else if(!e.firstChild){this.leafFallback(e,t);break out}let o=s&&s.skip?t:this.readStyles(e,t);o&&this.addAll(e,o),n&&this.sync(i),this.needsBlock=r}else{let n=this.readStyles(e,t);n&&this.addElementByRule(e,s,n,s.consuming===!1?o:void 0)}this.localPreserveWS=r}leafFallback(e,t){e.nodeName==`BR`&&this.top.type&&this.top.type.inlineContent&&this.addTextNode(e.ownerDocument.createTextNode(`
441
441
  `),t)}ignoreFallback(e,t){e.nodeName==`BR`&&(!this.top.type||!this.top.type.inlineContent)&&this.findPlace(this.parser.schema.text(`-`),t,!0)}readStyles(e,t){let n=e.style;if(n&&n.length)for(let e=0;e<this.parser.matchedStyles.length;e++){let r=this.parser.matchedStyles[e],i=n.getPropertyValue(r);if(i)for(let e;;){let n=this.parser.matchStyle(r,i,this,e);if(!n)break;if(n.ignore)return null;if(t=n.clearMark?t.filter(e=>!n.clearMark(e)):t.concat(this.parser.schema.marks[n.mark].create(n.attrs)),n.consuming===!1)e=n;else break}}return t}addElementByRule(e,t,n,r){let i,a;if(t.node)if(a=this.parser.schema.nodes[t.node],a.isLeaf)this.insertNode(a.create(t.attrs),n,e.nodeName==`BR`)||this.leafFallback(e,n);else{let e=this.enter(a,t.attrs||null,n,t.preserveWhitespace);e&&(i=!0,n=e)}else{let e=this.parser.schema.marks[t.mark];n=n.concat(e.create(t.attrs))}let o=this.top;if(a&&a.isLeaf)this.findInside(e);else if(r)this.addElement(e,n,r);else if(t.getContent)this.findInside(e),t.getContent(e,this.parser.schema).forEach(e=>this.insertNode(e,n,!1));else{let r=e;typeof t.contentElement==`string`?r=e.querySelector(t.contentElement):typeof t.contentElement==`function`?r=t.contentElement(e):t.contentElement&&(r=t.contentElement),this.findAround(e,r,!0),this.addAll(r,n),this.findAround(e,r,!1)}i&&this.sync(o)&&this.open--}addAll(e,t,n,r){let i=n||0;for(let a=n?e.childNodes[n]:e.firstChild,o=r==null?null:e.childNodes[r];a!=o;a=a.nextSibling,++i)this.findAtPoint(e,i),this.addDOM(a,t);this.findAtPoint(e,i)}findPlace(e,t,n){let r,i;for(let t=this.open,a=0;t>=0;t--){let o=this.nodes[t],s=o.findWrapping(e);if(s&&(!r||r.length>s.length+a)&&(r=s,i=o,!s.length))break;if(o.solid){if(n)break;a+=2}}if(!r)return null;this.sync(i);for(let e=0;e<r.length;e++)t=this.enterInner(r[e],null,t,!1);return t}insertNode(e,t,n){if(e.isInline&&this.needsBlock&&!this.top.type){let e=this.textblockFromContext();e&&(t=this.enterInner(e,null,t))}let r=this.findPlace(e,t,n);if(r){this.closeExtra();let t=this.top;t.match&&=t.match.matchType(e.type);let n=ec.none;for(let i of r.concat(e.marks))(t.type?t.type.allowsMarkType(i.type):ll(i.type,e.type))&&(n=i.addToSet(n));return t.content.push(e.mark(n)),!0}return!1}enter(e,t,n,r){let i=this.findPlace(e.create(t),n,!1);return i&&=this.enterInner(e,t,n,!0,r),i}enterInner(e,t,n,r=!1,i){this.closeExtra();let a=this.top;a.match=a.match&&a.match.matchType(e);let o=rl(e,i,a.options);a.options&nl&&a.content.length==0&&(o|=nl);let s=ec.none;return n=n.filter(t=>(a.type?a.type.allowsMarkType(t.type):ll(t.type,e))?(s=t.addToSet(s),!1):!0),this.nodes.push(new il(e,t,s,r,null,o)),this.open++,n}closeExtra(e=!1){let t=this.nodes.length-1;if(t>this.open){for(;t>this.open;t--)this.nodes[t-1].content.push(this.nodes[t].finish(e));this.nodes.length=this.open+1}}finish(){return this.open=0,this.closeExtra(this.isOpen),this.nodes[0].finish(!!(this.isOpen||this.options.topOpen))}sync(e){for(let t=this.open;t>=0;t--)if(this.nodes[t]==e)return this.open=t,!0;else this.localPreserveWS&&(this.nodes[t].options|=el);return!1}get currentPos(){this.closeExtra();let e=0;for(let t=this.open;t>=0;t--){let n=this.nodes[t].content;for(let t=n.length-1;t>=0;t--)e+=n[t].nodeSize;t&&e++}return e}findAtPoint(e,t){if(this.find)for(let n=0;n<this.find.length;n++)this.find[n].node==e&&this.find[n].offset==t&&(this.find[n].pos=this.currentPos)}findInside(e){if(this.find)for(let t=0;t<this.find.length;t++)this.find[t].pos==null&&e.nodeType==1&&e.contains(this.find[t].node)&&(this.find[t].pos=this.currentPos)}findAround(e,t,n){if(e!=t&&this.find)for(let r=0;r<this.find.length;r++)this.find[r].pos==null&&e.nodeType==1&&e.contains(this.find[r].node)&&t.compareDocumentPosition(this.find[r].node)&(n?2:4)&&(this.find[r].pos=this.currentPos)}findInText(e){if(this.find)for(let t=0;t<this.find.length;t++)this.find[t].node==e&&(this.find[t].pos=this.currentPos-(e.nodeValue.length-this.find[t].offset))}matchesContext(e){if(e.indexOf(`|`)>-1)return e.split(/\s*\|\s*/).some(this.matchesContext,this);let t=e.split(`/`),n=this.options.context,r=!this.isOpen&&(!n||n.parent.type==this.nodes[0].type),i=-(n?n.depth+1:0)+ +!r,a=(e,o)=>{for(;e>=0;e--){let s=t[e];if(s==``){if(e==t.length-1||e==0)continue;for(;o>=i;o--)if(a(e-1,o))return!0;return!1}else{let e=o>0||o==0&&r?this.nodes[o].type:n&&o>=i?n.node(o-i).type:null;if(!e||e.name!=s&&!e.isInGroup(s))return!1;o--}}return!0};return a(t.length-1,this.open)}textblockFromContext(){let e=this.options.context;if(e)for(let t=e.depth;t>=0;t--){let n=e.node(t).contentMatchAt(e.indexAfter(t)).defaultType;if(n&&n.isTextblock&&n.defaultAttrs)return n}for(let e in this.parser.schema.nodes){let t=this.parser.schema.nodes[e];if(t.isTextblock&&t.defaultAttrs)return t}}};function ol(e){for(let t=e.firstChild,n=null;t;t=t.nextSibling){let e=t.nodeType==1?t.nodeName.toLowerCase():null;e&&$c.hasOwnProperty(e)&&n?(n.appendChild(t),t=n):e==`li`?n=t:e&&(n=null)}}function sl(e,t){return(e.matches||e.msMatchesSelector||e.webkitMatchesSelector||e.mozMatchesSelector).call(e,t)}function cl(e){let t={};for(let n in e)t[n]=e[n];return t}function ll(e,t){let n=t.schema.nodes;for(let r in n){let i=n[r];if(!i.allowsMarkType(e))continue;let a=[],o=e=>{a.push(e);for(let n=0;n<e.edgeCount;n++){let{type:r,next:i}=e.edge(n);if(r==t||a.indexOf(i)<0&&o(i))return!0}};if(o(i.contentMatch))return!0}}var ul=class e{constructor(e,t){this.nodes=e,this.marks=t}serializeFragment(e,t={},n){n||=fl(t).createDocumentFragment();let r=n,i=[];return e.forEach(e=>{if(i.length||e.marks.length){let n=0,a=0;for(;n<i.length&&a<e.marks.length;){let t=e.marks[a];if(!this.marks[t.type.name]){a++;continue}if(!t.eq(i[n][0])||t.type.spec.spanning===!1)break;n++,a++}for(;n<i.length;)r=i.pop()[1];for(;a<e.marks.length;){let n=e.marks[a++],o=this.serializeMark(n,e.isInline,t);o&&(i.push([n,r]),r.appendChild(o.dom),r=o.contentDOM||o.dom)}}r.appendChild(this.serializeNodeInner(e,t))}),n}serializeNodeInner(e,t){if(e.isText)return fl(t).createTextNode(e.text);let{dom:n,contentDOM:r}=gl(fl(t),this.nodes[e.type.name](e),null,e.attrs);if(r){if(e.isLeaf)throw RangeError(`Content hole not allowed in a leaf node spec`);this.serializeFragment(e.content,t,r)}return n}serializeNode(e,t={}){let n=this.serializeNodeInner(e,t);for(let r=e.marks.length-1;r>=0;r--){let i=this.serializeMark(e.marks[r],e.isInline,t);i&&((i.contentDOM||i.dom).appendChild(n),n=i.dom)}return n}serializeMark(e,t,n={}){let r=this.marks[e.type.name];return r&&gl(fl(n),r(e,t),null,e.attrs)}static renderSpec(e,t,n=null,r){return typeof t==`string`?{dom:e.createTextNode(t)}:gl(e,t,n,r)}static fromSchema(t){return t.cached.domSerializer||(t.cached.domSerializer=new e(this.nodesFromSchema(t),this.marksFromSchema(t)))}static nodesFromSchema(e){let t=dl(e.nodes);return t.text||=e=>e.text,t}static marksFromSchema(e){return dl(e.marks)}};function dl(e){let t={};for(let n in e){let r=e[n].spec.toDOM;r&&(t[n]=r)}return t}function fl(e){return e.document||window.document}var pl=new WeakMap;function ml(e){let t=pl.get(e);return t===void 0&&pl.set(e,t=hl(e)),t}function hl(e){let t=null;function n(e){if(e&&typeof e==`object`)if(Array.isArray(e))if(typeof e[0]==`string`)t||=[],t.push(e);else for(let t=0;t<e.length;t++)n(e[t]);else for(let t in e)n(e[t])}return n(e),t}function gl(e,t,n,r){if(t.nodeType==1)return{dom:t};if(t.dom&&t.dom.nodeType==1)return t;let i=t[0],a;if(typeof i!=`string`)throw RangeError(`Invalid array passed to renderSpec`);if(r&&(a=ml(r))&&a.indexOf(t)>-1)throw RangeError(`Using an array from an attribute object as a DOM spec. This may be an attempted cross site scripting attack.`);let o=i.indexOf(` `);o>0&&(n=i.slice(0,o),i=i.slice(o+1));let s,c=n?e.createElementNS(n,i):e.createElement(i),l=t[1],u=1;if(l&&typeof l==`object`&&l.nodeType==null&&!Array.isArray(l)){u=2;for(let e in l)if(l[e]!=null){let t=e.indexOf(` `);t>0?c.setAttributeNS(e.slice(0,t),e.slice(t+1),l[e]):e==`style`&&c.style?c.style.cssText=l[e]:c.setAttribute(e,l[e])}}for(let i=u;i<t.length;i++){let a=t[i];if(a===0){if(i<t.length-1||i>u)throw RangeError(`Content hole must be the only child of its parent node`);return{dom:c,contentDOM:c}}else if(typeof a==`string`)c.appendChild(e.createTextNode(a));else{let{dom:t,contentDOM:i}=gl(e,a,n,r);if(c.appendChild(t),i){if(s)throw RangeError(`Multiple content holes`);s=i}}}return{dom:c,contentDOM:s}}var _l=65535,vl=2**16;function yl(e,t){return e+t*vl}function bl(e){return e&_l}function xl(e){return(e-(e&_l))/vl}var Sl=1,Cl=2,wl=4,Tl=8,El=class{constructor(e,t,n){this.pos=e,this.delInfo=t,this.recover=n}get deleted(){return(this.delInfo&Tl)>0}get deletedBefore(){return(this.delInfo&5)>0}get deletedAfter(){return(this.delInfo&6)>0}get deletedAcross(){return(this.delInfo&wl)>0}},Dl=class e{constructor(t,n=!1){if(this.ranges=t,this.inverted=n,!t.length&&e.empty)return e.empty}recover(e){let t=0,n=bl(e);if(!this.inverted)for(let e=0;e<n;e++)t+=this.ranges[e*3+2]-this.ranges[e*3+1];return this.ranges[n*3]+t+xl(e)}mapResult(e,t=1){return this._map(e,t,!1)}map(e,t=1){return this._map(e,t,!0)}_map(e,t,n){let r=0,i=this.inverted?2:1,a=this.inverted?1:2;for(let o=0;o<this.ranges.length;o+=3){let s=this.ranges[o]-(this.inverted?r:0);if(s>e)break;let c=this.ranges[o+i],l=this.ranges[o+a],u=s+c;if(e<=u){let i=c?e==s?-1:e==u?1:t:t,a=s+r+(i<0?0:l);if(n)return a;let d=e==(t<0?s:u)?null:yl(o/3,e-s),f=e==s?Cl:e==u?Sl:wl;return(t<0?e!=s:e!=u)&&(f|=Tl),new El(a,f,d)}r+=l-c}return n?e+r:new El(e+r,0,null)}touches(e,t){let n=0,r=bl(t),i=this.inverted?2:1,a=this.inverted?1:2;for(let t=0;t<this.ranges.length;t+=3){let o=this.ranges[t]-(this.inverted?n:0);if(o>e)break;let s=this.ranges[t+i];if(e<=o+s&&t==r*3)return!0;n+=this.ranges[t+a]-s}return!1}forEach(e){let t=this.inverted?2:1,n=this.inverted?1:2;for(let r=0,i=0;r<this.ranges.length;r+=3){let a=this.ranges[r],o=a-(this.inverted?i:0),s=a+(this.inverted?0:i),c=this.ranges[r+t],l=this.ranges[r+n];e(o,o+c,s,s+l),i+=l-c}}invert(){return new e(this.ranges,!this.inverted)}toString(){return(this.inverted?`-`:``)+JSON.stringify(this.ranges)}static offset(t){return t==0?e.empty:new e(t<0?[0,-t,0]:[0,0,t])}};Dl.empty=new Dl([]);var Ol=class e{constructor(e,t,n=0,r=e?e.length:0){this.mirror=t,this.from=n,this.to=r,this._maps=e||[],this.ownData=!(e||t)}get maps(){return this._maps}slice(t=0,n=this.maps.length){return new e(this._maps,this.mirror,t,n)}appendMap(e,t){this.ownData||=(this._maps=this._maps.slice(),this.mirror=this.mirror&&this.mirror.slice(),!0),this.to=this._maps.push(e),t!=null&&this.setMirror(this._maps.length-1,t)}appendMapping(e){for(let t=0,n=this._maps.length;t<e._maps.length;t++){let r=e.getMirror(t);this.appendMap(e._maps[t],r!=null&&r<t?n+r:void 0)}}getMirror(e){if(this.mirror){for(let t=0;t<this.mirror.length;t++)if(this.mirror[t]==e)return this.mirror[t+(t%2?-1:1)]}}setMirror(e,t){this.mirror||=[],this.mirror.push(e,t)}appendMappingInverted(e){for(let t=e.maps.length-1,n=this._maps.length+e._maps.length;t>=0;t--){let r=e.getMirror(t);this.appendMap(e._maps[t].invert(),r!=null&&r>t?n-r-1:void 0)}}invert(){let t=new e;return t.appendMappingInverted(this),t}map(e,t=1){if(this.mirror)return this._map(e,t,!0);for(let n=this.from;n<this.to;n++)e=this._maps[n].map(e,t);return e}mapResult(e,t=1){return this._map(e,t,!1)}_map(e,t,n){let r=0;for(let n=this.from;n<this.to;n++){let i=this._maps[n].mapResult(e,t);if(i.recover!=null){let t=this.getMirror(n);if(t!=null&&t>n&&t<this.to){n=t,e=this._maps[t].recover(i.recover);continue}}r|=i.delInfo,e=i.pos}return n?e:new El(e,r,null)}},kl=Object.create(null),Al=class{getMap(){return Dl.empty}merge(e){return null}static fromJSON(e,t){if(!t||!t.stepType)throw RangeError(`Invalid input for Step.fromJSON`);let n=kl[t.stepType];if(!n)throw RangeError(`No step type ${t.stepType} defined`);return n.fromJSON(e,t)}static jsonID(e,t){if(e in kl)throw RangeError(`Duplicate use of step JSON ID `+e);return kl[e]=t,t.prototype.jsonID=e,t}},jl=class e{constructor(e,t){this.doc=e,this.failed=t}static ok(t){return new e(t,null)}static fail(t){return new e(null,t)}static fromReplace(t,n,r,i){try{return e.ok(t.replace(n,r,i))}catch(t){if(t instanceof tc)return e.fail(t.message);throw t}}};function Ml(e,t,n){let r=[];for(let i=0;i<e.childCount;i++){let a=e.child(i);a.content.size&&(a=a.copy(Ml(a.content,t,a))),a.isInline&&(a=t(a,n,i)),r.push(a)}return Xs.fromArray(r)}var Nl=class e extends Al{constructor(e,t,n){super(),this.from=e,this.to=t,this.mark=n}apply(e){let t=e.slice(this.from,this.to),n=e.resolve(this.from),r=n.node(n.sharedDepth(this.to)),i=new nc(Ml(t.content,(e,t)=>!e.isAtom||!t.type.allowsMarkType(this.mark.type)?e:e.mark(this.mark.addToSet(e.marks)),r),t.openStart,t.openEnd);return jl.fromReplace(e,this.from,this.to,i)}invert(){return new Pl(this.from,this.to,this.mark)}map(t){let n=t.mapResult(this.from,1),r=t.mapResult(this.to,-1);return n.deleted&&r.deleted||n.pos>=r.pos?null:new e(n.pos,r.pos,this.mark)}merge(t){return t instanceof e&&t.mark.eq(this.mark)&&this.from<=t.to&&this.to>=t.from?new e(Math.min(this.from,t.from),Math.max(this.to,t.to),this.mark):null}toJSON(){return{stepType:`addMark`,mark:this.mark.toJSON(),from:this.from,to:this.to}}static fromJSON(t,n){if(typeof n.from!=`number`||typeof n.to!=`number`)throw RangeError(`Invalid input for AddMarkStep.fromJSON`);return new e(n.from,n.to,t.markFromJSON(n.mark))}};Al.jsonID(`addMark`,Nl);var Pl=class e extends Al{constructor(e,t,n){super(),this.from=e,this.to=t,this.mark=n}apply(e){let t=e.slice(this.from,this.to),n=new nc(Ml(t.content,e=>e.mark(this.mark.removeFromSet(e.marks)),e),t.openStart,t.openEnd);return jl.fromReplace(e,this.from,this.to,n)}invert(){return new Nl(this.from,this.to,this.mark)}map(t){let n=t.mapResult(this.from,1),r=t.mapResult(this.to,-1);return n.deleted&&r.deleted||n.pos>=r.pos?null:new e(n.pos,r.pos,this.mark)}merge(t){return t instanceof e&&t.mark.eq(this.mark)&&this.from<=t.to&&this.to>=t.from?new e(Math.min(this.from,t.from),Math.max(this.to,t.to),this.mark):null}toJSON(){return{stepType:`removeMark`,mark:this.mark.toJSON(),from:this.from,to:this.to}}static fromJSON(t,n){if(typeof n.from!=`number`||typeof n.to!=`number`)throw RangeError(`Invalid input for RemoveMarkStep.fromJSON`);return new e(n.from,n.to,t.markFromJSON(n.mark))}};Al.jsonID(`removeMark`,Pl);var Fl=class e extends Al{constructor(e,t){super(),this.pos=e,this.mark=t}apply(e){let t=e.nodeAt(this.pos);if(!t)return jl.fail(`No node at mark step's position`);let n=t.type.create(t.attrs,null,this.mark.addToSet(t.marks));return jl.fromReplace(e,this.pos,this.pos+1,new nc(Xs.from(n),0,+!t.isLeaf))}invert(t){let n=t.nodeAt(this.pos);if(n){let t=this.mark.addToSet(n.marks);if(t.length==n.marks.length){for(let r=0;r<n.marks.length;r++)if(!n.marks[r].isInSet(t))return new e(this.pos,n.marks[r]);return new e(this.pos,this.mark)}}return new Il(this.pos,this.mark)}map(t){let n=t.mapResult(this.pos,1);return n.deletedAfter?null:new e(n.pos,this.mark)}toJSON(){return{stepType:`addNodeMark`,pos:this.pos,mark:this.mark.toJSON()}}static fromJSON(t,n){if(typeof n.pos!=`number`)throw RangeError(`Invalid input for AddNodeMarkStep.fromJSON`);return new e(n.pos,t.markFromJSON(n.mark))}};Al.jsonID(`addNodeMark`,Fl);var Il=class e extends Al{constructor(e,t){super(),this.pos=e,this.mark=t}apply(e){let t=e.nodeAt(this.pos);if(!t)return jl.fail(`No node at mark step's position`);let n=t.type.create(t.attrs,null,this.mark.removeFromSet(t.marks));return jl.fromReplace(e,this.pos,this.pos+1,new nc(Xs.from(n),0,+!t.isLeaf))}invert(e){let t=e.nodeAt(this.pos);return!t||!this.mark.isInSet(t.marks)?this:new Fl(this.pos,this.mark)}map(t){let n=t.mapResult(this.pos,1);return n.deletedAfter?null:new e(n.pos,this.mark)}toJSON(){return{stepType:`removeNodeMark`,pos:this.pos,mark:this.mark.toJSON()}}static fromJSON(t,n){if(typeof n.pos!=`number`)throw RangeError(`Invalid input for RemoveNodeMarkStep.fromJSON`);return new e(n.pos,t.markFromJSON(n.mark))}};Al.jsonID(`removeNodeMark`,Il);var Ll=class e extends Al{constructor(e,t,n,r=!1){super(),this.from=e,this.to=t,this.slice=n,this.structure=r}apply(e){return this.structure&&zl(e,this.from,this.to)?jl.fail(`Structure replace would overwrite content`):jl.fromReplace(e,this.from,this.to,this.slice)}getMap(){return new Dl([this.from,this.to-this.from,this.slice.size])}invert(t){return new e(this.from,this.from+this.slice.size,t.slice(this.from,this.to))}map(t){let n=t.mapResult(this.to,-1),r=this.from==this.to&&e.MAP_BIAS<0?n:t.mapResult(this.from,1);return r.deletedAcross&&n.deletedAcross?null:new e(r.pos,Math.max(r.pos,n.pos),this.slice,this.structure)}merge(t){if(!(t instanceof e)||t.structure||this.structure)return null;if(this.from+this.slice.size==t.from&&!this.slice.openEnd&&!t.slice.openStart){let n=this.slice.size+t.slice.size==0?nc.empty:new nc(this.slice.content.append(t.slice.content),this.slice.openStart,t.slice.openEnd);return new e(this.from,this.to+(t.to-t.from),n,this.structure)}else if(t.to==this.from&&!this.slice.openStart&&!t.slice.openEnd){let n=this.slice.size+t.slice.size==0?nc.empty:new nc(t.slice.content.append(this.slice.content),t.slice.openStart,this.slice.openEnd);return new e(t.from,this.to,n,this.structure)}else return null}toJSON(){let e={stepType:`replace`,from:this.from,to:this.to};return this.slice.size&&(e.slice=this.slice.toJSON()),this.structure&&(e.structure=!0),e}static fromJSON(t,n){if(typeof n.from!=`number`||typeof n.to!=`number`)throw RangeError(`Invalid input for ReplaceStep.fromJSON`);return new e(n.from,n.to,nc.fromJSON(t,n.slice),!!n.structure)}};Ll.MAP_BIAS=1,Al.jsonID(`replace`,Ll);var Rl=class e extends Al{constructor(e,t,n,r,i,a,o=!1){super(),this.from=e,this.to=t,this.gapFrom=n,this.gapTo=r,this.slice=i,this.insert=a,this.structure=o}apply(e){if(this.structure&&(zl(e,this.from,this.gapFrom)||zl(e,this.gapTo,this.to)))return jl.fail(`Structure gap-replace would overwrite content`);let t=e.slice(this.gapFrom,this.gapTo);if(t.openStart||t.openEnd)return jl.fail(`Gap is not a flat range`);let n=this.slice.insertAt(this.insert,t.content);return n?jl.fromReplace(e,this.from,this.to,n):jl.fail(`Content does not fit in gap`)}getMap(){return new Dl([this.from,this.gapFrom-this.from,this.insert,this.gapTo,this.to-this.gapTo,this.slice.size-this.insert])}invert(t){let n=this.gapTo-this.gapFrom;return new e(this.from,this.from+this.slice.size+n,this.from+this.insert,this.from+this.insert+n,t.slice(this.from,this.to).removeBetween(this.gapFrom-this.from,this.gapTo-this.from),this.gapFrom-this.from,this.structure)}map(t){let n=t.mapResult(this.from,1),r=t.mapResult(this.to,-1),i=this.from==this.gapFrom?n.pos:t.map(this.gapFrom,-1),a=this.to==this.gapTo?r.pos:t.map(this.gapTo,1);return n.deletedAcross&&r.deletedAcross||i<n.pos||a>r.pos?null:new e(n.pos,r.pos,i,a,this.slice,this.insert,this.structure)}toJSON(){let e={stepType:`replaceAround`,from:this.from,to:this.to,gapFrom:this.gapFrom,gapTo:this.gapTo,insert:this.insert};return this.slice.size&&(e.slice=this.slice.toJSON()),this.structure&&(e.structure=!0),e}static fromJSON(t,n){if(typeof n.from!=`number`||typeof n.to!=`number`||typeof n.gapFrom!=`number`||typeof n.gapTo!=`number`||typeof n.insert!=`number`)throw RangeError(`Invalid input for ReplaceAroundStep.fromJSON`);return new e(n.from,n.to,n.gapFrom,n.gapTo,nc.fromJSON(t,n.slice),n.insert,!!n.structure)}};Al.jsonID(`replaceAround`,Rl);function zl(e,t,n){let r=e.resolve(t),i=n-t,a=r.depth;for(;i>0&&a>0&&r.indexAfter(a)==r.node(a).childCount;)a--,i--;if(i>0){let e=r.node(a).maybeChild(r.indexAfter(a));for(;i>0;){if(!e||e.isLeaf)return!0;e=e.firstChild,i--}}return!1}function Bl(e,t,n,r){let i=[],a=[],o,s;e.doc.nodesBetween(t,n,(e,c,l)=>{if(!e.isInline)return;let u=e.marks;if(!r.isInSet(u)&&l.type.allowsMarkType(r.type)){let l=Math.max(c,t),d=Math.min(c+e.nodeSize,n),f=r.addToSet(u);for(let e=0;e<u.length;e++)u[e].isInSet(f)||(o&&o.to==l&&o.mark.eq(u[e])?o.to=d:i.push(o=new Pl(l,d,u[e])));s&&s.to==l?s.to=d:a.push(s=new Nl(l,d,r))}}),i.forEach(t=>e.step(t)),a.forEach(t=>e.step(t))}function Vl(e,t,n,r){let i=[],a=0;e.doc.nodesBetween(t,n,(e,o)=>{if(!e.isInline)return;a++;let s=null;if(r instanceof Gc){let t=e.marks,n;for(;n=r.isInSet(t);)(s||=[]).push(n),t=n.removeFromSet(t)}else r?r.isInSet(e.marks)&&(s=[r]):s=e.marks;if(s&&s.length){let r=Math.min(o+e.nodeSize,n);for(let e=0;e<s.length;e++){let n=s[e],c;for(let e=0;e<i.length;e++){let t=i[e];t.step==a-1&&n.eq(i[e].style)&&(c=t)}c?(c.to=r,c.step=a):i.push({style:n,from:Math.max(o,t),to:r,step:a})}}}),i.forEach(t=>e.step(new Pl(t.from,t.to,t.style)))}function Hl(e,t,n,r=n.contentMatch,i=!0){let a=e.doc.nodeAt(t),o=[],s=t+1;for(let t=0;t<a.childCount;t++){let c=a.child(t),l=s+c.nodeSize,u=r.matchType(c.type);if(!u)o.push(new Ll(s,l,nc.empty));else{r=u;for(let t=0;t<c.marks.length;t++)n.allowsMarkType(c.marks[t].type)||e.step(new Pl(s,l,c.marks[t]));if(i&&c.isText&&n.whitespace!=`pre`){let e,t=/\r?\n|\r/g,r;for(;e=t.exec(c.text);)r||=new nc(Xs.from(n.schema.text(` `,n.allowedMarks(c.marks))),0,0),o.push(new Ll(s+e.index,s+e.index+e[0].length,r))}}s=l}if(!r.validEnd){let t=r.fillBefore(Xs.empty,!0);e.replace(s,s,new nc(t,0,0))}for(let t=o.length-1;t>=0;t--)e.step(o[t])}function Ul(e,t,n){return(t==0||e.canReplace(t,e.childCount))&&(n==e.childCount||e.canReplace(0,n))}function Wl(e){let t=e.parent.content.cutByIndex(e.startIndex,e.endIndex);for(let n=e.depth,r=0,i=0;;--n){let a=e.$from.node(n),o=e.$from.index(n)+r,s=e.$to.indexAfter(n)-i;if(n<e.depth&&a.canReplace(o,s,t))return n;if(n==0||a.type.spec.isolating||!Ul(a,o,s))break;o&&(r=1),s<a.childCount&&(i=1)}return null}function Gl(e,t,n){let{$from:r,$to:i,depth:a}=t,o=r.before(a+1),s=i.after(a+1),c=o,l=s,u=Xs.empty,d=0;for(let e=a,t=!1;e>n;e--)t||r.index(e)>0?(t=!0,u=Xs.from(r.node(e).copy(u)),d++):c--;let f=Xs.empty,p=0;for(let e=a,t=!1;e>n;e--)t||i.after(e+1)<i.end(e)?(t=!0,f=Xs.from(i.node(e).copy(f)),p++):l++;e.step(new Rl(c,l,o,s,new nc(u.append(f),d,p),u.size-d,!0))}function Kl(e,t,n=null,r=e){let i=Jl(e,t),a=i&&Yl(r,t);return a?i.map(ql).concat({type:t,attrs:n}).concat(a.map(ql)):null}function ql(e){return{type:e,attrs:null}}function Jl(e,t){let{parent:n,startIndex:r,endIndex:i}=e,a=n.contentMatchAt(r).findWrapping(t);if(!a)return null;let o=a.length?a[0]:t;return n.canReplaceWith(r,i,o)?a:null}function Yl(e,t){let{parent:n,startIndex:r,endIndex:i}=e,a=n.child(r),o=t.contentMatch.findWrapping(a.type);if(!o)return null;let s=(o.length?o[o.length-1]:t).contentMatch;for(let e=r;s&&e<i;e++)s=s.matchType(n.child(e).type);return!s||!s.validEnd?null:o}function Xl(e,t,n){let r=Xs.empty;for(let e=n.length-1;e>=0;e--){if(r.size){let t=n[e].type.contentMatch.matchFragment(r);if(!t||!t.validEnd)throw RangeError(`Wrapper type given to Transform.wrap does not form valid content of its parent wrapper`)}r=Xs.from(n[e].type.create(n[e].attrs,r))}let i=t.start,a=t.end;e.step(new Rl(i,a,i,a,new nc(r,0,0),n.length,!0))}function Zl(e,t,n,r,i){if(!r.isTextblock)throw RangeError(`Type given to setBlockType should be a textblock`);let a=e.steps.length;e.doc.nodesBetween(t,n,(t,n)=>{let o=typeof i==`function`?i(t):i;if(t.isTextblock&&!t.hasMarkup(r,o)&&eu(e.doc,e.mapping.slice(a).map(n),r)){let i=null;if(r.schema.linebreakReplacement){let e=r.whitespace==`pre`,t=!!r.contentMatch.matchType(r.schema.linebreakReplacement);e&&!t?i=!1:!e&&t&&(i=!0)}i===!1&&$l(e,t,n,a),Hl(e,e.mapping.slice(a).map(n,1),r,void 0,i===null);let s=e.mapping.slice(a),c=s.map(n,1),l=s.map(n+t.nodeSize,1);return e.step(new Rl(c,l,c+1,l-1,new nc(Xs.from(r.create(o,null,t.marks)),0,0),1,!0)),i===!0&&Ql(e,t,n,a),!1}})}function Ql(e,t,n,r){t.forEach((i,a)=>{if(i.isText){let o,s=/\r?\n|\r/g;for(;o=s.exec(i.text);){let i=e.mapping.slice(r).map(n+1+a+o.index);e.replaceWith(i,i+1,t.type.schema.linebreakReplacement.create())}}})}function $l(e,t,n,r){t.forEach((i,a)=>{if(i.type==i.type.schema.linebreakReplacement){let i=e.mapping.slice(r).map(n+1+a);e.replaceWith(i,i+1,t.type.schema.text(`
@@ -1074,7 +1074,7 @@ Expected it to be ${r}.`}var lW=class extends Error{name=`InvalidDocError`;const
1074
1074
  `),this.column=r,this.excerpt=i,this.line=a,this.name=`XmlError`,this.pos=t}},j=class extends w{constructor(e,t=``){super(),this.name=e,this.content=t}get type(){return w.TYPE_PROCESSING_INSTRUCTION}toJSON(){return Object.assign(w.prototype.toJSON.call(this),{name:this.name,content:this.content})}},M=``,ne=class{constructor(e,t={}){let n=this.document=new A;this.j=n,this.g=t,this.c=new f(e),this.g.includeOffsets&&(n.start=0,n.end=e.length),this.parse()}i(e,t){return e.parent=this.j,this.g.includeOffsets&&(e.start=this.c.f(t),e.end=this.c.f()),this.j.children.push(e),!0}y(e,t){let{children:n}=this.j,{length:r}=n;if(e=N(e),r>0){let t=n[r-1];if(t?.type===w.TYPE_TEXT){let n=t;return n.text+=e,this.g.includeOffsets&&(n.end=this.c.f()),!0}}return this.i(new T(e),t)}H(){let e=Object.create(null);for(;this.e();){let t=this.q();if(!t)break;let n=this.t()&&this.I();if(n===!1)throw this.a(`Attribute value expected`);if(t in e)throw this.a(`Duplicate attribute: ${t}`);if(t===`xml:space`&&n!=="default"&&n!==`preserve`)throw this.a('Value of the `xml:space` attribute must be "default" or "preserve"');e[t]=n}if(this.g.sortAttributes){let t=Object.keys(e).sort(),n=Object.create(null);for(let r=0;r<t.length;++r){let i=t[r];n[i]=e[i]}e=n}return e}I(){let{c:e}=this,t=e.m();if(t!==`"`&&t!==`'`)return!1;e.p();let n,r=!1,i=M,a=t===`"`?p:m;matchLoop:for(;!e.B;)switch(n=e.x(a),n&&(this.o(n),i+=n.replace(h,` `)),e.m()){case t:r=!0;break matchLoop;case`&`:i+=this.C();continue;case`<`:throw this.a("Unescaped `<` is not allowed in an attribute value");default:break matchLoop}if(!r)throw this.a(`Unclosed attribute`);return e.p(),i}J(){let{c:e}=this,t=e.d;if(!e.b(`<![CDATA[`))return!1;let n=e.s(`]]>`);if(this.o(n),!e.b(`]]>`))throw this.a(`Unclosed CDATA section`);return this.g.preserveCdata?this.i(new E(N(n)),t):this.y(n,t)}K(){let{c:e}=this,t=e.d,n=e.x(g);if(!n)return!1;if(this.o(n),e.m(3)===`]]>`)throw this.a("Element content may not contain the CDATA section close delimiter `]]>`");return this.y(n,t)}D(){let{c:e}=this,t=e.d;if(!e.b(`<!--`))return!1;let n=e.s(`--`);if(this.o(n),!e.b(`-->`))throw e.m(2)===`--`?this.a("The string `--` isn't allowed inside a comment"):this.a(`Unclosed comment`);return this.g.preserveComments?this.i(new D(N(n)),t):!0}L(){let e=this.c.d,t=this.C();return t?this.y(t,e):!1}M(){let{c:e}=this,t=e.d;if(!e.b(`<!DOCTYPE`))return!1;let n=this.e()&&this.q();if(!n)throw this.a(`Expected a name`);let r,i;if(this.e()){if(e.b(`PUBLIC`)){if(r=this.e()&&this.N(),r===!1)throw this.a(`Expected a public identifier`);this.e()}if(r!==void 0||e.b(`SYSTEM`)){if(this.e(),i=this.r(),i===!1)throw this.a(`Expected a system identifier`);this.e()}}let a;if(e.b(`[`)){if(a=e.x(/\][\x20\t\r\n]*>/),!e.b(`]`))throw this.a(`Unclosed internal subset`);this.e()}if(!e.b(`>`))throw this.a(`Unclosed doctype declaration`);return this.g.preserveDocumentType?this.i(new ee(n,r,i,a),t):!0}E(){let{c:e}=this,t=e.d;if(!e.b(`<`))return!1;let n=this.q();if(!n)return e.n(t),!1;let r=this.H(),i=!!e.b(`/>`),a=new k(n,r);if(a.parent=this.j,!i){if(!e.b(`>`))throw this.a(`Unclosed start tag for element \`${n}\``);this.j=a;do this.K();while(this.E()||this.L()||this.J()||this.F()||this.D());let t=e.d,r;if(!e.b(`</`)||!(r=this.q())||r!==n)throw e.n(t),this.a(`Missing end tag for element ${n}`);if(this.e(),!e.b(`>`))throw this.a(`Unclosed end tag for element ${n}`);this.j=a.parent}return this.i(a,t)}t(){return this.e(),this.c.b(`=`)?(this.e(),!0):!1}z(){return this.D()||this.F()||this.e()}q(){return y(this.c.m())?this.c.w(v):M}F(){let{c:e}=this,t=e.d;if(!e.b(`<?`))return!1;let n=this.q();if(n){if(n.toLowerCase()===`xml`)throw e.n(t),this.a(`XML declaration isn't allowed here`)}else throw this.a(`Invalid processing instruction`);if(!this.e()){if(e.b(`?>`))return this.i(new j(n),t);throw this.a(`Whitespace is required after a processing instruction name`)}let r=e.s(`?>`);if(this.o(r),!e.b(`?>`))throw this.a(`Unterminated processing instruction`);return this.i(new j(n,N(r)),t)}O(){let{c:e}=this,t=e.d;for(this.P();this.z(););if(this.M())for(;this.z(););return t<e.d}N(){let e=this.c.d,t=this.r();if(t!==!1&&!/^[-\x20\r\na-zA-Z0-9'()+,./:=?;!*#@$_%]*$/.test(t))throw this.c.n(e),this.a(`Invalid character in public identifier`);return t}C(){let{c:e}=this;if(!e.b(`&`))return!1;let t=e.w(b);if(e.G()!==`;`)throw this.a("Unterminated reference (a reference must end with `;`)");let n;if(t[0]===`#`){let e=t[1]===`x`?parseInt(t.slice(2),16):parseInt(t.slice(1),10);if(isNaN(e))throw this.a(`Invalid character reference`);if(!S(e))throw this.a(`Character reference resolves to an invalid character`);n=String.fromCodePoint(e)}else if(n=_[t],n===void 0){let{ignoreUndefinedEntities:n,resolveUndefinedEntity:r}=this.g,i=`&${t};`;if(r){let e=r(i);if(e!=null){let t=typeof e;if(t!==`string`)throw TypeError(`\`resolveUndefinedEntity()\` must return a string, \`null\`, or \`undefined\`, but returned a value of type ${t}`);return e}}if(n)return i;throw e.n(-i.length),this.a(`Named entity isn't defined: ${i}`)}return n}r(){let{c:e}=this,t=e.b(`"`)||e.b(`'`);if(!t)return!1;let n=e.s(t);if(this.o(n),!e.b(t))throw this.a(`Missing end quote`);return n}e(){return!!this.c.w(x)}P(){let{c:e}=this,t=e.d;if(!e.b(`<?xml`))return!1;if(!this.e())throw this.a(`Invalid XML declaration`);let n=!!e.b(`version`)&&this.t()&&this.r();if(n===!1)throw this.a(`XML version is missing or invalid`);if(!/^1\.[0-9]+$/.test(n))throw this.a(`Invalid character in version number`);let r,i;if(this.e()){if(r=!!e.b(`encoding`)&&this.t()&&this.r(),r){if(!/^[A-Za-z][\w.-]*$/.test(r))throw this.a(`Invalid character in encoding name`);this.e()}if(i=!!e.b(`standalone`)&&this.t()&&this.r(),i){if(i!==`yes`&&i!==`no`)throw this.a('Only "yes" and "no" are permitted as values of `standalone`');this.e()}}if(!e.b(`?>`))throw this.a(`Invalid or unclosed XML declaration`);return this.g.preserveXmlDeclaration?this.i(new O(n,r||void 0,i||void 0),t):!0}a(e){let{c:t}=this;return new te(e,t.d,t.h)}parse(){if(this.c.b(``),this.O(),!this.E())throw this.a(`Root element is missing or invalid`);for(;this.z(););if(!this.c.B)throw this.a(`Extra content at the end of the document`)}o(e){let{length:t}=e;for(let n=0;n<t;++n){let t=e.codePointAt(n);if(!S(t))throw this.c.n(-([...e].length-n)),this.a(`Invalid character`);t>65535&&(n+=1)}}};function N(e){let t=0;for(;(t=e.indexOf(`\r`,t))!==-1;)e=e[t+1]===`
1075
1075
  `?e.slice(0,t)+e.slice(t+1):e.slice(0,t)+`
1076
1076
  `+e.slice(t+1);return e}function re(e,t){return new ne(e,t).document}}));function I6(e){let t=String(e),n=[];return{toOffset:i,toPoint:r};function r(e){if(typeof e==`number`&&e>-1&&e<=t.length){let r=0;for(;;){let i=n[r];if(i===void 0){let e=L6(t,n[r-1]);i=e===-1?t.length+1:e+1,n[r]=i}if(i>e)return{line:r+1,column:e-(r>0?n[r-1]:0)+1,offset:e};r++}}}function i(e){if(e&&typeof e.line==`number`&&typeof e.column==`number`&&!Number.isNaN(e.line)&&!Number.isNaN(e.column)){for(;n.length<e.line;){let e=n[n.length-1],r=L6(t,e),i=r===-1?t.length+1:r+1;if(e===i)break;n.push(i)}let r=(e.line>1?n[e.line-2]:0)+e.column-1;if(r<n[e.line-1])return r}}}function L6(e,t){let n=e.indexOf(`\r`,t),r=e.indexOf(`
1077
- `,t);return r===-1?n:n===-1||n+1===r?r:n<r?n:r}var R6=F6();function z6(e){let t=I6(e),n;try{n=(0,R6.parseXml)(String(e),{includeOffsets:!0,preserveCdata:!0,preserveComments:!0,preserveDocumentType:!0,preserveXmlDeclaration:!0})}catch(e){let n=e,r=new FJ("Could not parse XML with `@rgrove/parse-xml`",{cause:n,place:t.toPoint(n.pos),ruleId:`error`,source:`xast-util-from-xml`});throw r.fatal=!0,r.url=`https://github.com/syntax-tree/xast-util-from-xml#throws`,r}let r={location:t},i=H6(n,r);return Y6(n,i,r),i}function B6(e){return{type:`cdata`,value:e.text}}function V6(e){return{type:`comment`,value:e.content}}function H6(e,t){return{type:`root`,children:J6(e.children,t)}}function U6(e){return{type:`doctype`,name:e.name,public:e.publicId||void 0,system:e.systemId||void 0}}function W6(e,t){let n=J6(e.children,t);return{type:`element`,name:e.name,attributes:{...e.attributes},children:n}}function G6(e){return{type:`instruction`,name:e.name,value:e.content}}function K6(e){return{type:`text`,value:e.text}}function q6(e){let t=[];return e.version&&t.push(`version="`+e.version+`"`),e.encoding&&t.push(`encoding="`+e.encoding+`"`),e.standalone&&t.push(`standalone="`+e.standalone+`"`),{type:`instruction`,name:`xml`,value:t.join(` `)}}function J6(e,t){let n=[],r=-1;for(;++r<e.length;){let i=e[r],a;i.type===`cdata`?a=B6(i):i.type===`comment`?a=V6(i):i.type===`doctype`?a=U6(i):i.type===`element`?a=W6(i,t):i.type===`pi`?a=G6(i):i.type===`text`?a=K6(i):i.type===`xmldecl`&&(a=q6(i)),a&&(Y6(i,a,t),n.push(a))}return n}function Y6(e,t,n){let r=e.start===-1?void 0:n.location.toPoint(e.start),i=e.end===-1?void 0:n.location.toPoint(e.end);r&&i&&(t.position={start:r,end:i})}function X6(e){return e.replace(/&/g,`&amp;`).replace(/</g,`&lt;`).replace(/"/g,`&quot;`)}var Z6={"image/png":`png`,"image/jpeg":`jpg`,"image/gif":`gif`,"image/svg+xml":`svg`,"image/webp":`webp`,"image/bmp":`bmp`,"image/tiff":`tiff`};function Q6(e){let t=e.split(/[?#]/)[0];return/\.([a-zA-Z0-9]+)$/.exec(t)?.[1]}function $6(e){return e.fileRef&&Q6(e.fileRef)||e.url&&Q6(e.url)||e.contentType&&Z6[e.contentType]}function e8(e,t,n){if(e.isFile&&!e.fileRef&&!e.url)return`<!-- image asset "${t}" is marked as file-based but has no fileRef or url -->`;let r=e.isFile?$6(e):void 0,i=e.isFile?` source="${X6(r?`${t}.${r}`:t)}"`:``,a=n?` width="${X6(n)}"`:``,o=e.source?.trim();return o?`<image${i}${a}>\n${o}\n</image>`:`<image${i}${a}/>`}function t8(e,t){let n=e.source?.trim();return n?`<interactive xml:id="${X6(t)}">\n${n}\n</interactive>`:`<interactive xml:id="${X6(t)}"></interactive>`}var n8={image:e8,doenet:t8},r8=new Set(Object.keys(n8));function i8(e,t,n,r){let i=n.find(n=>n.kind===e&&n.ref===t);return i?n8[e](i,t,r):`<!-- missing asset: ${e} ${t} -->`}var a8={closeEmptyElements:!0,tightClose:!0};function o8(){return`sec-${Date.now().toString(36)}-${Math.random().toString(36).slice(2,8)}`}function s8(e){let t=e.children.find(e=>e.type===`element`&&e.name===`title`);return t?t.children.filter(e=>e.type===`text`).map(e=>e.value).join(``).trim():``}var c8=new Set([`book`,`article`,`slideshow`,`part`,`chapter`,...new Set([`introduction`,`section`,`worksheet`,`handout`,`exercises`,`references`,`glossary`,`solutions`,`reading-questions`,`conclusion`])]);function l8(e){let t=0,n=e.length;for(;t<n;){let n=e[t];if(n.type!==`text`||/\S/.test(n.value))break;t+=1}for(;n>t;){let t=e[n-1];if(t.type!==`text`||/\S/.test(t.value))break;--n}return e.slice(t,n)}function u8(e){return e.replace(/^(?:[ \t]*\r?\n)+/,``).replace(/(?:\r?\n[ \t]*)+$/,``)}function d8(e){try{return z6(e)}catch{return null}}function f8(e){let t=d8(`<__title__>${e}</__title__>`)?.children.find(e=>e.type===`element`);return t?t.children:[{type:`text`,value:e}]}function p8(e){let t=e.match(/^\s*<([A-Za-z_][\w.:-]*)\b[^>]*?>/);if(!t||t.index===void 0)return e;let n=e.slice(t.index+t[0].length),r=RegExp(`\\s*</${q8(t[1])}\\s*>\\s*$`);return u8(n.replace(r,``))}function m8(e,t){let n=d8(e);if(!n)return e;let r=n.children.find(e=>e.type===`element`);if(!r)return e;let i=r.children.findIndex(e=>e.type===`element`&&e.name===`title`),a={type:`element`,name:`title`,attributes:{},children:f8(t)};return i===-1?r.children.unshift(a):r.children.splice(i,1,a),JY(n,a8)}function h8(e=`New Section`){let t=o8();return{id:t,xmlId:t,title:e,source:`<section xml:id="${t}">\n\t<title>${e}</title>\n\n\t<p>\n\n\t</p>\n\n</section>`,type:`section`,sourceFormat:`pretext`}}function g8(){let e=o8();return{id:e,xmlId:e,title:`Introduction`,source:`<introduction xml:id="${e}">\n\n\t<p>\n\n\t</p>\n\n</introduction>`,type:`introduction`,sourceFormat:`pretext`}}function _8(){let e=o8();return{id:e,xmlId:e,title:`Conclusion`,source:`<conclusion xml:id="${e}">\n\n\t<p>\n\n\t</p>\n\n</conclusion>`,type:`conclusion`,sourceFormat:`pretext`}}function v8(e){let t=d8(e);if(!t)return p8(e);let n=t.children.find(e=>e.type===`element`);return n?JY({type:`root`,children:l8(n.children)},a8):e}function y8(e,t){return`<${t}>\n${u8(e)}\n</${t}>`}function b8(e,t){return e.trimStart().startsWith(`<${t}`)?e:y8(e,t)}function x8(e,t){if(t===`introduction`||t===`conclusion`)return e;let n=e.trimStart();return n.startsWith(`\\begin{section}`)?n.replace(/^\\begin\{section\}\s*\n?/,``).replace(/\n?\\end\{section\}\s*$/,``):e.replace(/^\\section\*?\{[^}]*\}\s*\n?/,``)}function S8(e,t,n,r){return t===`introduction`||t===`conclusion`?e:r?.trimStart().startsWith(`\\begin{section}`)?`\\begin{section}\n\n${e}\n\n\\end{section}`:`\\section{${n}}\n\n${e}`}function C8(e,t,n,r){if(t===`introduction`||t===`conclusion`)return e;let i=e.trimStart();return i.startsWith(`\\section`)||i.startsWith(`\\begin{section}`)?e:S8(e,t,n,r)}var w8=/^(\s*)\\(?!begin\b|end\b)[A-Za-z][A-Za-z-]*(\*?\{)/;function T8(e,t){let n=/^(\s*\\(?!begin\b|end\b)[A-Za-z][A-Za-z-]*\*?\{)[^}]*/;return n.test(e)?e.replace(n,(e,n)=>`${n}${t}`):e.includes(`\\begin{section}`)?/\\title\{/.test(e)?e.replace(/\\title\{[^}]*\}/,`\\title{${t}}`):e.replace(`\\begin{section}`,`\\begin{section}\n\n\\title{${t}}\n\n`):e}function E8(e){let t=/^\s*\\(?!begin\b|end\b)[A-Za-z][A-Za-z-]*\*?\{([^}]*)\}/.exec(e);if(t)return t[1].trim();if(e.includes(`\\begin{section}`)){let t=/\\title\{([^}]*)\}/.exec(e);if(t)return t[1].trim()}return null}function D8(e){return/^\s*\\(?!begin\b|end\b)[A-Za-z][A-Za-z-]*\*?\{[^}]*\}\s*\\label\{([^}]*)\}/.exec(e)?.[1]?.trim()??``}function O8(e,t){let n=e.source;return t.type!==void 0&&(n=n.replace(w8,`$1\\${t.type}$2`)),t.title!==void 0&&(n=T8(n,t.title)),t.xmlId!==void 0&&(n=n.replace(/^(\s*\\(?!begin\b|end\b)[A-Za-z][A-Za-z-]*\*?\{[^}]*\})(\s*\\label\{[^}]*\})?/,(e,n)=>t.xmlId==null||t.xmlId===``?n:`${n}\\label{${t.xmlId}}`)),{...e,source:n,title:t.title??e.title,type:t.type??e.type,xmlId:t.xmlId||e.xmlId}}function k8(e){let{pretextSource:t,pretextError:n}=o6(e.source,`latex`);return n||t===void 0?null:S5.has(e.type)?`<${e.type} xml:id="${e.xmlId}">\n<title>${e.title}</title>\n\n${t}\n</${e.type}>`:t}function A8(e=`New Section`){let t=o8();return{id:t,xmlId:t,title:e,source:`\\section{${e}}\n\n`,type:`section`,sourceFormat:`latex`}}function j8(){let e=o8();return{id:e,xmlId:e,title:`Introduction`,source:`% Introduction
1077
+ `,t);return r===-1?n:n===-1||n+1===r?r:n<r?n:r}var R6=F6();function z6(e){let t=I6(e),n;try{n=(0,R6.parseXml)(String(e),{includeOffsets:!0,preserveCdata:!0,preserveComments:!0,preserveDocumentType:!0,preserveXmlDeclaration:!0})}catch(e){let n=e,r=new FJ("Could not parse XML with `@rgrove/parse-xml`",{cause:n,place:t.toPoint(n.pos),ruleId:`error`,source:`xast-util-from-xml`});throw r.fatal=!0,r.url=`https://github.com/syntax-tree/xast-util-from-xml#throws`,r}let r={location:t},i=H6(n,r);return Y6(n,i,r),i}function B6(e){return{type:`cdata`,value:e.text}}function V6(e){return{type:`comment`,value:e.content}}function H6(e,t){return{type:`root`,children:J6(e.children,t)}}function U6(e){return{type:`doctype`,name:e.name,public:e.publicId||void 0,system:e.systemId||void 0}}function W6(e,t){let n=J6(e.children,t);return{type:`element`,name:e.name,attributes:{...e.attributes},children:n}}function G6(e){return{type:`instruction`,name:e.name,value:e.content}}function K6(e){return{type:`text`,value:e.text}}function q6(e){let t=[];return e.version&&t.push(`version="`+e.version+`"`),e.encoding&&t.push(`encoding="`+e.encoding+`"`),e.standalone&&t.push(`standalone="`+e.standalone+`"`),{type:`instruction`,name:`xml`,value:t.join(` `)}}function J6(e,t){let n=[],r=-1;for(;++r<e.length;){let i=e[r],a;i.type===`cdata`?a=B6(i):i.type===`comment`?a=V6(i):i.type===`doctype`?a=U6(i):i.type===`element`?a=W6(i,t):i.type===`pi`?a=G6(i):i.type===`text`?a=K6(i):i.type===`xmldecl`&&(a=q6(i)),a&&(Y6(i,a,t),n.push(a))}return n}function Y6(e,t,n){let r=e.start===-1?void 0:n.location.toPoint(e.start),i=e.end===-1?void 0:n.location.toPoint(e.end);r&&i&&(t.position={start:r,end:i})}function X6(e){return e.replace(/&/g,`&amp;`).replace(/</g,`&lt;`).replace(/"/g,`&quot;`)}var Z6={"image/png":`png`,"image/jpeg":`jpg`,"image/gif":`gif`,"image/svg+xml":`svg`,"image/webp":`webp`,"image/bmp":`bmp`,"image/tiff":`tiff`};function Q6(e){let t=e.split(/[?#]/)[0];return/\.([a-zA-Z0-9]+)$/.exec(t)?.[1]}function $6(e){return e.fileRef&&Q6(e.fileRef)||e.url&&Q6(e.url)||e.contentType&&Z6[e.contentType]}function e8(e,t,n){if(e.isFile&&!e.fileRef&&!e.url)return`<!-- image asset "${t}" is marked as file-based but has no fileRef or url -->`;let r=e.isFile?$6(e):void 0,i=e.isFile?` source="${X6(r?`${t}.${r}`:t)}"`:``,a=n?` width="${X6(n)}"`:``,o=e.source?.trim();return o?`<image${i}${a}>\n${o}\n</image>`:`<image${i}${a}/>`}function t8(e,t){let n=e.source?.trim();return n?`<interactive xml:id="${X6(t)}">\n${n}\n</interactive>`:`<interactive xml:id="${X6(t)}"></interactive>`}var n8={image:e8,doenet:t8},r8=new Set(Object.keys(n8));function i8(e,t,n,r){let i=n.find(n=>n.kind===e&&n.ref===t);return i?n8[e](i,t,r):`<!-- missing asset: ${e} ${t} -->`}var a8={closeEmptyElements:!0,tightClose:!0};function o8(){return`sec-${Date.now().toString(36)}-${Math.random().toString(36).slice(2,8)}`}function s8(e){let t=e.children.find(e=>e.type===`element`&&e.name===`title`);return t?t.children.filter(e=>e.type===`text`).map(e=>e.value).join(``).trim():``}var c8=new Set([`book`,`article`,`slideshow`,`part`,`chapter`,...new Set([`introduction`,`section`,`worksheet`,`handout`,`exercises`,`references`,`glossary`,`solutions`,`reading-questions`,`conclusion`])]);function l8(e){let t=0,n=e.length;for(;t<n;){let n=e[t];if(n.type!==`text`||/\S/.test(n.value))break;t+=1}for(;n>t;){let t=e[n-1];if(t.type!==`text`||/\S/.test(t.value))break;--n}return e.slice(t,n)}function u8(e){return e.replace(/^(?:[ \t]*\r?\n)+/,``).replace(/(?:\r?\n[ \t]*)+$/,``)}function d8(e){try{return z6(e)}catch{return null}}function f8(e){let t=d8(`<__title__>${e}</__title__>`)?.children.find(e=>e.type===`element`);return t?t.children:[{type:`text`,value:e}]}function p8(e){let t=e.match(/^\s*<([A-Za-z_][\w.:-]*)\b[^>]*?>/);if(!t||t.index===void 0)return e;let n=e.slice(t.index+t[0].length),r=RegExp(`\\s*</${q8(t[1])}\\s*>\\s*$`);return u8(n.replace(r,``))}function m8(e,t){let n=d8(e);if(!n)return e;let r=n.children.find(e=>e.type===`element`);if(!r)return e;let i=r.children.findIndex(e=>e.type===`element`&&e.name===`title`),a={type:`element`,name:`title`,attributes:{},children:f8(t)};return i===-1?r.children.unshift(a):r.children.splice(i,1,a),JY(n,a8)}function h8(e=`New Section`){let t=o8();return{id:t,xmlId:t,title:e,source:`<section xml:id="${t}">\n\t<title>${e}</title>\n\n\t<p>\n\n\t</p>\n\n</section>`,type:`section`,sourceFormat:`pretext`}}function g8(){let e=o8();return{id:e,xmlId:e,title:`Introduction`,source:`<introduction xml:id="${e}">\n\n\t<p>\n\n\t</p>\n\n</introduction>`,type:`introduction`,sourceFormat:`pretext`}}function _8(){let e=o8();return{id:e,xmlId:e,title:`Conclusion`,source:`<conclusion xml:id="${e}">\n\n\t<p>\n\n\t</p>\n\n</conclusion>`,type:`conclusion`,sourceFormat:`pretext`}}function v8(e){let t=d8(e);if(!t)return p8(e);let n=t.children.find(e=>e.type===`element`);return n?JY({type:`root`,children:l8(n.children)},a8):e}function y8(e,t){return`<${t}>\n${u8(e)}\n</${t}>`}function b8(e,t){return e.trimStart().startsWith(`<${t}`)?e:y8(e,t)}function x8(e,t){if(t===`introduction`||t===`conclusion`)return e;let n=e.trimStart();return n.startsWith(`\\begin{section}`)?n.replace(/^\\begin\{section\}\s*\n?/,``).replace(/\n?\\end\{section\}\s*$/,``):e.replace(/^\\section\*?\{[^}]*\}\s*\n?/,``)}function S8(e,t,n,r){return t===`introduction`||t===`conclusion`?e:r?.trimStart().startsWith(`\\begin{section}`)?`\\begin{section}\n\n${e}\n\n\\end{section}`:`\\section{${n}}\n\n${e}`}function C8(e,t,n,r){if(t===`introduction`||t===`conclusion`)return e;let i=e.trimStart();return i.startsWith(`\\section`)||i.startsWith(`\\begin{section}`)?e:S8(e,t,n,r)}var w8=/^(\s*)\\(?!begin\b|end\b)[A-Za-z][A-Za-z-]*(\*?\{)/;function T8(e,t){let n=/^(\s*\\(?!begin\b|end\b)[A-Za-z][A-Za-z-]*\*?\{)[^}]*/;return n.test(e)?e.replace(n,(e,n)=>`${n}${t}`):e.includes(`\\begin{section}`)?/\\title\{/.test(e)?e.replace(/\\title\{[^}]*\}/,`\\title{${t}}`):e.replace(`\\begin{section}`,`\\begin{section}\n\n\\title{${t}}\n\n`):e}function E8(e){let t=/^\s*\\(?!begin\b|end\b)[A-Za-z][A-Za-z-]*\*?\{([^}]*)\}/.exec(e);if(t)return t[1].trim();if(e.includes(`\\begin{section}`)){let t=/\\title\{([^}]*)\}/.exec(e);if(t)return t[1].trim()}return null}function D8(e){return/^\s*\\(?!begin\b|end\b)[A-Za-z][A-Za-z-]*\*?\{[^}]*\}\s*\\label\{([^}]*)\}/.exec(e)?.[1]?.trim()??``}function O8(e,t){let n=e.source;return t.type!==void 0&&(n=n.replace(w8,`$1\\${t.type}$2`)),t.title!==void 0&&(n=T8(n,t.title)),t.xmlId!==void 0&&(n=n.replace(/^(\s*\\(?!begin\b|end\b)[A-Za-z][A-Za-z-]*\*?\{[^}]*\})(\s*\\label\{[^}]*\})?/,(e,n)=>t.xmlId==null||t.xmlId===``?n:`${n}\\label{${t.xmlId}}`)),{...e,source:n,title:t.title??e.title,type:t.type??e.type,xmlId:t.xmlId||e.xmlId}}function k8(e){let{pretextSource:t,pretextError:n}=o6(e.source,`latex`);return n||t===void 0?null:t}function A8(e=`New Section`){let t=o8();return{id:t,xmlId:t,title:e,source:`\\section{${e}}\n\n`,type:`section`,sourceFormat:`latex`}}function j8(){let e=o8();return{id:e,xmlId:e,title:`Introduction`,source:`% Introduction
1078
1078
 
1079
1079
  `,type:`introduction`,sourceFormat:`latex`}}function M8(){let e=o8();return{id:e,xmlId:e,title:`Conclusion`,source:`% Conclusion
1080
1080
 
@@ -1082,12 +1082,12 @@ Expected it to be ${r}.`}var lW=class extends Error{name=`InvalidDocError`;const
1082
1082
 
1083
1083
  `+n.trimStart()}}let r=d8(e.source),i=d8(t.source),a=r?.children.find(e=>e.type===`element`),o=i?.children.find(e=>e.type===`element`);if(!a||!o)return{...e,source:e.source+`
1084
1084
 
1085
- `+t.source};let s=o.children.filter(e=>!(e.type===`element`&&e.name===`title`)),c={...a,children:[...a.children,...s]};return{...e,source:JY({type:`root`,children:[c]},a8)}}function P8(e){try{let t=z6(e).children.find(e=>e.type===`element`);return t?{xmlId:t.attributes?.[`xml:id`]??``,label:t.attributes?.label??``}:{xmlId:``,label:``}}catch{return{xmlId:``,label:``}}}function F8(e){return e.trim().replace(/[^A-Za-z0-9_-]/g,`-`).replace(/^[^A-Za-z_]+/,``)}function I8(e){return F8(e.trim().toLowerCase().replace(/[^a-z0-9]+/g,`-`).replace(/^-+|-+$/g,``))}function L8(e){let t=d8(e);if(!t)return null;let n=t.children.find(e=>e.type===`element`);return!n||!c8.has(n.name)?null:{title:s8(n),type:n.name,xmlId:n.attributes?.[`xml:id`]??``,label:n.attributes?.label??``}}function R8(e,t){let n=t.type??e.type,r=t.title??e.title;try{let i=z6(e.source).children.find(e=>e.type===`element`);if(!i)return{...e,title:r,type:n};let a={...i,name:n,attributes:{...i.attributes}};t.xmlId!==void 0&&(t.xmlId===null||t.xmlId===``?delete a.attributes[`xml:id`]:a.attributes[`xml:id`]=t.xmlId),t.label!==void 0&&(t.label===null||t.label===``?delete a.attributes.label:a.attributes.label=t.label);let o=a.children.findIndex(e=>e.type===`element`&&e.name===`title`),s={type:`element`,name:`title`,attributes:{},children:f8(r)};o===-1?a.children=[s,...a.children]:a.children=[...a.children.slice(0,o),s,...a.children.slice(o+1)];let c=JY({type:`root`,children:[a]},a8),l=t.xmlId!==void 0&&t.xmlId!==null&&t.xmlId!==``?t.xmlId:e.xmlId;return{...e,title:r,type:n,xmlId:l,source:c}}catch{return{...e,title:r,type:n}}}var z8=/^\uFEFF?[ \t]*---[ \t]*\r?\n([\s\S]*?)\r?\n[ \t]*---[ \t]*(?:\r?\n|$)/;function B8(e){let t=e.trim();return t.length>=2&&t.startsWith(`"`)&&t.endsWith(`"`)?t.slice(1,-1).replace(/\\"/g,`"`).replace(/\\\\/g,`\\`):t.replace(/^['"]|['"]$/g,``)}function V8(e){return`"${e.replace(/\\/g,`\\\\`).replace(/"/g,`\\"`)}"`}function H8(e){let t=z8.exec(e);if(!t)return null;let n=e.slice(t[0].length),r=`section`,i=``,a=``,o=``;for(let e of t[1].split(/\r?\n/)){let t=/^[ \t]*(xmlid|xml:id|division|label|title)[ \t]*:[ \t]*(.*)$/.exec(e);if(!t)continue;let n=B8(t[2]);t[1]===`division`?r=n:t[1]===`xmlid`||t[1]===`xml:id`?i=n:t[1]===`title`?o=n:a=n}return{type:r||`section`,xmlId:i,label:a,title:o,body:n}}function U8(e){let t=[`division: ${e.type}`,`xmlid: ${e.xmlId}`];return e.title&&t.push(`title: ${V8(e.title)}`),e.label&&t.push(`label: ${e.label}`),`---\n${t.join(`
1085
+ `+t.source};let s=o.children.filter(e=>!(e.type===`element`&&e.name===`title`)),c={...a,children:[...a.children,...s]};return{...e,source:JY({type:`root`,children:[c]},a8)}}function P8(e){try{let t=z6(e).children.find(e=>e.type===`element`);return t?{xmlId:t.attributes?.[`xml:id`]??``,label:t.attributes?.label??``}:{xmlId:``,label:``}}catch{return{xmlId:``,label:``}}}function F8(e){return e.trim().replace(/[^A-Za-z0-9_-]/g,`-`).replace(/^[^A-Za-z_]+/,``)}function I8(e){return F8(e.trim().toLowerCase().replace(/[^a-z0-9]+/g,`-`).replace(/^-+|-+$/g,``))}function L8(e){let t=d8(e);if(!t)return null;let n=t.children.find(e=>e.type===`element`);return!n||!c8.has(n.name)?null:{title:s8(n),type:n.name,xmlId:n.attributes?.[`xml:id`]??``,label:n.attributes?.label??``}}function R8(e,t){let n=t.type??e.type,r=t.title??e.title;try{let i=z6(e.source).children.find(e=>e.type===`element`);if(!i)return{...e,title:r,type:n};let a={...i,name:n,attributes:{...i.attributes}};t.xmlId!==void 0&&(t.xmlId===null||t.xmlId===``?delete a.attributes[`xml:id`]:a.attributes[`xml:id`]=t.xmlId),t.label!==void 0&&(t.label===null||t.label===``?delete a.attributes.label:a.attributes.label=t.label);let o=a.children.findIndex(e=>e.type===`element`&&e.name===`title`),s={type:`element`,name:`title`,attributes:{},children:f8(r)};o===-1?a.children=[s,...a.children]:a.children=[...a.children.slice(0,o),s,...a.children.slice(o+1)];let c=JY({type:`root`,children:[a]},a8),l=t.xmlId!==void 0&&t.xmlId!==null&&t.xmlId!==``?t.xmlId:e.xmlId;return{...e,title:r,type:n,xmlId:l,source:c}}catch{return{...e,title:r,type:n}}}var z8=/^\uFEFF?[ \t]*---[ \t]*\r?\n([\s\S]*?)\r?\n[ \t]*---[ \t]*(?:\r?\n|$)/;function B8(e){let t=e.trim();return t.length>=2&&t.startsWith(`"`)&&t.endsWith(`"`)?t.slice(1,-1).replace(/\\"/g,`"`).replace(/\\\\/g,`\\`):t.replace(/^['"]|['"]$/g,``)}function V8(e){return`"${e.replace(/\\/g,`\\\\`).replace(/"/g,`\\"`)}"`}function H8(e){let t=z8.exec(e);if(!t)return null;let n=e.slice(t[0].length),r=`section`,i=``,a=``,o=``;for(let e of t[1].split(/\r?\n/)){let t=/^[ \t]*(id|xmlid|xml:id|division|label|title)[ \t]*:[ \t]*(.*)$/.exec(e);if(!t)continue;let n=B8(t[2]);t[1]===`division`?r=n:t[1]===`id`||t[1]===`xmlid`||t[1]===`xml:id`?i=n:t[1]===`title`?o=n:a=n}return{type:r||`section`,xmlId:i,label:a,title:o,body:n}}function U8(e){let t=[`division: ${e.type}`,`id: ${e.xmlId}`];return e.title&&t.push(`title: ${V8(e.title)}`),e.label&&t.push(`label: ${e.label}`),`---\n${t.join(`
1086
1086
  `)}\n---`}function W8(e){let t=/^[ \t]*#[ \t]+(.*)$/m.exec(e);return t?t[1].trim():null}function G8(e){let t=H8(e);return t?{title:t.title||W8(t.body)||``,type:t.type,xmlId:t.xmlId,label:t.label}:null}function K8(e,t){let n=H8(e.source),r=n?n.body:e.source,i=n?.type??e.type,a=n?.xmlId??e.xmlId,o=n?.label??``,s=n?.title||e.title,c=t.type??i,l=(t.xmlId===void 0?a:t.xmlId??``)||e.xmlId,u=t.label===void 0?o:t.label??``,d=t.title??s,f=`${U8({type:c,xmlId:l,label:u,title:d})}\n${r}`;return{...e,title:d,type:c,xmlId:l,source:f}}function q8(e){return e.replace(/[.*+?^${}()|[\]\\]/g,`\\$&`)}function J8(e){return e===`pretext`||e===`markdown`||e===`latex`}var Y8=new Set([`division`,...c8,`subsection`,`subsubsection`,`paragraphs`]),X8=Array.from(Y8).join(`|`);function Z8(e){let t=e===null?`ref="([^"]+)"`:`ref="${q8(e)}"`,n=`(?:${X8})`;return`<plus:${n}\\s[^>]*${t}[^>]*?(?:/>|>\\s*</plus:${n}>)`}function Q8(e){let t=e===null?`ref="([^"]+)"`:`ref="${q8(e)}"`;return`::${`(?:${X8})`}(?:\\[[^\\]]*\\])?\\{[^}]*${t}[^}]*\\}`}function $8(e){let t=e===null?`([^}]+)`:q8(e);return`\\\\plus\\{${`(?:${X8})`}\\}\\{${t}\\}`}function e5(e){return`(?:${Z8(e)}|${Q8(e)}|${$8(e)})`}function t5(e,t){switch(e){case`pretext`:return Z8(t);case`markdown`:return Q8(t);case`latex`:return $8(t)}}var n5={pretext:[`pre`,`c`,`cd`,`program`,`console`,`sage`,`latex-image`,`sageplot`,`asymptote`].map(e=>`<${e}(?:\\s[^>]*)?>[\\s\\S]*?</${e}>`),markdown:["```[\\s\\S]*?```",`~~~[\\s\\S]*?~~~`,"`[^`\\n]*`"],latex:[`\\\\begin\\{verbatim\\}[\\s\\S]*?\\\\end\\{verbatim\\}`,`\\\\begin\\{lstlisting\\}[\\s\\S]*?\\\\end\\{lstlisting\\}`,`\\\\verb\\*?\\|[^|\\n]*\\|`]};function r5(e,t){let n=n5[t];if(n.length===0)return e;let r=new RegExp(n.join(`|`),`g`);return e.replace(r,e=>e.replace(/[^\n]/g,` `))}function i5(e,t){let n=[],r=new RegExp(t5(t,null),`g`),i=r5(e,t),a;for(;(a=r.exec(i))!==null;)n.push(a[1]);return n}function a5(e,t){let n=[],r=X8,i={pretext:`<plus:(${r})\\s[^>]*ref="([^"]+)"[^>]*?(?:/>|>\\s*</plus:${`(?:${r})`}>)`,markdown:`::(${r})(?:\\[[^\\]]*\\])?\\{[^}]*ref="([^"]+)"[^}]*\\}`,latex:`\\\\plus\\{(${r})\\}\\{([^}]+)\\}`},a=new RegExp(i[t],`g`),o=r5(e,t),s;for(;(s=a.exec(o))!==null;){let e=s[1],t=s[2],r=e===`division`?`section`:e;n.push({type:r,xmlId:t})}return n}function o5(e,t){let n=[],r=new RegExp({pretext:`<plus:(image|doenet)\\b[^>]*\\bref="([^"]+)"`,markdown:`::(image|doenet)(?:\\[[^\\]]*\\])?\\{[^}]*\\bref="([^"]+)"[^}]*\\}`,latex:`\\\\plus\\{(image|doenet)\\}\\{([^}]+)\\}`}[t],`g`),i=r5(e,t),a;for(;(a=r.exec(i))!==null;)n.push({kind:a[1],ref:a[2]});return n}function s5(e,t,n,r){let i=q8(t),a=q8(n),o=RegExp(`(<plus:${i}\\b[^>]*?\\bref=")${a}(")`,`g`),s=RegExp(`(::${i}(?:\\[[^\\]]*\\])?\\{[^}]*?\\bref=")${a}(")`,`g`),c=RegExp(`(\\\\plus\\{${i}\\}\\{)${a}(\\})`,`g`);return e.replace(o,`$1${r}$2`).replace(s,`$1${r}$2`).replace(c,`$1${r}$2`)}function c5(e,t,n){let r=q8(t),i=q8(n),a=RegExp(`<plus:${r}\\b[^>]*?\\bref="${i}"[^>]*/?>`,`g`),o=RegExp(`::${r}(?:\\[[^\\]]*\\])?\\{[^}]*?\\bref="${i}"[^}]*\\}`,`g`),s=RegExp(`\\\\plus\\{${r}\\}\\{${i}\\}`,`g`);return e.replace(a,``).replace(o,``).replace(s,``)}function l5(e,t,n=`pretext`){return n===`markdown`?`::${e}{ref="${t}"}`:n===`latex`?`\\plus{${e}}{${t}}`:`<plus:${e} ref="${t}"/>`}function u5(e,t,n=`pretext`){let r=`New ${t.charAt(0).toUpperCase()+t.slice(1)}`;return{id:e,xmlId:e,title:r,type:t,sourceFormat:n,source:d5(t,n,r,e)}}function d5(e,t,n,r){return t===`latex`?e===`introduction`||e===`conclusion`?`% ${n}\n\n`:`\\${e}{${n}}\\label{${r}}\n\n`:t===`markdown`?`${U8({type:e,xmlId:r,label:``,title:n})}\n\n`:e===`introduction`||e===`conclusion`?`<${e} xml:id="${r}">\n\n\t<p>\n\n\t</p>\n\n</${e}>`:`<${e} xml:id="${r}">\n\t<title>${n}</title>\n\n\t<p>\n\n\t</p>\n\n</${e}>`}function f5(e,t,n,r,i=`pretext`){let a=i===`markdown`?`::${n}{ref="${t}"}`:i===`latex`?`\\plus{${n}}{${t}}`:`<plus:${n} ref="${t}"/>`;if(r!==null){let t=new RegExp(e5(r)).exec(e);if(t){let n=t.index+t[0].length;return e.slice(0,n)+`
1087
1087
  `+a+e.slice(n)}}let o=e.lastIndexOf(`</`);return o===-1?e+`
1088
1088
  `+a:e.slice(0,o)+a+`
1089
1089
  `+e.slice(o)}function p5(e,t){let n=RegExp(`[ \t]*${e5(t)}[ \t]*\n?`,`g`);return e.replace(n,``)}function m5(e,t,n){let r=new RegExp(e5(t)).exec(e),i=r?r[0]:`<plus:division ref="${t}"/>`,a=p5(e,t);if(n!==null){let e=new RegExp(e5(n)).exec(a);if(e){let t=e.index+e[0].length;return a.slice(0,t)+`
1090
1090
  `+i+a.slice(t)}}let o=a.lastIndexOf(`</`);return o===-1?a+`
1091
1091
  `+i:a.slice(0,o)+i+`
1092
- `+a.slice(o)}function h5(e,t,n,r){let i=new RegExp(e5(t));return e.replace(i,e=>{let t=e.trimStart();return t.startsWith(`::`)?`::${r}{ref="${n}"}`:t.startsWith(`\\plus`)?`\\plus{${r}}{${n}}`:`<plus:${r} ref="${n}"/>`})}function g5(e,t){let n=new RegExp(e5(t));return e.find(e=>n.test(e.source))??null}function _5(e,t){let n=e,r=null;for(let e of t)n=m5(n,e,r),r=e;return n}function v5(e,t){let n=new Set,r=[t];for(;r.length>0;){let t=r.pop();if(n.has(t))continue;n.add(t);let i=e.find(e=>e.xmlId===t);if(i)for(let e of i5(i.source,i.sourceFormat))r.push(e)}return n}function y5(e,t){let n=v5(e,t);return e.filter(e=>!n.has(e.xmlId))}function b5(e,t){let n=[],r=new Set([t]),i=(t,a)=>{let o=e.find(e=>e.xmlId===t);if(o)for(let s of i5(o.source,o.sourceFormat)){if(r.has(s))continue;let o=e.find(e=>e.xmlId===s);o&&(r.add(s),n.push({division:o,depth:a,parentXmlId:t}),i(s,a+1))}};return i(t,0),n}function x5(e,t){let n=y5(e,t),r=new Set(n.map(e=>e.xmlId)),i=new Set;for(let e of n)for(let t of i5(e.source,e.sourceFormat))r.has(t)&&i.add(t);return n.filter(e=>!i.has(e.xmlId))}var S5=new Set([`book`,`article`,`slideshow`]);function C5(e){try{let t=z6(e),n=t.children.find(e=>e.type===`element`),r=n?.name===`pretext`?n.children.find(e=>e.type===`element`&&S5.has(e.name)):n&&S5.has(n.name)?n:void 0;if(!r)return e;if(!r.attributes.label){let e=T5(t,`pretext-plus-preview`);return r.attributes.label=e,JY(t,a8)}return e}catch(t){return console.error(`Error ensuring label:`,t),e}}function w5(e,t){return e.type===`element`&&e.attributes?.label===t?!0:`children`in e&&e.children?e.children.some(e=>e.type===`element`?w5(e,t):!1):!1}function T5(e,t){let n=t,r=1;for(;w5(e,n);)n=`${t}-${r}`,r++;return n}function E5(e,t,n,r){let i=t.find(t=>t.xmlId===e);if(!i)return`<!-- missing division: ${e} -->`;if(n.has(e))return`<!-- circular reference: ${e} -->`;let a;if(i.sourceFormat===`pretext`)a=i.source;else if(i.sourceFormat===`markdown`){let{pretextSource:e,pretextError:t}=o6(i.source,`markdown`);a=e??`<!-- conversion error: ${t} -->`}else a=k8(i)??`<!-- conversion error: ${i.xmlId} -->`;let o=new Set(n).add(e);return a.replace(/<plus:([a-z-]+)\s([^>]*ref="[^"]+"[^>]*?)(?:\/>|>\s*<\/plus:\1>)/g,(e,n,i)=>{let a=/ref="([^"]+)"/.exec(i)?.[1]??``;if(!r8.has(n))return E5(a,t,o,r);let s=/width="([^"]+)"/.exec(i)?.[1];return i8(n,a,r,s)})}function D5(e,t,n=[]){return C5(E5(t,e,new Set,n))}function O5(e,t){return C5(`<pretext>\n${t.trim()?`${t.trim()}\n`:``}${e}\n</pretext>`)}function k5(e,t,n,r=[]){return O5(E5(t,e,new Set,r),n)}var A5=new Set([`part`,`chapter`]);function j5(e,t,n,r){return O5(S5.has(e)?t:A5.has(e)?`<book>\n<title>${r}</title>\n${t}\n</book>`:`<article>\n<title>${r}</title>\n${t}\n</article>`,n)}function M5(e){let t=e.trim(),n=t.match(/^<title\b[^>]*>([\s\S]*?)<\/title>\s*/);return n?{title:n[1].trim(),body:t.slice(n[0].length)}:{title:``,body:t}}function N5(e,t,n,r){let i=n===`book`?`book`:`article`;return e.map(e=>{if(e.sourceFormat===`markdown`){if(!e.title){let t=G8(e.source)?.title;if(t)return{...e,title:t}}return e}if(e.sourceFormat===`latex`){if(!e.title){let t=E8(e.source);if(t)return{...e,title:t}}return e}if(e.sourceFormat!==`pretext`)return e;let n=L8(e.source);if(e.xmlId===t&&!(n&&S5.has(n.type))){let{title:t,body:n}=M5(e.source),a=e.title||t||r||`Untitled`;return{...e,type:i,title:a,source:`<${i} xml:id="${e.xmlId}">\n<title>${a}</title>\n\n${n}\n</${i}>`}}return!e.title&&n?.title?{...e,title:n.title}:e})}function P5(e,t){let n=t??[],r=(e,t)=>n.find(n=>n.kind===e&&n.ref===t),i=[],a=new Set;for(let t of e??[])for(let{kind:e,ref:n}of o5(t.source,t.sourceFormat)){let t=`${e}:${n}`;if(a.has(t))continue;a.add(t);let o=r(e,n);i.push({kind:e,ref:n,asset:o,inDocument:!0,status:o?`linked`:`unlinked`})}for(let e of n){if(!e.ref)continue;let t=`${e.kind}:${e.ref}`;a.has(t)||(a.add(t),i.push({kind:e.kind,ref:e.ref,asset:e,inDocument:!1,status:`unused`}))}return i}function F5(e,t){let n=`${e}-copy`;if(!t.has(n))return n;for(let e=2;;e++){let r=`${n}-${e}`;if(!t.has(r))return r}}var I5={image:`Images`,doenet:`Doenet`},L5=[`image`,`doenet`].filter(e=>e!==`doenet`||!1);function R5(e){return`${e}-${Date.now()}`}var z5=({open:e,onClose:r,resolveTarget:i,replaceTarget:a,onLoadAssets:o,onLoadLibraryAssets:s,onAddFromLibrary:c,onUpload:l,onFetchUrl:u,onCreateDoenet:d,onRemoveAsset:f,onDuplicateAsset:p,onAssetAdded:m,onResolveRef:h,onReplaceAsset:g})=>{let _=m6(e=>e.divisions),v=m6(e=>e.activeDivisionId),y=_?.find(e=>e.xmlId===v)?.sourceFormat??`pretext`,b=(e,t)=>l5(e,t,y),x=m6(e=>e.projectAssets)??[],S=m6(e=>e.setProjectAssets),C=m6(e=>e.libraryAssets),w=m6(e=>e.openAssetEditor),T=m6(e=>e.openAssetResolver),E=m6(e=>e.removeAssetRefFromDocument),D=!!(o||s),[O,k]=(0,n.useState)(null),[A,ee]=(0,n.useState)(!1),[te,j]=(0,n.useState)(null),M=x,ne=O??C??M,N=new Set(M.map(e=>e.id)),[re,ie]=(0,n.useState)(`in-document`),[ae,oe]=(0,n.useState)(null),[se,P]=(0,n.useState)(`library`),[ce,le]=(0,n.useState)(`library`),[ue,de]=(0,n.useState)(null),[F,fe]=(0,n.useState)(!1),[pe,me]=(0,n.useState)(!1),[he,ge]=(0,n.useState)(null),I=(0,n.useRef)(null),[_e,ve]=(0,n.useState)(null),[ye,be]=(0,n.useState)(null),[xe,Se]=(0,n.useState)(``),[Ce,L]=(0,n.useState)(``),[we,Te]=(0,n.useState)(``),[Ee,R]=(0,n.useState)(!1),[De,Oe]=(0,n.useState)(null),[ke,z]=(0,n.useState)(``),[Ae,je]=(0,n.useState)(``),[Me,Ne]=(0,n.useState)(!1),[Pe,Fe]=(0,n.useState)(null),[Ie,Le]=(0,n.useState)(null),[Re,ze]=(0,n.useState)(null),[Be,Ve]=(0,n.useState)(null),He=(0,n.useCallback)(()=>{!o&&!s||(ee(!0),j(null),Promise.all([o?.()??Promise.resolve(null),s?.()??Promise.resolve(null)]).then(([e,t])=>{e!==null&&S(e),t!==null&&k(t)}).catch(e=>{j(e instanceof Error?e.message:`Failed to load assets.`)}).finally(()=>ee(!1)))},[o,s,S]),Ue=(0,n.useRef)(He);if((0,n.useEffect)(()=>{Ue.current=He}),(0,n.useEffect)(()=>{e&&Ue.current()},[e]),(0,n.useEffect)(()=>{if(!e)return;let t=e=>{e.key===`Escape`&&r()};return window.addEventListener(`keydown`,t),()=>window.removeEventListener(`keydown`,t)},[e,r]),(0,n.useEffect)(()=>()=>{ye&&URL.revokeObjectURL(ye)},[ye]),!e)return null;let B=P5(_,M),We=(e,t)=>{if(a){g(a,e,!!t?.fromLibrary),r();return}if(m(e),i){e.ref&&h(i.kind,i.ref,e.ref),r();return}e.ref&&navigator.clipboard.writeText(b(e.kind,e.ref)).catch(()=>{}),Ve(e)},Ge=(e,t)=>{let n=`${e}:${t}`;navigator.clipboard.writeText(b(e,t)).catch(()=>{}),Le(n),setTimeout(()=>Le(e=>e===n?null:e),2e3)},Ke=async e=>{if(!N.has(e.id)&&c){de(e.id);try{await c(e)}finally{de(null)}}We(e,{fromLibrary:!0})},V=e=>{ge(null),ve(e),Se(e.name),be(URL.createObjectURL(e))},qe=()=>{ve(null),be(null),Se(``)},Je=async()=>{if(!(!l||!_e)){ge(null),me(!0);try{let e=xe.trim()||_e.name;We(await l(new File([_e],e,{type:_e.type}))),qe()}catch(e){ge(e instanceof Error?e.message:`Upload failed.`)}finally{me(!1)}}},Ye=async()=>{let e=Ce.trim();if(!e)return;let t=we.trim();if(u&&l){Oe(null),R(!0);try{let n=await u(e);We(await l(t?new File([n],t,{type:n.type}):n))}catch(e){Oe(e instanceof Error?e.message:`Failed to add URL.`)}finally{R(!1)}}else We({id:R5(`url`),title:t||e,ref:e.split(`/`).pop()??`image`,kind:`image`,url:e})},Xe=async()=>{let e=ke.trim(),t=Ae.trim();if(!(!e||!t))if(d){Fe(null),Ne(!0);try{We(await d(e,t))}catch(e){Fe(e instanceof Error?e.message:`Failed to create activity.`)}finally{Ne(!1)}}else We({id:R5(`doenet`),title:e,ref:t,kind:`doenet`})},Ze=e=>{let n=ne.filter(t=>t.kind===e);return(0,t.jsxs)(`div`,{className:`pretext-plus-editor__am-library`,children:[D&&(0,t.jsxs)(`div`,{className:`pretext-plus-editor__am-toolbar`,children:[te&&(0,t.jsx)(`span`,{className:`pretext-plus-editor__asset-load-error`,children:te}),(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__asset-library-refresh`,onClick:He,disabled:A,children:A?`Loading…`:`Refresh`})]}),A&&n.length===0?(0,t.jsx)(`p`,{className:`pretext-plus-editor__am-empty-text`,children:`Loading…`}):n.length===0?(0,t.jsxs)(`p`,{className:`pretext-plus-editor__am-empty-text`,children:[`No `,I5[e].toLowerCase(),` assets in your library.`]}):(0,t.jsx)(`ul`,{className:`pretext-plus-editor__am-list`,children:n.map(e=>{let n=N.has(e.id),r=ue===e.id;return(0,t.jsx)(`li`,{children:(0,t.jsxs)(`button`,{type:`button`,className:[`pretext-plus-editor__am-lib-row`,n?`pretext-plus-editor__am-lib-row--in-project`:``].filter(Boolean).join(` `),onClick:()=>Ke(e),disabled:r||!e.ref,title:e.ref?n?`Use "${e.ref}"`:`Add to project & use "${e.ref}"`:`Asset has no reference — cannot use`,children:[(0,t.jsxs)(`div`,{className:`pretext-plus-editor__am-row-info`,children:[(0,t.jsx)(`span`,{className:`pretext-plus-editor__am-row-name`,children:e.title}),(0,t.jsx)(`span`,{className:`pretext-plus-editor__am-row-ref`,children:r?`Adding…`:e.ref})]}),n&&(0,t.jsx)(`span`,{className:`pretext-plus-editor__am-badge`,children:`✓`})]})},e.id)})})]})},Qe=()=>{if(B.length===0)return(0,t.jsxs)(`div`,{className:`pretext-plus-editor__am-placeholder`,children:[(0,t.jsx)(`p`,{children:`No assets in this project yet.`}),(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__dialog-button`,onClick:()=>{ie(`add`),oe(null)},children:`Add an asset`})]});let e=L5.map(e=>({kind:e,rows:B.filter(t=>t.kind===e)})).filter(e=>e.rows.length>0),n=e=>{let n=`${e.kind}:${e.ref}`,i=Re===n,a=()=>{e.status===`unlinked`?T(e.kind,e.ref):(w(e.kind,e.ref),r())};return(0,t.jsxs)(`li`,{className:`pretext-plus-editor__am-doc-row`,children:[(0,t.jsxs)(`button`,{type:`button`,className:`pretext-plus-editor__am-row-info pretext-plus-editor__am-row-info--btn`,onClick:a,title:e.status===`unlinked`?`No asset for this reference — click to link or create one`:`Edit asset`,children:[(0,t.jsx)(`span`,{className:`pretext-plus-editor__am-row-name`,children:e.asset?.title??e.ref}),(0,t.jsx)(`span`,{className:`pretext-plus-editor__am-row-ref`,children:e.ref})]}),e.status===`unlinked`&&(0,t.jsx)(`span`,{className:`pretext-plus-editor__am-status pretext-plus-editor__am-status--warn`,title:`No asset for this reference`,children:`needs asset`}),e.status===`unused`&&(0,t.jsx)(`span`,{className:`pretext-plus-editor__am-status`,title:`Not referenced in the document yet`,children:`not placed`}),(0,t.jsxs)(`div`,{className:`pretext-plus-editor__am-row-actions`,children:[(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__am-action-btn`,onClick:a,children:e.status===`unlinked`?`Link / create`:`Edit`}),(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__am-action-btn${Ie===n?` pretext-plus-editor__am-action-btn--done`:``}`,onClick:()=>Ge(e.kind,e.ref),title:`Copy ${b(e.kind,e.ref)}`,children:Ie===n?`Copied!`:`Copy embed code`}),p&&e.asset?.url&&(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__am-action-btn`,disabled:i,onClick:async()=>{ze(n);try{await p(e.asset),r()}catch{ze(null)}},title:`Create a copy of this asset under a new reference`,children:i?`Duplicating…`:`Duplicate`}),f&&e.asset&&(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__am-action-btn pretext-plus-editor__am-action-btn--danger`,onClick:()=>{e.inDocument&&!window.confirm(`Remove "${e.asset.title}" from the project? This also deletes its reference(s) from the document.`)||(f(e.asset),E(e.kind,e.ref),r())},title:`Remove from project`,children:`Remove`})]})]},n)};return(0,t.jsx)(`div`,{className:`pretext-plus-editor__am-in-doc`,children:e.map(({kind:e,rows:r})=>(0,t.jsxs)(`div`,{className:`pretext-plus-editor__am-kind-group`,children:[(0,t.jsxs)(`div`,{className:`pretext-plus-editor__am-kind-header`,children:[(0,t.jsx)(`span`,{"aria-hidden":`true`,children:`📁`}),(0,t.jsx)(`span`,{children:I5[e]}),(0,t.jsx)(`span`,{className:`pretext-plus-editor__am-kind-count`,children:r.length})]}),(0,t.jsx)(`ul`,{className:`pretext-plus-editor__am-list`,children:r.map(n)})]},e))})},$e=e=>(0,t.jsxs)(`div`,{className:`pretext-plus-editor__am-success`,children:[(0,t.jsxs)(`p`,{className:`pretext-plus-editor__am-success-title`,children:[`✓ Added “`,e.title,`” — embed code copied to clipboard`]}),(0,t.jsx)(`p`,{className:`pretext-plus-editor__dialog-helper-copy`,children:`It now appears in the Assets list. Paste this where you want it to appear:`}),e.ref&&(0,t.jsxs)(`div`,{className:`pretext-plus-editor__am-embed-row`,children:[(0,t.jsx)(`code`,{className:`pretext-plus-editor__am-embed-code`,children:b(e.kind,e.ref)}),(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__am-action-btn${Ie===`${e.kind}:${e.ref}`?` pretext-plus-editor__am-action-btn--done`:``}`,onClick:()=>Ge(e.kind,e.ref),children:Ie===`${e.kind}:${e.ref}`?`Copied!`:`Copy again`})]}),(0,t.jsxs)(`div`,{className:`pretext-plus-editor__am-success-actions`,children:[(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__dialog-button pretext-plus-editor__dialog-button--secondary`,onClick:()=>{Ve(null),oe(null),ie(`add`)},children:`Add another`}),(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__dialog-button`,onClick:()=>{Ve(null),ie(`in-document`)},children:`Done`})]})]}),et=()=>(0,t.jsxs)(`div`,{className:`pretext-plus-editor__am-kind-picker`,children:[(0,t.jsx)(`p`,{className:`pretext-plus-editor__am-kind-picker-label`,children:`What kind of asset?`}),(0,t.jsxs)(`div`,{className:`pretext-plus-editor__am-kind-cards`,children:[(0,t.jsxs)(`button`,{type:`button`,className:`pretext-plus-editor__am-kind-card`,onClick:()=>{oe(`image`),P(`library`)},children:[(0,t.jsx)(`span`,{className:`pretext-plus-editor__am-kind-card-icon`,"aria-hidden":`true`,children:`🖼️`}),(0,t.jsx)(`span`,{className:`pretext-plus-editor__am-kind-card-label`,children:`Image`}),(0,t.jsx)(`span`,{className:`pretext-plus-editor__am-kind-card-hint`,children:`PNG, JPEG, SVG, etc.`})]}),!1,(0,t.jsxs)(`div`,{className:`pretext-plus-editor__am-kind-card pretext-plus-editor__am-kind-card--soon`,"aria-disabled":`true`,children:[(0,t.jsx)(`span`,{className:`pretext-plus-editor__am-kind-card-icon`,"aria-hidden":`true`,children:`✨`}),(0,t.jsx)(`span`,{className:`pretext-plus-editor__am-kind-card-label`,children:`More coming soon`}),(0,t.jsx)(`span`,{className:`pretext-plus-editor__am-kind-card-hint`,children:`Interactive activities and more`})]})]})]}),tt=e=>(0,t.jsxs)(`div`,{className:`pretext-plus-editor__am-configure`,children:[e&&(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__am-back-btn`,onClick:()=>oe(null),children:`← Back`}),(0,t.jsxs)(`div`,{className:`pretext-plus-editor__dialog-tab-bar pretext-plus-editor__am-sub-tabs`,children:[(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__dialog-tab${se===`library`?` pretext-plus-editor__dialog-tab--active`:``}`,onClick:()=>P(`library`),children:`Library`}),l&&(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__dialog-tab${se===`upload`?` pretext-plus-editor__dialog-tab--active`:``}`,onClick:()=>P(`upload`),children:`Upload`}),(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__dialog-tab${se===`url`?` pretext-plus-editor__dialog-tab--active`:``}`,onClick:()=>P(`url`),children:`External URL`})]}),(0,t.jsxs)(`div`,{className:`pretext-plus-editor__am-configure-body`,children:[se===`library`&&Ze(`image`),se===`upload`&&l&&(0,t.jsx)(`div`,{className:`pretext-plus-editor__am-upload`,children:_e?(0,t.jsxs)(`div`,{className:`pretext-plus-editor__am-url-form`,children:[(0,t.jsx)(`img`,{src:ye??void 0,alt:`Preview`,className:`pretext-plus-editor__am-url-preview`}),(0,t.jsxs)(`label`,{className:`pretext-plus-editor__dialog-label`,htmlFor:`am-upload-title`,children:[`Title `,(0,t.jsx)(`span`,{className:`pretext-plus-editor__dialog-helper-copy`,children:`(optional)`})]}),(0,t.jsx)(`input`,{id:`am-upload-title`,type:`text`,className:`pretext-plus-editor__am-input`,placeholder:_e.name,value:xe,onChange:e=>Se(e.target.value),disabled:pe,autoFocus:!0}),he&&(0,t.jsx)(`p`,{className:`pretext-plus-editor__am-error`,children:he}),(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__dialog-button pretext-plus-editor__dialog-button--secondary`,onClick:qe,disabled:pe,children:`Choose a different file`})]}):(0,t.jsxs)(`div`,{className:[`pretext-plus-editor__am-drop-zone`,F?`pretext-plus-editor__am-drop-zone--active`:``].filter(Boolean).join(` `),onDragOver:e=>{e.preventDefault(),fe(!0)},onDragLeave:()=>fe(!1),onDrop:e=>{e.preventDefault(),fe(!1);let t=e.dataTransfer.files[0];t&&V(t)},onClick:()=>I.current?.click(),role:`button`,tabIndex:0,onKeyDown:e=>{(e.key===`Enter`||e.key===` `)&&(e.preventDefault(),I.current?.click())},"aria-label":`Upload image — click or drag and drop`,children:[(0,t.jsx)(`input`,{ref:I,type:`file`,accept:`image/*`,className:`pretext-plus-editor__dialog-file-input`,onChange:e=>{let t=e.target.files?.[0];t&&V(t)}}),(0,t.jsx)(`span`,{className:`pretext-plus-editor__am-drop-icon`,"aria-hidden":`true`,children:`↑`}),(0,t.jsx)(`p`,{className:`pretext-plus-editor__am-drop-text`,children:`Drag & drop an image, or click to browse`}),(0,t.jsx)(`p`,{className:`pretext-plus-editor__dialog-helper-copy`,children:`PNG, JPEG, GIF, SVG, WebP`})]})}),se===`url`&&(0,t.jsxs)(`div`,{className:`pretext-plus-editor__am-url-form`,children:[(0,t.jsx)(`label`,{className:`pretext-plus-editor__dialog-label`,htmlFor:`am-url-value`,children:`Image URL`}),(0,t.jsx)(`input`,{id:`am-url-value`,type:`url`,className:`pretext-plus-editor__am-input`,placeholder:`https://example.com/image.png`,value:Ce,onChange:e=>L(e.target.value),disabled:Ee,autoFocus:!0}),(0,t.jsxs)(`label`,{className:`pretext-plus-editor__dialog-label`,htmlFor:`am-url-title`,children:[`Title `,(0,t.jsx)(`span`,{className:`pretext-plus-editor__dialog-helper-copy`,children:`(optional)`})]}),(0,t.jsx)(`input`,{id:`am-url-title`,type:`text`,className:`pretext-plus-editor__am-input`,placeholder:`My image`,value:we,onChange:e=>Te(e.target.value),disabled:Ee}),De&&(0,t.jsx)(`p`,{className:`pretext-plus-editor__am-error`,children:De}),Ce.trim()&&(0,t.jsx)(`img`,{src:Ce.trim(),alt:`Preview`,className:`pretext-plus-editor__am-url-preview`,onError:e=>{e.currentTarget.style.display=`none`}})]})]})]}),nt=()=>(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__dialog-button`,onClick:Ye,disabled:!Ce.trim()||Ee,children:Ee?`Adding…`:u&&l?`Add to Project`:`Add`}),rt=()=>(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__dialog-button`,onClick:Je,disabled:!_e||pe,children:pe?`Uploading…`:`Add to Project`}),it=e=>(0,t.jsxs)(`div`,{className:`pretext-plus-editor__am-configure`,children:[e&&(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__am-back-btn`,onClick:()=>oe(null),children:`← Back`}),(0,t.jsxs)(`div`,{className:`pretext-plus-editor__dialog-tab-bar pretext-plus-editor__am-sub-tabs`,children:[(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__dialog-tab${ce===`library`?` pretext-plus-editor__dialog-tab--active`:``}`,onClick:()=>le(`library`),children:`Library`}),(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__dialog-tab${ce===`create`?` pretext-plus-editor__dialog-tab--active`:``}`,onClick:()=>le(`create`),children:`Create New`})]}),(0,t.jsxs)(`div`,{className:`pretext-plus-editor__am-configure-body`,children:[ce===`library`&&Ze(`doenet`),ce===`create`&&(0,t.jsxs)(`div`,{className:`pretext-plus-editor__am-create-form`,children:[(0,t.jsx)(`label`,{className:`pretext-plus-editor__dialog-label`,htmlFor:`am-doenet-title`,children:`Title`}),(0,t.jsx)(`input`,{id:`am-doenet-title`,type:`text`,className:`pretext-plus-editor__am-input`,placeholder:`My Activity`,value:ke,onChange:e=>z(e.target.value),disabled:Me,autoFocus:!0}),(0,t.jsx)(`label`,{className:`pretext-plus-editor__dialog-label`,htmlFor:`am-doenet-ref`,children:`Id`}),(0,t.jsx)(`input`,{id:`am-doenet-ref`,type:`text`,className:`pretext-plus-editor__am-input`,placeholder:`my-activity`,value:Ae,onChange:e=>je(e.target.value),disabled:Me}),(0,t.jsxs)(`p`,{className:`pretext-plus-editor__dialog-helper-copy`,children:[`The id is used in the embed code: `,(0,t.jsx)(`code`,{children:b(`doenet`,Ae||`my-activity`)})]}),Pe&&(0,t.jsx)(`p`,{className:`pretext-plus-editor__am-error`,children:Pe}),(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__dialog-button`,onClick:Xe,disabled:!ke.trim()||!Ae.trim()||Me,children:Me?`Creating…`:d?`Create`:`Add`})]})]})]}),at=(e,n,i)=>(0,t.jsxs)(t.Fragment,{children:[(0,t.jsxs)(`div`,{className:`pretext-plus-editor__dialog-header`,children:[(0,t.jsx)(`h2`,{className:`pretext-plus-editor__dialog-title`,children:n}),(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__dialog-close`,onClick:r,"aria-label":`Close`,children:`✕`})]}),(0,t.jsxs)(`div`,{className:`pretext-plus-editor__dialog-content pretext-plus-editor__dialog-content--single`,children:[(0,t.jsx)(`p`,{className:`pretext-plus-editor__dialog-helper-copy pretext-plus-editor__am-resolve-hint`,children:i}),e===`doenet`?it(!1):tt(!1)]}),(0,t.jsxs)(`div`,{className:`pretext-plus-editor__dialog-actions`,children:[(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__dialog-button pretext-plus-editor__dialog-button--secondary`,onClick:r,children:`Cancel`}),e===`image`&&se===`url`&&nt(),e===`image`&&se===`upload`&&l&&rt()]})]});return(0,t.jsx)(`div`,{className:`pretext-plus-editor__dialog-overlay`,onClick:e=>e.target===e.currentTarget&&r(),children:(0,t.jsx)(`div`,{className:`pretext-plus-editor__dialog pretext-plus-editor__dialog--asset-manager`,role:`dialog`,"aria-modal":`true`,"aria-label":`Asset manager`,children:i?(e=>at(e.kind,`Link asset`,(0,t.jsxs)(t.Fragment,{children:[`The reference `,(0,t.jsx)(`code`,{children:e.ref}),` has no asset yet. Choose or create one — the reference in your document will be updated to match.`]})))(i):a?(e=>at(e.kind,`Replace asset`,(0,t.jsxs)(t.Fragment,{children:[`Choose or upload a new asset to replace `,(0,t.jsx)(`code`,{children:e.title}),`. Every place it’s used in your document will show the new asset.`]})))(a):(0,t.jsxs)(t.Fragment,{children:[(0,t.jsxs)(`div`,{className:`pretext-plus-editor__dialog-header`,children:[(0,t.jsx)(`h2`,{className:`pretext-plus-editor__dialog-title`,children:`Assets`}),(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__dialog-close`,onClick:r,"aria-label":`Close`,children:`✕`})]}),Be?(0,t.jsx)(`div`,{className:`pretext-plus-editor__dialog-content pretext-plus-editor__dialog-content--single`,children:$e(Be)}):(0,t.jsxs)(t.Fragment,{children:[(0,t.jsxs)(`div`,{className:`pretext-plus-editor__dialog-tab-bar`,children:[(0,t.jsxs)(`button`,{type:`button`,className:`pretext-plus-editor__dialog-tab${re===`in-document`?` pretext-plus-editor__dialog-tab--active`:``}`,onClick:()=>ie(`in-document`),children:[`Assets`,B.length>0&&(0,t.jsx)(`span`,{className:`pretext-plus-editor__asset-tab-count`,children:B.length})]}),(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__dialog-tab${re===`add`?` pretext-plus-editor__dialog-tab--active`:``}`,onClick:()=>{ie(`add`),oe(null)},children:`Add Asset`})]}),(0,t.jsx)(`div`,{className:`pretext-plus-editor__dialog-content pretext-plus-editor__dialog-content--single`,children:re===`in-document`?Qe():ae===null?et():ae===`image`?tt(!0):it(!0)})]}),(0,t.jsxs)(`div`,{className:`pretext-plus-editor__dialog-actions`,children:[(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__dialog-button pretext-plus-editor__dialog-button--secondary`,onClick:r,children:`Close`}),!Be&&re===`add`&&ae===`image`&&se===`url`&&nt(),!Be&&re===`add`&&ae===`image`&&se===`upload`&&l&&rt()]})]})})})},B5={automaticLayout:!0,minimap:{enabled:!1},wordWrap:`on`,insertSpaces:!0,tabSize:2,padding:{top:10,bottom:10},scrollBeyondLastLine:!1},V5=160,H5={image:`image`,doenet:`interactive`},U5=({asset:e,projectAssets:r,onClose:i,onSave:a,onReplace:o,onDuplicate:s})=>{let c=m6(e=>e.divisions),l=m6(e=>e.activeDivisionId),u=c?.find(e=>e.xmlId===l)?.sourceFormat??`pretext`,d=e.ref??``,[f,p]=(0,n.useState)(e.title),[m,h]=(0,n.useState)(d),[g,_]=(0,n.useState)(e.source??``),[v,y]=(0,n.useState)(!1),[b,x]=(0,n.useState)(!1),[S,C]=(0,n.useState)(null),[w,T]=(0,n.useState)(!1),[E,D]=(0,n.useState)(V5),O=(0,n.useRef)(null);(0,n.useEffect)(()=>{O.current?.scrollTo({top:0})},[]);let k=e=>{let t=()=>D(Math.max(V5,e.getContentHeight()));e.onDidContentSizeChange(t),t()},A=l5(e.kind,m.trim()||d,u),ee=()=>{navigator.clipboard.writeText(A).catch(()=>{}),T(!0),setTimeout(()=>T(!1),2e3)},te=async()=>{let t=m.trim();if(!t){C(`Reference can't be empty — it identifies the asset and is used by every embed of it.`);return}if(t!==d&&r.some(n=>n.kind===e.kind&&n.ref===t)){C(`Reference "${t}" is already used by another asset. Choose a unique reference.`);return}C(null),y(!0);try{await a({...e,title:f.trim()||t,ref:t,source:g},d),i()}catch(e){C(e instanceof Error?e.message:`Failed to save asset.`),y(!1)}},j=async()=>{if(s){C(null),x(!0);try{await s(e)}catch(e){C(e instanceof Error?e.message:`Failed to duplicate asset.`),x(!1)}}},M=v||b,ne=e.kind===`image`&&!!e.url,N=e.kind===`image`&&!!o;return(0,t.jsx)(`div`,{className:`pretext-plus-editor__dialog-overlay`,onClick:e=>e.target===e.currentTarget&&i(),children:(0,t.jsxs)(`div`,{className:`pretext-plus-editor__dialog`,role:`dialog`,"aria-modal":`true`,"aria-label":`Edit asset ${e.title}`,children:[(0,t.jsxs)(`div`,{className:`pretext-plus-editor__dialog-header`,children:[(0,t.jsx)(`h2`,{className:`pretext-plus-editor__dialog-title`,children:`Edit asset`}),(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__dialog-close`,onClick:i,"aria-label":`Close`,children:`✕`})]}),(0,t.jsx)(`div`,{ref:O,className:`pretext-plus-editor__dialog-content pretext-plus-editor__dialog-content--single pretext-plus-editor__am-edit-content`,children:(0,t.jsxs)(`div`,{className:`pretext-plus-editor__dialog-section`,children:[(0,t.jsxs)(`div`,{className:`pretext-plus-editor__am-edit-grid`,children:[(0,t.jsxs)(`div`,{className:`pretext-plus-editor__am-edit-col`,children:[ne&&(0,t.jsx)(`img`,{src:e.url,alt:f,className:`pretext-plus-editor__am-url-preview`,onError:e=>{e.currentTarget.style.display=`none`}}),N&&(0,t.jsx)(`div`,{className:`pretext-plus-editor__am-replace-row`,children:(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__am-action-btn`,onClick:()=>o?.(e),disabled:M,title:`Choose or upload a different asset to use here`,children:`Replace image…`})})]}),(0,t.jsxs)(`div`,{className:`pretext-plus-editor__am-edit-col`,children:[(0,t.jsx)(`label`,{className:`pretext-plus-editor__dialog-label`,htmlFor:`am-edit-title`,children:`Title`}),(0,t.jsx)(`input`,{id:`am-edit-title`,type:`text`,className:`pretext-plus-editor__am-input`,value:f,onChange:e=>p(e.target.value),disabled:M}),(0,t.jsx)(`label`,{className:`pretext-plus-editor__dialog-label`,htmlFor:`am-edit-ref`,children:`Id`}),(0,t.jsx)(`input`,{id:`am-edit-ref`,type:`text`,className:`pretext-plus-editor__am-input`,value:m,onChange:e=>h(e.target.value),disabled:M}),(0,t.jsx)(`p`,{className:`pretext-plus-editor__dialog-helper-copy`,children:`Used in the embed code. Changing it updates every reference to this asset already in your document.`}),(0,t.jsxs)(`div`,{className:`pretext-plus-editor__am-embed-row`,children:[(0,t.jsx)(`code`,{className:`pretext-plus-editor__am-embed-code`,children:A}),(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__am-action-btn${w?` pretext-plus-editor__am-action-btn--done`:``}`,onClick:ee,title:`Copy embed code to clipboard`,children:w?`Copied!`:`Copy embed code`})]})]})]}),(0,t.jsx)(`label`,{className:`pretext-plus-editor__dialog-label`,children:`Asset content`}),(0,t.jsxs)(`p`,{className:`pretext-plus-editor__dialog-helper-copy`,children:[`Inserted verbatim inside the generated `,(0,t.jsx)(`code`,{children:`<${H5[e.kind]}>`}),` element — e.g. `,(0,t.jsx)(`code`,{children:`<shortdescription>...</shortdescription>`}),`.`]}),(0,t.jsx)(`div`,{className:`pretext-plus-editor__dialog-editor pretext-plus-editor__am-edit-editor`,style:{height:E},children:(0,t.jsx)(Yn,{options:{...B5,readOnly:M},height:`100%`,language:`xml`,value:g,onMount:k,onChange:e=>_(e??``)})}),S&&(0,t.jsx)(`p`,{className:`pretext-plus-editor__am-error`,children:S})]})}),(0,t.jsxs)(`div`,{className:`pretext-plus-editor__dialog-actions`,children:[s&&(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__dialog-button pretext-plus-editor__dialog-button--secondary pretext-plus-editor__am-duplicate-btn`,onClick:j,disabled:M,title:`Create a copy of this asset under a new reference`,children:b?`Duplicating…`:`Duplicate`}),(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__dialog-button pretext-plus-editor__dialog-button--secondary`,onClick:i,disabled:M,children:`Cancel`}),(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__dialog-button`,onClick:te,disabled:M,children:v?`Saving…`:`Save`})]})]})})},W5=e=>{let n=m6(e=>e.showFullPreview),r=m6(e=>e.setShowFullPreview),i=m6(e=>e.title),a=m6(e=>e.updateTitle),o;return o=e.showPreviewModeToggle===!1?null:(0,t.jsxs)(`label`,{className:`pretext-plus-editor__preview-toggle`,children:[(0,t.jsx)(`input`,{type:`checkbox`,checked:!n,onChange:()=>r(!n),className:`pretext-plus-editor__preview-toggle-input`}),(0,t.jsxs)(`div`,{className:`pretext-plus-editor__preview-toggle-container`,children:[(0,t.jsx)(`span`,{className:`pretext-plus-editor__preview-toggle-label`,children:`Simple`}),(0,t.jsx)(`span`,{className:`pretext-plus-editor__preview-toggle-slider ${n?`pretext-plus-editor__preview-toggle-slider--active`:``}`,children:(0,t.jsx)(`span`,{className:`pretext-plus-editor__preview-toggle-dot ${n?`pretext-plus-editor__preview-toggle-dot--active`:``}`})}),(0,t.jsx)(`span`,{className:`pretext-plus-editor__preview-toggle-label`,children:`Full`})]}),(0,t.jsx)(`span`,{className:`pretext-plus-editor__preview-toggle-caption`,children:`Preview Mode`}),(0,t.jsx)(`input`,{type:`checkbox`,checked:n,onChange:()=>r(!n),className:`pretext-plus-editor__preview-toggle-input`})]}),(0,t.jsxs)(`div`,{className:`pretext-plus-editor__menu-bar`,children:[(0,t.jsx)(`div`,{className:`pretext-plus-editor__menu-left`,children:(0,t.jsxs)(`label`,{className:`pretext-plus-editor__title-label`,children:[`Title`,` `,(0,t.jsx)(`input`,{className:`pretext-plus-editor__title-input`,type:`text`,value:i,onChange:e=>a(e.target.value)})]})}),(0,t.jsxs)(`div`,{className:`pretext-plus-editor__menu-right`,children:[e.onSaveButton&&(0,t.jsx)(`button`,{className:`pretext-plus-editor__button pretext-plus-editor__button--save`,onClick:e.onSaveButton,children:e.saveButtonLabel||`Save`}),e.onCancelButton&&(0,t.jsx)(`button`,{className:`pretext-plus-editor__button pretext-plus-editor__button--cancel`,onClick:e.onCancelButton,children:e.cancelButtonLabel||`Cancel`}),(0,t.jsx)(h6,{label:`Give feedback`,context:`main-editor`}),o]})]})},G5={pretext:`PreTeXt`,latex:`LaTeX`,markdown:`Markdown`},K5={book:`bk`,article:`art`,slideshow:`slides`,part:`pt`,chapter:`ch`,section:`sec`,subsection:`subsec`,subsubsection:`subsubsec`,introduction:`intro`,conclusion:`conc`,worksheet:`ws`,handout:`ho`,exercises:`ex`,references:`ref`,glossary:`gloss`,solutions:`sol`,"reading-questions":`rq`,paragraphs:`para`},q5={book:`Book`,article:`Article`,slideshow:`Slideshow`,part:`Part`,chapter:`Chapter`,section:`Section`,worksheet:`Worksheet`,handout:`Handout`,exercises:`Exercises`,references:`References`,glossary:`Glossary`,solutions:`Solutions`,"reading-questions":`Reading Questions`,introduction:`Introduction`,conclusion:`Conclusion`,subsection:`Subsection`,subsubsection:`Subsubsection`,paragraphs:`Paragraphs`},J5=[`article`,`book`],Y5=[`part`,`chapter`,`section`,`subsection`,`subsubsection`,`worksheet`,`handout`,`exercises`,`references`,`glossary`,`solutions`,`reading-questions`,`paragraphs`],X5=[`worksheet`,`handout`,`exercises`,`references`,`glossary`,`solutions`,`reading-questions`,`paragraphs`],Z5={book:[`part`,`chapter`],article:[`section`,...X5],slideshow:[`section`],part:[`chapter`],chapter:[`section`,...X5],section:[`subsection`,...X5],subsection:[`subsubsection`,...X5],subsubsection:[...X5],worksheet:[`paragraphs`],handout:[`paragraphs`]};function Q5(e){return e?Z5[e]??Y5:Y5}function $5(e,t){let n=K5[e]??`sec`,r=I8(t);return r?`${n}-${r}`:n}var e7=({draft:e,isNew:r=!1,isRoot:i=!1,parentType:a=null,onDraftChange:o,onCommit:s,onCancel:c})=>{let l=Q5(a),u=J5.includes(e.type)?J5:[e.type,...J5],d=(0,n.useRef)(r);return(0,n.useEffect)(()=>{i||l.length>0&&!l.includes(e.type)&&o({...e,type:l[0]})},[e,i,o,l]),(0,t.jsxs)(`div`,{className:`pretext-plus-editor__toc-edit-form`,children:[(0,t.jsxs)(`label`,{className:`pretext-plus-editor__toc-edit-field`,children:[(0,t.jsx)(`span`,{children:`Title`}),(0,t.jsx)(`input`,{type:`text`,value:e.title,onChange:t=>{let n=t.target.value;o(d.current?{...e,title:n,xmlId:$5(e.type,n)}:{...e,title:n})},onKeyDown:e=>{e.key===`Enter`&&s(),e.key===`Escape`&&c()},autoFocus:!0})]}),r?(0,t.jsxs)(`label`,{className:`pretext-plus-editor__toc-edit-field`,children:[(0,t.jsx)(`span`,{children:`Format`}),(0,t.jsx)(`select`,{value:e.sourceFormat,onChange:t=>o({...e,sourceFormat:t.target.value}),children:Object.keys(G5).map(e=>(0,t.jsx)(`option`,{value:e,children:G5[e]},e))})]}):void 0,(0,t.jsxs)(`label`,{className:`pretext-plus-editor__toc-edit-field`,children:[(0,t.jsx)(`span`,{children:`Type`}),(0,t.jsx)(`select`,{value:e.type,onChange:t=>{let n=t.target.value;o(d.current?{...e,type:n,xmlId:$5(n,e.title)}:{...e,type:n})},children:(i?u:l).map(e=>(0,t.jsx)(`option`,{value:e,children:q5[e]},e))})]}),(0,t.jsxs)(`label`,{className:`pretext-plus-editor__toc-edit-field`,children:[(0,t.jsx)(`span`,{children:`Id`}),(0,t.jsx)(`input`,{type:`text`,value:e.xmlId,placeholder:`unique identifier`,onChange:t=>{d.current=!1,o({...e,xmlId:t.target.value})}})]}),(0,t.jsxs)(`div`,{className:`pretext-plus-editor__toc-edit-actions`,children:[(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__toc-edit-save`,onClick:s,children:`Save`}),(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__toc-edit-cancel`,onClick:c,children:`Cancel`})]})]})},t7=({items:e})=>{let[i,a]=(0,n.useState)(!1),[o,s]=(0,n.useState)(null),c=(0,n.useRef)(null),l=(0,n.useRef)(null);(0,n.useEffect)(()=>{if(!i)return;let e=e=>{let t=e.target;c.current&&!c.current.contains(t)&&l.current&&!l.current.contains(t)&&a(!1)},t=e=>{e.key===`Escape`&&a(!1)},n=()=>a(!1);return document.addEventListener(`mousedown`,e),document.addEventListener(`keydown`,t),window.addEventListener(`scroll`,n,!0),window.addEventListener(`resize`,n),()=>{document.removeEventListener(`mousedown`,e),document.removeEventListener(`keydown`,t),window.removeEventListener(`scroll`,n,!0),window.removeEventListener(`resize`,n)}},[i]),(0,n.useLayoutEffect)(()=>{if(!i)return;let e=c.current,t=l.current;if(!e||!t)return;let n=e.getBoundingClientRect(),r=t.getBoundingClientRect(),a=window.innerHeight-n.bottom,o=r.height+4>a&&n.top>a;s({right:Math.max(4,window.innerWidth-n.right),...o?{bottom:window.innerHeight-n.top+3}:{top:n.bottom+3}})},[i]);let u=e=>{a(!1),e()};return(0,t.jsxs)(`div`,{ref:c,className:`pretext-plus-editor__toc-div-menu`,children:[(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__toc-div-menu-trigger`,onClick:e=>{e.stopPropagation(),a(e=>(e||s(null),!e))},"aria-haspopup":`true`,"aria-expanded":i,title:`More options`,children:`⋮`}),i&&(0,r.createPortal)((0,t.jsx)(`div`,{ref:l,className:`pretext-plus-editor__toc-div-menu-popup pretext-plus-editor__toc-div-menu-popup--portal`,style:{visibility:o?`visible`:`hidden`,right:o?.right,top:o?.top,bottom:o?.bottom},children:e.map(e=>(0,t.jsx)(`button`,{type:`button`,className:[`pretext-plus-editor__toc-div-menu-item`,e.danger?`pretext-plus-editor__toc-div-menu-item--danger`:``].filter(Boolean).join(` `),onClick:()=>u(e.onClick),children:e.label},e.label))}),document.body)]})},n7=({division:e,depth:n,isActive:r,hasChildren:i,isExpanded:a,onToggleExpand:o,editDraft:s,onSelect:c,onDraftChange:l,onEditCommit:u,onEditCancel:d,menuItems:f,isNew:p=!1,isRoot:m=!1,parentType:h=null})=>{let g=s!==null,_=e.type===`introduction`||e.type===`conclusion`?q5[e.type]:null;return(0,t.jsxs)(`li`,{className:[`pretext-plus-editor__toc-item`,`pretext-plus-editor__toc-item--${e.type}`,r?`pretext-plus-editor__toc-item--active`:``,g?`pretext-plus-editor__toc-item--editing`:``].filter(Boolean).join(` `),children:[(0,t.jsxs)(`div`,{className:`pretext-plus-editor__toc-item-row`,style:n>0?{paddingLeft:`${n*14}px`}:void 0,children:[(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__toc-expand-btn`,onClick:o,"aria-label":a?`Collapse`:`Expand`,tabIndex:i?0:-1,style:{visibility:i?`visible`:`hidden`},children:a?`▾`:`▸`}),(0,t.jsxs)(`button`,{type:`button`,className:`pretext-plus-editor__toc-select`,onClick:c,"aria-current":r?`true`:void 0,title:q5[e.type]??e.type,children:[(0,t.jsx)(`span`,{className:`pretext-plus-editor__toc-title`,children:e.title||_||(0,t.jsx)(`em`,{children:`Untitled`})}),e.xmlId&&(0,t.jsx)(`span`,{className:`pretext-plus-editor__toc-xmlid`,children:e.xmlId})]}),(0,t.jsx)(`div`,{className:`pretext-plus-editor__toc-actions`,children:(0,t.jsx)(t7,{items:f})})]}),g&&s&&(0,t.jsx)(e7,{draft:s,isNew:p,isRoot:m,parentType:h,onDraftChange:l,onCommit:u,onCancel:d})]})},r7=({onOpenAssetPicker:e})=>{let r=m6(e=>e.divisions),i=m6(e=>e.rootDivisionId),a=m6(e=>e.activeDivisionId),o=m6(e=>e.projectAssets)??[],s=m6(e=>e.selectSection),c=m6(e=>e.addSection),l=m6(e=>e.removeSection),u=m6(e=>e.divisionContentChange),d=m6(e=>e.insertAtCursor),f=m6(e=>e.openAssetEditor),p=m6(e=>e.openAssetResolver),m=m6(e=>e.removeAsset),h=m6(e=>e.removeAssetRefFromDocument),g=m6(e=>e.duplicateAsset),_=m6(e=>e.hasAssetDuplicate),v=m6(e=>e.startSectionEdit),y=m6(e=>e.setEditDraft),b=m6(e=>e.commitSectionEdit),x=m6(e=>e.cancelSectionEdit),S=m6(e=>e.editingId),C=m6(e=>e.editDraft),w=m6(e=>e.editingIsNew),T=r?r.find(e=>e.xmlId===i)??r.find(e=>e.type===`book`||e.type===`article`||e.type===`slideshow`)??r[0]??null:null,E=T&&r?b5(r,T.xmlId):[],D=T&&r?x5(r,T.xmlId):[],O=P5(r,o),k=L5.map(e=>({kind:e,rows:O.filter(t=>t.kind===e)})).filter(e=>e.rows.length>0),[A,ee]=(0,n.useState)(!1),[te,j]=(0,n.useState)(null),M=async e=>{if(!(!e.asset||te)){j(e.ref);try{await g(e.asset)}finally{j(null)}}},[ne,N]=(0,n.useState)(new Set),re=e=>!ne.has(e),ie=e=>{N(t=>{let n=new Set(t);return n.has(e)?n.delete(e):n.add(e),n})},[ae,oe]=(0,n.useState)(a);if(a!==ae&&(oe(a),a&&T)){let e=new Map(E.map(e=>[e.division.xmlId,e])),t=new Set;t.add(T.xmlId);let n=a;for(;n;){let r=e.get(n);if(!r?.parentXmlId)break;t.add(r.parentXmlId),n=r.parentXmlId}N(e=>{if([...t].every(t=>!e.has(t)))return e;let n=new Set(e);return t.forEach(e=>n.delete(e)),n})}let se=new Set(E.map(e=>e.parentXmlId).filter(Boolean)),P=[];if(T){let e=new Set;re(T.xmlId)&&e.add(T.xmlId);for(let t of E)t.parentXmlId&&e.has(t.parentXmlId)&&(P.push(t),re(t.division.xmlId)&&e.add(t.division.xmlId))}let ce=(e,t)=>{if(!r)return;let n=r.find(e=>e.xmlId===t);n&&u(n.xmlId,p5(n.source,e))},le=(e,t)=>{if(window.confirm(`Delete "${e.title||`Untitled`}"? This permanently removes the division.`)){if(t&&r){let n=r.find(e=>e.xmlId===t);n&&u(n.xmlId,p5(n.source,e.xmlId))}l(e.xmlId)}},ue=r?.find(e=>e.xmlId===a)?.sourceFormat??`pretext`,de=e=>{d(ue===`markdown`?`::${e.type}{ref="${e.xmlId}"}`:ue===`latex`?`\\plus{${e.type}}{${e.xmlId}}`:`<plus:${e.type} ref="${e.xmlId}"/>`)},F=e=>{T&&u(T.xmlId,f5(T.source,e.xmlId,e.type,null,T.sourceFormat))},fe=e=>e&&r?.find(t=>t.xmlId===e)?.type||null,pe=e=>e.status===`unlinked`?p(e.kind,e.ref):f(e.kind,e.ref),me=(e,t)=>{navigator.clipboard.writeText(l5(e,t,ue)).catch(()=>{})},he=e=>{let t=[{label:e.status===`unlinked`?`Link / create asset`:`Edit asset`,onClick:()=>pe(e)},{label:`Copy embed code`,onClick:()=>me(e.kind,e.ref)}];return _&&e.asset&&t.push({label:`Duplicate asset`,onClick:()=>M(e)}),e.status===`unlinked`?t.push({label:`Remove from document`,onClick:()=>h(e.kind,e.ref),danger:!0}):e.asset&&t.push({label:`Remove from project`,onClick:()=>{e.inDocument&&!window.confirm(`Remove "${e.asset.title}" from the project? This also deletes its ${e.inDocument?`reference(s)`:`reference`} from the document.`)||(m(e.asset),h(e.kind,e.ref))},danger:!0}),t};return(0,t.jsxs)(t.Fragment,{children:[(0,t.jsxs)(`ul`,{className:`pretext-plus-editor__toc-list`,role:`list`,children:[T&&(0,t.jsx)(n7,{division:T,depth:0,isActive:a===T.xmlId,hasChildren:se.has(T.xmlId),isExpanded:re(T.xmlId),onToggleExpand:()=>ie(T.xmlId),editDraft:S===T.xmlId?C:null,onSelect:()=>s(T.xmlId),onDraftChange:y,onEditCommit:b,onEditCancel:x,menuItems:[{label:`Edit properties`,onClick:()=>v(T)},...J8(T.sourceFormat)?[{label:`Add new division`,onClick:()=>c(T.xmlId)}]:[]],isNew:S===T.xmlId&&w,isRoot:!0}),P.length===0&&T&&re(T.xmlId)&&(0,t.jsx)(`li`,{className:`pretext-plus-editor__toc-no-sections`,children:(0,t.jsx)(`span`,{children:`No placed divisions`})}),P.map(e=>(0,t.jsx)(n7,{division:e.division,depth:e.depth+1,isActive:a===e.division.xmlId,hasChildren:se.has(e.division.xmlId),isExpanded:re(e.division.xmlId),onToggleExpand:()=>ie(e.division.xmlId),editDraft:S===e.division.xmlId?C:null,onSelect:()=>s(e.division.xmlId),onDraftChange:y,onEditCommit:b,onEditCancel:x,menuItems:[{label:`Edit properties`,onClick:()=>v(e.division)},...J8(e.division.sourceFormat)?[{label:`Add new division`,onClick:()=>c(e.division.xmlId)}]:[],{label:`Remove from document`,onClick:()=>ce(e.division.xmlId,e.parentXmlId)},{label:`Delete from project`,onClick:()=>le(e.division,e.parentXmlId),danger:!0}],isNew:S===e.division.xmlId&&w,parentType:fe(e.parentXmlId)},e.division.xmlId))]}),D.length>0&&(0,t.jsxs)(`div`,{className:`pretext-plus-editor__toc-orphans`,children:[(0,t.jsx)(`div`,{className:`pretext-plus-editor__toc-orphans-heading`,children:`Unplaced divisions`}),(0,t.jsx)(`ul`,{className:`pretext-plus-editor__toc-list`,children:D.map(e=>{let i=r?b5(r,e.xmlId):[],o=new Set(i.map(e=>e.parentXmlId).filter(Boolean));return(0,t.jsxs)(n.Fragment,{children:[(0,t.jsx)(n7,{division:e,depth:0,isActive:a===e.xmlId,hasChildren:o.has(e.xmlId),isExpanded:re(e.xmlId),onToggleExpand:()=>ie(e.xmlId),editDraft:S===e.xmlId?C:null,onSelect:()=>s(e.xmlId),onDraftChange:y,onEditCommit:b,onEditCancel:x,menuItems:[{label:`Edit properties`,onClick:()=>v(e)},{label:`Place in document`,onClick:()=>F(e)},{label:`Insert at cursor`,onClick:()=>de(e)},{label:`Delete from project`,onClick:()=>le(e,null),danger:!0}],parentType:null}),re(e.xmlId)&&i.map(e=>(0,t.jsx)(n7,{division:e.division,depth:e.depth+1,isActive:a===e.division.xmlId,hasChildren:o.has(e.division.xmlId),isExpanded:re(e.division.xmlId),onToggleExpand:()=>ie(e.division.xmlId),editDraft:S===e.division.xmlId?C:null,onSelect:()=>s(e.division.xmlId),onDraftChange:y,onEditCommit:b,onEditCancel:x,menuItems:[{label:`Edit properties`,onClick:()=>v(e.division)},{label:`Insert at cursor`,onClick:()=>de(e.division)},{label:`Delete from project`,onClick:()=>le(e.division,e.parentXmlId),danger:!0}],parentType:fe(e.parentXmlId)},e.division.xmlId))]},e.xmlId)})})]}),(0,t.jsxs)(`div`,{className:`pretext-plus-editor__toc-assets`,children:[(0,t.jsx)(`div`,{className:`pretext-plus-editor__toc-assets-header`,children:(0,t.jsxs)(`button`,{type:`button`,className:`pretext-plus-editor__toc-assets-toggle`,onClick:()=>ee(e=>!e),"aria-expanded":A,children:[(0,t.jsx)(`span`,{className:`pretext-plus-editor__toc-assets-chevron`,children:A?`▾`:`▸`}),(0,t.jsx)(`span`,{children:`Assets`}),O.length>0&&(0,t.jsx)(`span`,{className:`pretext-plus-editor__toc-assets-count`,children:O.length})]})}),A&&(0,t.jsx)(`div`,{className:`pretext-plus-editor__toc-assets-body`,children:O.length===0?(0,t.jsxs)(`p`,{className:`pretext-plus-editor__toc-assets-empty`,children:[`No assets in this project yet.`,` `,e&&(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__toc-assets-add-link`,onClick:e,children:`Add one`})]}):(0,t.jsx)(`div`,{className:`pretext-plus-editor__toc-assets-groups`,children:k.map(({kind:e,rows:n})=>(0,t.jsxs)(`div`,{children:[(0,t.jsx)(`div`,{className:`pretext-plus-editor__toc-assets-group-header`,children:I5[e]}),(0,t.jsx)(`ul`,{className:`pretext-plus-editor__toc-assets-list`,children:n.map(e=>(0,t.jsxs)(`li`,{className:[`pretext-plus-editor__toc-asset-item`,e.status===`unlinked`?`pretext-plus-editor__toc-asset-item--unlinked`:``,e.status===`unused`?`pretext-plus-editor__toc-asset-item--unused`:``,te===e.ref?`pretext-plus-editor__toc-asset-item--busy`:``].filter(Boolean).join(` `),children:[e.asset?.url?(0,t.jsx)(`img`,{src:e.asset.url,className:`pretext-plus-editor__toc-asset-img`,onClick:()=>pe(e)}):(0,t.jsx)(`span`,{className:`pretext-plus-editor__toc-asset-img pretext-plus-editor__toc-asset-img--placeholder`,onClick:()=>pe(e),title:e.status===`unlinked`?`No asset — click to link`:void 0,"aria-hidden":`true`,children:e.status===`unlinked`?`⚠`:`🖼`}),(0,t.jsxs)(`button`,{type:`button`,className:`pretext-plus-editor__toc-asset-name`,onClick:()=>pe(e),title:e.status===`unlinked`?`No asset for this reference — click to link or create one`:`Edit asset`,children:[(0,t.jsx)(`span`,{className:`pretext-plus-editor__toc-asset-label`,children:e.asset?.title??e.ref}),(0,t.jsx)(`span`,{className:`pretext-plus-editor__toc-asset-filename`,children:e.status===`unlinked`?`${e.ref} — needs asset`:e.status===`unused`?`${e.ref} — not placed`:e.ref})]}),(0,t.jsx)(`div`,{className:`pretext-plus-editor__toc-actions`,children:te===e.ref?(0,t.jsx)(`span`,{className:`pretext-plus-editor__toc-asset-spinner`,role:`status`,"aria-label":`Duplicating asset`,title:`Duplicating…`}):(0,t.jsx)(t7,{items:he(e)})})]},e.ref))})]},e))})})]}),e&&(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__toc-assets-btn`,onClick:e,children:`Manage Assets`})]})},i7=({isCollapsed:e,onToggleCollapse:n,onOpenAssetPicker:r})=>e?(0,t.jsx)(`div`,{className:`pretext-plus-editor__toc pretext-plus-editor__toc--collapsed`,children:(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__toc-toggle`,onClick:n,"aria-label":`Expand table of contents`,title:`Expand table of contents`,children:`☰`})}):(0,t.jsxs)(`div`,{className:`pretext-plus-editor__toc`,children:[(0,t.jsxs)(`div`,{className:`pretext-plus-editor__toc-header`,children:[(0,t.jsx)(`span`,{className:`pretext-plus-editor__toc-heading`,children:`Contents`}),(0,t.jsx)(`div`,{className:`pretext-plus-editor__toc-header-actions`,children:(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__toc-toggle`,onClick:n,"aria-label":`Collapse table of contents`,title:`Collapse table of contents`,children:`✕`})})]}),(0,t.jsx)(r7,{onOpenAssetPicker:r})]}),a7=class extends n.Component{state={error:null};static getDerivedStateFromError(e){return{error:e}}componentDidCatch(e,t){this.props.onError?.(e,t),console.error(`ErrorBoundary caught an error:`,e,t)}componentDidUpdate(e){if(this.state.error===null)return;let{resetKeys:t}=this.props,n=e.resetKeys;t&&n&&(t.length!==n.length||t.some((e,t)=>!Object.is(e,n[t])))&&this.reset()}reset=()=>{this.setState({error:null})};render(){let{error:e}=this.state;return e===null?this.props.children:this.props.fallback?this.props.fallback(e,this.reset):(0,t.jsxs)(`div`,{className:`pretext-plus-editor__error-boundary`,role:`alert`,children:[(0,t.jsx)(`strong`,{children:`Something went wrong rendering this view.`}),(0,t.jsx)(`p`,{className:`pretext-plus-editor__error-boundary-detail`,children:e.message}),(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__error-boundary-retry`,onClick:this.reset,children:`Try again`})]})}},o7=(e,t)=>e.kind===t.kind&&e.ref===t.ref;function s7(e){let t=()=>{},n={cbs:{selectDivision:t,addDivision:t,removeDivision:t,updateDivision:t,divisionContentChange:t,handleDivisionContentChange:t,assetInsert:t,updateTitle:t}};return{store:u6()((t,r)=>({source:e.source,sourceFormat:e.sourceFormat,projectAssets:e.projectAssets,libraryAssets:void 0,title:e.title,docinfo:e.docinfo,commonDocinfo:e.commonDocinfo,useCommonDocinfo:e.useCommonDocinfo,projectType:e.projectType,projectUrl:void 0,divisions:e.divisions,rootDivisionId:void 0,activeDivisionId:e.activeDivisionId,canConvertToPretext:!0,activeEditorSource:e.source,hasFeedback:!1,hasAssetDuplicate:!1,isTocCollapsed:!1,showFullPreview:!0,isNarrowScreen:typeof window<`u`?window.innerWidth<800:!1,activeTab:`editor`,isLatexDialogOpen:!1,isConvertDialogOpen:!1,isDocinfoEditorOpen:!1,isAssetPickerOpen:!1,isFullSourceOpen:!1,editingId:null,editDraft:null,editingIsNew:!1,editingAssetRef:null,assetResolveTarget:null,syncState:e=>t(e),applyExternalUpdate:e=>t(e),setDivisionContent:(e,n)=>t(t=>{if(!t.divisions)return{};let r=!1,i=t.divisions.map(t=>t.xmlId===e&&t.source!==n?(r=!0,{...t,source:n}):t);return r?{divisions:i}:{}}),patchDivision:(e,n)=>t(t=>t.divisions?{divisions:t.divisions.map(t=>t.xmlId===e?{...t,...n.id!==void 0&&{id:n.id},...n.title!==void 0&&{title:n.title},...n.type!==void 0&&{type:n.type},...n.xmlId!=null&&{xmlId:n.xmlId},...n.sourceFormat!==void 0&&{sourceFormat:n.sourceFormat}}:t)}:{}),addDivisionToPool:e=>t(t=>{let n=t.divisions??[];return n.some(t=>t.xmlId===e.xmlId)?{}:{divisions:[...n,e]}}),removeDivisionFromPool:e=>t(t=>({divisions:(t.divisions??[]).filter(t=>t.xmlId!==e)})),setActiveDivisionId:e=>t({activeDivisionId:e}),setTitle:e=>t({title:e}),setDocinfo:({docinfo:e,commonDocinfo:n,useCommonDocinfo:r})=>t({docinfo:e,commonDocinfo:n,useCommonDocinfo:r}),setShowFullPreview:e=>t({showFullPreview:e}),setActiveTab:e=>t({activeTab:e}),setIsNarrowScreen:e=>t({isNarrowScreen:e}),setIsTocCollapsed:e=>t(t=>({isTocCollapsed:typeof e==`function`?e(t.isTocCollapsed):e})),openModal:e=>t({[e]:!0}),closeModal:e=>t({[e]:!1}),selectSection:e=>n.cbs.selectDivision(e),addSection:e=>n.cbs.addDivision(e),removeSection:e=>n.cbs.removeDivision(e),updateSection:(e,t)=>n.cbs.updateDivision(e,t),divisionContentChange:(e,t)=>n.cbs.divisionContentChange?.(e,t),startSectionEdit:(e,n)=>{let{xmlId:r,label:i}=e.sourceFormat===`markdown`?G8(e.source)??{xmlId:e.xmlId,label:``}:e.sourceFormat===`latex`?{xmlId:D8(e.source)||e.xmlId,label:``}:(()=>{let t=P8(e.source);return{xmlId:t.xmlId||e.xmlId,label:t.label}})();t({editingId:e.xmlId,editDraft:{title:e.title,type:e.type,xmlId:r,label:i,sourceFormat:e.sourceFormat},editingIsNew:n?.isNew??!1})},setEditDraft:e=>t({editDraft:e}),commitSectionEdit:()=>{let{editingId:e,editDraft:i,divisions:a}=r();if(e&&i){let t=(a??[]).find(t=>t.xmlId===e),r=null;if(t){let t=F8(i.xmlId);if(!t){window.alert(`xml:id can't be empty — it identifies the division and is used by references to it.`);return}if((a??[]).some(n=>n.xmlId!==e&&n.xmlId===t)){window.alert(`xml:id "${t}" is already used by another division. Choose a unique id.`);return}r=t}n.cbs.updateDivision(e,{title:i.title.trim()||void 0,type:i.type,xmlId:r,label:i.label.trim()||null,sourceFormat:i.sourceFormat})}t({editingId:null,editDraft:null,editingIsNew:!1})},cancelSectionEdit:()=>t({editingId:null,editDraft:null,editingIsNew:!1}),insertAsset:e=>n.cbs.assetInsert(e),insertAtCursor:e=>n.cbs.insertContentAtCursor?.(e),openAssetEditor:(e,n)=>t({editingAssetRef:{kind:e,ref:n}}),closeAssetEditor:()=>t({editingAssetRef:null}),openAssetResolver:(e,n)=>t({assetResolveTarget:{kind:e,ref:n}}),closeAssetResolver:()=>t({assetResolveTarget:null}),removeAsset:e=>n.cbs.assetRemove?.(e),removeAssetRefFromDocument:(e,t)=>n.cbs.assetRefRemove?.(e,t),duplicateAsset:async e=>{await n.cbs.assetDuplicate?.(e)},setProjectAssets:e=>t({projectAssets:e}),addAssetToPool:e=>t(t=>{let n=t.projectAssets??[];return n.some(t=>o7(t,e))?{}:{projectAssets:[...n,e]}}),updateAssetInPool:e=>t(t=>{let n=t.projectAssets??[];return n.some(t=>o7(t,e))?{projectAssets:n.map(t=>o7(t,e)?e:t)}:{projectAssets:[...n,e]}}),renameAssetInPool:(e,n,r)=>t(t=>({projectAssets:[...(t.projectAssets??[]).filter(t=>!(t.kind===e&&t.ref===n)&&!o7(t,r)),r]})),removeAssetFromPool:e=>t(t=>({projectAssets:(t.projectAssets??[]).filter(t=>!o7(t,e))})),updateTitle:e=>n.cbs.updateTitle(e),feedbackSubmit:e=>n.cbs.feedbackSubmit?.(e)})),bindCallbacks:e=>{n.cbs=e}}}function c7({store:e,children:n}){return(0,t.jsx)(p6.Provider,{value:e,children:n})}var l7=(e,t)=>e.find(e=>t?e.xmlId===t:e.type===`book`||e.type===`article`||e.type===`slideshow`)??e[0]??null,u7=e=>{let[r]=(0,n.useState)(()=>{let t=l7(e.divisions,e.rootDivisionId),n=N5(e.divisions,t?.xmlId,e.projectType,e.title),r=n.find(e=>e.xmlId===t?.xmlId)??t,i=e.activeDivisionId??r?.xmlId??null,a=n.find(e=>e.xmlId===i)??r;return s7({source:a?.source??``,sourceFormat:a?.sourceFormat??`pretext`,title:e.title||r?.title||`Document Title`,docinfo:e.docinfo??``,commonDocinfo:e.commonDocinfo??``,useCommonDocinfo:e.useCommonDocinfo??!1,projectType:e.projectType,divisions:n,activeDivisionId:i,projectAssets:e.projectAssets})});return(0,t.jsx)(c7,{store:r.store,children:(0,t.jsx)(d7,{...e,bindCallbacks:r.bindCallbacks})})},d7=e=>{let{bindCallbacks:r}=e,i=m6(e=>e.showFullPreview),a=m6(e=>e.isNarrowScreen),o=m6(e=>e.setIsNarrowScreen),s=m6(e=>e.activeTab),c=m6(e=>e.setActiveTab),l=m6(e=>e.isTocCollapsed),u=m6(e=>e.setIsTocCollapsed),d=m6(e=>e.isLatexDialogOpen),f=m6(e=>e.isConvertDialogOpen),p=m6(e=>e.isDocinfoEditorOpen),m=m6(e=>e.isAssetPickerOpen),h=m6(e=>e.isFullSourceOpen),g=m6(e=>e.editingAssetRef),_=m6(e=>e.openAssetEditor),v=m6(e=>e.closeAssetEditor),y=m6(e=>e.assetResolveTarget),b=m6(e=>e.closeAssetResolver),[x,S]=(0,n.useState)(null),C=m6(e=>e.openModal),w=m6(e=>e.closeModal),T=m6(e=>e.syncState),E=m6(e=>e.projectAssets),D=m6(e=>e.addAssetToPool),O=m6(e=>e.updateAssetInPool),k=m6(e=>e.renameAssetInPool),A=m6(e=>e.removeAssetFromPool),ee=g?E?.find(e=>e.kind===g.kind&&e.ref===g.ref):void 0,te=m6(e=>e.divisions),j=(0,n.useMemo)(()=>te??[],[te]),M=m6(e=>e.activeDivisionId),ne=m6(e=>e.title),N=m6(e=>e.docinfo),re=m6(e=>e.commonDocinfo),ie=m6(e=>e.useCommonDocinfo),ae=m6(e=>e.applyExternalUpdate),oe=m6(e=>e.setDivisionContent),se=m6(e=>e.patchDivision),P=m6(e=>e.addDivisionToPool),ce=m6(e=>e.removeDivisionFromPool),le=m6(e=>e.setActiveDivisionId),ue=m6(e=>e.startSectionEdit),de=m6(e=>e.setTitle),F=m6(e=>e.setDocinfo),fe=m6(e=>e.editingId),pe=m6(e=>e.editingIsNew),me=(0,n.useRef)(null),he=(0,n.useRef)(null),ge=(0,n.useRef)(!1);(0,n.useEffect)(()=>{fe&&pe?ge.current=!0:ge.current&&(ge.current=!1,he.current?.focus())},[fe,pe]);let I=l7(j,e.rootDivisionId),_e=j.find(e=>e.xmlId===M)??j[0]??null,ve=_e?.sourceFormat??`pretext`,ye=_e?.source??``,be=(t,n,r,i)=>{oe(t,n),e.onContentChange({xmlId:t,source:n,sourceFormat:r,pretextSource:r===`pretext`?n:void 0,...i})},xe=(t,n)=>{se(t,n),e.onDivisionUpdate?.(t,n)},Se=(e,t)=>{let n=j.find(t=>t.xmlId===e);if(!n){xe(e,t);return}let r;if(t.sourceFormat!==void 0&&t.sourceFormat!==n.sourceFormat){let e=t.type??n.type,i=t.title??n.title,a=t.xmlId||n.xmlId;r={...n,sourceFormat:t.sourceFormat,type:e,title:i,xmlId:a,source:d5(e,t.sourceFormat,i,a)}}else r=n.sourceFormat===`markdown`?K8(n,t):n.sourceFormat===`latex`?O8(n,t):R8(n,t);let i=r.xmlId;if(r.source!==n.source&&be(n.xmlId,r.source,r.sourceFormat),xe(e,{...t,xmlId:i}),i!==n.xmlId||r.type!==n.type){let e=g5(j,n.xmlId);if(e){let t=h5(e.source,n.xmlId,i,r.type);t!==e.source&&be(e.xmlId,t,e.sourceFormat)}}i!==n.xmlId&&we(i)},Ce=t=>{P(t),(e.onDivisionAdd?.(t))?.then(e=>se(t.xmlId,{id:e})).catch(()=>{})},L=t=>{ce(t),e.onDivisionRemove?.(t)},we=t=>{le(t),e.onDivisionSelect?.(t)},Te=(0,n.useMemo)(()=>{if(!_e||ve===`pretext`)return;if(ve===`latex`)return k8(_e)??void 0;let e=o6(ye,ve);return e.pretextError?void 0:e.pretextSource},[_e,ve,ye]),Ee=e=>{if(!_e)return;let t=e||``;if(ve===`pretext`){let e=L8(t);e&&e.xmlId!==_e.xmlId&&(t=R8({..._e,source:t,title:e.title,type:e.type},{xmlId:_e.xmlId,label:e.label||null}).source)}else if(ve===`markdown`){let e=G8(t);e&&e.xmlId!==_e.xmlId&&(t=K8({..._e,source:t},{xmlId:_e.xmlId}).source)}if(t===_e.source)return;if(be(_e.xmlId,t,ve),ve===`pretext`){let e=L8(t);if(e&&(xe(_e.xmlId,{title:e.title,type:e.type,label:e.label||null}),e.type!==_e.type)){let t=g5(j,_e.xmlId);if(t){let n=h5(t.source,_e.xmlId,_e.xmlId,e.type);n!==t.source&&be(t.xmlId,n,t.sourceFormat)}}}else if(ve===`markdown`){let e=G8(t);if(e&&(xe(_e.xmlId,{title:e.title,type:e.type,label:e.label||null}),e.type!==_e.type)){let t=g5(j,_e.xmlId);if(t){let n=h5(t.source,_e.xmlId,_e.xmlId,e.type);n!==t.source&&be(t.xmlId,n,t.sourceFormat)}}}else if(ve===`latex`){let e=E8(t);e!==null&&xe(_e.xmlId,{title:e})}let n=new Set(j.map(e=>e.xmlId));for(let{xmlId:e,type:r}of a5(t,ve))n.has(e)||(Ce(u5(e,r,ve)),n.add(e))},R=e=>{we(e)},De=()=>{_e&&(u(!1),ue(_e))},Oe=e=>{let t=h8();if(Ce(t),e){let n=j.find(t=>t.xmlId===e);n&&be(n.xmlId,f5(n.source,t.xmlId,t.type,null,n.sourceFormat),n.sourceFormat)}le(t.xmlId),ue(t,{isNew:!0})},ke=e=>{D(e)},z=t=>{A(t),e.onAssetRemove?.(t)},Ae=!!(e.onAssetUpload&&e.onAssetFetchUrl&&e.onAssetUpdate),je=!!(e.onAssetRemove&&(e.onAssetUpload||e.onAssetAddFromLibrary)),Me=async t=>{if(!e.onAssetUpload||!e.onAssetFetchUrl||!t.ref||!t.url)return;let n=new Set(P5(j,E).map(e=>e.ref)),r=F5(t.ref,n),i=await e.onAssetFetchUrl(t.url),a=/\.[^./\\]+$/.exec(i.name)?.[0]??``,o=new File([i],`${r}${a}`,{type:i.type}),s={...await e.onAssetUpload(o),ref:r,title:`${t.title} (copy)`,source:t.source};await e.onAssetUpdate?.(s),D(s),_(s.kind,r)},Ne=async(t,n,r)=>{if(r)n.ref&&t.ref&&Pe(t.kind,t.ref,n.ref),D(n);else{let r={...n,ref:t.ref,title:t.title,source:t.source};await e.onAssetUpdate?.(r),O(r)}e.onAssetRemove?.(t),A(t)},Pe=(e,t,n)=>{if(t!==n)for(let r of j){let i=s5(r.source,e,t,n);i!==r.source&&be(r.xmlId,i,r.sourceFormat)}},Fe=(e,t)=>{for(let n of j){let r=c5(n.source,e,t);r!==n.source&&be(n.xmlId,r,n.sourceFormat)}};(0,n.useEffect)(()=>{let e=()=>o(window.innerWidth<800);return window.addEventListener(`resize`,e),()=>window.removeEventListener(`resize`,e)},[o]);let Ie=ve!==`pretext`;(0,n.useLayoutEffect)(()=>{r({selectDivision:R,addDivision:e=>Oe(e),removeDivision:e=>L(e),updateDivision:(e,t)=>Se(e,t),divisionContentChange:(e,t)=>{be(e,t,j.find(t=>t.xmlId===e)?.sourceFormat??`pretext`)},handleDivisionContentChange:Ee,assetInsert:ke,assetRemove:z,assetRefRemove:Fe,assetDuplicate:Ae?Me:void 0,insertContentAtCursor:e=>he.current?.insertAtCursor(e),updateTitle:t=>{de(t),e.onTitleChange?.(t)},feedbackSubmit:e.onFeedbackSubmit})}),(0,n.useEffect)(()=>{T({source:ye,sourceFormat:ve,libraryAssets:e.libraryAssets,projectType:e.projectType,projectUrl:e.projectUrl,rootDivisionId:I?.xmlId,canConvertToPretext:Te!==void 0,activeEditorSource:ye,hasFeedback:e.onFeedbackSubmit!==void 0,hasAssetDuplicate:Ae})});let Le=(0,n.useRef)({divisions:e.divisions,projectAssets:e.projectAssets,title:e.title,docinfo:e.docinfo,commonDocinfo:e.commonDocinfo,useCommonDocinfo:e.useCommonDocinfo,activeDivisionId:e.activeDivisionId});(0,n.useEffect)(()=>{let t=Le.current,n={},r=!1;if(e.projectAssets!==void 0&&e.projectAssets!==t.projectAssets&&(n.projectAssets=e.projectAssets,r=!0),e.divisions!==t.divisions){let t=l7(e.divisions,e.rootDivisionId),i=N5(e.divisions,t?.xmlId,e.projectType,e.title);n.divisions=i;let a=i.find(e=>e.xmlId===t?.xmlId)??t;(M==null||!e.divisions.some(e=>e.xmlId===M))&&(n.activeDivisionId=a?.xmlId??null),!e.title&&a?.title&&(n.title=a.title),r=!0}e.title!==void 0&&e.title!==t.title&&(n.title=e.title,r=!0),e.docinfo!==void 0&&e.docinfo!==t.docinfo&&(n.docinfo=e.docinfo,r=!0),e.commonDocinfo!==void 0&&e.commonDocinfo!==t.commonDocinfo&&(n.commonDocinfo=e.commonDocinfo,r=!0),e.useCommonDocinfo!==void 0&&e.useCommonDocinfo!==t.useCommonDocinfo&&(n.useCommonDocinfo=e.useCommonDocinfo,r=!0),e.activeDivisionId!==void 0&&e.activeDivisionId!==t.activeDivisionId&&(n.activeDivisionId=e.activeDivisionId,r=!0),r&&ae(n),Le.current={divisions:e.divisions,projectAssets:e.projectAssets,title:e.title,docinfo:e.docinfo,commonDocinfo:e.commonDocinfo,useCommonDocinfo:e.useCommonDocinfo,activeDivisionId:e.activeDivisionId}});let Re=(0,n.useRef)(null);(0,n.useEffect)(()=>{if(Re.current===e.divisions)return;Re.current=e.divisions;let t=l7(e.divisions,e.rootDivisionId),n=N5(e.divisions,t?.xmlId,e.projectType,e.title);for(let t of n){if(t.sourceFormat!==`pretext`)continue;let n=e.divisions.find(e=>e.xmlId===t.xmlId);!n||t.source===n.source||e.onContentChange({xmlId:t.xmlId,source:t.source,sourceFormat:`pretext`,pretextSource:t.source})}});let ze=ie?re:N,Be=(0,n.useMemo)(()=>{if(!h||!I)return``;try{return k5(j,I.xmlId,ze,E??[])}catch(e){return`<!-- Unable to assemble document source: ${e instanceof Error?e.message:String(e)} -->`}},[h,I,j,ze,E]),Ve=_e?D5(j,_e.xmlId,E):void 0,He=_e&&Ve!==void 0?j5(_e.type,Ve,ze,_e.title):void 0,Ue=()=>me.current?.rebuild(),B=()=>{e.onSave?.(),me.current?.rebuild()},We=t=>{let n=t.ctrlKey||t.metaKey;n&&t.key===`Enter`&&e.onPreviewRebuild?(t.preventDefault(),Ue()):n&&t.key===`s`&&(t.preventDefault(),B())},Ge=()=>{if(!_e||!Te)return;let e=h8(),t={id:e.xmlId,xmlId:_e.xmlId,title:_e.title,type:_e.type,sourceFormat:ve,source:_e.source};Ce(ve===`markdown`?K8(t,{xmlId:e.xmlId}):O8(t,{xmlId:e.xmlId})),be(_e.xmlId,Te,`pretext`),xe(_e.xmlId,{sourceFormat:`pretext`})},Ke=(0,t.jsx)(Ws,{ref:he,content:ye,sourceFormat:ve,onChange:Ee,onRebuild:e.onPreviewRebuild?Ue:void 0,onSave:B,onOpenLatexImport:()=>C(`isLatexDialogOpen`),onOpenDocinfoEditor:()=>C(`isDocinfoEditorOpen`),onOpenConvertToPretext:Ie&&Te!==void 0?()=>C(`isConvertDialogOpen`):void 0,canConvertToPretext:Te!==void 0,onOpenAssets:e.projectAssets!==void 0&&ve===`pretext`?()=>C(`isAssetPickerOpen`):void 0,onShowFullSource:()=>C(`isFullSourceOpen`),onRequestWrapperEdit:De}),V;V=i&&e.onPreviewRebuild?(0,t.jsx)(lR,{ref:me,content:He||``,title:ne,onRebuild:e.onPreviewRebuild}):(0,t.jsx)(oR,{content:ye,canEdit:ve===`pretext`,editDisabledReason:ve===`markdown`?`Visual editing is not available for Markdown documents.`:ve===`latex`?`Visual editing is not available for LaTeX documents.`:``,onChange:e=>Ee(e)});let qe=(0,t.jsx)(i7,{isCollapsed:l,onToggleCollapse:()=>u(e=>!e),onOpenAssetPicker:e.projectAssets===void 0?void 0:()=>C(`isAssetPickerOpen`)}),Je=`pretext-plus-tab-editor`,Ye=`pretext-plus-tab-preview`,Xe=`pretext-plus-tabpanel`,Ze;return Ze=a?(0,t.jsxs)(`div`,{className:`pretext-plus-editor__tabs`,children:[qe,(0,t.jsxs)(`div`,{className:`pretext-plus-editor__tabs-main`,children:[(0,t.jsxs)(`div`,{className:`pretext-plus-editor__tab-list`,role:`tablist`,children:[(0,t.jsx)(`button`,{type:`button`,id:Je,role:`tab`,"aria-controls":Xe,"aria-selected":s===`editor`,tabIndex:s===`editor`?0:-1,className:`pretext-plus-editor__tab-button ${s===`editor`?`is-active`:``}`,onClick:()=>c(`editor`),children:`Editor`}),(0,t.jsx)(`button`,{type:`button`,id:Ye,role:`tab`,"aria-controls":Xe,"aria-selected":s===`preview`,tabIndex:s===`preview`?0:-1,className:`pretext-plus-editor__tab-button ${s===`preview`?`is-active`:``}`,onClick:()=>c(`preview`),children:`Preview`})]}),(0,t.jsx)(`div`,{id:Xe,className:`pretext-plus-editor__tab-panel`,role:`tabpanel`,"aria-labelledby":s===`editor`?Je:Ye,children:(0,t.jsx)(`div`,{style:{height:`100%`},children:s===`editor`?Ke:V})})]})]}):(0,t.jsxs)(`div`,{className:`pretext-plus-editor__sectioned-layout`,children:[qe,(0,t.jsxs)(gt,{orientation:`horizontal`,className:`pretext-plus-editor__splitter`,children:[(0,t.jsx)(yt,{className:`pretext-plus-editor__editor-panel`,children:Ke}),(0,t.jsx)(St,{className:`pretext-plus-editor__resize-handle`,children:(0,t.jsx)(`div`,{className:`pretext-plus-editor__resize-dots`})}),(0,t.jsx)(yt,{className:`pretext-plus-editor__preview-panel`,children:V})]})]}),(0,t.jsxs)(`div`,{className:`pretext-plus-editor`,onKeyDown:We,children:[(0,t.jsx)(W5,{onSaveButton:e.onSaveButton,saveButtonLabel:e.saveButtonLabel,onCancelButton:e.onCancelButton,cancelButtonLabel:e.cancelButtonLabel,showPreviewModeToggle:e.onPreviewRebuild!==void 0}),(0,t.jsxs)(`div`,{className:`pretext-plus-editor__editor-displays`,children:[(0,t.jsx)(a7,{resetKeys:[ye,M],children:Ze}),d?(0,t.jsx)(g6,{onClose:()=>w(`isLatexDialogOpen`)}):null,f&&_e&&Te?(0,t.jsx)(b6,{source:ye,sourceFormat:ve,pretextSource:Te,onConfirm:Ge,onClose:()=>w(`isConvertDialogOpen`)}):null,p?(0,t.jsx)(M6,{docinfo:N,showCommonDocinfoControls:!0,commonDocinfo:re,initialUseCommonDocinfo:ie,onClose:t=>{if(w(`isDocinfoEditorOpen`),t!==void 0){F({docinfo:t.docinfo,commonDocinfo:t.commonDocinfo,useCommonDocinfo:t.useCommonDocinfo}),e.onCommonDocinfoChange?.(t.commonDocinfo),e.onUseCommonDocinfoChange?.(t.useCommonDocinfo);let n=I??_e;be(n?.xmlId??``,n?.source??``,n?.sourceFormat??`pretext`,{docinfo:t.docinfo,commonDocinfo:t.commonDocinfo,useCommonDocinfo:t.useCommonDocinfo})}}}):null,(m||y||x)&&e.projectAssets!==void 0?(0,t.jsx)(z5,{open:m||!!y||!!x,resolveTarget:y,replaceTarget:x,onClose:()=>{w(`isAssetPickerOpen`),b(),S(null)},onLoadAssets:e.onLoadAssets,onLoadLibraryAssets:e.onLoadLibraryAssets,onAddFromLibrary:e.onAssetAddFromLibrary,onUpload:e.onAssetUpload,onFetchUrl:e.onAssetFetchUrl,onCreateDoenet:e.onCreateDoenet,onRemoveAsset:e.onAssetRemove?z:void 0,onDuplicateAsset:Ae?Me:void 0,onAssetAdded:ke,onResolveRef:Pe,onReplaceAsset:Ne}):null,h?(0,t.jsx)(P6,{source:Be,onClose:()=>w(`isFullSourceOpen`)}):null,ee?(0,t.jsx)(U5,{asset:ee,projectAssets:E??[],onClose:v,onReplace:je?e=>{v(),S(e)}:void 0,onDuplicate:Ae?e=>Me(e):void 0,onSave:async(t,n)=>{t.ref&&t.ref!==n?(Pe(t.kind,n,t.ref),k(t.kind,n,t)):O(t),await e.onAssetUpdate?.(t)}},`${ee.kind}:${ee.ref}`):null]})]})};e.CodeEditor=Ws,e.DocinfoEditor=M6,e.Editors=u7,e.FeedbackLink=c6,e.FullPreview=lR,e.VisualEditor=oR,e.assembleFullProjectSource=k5,e.assembleProjectSource=D5,e.buildDivisionTree=b5,e.convertLatexToPretext=r6,e.createConclusion=_8,e.createIntroduction=g8,e.createLatexConclusion=M8,e.createLatexIntroduction=j8,e.createNewLatexSection=A8,e.createNewSection=h8,e.derivePretextContent=o6,e.detectSourceFormat=n6,e.ensureLatexSectionWrapper=C8,e.ensureSectionWrapper=b8,e.extractDivisionMetadata=L8,e.extractLatexDivisionTitle=E8,e.findDivisionParent=g5,e.getOrphanRoots=x5,e.getOrphanedDivisions=y5,e.getSectionAttributes=P8,e.insertDivisionRef=f5,e.mergeTwoSections=N8,e.moveDivisionRef=m5,e.parseDivisionRefs=i5,e.postToIframe=cR,e.removeDivisionRef=p5,e.renameDivisionRef=h5,e.reorderDivisionRefs=_5,e.rewrapLatexSection=S8,e.rewrapSection=y8,e.stripLatexSectionWrapper=x8,e.stripSectionWrapper=v8,e.updateDivisionTitle=m8,e.updateLatexSectionTitle=T8,e.updateSectionMetadata=R8,e.wrapDivisionForPreview=j5});
1092
+ `+a.slice(o)}function h5(e,t,n,r){let i=new RegExp(e5(t));return e.replace(i,e=>{let t=e.trimStart();return t.startsWith(`::`)?`::${r}{ref="${n}"}`:t.startsWith(`\\plus`)?`\\plus{${r}}{${n}}`:`<plus:${r} ref="${n}"/>`})}function g5(e,t){let n=new RegExp(e5(t));return e.find(e=>n.test(e.source))??null}function _5(e,t){let n=e,r=null;for(let e of t)n=m5(n,e,r),r=e;return n}function v5(e,t){let n=new Set,r=[t];for(;r.length>0;){let t=r.pop();if(n.has(t))continue;n.add(t);let i=e.find(e=>e.xmlId===t);if(i)for(let e of i5(i.source,i.sourceFormat))r.push(e)}return n}function y5(e,t){let n=v5(e,t);return e.filter(e=>!n.has(e.xmlId))}function b5(e,t){let n=[],r=new Set([t]),i=(t,a)=>{let o=e.find(e=>e.xmlId===t);if(o)for(let s of i5(o.source,o.sourceFormat)){if(r.has(s))continue;let o=e.find(e=>e.xmlId===s);o&&(r.add(s),n.push({division:o,depth:a,parentXmlId:t}),i(s,a+1))}};return i(t,0),n}function x5(e,t){let n=y5(e,t),r=new Set(n.map(e=>e.xmlId)),i=new Set;for(let e of n)for(let t of i5(e.source,e.sourceFormat))r.has(t)&&i.add(t);return n.filter(e=>!i.has(e.xmlId))}var S5=new Set([`book`,`article`,`slideshow`]);function C5(e){try{let t=z6(e),n=t.children.find(e=>e.type===`element`),r=n?.name===`pretext`?n.children.find(e=>e.type===`element`&&S5.has(e.name)):n&&S5.has(n.name)?n:void 0;if(!r)return e;let i=r.attributes[`xml:id`];return i||=T5(t,`main`),r.attributes.label?e:(r.attributes.label=i,JY(t,a8))}catch(t){return console.error(`Error ensuring label:`,t),e}}function w5(e,t){return e.type===`element`&&e.attributes?.label===t?!0:`children`in e&&e.children?e.children.some(e=>e.type===`element`?w5(e,t):!1):!1}function T5(e,t){let n=t,r=1;for(;w5(e,n);)n=`${t}-${r}`,r++;return n}function E5(e,t,n,r){let i=t.find(t=>t.xmlId===e);if(!i)return`<!-- missing division: ${e} -->`;if(n.has(e))return`<!-- circular reference: ${e} -->`;let a;if(i.sourceFormat===`pretext`)a=i.source;else if(i.sourceFormat===`markdown`){let{pretextSource:e,pretextError:t}=o6(i.source,`markdown`);a=e??`<!-- conversion error: ${t} -->`}else a=k8(i)??`<!-- conversion error: ${i.xmlId} -->`;let o=new Set(n).add(e);return a.replace(/<plus:([a-z-]+)\s([^>]*ref="[^"]+"[^>]*?)(?:\/>|>\s*<\/plus:\1>)/g,(e,n,i)=>{let a=/ref="([^"]+)"/.exec(i)?.[1]??``;if(!r8.has(n))return E5(a,t,o,r);let s=/width="([^"]+)"/.exec(i)?.[1];return i8(n,a,r,s)})}function D5(e,t,n=[]){return C5(E5(t,e,new Set,n))}function O5(e,t){return C5(`<pretext>\n${t.trim()?`${t.trim()}\n`:``}${e}\n</pretext>`)}function k5(e,t,n,r=[]){return O5(E5(t,e,new Set,r),n)}var A5=new Set([`part`,`chapter`]);function j5(e,t,n,r){return O5(S5.has(e)?t:A5.has(e)?`<book>\n<title>${r}</title>\n${t}\n</book>`:`<article>\n<title>${r}</title>\n${t}\n</article>`,n)}function M5(e){let t=e.trim(),n=t.match(/^<title\b[^>]*>([\s\S]*?)<\/title>\s*/);return n?{title:n[1].trim(),body:t.slice(n[0].length)}:{title:``,body:t}}function N5(e,t,n,r){let i=n===`book`?`book`:`article`;return e.map(e=>{if(e.sourceFormat===`markdown`){if(!e.title){let t=G8(e.source)?.title;if(t)return{...e,title:t}}return e}if(e.sourceFormat===`latex`){if(!e.title){let t=E8(e.source);if(t)return{...e,title:t}}return e}if(e.sourceFormat!==`pretext`)return e;let n=L8(e.source);if(e.xmlId===t&&!(n&&S5.has(n.type))){let{title:t,body:n}=M5(e.source),a=e.title||t||r||`Untitled`;return{...e,type:i,title:a,source:`<${i} xml:id="${e.xmlId}">\n<title>${a}</title>\n\n${n}\n</${i}>`}}return!e.title&&n?.title?{...e,title:n.title}:e})}function P5(e,t){let n=t??[],r=(e,t)=>n.find(n=>n.kind===e&&n.ref===t),i=[],a=new Set;for(let t of e??[])for(let{kind:e,ref:n}of o5(t.source,t.sourceFormat)){let t=`${e}:${n}`;if(a.has(t))continue;a.add(t);let o=r(e,n);i.push({kind:e,ref:n,asset:o,inDocument:!0,status:o?`linked`:`unlinked`})}for(let e of n){if(!e.ref)continue;let t=`${e.kind}:${e.ref}`;a.has(t)||(a.add(t),i.push({kind:e.kind,ref:e.ref,asset:e,inDocument:!1,status:`unused`}))}return i}function F5(e,t){let n=`${e}-copy`;if(!t.has(n))return n;for(let e=2;;e++){let r=`${n}-${e}`;if(!t.has(r))return r}}var I5={image:`Images`,doenet:`Doenet`},L5=[`image`,`doenet`].filter(e=>e!==`doenet`||!1);function R5(e){return`${e}-${Date.now()}`}var z5=({open:e,onClose:r,resolveTarget:i,replaceTarget:a,onUpload:o,onFetchUrl:s,onCreateDoenet:c,onRemoveAsset:l,onDuplicateAsset:u,onAssetAdded:d,onResolveRef:f,onReplaceAsset:p})=>{let m=m6(e=>e.divisions),h=m6(e=>e.activeDivisionId),g=m?.find(e=>e.xmlId===h)?.sourceFormat??`pretext`,_=(e,t)=>l5(e,t,g),v=m6(e=>e.projectAssets)??[],y=m6(e=>e.openAssetEditor),b=m6(e=>e.openAssetResolver),x=m6(e=>e.removeAssetRefFromDocument),[S,C]=(0,n.useState)(`in-document`),[w,T]=(0,n.useState)(null),[E,D]=(0,n.useState)(`upload`),[O,k]=(0,n.useState)(!1),[A,ee]=(0,n.useState)(!1),[te,j]=(0,n.useState)(null),M=(0,n.useRef)(null),[ne,N]=(0,n.useState)(null),[re,ie]=(0,n.useState)(null),[ae,oe]=(0,n.useState)(``),[se,P]=(0,n.useState)(``),[ce,le]=(0,n.useState)(``),[ue,de]=(0,n.useState)(!1),[F,fe]=(0,n.useState)(null),[pe,me]=(0,n.useState)(``),[he,ge]=(0,n.useState)(``),[I,_e]=(0,n.useState)(!1),[ve,ye]=(0,n.useState)(null),[be,xe]=(0,n.useState)(null),[Se,Ce]=(0,n.useState)(null),[L,we]=(0,n.useState)(null);if((0,n.useEffect)(()=>{if(!e)return;let t=e=>{e.key===`Escape`&&r()};return window.addEventListener(`keydown`,t),()=>window.removeEventListener(`keydown`,t)},[e,r]),(0,n.useEffect)(()=>()=>{re&&URL.revokeObjectURL(re)},[re]),!e)return null;let Te=P5(m,v),Ee=e=>{if(a){p(a,e),r();return}if(d(e),i){e.ref&&f(i.kind,i.ref,e.ref),r();return}e.ref&&navigator.clipboard.writeText(_(e.kind,e.ref)).catch(()=>{}),we(e)},R=(e,t)=>{let n=`${e}:${t}`;navigator.clipboard.writeText(_(e,t)).catch(()=>{}),xe(n),setTimeout(()=>xe(e=>e===n?null:e),2e3)},De=e=>{j(null),N(e),oe(e.name),ie(URL.createObjectURL(e))},Oe=()=>{N(null),ie(null),oe(``)},ke=async()=>{if(!(!o||!ne)){j(null),ee(!0);try{Ee(await o(ne,ae.trim()||ne.name)),Oe()}catch(e){j(e instanceof Error?e.message:`Upload failed.`)}finally{ee(!1)}}},z=async()=>{let e=se.trim();if(!e)return;let t=ce.trim();if(s&&o){fe(null),de(!0);try{Ee(await o(await s(e),t||void 0))}catch(e){fe(e instanceof Error?e.message:`Failed to add URL.`)}finally{de(!1)}}else Ee({id:R5(`url`),title:t||e,ref:e.split(`/`).pop()??`image`,kind:`image`,url:e})},Ae=async()=>{let e=pe.trim(),t=he.trim();if(!(!e||!t))if(c){ye(null),_e(!0);try{Ee(await c(e,t))}catch(e){ye(e instanceof Error?e.message:`Failed to create activity.`)}finally{_e(!1)}}else Ee({id:R5(`doenet`),title:e,ref:t,kind:`doenet`})},je=()=>{if(Te.length===0)return(0,t.jsxs)(`div`,{className:`pretext-plus-editor__am-placeholder`,children:[(0,t.jsx)(`p`,{children:`No assets in this project yet.`}),(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__dialog-button`,onClick:()=>{C(`add`),T(null)},children:`Add an asset`})]});let e=L5.map(e=>({kind:e,rows:Te.filter(t=>t.kind===e)})).filter(e=>e.rows.length>0),n=e=>{let n=`${e.kind}:${e.ref}`,i=Se===n,a=()=>{e.status===`unlinked`?b(e.kind,e.ref):(y(e.kind,e.ref),r())};return(0,t.jsxs)(`li`,{className:`pretext-plus-editor__am-doc-row`,children:[(0,t.jsxs)(`button`,{type:`button`,className:`pretext-plus-editor__am-row-info pretext-plus-editor__am-row-info--btn`,onClick:a,title:e.status===`unlinked`?`No asset for this reference — click to link or create one`:`Edit asset`,children:[(0,t.jsx)(`span`,{className:`pretext-plus-editor__am-row-name`,children:e.asset?.title??e.ref}),(0,t.jsx)(`span`,{className:`pretext-plus-editor__am-row-ref`,children:e.ref})]}),e.status===`unlinked`&&(0,t.jsx)(`span`,{className:`pretext-plus-editor__am-status pretext-plus-editor__am-status--warn`,title:`No asset for this reference`,children:`needs asset`}),e.status===`unused`&&(0,t.jsx)(`span`,{className:`pretext-plus-editor__am-status`,title:`Not referenced in the document yet`,children:`not placed`}),(0,t.jsxs)(`div`,{className:`pretext-plus-editor__am-row-actions`,children:[(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__am-action-btn`,onClick:a,children:e.status===`unlinked`?`Link / create`:`Edit`}),(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__am-action-btn${be===n?` pretext-plus-editor__am-action-btn--done`:``}`,onClick:()=>R(e.kind,e.ref),title:`Copy ${_(e.kind,e.ref)}`,children:be===n?`Copied!`:`Copy embed code`}),u&&e.asset?.url&&(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__am-action-btn`,disabled:i,onClick:async()=>{Ce(n);try{await u(e.asset),r()}catch{Ce(null)}},title:`Create a copy of this asset under a new reference`,children:i?`Duplicating…`:`Duplicate`}),l&&e.asset&&(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__am-action-btn pretext-plus-editor__am-action-btn--danger`,onClick:()=>{e.inDocument&&!window.confirm(`Remove "${e.asset.title}" from the project? This also deletes its reference(s) from the document.`)||(l(e.asset),x(e.kind,e.ref),r())},title:`Remove from project`,children:`Remove`})]})]},n)};return(0,t.jsx)(`div`,{className:`pretext-plus-editor__am-in-doc`,children:e.map(({kind:e,rows:r})=>(0,t.jsxs)(`div`,{className:`pretext-plus-editor__am-kind-group`,children:[(0,t.jsxs)(`div`,{className:`pretext-plus-editor__am-kind-header`,children:[(0,t.jsx)(`span`,{"aria-hidden":`true`,children:`📁`}),(0,t.jsx)(`span`,{children:I5[e]}),(0,t.jsx)(`span`,{className:`pretext-plus-editor__am-kind-count`,children:r.length})]}),(0,t.jsx)(`ul`,{className:`pretext-plus-editor__am-list`,children:r.map(n)})]},e))})},Me=e=>(0,t.jsxs)(`div`,{className:`pretext-plus-editor__am-success`,children:[(0,t.jsxs)(`p`,{className:`pretext-plus-editor__am-success-title`,children:[`✓ Added “`,e.title,`” — embed code copied to clipboard`]}),(0,t.jsx)(`p`,{className:`pretext-plus-editor__dialog-helper-copy`,children:`It now appears in the Assets list. Paste this where you want it to appear:`}),e.ref&&(0,t.jsxs)(`div`,{className:`pretext-plus-editor__am-embed-row`,children:[(0,t.jsx)(`code`,{className:`pretext-plus-editor__am-embed-code`,children:_(e.kind,e.ref)}),(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__am-action-btn${be===`${e.kind}:${e.ref}`?` pretext-plus-editor__am-action-btn--done`:``}`,onClick:()=>R(e.kind,e.ref),children:be===`${e.kind}:${e.ref}`?`Copied!`:`Copy again`})]}),(0,t.jsxs)(`div`,{className:`pretext-plus-editor__am-success-actions`,children:[(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__dialog-button pretext-plus-editor__dialog-button--secondary`,onClick:()=>{we(null),T(null),C(`add`)},children:`Add another`}),(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__dialog-button`,onClick:()=>{we(null),C(`in-document`)},children:`Done`})]})]}),Ne=()=>(0,t.jsxs)(`div`,{className:`pretext-plus-editor__am-kind-picker`,children:[(0,t.jsx)(`p`,{className:`pretext-plus-editor__am-kind-picker-label`,children:`What kind of asset?`}),(0,t.jsxs)(`div`,{className:`pretext-plus-editor__am-kind-cards`,children:[(0,t.jsxs)(`button`,{type:`button`,className:`pretext-plus-editor__am-kind-card`,onClick:()=>{T(`image`),D(o?`upload`:`url`)},children:[(0,t.jsx)(`span`,{className:`pretext-plus-editor__am-kind-card-icon`,"aria-hidden":`true`,children:`🖼️`}),(0,t.jsx)(`span`,{className:`pretext-plus-editor__am-kind-card-label`,children:`Image`}),(0,t.jsx)(`span`,{className:`pretext-plus-editor__am-kind-card-hint`,children:`PNG, JPEG, SVG, etc.`})]}),!1,(0,t.jsxs)(`div`,{className:`pretext-plus-editor__am-kind-card pretext-plus-editor__am-kind-card--soon`,"aria-disabled":`true`,children:[(0,t.jsx)(`span`,{className:`pretext-plus-editor__am-kind-card-icon`,"aria-hidden":`true`,children:`✨`}),(0,t.jsx)(`span`,{className:`pretext-plus-editor__am-kind-card-label`,children:`More coming soon`}),(0,t.jsx)(`span`,{className:`pretext-plus-editor__am-kind-card-hint`,children:`Interactive activities and more`})]})]})]}),Pe=e=>(0,t.jsxs)(`div`,{className:`pretext-plus-editor__am-configure`,children:[e&&(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__am-back-btn`,onClick:()=>T(null),children:`← Back`}),(0,t.jsxs)(`div`,{className:`pretext-plus-editor__dialog-tab-bar pretext-plus-editor__am-sub-tabs`,children:[o&&(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__dialog-tab${E===`upload`?` pretext-plus-editor__dialog-tab--active`:``}`,onClick:()=>D(`upload`),children:`Upload`}),(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__dialog-tab${E===`url`?` pretext-plus-editor__dialog-tab--active`:``}`,onClick:()=>D(`url`),children:`External URL`})]}),(0,t.jsxs)(`div`,{className:`pretext-plus-editor__am-configure-body`,children:[E===`upload`&&o&&(0,t.jsx)(`div`,{className:`pretext-plus-editor__am-upload`,children:ne?(0,t.jsxs)(`div`,{className:`pretext-plus-editor__am-url-form`,children:[(0,t.jsx)(`img`,{src:re??void 0,alt:`Preview`,className:`pretext-plus-editor__am-url-preview`}),(0,t.jsxs)(`label`,{className:`pretext-plus-editor__dialog-label`,htmlFor:`am-upload-title`,children:[`Title `,(0,t.jsx)(`span`,{className:`pretext-plus-editor__dialog-helper-copy`,children:`(optional)`})]}),(0,t.jsx)(`input`,{id:`am-upload-title`,type:`text`,className:`pretext-plus-editor__am-input`,placeholder:ne.name,value:ae,onChange:e=>oe(e.target.value),disabled:A,autoFocus:!0}),te&&(0,t.jsx)(`p`,{className:`pretext-plus-editor__am-error`,children:te}),(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__dialog-button pretext-plus-editor__dialog-button--secondary`,onClick:Oe,disabled:A,children:`Choose a different file`})]}):(0,t.jsxs)(`div`,{className:[`pretext-plus-editor__am-drop-zone`,O?`pretext-plus-editor__am-drop-zone--active`:``].filter(Boolean).join(` `),onDragOver:e=>{e.preventDefault(),k(!0)},onDragLeave:()=>k(!1),onDrop:e=>{e.preventDefault(),k(!1);let t=e.dataTransfer.files[0];t&&De(t)},onClick:()=>M.current?.click(),role:`button`,tabIndex:0,onKeyDown:e=>{(e.key===`Enter`||e.key===` `)&&(e.preventDefault(),M.current?.click())},"aria-label":`Upload image — click or drag and drop`,children:[(0,t.jsx)(`input`,{ref:M,type:`file`,accept:`image/*`,className:`pretext-plus-editor__dialog-file-input`,onChange:e=>{let t=e.target.files?.[0];t&&De(t)}}),(0,t.jsx)(`span`,{className:`pretext-plus-editor__am-drop-icon`,"aria-hidden":`true`,children:`↑`}),(0,t.jsx)(`p`,{className:`pretext-plus-editor__am-drop-text`,children:`Drag & drop an image, or click to browse`}),(0,t.jsx)(`p`,{className:`pretext-plus-editor__dialog-helper-copy`,children:`PNG, JPEG, GIF, SVG, WebP`})]})}),E===`url`&&(0,t.jsxs)(`div`,{className:`pretext-plus-editor__am-url-form`,children:[(0,t.jsx)(`label`,{className:`pretext-plus-editor__dialog-label`,htmlFor:`am-url-value`,children:`Image URL`}),(0,t.jsx)(`input`,{id:`am-url-value`,type:`url`,className:`pretext-plus-editor__am-input`,placeholder:`https://example.com/image.png`,value:se,onChange:e=>P(e.target.value),disabled:ue,autoFocus:!0}),(0,t.jsxs)(`label`,{className:`pretext-plus-editor__dialog-label`,htmlFor:`am-url-title`,children:[`Title `,(0,t.jsx)(`span`,{className:`pretext-plus-editor__dialog-helper-copy`,children:`(optional)`})]}),(0,t.jsx)(`input`,{id:`am-url-title`,type:`text`,className:`pretext-plus-editor__am-input`,placeholder:`My image`,value:ce,onChange:e=>le(e.target.value),disabled:ue}),F&&(0,t.jsx)(`p`,{className:`pretext-plus-editor__am-error`,children:F}),se.trim()&&(0,t.jsx)(`img`,{src:se.trim(),alt:`Preview`,className:`pretext-plus-editor__am-url-preview`,onError:e=>{e.currentTarget.style.display=`none`}})]})]})]}),Fe=()=>(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__dialog-button`,onClick:z,disabled:!se.trim()||ue,children:ue?`Adding…`:s&&o?`Add to Project`:`Add`}),Ie=()=>(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__dialog-button`,onClick:ke,disabled:!ne||A,children:A?`Uploading…`:`Add to Project`}),Le=e=>(0,t.jsxs)(`div`,{className:`pretext-plus-editor__am-configure`,children:[e&&(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__am-back-btn`,onClick:()=>T(null),children:`← Back`}),(0,t.jsx)(`div`,{className:`pretext-plus-editor__am-configure-body`,children:(0,t.jsxs)(`div`,{className:`pretext-plus-editor__am-create-form`,children:[(0,t.jsx)(`label`,{className:`pretext-plus-editor__dialog-label`,htmlFor:`am-doenet-title`,children:`Title`}),(0,t.jsx)(`input`,{id:`am-doenet-title`,type:`text`,className:`pretext-plus-editor__am-input`,placeholder:`My Activity`,value:pe,onChange:e=>me(e.target.value),disabled:I,autoFocus:!0}),(0,t.jsx)(`label`,{className:`pretext-plus-editor__dialog-label`,htmlFor:`am-doenet-ref`,children:`Id`}),(0,t.jsx)(`input`,{id:`am-doenet-ref`,type:`text`,className:`pretext-plus-editor__am-input`,placeholder:`my-activity`,value:he,onChange:e=>ge(e.target.value),disabled:I}),(0,t.jsxs)(`p`,{className:`pretext-plus-editor__dialog-helper-copy`,children:[`The id is used in the embed code: `,(0,t.jsx)(`code`,{children:_(`doenet`,he||`my-activity`)})]}),ve&&(0,t.jsx)(`p`,{className:`pretext-plus-editor__am-error`,children:ve}),(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__dialog-button`,onClick:Ae,disabled:!pe.trim()||!he.trim()||I,children:I?`Creating…`:c?`Create`:`Add`})]})})]}),Re=(e,n,i)=>(0,t.jsxs)(t.Fragment,{children:[(0,t.jsxs)(`div`,{className:`pretext-plus-editor__dialog-header`,children:[(0,t.jsx)(`h2`,{className:`pretext-plus-editor__dialog-title`,children:n}),(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__dialog-close`,onClick:r,"aria-label":`Close`,children:`✕`})]}),(0,t.jsxs)(`div`,{className:`pretext-plus-editor__dialog-content pretext-plus-editor__dialog-content--single`,children:[(0,t.jsx)(`p`,{className:`pretext-plus-editor__dialog-helper-copy pretext-plus-editor__am-resolve-hint`,children:i}),e===`doenet`?Le(!1):Pe(!1)]}),(0,t.jsxs)(`div`,{className:`pretext-plus-editor__dialog-actions`,children:[(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__dialog-button pretext-plus-editor__dialog-button--secondary`,onClick:r,children:`Cancel`}),e===`image`&&E===`url`&&Fe(),e===`image`&&E===`upload`&&o&&Ie()]})]});return(0,t.jsx)(`div`,{className:`pretext-plus-editor__dialog-overlay`,onClick:e=>e.target===e.currentTarget&&r(),children:(0,t.jsx)(`div`,{className:`pretext-plus-editor__dialog pretext-plus-editor__dialog--asset-manager`,role:`dialog`,"aria-modal":`true`,"aria-label":`Asset manager`,children:i?(e=>Re(e.kind,`Link asset`,(0,t.jsxs)(t.Fragment,{children:[`The reference `,(0,t.jsx)(`code`,{children:e.ref}),` has no asset yet. Choose or create one — the reference in your document will be updated to match.`]})))(i):a?(e=>Re(e.kind,`Replace asset`,(0,t.jsxs)(t.Fragment,{children:[`Choose or upload a new asset to replace `,(0,t.jsx)(`code`,{children:e.title}),`. Every place it’s used in your document will show the new asset.`]})))(a):(0,t.jsxs)(t.Fragment,{children:[(0,t.jsxs)(`div`,{className:`pretext-plus-editor__dialog-header`,children:[(0,t.jsx)(`h2`,{className:`pretext-plus-editor__dialog-title`,children:`Assets`}),(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__dialog-close`,onClick:r,"aria-label":`Close`,children:`✕`})]}),L?(0,t.jsx)(`div`,{className:`pretext-plus-editor__dialog-content pretext-plus-editor__dialog-content--single`,children:Me(L)}):(0,t.jsxs)(t.Fragment,{children:[(0,t.jsxs)(`div`,{className:`pretext-plus-editor__dialog-tab-bar`,children:[(0,t.jsxs)(`button`,{type:`button`,className:`pretext-plus-editor__dialog-tab${S===`in-document`?` pretext-plus-editor__dialog-tab--active`:``}`,onClick:()=>C(`in-document`),children:[`Assets`,Te.length>0&&(0,t.jsx)(`span`,{className:`pretext-plus-editor__asset-tab-count`,children:Te.length})]}),(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__dialog-tab${S===`add`?` pretext-plus-editor__dialog-tab--active`:``}`,onClick:()=>{C(`add`),T(null)},children:`Add Asset`})]}),(0,t.jsx)(`div`,{className:`pretext-plus-editor__dialog-content pretext-plus-editor__dialog-content--single`,children:S===`in-document`?je():w===null?Ne():w===`image`?Pe(!0):Le(!0)})]}),(0,t.jsxs)(`div`,{className:`pretext-plus-editor__dialog-actions`,children:[(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__dialog-button pretext-plus-editor__dialog-button--secondary`,onClick:r,children:`Close`}),!L&&S===`add`&&w===`image`&&E===`url`&&Fe(),!L&&S===`add`&&w===`image`&&E===`upload`&&o&&Ie()]})]})})})},B5={automaticLayout:!0,minimap:{enabled:!1},wordWrap:`on`,insertSpaces:!0,tabSize:2,padding:{top:10,bottom:10},scrollBeyondLastLine:!1},V5=160,H5={image:`image`,doenet:`interactive`},U5=({asset:e,projectAssets:r,onClose:i,onSave:a,onReplace:o,onDuplicate:s})=>{let c=m6(e=>e.divisions),l=m6(e=>e.activeDivisionId),u=c?.find(e=>e.xmlId===l)?.sourceFormat??`pretext`,d=e.ref??``,[f,p]=(0,n.useState)(e.title),[m,h]=(0,n.useState)(d),[g,_]=(0,n.useState)(e.source??``),[v,y]=(0,n.useState)(!1),[b,x]=(0,n.useState)(!1),[S,C]=(0,n.useState)(null),[w,T]=(0,n.useState)(!1),[E,D]=(0,n.useState)(V5),O=(0,n.useRef)(null);(0,n.useEffect)(()=>{O.current?.scrollTo({top:0})},[]);let k=e=>{let t=()=>D(Math.max(V5,e.getContentHeight()));e.onDidContentSizeChange(t),t()},A=l5(e.kind,m.trim()||d,u),ee=()=>{navigator.clipboard.writeText(A).catch(()=>{}),T(!0),setTimeout(()=>T(!1),2e3)},te=async()=>{let t=m.trim();if(!t){C(`Reference can't be empty — it identifies the asset and is used by every embed of it.`);return}if(t!==d&&r.some(n=>n.kind===e.kind&&n.ref===t)){C(`Reference "${t}" is already used by another asset. Choose a unique reference.`);return}C(null),y(!0);try{await a({...e,title:f.trim()||t,ref:t,source:g},d),i()}catch(e){C(e instanceof Error?e.message:`Failed to save asset.`),y(!1)}},j=async()=>{if(s){C(null),x(!0);try{await s(e)}catch(e){C(e instanceof Error?e.message:`Failed to duplicate asset.`),x(!1)}}},M=v||b,ne=e.kind===`image`&&!!e.url,N=e.kind===`image`&&!!o;return(0,t.jsx)(`div`,{className:`pretext-plus-editor__dialog-overlay`,onClick:e=>e.target===e.currentTarget&&i(),children:(0,t.jsxs)(`div`,{className:`pretext-plus-editor__dialog`,role:`dialog`,"aria-modal":`true`,"aria-label":`Edit asset ${e.title}`,children:[(0,t.jsxs)(`div`,{className:`pretext-plus-editor__dialog-header`,children:[(0,t.jsx)(`h2`,{className:`pretext-plus-editor__dialog-title`,children:`Edit asset`}),(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__dialog-close`,onClick:i,"aria-label":`Close`,children:`✕`})]}),(0,t.jsx)(`div`,{ref:O,className:`pretext-plus-editor__dialog-content pretext-plus-editor__dialog-content--single pretext-plus-editor__am-edit-content`,children:(0,t.jsxs)(`div`,{className:`pretext-plus-editor__dialog-section`,children:[(0,t.jsxs)(`div`,{className:`pretext-plus-editor__am-edit-grid`,children:[(0,t.jsxs)(`div`,{className:`pretext-plus-editor__am-edit-col`,children:[ne&&(0,t.jsx)(`img`,{src:e.url,alt:f,className:`pretext-plus-editor__am-url-preview`,onError:e=>{e.currentTarget.style.display=`none`}}),N&&(0,t.jsx)(`div`,{className:`pretext-plus-editor__am-replace-row`,children:(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__am-action-btn`,onClick:()=>o?.(e),disabled:M,title:`Choose or upload a different asset to use here`,children:`Replace image…`})})]}),(0,t.jsxs)(`div`,{className:`pretext-plus-editor__am-edit-col`,children:[(0,t.jsx)(`label`,{className:`pretext-plus-editor__dialog-label`,htmlFor:`am-edit-title`,children:`Title`}),(0,t.jsx)(`input`,{id:`am-edit-title`,type:`text`,className:`pretext-plus-editor__am-input`,value:f,onChange:e=>p(e.target.value),disabled:M}),(0,t.jsx)(`label`,{className:`pretext-plus-editor__dialog-label`,htmlFor:`am-edit-ref`,children:`Id`}),(0,t.jsx)(`input`,{id:`am-edit-ref`,type:`text`,className:`pretext-plus-editor__am-input`,value:m,onChange:e=>h(e.target.value),disabled:M}),(0,t.jsx)(`p`,{className:`pretext-plus-editor__dialog-helper-copy`,children:`Used in the embed code. Changing it updates every reference to this asset already in your document.`}),(0,t.jsxs)(`div`,{className:`pretext-plus-editor__am-embed-row`,children:[(0,t.jsx)(`code`,{className:`pretext-plus-editor__am-embed-code`,children:A}),(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__am-action-btn${w?` pretext-plus-editor__am-action-btn--done`:``}`,onClick:ee,title:`Copy embed code to clipboard`,children:w?`Copied!`:`Copy embed code`})]})]})]}),(0,t.jsx)(`label`,{className:`pretext-plus-editor__dialog-label`,children:`Asset content`}),(0,t.jsxs)(`p`,{className:`pretext-plus-editor__dialog-helper-copy`,children:[`Inserted verbatim inside the generated `,(0,t.jsx)(`code`,{children:`<${H5[e.kind]}>`}),` element — e.g. `,(0,t.jsx)(`code`,{children:`<shortdescription>...</shortdescription>`}),`.`]}),(0,t.jsx)(`div`,{className:`pretext-plus-editor__dialog-editor pretext-plus-editor__am-edit-editor`,style:{height:E},children:(0,t.jsx)(Yn,{options:{...B5,readOnly:M},height:`100%`,language:`xml`,value:g,onMount:k,onChange:e=>_(e??``)})}),S&&(0,t.jsx)(`p`,{className:`pretext-plus-editor__am-error`,children:S})]})}),(0,t.jsxs)(`div`,{className:`pretext-plus-editor__dialog-actions`,children:[s&&(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__dialog-button pretext-plus-editor__dialog-button--secondary pretext-plus-editor__am-duplicate-btn`,onClick:j,disabled:M,title:`Create a copy of this asset under a new reference`,children:b?`Duplicating…`:`Duplicate`}),(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__dialog-button pretext-plus-editor__dialog-button--secondary`,onClick:i,disabled:M,children:`Cancel`}),(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__dialog-button`,onClick:te,disabled:M,children:v?`Saving…`:`Save`})]})]})})},W5=e=>{let n=m6(e=>e.showFullPreview),r=m6(e=>e.setShowFullPreview),i=m6(e=>e.title),a=m6(e=>e.updateTitle),o;return o=e.showPreviewModeToggle===!1?null:(0,t.jsxs)(`label`,{className:`pretext-plus-editor__preview-toggle`,children:[(0,t.jsx)(`input`,{type:`checkbox`,checked:!n,onChange:()=>r(!n),className:`pretext-plus-editor__preview-toggle-input`}),(0,t.jsxs)(`div`,{className:`pretext-plus-editor__preview-toggle-container`,children:[(0,t.jsx)(`span`,{className:`pretext-plus-editor__preview-toggle-label`,children:`Simple`}),(0,t.jsx)(`span`,{className:`pretext-plus-editor__preview-toggle-slider ${n?`pretext-plus-editor__preview-toggle-slider--active`:``}`,children:(0,t.jsx)(`span`,{className:`pretext-plus-editor__preview-toggle-dot ${n?`pretext-plus-editor__preview-toggle-dot--active`:``}`})}),(0,t.jsx)(`span`,{className:`pretext-plus-editor__preview-toggle-label`,children:`Full`})]}),(0,t.jsx)(`span`,{className:`pretext-plus-editor__preview-toggle-caption`,children:`Preview Mode`}),(0,t.jsx)(`input`,{type:`checkbox`,checked:n,onChange:()=>r(!n),className:`pretext-plus-editor__preview-toggle-input`})]}),(0,t.jsxs)(`div`,{className:`pretext-plus-editor__menu-bar`,children:[(0,t.jsx)(`div`,{className:`pretext-plus-editor__menu-left`,children:(0,t.jsxs)(`label`,{className:`pretext-plus-editor__title-label`,children:[`Title`,` `,(0,t.jsx)(`input`,{className:`pretext-plus-editor__title-input`,type:`text`,value:i,onChange:e=>a(e.target.value)})]})}),(0,t.jsxs)(`div`,{className:`pretext-plus-editor__menu-right`,children:[e.onSaveButton&&(0,t.jsx)(`button`,{className:`pretext-plus-editor__button pretext-plus-editor__button--save`,onClick:e.onSaveButton,children:e.saveButtonLabel||`Save`}),e.onCancelButton&&(0,t.jsx)(`button`,{className:`pretext-plus-editor__button pretext-plus-editor__button--cancel`,onClick:e.onCancelButton,children:e.cancelButtonLabel||`Cancel`}),(0,t.jsx)(h6,{label:`Give feedback`,context:`main-editor`}),o]})]})},G5={pretext:`PreTeXt`,latex:`LaTeX`,markdown:`Markdown`},K5={book:`bk`,article:`art`,slideshow:`slides`,part:`pt`,chapter:`ch`,section:`sec`,subsection:`subsec`,subsubsection:`subsubsec`,introduction:`intro`,conclusion:`conc`,worksheet:`ws`,handout:`ho`,exercises:`ex`,references:`ref`,glossary:`gloss`,solutions:`sol`,"reading-questions":`rq`,paragraphs:`para`},q5={book:`Book`,article:`Article`,slideshow:`Slideshow`,part:`Part`,chapter:`Chapter`,section:`Section`,worksheet:`Worksheet`,handout:`Handout`,exercises:`Exercises`,references:`References`,glossary:`Glossary`,solutions:`Solutions`,"reading-questions":`Reading Questions`,introduction:`Introduction`,conclusion:`Conclusion`,subsection:`Subsection`,subsubsection:`Subsubsection`,paragraphs:`Paragraphs`},J5=[`article`,`book`],Y5=[`part`,`chapter`,`section`,`subsection`,`subsubsection`,`worksheet`,`handout`,`exercises`,`references`,`glossary`,`solutions`,`reading-questions`,`paragraphs`],X5=[`worksheet`,`handout`,`exercises`,`references`,`glossary`,`solutions`,`reading-questions`,`paragraphs`],Z5={book:[`part`,`chapter`],article:[`section`,...X5],slideshow:[`section`],part:[`chapter`],chapter:[`section`,...X5],section:[`subsection`,...X5],subsection:[`subsubsection`,...X5],subsubsection:[...X5],worksheet:[`paragraphs`],handout:[`paragraphs`]};function Q5(e){return e?Z5[e]??Y5:Y5}function $5(e,t){let n=K5[e]??`sec`,r=I8(t);return r?`${n}-${r}`:n}var e7=({draft:e,isNew:r=!1,isRoot:i=!1,parentType:a=null,onDraftChange:o,onCommit:s,onCancel:c})=>{let l=Q5(a),u=J5.includes(e.type)?J5:[e.type,...J5],d=(0,n.useRef)(r);return(0,n.useEffect)(()=>{i||l.length>0&&!l.includes(e.type)&&o({...e,type:l[0]})},[e,i,o,l]),(0,t.jsxs)(`div`,{className:`pretext-plus-editor__toc-edit-form`,children:[(0,t.jsxs)(`label`,{className:`pretext-plus-editor__toc-edit-field`,children:[(0,t.jsx)(`span`,{children:`Title`}),(0,t.jsx)(`input`,{type:`text`,value:e.title,onChange:t=>{let n=t.target.value;o(d.current?{...e,title:n,xmlId:$5(e.type,n)}:{...e,title:n})},onKeyDown:e=>{e.key===`Enter`&&s(),e.key===`Escape`&&c()},autoFocus:!0})]}),r?(0,t.jsxs)(`label`,{className:`pretext-plus-editor__toc-edit-field`,children:[(0,t.jsx)(`span`,{children:`Format`}),(0,t.jsx)(`select`,{value:e.sourceFormat,onChange:t=>o({...e,sourceFormat:t.target.value}),children:Object.keys(G5).map(e=>(0,t.jsx)(`option`,{value:e,children:G5[e]},e))})]}):void 0,(0,t.jsxs)(`label`,{className:`pretext-plus-editor__toc-edit-field`,children:[(0,t.jsx)(`span`,{children:`Type`}),(0,t.jsx)(`select`,{value:e.type,onChange:t=>{let n=t.target.value;o(d.current?{...e,type:n,xmlId:$5(n,e.title)}:{...e,type:n})},children:(i?u:l).map(e=>(0,t.jsx)(`option`,{value:e,children:q5[e]},e))})]}),(0,t.jsxs)(`label`,{className:`pretext-plus-editor__toc-edit-field`,children:[(0,t.jsx)(`span`,{children:`Id`}),(0,t.jsx)(`input`,{type:`text`,value:e.xmlId,placeholder:`unique identifier`,onChange:t=>{d.current=!1,o({...e,xmlId:t.target.value})}})]}),(0,t.jsxs)(`div`,{className:`pretext-plus-editor__toc-edit-actions`,children:[(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__toc-edit-save`,onClick:s,children:`Save`}),(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__toc-edit-cancel`,onClick:c,children:`Cancel`})]})]})},t7=({items:e})=>{let[i,a]=(0,n.useState)(!1),[o,s]=(0,n.useState)(null),c=(0,n.useRef)(null),l=(0,n.useRef)(null);(0,n.useEffect)(()=>{if(!i)return;let e=e=>{let t=e.target;c.current&&!c.current.contains(t)&&l.current&&!l.current.contains(t)&&a(!1)},t=e=>{e.key===`Escape`&&a(!1)},n=()=>a(!1);return document.addEventListener(`mousedown`,e),document.addEventListener(`keydown`,t),window.addEventListener(`scroll`,n,!0),window.addEventListener(`resize`,n),()=>{document.removeEventListener(`mousedown`,e),document.removeEventListener(`keydown`,t),window.removeEventListener(`scroll`,n,!0),window.removeEventListener(`resize`,n)}},[i]),(0,n.useLayoutEffect)(()=>{if(!i)return;let e=c.current,t=l.current;if(!e||!t)return;let n=e.getBoundingClientRect(),r=t.getBoundingClientRect(),a=window.innerHeight-n.bottom,o=r.height+4>a&&n.top>a;s({right:Math.max(4,window.innerWidth-n.right),...o?{bottom:window.innerHeight-n.top+3}:{top:n.bottom+3}})},[i]);let u=e=>{a(!1),e()};return(0,t.jsxs)(`div`,{ref:c,className:`pretext-plus-editor__toc-div-menu`,children:[(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__toc-div-menu-trigger`,onClick:e=>{e.stopPropagation(),a(e=>(e||s(null),!e))},"aria-haspopup":`true`,"aria-expanded":i,title:`More options`,children:`⋮`}),i&&(0,r.createPortal)((0,t.jsx)(`div`,{ref:l,className:`pretext-plus-editor__toc-div-menu-popup pretext-plus-editor__toc-div-menu-popup--portal`,style:{visibility:o?`visible`:`hidden`,right:o?.right,top:o?.top,bottom:o?.bottom},children:e.map(e=>(0,t.jsx)(`button`,{type:`button`,className:[`pretext-plus-editor__toc-div-menu-item`,e.danger?`pretext-plus-editor__toc-div-menu-item--danger`:``].filter(Boolean).join(` `),onClick:()=>u(e.onClick),children:e.label},e.label))}),document.body)]})},n7=({division:e,depth:n,isActive:r,hasChildren:i,isExpanded:a,onToggleExpand:o,editDraft:s,onSelect:c,onDraftChange:l,onEditCommit:u,onEditCancel:d,menuItems:f,isNew:p=!1,isRoot:m=!1,parentType:h=null})=>{let g=s!==null,_=e.type===`introduction`||e.type===`conclusion`?q5[e.type]:null;return(0,t.jsxs)(`li`,{className:[`pretext-plus-editor__toc-item`,`pretext-plus-editor__toc-item--${e.type}`,r?`pretext-plus-editor__toc-item--active`:``,g?`pretext-plus-editor__toc-item--editing`:``].filter(Boolean).join(` `),children:[(0,t.jsxs)(`div`,{className:`pretext-plus-editor__toc-item-row`,style:n>0?{paddingLeft:`${n*14}px`}:void 0,children:[(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__toc-expand-btn`,onClick:o,"aria-label":a?`Collapse`:`Expand`,tabIndex:i?0:-1,style:{visibility:i?`visible`:`hidden`},children:a?`▾`:`▸`}),(0,t.jsxs)(`button`,{type:`button`,className:`pretext-plus-editor__toc-select`,onClick:c,"aria-current":r?`true`:void 0,title:q5[e.type]??e.type,children:[(0,t.jsx)(`span`,{className:`pretext-plus-editor__toc-title`,children:e.title||_||(0,t.jsx)(`em`,{children:`Untitled`})}),e.xmlId&&(0,t.jsx)(`span`,{className:`pretext-plus-editor__toc-xmlid`,children:e.xmlId})]}),(0,t.jsx)(`div`,{className:`pretext-plus-editor__toc-actions`,children:(0,t.jsx)(t7,{items:f})})]}),g&&s&&(0,t.jsx)(e7,{draft:s,isNew:p,isRoot:m,parentType:h,onDraftChange:l,onCommit:u,onCancel:d})]})},r7=({onOpenAssetPicker:e,hideAssets:r})=>{let i=m6(e=>e.divisions),a=m6(e=>e.rootDivisionId),o=m6(e=>e.activeDivisionId),s=m6(e=>e.projectAssets)??[],c=m6(e=>e.selectSection),l=m6(e=>e.addSection),u=m6(e=>e.removeSection),d=m6(e=>e.divisionContentChange),f=m6(e=>e.insertAtCursor),p=m6(e=>e.openAssetEditor),m=m6(e=>e.openAssetResolver),h=m6(e=>e.removeAsset),g=m6(e=>e.removeAssetRefFromDocument),_=m6(e=>e.duplicateAsset),v=m6(e=>e.hasAssetDuplicate),y=m6(e=>e.startSectionEdit),b=m6(e=>e.setEditDraft),x=m6(e=>e.commitSectionEdit),S=m6(e=>e.cancelSectionEdit),C=m6(e=>e.editingId),w=m6(e=>e.editDraft),T=m6(e=>e.editingIsNew),E=i?i.find(e=>e.xmlId===a)??i.find(e=>e.type===`book`||e.type===`article`||e.type===`slideshow`)??i[0]??null:null,D=E&&i?b5(i,E.xmlId):[],O=E&&i?x5(i,E.xmlId):[],k=P5(i,s),A=L5.map(e=>({kind:e,rows:k.filter(t=>t.kind===e)})).filter(e=>e.rows.length>0),[ee,te]=(0,n.useState)(!1),[j,M]=(0,n.useState)(null),ne=async e=>{if(!(!e.asset||j)){M(e.ref);try{await _(e.asset)}finally{M(null)}}},[N,re]=(0,n.useState)(new Set),ie=e=>!N.has(e),ae=e=>{re(t=>{let n=new Set(t);return n.has(e)?n.delete(e):n.add(e),n})},[oe,se]=(0,n.useState)(o);if(o!==oe&&(se(o),o&&E)){let e=new Map(D.map(e=>[e.division.xmlId,e])),t=new Set;t.add(E.xmlId);let n=o;for(;n;){let r=e.get(n);if(!r?.parentXmlId)break;t.add(r.parentXmlId),n=r.parentXmlId}re(e=>{if([...t].every(t=>!e.has(t)))return e;let n=new Set(e);return t.forEach(e=>n.delete(e)),n})}let P=new Set(D.map(e=>e.parentXmlId).filter(Boolean)),ce=[];if(E){let e=new Set;ie(E.xmlId)&&e.add(E.xmlId);for(let t of D)t.parentXmlId&&e.has(t.parentXmlId)&&(ce.push(t),ie(t.division.xmlId)&&e.add(t.division.xmlId))}let le=(e,t)=>{if(!i)return;let n=i.find(e=>e.xmlId===t);n&&d(n.xmlId,p5(n.source,e))},ue=(e,t)=>{if(window.confirm(`Delete "${e.title||`Untitled`}"? This permanently removes the division.`)){if(t&&i){let n=i.find(e=>e.xmlId===t);n&&d(n.xmlId,p5(n.source,e.xmlId))}u(e.xmlId)}},de=i?.find(e=>e.xmlId===o)?.sourceFormat??`pretext`,F=e=>{f(de===`markdown`?`::${e.type}{ref="${e.xmlId}"}`:de===`latex`?`\\plus{${e.type}}{${e.xmlId}}`:`<plus:${e.type} ref="${e.xmlId}"/>`)},fe=e=>{E&&d(E.xmlId,f5(E.source,e.xmlId,e.type,null,E.sourceFormat))},pe=e=>e&&i?.find(t=>t.xmlId===e)?.type||null,me=e=>e.status===`unlinked`?m(e.kind,e.ref):p(e.kind,e.ref),he=(e,t)=>{navigator.clipboard.writeText(l5(e,t,de)).catch(()=>{})},ge=e=>{let t=[{label:e.status===`unlinked`?`Link / create asset`:`Edit asset`,onClick:()=>me(e)},{label:`Copy embed code`,onClick:()=>he(e.kind,e.ref)}];return v&&e.asset&&t.push({label:`Duplicate asset`,onClick:()=>ne(e)}),e.status===`unlinked`?t.push({label:`Remove from document`,onClick:()=>g(e.kind,e.ref),danger:!0}):e.asset&&t.push({label:`Remove from project`,onClick:()=>{e.inDocument&&!window.confirm(`Remove "${e.asset.title}" from the project? This also deletes its ${e.inDocument?`reference(s)`:`reference`} from the document.`)||(h(e.asset),g(e.kind,e.ref))},danger:!0}),t};return(0,t.jsxs)(t.Fragment,{children:[(0,t.jsxs)(`ul`,{className:`pretext-plus-editor__toc-list`,role:`list`,children:[E&&(0,t.jsx)(n7,{division:E,depth:0,isActive:o===E.xmlId,hasChildren:P.has(E.xmlId),isExpanded:ie(E.xmlId),onToggleExpand:()=>ae(E.xmlId),editDraft:C===E.xmlId?w:null,onSelect:()=>c(E.xmlId),onDraftChange:b,onEditCommit:x,onEditCancel:S,menuItems:[{label:`Edit properties`,onClick:()=>y(E)},...J8(E.sourceFormat)?[{label:`Add new division`,onClick:()=>l(E.xmlId)}]:[]],isNew:C===E.xmlId&&T,isRoot:!0}),ce.length===0&&E&&ie(E.xmlId)&&(0,t.jsx)(`li`,{className:`pretext-plus-editor__toc-no-sections`,children:(0,t.jsx)(`span`,{children:`No placed divisions`})}),ce.map(e=>(0,t.jsx)(n7,{division:e.division,depth:e.depth+1,isActive:o===e.division.xmlId,hasChildren:P.has(e.division.xmlId),isExpanded:ie(e.division.xmlId),onToggleExpand:()=>ae(e.division.xmlId),editDraft:C===e.division.xmlId?w:null,onSelect:()=>c(e.division.xmlId),onDraftChange:b,onEditCommit:x,onEditCancel:S,menuItems:[{label:`Edit properties`,onClick:()=>y(e.division)},...J8(e.division.sourceFormat)?[{label:`Add new division`,onClick:()=>l(e.division.xmlId)}]:[],{label:`Remove from document`,onClick:()=>le(e.division.xmlId,e.parentXmlId)},{label:`Delete from project`,onClick:()=>ue(e.division,e.parentXmlId),danger:!0}],isNew:C===e.division.xmlId&&T,parentType:pe(e.parentXmlId)},e.division.xmlId))]}),O.length>0&&(0,t.jsxs)(`div`,{className:`pretext-plus-editor__toc-orphans`,children:[(0,t.jsx)(`div`,{className:`pretext-plus-editor__toc-orphans-heading`,children:`Unplaced divisions`}),(0,t.jsx)(`ul`,{className:`pretext-plus-editor__toc-list`,children:O.map(e=>{let r=i?b5(i,e.xmlId):[],a=new Set(r.map(e=>e.parentXmlId).filter(Boolean));return(0,t.jsxs)(n.Fragment,{children:[(0,t.jsx)(n7,{division:e,depth:0,isActive:o===e.xmlId,hasChildren:a.has(e.xmlId),isExpanded:ie(e.xmlId),onToggleExpand:()=>ae(e.xmlId),editDraft:C===e.xmlId?w:null,onSelect:()=>c(e.xmlId),onDraftChange:b,onEditCommit:x,onEditCancel:S,menuItems:[{label:`Edit properties`,onClick:()=>y(e)},{label:`Place in document`,onClick:()=>fe(e)},{label:`Insert at cursor`,onClick:()=>F(e)},{label:`Delete from project`,onClick:()=>ue(e,null),danger:!0}],parentType:null}),ie(e.xmlId)&&r.map(e=>(0,t.jsx)(n7,{division:e.division,depth:e.depth+1,isActive:o===e.division.xmlId,hasChildren:a.has(e.division.xmlId),isExpanded:ie(e.division.xmlId),onToggleExpand:()=>ae(e.division.xmlId),editDraft:C===e.division.xmlId?w:null,onSelect:()=>c(e.division.xmlId),onDraftChange:b,onEditCommit:x,onEditCancel:S,menuItems:[{label:`Edit properties`,onClick:()=>y(e.division)},{label:`Insert at cursor`,onClick:()=>F(e.division)},{label:`Delete from project`,onClick:()=>ue(e.division,e.parentXmlId),danger:!0}],parentType:pe(e.parentXmlId)},e.division.xmlId))]},e.xmlId)})})]}),!r&&(0,t.jsxs)(t.Fragment,{children:[(0,t.jsxs)(`div`,{className:`pretext-plus-editor__toc-assets`,children:[(0,t.jsx)(`div`,{className:`pretext-plus-editor__toc-assets-header`,children:(0,t.jsxs)(`button`,{type:`button`,className:`pretext-plus-editor__toc-assets-toggle`,onClick:()=>te(e=>!e),"aria-expanded":ee,children:[(0,t.jsx)(`span`,{className:`pretext-plus-editor__toc-assets-chevron`,children:ee?`▾`:`▸`}),(0,t.jsx)(`span`,{children:`Assets`}),k.length>0&&(0,t.jsx)(`span`,{className:`pretext-plus-editor__toc-assets-count`,children:k.length})]})}),ee&&(0,t.jsx)(`div`,{className:`pretext-plus-editor__toc-assets-body`,children:k.length===0?(0,t.jsxs)(`p`,{className:`pretext-plus-editor__toc-assets-empty`,children:[`No assets in this project yet.`,` `,e&&(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__toc-assets-add-link`,onClick:e,children:`Add one`})]}):(0,t.jsx)(`div`,{className:`pretext-plus-editor__toc-assets-groups`,children:A.map(({kind:e,rows:n})=>(0,t.jsxs)(`div`,{children:[(0,t.jsx)(`div`,{className:`pretext-plus-editor__toc-assets-group-header`,children:I5[e]}),(0,t.jsx)(`ul`,{className:`pretext-plus-editor__toc-assets-list`,children:n.map(e=>(0,t.jsxs)(`li`,{className:[`pretext-plus-editor__toc-asset-item`,e.status===`unlinked`?`pretext-plus-editor__toc-asset-item--unlinked`:``,e.status===`unused`?`pretext-plus-editor__toc-asset-item--unused`:``,j===e.ref?`pretext-plus-editor__toc-asset-item--busy`:``].filter(Boolean).join(` `),children:[e.asset?.url?(0,t.jsx)(`img`,{src:e.asset.url,className:`pretext-plus-editor__toc-asset-img`,onClick:()=>me(e)}):(0,t.jsx)(`span`,{className:`pretext-plus-editor__toc-asset-img pretext-plus-editor__toc-asset-img--placeholder`,onClick:()=>me(e),title:e.status===`unlinked`?`No asset — click to link`:void 0,"aria-hidden":`true`,children:e.status===`unlinked`?`⚠`:`🖼`}),(0,t.jsxs)(`button`,{type:`button`,className:`pretext-plus-editor__toc-asset-name`,onClick:()=>me(e),title:e.status===`unlinked`?`No asset for this reference — click to link or create one`:`Edit asset`,children:[(0,t.jsx)(`span`,{className:`pretext-plus-editor__toc-asset-label`,children:e.asset?.title??e.ref}),(0,t.jsx)(`span`,{className:`pretext-plus-editor__toc-asset-filename`,children:e.status===`unlinked`?`${e.ref} — needs asset`:e.status===`unused`?`${e.ref} — not placed`:e.ref})]}),(0,t.jsx)(`div`,{className:`pretext-plus-editor__toc-actions`,children:j===e.ref?(0,t.jsx)(`span`,{className:`pretext-plus-editor__toc-asset-spinner`,role:`status`,"aria-label":`Duplicating asset`,title:`Duplicating…`}):(0,t.jsx)(t7,{items:ge(e)})})]},e.ref))})]},e))})})]}),e&&(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__toc-assets-btn`,onClick:e,children:`Manage Assets`})]})]})},i7=({isCollapsed:e,onToggleCollapse:n,onOpenAssetPicker:r,hideAssets:i})=>e?(0,t.jsx)(`div`,{className:`pretext-plus-editor__toc pretext-plus-editor__toc--collapsed`,children:(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__toc-toggle`,onClick:n,"aria-label":`Expand table of contents`,title:`Expand table of contents`,children:`☰`})}):(0,t.jsxs)(`div`,{className:`pretext-plus-editor__toc`,children:[(0,t.jsxs)(`div`,{className:`pretext-plus-editor__toc-header`,children:[(0,t.jsx)(`span`,{className:`pretext-plus-editor__toc-heading`,children:`Contents`}),(0,t.jsx)(`div`,{className:`pretext-plus-editor__toc-header-actions`,children:(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__toc-toggle`,onClick:n,"aria-label":`Collapse table of contents`,title:`Collapse table of contents`,children:`✕`})})]}),(0,t.jsx)(r7,{onOpenAssetPicker:r,hideAssets:i})]}),a7=class extends n.Component{state={error:null};static getDerivedStateFromError(e){return{error:e}}componentDidCatch(e,t){this.props.onError?.(e,t),console.error(`ErrorBoundary caught an error:`,e,t)}componentDidUpdate(e){if(this.state.error===null)return;let{resetKeys:t}=this.props,n=e.resetKeys;t&&n&&(t.length!==n.length||t.some((e,t)=>!Object.is(e,n[t])))&&this.reset()}reset=()=>{this.setState({error:null})};render(){let{error:e}=this.state;return e===null?this.props.children:this.props.fallback?this.props.fallback(e,this.reset):(0,t.jsxs)(`div`,{className:`pretext-plus-editor__error-boundary`,role:`alert`,children:[(0,t.jsx)(`strong`,{children:`Something went wrong rendering this view.`}),(0,t.jsx)(`p`,{className:`pretext-plus-editor__error-boundary-detail`,children:e.message}),(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__error-boundary-retry`,onClick:this.reset,children:`Try again`})]})}},o7=(e,t)=>e.kind===t.kind&&e.ref===t.ref;function s7(e){let t=()=>{},n={cbs:{selectDivision:t,addDivision:t,removeDivision:t,updateDivision:t,divisionContentChange:t,handleDivisionContentChange:t,assetInsert:t,updateTitle:t}};return{store:u6()((t,r)=>({source:e.source,sourceFormat:e.sourceFormat,projectAssets:e.projectAssets,title:e.title,docinfo:e.docinfo,commonDocinfo:e.commonDocinfo,useCommonDocinfo:e.useCommonDocinfo,projectType:e.projectType,projectUrl:void 0,divisions:e.divisions,rootDivisionId:void 0,activeDivisionId:e.activeDivisionId,canConvertToPretext:!0,activeEditorSource:e.source,hasFeedback:!1,hasAssetDuplicate:!1,isTocCollapsed:!1,showFullPreview:!0,isNarrowScreen:typeof window<`u`?window.innerWidth<800:!1,activeTab:`editor`,isLatexDialogOpen:!1,isConvertDialogOpen:!1,isDocinfoEditorOpen:!1,isAssetPickerOpen:!1,isFullSourceOpen:!1,editingId:null,editDraft:null,editingIsNew:!1,editingAssetRef:null,assetResolveTarget:null,syncState:e=>t(e),applyExternalUpdate:e=>t(e),setDivisionContent:(e,n)=>t(t=>{if(!t.divisions)return{};let r=!1,i=t.divisions.map(t=>t.xmlId===e&&t.source!==n?(r=!0,{...t,source:n}):t);return r?{divisions:i}:{}}),patchDivision:(e,n)=>t(t=>t.divisions?{divisions:t.divisions.map(t=>t.xmlId===e?{...t,...n.title!==void 0&&{title:n.title},...n.type!==void 0&&{type:n.type},...n.xmlId!=null&&{xmlId:n.xmlId},...n.sourceFormat!==void 0&&{sourceFormat:n.sourceFormat}}:t)}:{}),addDivisionToPool:e=>t(t=>{let n=t.divisions??[];return n.some(t=>t.xmlId===e.xmlId)?{}:{divisions:[...n,e]}}),removeDivisionFromPool:e=>t(t=>({divisions:(t.divisions??[]).filter(t=>t.xmlId!==e)})),setActiveDivisionId:e=>t({activeDivisionId:e}),setTitle:e=>t({title:e}),setDocinfo:({docinfo:e,commonDocinfo:n,useCommonDocinfo:r})=>t({docinfo:e,commonDocinfo:n,useCommonDocinfo:r}),setShowFullPreview:e=>t({showFullPreview:e}),setActiveTab:e=>t({activeTab:e}),setIsNarrowScreen:e=>t({isNarrowScreen:e}),setIsTocCollapsed:e=>t(t=>({isTocCollapsed:typeof e==`function`?e(t.isTocCollapsed):e})),openModal:e=>t({[e]:!0}),closeModal:e=>t({[e]:!1}),selectSection:e=>n.cbs.selectDivision(e),addSection:e=>n.cbs.addDivision(e),removeSection:e=>n.cbs.removeDivision(e),updateSection:(e,t)=>n.cbs.updateDivision(e,t),divisionContentChange:(e,t)=>n.cbs.divisionContentChange?.(e,t),startSectionEdit:(e,n)=>{let{xmlId:r,label:i}=e.sourceFormat===`markdown`?(()=>{let t=G8(e.source);return{xmlId:t?.xmlId||e.xmlId,label:t?.label??``}})():e.sourceFormat===`latex`?{xmlId:D8(e.source)||e.xmlId,label:``}:(()=>{let t=P8(e.source);return{xmlId:t.xmlId||e.xmlId,label:t.label}})();t({editingId:e.xmlId,editDraft:{title:e.title,type:e.type,xmlId:r,label:i,sourceFormat:e.sourceFormat},editingIsNew:n?.isNew??!1})},setEditDraft:e=>t({editDraft:e}),commitSectionEdit:()=>{let{editingId:e,editDraft:i,divisions:a}=r();if(e&&i){let t=(a??[]).find(t=>t.xmlId===e),r=null;if(t){let t=F8(i.xmlId);if(!t){window.alert(`xml:id can't be empty — it identifies the division and is used by references to it.`);return}if((a??[]).some(n=>n.xmlId!==e&&n.xmlId===t)){window.alert(`xml:id "${t}" is already used by another division. Choose a unique id.`);return}r=t}n.cbs.updateDivision(e,{title:i.title.trim()||void 0,type:i.type,xmlId:r,label:i.label.trim()||null,sourceFormat:i.sourceFormat})}t({editingId:null,editDraft:null,editingIsNew:!1})},cancelSectionEdit:()=>t({editingId:null,editDraft:null,editingIsNew:!1}),insertAsset:e=>n.cbs.assetInsert(e),insertAtCursor:e=>n.cbs.insertContentAtCursor?.(e),openAssetEditor:(e,n)=>t({editingAssetRef:{kind:e,ref:n}}),closeAssetEditor:()=>t({editingAssetRef:null}),openAssetResolver:(e,n)=>t({assetResolveTarget:{kind:e,ref:n}}),closeAssetResolver:()=>t({assetResolveTarget:null}),removeAsset:e=>n.cbs.assetRemove?.(e),removeAssetRefFromDocument:(e,t)=>n.cbs.assetRefRemove?.(e,t),duplicateAsset:async e=>{await n.cbs.assetDuplicate?.(e)},addAssetToPool:e=>t(t=>{let n=t.projectAssets??[];return n.some(t=>o7(t,e))?{}:{projectAssets:[...n,e]}}),updateAssetInPool:e=>t(t=>{let n=t.projectAssets??[];return n.some(t=>o7(t,e))?{projectAssets:n.map(t=>o7(t,e)?e:t)}:{projectAssets:[...n,e]}}),renameAssetInPool:(e,n,r)=>t(t=>({projectAssets:[...(t.projectAssets??[]).filter(t=>!(t.kind===e&&t.ref===n)&&!o7(t,r)),r]})),removeAssetFromPool:e=>t(t=>({projectAssets:(t.projectAssets??[]).filter(t=>!o7(t,e))})),updateTitle:e=>n.cbs.updateTitle(e),feedbackSubmit:e=>n.cbs.feedbackSubmit?.(e)})),bindCallbacks:e=>{n.cbs=e}}}function c7({store:e,children:n}){return(0,t.jsx)(p6.Provider,{value:e,children:n})}var l7=(e,t)=>e.find(e=>t?e.xmlId===t:e.type===`book`||e.type===`article`||e.type===`slideshow`)??e[0]??null,u7=e=>{let[r]=(0,n.useState)(()=>{let t=l7(e.divisions,e.rootDivisionId),n=N5(e.divisions,t?.xmlId,e.projectType,e.title),r=n.find(e=>e.xmlId===t?.xmlId)??t,i=e.activeDivisionId??r?.xmlId??null,a=n.find(e=>e.xmlId===i)??r;return s7({source:a?.source??``,sourceFormat:a?.sourceFormat??`pretext`,title:e.title||r?.title||`Document Title`,docinfo:e.docinfo??``,commonDocinfo:e.commonDocinfo??``,useCommonDocinfo:e.useCommonDocinfo??!1,projectType:e.projectType,divisions:n,activeDivisionId:i,projectAssets:e.projectAssets})});return(0,t.jsx)(c7,{store:r.store,children:(0,t.jsx)(d7,{...e,bindCallbacks:r.bindCallbacks})})},d7=e=>{let{bindCallbacks:r}=e,i=m6(e=>e.showFullPreview),a=m6(e=>e.isNarrowScreen),o=m6(e=>e.setIsNarrowScreen),s=m6(e=>e.activeTab),c=m6(e=>e.setActiveTab),l=m6(e=>e.isTocCollapsed),u=m6(e=>e.setIsTocCollapsed),d=m6(e=>e.isLatexDialogOpen),f=m6(e=>e.isConvertDialogOpen),p=m6(e=>e.isDocinfoEditorOpen),m=m6(e=>e.isAssetPickerOpen),h=m6(e=>e.isFullSourceOpen),g=m6(e=>e.editingAssetRef),_=m6(e=>e.openAssetEditor),v=m6(e=>e.closeAssetEditor),y=m6(e=>e.assetResolveTarget),b=m6(e=>e.closeAssetResolver),[x,S]=(0,n.useState)(null),C=m6(e=>e.openModal),w=m6(e=>e.closeModal),T=m6(e=>e.syncState),E=m6(e=>e.projectAssets),D=m6(e=>e.addAssetToPool),O=m6(e=>e.updateAssetInPool),k=m6(e=>e.renameAssetInPool),A=m6(e=>e.removeAssetFromPool),ee=g?E?.find(e=>e.kind===g.kind&&e.ref===g.ref):void 0,te=m6(e=>e.divisions),j=(0,n.useMemo)(()=>te??[],[te]),M=m6(e=>e.activeDivisionId),ne=m6(e=>e.title),N=m6(e=>e.docinfo),re=m6(e=>e.commonDocinfo),ie=m6(e=>e.useCommonDocinfo),ae=m6(e=>e.applyExternalUpdate),oe=m6(e=>e.setDivisionContent),se=m6(e=>e.patchDivision),P=m6(e=>e.addDivisionToPool),ce=m6(e=>e.removeDivisionFromPool),le=m6(e=>e.setActiveDivisionId),ue=m6(e=>e.startSectionEdit),de=m6(e=>e.setTitle),F=m6(e=>e.setDocinfo),fe=m6(e=>e.editingId),pe=m6(e=>e.editingIsNew),me=(0,n.useRef)(null),he=(0,n.useRef)(null),ge=(0,n.useRef)(!1);(0,n.useEffect)(()=>{fe&&pe?ge.current=!0:ge.current&&(ge.current=!1,he.current?.focus())},[fe,pe]);let I=l7(j,e.rootDivisionId),_e=j.find(e=>e.xmlId===M)??j[0]??null,ve=_e?.sourceFormat??`pretext`,ye=_e?.source??``,be=(t,n,r,i)=>{oe(t,n),e.onContentChange({xmlId:t,source:n,sourceFormat:r,pretextSource:r===`pretext`?n:void 0,...i})},xe=(t,n)=>{se(t,n),e.onDivisionUpdate?.(t,n)},Se=(e,t)=>{let n=j.find(t=>t.xmlId===e);if(!n){xe(e,t);return}let r;if(t.sourceFormat!==void 0&&t.sourceFormat!==n.sourceFormat){let e=t.type??n.type,i=t.title??n.title,a=t.xmlId||n.xmlId;r={...n,sourceFormat:t.sourceFormat,type:e,title:i,xmlId:a,source:d5(e,t.sourceFormat,i,a)}}else r=n.sourceFormat===`markdown`?K8(n,t):n.sourceFormat===`latex`?O8(n,t):R8(n,t);let i=r.xmlId;if(r.source!==n.source&&be(n.xmlId,r.source,r.sourceFormat),xe(e,{...t,xmlId:i}),i!==n.xmlId||r.type!==n.type){let e=g5(j,n.xmlId);if(e){let t=h5(e.source,n.xmlId,i,r.type);t!==e.source&&be(e.xmlId,t,e.sourceFormat)}}i!==n.xmlId&&we(i)},Ce=t=>{let n={...t,id:void 0};P(n),e.onDivisionAdd?.(n)},L=t=>{ce(t),e.onDivisionRemove?.(t)},we=t=>{le(t),e.onDivisionSelect?.(t)},Te=(0,n.useMemo)(()=>{if(!_e||ve===`pretext`)return;if(ve===`latex`)return k8(_e)??void 0;let e=o6(ye,ve);return e.pretextError?void 0:e.pretextSource},[_e,ve,ye]),Ee=e=>{if(!_e)return;let t=e||``;if(ve===`pretext`){let e=L8(t);e&&e.xmlId!==_e.xmlId&&(t=R8({..._e,source:t,title:e.title,type:e.type},{xmlId:_e.xmlId,label:e.label||null}).source)}else if(ve===`markdown`){let e=G8(t);e&&e.xmlId!==_e.xmlId&&(t=K8({..._e,source:t},{xmlId:_e.xmlId}).source)}if(t===_e.source)return;if(be(_e.xmlId,t,ve),ve===`pretext`){let e=L8(t);if(e&&(xe(_e.xmlId,{title:e.title,type:e.type,label:e.label||null}),e.type!==_e.type)){let t=g5(j,_e.xmlId);if(t){let n=h5(t.source,_e.xmlId,_e.xmlId,e.type);n!==t.source&&be(t.xmlId,n,t.sourceFormat)}}}else if(ve===`markdown`){let e=G8(t);if(e&&(xe(_e.xmlId,{title:e.title,type:e.type,label:e.label||null}),e.type!==_e.type)){let t=g5(j,_e.xmlId);if(t){let n=h5(t.source,_e.xmlId,_e.xmlId,e.type);n!==t.source&&be(t.xmlId,n,t.sourceFormat)}}}else if(ve===`latex`){let e=E8(t);e!==null&&xe(_e.xmlId,{title:e})}let n=new Set(j.map(e=>e.xmlId));for(let{xmlId:e,type:r}of a5(t,ve))n.has(e)||(Ce(u5(e,r,ve)),n.add(e))},R=e=>{we(e)},De=()=>{_e&&(u(!1),ue(_e))},Oe=e=>{let t=h8();if(Ce(t),e){let n=j.find(t=>t.xmlId===e);n&&be(n.xmlId,f5(n.source,t.xmlId,t.type,null,n.sourceFormat),n.sourceFormat)}le(t.xmlId),ue(t,{isNew:!0})},ke=e=>{D(e)},z=t=>{A(t),e.onAssetRemove?.(t)},Ae=!!(e.onAssetUpload&&e.onAssetFetchUrl&&e.onAssetUpdate),je=!!(e.onAssetRemove&&e.onAssetUpload),Me=async t=>{if(!e.onAssetUpload||!e.onAssetFetchUrl||!t.ref||!t.url)return;let n=new Set(P5(j,E).map(e=>e.ref)),r=F5(t.ref,n),i=await e.onAssetFetchUrl(t.url),a=/\.[^./\\]+$/.exec(i.name)?.[0]??``,o=new File([i],`${r}${a}`,{type:i.type}),s={...await e.onAssetUpload(o),ref:r,title:`${t.title} (copy)`,source:t.source};await e.onAssetUpdate?.(s),D(s),_(s.kind,r)},Ne=async(t,n)=>{let r={...n,ref:t.ref,title:t.title,source:t.source};await e.onAssetUpdate?.(r),O(r),e.onAssetRemove?.(t),A(t)},Pe=(e,t,n)=>{if(t!==n)for(let r of j){let i=s5(r.source,e,t,n);i!==r.source&&be(r.xmlId,i,r.sourceFormat)}},Fe=(e,t)=>{for(let n of j){let r=c5(n.source,e,t);r!==n.source&&be(n.xmlId,r,n.sourceFormat)}};(0,n.useEffect)(()=>{let e=()=>o(window.innerWidth<800);return window.addEventListener(`resize`,e),()=>window.removeEventListener(`resize`,e)},[o]);let Ie=ve!==`pretext`;(0,n.useLayoutEffect)(()=>{r({selectDivision:R,addDivision:e=>Oe(e),removeDivision:e=>L(e),updateDivision:(e,t)=>Se(e,t),divisionContentChange:(e,t)=>{be(e,t,j.find(t=>t.xmlId===e)?.sourceFormat??`pretext`)},handleDivisionContentChange:Ee,assetInsert:ke,assetRemove:z,assetRefRemove:Fe,assetDuplicate:Ae?Me:void 0,insertContentAtCursor:e=>he.current?.insertAtCursor(e),updateTitle:t=>{de(t),e.onTitleChange?.(t)},feedbackSubmit:e.onFeedbackSubmit})}),(0,n.useEffect)(()=>{T({source:ye,sourceFormat:ve,projectType:e.projectType,projectUrl:e.projectUrl,rootDivisionId:I?.xmlId,canConvertToPretext:Te!==void 0,activeEditorSource:ye,hasFeedback:e.onFeedbackSubmit!==void 0,hasAssetDuplicate:Ae})});let Le=(0,n.useRef)({divisions:e.divisions,projectAssets:e.projectAssets,title:e.title,docinfo:e.docinfo,commonDocinfo:e.commonDocinfo,useCommonDocinfo:e.useCommonDocinfo,activeDivisionId:e.activeDivisionId});(0,n.useEffect)(()=>{let t=Le.current,n={},r=!1;if(e.projectAssets!==void 0&&e.projectAssets!==t.projectAssets&&(n.projectAssets=e.projectAssets,r=!0),e.divisions!==t.divisions){let t=l7(e.divisions,e.rootDivisionId),i=N5(e.divisions,t?.xmlId,e.projectType,e.title);n.divisions=i;let a=i.find(e=>e.xmlId===t?.xmlId)??t;(M==null||!e.divisions.some(e=>e.xmlId===M))&&(n.activeDivisionId=a?.xmlId??null),!e.title&&a?.title&&(n.title=a.title),r=!0}e.title!==void 0&&e.title!==t.title&&(n.title=e.title,r=!0),e.docinfo!==void 0&&e.docinfo!==t.docinfo&&(n.docinfo=e.docinfo,r=!0),e.commonDocinfo!==void 0&&e.commonDocinfo!==t.commonDocinfo&&(n.commonDocinfo=e.commonDocinfo,r=!0),e.useCommonDocinfo!==void 0&&e.useCommonDocinfo!==t.useCommonDocinfo&&(n.useCommonDocinfo=e.useCommonDocinfo,r=!0),e.activeDivisionId!==void 0&&e.activeDivisionId!==t.activeDivisionId&&(n.activeDivisionId=e.activeDivisionId,r=!0),r&&ae(n),Le.current={divisions:e.divisions,projectAssets:e.projectAssets,title:e.title,docinfo:e.docinfo,commonDocinfo:e.commonDocinfo,useCommonDocinfo:e.useCommonDocinfo,activeDivisionId:e.activeDivisionId}});let Re=(0,n.useRef)(null);(0,n.useEffect)(()=>{if(Re.current===e.divisions)return;Re.current=e.divisions;let t=l7(e.divisions,e.rootDivisionId),n=N5(e.divisions,t?.xmlId,e.projectType,e.title);for(let t of n){if(t.sourceFormat!==`pretext`)continue;let n=e.divisions.find(e=>e.xmlId===t.xmlId);!n||t.source===n.source||e.onContentChange({xmlId:t.xmlId,source:t.source,sourceFormat:`pretext`,pretextSource:t.source})}});let ze=ie?re:N,Be=(0,n.useMemo)(()=>{if(!h||!I)return``;try{return k5(j,I.xmlId,ze,E??[])}catch(e){return`<!-- Unable to assemble document source: ${e instanceof Error?e.message:String(e)} -->`}},[h,I,j,ze,E]),Ve=_e?D5(j,_e.xmlId,E):void 0,He=_e&&Ve!==void 0?j5(_e.type,Ve,ze,_e.title):void 0,Ue=()=>me.current?.rebuild(),B=()=>{e.onSave?.(),me.current?.rebuild()},We=t=>{let n=t.ctrlKey||t.metaKey;n&&t.key===`Enter`&&e.onPreviewRebuild?(t.preventDefault(),Ue()):n&&t.key===`s`&&(t.preventDefault(),B())},Ge=()=>{if(!_e||!Te)return;let e=h8(),t={id:e.xmlId,xmlId:_e.xmlId,title:_e.title,type:_e.type,sourceFormat:ve,source:_e.source};Ce(ve===`markdown`?K8(t,{xmlId:e.xmlId}):O8(t,{xmlId:e.xmlId})),be(_e.xmlId,Te,`pretext`),xe(_e.xmlId,{sourceFormat:`pretext`})},Ke=(0,t.jsx)(Ws,{ref:he,content:ye,sourceFormat:ve,onChange:Ee,onRebuild:e.onPreviewRebuild?Ue:void 0,onSave:B,onOpenLatexImport:()=>C(`isLatexDialogOpen`),onOpenDocinfoEditor:()=>C(`isDocinfoEditorOpen`),onOpenConvertToPretext:Ie&&Te!==void 0?()=>C(`isConvertDialogOpen`):void 0,canConvertToPretext:Te!==void 0,onOpenAssets:e.projectAssets!==void 0&&ve===`pretext`?()=>C(`isAssetPickerOpen`):void 0,onShowFullSource:()=>C(`isFullSourceOpen`),onRequestWrapperEdit:De,hideAssets:e.hideAssets}),V;V=i&&e.onPreviewRebuild?(0,t.jsx)(lR,{ref:me,content:He||``,title:ne,onRebuild:e.onPreviewRebuild}):(0,t.jsx)(oR,{content:ye,canEdit:ve===`pretext`,editDisabledReason:ve===`markdown`?`Visual editing is not available for Markdown documents.`:ve===`latex`?`Visual editing is not available for LaTeX documents.`:``,onChange:e=>Ee(e)});let qe=(0,t.jsx)(i7,{isCollapsed:l,onToggleCollapse:()=>u(e=>!e),hideAssets:e.hideAssets,onOpenAssetPicker:e.projectAssets===void 0?void 0:()=>C(`isAssetPickerOpen`)}),Je=`pretext-plus-tab-editor`,Ye=`pretext-plus-tab-preview`,Xe=`pretext-plus-tabpanel`,Ze;return Ze=a?(0,t.jsxs)(`div`,{className:`pretext-plus-editor__tabs`,children:[qe,(0,t.jsxs)(`div`,{className:`pretext-plus-editor__tabs-main`,children:[(0,t.jsxs)(`div`,{className:`pretext-plus-editor__tab-list`,role:`tablist`,children:[(0,t.jsx)(`button`,{type:`button`,id:Je,role:`tab`,"aria-controls":Xe,"aria-selected":s===`editor`,tabIndex:s===`editor`?0:-1,className:`pretext-plus-editor__tab-button ${s===`editor`?`is-active`:``}`,onClick:()=>c(`editor`),children:`Editor`}),(0,t.jsx)(`button`,{type:`button`,id:Ye,role:`tab`,"aria-controls":Xe,"aria-selected":s===`preview`,tabIndex:s===`preview`?0:-1,className:`pretext-plus-editor__tab-button ${s===`preview`?`is-active`:``}`,onClick:()=>c(`preview`),children:`Preview`})]}),(0,t.jsx)(`div`,{id:Xe,className:`pretext-plus-editor__tab-panel`,role:`tabpanel`,"aria-labelledby":s===`editor`?Je:Ye,children:(0,t.jsx)(`div`,{style:{height:`100%`},children:s===`editor`?Ke:V})})]})]}):(0,t.jsxs)(`div`,{className:`pretext-plus-editor__sectioned-layout`,children:[qe,(0,t.jsxs)(gt,{orientation:`horizontal`,className:`pretext-plus-editor__splitter`,children:[(0,t.jsx)(yt,{className:`pretext-plus-editor__editor-panel`,children:Ke}),(0,t.jsx)(St,{className:`pretext-plus-editor__resize-handle`,children:(0,t.jsx)(`div`,{className:`pretext-plus-editor__resize-dots`})}),(0,t.jsx)(yt,{className:`pretext-plus-editor__preview-panel`,children:V})]})]}),(0,t.jsxs)(`div`,{className:`pretext-plus-editor`,onKeyDown:We,children:[(0,t.jsx)(W5,{onSaveButton:e.onSaveButton,saveButtonLabel:e.saveButtonLabel,onCancelButton:e.onCancelButton,cancelButtonLabel:e.cancelButtonLabel,showPreviewModeToggle:e.onPreviewRebuild!==void 0}),(0,t.jsxs)(`div`,{className:`pretext-plus-editor__editor-displays`,children:[(0,t.jsx)(a7,{resetKeys:[ye,M],children:Ze}),d?(0,t.jsx)(g6,{onClose:()=>w(`isLatexDialogOpen`)}):null,f&&_e&&Te?(0,t.jsx)(b6,{source:ye,sourceFormat:ve,pretextSource:Te,onConfirm:Ge,onClose:()=>w(`isConvertDialogOpen`)}):null,p?(0,t.jsx)(M6,{docinfo:N,showCommonDocinfoControls:!0,commonDocinfo:re,initialUseCommonDocinfo:ie,onClose:t=>{if(w(`isDocinfoEditorOpen`),t!==void 0){F({docinfo:t.docinfo,commonDocinfo:t.commonDocinfo,useCommonDocinfo:t.useCommonDocinfo}),e.onCommonDocinfoChange?.(t.commonDocinfo),e.onUseCommonDocinfoChange?.(t.useCommonDocinfo);let n=I??_e;be(n?.xmlId??``,n?.source??``,n?.sourceFormat??`pretext`,{docinfo:t.docinfo,commonDocinfo:t.commonDocinfo,useCommonDocinfo:t.useCommonDocinfo})}}}):null,(m||y||x)&&e.projectAssets!==void 0?(0,t.jsx)(z5,{open:m||!!y||!!x,resolveTarget:y,replaceTarget:x,onClose:()=>{w(`isAssetPickerOpen`),b(),S(null)},onUpload:e.onAssetUpload,onFetchUrl:e.onAssetFetchUrl,onCreateDoenet:e.onCreateDoenet,onRemoveAsset:e.onAssetRemove?z:void 0,onDuplicateAsset:Ae?Me:void 0,onAssetAdded:ke,onResolveRef:Pe,onReplaceAsset:Ne}):null,h?(0,t.jsx)(P6,{source:Be,onClose:()=>w(`isFullSourceOpen`)}):null,ee?(0,t.jsx)(U5,{asset:ee,projectAssets:E??[],onClose:v,onReplace:je?e=>{v(),S(e)}:void 0,onDuplicate:Ae?e=>Me(e):void 0,onSave:async(t,n)=>{t.ref&&t.ref!==n?(Pe(t.kind,n,t.ref),k(t.kind,n,t)):O(t),await e.onAssetUpdate?.(t)}},`${ee.kind}:${ee.ref}`):null]})]})};e.CodeEditor=Ws,e.DocinfoEditor=M6,e.Editors=u7,e.FeedbackLink=c6,e.FullPreview=lR,e.VisualEditor=oR,e.assembleFullProjectSource=k5,e.assembleProjectSource=D5,e.buildDivisionTree=b5,e.convertLatexToPretext=r6,e.createConclusion=_8,e.createIntroduction=g8,e.createLatexConclusion=M8,e.createLatexIntroduction=j8,e.createNewLatexSection=A8,e.createNewSection=h8,e.derivePretextContent=o6,e.detectSourceFormat=n6,e.ensureLatexSectionWrapper=C8,e.ensureSectionWrapper=b8,e.extractDivisionMetadata=L8,e.extractLatexDivisionTitle=E8,e.findDivisionParent=g5,e.getOrphanRoots=x5,e.getOrphanedDivisions=y5,e.getSectionAttributes=P8,e.insertDivisionRef=f5,e.mergeTwoSections=N8,e.moveDivisionRef=m5,e.parseDivisionRefs=i5,e.postToIframe=cR,e.removeDivisionRef=p5,e.renameDivisionRef=h5,e.reorderDivisionRefs=_5,e.rewrapLatexSection=S8,e.rewrapSection=y8,e.stripLatexSectionWrapper=x8,e.stripSectionWrapper=v8,e.updateDivisionTitle=m8,e.updateLatexSectionTitle=T8,e.updateSectionMetadata=R8,e.wrapDivisionForPreview=j5});
1093
1093
  //# sourceMappingURL=index.js.map