orvyn-lang 0.1.1 → 0.2.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/LANGUAGE.md +139 -3
- package/README.md +136 -1
- package/package.json +1 -1
- package/src/cli/orvyn.js +31 -480
- package/src/lang/codegen.js +90 -27
- package/src/lang/form.js +99 -0
- package/src/lang/native/android/OrvynRenderer.kt +5 -0
- package/src/lang/native/ios/OrvynEngine.swift +2 -0
- package/src/lang/native/ios/OrvynViews.swift +3 -0
- package/src/lang/native-runtime.js +22 -6
- package/src/lang/runtime-shared.js +28 -0
- package/src/lang/runtime.js +14 -1
- package/src/lang/validation.js +55 -0
- package/src/core/context.js +0 -19
- package/src/core/lifecycle.js +0 -8
- package/src/core/resource.js +0 -27
- package/src/core/router.js +0 -20
- package/src/core/theme.js +0 -10
- package/src/devtools/devicePreview/PreviewFrame.js +0 -214
- package/src/devtools/devicePreview/index.js +0 -6
- package/src/devtools/overlay.js +0 -105
- package/src/runtime/createRenderer.js +0 -22
- package/src/runtime/h.js +0 -93
- package/src/runtime/hydrate.js +0 -59
- package/src/runtime/jsx-dev-runtime.js +0 -7
- package/src/runtime/jsx-runtime.js +0 -13
- package/src/runtime/partialHydration.js +0 -154
- package/src/runtime/platform.js +0 -135
- package/src/runtime/renderToStream.js +0 -127
- package/src/runtime/renderToString.js +0 -121
- package/src/runtime/resume.js +0 -62
package/src/lang/codegen.js
CHANGED
|
@@ -6,7 +6,9 @@ const BUILTINS = {
|
|
|
6
6
|
Box: { tag: "div" },
|
|
7
7
|
Text: { tag: "span", arg: "child" },
|
|
8
8
|
Heading: { tag: "h1", arg: "child" },
|
|
9
|
-
Button: { tag: "button", arg: "child" },
|
|
9
|
+
Button: { tag: "button", props: { type: "button" }, arg: "child" },
|
|
10
|
+
Form: { tag: "form", props: { flex: true, direction: "column", noValidate: true } },
|
|
11
|
+
SubmitButton: { tag: "button", props: { type: "submit" }, arg: "child" },
|
|
10
12
|
Image: { tag: "img", arg: "src" },
|
|
11
13
|
Input: { tag: "input", arg: "placeholder" },
|
|
12
14
|
Link: { tag: "a", arg: "child" }
|
|
@@ -26,6 +28,7 @@ const GLOBALS = {
|
|
|
26
28
|
sleep: "__o.sleep",
|
|
27
29
|
random: "__o.random",
|
|
28
30
|
len: "__o.len",
|
|
31
|
+
validate: "__o.validate",
|
|
29
32
|
Math: "Math",
|
|
30
33
|
JSON: "JSON",
|
|
31
34
|
Date: "Date",
|
|
@@ -65,13 +68,22 @@ const JS_RESERVED = /* @__PURE__ */ new Set([
|
|
|
65
68
|
"instanceof",
|
|
66
69
|
"enum",
|
|
67
70
|
"static",
|
|
68
|
-
"arguments"
|
|
71
|
+
"arguments",
|
|
72
|
+
"eval",
|
|
73
|
+
"implements",
|
|
74
|
+
"interface",
|
|
75
|
+
"package",
|
|
76
|
+
"private",
|
|
77
|
+
"protected",
|
|
78
|
+
"public"
|
|
69
79
|
]);
|
|
70
80
|
class Scope {
|
|
71
81
|
vars = /* @__PURE__ */ new Map();
|
|
72
82
|
parent;
|
|
73
|
-
|
|
83
|
+
awaitAllowed;
|
|
84
|
+
constructor(parent = null, awaitAllowed = parent?.awaitAllowed ?? false) {
|
|
74
85
|
this.parent = parent;
|
|
86
|
+
this.awaitAllowed = awaitAllowed;
|
|
75
87
|
}
|
|
76
88
|
declare(name, binding, pos) {
|
|
77
89
|
if (this.vars.has(name)) {
|
|
@@ -83,13 +95,23 @@ class Scope {
|
|
|
83
95
|
return this.vars.get(name) ?? this.parent?.lookup(name);
|
|
84
96
|
}
|
|
85
97
|
}
|
|
98
|
+
const INTERNAL_NAMES = /* @__PURE__ */ new Set(["__o", "__signal", "__h", "__start", "__forms", "screens", "mountApp"]);
|
|
86
99
|
function jsName(name) {
|
|
87
|
-
return JS_RESERVED.has(name) ?
|
|
100
|
+
return JS_RESERVED.has(name) || INTERNAL_NAMES.has(name) || name.startsWith("__orvyn_") ? "__orvyn_" + Array.from(name, (c) => c.codePointAt(0).toString(16)).join("_") : name;
|
|
88
101
|
}
|
|
89
102
|
function fail(message, pos) {
|
|
90
103
|
throw new OrvynSyntaxError(message, pos.line, pos.col);
|
|
91
104
|
}
|
|
105
|
+
function containsWait(node) {
|
|
106
|
+
if (!node || typeof node !== "object") return false;
|
|
107
|
+
if (Array.isArray(node)) return node.some(containsWait);
|
|
108
|
+
const value = node;
|
|
109
|
+
if (value.kind === "Lambda") return false;
|
|
110
|
+
if (value.kind === "Wait") return true;
|
|
111
|
+
return Object.values(value).some(containsWait);
|
|
112
|
+
}
|
|
92
113
|
class Generator {
|
|
114
|
+
formId = 0;
|
|
93
115
|
out = [];
|
|
94
116
|
indent = 0;
|
|
95
117
|
global = new Scope();
|
|
@@ -123,6 +145,7 @@ class Generator {
|
|
|
123
145
|
imports.forEach((line) => this.line(line));
|
|
124
146
|
this.line("const __signal = __o.signal");
|
|
125
147
|
this.line("const __h = __o.createVNode");
|
|
148
|
+
this.line("const __forms = __o.formCache()");
|
|
126
149
|
this.line("");
|
|
127
150
|
for (const decl of this.program.body) {
|
|
128
151
|
if (decl.kind === "Use" || decl.kind === "Start") continue;
|
|
@@ -169,7 +192,7 @@ class Generator {
|
|
|
169
192
|
const inner = new Scope(scope);
|
|
170
193
|
decl.params.forEach((p) => inner.declare(p, "local", decl));
|
|
171
194
|
inner.declare("children", "local", decl);
|
|
172
|
-
const params = [...decl.params.map(jsName), "children"];
|
|
195
|
+
const params = [...decl.params.map((p) => p === jsName(p) ? p : `${p}: ${jsName(p)}`), "children"];
|
|
173
196
|
this.line(`export function ${decl.name}({ ${params.join(", ")} } = {}) {`);
|
|
174
197
|
this.indent++;
|
|
175
198
|
this.line(`return ${this.viewList(decl.body, inner)}`);
|
|
@@ -181,10 +204,10 @@ class Generator {
|
|
|
181
204
|
}
|
|
182
205
|
// `function name(params) { body }`, made async when the body uses wait
|
|
183
206
|
fnHeader(head, params, body, scope, pos = { line: 0, col: 0 }) {
|
|
184
|
-
const inner = new Scope(scope);
|
|
207
|
+
const inner = new Scope(scope, true);
|
|
185
208
|
params.forEach((p) => inner.declare(p, "local", pos));
|
|
186
209
|
const lines = this.capture(() => this.stmts(body, inner));
|
|
187
|
-
const isAsync =
|
|
210
|
+
const isAsync = containsWait(body);
|
|
188
211
|
const fullHead = isAsync ? head.replace(/^(export )?/, "$1async ") : head;
|
|
189
212
|
const code = [`${fullHead}(${params.map(jsName).join(", ")}) {`, ...lines.map((l) => " " + l), "}"];
|
|
190
213
|
return code.join("\n" + " ".repeat(this.indent));
|
|
@@ -258,8 +281,8 @@ class Generator {
|
|
|
258
281
|
if (binding === void 0) {
|
|
259
282
|
fail(`"${target.name}" is not declared. Add: state ${target.name} = ...`, target);
|
|
260
283
|
}
|
|
261
|
-
if (binding === "state") {
|
|
262
|
-
const name =
|
|
284
|
+
if (binding === "state" || binding === "imported") {
|
|
285
|
+
const name = this.stateRef(target.name, scope);
|
|
263
286
|
if (stmt.op === "=") return `${name}.set(${value})`;
|
|
264
287
|
const op = stmt.op[0];
|
|
265
288
|
return `${name}.set(${name}.get() ${op} (${value}))`;
|
|
@@ -272,14 +295,14 @@ class Generator {
|
|
|
272
295
|
}
|
|
273
296
|
return `${jsName(target.name)} ${stmt.op} ${value}`;
|
|
274
297
|
}
|
|
275
|
-
if ((target.kind === "Member" || target.kind === "Index") && target.object.kind === "Ident" && scope.lookup(target.object.name)
|
|
276
|
-
const name =
|
|
298
|
+
if ((target.kind === "Member" || target.kind === "Index") && target.object.kind === "Ident" && ["state", "imported"].includes(scope.lookup(target.object.name))) {
|
|
299
|
+
const name = this.stateRef(target.object.name, scope);
|
|
277
300
|
const current = target.kind === "Member" ? `${name}.get()?.${target.property}` : `${name}.get()?.[${this.expr(target.index, scope)}]`;
|
|
278
301
|
const next = stmt.op === "=" ? value : `${current} ${stmt.op[0]} (${value})`;
|
|
279
302
|
return target.kind === "Member" ? `${name}.set(__o.setField(${name}.get(), ${JSON.stringify(target.property)}, ${next}))` : `${name}.set(__o.setAt(${name}.get(), ${this.expr(target.index, scope)}, ${next}))`;
|
|
280
303
|
}
|
|
281
304
|
const root = rootIdent(target);
|
|
282
|
-
if (root && scope.lookup(root.name)
|
|
305
|
+
if (root && ["state", "imported"].includes(scope.lookup(root.name))) {
|
|
283
306
|
fail(
|
|
284
307
|
`Can't change part of state "${root.name}" directly. Assign a new value instead, like "${root.name} = ..."`,
|
|
285
308
|
target
|
|
@@ -295,15 +318,18 @@ class Generator {
|
|
|
295
318
|
}
|
|
296
319
|
// todos.add(x) on state → todos.set(__o.add(todos.get(), x))
|
|
297
320
|
stateListCall(expr, scope) {
|
|
298
|
-
if (expr.kind !== "Call" || expr.callee.kind !== "Member" || expr.callee.object.kind !== "Ident" || scope.lookup(expr.callee.object.name)
|
|
321
|
+
if (expr.kind !== "Call" || expr.callee.kind !== "Member" || expr.callee.object.kind !== "Ident" || !["state", "imported"].includes(scope.lookup(expr.callee.object.name))) {
|
|
299
322
|
return null;
|
|
300
323
|
}
|
|
301
324
|
const method = expr.callee.property;
|
|
302
325
|
if (!LIST_METHODS.has(method)) return null;
|
|
303
|
-
const name =
|
|
326
|
+
const name = this.stateRef(expr.callee.object.name, scope);
|
|
304
327
|
const args = [`${name}.get()`, ...expr.args.map((a) => this.expr(a, scope))];
|
|
305
328
|
return `${name}.set(__o.${method}(${args.join(", ")}))`;
|
|
306
329
|
}
|
|
330
|
+
stateRef(name, scope) {
|
|
331
|
+
return scope.lookup(name) === "imported" ? `__o.importedState(${jsName(name)}, ${JSON.stringify(name)})` : jsName(name);
|
|
332
|
+
}
|
|
307
333
|
// --- expressions ---------------------------------------------------------
|
|
308
334
|
// `bareWords`: in style props, unknown names become strings (bg: black)
|
|
309
335
|
expr(expr, scope, bareWords = false) {
|
|
@@ -321,8 +347,9 @@ class Generator {
|
|
|
321
347
|
case "Ident": {
|
|
322
348
|
const binding = scope.lookup(expr.name);
|
|
323
349
|
if (binding === "state") return `${jsName(expr.name)}.get()`;
|
|
350
|
+
if (binding === "imported") return `__o.readImported(${jsName(expr.name)})`;
|
|
324
351
|
if (binding !== void 0) return jsName(expr.name);
|
|
325
|
-
if (expr.name
|
|
352
|
+
if (Object.hasOwn(GLOBALS, expr.name)) return GLOBALS[expr.name];
|
|
326
353
|
if (bareWords) return JSON.stringify(expr.name);
|
|
327
354
|
return fail(`"${expr.name}" is not declared`, expr);
|
|
328
355
|
}
|
|
@@ -330,22 +357,24 @@ class Generator {
|
|
|
330
357
|
return `[${expr.items.map((item) => this.expr(item, scope)).join(", ")}]`;
|
|
331
358
|
// ?. so user.name is empty (not a crash) while data is loading
|
|
332
359
|
case "Member":
|
|
333
|
-
return `${this.
|
|
360
|
+
return `${this.postfix(expr.object, scope)}?.${expr.property}`;
|
|
334
361
|
case "Index":
|
|
335
|
-
return `${this.
|
|
362
|
+
return `${this.postfix(expr.object, scope)}?.[${this.expr(expr.index, scope)}]`;
|
|
336
363
|
case "Object":
|
|
337
364
|
return `{ ${expr.entries.map((e) => `${JSON.stringify(e.key)}: ${this.expr(e.value, scope)}`).join(", ")} }`;
|
|
338
365
|
case "Lambda": {
|
|
339
|
-
const inner = new Scope(scope);
|
|
366
|
+
const inner = new Scope(scope, true);
|
|
340
367
|
expr.params.forEach((p) => inner.declare(p, "local", expr));
|
|
341
|
-
|
|
368
|
+
const body = this.expr(expr.body, inner);
|
|
369
|
+
return `(${containsWait(expr.body) ? "async " : ""}(${expr.params.map(jsName).join(", ")}) => ${expr.body.kind === "Object" ? `(${body})` : body})`;
|
|
342
370
|
}
|
|
343
371
|
case "Ternary":
|
|
344
372
|
return `(${this.expr(expr.cond, scope)} ? ${this.expr(expr.then, scope, bareWords)} : ${this.expr(expr.else, scope, bareWords)})`;
|
|
345
373
|
case "Wait":
|
|
374
|
+
if (!scope.awaitAllowed) fail("Use wait inside fn, start, an event handler, or a lambda", expr);
|
|
346
375
|
return `(await ${this.expr(expr.expr, scope)})`;
|
|
347
376
|
case "Call":
|
|
348
|
-
return `${this.
|
|
377
|
+
return `${this.postfix(expr.callee, scope)}(${expr.args.map((a) => this.expr(a, scope)).join(", ")})`;
|
|
349
378
|
case "Unary":
|
|
350
379
|
return `${expr.op}${this.wrap(expr.operand, scope)}`;
|
|
351
380
|
case "Binary": {
|
|
@@ -354,6 +383,10 @@ class Generator {
|
|
|
354
383
|
}
|
|
355
384
|
}
|
|
356
385
|
}
|
|
386
|
+
postfix(expr, scope) {
|
|
387
|
+
const code = this.expr(expr, scope);
|
|
388
|
+
return ["Binary", "Unary", "Object"].includes(expr.kind) ? `(${code})` : code;
|
|
389
|
+
}
|
|
357
390
|
wrap(expr, scope) {
|
|
358
391
|
const code = this.expr(expr, scope);
|
|
359
392
|
return expr.kind === "Binary" || expr.kind === "Ternary" ? `(${code})` : code;
|
|
@@ -389,7 +422,7 @@ class Generator {
|
|
|
389
422
|
}
|
|
390
423
|
}
|
|
391
424
|
element(node, scope) {
|
|
392
|
-
const builtin = BUILTINS[node.name];
|
|
425
|
+
const builtin = Object.hasOwn(BUILTINS, node.name) ? BUILTINS[node.name] : void 0;
|
|
393
426
|
const imported = scope.lookup(node.name) === "imported";
|
|
394
427
|
const userComponent = this.components.get(node.name) ?? (imported ? null : void 0);
|
|
395
428
|
if (!builtin && userComponent === void 0) {
|
|
@@ -399,6 +432,12 @@ class Generator {
|
|
|
399
432
|
const children = [];
|
|
400
433
|
const props = [];
|
|
401
434
|
if (builtin) {
|
|
435
|
+
if (node.name === "Form" && !node.props.some((p) => p.key === "onSubmit")) {
|
|
436
|
+
fail("Form needs onSubmit, like Form onSubmit: saveProfile { ... }", node);
|
|
437
|
+
}
|
|
438
|
+
if (node.name === "SubmitButton" && node.props.some((p) => ["onTap", "onClick", "type"].includes(p.key))) {
|
|
439
|
+
fail("SubmitButton runs the Form's onSubmit. Put your action on Form onSubmit instead", node);
|
|
440
|
+
}
|
|
402
441
|
for (const [key, value] of Object.entries(builtin.props || {})) {
|
|
403
442
|
props.push(`${key}: ${JSON.stringify(value)}`);
|
|
404
443
|
}
|
|
@@ -408,7 +447,28 @@ class Generator {
|
|
|
408
447
|
else if (builtin.arg) props.push(`${builtin.arg}: ${arg}`);
|
|
409
448
|
else fail(`${node.name} doesn't take an unnamed argument. Use name: value`, node.arg);
|
|
410
449
|
}
|
|
411
|
-
|
|
450
|
+
const bind = node.props.find((p) => p.key === "bind");
|
|
451
|
+
if (bind) {
|
|
452
|
+
if (node.name !== "Input") fail("Use bind on Input, like Input bind: name", bind);
|
|
453
|
+
if (node.props.filter((p) => p.key === "bind").length > 1 || node.props.some((p) => ["value", "onChange", "onInput"].includes(p.key))) {
|
|
454
|
+
fail("bind supplies value and onChange. Remove the duplicate binding or event", bind);
|
|
455
|
+
}
|
|
456
|
+
if (bind.value.kind !== "Expr") fail("bind needs state, like bind: name or bind: user.name", bind);
|
|
457
|
+
const target = bind.value.expr;
|
|
458
|
+
const root = rootIdent(target);
|
|
459
|
+
if (!root || !["state", "imported"].includes(scope.lookup(root.name))) {
|
|
460
|
+
fail("bind needs changing state. Declare the value with state first", bind);
|
|
461
|
+
}
|
|
462
|
+
if (target.kind !== "Ident" && !((target.kind === "Member" || target.kind === "Index") && target.object.kind === "Ident")) {
|
|
463
|
+
fail("bind supports a state name or one field, like bind: user.name", bind);
|
|
464
|
+
}
|
|
465
|
+
const key = target.kind === "Member" ? JSON.stringify(target.property) : target.kind === "Index" ? this.expr(target.index, scope) : "undefined";
|
|
466
|
+
props.push(`value: ${this.expr(target, scope)}`);
|
|
467
|
+
props.push(`onInput: __o.bindInput(${this.stateRef(root.name, scope)}, ${key}, ${target.kind === "Index"})`);
|
|
468
|
+
}
|
|
469
|
+
for (const prop of node.props) {
|
|
470
|
+
if (prop.key !== "bind") props.push(this.prop(prop, node.name, scope));
|
|
471
|
+
}
|
|
412
472
|
} else {
|
|
413
473
|
if (node.arg) fail(`${node.name} needs named arguments, like ${node.name} title: "..."`, node.arg);
|
|
414
474
|
const params = userComponent;
|
|
@@ -417,9 +477,9 @@ class Generator {
|
|
|
417
477
|
fail(`${node.name} has no parameter "${prop.key}". It takes: ${params.join(", ") || "nothing"}`, prop);
|
|
418
478
|
}
|
|
419
479
|
if (prop.value.kind === "Handler") {
|
|
420
|
-
props.push(`${
|
|
480
|
+
props.push(`${prop.key}: ${this.handler(prop.value.body, scope)}`);
|
|
421
481
|
} else {
|
|
422
|
-
props.push(`${
|
|
482
|
+
props.push(`${prop.key}: ${this.expr(prop.value.expr, scope)}`);
|
|
423
483
|
}
|
|
424
484
|
}
|
|
425
485
|
}
|
|
@@ -427,6 +487,9 @@ class Generator {
|
|
|
427
487
|
if (!builtin) {
|
|
428
488
|
return `${node.name}({ ${[...props, `children: [${children.join(", ")}]`].join(", ")} })`;
|
|
429
489
|
}
|
|
490
|
+
if (node.name === "Form") {
|
|
491
|
+
return `__o.createForm(__forms, ${this.formId++}, { ${props.join(", ")} }, [${children.join(", ")}])`;
|
|
492
|
+
}
|
|
430
493
|
return `__h(${JSON.stringify(builtin.tag)}, { ${props.join(", ")} }, [${children.join(", ")}])`;
|
|
431
494
|
}
|
|
432
495
|
prop(prop, element, scope) {
|
|
@@ -453,13 +516,13 @@ class Generator {
|
|
|
453
516
|
} else {
|
|
454
517
|
handler = `() => ${this.expr(prop.value.expr, scope)}`;
|
|
455
518
|
}
|
|
456
|
-
return `${event}: ${handler}`;
|
|
519
|
+
return `${event}: ${element === "Form" && prop.key === "onSubmit" ? `__o.submitHandler(${handler})` : handler}`;
|
|
457
520
|
}
|
|
458
521
|
handler(body, scope, params = []) {
|
|
459
|
-
const inner = new Scope(scope);
|
|
522
|
+
const inner = new Scope(scope, true);
|
|
460
523
|
params.forEach((p) => inner.declare(p, "local", body[0] ?? { line: 0, col: 0 }));
|
|
461
524
|
const lines = this.capture(() => this.stmts(body, inner));
|
|
462
|
-
const isAsync =
|
|
525
|
+
const isAsync = containsWait(body);
|
|
463
526
|
return `${isAsync ? "async " : ""}(${params.join(", ")}) => {
|
|
464
527
|
${lines.join("\n")}
|
|
465
528
|
}`;
|
package/src/lang/form.js
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import { signal } from "../core/signal.js";
|
|
2
|
+
import { createVNode } from "../runtime/vnode.js";
|
|
3
|
+
import { validate } from "./validation.js";
|
|
4
|
+
const RULE_KEYS = ["required", "minLength", "maxLength", "email", "label", "message"];
|
|
5
|
+
const stateIds = /* @__PURE__ */ new WeakMap();
|
|
6
|
+
let nextId = 0;
|
|
7
|
+
function formCache() {
|
|
8
|
+
return /* @__PURE__ */ new Map();
|
|
9
|
+
}
|
|
10
|
+
function createForm(cache, slot, props, children) {
|
|
11
|
+
const fields = [];
|
|
12
|
+
const collect = (value, path) => {
|
|
13
|
+
if (Array.isArray(value)) return value.forEach((child, i) => collect(child, `${path}.${i}`));
|
|
14
|
+
if (!value || typeof value !== "object" || !value.tag || value.tag === "form") return;
|
|
15
|
+
if (value.tag === "input") {
|
|
16
|
+
const rules2 = {};
|
|
17
|
+
for (const key2 of RULE_KEYS) if (value.props[key2] !== void 0) rules2[key2] = value.props[key2];
|
|
18
|
+
if (value.props.type === "email") rules2.email = true;
|
|
19
|
+
if (Object.keys(rules2).length) {
|
|
20
|
+
if (!value.props.onInput) {
|
|
21
|
+
throw new Error('[Orvyn] A validated Input needs bind, like Input "Name", bind: name, required: true');
|
|
22
|
+
}
|
|
23
|
+
rules2.label ??= value.props.placeholder || "This field";
|
|
24
|
+
fields.push({ id: path, props: value.props, binding: value.props.onInput?.binding, rules: rules2 });
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
collect(value.children, `${path}.c`);
|
|
28
|
+
};
|
|
29
|
+
collect(children, "field");
|
|
30
|
+
const identity = fields.map((field) => {
|
|
31
|
+
const binding = field.binding;
|
|
32
|
+
if (!binding) return field.id;
|
|
33
|
+
if (!stateIds.has(binding.state)) stateIds.set(binding.state, ++nextId);
|
|
34
|
+
return [stateIds.get(binding.state), binding.key, binding.indexed];
|
|
35
|
+
});
|
|
36
|
+
const key = JSON.stringify([slot, props.key ?? identity]);
|
|
37
|
+
let errors = cache.get(key);
|
|
38
|
+
if (!errors) {
|
|
39
|
+
errors = { errors: signal({}), attempted: false, id: ++nextId };
|
|
40
|
+
cache.set(key, errors);
|
|
41
|
+
}
|
|
42
|
+
const currentErrors = errors.errors.get();
|
|
43
|
+
const byPath = new Map(fields.map((field) => [field.id, field]));
|
|
44
|
+
const values = () => Object.fromEntries(fields.map((field) => [
|
|
45
|
+
field.id,
|
|
46
|
+
field.binding ? field.binding.read() : field.props.value
|
|
47
|
+
]));
|
|
48
|
+
const rules = Object.fromEntries(fields.map((field) => [field.id, field.rules]));
|
|
49
|
+
validate(values(), rules);
|
|
50
|
+
const decorate = (value, path) => {
|
|
51
|
+
if (Array.isArray(value)) return value.map((child, i) => decorate(child, `${path}.${i}`));
|
|
52
|
+
if (!value || typeof value !== "object" || !value.tag || value.tag === "form") return value;
|
|
53
|
+
const field = byPath.get(path);
|
|
54
|
+
if (field) {
|
|
55
|
+
const inputProps = { ...value.props };
|
|
56
|
+
for (const key2 of RULE_KEYS) delete inputProps[key2];
|
|
57
|
+
inputProps["aria-invalid"] = !!currentErrors[field.id];
|
|
58
|
+
inputProps["aria-required"] = !!field.rules.required;
|
|
59
|
+
inputProps["aria-label"] ??= field.rules.label;
|
|
60
|
+
const errorId = `orvyn-form-${errors.id}-${field.id}-error`;
|
|
61
|
+
inputProps["aria-describedby"] = [inputProps["aria-describedby"], errorId].filter(Boolean).join(" ");
|
|
62
|
+
const original = inputProps.onInput;
|
|
63
|
+
if (original) inputProps.onInput = (event) => {
|
|
64
|
+
const result = original(event);
|
|
65
|
+
if (errors.attempted) {
|
|
66
|
+
const checked = validate(
|
|
67
|
+
{ [field.id]: field.binding ? field.binding.read() : event.target.value },
|
|
68
|
+
{ [field.id]: field.rules }
|
|
69
|
+
);
|
|
70
|
+
const next = { ...errors.errors.get() };
|
|
71
|
+
if (checked.valid) delete next[field.id];
|
|
72
|
+
else next[field.id] = checked.errors[field.id];
|
|
73
|
+
errors.errors.set(next);
|
|
74
|
+
}
|
|
75
|
+
return result;
|
|
76
|
+
};
|
|
77
|
+
return createVNode("div", { key: value.props.key ?? path, flex: true, direction: "column", gap: 4 }, [
|
|
78
|
+
createVNode("input", inputProps, value.children),
|
|
79
|
+
createVNode("span", { id: errorId, color: "red", role: "alert" }, [currentErrors[field.id] ?? ""])
|
|
80
|
+
]);
|
|
81
|
+
}
|
|
82
|
+
return createVNode(value.tag, value.props, decorate(value.children, `${path}.c`));
|
|
83
|
+
};
|
|
84
|
+
return createVNode("form", {
|
|
85
|
+
...props,
|
|
86
|
+
noValidate: true,
|
|
87
|
+
onSubmit: (event) => {
|
|
88
|
+
event?.preventDefault?.();
|
|
89
|
+
const result = validate(values(), rules);
|
|
90
|
+
errors.attempted = true;
|
|
91
|
+
errors.errors.set(result.errors);
|
|
92
|
+
if (result.valid) return props.onSubmit();
|
|
93
|
+
}
|
|
94
|
+
}, decorate(children, "field"));
|
|
95
|
+
}
|
|
96
|
+
export {
|
|
97
|
+
createForm,
|
|
98
|
+
formCache
|
|
99
|
+
};
|
|
@@ -15,6 +15,7 @@ import android.graphics.drawable.GradientDrawable
|
|
|
15
15
|
import android.os.Handler
|
|
16
16
|
import android.os.Looper
|
|
17
17
|
import android.text.Editable
|
|
18
|
+
import android.text.InputType
|
|
18
19
|
import android.text.TextWatcher
|
|
19
20
|
import android.util.TypedValue
|
|
20
21
|
import android.view.Gravity
|
|
@@ -134,6 +135,10 @@ class OrvynRenderer(
|
|
|
134
135
|
|
|
135
136
|
private fun updateInput(view: EditText, info: Info, node: JSONObject, style: JSONObject) {
|
|
136
137
|
view.hint = node.optString("ph", "")
|
|
138
|
+
val inputType = if (node.optString("inputType") == "email") {
|
|
139
|
+
InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS
|
|
140
|
+
} else InputType.TYPE_CLASS_TEXT
|
|
141
|
+
if (view.inputType != inputType) view.inputType = inputType
|
|
137
142
|
val value = if (node.has("v")) node.optString("v", "") else null
|
|
138
143
|
if (value != null && value != view.text.toString()) {
|
|
139
144
|
info.updating = true
|
|
@@ -13,6 +13,7 @@ struct ONode: Identifiable {
|
|
|
13
13
|
let s: [String: Any]
|
|
14
14
|
let x: String?
|
|
15
15
|
let ph: String?
|
|
16
|
+
let inputType: String?
|
|
16
17
|
let v: String?
|
|
17
18
|
let src: String?
|
|
18
19
|
let href: String?
|
|
@@ -26,6 +27,7 @@ struct ONode: Identifiable {
|
|
|
26
27
|
s = d["s"] as? [String: Any] ?? [:]
|
|
27
28
|
x = d["x"] as? String
|
|
28
29
|
ph = d["ph"] as? String
|
|
30
|
+
inputType = d["inputType"] as? String
|
|
29
31
|
v = d["v"] as? String
|
|
30
32
|
src = d["src"] as? String
|
|
31
33
|
href = d["href"] as? String
|
|
@@ -139,6 +139,9 @@ struct OInput: View {
|
|
|
139
139
|
|
|
140
140
|
var body: some View {
|
|
141
141
|
TextField(node.ph ?? "", text: $text)
|
|
142
|
+
.keyboardType(node.inputType == "email" ? .emailAddress : .default)
|
|
143
|
+
.textInputAutocapitalization(node.inputType == "email" ? .never : .sentences)
|
|
144
|
+
.disableAutocorrection(node.inputType == "email")
|
|
142
145
|
.padding(OStyle.number(node.s["padding"]) ?? 10)
|
|
143
146
|
.background(
|
|
144
147
|
RoundedRectangle(cornerRadius: OStyle.number(node.s["radius"]) ?? 8)
|
|
@@ -11,6 +11,10 @@ const savedSignal = createSavedSignal({
|
|
|
11
11
|
set: (key, value) => globalThis.__orvynStorageSet?.(key, value)
|
|
12
12
|
});
|
|
13
13
|
import {
|
|
14
|
+
readImported,
|
|
15
|
+
bindInput,
|
|
16
|
+
submitHandler,
|
|
17
|
+
importedState,
|
|
14
18
|
str,
|
|
15
19
|
add,
|
|
16
20
|
remove,
|
|
@@ -23,6 +27,8 @@ import {
|
|
|
23
27
|
random,
|
|
24
28
|
len
|
|
25
29
|
} from "./runtime-shared.js";
|
|
30
|
+
import { validate } from "./validation.js";
|
|
31
|
+
import { formCache, createForm } from "./form.js";
|
|
26
32
|
const host = globalThis;
|
|
27
33
|
let nextFetchId = 0;
|
|
28
34
|
const pendingFetches = /* @__PURE__ */ new Map();
|
|
@@ -133,7 +139,7 @@ function nodeType(tag, props) {
|
|
|
133
139
|
}
|
|
134
140
|
}
|
|
135
141
|
}
|
|
136
|
-
function toNative(value, id) {
|
|
142
|
+
function toNative(value, id, submit) {
|
|
137
143
|
if (typeof value === "string" || typeof value === "number") {
|
|
138
144
|
return { id, t: "text", s: {}, x: String(value) };
|
|
139
145
|
}
|
|
@@ -141,9 +147,10 @@ function toNative(value, id) {
|
|
|
141
147
|
if (typeof value.tag === "function") {
|
|
142
148
|
const rendered = value.tag({ ...value.props, children: value.children });
|
|
143
149
|
const list = flatten(Array.isArray(rendered) ? rendered : [rendered]);
|
|
144
|
-
return { id, t: "col", s: {}, c: list.map((child, i) => toNative(child, `${id}.${i}
|
|
150
|
+
return { id, t: "col", s: {}, c: list.map((child, i) => toNative(child, `${id}.${i}`, submit)).filter(Boolean) };
|
|
145
151
|
}
|
|
146
152
|
const props = value.props || {};
|
|
153
|
+
if (value.tag === "form") submit = props.onSubmit;
|
|
147
154
|
const t = nodeType(value.tag, props);
|
|
148
155
|
const node = { id: `${id}:${t}`, t, s: {} };
|
|
149
156
|
for (const key in props) {
|
|
@@ -152,13 +159,15 @@ function toNative(value, id) {
|
|
|
152
159
|
if (resolved !== void 0 && resolved !== null) node.s[key] = resolved;
|
|
153
160
|
}
|
|
154
161
|
}
|
|
155
|
-
|
|
156
|
-
|
|
162
|
+
const tap = props.onClick ?? (value.tag === "button" && props.type === "submit" ? submit : void 0);
|
|
163
|
+
if (tap) {
|
|
164
|
+
node.e = { ...node.e, tap: handlers.push(tap) - 1 };
|
|
157
165
|
}
|
|
158
166
|
if (props.onInput) {
|
|
159
167
|
node.e = { ...node.e, change: handlers.push(props.onInput) - 1 };
|
|
160
168
|
}
|
|
161
169
|
if (props.placeholder !== void 0) node.ph = String(props.placeholder);
|
|
170
|
+
if (t === "input" && props.type !== void 0) node.inputType = String(props.type);
|
|
162
171
|
if (props.value !== void 0) node.v = props.value === null ? "" : String(props.value);
|
|
163
172
|
if (props.src !== void 0) node.src = String(props.src);
|
|
164
173
|
if (props.href !== void 0) node.href = String(props.href);
|
|
@@ -167,7 +176,7 @@ function toNative(value, id) {
|
|
|
167
176
|
if (isLeaf) {
|
|
168
177
|
node.x = children.map((child) => typeof child === "string" || typeof child === "number" ? String(child) : "").join("");
|
|
169
178
|
} else if (t !== "input" && t !== "image") {
|
|
170
|
-
node.c = children.map((child, i) => toNative(child, `${node.id}.${i}
|
|
179
|
+
node.c = children.map((child, i) => toNative(child, `${node.id}.${i}`, submit)).filter(Boolean);
|
|
171
180
|
}
|
|
172
181
|
return node;
|
|
173
182
|
}
|
|
@@ -195,12 +204,17 @@ function startApp(_target, entry, onStart) {
|
|
|
195
204
|
}
|
|
196
205
|
export {
|
|
197
206
|
add,
|
|
207
|
+
bindInput,
|
|
198
208
|
clear,
|
|
209
|
+
createForm,
|
|
199
210
|
createVNode2 as createVNode,
|
|
200
211
|
fetchData,
|
|
212
|
+
formCache,
|
|
201
213
|
go,
|
|
214
|
+
importedState,
|
|
202
215
|
len,
|
|
203
216
|
random,
|
|
217
|
+
readImported,
|
|
204
218
|
remove,
|
|
205
219
|
removeAt,
|
|
206
220
|
savedSignal,
|
|
@@ -210,5 +224,7 @@ export {
|
|
|
210
224
|
sleep,
|
|
211
225
|
startApp,
|
|
212
226
|
str,
|
|
213
|
-
|
|
227
|
+
submitHandler,
|
|
228
|
+
update,
|
|
229
|
+
validate
|
|
214
230
|
};
|
|
@@ -1,4 +1,28 @@
|
|
|
1
1
|
import { signal } from "../core/signal.js";
|
|
2
|
+
function readImported(value) {
|
|
3
|
+
return value?.__orvynSignal === true ? value.get() : value;
|
|
4
|
+
}
|
|
5
|
+
function importedState(value, name) {
|
|
6
|
+
if (value?.__orvynSignal !== true) {
|
|
7
|
+
throw new Error(`[Orvyn] Can't change imported "${name}". Declare it with state in its source file.`);
|
|
8
|
+
}
|
|
9
|
+
return value;
|
|
10
|
+
}
|
|
11
|
+
function bindInput(state, key, indexed = false) {
|
|
12
|
+
const handler = (event) => {
|
|
13
|
+
const value = event.target.value;
|
|
14
|
+
state.set(indexed ? setAt(state.get(), key, value) : key !== void 0 ? setField(state.get(), String(key), value) : value);
|
|
15
|
+
};
|
|
16
|
+
return Object.assign(handler, {
|
|
17
|
+
binding: { state, key, indexed, read: () => key === void 0 && !indexed ? state.get() : state.get()?.[key] }
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
function submitHandler(handler) {
|
|
21
|
+
return (event) => {
|
|
22
|
+
event?.preventDefault?.();
|
|
23
|
+
return handler();
|
|
24
|
+
};
|
|
25
|
+
}
|
|
2
26
|
function createSavedSignal(storage) {
|
|
3
27
|
return function savedSignal(name, initial) {
|
|
4
28
|
const key = `orvyn:${name}`;
|
|
@@ -60,15 +84,19 @@ function len(value) {
|
|
|
60
84
|
}
|
|
61
85
|
export {
|
|
62
86
|
add,
|
|
87
|
+
bindInput,
|
|
63
88
|
clear,
|
|
64
89
|
createSavedSignal,
|
|
90
|
+
importedState,
|
|
65
91
|
len,
|
|
66
92
|
random,
|
|
93
|
+
readImported,
|
|
67
94
|
remove,
|
|
68
95
|
removeAt,
|
|
69
96
|
setAt,
|
|
70
97
|
setField,
|
|
71
98
|
sleep,
|
|
72
99
|
str,
|
|
100
|
+
submitHandler,
|
|
73
101
|
update
|
|
74
102
|
};
|
package/src/lang/runtime.js
CHANGED
|
@@ -18,6 +18,10 @@ const savedSignal = createSavedSignal({
|
|
|
18
18
|
}
|
|
19
19
|
});
|
|
20
20
|
import {
|
|
21
|
+
readImported,
|
|
22
|
+
bindInput,
|
|
23
|
+
submitHandler,
|
|
24
|
+
importedState,
|
|
21
25
|
str,
|
|
22
26
|
add,
|
|
23
27
|
remove,
|
|
@@ -30,6 +34,8 @@ import {
|
|
|
30
34
|
random,
|
|
31
35
|
len
|
|
32
36
|
} from "./runtime-shared.js";
|
|
37
|
+
import { validate } from "./validation.js";
|
|
38
|
+
import { formCache, createForm } from "./form.js";
|
|
33
39
|
async function fetchData(url, options = {}) {
|
|
34
40
|
const init = { ...options };
|
|
35
41
|
if (options.body !== void 0 && typeof options.body === "object") {
|
|
@@ -120,12 +126,17 @@ import { signal as signal2 } from "../core/signal.js";
|
|
|
120
126
|
import { createVNode as createVNode2 } from "../runtime/vnode.js";
|
|
121
127
|
export {
|
|
122
128
|
add,
|
|
129
|
+
bindInput,
|
|
123
130
|
clear,
|
|
131
|
+
createForm,
|
|
124
132
|
createVNode2 as createVNode,
|
|
125
133
|
fetchData,
|
|
134
|
+
formCache,
|
|
126
135
|
go,
|
|
136
|
+
importedState,
|
|
127
137
|
len,
|
|
128
138
|
random,
|
|
139
|
+
readImported,
|
|
129
140
|
remove,
|
|
130
141
|
removeAt,
|
|
131
142
|
savedSignal,
|
|
@@ -135,5 +146,7 @@ export {
|
|
|
135
146
|
sleep,
|
|
136
147
|
startApp,
|
|
137
148
|
str,
|
|
138
|
-
|
|
149
|
+
submitHandler,
|
|
150
|
+
update,
|
|
151
|
+
validate
|
|
139
152
|
};
|