@pretextbook/web-editor 0.12.0 → 0.13.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -1074,20 +1074,20 @@ Expected it to be ${r}.`}var lW=class extends Error{name=`InvalidDocError`;const
1074
1074
  `),this.column=r,this.excerpt=i,this.line=a,this.name=`XmlError`,this.pos=t}},j=class extends w{constructor(e,t=``){super(),this.name=e,this.content=t}get type(){return w.TYPE_PROCESSING_INSTRUCTION}toJSON(){return Object.assign(w.prototype.toJSON.call(this),{name:this.name,content:this.content})}},M=``,ne=class{constructor(e,t={}){let n=this.document=new A;this.j=n,this.g=t,this.c=new f(e),this.g.includeOffsets&&(n.start=0,n.end=e.length),this.parse()}i(e,t){return e.parent=this.j,this.g.includeOffsets&&(e.start=this.c.f(t),e.end=this.c.f()),this.j.children.push(e),!0}y(e,t){let{children:n}=this.j,{length:r}=n;if(e=N(e),r>0){let t=n[r-1];if(t?.type===w.TYPE_TEXT){let n=t;return n.text+=e,this.g.includeOffsets&&(n.end=this.c.f()),!0}}return this.i(new T(e),t)}H(){let e=Object.create(null);for(;this.e();){let t=this.q();if(!t)break;let n=this.t()&&this.I();if(n===!1)throw this.a(`Attribute value expected`);if(t in e)throw this.a(`Duplicate attribute: ${t}`);if(t===`xml:space`&&n!=="default"&&n!==`preserve`)throw this.a('Value of the `xml:space` attribute must be "default" or "preserve"');e[t]=n}if(this.g.sortAttributes){let t=Object.keys(e).sort(),n=Object.create(null);for(let r=0;r<t.length;++r){let i=t[r];n[i]=e[i]}e=n}return e}I(){let{c:e}=this,t=e.m();if(t!==`"`&&t!==`'`)return!1;e.p();let n,r=!1,i=M,a=t===`"`?p:m;matchLoop:for(;!e.B;)switch(n=e.x(a),n&&(this.o(n),i+=n.replace(h,` `)),e.m()){case t:r=!0;break matchLoop;case`&`:i+=this.C();continue;case`<`:throw this.a("Unescaped `<` is not allowed in an attribute value");default:break matchLoop}if(!r)throw this.a(`Unclosed attribute`);return e.p(),i}J(){let{c:e}=this,t=e.d;if(!e.b(`<![CDATA[`))return!1;let n=e.s(`]]>`);if(this.o(n),!e.b(`]]>`))throw this.a(`Unclosed CDATA section`);return this.g.preserveCdata?this.i(new E(N(n)),t):this.y(n,t)}K(){let{c:e}=this,t=e.d,n=e.x(g);if(!n)return!1;if(this.o(n),e.m(3)===`]]>`)throw this.a("Element content may not contain the CDATA section close delimiter `]]>`");return this.y(n,t)}D(){let{c:e}=this,t=e.d;if(!e.b(`<!--`))return!1;let n=e.s(`--`);if(this.o(n),!e.b(`-->`))throw e.m(2)===`--`?this.a("The string `--` isn't allowed inside a comment"):this.a(`Unclosed comment`);return this.g.preserveComments?this.i(new D(N(n)),t):!0}L(){let e=this.c.d,t=this.C();return t?this.y(t,e):!1}M(){let{c:e}=this,t=e.d;if(!e.b(`<!DOCTYPE`))return!1;let n=this.e()&&this.q();if(!n)throw this.a(`Expected a name`);let r,i;if(this.e()){if(e.b(`PUBLIC`)){if(r=this.e()&&this.N(),r===!1)throw this.a(`Expected a public identifier`);this.e()}if(r!==void 0||e.b(`SYSTEM`)){if(this.e(),i=this.r(),i===!1)throw this.a(`Expected a system identifier`);this.e()}}let a;if(e.b(`[`)){if(a=e.x(/\][\x20\t\r\n]*>/),!e.b(`]`))throw this.a(`Unclosed internal subset`);this.e()}if(!e.b(`>`))throw this.a(`Unclosed doctype declaration`);return this.g.preserveDocumentType?this.i(new ee(n,r,i,a),t):!0}E(){let{c:e}=this,t=e.d;if(!e.b(`<`))return!1;let n=this.q();if(!n)return e.n(t),!1;let r=this.H(),i=!!e.b(`/>`),a=new k(n,r);if(a.parent=this.j,!i){if(!e.b(`>`))throw this.a(`Unclosed start tag for element \`${n}\``);this.j=a;do this.K();while(this.E()||this.L()||this.J()||this.F()||this.D());let t=e.d,r;if(!e.b(`</`)||!(r=this.q())||r!==n)throw e.n(t),this.a(`Missing end tag for element ${n}`);if(this.e(),!e.b(`>`))throw this.a(`Unclosed end tag for element ${n}`);this.j=a.parent}return this.i(a,t)}t(){return this.e(),this.c.b(`=`)?(this.e(),!0):!1}z(){return this.D()||this.F()||this.e()}q(){return y(this.c.m())?this.c.w(v):M}F(){let{c:e}=this,t=e.d;if(!e.b(`<?`))return!1;let n=this.q();if(n){if(n.toLowerCase()===`xml`)throw e.n(t),this.a(`XML declaration isn't allowed here`)}else throw this.a(`Invalid processing instruction`);if(!this.e()){if(e.b(`?>`))return this.i(new j(n),t);throw this.a(`Whitespace is required after a processing instruction name`)}let r=e.s(`?>`);if(this.o(r),!e.b(`?>`))throw this.a(`Unterminated processing instruction`);return this.i(new j(n,N(r)),t)}O(){let{c:e}=this,t=e.d;for(this.P();this.z(););if(this.M())for(;this.z(););return t<e.d}N(){let e=this.c.d,t=this.r();if(t!==!1&&!/^[-\x20\r\na-zA-Z0-9'()+,./:=?;!*#@$_%]*$/.test(t))throw this.c.n(e),this.a(`Invalid character in public identifier`);return t}C(){let{c:e}=this;if(!e.b(`&`))return!1;let t=e.w(b);if(e.G()!==`;`)throw this.a("Unterminated reference (a reference must end with `;`)");let n;if(t[0]===`#`){let e=t[1]===`x`?parseInt(t.slice(2),16):parseInt(t.slice(1),10);if(isNaN(e))throw this.a(`Invalid character reference`);if(!S(e))throw this.a(`Character reference resolves to an invalid character`);n=String.fromCodePoint(e)}else if(n=_[t],n===void 0){let{ignoreUndefinedEntities:n,resolveUndefinedEntity:r}=this.g,i=`&${t};`;if(r){let e=r(i);if(e!=null){let t=typeof e;if(t!==`string`)throw TypeError(`\`resolveUndefinedEntity()\` must return a string, \`null\`, or \`undefined\`, but returned a value of type ${t}`);return e}}if(n)return i;throw e.n(-i.length),this.a(`Named entity isn't defined: ${i}`)}return n}r(){let{c:e}=this,t=e.b(`"`)||e.b(`'`);if(!t)return!1;let n=e.s(t);if(this.o(n),!e.b(t))throw this.a(`Missing end quote`);return n}e(){return!!this.c.w(x)}P(){let{c:e}=this,t=e.d;if(!e.b(`<?xml`))return!1;if(!this.e())throw this.a(`Invalid XML declaration`);let n=!!e.b(`version`)&&this.t()&&this.r();if(n===!1)throw this.a(`XML version is missing or invalid`);if(!/^1\.[0-9]+$/.test(n))throw this.a(`Invalid character in version number`);let r,i;if(this.e()){if(r=!!e.b(`encoding`)&&this.t()&&this.r(),r){if(!/^[A-Za-z][\w.-]*$/.test(r))throw this.a(`Invalid character in encoding name`);this.e()}if(i=!!e.b(`standalone`)&&this.t()&&this.r(),i){if(i!==`yes`&&i!==`no`)throw this.a('Only "yes" and "no" are permitted as values of `standalone`');this.e()}}if(!e.b(`?>`))throw this.a(`Invalid or unclosed XML declaration`);return this.g.preserveXmlDeclaration?this.i(new O(n,r||void 0,i||void 0),t):!0}a(e){let{c:t}=this;return new te(e,t.d,t.h)}parse(){if(this.c.b(``),this.O(),!this.E())throw this.a(`Root element is missing or invalid`);for(;this.z(););if(!this.c.B)throw this.a(`Extra content at the end of the document`)}o(e){let{length:t}=e;for(let n=0;n<t;++n){let t=e.codePointAt(n);if(!S(t))throw this.c.n(-([...e].length-n)),this.a(`Invalid character`);t>65535&&(n+=1)}}};function N(e){let t=0;for(;(t=e.indexOf(`\r`,t))!==-1;)e=e[t+1]===`
1075
1075
  `?e.slice(0,t)+e.slice(t+1):e.slice(0,t)+`
1076
1076
  `+e.slice(t+1);return e}function re(e,t){return new ne(e,t).document}}));function A6(e){let t=String(e),n=[];return{toOffset:i,toPoint:r};function r(e){if(typeof e==`number`&&e>-1&&e<=t.length){let r=0;for(;;){let i=n[r];if(i===void 0){let e=j6(t,n[r-1]);i=e===-1?t.length+1:e+1,n[r]=i}if(i>e)return{line:r+1,column:e-(r>0?n[r-1]:0)+1,offset:e};r++}}}function i(e){if(e&&typeof e.line==`number`&&typeof e.column==`number`&&!Number.isNaN(e.line)&&!Number.isNaN(e.column)){for(;n.length<e.line;){let e=n[n.length-1],r=j6(t,e),i=r===-1?t.length+1:r+1;if(e===i)break;n.push(i)}let r=(e.line>1?n[e.line-2]:0)+e.column-1;if(r<n[e.line-1])return r}}}function j6(e,t){let n=e.indexOf(`\r`,t),r=e.indexOf(`
1077
- `,t);return r===-1?n:n===-1||n+1===r?r:n<r?n:r}var M6=k6();function N6(e){let t=A6(e),n;try{n=(0,M6.parseXml)(String(e),{includeOffsets:!0,preserveCdata:!0,preserveComments:!0,preserveDocumentType:!0,preserveXmlDeclaration:!0})}catch(e){let n=e,r=new FJ("Could not parse XML with `@rgrove/parse-xml`",{cause:n,place:t.toPoint(n.pos),ruleId:`error`,source:`xast-util-from-xml`});throw r.fatal=!0,r.url=`https://github.com/syntax-tree/xast-util-from-xml#throws`,r}let r={location:t},i=I6(n,r);return U6(n,i,r),i}function P6(e){return{type:`cdata`,value:e.text}}function F6(e){return{type:`comment`,value:e.content}}function I6(e,t){return{type:`root`,children:H6(e.children,t)}}function L6(e){return{type:`doctype`,name:e.name,public:e.publicId||void 0,system:e.systemId||void 0}}function R6(e,t){let n=H6(e.children,t);return{type:`element`,name:e.name,attributes:{...e.attributes},children:n}}function z6(e){return{type:`instruction`,name:e.name,value:e.content}}function B6(e){return{type:`text`,value:e.text}}function V6(e){let t=[];return e.version&&t.push(`version="`+e.version+`"`),e.encoding&&t.push(`encoding="`+e.encoding+`"`),e.standalone&&t.push(`standalone="`+e.standalone+`"`),{type:`instruction`,name:`xml`,value:t.join(` `)}}function H6(e,t){let n=[],r=-1;for(;++r<e.length;){let i=e[r],a;i.type===`cdata`?a=P6(i):i.type===`comment`?a=F6(i):i.type===`doctype`?a=L6(i):i.type===`element`?a=R6(i,t):i.type===`pi`?a=z6(i):i.type===`text`?a=B6(i):i.type===`xmldecl`&&(a=V6(i)),a&&(U6(i,a,t),n.push(a))}return n}function U6(e,t,n){let r=e.start===-1?void 0:n.location.toPoint(e.start),i=e.end===-1?void 0:n.location.toPoint(e.end);r&&i&&(t.position={start:r,end:i})}function W6(e){return e.replace(/&/g,`&amp;`).replace(/</g,`&lt;`).replace(/"/g,`&quot;`)}function G6(e,t,n){if(e.isFile&&!e.fileRef&&!e.url)return`<!-- image asset "${t}" is marked as file-based but has no fileRef or url -->`;let r=e.isFile?` source="${W6(e.fileRef??e.url)}"`:``,i=n?` width="${W6(n)}"`:``,a=e.source?.trim();return a?`<image${r}${i}>\n${a}\n</image>`:`<image${r}${i}/>`}function K6(e,t){let n=e.source?.trim();return n?`<interactive xml:id="${W6(t)}">\n${n}\n</interactive>`:`<interactive xml:id="${W6(t)}"></interactive>`}var q6={image:G6,doenet:K6},J6=new Set(Object.keys(q6));function Y6(e,t,n,r){let i=n.find(n=>n.kind===e&&n.ref===t);return i?q6[e](i,t,r):`<!-- missing asset: ${e} ${t} -->`}var X6={closeEmptyElements:!0,tightClose:!0};function Z6(){return`sec-${Date.now().toString(36)}-${Math.random().toString(36).slice(2,8)}`}function Q6(e){let t=e.children.find(e=>e.type===`element`&&e.name===`title`);return t?t.children.filter(e=>e.type===`text`).map(e=>e.value).join(``).trim():``}var $6=new Set([`book`,`article`,`slideshow`,`part`,`chapter`,...new Set([`introduction`,`section`,`worksheet`,`handout`,`exercises`,`references`,`glossary`,`solutions`,`reading-questions`,`conclusion`])]);function e8(e){let t=0,n=e.length;for(;t<n;){let n=e[t];if(n.type!==`text`||/\S/.test(n.value))break;t+=1}for(;n>t;){let t=e[n-1];if(t.type!==`text`||/\S/.test(t.value))break;--n}return e.slice(t,n)}function t8(e){return e.replace(/^(?:[ \t]*\r?\n)+/,``).replace(/(?:\r?\n[ \t]*)+$/,``)}function n8(e){try{return N6(e)}catch{return null}}function r8(e){let t=n8(`<__title__>${e}</__title__>`)?.children.find(e=>e.type===`element`);return t?t.children:[{type:`text`,value:e}]}function i8(e){let t=e.match(/^\s*<([A-Za-z_][\w.:-]*)\b[^>]*?>/);if(!t||t.index===void 0)return e;let n=e.slice(t.index+t[0].length),r=RegExp(`\\s*</${L8(t[1])}\\s*>\\s*$`);return t8(n.replace(r,``))}function a8(e,t){let n=n8(e);if(!n)return e;let r=n.children.find(e=>e.type===`element`);if(!r)return e;let i=r.children.findIndex(e=>e.type===`element`&&e.name===`title`),a={type:`element`,name:`title`,attributes:{},children:r8(t)};return i===-1?r.children.unshift(a):r.children.splice(i,1,a),JY(n,X6)}function o8(e=`New Section`){let t=Z6();return{id:t,xmlId:t,title:e,content:`<section xml:id="${t}">\n\t<title>${e}</title>\n\n\t<p>\n\n\t</p>\n\n</section>`,type:`section`,sourceFormat:`pretext`}}function s8(){let e=Z6();return{id:e,xmlId:e,title:`Introduction`,content:`<introduction xml:id="${e}">\n\n\t<p>\n\n\t</p>\n\n</introduction>`,type:`introduction`,sourceFormat:`pretext`}}function c8(){let e=Z6();return{id:e,xmlId:e,title:`Conclusion`,content:`<conclusion xml:id="${e}">\n\n\t<p>\n\n\t</p>\n\n</conclusion>`,type:`conclusion`,sourceFormat:`pretext`}}function l8(e){let t=n8(e);if(!t)return i8(e);let n=t.children.find(e=>e.type===`element`);return n?JY({type:`root`,children:e8(n.children)},X6):e}function u8(e,t){return`<${t}>\n${t8(e)}\n</${t}>`}function d8(e,t){return e.trimStart().startsWith(`<${t}`)?e:u8(e,t)}function f8(e,t){if(t===`introduction`||t===`conclusion`)return e;let n=e.trimStart();return n.startsWith(`\\begin{section}`)?n.replace(/^\\begin\{section\}\s*\n?/,``).replace(/\n?\\end\{section\}\s*$/,``):e.replace(/^\\section\*?\{[^}]*\}\s*\n?/,``)}function p8(e,t,n,r){return t===`introduction`||t===`conclusion`?e:r?.trimStart().startsWith(`\\begin{section}`)?`\\begin{section}\n\n${e}\n\n\\end{section}`:`\\section{${n}}\n\n${e}`}function m8(e,t,n,r){if(t===`introduction`||t===`conclusion`)return e;let i=e.trimStart();return i.startsWith(`\\section`)||i.startsWith(`\\begin{section}`)?e:p8(e,t,n,r)}var h8=/^(\s*)\\(?!begin\b|end\b)[A-Za-z][A-Za-z-]*(\*?\{)/;function g8(e,t){let n=/^(\s*\\(?!begin\b|end\b)[A-Za-z][A-Za-z-]*\*?\{)[^}]*/;return n.test(e)?e.replace(n,(e,n)=>`${n}${t}`):e.includes(`\\begin{section}`)?/\\title\{/.test(e)?e.replace(/\\title\{[^}]*\}/,`\\title{${t}}`):e.replace(`\\begin{section}`,`\\begin{section}\n\n\\title{${t}}\n\n`):e}function _8(e){let t=/^\s*\\(?!begin\b|end\b)[A-Za-z][A-Za-z-]*\*?\{([^}]*)\}/.exec(e);if(t)return t[1].trim();if(e.includes(`\\begin{section}`)){let t=/\\title\{([^}]*)\}/.exec(e);if(t)return t[1].trim()}return null}function v8(e){return/^\s*\\(?!begin\b|end\b)[A-Za-z][A-Za-z-]*\*?\{[^}]*\}\s*\\label\{([^}]*)\}/.exec(e)?.[1]?.trim()??``}function y8(e,t){let n=e.content;return t.type!==void 0&&(n=n.replace(h8,`$1\\${t.type}$2`)),t.title!==void 0&&(n=g8(n,t.title)),t.xmlId!==void 0&&(n=n.replace(/^(\s*\\(?!begin\b|end\b)[A-Za-z][A-Za-z-]*\*?\{[^}]*\})(\s*\\label\{[^}]*\})?/,(e,n)=>t.xmlId==null||t.xmlId===``?n:`${n}\\label{${t.xmlId}}`)),{...e,content:n,title:t.title??e.title,type:t.type??e.type,xmlId:t.xmlId||e.xmlId}}function b8(e){let{pretextSource:t,pretextError:n}=e6(e.content,`latex`);return n||t===void 0?null:l5.has(e.type)?`<${e.type} xml:id="${e.xmlId}">\n<title>${e.title}</title>\n\n${t}\n</${e.type}>`:t}function x8(e=`New Section`){let t=Z6();return{id:t,xmlId:t,title:e,content:`\\section{${e}}\n\n`,type:`section`,sourceFormat:`latex`}}function S8(){let e=Z6();return{id:e,xmlId:e,title:`Introduction`,content:`% Introduction
1077
+ `,t);return r===-1?n:n===-1||n+1===r?r:n<r?n:r}var M6=k6();function N6(e){let t=A6(e),n;try{n=(0,M6.parseXml)(String(e),{includeOffsets:!0,preserveCdata:!0,preserveComments:!0,preserveDocumentType:!0,preserveXmlDeclaration:!0})}catch(e){let n=e,r=new FJ("Could not parse XML with `@rgrove/parse-xml`",{cause:n,place:t.toPoint(n.pos),ruleId:`error`,source:`xast-util-from-xml`});throw r.fatal=!0,r.url=`https://github.com/syntax-tree/xast-util-from-xml#throws`,r}let r={location:t},i=I6(n,r);return U6(n,i,r),i}function P6(e){return{type:`cdata`,value:e.text}}function F6(e){return{type:`comment`,value:e.content}}function I6(e,t){return{type:`root`,children:H6(e.children,t)}}function L6(e){return{type:`doctype`,name:e.name,public:e.publicId||void 0,system:e.systemId||void 0}}function R6(e,t){let n=H6(e.children,t);return{type:`element`,name:e.name,attributes:{...e.attributes},children:n}}function z6(e){return{type:`instruction`,name:e.name,value:e.content}}function B6(e){return{type:`text`,value:e.text}}function V6(e){let t=[];return e.version&&t.push(`version="`+e.version+`"`),e.encoding&&t.push(`encoding="`+e.encoding+`"`),e.standalone&&t.push(`standalone="`+e.standalone+`"`),{type:`instruction`,name:`xml`,value:t.join(` `)}}function H6(e,t){let n=[],r=-1;for(;++r<e.length;){let i=e[r],a;i.type===`cdata`?a=P6(i):i.type===`comment`?a=F6(i):i.type===`doctype`?a=L6(i):i.type===`element`?a=R6(i,t):i.type===`pi`?a=z6(i):i.type===`text`?a=B6(i):i.type===`xmldecl`&&(a=V6(i)),a&&(U6(i,a,t),n.push(a))}return n}function U6(e,t,n){let r=e.start===-1?void 0:n.location.toPoint(e.start),i=e.end===-1?void 0:n.location.toPoint(e.end);r&&i&&(t.position={start:r,end:i})}function W6(e){return e.replace(/&/g,`&amp;`).replace(/</g,`&lt;`).replace(/"/g,`&quot;`)}var G6={"image/png":`png`,"image/jpeg":`jpg`,"image/gif":`gif`,"image/svg+xml":`svg`,"image/webp":`webp`,"image/bmp":`bmp`,"image/tiff":`tiff`};function K6(e){let t=e.split(/[?#]/)[0];return/\.([a-zA-Z0-9]+)$/.exec(t)?.[1]}function q6(e){return e.fileRef&&K6(e.fileRef)||e.url&&K6(e.url)||e.contentType&&G6[e.contentType]}function J6(e,t,n){if(e.isFile&&!e.fileRef&&!e.url)return`<!-- image asset "${t}" is marked as file-based but has no fileRef or url -->`;let r=e.isFile?q6(e):void 0,i=e.isFile?` source="${W6(r?`${t}.${r}`:t)}"`:``,a=n?` width="${W6(n)}"`:``,o=e.source?.trim();return o?`<image${i}${a}>\n${o}\n</image>`:`<image${i}${a}/>`}function Y6(e,t){let n=e.source?.trim();return n?`<interactive xml:id="${W6(t)}">\n${n}\n</interactive>`:`<interactive xml:id="${W6(t)}"></interactive>`}var X6={image:J6,doenet:Y6},Z6=new Set(Object.keys(X6));function Q6(e,t,n,r){let i=n.find(n=>n.kind===e&&n.ref===t);return i?X6[e](i,t,r):`<!-- missing asset: ${e} ${t} -->`}var $6={closeEmptyElements:!0,tightClose:!0};function e8(){return`sec-${Date.now().toString(36)}-${Math.random().toString(36).slice(2,8)}`}function t8(e){let t=e.children.find(e=>e.type===`element`&&e.name===`title`);return t?t.children.filter(e=>e.type===`text`).map(e=>e.value).join(``).trim():``}var n8=new Set([`book`,`article`,`slideshow`,`part`,`chapter`,...new Set([`introduction`,`section`,`worksheet`,`handout`,`exercises`,`references`,`glossary`,`solutions`,`reading-questions`,`conclusion`])]);function r8(e){let t=0,n=e.length;for(;t<n;){let n=e[t];if(n.type!==`text`||/\S/.test(n.value))break;t+=1}for(;n>t;){let t=e[n-1];if(t.type!==`text`||/\S/.test(t.value))break;--n}return e.slice(t,n)}function i8(e){return e.replace(/^(?:[ \t]*\r?\n)+/,``).replace(/(?:\r?\n[ \t]*)+$/,``)}function a8(e){try{return N6(e)}catch{return null}}function o8(e){let t=a8(`<__title__>${e}</__title__>`)?.children.find(e=>e.type===`element`);return t?t.children:[{type:`text`,value:e}]}function s8(e){let t=e.match(/^\s*<([A-Za-z_][\w.:-]*)\b[^>]*?>/);if(!t||t.index===void 0)return e;let n=e.slice(t.index+t[0].length),r=RegExp(`\\s*</${B8(t[1])}\\s*>\\s*$`);return i8(n.replace(r,``))}function c8(e,t){let n=a8(e);if(!n)return e;let r=n.children.find(e=>e.type===`element`);if(!r)return e;let i=r.children.findIndex(e=>e.type===`element`&&e.name===`title`),a={type:`element`,name:`title`,attributes:{},children:o8(t)};return i===-1?r.children.unshift(a):r.children.splice(i,1,a),JY(n,$6)}function l8(e=`New Section`){let t=e8();return{id:t,xmlId:t,title:e,content:`<section xml:id="${t}">\n\t<title>${e}</title>\n\n\t<p>\n\n\t</p>\n\n</section>`,type:`section`,sourceFormat:`pretext`}}function u8(){let e=e8();return{id:e,xmlId:e,title:`Introduction`,content:`<introduction xml:id="${e}">\n\n\t<p>\n\n\t</p>\n\n</introduction>`,type:`introduction`,sourceFormat:`pretext`}}function d8(){let e=e8();return{id:e,xmlId:e,title:`Conclusion`,content:`<conclusion xml:id="${e}">\n\n\t<p>\n\n\t</p>\n\n</conclusion>`,type:`conclusion`,sourceFormat:`pretext`}}function f8(e){let t=a8(e);if(!t)return s8(e);let n=t.children.find(e=>e.type===`element`);return n?JY({type:`root`,children:r8(n.children)},$6):e}function p8(e,t){return`<${t}>\n${i8(e)}\n</${t}>`}function m8(e,t){return e.trimStart().startsWith(`<${t}`)?e:p8(e,t)}function h8(e,t){if(t===`introduction`||t===`conclusion`)return e;let n=e.trimStart();return n.startsWith(`\\begin{section}`)?n.replace(/^\\begin\{section\}\s*\n?/,``).replace(/\n?\\end\{section\}\s*$/,``):e.replace(/^\\section\*?\{[^}]*\}\s*\n?/,``)}function g8(e,t,n,r){return t===`introduction`||t===`conclusion`?e:r?.trimStart().startsWith(`\\begin{section}`)?`\\begin{section}\n\n${e}\n\n\\end{section}`:`\\section{${n}}\n\n${e}`}function _8(e,t,n,r){if(t===`introduction`||t===`conclusion`)return e;let i=e.trimStart();return i.startsWith(`\\section`)||i.startsWith(`\\begin{section}`)?e:g8(e,t,n,r)}var v8=/^(\s*)\\(?!begin\b|end\b)[A-Za-z][A-Za-z-]*(\*?\{)/;function y8(e,t){let n=/^(\s*\\(?!begin\b|end\b)[A-Za-z][A-Za-z-]*\*?\{)[^}]*/;return n.test(e)?e.replace(n,(e,n)=>`${n}${t}`):e.includes(`\\begin{section}`)?/\\title\{/.test(e)?e.replace(/\\title\{[^}]*\}/,`\\title{${t}}`):e.replace(`\\begin{section}`,`\\begin{section}\n\n\\title{${t}}\n\n`):e}function b8(e){let t=/^\s*\\(?!begin\b|end\b)[A-Za-z][A-Za-z-]*\*?\{([^}]*)\}/.exec(e);if(t)return t[1].trim();if(e.includes(`\\begin{section}`)){let t=/\\title\{([^}]*)\}/.exec(e);if(t)return t[1].trim()}return null}function x8(e){return/^\s*\\(?!begin\b|end\b)[A-Za-z][A-Za-z-]*\*?\{[^}]*\}\s*\\label\{([^}]*)\}/.exec(e)?.[1]?.trim()??``}function S8(e,t){let n=e.content;return t.type!==void 0&&(n=n.replace(v8,`$1\\${t.type}$2`)),t.title!==void 0&&(n=y8(n,t.title)),t.xmlId!==void 0&&(n=n.replace(/^(\s*\\(?!begin\b|end\b)[A-Za-z][A-Za-z-]*\*?\{[^}]*\})(\s*\\label\{[^}]*\})?/,(e,n)=>t.xmlId==null||t.xmlId===``?n:`${n}\\label{${t.xmlId}}`)),{...e,content:n,title:t.title??e.title,type:t.type??e.type,xmlId:t.xmlId||e.xmlId}}function C8(e){let{pretextSource:t,pretextError:n}=e6(e.content,`latex`);return n||t===void 0?null:h5.has(e.type)?`<${e.type} xml:id="${e.xmlId}">\n<title>${e.title}</title>\n\n${t}\n</${e.type}>`:t}function w8(e=`New Section`){let t=e8();return{id:t,xmlId:t,title:e,content:`\\section{${e}}\n\n`,type:`section`,sourceFormat:`latex`}}function T8(){let e=e8();return{id:e,xmlId:e,title:`Introduction`,content:`% Introduction
1078
1078
 
1079
- `,type:`introduction`,sourceFormat:`latex`}}function C8(){let e=Z6();return{id:e,xmlId:e,title:`Conclusion`,content:`% Conclusion
1079
+ `,type:`introduction`,sourceFormat:`latex`}}function E8(){let e=e8();return{id:e,xmlId:e,title:`Conclusion`,content:`% Conclusion
1080
1080
 
1081
- `,type:`conclusion`,sourceFormat:`latex`}}function w8(e,t,n){if(n){let n=f8(t.content,t.type);return{...e,content:e.content.trimEnd()+`
1081
+ `,type:`conclusion`,sourceFormat:`latex`}}function D8(e,t,n){if(n){let n=h8(t.content,t.type);return{...e,content:e.content.trimEnd()+`
1082
1082
 
1083
- `+n.trimStart()}}let r=n8(e.content),i=n8(t.content),a=r?.children.find(e=>e.type===`element`),o=i?.children.find(e=>e.type===`element`);if(!a||!o)return{...e,content:e.content+`
1083
+ `+n.trimStart()}}let r=a8(e.content),i=a8(t.content),a=r?.children.find(e=>e.type===`element`),o=i?.children.find(e=>e.type===`element`);if(!a||!o)return{...e,content:e.content+`
1084
1084
 
1085
- `+t.content};let s=o.children.filter(e=>!(e.type===`element`&&e.name===`title`)),c={...a,children:[...a.children,...s]};return{...e,content:JY({type:`root`,children:[c]},X6)}}function T8(e){try{let t=N6(e).children.find(e=>e.type===`element`);return t?{xmlId:t.attributes?.[`xml:id`]??``,label:t.attributes?.label??``}:{xmlId:``,label:``}}catch{return{xmlId:``,label:``}}}function E8(e){return e.trim().replace(/[^A-Za-z0-9_-]/g,`-`).replace(/^[^A-Za-z_]+/,``)}function D8(e){return E8(e.trim().toLowerCase().replace(/[^a-z0-9]+/g,`-`).replace(/^-+|-+$/g,``))}function O8(e){let t=n8(e);if(!t)return null;let n=t.children.find(e=>e.type===`element`);return!n||!$6.has(n.name)?null:{title:Q6(n),type:n.name,xmlId:n.attributes?.[`xml:id`]??``,label:n.attributes?.label??``}}function k8(e,t){let n=t.type??e.type,r=t.title??e.title;try{let i=N6(e.content).children.find(e=>e.type===`element`);if(!i)return{...e,title:r,type:n};let a={...i,name:n,attributes:{...i.attributes}};t.xmlId!==void 0&&(t.xmlId===null||t.xmlId===``?delete a.attributes[`xml:id`]:a.attributes[`xml:id`]=t.xmlId),t.label!==void 0&&(t.label===null||t.label===``?delete a.attributes.label:a.attributes.label=t.label);let o=a.children.findIndex(e=>e.type===`element`&&e.name===`title`),s={type:`element`,name:`title`,attributes:{},children:r8(r)};o===-1?a.children=[s,...a.children]:a.children=[...a.children.slice(0,o),s,...a.children.slice(o+1)];let c=JY({type:`root`,children:[a]},X6),l=t.xmlId!==void 0&&t.xmlId!==null&&t.xmlId!==``?t.xmlId:e.xmlId;return{...e,title:r,type:n,xmlId:l,content:c}}catch{return{...e,title:r,type:n}}}var A8=/^\uFEFF?[ \t]*---[ \t]*\r?\n([\s\S]*?)\r?\n[ \t]*---[ \t]*(?:\r?\n|$)/;function j8(e){let t=A8.exec(e);if(!t)return null;let n=e.slice(t[0].length),r=`section`,i=``,a=``;for(let e of t[1].split(/\r?\n/)){let t=/^[ \t]*(xmlid|xml:id|division|label)[ \t]*:[ \t]*(.*)$/.exec(e);if(!t)continue;let n=t[2].trim().replace(/^["']|["']$/g,``);t[1]===`division`?r=n:t[1]===`xmlid`||t[1]===`xml:id`?i=n:a=n}return{type:r||`section`,xmlId:i,label:a,body:n}}function M8(e){let t=[`division: ${e.type}`,`xmlid: ${e.xmlId}`];return e.label&&t.push(`label: ${e.label}`),`---\n${t.join(`
1086
- `)}\n---`}function N8(e){let t=/^[ \t]*#[ \t]+(.*)$/m.exec(e);return t?t[1].trim():null}function P8(e,t){let n=/^[ \t]*#[ \t]+.*$/m.exec(e);return n?e.slice(0,n.index)+`# ${t}`+e.slice(n.index+n[0].length):`# ${t}\n\n${e.replace(/^\s+/,``)}`}function F8(e){let t=j8(e);return t?{title:N8(t.body)??``,type:t.type,xmlId:t.xmlId,label:t.label}:null}function I8(e,t){let n=j8(e.content),r=n?n.body:e.content,i=n?.type??e.type,a=n?.xmlId??e.xmlId,o=n?.label??``,s=t.type??i,c=(t.xmlId===void 0?a:t.xmlId??``)||e.xmlId,l=t.label===void 0?o:t.label??``,u=t.title===void 0?r:P8(r,t.title),d=`${M8({type:s,xmlId:c,label:l})}\n${u}`;return{...e,title:t.title??e.title,type:s,xmlId:c,content:d}}function L8(e){return e.replace(/[.*+?^${}()|[\]\\]/g,`\\$&`)}function R8(e){return e===`pretext`||e===`markdown`||e===`latex`}var z8=new Set([`division`,...$6,`subsection`,`subsubsection`,`paragraphs`]),B8=Array.from(z8).join(`|`);function V8(e){let t=e===null?`ref="([^"]+)"`:`ref="${L8(e)}"`,n=`(?:${B8})`;return`<plus:${n}\\s[^>]*${t}[^>]*?(?:/>|>\\s*</plus:${n}>)`}function H8(e){let t=e===null?`ref="([^"]+)"`:`ref="${L8(e)}"`;return`::${`(?:${B8})`}(?:\\[[^\\]]*\\])?\\{[^}]*${t}[^}]*\\}`}function U8(e){let t=e===null?`([^}]+)`:L8(e);return`\\\\plus\\{${`(?:${B8})`}\\}\\{${t}\\}`}function W8(e){return`(?:${V8(e)}|${H8(e)}|${U8(e)})`}function G8(e){let t=[],n=new RegExp(W8(null),`g`),r;for(;(r=n.exec(e))!==null;)t.push(r[1]??r[2]??r[3]);return t}function K8(e){let t=[],n=`(?:${B8})`,r=RegExp(`<plus:(${B8})\\s[^>]*ref="([^"]+)"[^>]*?(?:/>|>\\s*</plus:${n}>)|::(${B8})(?:\\[[^\\]]*\\])?\\{[^}]*ref="([^"]+)"[^}]*\\}|\\\\plus\\{(${B8})\\}\\{([^}]+)\\}`,`g`),i;for(;(i=r.exec(e))!==null;){let e=i[1]??i[3]??i[5],n=i[2]??i[4]??i[6],r=e===`division`?`section`:e;t.push({type:r,xmlId:n})}return t}function q8(e){let t=[],n=/<plus:(image|doenet)\b[^>]*\bref="([^"]+)"|::(image|doenet)(?:\[[^\]]*\])?\{[^}]*\bref="([^"]+)"[^}]*\}|\\plus\{(image|doenet)\}\{([^}]+)\}/g,r;for(;(r=n.exec(e))!==null;)t.push({kind:r[1]??r[3]??r[5],ref:r[2]??r[4]??r[6]});return t}function J8(e,t,n,r){let i=L8(t),a=L8(n),o=RegExp(`(<plus:${i}\\b[^>]*?\\bref=")${a}(")`,`g`),s=RegExp(`(::${i}(?:\\[[^\\]]*\\])?\\{[^}]*?\\bref=")${a}(")`,`g`),c=RegExp(`(\\\\plus\\{${i}\\}\\{)${a}(\\})`,`g`);return e.replace(o,`$1${r}$2`).replace(s,`$1${r}$2`).replace(c,`$1${r}$2`)}function Y8(e,t,n){let r=L8(t),i=L8(n),a=RegExp(`<plus:${r}\\b[^>]*?\\bref="${i}"[^>]*/?>`,`g`),o=RegExp(`::${r}(?:\\[[^\\]]*\\])?\\{[^}]*?\\bref="${i}"[^}]*\\}`,`g`),s=RegExp(`\\\\plus\\{${r}\\}\\{${i}\\}`,`g`);return e.replace(a,``).replace(o,``).replace(s,``)}function X8(e,t,n=`pretext`){return n===`markdown`?`::${e}{ref="${t}"}`:n===`latex`?`\\plus{${e}}{${t}}`:`<plus:${e} ref="${t}"/>`}function Z8(e,t,n=`pretext`){let r=`New ${t.charAt(0).toUpperCase()+t.slice(1)}`;return{id:e,xmlId:e,title:r,type:t,sourceFormat:n,content:Q8(t,n,r,e)}}function Q8(e,t,n,r){return t===`latex`?e===`introduction`||e===`conclusion`?`% ${n}\n\n`:`\\${e}{${n}}\\label{${r}}\n\n`:t===`markdown`?`${M8({type:e,xmlId:r,label:``})}\n# ${n}\n\n`:e===`introduction`||e===`conclusion`?`<${e} xml:id="${r}">\n\n\t<p>\n\n\t</p>\n\n</${e}>`:`<${e} xml:id="${r}">\n\t<title>${n}</title>\n\n\t<p>\n\n\t</p>\n\n</${e}>`}function $8(e,t,n,r,i=`pretext`){let a=i===`markdown`?`::${n}{ref="${t}"}`:i===`latex`?`\\plus{${n}}{${t}}`:`<plus:${n} ref="${t}"/>`;if(r!==null){let t=new RegExp(W8(r)).exec(e);if(t){let n=t.index+t[0].length;return e.slice(0,n)+`
1085
+ `+t.content};let s=o.children.filter(e=>!(e.type===`element`&&e.name===`title`)),c={...a,children:[...a.children,...s]};return{...e,content:JY({type:`root`,children:[c]},$6)}}function O8(e){try{let t=N6(e).children.find(e=>e.type===`element`);return t?{xmlId:t.attributes?.[`xml:id`]??``,label:t.attributes?.label??``}:{xmlId:``,label:``}}catch{return{xmlId:``,label:``}}}function k8(e){return e.trim().replace(/[^A-Za-z0-9_-]/g,`-`).replace(/^[^A-Za-z_]+/,``)}function A8(e){return k8(e.trim().toLowerCase().replace(/[^a-z0-9]+/g,`-`).replace(/^-+|-+$/g,``))}function j8(e){let t=a8(e);if(!t)return null;let n=t.children.find(e=>e.type===`element`);return!n||!n8.has(n.name)?null:{title:t8(n),type:n.name,xmlId:n.attributes?.[`xml:id`]??``,label:n.attributes?.label??``}}function M8(e,t){let n=t.type??e.type,r=t.title??e.title;try{let i=N6(e.content).children.find(e=>e.type===`element`);if(!i)return{...e,title:r,type:n};let a={...i,name:n,attributes:{...i.attributes}};t.xmlId!==void 0&&(t.xmlId===null||t.xmlId===``?delete a.attributes[`xml:id`]:a.attributes[`xml:id`]=t.xmlId),t.label!==void 0&&(t.label===null||t.label===``?delete a.attributes.label:a.attributes.label=t.label);let o=a.children.findIndex(e=>e.type===`element`&&e.name===`title`),s={type:`element`,name:`title`,attributes:{},children:o8(r)};o===-1?a.children=[s,...a.children]:a.children=[...a.children.slice(0,o),s,...a.children.slice(o+1)];let c=JY({type:`root`,children:[a]},$6),l=t.xmlId!==void 0&&t.xmlId!==null&&t.xmlId!==``?t.xmlId:e.xmlId;return{...e,title:r,type:n,xmlId:l,content:c}}catch{return{...e,title:r,type:n}}}var N8=/^\uFEFF?[ \t]*---[ \t]*\r?\n([\s\S]*?)\r?\n[ \t]*---[ \t]*(?:\r?\n|$)/;function P8(e){let t=N8.exec(e);if(!t)return null;let n=e.slice(t[0].length),r=`section`,i=``,a=``;for(let e of t[1].split(/\r?\n/)){let t=/^[ \t]*(xmlid|xml:id|division|label)[ \t]*:[ \t]*(.*)$/.exec(e);if(!t)continue;let n=t[2].trim().replace(/^["']|["']$/g,``);t[1]===`division`?r=n:t[1]===`xmlid`||t[1]===`xml:id`?i=n:a=n}return{type:r||`section`,xmlId:i,label:a,body:n}}function F8(e){let t=[`division: ${e.type}`,`xmlid: ${e.xmlId}`];return e.label&&t.push(`label: ${e.label}`),`---\n${t.join(`
1086
+ `)}\n---`}function I8(e){let t=/^[ \t]*#[ \t]+(.*)$/m.exec(e);return t?t[1].trim():null}function L8(e,t){let n=/^[ \t]*#[ \t]+.*$/m.exec(e);return n?e.slice(0,n.index)+`# ${t}`+e.slice(n.index+n[0].length):`# ${t}\n\n${e.replace(/^\s+/,``)}`}function R8(e){let t=P8(e);return t?{title:I8(t.body)??``,type:t.type,xmlId:t.xmlId,label:t.label}:null}function z8(e,t){let n=P8(e.content),r=n?n.body:e.content,i=n?.type??e.type,a=n?.xmlId??e.xmlId,o=n?.label??``,s=t.type??i,c=(t.xmlId===void 0?a:t.xmlId??``)||e.xmlId,l=t.label===void 0?o:t.label??``,u=t.title===void 0?r:L8(r,t.title),d=`${F8({type:s,xmlId:c,label:l})}\n${u}`;return{...e,title:t.title??e.title,type:s,xmlId:c,content:d}}function B8(e){return e.replace(/[.*+?^${}()|[\]\\]/g,`\\$&`)}function V8(e){return e===`pretext`||e===`markdown`||e===`latex`}var H8=new Set([`division`,...n8,`subsection`,`subsubsection`,`paragraphs`]),U8=Array.from(H8).join(`|`);function W8(e){let t=e===null?`ref="([^"]+)"`:`ref="${B8(e)}"`,n=`(?:${U8})`;return`<plus:${n}\\s[^>]*${t}[^>]*?(?:/>|>\\s*</plus:${n}>)`}function G8(e){let t=e===null?`ref="([^"]+)"`:`ref="${B8(e)}"`;return`::${`(?:${U8})`}(?:\\[[^\\]]*\\])?\\{[^}]*${t}[^}]*\\}`}function K8(e){let t=e===null?`([^}]+)`:B8(e);return`\\\\plus\\{${`(?:${U8})`}\\}\\{${t}\\}`}function q8(e){return`(?:${W8(e)}|${G8(e)}|${K8(e)})`}function J8(e,t){switch(e){case`pretext`:return W8(t);case`markdown`:return G8(t);case`latex`:return K8(t)}}var Y8={pretext:[],markdown:["```[\\s\\S]*?```",`~~~[\\s\\S]*?~~~`,"`[^`\\n]*`"],latex:[`\\\\begin\\{verbatim\\}[\\s\\S]*?\\\\end\\{verbatim\\}`,`\\\\begin\\{lstlisting\\}[\\s\\S]*?\\\\end\\{lstlisting\\}`,`\\\\verb\\*?\\|[^|\\n]*\\|`]};function X8(e,t){let n=Y8[t];if(n.length===0)return e;let r=new RegExp(n.join(`|`),`g`);return e.replace(r,e=>e.replace(/[^\n]/g,` `))}function Z8(e,t){let n=[],r=new RegExp(J8(t,null),`g`),i=X8(e,t),a;for(;(a=r.exec(i))!==null;)n.push(a[1]);return n}function Q8(e,t){let n=[],r=U8,i={pretext:`<plus:(${r})\\s[^>]*ref="([^"]+)"[^>]*?(?:/>|>\\s*</plus:${`(?:${r})`}>)`,markdown:`::(${r})(?:\\[[^\\]]*\\])?\\{[^}]*ref="([^"]+)"[^}]*\\}`,latex:`\\\\plus\\{(${r})\\}\\{([^}]+)\\}`},a=new RegExp(i[t],`g`),o=X8(e,t),s;for(;(s=a.exec(o))!==null;){let e=s[1],t=s[2],r=e===`division`?`section`:e;n.push({type:r,xmlId:t})}return n}function $8(e,t){let n=[],r=new RegExp({pretext:`<plus:(image|doenet)\\b[^>]*\\bref="([^"]+)"`,markdown:`::(image|doenet)(?:\\[[^\\]]*\\])?\\{[^}]*\\bref="([^"]+)"[^}]*\\}`,latex:`\\\\plus\\{(image|doenet)\\}\\{([^}]+)\\}`}[t],`g`),i=X8(e,t),a;for(;(a=r.exec(i))!==null;)n.push({kind:a[1],ref:a[2]});return n}function e5(e,t,n,r){let i=B8(t),a=B8(n),o=RegExp(`(<plus:${i}\\b[^>]*?\\bref=")${a}(")`,`g`),s=RegExp(`(::${i}(?:\\[[^\\]]*\\])?\\{[^}]*?\\bref=")${a}(")`,`g`),c=RegExp(`(\\\\plus\\{${i}\\}\\{)${a}(\\})`,`g`);return e.replace(o,`$1${r}$2`).replace(s,`$1${r}$2`).replace(c,`$1${r}$2`)}function t5(e,t,n){let r=B8(t),i=B8(n),a=RegExp(`<plus:${r}\\b[^>]*?\\bref="${i}"[^>]*/?>`,`g`),o=RegExp(`::${r}(?:\\[[^\\]]*\\])?\\{[^}]*?\\bref="${i}"[^}]*\\}`,`g`),s=RegExp(`\\\\plus\\{${r}\\}\\{${i}\\}`,`g`);return e.replace(a,``).replace(o,``).replace(s,``)}function n5(e,t,n=`pretext`){return n===`markdown`?`::${e}{ref="${t}"}`:n===`latex`?`\\plus{${e}}{${t}}`:`<plus:${e} ref="${t}"/>`}function r5(e,t,n=`pretext`){let r=`New ${t.charAt(0).toUpperCase()+t.slice(1)}`;return{id:e,xmlId:e,title:r,type:t,sourceFormat:n,content:i5(t,n,r,e)}}function i5(e,t,n,r){return t===`latex`?e===`introduction`||e===`conclusion`?`% ${n}\n\n`:`\\${e}{${n}}\\label{${r}}\n\n`:t===`markdown`?`${F8({type:e,xmlId:r,label:``})}\n# ${n}\n\n`:e===`introduction`||e===`conclusion`?`<${e} xml:id="${r}">\n\n\t<p>\n\n\t</p>\n\n</${e}>`:`<${e} xml:id="${r}">\n\t<title>${n}</title>\n\n\t<p>\n\n\t</p>\n\n</${e}>`}function a5(e,t,n,r,i=`pretext`){let a=i===`markdown`?`::${n}{ref="${t}"}`:i===`latex`?`\\plus{${n}}{${t}}`:`<plus:${n} ref="${t}"/>`;if(r!==null){let t=new RegExp(q8(r)).exec(e);if(t){let n=t.index+t[0].length;return e.slice(0,n)+`
1087
1087
  `+a+e.slice(n)}}let o=e.lastIndexOf(`</`);return o===-1?e+`
1088
1088
  `+a:e.slice(0,o)+a+`
1089
- `+e.slice(o)}function e5(e,t){let n=RegExp(`[ \t]*${W8(t)}[ \t]*\n?`,`g`);return e.replace(n,``)}function t5(e,t,n){let r=new RegExp(W8(t)).exec(e),i=r?r[0]:`<plus:division ref="${t}"/>`,a=e5(e,t);if(n!==null){let e=new RegExp(W8(n)).exec(a);if(e){let t=e.index+e[0].length;return a.slice(0,t)+`
1089
+ `+e.slice(o)}function o5(e,t){let n=RegExp(`[ \t]*${q8(t)}[ \t]*\n?`,`g`);return e.replace(n,``)}function s5(e,t,n){let r=new RegExp(q8(t)).exec(e),i=r?r[0]:`<plus:division ref="${t}"/>`,a=o5(e,t);if(n!==null){let e=new RegExp(q8(n)).exec(a);if(e){let t=e.index+e[0].length;return a.slice(0,t)+`
1090
1090
  `+i+a.slice(t)}}let o=a.lastIndexOf(`</`);return o===-1?a+`
1091
1091
  `+i:a.slice(0,o)+i+`
1092
- `+a.slice(o)}function n5(e,t,n,r){let i=new RegExp(W8(t));return e.replace(i,e=>{let t=e.trimStart();return t.startsWith(`::`)?`::${r}{ref="${n}"}`:t.startsWith(`\\plus`)?`\\plus{${r}}{${n}}`:`<plus:${r} ref="${n}"/>`})}function r5(e,t){let n=new RegExp(W8(t));return e.find(e=>n.test(e.content))??null}function i5(e,t){let n=e,r=null;for(let e of t)n=t5(n,e,r),r=e;return n}function a5(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 G8(i.content))r.push(e)}return n}function o5(e,t){let n=a5(e,t);return e.filter(e=>!n.has(e.xmlId))}function s5(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 G8(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 c5(e,t){let n=o5(e,t),r=new Set(n.map(e=>e.xmlId)),i=new Set;for(let e of n)for(let t of G8(e.content))r.has(t)&&i.add(t);return n.filter(e=>!i.has(e.xmlId))}var l5=new Set([`book`,`article`,`slideshow`]);function u5(e){try{let t=N6(e),n=t.children.find(e=>e.type===`element`),r=n?.name===`pretext`?n.children.find(e=>e.type===`element`&&l5.has(e.name)):n&&l5.has(n.name)?n:void 0;if(!r)return e;if(!r.attributes.label){let e=f5(t,`pretext-plus-preview`);return r.attributes.label=e,JY(t,X6)}return e}catch(t){return console.error(`Error ensuring label:`,t),e}}function d5(e,t){return e.type===`element`&&e.attributes?.label===t?!0:`children`in e&&e.children?e.children.some(e=>e.type===`element`?d5(e,t):!1):!1}function f5(e,t){let n=t,r=1;for(;d5(e,n);)n=`${t}-${r}`,r++;return n}function p5(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 if(i.sourceFormat===`markdown`){let{pretextSource:e,pretextError:t}=e6(i.content,`markdown`);a=e??`<!-- conversion error: ${t} -->`}else a=b8(i)??`<!-- conversion error: ${i.xmlId} -->`;let o=new Set(n).add(e);return a.replace(/<plus:([a-z-]+)\s([^>]*ref="[^"]+"[^>]*?)(?:\/>|>\s*<\/plus:\1>)/g,(e,n,i)=>{let a=/ref="([^"]+)"/.exec(i)?.[1]??``;if(!J6.has(n))return p5(a,t,o,r);let s=/width="([^"]+)"/.exec(i)?.[1];return Y6(n,a,r,s)})}function m5(e,t,n=[]){return u5(p5(t,e,new Set,n))}function h5(e,t){return u5(`<pretext>\n${t.trim()?`${t.trim()}\n`:``}${e}\n</pretext>`)}function g5(e,t,n,r=[]){return h5(p5(t,e,new Set,r),n)}var _5=new Set([`part`,`chapter`]);function v5(e,t,n,r){return h5(l5.has(e)?t:_5.has(e)?`<book>\n<title>${r}</title>\n${t}\n</book>`:`<article>\n<title>${r}</title>\n${t}\n</article>`,n)}function y5(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 b5(e,t,n,r){let i=n===`book`?`book`:`article`;return e.map(e=>{if(e.sourceFormat===`markdown`){if(!e.title){let t=F8(e.content)?.title;if(t)return{...e,title:t}}return e}if(e.sourceFormat===`latex`){if(!e.title){let t=_8(e.content);if(t)return{...e,title:t}}return e}if(e.sourceFormat!==`pretext`)return e;let n=O8(e.content);if(e.xmlId===t&&!(n&&l5.has(n.type))){let{title:t,body:n}=y5(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})}function x5(e,t){let n=t??[],r=(e,t)=>n.find(n=>n.kind===e&&n.ref===t),i=[],a=new Set;for(let t of e??[])for(let{kind:e,ref:n}of q8(t.content)){let t=`${e}:${n}`;if(a.has(t))continue;a.add(t);let o=r(e,n);i.push({kind:e,ref:n,asset:o,inDocument:!0,status:o?`linked`:`unlinked`})}for(let e of n){if(!e.ref)continue;let t=`${e.kind}:${e.ref}`;a.has(t)||(a.add(t),i.push({kind:e.kind,ref:e.ref,asset:e,inDocument:!1,status:`unused`}))}return i}function S5(e,t){let n=`${e}-copy`;if(!t.has(n))return n;for(let e=2;;e++){let r=`${n}-${e}`;if(!t.has(r))return r}}var C5={image:`Images`,doenet:`Doenet`},w5=[`image`,`doenet`].filter(e=>e!==`doenet`||!1);function T5(e){return`${e}-${Date.now()}`}var E5=({open:e,onClose:r,resolveTarget:i,replaceTarget:a,onLoadAssets:o,onLoadLibraryAssets:s,onAddFromLibrary:c,onUpload:l,onFetchUrl:u,onCreateDoenet:d,onRemoveAsset:f,onDuplicateAsset:p,onAssetAdded:m,onResolveRef:h,onReplaceAsset:g})=>{let _=c6(e=>e.divisions),v=c6(e=>e.activeDivisionId),y=_?.find(e=>e.xmlId===v)?.sourceFormat??`pretext`,b=(e,t)=>X8(e,t,y),x=c6(e=>e.projectAssets)??[],S=c6(e=>e.setProjectAssets),C=c6(e=>e.libraryAssets),w=c6(e=>e.openAssetEditor),T=c6(e=>e.openAssetResolver),E=c6(e=>e.removeAssetRefFromDocument),D=!!(o||s),[O,k]=(0,n.useState)(null),[A,ee]=(0,n.useState)(!1),[te,j]=(0,n.useState)(null),M=x,ne=O??C??M,N=new Set(M.map(e=>e.id)),[re,ie]=(0,n.useState)(`in-document`),[ae,oe]=(0,n.useState)(null),[se,P]=(0,n.useState)(`library`),[ce,le]=(0,n.useState)(`library`),[ue,de]=(0,n.useState)(null),[F,fe]=(0,n.useState)(!1),[pe,me]=(0,n.useState)(!1),[he,ge]=(0,n.useState)(null),I=(0,n.useRef)(null),[_e,ve]=(0,n.useState)(``),[ye,be]=(0,n.useState)(``),[xe,Se]=(0,n.useState)(!1),[Ce,L]=(0,n.useState)(null),[we,Te]=(0,n.useState)(``),[Ee,R]=(0,n.useState)(``),[De,Oe]=(0,n.useState)(!1),[ke,z]=(0,n.useState)(null),[Ae,je]=(0,n.useState)(null),[Me,Ne]=(0,n.useState)(null),[Pe,Fe]=(0,n.useState)(null),Ie=(0,n.useCallback)(()=>{!o&&!s||(ee(!0),j(null),Promise.all([o?.()??Promise.resolve(null),s?.()??Promise.resolve(null)]).then(([e,t])=>{e!==null&&S(e),t!==null&&k(t)}).catch(e=>{j(e instanceof Error?e.message:`Failed to load assets.`)}).finally(()=>ee(!1)))},[o,s,S]),Le=(0,n.useRef)(Ie);if((0,n.useEffect)(()=>{Le.current=Ie}),(0,n.useEffect)(()=>{e&&Le.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 Re=x5(_,M),ze=(e,t)=>{if(a){g(a,e,!!t?.fromLibrary),r();return}if(m(e),i){e.ref&&h(i.kind,i.ref,e.ref),r();return}e.ref&&navigator.clipboard.writeText(b(e.kind,e.ref)).catch(()=>{}),Fe(e)},Be=(e,t)=>{let n=`${e}:${t}`;navigator.clipboard.writeText(b(e,t)).catch(()=>{}),je(n),setTimeout(()=>je(e=>e===n?null:e),2e3)},Ve=async e=>{if(!N.has(e.id)&&c){de(e.id);try{await c(e)}finally{de(null)}}ze(e,{fromLibrary:!0})},He=async e=>{if(l){ge(null),me(!0);try{ze(await l(e))}catch(e){ge(e instanceof Error?e.message:`Upload failed.`)}finally{me(!1)}}},Ue=async()=>{let e=_e.trim();if(!e)return;let t=ye.trim();if(u&&l){L(null),Se(!0);try{let n=await u(e);ze(await l(t?new File([n],t,{type:n.type}):n))}catch(e){L(e instanceof Error?e.message:`Failed to add URL.`)}finally{Se(!1)}}else ze({id:T5(`url`),name:t||e,ref:e.split(`/`).pop()??`image`,kind:`image`,url:e})},B=async()=>{let e=we.trim(),t=Ee.trim();if(!(!e||!t))if(d){z(null),Oe(!0);try{ze(await d(e,t))}catch(e){z(e instanceof Error?e.message:`Failed to create activity.`)}finally{Oe(!1)}}else ze({id:T5(`doenet`),name:e,ref:t,kind:`doenet`})},We=e=>{let n=ne.filter(t=>t.kind===e);return(0,t.jsxs)(`div`,{className:`pretext-plus-editor__am-library`,children:[D&&(0,t.jsxs)(`div`,{className:`pretext-plus-editor__am-toolbar`,children:[te&&(0,t.jsx)(`span`,{className:`pretext-plus-editor__asset-load-error`,children:te}),(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__asset-library-refresh`,onClick:Ie,disabled:A,children:A?`Loading…`:`Refresh`})]}),A&&n.length===0?(0,t.jsx)(`p`,{className:`pretext-plus-editor__am-empty-text`,children:`Loading…`}):n.length===0?(0,t.jsxs)(`p`,{className:`pretext-plus-editor__am-empty-text`,children:[`No `,C5[e].toLowerCase(),` assets in your library.`]}):(0,t.jsx)(`ul`,{className:`pretext-plus-editor__am-list`,children:n.map(e=>{let n=N.has(e.id),r=ue===e.id;return(0,t.jsx)(`li`,{children:(0,t.jsxs)(`button`,{type:`button`,className:[`pretext-plus-editor__am-lib-row`,n?`pretext-plus-editor__am-lib-row--in-project`:``].filter(Boolean).join(` `),onClick:()=>Ve(e),disabled:r||!e.ref,title:e.ref?n?`Use "${e.ref}"`:`Add to project & use "${e.ref}"`:`Asset has no reference — cannot use`,children:[(0,t.jsxs)(`div`,{className:`pretext-plus-editor__am-row-info`,children:[(0,t.jsx)(`span`,{className:`pretext-plus-editor__am-row-name`,children:e.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)})})]})},Ge=()=>{if(Re.length===0)return(0,t.jsxs)(`div`,{className:`pretext-plus-editor__am-placeholder`,children:[(0,t.jsx)(`p`,{children:`No assets in this project yet.`}),(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__dialog-button`,onClick:()=>{ie(`add`),oe(null)},children:`Add an asset`})]});let e=w5.map(e=>({kind:e,rows:Re.filter(t=>t.kind===e)})).filter(e=>e.rows.length>0),n=e=>{let n=`${e.kind}:${e.ref}`,i=Me===n,a=()=>{e.status===`unlinked`?T(e.kind,e.ref):(w(e.kind,e.ref),r())};return(0,t.jsxs)(`li`,{className:`pretext-plus-editor__am-doc-row`,children:[(0,t.jsxs)(`button`,{type:`button`,className:`pretext-plus-editor__am-row-info pretext-plus-editor__am-row-info--btn`,onClick:a,title:e.status===`unlinked`?`No asset for this reference — click to link or create one`:`Edit asset`,children:[(0,t.jsx)(`span`,{className:`pretext-plus-editor__am-row-name`,children:e.asset?.name??e.ref}),(0,t.jsx)(`span`,{className:`pretext-plus-editor__am-row-ref`,children:e.ref})]}),e.status===`unlinked`&&(0,t.jsx)(`span`,{className:`pretext-plus-editor__am-status pretext-plus-editor__am-status--warn`,title:`No asset for this reference`,children:`needs asset`}),e.status===`unused`&&(0,t.jsx)(`span`,{className:`pretext-plus-editor__am-status`,title:`Not referenced in the document yet`,children:`not placed`}),(0,t.jsxs)(`div`,{className:`pretext-plus-editor__am-row-actions`,children:[(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__am-action-btn`,onClick:a,children:e.status===`unlinked`?`Link / create`:`Edit`}),(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__am-action-btn${Ae===n?` pretext-plus-editor__am-action-btn--done`:``}`,onClick:()=>Be(e.kind,e.ref),title:`Copy ${b(e.kind,e.ref)}`,children:Ae===n?`Copied!`:`Copy embed code`}),p&&e.asset?.url&&(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__am-action-btn`,disabled:i,onClick:async()=>{Ne(n);try{await p(e.asset),r()}catch{Ne(null)}},title:`Create a copy of this asset under a new reference`,children:i?`Duplicating…`:`Duplicate`}),f&&e.asset&&(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__am-action-btn pretext-plus-editor__am-action-btn--danger`,onClick:()=>{e.inDocument&&!window.confirm(`Remove "${e.asset.name}" from the project? This also deletes its reference(s) from the document.`)||(f(e.asset),E(e.kind,e.ref),r())},title:`Remove from project`,children:`Remove`})]})]},n)};return(0,t.jsx)(`div`,{className:`pretext-plus-editor__am-in-doc`,children:e.map(({kind:e,rows:r})=>(0,t.jsxs)(`div`,{className:`pretext-plus-editor__am-kind-group`,children:[(0,t.jsxs)(`div`,{className:`pretext-plus-editor__am-kind-header`,children:[(0,t.jsx)(`span`,{"aria-hidden":`true`,children:`📁`}),(0,t.jsx)(`span`,{children:C5[e]}),(0,t.jsx)(`span`,{className:`pretext-plus-editor__am-kind-count`,children:r.length})]}),(0,t.jsx)(`ul`,{className:`pretext-plus-editor__am-list`,children:r.map(n)})]},e))})},Ke=e=>(0,t.jsxs)(`div`,{className:`pretext-plus-editor__am-success`,children:[(0,t.jsxs)(`p`,{className:`pretext-plus-editor__am-success-title`,children:[`✓ Added “`,e.name,`” — embed code copied to clipboard`]}),(0,t.jsx)(`p`,{className:`pretext-plus-editor__dialog-helper-copy`,children:`It now appears in the Assets list. Paste this where you want it to appear:`}),e.ref&&(0,t.jsxs)(`div`,{className:`pretext-plus-editor__am-embed-row`,children:[(0,t.jsx)(`code`,{className:`pretext-plus-editor__am-embed-code`,children:b(e.kind,e.ref)}),(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__am-action-btn${Ae===`${e.kind}:${e.ref}`?` pretext-plus-editor__am-action-btn--done`:``}`,onClick:()=>Be(e.kind,e.ref),children:Ae===`${e.kind}:${e.ref}`?`Copied!`:`Copy again`})]}),(0,t.jsxs)(`div`,{className:`pretext-plus-editor__am-success-actions`,children:[(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__dialog-button pretext-plus-editor__dialog-button--secondary`,onClick:()=>{Fe(null),oe(null),ie(`add`)},children:`Add another`}),(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__dialog-button`,onClick:()=>{Fe(null),ie(`in-document`)},children:`Done`})]})]}),V=()=>(0,t.jsxs)(`div`,{className:`pretext-plus-editor__am-kind-picker`,children:[(0,t.jsx)(`p`,{className:`pretext-plus-editor__am-kind-picker-label`,children:`What kind of asset?`}),(0,t.jsxs)(`div`,{className:`pretext-plus-editor__am-kind-cards`,children:[(0,t.jsxs)(`button`,{type:`button`,className:`pretext-plus-editor__am-kind-card`,onClick:()=>{oe(`image`),P(`library`)},children:[(0,t.jsx)(`span`,{className:`pretext-plus-editor__am-kind-card-icon`,"aria-hidden":`true`,children:`🖼️`}),(0,t.jsx)(`span`,{className:`pretext-plus-editor__am-kind-card-label`,children:`Image`}),(0,t.jsx)(`span`,{className:`pretext-plus-editor__am-kind-card-hint`,children:`PNG, JPEG, SVG, etc.`})]}),!1,(0,t.jsxs)(`div`,{className:`pretext-plus-editor__am-kind-card pretext-plus-editor__am-kind-card--soon`,"aria-disabled":`true`,children:[(0,t.jsx)(`span`,{className:`pretext-plus-editor__am-kind-card-icon`,"aria-hidden":`true`,children:`✨`}),(0,t.jsx)(`span`,{className:`pretext-plus-editor__am-kind-card-label`,children:`More coming soon`}),(0,t.jsx)(`span`,{className:`pretext-plus-editor__am-kind-card-hint`,children:`Interactive activities and more`})]})]})]}),qe=e=>(0,t.jsxs)(`div`,{className:`pretext-plus-editor__am-configure`,children:[e&&(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__am-back-btn`,onClick:()=>oe(null),children:`← Back`}),(0,t.jsxs)(`div`,{className:`pretext-plus-editor__dialog-tab-bar pretext-plus-editor__am-sub-tabs`,children:[(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__dialog-tab${se===`library`?` pretext-plus-editor__dialog-tab--active`:``}`,onClick:()=>P(`library`),children:`Library`}),l&&(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__dialog-tab${se===`upload`?` pretext-plus-editor__dialog-tab--active`:``}`,onClick:()=>P(`upload`),children:`Upload`}),(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__dialog-tab${se===`url`?` pretext-plus-editor__dialog-tab--active`:``}`,onClick:()=>P(`url`),children:`External URL`})]}),(0,t.jsxs)(`div`,{className:`pretext-plus-editor__am-configure-body`,children:[se===`library`&&We(`image`),se===`upload`&&l&&(0,t.jsxs)(`div`,{className:`pretext-plus-editor__am-upload`,children:[(0,t.jsxs)(`div`,{className:[`pretext-plus-editor__am-drop-zone`,F?`pretext-plus-editor__am-drop-zone--active`:``,pe?`pretext-plus-editor__am-drop-zone--uploading`:``].filter(Boolean).join(` `),onDragOver:e=>{e.preventDefault(),fe(!0)},onDragLeave:()=>fe(!1),onDrop:e=>{e.preventDefault(),fe(!1);let t=e.dataTransfer.files[0];t&&He(t)},onClick:()=>!pe&&I.current?.click(),role:`button`,tabIndex:0,onKeyDown:e=>{(e.key===`Enter`||e.key===` `)&&(e.preventDefault(),I.current?.click())},"aria-label":`Upload image — click or drag and drop`,children:[(0,t.jsx)(`input`,{ref:I,type:`file`,accept:`image/*`,className:`pretext-plus-editor__dialog-file-input`,onChange:e=>{let t=e.target.files?.[0];t&&He(t)}}),pe?(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`})]})]}),he&&(0,t.jsx)(`p`,{className:`pretext-plus-editor__am-error`,children:he})]}),se===`url`&&(0,t.jsxs)(`div`,{className:`pretext-plus-editor__am-url-form`,children:[(0,t.jsx)(`label`,{className:`pretext-plus-editor__dialog-label`,htmlFor:`am-url-value`,children:`Image URL`}),(0,t.jsx)(`input`,{id:`am-url-value`,type:`url`,className:`pretext-plus-editor__am-input`,placeholder:`https://example.com/image.png`,value:_e,onChange:e=>ve(e.target.value),disabled:xe,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:ye,onChange:e=>be(e.target.value),disabled:xe}),Ce&&(0,t.jsx)(`p`,{className:`pretext-plus-editor__am-error`,children:Ce}),_e.trim()&&(0,t.jsx)(`img`,{src:_e.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:Ue,disabled:!_e.trim()||xe,children:xe?`Adding…`:u&&l?`Add to Project`:`Add`})]})]})]}),Je=e=>(0,t.jsxs)(`div`,{className:`pretext-plus-editor__am-configure`,children:[e&&(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__am-back-btn`,onClick:()=>oe(null),children:`← Back`}),(0,t.jsxs)(`div`,{className:`pretext-plus-editor__dialog-tab-bar pretext-plus-editor__am-sub-tabs`,children:[(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__dialog-tab${ce===`library`?` pretext-plus-editor__dialog-tab--active`:``}`,onClick:()=>le(`library`),children:`Library`}),(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__dialog-tab${ce===`create`?` pretext-plus-editor__dialog-tab--active`:``}`,onClick:()=>le(`create`),children:`Create New`})]}),(0,t.jsxs)(`div`,{className:`pretext-plus-editor__am-configure-body`,children:[ce===`library`&&We(`doenet`),ce===`create`&&(0,t.jsxs)(`div`,{className:`pretext-plus-editor__am-create-form`,children:[(0,t.jsx)(`label`,{className:`pretext-plus-editor__dialog-label`,htmlFor:`am-doenet-name`,children:`Name`}),(0,t.jsx)(`input`,{id:`am-doenet-name`,type:`text`,className:`pretext-plus-editor__am-input`,placeholder:`My Activity`,value:we,onChange:e=>Te(e.target.value),disabled:De,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:Ee,onChange:e=>R(e.target.value),disabled:De}),(0,t.jsxs)(`p`,{className:`pretext-plus-editor__dialog-helper-copy`,children:[`The reference ID is used in the embed code: `,(0,t.jsx)(`code`,{children:b(`doenet`,Ee||`my-activity`)})]}),ke&&(0,t.jsx)(`p`,{className:`pretext-plus-editor__am-error`,children:ke}),(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__dialog-button`,onClick:B,disabled:!we.trim()||!Ee.trim()||De,children:De?`Creating…`:d?`Create`:`Add`})]})]})]}),Ye=(e,n,i)=>(0,t.jsxs)(t.Fragment,{children:[(0,t.jsxs)(`div`,{className:`pretext-plus-editor__dialog-header`,children:[(0,t.jsx)(`h2`,{className:`pretext-plus-editor__dialog-title`,children:n}),(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__dialog-close`,onClick:r,"aria-label":`Close`,children:`✕`})]}),(0,t.jsxs)(`div`,{className:`pretext-plus-editor__dialog-content pretext-plus-editor__dialog-content--single`,children:[(0,t.jsx)(`p`,{className:`pretext-plus-editor__dialog-helper-copy pretext-plus-editor__am-resolve-hint`,children:i}),e===`doenet`?Je(!1):qe(!1)]}),(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:`Cancel`})})]});return(0,t.jsx)(`div`,{className:`pretext-plus-editor__dialog-overlay`,onClick:e=>e.target===e.currentTarget&&r(),children:(0,t.jsx)(`div`,{className:`pretext-plus-editor__dialog pretext-plus-editor__dialog--asset-manager`,role:`dialog`,"aria-modal":`true`,"aria-label":`Asset manager`,children:i?(e=>Ye(e.kind,`Link asset`,(0,t.jsxs)(t.Fragment,{children:[`The reference `,(0,t.jsx)(`code`,{children:e.ref}),` has no asset yet. Choose or create one — the reference in your document will be updated to match.`]})))(i):a?(e=>Ye(e.kind,`Replace asset`,(0,t.jsxs)(t.Fragment,{children:[`Choose or upload a new asset to replace `,(0,t.jsx)(`code`,{children:e.name}),`. Every place it’s used in your document will show the new asset.`]})))(a):(0,t.jsxs)(t.Fragment,{children:[(0,t.jsxs)(`div`,{className:`pretext-plus-editor__dialog-header`,children:[(0,t.jsx)(`h2`,{className:`pretext-plus-editor__dialog-title`,children:`Assets`}),(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__dialog-close`,onClick:r,"aria-label":`Close`,children:`✕`})]}),Pe?(0,t.jsx)(`div`,{className:`pretext-plus-editor__dialog-content pretext-plus-editor__dialog-content--single`,children:Ke(Pe)}):(0,t.jsxs)(t.Fragment,{children:[(0,t.jsxs)(`div`,{className:`pretext-plus-editor__dialog-tab-bar`,children:[(0,t.jsxs)(`button`,{type:`button`,className:`pretext-plus-editor__dialog-tab${re===`in-document`?` pretext-plus-editor__dialog-tab--active`:``}`,onClick:()=>ie(`in-document`),children:[`Assets`,Re.length>0&&(0,t.jsx)(`span`,{className:`pretext-plus-editor__asset-tab-count`,children:Re.length})]}),(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__dialog-tab${re===`add`?` pretext-plus-editor__dialog-tab--active`:``}`,onClick:()=>{ie(`add`),oe(null)},children:`Add Asset`})]}),(0,t.jsx)(`div`,{className:`pretext-plus-editor__dialog-content pretext-plus-editor__dialog-content--single`,children:re===`in-document`?Ge():ae===null?V():ae===`image`?qe(!0):Je(!0)})]}),(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`})})]})})})},D5={automaticLayout:!0,minimap:{enabled:!1},wordWrap:`on`,insertSpaces:!0,tabSize:2,padding:{top:10,bottom:10},scrollBeyondLastLine:!1},O5=160,k5={image:`image`,doenet:`interactive`},A5=({asset:e,projectAssets:r,onClose:i,onSave:a,onReplace:o,onDuplicate:s})=>{let c=c6(e=>e.divisions),l=c6(e=>e.activeDivisionId),u=c?.find(e=>e.xmlId===l)?.sourceFormat??`pretext`,d=e.ref??``,[f,p]=(0,n.useState)(e.name),[m,h]=(0,n.useState)(d),[g,_]=(0,n.useState)(e.source??``),[v,y]=(0,n.useState)(!1),[b,x]=(0,n.useState)(!1),[S,C]=(0,n.useState)(null),[w,T]=(0,n.useState)(!1),[E,D]=(0,n.useState)(O5),O=(0,n.useRef)(null);(0,n.useEffect)(()=>{O.current?.scrollTo({top:0})},[]);let k=e=>{let t=()=>D(Math.max(O5,e.getContentHeight()));e.onDidContentSizeChange(t),t()},A=X8(e.kind,m.trim()||d,u),ee=()=>{navigator.clipboard.writeText(A).catch(()=>{}),T(!0),setTimeout(()=>T(!1),2e3)},te=async()=>{let t=m.trim();if(!t){C(`Reference can't be empty — it identifies the asset and is used by every embed of it.`);return}if(t!==d&&r.some(n=>n.kind===e.kind&&n.ref===t)){C(`Reference "${t}" is already used by another asset. Choose a unique reference.`);return}C(null),y(!0);try{await a({...e,name:f.trim()||t,ref:t,source:g},d),i()}catch(e){C(e instanceof Error?e.message:`Failed to save asset.`),y(!1)}},j=async()=>{if(s){C(null),x(!0);try{await s(e)}catch(e){C(e instanceof Error?e.message:`Failed to duplicate asset.`),x(!1)}}},M=v||b,ne=e.kind===`image`&&!!e.url,N=e.kind===`image`&&!!o;return(0,t.jsx)(`div`,{className:`pretext-plus-editor__dialog-overlay`,onClick:e=>e.target===e.currentTarget&&i(),children:(0,t.jsxs)(`div`,{className:`pretext-plus-editor__dialog`,role:`dialog`,"aria-modal":`true`,"aria-label":`Edit asset ${e.name}`,children:[(0,t.jsxs)(`div`,{className:`pretext-plus-editor__dialog-header`,children:[(0,t.jsx)(`h2`,{className:`pretext-plus-editor__dialog-title`,children:`Edit asset`}),(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__dialog-close`,onClick:i,"aria-label":`Close`,children:`✕`})]}),(0,t.jsx)(`div`,{ref:O,className:`pretext-plus-editor__dialog-content pretext-plus-editor__dialog-content--single pretext-plus-editor__am-edit-content`,children:(0,t.jsxs)(`div`,{className:`pretext-plus-editor__dialog-section`,children:[(0,t.jsxs)(`div`,{className:`pretext-plus-editor__am-edit-grid`,children:[(0,t.jsxs)(`div`,{className:`pretext-plus-editor__am-edit-col`,children:[ne&&(0,t.jsx)(`img`,{src:e.url,alt:f,className:`pretext-plus-editor__am-url-preview`,onError:e=>{e.currentTarget.style.display=`none`}}),N&&(0,t.jsx)(`div`,{className:`pretext-plus-editor__am-replace-row`,children:(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__am-action-btn`,onClick:()=>o?.(e),disabled:M,title:`Choose or upload a different asset to use here`,children:`Replace image…`})})]}),(0,t.jsxs)(`div`,{className:`pretext-plus-editor__am-edit-col`,children:[(0,t.jsx)(`label`,{className:`pretext-plus-editor__dialog-label`,htmlFor:`am-edit-name`,children:`Name`}),(0,t.jsx)(`input`,{id:`am-edit-name`,type:`text`,className:`pretext-plus-editor__am-input`,value:f,onChange:e=>p(e.target.value),disabled:M}),(0,t.jsx)(`label`,{className:`pretext-plus-editor__dialog-label`,htmlFor:`am-edit-ref`,children:`Reference`}),(0,t.jsx)(`input`,{id:`am-edit-ref`,type:`text`,className:`pretext-plus-editor__am-input`,value:m,onChange:e=>h(e.target.value),disabled:M}),(0,t.jsx)(`p`,{className:`pretext-plus-editor__dialog-helper-copy`,children:`Used in the embed code. Changing it updates every reference to this asset already in your document.`}),(0,t.jsxs)(`div`,{className:`pretext-plus-editor__am-embed-row`,children:[(0,t.jsx)(`code`,{className:`pretext-plus-editor__am-embed-code`,children:A}),(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__am-action-btn${w?` pretext-plus-editor__am-action-btn--done`:``}`,onClick:ee,title:`Copy embed code to clipboard`,children:w?`Copied!`:`Copy embed code`})]})]})]}),(0,t.jsx)(`label`,{className:`pretext-plus-editor__dialog-label`,children:`Asset content`}),(0,t.jsxs)(`p`,{className:`pretext-plus-editor__dialog-helper-copy`,children:[`Inserted verbatim inside the generated `,(0,t.jsx)(`code`,{children:`<${k5[e.kind]}>`}),` element — e.g. `,(0,t.jsx)(`code`,{children:`<shortdescription>...</shortdescription>`}),`.`]}),(0,t.jsx)(`div`,{className:`pretext-plus-editor__dialog-editor pretext-plus-editor__am-edit-editor`,style:{height:E},children:(0,t.jsx)(Yn,{options:{...D5,readOnly:M},height:`100%`,language:`xml`,value:g,onMount:k,onChange:e=>_(e??``)})}),S&&(0,t.jsx)(`p`,{className:`pretext-plus-editor__am-error`,children:S})]})}),(0,t.jsxs)(`div`,{className:`pretext-plus-editor__dialog-actions`,children:[s&&(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__dialog-button pretext-plus-editor__dialog-button--secondary pretext-plus-editor__am-duplicate-btn`,onClick:j,disabled:M,title:`Create a copy of this asset under a new reference`,children:b?`Duplicating…`:`Duplicate`}),(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__dialog-button pretext-plus-editor__dialog-button--secondary`,onClick:i,disabled:M,children:`Cancel`}),(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__dialog-button`,onClick:te,disabled:M,children:v?`Saving…`:`Save`})]})]})})},j5=e=>{let n=c6(e=>e.showFullPreview),r=c6(e=>e.setShowFullPreview),i=c6(e=>e.title),a=c6(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)(l6,{label:`Give feedback`,context:`main-editor`}),o]})]})},M5={pretext:`PreTeXt`,latex:`LaTeX`,markdown:`Markdown`},N5={book:`bk`,article:`art`,slideshow:`slides`,part:`pt`,chapter:`ch`,section:`sec`,subsection:`subsec`,subsubsection:`subsubsec`,introduction:`intro`,conclusion:`conc`,worksheet:`ws`,handout:`ho`,exercises:`ex`,references:`ref`,glossary:`gloss`,solutions:`sol`,"reading-questions":`rq`,paragraphs:`para`},P5={book:`Book`,article:`Article`,slideshow:`Slideshow`,part:`Part`,chapter:`Chapter`,section:`Section`,worksheet:`Worksheet`,handout:`Handout`,exercises:`Exercises`,references:`References`,glossary:`Glossary`,solutions:`Solutions`,"reading-questions":`Reading Questions`,introduction:`Introduction`,conclusion:`Conclusion`,subsection:`Subsection`,subsubsection:`Subsubsection`,paragraphs:`Paragraphs`},F5=[`part`,`chapter`,`section`,`subsection`,`subsubsection`,`worksheet`,`handout`,`exercises`,`references`,`glossary`,`solutions`,`reading-questions`,`paragraphs`],I5=[`worksheet`,`handout`,`exercises`,`references`,`glossary`,`solutions`,`reading-questions`,`paragraphs`],L5={book:[`part`,`chapter`],article:[`section`,...I5],slideshow:[`section`],part:[`chapter`],chapter:[`section`,...I5],section:[`subsection`,...I5],subsection:[`subsubsection`,...I5],subsubsection:[...I5],worksheet:[`paragraphs`],handout:[`paragraphs`]};function R5(e){return e?L5[e]??F5:F5}function z5(e,t){let n=N5[e]??`sec`,r=D8(t);return r?`${n}-${r}`:n}var B5=({draft:e,isNew:r=!1,isRoot:i=!1,parentType:a=null,onDraftChange:o,onCommit:s,onCancel:c})=>{let l=R5(a),u=(0,n.useRef)(r);return(0,n.useEffect)(()=>{i||l.length>0&&!l.includes(e.type)&&o({...e,type:l[0]})},[e,i,o,l]),(0,t.jsxs)(`div`,{className:`pretext-plus-editor__toc-edit-form`,children:[(0,t.jsxs)(`label`,{className:`pretext-plus-editor__toc-edit-field`,children:[(0,t.jsx)(`span`,{children:`Title`}),(0,t.jsx)(`input`,{type:`text`,value:e.title,onChange:t=>{let n=t.target.value;o(u.current?{...e,title:n,xmlId:z5(e.type,n)}:{...e,title:n})},onKeyDown:e=>{e.key===`Enter`&&s(),e.key===`Escape`&&c()},autoFocus:!0})]}),r?(0,t.jsxs)(`label`,{className:`pretext-plus-editor__toc-edit-field`,children:[(0,t.jsx)(`span`,{children:`Format`}),(0,t.jsx)(`select`,{value:e.sourceFormat,onChange:t=>o({...e,sourceFormat:t.target.value}),children:Object.keys(M5).map(e=>(0,t.jsx)(`option`,{value:e,children:M5[e]},e))})]}):void 0,!i&&(0,t.jsxs)(`label`,{className:`pretext-plus-editor__toc-edit-field`,children:[(0,t.jsx)(`span`,{children:`Type`}),(0,t.jsx)(`select`,{value:e.type,onChange:t=>{let n=t.target.value;o(u.current?{...e,type:n,xmlId:z5(n,e.title)}:{...e,type:n})},children:l.map(e=>(0,t.jsx)(`option`,{value:e,children:P5[e]},e))})]}),(0,t.jsxs)(`label`,{className:`pretext-plus-editor__toc-edit-field`,children:[(0,t.jsx)(`span`,{children:`Id`}),(0,t.jsx)(`input`,{type:`text`,value:e.xmlId,placeholder:`unique identifier`,onChange:t=>{u.current=!1,o({...e,xmlId:t.target.value})}})]}),(0,t.jsxs)(`div`,{className:`pretext-plus-editor__toc-edit-actions`,children:[(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__toc-edit-save`,onClick:s,children:`Save`}),(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__toc-edit-cancel`,onClick:c,children:`Cancel`})]})]})},V5=({items:e})=>{let[i,a]=(0,n.useState)(!1),[o,s]=(0,n.useState)(null),c=(0,n.useRef)(null),l=(0,n.useRef)(null);(0,n.useEffect)(()=>{if(!i)return;let e=e=>{let t=e.target;c.current&&!c.current.contains(t)&&l.current&&!l.current.contains(t)&&a(!1)},t=e=>{e.key===`Escape`&&a(!1)},n=()=>a(!1);return document.addEventListener(`mousedown`,e),document.addEventListener(`keydown`,t),window.addEventListener(`scroll`,n,!0),window.addEventListener(`resize`,n),()=>{document.removeEventListener(`mousedown`,e),document.removeEventListener(`keydown`,t),window.removeEventListener(`scroll`,n,!0),window.removeEventListener(`resize`,n)}},[i]),(0,n.useLayoutEffect)(()=>{if(!i)return;let e=c.current,t=l.current;if(!e||!t)return;let n=e.getBoundingClientRect(),r=t.getBoundingClientRect(),a=window.innerHeight-n.bottom,o=r.height+4>a&&n.top>a;s({right:Math.max(4,window.innerWidth-n.right),...o?{bottom:window.innerHeight-n.top+3}:{top:n.bottom+3}})},[i]);let u=e=>{a(!1),e()};return(0,t.jsxs)(`div`,{ref:c,className:`pretext-plus-editor__toc-div-menu`,children:[(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__toc-div-menu-trigger`,onClick:e=>{e.stopPropagation(),a(e=>(e||s(null),!e))},"aria-haspopup":`true`,"aria-expanded":i,title:`More options`,children:`⋮`}),i&&(0,r.createPortal)((0,t.jsx)(`div`,{ref:l,className:`pretext-plus-editor__toc-div-menu-popup pretext-plus-editor__toc-div-menu-popup--portal`,style:{visibility:o?`visible`:`hidden`,right:o?.right,top:o?.top,bottom:o?.bottom},children:e.map(e=>(0,t.jsx)(`button`,{type:`button`,className:[`pretext-plus-editor__toc-div-menu-item`,e.danger?`pretext-plus-editor__toc-div-menu-item--danger`:``].filter(Boolean).join(` `),onClick:()=>u(e.onClick),children:e.label},e.label))}),document.body)]})},H5=({division:e,depth:n,isActive:r,hasChildren:i,isExpanded:a,onToggleExpand:o,editDraft:s,onSelect:c,onDraftChange:l,onEditCommit:u,onEditCancel:d,menuItems:f,isNew:p=!1,isRoot:m=!1,parentType:h=null})=>{let g=s!==null,_=e.type===`introduction`||e.type===`conclusion`?P5[e.type]:null;return(0,t.jsxs)(`li`,{className:[`pretext-plus-editor__toc-item`,`pretext-plus-editor__toc-item--${e.type}`,r?`pretext-plus-editor__toc-item--active`:``,g?`pretext-plus-editor__toc-item--editing`:``].filter(Boolean).join(` `),children:[(0,t.jsxs)(`div`,{className:`pretext-plus-editor__toc-item-row`,style:n>0?{paddingLeft:`${n*14}px`}:void 0,children:[(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__toc-expand-btn`,onClick:o,"aria-label":a?`Collapse`:`Expand`,tabIndex:i?0:-1,style:{visibility:i?`visible`:`hidden`},children:a?`▾`:`▸`}),(0,t.jsxs)(`button`,{type:`button`,className:`pretext-plus-editor__toc-select`,onClick:c,"aria-current":r?`true`:void 0,title:P5[e.type]??e.type,children:[(0,t.jsx)(`span`,{className:`pretext-plus-editor__toc-title`,children:e.title||_||(0,t.jsx)(`em`,{children:`Untitled`})}),e.xmlId&&(0,t.jsx)(`span`,{className:`pretext-plus-editor__toc-xmlid`,children:e.xmlId})]}),(0,t.jsx)(`div`,{className:`pretext-plus-editor__toc-actions`,children:(0,t.jsx)(V5,{items:f})})]}),g&&s&&(0,t.jsx)(B5,{draft:s,isNew:p,isRoot:m,parentType:h,onDraftChange:l,onCommit:u,onCancel:d})]})},U5=({onOpenAssetPicker:e})=>{let r=c6(e=>e.divisions),i=c6(e=>e.rootDivisionId),a=c6(e=>e.activeDivisionId),o=c6(e=>e.projectAssets)??[],s=c6(e=>e.selectSection),c=c6(e=>e.addSection),l=c6(e=>e.removeSection),u=c6(e=>e.divisionContentChange),d=c6(e=>e.insertAtCursor),f=c6(e=>e.openAssetEditor),p=c6(e=>e.openAssetResolver),m=c6(e=>e.removeAsset),h=c6(e=>e.removeAssetRefFromDocument),g=c6(e=>e.duplicateAsset),_=c6(e=>e.hasAssetDuplicate),v=c6(e=>e.startSectionEdit),y=c6(e=>e.setEditDraft),b=c6(e=>e.commitSectionEdit),x=c6(e=>e.cancelSectionEdit),S=c6(e=>e.editingId),C=c6(e=>e.editDraft),w=c6(e=>e.editingIsNew),T=r?r.find(e=>e.xmlId===i)??r.find(e=>e.type===`book`||e.type===`article`||e.type===`slideshow`)??r[0]??null:null,E=T&&r?s5(r,T.xmlId):[],D=T&&r?c5(r,T.xmlId):[],O=x5(r,o),k=w5.map(e=>({kind:e,rows:O.filter(t=>t.kind===e)})).filter(e=>e.rows.length>0),[A,ee]=(0,n.useState)(!1),[te,j]=(0,n.useState)(null),M=async e=>{if(!(!e.asset||te)){j(e.ref);try{await g(e.asset)}finally{j(null)}}},[ne,N]=(0,n.useState)(new Set),re=e=>!ne.has(e),ie=e=>{N(t=>{let n=new Set(t);return n.has(e)?n.delete(e):n.add(e),n})},[ae,oe]=(0,n.useState)(a);if(a!==ae&&(oe(a),a&&T)){let e=new Map(E.map(e=>[e.division.xmlId,e])),t=new Set;t.add(T.xmlId);let n=a;for(;n;){let r=e.get(n);if(!r?.parentXmlId)break;t.add(r.parentXmlId),n=r.parentXmlId}N(e=>{if([...t].every(t=>!e.has(t)))return e;let n=new Set(e);return t.forEach(e=>n.delete(e)),n})}let se=new Set(E.map(e=>e.parentXmlId).filter(Boolean)),P=[];if(T){let e=new Set;re(T.xmlId)&&e.add(T.xmlId);for(let t of E)t.parentXmlId&&e.has(t.parentXmlId)&&(P.push(t),re(t.division.xmlId)&&e.add(t.division.xmlId))}let ce=(e,t)=>{if(!r)return;let n=r.find(e=>e.xmlId===t);n&&u(n.xmlId,e5(n.content,e))},le=(e,t)=>{if(window.confirm(`Delete "${e.title||`Untitled`}"? This permanently removes the division.`)){if(t&&r){let n=r.find(e=>e.xmlId===t);n&&u(n.xmlId,e5(n.content,e.xmlId))}l(e.xmlId)}},ue=r?.find(e=>e.xmlId===a)?.sourceFormat??`pretext`,de=e=>{d(ue===`markdown`?`::${e.type}{ref="${e.xmlId}"}`:ue===`latex`?`\\plus{${e.type}}{${e.xmlId}}`:`<plus:${e.type} ref="${e.xmlId}"/>`)},F=e=>{T&&u(T.xmlId,$8(T.content,e.xmlId,e.type,null,T.sourceFormat))},fe=e=>e&&r?.find(t=>t.xmlId===e)?.type||null,pe=e=>e.status===`unlinked`?p(e.kind,e.ref):f(e.kind,e.ref),me=(e,t)=>{navigator.clipboard.writeText(X8(e,t,ue)).catch(()=>{})},he=e=>{let t=[{label:e.status===`unlinked`?`Link / create asset`:`Edit asset`,onClick:()=>pe(e)},{label:`Copy embed code`,onClick:()=>me(e.kind,e.ref)}];return _&&e.asset&&t.push({label:`Duplicate asset`,onClick:()=>M(e)}),e.status===`unlinked`?t.push({label:`Remove from document`,onClick:()=>h(e.kind,e.ref),danger:!0}):e.asset&&t.push({label:`Remove from project`,onClick:()=>{e.inDocument&&!window.confirm(`Remove "${e.asset.name}" from the project? This also deletes its ${e.inDocument?`reference(s)`:`reference`} from the document.`)||(m(e.asset),h(e.kind,e.ref))},danger:!0}),t};return(0,t.jsxs)(t.Fragment,{children:[(0,t.jsxs)(`ul`,{className:`pretext-plus-editor__toc-list`,role:`list`,children:[T&&(0,t.jsx)(H5,{division:T,depth:0,isActive:a===T.xmlId,hasChildren:se.has(T.xmlId),isExpanded:re(T.xmlId),onToggleExpand:()=>ie(T.xmlId),editDraft:S===T.xmlId?C:null,onSelect:()=>s(T.xmlId),onDraftChange:y,onEditCommit:b,onEditCancel:x,menuItems:[{label:`Edit properties`,onClick:()=>v(T)},...R8(T.sourceFormat)?[{label:`Add new division`,onClick:()=>c(T.xmlId)}]:[]],isNew:S===T.xmlId&&w,isRoot:!0}),P.length===0&&T&&re(T.xmlId)&&(0,t.jsx)(`li`,{className:`pretext-plus-editor__toc-no-sections`,children:(0,t.jsx)(`span`,{children:`No placed divisions`})}),P.map(e=>(0,t.jsx)(H5,{division:e.division,depth:e.depth+1,isActive:a===e.division.xmlId,hasChildren:se.has(e.division.xmlId),isExpanded:re(e.division.xmlId),onToggleExpand:()=>ie(e.division.xmlId),editDraft:S===e.division.xmlId?C:null,onSelect:()=>s(e.division.xmlId),onDraftChange:y,onEditCommit:b,onEditCancel:x,menuItems:[{label:`Edit properties`,onClick:()=>v(e.division)},...R8(e.division.sourceFormat)?[{label:`Add new division`,onClick:()=>c(e.division.xmlId)}]:[],{label:`Remove from document`,onClick:()=>ce(e.division.xmlId,e.parentXmlId)},{label:`Delete from project`,onClick:()=>le(e.division,e.parentXmlId),danger:!0}],isNew:S===e.division.xmlId&&w,parentType:fe(e.parentXmlId)},e.division.xmlId))]}),D.length>0&&(0,t.jsxs)(`div`,{className:`pretext-plus-editor__toc-orphans`,children:[(0,t.jsx)(`div`,{className:`pretext-plus-editor__toc-orphans-heading`,children:`Unplaced divisions`}),(0,t.jsx)(`ul`,{className:`pretext-plus-editor__toc-list`,children:D.map(e=>{let i=r?s5(r,e.xmlId):[],o=new Set(i.map(e=>e.parentXmlId).filter(Boolean));return(0,t.jsxs)(n.Fragment,{children:[(0,t.jsx)(H5,{division:e,depth:0,isActive:a===e.xmlId,hasChildren:o.has(e.xmlId),isExpanded:re(e.xmlId),onToggleExpand:()=>ie(e.xmlId),editDraft:S===e.xmlId?C:null,onSelect:()=>s(e.xmlId),onDraftChange:y,onEditCommit:b,onEditCancel:x,menuItems:[{label:`Edit properties`,onClick:()=>v(e)},{label:`Place in document`,onClick:()=>F(e)},{label:`Insert at cursor`,onClick:()=>de(e)},{label:`Delete from project`,onClick:()=>le(e,null),danger:!0}],parentType:null}),re(e.xmlId)&&i.map(e=>(0,t.jsx)(H5,{division:e.division,depth:e.depth+1,isActive:a===e.division.xmlId,hasChildren:o.has(e.division.xmlId),isExpanded:re(e.division.xmlId),onToggleExpand:()=>ie(e.division.xmlId),editDraft:S===e.division.xmlId?C:null,onSelect:()=>s(e.division.xmlId),onDraftChange:y,onEditCommit:b,onEditCancel:x,menuItems:[{label:`Edit properties`,onClick:()=>v(e.division)},{label:`Insert at cursor`,onClick:()=>de(e.division)},{label:`Delete from project`,onClick:()=>le(e.division,e.parentXmlId),danger:!0}],parentType:fe(e.parentXmlId)},e.division.xmlId))]},e.xmlId)})})]}),(0,t.jsxs)(`div`,{className:`pretext-plus-editor__toc-assets`,children:[(0,t.jsx)(`div`,{className:`pretext-plus-editor__toc-assets-header`,children:(0,t.jsxs)(`button`,{type:`button`,className:`pretext-plus-editor__toc-assets-toggle`,onClick:()=>ee(e=>!e),"aria-expanded":A,children:[(0,t.jsx)(`span`,{className:`pretext-plus-editor__toc-assets-chevron`,children:A?`▾`:`▸`}),(0,t.jsx)(`span`,{children:`Assets`}),O.length>0&&(0,t.jsx)(`span`,{className:`pretext-plus-editor__toc-assets-count`,children:O.length})]})}),A&&(0,t.jsx)(`div`,{className:`pretext-plus-editor__toc-assets-body`,children:O.length===0?(0,t.jsxs)(`p`,{className:`pretext-plus-editor__toc-assets-empty`,children:[`No assets in this project yet.`,` `,e&&(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__toc-assets-add-link`,onClick:e,children:`Add one`})]}):(0,t.jsx)(`div`,{className:`pretext-plus-editor__toc-assets-groups`,children:k.map(({kind:e,rows:n})=>(0,t.jsxs)(`div`,{children:[(0,t.jsx)(`div`,{className:`pretext-plus-editor__toc-assets-group-header`,children:C5[e]}),(0,t.jsx)(`ul`,{className:`pretext-plus-editor__toc-assets-list`,children:n.map(e=>(0,t.jsxs)(`li`,{className:[`pretext-plus-editor__toc-asset-item`,e.status===`unlinked`?`pretext-plus-editor__toc-asset-item--unlinked`:``,e.status===`unused`?`pretext-plus-editor__toc-asset-item--unused`:``,te===e.ref?`pretext-plus-editor__toc-asset-item--busy`:``].filter(Boolean).join(` `),children:[e.asset?.url?(0,t.jsx)(`img`,{src:e.asset.url,className:`pretext-plus-editor__toc-asset-img`,onClick:()=>pe(e)}):(0,t.jsx)(`span`,{className:`pretext-plus-editor__toc-asset-img pretext-plus-editor__toc-asset-img--placeholder`,onClick:()=>pe(e),title:e.status===`unlinked`?`No asset — click to link`:void 0,"aria-hidden":`true`,children:e.status===`unlinked`?`⚠`:`🖼`}),(0,t.jsxs)(`button`,{type:`button`,className:`pretext-plus-editor__toc-asset-name`,onClick:()=>pe(e),title:e.status===`unlinked`?`No asset for this reference — click to link or create one`:`Edit asset`,children:[(0,t.jsx)(`span`,{className:`pretext-plus-editor__toc-asset-label`,children:e.asset?.name??e.ref}),(0,t.jsx)(`span`,{className:`pretext-plus-editor__toc-asset-filename`,children:e.status===`unlinked`?`${e.ref} — needs asset`:e.status===`unused`?`${e.ref} — not placed`:e.ref})]}),(0,t.jsx)(`div`,{className:`pretext-plus-editor__toc-actions`,children:te===e.ref?(0,t.jsx)(`span`,{className:`pretext-plus-editor__toc-asset-spinner`,role:`status`,"aria-label":`Duplicating asset`,title:`Duplicating…`}):(0,t.jsx)(V5,{items:he(e)})})]},e.ref))})]},e))})})]}),e&&(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__toc-assets-btn`,onClick:e,children:`Manage Assets`})]})},W5=({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)(U5,{onOpenAssetPicker:r})]}),G5=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`})]})}},K5=(e,t)=>e.kind===t.kind&&e.ref===t.ref;function q5(e){let t=()=>{},n={cbs:{selectDivision:t,addDivision:t,removeDivision:t,updateDivision:t,divisionContentChange:t,handleDivisionContentChange:t,assetInsert:t,updateTitle:t}};return{store:i6()((t,r)=>({source:e.source,sourceFormat:e.sourceFormat,projectAssets:e.projectAssets,libraryAssets:void 0,title:e.title,docinfo:e.docinfo,commonDocinfo:e.commonDocinfo,useCommonDocinfo:e.useCommonDocinfo,projectType:e.projectType,projectUrl:void 0,divisions:e.divisions,rootDivisionId:void 0,activeDivisionId:e.activeDivisionId,canConvertToPretext:!0,activeEditorSource:e.source,hasFeedback:!1,hasAssetDuplicate:!1,isTocCollapsed:!1,showFullPreview:!0,isNarrowScreen:typeof window<`u`?window.innerWidth<800:!1,activeTab:`editor`,isLatexDialogOpen:!1,isConvertDialogOpen:!1,isDocinfoEditorOpen:!1,isAssetPickerOpen:!1,isFullSourceOpen:!1,editingId:null,editDraft:null,editingIsNew:!1,editingAssetRef:null,assetResolveTarget:null,syncState:e=>t(e),applyExternalUpdate:e=>t(e),setDivisionContent:(e,n)=>t(t=>{if(!t.divisions)return{};let r=!1,i=t.divisions.map(t=>t.xmlId===e&&t.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,n)=>{let{xmlId:r,label:i}=e.sourceFormat===`markdown`?F8(e.content)??{xmlId:e.xmlId,label:``}:e.sourceFormat===`latex`?{xmlId:v8(e.content)||e.xmlId,label:``}:T8(e.content);t({editingId:e.xmlId,editDraft:{title:e.title,type:e.type,xmlId:r,label:i,sourceFormat:e.sourceFormat},editingIsNew:n?.isNew??!1})},setEditDraft:e=>t({editDraft:e}),commitSectionEdit:()=>{let{editingId:e,editDraft:i,divisions:a}=r();if(e&&i){let t=(a??[]).find(t=>t.xmlId===e),r=null;if(t){let t=E8(i.xmlId);if(!t){window.alert(`xml:id can't be empty — it identifies the division and is used by references to it.`);return}if((a??[]).some(n=>n.xmlId!==e&&n.xmlId===t)){window.alert(`xml:id "${t}" is already used by another division. Choose a unique id.`);return}r=t}n.cbs.updateDivision(e,{title:i.title.trim()||void 0,type:i.type,xmlId:r,label:i.label.trim()||null,sourceFormat:i.sourceFormat})}t({editingId:null,editDraft:null,editingIsNew:!1})},cancelSectionEdit:()=>t({editingId:null,editDraft:null,editingIsNew:!1}),insertAsset:e=>n.cbs.assetInsert(e),insertAtCursor:e=>n.cbs.insertContentAtCursor?.(e),openAssetEditor:(e,n)=>t({editingAssetRef:{kind:e,ref:n}}),closeAssetEditor:()=>t({editingAssetRef:null}),openAssetResolver:(e,n)=>t({assetResolveTarget:{kind:e,ref:n}}),closeAssetResolver:()=>t({assetResolveTarget:null}),removeAsset:e=>n.cbs.assetRemove?.(e),removeAssetRefFromDocument:(e,t)=>n.cbs.assetRefRemove?.(e,t),duplicateAsset:async e=>{await n.cbs.assetDuplicate?.(e)},setProjectAssets:e=>t({projectAssets:e}),addAssetToPool:e=>t(t=>{let n=t.projectAssets??[];return n.some(t=>K5(t,e))?{}:{projectAssets:[...n,e]}}),updateAssetInPool:e=>t(t=>{let n=t.projectAssets??[];return n.some(t=>K5(t,e))?{projectAssets:n.map(t=>K5(t,e)?e:t)}:{projectAssets:[...n,e]}}),renameAssetInPool:(e,n,r)=>t(t=>({projectAssets:[...(t.projectAssets??[]).filter(t=>!(t.kind===e&&t.ref===n)&&!K5(t,r)),r]})),removeAssetFromPool:e=>t(t=>({projectAssets:(t.projectAssets??[]).filter(t=>!K5(t,e))})),updateTitle:e=>n.cbs.updateTitle(e),feedbackSubmit:e=>n.cbs.feedbackSubmit?.(e)})),bindCallbacks:e=>{n.cbs=e}}}function J5({store:e,children:n}){return(0,t.jsx)(s6.Provider,{value:e,children:n})}var Y5=(e,t)=>e.find(e=>t?e.xmlId===t:e.type===`book`||e.type===`article`||e.type===`slideshow`)??e[0]??null,X5=e=>{let[r]=(0,n.useState)(()=>{let t=Y5(e.divisions,e.rootDivisionId),n=b5(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 q5({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)(J5,{store:r.store,children:(0,t.jsx)(Z5,{...e,bindCallbacks:r.bindCallbacks})})},Z5=e=>{let{bindCallbacks:r}=e,i=c6(e=>e.showFullPreview),a=c6(e=>e.isNarrowScreen),o=c6(e=>e.setIsNarrowScreen),s=c6(e=>e.activeTab),c=c6(e=>e.setActiveTab),l=c6(e=>e.isTocCollapsed),u=c6(e=>e.setIsTocCollapsed),d=c6(e=>e.isLatexDialogOpen),f=c6(e=>e.isConvertDialogOpen),p=c6(e=>e.isDocinfoEditorOpen),m=c6(e=>e.isAssetPickerOpen),h=c6(e=>e.isFullSourceOpen),g=c6(e=>e.editingAssetRef),_=c6(e=>e.openAssetEditor),v=c6(e=>e.closeAssetEditor),y=c6(e=>e.assetResolveTarget),b=c6(e=>e.closeAssetResolver),[x,S]=(0,n.useState)(null),C=c6(e=>e.openModal),w=c6(e=>e.closeModal),T=c6(e=>e.syncState),E=c6(e=>e.projectAssets),D=c6(e=>e.addAssetToPool),O=c6(e=>e.updateAssetInPool),k=c6(e=>e.renameAssetInPool),A=c6(e=>e.removeAssetFromPool),ee=g?E?.find(e=>e.kind===g.kind&&e.ref===g.ref):void 0,te=c6(e=>e.divisions),j=(0,n.useMemo)(()=>te??[],[te]),M=c6(e=>e.activeDivisionId),ne=c6(e=>e.title),N=c6(e=>e.docinfo),re=c6(e=>e.commonDocinfo),ie=c6(e=>e.useCommonDocinfo),ae=c6(e=>e.applyExternalUpdate),oe=c6(e=>e.setDivisionContent),se=c6(e=>e.patchDivision),P=c6(e=>e.addDivisionToPool),ce=c6(e=>e.removeDivisionFromPool),le=c6(e=>e.setActiveDivisionId),ue=c6(e=>e.startSectionEdit),de=c6(e=>e.setTitle),F=c6(e=>e.setDocinfo),fe=c6(e=>e.editingId),pe=c6(e=>e.editingIsNew),me=(0,n.useRef)(null),he=(0,n.useRef)(null),ge=(0,n.useRef)(!1);(0,n.useEffect)(()=>{fe&&pe?ge.current=!0:ge.current&&(ge.current=!1,he.current?.focus())},[fe,pe]);let I=Y5(j,e.rootDivisionId),_e=j.find(e=>e.xmlId===M)??j[0]??null,ve=_e?.sourceFormat??`pretext`,ye=_e?.content??``,be=(t,n,r,i)=>{oe(t,n),e.onContentChange({xmlId:t,sourceContent:n,sourceFormat:r,pretextSource:r===`pretext`?n:void 0,...i})},xe=(t,n)=>{se(t,n),e.onDivisionUpdate?.(t,n)},Se=(e,t)=>{let n=j.find(t=>t.xmlId===e);if(!n){xe(e,t);return}let r;if(t.sourceFormat!==void 0&&t.sourceFormat!==n.sourceFormat){let e=t.type??n.type,i=t.title??n.title,a=t.xmlId||n.xmlId;r={...n,sourceFormat:t.sourceFormat,type:e,title:i,xmlId:a,content:Q8(e,t.sourceFormat,i,a)}}else r=n.sourceFormat===`markdown`?I8(n,t):n.sourceFormat===`latex`?y8(n,t):k8(n,t);let i=r.xmlId;if(r.content!==n.content&&be(n.xmlId,r.content,r.sourceFormat),xe(e,{...t,xmlId:i}),i!==n.xmlId||r.type!==n.type){let e=r5(j,n.xmlId);if(e){let t=n5(e.content,n.xmlId,i,r.type);t!==e.content&&be(e.xmlId,t,e.sourceFormat)}}i!==n.xmlId&&we(i)},Ce=t=>{P(t),(e.onDivisionAdd?.(t))?.then(e=>se(t.xmlId,{id:e})).catch(()=>{})},L=t=>{ce(t),e.onDivisionRemove?.(t)},we=t=>{le(t),e.onDivisionSelect?.(t)},Te=(0,n.useMemo)(()=>{if(!_e||ve===`pretext`)return;if(ve===`latex`)return b8(_e)??void 0;let e=e6(ye,ve);return e.pretextError?void 0:e.pretextSource},[_e,ve,ye]),Ee=e=>{if(!_e)return;let t=e||``;if(ve===`pretext`){let e=O8(t);e&&e.xmlId!==_e.xmlId&&(t=k8({..._e,content:t,title:e.title,type:e.type},{xmlId:_e.xmlId,label:e.label||null}).content)}else if(ve===`markdown`){let e=F8(t);e&&e.xmlId!==_e.xmlId&&(t=I8({..._e,content:t},{xmlId:_e.xmlId}).content)}if(t===_e.content)return;if(be(_e.xmlId,t,ve),ve===`pretext`){let e=O8(t);if(e&&(xe(_e.xmlId,{title:e.title,type:e.type,label:e.label||null}),e.type!==_e.type)){let t=r5(j,_e.xmlId);if(t){let n=n5(t.content,_e.xmlId,_e.xmlId,e.type);n!==t.content&&be(t.xmlId,n,t.sourceFormat)}}}else if(ve===`markdown`){let e=F8(t);if(e&&(xe(_e.xmlId,{title:e.title,type:e.type,label:e.label||null}),e.type!==_e.type)){let t=r5(j,_e.xmlId);if(t){let n=n5(t.content,_e.xmlId,_e.xmlId,e.type);n!==t.content&&be(t.xmlId,n,t.sourceFormat)}}}else if(ve===`latex`){let e=_8(t);e!==null&&xe(_e.xmlId,{title:e})}let n=new Set(j.map(e=>e.xmlId));for(let{xmlId:e,type:r}of K8(t))n.has(e)||(Ce(Z8(e,r,ve)),n.add(e))},R=e=>{we(e)},De=()=>{_e&&(u(!1),ue(_e))},Oe=e=>{let t=o8();if(Ce(t),e){let n=j.find(t=>t.xmlId===e);n&&be(n.xmlId,$8(n.content,t.xmlId,t.type,null,n.sourceFormat),n.sourceFormat)}le(t.xmlId),ue(t,{isNew:!0})},ke=e=>{D(e)},z=t=>{A(t),e.onAssetRemove?.(t)},Ae=!!(e.onAssetUpload&&e.onAssetFetchUrl&&e.onAssetUpdate),je=!!(e.onAssetRemove&&(e.onAssetUpload||e.onAssetAddFromLibrary)),Me=async t=>{if(!e.onAssetUpload||!e.onAssetFetchUrl||!t.ref||!t.url)return;let n=new Set(x5(j,E).map(e=>e.ref)),r=S5(t.ref,n),i=await e.onAssetFetchUrl(t.url),a=/\.[^./\\]+$/.exec(i.name)?.[0]??``,o=new File([i],`${r}${a}`,{type:i.type}),s={...await e.onAssetUpload(o),ref:r,name:`${t.name} (copy)`,source:t.source};await e.onAssetUpdate?.(s),D(s),_(s.kind,r)},Ne=async(t,n,r)=>{if(r)n.ref&&t.ref&&Pe(t.kind,t.ref,n.ref),D(n);else{let r={...n,ref:t.ref,name:t.name,source:t.source};await e.onAssetUpdate?.(r),O(r)}e.onAssetRemove?.(t),A(t)},Pe=(e,t,n)=>{if(t!==n)for(let r of j){let i=J8(r.content,e,t,n);i!==r.content&&be(r.xmlId,i,r.sourceFormat)}},Fe=(e,t)=>{for(let n of j){let r=Y8(n.content,e,t);r!==n.content&&be(n.xmlId,r,n.sourceFormat)}};(0,n.useEffect)(()=>{let e=()=>o(window.innerWidth<800);return window.addEventListener(`resize`,e),()=>window.removeEventListener(`resize`,e)},[o]);let Ie=ve!==`pretext`;(0,n.useLayoutEffect)(()=>{r({selectDivision:R,addDivision:e=>Oe(e),removeDivision:e=>L(e),updateDivision:(e,t)=>Se(e,t),divisionContentChange:(e,t)=>{be(e,t,j.find(t=>t.xmlId===e)?.sourceFormat??`pretext`)},handleDivisionContentChange:Ee,assetInsert:ke,assetRemove:z,assetRefRemove:Fe,assetDuplicate:Ae?Me:void 0,insertContentAtCursor:e=>he.current?.insertAtCursor(e),updateTitle:t=>{de(t),e.onTitleChange?.(t)},feedbackSubmit:e.onFeedbackSubmit})}),(0,n.useEffect)(()=>{T({source:ye,sourceFormat:ve,libraryAssets:e.libraryAssets,projectType:e.projectType,projectUrl:e.projectUrl,rootDivisionId:I?.xmlId,canConvertToPretext:Te!==void 0,activeEditorSource:ye,hasFeedback:e.onFeedbackSubmit!==void 0,hasAssetDuplicate:Ae})});let Le=(0,n.useRef)({divisions:e.divisions,projectAssets:e.projectAssets,title:e.title,docinfo:e.docinfo,commonDocinfo:e.commonDocinfo,useCommonDocinfo:e.useCommonDocinfo,activeDivisionId:e.activeDivisionId});(0,n.useEffect)(()=>{let t=Le.current,n={},r=!1;if(e.projectAssets!==void 0&&e.projectAssets!==t.projectAssets&&(n.projectAssets=e.projectAssets,r=!0),e.divisions!==t.divisions){let t=Y5(e.divisions,e.rootDivisionId),i=b5(e.divisions,t?.xmlId,e.projectType,e.title);n.divisions=i;let a=i.find(e=>e.xmlId===t?.xmlId)??t;(M==null||!e.divisions.some(e=>e.xmlId===M))&&(n.activeDivisionId=a?.xmlId??null),!e.title&&a?.title&&(n.title=a.title),r=!0}e.title!==void 0&&e.title!==t.title&&(n.title=e.title,r=!0),e.docinfo!==void 0&&e.docinfo!==t.docinfo&&(n.docinfo=e.docinfo,r=!0),e.commonDocinfo!==void 0&&e.commonDocinfo!==t.commonDocinfo&&(n.commonDocinfo=e.commonDocinfo,r=!0),e.useCommonDocinfo!==void 0&&e.useCommonDocinfo!==t.useCommonDocinfo&&(n.useCommonDocinfo=e.useCommonDocinfo,r=!0),e.activeDivisionId!==void 0&&e.activeDivisionId!==t.activeDivisionId&&(n.activeDivisionId=e.activeDivisionId,r=!0),r&&ae(n),Le.current={divisions:e.divisions,projectAssets:e.projectAssets,title:e.title,docinfo:e.docinfo,commonDocinfo:e.commonDocinfo,useCommonDocinfo:e.useCommonDocinfo,activeDivisionId:e.activeDivisionId}});let Re=(0,n.useRef)(null);(0,n.useEffect)(()=>{if(Re.current===e.divisions)return;Re.current=e.divisions;let t=Y5(e.divisions,e.rootDivisionId),n=b5(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 ze=ie?re:N,Be=(0,n.useMemo)(()=>{if(!h||!I)return``;try{return g5(j,I.xmlId,ze,E??[])}catch(e){return`<!-- Unable to assemble document source: ${e instanceof Error?e.message:String(e)} -->`}},[h,I,j,ze,E]),Ve=_e?ve===`pretext`?m5(j,_e.xmlId,E):Te:void 0,He=_e&&Ve!==void 0?v5(_e.type,Ve,ze,_e.title):void 0,Ue=()=>me.current?.rebuild(),B=()=>{e.onSave?.(),me.current?.rebuild()},We=t=>{let n=t.ctrlKey||t.metaKey;n&&t.key===`Enter`&&e.onPreviewRebuild?(t.preventDefault(),Ue()):n&&t.key===`s`&&(t.preventDefault(),B())},Ge=()=>{if(!_e||!Te)return;let e=o8(),t={id:e.xmlId,xmlId:_e.xmlId,title:_e.title,type:_e.type,sourceFormat:ve,content:_e.content};Ce(ve===`markdown`?I8(t,{xmlId:e.xmlId}):y8(t,{xmlId:e.xmlId})),be(_e.xmlId,Te,`pretext`),xe(_e.xmlId,{sourceFormat:`pretext`})},Ke=(0,t.jsx)(Ws,{ref:he,content:ye,sourceFormat:ve,onChange:Ee,onRebuild:e.onPreviewRebuild?Ue:void 0,onSave:B,onOpenLatexImport:()=>C(`isLatexDialogOpen`),onOpenDocinfoEditor:()=>C(`isDocinfoEditorOpen`),onOpenConvertToPretext:Ie&&Te!==void 0?()=>C(`isConvertDialogOpen`):void 0,canConvertToPretext:Te!==void 0,onOpenAssets:e.projectAssets!==void 0&&ve===`pretext`?()=>C(`isAssetPickerOpen`):void 0,onShowFullSource:()=>C(`isFullSourceOpen`),onRequestWrapperEdit:De}),V;V=i&&e.onPreviewRebuild?(0,t.jsx)(lR,{ref:me,content:He||``,title:ne,onRebuild:e.onPreviewRebuild}):(0,t.jsx)(oR,{content:ye,canEdit:ve===`pretext`,editDisabledReason:ve===`markdown`?`Visual editing is not available for Markdown documents.`:ve===`latex`?`Visual editing is not available for LaTeX documents.`:``,onChange:e=>Ee(e)});let qe=(0,t.jsx)(W5,{isCollapsed:l,onToggleCollapse:()=>u(e=>!e),onOpenAssetPicker:e.projectAssets===void 0?void 0:()=>C(`isAssetPickerOpen`)}),Je=`pretext-plus-tab-editor`,Ye=`pretext-plus-tab-preview`,Xe=`pretext-plus-tabpanel`,Ze;return Ze=a?(0,t.jsxs)(`div`,{className:`pretext-plus-editor__tabs`,children:[qe,(0,t.jsxs)(`div`,{className:`pretext-plus-editor__tabs-main`,children:[(0,t.jsxs)(`div`,{className:`pretext-plus-editor__tab-list`,role:`tablist`,children:[(0,t.jsx)(`button`,{type:`button`,id:Je,role:`tab`,"aria-controls":Xe,"aria-selected":s===`editor`,tabIndex:s===`editor`?0:-1,className:`pretext-plus-editor__tab-button ${s===`editor`?`is-active`:``}`,onClick:()=>c(`editor`),children:`Editor`}),(0,t.jsx)(`button`,{type:`button`,id:Ye,role:`tab`,"aria-controls":Xe,"aria-selected":s===`preview`,tabIndex:s===`preview`?0:-1,className:`pretext-plus-editor__tab-button ${s===`preview`?`is-active`:``}`,onClick:()=>c(`preview`),children:`Preview`})]}),(0,t.jsx)(`div`,{id:Xe,className:`pretext-plus-editor__tab-panel`,role:`tabpanel`,"aria-labelledby":s===`editor`?Je:Ye,children:(0,t.jsx)(`div`,{style:{height:`100%`},children:s===`editor`?Ke:V})})]})]}):(0,t.jsxs)(`div`,{className:`pretext-plus-editor__sectioned-layout`,children:[qe,(0,t.jsxs)(gt,{orientation:`horizontal`,className:`pretext-plus-editor__splitter`,children:[(0,t.jsx)(yt,{className:`pretext-plus-editor__editor-panel`,children:Ke}),(0,t.jsx)(St,{className:`pretext-plus-editor__resize-handle`,children:(0,t.jsx)(`div`,{className:`pretext-plus-editor__resize-dots`})}),(0,t.jsx)(yt,{className:`pretext-plus-editor__preview-panel`,children:V})]})]}),(0,t.jsxs)(`div`,{className:`pretext-plus-editor`,onKeyDown:We,children:[(0,t.jsx)(j5,{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)(G5,{resetKeys:[ye,M],children:Ze}),d?(0,t.jsx)(u6,{onClose:()=>w(`isLatexDialogOpen`)}):null,f&&_e&&Te?(0,t.jsx)(m6,{sourceContent:ye,sourceFormat:ve,pretextSource:Te,onConfirm:Ge,onClose:()=>w(`isConvertDialogOpen`)}):null,p?(0,t.jsx)(E6,{docinfo:N,showCommonDocinfoControls:!0,commonDocinfo:re,initialUseCommonDocinfo:ie,onClose:t=>{if(w(`isDocinfoEditorOpen`),t!==void 0){F({docinfo:t.docinfo,commonDocinfo:t.commonDocinfo,useCommonDocinfo:t.useCommonDocinfo}),e.onCommonDocinfoChange?.(t.commonDocinfo),e.onUseCommonDocinfoChange?.(t.useCommonDocinfo);let n=I??_e;be(n?.xmlId??``,n?.content??``,n?.sourceFormat??`pretext`,{docinfo:t.docinfo,commonDocinfo:t.commonDocinfo,useCommonDocinfo:t.useCommonDocinfo})}}}):null,(m||y||x)&&e.projectAssets!==void 0?(0,t.jsx)(E5,{open:m||!!y||!!x,resolveTarget:y,replaceTarget:x,onClose:()=>{w(`isAssetPickerOpen`),b(),S(null)},onLoadAssets:e.onLoadAssets,onLoadLibraryAssets:e.onLoadLibraryAssets,onAddFromLibrary:e.onAssetAddFromLibrary,onUpload:e.onAssetUpload,onFetchUrl:e.onAssetFetchUrl,onCreateDoenet:e.onCreateDoenet,onRemoveAsset:e.onAssetRemove?z:void 0,onDuplicateAsset:Ae?Me:void 0,onAssetAdded:ke,onResolveRef:Pe,onReplaceAsset:Ne}):null,h?(0,t.jsx)(O6,{source:Be,onClose:()=>w(`isFullSourceOpen`)}):null,ee?(0,t.jsx)(A5,{asset:ee,projectAssets:E??[],onClose:v,onReplace:je?e=>{v(),S(e)}:void 0,onDuplicate:Ae?e=>Me(e):void 0,onSave:async(t,n)=>{t.ref&&t.ref!==n?(Pe(t.kind,n,t.ref),k(t.kind,n,t)):O(t),await e.onAssetUpdate?.(t)}},`${ee.kind}:${ee.ref}`):null]})]})};e.CodeEditor=Ws,e.DocinfoEditor=E6,e.Editors=X5,e.FeedbackLink=n6,e.FullPreview=lR,e.VisualEditor=oR,e.assembleFullProjectSource=g5,e.assembleProjectSource=m5,e.buildDivisionTree=s5,e.convertLatexToPretext=Z3,e.createConclusion=c8,e.createIntroduction=s8,e.createLatexConclusion=C8,e.createLatexIntroduction=S8,e.createNewLatexSection=x8,e.createNewSection=o8,e.derivePretextContent=e6,e.detectSourceFormat=X3,e.ensureLatexSectionWrapper=m8,e.ensureSectionWrapper=d8,e.extractDivisionMetadata=O8,e.extractLatexDivisionTitle=_8,e.findDivisionParent=r5,e.getOrphanRoots=c5,e.getOrphanedDivisions=o5,e.getSectionAttributes=T8,e.insertDivisionRef=$8,e.mergeTwoSections=w8,e.moveDivisionRef=t5,e.parseDivisionRefs=G8,e.postToIframe=cR,e.removeDivisionRef=e5,e.renameDivisionRef=n5,e.reorderDivisionRefs=i5,e.rewrapLatexSection=p8,e.rewrapSection=u8,e.stripLatexSectionWrapper=f8,e.stripSectionWrapper=l8,e.updateDivisionTitle=a8,e.updateLatexSectionTitle=g8,e.updateSectionMetadata=k8,e.wrapDivisionForPreview=v5});
1092
+ `+a.slice(o)}function c5(e,t,n,r){let i=new RegExp(q8(t));return e.replace(i,e=>{let t=e.trimStart();return t.startsWith(`::`)?`::${r}{ref="${n}"}`:t.startsWith(`\\plus`)?`\\plus{${r}}{${n}}`:`<plus:${r} ref="${n}"/>`})}function l5(e,t){let n=new RegExp(q8(t));return e.find(e=>n.test(e.content))??null}function u5(e,t){let n=e,r=null;for(let e of t)n=s5(n,e,r),r=e;return n}function d5(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 Z8(i.content,i.sourceFormat))r.push(e)}return n}function f5(e,t){let n=d5(e,t);return e.filter(e=>!n.has(e.xmlId))}function p5(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 Z8(o.content,o.sourceFormat)){if(r.has(s))continue;let o=e.find(e=>e.xmlId===s);o&&(r.add(s),n.push({division:o,depth:a,parentXmlId:t}),i(s,a+1))}};return i(t,0),n}function m5(e,t){let n=f5(e,t),r=new Set(n.map(e=>e.xmlId)),i=new Set;for(let e of n)for(let t of Z8(e.content,e.sourceFormat))r.has(t)&&i.add(t);return n.filter(e=>!i.has(e.xmlId))}var h5=new Set([`book`,`article`,`slideshow`]);function g5(e){try{let t=N6(e),n=t.children.find(e=>e.type===`element`),r=n?.name===`pretext`?n.children.find(e=>e.type===`element`&&h5.has(e.name)):n&&h5.has(n.name)?n:void 0;if(!r)return e;if(!r.attributes.label){let e=v5(t,`pretext-plus-preview`);return r.attributes.label=e,JY(t,$6)}return e}catch(t){return console.error(`Error ensuring label:`,t),e}}function _5(e,t){return e.type===`element`&&e.attributes?.label===t?!0:`children`in e&&e.children?e.children.some(e=>e.type===`element`?_5(e,t):!1):!1}function v5(e,t){let n=t,r=1;for(;_5(e,n);)n=`${t}-${r}`,r++;return n}function y5(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 if(i.sourceFormat===`markdown`){let{pretextSource:e,pretextError:t}=e6(i.content,`markdown`);a=e??`<!-- conversion error: ${t} -->`}else a=C8(i)??`<!-- conversion error: ${i.xmlId} -->`;let o=new Set(n).add(e);return a.replace(/<plus:([a-z-]+)\s([^>]*ref="[^"]+"[^>]*?)(?:\/>|>\s*<\/plus:\1>)/g,(e,n,i)=>{let a=/ref="([^"]+)"/.exec(i)?.[1]??``;if(!Z6.has(n))return y5(a,t,o,r);let s=/width="([^"]+)"/.exec(i)?.[1];return Q6(n,a,r,s)})}function b5(e,t,n=[]){return g5(y5(t,e,new Set,n))}function x5(e,t){return g5(`<pretext>\n${t.trim()?`${t.trim()}\n`:``}${e}\n</pretext>`)}function S5(e,t,n,r=[]){return x5(y5(t,e,new Set,r),n)}var C5=new Set([`part`,`chapter`]);function w5(e,t,n,r){return x5(h5.has(e)?t:C5.has(e)?`<book>\n<title>${r}</title>\n${t}\n</book>`:`<article>\n<title>${r}</title>\n${t}\n</article>`,n)}function T5(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 E5(e,t,n,r){let i=n===`book`?`book`:`article`;return e.map(e=>{if(e.sourceFormat===`markdown`){if(!e.title){let t=R8(e.content)?.title;if(t)return{...e,title:t}}return e}if(e.sourceFormat===`latex`){if(!e.title){let t=b8(e.content);if(t)return{...e,title:t}}return e}if(e.sourceFormat!==`pretext`)return e;let n=j8(e.content);if(e.xmlId===t&&!(n&&h5.has(n.type))){let{title:t,body:n}=T5(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})}function D5(e,t){let n=t??[],r=(e,t)=>n.find(n=>n.kind===e&&n.ref===t),i=[],a=new Set;for(let t of e??[])for(let{kind:e,ref:n}of $8(t.content,t.sourceFormat)){let t=`${e}:${n}`;if(a.has(t))continue;a.add(t);let o=r(e,n);i.push({kind:e,ref:n,asset:o,inDocument:!0,status:o?`linked`:`unlinked`})}for(let e of n){if(!e.ref)continue;let t=`${e.kind}:${e.ref}`;a.has(t)||(a.add(t),i.push({kind:e.kind,ref:e.ref,asset:e,inDocument:!1,status:`unused`}))}return i}function O5(e,t){let n=`${e}-copy`;if(!t.has(n))return n;for(let e=2;;e++){let r=`${n}-${e}`;if(!t.has(r))return r}}var k5={image:`Images`,doenet:`Doenet`},A5=[`image`,`doenet`].filter(e=>e!==`doenet`||!1);function j5(e){return`${e}-${Date.now()}`}var M5=({open:e,onClose:r,resolveTarget:i,replaceTarget:a,onLoadAssets:o,onLoadLibraryAssets:s,onAddFromLibrary:c,onUpload:l,onFetchUrl:u,onCreateDoenet:d,onRemoveAsset:f,onDuplicateAsset:p,onAssetAdded:m,onResolveRef:h,onReplaceAsset:g})=>{let _=c6(e=>e.divisions),v=c6(e=>e.activeDivisionId),y=_?.find(e=>e.xmlId===v)?.sourceFormat??`pretext`,b=(e,t)=>n5(e,t,y),x=c6(e=>e.projectAssets)??[],S=c6(e=>e.setProjectAssets),C=c6(e=>e.libraryAssets),w=c6(e=>e.openAssetEditor),T=c6(e=>e.openAssetResolver),E=c6(e=>e.removeAssetRefFromDocument),D=!!(o||s),[O,k]=(0,n.useState)(null),[A,ee]=(0,n.useState)(!1),[te,j]=(0,n.useState)(null),M=x,ne=O??C??M,N=new Set(M.map(e=>e.id)),[re,ie]=(0,n.useState)(`in-document`),[ae,oe]=(0,n.useState)(null),[se,P]=(0,n.useState)(`library`),[ce,le]=(0,n.useState)(`library`),[ue,de]=(0,n.useState)(null),[F,fe]=(0,n.useState)(!1),[pe,me]=(0,n.useState)(!1),[he,ge]=(0,n.useState)(null),I=(0,n.useRef)(null),[_e,ve]=(0,n.useState)(``),[ye,be]=(0,n.useState)(``),[xe,Se]=(0,n.useState)(!1),[Ce,L]=(0,n.useState)(null),[we,Te]=(0,n.useState)(``),[Ee,R]=(0,n.useState)(``),[De,Oe]=(0,n.useState)(!1),[ke,z]=(0,n.useState)(null),[Ae,je]=(0,n.useState)(null),[Me,Ne]=(0,n.useState)(null),[Pe,Fe]=(0,n.useState)(null),Ie=(0,n.useCallback)(()=>{!o&&!s||(ee(!0),j(null),Promise.all([o?.()??Promise.resolve(null),s?.()??Promise.resolve(null)]).then(([e,t])=>{e!==null&&S(e),t!==null&&k(t)}).catch(e=>{j(e instanceof Error?e.message:`Failed to load assets.`)}).finally(()=>ee(!1)))},[o,s,S]),Le=(0,n.useRef)(Ie);if((0,n.useEffect)(()=>{Le.current=Ie}),(0,n.useEffect)(()=>{e&&Le.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 Re=D5(_,M),ze=(e,t)=>{if(a){g(a,e,!!t?.fromLibrary),r();return}if(m(e),i){e.ref&&h(i.kind,i.ref,e.ref),r();return}e.ref&&navigator.clipboard.writeText(b(e.kind,e.ref)).catch(()=>{}),Fe(e)},Be=(e,t)=>{let n=`${e}:${t}`;navigator.clipboard.writeText(b(e,t)).catch(()=>{}),je(n),setTimeout(()=>je(e=>e===n?null:e),2e3)},Ve=async e=>{if(!N.has(e.id)&&c){de(e.id);try{await c(e)}finally{de(null)}}ze(e,{fromLibrary:!0})},He=async e=>{if(l){ge(null),me(!0);try{ze(await l(e))}catch(e){ge(e instanceof Error?e.message:`Upload failed.`)}finally{me(!1)}}},Ue=async()=>{let e=_e.trim();if(!e)return;let t=ye.trim();if(u&&l){L(null),Se(!0);try{let n=await u(e);ze(await l(t?new File([n],t,{type:n.type}):n))}catch(e){L(e instanceof Error?e.message:`Failed to add URL.`)}finally{Se(!1)}}else ze({id:j5(`url`),name:t||e,ref:e.split(`/`).pop()??`image`,kind:`image`,url:e})},B=async()=>{let e=we.trim(),t=Ee.trim();if(!(!e||!t))if(d){z(null),Oe(!0);try{ze(await d(e,t))}catch(e){z(e instanceof Error?e.message:`Failed to create activity.`)}finally{Oe(!1)}}else ze({id:j5(`doenet`),name:e,ref:t,kind:`doenet`})},We=e=>{let n=ne.filter(t=>t.kind===e);return(0,t.jsxs)(`div`,{className:`pretext-plus-editor__am-library`,children:[D&&(0,t.jsxs)(`div`,{className:`pretext-plus-editor__am-toolbar`,children:[te&&(0,t.jsx)(`span`,{className:`pretext-plus-editor__asset-load-error`,children:te}),(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__asset-library-refresh`,onClick:Ie,disabled:A,children:A?`Loading…`:`Refresh`})]}),A&&n.length===0?(0,t.jsx)(`p`,{className:`pretext-plus-editor__am-empty-text`,children:`Loading…`}):n.length===0?(0,t.jsxs)(`p`,{className:`pretext-plus-editor__am-empty-text`,children:[`No `,k5[e].toLowerCase(),` assets in your library.`]}):(0,t.jsx)(`ul`,{className:`pretext-plus-editor__am-list`,children:n.map(e=>{let n=N.has(e.id),r=ue===e.id;return(0,t.jsx)(`li`,{children:(0,t.jsxs)(`button`,{type:`button`,className:[`pretext-plus-editor__am-lib-row`,n?`pretext-plus-editor__am-lib-row--in-project`:``].filter(Boolean).join(` `),onClick:()=>Ve(e),disabled:r||!e.ref,title:e.ref?n?`Use "${e.ref}"`:`Add to project & use "${e.ref}"`:`Asset has no reference — cannot use`,children:[(0,t.jsxs)(`div`,{className:`pretext-plus-editor__am-row-info`,children:[(0,t.jsx)(`span`,{className:`pretext-plus-editor__am-row-name`,children:e.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)})})]})},Ge=()=>{if(Re.length===0)return(0,t.jsxs)(`div`,{className:`pretext-plus-editor__am-placeholder`,children:[(0,t.jsx)(`p`,{children:`No assets in this project yet.`}),(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__dialog-button`,onClick:()=>{ie(`add`),oe(null)},children:`Add an asset`})]});let e=A5.map(e=>({kind:e,rows:Re.filter(t=>t.kind===e)})).filter(e=>e.rows.length>0),n=e=>{let n=`${e.kind}:${e.ref}`,i=Me===n,a=()=>{e.status===`unlinked`?T(e.kind,e.ref):(w(e.kind,e.ref),r())};return(0,t.jsxs)(`li`,{className:`pretext-plus-editor__am-doc-row`,children:[(0,t.jsxs)(`button`,{type:`button`,className:`pretext-plus-editor__am-row-info pretext-plus-editor__am-row-info--btn`,onClick:a,title:e.status===`unlinked`?`No asset for this reference — click to link or create one`:`Edit asset`,children:[(0,t.jsx)(`span`,{className:`pretext-plus-editor__am-row-name`,children:e.asset?.name??e.ref}),(0,t.jsx)(`span`,{className:`pretext-plus-editor__am-row-ref`,children:e.ref})]}),e.status===`unlinked`&&(0,t.jsx)(`span`,{className:`pretext-plus-editor__am-status pretext-plus-editor__am-status--warn`,title:`No asset for this reference`,children:`needs asset`}),e.status===`unused`&&(0,t.jsx)(`span`,{className:`pretext-plus-editor__am-status`,title:`Not referenced in the document yet`,children:`not placed`}),(0,t.jsxs)(`div`,{className:`pretext-plus-editor__am-row-actions`,children:[(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__am-action-btn`,onClick:a,children:e.status===`unlinked`?`Link / create`:`Edit`}),(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__am-action-btn${Ae===n?` pretext-plus-editor__am-action-btn--done`:``}`,onClick:()=>Be(e.kind,e.ref),title:`Copy ${b(e.kind,e.ref)}`,children:Ae===n?`Copied!`:`Copy embed code`}),p&&e.asset?.url&&(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__am-action-btn`,disabled:i,onClick:async()=>{Ne(n);try{await p(e.asset),r()}catch{Ne(null)}},title:`Create a copy of this asset under a new reference`,children:i?`Duplicating…`:`Duplicate`}),f&&e.asset&&(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__am-action-btn pretext-plus-editor__am-action-btn--danger`,onClick:()=>{e.inDocument&&!window.confirm(`Remove "${e.asset.name}" from the project? This also deletes its reference(s) from the document.`)||(f(e.asset),E(e.kind,e.ref),r())},title:`Remove from project`,children:`Remove`})]})]},n)};return(0,t.jsx)(`div`,{className:`pretext-plus-editor__am-in-doc`,children:e.map(({kind:e,rows:r})=>(0,t.jsxs)(`div`,{className:`pretext-plus-editor__am-kind-group`,children:[(0,t.jsxs)(`div`,{className:`pretext-plus-editor__am-kind-header`,children:[(0,t.jsx)(`span`,{"aria-hidden":`true`,children:`📁`}),(0,t.jsx)(`span`,{children:k5[e]}),(0,t.jsx)(`span`,{className:`pretext-plus-editor__am-kind-count`,children:r.length})]}),(0,t.jsx)(`ul`,{className:`pretext-plus-editor__am-list`,children:r.map(n)})]},e))})},Ke=e=>(0,t.jsxs)(`div`,{className:`pretext-plus-editor__am-success`,children:[(0,t.jsxs)(`p`,{className:`pretext-plus-editor__am-success-title`,children:[`✓ Added “`,e.name,`” — embed code copied to clipboard`]}),(0,t.jsx)(`p`,{className:`pretext-plus-editor__dialog-helper-copy`,children:`It now appears in the Assets list. Paste this where you want it to appear:`}),e.ref&&(0,t.jsxs)(`div`,{className:`pretext-plus-editor__am-embed-row`,children:[(0,t.jsx)(`code`,{className:`pretext-plus-editor__am-embed-code`,children:b(e.kind,e.ref)}),(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__am-action-btn${Ae===`${e.kind}:${e.ref}`?` pretext-plus-editor__am-action-btn--done`:``}`,onClick:()=>Be(e.kind,e.ref),children:Ae===`${e.kind}:${e.ref}`?`Copied!`:`Copy again`})]}),(0,t.jsxs)(`div`,{className:`pretext-plus-editor__am-success-actions`,children:[(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__dialog-button pretext-plus-editor__dialog-button--secondary`,onClick:()=>{Fe(null),oe(null),ie(`add`)},children:`Add another`}),(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__dialog-button`,onClick:()=>{Fe(null),ie(`in-document`)},children:`Done`})]})]}),V=()=>(0,t.jsxs)(`div`,{className:`pretext-plus-editor__am-kind-picker`,children:[(0,t.jsx)(`p`,{className:`pretext-plus-editor__am-kind-picker-label`,children:`What kind of asset?`}),(0,t.jsxs)(`div`,{className:`pretext-plus-editor__am-kind-cards`,children:[(0,t.jsxs)(`button`,{type:`button`,className:`pretext-plus-editor__am-kind-card`,onClick:()=>{oe(`image`),P(`library`)},children:[(0,t.jsx)(`span`,{className:`pretext-plus-editor__am-kind-card-icon`,"aria-hidden":`true`,children:`🖼️`}),(0,t.jsx)(`span`,{className:`pretext-plus-editor__am-kind-card-label`,children:`Image`}),(0,t.jsx)(`span`,{className:`pretext-plus-editor__am-kind-card-hint`,children:`PNG, JPEG, SVG, etc.`})]}),!1,(0,t.jsxs)(`div`,{className:`pretext-plus-editor__am-kind-card pretext-plus-editor__am-kind-card--soon`,"aria-disabled":`true`,children:[(0,t.jsx)(`span`,{className:`pretext-plus-editor__am-kind-card-icon`,"aria-hidden":`true`,children:`✨`}),(0,t.jsx)(`span`,{className:`pretext-plus-editor__am-kind-card-label`,children:`More coming soon`}),(0,t.jsx)(`span`,{className:`pretext-plus-editor__am-kind-card-hint`,children:`Interactive activities and more`})]})]})]}),qe=e=>(0,t.jsxs)(`div`,{className:`pretext-plus-editor__am-configure`,children:[e&&(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__am-back-btn`,onClick:()=>oe(null),children:`← Back`}),(0,t.jsxs)(`div`,{className:`pretext-plus-editor__dialog-tab-bar pretext-plus-editor__am-sub-tabs`,children:[(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__dialog-tab${se===`library`?` pretext-plus-editor__dialog-tab--active`:``}`,onClick:()=>P(`library`),children:`Library`}),l&&(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__dialog-tab${se===`upload`?` pretext-plus-editor__dialog-tab--active`:``}`,onClick:()=>P(`upload`),children:`Upload`}),(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__dialog-tab${se===`url`?` pretext-plus-editor__dialog-tab--active`:``}`,onClick:()=>P(`url`),children:`External URL`})]}),(0,t.jsxs)(`div`,{className:`pretext-plus-editor__am-configure-body`,children:[se===`library`&&We(`image`),se===`upload`&&l&&(0,t.jsxs)(`div`,{className:`pretext-plus-editor__am-upload`,children:[(0,t.jsxs)(`div`,{className:[`pretext-plus-editor__am-drop-zone`,F?`pretext-plus-editor__am-drop-zone--active`:``,pe?`pretext-plus-editor__am-drop-zone--uploading`:``].filter(Boolean).join(` `),onDragOver:e=>{e.preventDefault(),fe(!0)},onDragLeave:()=>fe(!1),onDrop:e=>{e.preventDefault(),fe(!1);let t=e.dataTransfer.files[0];t&&He(t)},onClick:()=>!pe&&I.current?.click(),role:`button`,tabIndex:0,onKeyDown:e=>{(e.key===`Enter`||e.key===` `)&&(e.preventDefault(),I.current?.click())},"aria-label":`Upload image — click or drag and drop`,children:[(0,t.jsx)(`input`,{ref:I,type:`file`,accept:`image/*`,className:`pretext-plus-editor__dialog-file-input`,onChange:e=>{let t=e.target.files?.[0];t&&He(t)}}),pe?(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`})]})]}),he&&(0,t.jsx)(`p`,{className:`pretext-plus-editor__am-error`,children:he})]}),se===`url`&&(0,t.jsxs)(`div`,{className:`pretext-plus-editor__am-url-form`,children:[(0,t.jsx)(`label`,{className:`pretext-plus-editor__dialog-label`,htmlFor:`am-url-value`,children:`Image URL`}),(0,t.jsx)(`input`,{id:`am-url-value`,type:`url`,className:`pretext-plus-editor__am-input`,placeholder:`https://example.com/image.png`,value:_e,onChange:e=>ve(e.target.value),disabled:xe,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:ye,onChange:e=>be(e.target.value),disabled:xe}),Ce&&(0,t.jsx)(`p`,{className:`pretext-plus-editor__am-error`,children:Ce}),_e.trim()&&(0,t.jsx)(`img`,{src:_e.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:Ue,disabled:!_e.trim()||xe,children:xe?`Adding…`:u&&l?`Add to Project`:`Add`})]})]})]}),Je=e=>(0,t.jsxs)(`div`,{className:`pretext-plus-editor__am-configure`,children:[e&&(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__am-back-btn`,onClick:()=>oe(null),children:`← Back`}),(0,t.jsxs)(`div`,{className:`pretext-plus-editor__dialog-tab-bar pretext-plus-editor__am-sub-tabs`,children:[(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__dialog-tab${ce===`library`?` pretext-plus-editor__dialog-tab--active`:``}`,onClick:()=>le(`library`),children:`Library`}),(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__dialog-tab${ce===`create`?` pretext-plus-editor__dialog-tab--active`:``}`,onClick:()=>le(`create`),children:`Create New`})]}),(0,t.jsxs)(`div`,{className:`pretext-plus-editor__am-configure-body`,children:[ce===`library`&&We(`doenet`),ce===`create`&&(0,t.jsxs)(`div`,{className:`pretext-plus-editor__am-create-form`,children:[(0,t.jsx)(`label`,{className:`pretext-plus-editor__dialog-label`,htmlFor:`am-doenet-name`,children:`Name`}),(0,t.jsx)(`input`,{id:`am-doenet-name`,type:`text`,className:`pretext-plus-editor__am-input`,placeholder:`My Activity`,value:we,onChange:e=>Te(e.target.value),disabled:De,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:Ee,onChange:e=>R(e.target.value),disabled:De}),(0,t.jsxs)(`p`,{className:`pretext-plus-editor__dialog-helper-copy`,children:[`The reference ID is used in the embed code: `,(0,t.jsx)(`code`,{children:b(`doenet`,Ee||`my-activity`)})]}),ke&&(0,t.jsx)(`p`,{className:`pretext-plus-editor__am-error`,children:ke}),(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__dialog-button`,onClick:B,disabled:!we.trim()||!Ee.trim()||De,children:De?`Creating…`:d?`Create`:`Add`})]})]})]}),Ye=(e,n,i)=>(0,t.jsxs)(t.Fragment,{children:[(0,t.jsxs)(`div`,{className:`pretext-plus-editor__dialog-header`,children:[(0,t.jsx)(`h2`,{className:`pretext-plus-editor__dialog-title`,children:n}),(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__dialog-close`,onClick:r,"aria-label":`Close`,children:`✕`})]}),(0,t.jsxs)(`div`,{className:`pretext-plus-editor__dialog-content pretext-plus-editor__dialog-content--single`,children:[(0,t.jsx)(`p`,{className:`pretext-plus-editor__dialog-helper-copy pretext-plus-editor__am-resolve-hint`,children:i}),e===`doenet`?Je(!1):qe(!1)]}),(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:`Cancel`})})]});return(0,t.jsx)(`div`,{className:`pretext-plus-editor__dialog-overlay`,onClick:e=>e.target===e.currentTarget&&r(),children:(0,t.jsx)(`div`,{className:`pretext-plus-editor__dialog pretext-plus-editor__dialog--asset-manager`,role:`dialog`,"aria-modal":`true`,"aria-label":`Asset manager`,children:i?(e=>Ye(e.kind,`Link asset`,(0,t.jsxs)(t.Fragment,{children:[`The reference `,(0,t.jsx)(`code`,{children:e.ref}),` has no asset yet. Choose or create one — the reference in your document will be updated to match.`]})))(i):a?(e=>Ye(e.kind,`Replace asset`,(0,t.jsxs)(t.Fragment,{children:[`Choose or upload a new asset to replace `,(0,t.jsx)(`code`,{children:e.name}),`. Every place it’s used in your document will show the new asset.`]})))(a):(0,t.jsxs)(t.Fragment,{children:[(0,t.jsxs)(`div`,{className:`pretext-plus-editor__dialog-header`,children:[(0,t.jsx)(`h2`,{className:`pretext-plus-editor__dialog-title`,children:`Assets`}),(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__dialog-close`,onClick:r,"aria-label":`Close`,children:`✕`})]}),Pe?(0,t.jsx)(`div`,{className:`pretext-plus-editor__dialog-content pretext-plus-editor__dialog-content--single`,children:Ke(Pe)}):(0,t.jsxs)(t.Fragment,{children:[(0,t.jsxs)(`div`,{className:`pretext-plus-editor__dialog-tab-bar`,children:[(0,t.jsxs)(`button`,{type:`button`,className:`pretext-plus-editor__dialog-tab${re===`in-document`?` pretext-plus-editor__dialog-tab--active`:``}`,onClick:()=>ie(`in-document`),children:[`Assets`,Re.length>0&&(0,t.jsx)(`span`,{className:`pretext-plus-editor__asset-tab-count`,children:Re.length})]}),(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__dialog-tab${re===`add`?` pretext-plus-editor__dialog-tab--active`:``}`,onClick:()=>{ie(`add`),oe(null)},children:`Add Asset`})]}),(0,t.jsx)(`div`,{className:`pretext-plus-editor__dialog-content pretext-plus-editor__dialog-content--single`,children:re===`in-document`?Ge():ae===null?V():ae===`image`?qe(!0):Je(!0)})]}),(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`})})]})})})},N5={automaticLayout:!0,minimap:{enabled:!1},wordWrap:`on`,insertSpaces:!0,tabSize:2,padding:{top:10,bottom:10},scrollBeyondLastLine:!1},P5=160,F5={image:`image`,doenet:`interactive`},I5=({asset:e,projectAssets:r,onClose:i,onSave:a,onReplace:o,onDuplicate:s})=>{let c=c6(e=>e.divisions),l=c6(e=>e.activeDivisionId),u=c?.find(e=>e.xmlId===l)?.sourceFormat??`pretext`,d=e.ref??``,[f,p]=(0,n.useState)(e.name),[m,h]=(0,n.useState)(d),[g,_]=(0,n.useState)(e.source??``),[v,y]=(0,n.useState)(!1),[b,x]=(0,n.useState)(!1),[S,C]=(0,n.useState)(null),[w,T]=(0,n.useState)(!1),[E,D]=(0,n.useState)(P5),O=(0,n.useRef)(null);(0,n.useEffect)(()=>{O.current?.scrollTo({top:0})},[]);let k=e=>{let t=()=>D(Math.max(P5,e.getContentHeight()));e.onDidContentSizeChange(t),t()},A=n5(e.kind,m.trim()||d,u),ee=()=>{navigator.clipboard.writeText(A).catch(()=>{}),T(!0),setTimeout(()=>T(!1),2e3)},te=async()=>{let t=m.trim();if(!t){C(`Reference can't be empty — it identifies the asset and is used by every embed of it.`);return}if(t!==d&&r.some(n=>n.kind===e.kind&&n.ref===t)){C(`Reference "${t}" is already used by another asset. Choose a unique reference.`);return}C(null),y(!0);try{await a({...e,name:f.trim()||t,ref:t,source:g},d),i()}catch(e){C(e instanceof Error?e.message:`Failed to save asset.`),y(!1)}},j=async()=>{if(s){C(null),x(!0);try{await s(e)}catch(e){C(e instanceof Error?e.message:`Failed to duplicate asset.`),x(!1)}}},M=v||b,ne=e.kind===`image`&&!!e.url,N=e.kind===`image`&&!!o;return(0,t.jsx)(`div`,{className:`pretext-plus-editor__dialog-overlay`,onClick:e=>e.target===e.currentTarget&&i(),children:(0,t.jsxs)(`div`,{className:`pretext-plus-editor__dialog`,role:`dialog`,"aria-modal":`true`,"aria-label":`Edit asset ${e.name}`,children:[(0,t.jsxs)(`div`,{className:`pretext-plus-editor__dialog-header`,children:[(0,t.jsx)(`h2`,{className:`pretext-plus-editor__dialog-title`,children:`Edit asset`}),(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__dialog-close`,onClick:i,"aria-label":`Close`,children:`✕`})]}),(0,t.jsx)(`div`,{ref:O,className:`pretext-plus-editor__dialog-content pretext-plus-editor__dialog-content--single pretext-plus-editor__am-edit-content`,children:(0,t.jsxs)(`div`,{className:`pretext-plus-editor__dialog-section`,children:[(0,t.jsxs)(`div`,{className:`pretext-plus-editor__am-edit-grid`,children:[(0,t.jsxs)(`div`,{className:`pretext-plus-editor__am-edit-col`,children:[ne&&(0,t.jsx)(`img`,{src:e.url,alt:f,className:`pretext-plus-editor__am-url-preview`,onError:e=>{e.currentTarget.style.display=`none`}}),N&&(0,t.jsx)(`div`,{className:`pretext-plus-editor__am-replace-row`,children:(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__am-action-btn`,onClick:()=>o?.(e),disabled:M,title:`Choose or upload a different asset to use here`,children:`Replace image…`})})]}),(0,t.jsxs)(`div`,{className:`pretext-plus-editor__am-edit-col`,children:[(0,t.jsx)(`label`,{className:`pretext-plus-editor__dialog-label`,htmlFor:`am-edit-name`,children:`Name`}),(0,t.jsx)(`input`,{id:`am-edit-name`,type:`text`,className:`pretext-plus-editor__am-input`,value:f,onChange:e=>p(e.target.value),disabled:M}),(0,t.jsx)(`label`,{className:`pretext-plus-editor__dialog-label`,htmlFor:`am-edit-ref`,children:`Reference`}),(0,t.jsx)(`input`,{id:`am-edit-ref`,type:`text`,className:`pretext-plus-editor__am-input`,value:m,onChange:e=>h(e.target.value),disabled:M}),(0,t.jsx)(`p`,{className:`pretext-plus-editor__dialog-helper-copy`,children:`Used in the embed code. Changing it updates every reference to this asset already in your document.`}),(0,t.jsxs)(`div`,{className:`pretext-plus-editor__am-embed-row`,children:[(0,t.jsx)(`code`,{className:`pretext-plus-editor__am-embed-code`,children:A}),(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__am-action-btn${w?` pretext-plus-editor__am-action-btn--done`:``}`,onClick:ee,title:`Copy embed code to clipboard`,children:w?`Copied!`:`Copy embed code`})]})]})]}),(0,t.jsx)(`label`,{className:`pretext-plus-editor__dialog-label`,children:`Asset content`}),(0,t.jsxs)(`p`,{className:`pretext-plus-editor__dialog-helper-copy`,children:[`Inserted verbatim inside the generated `,(0,t.jsx)(`code`,{children:`<${F5[e.kind]}>`}),` element — e.g. `,(0,t.jsx)(`code`,{children:`<shortdescription>...</shortdescription>`}),`.`]}),(0,t.jsx)(`div`,{className:`pretext-plus-editor__dialog-editor pretext-plus-editor__am-edit-editor`,style:{height:E},children:(0,t.jsx)(Yn,{options:{...N5,readOnly:M},height:`100%`,language:`xml`,value:g,onMount:k,onChange:e=>_(e??``)})}),S&&(0,t.jsx)(`p`,{className:`pretext-plus-editor__am-error`,children:S})]})}),(0,t.jsxs)(`div`,{className:`pretext-plus-editor__dialog-actions`,children:[s&&(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__dialog-button pretext-plus-editor__dialog-button--secondary pretext-plus-editor__am-duplicate-btn`,onClick:j,disabled:M,title:`Create a copy of this asset under a new reference`,children:b?`Duplicating…`:`Duplicate`}),(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__dialog-button pretext-plus-editor__dialog-button--secondary`,onClick:i,disabled:M,children:`Cancel`}),(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__dialog-button`,onClick:te,disabled:M,children:v?`Saving…`:`Save`})]})]})})},L5=e=>{let n=c6(e=>e.showFullPreview),r=c6(e=>e.setShowFullPreview),i=c6(e=>e.title),a=c6(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)(l6,{label:`Give feedback`,context:`main-editor`}),o]})]})},R5={pretext:`PreTeXt`,latex:`LaTeX`,markdown:`Markdown`},z5={book:`bk`,article:`art`,slideshow:`slides`,part:`pt`,chapter:`ch`,section:`sec`,subsection:`subsec`,subsubsection:`subsubsec`,introduction:`intro`,conclusion:`conc`,worksheet:`ws`,handout:`ho`,exercises:`ex`,references:`ref`,glossary:`gloss`,solutions:`sol`,"reading-questions":`rq`,paragraphs:`para`},B5={book:`Book`,article:`Article`,slideshow:`Slideshow`,part:`Part`,chapter:`Chapter`,section:`Section`,worksheet:`Worksheet`,handout:`Handout`,exercises:`Exercises`,references:`References`,glossary:`Glossary`,solutions:`Solutions`,"reading-questions":`Reading Questions`,introduction:`Introduction`,conclusion:`Conclusion`,subsection:`Subsection`,subsubsection:`Subsubsection`,paragraphs:`Paragraphs`},V5=[`part`,`chapter`,`section`,`subsection`,`subsubsection`,`worksheet`,`handout`,`exercises`,`references`,`glossary`,`solutions`,`reading-questions`,`paragraphs`],H5=[`worksheet`,`handout`,`exercises`,`references`,`glossary`,`solutions`,`reading-questions`,`paragraphs`],U5={book:[`part`,`chapter`],article:[`section`,...H5],slideshow:[`section`],part:[`chapter`],chapter:[`section`,...H5],section:[`subsection`,...H5],subsection:[`subsubsection`,...H5],subsubsection:[...H5],worksheet:[`paragraphs`],handout:[`paragraphs`]};function W5(e){return e?U5[e]??V5:V5}function G5(e,t){let n=z5[e]??`sec`,r=A8(t);return r?`${n}-${r}`:n}var K5=({draft:e,isNew:r=!1,isRoot:i=!1,parentType:a=null,onDraftChange:o,onCommit:s,onCancel:c})=>{let l=W5(a),u=(0,n.useRef)(r);return(0,n.useEffect)(()=>{i||l.length>0&&!l.includes(e.type)&&o({...e,type:l[0]})},[e,i,o,l]),(0,t.jsxs)(`div`,{className:`pretext-plus-editor__toc-edit-form`,children:[(0,t.jsxs)(`label`,{className:`pretext-plus-editor__toc-edit-field`,children:[(0,t.jsx)(`span`,{children:`Title`}),(0,t.jsx)(`input`,{type:`text`,value:e.title,onChange:t=>{let n=t.target.value;o(u.current?{...e,title:n,xmlId:G5(e.type,n)}:{...e,title:n})},onKeyDown:e=>{e.key===`Enter`&&s(),e.key===`Escape`&&c()},autoFocus:!0})]}),r?(0,t.jsxs)(`label`,{className:`pretext-plus-editor__toc-edit-field`,children:[(0,t.jsx)(`span`,{children:`Format`}),(0,t.jsx)(`select`,{value:e.sourceFormat,onChange:t=>o({...e,sourceFormat:t.target.value}),children:Object.keys(R5).map(e=>(0,t.jsx)(`option`,{value:e,children:R5[e]},e))})]}):void 0,!i&&(0,t.jsxs)(`label`,{className:`pretext-plus-editor__toc-edit-field`,children:[(0,t.jsx)(`span`,{children:`Type`}),(0,t.jsx)(`select`,{value:e.type,onChange:t=>{let n=t.target.value;o(u.current?{...e,type:n,xmlId:G5(n,e.title)}:{...e,type:n})},children:l.map(e=>(0,t.jsx)(`option`,{value:e,children:B5[e]},e))})]}),(0,t.jsxs)(`label`,{className:`pretext-plus-editor__toc-edit-field`,children:[(0,t.jsx)(`span`,{children:`Id`}),(0,t.jsx)(`input`,{type:`text`,value:e.xmlId,placeholder:`unique identifier`,onChange:t=>{u.current=!1,o({...e,xmlId:t.target.value})}})]}),(0,t.jsxs)(`div`,{className:`pretext-plus-editor__toc-edit-actions`,children:[(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__toc-edit-save`,onClick:s,children:`Save`}),(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__toc-edit-cancel`,onClick:c,children:`Cancel`})]})]})},q5=({items:e})=>{let[i,a]=(0,n.useState)(!1),[o,s]=(0,n.useState)(null),c=(0,n.useRef)(null),l=(0,n.useRef)(null);(0,n.useEffect)(()=>{if(!i)return;let e=e=>{let t=e.target;c.current&&!c.current.contains(t)&&l.current&&!l.current.contains(t)&&a(!1)},t=e=>{e.key===`Escape`&&a(!1)},n=()=>a(!1);return document.addEventListener(`mousedown`,e),document.addEventListener(`keydown`,t),window.addEventListener(`scroll`,n,!0),window.addEventListener(`resize`,n),()=>{document.removeEventListener(`mousedown`,e),document.removeEventListener(`keydown`,t),window.removeEventListener(`scroll`,n,!0),window.removeEventListener(`resize`,n)}},[i]),(0,n.useLayoutEffect)(()=>{if(!i)return;let e=c.current,t=l.current;if(!e||!t)return;let n=e.getBoundingClientRect(),r=t.getBoundingClientRect(),a=window.innerHeight-n.bottom,o=r.height+4>a&&n.top>a;s({right:Math.max(4,window.innerWidth-n.right),...o?{bottom:window.innerHeight-n.top+3}:{top:n.bottom+3}})},[i]);let u=e=>{a(!1),e()};return(0,t.jsxs)(`div`,{ref:c,className:`pretext-plus-editor__toc-div-menu`,children:[(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__toc-div-menu-trigger`,onClick:e=>{e.stopPropagation(),a(e=>(e||s(null),!e))},"aria-haspopup":`true`,"aria-expanded":i,title:`More options`,children:`⋮`}),i&&(0,r.createPortal)((0,t.jsx)(`div`,{ref:l,className:`pretext-plus-editor__toc-div-menu-popup pretext-plus-editor__toc-div-menu-popup--portal`,style:{visibility:o?`visible`:`hidden`,right:o?.right,top:o?.top,bottom:o?.bottom},children:e.map(e=>(0,t.jsx)(`button`,{type:`button`,className:[`pretext-plus-editor__toc-div-menu-item`,e.danger?`pretext-plus-editor__toc-div-menu-item--danger`:``].filter(Boolean).join(` `),onClick:()=>u(e.onClick),children:e.label},e.label))}),document.body)]})},J5=({division:e,depth:n,isActive:r,hasChildren:i,isExpanded:a,onToggleExpand:o,editDraft:s,onSelect:c,onDraftChange:l,onEditCommit:u,onEditCancel:d,menuItems:f,isNew:p=!1,isRoot:m=!1,parentType:h=null})=>{let g=s!==null,_=e.type===`introduction`||e.type===`conclusion`?B5[e.type]:null;return(0,t.jsxs)(`li`,{className:[`pretext-plus-editor__toc-item`,`pretext-plus-editor__toc-item--${e.type}`,r?`pretext-plus-editor__toc-item--active`:``,g?`pretext-plus-editor__toc-item--editing`:``].filter(Boolean).join(` `),children:[(0,t.jsxs)(`div`,{className:`pretext-plus-editor__toc-item-row`,style:n>0?{paddingLeft:`${n*14}px`}:void 0,children:[(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__toc-expand-btn`,onClick:o,"aria-label":a?`Collapse`:`Expand`,tabIndex:i?0:-1,style:{visibility:i?`visible`:`hidden`},children:a?`▾`:`▸`}),(0,t.jsxs)(`button`,{type:`button`,className:`pretext-plus-editor__toc-select`,onClick:c,"aria-current":r?`true`:void 0,title:B5[e.type]??e.type,children:[(0,t.jsx)(`span`,{className:`pretext-plus-editor__toc-title`,children:e.title||_||(0,t.jsx)(`em`,{children:`Untitled`})}),e.xmlId&&(0,t.jsx)(`span`,{className:`pretext-plus-editor__toc-xmlid`,children:e.xmlId})]}),(0,t.jsx)(`div`,{className:`pretext-plus-editor__toc-actions`,children:(0,t.jsx)(q5,{items:f})})]}),g&&s&&(0,t.jsx)(K5,{draft:s,isNew:p,isRoot:m,parentType:h,onDraftChange:l,onCommit:u,onCancel:d})]})},Y5=({onOpenAssetPicker:e})=>{let r=c6(e=>e.divisions),i=c6(e=>e.rootDivisionId),a=c6(e=>e.activeDivisionId),o=c6(e=>e.projectAssets)??[],s=c6(e=>e.selectSection),c=c6(e=>e.addSection),l=c6(e=>e.removeSection),u=c6(e=>e.divisionContentChange),d=c6(e=>e.insertAtCursor),f=c6(e=>e.openAssetEditor),p=c6(e=>e.openAssetResolver),m=c6(e=>e.removeAsset),h=c6(e=>e.removeAssetRefFromDocument),g=c6(e=>e.duplicateAsset),_=c6(e=>e.hasAssetDuplicate),v=c6(e=>e.startSectionEdit),y=c6(e=>e.setEditDraft),b=c6(e=>e.commitSectionEdit),x=c6(e=>e.cancelSectionEdit),S=c6(e=>e.editingId),C=c6(e=>e.editDraft),w=c6(e=>e.editingIsNew),T=r?r.find(e=>e.xmlId===i)??r.find(e=>e.type===`book`||e.type===`article`||e.type===`slideshow`)??r[0]??null:null,E=T&&r?p5(r,T.xmlId):[],D=T&&r?m5(r,T.xmlId):[],O=D5(r,o),k=A5.map(e=>({kind:e,rows:O.filter(t=>t.kind===e)})).filter(e=>e.rows.length>0),[A,ee]=(0,n.useState)(!1),[te,j]=(0,n.useState)(null),M=async e=>{if(!(!e.asset||te)){j(e.ref);try{await g(e.asset)}finally{j(null)}}},[ne,N]=(0,n.useState)(new Set),re=e=>!ne.has(e),ie=e=>{N(t=>{let n=new Set(t);return n.has(e)?n.delete(e):n.add(e),n})},[ae,oe]=(0,n.useState)(a);if(a!==ae&&(oe(a),a&&T)){let e=new Map(E.map(e=>[e.division.xmlId,e])),t=new Set;t.add(T.xmlId);let n=a;for(;n;){let r=e.get(n);if(!r?.parentXmlId)break;t.add(r.parentXmlId),n=r.parentXmlId}N(e=>{if([...t].every(t=>!e.has(t)))return e;let n=new Set(e);return t.forEach(e=>n.delete(e)),n})}let se=new Set(E.map(e=>e.parentXmlId).filter(Boolean)),P=[];if(T){let e=new Set;re(T.xmlId)&&e.add(T.xmlId);for(let t of E)t.parentXmlId&&e.has(t.parentXmlId)&&(P.push(t),re(t.division.xmlId)&&e.add(t.division.xmlId))}let ce=(e,t)=>{if(!r)return;let n=r.find(e=>e.xmlId===t);n&&u(n.xmlId,o5(n.content,e))},le=(e,t)=>{if(window.confirm(`Delete "${e.title||`Untitled`}"? This permanently removes the division.`)){if(t&&r){let n=r.find(e=>e.xmlId===t);n&&u(n.xmlId,o5(n.content,e.xmlId))}l(e.xmlId)}},ue=r?.find(e=>e.xmlId===a)?.sourceFormat??`pretext`,de=e=>{d(ue===`markdown`?`::${e.type}{ref="${e.xmlId}"}`:ue===`latex`?`\\plus{${e.type}}{${e.xmlId}}`:`<plus:${e.type} ref="${e.xmlId}"/>`)},F=e=>{T&&u(T.xmlId,a5(T.content,e.xmlId,e.type,null,T.sourceFormat))},fe=e=>e&&r?.find(t=>t.xmlId===e)?.type||null,pe=e=>e.status===`unlinked`?p(e.kind,e.ref):f(e.kind,e.ref),me=(e,t)=>{navigator.clipboard.writeText(n5(e,t,ue)).catch(()=>{})},he=e=>{let t=[{label:e.status===`unlinked`?`Link / create asset`:`Edit asset`,onClick:()=>pe(e)},{label:`Copy embed code`,onClick:()=>me(e.kind,e.ref)}];return _&&e.asset&&t.push({label:`Duplicate asset`,onClick:()=>M(e)}),e.status===`unlinked`?t.push({label:`Remove from document`,onClick:()=>h(e.kind,e.ref),danger:!0}):e.asset&&t.push({label:`Remove from project`,onClick:()=>{e.inDocument&&!window.confirm(`Remove "${e.asset.name}" from the project? This also deletes its ${e.inDocument?`reference(s)`:`reference`} from the document.`)||(m(e.asset),h(e.kind,e.ref))},danger:!0}),t};return(0,t.jsxs)(t.Fragment,{children:[(0,t.jsxs)(`ul`,{className:`pretext-plus-editor__toc-list`,role:`list`,children:[T&&(0,t.jsx)(J5,{division:T,depth:0,isActive:a===T.xmlId,hasChildren:se.has(T.xmlId),isExpanded:re(T.xmlId),onToggleExpand:()=>ie(T.xmlId),editDraft:S===T.xmlId?C:null,onSelect:()=>s(T.xmlId),onDraftChange:y,onEditCommit:b,onEditCancel:x,menuItems:[{label:`Edit properties`,onClick:()=>v(T)},...V8(T.sourceFormat)?[{label:`Add new division`,onClick:()=>c(T.xmlId)}]:[]],isNew:S===T.xmlId&&w,isRoot:!0}),P.length===0&&T&&re(T.xmlId)&&(0,t.jsx)(`li`,{className:`pretext-plus-editor__toc-no-sections`,children:(0,t.jsx)(`span`,{children:`No placed divisions`})}),P.map(e=>(0,t.jsx)(J5,{division:e.division,depth:e.depth+1,isActive:a===e.division.xmlId,hasChildren:se.has(e.division.xmlId),isExpanded:re(e.division.xmlId),onToggleExpand:()=>ie(e.division.xmlId),editDraft:S===e.division.xmlId?C:null,onSelect:()=>s(e.division.xmlId),onDraftChange:y,onEditCommit:b,onEditCancel:x,menuItems:[{label:`Edit properties`,onClick:()=>v(e.division)},...V8(e.division.sourceFormat)?[{label:`Add new division`,onClick:()=>c(e.division.xmlId)}]:[],{label:`Remove from document`,onClick:()=>ce(e.division.xmlId,e.parentXmlId)},{label:`Delete from project`,onClick:()=>le(e.division,e.parentXmlId),danger:!0}],isNew:S===e.division.xmlId&&w,parentType:fe(e.parentXmlId)},e.division.xmlId))]}),D.length>0&&(0,t.jsxs)(`div`,{className:`pretext-plus-editor__toc-orphans`,children:[(0,t.jsx)(`div`,{className:`pretext-plus-editor__toc-orphans-heading`,children:`Unplaced divisions`}),(0,t.jsx)(`ul`,{className:`pretext-plus-editor__toc-list`,children:D.map(e=>{let i=r?p5(r,e.xmlId):[],o=new Set(i.map(e=>e.parentXmlId).filter(Boolean));return(0,t.jsxs)(n.Fragment,{children:[(0,t.jsx)(J5,{division:e,depth:0,isActive:a===e.xmlId,hasChildren:o.has(e.xmlId),isExpanded:re(e.xmlId),onToggleExpand:()=>ie(e.xmlId),editDraft:S===e.xmlId?C:null,onSelect:()=>s(e.xmlId),onDraftChange:y,onEditCommit:b,onEditCancel:x,menuItems:[{label:`Edit properties`,onClick:()=>v(e)},{label:`Place in document`,onClick:()=>F(e)},{label:`Insert at cursor`,onClick:()=>de(e)},{label:`Delete from project`,onClick:()=>le(e,null),danger:!0}],parentType:null}),re(e.xmlId)&&i.map(e=>(0,t.jsx)(J5,{division:e.division,depth:e.depth+1,isActive:a===e.division.xmlId,hasChildren:o.has(e.division.xmlId),isExpanded:re(e.division.xmlId),onToggleExpand:()=>ie(e.division.xmlId),editDraft:S===e.division.xmlId?C:null,onSelect:()=>s(e.division.xmlId),onDraftChange:y,onEditCommit:b,onEditCancel:x,menuItems:[{label:`Edit properties`,onClick:()=>v(e.division)},{label:`Insert at cursor`,onClick:()=>de(e.division)},{label:`Delete from project`,onClick:()=>le(e.division,e.parentXmlId),danger:!0}],parentType:fe(e.parentXmlId)},e.division.xmlId))]},e.xmlId)})})]}),(0,t.jsxs)(`div`,{className:`pretext-plus-editor__toc-assets`,children:[(0,t.jsx)(`div`,{className:`pretext-plus-editor__toc-assets-header`,children:(0,t.jsxs)(`button`,{type:`button`,className:`pretext-plus-editor__toc-assets-toggle`,onClick:()=>ee(e=>!e),"aria-expanded":A,children:[(0,t.jsx)(`span`,{className:`pretext-plus-editor__toc-assets-chevron`,children:A?`▾`:`▸`}),(0,t.jsx)(`span`,{children:`Assets`}),O.length>0&&(0,t.jsx)(`span`,{className:`pretext-plus-editor__toc-assets-count`,children:O.length})]})}),A&&(0,t.jsx)(`div`,{className:`pretext-plus-editor__toc-assets-body`,children:O.length===0?(0,t.jsxs)(`p`,{className:`pretext-plus-editor__toc-assets-empty`,children:[`No assets in this project yet.`,` `,e&&(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__toc-assets-add-link`,onClick:e,children:`Add one`})]}):(0,t.jsx)(`div`,{className:`pretext-plus-editor__toc-assets-groups`,children:k.map(({kind:e,rows:n})=>(0,t.jsxs)(`div`,{children:[(0,t.jsx)(`div`,{className:`pretext-plus-editor__toc-assets-group-header`,children:k5[e]}),(0,t.jsx)(`ul`,{className:`pretext-plus-editor__toc-assets-list`,children:n.map(e=>(0,t.jsxs)(`li`,{className:[`pretext-plus-editor__toc-asset-item`,e.status===`unlinked`?`pretext-plus-editor__toc-asset-item--unlinked`:``,e.status===`unused`?`pretext-plus-editor__toc-asset-item--unused`:``,te===e.ref?`pretext-plus-editor__toc-asset-item--busy`:``].filter(Boolean).join(` `),children:[e.asset?.url?(0,t.jsx)(`img`,{src:e.asset.url,className:`pretext-plus-editor__toc-asset-img`,onClick:()=>pe(e)}):(0,t.jsx)(`span`,{className:`pretext-plus-editor__toc-asset-img pretext-plus-editor__toc-asset-img--placeholder`,onClick:()=>pe(e),title:e.status===`unlinked`?`No asset — click to link`:void 0,"aria-hidden":`true`,children:e.status===`unlinked`?`⚠`:`🖼`}),(0,t.jsxs)(`button`,{type:`button`,className:`pretext-plus-editor__toc-asset-name`,onClick:()=>pe(e),title:e.status===`unlinked`?`No asset for this reference — click to link or create one`:`Edit asset`,children:[(0,t.jsx)(`span`,{className:`pretext-plus-editor__toc-asset-label`,children:e.asset?.name??e.ref}),(0,t.jsx)(`span`,{className:`pretext-plus-editor__toc-asset-filename`,children:e.status===`unlinked`?`${e.ref} — needs asset`:e.status===`unused`?`${e.ref} — not placed`:e.ref})]}),(0,t.jsx)(`div`,{className:`pretext-plus-editor__toc-actions`,children:te===e.ref?(0,t.jsx)(`span`,{className:`pretext-plus-editor__toc-asset-spinner`,role:`status`,"aria-label":`Duplicating asset`,title:`Duplicating…`}):(0,t.jsx)(q5,{items:he(e)})})]},e.ref))})]},e))})})]}),e&&(0,t.jsx)(`button`,{type:`button`,className:`pretext-plus-editor__toc-assets-btn`,onClick:e,children:`Manage Assets`})]})},X5=({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)(Y5,{onOpenAssetPicker:r})]}),Z5=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`})]})}},Q5=(e,t)=>e.kind===t.kind&&e.ref===t.ref;function $5(e){let t=()=>{},n={cbs:{selectDivision:t,addDivision:t,removeDivision:t,updateDivision:t,divisionContentChange:t,handleDivisionContentChange:t,assetInsert:t,updateTitle:t}};return{store:i6()((t,r)=>({source:e.source,sourceFormat:e.sourceFormat,projectAssets:e.projectAssets,libraryAssets:void 0,title:e.title,docinfo:e.docinfo,commonDocinfo:e.commonDocinfo,useCommonDocinfo:e.useCommonDocinfo,projectType:e.projectType,projectUrl:void 0,divisions:e.divisions,rootDivisionId:void 0,activeDivisionId:e.activeDivisionId,canConvertToPretext:!0,activeEditorSource:e.source,hasFeedback:!1,hasAssetDuplicate:!1,isTocCollapsed:!1,showFullPreview:!0,isNarrowScreen:typeof window<`u`?window.innerWidth<800:!1,activeTab:`editor`,isLatexDialogOpen:!1,isConvertDialogOpen:!1,isDocinfoEditorOpen:!1,isAssetPickerOpen:!1,isFullSourceOpen:!1,editingId:null,editDraft:null,editingIsNew:!1,editingAssetRef:null,assetResolveTarget:null,syncState:e=>t(e),applyExternalUpdate:e=>t(e),setDivisionContent:(e,n)=>t(t=>{if(!t.divisions)return{};let r=!1,i=t.divisions.map(t=>t.xmlId===e&&t.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,n)=>{let{xmlId:r,label:i}=e.sourceFormat===`markdown`?R8(e.content)??{xmlId:e.xmlId,label:``}:e.sourceFormat===`latex`?{xmlId:x8(e.content)||e.xmlId,label:``}:O8(e.content);t({editingId:e.xmlId,editDraft:{title:e.title,type:e.type,xmlId:r,label:i,sourceFormat:e.sourceFormat},editingIsNew:n?.isNew??!1})},setEditDraft:e=>t({editDraft:e}),commitSectionEdit:()=>{let{editingId:e,editDraft:i,divisions:a}=r();if(e&&i){let t=(a??[]).find(t=>t.xmlId===e),r=null;if(t){let t=k8(i.xmlId);if(!t){window.alert(`xml:id can't be empty — it identifies the division and is used by references to it.`);return}if((a??[]).some(n=>n.xmlId!==e&&n.xmlId===t)){window.alert(`xml:id "${t}" is already used by another division. Choose a unique id.`);return}r=t}n.cbs.updateDivision(e,{title:i.title.trim()||void 0,type:i.type,xmlId:r,label:i.label.trim()||null,sourceFormat:i.sourceFormat})}t({editingId:null,editDraft:null,editingIsNew:!1})},cancelSectionEdit:()=>t({editingId:null,editDraft:null,editingIsNew:!1}),insertAsset:e=>n.cbs.assetInsert(e),insertAtCursor:e=>n.cbs.insertContentAtCursor?.(e),openAssetEditor:(e,n)=>t({editingAssetRef:{kind:e,ref:n}}),closeAssetEditor:()=>t({editingAssetRef:null}),openAssetResolver:(e,n)=>t({assetResolveTarget:{kind:e,ref:n}}),closeAssetResolver:()=>t({assetResolveTarget:null}),removeAsset:e=>n.cbs.assetRemove?.(e),removeAssetRefFromDocument:(e,t)=>n.cbs.assetRefRemove?.(e,t),duplicateAsset:async e=>{await n.cbs.assetDuplicate?.(e)},setProjectAssets:e=>t({projectAssets:e}),addAssetToPool:e=>t(t=>{let n=t.projectAssets??[];return n.some(t=>Q5(t,e))?{}:{projectAssets:[...n,e]}}),updateAssetInPool:e=>t(t=>{let n=t.projectAssets??[];return n.some(t=>Q5(t,e))?{projectAssets:n.map(t=>Q5(t,e)?e:t)}:{projectAssets:[...n,e]}}),renameAssetInPool:(e,n,r)=>t(t=>({projectAssets:[...(t.projectAssets??[]).filter(t=>!(t.kind===e&&t.ref===n)&&!Q5(t,r)),r]})),removeAssetFromPool:e=>t(t=>({projectAssets:(t.projectAssets??[]).filter(t=>!Q5(t,e))})),updateTitle:e=>n.cbs.updateTitle(e),feedbackSubmit:e=>n.cbs.feedbackSubmit?.(e)})),bindCallbacks:e=>{n.cbs=e}}}function e7({store:e,children:n}){return(0,t.jsx)(s6.Provider,{value:e,children:n})}var t7=(e,t)=>e.find(e=>t?e.xmlId===t:e.type===`book`||e.type===`article`||e.type===`slideshow`)??e[0]??null,n7=e=>{let[r]=(0,n.useState)(()=>{let t=t7(e.divisions,e.rootDivisionId),n=E5(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 $5({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)(e7,{store:r.store,children:(0,t.jsx)(r7,{...e,bindCallbacks:r.bindCallbacks})})},r7=e=>{let{bindCallbacks:r}=e,i=c6(e=>e.showFullPreview),a=c6(e=>e.isNarrowScreen),o=c6(e=>e.setIsNarrowScreen),s=c6(e=>e.activeTab),c=c6(e=>e.setActiveTab),l=c6(e=>e.isTocCollapsed),u=c6(e=>e.setIsTocCollapsed),d=c6(e=>e.isLatexDialogOpen),f=c6(e=>e.isConvertDialogOpen),p=c6(e=>e.isDocinfoEditorOpen),m=c6(e=>e.isAssetPickerOpen),h=c6(e=>e.isFullSourceOpen),g=c6(e=>e.editingAssetRef),_=c6(e=>e.openAssetEditor),v=c6(e=>e.closeAssetEditor),y=c6(e=>e.assetResolveTarget),b=c6(e=>e.closeAssetResolver),[x,S]=(0,n.useState)(null),C=c6(e=>e.openModal),w=c6(e=>e.closeModal),T=c6(e=>e.syncState),E=c6(e=>e.projectAssets),D=c6(e=>e.addAssetToPool),O=c6(e=>e.updateAssetInPool),k=c6(e=>e.renameAssetInPool),A=c6(e=>e.removeAssetFromPool),ee=g?E?.find(e=>e.kind===g.kind&&e.ref===g.ref):void 0,te=c6(e=>e.divisions),j=(0,n.useMemo)(()=>te??[],[te]),M=c6(e=>e.activeDivisionId),ne=c6(e=>e.title),N=c6(e=>e.docinfo),re=c6(e=>e.commonDocinfo),ie=c6(e=>e.useCommonDocinfo),ae=c6(e=>e.applyExternalUpdate),oe=c6(e=>e.setDivisionContent),se=c6(e=>e.patchDivision),P=c6(e=>e.addDivisionToPool),ce=c6(e=>e.removeDivisionFromPool),le=c6(e=>e.setActiveDivisionId),ue=c6(e=>e.startSectionEdit),de=c6(e=>e.setTitle),F=c6(e=>e.setDocinfo),fe=c6(e=>e.editingId),pe=c6(e=>e.editingIsNew),me=(0,n.useRef)(null),he=(0,n.useRef)(null),ge=(0,n.useRef)(!1);(0,n.useEffect)(()=>{fe&&pe?ge.current=!0:ge.current&&(ge.current=!1,he.current?.focus())},[fe,pe]);let I=t7(j,e.rootDivisionId),_e=j.find(e=>e.xmlId===M)??j[0]??null,ve=_e?.sourceFormat??`pretext`,ye=_e?.content??``,be=(t,n,r,i)=>{oe(t,n),e.onContentChange({xmlId:t,sourceContent:n,sourceFormat:r,pretextSource:r===`pretext`?n:void 0,...i})},xe=(t,n)=>{se(t,n),e.onDivisionUpdate?.(t,n)},Se=(e,t)=>{let n=j.find(t=>t.xmlId===e);if(!n){xe(e,t);return}let r;if(t.sourceFormat!==void 0&&t.sourceFormat!==n.sourceFormat){let e=t.type??n.type,i=t.title??n.title,a=t.xmlId||n.xmlId;r={...n,sourceFormat:t.sourceFormat,type:e,title:i,xmlId:a,content:i5(e,t.sourceFormat,i,a)}}else r=n.sourceFormat===`markdown`?z8(n,t):n.sourceFormat===`latex`?S8(n,t):M8(n,t);let i=r.xmlId;if(r.content!==n.content&&be(n.xmlId,r.content,r.sourceFormat),xe(e,{...t,xmlId:i}),i!==n.xmlId||r.type!==n.type){let e=l5(j,n.xmlId);if(e){let t=c5(e.content,n.xmlId,i,r.type);t!==e.content&&be(e.xmlId,t,e.sourceFormat)}}i!==n.xmlId&&we(i)},Ce=t=>{P(t),(e.onDivisionAdd?.(t))?.then(e=>se(t.xmlId,{id:e})).catch(()=>{})},L=t=>{ce(t),e.onDivisionRemove?.(t)},we=t=>{le(t),e.onDivisionSelect?.(t)},Te=(0,n.useMemo)(()=>{if(!_e||ve===`pretext`)return;if(ve===`latex`)return C8(_e)??void 0;let e=e6(ye,ve);return e.pretextError?void 0:e.pretextSource},[_e,ve,ye]),Ee=e=>{if(!_e)return;let t=e||``;if(ve===`pretext`){let e=j8(t);e&&e.xmlId!==_e.xmlId&&(t=M8({..._e,content:t,title:e.title,type:e.type},{xmlId:_e.xmlId,label:e.label||null}).content)}else if(ve===`markdown`){let e=R8(t);e&&e.xmlId!==_e.xmlId&&(t=z8({..._e,content:t},{xmlId:_e.xmlId}).content)}if(t===_e.content)return;if(be(_e.xmlId,t,ve),ve===`pretext`){let e=j8(t);if(e&&(xe(_e.xmlId,{title:e.title,type:e.type,label:e.label||null}),e.type!==_e.type)){let t=l5(j,_e.xmlId);if(t){let n=c5(t.content,_e.xmlId,_e.xmlId,e.type);n!==t.content&&be(t.xmlId,n,t.sourceFormat)}}}else if(ve===`markdown`){let e=R8(t);if(e&&(xe(_e.xmlId,{title:e.title,type:e.type,label:e.label||null}),e.type!==_e.type)){let t=l5(j,_e.xmlId);if(t){let n=c5(t.content,_e.xmlId,_e.xmlId,e.type);n!==t.content&&be(t.xmlId,n,t.sourceFormat)}}}else if(ve===`latex`){let e=b8(t);e!==null&&xe(_e.xmlId,{title:e})}let n=new Set(j.map(e=>e.xmlId));for(let{xmlId:e,type:r}of Q8(t,ve))n.has(e)||(Ce(r5(e,r,ve)),n.add(e))},R=e=>{we(e)},De=()=>{_e&&(u(!1),ue(_e))},Oe=e=>{let t=l8();if(Ce(t),e){let n=j.find(t=>t.xmlId===e);n&&be(n.xmlId,a5(n.content,t.xmlId,t.type,null,n.sourceFormat),n.sourceFormat)}le(t.xmlId),ue(t,{isNew:!0})},ke=e=>{D(e)},z=t=>{A(t),e.onAssetRemove?.(t)},Ae=!!(e.onAssetUpload&&e.onAssetFetchUrl&&e.onAssetUpdate),je=!!(e.onAssetRemove&&(e.onAssetUpload||e.onAssetAddFromLibrary)),Me=async t=>{if(!e.onAssetUpload||!e.onAssetFetchUrl||!t.ref||!t.url)return;let n=new Set(D5(j,E).map(e=>e.ref)),r=O5(t.ref,n),i=await e.onAssetFetchUrl(t.url),a=/\.[^./\\]+$/.exec(i.name)?.[0]??``,o=new File([i],`${r}${a}`,{type:i.type}),s={...await e.onAssetUpload(o),ref:r,name:`${t.name} (copy)`,source:t.source};await e.onAssetUpdate?.(s),D(s),_(s.kind,r)},Ne=async(t,n,r)=>{if(r)n.ref&&t.ref&&Pe(t.kind,t.ref,n.ref),D(n);else{let r={...n,ref:t.ref,name:t.name,source:t.source};await e.onAssetUpdate?.(r),O(r)}e.onAssetRemove?.(t),A(t)},Pe=(e,t,n)=>{if(t!==n)for(let r of j){let i=e5(r.content,e,t,n);i!==r.content&&be(r.xmlId,i,r.sourceFormat)}},Fe=(e,t)=>{for(let n of j){let r=t5(n.content,e,t);r!==n.content&&be(n.xmlId,r,n.sourceFormat)}};(0,n.useEffect)(()=>{let e=()=>o(window.innerWidth<800);return window.addEventListener(`resize`,e),()=>window.removeEventListener(`resize`,e)},[o]);let Ie=ve!==`pretext`;(0,n.useLayoutEffect)(()=>{r({selectDivision:R,addDivision:e=>Oe(e),removeDivision:e=>L(e),updateDivision:(e,t)=>Se(e,t),divisionContentChange:(e,t)=>{be(e,t,j.find(t=>t.xmlId===e)?.sourceFormat??`pretext`)},handleDivisionContentChange:Ee,assetInsert:ke,assetRemove:z,assetRefRemove:Fe,assetDuplicate:Ae?Me:void 0,insertContentAtCursor:e=>he.current?.insertAtCursor(e),updateTitle:t=>{de(t),e.onTitleChange?.(t)},feedbackSubmit:e.onFeedbackSubmit})}),(0,n.useEffect)(()=>{T({source:ye,sourceFormat:ve,libraryAssets:e.libraryAssets,projectType:e.projectType,projectUrl:e.projectUrl,rootDivisionId:I?.xmlId,canConvertToPretext:Te!==void 0,activeEditorSource:ye,hasFeedback:e.onFeedbackSubmit!==void 0,hasAssetDuplicate:Ae})});let Le=(0,n.useRef)({divisions:e.divisions,projectAssets:e.projectAssets,title:e.title,docinfo:e.docinfo,commonDocinfo:e.commonDocinfo,useCommonDocinfo:e.useCommonDocinfo,activeDivisionId:e.activeDivisionId});(0,n.useEffect)(()=>{let t=Le.current,n={},r=!1;if(e.projectAssets!==void 0&&e.projectAssets!==t.projectAssets&&(n.projectAssets=e.projectAssets,r=!0),e.divisions!==t.divisions){let t=t7(e.divisions,e.rootDivisionId),i=E5(e.divisions,t?.xmlId,e.projectType,e.title);n.divisions=i;let a=i.find(e=>e.xmlId===t?.xmlId)??t;(M==null||!e.divisions.some(e=>e.xmlId===M))&&(n.activeDivisionId=a?.xmlId??null),!e.title&&a?.title&&(n.title=a.title),r=!0}e.title!==void 0&&e.title!==t.title&&(n.title=e.title,r=!0),e.docinfo!==void 0&&e.docinfo!==t.docinfo&&(n.docinfo=e.docinfo,r=!0),e.commonDocinfo!==void 0&&e.commonDocinfo!==t.commonDocinfo&&(n.commonDocinfo=e.commonDocinfo,r=!0),e.useCommonDocinfo!==void 0&&e.useCommonDocinfo!==t.useCommonDocinfo&&(n.useCommonDocinfo=e.useCommonDocinfo,r=!0),e.activeDivisionId!==void 0&&e.activeDivisionId!==t.activeDivisionId&&(n.activeDivisionId=e.activeDivisionId,r=!0),r&&ae(n),Le.current={divisions:e.divisions,projectAssets:e.projectAssets,title:e.title,docinfo:e.docinfo,commonDocinfo:e.commonDocinfo,useCommonDocinfo:e.useCommonDocinfo,activeDivisionId:e.activeDivisionId}});let Re=(0,n.useRef)(null);(0,n.useEffect)(()=>{if(Re.current===e.divisions)return;Re.current=e.divisions;let t=t7(e.divisions,e.rootDivisionId),n=E5(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 ze=ie?re:N,Be=(0,n.useMemo)(()=>{if(!h||!I)return``;try{return S5(j,I.xmlId,ze,E??[])}catch(e){return`<!-- Unable to assemble document source: ${e instanceof Error?e.message:String(e)} -->`}},[h,I,j,ze,E]),Ve=_e?ve===`pretext`?b5(j,_e.xmlId,E):Te:void 0,He=_e&&Ve!==void 0?w5(_e.type,Ve,ze,_e.title):void 0,Ue=()=>me.current?.rebuild(),B=()=>{e.onSave?.(),me.current?.rebuild()},We=t=>{let n=t.ctrlKey||t.metaKey;n&&t.key===`Enter`&&e.onPreviewRebuild?(t.preventDefault(),Ue()):n&&t.key===`s`&&(t.preventDefault(),B())},Ge=()=>{if(!_e||!Te)return;let e=l8(),t={id:e.xmlId,xmlId:_e.xmlId,title:_e.title,type:_e.type,sourceFormat:ve,content:_e.content};Ce(ve===`markdown`?z8(t,{xmlId:e.xmlId}):S8(t,{xmlId:e.xmlId})),be(_e.xmlId,Te,`pretext`),xe(_e.xmlId,{sourceFormat:`pretext`})},Ke=(0,t.jsx)(Ws,{ref:he,content:ye,sourceFormat:ve,onChange:Ee,onRebuild:e.onPreviewRebuild?Ue:void 0,onSave:B,onOpenLatexImport:()=>C(`isLatexDialogOpen`),onOpenDocinfoEditor:()=>C(`isDocinfoEditorOpen`),onOpenConvertToPretext:Ie&&Te!==void 0?()=>C(`isConvertDialogOpen`):void 0,canConvertToPretext:Te!==void 0,onOpenAssets:e.projectAssets!==void 0&&ve===`pretext`?()=>C(`isAssetPickerOpen`):void 0,onShowFullSource:()=>C(`isFullSourceOpen`),onRequestWrapperEdit:De}),V;V=i&&e.onPreviewRebuild?(0,t.jsx)(lR,{ref:me,content:He||``,title:ne,onRebuild:e.onPreviewRebuild}):(0,t.jsx)(oR,{content:ye,canEdit:ve===`pretext`,editDisabledReason:ve===`markdown`?`Visual editing is not available for Markdown documents.`:ve===`latex`?`Visual editing is not available for LaTeX documents.`:``,onChange:e=>Ee(e)});let qe=(0,t.jsx)(X5,{isCollapsed:l,onToggleCollapse:()=>u(e=>!e),onOpenAssetPicker:e.projectAssets===void 0?void 0:()=>C(`isAssetPickerOpen`)}),Je=`pretext-plus-tab-editor`,Ye=`pretext-plus-tab-preview`,Xe=`pretext-plus-tabpanel`,Ze;return Ze=a?(0,t.jsxs)(`div`,{className:`pretext-plus-editor__tabs`,children:[qe,(0,t.jsxs)(`div`,{className:`pretext-plus-editor__tabs-main`,children:[(0,t.jsxs)(`div`,{className:`pretext-plus-editor__tab-list`,role:`tablist`,children:[(0,t.jsx)(`button`,{type:`button`,id:Je,role:`tab`,"aria-controls":Xe,"aria-selected":s===`editor`,tabIndex:s===`editor`?0:-1,className:`pretext-plus-editor__tab-button ${s===`editor`?`is-active`:``}`,onClick:()=>c(`editor`),children:`Editor`}),(0,t.jsx)(`button`,{type:`button`,id:Ye,role:`tab`,"aria-controls":Xe,"aria-selected":s===`preview`,tabIndex:s===`preview`?0:-1,className:`pretext-plus-editor__tab-button ${s===`preview`?`is-active`:``}`,onClick:()=>c(`preview`),children:`Preview`})]}),(0,t.jsx)(`div`,{id:Xe,className:`pretext-plus-editor__tab-panel`,role:`tabpanel`,"aria-labelledby":s===`editor`?Je:Ye,children:(0,t.jsx)(`div`,{style:{height:`100%`},children:s===`editor`?Ke:V})})]})]}):(0,t.jsxs)(`div`,{className:`pretext-plus-editor__sectioned-layout`,children:[qe,(0,t.jsxs)(gt,{orientation:`horizontal`,className:`pretext-plus-editor__splitter`,children:[(0,t.jsx)(yt,{className:`pretext-plus-editor__editor-panel`,children:Ke}),(0,t.jsx)(St,{className:`pretext-plus-editor__resize-handle`,children:(0,t.jsx)(`div`,{className:`pretext-plus-editor__resize-dots`})}),(0,t.jsx)(yt,{className:`pretext-plus-editor__preview-panel`,children:V})]})]}),(0,t.jsxs)(`div`,{className:`pretext-plus-editor`,onKeyDown:We,children:[(0,t.jsx)(L5,{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)(Z5,{resetKeys:[ye,M],children:Ze}),d?(0,t.jsx)(u6,{onClose:()=>w(`isLatexDialogOpen`)}):null,f&&_e&&Te?(0,t.jsx)(m6,{sourceContent:ye,sourceFormat:ve,pretextSource:Te,onConfirm:Ge,onClose:()=>w(`isConvertDialogOpen`)}):null,p?(0,t.jsx)(E6,{docinfo:N,showCommonDocinfoControls:!0,commonDocinfo:re,initialUseCommonDocinfo:ie,onClose:t=>{if(w(`isDocinfoEditorOpen`),t!==void 0){F({docinfo:t.docinfo,commonDocinfo:t.commonDocinfo,useCommonDocinfo:t.useCommonDocinfo}),e.onCommonDocinfoChange?.(t.commonDocinfo),e.onUseCommonDocinfoChange?.(t.useCommonDocinfo);let n=I??_e;be(n?.xmlId??``,n?.content??``,n?.sourceFormat??`pretext`,{docinfo:t.docinfo,commonDocinfo:t.commonDocinfo,useCommonDocinfo:t.useCommonDocinfo})}}}):null,(m||y||x)&&e.projectAssets!==void 0?(0,t.jsx)(M5,{open:m||!!y||!!x,resolveTarget:y,replaceTarget:x,onClose:()=>{w(`isAssetPickerOpen`),b(),S(null)},onLoadAssets:e.onLoadAssets,onLoadLibraryAssets:e.onLoadLibraryAssets,onAddFromLibrary:e.onAssetAddFromLibrary,onUpload:e.onAssetUpload,onFetchUrl:e.onAssetFetchUrl,onCreateDoenet:e.onCreateDoenet,onRemoveAsset:e.onAssetRemove?z:void 0,onDuplicateAsset:Ae?Me:void 0,onAssetAdded:ke,onResolveRef:Pe,onReplaceAsset:Ne}):null,h?(0,t.jsx)(O6,{source:Be,onClose:()=>w(`isFullSourceOpen`)}):null,ee?(0,t.jsx)(I5,{asset:ee,projectAssets:E??[],onClose:v,onReplace:je?e=>{v(),S(e)}:void 0,onDuplicate:Ae?e=>Me(e):void 0,onSave:async(t,n)=>{t.ref&&t.ref!==n?(Pe(t.kind,n,t.ref),k(t.kind,n,t)):O(t),await e.onAssetUpdate?.(t)}},`${ee.kind}:${ee.ref}`):null]})]})};e.CodeEditor=Ws,e.DocinfoEditor=E6,e.Editors=n7,e.FeedbackLink=n6,e.FullPreview=lR,e.VisualEditor=oR,e.assembleFullProjectSource=S5,e.assembleProjectSource=b5,e.buildDivisionTree=p5,e.convertLatexToPretext=Z3,e.createConclusion=d8,e.createIntroduction=u8,e.createLatexConclusion=E8,e.createLatexIntroduction=T8,e.createNewLatexSection=w8,e.createNewSection=l8,e.derivePretextContent=e6,e.detectSourceFormat=X3,e.ensureLatexSectionWrapper=_8,e.ensureSectionWrapper=m8,e.extractDivisionMetadata=j8,e.extractLatexDivisionTitle=b8,e.findDivisionParent=l5,e.getOrphanRoots=m5,e.getOrphanedDivisions=f5,e.getSectionAttributes=O8,e.insertDivisionRef=a5,e.mergeTwoSections=D8,e.moveDivisionRef=s5,e.parseDivisionRefs=Z8,e.postToIframe=cR,e.removeDivisionRef=o5,e.renameDivisionRef=c5,e.reorderDivisionRefs=u5,e.rewrapLatexSection=g8,e.rewrapSection=p8,e.stripLatexSectionWrapper=h8,e.stripSectionWrapper=f8,e.updateDivisionTitle=c8,e.updateLatexSectionTitle=y8,e.updateSectionMetadata=M8,e.wrapDivisionForPreview=w5});
1093
1093
  //# sourceMappingURL=index.js.map