@yannosay/sinth 0.13.0 → 0.13.1

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/package.json +2 -2
  2. package/src/core/compiler.ts +130 -60
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@yannosay/sinth",
3
- "version": "0.13.0",
4
- "description": "A declarative language that compiles to pure HTML, CSS, and JavaScript.",
3
+ "version": "0.13.1",
4
+ "description": "A declarative & reactive language that compiles to pure HTML, CSS, and JavaScript.",
5
5
  "author": "Yannosay Productions",
6
6
  "license": "AGPL-3.0",
7
7
  "homepage": "https://sinth.yannosay.com",
@@ -505,20 +505,29 @@ if (name === "model" && value?.kind === "str") {
505
505
 
506
506
  if (name === "delay") {
507
507
  if (value.kind === "num") {
508
- return `data-sinth-delay="${value.value}" style="display:none"`;
508
+ return `data-sinth-delay="${value.value}"`;
509
509
  }
510
510
  const v = litToString(value);
511
- if (/^\d+$/.test(v)) return `data-sinth-delay="${escAttr(v)}" style="display:none"`;
511
+ if (/^\d+$/.test(v)) return `data-sinth-delay="${escAttr(v)}"`;
512
512
  if (v.startsWith("__EXPR__")) {
513
513
  try {
514
514
  const expr: Expression = JSON.parse(v.substring(8));
515
515
  const id = registerExpr(ctx, expr);
516
- return `data-sinth-delay-expr-id="${id}" style="display:none"`;
516
+ return `data-sinth-delay-expr-id="${id}"`;
517
517
  } catch {}
518
518
  }
519
- // plain variable name – register as expression
520
519
  const id = registerExpr(ctx, { kind: "variable", name: v });
521
- return `data-sinth-delay-expr-id="${id}" style="display:none"`;
520
+ return `data-sinth-delay-expr-id="${id}"`;
521
+ }
522
+
523
+ if (name === "hide") {
524
+ if (value && (value as any).kind === "bool") {
525
+ if (!(value as any).value) {
526
+ return `data-sinth-delay-hide="false"`;
527
+ }
528
+ return "";
529
+ }
530
+ return "";
522
531
  }
523
532
 
524
533
  if (name === "checked") {
@@ -711,16 +720,14 @@ export function renderChild(
711
720
  return renderIfBlock(child, ctx, params, depth);
712
721
 
713
722
  case "for": {
714
- // collect loop‑variable names
715
723
  const loopVars = new Set<string>();
716
724
  loopVars.add(child.itemVar);
717
725
  if (child.keyVar) loopVars.add(child.keyVar);
718
726
  if (child.indexVar) loopVars.add(child.indexVar);
719
- // set them on ctx for compilation of the body
720
727
  const prev = ctx.loopVars;
721
728
  ctx.loopVars = loopVars;
722
729
  const bodyHTML = child.body.map(c => renderChild(c, ctx, params, depth + 1)).join("");
723
- ctx.loopVars = prev; // restore
730
+ ctx.loopVars = prev;
724
731
  const keyAttr = child.keyVar ? ` data-sinth-key="${escAttr(child.keyVar)}"` : "";
725
732
  const idxAttr = child.indexVar ? ` data-sinth-index="${escAttr(child.indexVar)}"` : "";
726
733
  return (
@@ -771,9 +778,40 @@ depth: number,
771
778
  replaceAttr = ` data-sinth-if-replace="${escAttr(idAttr.value.value)}"`;
772
779
  }
773
780
  }
781
+ let delayAttr = "";
782
+ let delayHideAttr = "";
783
+ if (firstComp) {
784
+ const delayA = firstComp.attrs.find(a => a.name === "delay");
785
+ const hideA = firstComp.attrs.find(a => a.name === "hide");
786
+ if (delayA && hideA && hideA.value && (hideA.value as any).kind === "bool" && !(hideA.value as any).value) {
787
+ if (delayA.value?.kind === "num") {
788
+ delayAttr = ` data-sinth-if-delay="${delayA.value.value}"`;
789
+ } else if (delayA.value?.kind === "str") {
790
+ const v = litToString(delayA.value);
791
+ if (/^\d+$/.test(v)) delayAttr = ` data-sinth-if-delay="${v}"`;
792
+ }
793
+ delayHideAttr = ` data-sinth-if-delay-hide="false"`;
794
+ }
795
+ }
796
+ let elseDelayAttr = "";
797
+ let elseDelayHideAttr = "";
798
+ const elseFirstComp = (ifBlock.elseBody ?? []).find(c => c.kind === "use") as CompUse | undefined;
799
+ if (elseFirstComp) {
800
+ const delayA = elseFirstComp.attrs.find(a => a.name === "delay");
801
+ const hideA = elseFirstComp.attrs.find(a => a.name === "hide");
802
+ if (delayA && hideA && hideA.value && (hideA.value as any).kind === "bool" && !(hideA.value as any).value) {
803
+ if (delayA.value?.kind === "num") {
804
+ elseDelayAttr = ` data-sinth-if-delay="${delayA.value.value}"`;
805
+ } else if (delayA.value?.kind === "str") {
806
+ const v = litToString(delayA.value);
807
+ if (/^\d+$/.test(v)) elseDelayAttr = ` data-sinth-if-delay="${v}"`;
808
+ }
809
+ elseDelayHideAttr = ` data-sinth-if-delay-hide="false"`;
810
+ }
811
+ }
774
812
  return (
775
- `<template data-sinth-if-id="${tplId}" data-sinth-if-expr="${condId}"${replaceAttr}>${bodyHTML}</template>` +
776
- (elseHTML ? `<template data-sinth-else data-sinth-if-id="${tplId}">${elseHTML}</template>` : "")
813
+ `<template data-sinth-if-id="${tplId}" data-sinth-if-expr="${condId}"${replaceAttr}${delayAttr}${delayHideAttr}>${bodyHTML}</template>` +
814
+ (elseHTML ? `<template data-sinth-else data-sinth-if-id="${tplId}"${elseDelayAttr}${elseDelayHideAttr}>${elseHTML}</template>` : "")
777
815
  );
778
816
  }
779
817
 
@@ -835,7 +873,6 @@ export function renderCompUse(
835
873
  params: Map<string, string>,
836
874
  depth: number,
837
875
  ): string {
838
- // transparent wrapper inserted by flattenIfToUses()
839
876
  if (use.name === "__IF_ROOT__") {
840
877
  return use.children.map(c => renderChild(c, ctx, params, depth)).join("");
841
878
  }
@@ -887,6 +924,12 @@ export function renderCompUse(
887
924
  continue;
888
925
  }
889
926
  if (use.name === "Checkbox" && (attr.name === "checked" || attr.name === "onChange" || attr.name === "label")) continue;
927
+ if (attr.name === "hide") {
928
+ if (attr.value && (attr.value as any).kind === "bool" && !(attr.value as any).value) {
929
+ attrParts.push(`data-sinth-delay-hide="false"`);
930
+ }
931
+ continue;
932
+ }
890
933
  const rendered = renderAttr(attr, params, ctx);
891
934
  if (rendered) attrParts.push(rendered);
892
935
  }
@@ -1299,10 +1342,12 @@ function sinthReplaceInsert(t, anchor, ifId, replaceId) {
1299
1342
  if (needsDelay) {
1300
1343
  helpers += `
1301
1344
  function sinthDelay(el) {
1302
- if (el.dataset.sinthDelayDone) { el.style.display = ''; return; }
1345
+ if (el.dataset.sinthDelayDone) { return; }
1303
1346
  el.dataset.sinthDelayDone = '1';
1304
1347
  var ms = parseInt(el.dataset.sinthDelay) || 0;
1305
- el.style.display = 'none';
1348
+ if (el.dataset.sinthDelayHide !== 'false') {
1349
+ el.style.display = 'none';
1350
+ }
1306
1351
  var show = function() {
1307
1352
  el.style.display = '';
1308
1353
  el.querySelectorAll('.sinth-expr').forEach(sinthExpr);
@@ -1334,61 +1379,86 @@ function sinthIfBlock(t) {
1334
1379
  var condFn = __X[t.dataset.sinthIfExpr];
1335
1380
  var cond = condFn ? condFn() : false;
1336
1381
  if (cond) {
1337
- anchor = sinthReplaceInsert(t, anchor, ifId, t.dataset.sinthIfReplace);
1382
+ var _hasContent = anchor || t.parentNode.querySelector('[data-sinth-if-anchor="__else__' + ifId + '"]');
1383
+ if (t.dataset.sinthIfDelayHide === 'false' && t.dataset.sinthIfDelay && _hasContent) {
1384
+ var dms = parseInt(t.dataset.sinthIfDelay) || 0;
1385
+ setTimeout(function() {
1386
+ sinthReplaceInsert(t, anchor, ifId, t.dataset.sinthIfReplace);
1387
+ }, dms);
1388
+ } else {
1389
+ anchor = sinthReplaceInsert(t, anchor, ifId, t.dataset.sinthIfReplace);
1390
+ }
1338
1391
  } else {
1339
- if (anchor) {
1340
- if (anchor._sinthReplaced) {
1341
- var insFirst = anchor._sinthInsertedFirst;
1342
- var insLast = anchor._sinthInsertedLast;
1343
- var rpParent = anchor._sinthReplacedParent;
1344
- var rpNext = anchor._sinthReplacedNext;
1345
- if (insFirst && insLast) {
1346
- var cur = insFirst;
1347
- while (cur && cur !== insLast) {
1348
- var next = cur.nextSibling;
1349
- cur.remove();
1350
- cur = next;
1392
+ var runElse = function() {
1393
+ if (anchor) {
1394
+ if (anchor._sinthReplaced) {
1395
+ var insFirst = anchor._sinthInsertedFirst;
1396
+ var insLast = anchor._sinthInsertedLast;
1397
+ var rpParent = anchor._sinthReplacedParent;
1398
+ var rpNext = anchor._sinthReplacedNext;
1399
+ if (insFirst && insLast) {
1400
+ var cur = insFirst;
1401
+ while (cur && cur !== insLast) {
1402
+ var next = cur.nextSibling;
1403
+ cur.remove();
1404
+ cur = next;
1405
+ }
1406
+ if (insLast) insLast.remove();
1407
+ }
1408
+ if (rpParent && rpNext) {
1409
+ rpParent.insertBefore(anchor._sinthReplaced, rpNext);
1410
+ } else if (rpParent) {
1411
+ rpParent.appendChild(anchor._sinthReplaced);
1412
+ }
1413
+ if (anchor._sinthReplaced && anchor._sinthReplaced.dataset.sinthDelay) {
1414
+ delete anchor._sinthReplaced.dataset.sinthDelayDone;
1415
+ sinthDelay(anchor._sinthReplaced);
1351
1416
  }
1352
- if (insLast) insLast.remove();
1417
+ } else {
1418
+ var cur2 = anchor.nextSibling;
1419
+ while (cur2 && cur2 !== t) { var nx2 = cur2.nextSibling; cur2.remove(); cur2 = nx2; }
1420
+ }
1421
+ anchor.remove();
1422
+ }
1423
+ var elseT = t.nextElementSibling;
1424
+ if (elseT && elseT.hasAttribute('data-sinth-else')) {
1425
+ var elseIfId = elseT.dataset.sinthIfId;
1426
+ var ea = t.parentNode.querySelector('[data-sinth-if-anchor="__else__' + elseIfId + '"]');
1427
+ if (ea) {
1428
+ var cur3 = ea.nextSibling;
1429
+ while (cur3 && cur3 !== t) { var nx3 = cur3.nextSibling; cur3.remove(); cur3 = nx3; }
1430
+ } else {
1431
+ ea = document.createElement('span');
1432
+ ea.style.display = 'none';
1433
+ ea.dataset.sinthIfAnchor = '__else__' + elseIfId;
1434
+ t.parentNode.insertBefore(ea, t);
1353
1435
  }
1354
- if (rpParent && rpNext) {
1355
- rpParent.insertBefore(anchor._sinthReplaced, rpNext);
1356
- } else if (rpParent) {
1357
- rpParent.appendChild(anchor._sinthReplaced);
1436
+ var ef = document.createRange().createContextualFragment(elseT.innerHTML);
1437
+ ef.querySelectorAll('.sinth-expr').forEach(sinthExpr);
1438
+ if (${needsDelay}) {
1439
+ ef.querySelectorAll('[data-sinth-delay]').forEach(sinthDelay);
1440
+ ef.querySelectorAll('[data-sinth-delay-expr-id]').forEach(function(el) { sinthDelayExpr(el, {}); });
1358
1441
  }
1442
+ t.parentNode.insertBefore(ef, t);
1443
+ ef.querySelectorAll('[data-sinth-delay]').forEach(function(el) {
1444
+ delete el.dataset.sinthDelayDone;
1445
+ sinthDelay(el);
1446
+ });
1359
1447
  } else {
1360
- var cur2 = anchor.nextSibling;
1361
- while (cur2 && cur2 !== t) { var nx2 = cur2.nextSibling; cur2.remove(); cur2 = nx2; }
1448
+ var ea2 = t.parentNode.querySelector('[data-sinth-if-anchor="__else__' + ifId + '"]');
1449
+ if (ea2) {
1450
+ var ec = ea2.nextSibling;
1451
+ while (ec && ec !== t) { var en = ec.nextSibling; ec.remove(); ec = en; }
1452
+ ea2.remove();
1453
+ }
1362
1454
  }
1363
- anchor.remove();
1364
- }
1455
+ };
1365
1456
  var elseT = t.nextElementSibling;
1366
- if (elseT && elseT.hasAttribute('data-sinth-else')) {
1367
- var elseIfId = elseT.dataset.sinthIfId;
1368
- var ea = t.parentNode.querySelector('[data-sinth-if-anchor="__else__' + elseIfId + '"]');
1369
- if (ea) {
1370
- var cur3 = ea.nextSibling;
1371
- while (cur3 && cur3 !== t) { var nx3 = cur3.nextSibling; cur3.remove(); cur3 = nx3; }
1372
- } else {
1373
- ea = document.createElement('span');
1374
- ea.style.display = 'none';
1375
- ea.dataset.sinthIfAnchor = '__else__' + elseIfId;
1376
- t.parentNode.insertBefore(ea, t);
1377
- }
1378
- var ef = document.createRange().createContextualFragment(elseT.innerHTML);
1379
- ef.querySelectorAll('.sinth-expr').forEach(sinthExpr);
1380
- if (${needsDelay}) {
1381
- ef.querySelectorAll('[data-sinth-delay]').forEach(sinthDelay);
1382
- ef.querySelectorAll('[data-sinth-delay-expr-id]').forEach(function(el) { sinthDelayExpr(el, {}); });
1383
- }
1384
- t.parentNode.insertBefore(ef, t);
1457
+ if (elseT && elseT.hasAttribute('data-sinth-else') && elseT.dataset.sinthIfDelayHide === 'false' && elseT.dataset.sinthIfDelay && anchor) {
1458
+ var edms = parseInt(elseT.dataset.sinthIfDelay) || 0;
1459
+ setTimeout(runElse, edms);
1385
1460
  } else {
1386
- var ea2 = t.parentNode.querySelector('[data-sinth-if-anchor="__else__' + ifId + '"]');
1387
- if (ea2) {
1388
- var ec = ea2.nextSibling;
1389
- while (ec && ec !== t) { var en = ec.nextSibling; ec.remove(); ec = en; }
1390
- ea2.remove();
1391
- }
1461
+ runElse();
1392
1462
  }
1393
1463
  }
1394
1464
  }