rosetack 0.1.3 → 0.1.4

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.
@@ -17,6 +17,7 @@ function state(key, initial) {
17
17
  const setter = (v) => {
18
18
  e.value = v;
19
19
  e.subs.forEach((fn) => fn());
20
+ if (persistedKeys.has(key)) writeStorage(key, v);
20
21
  };
21
22
  return [getter, setter];
22
23
  }
@@ -27,6 +28,37 @@ function setState(key, value) {
27
28
  const e = entry(key, value);
28
29
  e.value = value;
29
30
  e.subs.forEach((fn) => fn());
31
+ if (persistedKeys.has(key)) writeStorage(key, value);
32
+ }
33
+ var persistedKeys = /* @__PURE__ */ new Set();
34
+ function writeStorage(key, v) {
35
+ try {
36
+ globalThis.localStorage?.setItem("rosetack:" + key, JSON.stringify(v));
37
+ } catch {
38
+ }
39
+ }
40
+ function persistState(key, initial) {
41
+ persistedKeys.add(key);
42
+ let seeded = initial;
43
+ let hasStored = false;
44
+ try {
45
+ const raw = globalThis.localStorage?.getItem("rosetack:" + key);
46
+ if (raw != null) {
47
+ seeded = JSON.parse(raw);
48
+ hasStored = true;
49
+ }
50
+ } catch {
51
+ }
52
+ const [get, set] = state(key, seeded);
53
+ if (hasStored) set(seeded);
54
+ return [get, set];
55
+ }
56
+ var lastBoundaryError = null;
57
+ function $setBoundaryError(e) {
58
+ lastBoundaryError = e instanceof Error ? e : new Error(String(e));
59
+ }
60
+ function $boundaryError() {
61
+ return lastBoundaryError;
30
62
  }
31
63
  var cleanups = /* @__PURE__ */ new Set();
32
64
  var fxOwner = null;
@@ -101,7 +133,11 @@ function textMark(closes, fn, pair, arg) {
101
133
  }
102
134
  function attrMark(closes, fn, name, arg) {
103
135
  const i = closes.push(fn) - 1;
104
- return `${name}="${esc(fn(arg))}" data-b="${i}:${name}"`;
136
+ return `${name}="${esc(fn(arg))}" data-b${i}="${name}"`;
137
+ }
138
+ function boolAttrMark(closes, fn, name, arg) {
139
+ const i = closes.push(fn) - 1;
140
+ return (fn(arg) ? ` ${name}=""` : "") + ` data-b${i}="!${name}"`;
105
141
  }
106
142
  function ifMark(closes, cond, fn, scope) {
107
143
  let snap = null;
@@ -119,8 +155,8 @@ function ifMark(closes, cond, fn, scope) {
119
155
  return `<!--\u27E6i:${i}\u27E7-->` + (r ? r.html : "") + `<!--\u27E6/i:${i}\u27E7-->`;
120
156
  }
121
157
  function eachMark(closes, items, fn) {
122
- const i = closes.push(() => items().map((v) => {
123
- return { html: String(fn(v, closes)), closes, scope: v };
158
+ const i = closes.push(() => items().map((v, vi) => {
159
+ return { html: String(fn(v, closes, vi)), closes, scope: v };
124
160
  })) - 1;
125
161
  let h = `<!--\u27E6l:${i}\u27E7-->`;
126
162
  for (const r of closes[i]()) h += r.html;
@@ -146,6 +182,7 @@ function slotBlockMark(closes, hit) {
146
182
  var MARK_RE = /^\u27e6([milh]):(\d+)\u27e7$/;
147
183
  var END_RE = /^\u27e6\/([milh]):(\d+)\u27e7$/;
148
184
  var wired = /* @__PURE__ */ new WeakSet();
185
+ var pendingIslandObservers = /* @__PURE__ */ new Set();
149
186
  function tpl(html) {
150
187
  const t = document.createElement("template");
151
188
  t.innerHTML = html;
@@ -153,6 +190,13 @@ function tpl(html) {
153
190
  }
154
191
  var HEAD_ATTR = "data-rosetack-head";
155
192
  var handlers = globalThis.__rosetack_handlers ??= {};
193
+ var actionDispatch = null;
194
+ function setActionDispatcher(fn) {
195
+ actionDispatch = fn;
196
+ }
197
+ var directBound = /* @__PURE__ */ new WeakMap();
198
+ var directives = globalThis.__rosetack_directives ??= {};
199
+ var actionRecords = /* @__PURE__ */ new WeakMap();
156
200
  function applyHead(html) {
157
201
  const seen = /* @__PURE__ */ new Set();
158
202
  const positional = /* @__PURE__ */ new Map();
@@ -238,12 +282,39 @@ function wire(root, closes, scope) {
238
282
  }
239
283
  const headBlocks = [];
240
284
  const ownsHead = root === document.getElementById("app");
285
+ let islandSkip = false;
286
+ if (ownsHead) {
287
+ const islandEls = Array.from(
288
+ holder.querySelectorAll("[data-rv-island]")
289
+ ).filter((el) => !(el.parentElement && el.parentElement.closest("[data-rv-island]")));
290
+ islandSkip = islandEls.length > 0;
291
+ for (const isl of islandEls) {
292
+ const fire = () => wire(isl, closes, scope);
293
+ const mode = isl.getAttribute("data-rv-island-hydrate");
294
+ if (mode === "visible" && typeof IntersectionObserver !== "undefined") {
295
+ const io = new IntersectionObserver((entries) => {
296
+ if (entries.some((en) => en.isIntersecting)) {
297
+ io.disconnect();
298
+ pendingIslandObservers.delete(io);
299
+ fire();
300
+ }
301
+ });
302
+ io.observe(isl);
303
+ pendingIslandObservers.add(io);
304
+ } else if (mode === "idle" && typeof globalThis.requestIdleCallback === "function") {
305
+ globalThis.requestIdleCallback(() => fire());
306
+ } else {
307
+ fire();
308
+ }
309
+ }
310
+ }
241
311
  for (const c of comments) {
242
312
  if (nested.has(c) || wired.has(c) || !c.parentNode) continue;
243
313
  const m = (c.nodeValue ?? "").match(MARK_RE);
244
314
  if (!m) continue;
245
315
  const kind = m[1];
246
316
  const idx = +m[2];
317
+ if (islandSkip && kind !== "h" && c.parentNode.closest?.("[data-rv-island]")) continue;
247
318
  if (kind === "h" && !ownsHead) continue;
248
319
  wired.add(c);
249
320
  if (kind === "h") {
@@ -354,17 +425,119 @@ function wire(root, closes, scope) {
354
425
  }
355
426
  } else if (root.nodeType !== 11) clearHead();
356
427
  }
357
- const els = holder.querySelectorAll("[data-b]");
358
- els.forEach((el) => {
359
- const spec = el.getAttribute("data-b");
360
- const sep = spec.indexOf(":");
361
- const idx = +spec.slice(0, sep);
362
- const attr = spec.slice(sep + 1);
363
- el.removeAttribute("data-b");
364
- weff(() => {
365
- el.setAttribute(attr, String(closes[idx](scope)));
366
- });
428
+ const all = holder.querySelectorAll("*");
429
+ all.forEach((el) => {
430
+ if (islandSkip && el.closest("[data-rv-island]")) return;
431
+ for (const an of el.getAttributeNames()) {
432
+ if (!an.startsWith("data-b")) continue;
433
+ const idx = Number(an.slice(6));
434
+ if (!Number.isFinite(idx)) continue;
435
+ const attr = el.getAttribute(an);
436
+ el.removeAttribute(an);
437
+ const effect2 = () => String(closes[idx](scope));
438
+ if (attr === "data-use-param") {
439
+ weff(() => {
440
+ const raw = effect2();
441
+ let p = raw;
442
+ try {
443
+ p = JSON.parse(raw);
444
+ } catch {
445
+ }
446
+ actionRecords.get(el)?.update?.(p);
447
+ });
448
+ continue;
449
+ }
450
+ if (attr === "value") {
451
+ weff(() => {
452
+ const v = effect2();
453
+ const inp = el;
454
+ if (inp.value !== v) inp.value = v;
455
+ });
456
+ } else if (attr.startsWith("!")) {
457
+ const name = attr.slice(1);
458
+ weff(() => {
459
+ const v = closes[idx](scope);
460
+ const live = !(v === false || v == null || v === "" || v === "false" || v === 0);
461
+ const target = el;
462
+ if (target[name] !== live) target[name] = live;
463
+ if (live) el.setAttribute(name, "");
464
+ else el.removeAttribute(name);
465
+ });
466
+ } else {
467
+ weff(() => {
468
+ el.setAttribute(attr, effect2());
469
+ });
470
+ }
471
+ }
367
472
  });
473
+ const uses = holder.querySelectorAll("[data-use]");
474
+ uses.forEach((el) => {
475
+ if (islandSkip && el.closest("[data-rv-island]")) return;
476
+ const name = el.getAttribute("data-use");
477
+ const fn = directives[name];
478
+ if (!fn) {
479
+ console.warn(`rosetack: use:${name} has no registered action (directives.${name}?)`);
480
+ return;
481
+ }
482
+ const rawParam = el.getAttribute("data-use-param");
483
+ let initial = rawParam ?? void 0;
484
+ if (typeof initial === "string") {
485
+ try {
486
+ initial = JSON.parse(initial);
487
+ } catch {
488
+ }
489
+ }
490
+ const ret = fn(el, initial);
491
+ if (typeof ret === "function") created.push(ret);
492
+ else if (ret && typeof ret === "object") {
493
+ actionRecords.set(el, { update: ret.update, destroy: ret.destroy });
494
+ if (ret.destroy) created.push(ret.destroy);
495
+ }
496
+ });
497
+ const refs = holder.querySelectorAll("[data-bind-this]");
498
+ refs.forEach((el) => {
499
+ if (islandSkip && el.closest("[data-rv-island]")) return;
500
+ const key = el.getAttribute("data-bind-this");
501
+ setState(key, el);
502
+ created.push(() => setState(key, null));
503
+ });
504
+ const DELEGATED_SET = /* @__PURE__ */ new Set(["click", "input", "change", "submit"]);
505
+ const evRoot = holder;
506
+ const evTargets = [evRoot, ...Array.from(evRoot.querySelectorAll("*"))];
507
+ for (const el of evTargets) {
508
+ if (!el.getAttributeNames) continue;
509
+ if (islandSkip && el.closest && el.closest("[data-rv-island]")) continue;
510
+ for (const an of el.getAttributeNames()) {
511
+ const mm = /^data-on(w|d)?-(.+)$/.exec(an);
512
+ if (!mm) continue;
513
+ const ev = mm[2];
514
+ if (!mm[1] && DELEGATED_SET.has(ev)) continue;
515
+ const g = globalThis;
516
+ const win = g.window ?? g.document?.defaultView;
517
+ const target = mm[1] === "w" ? win : mm[1] === "d" ? g.document : el;
518
+ if (!target) continue;
519
+ let seen = directBound.get(el);
520
+ if (!seen) directBound.set(el, seen = /* @__PURE__ */ new Set());
521
+ if (seen.has(ev)) continue;
522
+ seen.add(ev);
523
+ const spec = el.getAttribute(an);
524
+ const listener = (e) => {
525
+ if (!el.hasAttribute(an)) return;
526
+ if (spec.startsWith("$action:")) {
527
+ if (actionDispatch) actionDispatch(spec.slice("$action:".length), e);
528
+ return;
529
+ }
530
+ const h = handlers[spec];
531
+ if (h) h(e);
532
+ };
533
+ target.addEventListener(ev, listener);
534
+ created.push(() => {
535
+ target.removeEventListener(ev, listener);
536
+ const s = directBound.get(el);
537
+ if (s) s.delete(ev);
538
+ });
539
+ }
540
+ }
368
541
  if (holder !== root) {
369
542
  while (holder.firstChild) root.appendChild(holder.firstChild);
370
543
  }
@@ -373,6 +546,8 @@ function wire(root, closes, scope) {
373
546
  function resetEffects() {
374
547
  cleanups.forEach((fn) => fn());
375
548
  cleanups.clear();
549
+ pendingIslandObservers.forEach((io) => io.disconnect());
550
+ pendingIslandObservers.clear();
376
551
  }
377
552
  var dataCache = /* @__PURE__ */ new Map();
378
553
  function $data(fn) {
@@ -716,6 +891,7 @@ export {
716
891
  $actionError,
717
892
  $actionErrorOrThrow,
718
893
  $append,
894
+ $boundaryError,
719
895
  $boundaryFallback,
720
896
  $cookies,
721
897
  $data,
@@ -723,6 +899,7 @@ export {
723
899
  $prepend,
724
900
  $query,
725
901
  $sessionCookie,
902
+ $setBoundaryError,
726
903
  $store,
727
904
  $t,
728
905
  ActionError,
@@ -736,9 +913,11 @@ export {
736
913
  applyStorePatch,
737
914
  attrMark,
738
915
  bestLocale,
916
+ boolAttrMark,
739
917
  clearHead,
740
918
  clearMounts,
741
919
  clearRequestState,
920
+ directives,
742
921
  eachMark,
743
922
  effect,
744
923
  ensureLocale,
@@ -757,6 +936,7 @@ export {
757
936
  notFound,
758
937
  onCleanup,
759
938
  onMount,
939
+ persistState,
760
940
  rawMark,
761
941
  redirect,
762
942
  refresh,
@@ -767,6 +947,7 @@ export {
767
947
  restoreState,
768
948
  resumeState,
769
949
  serializeState,
950
+ setActionDispatcher,
770
951
  setFxOwner,
771
952
  setLocalePacks,
772
953
  setLocales,