mosaic 1.0.5 → 1.0.6

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/cjs/render.js CHANGED
@@ -103,19 +103,15 @@ const render = (
103
103
  */
104
104
 
105
105
  node.addEventListener(eventType, (event) => {
106
- let isGlobal = actionType.startsWith("$");
107
-
108
106
  let action = {
109
107
  type: actionType,
110
108
  event,
109
+ context: context ? context.wrap(getState()) : getState(),
111
110
  };
112
111
 
113
- if (!isGlobal)
114
- action.context = context ? context.wrap(getState()) : getState();
115
-
116
112
  dispatch(action);
117
113
 
118
- if (isGlobal) {
114
+ if (actionType.startsWith("$")) {
119
115
  node.dispatchEvent(
120
116
  new CustomEvent(actionType, {
121
117
  detail: action,
package/dist/mosaic.js CHANGED
@@ -676,19 +676,15 @@ const render = (
676
676
  */
677
677
 
678
678
  node.addEventListener(eventType, (event) => {
679
- let isGlobal = actionType.startsWith("$");
680
-
681
679
  let action = {
682
680
  type: actionType,
683
681
  event,
682
+ context: context ? context.wrap(getState()) : getState(),
684
683
  };
685
684
 
686
- if (!isGlobal)
687
- action.context = context ? context.wrap(getState()) : getState();
688
-
689
685
  dispatch(action);
690
686
 
691
- if (isGlobal) {
687
+ if (actionType.startsWith("$")) {
692
688
  node.dispatchEvent(
693
689
  new CustomEvent(actionType, {
694
690
  detail: action,
@@ -1 +1 @@
1
- const e=(e=[])=>e[e.length-1],t=e=>e.nodeType===e.TEXT_NODE&&""===e.nodeValue.trim(),n=(e,r,i=!0)=>{if(e){if(!t(e)){let t=r(e);if(!1===t)return;if(t?.nodeName)return n(t,r,i)}i&&n(e.firstChild,r,i),n(e.nextSibling,r,i)}},r=(t,n)=>{let[i,a]=((t,n)=>{let i=((e="")=>e.split(/(\[[^\]]+\])/).filter((e=>e)).reduce(((e,t)=>e+("["===t.charAt(0)?"."+t.replace(/\./g,":"):t)),""))(t).split(".").map((e=>{if("["===e.charAt(0)){let t=e.slice(1,-1).replace(/:/g,".");return r(t,n)}return e}));return[i.slice(0,-1).reduce(((e,t)=>e&&e[t]),n)||n,e(i)]})(t,n),l=i?.[a];return"function"==typeof l?l.bind(i):l},i=e=>{if("string"==typeof e){let t=document.createElement("template");return t.innerHTML=e.trim(),t.content}return"TEMPLATE"===e.nodeName?e.cloneNode(!0).content:"defs"===e.nodeName?e.firstElementChild.cloneNode(!0):void 0},a=e=>null===e||"object"!=typeof e,l=e=>e.replace(/[\w]([A-Z])/g,(function(e){return e[0]+"-"+e[1].toLowerCase()})),c=(e,t,n)=>{t=l(t),"boolean"==typeof n&&(t.startsWith("aria-")?n=""+n:n&&(n="")),"string"==typeof n||"number"==typeof n?e.setAttribute(t,n):e.removeAttribute(t)},s=(e,t)=>{let n=e.replace(/[\w]-([\w])/g,(function(e){return e[0]+e[2].toUpperCase()}));return""===t&&(t=!0),e.startsWith("aria-")&&("true"===t&&(t=!0),"false"===t&&(t=!1)),{name:n,value:t}};const o=e=>{let t=0;return n(e.content?.firstChild||e.firstChild,(()=>t++),!1),t},u=e=>t(e.nextSibling)?u(e.nextSibling):e.nextSibling;const d=(e,t,n,r)=>{let i=e.getAttribute("length")||0,a=((e,t)=>{let n=o(e),r=[];if(t)for(;t--;){let t=document.createDocumentFragment(),i=n;for(;i--;)t.appendChild(u(e));r.push(t)}return r})(e,i),l=e;t.forEach(((e,t)=>{let i=-1===e?r(n[t][1],n[t][0]):a[e],c=(s=i).nodeType===s.DOCUMENT_FRAGMENT_NODE&&s.lastChild||s;var s;l.after(i),l=c})),e.setAttribute("length",t.length)},p=e=>e.trim().split(/({{[^{}]+}})/).filter((e=>e)).map((e=>{let t=e.match(/{{([^{}]+)}}/);if(!t)return{type:1,value:e,negated:!1};let n="!"===(e=t[1].trim()).charAt(0);return n&&(e=e.slice(1)),{type:2,value:e,negated:n}})),h=(e,t)=>t.reduce(((t,n)=>{let i,{type:a,value:l,negated:c}=n;if(1===a&&(i=l),2===a&&(i=r(l,e)),3===a){let t=n.args.map((t=>r(t,e)));i=r(n.method,e)?.(...t)}return c&&(i=!i),t||0===t?t+i:i}),""),f=e=>e.replace(/[\w]([A-Z])/g,(function(e){return e[0]+"-"+e[1].toLowerCase()})),m=e=>{let t=typeof e;return"string"===t?e.split(";").reduce(((e,t)=>{const[n,r]=t.split(":").map((e=>e.trim()));return n&&(e[n]=r),e}),{}):"object"===t?e:{}},b=(e,t,n)=>{if("style"===t)n=(e=>Object.entries(e).map((([e,t])=>`${e}: ${t};`)).join(" "))((l={...m(e.getAttribute("style")),...m(n)},Object.keys(l).reduce(((e,t)=>(e[f(t)]=l[t],e)),{})));else if("class"===t)switch(i=n,Object.prototype.toString.call(i).match(/\s(.+[^\]])/)[1]){case"Array":n=n.join(" ");break;case"Object":n=Object.keys(n).reduce(((e,t)=>(n[t]&&e.push(t),e)),[]).join(" ")}else if(!a(n))return e[(r=t,r.replace(/[\w]-([\w])/g,(function(e){return e[0]+e[2].toUpperCase()})))]=n;var r,i,l;t=f(t),"boolean"==typeof n&&(t.startsWith("aria-")?n=""+n:n&&(n=""));let c=e.getAttribute(t);n!==c&&("string"==typeof n||"number"==typeof n?e.setAttribute(t,n):e.removeAttribute(t))},y=(e=[])=>{let t=e;return{get:()=>t,push:e=>t.push(e),wrap:e=>t.reduce(((e,t)=>new Proxy(e,(({path:e,identifier:t,key:n,index:i,i:l,k:c})=>({get(a,s){let o=r(e,a);if(s===t)for(let e in o){let t=o[e];if(n){if(t[n]===c)return t}else if(e==l)return t}if(s===i)for(let e in o){let t=o[e];if(n){if(t[n]===c)return e}else if(e==l)return e}let u=n?o.find((e=>e[n]===c)):o[l];return u?.hasOwnProperty?.(s)?u[s]:Reflect.get(...arguments)},set(t,i,s){let o=r(e,t),u=n?o.find((e=>e[n]===c)):o[l];return u&&!a(u)?(u[i]=s,!0):Reflect.set(...arguments)}}))(t))),e)}},g=(t,{getState:a,dispatch:l},c,s,f)=>{const m={1:({value:e,node:t,context:n},{getState:r})=>({handler:()=>{let i=n?n.wrap(r()):r(),a=t.textContent,l=h(i,p(e));a!==l&&(t.textContent=l)}}),2:({value:e,node:t,name:n,context:i},{getState:a})=>({handler:()=>{let l=i?i.wrap(a()):a(),c=h(l,p(e));if(b(t,n,c),"OPTION"===t.nodeName){let e=t.parentNode.getAttribute("name"),n=r(e,l);t.selected=n===c}}}),3:({node:e,path:t,context:n},{getState:i,dispatch:a})=>(e.addEventListener("input",(()=>{let r="checkbox"===e.getAttribute("type")?e.checked:e.value;if(r.trim?.().length&&!isNaN(r)&&(r=+r),n){let e=n.wrap(i());e[t]=r,a({type:"MERGE",payload:e})}else a({type:"SET",payload:{name:t,value:r,context:n}})})),{handler:()=>{let a=n?n.wrap(i()):i();((e,t)=>{if("SELECT"===e.nodeName)return void Array.from(e.querySelectorAll("option")).forEach((e=>{e.selected=t.includes(e.value)}));let n;switch(e.getAttribute("type")){case"checkbox":if(n=t,e.checked===n)break;n?e.setAttribute("checked",""):e.removeAttribute("checked");break;case"radio":if(n=t===e.getAttribute("value"),e.checked===n)break;e.checked=n,n?e.setAttribute("checked",""):e.removeAttribute("checked");break;default:if(e.value===t)break;e.setAttribute("value",e.value=t||"")}})(e,r(t,a))}}),4:({node:e,eventType:t,actionType:n,context:r},{dispatch:i,getState:a})=>(e.addEventListener(t,(t=>{let l=n.startsWith("$"),c={type:n,event:t};l||(c.context=r?r.wrap(a()):a()),i(c),l&&e.dispatchEvent(new CustomEvent(n,{detail:c,bubbles:!0}))})),{handler:()=>{}}),5:({node:t,context:a,map:l,path:c,identifier:s,index:p,key:h,blockIndex:f,hydrate:m,pickupNode:b},{getState:g})=>{let E;t.$t=f-1;const A=(e,t,r,i)=>{n(e,k((e=>{if(e===i)return!1}),T(l,m,y((a?.get()||[]).concat({path:c,identifier:s,key:h,index:p,i:t,k:r}))),(e=>e.$t=f)))};const v=(e,n)=>{let r=e[h],a=i(t);var l;return A((l=a).nodeType===l.DOCUMENT_FRAGMENT_NODE&&l.firstChild||l,n,r),a};if(m){let n=r(c,g()),i=(e=>{let t=e.getAttribute("length"),n=o(e),r=[],i=e;if(t)for(;t--;){let e=[],t=n;for(;t--;)i=u(i),e.push(i);r.push(e)}return r})(t);i.forEach(((t,r)=>{let i=n[r]?.[h];A(t[0],r,i,e(t).nextSibling)})),b=e(e(i)).nextSibling}return{handler:()=>{let e=a?a.wrap(g()):g();const n=Object.entries(r(c,e)||[]),i=((e,t=[],n=[])=>{let r=n.map((([n,r])=>e?t.findIndex((([t,n])=>n[e]===r[e])):n in t?n:-1));if(t.length!==n.length||!r.every(((e,t)=>e===t)))return r})(h,E,n);i&&d(t,i,n,v),E=n.slice(0)},pickupNode:b}}},{bind:g,update:E}=(()=>{const e=(()=>{let e=new Set;return{publish:t=>{for(let t of e)t();t?.()},subscribe(t){e.add(t)}}})();return{bind(t){let n=m[t.type](t,{getState:a,dispatch:l});return e.subscribe(n.handler),n},update:t=>e.publish(t)}})();let A=0;const v=e=>{let t=0,r={};return n(e,(e=>{let n,i=[];switch(e.nodeType){case e.TEXT_NODE:{let t=e.textContent;t.match(/({{[^{}]+}})/)&&i.push({type:1,value:t});break}case e.ELEMENT_NODE:{let t=function(e){let t=e.getAttribute("each"),n=t?.match(/(.+)\s+in\s+(.+)/);if(!n)return t?{path:t.trim(),key:e.getAttribute("key")}:n;let[r,i,a]=n,l=i.match(/\(([^\)]+)\)/),[c,s]=(l?l[1].split(","):[i]).map((e=>e.trim()));return{path:a.trim(),identifier:s||c,index:s?c:s,key:e.getAttribute("key")}}(e);if(t){let r,a=e.namespaceURI;if(a.endsWith("/svg")){e.removeAttribute("each");let t=document.createElementNS(a,"defs");t.innerHTML=e.outerHTML,e.parentNode.replaceChild(t,e),r=v((e=t).firstChild)}else{if("TEMPLATE"!==e.nodeName){e.removeAttribute("each");let t=document.createElement("template");t.innerHTML=e.outerHTML,e.parentNode.replaceChild(t,e),e=t}r=v(e.content.firstChild)}n=e.nextSibling,i.push({type:5,map:r,blockIndex:A++,...t,pickupNode:n});break}let r=e.attributes,a=r.length;for(;a--;){let{name:t,value:n}=r[a];if(":name"!==t||!n||"INPUT"!==e.nodeName&&"SELECT"!==e.nodeName&&"TEXTAREA"!==e.nodeName){if(t.startsWith(":on")){e.removeAttribute(t);let r=t.split(":on")[1];i.push({type:4,eventType:r,actionType:n})}else if(t.startsWith(":")){let r=t.slice(1),a=n||r;a.includes("{{")||(a=`{{${a}}}`),i.push({type:2,name:r,value:a}),e.removeAttribute(t)}}else i.push({type:3,path:n}),e.removeAttribute(t),e.setAttribute("name",n)}}}return i.length&&(r[t]=i),t++,n})),r},k=(...e)=>(...t)=>{for(let n of e){if(!1===n(...t))return!1}},T=(e,t=0,n)=>{let r=0;return i=>{let a;return r in e&&(e[r].forEach((e=>{let r=g({...e,node:i,context:n,hydrate:t});a=r.pickupNode})),i.$i=r),r++,a}};let N=i(c),C=v(N);return n(N,T(C)),f?.(N),t.prepend(N),E(),(e=>{let t=!1,n=!1;return()=>{t?n=!0:(t=!0,e(),requestAnimationFrame((()=>{n&&e(),t=!1})))}})((()=>E(s)))},E=e=>{let t=document.createDocumentFragment();for(;e.firstChild;)t.appendChild(e.firstChild);return t},A=(e,t,n)=>customElements.define(e,class extends HTMLElement{async connectedCallback(){if(!this.initialised){let e=t(this);e instanceof Promise&&(e=await e);let{subscribe:r,shadow:i,initialState:o={}}=e;this.connectedCallback=e.connectedCallback,this.disconnectedCallback=e.disconnectedCallback;const{dispatch:u,getState:d,onUpdate:p,flush:h}=function({update:e={},middleware:t=[],derivations:n={},initialState:r={}},i){let a,l=[],c=()=>{};function s(e){a={...e};for(let t in n)a[t]=n[t](e)}function o(){return{...a}}function u(e){l.push(e)}function d(n){const{type:r}=n;if("SET"===r||"MERGE"===r)s(function(e,t){switch(t.type){case"SET":{const{name:n,value:r}=t.payload;return{...e,[n]:r}}case"MERGE":return{...e,...t.payload}}}(o(),n));else if(t.length){let i=t.slice(),a=t=>{if(i.length){let e=i.shift();t.type in e?e[t.type](t,a,{getState:o,dispatch:d,afterNextRender:u}):a(t)}else t.type in e&&s(e[t.type](o(),t))},l=i.shift();r in l?l[r](n,a,{getState:o,dispatch:d,afterNextRender:u}):a(n)}else r in e&&s(e[r](o(),n));c()}s(r);for(let t in e)t.startsWith("$")&&i.addEventListener(t,(({detail:e})=>d(e)));return{dispatch:d,getState:o,onUpdate:function(e){c=e},flush:function(){l.forEach((e=>e())),l=[]}}}(e,this);o=d();let f,m=Object.keys(o).filter((e=>"$"===e.charAt(0))),b=m.map((e=>e.slice(1))).map(l),y=this.setAttribute;this.setAttribute=(e,t)=>{if(b.includes(e)){let{name:n,value:r}=s(e,t);u({type:"SET",payload:{name:"$"+n,value:r}})}y.apply(this,[e,t])},b.forEach((e=>{let t,n=s(e).name;t=this.hasAttribute(e)?this.getAttribute(e):this[n]||o["$"+n],Object.defineProperty(this,n,{get:()=>d()["$"+n],set(e){u({type:"SET",payload:{name:"$"+n,value:e}}),a(e)&&c(this,n,e)}}),this[n]=t})),i?this.attachShadow({mode:i}):f=e=>((e,t)=>{t.querySelectorAll("slot[name]").forEach((t=>{let n=t.attributes.name.value,r=e.querySelector(`[slot="${n}"]`);r?(r.removeAttribute("slot"),t.parentNode.replaceChild(r,t)):t.parentNode.replaceChild(E(t),t)}));let n=t.querySelector("slot:not([name])");n&&n.parentNode.replaceChild(E(e.innerHTML.trim()?e:n),n)})(this,e),p(g(this.shadowRoot||this,{getState:d,dispatch:u},n,(()=>{m.forEach((e=>{let t=d()[e];a(t)&&c(this,e.slice(1),t)})),r?.(d()),h()}),f)),this.initialised=!0}this.connectedCallback?.()}disconnectedCallback(){this.disconnectedCallback?.()}});export{A as define};
1
+ const e=(e=[])=>e[e.length-1],t=e=>e.nodeType===e.TEXT_NODE&&""===e.nodeValue.trim(),n=(e,r,i=!0)=>{if(e){if(!t(e)){let t=r(e);if(!1===t)return;if(t?.nodeName)return n(t,r,i)}i&&n(e.firstChild,r,i),n(e.nextSibling,r,i)}},r=(t,n)=>{let[i,a]=((t,n)=>{let i=((e="")=>e.split(/(\[[^\]]+\])/).filter((e=>e)).reduce(((e,t)=>e+("["===t.charAt(0)?"."+t.replace(/\./g,":"):t)),""))(t).split(".").map((e=>{if("["===e.charAt(0)){let t=e.slice(1,-1).replace(/:/g,".");return r(t,n)}return e}));return[i.slice(0,-1).reduce(((e,t)=>e&&e[t]),n)||n,e(i)]})(t,n),l=i?.[a];return"function"==typeof l?l.bind(i):l},i=e=>{if("string"==typeof e){let t=document.createElement("template");return t.innerHTML=e.trim(),t.content}return"TEMPLATE"===e.nodeName?e.cloneNode(!0).content:"defs"===e.nodeName?e.firstElementChild.cloneNode(!0):void 0},a=e=>null===e||"object"!=typeof e,l=e=>e.replace(/[\w]([A-Z])/g,(function(e){return e[0]+"-"+e[1].toLowerCase()})),c=(e,t,n)=>{t=l(t),"boolean"==typeof n&&(t.startsWith("aria-")?n=""+n:n&&(n="")),"string"==typeof n||"number"==typeof n?e.setAttribute(t,n):e.removeAttribute(t)},s=(e,t)=>{let n=e.replace(/[\w]-([\w])/g,(function(e){return e[0]+e[2].toUpperCase()}));return""===t&&(t=!0),e.startsWith("aria-")&&("true"===t&&(t=!0),"false"===t&&(t=!1)),{name:n,value:t}};const o=e=>{let t=0;return n(e.content?.firstChild||e.firstChild,(()=>t++),!1),t},u=e=>t(e.nextSibling)?u(e.nextSibling):e.nextSibling;const d=(e,t,n,r)=>{let i=e.getAttribute("length")||0,a=((e,t)=>{let n=o(e),r=[];if(t)for(;t--;){let t=document.createDocumentFragment(),i=n;for(;i--;)t.appendChild(u(e));r.push(t)}return r})(e,i),l=e;t.forEach(((e,t)=>{let i=-1===e?r(n[t][1],n[t][0]):a[e],c=(s=i).nodeType===s.DOCUMENT_FRAGMENT_NODE&&s.lastChild||s;var s;l.after(i),l=c})),e.setAttribute("length",t.length)},p=e=>e.trim().split(/({{[^{}]+}})/).filter((e=>e)).map((e=>{let t=e.match(/{{([^{}]+)}}/);if(!t)return{type:1,value:e,negated:!1};let n="!"===(e=t[1].trim()).charAt(0);return n&&(e=e.slice(1)),{type:2,value:e,negated:n}})),h=(e,t)=>t.reduce(((t,n)=>{let i,{type:a,value:l,negated:c}=n;if(1===a&&(i=l),2===a&&(i=r(l,e)),3===a){let t=n.args.map((t=>r(t,e)));i=r(n.method,e)?.(...t)}return c&&(i=!i),t||0===t?t+i:i}),""),f=e=>e.replace(/[\w]([A-Z])/g,(function(e){return e[0]+"-"+e[1].toLowerCase()})),m=e=>{let t=typeof e;return"string"===t?e.split(";").reduce(((e,t)=>{const[n,r]=t.split(":").map((e=>e.trim()));return n&&(e[n]=r),e}),{}):"object"===t?e:{}},b=(e,t,n)=>{if("style"===t)n=(e=>Object.entries(e).map((([e,t])=>`${e}: ${t};`)).join(" "))((l={...m(e.getAttribute("style")),...m(n)},Object.keys(l).reduce(((e,t)=>(e[f(t)]=l[t],e)),{})));else if("class"===t)switch(i=n,Object.prototype.toString.call(i).match(/\s(.+[^\]])/)[1]){case"Array":n=n.join(" ");break;case"Object":n=Object.keys(n).reduce(((e,t)=>(n[t]&&e.push(t),e)),[]).join(" ")}else if(!a(n))return e[(r=t,r.replace(/[\w]-([\w])/g,(function(e){return e[0]+e[2].toUpperCase()})))]=n;var r,i,l;t=f(t),"boolean"==typeof n&&(t.startsWith("aria-")?n=""+n:n&&(n=""));let c=e.getAttribute(t);n!==c&&("string"==typeof n||"number"==typeof n?e.setAttribute(t,n):e.removeAttribute(t))},y=(e=[])=>{let t=e;return{get:()=>t,push:e=>t.push(e),wrap:e=>t.reduce(((e,t)=>new Proxy(e,(({path:e,identifier:t,key:n,index:i,i:l,k:c})=>({get(a,s){let o=r(e,a);if(s===t)for(let e in o){let t=o[e];if(n){if(t[n]===c)return t}else if(e==l)return t}if(s===i)for(let e in o){let t=o[e];if(n){if(t[n]===c)return e}else if(e==l)return e}let u=n?o.find((e=>e[n]===c)):o[l];return u?.hasOwnProperty?.(s)?u[s]:Reflect.get(...arguments)},set(t,i,s){let o=r(e,t),u=n?o.find((e=>e[n]===c)):o[l];return u&&!a(u)?(u[i]=s,!0):Reflect.set(...arguments)}}))(t))),e)}},g=(t,{getState:a,dispatch:l},c,s,f)=>{const m={1:({value:e,node:t,context:n},{getState:r})=>({handler:()=>{let i=n?n.wrap(r()):r(),a=t.textContent,l=h(i,p(e));a!==l&&(t.textContent=l)}}),2:({value:e,node:t,name:n,context:i},{getState:a})=>({handler:()=>{let l=i?i.wrap(a()):a(),c=h(l,p(e));if(b(t,n,c),"OPTION"===t.nodeName){let e=t.parentNode.getAttribute("name"),n=r(e,l);t.selected=n===c}}}),3:({node:e,path:t,context:n},{getState:i,dispatch:a})=>(e.addEventListener("input",(()=>{let r="checkbox"===e.getAttribute("type")?e.checked:e.value;if(r.trim?.().length&&!isNaN(r)&&(r=+r),n){let e=n.wrap(i());e[t]=r,a({type:"MERGE",payload:e})}else a({type:"SET",payload:{name:t,value:r,context:n}})})),{handler:()=>{let a=n?n.wrap(i()):i();((e,t)=>{if("SELECT"===e.nodeName)return void Array.from(e.querySelectorAll("option")).forEach((e=>{e.selected=t.includes(e.value)}));let n;switch(e.getAttribute("type")){case"checkbox":if(n=t,e.checked===n)break;n?e.setAttribute("checked",""):e.removeAttribute("checked");break;case"radio":if(n=t===e.getAttribute("value"),e.checked===n)break;e.checked=n,n?e.setAttribute("checked",""):e.removeAttribute("checked");break;default:if(e.value===t)break;e.setAttribute("value",e.value=t||"")}})(e,r(t,a))}}),4:({node:e,eventType:t,actionType:n,context:r},{dispatch:i,getState:a})=>(e.addEventListener(t,(t=>{let l={type:n,event:t,context:r?r.wrap(a()):a()};i(l),n.startsWith("$")&&e.dispatchEvent(new CustomEvent(n,{detail:l,bubbles:!0}))})),{handler:()=>{}}),5:({node:t,context:a,map:l,path:c,identifier:s,index:p,key:h,blockIndex:f,hydrate:m,pickupNode:b},{getState:g})=>{let E;t.$t=f-1;const A=(e,t,r,i)=>{n(e,k((e=>{if(e===i)return!1}),T(l,m,y((a?.get()||[]).concat({path:c,identifier:s,key:h,index:p,i:t,k:r}))),(e=>e.$t=f)))};const v=(e,n)=>{let r=e[h],a=i(t);var l;return A((l=a).nodeType===l.DOCUMENT_FRAGMENT_NODE&&l.firstChild||l,n,r),a};if(m){let n=r(c,g()),i=(e=>{let t=e.getAttribute("length"),n=o(e),r=[],i=e;if(t)for(;t--;){let e=[],t=n;for(;t--;)i=u(i),e.push(i);r.push(e)}return r})(t);i.forEach(((t,r)=>{let i=n[r]?.[h];A(t[0],r,i,e(t).nextSibling)})),b=e(e(i)).nextSibling}return{handler:()=>{let e=a?a.wrap(g()):g();const n=Object.entries(r(c,e)||[]),i=((e,t=[],n=[])=>{let r=n.map((([n,r])=>e?t.findIndex((([t,n])=>n[e]===r[e])):n in t?n:-1));if(t.length!==n.length||!r.every(((e,t)=>e===t)))return r})(h,E,n);i&&d(t,i,n,v),E=n.slice(0)},pickupNode:b}}},{bind:g,update:E}=(()=>{const e=(()=>{let e=new Set;return{publish:t=>{for(let t of e)t();t?.()},subscribe(t){e.add(t)}}})();return{bind(t){let n=m[t.type](t,{getState:a,dispatch:l});return e.subscribe(n.handler),n},update:t=>e.publish(t)}})();let A=0;const v=e=>{let t=0,r={};return n(e,(e=>{let n,i=[];switch(e.nodeType){case e.TEXT_NODE:{let t=e.textContent;t.match(/({{[^{}]+}})/)&&i.push({type:1,value:t});break}case e.ELEMENT_NODE:{let t=function(e){let t=e.getAttribute("each"),n=t?.match(/(.+)\s+in\s+(.+)/);if(!n)return t?{path:t.trim(),key:e.getAttribute("key")}:n;let[r,i,a]=n,l=i.match(/\(([^\)]+)\)/),[c,s]=(l?l[1].split(","):[i]).map((e=>e.trim()));return{path:a.trim(),identifier:s||c,index:s?c:s,key:e.getAttribute("key")}}(e);if(t){let r,a=e.namespaceURI;if(a.endsWith("/svg")){e.removeAttribute("each");let t=document.createElementNS(a,"defs");t.innerHTML=e.outerHTML,e.parentNode.replaceChild(t,e),r=v((e=t).firstChild)}else{if("TEMPLATE"!==e.nodeName){e.removeAttribute("each");let t=document.createElement("template");t.innerHTML=e.outerHTML,e.parentNode.replaceChild(t,e),e=t}r=v(e.content.firstChild)}n=e.nextSibling,i.push({type:5,map:r,blockIndex:A++,...t,pickupNode:n});break}let r=e.attributes,a=r.length;for(;a--;){let{name:t,value:n}=r[a];if(":name"!==t||!n||"INPUT"!==e.nodeName&&"SELECT"!==e.nodeName&&"TEXTAREA"!==e.nodeName){if(t.startsWith(":on")){e.removeAttribute(t);let r=t.split(":on")[1];i.push({type:4,eventType:r,actionType:n})}else if(t.startsWith(":")){let r=t.slice(1),a=n||r;a.includes("{{")||(a=`{{${a}}}`),i.push({type:2,name:r,value:a}),e.removeAttribute(t)}}else i.push({type:3,path:n}),e.removeAttribute(t),e.setAttribute("name",n)}}}return i.length&&(r[t]=i),t++,n})),r},k=(...e)=>(...t)=>{for(let n of e){if(!1===n(...t))return!1}},T=(e,t=0,n)=>{let r=0;return i=>{let a;return r in e&&(e[r].forEach((e=>{let r=g({...e,node:i,context:n,hydrate:t});a=r.pickupNode})),i.$i=r),r++,a}};let N=i(c),C=v(N);return n(N,T(C)),f?.(N),t.prepend(N),E(),(e=>{let t=!1,n=!1;return()=>{t?n=!0:(t=!0,e(),requestAnimationFrame((()=>{n&&e(),t=!1})))}})((()=>E(s)))},E=e=>{let t=document.createDocumentFragment();for(;e.firstChild;)t.appendChild(e.firstChild);return t},A=(e,t,n)=>customElements.define(e,class extends HTMLElement{async connectedCallback(){if(!this.initialised){let e=t(this);e instanceof Promise&&(e=await e);let{subscribe:r,shadow:i,initialState:o={}}=e;this.connectedCallback=e.connectedCallback,this.disconnectedCallback=e.disconnectedCallback;const{dispatch:u,getState:d,onUpdate:p,flush:h}=function({update:e={},middleware:t=[],derivations:n={},initialState:r={}},i){let a,l=[],c=()=>{};function s(e){a={...e};for(let t in n)a[t]=n[t](e)}function o(){return{...a}}function u(e){l.push(e)}function d(n){const{type:r}=n;if("SET"===r||"MERGE"===r)s(function(e,t){switch(t.type){case"SET":{const{name:n,value:r}=t.payload;return{...e,[n]:r}}case"MERGE":return{...e,...t.payload}}}(o(),n));else if(t.length){let i=t.slice(),a=t=>{if(i.length){let e=i.shift();t.type in e?e[t.type](t,a,{getState:o,dispatch:d,afterNextRender:u}):a(t)}else t.type in e&&s(e[t.type](o(),t))},l=i.shift();r in l?l[r](n,a,{getState:o,dispatch:d,afterNextRender:u}):a(n)}else r in e&&s(e[r](o(),n));c()}s(r);for(let t in e)t.startsWith("$")&&i.addEventListener(t,(({detail:e})=>d(e)));return{dispatch:d,getState:o,onUpdate:function(e){c=e},flush:function(){l.forEach((e=>e())),l=[]}}}(e,this);o=d();let f,m=Object.keys(o).filter((e=>"$"===e.charAt(0))),b=m.map((e=>e.slice(1))).map(l),y=this.setAttribute;this.setAttribute=(e,t)=>{if(b.includes(e)){let{name:n,value:r}=s(e,t);u({type:"SET",payload:{name:"$"+n,value:r}})}y.apply(this,[e,t])},b.forEach((e=>{let t,n=s(e).name;t=this.hasAttribute(e)?this.getAttribute(e):this[n]||o["$"+n],Object.defineProperty(this,n,{get:()=>d()["$"+n],set(e){u({type:"SET",payload:{name:"$"+n,value:e}}),a(e)&&c(this,n,e)}}),this[n]=t})),i?this.attachShadow({mode:i}):f=e=>((e,t)=>{t.querySelectorAll("slot[name]").forEach((t=>{let n=t.attributes.name.value,r=e.querySelector(`[slot="${n}"]`);r?(r.removeAttribute("slot"),t.parentNode.replaceChild(r,t)):t.parentNode.replaceChild(E(t),t)}));let n=t.querySelector("slot:not([name])");n&&n.parentNode.replaceChild(E(e.innerHTML.trim()?e:n),n)})(this,e),p(g(this.shadowRoot||this,{getState:d,dispatch:u},n,(()=>{m.forEach((e=>{let t=d()[e];a(t)&&c(this,e.slice(1),t)})),r?.(d()),h()}),f)),this.initialised=!0}this.connectedCallback?.()}disconnectedCallback(){this.disconnectedCallback?.()}});export{A as define};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mosaic",
3
- "version": "1.0.5",
3
+ "version": "1.0.6",
4
4
  "description": "The Web UI Framework",
5
5
  "main": "dist/mosaic.min.js",
6
6
  "browser": "dist/mosaic.min.js",
package/src/render.js CHANGED
@@ -107,19 +107,15 @@ export const render = (
107
107
  */
108
108
 
109
109
  node.addEventListener(eventType, (event) => {
110
- let isGlobal = actionType.startsWith("$")
111
-
112
110
  let action = {
113
111
  type: actionType,
114
112
  event,
113
+ context: context ? context.wrap(getState()) : getState(),
115
114
  }
116
115
 
117
- if (!isGlobal)
118
- action.context = context ? context.wrap(getState()) : getState()
119
-
120
116
  dispatch(action)
121
117
 
122
- if (isGlobal) {
118
+ if (actionType.startsWith("$")) {
123
119
  node.dispatchEvent(
124
120
  new CustomEvent(actionType, {
125
121
  detail: action,