@yannosay/sinth 0.14.1 → 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.1",
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,16 +617,59 @@ 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
- if (use.attrs.some(a => a.name === "delay") && use.attrs.some(a => a.name === "hide")) {
621
- const hideAttr = use.attrs.find(a => a.name === "hide")!;
622
- const hideVal = hideAttr.value;
623
- const isHidden = (hideVal === null) ||
624
- (hideVal?.kind === "bool" && hideVal.value) ||
625
- (hideVal?.kind === "str" && hideVal.value !== "false");
626
- attrParts.push(`data-sinth-delay-hide="${isHidden ? "true" : "false"}"`);
627
- if (!isHidden) {
628
- // hide: false → show initial content immediately, delay only on updates
629
- attrParts.push(`data-sinth-delay-done="1"`);
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
+ }
630
673
  }
631
674
  }
632
675
  if (use.name === "Input") {
@@ -1021,6 +1064,7 @@ export function compileFile(filePath: string, opts: CompileOptions): { html: str
1021
1064
  exprRegistry: [],
1022
1065
  exprMap: new Map(),
1023
1066
  varDecls: allVarDecls,
1067
+ actionButtons: [],
1024
1068
  };
1025
1069
 
1026
1070
  if (!file.isPage) {
@@ -1113,12 +1157,22 @@ const sharedRuntimeTag = (() => {
1113
1157
  return `<script src="${runtimeSrc}"></script>`;
1114
1158
  })();
1115
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
+
1116
1169
  const html = [
1117
1170
  "<!DOCTYPE html>",
1118
1171
  `<html lang="${escAttr(headData.lang)}">`,
1119
1172
  head,
1120
1173
  `<body data-s="${hash}">`,
1121
1174
  bodyHTML,
1175
+ actionInitScript,
1122
1176
  sharedRuntimeTag,
1123
1177
  runtimeJS.trim() ? `<script>\n${runtimeJS}\n</script>` : "",
1124
1178
  scriptTags.join("\n"),
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 {