@yannosay/sinth 0.13.6 → 0.13.7
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 +28 -3
- package/package.json +1 -1
- package/src/core/compiler.ts +33 -3
- package/src/core/runtime/render.ts +4 -1
- package/src/core/types.ts +1 -0
package/README.md
CHANGED
|
@@ -176,6 +176,32 @@ Main {
|
|
|
176
176
|
```
|
|
177
177
|
|
|
178
178
|
|
|
179
|
+
```ts
|
|
180
|
+
page
|
|
181
|
+
title = "Counter Demo"
|
|
182
|
+
|
|
183
|
+
var int count = 0
|
|
184
|
+
var int addVar = 1
|
|
185
|
+
var int stepSize = 1
|
|
186
|
+
|
|
187
|
+
Main {
|
|
188
|
+
Heading(level: 1) {count}
|
|
189
|
+
|
|
190
|
+
Paragraph {"Increment by:"}
|
|
191
|
+
Input(bind: addVar, step: stepSize)
|
|
192
|
+
|
|
193
|
+
Br()
|
|
194
|
+
Button(onClick: count = count + addVar) {"Increment"}
|
|
195
|
+
|
|
196
|
+
Br()
|
|
197
|
+
Br()
|
|
198
|
+
Paragraph {"Step size for arrows:"}
|
|
199
|
+
Input(bind: stepSize, step: 1)
|
|
200
|
+
}
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
|
|
204
|
+
|
|
179
205
|
# Install
|
|
180
206
|
npm install -g @yannosay/sinth
|
|
181
207
|
|
|
@@ -198,11 +224,10 @@ Open your browser. Edit your .sinth file. Watch it update live.
|
|
|
198
224
|
page
|
|
199
225
|
title = "My Sinth Project"
|
|
200
226
|
|
|
201
|
-
var str name = "World"
|
|
202
227
|
|
|
203
228
|
Main {
|
|
204
|
-
var str
|
|
205
|
-
Heading(level: 1) { "Hello, " +
|
|
229
|
+
var str nameVar = "User"
|
|
230
|
+
Heading(level: 1) { "Hello, " + nameVar }
|
|
206
231
|
Paragraph { "Welcome to Sinth." }
|
|
207
232
|
}
|
|
208
233
|
```
|
package/package.json
CHANGED
package/src/core/compiler.ts
CHANGED
|
@@ -81,7 +81,23 @@ export function renderAttr(attr: Attr, paramMap: Map<string, string>, ctx?: Comp
|
|
|
81
81
|
|
|
82
82
|
if (name === "model" && value?.kind === "str") {
|
|
83
83
|
const vName = interpolateAttr(value.value, paramMap);
|
|
84
|
-
|
|
84
|
+
const varDecl = ctx?.varDecls?.find(v => v.name === vName);
|
|
85
|
+
const rhs = (varDecl && varDecl.varType === "int")
|
|
86
|
+
? `Number(e.target.value) || 0`
|
|
87
|
+
: `e.target.value`;
|
|
88
|
+
return `oninput="(function(e){ ${vName} = ${rhs}; sinthRender(); })(event)" value="${escAttr(vName)}"`;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
if (name === "step" && value?.kind === "str") {
|
|
92
|
+
const raw = value.value;
|
|
93
|
+
if (raw.startsWith("__EXPR__")) {
|
|
94
|
+
try {
|
|
95
|
+
const expr: Expression = JSON.parse(raw.substring(8));
|
|
96
|
+
if (expr.kind === "variable" && expr.name) {
|
|
97
|
+
return `data-sinth-step="${escAttr(expr.name)}"`;
|
|
98
|
+
}
|
|
99
|
+
} catch {}
|
|
100
|
+
}
|
|
85
101
|
}
|
|
86
102
|
|
|
87
103
|
|
|
@@ -580,8 +596,21 @@ export function renderCompUse(
|
|
|
580
596
|
if (expr.kind === "variable" && expr.name) vName = expr.name;
|
|
581
597
|
} catch {}
|
|
582
598
|
}
|
|
583
|
-
|
|
584
|
-
|
|
599
|
+
const varDecl = ctx.varDecls?.find(v => v.name === vName);
|
|
600
|
+
if (!use.attrs.some(a => a.name === "type")) {
|
|
601
|
+
if (varDecl && varDecl.varType === "int") {
|
|
602
|
+
attrParts.push(`type="number"`);
|
|
603
|
+
} else {
|
|
604
|
+
attrParts.push(`type="text"`);
|
|
605
|
+
}
|
|
606
|
+
}
|
|
607
|
+
if (varDecl && varDecl.varType === "int" && !use.attrs.some(a => a.name === "step")) {
|
|
608
|
+
attrParts.push(`step="1"`);
|
|
609
|
+
}
|
|
610
|
+
const rhs = (varDecl && varDecl.varType === "int")
|
|
611
|
+
? `Number(e.target.value) || 0`
|
|
612
|
+
: `e.target.value`;
|
|
613
|
+
attrParts.push(`oninput="(function(e){ ${vName} = ${rhs}; sinthRender(); })(event)"`);
|
|
585
614
|
attrParts.push(`data-sinth-value="${escAttr(vName)}"`);
|
|
586
615
|
}
|
|
587
616
|
}
|
|
@@ -884,6 +913,7 @@ export function compileFile(filePath: string, opts: CompileOptions): { html: str
|
|
|
884
913
|
ifIdCounter: 0,
|
|
885
914
|
exprRegistry: [],
|
|
886
915
|
exprMap: new Map(),
|
|
916
|
+
varDecls: allVarDecls,
|
|
887
917
|
};
|
|
888
918
|
|
|
889
919
|
if (!file.isPage) {
|
|
@@ -55,7 +55,10 @@ export function buildRenderBody(opts: {
|
|
|
55
55
|
renderBody += ` document.querySelectorAll('template[data-sinth-for]').forEach(sinthForBlock);\n`;
|
|
56
56
|
}
|
|
57
57
|
renderBody += ` document.querySelectorAll('[data-sinth-value]').forEach(function(el) {
|
|
58
|
-
try { el.value = window[el.dataset.sinthValue] || ''; } catch(e) {}
|
|
58
|
+
try { if (document.activeElement !== el) el.value = window[el.dataset.sinthValue] || ''; } catch(e) {}
|
|
59
|
+
});\n`;
|
|
60
|
+
renderBody += ` document.querySelectorAll('[data-sinth-step]').forEach(function(el) {
|
|
61
|
+
try { el.step = window[el.dataset.sinthStep] || 1; } catch(e) {}
|
|
59
62
|
});\n`;
|
|
60
63
|
renderBody += ` document.querySelectorAll('[data-sinth-checked]').forEach(function(el) {
|
|
61
64
|
try { el.checked = !!window[el.dataset.sinthChecked]; } catch(e) {}
|