diff --git a/index.js b/index.js index c493b42..ed589eb 100644 --- a/index.js +++ b/index.js @@ -9701,10 +9701,31 @@ var WCCodeMirror = class extends HTMLElement { return this.editor.getValue(); } set value(value) { - this.setValue(value); + if (this.__initialized) { + this.setValueForced(value); + } else { + this.__preInitValue = value; + } } constructor() { super(); + const observerConfig = { + childList: true, + characterData: true, + subtree: true + }; + const linkChanged = (e) => { + return e.type === "childList" && (Array.from(e.addedNodes).some((e2) => e2.tagName === "LINK") || Array.from(e.removedNodes).some((e2) => e2.tagName === "LINK")); + }; + this.__observer = new MutationObserver((mutationsList, observer) => { + if (mutationsList.some(linkChanged)) { + this.refreshStyles(); + } + this.lookupInnerScript((data) => { + this.value = data; + }); + }); + this.__observer.observe(this, observerConfig); this.__initialized = false; this.__element = null; this.editor = null; @@ -9727,14 +9748,9 @@ var WCCodeMirror = class extends HTMLElement { else if (readOnly !== "nocursor") readOnly = false; this.refreshStyles(); - let content = ""; - const innerScriptTag = this.querySelector("script"); - if (innerScriptTag) { - if (innerScriptTag.getAttribute("type") === "wc-content") { - content = WCCodeMirror.dedentText(innerScriptTag.innerHTML); - content = content.replace(/<(\/?script)(.*?)>/g, "<$1$2>"); - } - } + this.lookupInnerScript((data) => { + this.value = data; + }); let viewportMargin = codemirror_default.defaults.viewportMargin; if (this.hasAttribute("viewport-margin")) { const viewportMarginAttr = this.getAttribute("viewport-margin").toLowerCase(); @@ -9748,24 +9764,26 @@ var WCCodeMirror = class extends HTMLElement { viewportMargin }); if (this.hasAttribute("src")) { - this.setSrc(this.getAttribute("src")); - } else { - await new Promise((resolve) => setTimeout(resolve, 50)); - this.value = content; + this.setSrc(); } + await new Promise((resolve) => setTimeout(resolve, 50)); this.__initialized = true; + if (this.__preInitValue !== void 0) { + this.setValueForced(this.__preInitValue); + } } disconnectedCallback() { this.editor && this.editor.toTextArea(); this.editor = null; this.__initialized = false; + this.__observer.disconnect(); } async setSrc() { const src = this.getAttribute("src"); const contents = await this.fetchSrc(src); this.value = contents; } - async setValue(value) { + async setValueForced(value) { this.editor.swapDoc(codemirror_default.Doc(value, this.getAttribute("mode"))); this.editor.refresh(); } @@ -9790,6 +9808,16 @@ var WCCodeMirror = class extends HTMLElement { `; } + lookupInnerScript(callback) { + const innerScriptTag = this.querySelector("script"); + if (innerScriptTag) { + if (innerScriptTag.getAttribute("type") === "wc-content") { + let data = WCCodeMirror.dedentText(innerScriptTag.innerHTML); + data = data.replace(/<(\/?script)(.*?)>/g, "<$1$2>"); + callback(data); + } + } + } static dedentText(text) { const lines = text.split("\n"); if (lines[0] === "") diff --git a/index.min.js b/index.min.js index c2e4ed6..bee5ec1 100644 --- a/index.min.js +++ b/index.min.js @@ -371,8 +371,8 @@ div.CodeMirror-dragcursors { /* Help users use markselection to safely style text background */ span.CodeMirror-selectedtext { background: none; } -`;self.CodeMirror=ar;var ur=class extends HTMLElement{static get observedAttributes(){return["src","readonly","mode","theme"]}attributeChangedCallback(t,i,r){!this.__initialized||i!==r&&(t==="readonly"?this[t]=r!==null:this[t]=r)}get readonly(){return this.editor.getOption("readOnly")}set readonly(t){this.editor.setOption("readOnly",t)}get mode(){return this.editor.getOption("mode")}set mode(t){this.editor.setOption("mode",t)}get theme(){return this.editor.getOption("theme")}set theme(t){this.editor.setOption("theme",t)}get src(){return this.getAttribute("src")}set src(t){this.setAttribute("src",t),this.setSrc()}get value(){return this.editor.getValue()}set value(t){this.setValue(t)}constructor(){super();this.__initialized=!1,this.__element=null,this.editor=null}async connectedCallback(){let t=this.attachShadow({mode:"open"}),i=document.createElement("template"),r=document.createElement("style");r.innerHTML=Hs,i.innerHTML=ur.template(),t.appendChild(r),t.appendChild(i.content.cloneNode(!0)),this.style.display="block",this.__element=t.querySelector("textarea");let l=this.hasAttribute("mode")?this.getAttribute("mode"):"null",n=this.hasAttribute("theme")?this.getAttribute("theme"):"default",o=this.getAttribute("readonly");o===""?o=!0:o!=="nocursor"&&(o=!1),this.refreshStyles();let s="",a=this.querySelector("script");a&&a.getAttribute("type")==="wc-content"&&(s=ur.dedentText(a.innerHTML),s=s.replace(/<(\/?script)(.*?)>/g,"<$1$2>"));let u=ar.defaults.viewportMargin;if(this.hasAttribute("viewport-margin")){let f=this.getAttribute("viewport-margin").toLowerCase();u=f==="infinity"?Infinity:parseInt(f)}this.editor=ar.fromTextArea(this.__element,{lineNumbers:!0,readOnly:o,mode:l,theme:n,viewportMargin:u}),this.hasAttribute("src")?this.setSrc(this.getAttribute("src")):(await new Promise(f=>setTimeout(f,50)),this.value=s),this.__initialized=!0}disconnectedCallback(){this.editor&&this.editor.toTextArea(),this.editor=null,this.__initialized=!1}async setSrc(){let t=this.getAttribute("src"),i=await this.fetchSrc(t);this.value=i}async setValue(t){this.editor.swapDoc(ar.Doc(t,this.getAttribute("mode"))),this.editor.refresh()}async fetchSrc(t){return(await fetch(t)).text()}refreshStyles(){Array.from(this.shadowRoot.children).forEach(t=>{t.tagName==="LINK"&&t.getAttribute("rel")==="stylesheet"&&t.remove()}),Array.from(this.children).forEach(t=>{t.tagName==="LINK"&&t.getAttribute("rel")==="stylesheet"&&this.shadowRoot.appendChild(t.cloneNode(!0))})}static template(){return` +`;self.CodeMirror=ar;var ur=class extends HTMLElement{static get observedAttributes(){return["src","readonly","mode","theme"]}attributeChangedCallback(t,i,r){!this.__initialized||i!==r&&(t==="readonly"?this[t]=r!==null:this[t]=r)}get readonly(){return this.editor.getOption("readOnly")}set readonly(t){this.editor.setOption("readOnly",t)}get mode(){return this.editor.getOption("mode")}set mode(t){this.editor.setOption("mode",t)}get theme(){return this.editor.getOption("theme")}set theme(t){this.editor.setOption("theme",t)}get src(){return this.getAttribute("src")}set src(t){this.setAttribute("src",t),this.setSrc()}get value(){return this.editor.getValue()}set value(t){this.__initialized?this.setValueForced(t):this.__preInitValue=t}constructor(){super();let t={childList:!0,characterData:!0,subtree:!0},i=r=>r.type==="childList"&&(Array.from(r.addedNodes).some(l=>l.tagName==="LINK")||Array.from(r.removedNodes).some(l=>l.tagName==="LINK"));this.__observer=new MutationObserver((r,l)=>{r.some(i)&&this.refreshStyles(),this.lookupInnerScript(n=>{this.value=n})}),this.__observer.observe(this,t),this.__initialized=!1,this.__element=null,this.editor=null}async connectedCallback(){let t=this.attachShadow({mode:"open"}),i=document.createElement("template"),r=document.createElement("style");r.innerHTML=Hs,i.innerHTML=ur.template(),t.appendChild(r),t.appendChild(i.content.cloneNode(!0)),this.style.display="block",this.__element=t.querySelector("textarea");let l=this.hasAttribute("mode")?this.getAttribute("mode"):"null",n=this.hasAttribute("theme")?this.getAttribute("theme"):"default",o=this.getAttribute("readonly");o===""?o=!0:o!=="nocursor"&&(o=!1),this.refreshStyles(),this.lookupInnerScript(a=>{this.value=a});let s=ar.defaults.viewportMargin;if(this.hasAttribute("viewport-margin")){let a=this.getAttribute("viewport-margin").toLowerCase();s=a==="infinity"?Infinity:parseInt(a)}this.editor=ar.fromTextArea(this.__element,{lineNumbers:!0,readOnly:o,mode:l,theme:n,viewportMargin:s}),this.hasAttribute("src")&&this.setSrc(),await new Promise(a=>setTimeout(a,50)),this.__initialized=!0,this.__preInitValue!==void 0&&this.setValueForced(this.__preInitValue)}disconnectedCallback(){this.editor&&this.editor.toTextArea(),this.editor=null,this.__initialized=!1,this.__observer.disconnect()}async setSrc(){let t=this.getAttribute("src"),i=await this.fetchSrc(t);this.value=i}async setValueForced(t){this.editor.swapDoc(ar.Doc(t,this.getAttribute("mode"))),this.editor.refresh()}async fetchSrc(t){return(await fetch(t)).text()}refreshStyles(){Array.from(this.shadowRoot.children).forEach(t=>{t.tagName==="LINK"&&t.getAttribute("rel")==="stylesheet"&&t.remove()}),Array.from(this.children).forEach(t=>{t.tagName==="LINK"&&t.getAttribute("rel")==="stylesheet"&&this.shadowRoot.appendChild(t.cloneNode(!0))})}static template(){return` - `}static dedentText(t){let i=t.split(` + `}lookupInnerScript(t){let i=this.querySelector("script");if(i&&i.getAttribute("type")==="wc-content"){let r=ur.dedentText(i.innerHTML);r=r.replace(/<(\/?script)(.*?)>/g,"<$1$2>"),t(r)}}static dedentText(t){let i=t.split(` `);i[0]===""&&i.splice(0,1);let r=i[0],l=0,o=r[0]===" "?" ":" ";for(;r[l]===o;)l+=1;let s=[];for(let a of i){let u=a;for(let f=0;f