@yannosay/sinth 0.19.0 → 1.1.0
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 +81 -4
- package/package.json +1 -1
- package/src/core/compiler.ts +15 -5
- package/src/core/expr.ts +4 -0
- package/src/core/parser.ts +14 -3
- package/src/core/runtime/render.ts +3 -3
package/README.md
CHANGED
|
@@ -169,11 +169,31 @@ Chain statements with ;. No wrapper functions. No script blocks.
|
|
|
169
169
|
```
|
|
170
170
|
|
|
171
171
|
```ts
|
|
172
|
-
|
|
173
|
-
|
|
172
|
+
page
|
|
173
|
+
|
|
174
|
+
title = "Remove Test"
|
|
175
|
+
|
|
176
|
+
var bool show = true
|
|
177
|
+
var bool deleted = false
|
|
178
|
+
|
|
179
|
+
Main {
|
|
180
|
+
Button(onClick: show = not show) {
|
|
181
|
+
if show { "Hide Box" } else { "Show Box" }
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
Button(onClick: remove("my-box"); deleted = true) {
|
|
185
|
+
"Delete Box Forever"
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
if show and not deleted {
|
|
189
|
+
Div(id: "my-box") {
|
|
190
|
+
Paragraph { "I'm a box!" }
|
|
191
|
+
}
|
|
192
|
+
}
|
|
174
193
|
}
|
|
194
|
+
|
|
175
195
|
```
|
|
176
|
-
Delete elements.
|
|
196
|
+
Delete elements. Easy.
|
|
177
197
|
|
|
178
198
|
Compiles to pure HTML.
|
|
179
199
|
|
|
@@ -242,6 +262,63 @@ Main {
|
|
|
242
262
|
}
|
|
243
263
|
```
|
|
244
264
|
|
|
265
|
+
## Use in real life
|
|
266
|
+
```ts
|
|
267
|
+
page
|
|
268
|
+
|
|
269
|
+
title = "Kick Users Admin Panel"
|
|
270
|
+
|
|
271
|
+
var str userName = "test username"
|
|
272
|
+
var int count = 0
|
|
273
|
+
|
|
274
|
+
style lang= "scss"{
|
|
275
|
+
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;600;700&display=swap');
|
|
276
|
+
|
|
277
|
+
* {
|
|
278
|
+
font-family: 'Inter', sans-serif;
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
Main {
|
|
283
|
+
Div(display: "flex", flexDirection: "column", gap: "16px", alignItems: "center", padding: "40px", fontFamily: "Inter") {
|
|
284
|
+
Heading(level: 1, fontSize: "28px", color: "#000000", fontWeight: "700") {
|
|
285
|
+
"Kick Counter: " + count
|
|
286
|
+
}
|
|
287
|
+
Div(display: "flex", gap: "12px", alignItems: "center") {
|
|
288
|
+
Input(
|
|
289
|
+
bind: userName,
|
|
290
|
+
padding: "10px 14px",
|
|
291
|
+
fontSize: "16px",
|
|
292
|
+
borderRadius: "8px",
|
|
293
|
+
border: "2px solid #6366f1",
|
|
294
|
+
outline: "none",
|
|
295
|
+
width: "250px",
|
|
296
|
+
backgroundColor: "#1e1b4b",
|
|
297
|
+
color: "#e0e7ff",
|
|
298
|
+
fontWeight: "400"
|
|
299
|
+
)
|
|
300
|
+
Button(
|
|
301
|
+
onClick: alert("Kicked: " + userName); console.log("User kicked"); count += 1,
|
|
302
|
+
padding: "10px 20px",
|
|
303
|
+
fontSize: "16px",
|
|
304
|
+
fontWeight: "600",
|
|
305
|
+
borderRadius: "8px",
|
|
306
|
+
border: "none",
|
|
307
|
+
backgroundColor: "#6366f1",
|
|
308
|
+
color: "#ffffff",
|
|
309
|
+
cursor: "pointer",
|
|
310
|
+
minWidth: "180px",
|
|
311
|
+
textAlign: "left"
|
|
312
|
+
) {
|
|
313
|
+
"Kick " + userName
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
Paragraph(fontSize: "14px", color: "#a5b4fc", fontWeight: "400") {
|
|
317
|
+
"Total kicks: " + count
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
```
|
|
245
322
|
|
|
246
323
|
# Install
|
|
247
324
|
npm install -g @yannosay/sinth
|
|
@@ -348,4 +425,4 @@ Main {
|
|
|
348
425
|
|
|
349
426
|
## License
|
|
350
427
|
|
|
351
|
-
AGPL-3.0
|
|
428
|
+
AGPL-3.0 (Just the compiler, the generated Output is 100% yours)
|
package/package.json
CHANGED
package/src/core/compiler.ts
CHANGED
|
@@ -159,7 +159,8 @@ if (name === "step" && value?.kind === "str") {
|
|
|
159
159
|
try {
|
|
160
160
|
const expr: Expression = JSON.parse(raw.substring(8));
|
|
161
161
|
if (expr.kind === "variable" && expr.name) {
|
|
162
|
-
|
|
162
|
+
const stepExprId = registerExpr(ctx, expr);
|
|
163
|
+
return `data-sinth-step="${stepExprId}"`;
|
|
163
164
|
}
|
|
164
165
|
} catch {}
|
|
165
166
|
}
|
|
@@ -385,6 +386,12 @@ export function renderChild(
|
|
|
385
386
|
}
|
|
386
387
|
if (child.expression.kind === "call" && child.expression.callee?.kind === "variable") {
|
|
387
388
|
const fnName = child.expression.callee.name;
|
|
389
|
+
if (fnName === "remove" && child.expression.args && child.expression.args.length === 1) {
|
|
390
|
+
const arg = child.expression.args[0];
|
|
391
|
+
if (arg.kind === "literal" && arg.value?.kind === "str") {
|
|
392
|
+
return `<span data-sinth-remove="${esc(arg.value.value)}"></span>`;
|
|
393
|
+
}
|
|
394
|
+
}
|
|
388
395
|
const fnDef = fnName ? ctx.functionDefs.find(f => f.name === fnName) : undefined;
|
|
389
396
|
if (fnDef && fnDef.returnType === "ui") {
|
|
390
397
|
const callArgs = child.expression.args ?? [];
|
|
@@ -829,7 +836,8 @@ export function renderCompUse(
|
|
|
829
836
|
const initialVal = varDecl?.value ? litToString(varDecl.value).replace(/"/g, '"') : "";
|
|
830
837
|
attrParts.push(`oninput="(function(e){ ${vName} = ${rhs}; sinthRender(); })(event)"`);
|
|
831
838
|
attrParts.push(`value="${escAttr(initialVal)}"`);
|
|
832
|
-
|
|
839
|
+
const valueExprId = registerExpr(ctx, { kind: "variable", name: vName });
|
|
840
|
+
attrParts.push(`data-sinth-value="${valueExprId}"`);
|
|
833
841
|
}
|
|
834
842
|
}
|
|
835
843
|
if (use.name === "Checkbox") {
|
|
@@ -845,14 +853,16 @@ export function renderCompUse(
|
|
|
845
853
|
try {
|
|
846
854
|
const expr: Expression = JSON.parse(raw.substring(8));
|
|
847
855
|
if (expr.kind === "variable" && expr.name) {
|
|
848
|
-
|
|
856
|
+
const checkedExprId = registerExpr(ctx, { kind: "variable", name: expr.name });
|
|
857
|
+
attrParts.push(`data-sinth-checked="${checkedExprId}"`);
|
|
849
858
|
} else {
|
|
850
859
|
const exprId = registerExpr(ctx, expr);
|
|
851
860
|
attrParts.push(`data-sinth-checked-expr="${exprId}"`);
|
|
852
861
|
}
|
|
853
862
|
} catch {}
|
|
854
863
|
} else {
|
|
855
|
-
|
|
864
|
+
const checkedExprId = registerExpr(ctx, { kind: "variable", name: raw });
|
|
865
|
+
attrParts.push(`data-sinth-checked="${checkedExprId}"`);
|
|
856
866
|
}
|
|
857
867
|
}
|
|
858
868
|
}
|
|
@@ -1061,7 +1071,7 @@ export function buildRuntime(opts: {
|
|
|
1061
1071
|
? `let __X = [${exprRegistry.map((js) => `function(_ctx){ return ${js}; }`).join(",")}];\n`
|
|
1062
1072
|
: "";
|
|
1063
1073
|
|
|
1064
|
-
let renderFunc = needsRender ? `
|
|
1074
|
+
let renderFunc = needsRender ? `var _sinthRenderPending = false;\nfunction sinthRender() {\n if (_sinthRenderPending) return;\n _sinthRenderPending = true;\n requestAnimationFrame(function() {\n _sinthRenderPending = false;\n${renderBody.replace(/^/gm, " ")}\n });\n}\nsinthRender();` : "";
|
|
1065
1075
|
|
|
1066
1076
|
if (needsFullscreenSync) {
|
|
1067
1077
|
renderFunc += `
|
package/src/core/expr.ts
CHANGED
|
@@ -38,6 +38,10 @@ export function compileExprToJS(expr: Expression, loopVars?: Set<string>): strin
|
|
|
38
38
|
return `${expr.target}${expr.op}`;
|
|
39
39
|
case "call": {
|
|
40
40
|
const callee = compileExprToJS(expr.callee!, loopVars);
|
|
41
|
+
if (callee === "remove") {
|
|
42
|
+
const arg = expr.args && expr.args[0] ? compileExprToJS(expr.args[0], loopVars) : "''";
|
|
43
|
+
return `(function(){ var _el=document.getElementById(${arg}); if(_el)_el.remove(); })()`;
|
|
44
|
+
}
|
|
41
45
|
const args = (expr.args ?? []).map(a => compileExprToJS(a, loopVars)).join(", ");
|
|
42
46
|
if (expr.args && expr.args.length === 0 && /\.length$/.test(callee)) {
|
|
43
47
|
return callee;
|
package/src/core/parser.ts
CHANGED
|
@@ -274,7 +274,8 @@ private parseArrayLiteral(): Literal {
|
|
|
274
274
|
parseExpression(skipBinary: boolean = false): Expression | null {
|
|
275
275
|
const tok = this.peek();
|
|
276
276
|
|
|
277
|
-
|
|
277
|
+
|
|
278
|
+
// unary 'not'
|
|
278
279
|
if ((tok.type === TT.IDENT && tok.value === "not") || tok.type === TT.OP_NOT) {
|
|
279
280
|
if (tok.type === TT.IDENT) this.consume(TT.IDENT); else this.consume(TT.OP_NOT);
|
|
280
281
|
const operand = this.parseExpression();
|
|
@@ -300,7 +301,17 @@ private parseArrayLiteral(): Literal {
|
|
|
300
301
|
return this.parseBinaryRHS(this.parseCallExpr(this.parsePostfix(base)));
|
|
301
302
|
}
|
|
302
303
|
|
|
304
|
+
// remove as function
|
|
305
|
+
if ((tok.type as any) === TT.KW_REMOVE) {
|
|
306
|
+
const name = this.consume(TT.KW_REMOVE).value;
|
|
307
|
+
return this.parseBinaryRHS(this.parseCallExpr({ kind: "variable", name }));
|
|
308
|
+
}
|
|
309
|
+
|
|
303
310
|
// var or assignment
|
|
311
|
+
if ((tok.type as any) === TT.KW_REMOVE) {
|
|
312
|
+
const name = this.consume(TT.KW_REMOVE).value;
|
|
313
|
+
return { kind: "variable", name };
|
|
314
|
+
}
|
|
304
315
|
if (tok.type === TT.IDENT) {
|
|
305
316
|
const name = this.consume(TT.IDENT).value;
|
|
306
317
|
let varExpr: Expression;
|
|
@@ -1145,8 +1156,8 @@ private parseArrayLiteral(): Literal {
|
|
|
1145
1156
|
continue;
|
|
1146
1157
|
}
|
|
1147
1158
|
else if (this.check(TT.KW_RETURN)) { children.push(this.parseReturnStmt()); continue; }
|
|
1148
|
-
else if (this.check(TT.KW_IF)) { children.push(this.parseIfBlock()); }
|
|
1149
|
-
else if (this.check(TT.KW_FOR)) { children.push(this.parseForLoop()); }
|
|
1159
|
+
else if (this.check(TT.KW_IF)) { children.push(this.parseIfBlock()); continue; }
|
|
1160
|
+
else if (this.check(TT.KW_FOR)) { children.push(this.parseForLoop()); continue; }
|
|
1150
1161
|
else if (this.check(TT.IDENT)) {
|
|
1151
1162
|
const loc = this.peek().loc;
|
|
1152
1163
|
const rawName = this.peek().value;
|
|
@@ -55,13 +55,13 @@ 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 { if (document.activeElement !== el) { el.value =
|
|
58
|
+
try { if (document.activeElement !== el) { let v=el.dataset.sinthValue; el.value = __X[v] ? __X[v]({}) : ''; } } catch(e) {}
|
|
59
59
|
});\n`;
|
|
60
60
|
renderBody += ` document.querySelectorAll('[data-sinth-step]').forEach(function(el) {
|
|
61
|
-
try {
|
|
61
|
+
try { let s=el.dataset.sinthStep; el.step = __X[s] ? __X[s]({}) : 1; } catch(e) {}
|
|
62
62
|
});\n`;
|
|
63
63
|
renderBody += ` document.querySelectorAll('[data-sinth-checked]').forEach(function(el) {
|
|
64
|
-
try { el.checked =
|
|
64
|
+
try { let c=el.dataset.sinthChecked; el.checked = __X[c] ? !!__X[c]({}) : false; } catch(e) {}
|
|
65
65
|
});\n`;
|
|
66
66
|
if (bodyHTML.includes("data-sinth-checked-expr")) {
|
|
67
67
|
renderBody += ` document.querySelectorAll('[data-sinth-checked-expr]').forEach(function(el) {
|