diff options
author | Hayleigh Thompson <me@hayleigh.dev> | 2024-06-13 23:53:36 +0100 |
---|---|---|
committer | Hayleigh Thompson <me@hayleigh.dev> | 2024-06-13 23:53:36 +0100 |
commit | 483ab48f3ecd34af451dd38235ed791d81436cd4 (patch) | |
tree | 1cb16c80c3d64e6883fadbd3821bb559f7263815 /priv | |
parent | 4081a0941fcf55ce60866d4dcd60fc6fc99e7e8b (diff) | |
download | lustre-483ab48f3ecd34af451dd38235ed791d81436cd4.tar.gz lustre-483ab48f3ecd34af451dd38235ed791d81436cd4.zip |
:rocket: Build server component runtime.
Diffstat (limited to 'priv')
-rw-r--r-- | priv/static/lustre-server-component.min.mjs | 2 | ||||
-rw-r--r-- | priv/static/lustre-server-component.mjs | 18 |
2 files changed, 18 insertions, 2 deletions
diff --git a/priv/static/lustre-server-component.min.mjs b/priv/static/lustre-server-component.min.mjs index 6393bf1..c0d3a1a 100644 --- a/priv/static/lustre-server-component.min.mjs +++ b/priv/static/lustre-server-component.min.mjs @@ -1 +1 @@ -function k(t,e,r,o=!1){let s,a=[{prev:t,next:e,parent:t.parentNode}];for(;a.length;){let{prev:n,next:i,parent:l}=a.pop();if(i.subtree!==void 0&&(i=i.subtree()),i.content!==void 0)if(n)if(n.nodeType===Node.TEXT_NODE)n.textContent!==i.content&&(n.textContent=i.content),s??=n;else{let c=document.createTextNode(i.content);l.replaceChild(c,n),s??=c}else{let c=document.createTextNode(i.content);l.appendChild(c),s??=c}else if(i.tag!==void 0){let c=D({prev:n,next:i,dispatch:r,stack:a,isComponent:o});n?n!==c&&l.replaceChild(c,n):l.appendChild(c),s??=c}else i.elements!==void 0?x(i,c=>{a.unshift({prev:n,next:c,parent:l}),n=n?.nextSibling}):i.subtree!==void 0&&a.push({prev:n,next:i,parent:l})}return s}function J(t,e,r){let o=t.parentNode;for(let s of e[0]){let a=s[0].split("-"),n=s[1],i=N(o,a),l;if(i!==null&&i!==o)l=k(i,n,r);else{let c=N(o,a.slice(0,-1)),u=document.createTextNode("");c.appendChild(u),l=k(u,n,r)}a==="0"&&(t=l)}for(let s of e[1]){let a=s[0].split("-");N(o,a).remove()}for(let s of e[2]){let a=s[0].split("-"),n=s[1],i=N(o,a),l=w.get(i);for(let c of n[0]){let u=c[0],m=c[1];if(u.startsWith("data-lustre-on-")){let b=u.slice(15),p=r(M);l.has(b)||el.addEventListener(b,y),l.set(b,p),el.setAttribute(u,m)}else i.setAttribute(u,m),i[u]=m}for(let c of n[1])if(c[0].startsWith("data-lustre-on-")){let u=c[0].slice(15);i.removeEventListener(u,y),l.delete(u)}else i.removeAttribute(c[0])}return t}function D({prev:t,next:e,dispatch:r,stack:o}){let s=e.namespace||"http://www.w3.org/1999/xhtml",a=t&&t.nodeType===Node.ELEMENT_NODE&&t.localName===e.tag&&t.namespaceURI===(e.namespace||"http://www.w3.org/1999/xhtml"),n=a?t:s?document.createElementNS(s,e.tag):document.createElement(e.tag),i;if(w.has(n))i=w.get(n);else{let d=new Map;w.set(n,d),i=d}let l=a?new Set(i.keys()):null,c=a?new Set(Array.from(t.attributes,d=>d.name)):null,u=null,m=null,b=null;for(let d of e.attrs){let f=d[0],h=d[1];if(d.as_property)n[f]!==h&&(n[f]=h),a&&c.delete(f);else if(f.startsWith("on")){let g=f.slice(2),v=r(h);i.has(g)||n.addEventListener(g,y),i.set(g,v),a&&l.delete(g)}else if(f.startsWith("data-lustre-on-")){let g=f.slice(15),v=r(M);i.has(g)||n.addEventListener(g,y),i.set(g,v),n.setAttribute(f,h)}else f==="class"?u=u===null?h:u+" "+h:f==="style"?m=m===null?h:m+h:f==="dangerous-unescaped-html"?b=h:(n.getAttribute(f)!==h&&n.setAttribute(f,h),(f==="value"||f==="selected")&&(n[f]=h),a&&c.delete(f))}if(u!==null&&(n.setAttribute("class",u),a&&c.delete("class")),m!==null&&(n.setAttribute("style",m),a&&c.delete("style")),a){for(let d of c)n.removeAttribute(d);for(let d of l)i.delete(d),n.removeEventListener(d,y)}if(e.key!==void 0&&e.key!=="")n.setAttribute("data-lustre-key",e.key);else if(b!==null)return n.innerHTML=b,n;let p=n.firstChild,A=null,T=null,C=null,E=e.children[Symbol.iterator]().next().value;a&&E!==void 0&&E.key!==void 0&&E.key!==""&&(A=new Set,T=L(t),C=L(e));for(let d of e.children)x(d,f=>{f.key!==void 0&&A!==null?p=W(p,f,n,o,C,T,A):(o.unshift({prev:p,next:f,parent:n}),p=p?.nextSibling)});for(;p;){let d=p.nextSibling;n.removeChild(p),p=d}return n}var w=new WeakMap;function y(t){let e=t.currentTarget;if(!w.has(e)){e.removeEventListener(t.type,y);return}let r=w.get(e);if(!r.has(t.type)){e.removeEventListener(t.type,y);return}r.get(t.type)(t)}function M(t){let e=t.currentTarget,r=e.getAttribute(`data-lustre-on-${t.type}`),o=JSON.parse(e.getAttribute("data-lustre-data")||"{}"),s=JSON.parse(e.getAttribute("data-lustre-include")||"[]");switch(t.type){case"input":case"change":s.push("target.value");break}return{tag:r,data:s.reduce((a,n)=>{let i=n.split(".");for(let l=0,c=a,u=t;l<i.length;l++)l===i.length-1?c[i[l]]=u[i[l]]:(c[i[l]]??={},u=u[i[l]],c=c[i[l]]);return a},{data:o})}}function L(t){let e=new Map;if(t)for(let r of t.children)x(r,o=>{let s=o?.key||o?.getAttribute?.("data-lustre-key");s&&e.set(s,o)});return e}function N(t,e){let r,o,s=t;for(;[r,...o]=e,r!==void 0;)s=s.childNodes.item(r),e=o;return s}function W(t,e,r,o,s,a,n){for(;t&&!s.has(t.getAttribute("data-lustre-key"));){let l=t.nextSibling;r.removeChild(t),t=l}if(a.size===0)return x(e,l=>{o.unshift({prev:t,next:l,parent:r}),t=t?.nextSibling}),t;if(n.has(e.key))return console.warn(`Duplicate key found in Lustre vnode: ${e.key}`),o.unshift({prev:null,next:e,parent:r}),t;n.add(e.key);let i=a.get(e.key);if(!i&&!t)return o.unshift({prev:null,next:e,parent:r}),t;if(!i&&t!==null){let l=document.createTextNode("");return r.insertBefore(l,t),o.unshift({prev:l,next:e,parent:r}),t}return!i||i===t?(o.unshift({prev:t,next:e,parent:r}),t=t?.nextSibling,t):(r.insertBefore(i,t),o.unshift({prev:i,next:e,parent:r}),t)}function x(t,e){if(t.elements!==void 0)for(let r of t.elements)e(r);else e(t)}var O=class extends HTMLElement{static get observedAttributes(){return["route"]}#n=null;#t=null;#e=null;constructor(){super(),this.#n=new MutationObserver(e=>{let r=[];for(let o of e)if(o.type==="attributes"){let{attributeName:s,oldValue:a}=o,n=this.getAttribute(s);if(a!==n)try{r.push([s,JSON.parse(n)])}catch{r.push([s,n])}}r.length&&this.#e?.send(JSON.stringify([5,r]))})}connectedCallback(){this.#t=document.createElement("div"),this.appendChild(this.#t)}attributeChangedCallback(e,r,o){switch(e){case"route":if(!o)this.#e?.close(),this.#e=null;else if(r!==o){let s=this.getAttribute("id"),a=o+(s?`?id=${s}`:""),n=window.location.protocol==="https:"?"wss":"ws";this.#e?.close(),this.#e=new WebSocket(`${n}://${window.location.host}${a}`),this.#e.addEventListener("message",i=>this.messageReceivedCallback(i))}}}messageReceivedCallback({data:e}){let[r,...o]=JSON.parse(e);switch(r){case 0:return this.diff(o);case 1:return this.emit(o);case 2:return this.init(o)}}init([e,r]){let o=[];for(let s of e)s in this?o.push([s,this[s]]):this.hasAttribute(s)&&o.push([s,this.getAttribute(s)]),Object.defineProperty(this,s,{get(){return this[`_${s}`]??this.getAttribute(s)},set(a){let n=this[s];typeof a=="string"?this.setAttribute(s,a):this[`_${s}`]=a,n!==a&&this.#e?.send(JSON.stringify([5,[[s,a]]]))}});this.#n.observe(this,{attributeFilter:e,attributeOldValue:!0,attributes:!0,characterData:!1,characterDataOldValue:!1,childList:!1,subtree:!1}),this.morph(r),o.length&&this.#e?.send(JSON.stringify([5,o]))}morph(e){this.#t=k(this.#t,e,r=>o=>{let s=r(o);this.#e?.send(JSON.stringify([4,s.tag,s.data]))})}diff([e]){this.#t=J(this.#t,e,r=>o=>{let s=r(o);this.#e?.send(JSON.stringify([4,s.tag,s.data]))})}emit([e,r]){this.dispatchEvent(new CustomEvent(e,{detail:r}))}disconnectedCallback(){this.#e?.close()}};window.customElements.define("lustre-server-component",O);export{O as LustreServerComponent}; +function N(t,e,i,r=!1){let s,a=[{prev:t,next:e,parent:t.parentNode}];for(;a.length;){let{prev:n,next:o,parent:l}=a.pop();if(o.subtree!==void 0&&(o=o.subtree()),o.content!==void 0)if(n)if(n.nodeType===Node.TEXT_NODE)n.textContent!==o.content&&(n.textContent=o.content),s??=n;else{let c=document.createTextNode(o.content);l.replaceChild(c,n),s??=c}else{let c=document.createTextNode(o.content);l.appendChild(c),s??=c}else if(o.tag!==void 0){let c=D({prev:n,next:o,dispatch:i,stack:a,isComponent:r});n?n!==c&&l.replaceChild(c,n):l.appendChild(c),s??=c}else o.elements!==void 0?k(o,c=>{a.unshift({prev:n,next:c,parent:l}),n=n?.nextSibling}):o.subtree!==void 0&&a.push({prev:n,next:o,parent:l})}return s}function J(t,e,i){let r=t.parentNode;for(let s of e[0]){let a=s[0].split("-"),n=s[1],o=w(r,a),l;if(o!==null&&o!==r)l=N(o,n,i);else{let c=w(r,a.slice(0,-1)),u=document.createTextNode("");c.appendChild(u),l=N(u,n,i)}a==="0"&&(t=l)}for(let s of e[1]){let a=s[0].split("-");w(r,a).remove()}for(let s of e[2]){let a=s[0].split("-"),n=s[1],o=w(r,a),l=S.get(o);for(let c of n[0]){let u=c[0],m=c[1];if(u.startsWith("data-lustre-on-")){let y=u.slice(15),p=i(M);l.has(y)||el.addEventListener(y,b),l.set(y,p),el.setAttribute(u,m)}else o.setAttribute(u,m),o[u]=m}for(let c of n[1])if(c[0].startsWith("data-lustre-on-")){let u=c[0].slice(15);o.removeEventListener(u,b),l.delete(u)}else o.removeAttribute(c[0])}return t}function D({prev:t,next:e,dispatch:i,stack:r}){let s=e.namespace||"http://www.w3.org/1999/xhtml",a=t&&t.nodeType===Node.ELEMENT_NODE&&t.localName===e.tag&&t.namespaceURI===(e.namespace||"http://www.w3.org/1999/xhtml"),n=a?t:s?document.createElementNS(s,e.tag):document.createElement(e.tag),o;if(S.has(n))o=S.get(n);else{let f=new Map;S.set(n,f),o=f}let l=a?new Set(o.keys()):null,c=a?new Set(Array.from(t.attributes,f=>f.name)):null,u=null,m=null,y=null;for(let f of e.attrs){let d=f[0],h=f[1];if(f.as_property)n[d]!==h&&(n[d]=h),a&&c.delete(d);else if(d.startsWith("on")){let g=d.slice(2),E=i(h);o.has(g)||n.addEventListener(g,b),o.set(g,E),a&&l.delete(g)}else if(d.startsWith("data-lustre-on-")){let g=d.slice(15),E=i(M);o.has(g)||n.addEventListener(g,b),o.set(g,E),n.setAttribute(d,h)}else d==="class"?u=u===null?h:u+" "+h:d==="style"?m=m===null?h:m+h:d==="dangerous-unescaped-html"?y=h:(n.getAttribute(d)!==h&&n.setAttribute(d,h),(d==="value"||d==="selected")&&(n[d]=h),a&&c.delete(d))}if(u!==null&&(n.setAttribute("class",u),a&&c.delete("class")),m!==null&&(n.setAttribute("style",m),a&&c.delete("style")),a){for(let f of c)n.removeAttribute(f);for(let f of l)o.delete(f),n.removeEventListener(f,b)}if(e.key!==void 0&&e.key!=="")n.setAttribute("data-lustre-key",e.key);else if(y!==null)return n.innerHTML=y,n;let p=n.firstChild,x=null,C=null,O=null,A=e.children[Symbol.iterator]().next().value;a&&A!==void 0&&A.key!==void 0&&A.key!==""&&(x=new Set,C=L(t),O=L(e));for(let f of e.children)k(f,d=>{d.key!==void 0&&x!==null?p=W(p,d,n,r,O,C,x):(r.unshift({prev:p,next:d,parent:n}),p=p?.nextSibling)});for(;p;){let f=p.nextSibling;n.removeChild(p),p=f}return n}var S=new WeakMap;function b(t){let e=t.currentTarget;if(!S.has(e)){e.removeEventListener(t.type,b);return}let i=S.get(e);if(!i.has(t.type)){e.removeEventListener(t.type,b);return}i.get(t.type)(t)}function M(t){let e=t.currentTarget,i=e.getAttribute(`data-lustre-on-${t.type}`),r=JSON.parse(e.getAttribute("data-lustre-data")||"{}"),s=JSON.parse(e.getAttribute("data-lustre-include")||"[]");switch(t.type){case"input":case"change":s.push("target.value");break}return{tag:i,data:s.reduce((a,n)=>{let o=n.split(".");for(let l=0,c=a,u=t;l<o.length;l++)l===o.length-1?c[o[l]]=u[o[l]]:(c[o[l]]??={},u=u[o[l]],c=c[o[l]]);return a},{data:r})}}function L(t){let e=new Map;if(t)for(let i of t.children)k(i,r=>{let s=r?.key||r?.getAttribute?.("data-lustre-key");s&&e.set(s,r)});return e}function w(t,e){let i,r,s=t;for(;[i,...r]=e,i!==void 0;)s=s.childNodes.item(i),e=r;return s}function W(t,e,i,r,s,a,n){for(;t&&!s.has(t.getAttribute("data-lustre-key"));){let l=t.nextSibling;i.removeChild(t),t=l}if(a.size===0)return k(e,l=>{r.unshift({prev:t,next:l,parent:i}),t=t?.nextSibling}),t;if(n.has(e.key))return console.warn(`Duplicate key found in Lustre vnode: ${e.key}`),r.unshift({prev:null,next:e,parent:i}),t;n.add(e.key);let o=a.get(e.key);if(!o&&!t)return r.unshift({prev:null,next:e,parent:i}),t;if(!o&&t!==null){let l=document.createTextNode("");return i.insertBefore(l,t),r.unshift({prev:l,next:e,parent:i}),t}return!o||o===t?(r.unshift({prev:t,next:e,parent:i}),t=t?.nextSibling,t):(i.insertBefore(o,t),r.unshift({prev:o,next:e,parent:i}),t)}function k(t,e){if(t.elements!==void 0)for(let i of t.elements)e(i);else e(t)}var T=class extends HTMLElement{static get observedAttributes(){return["route"]}#s=null;#t=null;#e=null;#n=null;constructor(){super(),this.#n=this.attachShadow({mode:"closed"}),this.#s=new MutationObserver(e=>{let i=[];for(let r of e)if(r.type==="attributes"){let{attributeName:s,oldValue:a}=r,n=this.getAttribute(s);if(a!==n)try{i.push([s,JSON.parse(n)])}catch{i.push([s,n])}}i.length&&this.#e?.send(JSON.stringify([5,i]))})}connectedCallback(){this.#t=document.createElement("div"),this.#n.appendChild(this.#t);let e=new CSSStyleSheet;for(let{cssRules:i}of document.styleSheets)for(let r of i)console.log(r),e.insertRule(r.cssText);this.#n.adoptedStyleSheets=[e]}attributeChangedCallback(e,i,r){switch(e){case"route":if(!r)this.#e?.close(),this.#e=null;else if(i!==r){let s=this.getAttribute("id"),a=r+(s?`?id=${s}`:""),n=window.location.protocol==="https:"?"wss":"ws";this.#e?.close(),this.#e=new WebSocket(`${n}://${window.location.host}${a}`),this.#e.addEventListener("message",o=>this.messageReceivedCallback(o))}}}messageReceivedCallback({data:e}){let[i,...r]=JSON.parse(e);switch(i){case 0:return this.diff(r);case 1:return this.emit(r);case 2:return this.init(r)}}init([e,i]){let r=[];for(let s of e)s in this?r.push([s,this[s]]):this.hasAttribute(s)&&r.push([s,this.getAttribute(s)]),Object.defineProperty(this,s,{get(){return this[`_${s}`]??this.getAttribute(s)},set(a){let n=this[s];typeof a=="string"?this.setAttribute(s,a):this[`_${s}`]=a,n!==a&&this.#e?.send(JSON.stringify([5,[[s,a]]]))}});this.#s.observe(this,{attributeFilter:e,attributeOldValue:!0,attributes:!0,characterData:!1,characterDataOldValue:!1,childList:!1,subtree:!1}),this.morph(i),r.length&&this.#e?.send(JSON.stringify([5,r]))}morph(e){this.#t=N(this.#t,e,i=>r=>{let s=i(r);this.#e?.send(JSON.stringify([4,s.tag,s.data]))})}diff([e]){this.#t=J(this.#t,e,i=>r=>{let s=i(r);this.#e?.send(JSON.stringify([4,s.tag,s.data]))})}emit([e,i]){this.dispatchEvent(new CustomEvent(e,{detail:i}))}disconnectedCallback(){this.#e?.close()}get adoptedStyleSheets(){return this.#n.adoptedStyleSheets}set adoptedStyleSheets(e){this.#n.adoptedStyleSheets=e}};window.customElements.define("lustre-server-component",T);export{T as LustreServerComponent}; diff --git a/priv/static/lustre-server-component.mjs b/priv/static/lustre-server-component.mjs index 8913469..0892fb5 100644 --- a/priv/static/lustre-server-component.mjs +++ b/priv/static/lustre-server-component.mjs @@ -353,8 +353,10 @@ var LustreServerComponent = class extends HTMLElement { #observer = null; #root = null; #socket = null; + #shadow = null; constructor() { super(); + this.#shadow = this.attachShadow({ mode: "closed" }); this.#observer = new MutationObserver((mutations) => { const changed = []; for (const mutation of mutations) { @@ -377,7 +379,15 @@ var LustreServerComponent = class extends HTMLElement { } connectedCallback() { this.#root = document.createElement("div"); - this.appendChild(this.#root); + this.#shadow.appendChild(this.#root); + const sheet = new CSSStyleSheet(); + for (const { cssRules } of document.styleSheets) { + for (const rule of cssRules) { + console.log(rule); + sheet.insertRule(rule.cssText); + } + } + this.#shadow.adoptedStyleSheets = [sheet]; } attributeChangedCallback(name, prev, next) { switch (name) { @@ -471,6 +481,12 @@ var LustreServerComponent = class extends HTMLElement { disconnectedCallback() { this.#socket?.close(); } + get adoptedStyleSheets() { + return this.#shadow.adoptedStyleSheets; + } + set adoptedStyleSheets(value) { + this.#shadow.adoptedStyleSheets = value; + } }; window.customElements.define("lustre-server-component", LustreServerComponent); export { |