@radishland/runtime 0.4.2 → 0.6.0

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.
Files changed (2) hide show
  1. package/client/index.js +90 -121
  2. package/package.json +4 -4
package/client/index.js CHANGED
@@ -128,6 +128,11 @@ var object = (init) => {
128
128
  };
129
129
 
130
130
  // src/handler-registry.ts
131
+ var DirectiveEvent = class extends CustomEvent {
132
+ constructor(type2, detail) {
133
+ super(type2, { bubbles: true, composed: true, cancelable: true, detail });
134
+ }
135
+ };
131
136
  var HandlerRegistry = class extends HTMLElement {
132
137
  #cleanup = [];
133
138
  /**
@@ -154,47 +159,47 @@ var HandlerRegistry = class extends HTMLElement {
154
159
  return this[identifier];
155
160
  }
156
161
  #handleAttr(e) {
157
- if (e instanceof CustomEvent) {
158
- const { identifier, attribute, target } = e.detail;
159
- if (identifier in this && target instanceof HTMLElement && attribute in target) {
162
+ if (e instanceof DirectiveEvent) {
163
+ const { identifier, key, target } = e.detail;
164
+ if (identifier in this && target instanceof HTMLElement && key in target) {
160
165
  const ref = this.lookup(identifier);
161
166
  this.effect(() => {
162
- if (booleanAttributes.includes(attribute)) {
163
- target.toggleAttribute(attribute, ref.valueOf());
167
+ if (booleanAttributes.includes(key)) {
168
+ target.toggleAttribute(key, ref.valueOf());
164
169
  } else {
165
- target.setAttribute(attribute, `${ref}`);
170
+ target.setAttribute(key, `${ref}`);
166
171
  }
167
172
  });
168
- target.removeAttribute("attr:" + attribute);
173
+ target.removeAttribute("attr:" + key);
169
174
  e.stopPropagation();
170
175
  }
171
176
  }
172
177
  }
173
178
  #handleBool(e) {
174
- if (e instanceof CustomEvent) {
175
- const { identifier, attribute, target } = e.detail;
179
+ if (e instanceof DirectiveEvent) {
180
+ const { identifier, key, target } = e.detail;
176
181
  if (identifier in this && target instanceof HTMLElement) {
177
182
  const ref = this.lookup(identifier);
178
183
  this.effect(() => {
179
- target.toggleAttribute(attribute, ref.valueOf());
184
+ target.toggleAttribute(key, ref.valueOf());
180
185
  });
181
- target.removeAttribute("bool:" + attribute);
186
+ target.removeAttribute("bool:" + key);
182
187
  e.stopPropagation();
183
188
  }
184
189
  }
185
190
  }
186
191
  #handleOn(e) {
187
- if (e instanceof CustomEvent) {
188
- const { identifier, type: type2, target } = e.detail;
192
+ if (e instanceof DirectiveEvent) {
193
+ const { identifier, key, target } = e.detail;
189
194
  if (identifier in this && target instanceof HTMLElement && typeof this.lookup(identifier) === "function") {
190
- target.addEventListener(type2, this.lookup(identifier).bind(this));
191
- target.removeAttribute("on:" + type2);
195
+ target.addEventListener(key, this.lookup(identifier).bind(this));
196
+ target.removeAttribute("on:" + key);
192
197
  e.stopPropagation();
193
198
  }
194
199
  }
195
200
  }
196
201
  #handleClass(e) {
197
- if (e instanceof CustomEvent) {
202
+ if (e instanceof DirectiveEvent) {
198
203
  const { identifier, target } = e.detail;
199
204
  if (identifier in this && target instanceof HTMLElement) {
200
205
  this.effect(() => {
@@ -214,50 +219,50 @@ var HandlerRegistry = class extends HTMLElement {
214
219
  }
215
220
  }
216
221
  #handleUse(e) {
217
- if (e instanceof CustomEvent) {
218
- const { identifier, target } = e.detail;
219
- if (identifier in this && typeof this.lookup(identifier) === "function" && target instanceof HTMLElement) {
220
- const cleanup = this.lookup(identifier).bind(this)(target);
222
+ if (e instanceof DirectiveEvent) {
223
+ const { key, target } = e.detail;
224
+ if (key in this && typeof this.lookup(key) === "function" && target instanceof HTMLElement) {
225
+ const cleanup = this.lookup(key).bind(this)(target);
221
226
  if (typeof cleanup === "function") {
222
227
  this.#cleanup.push(cleanup);
223
228
  }
224
- target.removeAttribute("use:" + identifier);
229
+ target.removeAttribute("use:" + key);
225
230
  e.stopPropagation();
226
231
  }
227
232
  }
228
233
  }
229
234
  #handleProp(e) {
230
- if (e instanceof CustomEvent) {
231
- const { identifier, property, target } = e.detail;
232
- if (identifier in this && property in target && target instanceof HTMLElement) {
235
+ if (e instanceof DirectiveEvent) {
236
+ const { identifier, key, target } = e.detail;
237
+ if (identifier in this && key in target && target instanceof HTMLElement) {
233
238
  const ref = this.lookup(identifier);
234
239
  this.effect(() => {
235
- if (isState(target[property])) {
236
- target[property].value = ref.valueOf();
240
+ if (isState(target[key])) {
241
+ target[key].value = ref.valueOf();
237
242
  } else {
238
- target[property] = ref.valueOf();
243
+ target[key] = ref.valueOf();
239
244
  }
240
245
  });
241
- target.removeAttribute("prop:" + property);
246
+ target.removeAttribute("prop:" + key);
242
247
  e.stopPropagation();
243
248
  }
244
249
  }
245
250
  }
246
- #handleText(e) {
247
- if (e instanceof CustomEvent) {
251
+ #handleTextContent(e) {
252
+ if (e instanceof DirectiveEvent) {
248
253
  const { identifier, target } = e.detail;
249
254
  if (identifier in this && target instanceof HTMLElement) {
250
255
  const ref = this.lookup(identifier);
251
256
  this.effect(() => {
252
257
  target.textContent = `${ref}`;
253
258
  });
254
- target.removeAttribute("text");
259
+ target.removeAttribute("textContent");
255
260
  e.stopPropagation();
256
261
  }
257
262
  }
258
263
  }
259
264
  #handleHTML(e) {
260
- if (e instanceof CustomEvent) {
265
+ if (e instanceof DirectiveEvent) {
261
266
  const { identifier, target } = e.detail;
262
267
  if (identifier in this && target instanceof HTMLElement) {
263
268
  const ref = this.lookup(identifier);
@@ -270,20 +275,23 @@ var HandlerRegistry = class extends HTMLElement {
270
275
  }
271
276
  }
272
277
  #handleBind(e) {
273
- if (e instanceof CustomEvent) {
274
- const { identifier, property, target } = e.detail;
275
- if (identifier in this && target instanceof HTMLElement && property in target) {
278
+ if (e instanceof DirectiveEvent) {
279
+ const { identifier, key, target } = e.detail;
280
+ if (identifier in this && target instanceof HTMLElement && key in target) {
276
281
  const state = this.lookup(identifier);
277
282
  if (isState(state)) {
278
- state.value = target[property];
279
- target.addEventListener(bindingConfig[property].event, () => {
280
- state.value = target[property];
281
- });
283
+ state.value = target[key];
284
+ target.addEventListener(
285
+ bindingConfig[key].event,
286
+ () => {
287
+ state.value = target[key];
288
+ }
289
+ );
282
290
  this.effect(() => {
283
- target[property] = state.value;
291
+ target[key] = state.value;
284
292
  });
285
293
  }
286
- target.removeAttribute("bind:" + property);
294
+ target.removeAttribute("bind:" + key);
287
295
  e.stopPropagation();
288
296
  }
289
297
  }
@@ -295,30 +303,20 @@ var HandlerRegistry = class extends HTMLElement {
295
303
  for (const attribute of attr) {
296
304
  const [_, key] = attribute.localName.split(":");
297
305
  if (!key) throw new Error("Missing <key> in attr:<key>");
298
- const attrRequest = new CustomEvent("rad::attr", {
299
- bubbles: true,
300
- cancelable: true,
301
- composed: true,
302
- detail: {
303
- attribute: key,
304
- identifier: attribute.value || key,
305
- target: element
306
- }
306
+ const attrRequest = new DirectiveEvent("rad::attr", {
307
+ key,
308
+ identifier: attribute.value || key,
309
+ target: element
307
310
  });
308
311
  element.dispatchEvent(attrRequest);
309
312
  }
310
313
  for (const property of Object.keys(bindingConfig)) {
311
314
  if (element.hasAttribute(`bind:${property}`)) {
312
315
  const identifier = element.getAttribute(`bind:${property}`)?.trim() || property;
313
- const bindRequest = new CustomEvent("rad::bind", {
314
- bubbles: true,
315
- cancelable: true,
316
- composed: true,
317
- detail: {
318
- property,
319
- identifier,
320
- target: element
321
- }
316
+ const bindRequest = new DirectiveEvent("rad::bind", {
317
+ key: property,
318
+ identifier,
319
+ target: element
322
320
  });
323
321
  element.dispatchEvent(bindRequest);
324
322
  }
@@ -328,41 +326,28 @@ var HandlerRegistry = class extends HTMLElement {
328
326
  const [_, key] = bool.localName.split(":");
329
327
  if (!key) throw new Error("Missing <key> in bool:<key>");
330
328
  element.dispatchEvent(
331
- new CustomEvent("rad::bool", {
332
- bubbles: true,
333
- cancelable: true,
334
- composed: true,
335
- detail: {
336
- attribute: key,
337
- identifier: bool.value || key,
338
- target: element
339
- }
329
+ new DirectiveEvent("rad::bool", {
330
+ key,
331
+ identifier: bool.value || key,
332
+ target: element
340
333
  })
341
334
  );
342
335
  }
343
336
  const classList = element.getAttribute("classlist");
344
337
  if (classList) {
345
- const classRequest = new CustomEvent("rad::classlist", {
346
- bubbles: true,
347
- cancelable: true,
348
- composed: true,
349
- detail: {
350
- identifier: classList,
351
- target: element
352
- }
338
+ const classRequest = new DirectiveEvent("rad::classlist", {
339
+ identifier: classList,
340
+ key: "classList",
341
+ target: element
353
342
  });
354
343
  element.dispatchEvent(classRequest);
355
344
  }
356
345
  const html = element.getAttribute("html");
357
346
  if (html) {
358
- const htmlRequest = new CustomEvent("rad::html", {
359
- bubbles: true,
360
- cancelable: true,
361
- composed: true,
362
- detail: {
363
- identifier: html,
364
- target: element
365
- }
347
+ const htmlRequest = new DirectiveEvent("rad::html", {
348
+ identifier: html,
349
+ key: "innerHTML",
350
+ target: element
366
351
  });
367
352
  element.dispatchEvent(htmlRequest);
368
353
  }
@@ -370,15 +355,10 @@ var HandlerRegistry = class extends HTMLElement {
370
355
  for (const event of events) {
371
356
  const [_, type2] = event.localName.split(":");
372
357
  if (!type2) throw new Error("Missing <type> in on:<type>");
373
- const onRequest = new CustomEvent("rad::on", {
374
- bubbles: true,
375
- cancelable: true,
376
- composed: true,
377
- detail: {
378
- type: type2,
379
- identifier: event.value || type2,
380
- target: element
381
- }
358
+ const onRequest = new DirectiveEvent("rad::on", {
359
+ key: type2,
360
+ identifier: event.value || type2,
361
+ target: element
382
362
  });
383
363
  element.dispatchEvent(onRequest);
384
364
  }
@@ -386,28 +366,19 @@ var HandlerRegistry = class extends HTMLElement {
386
366
  for (const prop of props) {
387
367
  const [_, key] = prop.localName.split(":");
388
368
  if (!key) throw new Error("Missing <key> in prop:<key>");
389
- const propRequest = new CustomEvent("rad::prop", {
390
- bubbles: true,
391
- cancelable: true,
392
- composed: true,
393
- detail: {
394
- property: key,
395
- identifier: prop.value || key,
396
- target: element
397
- }
369
+ const propRequest = new DirectiveEvent("rad::prop", {
370
+ key,
371
+ identifier: prop.value || key,
372
+ target: element
398
373
  });
399
374
  element.dispatchEvent(propRequest);
400
375
  }
401
- const text = element.getAttribute("text");
402
- if (text) {
403
- const textRequest = new CustomEvent("rad::text", {
404
- bubbles: true,
405
- cancelable: true,
406
- composed: true,
407
- detail: {
408
- identifier: text,
409
- target: element
410
- }
376
+ const textContent = element.getAttribute("textContent");
377
+ if (textContent) {
378
+ const textRequest = new DirectiveEvent("rad::textContent", {
379
+ identifier: textContent,
380
+ key: "textContent",
381
+ target: element
411
382
  });
412
383
  element.dispatchEvent(textRequest);
413
384
  }
@@ -415,14 +386,10 @@ var HandlerRegistry = class extends HTMLElement {
415
386
  for (const hook of hooks) {
416
387
  const [_, identifier] = hook.localName.split(":");
417
388
  if (!identifier) throw new Error("Missing <id> in use:<id>");
418
- const useRequest = new CustomEvent("rad::use", {
419
- bubbles: true,
420
- cancelable: true,
421
- composed: true,
422
- detail: {
423
- identifier,
424
- target: element
425
- }
389
+ const useRequest = new DirectiveEvent("rad::use", {
390
+ key: identifier,
391
+ identifier: "",
392
+ target: element
426
393
  });
427
394
  element.dispatchEvent(useRequest);
428
395
  }
@@ -453,7 +420,9 @@ var HandlerRegistry = class extends HTMLElement {
453
420
  this.addEventListener("rad::html", this.#handleHTML, { signal: signal2 });
454
421
  this.addEventListener("rad::on", this.#handleOn, { signal: signal2 });
455
422
  this.addEventListener("rad::prop", this.#handleProp, { signal: signal2 });
456
- this.addEventListener("rad::text", this.#handleText, { signal: signal2 });
423
+ this.addEventListener("rad::textContent", this.#handleTextContent, {
424
+ signal: signal2
425
+ });
457
426
  this.addEventListener("rad::use", this.#handleUse, { signal: signal2 });
458
427
  }
459
428
  disconnectedCallback() {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@radishland/runtime",
3
- "version": "0.4.2",
3
+ "version": "0.6.0",
4
4
  "type": "module",
5
5
  "description": "The Radish runtime",
6
6
  "author": "Frédéric Crozatier",
@@ -33,11 +33,11 @@
33
33
  "runtime"
34
34
  ],
35
35
  "dependencies": {
36
- "@preact/signals-core": "^1.8.0"
36
+ "@preact/signals-core": "^1.10.0"
37
37
  },
38
38
  "devDependencies": {
39
- "tsup": "^8.4.0",
40
- "typescript": "^5.7.3"
39
+ "tsup": "^8.5.0",
40
+ "typescript": "^5.8.3"
41
41
  },
42
42
  "scripts": {
43
43
  "build": "tsup"