@yannosay/sinth 0.14.0 → 0.14.2

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yannosay/sinth",
3
- "version": "0.14.0",
3
+ "version": "0.14.2",
4
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",
@@ -617,6 +617,61 @@ export function renderCompUse(
617
617
  if (classes) attrParts.push(`class="${escAttr(classes)}"`);
618
618
  if (inlineStyleParts.length > 0) attrParts.push(`style="${escAttr(inlineStyleParts.join("; "))}"`);
619
619
  if (tag === "button" && !use.attrs.some(a => a.name === "type")) attrParts.push(`type="button"`);
620
+
621
+ const delayAttrVal = use.attrs.find(a => a.name === "delay");
622
+ if (delayAttrVal) {
623
+ const delayMs = delayAttrVal.value?.kind === "num" ? delayAttrVal.value.value : 1000;
624
+ const eventNames = ["onClick", "onChange", "onInput", "onSubmit"];
625
+ let hasEvent = false;
626
+ for (const en of eventNames) {
627
+ const evName = en.toLowerCase();
628
+ const idx = attrParts.findIndex(p => p.startsWith(evName + "="));
629
+ if (idx >= 0) {
630
+ hasEvent = true;
631
+ const current = attrParts[idx];
632
+ const match = current.match(/\(function\(\)\{\s*(.+?);\s*sinthRender\(\)/);
633
+ if (match) {
634
+ const action = match[1];
635
+ const qid = `__sq${ctx.ifIdCounter++}`;
636
+ const hideAttr = use.attrs.find(a => a.name === "hide");
637
+ const hideVal = hideAttr
638
+ ? (hideAttr.value === null
639
+ ? true
640
+ : (hideAttr.value.kind === "bool"
641
+ ? hideAttr.value.value
642
+ : (hideAttr.value.kind === "str" && hideAttr.value.value !== "false")))
643
+ : true;
644
+ ctx.actionButtons.push({ qid, delayMs, hide: hideVal });
645
+ const delayIdx = attrParts.findIndex(p => p.startsWith("data-sinth-delay"));
646
+ if (delayIdx >= 0) attrParts.splice(delayIdx, 1);
647
+ const hideIdx = attrParts.findIndex(p => p.startsWith("data-sinth-delay-hide"));
648
+ if (hideIdx >= 0) attrParts.splice(hideIdx, 1);
649
+ const doneIdx = attrParts.findIndex(p => p.startsWith("data-sinth-delay-done"));
650
+ if (doneIdx >= 0) attrParts.splice(doneIdx, 1);
651
+ attrParts.push(`data-sinth-queue-id="${qid}"`);
652
+ if (hideVal) {
653
+ attrParts.push(`style="display:none"`);
654
+ }
655
+ attrParts[idx] = `${evName}="${escAttr(`(function(e){ var el=document.querySelector('[data-sinth-queue-id=${qid}]'); if(!el)return; var hide=${hideVal}; if(hide)el.style.display='none'; var q=window._sinthQ=q||{}; var sq=q.${qid}=q.${qid}||[]; sq.push(function(){ if(hide)el.style.display=''; ${action}; sinthRender(); }); if(sq.length===1){ (function p(first){ if(!sq||sq.length===0){delete q.${qid};return;} var a=sq[0]; sq.shift(); var d=first?${delayMs}:0; setTimeout(function(){ a(); if(sq.length>0)setTimeout(function(){p(false)},${delayMs}); },d); })(true); } })()`)}"`;
656
+ }
657
+ }
658
+ }
659
+ if (!hasEvent) {
660
+ if (use.attrs.some(a => a.name === "hide")) {
661
+ const hideAttr = use.attrs.find(a => a.name === "hide")!;
662
+ const hideVal = hideAttr.value;
663
+ const isHidden = (hideVal === null) ||
664
+ (hideVal?.kind === "bool" && hideVal.value) ||
665
+ (hideVal?.kind === "str" && hideVal.value !== "false");
666
+ attrParts.push(`data-sinth-delay-hide="${isHidden ? "true" : "false"}"`);
667
+ if (!isHidden) {
668
+ attrParts.push(`data-sinth-delay-done="1"`);
669
+ }
670
+ } else {
671
+ attrParts.push(`data-sinth-delay-hide="true"`);
672
+ }
673
+ }
674
+ }
620
675
  if (use.name === "Input") {
621
676
  const bindAttr = use.attrs.find(a => a.name === "bind");
622
677
  const modelAttr = use.attrs.find(a => a.name === "model");
@@ -1009,6 +1064,7 @@ export function compileFile(filePath: string, opts: CompileOptions): { html: str
1009
1064
  exprRegistry: [],
1010
1065
  exprMap: new Map(),
1011
1066
  varDecls: allVarDecls,
1067
+ actionButtons: [],
1012
1068
  };
1013
1069
 
1014
1070
  if (!file.isPage) {
@@ -1101,12 +1157,22 @@ const sharedRuntimeTag = (() => {
1101
1157
  return `<script src="${runtimeSrc}"></script>`;
1102
1158
  })();
1103
1159
 
1160
+ let actionInitScript = '';
1161
+ if (ctx.actionButtons.length > 0) {
1162
+ actionInitScript = '<script>' +
1163
+ ctx.actionButtons.map(b =>
1164
+ `setTimeout(function(){ var el=document.querySelector('[data-sinth-queue-id="${b.qid}"]'); if(el)el.style.display=''; },${b.delayMs});`
1165
+ ).join('') +
1166
+ '</script>';
1167
+ }
1168
+
1104
1169
  const html = [
1105
1170
  "<!DOCTYPE html>",
1106
1171
  `<html lang="${escAttr(headData.lang)}">`,
1107
1172
  head,
1108
1173
  `<body data-s="${hash}">`,
1109
1174
  bodyHTML,
1175
+ actionInitScript,
1110
1176
  sharedRuntimeTag,
1111
1177
  runtimeJS.trim() ? `<script>\n${runtimeJS}\n</script>` : "",
1112
1178
  scriptTags.join("\n"),
@@ -73,15 +73,19 @@ function sinthDelay(el) {
73
73
  if (el.dataset.sinthDelayDone) { return; }
74
74
  el.dataset.sinthDelayDone = '1';
75
75
  var ms = parseInt(el.dataset.sinthDelay) || 0;
76
- if (el.dataset.sinthDelayHide !== 'false') {
77
- el.style.display = 'none';
78
- }
76
+ var hideEl = el.dataset.sinthDelayHide !== 'false';
79
77
  var show = function() {
80
- el.style.display = '';
78
+ if (hideEl) el.style.display = '';
81
79
  el.querySelectorAll('.sinth-expr').forEach(sinthExpr);
82
80
  };
83
- if (ms > 0) setTimeout(show, ms);
84
- else show();
81
+ if (hideEl) {
82
+ el.style.display = 'none';
83
+ if (ms > 0) setTimeout(show, ms);
84
+ else show();
85
+ } else {
86
+ if (ms > 0) setTimeout(show, ms);
87
+ else show();
88
+ }
85
89
  }
86
90
  function sinthDelayExpr(el, _ctx) {
87
91
  _ctx = _ctx || {};
@@ -70,8 +70,28 @@ export function buildRenderBody(opts: {
70
70
  } catch(e) {}
71
71
  });\n`;
72
72
  }
73
+ if (needsDelay) {
74
+ renderBody += ` document.querySelectorAll('[data-sinth-delay]').forEach(function(el) {
75
+ var newContent = '';
76
+ el.querySelectorAll('.sinth-expr').forEach(function(exprEl) {
77
+ var exprFn = __X[exprEl.dataset.exprId];
78
+ if (exprFn) newContent += exprFn({});
79
+ });
80
+ if (el._sinthLastContent === undefined) {
81
+ el._sinthLastContent = newContent;
82
+ } else if (el._sinthLastContent !== newContent) {
83
+ el._sinthLastContent = newContent;
84
+ delete el.dataset.sinthDelayDone;
85
+ }
86
+ });\n`;
87
+ }
73
88
  if (needsExpr) {
74
- renderBody += ` document.querySelectorAll('.sinth-expr').forEach(sinthExpr);\n`;
89
+ renderBody += ` document.querySelectorAll('.sinth-expr').forEach(function(el) {
90
+ var delayParent = el.closest('[data-sinth-delay]');
91
+ if (!delayParent || delayParent.dataset.sinthDelayDone) {
92
+ sinthExpr(el);
93
+ }
94
+ });\n`;
75
95
  }
76
96
  renderBody += ` document.querySelectorAll('[data-sinth-hide]').forEach(function(el) {
77
97
  var exprId = el.dataset.sinthHide;
package/src/core/types.ts CHANGED
@@ -144,6 +144,7 @@ export interface CompileCtx {
144
144
  exprRegistry: string[];
145
145
  exprMap: Map<string, number>;
146
146
  varDecls?: VarDeclaration[];
147
+ actionButtons: { qid: string; delayMs: number; hide: boolean }[];
147
148
  loopVars?: Set<string>;
148
149
  }
149
150
 
package/src/utils.ts CHANGED
@@ -25,7 +25,7 @@ export function esc(s: string): string {
25
25
  }
26
26
 
27
27
  export function escAttr(s: string): string {
28
- return esc(s).replace(/"/g, "&quot;");
28
+ return s.replace(/&/g, "&amp;").replace(/"/g, "&quot;").replace(/</g, "&lt;");
29
29
  }
30
30
 
31
31
  export function litToString(lit: Literal): string {