@yannosay/sinth 4.0.0 → 5.1.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 (78) hide show
  1. package/CHANGELOG.md +83 -0
  2. package/DOCS.md +1359 -0
  3. package/README.md +83 -74
  4. package/bin/sinth.js +2 -1
  5. package/dist/cli/args.d.ts +11 -0
  6. package/dist/cli/args.js +74 -0
  7. package/dist/cli/index.d.ts +1 -0
  8. package/dist/cli/index.js +437 -0
  9. package/dist/cli/templates.d.ts +10 -0
  10. package/dist/cli/templates.js +193 -0
  11. package/dist/cli/ui.d.ts +28 -0
  12. package/dist/cli/ui.js +61 -0
  13. package/dist/cli/update-check.d.ts +2 -0
  14. package/dist/cli/update-check.js +124 -0
  15. package/dist/compiler/ast.d.ts +315 -0
  16. package/dist/compiler/ast.js +4 -0
  17. package/dist/compiler/codegen.d.ts +143 -0
  18. package/dist/compiler/codegen.js +1326 -0
  19. package/dist/compiler/diagnostics.d.ts +36 -0
  20. package/dist/compiler/diagnostics.js +142 -0
  21. package/dist/compiler/elements.d.ts +14 -0
  22. package/dist/compiler/elements.js +78 -0
  23. package/dist/compiler/globals.d.ts +2 -0
  24. package/dist/compiler/globals.js +27 -0
  25. package/dist/compiler/html.d.ts +37 -0
  26. package/dist/compiler/html.js +152 -0
  27. package/dist/compiler/lexer.d.ts +42 -0
  28. package/dist/compiler/lexer.js +404 -0
  29. package/dist/compiler/parser.d.ts +95 -0
  30. package/dist/compiler/parser.js +1374 -0
  31. package/dist/compiler/prerender.d.ts +72 -0
  32. package/dist/compiler/prerender.js +910 -0
  33. package/dist/compiler/script-analysis.d.ts +6 -0
  34. package/dist/compiler/script-analysis.js +106 -0
  35. package/dist/compiler/style.d.ts +39 -0
  36. package/dist/compiler/style.js +463 -0
  37. package/dist/deploy/cloudflare.d.ts +9 -0
  38. package/dist/deploy/cloudflare.js +111 -0
  39. package/dist/index.d.ts +13 -0
  40. package/dist/index.js +23 -0
  41. package/dist/project/build.d.ts +31 -0
  42. package/dist/project/build.js +425 -0
  43. package/dist/project/config.d.ts +33 -0
  44. package/dist/project/config.js +142 -0
  45. package/dist/project/project.d.ts +74 -0
  46. package/dist/project/project.js +535 -0
  47. package/dist/runtime/index.d.ts +68 -0
  48. package/dist/runtime/index.js +912 -0
  49. package/dist/server/client.d.ts +1 -0
  50. package/dist/server/client.js +112 -0
  51. package/dist/server/dev.d.ts +8 -0
  52. package/dist/server/dev.js +244 -0
  53. package/dist/server/preview.d.ts +6 -0
  54. package/dist/server/preview.js +106 -0
  55. package/dist/server/static.d.ts +10 -0
  56. package/dist/server/static.js +136 -0
  57. package/lib/ui/index.sinth +436 -0
  58. package/package.json +33 -15
  59. package/schema/sinth.config.schema.json +54 -0
  60. package/dist/browser/compile.js +0 -62
  61. package/dist/cli.js +0 -509
  62. package/dist/core/builtins.js +0 -117
  63. package/dist/core/cli-compiler.js +0 -683
  64. package/dist/core/compiler.js +0 -1428
  65. package/dist/core/expr.js +0 -125
  66. package/dist/core/head-builder.js +0 -130
  67. package/dist/core/lexer.js +0 -414
  68. package/dist/core/parser.js +0 -1665
  69. package/dist/core/runtime/custom-element.js +0 -81
  70. package/dist/core/runtime/functions.js +0 -99
  71. package/dist/core/runtime/helpers.js +0 -347
  72. package/dist/core/runtime/render.js +0 -136
  73. package/dist/core/style-processor.js +0 -317
  74. package/dist/core/types.js +0 -93
  75. package/dist/resolver.js +0 -175
  76. package/dist/server.js +0 -174
  77. package/dist/update-check.js +0 -109
  78. package/dist/utils.js +0 -80
@@ -0,0 +1,912 @@
1
+ const SVG_NS = "http://www.w3.org/2000/svg";
2
+ const BLOCKED_URL = /^\s*(javascript|vbscript|data:(?!image\/(png|gif|jpe?g|webp|avif|svg\+xml)|video\/|audio\/))/i;
3
+ let rootOwner = createOwner(null);
4
+ let current = rootOwner;
5
+ let updating = false;
6
+ let rerun = false;
7
+ let scheduled = false;
8
+ let devMode = false;
9
+ function createOwner(parent) {
10
+ const owner = { parent, effects: [], children: [], cleanups: [], disposed: false, paused: false };
11
+ if (parent)
12
+ parent.children.push(owner);
13
+ return owner;
14
+ }
15
+ function disposeOwner(owner) {
16
+ if (owner.disposed)
17
+ return;
18
+ owner.disposed = true;
19
+ for (const child of owner.children.slice())
20
+ disposeOwner(child);
21
+ for (const fn of owner.cleanups) {
22
+ try {
23
+ fn();
24
+ }
25
+ catch (err) {
26
+ reportError(err);
27
+ }
28
+ }
29
+ owner.children.length = 0;
30
+ owner.effects.length = 0;
31
+ owner.cleanups.length = 0;
32
+ const parent = owner.parent;
33
+ if (parent) {
34
+ const i = parent.children.indexOf(owner);
35
+ if (i !== -1)
36
+ parent.children.splice(i, 1);
37
+ }
38
+ }
39
+ function withOwner(owner, fn) {
40
+ const prev = current;
41
+ current = owner;
42
+ try {
43
+ return fn();
44
+ }
45
+ finally {
46
+ current = prev;
47
+ }
48
+ }
49
+ function reportError(err) {
50
+ console.error("[sinth]", err);
51
+ const hook = globalThis.__sinth_error__;
52
+ if (hook)
53
+ hook(err);
54
+ }
55
+ function effect(fn) {
56
+ const owner = current;
57
+ const guarded = () => {
58
+ try {
59
+ fn();
60
+ }
61
+ catch (err) {
62
+ reportError(err);
63
+ }
64
+ };
65
+ owner.effects.push(guarded);
66
+ guarded();
67
+ }
68
+ function runOwner(owner) {
69
+ if (owner.disposed || owner.paused)
70
+ return;
71
+ const effects = owner.effects.slice();
72
+ for (const fn of effects) {
73
+ if (owner.disposed)
74
+ return;
75
+ fn();
76
+ }
77
+ for (const child of owner.children.slice())
78
+ runOwner(child);
79
+ }
80
+ export function update() {
81
+ if (updating) {
82
+ rerun = true;
83
+ return;
84
+ }
85
+ updating = true;
86
+ try {
87
+ let passes = 0;
88
+ do {
89
+ rerun = false;
90
+ runOwner(rootOwner);
91
+ if (++passes > 50) {
92
+ console.warn("[sinth] Updates keep changing state (possible infinite loop). Stopped after 50 passes.");
93
+ break;
94
+ }
95
+ } while (rerun);
96
+ }
97
+ finally {
98
+ updating = false;
99
+ }
100
+ }
101
+ export function schedule() {
102
+ if (scheduled)
103
+ return;
104
+ scheduled = true;
105
+ queueMicrotask(() => {
106
+ scheduled = false;
107
+ update();
108
+ });
109
+ }
110
+ function settle(result) {
111
+ if (result && typeof result.then === "function") {
112
+ result.then(schedule, (err) => {
113
+ reportError(err);
114
+ schedule();
115
+ });
116
+ }
117
+ }
118
+ export function toText(value) {
119
+ if (value === null || value === undefined || value === false)
120
+ return "";
121
+ if (typeof value === "string")
122
+ return value;
123
+ if (typeof value === "number")
124
+ return String(value);
125
+ if (Array.isArray(value))
126
+ return value.map(toText).join(", ");
127
+ if (value instanceof Date)
128
+ return value.toLocaleString();
129
+ if (typeof value === "object") {
130
+ try {
131
+ return JSON.stringify(value);
132
+ }
133
+ catch {
134
+ return String(value);
135
+ }
136
+ }
137
+ return String(value);
138
+ }
139
+ export function el(tag) {
140
+ return document.createElement(tag);
141
+ }
142
+ export function svg(tag) {
143
+ return document.createElementNS(SVG_NS, tag);
144
+ }
145
+ const templateCache = new Map();
146
+ export function tpl(html, svgContext = false) {
147
+ let t = templateCache.get(html);
148
+ if (!t) {
149
+ t = document.createElement("template");
150
+ t.innerHTML = svgContext ? `<svg>${html}</svg>` : html;
151
+ templateCache.set(html, t);
152
+ }
153
+ const clone = t.content.cloneNode(true);
154
+ if (svgContext) {
155
+ const frag = document.createDocumentFragment();
156
+ const wrapper = clone.firstChild;
157
+ while (wrapper.firstChild)
158
+ frag.appendChild(wrapper.firstChild);
159
+ return frag;
160
+ }
161
+ return clone;
162
+ }
163
+ export function text(parent, get) {
164
+ const node = document.createTextNode("");
165
+ parent.appendChild(node);
166
+ let last;
167
+ effect(() => {
168
+ const value = toText(get());
169
+ if (value !== last) {
170
+ last = value;
171
+ node.data = value;
172
+ }
173
+ });
174
+ }
175
+ export function sanitizeUrl(value) {
176
+ if (BLOCKED_URL.test(value)) {
177
+ if (devMode)
178
+ console.warn(`[sinth] Blocked unsafe URL: ${value.slice(0, 64)}`);
179
+ return "about:blank#blocked";
180
+ }
181
+ return value;
182
+ }
183
+ const URL_ATTRS = new Set(["href", "src", "action", "formaction", "poster", "cite", "background", "xlink:href", "data"]);
184
+ function setAttr(node, name, value) {
185
+ if (value === null || value === undefined || value === false) {
186
+ node.removeAttribute(name);
187
+ return;
188
+ }
189
+ if (value === true) {
190
+ node.setAttribute(name, "");
191
+ return;
192
+ }
193
+ const str = typeof value === "object" ? JSON.stringify(value) : String(value);
194
+ node.setAttribute(name, URL_ATTRS.has(name) ? sanitizeUrl(str) : str);
195
+ }
196
+ export function attr(node, name, get) {
197
+ if (/^on/i.test(name))
198
+ throw new Error(`[sinth] Refusing to bind event attribute '${name}' as a string`);
199
+ let last = {};
200
+ effect(() => {
201
+ const value = get();
202
+ if (value !== last) {
203
+ last = value;
204
+ setAttr(node, name, value);
205
+ }
206
+ });
207
+ }
208
+ export function prop(node, name, get) {
209
+ const target = node;
210
+ effect(() => {
211
+ const value = get();
212
+ if (target[name] !== value)
213
+ target[name] = value;
214
+ });
215
+ }
216
+ export function classValue(value) {
217
+ if (!value)
218
+ return "";
219
+ if (typeof value === "string")
220
+ return value;
221
+ if (Array.isArray(value))
222
+ return value.map(classValue).filter(Boolean).join(" ");
223
+ if (typeof value === "object") {
224
+ return Object.entries(value).filter(([, on]) => !!on).map(([k]) => k).join(" ");
225
+ }
226
+ return String(value);
227
+ }
228
+ export function cls(node, get) {
229
+ let applied = [];
230
+ effect(() => {
231
+ const next = classValue(get()).split(/\s+/).filter(Boolean);
232
+ for (const c of applied)
233
+ if (!next.includes(c))
234
+ node.classList.remove(c);
235
+ for (const c of next)
236
+ if (!applied.includes(c))
237
+ node.classList.add(c);
238
+ applied = next;
239
+ });
240
+ }
241
+ function kebab(name) {
242
+ if (name.startsWith("--"))
243
+ return name;
244
+ return name.replace(/^(Webkit|Moz|Ms)/, m => "-" + m.toLowerCase()).replace(/[A-Z]/g, m => "-" + m.toLowerCase());
245
+ }
246
+ function styleText(value) {
247
+ if (value === null || value === undefined || value === false)
248
+ return "";
249
+ if (typeof value === "number")
250
+ return String(value);
251
+ return String(value);
252
+ }
253
+ export function style(node, get) {
254
+ let applied = new Set();
255
+ effect(() => {
256
+ const value = get();
257
+ if (typeof value === "string" || value === null || value === undefined) {
258
+ const css = value ?? "";
259
+ if (node.style.cssText !== css)
260
+ node.style.cssText = css;
261
+ applied = new Set();
262
+ return;
263
+ }
264
+ const next = new Set();
265
+ for (const [k, v] of Object.entries(value)) {
266
+ const name = kebab(k);
267
+ next.add(name);
268
+ const str = styleText(v);
269
+ if (node.style.getPropertyValue(name) !== str)
270
+ node.style.setProperty(name, str);
271
+ }
272
+ for (const name of applied)
273
+ if (!next.has(name))
274
+ node.style.removeProperty(name);
275
+ applied = next;
276
+ });
277
+ }
278
+ export function styleProp(node, name, get) {
279
+ const prop = kebab(name);
280
+ effect(() => {
281
+ const str = styleText(get());
282
+ if (node.style.getPropertyValue(prop) !== str) {
283
+ if (str)
284
+ node.style.setProperty(prop, str);
285
+ else
286
+ node.style.removeProperty(prop);
287
+ }
288
+ });
289
+ }
290
+ export function cssVars(nodes, vars) {
291
+ for (const node of nodes) {
292
+ if (!node || !("style" in node))
293
+ continue;
294
+ for (const [name, get] of Object.entries(vars))
295
+ styleProp(node, name, get);
296
+ }
297
+ }
298
+ export function show(node, get) {
299
+ let initial = null;
300
+ effect(() => {
301
+ const visible = !!get();
302
+ if (initial === null)
303
+ initial = node.style.display === "none" ? "" : node.style.display;
304
+ const next = visible ? initial : "none";
305
+ if (node.style.display !== next)
306
+ node.style.display = next;
307
+ });
308
+ }
309
+ const KEY_ALIASES = {
310
+ enter: ["Enter"], esc: ["Escape"], escape: ["Escape"], space: [" ", "Spacebar"], tab: ["Tab"],
311
+ up: ["ArrowUp"], down: ["ArrowDown"], left: ["ArrowLeft"], right: ["ArrowRight"], delete: ["Delete", "Backspace"],
312
+ backspace: ["Backspace"],
313
+ };
314
+ const LISTENER_OPTIONS = new Set(["capture", "passive", "once"]);
315
+ export function on(node, event, handler, modifiers = []) {
316
+ const keys = modifiers.filter(m => KEY_ALIASES[m] || /^key[A-Z0-9]/.test(m));
317
+ const systemKeys = modifiers.filter(m => m === "ctrl" || m === "shift" || m === "alt" || m === "meta");
318
+ const listener = (e) => {
319
+ if (modifiers.includes("self") && e.target !== e.currentTarget)
320
+ return;
321
+ if (keys.length) {
322
+ const key = e.key;
323
+ const ok = keys.some(k => (KEY_ALIASES[k] ? KEY_ALIASES[k].includes(key) : key.toLowerCase() === k.slice(3).toLowerCase()));
324
+ if (!ok)
325
+ return;
326
+ }
327
+ for (const sk of systemKeys)
328
+ if (!e[`${sk}Key`])
329
+ return;
330
+ if (modifiers.includes("prevent"))
331
+ e.preventDefault();
332
+ if (modifiers.includes("stop"))
333
+ e.stopPropagation();
334
+ let result;
335
+ try {
336
+ result = handler(e);
337
+ }
338
+ catch (err) {
339
+ reportError(err);
340
+ }
341
+ update();
342
+ settle(result);
343
+ };
344
+ const options = {};
345
+ for (const m of modifiers)
346
+ if (LISTENER_OPTIONS.has(m))
347
+ options[m] = true;
348
+ node.addEventListener(event, listener, options);
349
+ const owner = current;
350
+ if (node === window || node === document)
351
+ owner.cleanups.push(() => node.removeEventListener(event, listener, options));
352
+ }
353
+ export function model(node, get, set, numeric = false) {
354
+ const input = node;
355
+ const tag = node.tagName;
356
+ const type = (input.type || "").toLowerCase();
357
+ if (type === "checkbox") {
358
+ effect(() => {
359
+ const v = get();
360
+ const checked = Array.isArray(v) ? v.includes(input.value) : !!v;
361
+ if (input.checked !== checked)
362
+ input.checked = checked;
363
+ });
364
+ on(node, "change", () => {
365
+ const v = get();
366
+ if (Array.isArray(v)) {
367
+ const next = v.filter(x => x !== input.value);
368
+ if (input.checked)
369
+ next.push(input.value);
370
+ set(next);
371
+ }
372
+ else
373
+ set(input.checked);
374
+ });
375
+ return;
376
+ }
377
+ if (type === "radio") {
378
+ effect(() => {
379
+ const checked = String(get()) === input.value;
380
+ if (input.checked !== checked)
381
+ input.checked = checked;
382
+ });
383
+ on(node, "change", () => {
384
+ if (input.checked)
385
+ set(numeric ? Number(input.value) : input.value);
386
+ });
387
+ return;
388
+ }
389
+ if (tag === "SELECT" && node.multiple) {
390
+ const select = node;
391
+ effect(() => {
392
+ const v = get();
393
+ const values = Array.isArray(v) ? v.map(String) : [];
394
+ for (const opt of Array.from(select.options))
395
+ opt.selected = values.includes(opt.value);
396
+ });
397
+ on(node, "change", () => set(Array.from(select.selectedOptions).map(o => (numeric ? Number(o.value) : o.value))));
398
+ return;
399
+ }
400
+ const isNumber = numeric || type === "number" || type === "range";
401
+ effect(() => {
402
+ const v = get();
403
+ const str = v === null || v === undefined ? "" : String(v);
404
+ if (input.value !== str && !(isNumber && Number(input.value) === v && input.value !== "")) {
405
+ input.value = str;
406
+ }
407
+ });
408
+ const evt = tag === "SELECT" || type === "range" || type === "color" || type === "date" || type === "file" ? "change" : "input";
409
+ on(node, evt, () => {
410
+ if (!isNumber) {
411
+ set(input.value);
412
+ return;
413
+ }
414
+ if (input.value === "")
415
+ return;
416
+ const n = Number(input.value);
417
+ if (!Number.isNaN(n))
418
+ set(n);
419
+ });
420
+ if (evt === "change" && type === "range")
421
+ on(node, "input", () => set(Number(input.value)));
422
+ }
423
+ export function ref(node, set) {
424
+ set(node);
425
+ current.cleanups.push(() => set(null));
426
+ }
427
+ function makeRange(parent, label) {
428
+ const start = document.createComment(devMode ? label : "");
429
+ const end = document.createComment(devMode ? "/" + label : "");
430
+ parent.appendChild(start);
431
+ parent.appendChild(end);
432
+ return [start, end];
433
+ }
434
+ function clearRange(start, end) {
435
+ const parent = end.parentNode;
436
+ if (!parent)
437
+ return;
438
+ while (start.nextSibling && start.nextSibling !== end)
439
+ parent.removeChild(start.nextSibling);
440
+ }
441
+ function takeRange(start, end) {
442
+ const frag = document.createDocumentFragment();
443
+ while (start.nextSibling && start.nextSibling !== end)
444
+ frag.appendChild(start.nextSibling);
445
+ return frag;
446
+ }
447
+ function renderInto(owner, render) {
448
+ const frag = document.createDocumentFragment();
449
+ withOwner(owner, () => render(frag));
450
+ return frag;
451
+ }
452
+ export function when(parent, branches, options = {}) {
453
+ const [start, end] = makeRange(parent, "if");
454
+ const owner = current;
455
+ let active = -2;
456
+ let branch = null;
457
+ const cache = new Map();
458
+ const remove = (b, index) => {
459
+ if (options.persist) {
460
+ b.owner.paused = true;
461
+ b.saved = takeRange(start, end);
462
+ cache.set(index, b);
463
+ return;
464
+ }
465
+ const delay = options.delay ? Number(options.delay()) : 0;
466
+ if (delay > 0 || options.transition) {
467
+ const leaving = Array.from(takeRange(start, end).childNodes);
468
+ for (const n of leaving) {
469
+ start.parentNode?.insertBefore(n, start);
470
+ if (n instanceof Element && options.transition)
471
+ n.classList.add(`${options.transition}-leave`);
472
+ }
473
+ setTimeout(() => {
474
+ for (const n of leaving)
475
+ n.parentNode?.removeChild(n);
476
+ disposeOwner(b.owner);
477
+ }, delay || 200);
478
+ return;
479
+ }
480
+ clearRange(start, end);
481
+ disposeOwner(b.owner);
482
+ };
483
+ effect(() => {
484
+ let next = -1;
485
+ for (let i = 0; i < branches.length; i++) {
486
+ const test = branches[i][0];
487
+ if (test === null || test()) {
488
+ next = i;
489
+ break;
490
+ }
491
+ }
492
+ if (next === active)
493
+ return;
494
+ if (branch)
495
+ remove(branch, active);
496
+ active = next;
497
+ branch = null;
498
+ if (next === -1)
499
+ return;
500
+ const cached = cache.get(next);
501
+ if (cached && cached.saved) {
502
+ cached.owner.paused = false;
503
+ end.parentNode?.insertBefore(cached.saved, end);
504
+ cached.saved = null;
505
+ cache.delete(next);
506
+ branch = cached;
507
+ return;
508
+ }
509
+ const childOwner = createOwner(owner);
510
+ const frag = renderInto(childOwner, branches[next][1]);
511
+ if (options.transition) {
512
+ for (const n of Array.from(frag.childNodes)) {
513
+ if (n instanceof Element) {
514
+ n.classList.add(`${options.transition}-enter`);
515
+ requestAnimationFrame(() => requestAnimationFrame(() => n.classList.remove(`${options.transition}-enter`)));
516
+ }
517
+ }
518
+ }
519
+ end.parentNode?.insertBefore(frag, end);
520
+ branch = { owner: childOwner, saved: null };
521
+ });
522
+ }
523
+ export function iterate(value) {
524
+ if (value === null || value === undefined || value === false)
525
+ return [];
526
+ if (typeof value === "number")
527
+ return range(value).map((n, i) => ({ item: n, index: i, key: i }));
528
+ if (Array.isArray(value))
529
+ return value.map((item, index) => ({ item, index, key: index }));
530
+ if (value instanceof Map)
531
+ return Array.from(value.entries()).map(([key, item], index) => ({ item, index, key }));
532
+ if (typeof value[Symbol.iterator] === "function" && typeof value !== "string") {
533
+ return Array.from(value).map((item, index) => ({ item, index, key: index }));
534
+ }
535
+ if (typeof value === "string")
536
+ return Array.from(value).map((item, index) => ({ item, index, key: index }));
537
+ if (typeof value === "object") {
538
+ return Object.entries(value).map(([key, item], index) => ({ item, index, key }));
539
+ }
540
+ return [];
541
+ }
542
+ export function each(parent, list, render, keyOf, filter, empty) {
543
+ const [start, end] = makeRange(parent, "for");
544
+ const owner = current;
545
+ let rows = new Map();
546
+ let emptyOwner = null;
547
+ effect(() => {
548
+ let entries = iterate(list());
549
+ if (filter)
550
+ entries = entries.filter(ctx => !!filter(ctx));
551
+ const next = new Map();
552
+ const order = [];
553
+ entries.forEach((ctx, position) => {
554
+ ctx.index = position;
555
+ let key = keyOf ? keyOf(ctx) : typeof ctx.item === "object" && ctx.item !== null ? ctx.item : ctx.key;
556
+ if (next.has(key)) {
557
+ if (devMode)
558
+ console.warn("[sinth] Duplicate key in for loop:", key);
559
+ key = { duplicate: key, position };
560
+ }
561
+ let row = rows.get(key);
562
+ if (row) {
563
+ row.ctx.item = ctx.item;
564
+ row.ctx.index = ctx.index;
565
+ row.ctx.key = ctx.key;
566
+ rows.delete(key);
567
+ }
568
+ else {
569
+ const childOwner = createOwner(owner);
570
+ const rowStart = document.createComment("");
571
+ const rowEnd = document.createComment("");
572
+ const rowCtx = { item: ctx.item, index: ctx.index, key: ctx.key };
573
+ const frag = document.createDocumentFragment();
574
+ frag.appendChild(rowStart);
575
+ withOwner(childOwner, () => render(frag, rowCtx));
576
+ frag.appendChild(rowEnd);
577
+ row = { ctx: rowCtx, owner: childOwner, start: rowStart, end: rowEnd };
578
+ end.parentNode?.insertBefore(frag, end);
579
+ }
580
+ next.set(key, row);
581
+ order.push(row);
582
+ });
583
+ for (const row of rows.values()) {
584
+ clearRange(row.start, row.end);
585
+ row.start.remove();
586
+ row.end.remove();
587
+ disposeOwner(row.owner);
588
+ }
589
+ rows = next;
590
+ const host = end.parentNode;
591
+ if (host) {
592
+ let prev = start;
593
+ for (const row of order) {
594
+ if (prev.nextSibling !== row.start) {
595
+ const frag = document.createDocumentFragment();
596
+ let n = row.start;
597
+ while (n) {
598
+ const following = n.nextSibling;
599
+ frag.appendChild(n);
600
+ if (n === row.end)
601
+ break;
602
+ n = following;
603
+ }
604
+ host.insertBefore(frag, prev.nextSibling);
605
+ }
606
+ prev = row.end;
607
+ }
608
+ }
609
+ if (empty) {
610
+ if (order.length === 0 && !emptyOwner) {
611
+ emptyOwner = createOwner(owner);
612
+ const frag = renderInto(emptyOwner, empty);
613
+ end.parentNode?.insertBefore(frag, end);
614
+ }
615
+ else if (order.length > 0 && emptyOwner) {
616
+ disposeOwner(emptyOwner);
617
+ emptyOwner = null;
618
+ let n = start.nextSibling;
619
+ while (n && n !== end && !rowsHasStart(rows, n)) {
620
+ const following = n.nextSibling;
621
+ n.parentNode?.removeChild(n);
622
+ n = following;
623
+ }
624
+ }
625
+ }
626
+ });
627
+ }
628
+ function rowsHasStart(rows, node) {
629
+ for (const row of rows.values())
630
+ if (row.start === node)
631
+ return true;
632
+ return false;
633
+ }
634
+ export function html(parent, get) {
635
+ const [start, end] = makeRange(parent, "html");
636
+ let last;
637
+ effect(() => {
638
+ const value = toText(get());
639
+ if (value === last)
640
+ return;
641
+ last = value;
642
+ clearRange(start, end);
643
+ const t = document.createElement("template");
644
+ t.innerHTML = value;
645
+ end.parentNode?.insertBefore(t.content, end);
646
+ });
647
+ }
648
+ export function component(parent, comp, props, slots, fallthrough) {
649
+ const before = parent.lastChild;
650
+ const owner = createOwner(current);
651
+ withOwner(owner, () => comp(parent, props, slots, fallthrough));
652
+ let n = before ? before.nextSibling : parent.firstChild;
653
+ while (n && n.nodeType !== 1)
654
+ n = n.nextSibling;
655
+ return n;
656
+ }
657
+ export function slot(parent, slots, name, fallback) {
658
+ const fill = slots[name];
659
+ if (fill)
660
+ fill(parent);
661
+ else if (fallback)
662
+ fallback(parent);
663
+ }
664
+ export function onMount(fn) {
665
+ queueMicrotask(() => {
666
+ try {
667
+ const cleanup = fn();
668
+ if (typeof cleanup === "function")
669
+ current.cleanups.push(cleanup);
670
+ settle(cleanup);
671
+ }
672
+ catch (err) {
673
+ reportError(err);
674
+ }
675
+ update();
676
+ });
677
+ }
678
+ export function onDestroy(fn) {
679
+ current.cleanups.push(fn);
680
+ }
681
+ function snapshot(value) {
682
+ if (value === null || typeof value !== "object")
683
+ return value;
684
+ try {
685
+ return JSON.stringify(value);
686
+ }
687
+ catch {
688
+ return value;
689
+ }
690
+ }
691
+ export function watch(get, fn) {
692
+ let first = true;
693
+ let last;
694
+ let lastValue;
695
+ effect(() => {
696
+ const value = get();
697
+ const snap = snapshot(value);
698
+ if (first) {
699
+ first = false;
700
+ last = snap;
701
+ lastValue = value;
702
+ return;
703
+ }
704
+ if (Object.is(snap, last))
705
+ return;
706
+ const prev = lastValue;
707
+ last = snap;
708
+ lastValue = value;
709
+ settle(fn(value, prev));
710
+ rerun = true;
711
+ });
712
+ }
713
+ export function persisted(key, fallback) {
714
+ try {
715
+ const raw = localStorage.getItem(key);
716
+ return raw === null ? fallback : JSON.parse(raw);
717
+ }
718
+ catch {
719
+ return fallback;
720
+ }
721
+ }
722
+ export function persist(key, get) {
723
+ let last;
724
+ effect(() => {
725
+ let raw;
726
+ try {
727
+ raw = JSON.stringify(get());
728
+ }
729
+ catch {
730
+ return;
731
+ }
732
+ if (raw === last)
733
+ return;
734
+ last = raw;
735
+ try {
736
+ localStorage.setItem(key, raw);
737
+ }
738
+ catch {
739
+ return;
740
+ }
741
+ });
742
+ }
743
+ const memoCache = new Map();
744
+ export function memo(site, fn, args) {
745
+ let cache = memoCache.get(site);
746
+ if (!cache) {
747
+ cache = new Map();
748
+ memoCache.set(site, cache);
749
+ }
750
+ let key;
751
+ try {
752
+ key = JSON.stringify(args);
753
+ }
754
+ catch {
755
+ return fn();
756
+ }
757
+ if (cache.has(key))
758
+ return cache.get(key);
759
+ const value = fn();
760
+ cache.set(key, value);
761
+ return value;
762
+ }
763
+ export function range(a, b, step = 1) {
764
+ const startN = b === undefined ? 0 : a;
765
+ const endN = b === undefined ? a : b;
766
+ if (step === 0)
767
+ throw new Error("range() step cannot be 0");
768
+ const out = [];
769
+ if (step > 0)
770
+ for (let i = startN; i < endN; i += step)
771
+ out.push(i);
772
+ else
773
+ for (let i = startN; i > endN; i += step)
774
+ out.push(i);
775
+ return out;
776
+ }
777
+ export function iter(value) {
778
+ return iterate(value).map(ctx => [ctx.item, ctx.index, ctx.key]);
779
+ }
780
+ export function injectCss(id, css) {
781
+ if (typeof document === "undefined" || document.getElementById(id))
782
+ return;
783
+ const tag = document.createElement("style");
784
+ tag.id = id;
785
+ tag.textContent = css;
786
+ document.head.appendChild(tag);
787
+ }
788
+ export function mount(target, render) {
789
+ const owner = createOwner(rootOwner);
790
+ const frag = renderInto(owner, render);
791
+ target.replaceChildren(frag);
792
+ return {
793
+ update,
794
+ dispose: () => {
795
+ disposeOwner(owner);
796
+ target.replaceChildren();
797
+ },
798
+ };
799
+ }
800
+ export function boot(page) {
801
+ devMode = !!page.dev;
802
+ const g = globalThis;
803
+ if (g.__sinth_hmr_state__ && page.restore) {
804
+ page.restore(g.__sinth_hmr_state__);
805
+ g.__sinth_hmr_state__ = undefined;
806
+ }
807
+ g.sinthRender = update;
808
+ g.sinth = { update, schedule };
809
+ const start = () => {
810
+ const app = mount(document.body, page.render);
811
+ g.__sinth_app__ = {
812
+ dispose: () => {
813
+ if (page.state)
814
+ g.__sinth_hmr_state__ = page.state();
815
+ app.dispose();
816
+ rootOwner = createOwner(null);
817
+ current = rootOwner;
818
+ },
819
+ };
820
+ };
821
+ if (document.readyState === "loading")
822
+ document.addEventListener("DOMContentLoaded", start, { once: true });
823
+ else
824
+ start();
825
+ }
826
+ export function defineElement(tag, comp, params, css) {
827
+ if (typeof customElements === "undefined" || customElements.get(tag))
828
+ return;
829
+ const kinds = new Map(params);
830
+ const attrNames = params.map(([name]) => kebab(name));
831
+ class SinthElement extends HTMLElement {
832
+ constructor() {
833
+ super(...arguments);
834
+ this.app = null;
835
+ this.values = {};
836
+ }
837
+ static get observedAttributes() {
838
+ return attrNames;
839
+ }
840
+ connectedCallback() {
841
+ if (css)
842
+ injectCss(css[0], css[1]);
843
+ if (this.app)
844
+ return;
845
+ for (const name of kinds.keys()) {
846
+ const own = this[name];
847
+ if (own !== undefined)
848
+ this.values[name] = own;
849
+ const attrValue = this.getAttribute(kebab(name));
850
+ if (attrValue !== null)
851
+ this.values[name] = coerce(attrValue, kinds.get(name));
852
+ Object.defineProperty(this, name, {
853
+ get: () => this.values[name],
854
+ set: (v) => {
855
+ this.values[name] = v;
856
+ update();
857
+ },
858
+ configurable: true,
859
+ });
860
+ }
861
+ const props = {};
862
+ for (const name of kinds.keys())
863
+ Object.defineProperty(props, name, { get: () => this.values[name], enumerable: true });
864
+ const slots = {};
865
+ const light = Array.from(this.childNodes);
866
+ if (light.length)
867
+ slots.default = (p) => light.forEach(n => p.appendChild(n));
868
+ this.app = mount(this, (p) => component(p, comp, props, slots));
869
+ }
870
+ disconnectedCallback() {
871
+ this.app?.dispose();
872
+ this.app = null;
873
+ }
874
+ attributeChangedCallback(name, _old, value) {
875
+ const key = params.find(([n]) => kebab(n) === name)?.[0];
876
+ if (!key)
877
+ return;
878
+ this.values[key] = value === null ? undefined : coerce(value, kinds.get(key));
879
+ update();
880
+ }
881
+ }
882
+ customElements.define(tag, SinthElement);
883
+ }
884
+ function coerce(value, kind) {
885
+ if (kind === "num" || kind === "int")
886
+ return Number(value);
887
+ if (kind === "bool")
888
+ return value !== "false";
889
+ if (kind === "obj" || (kind && kind.endsWith("[]"))) {
890
+ try {
891
+ return JSON.parse(value);
892
+ }
893
+ catch {
894
+ return value;
895
+ }
896
+ }
897
+ return value;
898
+ }
899
+ let routeParams;
900
+ /** Reads a route parameter of a dynamic page (`pages/blog/[slug].sinth`). */
901
+ export function param(name) {
902
+ if (!routeParams) {
903
+ const node = typeof document !== "undefined" ? document.getElementById("sinth-params") : null;
904
+ try {
905
+ routeParams = node?.textContent ? JSON.parse(node.textContent) : {};
906
+ }
907
+ catch {
908
+ routeParams = {};
909
+ }
910
+ }
911
+ return routeParams[name] ?? "";
912
+ }