@yannosay/sinth 0.13.2 → 0.13.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.
- package/README.md +13 -1
- package/package.json +1 -1
- package/src/core/compiler.ts +16 -7
- package/src/core/runtime/render.ts +11 -0
package/README.md
CHANGED
|
@@ -18,7 +18,19 @@ Input(bind: userName, placeholder: "Enter your name")
|
|
|
18
18
|
Paragraph { "Hello, " + userName }
|
|
19
19
|
```
|
|
20
20
|
|
|
21
|
-
No useState. No onChange. No e.target.value.
|
|
21
|
+
No useState. No onChange. No e.target.value.
|
|
22
|
+
|
|
23
|
+
```ts
|
|
24
|
+
page
|
|
25
|
+
title = "Counter"
|
|
26
|
+
|
|
27
|
+
var int count = 0
|
|
28
|
+
|
|
29
|
+
Main {
|
|
30
|
+
Heading(level: 1) {count}
|
|
31
|
+
Button(onClick: count = count + 1) {"Increment"}
|
|
32
|
+
}
|
|
33
|
+
```
|
|
22
34
|
|
|
23
35
|
#### Logic and UI live together.
|
|
24
36
|
```ts
|
package/package.json
CHANGED
package/src/core/compiler.ts
CHANGED
|
@@ -104,11 +104,21 @@ if (name === "delay") {
|
|
|
104
104
|
}
|
|
105
105
|
|
|
106
106
|
if (name === "hide") {
|
|
107
|
+
if (value === null) return `data-sinth-hide=""`;
|
|
107
108
|
if (value && (value as any).kind === "bool") {
|
|
108
|
-
|
|
109
|
-
|
|
109
|
+
return (value as any).value ? `data-sinth-hide=""` : "";
|
|
110
|
+
}
|
|
111
|
+
if (value?.kind === "str") {
|
|
112
|
+
const v = value.value;
|
|
113
|
+
if (v === "true") return `data-sinth-hide=""`;
|
|
114
|
+
if (v === "false") return "";
|
|
115
|
+
if (v.startsWith("__EXPR__")) {
|
|
116
|
+
try {
|
|
117
|
+
const expr: Expression = JSON.parse(v.substring(8));
|
|
118
|
+
const id = registerExpr(ctx, expr);
|
|
119
|
+
return `data-sinth-hide="${id}"`;
|
|
120
|
+
} catch { return ""; }
|
|
110
121
|
}
|
|
111
|
-
return "";
|
|
112
122
|
}
|
|
113
123
|
return "";
|
|
114
124
|
}
|
|
@@ -516,9 +526,8 @@ export function renderCompUse(
|
|
|
516
526
|
}
|
|
517
527
|
if (use.name === "Checkbox" && (attr.name === "checked" || attr.name === "onChange" || attr.name === "label")) continue;
|
|
518
528
|
if (attr.name === "hide") {
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
}
|
|
529
|
+
const rendered = renderAttr(attr, params, ctx);
|
|
530
|
+
if (rendered) attrParts.push(rendered);
|
|
522
531
|
continue;
|
|
523
532
|
}
|
|
524
533
|
const rendered = renderAttr(attr, params, ctx);
|
|
@@ -729,7 +738,7 @@ export function buildRuntime(opts: {
|
|
|
729
738
|
const needsDelay = bodyHTML.includes("data-sinth-delay") || bodyHTML.includes("data-sinth-delay-expr-id") || mixedBlocks.some(mb => mb.ifHTML.includes("data-sinth-delay") || mb.ifHTML.includes("data-sinth-delay-expr-id") || mb.elseHTML.includes("data-sinth-delay") || mb.elseHTML.includes("data-sinth-delay-expr-id"));
|
|
730
739
|
const needsMixed = mixedBlocks.length > 0;
|
|
731
740
|
const needsLogic = logicBlocks.length > 0;
|
|
732
|
-
const needsRender = needsExpr || needsIf || needsFor || needsMixed || needsLogic;
|
|
741
|
+
const needsRender = needsExpr || needsIf || needsFor || needsMixed || needsLogic || bodyHTML.includes("data-sinth-hide");
|
|
733
742
|
|
|
734
743
|
const varLines = varDecls.map(v => {
|
|
735
744
|
if (!v.value) {
|
|
@@ -71,6 +71,17 @@ export function buildRenderBody(opts: {
|
|
|
71
71
|
if (needsExpr) {
|
|
72
72
|
renderBody += ` document.querySelectorAll('.sinth-expr').forEach(sinthExpr);\n`;
|
|
73
73
|
}
|
|
74
|
+
renderBody += ` document.querySelectorAll('[data-sinth-hide]').forEach(function(el) {
|
|
75
|
+
var exprId = el.dataset.sinthHide;
|
|
76
|
+
if (exprId) {
|
|
77
|
+
try {
|
|
78
|
+
var exprFn = __X[exprId];
|
|
79
|
+
if (exprFn) el.style.display = exprFn({}) ? 'none' : '';
|
|
80
|
+
} catch(e) {}
|
|
81
|
+
} else {
|
|
82
|
+
el.style.display = 'none';
|
|
83
|
+
}
|
|
84
|
+
});\n`;
|
|
74
85
|
if (needsDelay) {
|
|
75
86
|
renderBody += ` setTimeout(function() {
|
|
76
87
|
document.querySelectorAll('[data-sinth-delay]').forEach(sinthDelay);
|