mosaic 1.0.7 → 1.0.8
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 +0 -15
- package/cjs/update.js +10 -0
- package/dist/mosaic.js +10 -15
- package/dist/mosaic.min.js +1 -1
- package/package.json +1 -1
- package/src/render.js +0 -15
- package/src/update.js +10 -0
package/cjs/render.js
CHANGED
|
@@ -96,12 +96,6 @@ const render = (
|
|
|
96
96
|
{ node, eventType, actionType, context },
|
|
97
97
|
{ dispatch, getState }
|
|
98
98
|
) => {
|
|
99
|
-
/*
|
|
100
|
-
|
|
101
|
-
NB that context is only passed for local actions not prefixed with "$"
|
|
102
|
-
|
|
103
|
-
*/
|
|
104
|
-
|
|
105
99
|
node.addEventListener(eventType, (event) => {
|
|
106
100
|
let action = {
|
|
107
101
|
type: actionType,
|
|
@@ -110,15 +104,6 @@ const render = (
|
|
|
110
104
|
};
|
|
111
105
|
|
|
112
106
|
dispatch(action);
|
|
113
|
-
|
|
114
|
-
if (actionType.startsWith("$")) {
|
|
115
|
-
node.dispatchEvent(
|
|
116
|
-
new CustomEvent(actionType, {
|
|
117
|
-
detail: action,
|
|
118
|
-
bubbles: true,
|
|
119
|
-
})
|
|
120
|
-
);
|
|
121
|
-
}
|
|
122
107
|
});
|
|
123
108
|
return {
|
|
124
109
|
handler: () => {},
|
package/cjs/update.js
CHANGED
|
@@ -69,6 +69,16 @@ function configure(
|
|
|
69
69
|
|
|
70
70
|
function dispatch(action) {
|
|
71
71
|
const { type } = action;
|
|
72
|
+
|
|
73
|
+
if (type.startsWith("$") && !(type in update)) {
|
|
74
|
+
node.dispatchEvent(
|
|
75
|
+
new CustomEvent(type, {
|
|
76
|
+
detail: action,
|
|
77
|
+
bubbles: true,
|
|
78
|
+
})
|
|
79
|
+
);
|
|
80
|
+
}
|
|
81
|
+
|
|
72
82
|
if (type === "SET" || type === "MERGE") {
|
|
73
83
|
updateState(systemReducer(getState(), action));
|
|
74
84
|
} else {
|
package/dist/mosaic.js
CHANGED
|
@@ -65,6 +65,16 @@ function configure(
|
|
|
65
65
|
|
|
66
66
|
function dispatch(action) {
|
|
67
67
|
const { type } = action;
|
|
68
|
+
|
|
69
|
+
if (type.startsWith("$") && !(type in update)) {
|
|
70
|
+
node.dispatchEvent(
|
|
71
|
+
new CustomEvent(type, {
|
|
72
|
+
detail: action,
|
|
73
|
+
bubbles: true,
|
|
74
|
+
})
|
|
75
|
+
);
|
|
76
|
+
}
|
|
77
|
+
|
|
68
78
|
if (type === "SET" || type === "MERGE") {
|
|
69
79
|
updateState(systemReducer(getState(), action));
|
|
70
80
|
} else {
|
|
@@ -669,12 +679,6 @@ const render = (
|
|
|
669
679
|
{ node, eventType, actionType, context },
|
|
670
680
|
{ dispatch, getState }
|
|
671
681
|
) => {
|
|
672
|
-
/*
|
|
673
|
-
|
|
674
|
-
NB that context is only passed for local actions not prefixed with "$"
|
|
675
|
-
|
|
676
|
-
*/
|
|
677
|
-
|
|
678
682
|
node.addEventListener(eventType, (event) => {
|
|
679
683
|
let action = {
|
|
680
684
|
type: actionType,
|
|
@@ -683,15 +687,6 @@ const render = (
|
|
|
683
687
|
};
|
|
684
688
|
|
|
685
689
|
dispatch(action);
|
|
686
|
-
|
|
687
|
-
if (actionType.startsWith("$")) {
|
|
688
|
-
node.dispatchEvent(
|
|
689
|
-
new CustomEvent(actionType, {
|
|
690
|
-
detail: action,
|
|
691
|
-
bubbles: true,
|
|
692
|
-
})
|
|
693
|
-
);
|
|
694
|
-
}
|
|
695
690
|
});
|
|
696
691
|
return {
|
|
697
692
|
handler: () => {},
|
package/dist/mosaic.min.js
CHANGED
|
@@ -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()})),s=e=>e.replace(/[\w]-([\w])/g,(function(e){return e[0]+e[2].toUpperCase()})),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)},o=(e,t)=>{let n=s(e);return""===t&&(t=!0),e.startsWith("aria-")&&("true"===t&&(t=!0),"false"===t&&(t=!1)),{name:n,value:t}};const u=e=>{let t=0;return n(e.content?.firstChild||e.firstChild,(()=>t++),!1),t},d=e=>t(e.nextSibling)?d(e.nextSibling):e.nextSibling;const p=(e,t,n,r)=>{let i=e.getAttribute("length")||0,a=((e,t)=>{let n=u(e),r=[];if(t)for(;t--;){let t=document.createDocumentFragment(),i=n;for(;i--;)t.appendChild(d(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],s=(c=i).nodeType===c.DOCUMENT_FRAGMENT_NODE&&c.lastChild||c;var c;l.after(i),l=s})),e.setAttribute("length",t.length)},h=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}})),f=(e,t)=>t.reduce(((t,n)=>{let i,{type:a,value:l,negated:s}=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 s&&(i=!i),t||0===t?t+i:i}),""),m=e=>e.replace(/[\w]([A-Z])/g,(function(e){return e[0]+"-"+e[1].toLowerCase()})),b=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:{}},y=(e,t,n)=>{if("style"===t)n=(e=>Object.entries(e).map((([e,t])=>`${e}: ${t};`)).join(" "))((l={...b(e.getAttribute("style")),...b(n)},Object.keys(l).reduce(((e,t)=>(e[m(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=m(t),"boolean"==typeof n&&(t.startsWith("aria-")?n=""+n:n&&(n=""));let s=e.getAttribute(t);n!==s&&("string"==typeof n||"number"==typeof n?e.setAttribute(t,n):e.removeAttribute(t))},g=(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:s})=>({get(a,c){let o=r(e,a);if(c===t)for(let e in o){let t=o[e];if(n){if(t[n]===s)return t}else if(e==l)return t}if(c===i)for(let e in o){let t=o[e];if(n){if(t[n]===s)return e}else if(e==l)return e}let u=n?o.find((e=>e[n]===s)):o[l];return u?.hasOwnProperty?.(c)?u[c]:Reflect.get(...arguments)},set(t,i,c){let o=r(e,t),u=n?o.find((e=>e[n]===s)):o[l];return u&&!a(u)?(u[i]=c,!0):Reflect.set(...arguments)}}))(t))),e)}},E=(t,{getState:a,dispatch:l},s,c,o)=>{const m={1:({value:e,node:t,context:n},{getState:r})=>({handler:()=>{let i=n?n.wrap(r()):r(),a=t.textContent,l=f(i,h(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(),s=f(l,h(e));if(y(t,n,s),"OPTION"===t.nodeName){let e=t.parentNode.getAttribute("name"),n=r(e,l);t.selected=n===s}}}),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:s,identifier:c,index:o,key:h,blockIndex:f,hydrate:m,pickupNode:b},{getState:y})=>{let E;t.$t=f-1;const v=(e,t,r,i)=>{n(e,k((e=>{if(e===i)return!1}),T(l,m,g((a?.get()||[]).concat({path:s,identifier:c,key:h,index:o,i:t,k:r}))),(e=>e.$t=f)))};const A=(e,n)=>{let r=e[h],a=i(t);var l;return v((l=a).nodeType===l.DOCUMENT_FRAGMENT_NODE&&l.firstChild||l,n,r),a};if(m){let n=r(s,y()),i=(e=>{let t=e.getAttribute("length"),n=u(e),r=[],i=e;if(t)for(;t--;){let e=[],t=n;for(;t--;)i=d(i),e.push(i);r.push(e)}return r})(t);i.forEach(((t,r)=>{let i=n[r]?.[h];v(t[0],r,i,e(t).nextSibling)})),b=e(e(i)).nextSibling}return{handler:()=>{let e=a?a.wrap(y()):y();const n=Object.entries(r(s,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&&p(t,i,n,A),E=n.slice(0)},pickupNode:b}}},{bind:b,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 v=0;const A=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(/\(([^\)]+)\)/),[s,c]=(l?l[1].split(","):[i]).map((e=>e.trim()));return{path:a.trim(),identifier:c||s,index:c?s:c,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=A((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=A(e.content.firstChild)}n=e.nextSibling,i.push({type:5,map:r,blockIndex:v++,...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=b({...e,node:i,context:n,hydrate:t});a=r.pickupNode})),i.$i=r),r++,a}};let N=i(s),C=A(N);return n(N,T(C)),o?.(N),t.prepend(N),E(),(e=>{let t=!1,n=!1;return()=>{t?n=!0:(t=!0,e(),requestAnimationFrame((()=>{n&&e(),t=!1})))}})((()=>E(c)))},v=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:u={},observe:d=[]}=e;this.connectedCallback=e.connectedCallback,this.disconnectedCallback=e.disconnectedCallback;const{dispatch:p,getState:h,onUpdate:f,flush:m}=function({update:e={},middleware:t=[],derivations:n={},initialState:r={}},i){let a,l=[],s=()=>{};function c(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)c(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&&c(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&&c(e[r](o(),n));s()}c(r);for(let t in e)t.startsWith("$")&&i.addEventListener(t,(({detail:e})=>d(e)));return{dispatch:d,getState:o,onUpdate:function(e){s=e},flush:function(){l.forEach((e=>e())),l=[]}}}(e,this);u=h(),d=d.map(s);let b,y=d.map(l),g=this.setAttribute;this.setAttribute=(e,t)=>{if(y.includes(e)){let{name:n,value:r}=o(e,t);p({type:"SET",payload:{name:n,value:r}})}g.apply(this,[e,t])},y.forEach((e=>{let t,n=o(e).name;t=this.hasAttribute(e)?this.getAttribute(e):this[n]||u[n],Object.defineProperty(this,n,{get:()=>h()[n],set(e){p({type:"SET",payload:{name:n,value:e}}),a(e)&&c(this,n,e)}}),this[n]=t})),i?this.attachShadow({mode:i}):b=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(v(t),t)}));let n=t.querySelector("slot:not([name])");n&&n.parentNode.replaceChild(v(e.innerHTML.trim()?e:n),n)})(this,e),f(E(this.shadowRoot||this,{getState:h,dispatch:p},n,(()=>{d.forEach((e=>{let t=h()[e];a(t)&&c(this,e,t)})),r?.(h()),m()}),b)),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()})),s=e=>e.replace(/[\w]-([\w])/g,(function(e){return e[0]+e[2].toUpperCase()})),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)},o=(e,t)=>{let n=s(e);return""===t&&(t=!0),e.startsWith("aria-")&&("true"===t&&(t=!0),"false"===t&&(t=!1)),{name:n,value:t}};const u=e=>{let t=0;return n(e.content?.firstChild||e.firstChild,(()=>t++),!1),t},d=e=>t(e.nextSibling)?d(e.nextSibling):e.nextSibling;const p=(e,t,n,r)=>{let i=e.getAttribute("length")||0,a=((e,t)=>{let n=u(e),r=[];if(t)for(;t--;){let t=document.createDocumentFragment(),i=n;for(;i--;)t.appendChild(d(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],s=(c=i).nodeType===c.DOCUMENT_FRAGMENT_NODE&&c.lastChild||c;var c;l.after(i),l=s})),e.setAttribute("length",t.length)},h=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}})),f=(e,t)=>t.reduce(((t,n)=>{let i,{type:a,value:l,negated:s}=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 s&&(i=!i),t||0===t?t+i:i}),""),m=e=>e.replace(/[\w]([A-Z])/g,(function(e){return e[0]+"-"+e[1].toLowerCase()})),b=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:{}},y=(e,t,n)=>{if("style"===t)n=(e=>Object.entries(e).map((([e,t])=>`${e}: ${t};`)).join(" "))((l={...b(e.getAttribute("style")),...b(n)},Object.keys(l).reduce(((e,t)=>(e[m(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=m(t),"boolean"==typeof n&&(t.startsWith("aria-")?n=""+n:n&&(n=""));let s=e.getAttribute(t);n!==s&&("string"==typeof n||"number"==typeof n?e.setAttribute(t,n):e.removeAttribute(t))},g=(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:s})=>({get(a,c){let o=r(e,a);if(c===t)for(let e in o){let t=o[e];if(n){if(t[n]===s)return t}else if(e==l)return t}if(c===i)for(let e in o){let t=o[e];if(n){if(t[n]===s)return e}else if(e==l)return e}let u=n?o.find((e=>e[n]===s)):o[l];return u?.hasOwnProperty?.(c)?u[c]:Reflect.get(...arguments)},set(t,i,c){let o=r(e,t),u=n?o.find((e=>e[n]===s)):o[l];return u&&!a(u)?(u[i]=c,!0):Reflect.set(...arguments)}}))(t))),e)}},E=(t,{getState:a,dispatch:l},s,c,o)=>{const m={1:({value:e,node:t,context:n},{getState:r})=>({handler:()=>{let i=n?n.wrap(r()):r(),a=t.textContent,l=f(i,h(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(),s=f(l,h(e));if(y(t,n,s),"OPTION"===t.nodeName){let e=t.parentNode.getAttribute("name"),n=r(e,l);t.selected=n===s}}}),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,(e=>{let t={type:n,event:e,context:r?r.wrap(a()):a()};i(t)})),{handler:()=>{}}),5:({node:t,context:a,map:l,path:s,identifier:c,index:o,key:h,blockIndex:f,hydrate:m,pickupNode:b},{getState:y})=>{let E;t.$t=f-1;const v=(e,t,r,i)=>{n(e,k((e=>{if(e===i)return!1}),T(l,m,g((a?.get()||[]).concat({path:s,identifier:c,key:h,index:o,i:t,k:r}))),(e=>e.$t=f)))};const A=(e,n)=>{let r=e[h],a=i(t);var l;return v((l=a).nodeType===l.DOCUMENT_FRAGMENT_NODE&&l.firstChild||l,n,r),a};if(m){let n=r(s,y()),i=(e=>{let t=e.getAttribute("length"),n=u(e),r=[],i=e;if(t)for(;t--;){let e=[],t=n;for(;t--;)i=d(i),e.push(i);r.push(e)}return r})(t);i.forEach(((t,r)=>{let i=n[r]?.[h];v(t[0],r,i,e(t).nextSibling)})),b=e(e(i)).nextSibling}return{handler:()=>{let e=a?a.wrap(y()):y();const n=Object.entries(r(s,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&&p(t,i,n,A),E=n.slice(0)},pickupNode:b}}},{bind:b,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 v=0;const A=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(/\(([^\)]+)\)/),[s,c]=(l?l[1].split(","):[i]).map((e=>e.trim()));return{path:a.trim(),identifier:c||s,index:c?s:c,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=A((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=A(e.content.firstChild)}n=e.nextSibling,i.push({type:5,map:r,blockIndex:v++,...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=b({...e,node:i,context:n,hydrate:t});a=r.pickupNode})),i.$i=r),r++,a}};let N=i(s),C=A(N);return n(N,T(C)),o?.(N),t.prepend(N),E(),(e=>{let t=!1,n=!1;return()=>{t?n=!0:(t=!0,e(),requestAnimationFrame((()=>{n&&e(),t=!1})))}})((()=>E(c)))},v=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:u={},observe:d=[]}=e;this.connectedCallback=e.connectedCallback,this.disconnectedCallback=e.disconnectedCallback;const{dispatch:p,getState:h,onUpdate:f,flush:m}=function({update:e={},middleware:t=[],derivations:n={},initialState:r={}},i){let a,l=[],s=()=>{};function c(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(r.startsWith("$")&&!(r in e)&&i.dispatchEvent(new CustomEvent(r,{detail:n,bubbles:!0})),"SET"===r||"MERGE"===r)c(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&&c(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&&c(e[r](o(),n));s()}c(r);for(let t in e)t.startsWith("$")&&i.addEventListener(t,(({detail:e})=>d(e)));return{dispatch:d,getState:o,onUpdate:function(e){s=e},flush:function(){l.forEach((e=>e())),l=[]}}}(e,this);u=h(),d=d.map(s);let b,y=d.map(l),g=this.setAttribute;this.setAttribute=(e,t)=>{if(y.includes(e)){let{name:n,value:r}=o(e,t);p({type:"SET",payload:{name:n,value:r}})}g.apply(this,[e,t])},y.forEach((e=>{let t,n=o(e).name;t=this.hasAttribute(e)?this.getAttribute(e):this[n]||u[n],Object.defineProperty(this,n,{get:()=>h()[n],set(e){p({type:"SET",payload:{name:n,value:e}}),a(e)&&c(this,n,e)}}),this[n]=t})),i?this.attachShadow({mode:i}):b=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(v(t),t)}));let n=t.querySelector("slot:not([name])");n&&n.parentNode.replaceChild(v(e.innerHTML.trim()?e:n),n)})(this,e),f(E(this.shadowRoot||this,{getState:h,dispatch:p},n,(()=>{d.forEach((e=>{let t=h()[e];a(t)&&c(this,e,t)})),r?.(h()),m()}),b)),this.initialised=!0}this.connectedCallback?.()}disconnectedCallback(){this.disconnectedCallback?.()}});export{A as define};
|
package/package.json
CHANGED
package/src/render.js
CHANGED
|
@@ -100,12 +100,6 @@ export const render = (
|
|
|
100
100
|
{ node, eventType, actionType, context },
|
|
101
101
|
{ dispatch, getState }
|
|
102
102
|
) => {
|
|
103
|
-
/*
|
|
104
|
-
|
|
105
|
-
NB that context is only passed for local actions not prefixed with "$"
|
|
106
|
-
|
|
107
|
-
*/
|
|
108
|
-
|
|
109
103
|
node.addEventListener(eventType, (event) => {
|
|
110
104
|
let action = {
|
|
111
105
|
type: actionType,
|
|
@@ -114,15 +108,6 @@ export const render = (
|
|
|
114
108
|
}
|
|
115
109
|
|
|
116
110
|
dispatch(action)
|
|
117
|
-
|
|
118
|
-
if (actionType.startsWith("$")) {
|
|
119
|
-
node.dispatchEvent(
|
|
120
|
-
new CustomEvent(actionType, {
|
|
121
|
-
detail: action,
|
|
122
|
-
bubbles: true,
|
|
123
|
-
})
|
|
124
|
-
)
|
|
125
|
-
}
|
|
126
111
|
})
|
|
127
112
|
return {
|
|
128
113
|
handler: () => {},
|
package/src/update.js
CHANGED
|
@@ -65,6 +65,16 @@ export function configure(
|
|
|
65
65
|
|
|
66
66
|
function dispatch(action) {
|
|
67
67
|
const { type } = action
|
|
68
|
+
|
|
69
|
+
if (type.startsWith("$") && !(type in update)) {
|
|
70
|
+
node.dispatchEvent(
|
|
71
|
+
new CustomEvent(type, {
|
|
72
|
+
detail: action,
|
|
73
|
+
bubbles: true,
|
|
74
|
+
})
|
|
75
|
+
)
|
|
76
|
+
}
|
|
77
|
+
|
|
68
78
|
if (type === "SET" || type === "MERGE") {
|
|
69
79
|
updateState(systemReducer(getState(), action))
|
|
70
80
|
} else {
|