modality-kit 0.8.8 → 0.8.10

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/browser.js CHANGED
@@ -1 +1,263 @@
1
- var{defineProperty:Z,getOwnPropertyNames:O,getOwnPropertyDescriptor:V}=Object,W=Object.prototype.hasOwnProperty;var _=new WeakMap,H=(x)=>{var j=_.get(x),q;if(j)return j;if(j=Z({},"__esModule",{value:!0}),x&&typeof x==="object"||typeof x==="function")O(x).map((B)=>!W.call(j,B)&&Z(j,B,{get:()=>x[B],enumerable:!(q=V(x,B))||q.enumerable}));return _.set(x,j),j};var M=(x,j)=>{for(var q in j)Z(x,q,{get:j[q],enumerable:!0,configurable:!0,set:(B)=>j[q]=()=>B})};var f={};M(f,{render:()=>$,registerStore:()=>F,ReactiveComponent:()=>Q});module.exports=H(f);class Q extends HTMLElement{#j;#G=!1;#q=!1;#K=!1;#x;#J;#B={};#Q=["click","dblclick","mousedown","mouseup","mousemove","mouseover","mouseout","focus","blur","change","input","submit","keydown","keyup","keypress"];_stores;_storeListeners;constructor(x={}){super();this.#J=x,this.#j=x.initialState||{}}get state(){return Object.freeze({...this.#j})}setState(x,j,q){let B=q||{...this.#j},J;if(typeof x==="function")J=x(this.#j);else J=x;let G={...this.#j,...J};if(this.#J.shouldUpdate&&!this.#J.shouldUpdate(G,B))return this.#j;else return this.#j=G,this.#X(B),G}forceUpdate(){this.#X({...this.#j})}#X(x){if(this.#G||this.#q)return;this.#q=!0,queueMicrotask(()=>{if(this.#q&&this.isConnected)this.#Y(x)})}#Y(x){this.#G=!0,this.#q=!1;let j=!this.#K,q=x||{...this.#j};try{if(this.#$(),this.#K=!0,!j&&typeof this.componentDidUpdate==="function")this.componentDidUpdate(this.#j,q)}catch(B){console.error("Error during component update:",B)}finally{this.#G=!1}}static#Z=(()=>{if(typeof window!=="undefined"&&window.trustedTypes&&window.trustedTypes.createPolicy)try{return window.trustedTypes.createPolicy("reactive-component",{createHTML:(x)=>x})}catch(x){return console.warn("Failed to create trusted types policy:",x),null}return null})();#_(x,j){if(Q.#Z)x.innerHTML=Q.#Z.createHTML(j);else x.innerHTML=j}#$(){if(!this.#x)this.#x=this.shadowRoot||this.attachShadow({mode:"open"}),this.#F();if(typeof window!=="undefined"&&window.trustedTypes&&window.trustedTypes.emptyHTML)this.#x.innerHTML=window.trustedTypes.emptyHTML;else while(this.#x.firstChild)this.#x.removeChild(this.#x.firstChild);let x=this.render();if(typeof x==="string"){let j=document.createElement("template");this.#_(j,x),this.#x.appendChild(j.content.cloneNode(!0))}else if(x instanceof DocumentFragment||x instanceof Element)this.#x.appendChild(x)}#F(){this.#Q.forEach((x)=>{let j=(B)=>{this.#I(B)};this.#B[x]=j;let q=!["mousedown","keydown","submit"].includes(x);this.#x.addEventListener(x,j,{passive:q})})}#I(x){let{type:j,target:q}=x;if(!q)return;let B=`data-${j}`,J=this.#x.querySelectorAll(`[${B}]`);Array.from(J).forEach((G)=>{if(q.isSameNode(G)||G.contains(q)){let K=G.getAttribute(B);if(K&&typeof this[K]==="function")this[K](x)}})}connectedCallback(){if(this._stores&&this._stores.length>0)this._storeListeners=[],this._stores.forEach((x)=>{let j=this.setState.bind(this);x.addListener(j),this._storeListeners.push({store:x,listener:j});let q=x.getState();this.setState(q)});if(this.#Y(),typeof this.componentDidMount==="function")this.componentDidMount()}disconnectedCallback(){if(this.#x&&Object.keys(this.#B).length>0)this.#Q.forEach((x)=>{let j=this.#B[x];if(j)this.#x.removeEventListener(x,j)}),this.#B={};if(this._storeListeners&&this._storeListeners.length>0)this._storeListeners.forEach(({store:x,listener:j})=>{x.removeListener(j)}),this._storeListeners=[]}}function $(x,j={},q){let B=document.createElement(x),{appendTo:J=document.body,...G}=j;if(Object.keys(G).forEach((K)=>{let X=G[K];if(X!=null){let I=typeof X==="object"?JSON.stringify(X):String(X);B.setAttribute(K,I)}}),q&&q.length>0)B._stores=q;if(J instanceof HTMLElement)J.appendChild(B);return B}var Y={current:{}};function F(x,j){let q=document.querySelector("connection-section");if(q)q._stores=j;else Y.current[x]=j}if(typeof document!=="undefined")document.addEventListener("DOMContentLoaded",()=>{let x=Object.keys(Y.current).map(async(j)=>{let q=document.querySelector(j);if(q)q._stores=Y.current[j],delete Y.current[j]});Promise.all(x)});
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropNames = Object.getOwnPropertyNames;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __moduleCache = /* @__PURE__ */ new WeakMap;
6
+ var __toCommonJS = (from) => {
7
+ var entry = __moduleCache.get(from), desc;
8
+ if (entry)
9
+ return entry;
10
+ entry = __defProp({}, "__esModule", { value: true });
11
+ if (from && typeof from === "object" || typeof from === "function")
12
+ __getOwnPropNames(from).map((key) => !__hasOwnProp.call(entry, key) && __defProp(entry, key, {
13
+ get: () => from[key],
14
+ enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
15
+ }));
16
+ __moduleCache.set(from, entry);
17
+ return entry;
18
+ };
19
+ var __export = (target, all) => {
20
+ for (var name in all)
21
+ __defProp(target, name, {
22
+ get: all[name],
23
+ enumerable: true,
24
+ configurable: true,
25
+ set: (newValue) => all[name] = () => newValue
26
+ });
27
+ };
28
+
29
+ // src/browser.ts
30
+ var exports_browser = {};
31
+ __export(exports_browser, {
32
+ render: () => render,
33
+ registerStore: () => registerStore,
34
+ ReactiveComponent: () => ReactiveComponent
35
+ });
36
+ module.exports = __toCommonJS(exports_browser);
37
+
38
+ // src/ReactiveComponent.ts
39
+ class ReactiveComponent extends HTMLElement {
40
+ #state;
41
+ #isRendering = false;
42
+ #pendingUpdate = false;
43
+ #hasRendered = false;
44
+ #shadow;
45
+ #options;
46
+ #delegatedEventListeners = {};
47
+ #eventTypes = [
48
+ "click",
49
+ "dblclick",
50
+ "mousedown",
51
+ "mouseup",
52
+ "mousemove",
53
+ "mouseover",
54
+ "mouseout",
55
+ "focus",
56
+ "blur",
57
+ "change",
58
+ "input",
59
+ "submit",
60
+ "keydown",
61
+ "keyup",
62
+ "keypress"
63
+ ];
64
+ _stores;
65
+ _storeListeners;
66
+ constructor(options = {}) {
67
+ super();
68
+ this.#options = options;
69
+ this.#state = options.initialState || {};
70
+ }
71
+ get state() {
72
+ return Object.freeze({ ...this.#state });
73
+ }
74
+ setState(updator, _action, prevState) {
75
+ const oldState = prevState || { ...this.#state };
76
+ let newUpdates;
77
+ if (typeof updator === "function") {
78
+ newUpdates = updator(this.#state);
79
+ } else {
80
+ newUpdates = updator;
81
+ }
82
+ const newState = { ...this.#state, ...newUpdates };
83
+ if (this.#options.shouldUpdate && !this.#options.shouldUpdate(newState, oldState)) {
84
+ return this.#state;
85
+ } else {
86
+ this.#state = newState;
87
+ this.#scheduleUpdate(oldState);
88
+ return newState;
89
+ }
90
+ }
91
+ forceUpdate() {
92
+ this.#scheduleUpdate({ ...this.#state });
93
+ }
94
+ #scheduleUpdate(previousState) {
95
+ if (this.#isRendering || this.#pendingUpdate) {
96
+ return;
97
+ }
98
+ this.#pendingUpdate = true;
99
+ queueMicrotask(() => {
100
+ if (this.#pendingUpdate && this.isConnected) {
101
+ this.#performUpdate(previousState);
102
+ }
103
+ });
104
+ }
105
+ #performUpdate(previousState) {
106
+ this.#isRendering = true;
107
+ this.#pendingUpdate = false;
108
+ const wasFirstRender = !this.#hasRendered;
109
+ const prevState = previousState || { ...this.#state };
110
+ try {
111
+ this.#updateDOM();
112
+ this.#hasRendered = true;
113
+ if (!wasFirstRender && typeof this.componentDidUpdate === "function") {
114
+ this.componentDidUpdate(this.#state, prevState);
115
+ }
116
+ } catch (error) {
117
+ console.error("Error during component update:", error);
118
+ } finally {
119
+ this.#isRendering = false;
120
+ }
121
+ }
122
+ static #trustedTypesPolicy = (() => {
123
+ if (typeof window !== "undefined" && window.trustedTypes && window.trustedTypes.createPolicy) {
124
+ try {
125
+ return window.trustedTypes.createPolicy("reactive-component", {
126
+ createHTML: (string) => string
127
+ });
128
+ } catch (e) {
129
+ console.warn("Failed to create trusted types policy:", e);
130
+ return null;
131
+ }
132
+ }
133
+ return null;
134
+ })();
135
+ #safeSetInnerHTML(element, html) {
136
+ if (ReactiveComponent.#trustedTypesPolicy) {
137
+ element.innerHTML = ReactiveComponent.#trustedTypesPolicy.createHTML(html);
138
+ } else {
139
+ element.innerHTML = html;
140
+ }
141
+ }
142
+ #updateDOM() {
143
+ if (!this.#shadow) {
144
+ this.#shadow = this.shadowRoot || this.attachShadow({ mode: "open" });
145
+ this.#setupEventDelegation();
146
+ }
147
+ if (typeof window !== "undefined" && window.trustedTypes && window.trustedTypes.emptyHTML) {
148
+ this.#shadow.innerHTML = window.trustedTypes.emptyHTML;
149
+ } else {
150
+ while (this.#shadow.firstChild) {
151
+ this.#shadow.removeChild(this.#shadow.firstChild);
152
+ }
153
+ }
154
+ const renderResult = this.render();
155
+ if (typeof renderResult === "string") {
156
+ const template = document.createElement("template");
157
+ this.#safeSetInnerHTML(template, renderResult);
158
+ this.#shadow.appendChild(template.content.cloneNode(true));
159
+ } else if (renderResult instanceof DocumentFragment || renderResult instanceof Element) {
160
+ this.#shadow.appendChild(renderResult);
161
+ }
162
+ }
163
+ #setupEventDelegation() {
164
+ this.#eventTypes.forEach((eventType) => {
165
+ const listener = (e) => {
166
+ this.#handleDelegatedEvent(e);
167
+ };
168
+ this.#delegatedEventListeners[eventType] = listener;
169
+ const usePassive = !["mousedown", "keydown", "submit"].includes(eventType);
170
+ this.#shadow.addEventListener(eventType, listener, {
171
+ passive: usePassive
172
+ });
173
+ });
174
+ }
175
+ #handleDelegatedEvent(e) {
176
+ const eventType = e.type;
177
+ const target = e.target;
178
+ if (!target)
179
+ return;
180
+ const handlerAttribute = `data-${eventType}`;
181
+ const elementsWithHandlers = this.#shadow.querySelectorAll(`[${handlerAttribute}]`);
182
+ Array.from(elementsWithHandlers).forEach((element) => {
183
+ if (target.isSameNode(element) || element.contains(target)) {
184
+ const handlerName = element.getAttribute(handlerAttribute);
185
+ if (handlerName && typeof this[handlerName] === "function") {
186
+ this[handlerName](e);
187
+ }
188
+ }
189
+ });
190
+ }
191
+ connectedCallback() {
192
+ if (this._stores && this._stores.length > 0) {
193
+ this._storeListeners = [];
194
+ this._stores.forEach((store) => {
195
+ const boundSetState = this.setState.bind(this);
196
+ store.addListener(boundSetState);
197
+ this._storeListeners.push({ store, listener: boundSetState });
198
+ const initialState = store.getState();
199
+ this.setState(initialState);
200
+ });
201
+ }
202
+ this.#performUpdate();
203
+ if (typeof this.componentDidMount === "function") {
204
+ this.componentDidMount();
205
+ }
206
+ }
207
+ disconnectedCallback() {
208
+ if (this.#shadow && Object.keys(this.#delegatedEventListeners).length > 0) {
209
+ this.#eventTypes.forEach((eventType) => {
210
+ const listener = this.#delegatedEventListeners[eventType];
211
+ if (listener) {
212
+ this.#shadow.removeEventListener(eventType, listener);
213
+ }
214
+ });
215
+ this.#delegatedEventListeners = {};
216
+ }
217
+ if (this._storeListeners && this._storeListeners.length > 0) {
218
+ this._storeListeners.forEach(({ store, listener }) => {
219
+ store.removeListener(listener);
220
+ });
221
+ this._storeListeners = [];
222
+ }
223
+ }
224
+ }
225
+ function render(componentName, props = {}, stores) {
226
+ const element = document.createElement(componentName);
227
+ const { appendTo = document.body, ...restProps } = props;
228
+ Object.keys(restProps).forEach((key) => {
229
+ const value = restProps[key];
230
+ if (value != null) {
231
+ const stringValue = typeof value === "object" ? JSON.stringify(value) : String(value);
232
+ element.setAttribute(key, stringValue);
233
+ }
234
+ });
235
+ if (stores && stores.length > 0) {
236
+ element._stores = stores;
237
+ }
238
+ if (appendTo instanceof HTMLElement) {
239
+ appendTo.appendChild(element);
240
+ }
241
+ return element;
242
+ }
243
+ var lazyStores = { current: {} };
244
+ function registerStore(componentName, stores) {
245
+ const connectionSectionElement = document.querySelector("connection-section");
246
+ if (connectionSectionElement) {
247
+ connectionSectionElement._stores = stores;
248
+ } else {
249
+ lazyStores.current[componentName] = stores;
250
+ }
251
+ }
252
+ if (typeof document !== "undefined") {
253
+ document.addEventListener("DOMContentLoaded", () => {
254
+ const promise = Object.keys(lazyStores.current).map(async (componentName) => {
255
+ const connectionSectionElement = document.querySelector(componentName);
256
+ if (connectionSectionElement) {
257
+ connectionSectionElement._stores = lazyStores.current[componentName];
258
+ delete lazyStores.current[componentName];
259
+ }
260
+ });
261
+ Promise.all(promise);
262
+ });
263
+ }