@pretextbook/web-editor 0.8.0 → 0.8.1

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
@@ -1080,5 +1080,5 @@ Expected it to be ${r}.`}var QU=class extends Error{name=`InvalidDocError`;const
1080
1080
  `+e.slice(a)}function E6(e,t){let n=RegExp(`[ \t]*${b6(t)}[ \t]*\n?`,`g`);return e.replace(n,``)}function D6(e,t,n){let r=new RegExp(b6(t)).exec(e),i=r?j6(r[0]):`<plus:division ref="${t}"/>`,a=E6(e,t);if(n!==null){let e=new RegExp(b6(n)).exec(a);if(e){let t=e.index+e[0].length;return a.slice(0,t)+`
1081
1081
  `+i+a.slice(t)}}let o=a.lastIndexOf(`</`);return o===-1?a+`
1082
1082
  `+i:a.slice(0,o)+i+`
1083
- `+a.slice(o)}function O6(e,t,n,r){let i=new RegExp(b6(t));return i.test(e)?e.replace(i,`<plus:${r} ref="${n}"/>`):e}function k6(e,t){let n=new RegExp(b6(t));return e.find(e=>n.test(e.content))??null}function A6(e,t){let n=e,r=null;for(let e of t)n=D6(n,e,r),r=e;return n}function j6(e){return e.replace(/<plus:([a-z-]+)((?:\s[^>]*?)?)>\s*<\/plus:\1>/g,(e,t,n)=>`<plus:${t}${n}/>`)}function M6(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 x6(i.content))r.push(e)}return n}function N6(e,t){let n=M6(e,t);return e.filter(e=>!n.has(e.xmlId))}function P6(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 x6(o.content)){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 F6(e,t){let n=N6(e,t),r=new Set(n.map(e=>e.xmlId)),i=new Set;for(let e of n)for(let t of x6(e.content))r.has(t)&&i.add(t);return n.filter(e=>!i.has(e.xmlId))}var I6=new Set([`book`,`article`,`slideshow`]);function L6(e){try{let t=O3(e),n=t.children.find(e=>e.type===`element`),r=n?.name===`pretext`?n.children.find(e=>e.type===`element`&&I6.has(e.name)):n&&I6.has(n.name)?n:void 0;if(!r)return e;if(!r.attributes.label){let e=z6(t,`pretext-plus-preview`);return r.attributes.label=e,TY(t)}return e}catch(t){return console.error(`Error ensuring label:`,t),e}}function R6(e,t){return e.type===`element`&&e.attributes?.label===t?!0:`children`in e&&e.children?e.children.some(e=>e.type===`element`?R6(e,t):!1):!1}function z6(e,t){let n=t,r=1;for(;R6(e,n);)n=`${t}-${r}`,r++;return n}function B6(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.content;else{let{pretextSource:e,pretextError:t}=Z4(i.sourceFormat===`latex`?a6(i.content,i.type):n6(i.content),i.sourceFormat),n=e??`<!-- conversion error: ${t} -->`;a=`<${i.type} xml:id="${i.xmlId}">\n<title>${i.title}</title>\n\n${n}\n</${i.type}>`}if(i.sourceFormat!==`pretext`)return a;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(!U3.has(n))return B6(a,t,o,r);let s=/width="([^"]+)"/.exec(i)?.[1];return W3(n,a,r,s)})}function V6(e,t,n=[]){return L6(B6(t,e,new Set,n))}function H6(e,t){return L6(`<pretext>\n${t.trim()?`${t.trim()}\n`:``}${e}\n</pretext>`)}function U6(e,t,n,r=[]){return H6(B6(t,e,new Set,r),n)}var W6=new Set([`part`,`chapter`]);function G6(e,t,n,r){return H6(I6.has(e)?t:W6.has(e)?`<book>\n<title>${r}</title>\n${t}\n</book>`:`<article>\n<title>${r}</title>\n${t}\n</article>`,n)}function K6(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 q6(e,t,n){let r=n===`book`?`book`:`article`;return e.map(e=>{if(e.sourceFormat!==`pretext`)return e;let n=h6(e.content);if(e.xmlId===t&&!(n&&I6.has(n.type))){let{title:t,body:n}=K6(e.content),i=e.title||t||`Untitled`;return{...e,type:r,title:i,content:`<${r} xml:id="${e.xmlId}">\n<title>${i}</title>\n\n${n}\n</${r}>`}}return!e.title&&n?.title?{...e,title:n.title}:e})}var J6={image:`Images`,doenet:`Doenet`},Y6=[`image`,`doenet`].filter(e=>e!==`doenet`||!1);function X6(e){let t=new Set,n=[];for(let{kind:r,ref:i}of C6(e)){let e=`${r}:${i}`;t.has(e)||(t.add(e),n.push({kind:r,ref:i}))}return n}var Z6=({open:e,onClose:r,onLoadAssets:i,onLoadLibraryAssets:a,onInsert:o,onAddFromLibrary:s,onUpload:c,onFetchUrl:l,onCreateDoenet:u,onRemoveAsset:d})=>{let f=a3(e=>e.activeEditorSource),p=a3(e=>e.projectAssets)??[],m=a3(e=>e.setProjectAssets),h=a3(e=>e.libraryAssets),g=a3(e=>e.openAssetEditor),_=!!(i||a),[v,y]=(0,n.useState)(null),[b,x]=(0,n.useState)(!1),[S,C]=(0,n.useState)(null),w=p,T=v??h??w,E=new Set(w.map(e=>e.id)),[D,O]=(0,n.useState)(`in-document`),[k,A]=(0,n.useState)(null),[j,ee]=(0,n.useState)(`library`),[te,ne]=(0,n.useState)(`library`),[re,M]=(0,n.useState)(null),[ie,ae]=(0,n.useState)(!1),[oe,N]=(0,n.useState)(!1),[se,P]=(0,n.useState)(null),ce=(0,n.useRef)(null),[le,ue]=(0,n.useState)(``),[de,F]=(0,n.useState)(``),[fe,pe]=(0,n.useState)(!1),[me,he]=(0,n.useState)(null),[ge,I]=(0,n.useState)(``),[_e,ve]=(0,n.useState)(``),[ye,be]=(0,n.useState)(!1),[xe,Se]=(0,n.useState)(null),[Ce,L]=(0,n.useState)(null),we=(0,n.useCallback)(()=>{!i&&!a||(x(!0),C(null),Promise.all([i?.()??Promise.resolve(null),a?.()??Promise.resolve(null)]).then(([e,t])=>{e!==null&&m(e),t!==null&&y(t)}).catch(e=>{C(e instanceof Error?e.message:`Failed to load assets.`)}).finally(()=>x(!1)))},[i,a,m]);if((0,n.useEffect)(()=>{if(!e)return;we();let t=e=>{e.key===`Escape`&&r()};return window.addEventListener(`keydown`,t),()=>window.removeEventListener(`keydown`,t)},[e,r,we]),!e)return null;let Te=X6(f),Ee=e=>{o(e),r()},R=(e,t)=>{let n=`${e}:${t}`;navigator.clipboard.writeText(`<plus:${e} ref="${t}"/>`).catch(()=>{}),L(n),setTimeout(()=>L(e=>e===n?null:e),2e3)},De=async e=>{if(!E.has(e.id)&&s){M(e.id);try{await s(e)}finally{M(null)}}Ee(e)},Oe=async e=>{if(c){P(null),N(!0);try{Ee(await c(e))}catch(e){P(e instanceof Error?e.message:`Upload failed.`),N(!1)}}},ke=async()=>{let e=le.trim();if(!e)return;let t=de.trim();if(l&&c){he(null),pe(!0);try{let n=await l(e);Ee(await c(t?new File([n],t,{type:n.type}):n))}catch(e){he(e instanceof Error?e.message:`Failed to add URL.`),pe(!1)}}else Ee({id:`url-${Date.now()}`,name:t||e,ref:e.split(`/`).pop()??`image`,kind:`image`,url:e})},z=async()=>{let e=ge.trim(),t=_e.trim();if(!(!e||!t))if(u){Se(null),be(!0);try{Ee(await u(e,t))}catch(e){Se(e instanceof Error?e.message:`Failed to create activity.`),be(!1)}}else Ee({id:`doenet-${Date.now()}`,name:e,ref:t,kind:`doenet`})},Ae=e=>{let n=T.filter(t=>t.kind===e);return(0,t.jsxs)(`div`,{className:`pretext-plus-editor__am-library`,children:[_&&(0,t.jsxs)(`div`,{className:`pretext-plus-editor__am-toolbar`,children:[S&&(0,t.jsx)(`span`,{className:`pretext-plus-editor__asset-load-error`,children:S}),(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__asset-library-refresh`,onClick:we,disabled:b,children:b?`Loading…`:`Refresh`})]}),b&&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 `,J6[e].toLowerCase(),` assets in your library.`]}):(0,t.jsx)(`ul`,{className:`pretext-plus-editor__am-list`,children:n.map(e=>{let n=E.has(e.id),r=re===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:()=>De(e),disabled:r||!e.ref,title:e.ref?n?`Insert "${e.ref}"`:`Add to project & insert "${e.ref}"`:`Asset has no reference — cannot insert`,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.name}),(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)})})]})};return(0,t.jsx)(`div`,{className:`pretext-plus-editor__dialog-overlay`,onClick:e=>e.target===e.currentTarget&&r(),children:(0,t.jsxs)(`div`,{className:`pretext-plus-editor__dialog pretext-plus-editor__dialog--asset-manager`,role:`dialog`,"aria-modal":`true`,"aria-label":`Asset manager`,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:`✕`})]}),(0,t.jsxs)(`div`,{className:`pretext-plus-editor__dialog-tab-bar`,children:[(0,t.jsxs)(`button`,{type:`button`,className:`pretext-plus-editor__dialog-tab${D===`in-document`?` pretext-plus-editor__dialog-tab--active`:``}`,onClick:()=>O(`in-document`),children:[`In Document`,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${D===`add`?` pretext-plus-editor__dialog-tab--active`:``}`,onClick:()=>{O(`add`),A(null)},children:`Add Asset`})]}),(0,t.jsx)(`div`,{className:`pretext-plus-editor__dialog-content pretext-plus-editor__dialog-content--single`,children:D===`in-document`?(()=>{if(Te.length===0)return(0,t.jsxs)(`div`,{className:`pretext-plus-editor__am-placeholder`,children:[(0,t.jsx)(`p`,{children:`No assets referenced in this document yet.`}),(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__dialog-button`,onClick:()=>{O(`add`),A(null)},children:`Add an asset`})]});let e=Y6.reduce((e,t)=>(e[t]=Te.filter(e=>e.kind===t).map(e=>({ref:e.ref,asset:w.find(t=>t.kind===e.kind&&t.ref===e.ref)})),e),{image:[],doenet:[]});return(0,t.jsx)(`div`,{className:`pretext-plus-editor__am-in-doc`,children:Y6.filter(t=>e[t].length>0).map(n=>(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:J6[n]}),(0,t.jsx)(`span`,{className:`pretext-plus-editor__am-kind-count`,children:e[n].length})]}),(0,t.jsx)(`ul`,{className:`pretext-plus-editor__am-list`,children:e[n].map(({ref:e,asset:i})=>{let a=`${n}:${e}`;return(0,t.jsxs)(`li`,{className:`pretext-plus-editor__am-doc-row`,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:i?.name??e}),(0,t.jsx)(`span`,{className:`pretext-plus-editor__am-row-ref`,children:e})]}),(0,t.jsxs)(`div`,{className:`pretext-plus-editor__am-row-actions`,children:[i&&(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__am-action-btn`,onClick:()=>Ee(i),title:`Insert <plus:${n} ref="${e}"/>`,children:`Insert`}),i&&(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__am-action-btn`,onClick:()=>g(n,e),title:`Edit asset content`,children:`Edit`}),(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__am-action-btn${Ce===a?` pretext-plus-editor__am-action-btn--done`:``}`,onClick:()=>R(n,e),title:`Copy <plus:${n} ref="${e}"/>`,children:Ce===a?`Copied!`:`Copy`}),d&&i&&(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__am-action-btn pretext-plus-editor__am-action-btn--danger`,onClick:()=>{d(i),r()},title:`Remove from project`,children:`Remove`})]})]},e)})})]},n))})})():k===null?(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:()=>{A(`image`),ee(`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`})]})]})]}):k===`image`?(0,t.jsxs)(`div`,{className:`pretext-plus-editor__am-configure`,children:[(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__am-back-btn`,onClick:()=>A(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${j===`library`?` pretext-plus-editor__dialog-tab--active`:``}`,onClick:()=>ee(`library`),children:`Library`}),c&&(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__dialog-tab${j===`upload`?` pretext-plus-editor__dialog-tab--active`:``}`,onClick:()=>ee(`upload`),children:`Upload`}),(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__dialog-tab${j===`url`?` pretext-plus-editor__dialog-tab--active`:``}`,onClick:()=>ee(`url`),children:`External URL`})]}),(0,t.jsxs)(`div`,{className:`pretext-plus-editor__am-configure-body`,children:[j===`library`&&Ae(`image`),j===`upload`&&c&&(0,t.jsxs)(`div`,{className:`pretext-plus-editor__am-upload`,children:[(0,t.jsxs)(`div`,{className:[`pretext-plus-editor__am-drop-zone`,ie?`pretext-plus-editor__am-drop-zone--active`:``,oe?`pretext-plus-editor__am-drop-zone--uploading`:``].filter(Boolean).join(` `),onDragOver:e=>{e.preventDefault(),ae(!0)},onDragLeave:()=>ae(!1),onDrop:e=>{e.preventDefault(),ae(!1);let t=e.dataTransfer.files[0];t&&Oe(t)},onClick:()=>!oe&&ce.current?.click(),role:`button`,tabIndex:0,onKeyDown:e=>{(e.key===`Enter`||e.key===` `)&&(e.preventDefault(),ce.current?.click())},"aria-label":`Upload image — click or drag and drop`,children:[(0,t.jsx)(`input`,{ref:ce,type:`file`,accept:`image/*`,className:`pretext-plus-editor__dialog-file-input`,onChange:e=>{let t=e.target.files?.[0];t&&Oe(t)}}),oe?(0,t.jsx)(`p`,{className:`pretext-plus-editor__am-drop-text`,children:`Uploading…`}):(0,t.jsxs)(t.Fragment,{children:[(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&&(0,t.jsx)(`p`,{className:`pretext-plus-editor__am-error`,children:se})]}),j===`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:le,onChange:e=>ue(e.target.value),disabled:fe,autoFocus:!0}),(0,t.jsxs)(`label`,{className:`pretext-plus-editor__dialog-label`,htmlFor:`am-url-name`,children:[`Name `,(0,t.jsx)(`span`,{className:`pretext-plus-editor__dialog-helper-copy`,children:`(optional)`})]}),(0,t.jsx)(`input`,{id:`am-url-name`,type:`text`,className:`pretext-plus-editor__am-input`,placeholder:`My image`,value:de,onChange:e=>F(e.target.value),disabled:fe}),me&&(0,t.jsx)(`p`,{className:`pretext-plus-editor__am-error`,children:me}),le.trim()&&(0,t.jsx)(`img`,{src:le.trim(),alt:`Preview`,className:`pretext-plus-editor__am-url-preview`,onError:e=>{e.currentTarget.style.display=`none`}}),(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__dialog-button`,onClick:ke,disabled:!le.trim()||fe,children:fe?`Adding…`:l&&c?`Add to Library & Insert`:`Insert`})]})]})]}):(0,t.jsxs)(`div`,{className:`pretext-plus-editor__am-configure`,children:[(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__am-back-btn`,onClick:()=>A(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${te===`library`?` pretext-plus-editor__dialog-tab--active`:``}`,onClick:()=>ne(`library`),children:`Library`}),(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__dialog-tab${te===`create`?` pretext-plus-editor__dialog-tab--active`:``}`,onClick:()=>ne(`create`),children:`Create New`})]}),(0,t.jsxs)(`div`,{className:`pretext-plus-editor__am-configure-body`,children:[te===`library`&&Ae(`doenet`),te===`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-name`,children:`Name`}),(0,t.jsx)(`input`,{id:`am-doenet-name`,type:`text`,className:`pretext-plus-editor__am-input`,placeholder:`My Activity`,value:ge,onChange:e=>I(e.target.value),disabled:ye,autoFocus:!0}),(0,t.jsx)(`label`,{className:`pretext-plus-editor__dialog-label`,htmlFor:`am-doenet-ref`,children:`Reference ID`}),(0,t.jsx)(`input`,{id:`am-doenet-ref`,type:`text`,className:`pretext-plus-editor__am-input`,placeholder:`my-activity`,value:_e,onChange:e=>ve(e.target.value),disabled:ye}),(0,t.jsxs)(`p`,{className:`pretext-plus-editor__dialog-helper-copy`,children:[`The reference ID is used in the inserted tag: `,(0,t.jsx)(`code`,{children:`<plus:doenet ref="${_e||`my-activity`}"/>`})]}),xe&&(0,t.jsx)(`p`,{className:`pretext-plus-editor__am-error`,children:xe}),(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__dialog-button`,onClick:z,disabled:!ge.trim()||!_e.trim()||ye,children:ye?`Creating…`:u?`Create & Insert`:`Insert`})]})]})]})}),(0,t.jsx)(`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`})})]})})},Q6={automaticLayout:!0,minimap:{enabled:!1},wordWrap:`on`,insertSpaces:!0,tabSize:2,padding:{top:10,bottom:10}},$6={image:`image`,doenet:`interactive`},e8=({asset:e,onClose:r,onSave:i})=>{let[a,o]=(0,n.useState)(e.source??``),[s,c]=(0,n.useState)(!1),[l,u]=(0,n.useState)(null),d=async()=>{u(null),c(!0);try{await i({...e,source:a}),r()}catch(e){u(e instanceof Error?e.message:`Failed to save asset.`),c(!1)}},f=e.kind===`image`&&!!e.url;return(0,t.jsx)(`div`,{className:`pretext-plus-editor__dialog-overlay`,onClick:e=>e.target===e.currentTarget&&r(),children:(0,t.jsxs)(`div`,{className:`pretext-plus-editor__dialog`,role:`dialog`,"aria-modal":`true`,"aria-label":`Edit asset ${e.name}`,children:[(0,t.jsxs)(`div`,{className:`pretext-plus-editor__dialog-header`,children:[(0,t.jsxs)(`div`,{children:[(0,t.jsx)(`h2`,{className:`pretext-plus-editor__dialog-title`,children:e.name}),(0,t.jsxs)(`p`,{className:`pretext-plus-editor__dialog-copy`,children:[`Reference: `,(0,t.jsx)(`code`,{children:e.ref})]})]}),(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__dialog-close`,onClick:r,"aria-label":`Close`,children:`✕`})]}),(0,t.jsx)(`div`,{className:`pretext-plus-editor__dialog-content pretext-plus-editor__dialog-content--single`,children:(0,t.jsxs)(`div`,{className:`pretext-plus-editor__dialog-section`,children:[f&&(0,t.jsx)(`img`,{src:e.url,alt:e.name,className:`pretext-plus-editor__am-url-preview`,onError:e=>{e.currentTarget.style.display=`none`}}),(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:`<${$6[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`,children:(0,t.jsx)(Yn,{options:{...Q6,readOnly:s},height:`100%`,language:`xml`,value:a,onMount:e=>e.focus(),onChange:e=>o(e??``)})}),l&&(0,t.jsx)(`p`,{className:`pretext-plus-editor__am-error`,children:l})]})}),(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,disabled:s,children:`Cancel`}),(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__dialog-button`,onClick:d,disabled:s,children:s?`Saving…`:`Save`})]})]})})},t8=e=>{let n=a3(e=>e.showFullPreview),r=a3(e=>e.setShowFullPreview),i=a3(e=>e.title),a=a3(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)(o3,{label:`Give feedback`,context:`main-editor`}),o]})]})},n8={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`},r8=[`part`,`chapter`,`section`,`worksheet`,`handout`,`exercises`,`references`,`glossary`,`solutions`,`reading-questions`],i8=({draft:e,isLatex:n,isRoot:r=!1,onDraftChange:i,onCommit:a,onCancel:o})=>(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=>i({...e,title:t.target.value}),onKeyDown:e=>{e.key===`Enter`&&a(),e.key===`Escape`&&o()},autoFocus:!0})]}),!n&&(0,t.jsxs)(t.Fragment,{children:[!r&&(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=>i({...e,type:t.target.value}),children:r8.map(e=>(0,t.jsx)(`option`,{value:e,children:n8[e]},e))})]}),(0,t.jsxs)(`label`,{className:`pretext-plus-editor__toc-edit-field`,children:[(0,t.jsx)(`span`,{children:`xml:id`}),(0,t.jsx)(`input`,{type:`text`,value:e.xmlId,placeholder:`optional`,onChange:t=>i({...e,xmlId:t.target.value})})]}),(0,t.jsxs)(`label`,{className:`pretext-plus-editor__toc-edit-field`,children:[(0,t.jsx)(`span`,{children:`label`}),(0,t.jsx)(`input`,{type:`text`,value:e.label,placeholder:`optional`,onChange:t=>i({...e,label: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:a,children:`Save`}),(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__toc-edit-cancel`,onClick:o,children:`Cancel`})]})]});function a8(e){let t=e?.parentElement??null;for(;t;){let{overflowY:e}=getComputedStyle(t);if((e===`auto`||e===`scroll`)&&t.scrollHeight>t.clientHeight)return t;t=t.parentElement}return null}var o8=({items:e})=>{let[r,i]=(0,n.useState)(!1),[a,o]=(0,n.useState)(`below`),s=(0,n.useRef)(null),c=(0,n.useRef)(null);(0,n.useEffect)(()=>{if(!r)return;let e=e=>{s.current&&!s.current.contains(e.target)&&i(!1)},t=e=>{e.key===`Escape`&&i(!1)};return document.addEventListener(`mousedown`,e),document.addEventListener(`keydown`,t),()=>{document.removeEventListener(`mousedown`,e),document.removeEventListener(`keydown`,t)}},[r]),(0,n.useLayoutEffect)(()=>{if(!r)return;let e=c.current,t=s.current;if(!e||!t)return;let n=a8(t)??document.documentElement,i=e.getBoundingClientRect(),a=n.getBoundingClientRect(),l=t.getBoundingClientRect(),u=a.bottom-l.bottom,d=l.top-a.top;o(i.height>u&&d>u?`above`:`below`)},[r]);let l=e=>{i(!1),e()};return(0,t.jsxs)(`div`,{ref:s,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(),i(e=>(e||o(`below`),!e))},"aria-haspopup":`true`,"aria-expanded":r,title:`More options`,children:`⋮`}),r&&(0,t.jsx)(`div`,{ref:c,className:[`pretext-plus-editor__toc-div-menu-popup`,a===`above`?`pretext-plus-editor__toc-div-menu-popup--above`:`pretext-plus-editor__toc-div-menu-popup--below`].join(` `),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:()=>l(e.onClick),children:e.label},e.label))})]})},s8=({division:e,depth:n,isActive:r,hasChildren:i,isExpanded:a,onToggleExpand:o,editDraft:s,onSelect:c,onDraftChange:l,onEditCommit:u,onEditCancel:d,menuItems:f,isLatex:p,isRoot:m=!1})=>{let h=s!==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`:``,h?`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.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__toc-select`,onClick:c,"aria-current":r?`true`:void 0,title:n8[e.type]??e.type,children:(0,t.jsx)(`span`,{className:`pretext-plus-editor__toc-title`,children:e.title||(0,t.jsx)(`em`,{children:`Untitled`})})}),(0,t.jsx)(`div`,{className:`pretext-plus-editor__toc-actions`,children:(0,t.jsx)(o8,{items:f})})]}),h&&s&&(0,t.jsx)(i8,{draft:s,isLatex:p,isRoot:m,onDraftChange:l,onCommit:u,onCancel:d})]})},c8=({onOpenAssetPicker:e})=>{let r=a3(e=>e.divisions),i=a3(e=>e.rootDivisionId),a=a3(e=>e.activeDivisionId),o=a3(e=>e.projectAssets)??[],s=a3(e=>e.selectSection),c=a3(e=>e.removeSection),l=a3(e=>e.divisionContentChange),u=a3(e=>e.insertAtCursor),d=a3(e=>e.openAssetEditor),f=a3(e=>e.startSectionEdit),p=a3(e=>e.setEditDraft),m=a3(e=>e.commitSectionEdit),h=a3(e=>e.cancelSectionEdit),g=a3(e=>e.editingId),_=a3(e=>e.editDraft),v=r?r.find(e=>e.xmlId===i)??r.find(e=>e.type===`book`||e.type===`article`||e.type===`slideshow`)??r[0]??null:null,y=v&&r?P6(r,v.xmlId):[],b=v&&r?F6(r,v.xmlId):[],x=(()=>{if(!r)return[];let e=new Set,t=[];for(let n of r)for(let r of C6(n.content)){let n=`${r.kind}:${r.ref}`;e.has(n)||(e.add(n),t.push(r))}return t})(),S=Y6.map(e=>({kind:e,items:x.filter(t=>t.kind===e)})).filter(e=>e.items.length>0),[C,w]=(0,n.useState)(!1),[T,E]=(0,n.useState)(new Set),D=e=>!T.has(e),O=e=>{E(t=>{let n=new Set(t);return n.has(e)?n.delete(e):n.add(e),n})};(0,n.useEffect)(()=>{if(!a||!v)return;let e=new Map(y.map(e=>[e.division.xmlId,e])),t=new Set;t.add(v.xmlId);let n=a;for(;n;){let r=e.get(n);if(!r?.parentXmlId)break;t.add(r.parentXmlId),n=r.parentXmlId}E(e=>{if([...t].every(t=>!e.has(t)))return e;let n=new Set(e);return t.forEach(e=>n.delete(e)),n})},[a]);let k=new Set(y.map(e=>e.parentXmlId).filter(Boolean)),A=[];if(v){let e=new Set;D(v.xmlId)&&e.add(v.xmlId);for(let t of y)t.parentXmlId&&e.has(t.parentXmlId)&&(A.push(t),D(t.division.xmlId)&&e.add(t.division.xmlId))}let j=(e,t)=>{if(!r)return;let n=r.find(e=>e.xmlId===t);n&&l(n.xmlId,j6(E6(n.content,e)))},ee=(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&&l(n.xmlId,j6(E6(n.content,e.xmlId)))}c(e.xmlId)}},te=e=>{u(`<plus:${e.type} ref="${e.xmlId}"/>`)},ne=e=>{v&&l(v.xmlId,j6(T6(v.content,e.xmlId,e.type,null)))},re=(e,t)=>{u(`<plus:${e} ref="${t}"/>`)};return(0,t.jsxs)(t.Fragment,{children:[(0,t.jsxs)(`ul`,{className:`pretext-plus-editor__toc-list`,role:`list`,children:[v&&(0,t.jsx)(s8,{division:v,depth:0,isActive:a===v.xmlId,hasChildren:k.has(v.xmlId),isExpanded:D(v.xmlId),onToggleExpand:()=>O(v.xmlId),editDraft:g===v.xmlId?_:null,onSelect:()=>s(v.xmlId),onDraftChange:p,onEditCommit:m,onEditCancel:h,menuItems:[{label:`Edit properties`,onClick:()=>f(v)}],isLatex:v.sourceFormat===`latex`,isRoot:!0}),A.length===0&&v&&D(v.xmlId)&&(0,t.jsx)(`li`,{className:`pretext-plus-editor__toc-no-sections`,children:(0,t.jsx)(`span`,{children:`No placed divisions`})}),A.map(e=>(0,t.jsx)(s8,{division:e.division,depth:e.depth+1,isActive:a===e.division.xmlId,hasChildren:k.has(e.division.xmlId),isExpanded:D(e.division.xmlId),onToggleExpand:()=>O(e.division.xmlId),editDraft:g===e.division.xmlId?_:null,onSelect:()=>s(e.division.xmlId),onDraftChange:p,onEditCommit:m,onEditCancel:h,menuItems:[{label:`Edit properties`,onClick:()=>f(e.division)},{label:`Remove from document`,onClick:()=>j(e.division.xmlId,e.parentXmlId)},{label:`Delete from project`,onClick:()=>ee(e.division,e.parentXmlId),danger:!0}],isLatex:e.division.sourceFormat===`latex`},e.division.xmlId))]}),b.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:b.map(e=>{let i=r?P6(r,e.xmlId):[],o=new Set(i.map(e=>e.parentXmlId).filter(Boolean));return(0,t.jsxs)(n.Fragment,{children:[(0,t.jsx)(s8,{division:e,depth:0,isActive:a===e.xmlId,hasChildren:o.has(e.xmlId),isExpanded:D(e.xmlId),onToggleExpand:()=>O(e.xmlId),editDraft:g===e.xmlId?_:null,onSelect:()=>s(e.xmlId),onDraftChange:p,onEditCommit:m,onEditCancel:h,menuItems:[{label:`Edit properties`,onClick:()=>f(e)},{label:`Place in document`,onClick:()=>ne(e)},{label:`Insert at cursor`,onClick:()=>te(e)},{label:`Delete from project`,onClick:()=>ee(e,null),danger:!0}],isLatex:e.sourceFormat===`latex`}),D(e.xmlId)&&i.map(e=>(0,t.jsx)(s8,{division:e.division,depth:e.depth+1,isActive:a===e.division.xmlId,hasChildren:o.has(e.division.xmlId),isExpanded:D(e.division.xmlId),onToggleExpand:()=>O(e.division.xmlId),editDraft:g===e.division.xmlId?_:null,onSelect:()=>s(e.division.xmlId),onDraftChange:p,onEditCommit:m,onEditCancel:h,menuItems:[{label:`Edit properties`,onClick:()=>f(e.division)},{label:`Insert at cursor`,onClick:()=>te(e.division)},{label:`Delete from project`,onClick:()=>ee(e.division,e.parentXmlId),danger:!0}],isLatex:e.division.sourceFormat===`latex`},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:()=>w(e=>!e),"aria-expanded":C,children:[(0,t.jsx)(`span`,{className:`pretext-plus-editor__toc-assets-chevron`,children:C?`▾`:`▸`}),(0,t.jsx)(`span`,{children:`Assets`}),x.length>0&&(0,t.jsx)(`span`,{className:`pretext-plus-editor__toc-assets-count`,children:x.length})]})}),C&&(0,t.jsx)(`div`,{className:`pretext-plus-editor__toc-assets-body`,children:x.length===0?(0,t.jsxs)(`p`,{className:`pretext-plus-editor__toc-assets-empty`,children:[`No assets referenced in this document.`,` `,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:S.map(({kind:e,items:n})=>(0,t.jsxs)(`div`,{children:[(0,t.jsx)(`div`,{className:`pretext-plus-editor__toc-assets-group-header`,children:J6[e]}),(0,t.jsx)(`ul`,{className:`pretext-plus-editor__toc-assets-list`,children:n.map(({ref:n})=>(0,t.jsxs)(`li`,{className:`pretext-plus-editor__toc-asset-item`,children:[(0,t.jsxs)(`button`,{type:`button`,className:`pretext-plus-editor__toc-asset-name`,onClick:()=>d(e,n),title:`Edit asset content`,children:[(0,t.jsx)(`span`,{className:`pretext-plus-editor__toc-asset-label`,children:o.find(t=>t.kind===e&&t.ref===n)?.name??n}),(0,t.jsx)(`span`,{className:`pretext-plus-editor__toc-asset-filename`,children:n})]}),(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__toc-action-btn`,onClick:()=>re(e,n),title:`Insert <plus:${e} ref="${n}"/> at cursor`,children:`Insert`})]},n))})]},e))})})]}),e&&(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__toc-assets-btn`,onClick:e,children:`Manage Assets`})]})},l8=({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)(c8,{onOpenAssetPicker:r})]}),u8=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`})]})}},d8=(e,t)=>e.kind===t.kind&&e.ref===t.ref;function f8(e){let t=()=>{},n={cbs:{selectDivision:t,addDivision:t,removeDivision:t,updateDivision:t,divisionContentChange:t,handleDivisionContentChange:t,assetInsert:t,updateTitle:t}};return{store:t3()((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,isTocCollapsed:!1,showFullPreview:!0,isNarrowScreen:typeof window<`u`?window.innerWidth<800:!1,activeTab:`editor`,isLatexDialogOpen:!1,isConvertDialogOpen:!1,isDocinfoEditorOpen:!1,isAssetPickerOpen:!1,editingId:null,editDraft:null,editingAssetRef: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.content!==n?(r=!0,{...t,content: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=>{let{xmlId:n,label:r}=m6(e.content);t({editingId:e.xmlId,editDraft:{title:e.title,type:e.type,xmlId:n,label:r}})},setEditDraft:e=>t({editDraft:e}),commitSectionEdit:()=>{let{editingId:e,editDraft:i}=r();e&&i&&n.cbs.updateDivision(e,{title:i.title.trim()||void 0,type:i.type,xmlId:i.xmlId.trim()||null,label:i.label.trim()||null}),t({editingId:null,editDraft:null})},cancelSectionEdit:()=>t({editingId:null,editDraft:null}),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}),setProjectAssets:e=>t({projectAssets:e}),addAssetToPool:e=>t(t=>{let n=t.projectAssets??[];return n.some(t=>d8(t,e))?{}:{projectAssets:[...n,e]}}),updateAssetInPool:e=>t(t=>{let n=t.projectAssets??[];return n.some(t=>d8(t,e))?{projectAssets:n.map(t=>d8(t,e)?e:t)}:{projectAssets:[...n,e]}}),removeAssetFromPool:e=>t(t=>({projectAssets:(t.projectAssets??[]).filter(t=>!d8(t,e))})),updateTitle:e=>n.cbs.updateTitle(e),feedbackSubmit:e=>n.cbs.feedbackSubmit?.(e)})),bindCallbacks:e=>{n.cbs=e}}}function p8({store:e,children:n}){return(0,t.jsx)(i3.Provider,{value:e,children:n})}var m8=(e,t)=>e.find(e=>t?e.xmlId===t:e.type===`book`||e.type===`article`||e.type===`slideshow`)??e[0]??null,h8=e=>{let[r]=(0,n.useState)(()=>{let t=m8(e.divisions,e.rootDivisionId),n=q6(e.divisions,t?.xmlId,e.projectType),r=n.find(e=>e.xmlId===t?.xmlId)??t,i=e.activeDivisionId??r?.xmlId??null,a=n.find(e=>e.xmlId===i)??r;return f8({source:a?.content??``,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)(p8,{store:r.store,children:(0,t.jsx)(g8,{...e,bindCallbacks:r.bindCallbacks})})},g8=e=>{let{bindCallbacks:r}=e,i=a3(e=>e.showFullPreview),a=a3(e=>e.isNarrowScreen),o=a3(e=>e.setIsNarrowScreen),s=a3(e=>e.activeTab),c=a3(e=>e.setActiveTab),l=a3(e=>e.isTocCollapsed),u=a3(e=>e.setIsTocCollapsed),d=a3(e=>e.isLatexDialogOpen),f=a3(e=>e.isConvertDialogOpen),p=a3(e=>e.isDocinfoEditorOpen),m=a3(e=>e.isAssetPickerOpen),h=a3(e=>e.editingAssetRef),g=a3(e=>e.closeAssetEditor),_=a3(e=>e.openModal),v=a3(e=>e.closeModal),y=a3(e=>e.syncState),b=a3(e=>e.projectAssets),x=a3(e=>e.addAssetToPool),S=a3(e=>e.updateAssetInPool),C=a3(e=>e.removeAssetFromPool),w=h?b?.find(e=>e.kind===h.kind&&e.ref===h.ref):void 0,T=a3(e=>e.divisions)??[],E=a3(e=>e.activeDivisionId),D=a3(e=>e.title),O=a3(e=>e.docinfo),k=a3(e=>e.commonDocinfo),A=a3(e=>e.useCommonDocinfo),j=a3(e=>e.applyExternalUpdate),ee=a3(e=>e.setDivisionContent),te=a3(e=>e.patchDivision),ne=a3(e=>e.addDivisionToPool),re=a3(e=>e.removeDivisionFromPool),M=a3(e=>e.setActiveDivisionId),ie=a3(e=>e.setTitle),ae=a3(e=>e.setDocinfo),oe=(0,n.useRef)(null),N=(0,n.useRef)(null),se=m8(T,e.rootDivisionId),P=T.find(e=>e.xmlId===E)??T[0]??null,ce=P?.sourceFormat??`pretext`,le=P?.content??``,ue=(t,n,r,i)=>{ee(t,n),e.onContentChange({xmlId:t,sourceContent:n,sourceFormat:r,pretextSource:r===`pretext`?n:void 0,...i})},de=(t,n)=>{te(t,n),e.onDivisionUpdate?.(t,n)},F=t=>{ne(t),(e.onDivisionAdd?.(t))?.then(e=>te(t.xmlId,{id:e})).catch(()=>{})},fe=t=>{re(t),e.onDivisionRemove?.(t)},pe=t=>{M(t),e.onDivisionSelect?.(t)},me=(0,n.useMemo)(()=>{if(!P||ce===`pretext`)return;let e=Z4(le,ce);return e.pretextError?void 0:e.pretextSource},[P,ce,le]),he=e=>{if(!P)return;let t=ce===`pretext`?j6(e||``):e||``;if(t===P.content)return;if(ue(P.xmlId,t,ce),ce===`pretext`){let e=h6(t);if(e){de(P.xmlId,{title:e.title,type:e.type,xmlId:e.xmlId||null,label:e.label||null});let t=e.xmlId||P.xmlId;if(t!==P.xmlId||e.type!==P.type){let n=k6(T,P.xmlId);if(n){let r=O6(n.content,P.xmlId,t,e.type);r!==n.content&&ue(n.xmlId,r,n.sourceFormat)}}e.xmlId&&e.xmlId!==P.xmlId&&pe(e.xmlId)}}else if(ce===`latex`&&P.type===`section`){let e=l6(t);e!==null&&de(P.xmlId,{title:e})}let n=new Set(T.map(e=>e.xmlId));for(let{xmlId:e,type:r}of S6(t))n.has(e)||(F(w6(e,r,ce)),n.add(e))},ge=e=>{pe(e)},I=()=>{let e=$3();F(e),M(e.xmlId)},_e=e=>e.ref?`<plus:${e.kind} ref="${e.ref}"/>`:``,ve=t=>{let n=_e(t);n&&N.current?.insertAtCursor(n),x(t),e.onAssetInsert?.(t)},ye=t=>{C(t),e.onAssetRemove?.(t)};(0,n.useEffect)(()=>{let e=()=>o(window.innerWidth<800);return window.addEventListener(`resize`,e),()=>window.removeEventListener(`resize`,e)},[o]);let be=ce!==`pretext`;(0,n.useLayoutEffect)(()=>{r({selectDivision:ge,addDivision:()=>I(),removeDivision:e=>fe(e),updateDivision:(e,t)=>de(e,t),divisionContentChange:(e,t)=>{ue(e,t,T.find(t=>t.xmlId===e)?.sourceFormat??`pretext`)},handleDivisionContentChange:he,assetInsert:ve,insertContentAtCursor:e=>N.current?.insertAtCursor(e),updateTitle:t=>{ie(t),e.onTitleChange?.(t)},feedbackSubmit:e.onFeedbackSubmit})}),(0,n.useEffect)(()=>{y({source:le,sourceFormat:ce,libraryAssets:e.libraryAssets,projectType:e.projectType,projectUrl:e.projectUrl,rootDivisionId:se?.xmlId,canConvertToPretext:me!==void 0,activeEditorSource:le,hasFeedback:e.onFeedbackSubmit!==void 0})});let xe=(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=xe.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=m8(e.divisions,e.rootDivisionId),i=q6(e.divisions,t?.xmlId,e.projectType);n.divisions=i;let a=i.find(e=>e.xmlId===t?.xmlId)??t;(E==null||!e.divisions.some(e=>e.xmlId===E))&&(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&&j(n),xe.current={divisions:e.divisions,projectAssets:e.projectAssets,title:e.title,docinfo:e.docinfo,commonDocinfo:e.commonDocinfo,useCommonDocinfo:e.useCommonDocinfo,activeDivisionId:e.activeDivisionId}});let Se=A?k:O,Ce=P?ce===`pretext`?V6(T,P.xmlId,b):me===void 0?void 0:`<${P.type} xml:id="${P.xmlId}">\n<title>${P.title}</title>\n\n${me}\n</${P.type}>`:void 0,L=P&&Ce!==void 0?G6(P.type,Ce,Se,P.title):void 0,we=()=>oe.current?.rebuild(),Te=()=>{e.onSave?.(),oe.current?.rebuild()},Ee=t=>{let n=t.ctrlKey||t.metaKey;n&&t.key===`Enter`&&e.onPreviewRebuild?(t.preventDefault(),we()):n&&t.key===`s`&&(t.preventDefault(),Te())},R=()=>{if(!P||!me)return;let e=$3(),t=j6(r6(me,P.type));F({id:e.xmlId,xmlId:e.xmlId,title:P.title,type:P.type,sourceFormat:`pretext`,content:t})},De=(0,t.jsx)(Ps,{ref:N,content:le,sourceFormat:ce,onChange:he,onRebuild:e.onPreviewRebuild?we:void 0,onSave:Te,onOpenLatexImport:()=>_(`isLatexDialogOpen`),onOpenDocinfoEditor:()=>_(`isDocinfoEditorOpen`),onOpenConvertToPretext:be&&me!==void 0?()=>_(`isConvertDialogOpen`):void 0,canConvertToPretext:me!==void 0,onOpenAssets:e.projectAssets!==void 0&&ce===`pretext`?()=>_(`isAssetPickerOpen`):void 0}),Oe;Oe=i&&e.onPreviewRebuild?(0,t.jsx)($L,{ref:oe,content:L||``,title:D,onRebuild:e.onPreviewRebuild}):(0,t.jsx)(XL,{content:le,canEdit:ce===`pretext`,editDisabledReason:ce===`markdown`?`Visual editing is not available for Markdown documents.`:ce===`latex`?`Visual editing is not available for LaTeX documents.`:``,onChange:e=>he(e)});let ke=(0,t.jsx)(l8,{isCollapsed:l,onToggleCollapse:()=>u(e=>!e),onOpenAssetPicker:e.projectAssets===void 0?void 0:()=>_(`isAssetPickerOpen`)}),z=`pretext-plus-tab-editor`,Ae=`pretext-plus-tab-preview`,je=`pretext-plus-tabpanel`,Me;return Me=a?(0,t.jsxs)(`div`,{className:`pretext-plus-editor__tabs`,children:[ke,(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:z,role:`tab`,"aria-controls":je,"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:Ae,role:`tab`,"aria-controls":je,"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:je,className:`pretext-plus-editor__tab-panel`,role:`tabpanel`,"aria-labelledby":s===`editor`?z:Ae,children:(0,t.jsx)(`div`,{style:{height:`100%`},children:s===`editor`?De:Oe})})]})]}):(0,t.jsxs)(`div`,{className:`pretext-plus-editor__sectioned-layout`,children:[ke,(0,t.jsxs)(gt,{orientation:`horizontal`,className:`pretext-plus-editor__splitter`,children:[(0,t.jsx)(yt,{className:`pretext-plus-editor__editor-panel`,children:De}),(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:Oe})]})]}),(0,t.jsxs)(`div`,{className:`pretext-plus-editor`,onKeyDown:Ee,children:[(0,t.jsx)(t8,{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)(u8,{resetKeys:[le,E],children:Me}),d?(0,t.jsx)(s3,{onClose:()=>v(`isLatexDialogOpen`)}):null,f&&P&&me?(0,t.jsx)(d3,{sourceContent:le,sourceFormat:ce,pretextSource:me,onConfirm:R,onClose:()=>v(`isConvertDialogOpen`)}):null,p?(0,t.jsx)(C3,{docinfo:O,showCommonDocinfoControls:!0,commonDocinfo:k,initialUseCommonDocinfo:A,onClose:t=>{if(v(`isDocinfoEditorOpen`),t!==void 0){ae({docinfo:t.docinfo,commonDocinfo:t.commonDocinfo,useCommonDocinfo:t.useCommonDocinfo}),e.onCommonDocinfoChange?.(t.commonDocinfo),e.onUseCommonDocinfoChange?.(t.useCommonDocinfo);let n=se??P;ue(n?.xmlId??``,n?.content??``,n?.sourceFormat??`pretext`,{docinfo:t.docinfo,commonDocinfo:t.commonDocinfo,useCommonDocinfo:t.useCommonDocinfo})}}}):null,m&&e.projectAssets!==void 0?(0,t.jsx)(Z6,{open:m,onClose:()=>v(`isAssetPickerOpen`),onLoadAssets:e.onLoadAssets,onLoadLibraryAssets:e.onLoadLibraryAssets,onAddFromLibrary:e.onAssetAddFromLibrary,onUpload:e.onAssetUpload,onFetchUrl:e.onAssetFetchUrl,onCreateDoenet:e.onCreateDoenet,onRemoveAsset:e.onAssetRemove?ye:void 0,onInsert:ve}):null,w?(0,t.jsx)(e8,{asset:w,onClose:g,onSave:async t=>{S(t),await e.onAssetUpdate?.(t)}}):null]})]})};e.CodeEditor=Ps,e.DocinfoEditor=C3,e.Editors=h8,e.FeedbackLink=$4,e.FullPreview=$L,e.VisualEditor=XL,e.assembleFullProjectSource=U6,e.assembleProjectSource=V6,e.buildDivisionTree=P6,e.convertLatexToPretext=J4,e.createConclusion=t6,e.createIntroduction=e6,e.createLatexConclusion=f6,e.createLatexIntroduction=d6,e.createNewLatexSection=u6,e.createNewSection=$3,e.derivePretextContent=Z4,e.detectSourceFormat=q4,e.ensureLatexSectionWrapper=s6,e.ensureSectionWrapper=i6,e.extractDivisionMetadata=h6,e.extractLatexDivisionTitle=l6,e.findDivisionParent=k6,e.getOrphanRoots=F6,e.getOrphanedDivisions=N6,e.getSectionAttributes=m6,e.insertDivisionRef=T6,e.mergeTwoSections=p6,e.moveDivisionRef=D6,e.normalizeSelfClosingRefs=j6,e.parseDivisionRefs=x6,e.postToIframe=QL,e.removeDivisionRef=E6,e.renameDivisionRef=O6,e.reorderDivisionRefs=A6,e.rewrapLatexSection=o6,e.rewrapSection=r6,e.stripLatexSectionWrapper=a6,e.stripSectionWrapper=n6,e.updateDivisionTitle=Q3,e.updateLatexSectionTitle=c6,e.updateSectionMetadata=g6,e.wrapDivisionForPreview=G6});
1083
+ `+a.slice(o)}function O6(e,t,n,r){let i=new RegExp(b6(t));return i.test(e)?e.replace(i,`<plus:${r} ref="${n}"/>`):e}function k6(e,t){let n=new RegExp(b6(t));return e.find(e=>n.test(e.content))??null}function A6(e,t){let n=e,r=null;for(let e of t)n=D6(n,e,r),r=e;return n}function j6(e){return e.replace(/<plus:([a-z-]+)((?:\s[^>]*?)?)>\s*<\/plus:\1>/g,(e,t,n)=>`<plus:${t}${n}/>`)}function M6(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 x6(i.content))r.push(e)}return n}function N6(e,t){let n=M6(e,t);return e.filter(e=>!n.has(e.xmlId))}function P6(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 x6(o.content)){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 F6(e,t){let n=N6(e,t),r=new Set(n.map(e=>e.xmlId)),i=new Set;for(let e of n)for(let t of x6(e.content))r.has(t)&&i.add(t);return n.filter(e=>!i.has(e.xmlId))}var I6=new Set([`book`,`article`,`slideshow`]);function L6(e){try{let t=O3(e),n=t.children.find(e=>e.type===`element`),r=n?.name===`pretext`?n.children.find(e=>e.type===`element`&&I6.has(e.name)):n&&I6.has(n.name)?n:void 0;if(!r)return e;if(!r.attributes.label){let e=z6(t,`pretext-plus-preview`);return r.attributes.label=e,TY(t)}return e}catch(t){return console.error(`Error ensuring label:`,t),e}}function R6(e,t){return e.type===`element`&&e.attributes?.label===t?!0:`children`in e&&e.children?e.children.some(e=>e.type===`element`?R6(e,t):!1):!1}function z6(e,t){let n=t,r=1;for(;R6(e,n);)n=`${t}-${r}`,r++;return n}function B6(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.content;else{let{pretextSource:e,pretextError:t}=Z4(i.sourceFormat===`latex`?a6(i.content,i.type):n6(i.content),i.sourceFormat),n=e??`<!-- conversion error: ${t} -->`;a=`<${i.type} xml:id="${i.xmlId}">\n<title>${i.title}</title>\n\n${n}\n</${i.type}>`}if(i.sourceFormat!==`pretext`)return a;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(!U3.has(n))return B6(a,t,o,r);let s=/width="([^"]+)"/.exec(i)?.[1];return W3(n,a,r,s)})}function V6(e,t,n=[]){return L6(B6(t,e,new Set,n))}function H6(e,t){return L6(`<pretext>\n${t.trim()?`${t.trim()}\n`:``}${e}\n</pretext>`)}function U6(e,t,n,r=[]){return H6(B6(t,e,new Set,r),n)}var W6=new Set([`part`,`chapter`]);function G6(e,t,n,r){return H6(I6.has(e)?t:W6.has(e)?`<book>\n<title>${r}</title>\n${t}\n</book>`:`<article>\n<title>${r}</title>\n${t}\n</article>`,n)}function K6(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 q6(e,t,n,r){let i=n===`book`?`book`:`article`;return e.map(e=>{if(e.sourceFormat!==`pretext`)return e;let n=h6(e.content);if(e.xmlId===t&&!(n&&I6.has(n.type))){let{title:t,body:n}=K6(e.content),a=e.title||t||r||`Untitled`;return{...e,type:i,title:a,content:`<${i} xml:id="${e.xmlId}">\n<title>${a}</title>\n\n${n}\n</${i}>`}}return!e.title&&n?.title?{...e,title:n.title}:e})}var J6={image:`Images`,doenet:`Doenet`},Y6=[`image`,`doenet`].filter(e=>e!==`doenet`||!1);function X6(e){let t=new Set,n=[];for(let{kind:r,ref:i}of C6(e)){let e=`${r}:${i}`;t.has(e)||(t.add(e),n.push({kind:r,ref:i}))}return n}var Z6=({open:e,onClose:r,onLoadAssets:i,onLoadLibraryAssets:a,onInsert:o,onAddFromLibrary:s,onUpload:c,onFetchUrl:l,onCreateDoenet:u,onRemoveAsset:d})=>{let f=a3(e=>e.activeEditorSource),p=a3(e=>e.projectAssets)??[],m=a3(e=>e.setProjectAssets),h=a3(e=>e.libraryAssets),g=a3(e=>e.openAssetEditor),_=!!(i||a),[v,y]=(0,n.useState)(null),[b,x]=(0,n.useState)(!1),[S,C]=(0,n.useState)(null),w=p,T=v??h??w,E=new Set(w.map(e=>e.id)),[D,O]=(0,n.useState)(`in-document`),[k,A]=(0,n.useState)(null),[j,ee]=(0,n.useState)(`library`),[te,ne]=(0,n.useState)(`library`),[re,M]=(0,n.useState)(null),[ie,ae]=(0,n.useState)(!1),[oe,N]=(0,n.useState)(!1),[se,P]=(0,n.useState)(null),ce=(0,n.useRef)(null),[le,ue]=(0,n.useState)(``),[de,F]=(0,n.useState)(``),[fe,pe]=(0,n.useState)(!1),[me,he]=(0,n.useState)(null),[ge,I]=(0,n.useState)(``),[_e,ve]=(0,n.useState)(``),[ye,be]=(0,n.useState)(!1),[xe,Se]=(0,n.useState)(null),[Ce,L]=(0,n.useState)(null),we=(0,n.useCallback)(()=>{!i&&!a||(x(!0),C(null),Promise.all([i?.()??Promise.resolve(null),a?.()??Promise.resolve(null)]).then(([e,t])=>{e!==null&&m(e),t!==null&&y(t)}).catch(e=>{C(e instanceof Error?e.message:`Failed to load assets.`)}).finally(()=>x(!1)))},[i,a,m]),Te=(0,n.useRef)(we);if((0,n.useEffect)(()=>{Te.current=we}),(0,n.useEffect)(()=>{e&&Te.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]),!e)return null;let Ee=X6(f),R=e=>{o(e),r()},De=(e,t)=>{let n=`${e}:${t}`;navigator.clipboard.writeText(`<plus:${e} ref="${t}"/>`).catch(()=>{}),L(n),setTimeout(()=>L(e=>e===n?null:e),2e3)},Oe=async e=>{if(!E.has(e.id)&&s){M(e.id);try{await s(e)}finally{M(null)}}R(e)},ke=async e=>{if(c){P(null),N(!0);try{R(await c(e))}catch(e){P(e instanceof Error?e.message:`Upload failed.`),N(!1)}}},z=async()=>{let e=le.trim();if(!e)return;let t=de.trim();if(l&&c){he(null),pe(!0);try{let n=await l(e);R(await c(t?new File([n],t,{type:n.type}):n))}catch(e){he(e instanceof Error?e.message:`Failed to add URL.`),pe(!1)}}else R({id:`url-${Date.now()}`,name:t||e,ref:e.split(`/`).pop()??`image`,kind:`image`,url:e})},Ae=async()=>{let e=ge.trim(),t=_e.trim();if(!(!e||!t))if(u){Se(null),be(!0);try{R(await u(e,t))}catch(e){Se(e instanceof Error?e.message:`Failed to create activity.`),be(!1)}}else R({id:`doenet-${Date.now()}`,name:e,ref:t,kind:`doenet`})},je=e=>{let n=T.filter(t=>t.kind===e);return(0,t.jsxs)(`div`,{className:`pretext-plus-editor__am-library`,children:[_&&(0,t.jsxs)(`div`,{className:`pretext-plus-editor__am-toolbar`,children:[S&&(0,t.jsx)(`span`,{className:`pretext-plus-editor__asset-load-error`,children:S}),(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__asset-library-refresh`,onClick:we,disabled:b,children:b?`Loading…`:`Refresh`})]}),b&&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 `,J6[e].toLowerCase(),` assets in your library.`]}):(0,t.jsx)(`ul`,{className:`pretext-plus-editor__am-list`,children:n.map(e=>{let n=E.has(e.id),r=re===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:()=>Oe(e),disabled:r||!e.ref,title:e.ref?n?`Insert "${e.ref}"`:`Add to project & insert "${e.ref}"`:`Asset has no reference — cannot insert`,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.name}),(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)})})]})};return(0,t.jsx)(`div`,{className:`pretext-plus-editor__dialog-overlay`,onClick:e=>e.target===e.currentTarget&&r(),children:(0,t.jsxs)(`div`,{className:`pretext-plus-editor__dialog pretext-plus-editor__dialog--asset-manager`,role:`dialog`,"aria-modal":`true`,"aria-label":`Asset manager`,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:`✕`})]}),(0,t.jsxs)(`div`,{className:`pretext-plus-editor__dialog-tab-bar`,children:[(0,t.jsxs)(`button`,{type:`button`,className:`pretext-plus-editor__dialog-tab${D===`in-document`?` pretext-plus-editor__dialog-tab--active`:``}`,onClick:()=>O(`in-document`),children:[`In Document`,Ee.length>0&&(0,t.jsx)(`span`,{className:`pretext-plus-editor__asset-tab-count`,children:Ee.length})]}),(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__dialog-tab${D===`add`?` pretext-plus-editor__dialog-tab--active`:``}`,onClick:()=>{O(`add`),A(null)},children:`Add Asset`})]}),(0,t.jsx)(`div`,{className:`pretext-plus-editor__dialog-content pretext-plus-editor__dialog-content--single`,children:D===`in-document`?(()=>{if(Ee.length===0)return(0,t.jsxs)(`div`,{className:`pretext-plus-editor__am-placeholder`,children:[(0,t.jsx)(`p`,{children:`No assets referenced in this document yet.`}),(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__dialog-button`,onClick:()=>{O(`add`),A(null)},children:`Add an asset`})]});let e=Y6.reduce((e,t)=>(e[t]=Ee.filter(e=>e.kind===t).map(e=>({ref:e.ref,asset:w.find(t=>t.kind===e.kind&&t.ref===e.ref)})),e),{image:[],doenet:[]});return(0,t.jsx)(`div`,{className:`pretext-plus-editor__am-in-doc`,children:Y6.filter(t=>e[t].length>0).map(n=>(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:J6[n]}),(0,t.jsx)(`span`,{className:`pretext-plus-editor__am-kind-count`,children:e[n].length})]}),(0,t.jsx)(`ul`,{className:`pretext-plus-editor__am-list`,children:e[n].map(({ref:e,asset:i})=>{let a=`${n}:${e}`;return(0,t.jsxs)(`li`,{className:`pretext-plus-editor__am-doc-row`,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:i?.name??e}),(0,t.jsx)(`span`,{className:`pretext-plus-editor__am-row-ref`,children:e})]}),(0,t.jsxs)(`div`,{className:`pretext-plus-editor__am-row-actions`,children:[i&&(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__am-action-btn`,onClick:()=>R(i),title:`Insert <plus:${n} ref="${e}"/>`,children:`Insert`}),i&&(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__am-action-btn`,onClick:()=>g(n,e),title:`Edit asset content`,children:`Edit`}),(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__am-action-btn${Ce===a?` pretext-plus-editor__am-action-btn--done`:``}`,onClick:()=>De(n,e),title:`Copy <plus:${n} ref="${e}"/>`,children:Ce===a?`Copied!`:`Copy`}),d&&i&&(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__am-action-btn pretext-plus-editor__am-action-btn--danger`,onClick:()=>{d(i),r()},title:`Remove from project`,children:`Remove`})]})]},e)})})]},n))})})():k===null?(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:()=>{A(`image`),ee(`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`})]})]})]}):k===`image`?(0,t.jsxs)(`div`,{className:`pretext-plus-editor__am-configure`,children:[(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__am-back-btn`,onClick:()=>A(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${j===`library`?` pretext-plus-editor__dialog-tab--active`:``}`,onClick:()=>ee(`library`),children:`Library`}),c&&(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__dialog-tab${j===`upload`?` pretext-plus-editor__dialog-tab--active`:``}`,onClick:()=>ee(`upload`),children:`Upload`}),(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__dialog-tab${j===`url`?` pretext-plus-editor__dialog-tab--active`:``}`,onClick:()=>ee(`url`),children:`External URL`})]}),(0,t.jsxs)(`div`,{className:`pretext-plus-editor__am-configure-body`,children:[j===`library`&&je(`image`),j===`upload`&&c&&(0,t.jsxs)(`div`,{className:`pretext-plus-editor__am-upload`,children:[(0,t.jsxs)(`div`,{className:[`pretext-plus-editor__am-drop-zone`,ie?`pretext-plus-editor__am-drop-zone--active`:``,oe?`pretext-plus-editor__am-drop-zone--uploading`:``].filter(Boolean).join(` `),onDragOver:e=>{e.preventDefault(),ae(!0)},onDragLeave:()=>ae(!1),onDrop:e=>{e.preventDefault(),ae(!1);let t=e.dataTransfer.files[0];t&&ke(t)},onClick:()=>!oe&&ce.current?.click(),role:`button`,tabIndex:0,onKeyDown:e=>{(e.key===`Enter`||e.key===` `)&&(e.preventDefault(),ce.current?.click())},"aria-label":`Upload image — click or drag and drop`,children:[(0,t.jsx)(`input`,{ref:ce,type:`file`,accept:`image/*`,className:`pretext-plus-editor__dialog-file-input`,onChange:e=>{let t=e.target.files?.[0];t&&ke(t)}}),oe?(0,t.jsx)(`p`,{className:`pretext-plus-editor__am-drop-text`,children:`Uploading…`}):(0,t.jsxs)(t.Fragment,{children:[(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&&(0,t.jsx)(`p`,{className:`pretext-plus-editor__am-error`,children:se})]}),j===`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:le,onChange:e=>ue(e.target.value),disabled:fe,autoFocus:!0}),(0,t.jsxs)(`label`,{className:`pretext-plus-editor__dialog-label`,htmlFor:`am-url-name`,children:[`Name `,(0,t.jsx)(`span`,{className:`pretext-plus-editor__dialog-helper-copy`,children:`(optional)`})]}),(0,t.jsx)(`input`,{id:`am-url-name`,type:`text`,className:`pretext-plus-editor__am-input`,placeholder:`My image`,value:de,onChange:e=>F(e.target.value),disabled:fe}),me&&(0,t.jsx)(`p`,{className:`pretext-plus-editor__am-error`,children:me}),le.trim()&&(0,t.jsx)(`img`,{src:le.trim(),alt:`Preview`,className:`pretext-plus-editor__am-url-preview`,onError:e=>{e.currentTarget.style.display=`none`}}),(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__dialog-button`,onClick:z,disabled:!le.trim()||fe,children:fe?`Adding…`:l&&c?`Add to Library & Insert`:`Insert`})]})]})]}):(0,t.jsxs)(`div`,{className:`pretext-plus-editor__am-configure`,children:[(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__am-back-btn`,onClick:()=>A(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${te===`library`?` pretext-plus-editor__dialog-tab--active`:``}`,onClick:()=>ne(`library`),children:`Library`}),(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__dialog-tab${te===`create`?` pretext-plus-editor__dialog-tab--active`:``}`,onClick:()=>ne(`create`),children:`Create New`})]}),(0,t.jsxs)(`div`,{className:`pretext-plus-editor__am-configure-body`,children:[te===`library`&&je(`doenet`),te===`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-name`,children:`Name`}),(0,t.jsx)(`input`,{id:`am-doenet-name`,type:`text`,className:`pretext-plus-editor__am-input`,placeholder:`My Activity`,value:ge,onChange:e=>I(e.target.value),disabled:ye,autoFocus:!0}),(0,t.jsx)(`label`,{className:`pretext-plus-editor__dialog-label`,htmlFor:`am-doenet-ref`,children:`Reference ID`}),(0,t.jsx)(`input`,{id:`am-doenet-ref`,type:`text`,className:`pretext-plus-editor__am-input`,placeholder:`my-activity`,value:_e,onChange:e=>ve(e.target.value),disabled:ye}),(0,t.jsxs)(`p`,{className:`pretext-plus-editor__dialog-helper-copy`,children:[`The reference ID is used in the inserted tag: `,(0,t.jsx)(`code`,{children:`<plus:doenet ref="${_e||`my-activity`}"/>`})]}),xe&&(0,t.jsx)(`p`,{className:`pretext-plus-editor__am-error`,children:xe}),(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__dialog-button`,onClick:Ae,disabled:!ge.trim()||!_e.trim()||ye,children:ye?`Creating…`:u?`Create & Insert`:`Insert`})]})]})]})}),(0,t.jsx)(`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`})})]})})},Q6={automaticLayout:!0,minimap:{enabled:!1},wordWrap:`on`,insertSpaces:!0,tabSize:2,padding:{top:10,bottom:10}},$6={image:`image`,doenet:`interactive`},e8=({asset:e,onClose:r,onSave:i})=>{let[a,o]=(0,n.useState)(e.source??``),[s,c]=(0,n.useState)(!1),[l,u]=(0,n.useState)(null),d=async()=>{u(null),c(!0);try{await i({...e,source:a}),r()}catch(e){u(e instanceof Error?e.message:`Failed to save asset.`),c(!1)}},f=e.kind===`image`&&!!e.url;return(0,t.jsx)(`div`,{className:`pretext-plus-editor__dialog-overlay`,onClick:e=>e.target===e.currentTarget&&r(),children:(0,t.jsxs)(`div`,{className:`pretext-plus-editor__dialog`,role:`dialog`,"aria-modal":`true`,"aria-label":`Edit asset ${e.name}`,children:[(0,t.jsxs)(`div`,{className:`pretext-plus-editor__dialog-header`,children:[(0,t.jsxs)(`div`,{children:[(0,t.jsx)(`h2`,{className:`pretext-plus-editor__dialog-title`,children:e.name}),(0,t.jsxs)(`p`,{className:`pretext-plus-editor__dialog-copy`,children:[`Reference: `,(0,t.jsx)(`code`,{children:e.ref})]})]}),(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__dialog-close`,onClick:r,"aria-label":`Close`,children:`✕`})]}),(0,t.jsx)(`div`,{className:`pretext-plus-editor__dialog-content pretext-plus-editor__dialog-content--single`,children:(0,t.jsxs)(`div`,{className:`pretext-plus-editor__dialog-section`,children:[f&&(0,t.jsx)(`img`,{src:e.url,alt:e.name,className:`pretext-plus-editor__am-url-preview`,onError:e=>{e.currentTarget.style.display=`none`}}),(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:`<${$6[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`,children:(0,t.jsx)(Yn,{options:{...Q6,readOnly:s},height:`100%`,language:`xml`,value:a,onMount:e=>e.focus(),onChange:e=>o(e??``)})}),l&&(0,t.jsx)(`p`,{className:`pretext-plus-editor__am-error`,children:l})]})}),(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,disabled:s,children:`Cancel`}),(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__dialog-button`,onClick:d,disabled:s,children:s?`Saving…`:`Save`})]})]})})},t8=e=>{let n=a3(e=>e.showFullPreview),r=a3(e=>e.setShowFullPreview),i=a3(e=>e.title),a=a3(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)(o3,{label:`Give feedback`,context:`main-editor`}),o]})]})},n8={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`},r8=[`part`,`chapter`,`section`,`worksheet`,`handout`,`exercises`,`references`,`glossary`,`solutions`,`reading-questions`],i8=({draft:e,isLatex:n,isRoot:r=!1,onDraftChange:i,onCommit:a,onCancel:o})=>(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=>i({...e,title:t.target.value}),onKeyDown:e=>{e.key===`Enter`&&a(),e.key===`Escape`&&o()},autoFocus:!0})]}),!n&&(0,t.jsxs)(t.Fragment,{children:[!r&&(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=>i({...e,type:t.target.value}),children:r8.map(e=>(0,t.jsx)(`option`,{value:e,children:n8[e]},e))})]}),(0,t.jsxs)(`label`,{className:`pretext-plus-editor__toc-edit-field`,children:[(0,t.jsx)(`span`,{children:`xml:id`}),(0,t.jsx)(`input`,{type:`text`,value:e.xmlId,placeholder:`optional`,onChange:t=>i({...e,xmlId:t.target.value})})]}),(0,t.jsxs)(`label`,{className:`pretext-plus-editor__toc-edit-field`,children:[(0,t.jsx)(`span`,{children:`label`}),(0,t.jsx)(`input`,{type:`text`,value:e.label,placeholder:`optional`,onChange:t=>i({...e,label: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:a,children:`Save`}),(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__toc-edit-cancel`,onClick:o,children:`Cancel`})]})]});function a8(e){let t=e?.parentElement??null;for(;t;){let{overflowY:e}=getComputedStyle(t);if((e===`auto`||e===`scroll`)&&t.scrollHeight>t.clientHeight)return t;t=t.parentElement}return null}var o8=({items:e})=>{let[r,i]=(0,n.useState)(!1),[a,o]=(0,n.useState)(`below`),s=(0,n.useRef)(null),c=(0,n.useRef)(null);(0,n.useEffect)(()=>{if(!r)return;let e=e=>{s.current&&!s.current.contains(e.target)&&i(!1)},t=e=>{e.key===`Escape`&&i(!1)};return document.addEventListener(`mousedown`,e),document.addEventListener(`keydown`,t),()=>{document.removeEventListener(`mousedown`,e),document.removeEventListener(`keydown`,t)}},[r]),(0,n.useLayoutEffect)(()=>{if(!r)return;let e=c.current,t=s.current;if(!e||!t)return;let n=a8(t)??document.documentElement,i=e.getBoundingClientRect(),a=n.getBoundingClientRect(),l=t.getBoundingClientRect(),u=a.bottom-l.bottom,d=l.top-a.top;o(i.height>u&&d>u?`above`:`below`)},[r]);let l=e=>{i(!1),e()};return(0,t.jsxs)(`div`,{ref:s,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(),i(e=>(e||o(`below`),!e))},"aria-haspopup":`true`,"aria-expanded":r,title:`More options`,children:`⋮`}),r&&(0,t.jsx)(`div`,{ref:c,className:[`pretext-plus-editor__toc-div-menu-popup`,a===`above`?`pretext-plus-editor__toc-div-menu-popup--above`:`pretext-plus-editor__toc-div-menu-popup--below`].join(` `),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:()=>l(e.onClick),children:e.label},e.label))})]})},s8=({division:e,depth:n,isActive:r,hasChildren:i,isExpanded:a,onToggleExpand:o,editDraft:s,onSelect:c,onDraftChange:l,onEditCommit:u,onEditCancel:d,menuItems:f,isLatex:p,isRoot:m=!1})=>{let h=s!==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`:``,h?`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.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__toc-select`,onClick:c,"aria-current":r?`true`:void 0,title:n8[e.type]??e.type,children:(0,t.jsx)(`span`,{className:`pretext-plus-editor__toc-title`,children:e.title||(0,t.jsx)(`em`,{children:`Untitled`})})}),(0,t.jsx)(`div`,{className:`pretext-plus-editor__toc-actions`,children:(0,t.jsx)(o8,{items:f})})]}),h&&s&&(0,t.jsx)(i8,{draft:s,isLatex:p,isRoot:m,onDraftChange:l,onCommit:u,onCancel:d})]})},c8=({onOpenAssetPicker:e})=>{let r=a3(e=>e.divisions),i=a3(e=>e.rootDivisionId),a=a3(e=>e.activeDivisionId),o=a3(e=>e.projectAssets)??[],s=a3(e=>e.selectSection),c=a3(e=>e.removeSection),l=a3(e=>e.divisionContentChange),u=a3(e=>e.insertAtCursor),d=a3(e=>e.openAssetEditor),f=a3(e=>e.startSectionEdit),p=a3(e=>e.setEditDraft),m=a3(e=>e.commitSectionEdit),h=a3(e=>e.cancelSectionEdit),g=a3(e=>e.editingId),_=a3(e=>e.editDraft),v=r?r.find(e=>e.xmlId===i)??r.find(e=>e.type===`book`||e.type===`article`||e.type===`slideshow`)??r[0]??null:null,y=v&&r?P6(r,v.xmlId):[],b=v&&r?F6(r,v.xmlId):[],x=(()=>{if(!r)return[];let e=new Set,t=[];for(let n of r)for(let r of C6(n.content)){let n=`${r.kind}:${r.ref}`;e.has(n)||(e.add(n),t.push(r))}return t})(),S=Y6.map(e=>({kind:e,items:x.filter(t=>t.kind===e)})).filter(e=>e.items.length>0),[C,w]=(0,n.useState)(!1),[T,E]=(0,n.useState)(new Set),D=e=>!T.has(e),O=e=>{E(t=>{let n=new Set(t);return n.has(e)?n.delete(e):n.add(e),n})};(0,n.useEffect)(()=>{if(!a||!v)return;let e=new Map(y.map(e=>[e.division.xmlId,e])),t=new Set;t.add(v.xmlId);let n=a;for(;n;){let r=e.get(n);if(!r?.parentXmlId)break;t.add(r.parentXmlId),n=r.parentXmlId}E(e=>{if([...t].every(t=>!e.has(t)))return e;let n=new Set(e);return t.forEach(e=>n.delete(e)),n})},[a]);let k=new Set(y.map(e=>e.parentXmlId).filter(Boolean)),A=[];if(v){let e=new Set;D(v.xmlId)&&e.add(v.xmlId);for(let t of y)t.parentXmlId&&e.has(t.parentXmlId)&&(A.push(t),D(t.division.xmlId)&&e.add(t.division.xmlId))}let j=(e,t)=>{if(!r)return;let n=r.find(e=>e.xmlId===t);n&&l(n.xmlId,j6(E6(n.content,e)))},ee=(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&&l(n.xmlId,j6(E6(n.content,e.xmlId)))}c(e.xmlId)}},te=e=>{u(`<plus:${e.type} ref="${e.xmlId}"/>`)},ne=e=>{v&&l(v.xmlId,j6(T6(v.content,e.xmlId,e.type,null)))},re=(e,t)=>{u(`<plus:${e} ref="${t}"/>`)};return(0,t.jsxs)(t.Fragment,{children:[(0,t.jsxs)(`ul`,{className:`pretext-plus-editor__toc-list`,role:`list`,children:[v&&(0,t.jsx)(s8,{division:v,depth:0,isActive:a===v.xmlId,hasChildren:k.has(v.xmlId),isExpanded:D(v.xmlId),onToggleExpand:()=>O(v.xmlId),editDraft:g===v.xmlId?_:null,onSelect:()=>s(v.xmlId),onDraftChange:p,onEditCommit:m,onEditCancel:h,menuItems:[{label:`Edit properties`,onClick:()=>f(v)}],isLatex:v.sourceFormat===`latex`,isRoot:!0}),A.length===0&&v&&D(v.xmlId)&&(0,t.jsx)(`li`,{className:`pretext-plus-editor__toc-no-sections`,children:(0,t.jsx)(`span`,{children:`No placed divisions`})}),A.map(e=>(0,t.jsx)(s8,{division:e.division,depth:e.depth+1,isActive:a===e.division.xmlId,hasChildren:k.has(e.division.xmlId),isExpanded:D(e.division.xmlId),onToggleExpand:()=>O(e.division.xmlId),editDraft:g===e.division.xmlId?_:null,onSelect:()=>s(e.division.xmlId),onDraftChange:p,onEditCommit:m,onEditCancel:h,menuItems:[{label:`Edit properties`,onClick:()=>f(e.division)},{label:`Remove from document`,onClick:()=>j(e.division.xmlId,e.parentXmlId)},{label:`Delete from project`,onClick:()=>ee(e.division,e.parentXmlId),danger:!0}],isLatex:e.division.sourceFormat===`latex`},e.division.xmlId))]}),b.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:b.map(e=>{let i=r?P6(r,e.xmlId):[],o=new Set(i.map(e=>e.parentXmlId).filter(Boolean));return(0,t.jsxs)(n.Fragment,{children:[(0,t.jsx)(s8,{division:e,depth:0,isActive:a===e.xmlId,hasChildren:o.has(e.xmlId),isExpanded:D(e.xmlId),onToggleExpand:()=>O(e.xmlId),editDraft:g===e.xmlId?_:null,onSelect:()=>s(e.xmlId),onDraftChange:p,onEditCommit:m,onEditCancel:h,menuItems:[{label:`Edit properties`,onClick:()=>f(e)},{label:`Place in document`,onClick:()=>ne(e)},{label:`Insert at cursor`,onClick:()=>te(e)},{label:`Delete from project`,onClick:()=>ee(e,null),danger:!0}],isLatex:e.sourceFormat===`latex`}),D(e.xmlId)&&i.map(e=>(0,t.jsx)(s8,{division:e.division,depth:e.depth+1,isActive:a===e.division.xmlId,hasChildren:o.has(e.division.xmlId),isExpanded:D(e.division.xmlId),onToggleExpand:()=>O(e.division.xmlId),editDraft:g===e.division.xmlId?_:null,onSelect:()=>s(e.division.xmlId),onDraftChange:p,onEditCommit:m,onEditCancel:h,menuItems:[{label:`Edit properties`,onClick:()=>f(e.division)},{label:`Insert at cursor`,onClick:()=>te(e.division)},{label:`Delete from project`,onClick:()=>ee(e.division,e.parentXmlId),danger:!0}],isLatex:e.division.sourceFormat===`latex`},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:()=>w(e=>!e),"aria-expanded":C,children:[(0,t.jsx)(`span`,{className:`pretext-plus-editor__toc-assets-chevron`,children:C?`▾`:`▸`}),(0,t.jsx)(`span`,{children:`Assets`}),x.length>0&&(0,t.jsx)(`span`,{className:`pretext-plus-editor__toc-assets-count`,children:x.length})]})}),C&&(0,t.jsx)(`div`,{className:`pretext-plus-editor__toc-assets-body`,children:x.length===0?(0,t.jsxs)(`p`,{className:`pretext-plus-editor__toc-assets-empty`,children:[`No assets referenced in this document.`,` `,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:S.map(({kind:e,items:n})=>(0,t.jsxs)(`div`,{children:[(0,t.jsx)(`div`,{className:`pretext-plus-editor__toc-assets-group-header`,children:J6[e]}),(0,t.jsx)(`ul`,{className:`pretext-plus-editor__toc-assets-list`,children:n.map(({ref:n})=>(0,t.jsxs)(`li`,{className:`pretext-plus-editor__toc-asset-item`,children:[(0,t.jsxs)(`button`,{type:`button`,className:`pretext-plus-editor__toc-asset-name`,onClick:()=>d(e,n),title:`Edit asset content`,children:[(0,t.jsx)(`span`,{className:`pretext-plus-editor__toc-asset-label`,children:o.find(t=>t.kind===e&&t.ref===n)?.name??n}),(0,t.jsx)(`span`,{className:`pretext-plus-editor__toc-asset-filename`,children:n})]}),(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__toc-action-btn`,onClick:()=>re(e,n),title:`Insert <plus:${e} ref="${n}"/> at cursor`,children:`Insert`})]},n))})]},e))})})]}),e&&(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__toc-assets-btn`,onClick:e,children:`Manage Assets`})]})},l8=({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)(c8,{onOpenAssetPicker:r})]}),u8=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`})]})}},d8=(e,t)=>e.kind===t.kind&&e.ref===t.ref;function f8(e){let t=()=>{},n={cbs:{selectDivision:t,addDivision:t,removeDivision:t,updateDivision:t,divisionContentChange:t,handleDivisionContentChange:t,assetInsert:t,updateTitle:t}};return{store:t3()((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,isTocCollapsed:!1,showFullPreview:!0,isNarrowScreen:typeof window<`u`?window.innerWidth<800:!1,activeTab:`editor`,isLatexDialogOpen:!1,isConvertDialogOpen:!1,isDocinfoEditorOpen:!1,isAssetPickerOpen:!1,editingId:null,editDraft:null,editingAssetRef: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.content!==n?(r=!0,{...t,content: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=>{let{xmlId:n,label:r}=m6(e.content);t({editingId:e.xmlId,editDraft:{title:e.title,type:e.type,xmlId:n,label:r}})},setEditDraft:e=>t({editDraft:e}),commitSectionEdit:()=>{let{editingId:e,editDraft:i}=r();e&&i&&n.cbs.updateDivision(e,{title:i.title.trim()||void 0,type:i.type,xmlId:i.xmlId.trim()||null,label:i.label.trim()||null}),t({editingId:null,editDraft:null})},cancelSectionEdit:()=>t({editingId:null,editDraft:null}),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}),setProjectAssets:e=>t({projectAssets:e}),addAssetToPool:e=>t(t=>{let n=t.projectAssets??[];return n.some(t=>d8(t,e))?{}:{projectAssets:[...n,e]}}),updateAssetInPool:e=>t(t=>{let n=t.projectAssets??[];return n.some(t=>d8(t,e))?{projectAssets:n.map(t=>d8(t,e)?e:t)}:{projectAssets:[...n,e]}}),removeAssetFromPool:e=>t(t=>({projectAssets:(t.projectAssets??[]).filter(t=>!d8(t,e))})),updateTitle:e=>n.cbs.updateTitle(e),feedbackSubmit:e=>n.cbs.feedbackSubmit?.(e)})),bindCallbacks:e=>{n.cbs=e}}}function p8({store:e,children:n}){return(0,t.jsx)(i3.Provider,{value:e,children:n})}var m8=(e,t)=>e.find(e=>t?e.xmlId===t:e.type===`book`||e.type===`article`||e.type===`slideshow`)??e[0]??null,h8=e=>{let[r]=(0,n.useState)(()=>{let t=m8(e.divisions,e.rootDivisionId),n=q6(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 f8({source:a?.content??``,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)(p8,{store:r.store,children:(0,t.jsx)(g8,{...e,bindCallbacks:r.bindCallbacks})})},g8=e=>{let{bindCallbacks:r}=e,i=a3(e=>e.showFullPreview),a=a3(e=>e.isNarrowScreen),o=a3(e=>e.setIsNarrowScreen),s=a3(e=>e.activeTab),c=a3(e=>e.setActiveTab),l=a3(e=>e.isTocCollapsed),u=a3(e=>e.setIsTocCollapsed),d=a3(e=>e.isLatexDialogOpen),f=a3(e=>e.isConvertDialogOpen),p=a3(e=>e.isDocinfoEditorOpen),m=a3(e=>e.isAssetPickerOpen),h=a3(e=>e.editingAssetRef),g=a3(e=>e.closeAssetEditor),_=a3(e=>e.openModal),v=a3(e=>e.closeModal),y=a3(e=>e.syncState),b=a3(e=>e.projectAssets),x=a3(e=>e.addAssetToPool),S=a3(e=>e.updateAssetInPool),C=a3(e=>e.removeAssetFromPool),w=h?b?.find(e=>e.kind===h.kind&&e.ref===h.ref):void 0,T=a3(e=>e.divisions)??[],E=a3(e=>e.activeDivisionId),D=a3(e=>e.title),O=a3(e=>e.docinfo),k=a3(e=>e.commonDocinfo),A=a3(e=>e.useCommonDocinfo),j=a3(e=>e.applyExternalUpdate),ee=a3(e=>e.setDivisionContent),te=a3(e=>e.patchDivision),ne=a3(e=>e.addDivisionToPool),re=a3(e=>e.removeDivisionFromPool),M=a3(e=>e.setActiveDivisionId),ie=a3(e=>e.setTitle),ae=a3(e=>e.setDocinfo),oe=(0,n.useRef)(null),N=(0,n.useRef)(null),se=m8(T,e.rootDivisionId),P=T.find(e=>e.xmlId===E)??T[0]??null,ce=P?.sourceFormat??`pretext`,le=P?.content??``,ue=(t,n,r,i)=>{ee(t,n),e.onContentChange({xmlId:t,sourceContent:n,sourceFormat:r,pretextSource:r===`pretext`?n:void 0,...i})},de=(t,n)=>{te(t,n),e.onDivisionUpdate?.(t,n)},F=t=>{ne(t),(e.onDivisionAdd?.(t))?.then(e=>te(t.xmlId,{id:e})).catch(()=>{})},fe=t=>{re(t),e.onDivisionRemove?.(t)},pe=t=>{M(t),e.onDivisionSelect?.(t)},me=(0,n.useMemo)(()=>{if(!P||ce===`pretext`)return;let e=Z4(le,ce);return e.pretextError?void 0:e.pretextSource},[P,ce,le]),he=e=>{if(!P)return;let t=ce===`pretext`?j6(e||``):e||``;if(t===P.content)return;if(ue(P.xmlId,t,ce),ce===`pretext`){let e=h6(t);if(e){de(P.xmlId,{title:e.title,type:e.type,xmlId:e.xmlId||null,label:e.label||null});let t=e.xmlId||P.xmlId;if(t!==P.xmlId||e.type!==P.type){let n=k6(T,P.xmlId);if(n){let r=O6(n.content,P.xmlId,t,e.type);r!==n.content&&ue(n.xmlId,r,n.sourceFormat)}}e.xmlId&&e.xmlId!==P.xmlId&&pe(e.xmlId)}}else if(ce===`latex`&&P.type===`section`){let e=l6(t);e!==null&&de(P.xmlId,{title:e})}let n=new Set(T.map(e=>e.xmlId));for(let{xmlId:e,type:r}of S6(t))n.has(e)||(F(w6(e,r,ce)),n.add(e))},ge=e=>{pe(e)},I=()=>{let e=$3();F(e),M(e.xmlId)},_e=e=>e.ref?`<plus:${e.kind} ref="${e.ref}"/>`:``,ve=t=>{let n=_e(t);n&&N.current?.insertAtCursor(n),x(t),e.onAssetInsert?.(t)},ye=t=>{C(t),e.onAssetRemove?.(t)};(0,n.useEffect)(()=>{let e=()=>o(window.innerWidth<800);return window.addEventListener(`resize`,e),()=>window.removeEventListener(`resize`,e)},[o]);let be=ce!==`pretext`;(0,n.useLayoutEffect)(()=>{r({selectDivision:ge,addDivision:()=>I(),removeDivision:e=>fe(e),updateDivision:(e,t)=>de(e,t),divisionContentChange:(e,t)=>{ue(e,t,T.find(t=>t.xmlId===e)?.sourceFormat??`pretext`)},handleDivisionContentChange:he,assetInsert:ve,insertContentAtCursor:e=>N.current?.insertAtCursor(e),updateTitle:t=>{ie(t),e.onTitleChange?.(t)},feedbackSubmit:e.onFeedbackSubmit})}),(0,n.useEffect)(()=>{y({source:le,sourceFormat:ce,libraryAssets:e.libraryAssets,projectType:e.projectType,projectUrl:e.projectUrl,rootDivisionId:se?.xmlId,canConvertToPretext:me!==void 0,activeEditorSource:le,hasFeedback:e.onFeedbackSubmit!==void 0})});let xe=(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=xe.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=m8(e.divisions,e.rootDivisionId),i=q6(e.divisions,t?.xmlId,e.projectType,e.title);n.divisions=i;let a=i.find(e=>e.xmlId===t?.xmlId)??t;(E==null||!e.divisions.some(e=>e.xmlId===E))&&(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&&j(n),xe.current={divisions:e.divisions,projectAssets:e.projectAssets,title:e.title,docinfo:e.docinfo,commonDocinfo:e.commonDocinfo,useCommonDocinfo:e.useCommonDocinfo,activeDivisionId:e.activeDivisionId}});let Se=(0,n.useRef)(null);(0,n.useEffect)(()=>{if(Se.current===e.divisions)return;Se.current=e.divisions;let t=m8(e.divisions,e.rootDivisionId),n=q6(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.content===n.content||e.onContentChange({xmlId:t.xmlId,sourceContent:t.content,sourceFormat:`pretext`,pretextSource:t.content})}});let Ce=A?k:O,L=P?ce===`pretext`?V6(T,P.xmlId,b):me===void 0?void 0:`<${P.type} xml:id="${P.xmlId}">\n<title>${P.title}</title>\n\n${me}\n</${P.type}>`:void 0,we=P&&L!==void 0?G6(P.type,L,Ce,P.title):void 0,Te=()=>oe.current?.rebuild(),Ee=()=>{e.onSave?.(),oe.current?.rebuild()},R=t=>{let n=t.ctrlKey||t.metaKey;n&&t.key===`Enter`&&e.onPreviewRebuild?(t.preventDefault(),Te()):n&&t.key===`s`&&(t.preventDefault(),Ee())},De=()=>{if(!P||!me)return;let e=$3(),t=j6(r6(me,P.type));F({id:e.xmlId,xmlId:e.xmlId,title:P.title,type:P.type,sourceFormat:`pretext`,content:t})},Oe=(0,t.jsx)(Ps,{ref:N,content:le,sourceFormat:ce,onChange:he,onRebuild:e.onPreviewRebuild?Te:void 0,onSave:Ee,onOpenLatexImport:()=>_(`isLatexDialogOpen`),onOpenDocinfoEditor:()=>_(`isDocinfoEditorOpen`),onOpenConvertToPretext:be&&me!==void 0?()=>_(`isConvertDialogOpen`):void 0,canConvertToPretext:me!==void 0,onOpenAssets:e.projectAssets!==void 0&&ce===`pretext`?()=>_(`isAssetPickerOpen`):void 0}),ke;ke=i&&e.onPreviewRebuild?(0,t.jsx)($L,{ref:oe,content:we||``,title:D,onRebuild:e.onPreviewRebuild}):(0,t.jsx)(XL,{content:le,canEdit:ce===`pretext`,editDisabledReason:ce===`markdown`?`Visual editing is not available for Markdown documents.`:ce===`latex`?`Visual editing is not available for LaTeX documents.`:``,onChange:e=>he(e)});let z=(0,t.jsx)(l8,{isCollapsed:l,onToggleCollapse:()=>u(e=>!e),onOpenAssetPicker:e.projectAssets===void 0?void 0:()=>_(`isAssetPickerOpen`)}),Ae=`pretext-plus-tab-editor`,je=`pretext-plus-tab-preview`,Me=`pretext-plus-tabpanel`,Ne;return Ne=a?(0,t.jsxs)(`div`,{className:`pretext-plus-editor__tabs`,children:[z,(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:Ae,role:`tab`,"aria-controls":Me,"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:je,role:`tab`,"aria-controls":Me,"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:Me,className:`pretext-plus-editor__tab-panel`,role:`tabpanel`,"aria-labelledby":s===`editor`?Ae:je,children:(0,t.jsx)(`div`,{style:{height:`100%`},children:s===`editor`?Oe:ke})})]})]}):(0,t.jsxs)(`div`,{className:`pretext-plus-editor__sectioned-layout`,children:[z,(0,t.jsxs)(gt,{orientation:`horizontal`,className:`pretext-plus-editor__splitter`,children:[(0,t.jsx)(yt,{className:`pretext-plus-editor__editor-panel`,children:Oe}),(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:ke})]})]}),(0,t.jsxs)(`div`,{className:`pretext-plus-editor`,onKeyDown:R,children:[(0,t.jsx)(t8,{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)(u8,{resetKeys:[le,E],children:Ne}),d?(0,t.jsx)(s3,{onClose:()=>v(`isLatexDialogOpen`)}):null,f&&P&&me?(0,t.jsx)(d3,{sourceContent:le,sourceFormat:ce,pretextSource:me,onConfirm:De,onClose:()=>v(`isConvertDialogOpen`)}):null,p?(0,t.jsx)(C3,{docinfo:O,showCommonDocinfoControls:!0,commonDocinfo:k,initialUseCommonDocinfo:A,onClose:t=>{if(v(`isDocinfoEditorOpen`),t!==void 0){ae({docinfo:t.docinfo,commonDocinfo:t.commonDocinfo,useCommonDocinfo:t.useCommonDocinfo}),e.onCommonDocinfoChange?.(t.commonDocinfo),e.onUseCommonDocinfoChange?.(t.useCommonDocinfo);let n=se??P;ue(n?.xmlId??``,n?.content??``,n?.sourceFormat??`pretext`,{docinfo:t.docinfo,commonDocinfo:t.commonDocinfo,useCommonDocinfo:t.useCommonDocinfo})}}}):null,m&&e.projectAssets!==void 0?(0,t.jsx)(Z6,{open:m,onClose:()=>v(`isAssetPickerOpen`),onLoadAssets:e.onLoadAssets,onLoadLibraryAssets:e.onLoadLibraryAssets,onAddFromLibrary:e.onAssetAddFromLibrary,onUpload:e.onAssetUpload,onFetchUrl:e.onAssetFetchUrl,onCreateDoenet:e.onCreateDoenet,onRemoveAsset:e.onAssetRemove?ye:void 0,onInsert:ve}):null,w?(0,t.jsx)(e8,{asset:w,onClose:g,onSave:async t=>{S(t),await e.onAssetUpdate?.(t)}}):null]})]})};e.CodeEditor=Ps,e.DocinfoEditor=C3,e.Editors=h8,e.FeedbackLink=$4,e.FullPreview=$L,e.VisualEditor=XL,e.assembleFullProjectSource=U6,e.assembleProjectSource=V6,e.buildDivisionTree=P6,e.convertLatexToPretext=J4,e.createConclusion=t6,e.createIntroduction=e6,e.createLatexConclusion=f6,e.createLatexIntroduction=d6,e.createNewLatexSection=u6,e.createNewSection=$3,e.derivePretextContent=Z4,e.detectSourceFormat=q4,e.ensureLatexSectionWrapper=s6,e.ensureSectionWrapper=i6,e.extractDivisionMetadata=h6,e.extractLatexDivisionTitle=l6,e.findDivisionParent=k6,e.getOrphanRoots=F6,e.getOrphanedDivisions=N6,e.getSectionAttributes=m6,e.insertDivisionRef=T6,e.mergeTwoSections=p6,e.moveDivisionRef=D6,e.normalizeSelfClosingRefs=j6,e.parseDivisionRefs=x6,e.postToIframe=QL,e.removeDivisionRef=E6,e.renameDivisionRef=O6,e.reorderDivisionRefs=A6,e.rewrapLatexSection=o6,e.rewrapSection=r6,e.stripLatexSectionWrapper=a6,e.stripSectionWrapper=n6,e.updateDivisionTitle=Q3,e.updateLatexSectionTitle=c6,e.updateSectionMetadata=g6,e.wrapDivisionForPreview=G6});
1084
1084
  //# sourceMappingURL=index.js.map