@yannosay/sinth 0.13.5 → 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 +62 -8
- package/package.json +1 -1
- package/src/core/compiler.ts +70 -6
- package/src/core/runtime/render.ts +4 -1
- package/src/core/types.ts +1 -0
package/README.md
CHANGED
|
@@ -45,7 +45,7 @@ No JSX ternaries. No v-if. No separate script tags. Mixed logic. Clean.
|
|
|
45
45
|
```ts
|
|
46
46
|
page
|
|
47
47
|
|
|
48
|
-
title = "
|
|
48
|
+
title = "Functions"
|
|
49
49
|
|
|
50
50
|
var str userName = "Sinths User"
|
|
51
51
|
|
|
@@ -96,7 +96,7 @@ Paragraph {
|
|
|
96
96
|
|
|
97
97
|
```ts
|
|
98
98
|
page
|
|
99
|
-
title = "
|
|
99
|
+
title = "Fullscreen"
|
|
100
100
|
|
|
101
101
|
var bool isFullscreen = false
|
|
102
102
|
var str userName = ""
|
|
@@ -111,8 +111,8 @@ Main {
|
|
|
111
111
|
}
|
|
112
112
|
Input(bind: userName)
|
|
113
113
|
Paragraph {
|
|
114
|
-
if userName == "
|
|
115
|
-
"
|
|
114
|
+
if userName == "Sinth" {
|
|
115
|
+
"Oh, welcome back!"
|
|
116
116
|
} else {
|
|
117
117
|
"Hello, " + userName + "!"
|
|
118
118
|
}
|
|
@@ -120,6 +120,19 @@ Main {
|
|
|
120
120
|
}
|
|
121
121
|
```
|
|
122
122
|
|
|
123
|
+
#### Fullscreen Sync (0.13.6)
|
|
124
|
+
|
|
125
|
+
Opt-in Escape key handling. `fullscreenSync` keeps your variable in sync with the browser's fullscreen state.
|
|
126
|
+
|
|
127
|
+
```ts
|
|
128
|
+
Button(onClick: isFullscreen = !isFullscreen, fullscreen: isFullscreen, fullscreenSync: isFullscreen) {
|
|
129
|
+
if isFullscreen { "Exit Fullscreen" } else { "Enter Fullscreen" }
|
|
130
|
+
}
|
|
131
|
+
```
|
|
132
|
+
Without fullscreenSync, pressing Escape exits fullscreen but the variable stays true.
|
|
133
|
+
With it, Escape resets the variable to false — button text updates automatically.
|
|
134
|
+
|
|
135
|
+
|
|
123
136
|
#### Animations that make sense.
|
|
124
137
|
```ts
|
|
125
138
|
for item, index in items {
|
|
@@ -144,7 +157,49 @@ Delete elements for good. No virtual DOM diffing.
|
|
|
144
157
|
|
|
145
158
|
Compiles to pure HTML.
|
|
146
159
|
|
|
147
|
-
####
|
|
160
|
+
#### Reactivity at its finest
|
|
161
|
+
```ts
|
|
162
|
+
page
|
|
163
|
+
title = "Reactivity but easy"
|
|
164
|
+
|
|
165
|
+
var str begForFullscreen = ""
|
|
166
|
+
var bool doFullscreen = false
|
|
167
|
+
|
|
168
|
+
Main {
|
|
169
|
+
Input(bind: begForFullscreen, fullscreen: doFullscreen)
|
|
170
|
+
if begForFullscreen == "do fullscreen pls!!" {
|
|
171
|
+
doFullscreen = true
|
|
172
|
+
} else {
|
|
173
|
+
doFullscreen = false
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
```
|
|
177
|
+
|
|
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
|
+
|
|
148
203
|
|
|
149
204
|
|
|
150
205
|
# Install
|
|
@@ -169,11 +224,10 @@ Open your browser. Edit your .sinth file. Watch it update live.
|
|
|
169
224
|
page
|
|
170
225
|
title = "My Sinth Project"
|
|
171
226
|
|
|
172
|
-
var str name = "World"
|
|
173
227
|
|
|
174
228
|
Main {
|
|
175
|
-
var str
|
|
176
|
-
Heading(level: 1) { "Hello, " +
|
|
229
|
+
var str nameVar = "User"
|
|
230
|
+
Heading(level: 1) { "Hello, " + nameVar }
|
|
177
231
|
Paragraph { "Welcome to Sinth." }
|
|
178
232
|
}
|
|
179
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
|
|
|
@@ -137,6 +153,22 @@ if (name === "delay") {
|
|
|
137
153
|
return "";
|
|
138
154
|
}
|
|
139
155
|
|
|
156
|
+
if (name === "fullscreenSync") {
|
|
157
|
+
if (value?.kind === "str") {
|
|
158
|
+
const raw = value.value;
|
|
159
|
+
if (raw.startsWith("__EXPR__")) {
|
|
160
|
+
try {
|
|
161
|
+
const expr: Expression = JSON.parse(raw.substring(8));
|
|
162
|
+
if (expr.kind === "variable" && expr.name) {
|
|
163
|
+
return `data-sinth-fullscreen-sync="${escAttr(expr.name)}"`;
|
|
164
|
+
}
|
|
165
|
+
} catch {}
|
|
166
|
+
}
|
|
167
|
+
return `data-sinth-fullscreen-sync="${escAttr(raw)}"`;
|
|
168
|
+
}
|
|
169
|
+
return "";
|
|
170
|
+
}
|
|
171
|
+
|
|
140
172
|
if (name === "checked") {
|
|
141
173
|
return (value as any)?.kind === "bool" && (value as any).value ? "checked" : "";
|
|
142
174
|
}
|
|
@@ -564,8 +596,21 @@ export function renderCompUse(
|
|
|
564
596
|
if (expr.kind === "variable" && expr.name) vName = expr.name;
|
|
565
597
|
} catch {}
|
|
566
598
|
}
|
|
567
|
-
|
|
568
|
-
|
|
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)"`);
|
|
569
614
|
attrParts.push(`data-sinth-value="${escAttr(vName)}"`);
|
|
570
615
|
}
|
|
571
616
|
}
|
|
@@ -744,8 +789,8 @@ export function buildRuntime(opts: {
|
|
|
744
789
|
sharedRuntime: boolean;
|
|
745
790
|
functionsJS: string;
|
|
746
791
|
}): string | { page: string; shared: string } {
|
|
792
|
+
|
|
747
793
|
const { varDecls, bodyHTML, logicBlocks, mixedBlocks, assignedVars, exprRegistry, functionsJS } = opts;
|
|
748
|
-
|
|
749
794
|
const needsMixed = mixedBlocks.length > 0;
|
|
750
795
|
const needsExpr = bodyHTML.includes("sinth-expr") || needsMixed;
|
|
751
796
|
const needsIf = bodyHTML.includes("data-sinth-if") || needsMixed;
|
|
@@ -753,7 +798,8 @@ export function buildRuntime(opts: {
|
|
|
753
798
|
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"));
|
|
754
799
|
const needsLogic = logicBlocks.length > 0;
|
|
755
800
|
const needsFullscreen = bodyHTML.includes("data-sinth-fullscreen");
|
|
756
|
-
const
|
|
801
|
+
const needsFullscreenSync = bodyHTML.includes("data-sinth-fullscreen-sync");
|
|
802
|
+
const needsRender = needsExpr || needsIf || needsFor || needsMixed || needsLogic || bodyHTML.includes("data-sinth-hide") || needsFullscreen || needsFullscreenSync;
|
|
757
803
|
|
|
758
804
|
const varLines = varDecls.map(v => {
|
|
759
805
|
if (!v.value) {
|
|
@@ -797,7 +843,24 @@ export function buildRuntime(opts: {
|
|
|
797
843
|
? `var __X = [${exprRegistry.map((js) => `function(_ctx){ return ${js}; }`).join(",")}];\n`
|
|
798
844
|
: "";
|
|
799
845
|
|
|
800
|
-
|
|
846
|
+
let renderFunc = needsRender ? `function sinthRender() {\n${renderBody}}\nsinthRender();` : "";
|
|
847
|
+
|
|
848
|
+
if (needsFullscreenSync) {
|
|
849
|
+
renderFunc += `
|
|
850
|
+
(function() {
|
|
851
|
+
var syncedEls = document.querySelectorAll('[data-sinth-fullscreen-sync]');
|
|
852
|
+
if (syncedEls.length > 0) {
|
|
853
|
+
var vars = [];
|
|
854
|
+
syncedEls.forEach(function(el) { vars.push(el.dataset.sinthFullscreenSync); });
|
|
855
|
+
document.addEventListener('fullscreenchange', function() {
|
|
856
|
+
if (!document.fullscreenElement) {
|
|
857
|
+
vars.forEach(function(v) { window[v] = false; });
|
|
858
|
+
sinthRender();
|
|
859
|
+
}
|
|
860
|
+
});
|
|
861
|
+
}
|
|
862
|
+
})();`;
|
|
863
|
+
}
|
|
801
864
|
|
|
802
865
|
const pageCode = `// Sinth page runtime
|
|
803
866
|
${varLines}
|
|
@@ -850,6 +913,7 @@ export function compileFile(filePath: string, opts: CompileOptions): { html: str
|
|
|
850
913
|
ifIdCounter: 0,
|
|
851
914
|
exprRegistry: [],
|
|
852
915
|
exprMap: new Map(),
|
|
916
|
+
varDecls: allVarDecls,
|
|
853
917
|
};
|
|
854
918
|
|
|
855
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) {}
|