@c9up/orbit 0.0.0-stage → 0.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/Cargo.lock +146 -0
- package/Cargo.toml +16 -0
- package/LICENSE +21 -0
- package/README.md +314 -2
- package/android/orbit.cmake +31 -0
- package/android/src/main/cpp/NativeOrbit.cpp +309 -0
- package/android/src/main/cpp/NativeOrbit.h +35 -0
- package/android/src/main/cpp/orbit_host.h +34 -0
- package/android/src/main/java/com/c9up/orbit/OrbitEffects.kt +286 -0
- package/android/src/main/java/com/c9up/orbit/OrbitHost.kt +258 -0
- package/android/src/main/java/com/c9up/orbit/OrbitImage.kt +346 -0
- package/android/src/main/java/com/c9up/orbit/OrbitInput.kt +145 -0
- package/android/src/main/java/com/c9up/orbit/OrbitPress.kt +110 -0
- package/android/src/main/java/com/c9up/orbit/OrbitVector.kt +115 -0
- package/android/src/main/java/com/c9up/orbit/OrbitViews.kt +362 -0
- package/crates/orbit-host/Cargo.toml +14 -0
- package/crates/orbit-host/src/gesture.rs +561 -0
- package/crates/orbit-host/src/lib.rs +1608 -0
- package/crates/orbit-host/src/motion.rs +328 -0
- package/crates/orbit-host/src/vector.rs +917 -0
- package/dist/color.d.ts +10 -0
- package/dist/color.d.ts.map +1 -0
- package/dist/color.js +62 -0
- package/dist/color.js.map +1 -0
- package/dist/commit.d.ts +45 -0
- package/dist/commit.d.ts.map +1 -0
- package/dist/commit.js +172 -0
- package/dist/commit.js.map +1 -0
- package/dist/errors.d.ts +42 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +15 -0
- package/dist/errors.js.map +1 -0
- package/dist/host.d.ts +49 -0
- package/dist/host.d.ts.map +1 -0
- package/dist/host.js +18 -0
- package/dist/host.js.map +1 -0
- package/dist/index.d.ts +29 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +29 -0
- package/dist/index.js.map +1 -0
- package/dist/motion.d.ts +18 -0
- package/dist/motion.d.ts.map +1 -0
- package/dist/motion.js +66 -0
- package/dist/motion.js.map +1 -0
- package/dist/node.d.ts +46 -0
- package/dist/node.d.ts.map +1 -0
- package/dist/node.js +110 -0
- package/dist/node.js.map +1 -0
- package/dist/pages.d.ts +56 -0
- package/dist/pages.d.ts.map +1 -0
- package/dist/pages.js +157 -0
- package/dist/pages.js.map +1 -0
- package/dist/react-native/index.d.ts +25 -0
- package/dist/react-native/index.d.ts.map +1 -0
- package/dist/react-native/index.js +43 -0
- package/dist/react-native/index.js.map +1 -0
- package/dist/server.d.ts +85 -0
- package/dist/server.d.ts.map +1 -0
- package/dist/server.js +282 -0
- package/dist/server.js.map +1 -0
- package/dist/stack.d.ts +117 -0
- package/dist/stack.d.ts.map +1 -0
- package/dist/stack.js +299 -0
- package/dist/stack.js.map +1 -0
- package/dist/styles.d.ts +138 -0
- package/dist/styles.d.ts.map +1 -0
- package/dist/styles.js +920 -0
- package/dist/styles.js.map +1 -0
- package/dist/template.d.ts +27 -0
- package/dist/template.d.ts.map +1 -0
- package/dist/template.js +1392 -0
- package/dist/template.js.map +1 -0
- package/dist/url.d.ts +45 -0
- package/dist/url.d.ts.map +1 -0
- package/dist/url.js +302 -0
- package/dist/url.js.map +1 -0
- package/package.json +90 -3
- package/scripts/build-android.sh +27 -0
- package/scripts/test-styles.mjs +31 -0
- package/specs/NativeOrbit.ts +34 -0
- package/src/color.ts +73 -0
- package/src/commit.ts +206 -0
- package/src/errors.ts +48 -0
- package/src/host.ts +92 -0
- package/src/index.ts +60 -0
- package/src/motion.ts +86 -0
- package/src/node.ts +133 -0
- package/src/pages.ts +210 -0
- package/src/react-native/index.ts +76 -0
- package/src/react-native/react-native.d.ts +31 -0
- package/src/server.ts +359 -0
- package/src/stack.ts +434 -0
- package/src/styles.ts +1024 -0
- package/src/template.ts +1657 -0
- package/src/url.ts +326 -0
package/src/template.ts
ADDED
|
@@ -0,0 +1,1657 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* aurora templates as native views. `html` only returns `{strings, values}`;
|
|
3
|
+
* this is the native counterpart of aurora's DOM compiler.
|
|
4
|
+
*
|
|
5
|
+
* Mapping (web semantics kept where native differs):
|
|
6
|
+
* - block elements → RCTView; p / h1–h6 / label → RCTText; inline elements → RCTVirtualText.
|
|
7
|
+
* - Text sitting in a view (illegal on native) is wrapped in an anonymous RCTText.
|
|
8
|
+
* - Text styles cascade like CSS inheritance; views carry only box styles.
|
|
9
|
+
* - In a flex container every child is its own item; `flex` is a row, as on the web.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import {
|
|
13
|
+
batch,
|
|
14
|
+
effect,
|
|
15
|
+
isTemplateResult,
|
|
16
|
+
memo,
|
|
17
|
+
type Signal,
|
|
18
|
+
signal,
|
|
19
|
+
untrack,
|
|
20
|
+
} from "@c9up/aurora/core";
|
|
21
|
+
import {
|
|
22
|
+
assertBindable,
|
|
23
|
+
bindTemplateContext,
|
|
24
|
+
type EachList,
|
|
25
|
+
firstWindow,
|
|
26
|
+
LazyLayout,
|
|
27
|
+
type LazyList,
|
|
28
|
+
type LazyView,
|
|
29
|
+
readComponentLifecycle,
|
|
30
|
+
readEach,
|
|
31
|
+
readEachItems,
|
|
32
|
+
sameWindow,
|
|
33
|
+
setOwner,
|
|
34
|
+
type TemplateResult,
|
|
35
|
+
TemplateScanner,
|
|
36
|
+
} from "@c9up/aurora/renderer";
|
|
37
|
+
import { colorToArgb } from "./color.js";
|
|
38
|
+
import {
|
|
39
|
+
afterCommit,
|
|
40
|
+
type MountOptions,
|
|
41
|
+
mountTree,
|
|
42
|
+
onEveryCommit,
|
|
43
|
+
type Root,
|
|
44
|
+
treeHost,
|
|
45
|
+
} from "./commit.js";
|
|
46
|
+
import { OrbitError } from "./errors.js";
|
|
47
|
+
import {
|
|
48
|
+
bindMotion,
|
|
49
|
+
bindScrollY,
|
|
50
|
+
type MotionStyle,
|
|
51
|
+
MotionValue,
|
|
52
|
+
readMotion,
|
|
53
|
+
} from "./motion.js";
|
|
54
|
+
import {
|
|
55
|
+
type Child,
|
|
56
|
+
flatten,
|
|
57
|
+
NativeNode,
|
|
58
|
+
type Props,
|
|
59
|
+
Region,
|
|
60
|
+
rawText,
|
|
61
|
+
type Style,
|
|
62
|
+
} from "./node.js";
|
|
63
|
+
import { Stack } from "./stack.js";
|
|
64
|
+
import {
|
|
65
|
+
type Computed,
|
|
66
|
+
computeStyle,
|
|
67
|
+
type StyledElement,
|
|
68
|
+
viewportSize,
|
|
69
|
+
} from "./styles.js";
|
|
70
|
+
|
|
71
|
+
type Part = string | number; // number = index into the template's values
|
|
72
|
+
type TAttr = { name: string; parts: Part[] };
|
|
73
|
+
type TElement = { kind: "el"; tag: string; attrs: TAttr[]; children: TNode[] };
|
|
74
|
+
type TNode = TElement | { kind: "text"; parts: Part[] };
|
|
75
|
+
|
|
76
|
+
const OPEN = "";
|
|
77
|
+
const CLOSE = "";
|
|
78
|
+
const SLOT = /(\d+)/;
|
|
79
|
+
const VOID = new Set(["br", "hr", "img", "input", "meta", "link"]);
|
|
80
|
+
const TEXT_BLOCK = new Set(["p", "h1", "h2", "h3", "h4", "h5", "h6", "label"]);
|
|
81
|
+
const INLINE = new Set([
|
|
82
|
+
"span",
|
|
83
|
+
"strong",
|
|
84
|
+
"b",
|
|
85
|
+
"em",
|
|
86
|
+
"i",
|
|
87
|
+
"small",
|
|
88
|
+
"code",
|
|
89
|
+
"a",
|
|
90
|
+
]);
|
|
91
|
+
const UNSUPPORTED = new Set(["select", "canvas", "iframe"]);
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Inline, as CSS lays it out: a run inside the surrounding text. An `<a>`
|
|
95
|
+
* holding blocks (a card that is a link) is a block, as HTML lets it be.
|
|
96
|
+
*/
|
|
97
|
+
function isInline(n: TElement): boolean {
|
|
98
|
+
if (!INLINE.has(n.tag)) return false;
|
|
99
|
+
if (n.tag !== "a") return true;
|
|
100
|
+
return n.children.every((child) => child.kind !== "el" || isInline(child));
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* A link: an `<a href>`. Sitting in a view, it is a touch target of its own
|
|
105
|
+
* rather than a run of the text around it — a native text's runs cannot be
|
|
106
|
+
* pressed one by one — so links in a block (`<nav>`, `<li>`) each take their
|
|
107
|
+
* line, as a row of them does in a `flex` parent.
|
|
108
|
+
*/
|
|
109
|
+
function isLink(n: TElement): boolean {
|
|
110
|
+
return n.tag === "a" && n.attrs.some((attr) => attr.name === "href");
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/** Where a pressed `<a href>` goes: server-driven pages set it (`@c9up/orbit/pages`). */
|
|
114
|
+
let followLink: ((href: string) => void) | undefined;
|
|
115
|
+
|
|
116
|
+
/** @internal Who follows a pressed link; `undefined` leaves links inert. */
|
|
117
|
+
export function setLinkHandler(
|
|
118
|
+
handler: ((href: string) => void) | undefined,
|
|
119
|
+
): void {
|
|
120
|
+
followLink = handler;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// Tailwind's preflight resets headings; only these keep a user-agent look.
|
|
124
|
+
const TAG_TEXT: { [tag: string]: Style } = {
|
|
125
|
+
strong: { fontWeight: "700" },
|
|
126
|
+
b: { fontWeight: "700" },
|
|
127
|
+
em: { fontStyle: "italic" },
|
|
128
|
+
i: { fontStyle: "italic" },
|
|
129
|
+
small: { fontSize: 13 },
|
|
130
|
+
code: { fontFamily: "monospace" },
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
const ENTITIES: { [name: string]: string } = {
|
|
134
|
+
amp: "&",
|
|
135
|
+
lt: "<",
|
|
136
|
+
gt: ">",
|
|
137
|
+
quot: '"',
|
|
138
|
+
"#39": "'",
|
|
139
|
+
nbsp: " ",
|
|
140
|
+
};
|
|
141
|
+
|
|
142
|
+
const decode = (s: string) =>
|
|
143
|
+
s.replace(
|
|
144
|
+
/&(amp|lt|gt|quot|#39|nbsp);/g,
|
|
145
|
+
(_, name: string) => ENTITIES[name] ?? "",
|
|
146
|
+
);
|
|
147
|
+
|
|
148
|
+
function splitParts(raw: string): Part[] {
|
|
149
|
+
const out: Part[] = [];
|
|
150
|
+
raw.split(SLOT).forEach((piece, i) => {
|
|
151
|
+
if (i % 2 === 1) out.push(Number(piece));
|
|
152
|
+
else if (piece) out.push(decode(piece));
|
|
153
|
+
});
|
|
154
|
+
return out;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
// ─── Parsing ─────────────────────────────────────────────────────
|
|
158
|
+
|
|
159
|
+
const astCache = new WeakMap<TemplateStringsArray, TNode[]>();
|
|
160
|
+
|
|
161
|
+
function parse(strings: TemplateStringsArray): TNode[] {
|
|
162
|
+
const hit = astCache.get(strings);
|
|
163
|
+
if (hit) return hit;
|
|
164
|
+
|
|
165
|
+
// Same refusals as the web path: aurora's own scanner vets every slot position.
|
|
166
|
+
const scanner = new TemplateScanner();
|
|
167
|
+
for (let i = 0; i < strings.length - 1; i++) {
|
|
168
|
+
scanner.consume(strings[i] ?? "");
|
|
169
|
+
assertBindable(scanner);
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
let s = strings[0] ?? "";
|
|
173
|
+
for (let i = 1; i < strings.length; i++)
|
|
174
|
+
s += `${OPEN}${i - 1}${CLOSE}${strings[i] ?? ""}`;
|
|
175
|
+
|
|
176
|
+
const root: TNode[] = [];
|
|
177
|
+
const stack: { tag: string; children: TNode[] }[] = [
|
|
178
|
+
{ tag: "", children: root },
|
|
179
|
+
];
|
|
180
|
+
const top = () => stack[stack.length - 1] ?? { tag: "", children: root };
|
|
181
|
+
let i = 0;
|
|
182
|
+
while (i < s.length) {
|
|
183
|
+
if (s.startsWith("<!--", i)) {
|
|
184
|
+
const end = s.indexOf("-->", i + 4);
|
|
185
|
+
i = end < 0 ? s.length : end + 3;
|
|
186
|
+
} else if (s.startsWith("</", i)) {
|
|
187
|
+
const end = s.indexOf(">", i);
|
|
188
|
+
const name = s
|
|
189
|
+
.slice(i + 2, end)
|
|
190
|
+
.trim()
|
|
191
|
+
.toLowerCase();
|
|
192
|
+
while (stack.length > 1 && stack.pop()?.tag !== name) {
|
|
193
|
+
/* pop up to the matching open tag */
|
|
194
|
+
}
|
|
195
|
+
i = end + 1;
|
|
196
|
+
} else if (s.charAt(i) === "<" && /[a-zA-Z]/.test(s.charAt(i + 1))) {
|
|
197
|
+
let j = i + 1;
|
|
198
|
+
while (/[a-zA-Z0-9-]/.test(s.charAt(j))) j++;
|
|
199
|
+
const tag = s.slice(i + 1, j).toLowerCase();
|
|
200
|
+
const attrs: TAttr[] = [];
|
|
201
|
+
let selfClosing = false;
|
|
202
|
+
while (j < s.length) {
|
|
203
|
+
while (/\s/.test(s.charAt(j))) j++;
|
|
204
|
+
if (s.charAt(j) === ">") {
|
|
205
|
+
j++;
|
|
206
|
+
break;
|
|
207
|
+
}
|
|
208
|
+
if (s.startsWith("/>", j)) {
|
|
209
|
+
selfClosing = true;
|
|
210
|
+
j += 2;
|
|
211
|
+
break;
|
|
212
|
+
}
|
|
213
|
+
let k = j;
|
|
214
|
+
while (
|
|
215
|
+
k < s.length &&
|
|
216
|
+
!/[\s=>]/.test(s.charAt(k)) &&
|
|
217
|
+
!s.startsWith("/>", k)
|
|
218
|
+
)
|
|
219
|
+
k++;
|
|
220
|
+
const name = s.slice(j, k);
|
|
221
|
+
j = k;
|
|
222
|
+
let value = "";
|
|
223
|
+
if (s.charAt(j) === "=") {
|
|
224
|
+
j++;
|
|
225
|
+
const quote = s.charAt(j);
|
|
226
|
+
if (quote === '"' || quote === "'") {
|
|
227
|
+
const end = s.indexOf(quote, j + 1);
|
|
228
|
+
value = s.slice(j + 1, end);
|
|
229
|
+
j = end + 1;
|
|
230
|
+
} else {
|
|
231
|
+
let end = j;
|
|
232
|
+
while (end < s.length && !/[\s>]/.test(s.charAt(end))) end++;
|
|
233
|
+
value = s.slice(j, end);
|
|
234
|
+
j = end;
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
attrs.push({ name, parts: splitParts(value) });
|
|
238
|
+
}
|
|
239
|
+
const el: TElement = { kind: "el", tag, attrs, children: [] };
|
|
240
|
+
top().children.push(el);
|
|
241
|
+
if (!selfClosing && !VOID.has(tag))
|
|
242
|
+
stack.push({ tag, children: el.children });
|
|
243
|
+
i = j;
|
|
244
|
+
} else {
|
|
245
|
+
let end = s.indexOf("<", i + 1);
|
|
246
|
+
if (end < 0) end = s.length;
|
|
247
|
+
// CSS-like whitespace: runs collapse; edges are trimmed later, at block boundaries.
|
|
248
|
+
const collapsed = s.slice(i, end).replace(/\s+/g, " ");
|
|
249
|
+
if (collapsed.trim())
|
|
250
|
+
top().children.push({ kind: "text", parts: splitParts(collapsed) });
|
|
251
|
+
i = end;
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
trimAtBlocks(root);
|
|
255
|
+
astCache.set(strings, root);
|
|
256
|
+
return root;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
const isBlock = (n: TNode | undefined) =>
|
|
260
|
+
n === undefined || (n.kind === "el" && !isInline(n));
|
|
261
|
+
|
|
262
|
+
// A space next to a block boundary renders nothing in CSS; next to inline content it stays.
|
|
263
|
+
function trimAtBlocks(children: TNode[]): void {
|
|
264
|
+
children.forEach((n, i) => {
|
|
265
|
+
if (n.kind === "el") {
|
|
266
|
+
trimAtBlocks(n.children);
|
|
267
|
+
return;
|
|
268
|
+
}
|
|
269
|
+
const first = n.parts[0];
|
|
270
|
+
if (isBlock(children[i - 1]) && typeof first === "string")
|
|
271
|
+
n.parts[0] = first.trimStart();
|
|
272
|
+
const lastIndex = n.parts.length - 1;
|
|
273
|
+
const last = n.parts[lastIndex];
|
|
274
|
+
if (isBlock(children[i + 1]) && typeof last === "string")
|
|
275
|
+
n.parts[lastIndex] = last.trimEnd();
|
|
276
|
+
n.parts = n.parts.filter((p) => p !== "");
|
|
277
|
+
});
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
// ─── Values ──────────────────────────────────────────────────────
|
|
281
|
+
|
|
282
|
+
const isGetter = (v: unknown): v is () => unknown => typeof v === "function";
|
|
283
|
+
const isPrimitive = (v: unknown) =>
|
|
284
|
+
v === null || (typeof v !== "object" && typeof v !== "function");
|
|
285
|
+
|
|
286
|
+
function str(v: unknown): string {
|
|
287
|
+
if (v === null || v === undefined || v === false) return "";
|
|
288
|
+
return typeof v === "string" ||
|
|
289
|
+
typeof v === "number" ||
|
|
290
|
+
typeof v === "boolean" ||
|
|
291
|
+
typeof v === "bigint"
|
|
292
|
+
? String(v)
|
|
293
|
+
: "";
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
// Disposers of the reactive scope being built: a Region tears them down when
|
|
297
|
+
// its slot re-runs, so replaced subtrees release their signal subscriptions.
|
|
298
|
+
let collecting: Array<() => void> | null = null;
|
|
299
|
+
|
|
300
|
+
function ownLifecycle(result: TemplateResult): void {
|
|
301
|
+
const lifecycle = readComponentLifecycle(result);
|
|
302
|
+
if (!lifecycle) return;
|
|
303
|
+
const scope = collecting;
|
|
304
|
+
scope?.push(...lifecycle.cleanups);
|
|
305
|
+
afterCommit(() => {
|
|
306
|
+
for (const hook of lifecycle.mountHooks) {
|
|
307
|
+
const teardown = hook();
|
|
308
|
+
if (typeof teardown === "function") scope?.push(teardown);
|
|
309
|
+
}
|
|
310
|
+
});
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
function region(
|
|
314
|
+
slot: () => unknown,
|
|
315
|
+
build: (value: unknown) => Child[],
|
|
316
|
+
): Region {
|
|
317
|
+
const out = new Region();
|
|
318
|
+
let disposers: Array<() => void> = [];
|
|
319
|
+
effect(() => {
|
|
320
|
+
const value = slot();
|
|
321
|
+
untrack(() => {
|
|
322
|
+
for (const dispose of disposers.splice(0)) dispose();
|
|
323
|
+
const scope: Array<() => void> = [];
|
|
324
|
+
const prevOwner = setOwner(scope);
|
|
325
|
+
const prevCollecting = collecting;
|
|
326
|
+
collecting = scope;
|
|
327
|
+
try {
|
|
328
|
+
out.set(build(value));
|
|
329
|
+
} finally {
|
|
330
|
+
collecting = prevCollecting;
|
|
331
|
+
setOwner(prevOwner);
|
|
332
|
+
}
|
|
333
|
+
disposers = scope;
|
|
334
|
+
});
|
|
335
|
+
});
|
|
336
|
+
return out;
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
/** Run `build` as a scope of its own: what it creates is ended by the returned disposers. */
|
|
340
|
+
function scoped<T>(build: () => T): [T, Array<() => void>] {
|
|
341
|
+
const scope: Array<() => void> = [];
|
|
342
|
+
const prevOwner = setOwner(scope);
|
|
343
|
+
const prevCollecting = collecting;
|
|
344
|
+
collecting = scope;
|
|
345
|
+
try {
|
|
346
|
+
return [build(), scope];
|
|
347
|
+
} finally {
|
|
348
|
+
collecting = prevCollecting;
|
|
349
|
+
setOwner(prevOwner);
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
/**
|
|
354
|
+
* A reactive slot, read once per change, in its region only — never called
|
|
355
|
+
* beforehand to see what it gives, which ran a component twice and kept the
|
|
356
|
+
* first one's effects. Its first value decides where it sits: a string, a
|
|
357
|
+
* number or `true` joins the text around it as one raw run, updated in place;
|
|
358
|
+
* anything else is built by `other`. Nothing at first (`null`, `undefined`,
|
|
359
|
+
* `false`: `${() => open() ? Panel() : null}`) is not text — what comes later
|
|
360
|
+
* is most often an element, which a text run cannot hold.
|
|
361
|
+
*/
|
|
362
|
+
function reactiveSlot(
|
|
363
|
+
slot: () => unknown,
|
|
364
|
+
other: (value: unknown) => Child[],
|
|
365
|
+
): { child: Region; text: boolean } {
|
|
366
|
+
const kind = { known: false, text: false };
|
|
367
|
+
let raw: NativeNode | null = null;
|
|
368
|
+
const child = region(slot, (value) => {
|
|
369
|
+
if (!kind.known) {
|
|
370
|
+
kind.known = true;
|
|
371
|
+
kind.text =
|
|
372
|
+
isPrimitive(value) &&
|
|
373
|
+
value !== null &&
|
|
374
|
+
value !== undefined &&
|
|
375
|
+
value !== false;
|
|
376
|
+
}
|
|
377
|
+
if (!kind.text) return other(value);
|
|
378
|
+
if (raw) raw.setProps({ text: str(value) });
|
|
379
|
+
else raw = rawText(str(value));
|
|
380
|
+
return [raw];
|
|
381
|
+
});
|
|
382
|
+
return { child, text: kind.text };
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
interface KeyedRow {
|
|
386
|
+
item: Signal<unknown>;
|
|
387
|
+
index: Signal<number>;
|
|
388
|
+
region: Region;
|
|
389
|
+
disposers: Array<() => void>;
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
/**
|
|
393
|
+
* aurora's `each`: one row per key, as on the DOM. A row whose key stays keeps
|
|
394
|
+
* its native views and its effects (it sees its new element and position
|
|
395
|
+
* through its accessors, and moves if the order changed); a new key builds one
|
|
396
|
+
* row; a key that left ends its row, and the host deletes its views.
|
|
397
|
+
*/
|
|
398
|
+
function keyedRegion(
|
|
399
|
+
list: EachList,
|
|
400
|
+
build: (value: unknown) => Child[],
|
|
401
|
+
placed?: (rows: Array<[unknown, Region]>) => void,
|
|
402
|
+
): Region {
|
|
403
|
+
if (list.lazy) return lazyRegion(list, list.lazy, build);
|
|
404
|
+
const out = new Region();
|
|
405
|
+
let rows = new Map<unknown, KeyedRow>();
|
|
406
|
+
effect(() => {
|
|
407
|
+
const items = readEachItems(list.items);
|
|
408
|
+
untrack(() => {
|
|
409
|
+
const next = new Map<unknown, KeyedRow>();
|
|
410
|
+
items.forEach((element, position) => {
|
|
411
|
+
const key = list.key(element, position);
|
|
412
|
+
if (next.has(key)) {
|
|
413
|
+
throw new OrbitError(
|
|
414
|
+
"E_ORBIT_DUPLICATE_KEY",
|
|
415
|
+
`[orbit] each(): two elements share the key ${String(key)} — a key names one row`,
|
|
416
|
+
);
|
|
417
|
+
}
|
|
418
|
+
const kept = rows.get(key);
|
|
419
|
+
if (kept) {
|
|
420
|
+
kept.item(element);
|
|
421
|
+
kept.index(position);
|
|
422
|
+
next.set(key, kept);
|
|
423
|
+
return;
|
|
424
|
+
}
|
|
425
|
+
const item = signal(element);
|
|
426
|
+
const index = signal(position);
|
|
427
|
+
const region = new Region();
|
|
428
|
+
const [, disposers] = scoped(() =>
|
|
429
|
+
region.set(build(list.run(() => list.render(item, index)))),
|
|
430
|
+
);
|
|
431
|
+
next.set(key, { item, index, region, disposers });
|
|
432
|
+
});
|
|
433
|
+
for (const [key, row] of rows) {
|
|
434
|
+
if (!next.has(key)) for (const end of row.disposers.splice(0)) end();
|
|
435
|
+
}
|
|
436
|
+
rows = next;
|
|
437
|
+
out.set([...next.values()].map((row) => row.region));
|
|
438
|
+
placed?.([...next.entries()].map(([key, row]) => [key, row.region]));
|
|
439
|
+
});
|
|
440
|
+
});
|
|
441
|
+
collecting?.push(() => {
|
|
442
|
+
for (const row of rows.values())
|
|
443
|
+
for (const end of row.disposers.splice(0)) end();
|
|
444
|
+
rows = new Map();
|
|
445
|
+
});
|
|
446
|
+
return out;
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
/** The native nodes a region holds now, in order. */
|
|
450
|
+
function nodesOf(region: Region): NativeNode[] {
|
|
451
|
+
const out: NativeNode[] = [];
|
|
452
|
+
for (const item of region.items) {
|
|
453
|
+
if (item instanceof Region) out.push(...nodesOf(item));
|
|
454
|
+
else out.push(item);
|
|
455
|
+
}
|
|
456
|
+
return out;
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
type Frame = [number, number, number, number];
|
|
460
|
+
|
|
461
|
+
function isFrame(value: unknown): value is Frame {
|
|
462
|
+
return (
|
|
463
|
+
Array.isArray(value) &&
|
|
464
|
+
value.length === 4 &&
|
|
465
|
+
value.every((n) => typeof n === "number")
|
|
466
|
+
);
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
/** The host's frames for `nodes` (dp, root coordinates), null where it has none. */
|
|
470
|
+
function framesOf(nodes: readonly NativeNode[]): Array<Frame | null> {
|
|
471
|
+
const answer: unknown = JSON.parse(
|
|
472
|
+
treeHost().layout(JSON.stringify(nodes.map((node) => node.tag))),
|
|
473
|
+
);
|
|
474
|
+
const list = Array.isArray(answer) ? answer : [];
|
|
475
|
+
return nodes.map((_, i) => {
|
|
476
|
+
const frame: unknown = list[i];
|
|
477
|
+
return isFrame(frame) ? frame : null;
|
|
478
|
+
});
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
/** A spacer's props: hidden when empty, so a gap-separated container gives it no gap. */
|
|
482
|
+
const spacerProps = (height: number) =>
|
|
483
|
+
height > 0 ? { height, display: "flex" } : { height: 0, display: "none" };
|
|
484
|
+
|
|
485
|
+
/**
|
|
486
|
+
* aurora's lazy `each` on native: only the rows intersecting the nearest
|
|
487
|
+
* scroll view's viewport, plus `overscan` dp on each side, exist — as a keyed
|
|
488
|
+
* list over that window, between two spacer views standing for the others at
|
|
489
|
+
* their measured heights (or the estimate). The window comes from aurora's
|
|
490
|
+
* own math (`LazyLayout`), as on the DOM.
|
|
491
|
+
*
|
|
492
|
+
* After each commit it reads its rows' frames from the host (top to next
|
|
493
|
+
* top, gaps included), picks the window again on each native scroll, and when
|
|
494
|
+
* a measure corrected an estimate above the viewport, scrolls the native view
|
|
495
|
+
* by the difference so what was on screen stays put. `from: "end"` starts at
|
|
496
|
+
* the bottom and follows new rows while the reader is there.
|
|
497
|
+
*/
|
|
498
|
+
function lazyRegion(
|
|
499
|
+
list: EachList,
|
|
500
|
+
lazy: LazyList,
|
|
501
|
+
build: (value: unknown) => Child[],
|
|
502
|
+
): Region {
|
|
503
|
+
const out = new Region();
|
|
504
|
+
const before = new NativeNode("RCTView", spacerProps(0), []);
|
|
505
|
+
const after = new NativeNode("RCTView", spacerProps(0), []);
|
|
506
|
+
const layout = new LazyLayout(lazy);
|
|
507
|
+
const view = signal<LazyView | null>(null);
|
|
508
|
+
const measured = signal(0);
|
|
509
|
+
let placed: Array<[unknown, Region]> = [];
|
|
510
|
+
|
|
511
|
+
const keys = memo(() => {
|
|
512
|
+
const all = readEachItems(list.items).map((item, index) =>
|
|
513
|
+
list.key(item, index),
|
|
514
|
+
);
|
|
515
|
+
if (new Set(all).size !== all.length) {
|
|
516
|
+
const duplicate = all.find((key, i) => all.indexOf(key) !== i);
|
|
517
|
+
throw new OrbitError(
|
|
518
|
+
"E_ORBIT_DUPLICATE_KEY",
|
|
519
|
+
`[orbit] each(): two elements share the key ${String(duplicate)} — a key names one row`,
|
|
520
|
+
);
|
|
521
|
+
}
|
|
522
|
+
return all;
|
|
523
|
+
});
|
|
524
|
+
const win = memo(
|
|
525
|
+
() => {
|
|
526
|
+
const all = keys();
|
|
527
|
+
measured();
|
|
528
|
+
const current = view();
|
|
529
|
+
layout.keep(all);
|
|
530
|
+
if (current === null) {
|
|
531
|
+
const [first, last] = firstWindow(all.length, lazy);
|
|
532
|
+
return layout.spacers(all, first, last);
|
|
533
|
+
}
|
|
534
|
+
return layout.window(all, current);
|
|
535
|
+
},
|
|
536
|
+
{ equals: sameWindow },
|
|
537
|
+
);
|
|
538
|
+
const start = memo(() => win().start);
|
|
539
|
+
effect(() => {
|
|
540
|
+
const w = win();
|
|
541
|
+
before.setProps(spacerProps(w.before));
|
|
542
|
+
after.setProps(spacerProps(w.after));
|
|
543
|
+
});
|
|
544
|
+
const windowList: EachList = {
|
|
545
|
+
items: () => {
|
|
546
|
+
const w = win();
|
|
547
|
+
return readEachItems(list.items).slice(w.start, w.end);
|
|
548
|
+
},
|
|
549
|
+
key: (item, index) => list.key(item, index + untrack(start)),
|
|
550
|
+
render: (item, index) =>
|
|
551
|
+
list.render(
|
|
552
|
+
item,
|
|
553
|
+
memo(() => index() + start()),
|
|
554
|
+
),
|
|
555
|
+
run: list.run,
|
|
556
|
+
};
|
|
557
|
+
const rows = keyedRegion(windowList, build, (next) => {
|
|
558
|
+
placed = next;
|
|
559
|
+
});
|
|
560
|
+
out.set([before, rows, after]);
|
|
561
|
+
|
|
562
|
+
afterCommit(() => {
|
|
563
|
+
let scroll: NativeNode | null = before.parent;
|
|
564
|
+
while (scroll !== null && scroll.viewName !== "RCTScrollView")
|
|
565
|
+
scroll = scroll.parent;
|
|
566
|
+
const content = scroll?.flatChildren()[0];
|
|
567
|
+
if (!scroll || !content) return;
|
|
568
|
+
const scroller = scroll;
|
|
569
|
+
let offset = 0;
|
|
570
|
+
let pinned = lazy.from === "end";
|
|
571
|
+
/** The row to keep in place across the next commit, and where it was. */
|
|
572
|
+
let anchor: { key: unknown; top: number } | null = null;
|
|
573
|
+
|
|
574
|
+
/** Where each shown row starts (dp, root coordinates), and where the last ends. */
|
|
575
|
+
const rowTops = () => {
|
|
576
|
+
const heads = placed.map(([, region]) => nodesOf(region)[0] ?? null);
|
|
577
|
+
const lastRow = placed.at(-1);
|
|
578
|
+
const tail = lastRow ? (nodesOf(lastRow[1]).at(-1) ?? null) : null;
|
|
579
|
+
const asked = [scroller, content, before, after, tail, ...heads].filter(
|
|
580
|
+
(node): node is NativeNode => node !== null,
|
|
581
|
+
);
|
|
582
|
+
const frames = new Map<NativeNode, Frame>();
|
|
583
|
+
framesOf(asked).forEach((frame, i) => {
|
|
584
|
+
const node = asked[i];
|
|
585
|
+
if (frame && node) frames.set(node, frame);
|
|
586
|
+
});
|
|
587
|
+
const tops: Array<[unknown, number]> = [];
|
|
588
|
+
placed.forEach(([key], i) => {
|
|
589
|
+
const head = heads[i];
|
|
590
|
+
const frame = head ? frames.get(head) : undefined;
|
|
591
|
+
if (frame) tops.push([key, frame[1]]);
|
|
592
|
+
});
|
|
593
|
+
const afterFrame = frames.get(after);
|
|
594
|
+
const tailFrame = tail ? frames.get(tail) : undefined;
|
|
595
|
+
const bottom =
|
|
596
|
+
after.props.display !== "none" && afterFrame
|
|
597
|
+
? afterFrame[1]
|
|
598
|
+
: tailFrame
|
|
599
|
+
? tailFrame[1] + tailFrame[3]
|
|
600
|
+
: Number.NaN;
|
|
601
|
+
const beforeFrame = frames.get(before);
|
|
602
|
+
const listTop =
|
|
603
|
+
before.props.display !== "none" && beforeFrame
|
|
604
|
+
? beforeFrame[1]
|
|
605
|
+
: (tops[0]?.[1] ?? afterFrame?.[1] ?? 0);
|
|
606
|
+
return {
|
|
607
|
+
tops,
|
|
608
|
+
bottom,
|
|
609
|
+
listTop,
|
|
610
|
+
port: frames.get(scroller),
|
|
611
|
+
inner: frames.get(content),
|
|
612
|
+
};
|
|
613
|
+
};
|
|
614
|
+
|
|
615
|
+
const refresh = () => {
|
|
616
|
+
const { tops, bottom, listTop, port, inner } = rowTops();
|
|
617
|
+
if (!port || !inner) return;
|
|
618
|
+
let changed = false;
|
|
619
|
+
tops.forEach(([key, top], i) => {
|
|
620
|
+
const next = tops[i + 1]?.[1] ?? bottom;
|
|
621
|
+
if (Number.isFinite(next) && layout.measure(key, next - top))
|
|
622
|
+
changed = true;
|
|
623
|
+
});
|
|
624
|
+
// The viewport's top, in root coordinates of the unscrolled layout.
|
|
625
|
+
const top = inner[1] + offset;
|
|
626
|
+
const height = port[3];
|
|
627
|
+
const first = tops.find(
|
|
628
|
+
([, rowTop], i) =>
|
|
629
|
+
(tops[i + 1]?.[1] ?? bottom) > top && rowTop <= top + height,
|
|
630
|
+
);
|
|
631
|
+
anchor = pinned || !first ? null : { key: first[0], top: first[1] };
|
|
632
|
+
batch(() => {
|
|
633
|
+
if (changed) measured((n) => n + 1);
|
|
634
|
+
view(
|
|
635
|
+
pinned
|
|
636
|
+
? { kind: "end", height }
|
|
637
|
+
: { kind: "span", from: top - listTop, to: top - listTop + height },
|
|
638
|
+
);
|
|
639
|
+
});
|
|
640
|
+
};
|
|
641
|
+
|
|
642
|
+
// After a commit: hold what was on screen, follow the end, measure anew.
|
|
643
|
+
const stop = onEveryCommit(() => {
|
|
644
|
+
const held = anchor;
|
|
645
|
+
anchor = null;
|
|
646
|
+
if (pinned) {
|
|
647
|
+
treeHost().scroll(scroller.tag, 0, true);
|
|
648
|
+
} else if (held) {
|
|
649
|
+
const now = rowTops().tops.find(([key]) => Object.is(key, held.key));
|
|
650
|
+
if (now && Math.abs(now[1] - held.top) > 0.5)
|
|
651
|
+
treeHost().scroll(scroller.tag, now[1] - held.top, false);
|
|
652
|
+
}
|
|
653
|
+
refresh();
|
|
654
|
+
});
|
|
655
|
+
collecting?.push(stop);
|
|
656
|
+
// A scroll reports every frame, and the JS thread may be busier than
|
|
657
|
+
// that: each event only records the offset, and the window is picked
|
|
658
|
+
// once for all of them. Refreshing per event queued them faster than
|
|
659
|
+
// they were handled, and the window fell further behind the finger.
|
|
660
|
+
let scheduled = false;
|
|
661
|
+
const nextFrame: (run: () => void) => void =
|
|
662
|
+
typeof requestAnimationFrame === "function"
|
|
663
|
+
? (run) => {
|
|
664
|
+
requestAnimationFrame(run);
|
|
665
|
+
}
|
|
666
|
+
: (run) => {
|
|
667
|
+
setTimeout(run, 0);
|
|
668
|
+
};
|
|
669
|
+
const onScroll = (y: number) => {
|
|
670
|
+
offset = y;
|
|
671
|
+
if (scheduled) return;
|
|
672
|
+
scheduled = true;
|
|
673
|
+
nextFrame(() => {
|
|
674
|
+
scheduled = false;
|
|
675
|
+
if (lazy.from === "end") {
|
|
676
|
+
const { port, inner } = rowTops();
|
|
677
|
+
if (port && inner) pinned = offset + port[3] >= inner[3] - 2;
|
|
678
|
+
}
|
|
679
|
+
refresh();
|
|
680
|
+
});
|
|
681
|
+
};
|
|
682
|
+
scroller.scrollListeners.push(onScroll);
|
|
683
|
+
collecting?.push(() => {
|
|
684
|
+
const at = scroller.scrollListeners.indexOf(onScroll);
|
|
685
|
+
if (at >= 0) scroller.scrollListeners.splice(at, 1);
|
|
686
|
+
});
|
|
687
|
+
refresh();
|
|
688
|
+
if (pinned) treeHost().scroll(scroller.tag, 0, true);
|
|
689
|
+
});
|
|
690
|
+
return out;
|
|
691
|
+
}
|
|
692
|
+
|
|
693
|
+
// ─── Instantiation ───────────────────────────────────────────────
|
|
694
|
+
|
|
695
|
+
/** What a gesture handler receives: the shape a DOM handler reads, plus the gesture's numbers. */
|
|
696
|
+
export type GestureEvent = { type: string } & { [key: string]: unknown };
|
|
697
|
+
type GestureHandler = (event: GestureEvent) => void;
|
|
698
|
+
|
|
699
|
+
type GestureName = "@click" | "@longpress" | "@pan" | "@swipe";
|
|
700
|
+
|
|
701
|
+
/** The gestures a template can listen to, and the flag orbit-host recognizes each by. */
|
|
702
|
+
const GESTURES: Record<
|
|
703
|
+
GestureName,
|
|
704
|
+
{ flag: number; native: string; type: string }
|
|
705
|
+
> = {
|
|
706
|
+
"@click": { flag: 1, native: "topPress", type: "click" },
|
|
707
|
+
"@longpress": { flag: 2, native: "topLongPress", type: "longpress" },
|
|
708
|
+
"@pan": { flag: 4, native: "topPan", type: "pan" },
|
|
709
|
+
"@swipe": { flag: 8, native: "topSwipe", type: "swipe" },
|
|
710
|
+
};
|
|
711
|
+
|
|
712
|
+
const isGestureName = (name: string): name is GestureName =>
|
|
713
|
+
Object.hasOwn(GESTURES, name);
|
|
714
|
+
|
|
715
|
+
type Attrs = {
|
|
716
|
+
className: string | (() => string);
|
|
717
|
+
gestures: Map<GestureName, GestureHandler>;
|
|
718
|
+
motion: MotionStyle | null;
|
|
719
|
+
scrollY: MotionValue | null;
|
|
720
|
+
/** The others (`data-*`, `aria-*`, `disabled`…), as written: what styles may test. */
|
|
721
|
+
attributes: Map<string, unknown>;
|
|
722
|
+
};
|
|
723
|
+
|
|
724
|
+
function readAttrs(el: TElement, values: readonly unknown[]): Attrs {
|
|
725
|
+
const attrs: Attrs = {
|
|
726
|
+
className: "",
|
|
727
|
+
gestures: new Map(),
|
|
728
|
+
motion: null,
|
|
729
|
+
scrollY: null,
|
|
730
|
+
attributes: new Map(),
|
|
731
|
+
};
|
|
732
|
+
let href: TAttr | undefined;
|
|
733
|
+
for (const attr of el.attrs) {
|
|
734
|
+
if (attr.name === "href") href = attr;
|
|
735
|
+
if (attr.name === "class") {
|
|
736
|
+
const reactive = attr.parts.some(
|
|
737
|
+
(p) => typeof p === "number" && isGetter(values[p]),
|
|
738
|
+
);
|
|
739
|
+
const compute = () =>
|
|
740
|
+
attr.parts
|
|
741
|
+
.map((p) => {
|
|
742
|
+
if (typeof p === "string") return p;
|
|
743
|
+
const v = values[p];
|
|
744
|
+
return str(isGetter(v) ? v() : v);
|
|
745
|
+
})
|
|
746
|
+
.join("");
|
|
747
|
+
attrs.className = reactive ? compute : compute();
|
|
748
|
+
} else if (isGestureName(attr.name)) {
|
|
749
|
+
const slot = attr.parts[0];
|
|
750
|
+
const handler = typeof slot === "number" ? values[slot] : undefined;
|
|
751
|
+
if (typeof handler === "function")
|
|
752
|
+
attrs.gestures.set(attr.name, (event) => void handler(event));
|
|
753
|
+
} else if (attr.name === "motion" || attr.name === "scroll-y") {
|
|
754
|
+
const slot = attr.parts[0];
|
|
755
|
+
const value = typeof slot === "number" ? values[slot] : undefined;
|
|
756
|
+
if (attr.name === "motion") attrs.motion = readMotion(value);
|
|
757
|
+
else if (value instanceof MotionValue) attrs.scrollY = value;
|
|
758
|
+
else
|
|
759
|
+
throw new OrbitError(
|
|
760
|
+
"E_ORBIT_INVALID_SCROLL_BINDING",
|
|
761
|
+
"[orbit] scroll-y expects a motion value",
|
|
762
|
+
);
|
|
763
|
+
} else if (attr.name.startsWith("?")) {
|
|
764
|
+
// aurora's boolean attribute: present while its value is truthy.
|
|
765
|
+
attrs.attributes.set(attr.name.slice(1), attrValue(attr, values));
|
|
766
|
+
} else if (!attr.name.startsWith("@") && !attr.name.startsWith(".")) {
|
|
767
|
+
// data-*, aria-*, disabled, role…: what the style table's variants test.
|
|
768
|
+
attrs.attributes.set(attr.name, attrValue(attr, values));
|
|
769
|
+
}
|
|
770
|
+
}
|
|
771
|
+
// A link follows its href when pressed, unless the template handles the press.
|
|
772
|
+
const follow = followLink;
|
|
773
|
+
if (
|
|
774
|
+
el.tag === "a" &&
|
|
775
|
+
href !== undefined &&
|
|
776
|
+
follow !== undefined &&
|
|
777
|
+
!attrs.gestures.has("@click")
|
|
778
|
+
) {
|
|
779
|
+
const target = href;
|
|
780
|
+
attrs.gestures.set("@click", () => {
|
|
781
|
+
const value = attrValue(target, values);
|
|
782
|
+
const url = str(isGetter(value) ? value() : value);
|
|
783
|
+
if (url !== "") follow(url);
|
|
784
|
+
});
|
|
785
|
+
}
|
|
786
|
+
return attrs;
|
|
787
|
+
}
|
|
788
|
+
|
|
789
|
+
// ─── Styles ──────────────────────────────────────────────────────
|
|
790
|
+
|
|
791
|
+
/** Where an element is built: what it inherits, and who is around it. */
|
|
792
|
+
interface Ctx {
|
|
793
|
+
/** The text style it inherits (reactive: a parent's state may change it). */
|
|
794
|
+
text: () => Style;
|
|
795
|
+
parent: StyledElement | null;
|
|
796
|
+
/** The parent lays its children out as flex items: they shrink, as in CSS. */
|
|
797
|
+
flexParent: boolean;
|
|
798
|
+
/** The element built just before in the same parent (`peer-*`). */
|
|
799
|
+
siblings: { last: StyledElement | null };
|
|
800
|
+
}
|
|
801
|
+
|
|
802
|
+
/** An attribute's value for a style condition: null when absent. */
|
|
803
|
+
function attributeOf(attrs: Attrs, name: string): string | null {
|
|
804
|
+
const raw = attrs.attributes.get(name);
|
|
805
|
+
const v = isGetter(raw) ? raw() : raw;
|
|
806
|
+
if (v === null || v === undefined || v === false) return null;
|
|
807
|
+
return v === true ? "" : str(v);
|
|
808
|
+
}
|
|
809
|
+
|
|
810
|
+
const splitClasses = (classes: string) => classes.split(/\s+/).filter(Boolean);
|
|
811
|
+
|
|
812
|
+
interface Styled {
|
|
813
|
+
el: StyledElement;
|
|
814
|
+
computed: () => Computed;
|
|
815
|
+
}
|
|
816
|
+
|
|
817
|
+
/** An element as the style table sees it, and its computed style (reactive). */
|
|
818
|
+
function styled(tag: string, attrs: Attrs, ctx: Ctx, root = false): Styled {
|
|
819
|
+
const { className } = attrs;
|
|
820
|
+
let classes: () => readonly string[];
|
|
821
|
+
if (typeof className === "string") {
|
|
822
|
+
const list = splitClasses(className);
|
|
823
|
+
classes = () => list;
|
|
824
|
+
} else {
|
|
825
|
+
classes = memo(() => splitClasses(className()));
|
|
826
|
+
}
|
|
827
|
+
const holder: { computed: (() => Computed) | null } = { computed: null };
|
|
828
|
+
const el: StyledElement = {
|
|
829
|
+
tag,
|
|
830
|
+
root,
|
|
831
|
+
classes,
|
|
832
|
+
attribute: (name) => attributeOf(attrs, name),
|
|
833
|
+
parent: ctx.parent,
|
|
834
|
+
previous: ctx.siblings.last,
|
|
835
|
+
get vars() {
|
|
836
|
+
return holder.computed?.().vars;
|
|
837
|
+
},
|
|
838
|
+
};
|
|
839
|
+
ctx.siblings.last = el;
|
|
840
|
+
const computed = memo(() => computeStyle(el, ctx.text()));
|
|
841
|
+
holder.computed = computed;
|
|
842
|
+
return { el, computed };
|
|
843
|
+
}
|
|
844
|
+
|
|
845
|
+
/** A view's box: its computed box, and CSS's flex-shrink in a flex container. */
|
|
846
|
+
function boxOf(c: Computed, ctx: Ctx): Style {
|
|
847
|
+
return ctx.flexParent && c.box.flexShrink === undefined
|
|
848
|
+
? { flexShrink: 1, ...c.box }
|
|
849
|
+
: c.box;
|
|
850
|
+
}
|
|
851
|
+
|
|
852
|
+
/** Text props: a unitless line height (`1.5x`) becomes px against the font size. */
|
|
853
|
+
function textProps(style: Style): Style {
|
|
854
|
+
const lh = style.lineHeight;
|
|
855
|
+
if (typeof lh !== "string" || !lh.endsWith("x")) return style;
|
|
856
|
+
const size = typeof style.fontSize === "number" ? style.fontSize : 16;
|
|
857
|
+
return { ...style, lineHeight: Number(lh.slice(0, -1)) * size };
|
|
858
|
+
}
|
|
859
|
+
|
|
860
|
+
/** The children's context: what this element hands down. */
|
|
861
|
+
function childCtx(s: Styled, ctx: Ctx, own: Style = {}): Ctx {
|
|
862
|
+
return {
|
|
863
|
+
text: memo(() => ({ ...ctx.text(), ...own, ...s.computed().text })),
|
|
864
|
+
parent: s.el,
|
|
865
|
+
flexParent: untrack(s.computed).flex,
|
|
866
|
+
siblings: { last: null },
|
|
867
|
+
};
|
|
868
|
+
}
|
|
869
|
+
|
|
870
|
+
/** Keep `node`'s style props equal to `compute()`, patching what changed; returns the first. */
|
|
871
|
+
function bindStyle(node: NativeNode, compute: () => Style): Style {
|
|
872
|
+
let previous: Props = {};
|
|
873
|
+
let initial: Style = {};
|
|
874
|
+
let first = true;
|
|
875
|
+
effect(() => {
|
|
876
|
+
const next = compute();
|
|
877
|
+
const flat = flatten(next);
|
|
878
|
+
if (first) {
|
|
879
|
+
initial = next;
|
|
880
|
+
first = false;
|
|
881
|
+
} else {
|
|
882
|
+
const patch: Props = {};
|
|
883
|
+
for (const [key, value] of Object.entries(flat))
|
|
884
|
+
if (previous[key] !== value) patch[key] = value;
|
|
885
|
+
for (const key of Object.keys(previous))
|
|
886
|
+
if (!(key in flat)) patch[key] = null;
|
|
887
|
+
if (Object.keys(patch).length > 0) node.setProps(patch);
|
|
888
|
+
}
|
|
889
|
+
previous = flat;
|
|
890
|
+
});
|
|
891
|
+
return initial;
|
|
892
|
+
}
|
|
893
|
+
|
|
894
|
+
/** A text node holding a run, styled by what it inherits. */
|
|
895
|
+
function textRun(ctx: Ctx, run: Child[]): NativeNode {
|
|
896
|
+
const node = new NativeNode("RCTText", {}, run);
|
|
897
|
+
node.props = flatten(bindStyle(node, () => textProps(ctx.text())));
|
|
898
|
+
return node;
|
|
899
|
+
}
|
|
900
|
+
|
|
901
|
+
// ─── Gestures ────────────────────────────────────────────────────
|
|
902
|
+
|
|
903
|
+
function gestureData(payload: unknown): { [key: string]: unknown } {
|
|
904
|
+
const text: unknown =
|
|
905
|
+
typeof payload === "object" && payload !== null
|
|
906
|
+
? Reflect.get(payload, "text")
|
|
907
|
+
: undefined;
|
|
908
|
+
if (typeof text !== "string" || text === "") return {};
|
|
909
|
+
try {
|
|
910
|
+
const data: unknown = JSON.parse(text);
|
|
911
|
+
return typeof data === "object" && data !== null && !Array.isArray(data)
|
|
912
|
+
? Object.fromEntries(Object.entries(data))
|
|
913
|
+
: {};
|
|
914
|
+
} catch {
|
|
915
|
+
return {};
|
|
916
|
+
}
|
|
917
|
+
}
|
|
918
|
+
|
|
919
|
+
/**
|
|
920
|
+
* `@click`, `@longpress`, `@pan` and `@swipe`, recognized natively: the node
|
|
921
|
+
* tells orbit-host which it listens to (`gestures`, a set of flags), the host
|
|
922
|
+
* recognizes them on the UI thread — the press feedback starts there, even
|
|
923
|
+
* with this thread busy — and delivers the outcome here. A click fires on
|
|
924
|
+
* release if the finger stayed put and nothing took the touch over (a scroll
|
|
925
|
+
* view, a long press); a pan reports `phase`, `dx`, `dy`, `vx`, `vy` (dp,
|
|
926
|
+
* dp/s); a swipe is a pan that ended fast, with its `direction`.
|
|
927
|
+
*/
|
|
928
|
+
function withEvents(node: NativeNode, attrs: Attrs): NativeNode {
|
|
929
|
+
const { gestures } = attrs;
|
|
930
|
+
if (gestures.size === 0) return node;
|
|
931
|
+
let flags = 0;
|
|
932
|
+
const byNative = new Map<string, [string, GestureHandler]>();
|
|
933
|
+
for (const [name, handler] of gestures) {
|
|
934
|
+
const gesture = GESTURES[name];
|
|
935
|
+
flags |= gesture.flag;
|
|
936
|
+
byNative.set(gesture.native, [gesture.type, handler]);
|
|
937
|
+
}
|
|
938
|
+
node.props.gestures = flags;
|
|
939
|
+
node.onEvent = (type, payload) => {
|
|
940
|
+
const entry = byNative.get(type);
|
|
941
|
+
if (!entry) return;
|
|
942
|
+
const [eventType, handler] = entry;
|
|
943
|
+
handler({ ...gestureData(payload), type: eventType });
|
|
944
|
+
};
|
|
945
|
+
return node;
|
|
946
|
+
}
|
|
947
|
+
|
|
948
|
+
function withMotion(node: NativeNode, attrs: Attrs): NativeNode {
|
|
949
|
+
const { motion } = attrs;
|
|
950
|
+
if (motion) afterCommit(() => bindMotion(node.tag, motion));
|
|
951
|
+
return node;
|
|
952
|
+
}
|
|
953
|
+
|
|
954
|
+
// A scrolling element: RCTScrollView (the viewport) around a content view
|
|
955
|
+
// that carries the element's padding, gap and flow.
|
|
956
|
+
function scrollElement(
|
|
957
|
+
el: TElement,
|
|
958
|
+
values: readonly unknown[],
|
|
959
|
+
ctx: Ctx,
|
|
960
|
+
attrs: Attrs,
|
|
961
|
+
s: Styled,
|
|
962
|
+
): NativeNode {
|
|
963
|
+
const split = (c: Computed) => {
|
|
964
|
+
const outer: Style = {};
|
|
965
|
+
const content: Style = {};
|
|
966
|
+
for (const [key, value] of Object.entries(boxOf(c, ctx)))
|
|
967
|
+
(CONTENT_KEYS.has(key) ? content : outer)[key] = value;
|
|
968
|
+
if (c.flex && content.flexDirection === undefined)
|
|
969
|
+
content.flexDirection = "row";
|
|
970
|
+
return { outer, content };
|
|
971
|
+
};
|
|
972
|
+
const container = new NativeNode(
|
|
973
|
+
"RCTView",
|
|
974
|
+
{},
|
|
975
|
+
viewChildren(
|
|
976
|
+
el.children,
|
|
977
|
+
values,
|
|
978
|
+
childCtx(s, ctx),
|
|
979
|
+
untrack(s.computed).flex,
|
|
980
|
+
),
|
|
981
|
+
);
|
|
982
|
+
container.props = flatten(
|
|
983
|
+
bindStyle(container, () => split(s.computed()).content),
|
|
984
|
+
);
|
|
985
|
+
const node = new NativeNode("RCTScrollView", {}, [container]);
|
|
986
|
+
node.props = flatten(bindStyle(node, () => split(s.computed()).outer));
|
|
987
|
+
const { scrollY } = attrs;
|
|
988
|
+
if (scrollY) afterCommit(() => bindScrollY(node.tag, scrollY));
|
|
989
|
+
return withMotion(node, attrs);
|
|
990
|
+
}
|
|
991
|
+
|
|
992
|
+
// On the web a scrolling element's padding, gap and flow lay out its content;
|
|
993
|
+
// on native those belong to the scroll view's content container.
|
|
994
|
+
const CONTENT_KEYS = new Set([
|
|
995
|
+
"padding",
|
|
996
|
+
"paddingHorizontal",
|
|
997
|
+
"paddingVertical",
|
|
998
|
+
"paddingTop",
|
|
999
|
+
"paddingBottom",
|
|
1000
|
+
"paddingLeft",
|
|
1001
|
+
"paddingRight",
|
|
1002
|
+
"gap",
|
|
1003
|
+
"rowGap",
|
|
1004
|
+
"columnGap",
|
|
1005
|
+
"flexDirection",
|
|
1006
|
+
"flexWrap",
|
|
1007
|
+
"alignItems",
|
|
1008
|
+
"justifyContent",
|
|
1009
|
+
]);
|
|
1010
|
+
|
|
1011
|
+
// ─── Text fields ─────────────────────────────────────────────────
|
|
1012
|
+
|
|
1013
|
+
/** An attribute's value: its single slot's value, or its parts joined. */
|
|
1014
|
+
function attrValue(attr: TAttr, values: readonly unknown[]): unknown {
|
|
1015
|
+
const [only] = attr.parts;
|
|
1016
|
+
if (attr.parts.length === 1 && typeof only === "number") return values[only];
|
|
1017
|
+
return attr.parts
|
|
1018
|
+
.map((p) => (typeof p === "string" ? p : str(values[p])))
|
|
1019
|
+
.join("");
|
|
1020
|
+
}
|
|
1021
|
+
|
|
1022
|
+
const KEYBOARDS: { [type: string]: string } = {
|
|
1023
|
+
email: "email",
|
|
1024
|
+
number: "numeric",
|
|
1025
|
+
tel: "phone",
|
|
1026
|
+
url: "url",
|
|
1027
|
+
search: "search",
|
|
1028
|
+
numeric: "numeric",
|
|
1029
|
+
decimal: "decimal",
|
|
1030
|
+
};
|
|
1031
|
+
|
|
1032
|
+
// What CSS gives a placeholder by default (slate-400 in nebula's theme).
|
|
1033
|
+
const PLACEHOLDER = "#94a3b8";
|
|
1034
|
+
|
|
1035
|
+
/** The event a field handler receives: the shape DOM handlers read. */
|
|
1036
|
+
function fieldEvent(type: string, value: string, key?: string) {
|
|
1037
|
+
const target = { value };
|
|
1038
|
+
return { type, key, target, currentTarget: target };
|
|
1039
|
+
}
|
|
1040
|
+
|
|
1041
|
+
function call(handler: unknown, event: unknown): void {
|
|
1042
|
+
if (typeof handler === "function") handler(event);
|
|
1043
|
+
}
|
|
1044
|
+
|
|
1045
|
+
/**
|
|
1046
|
+
* `<input>` / `<textarea>`: a native text field. The field owns its text while
|
|
1047
|
+
* the user types — nothing round-trips through JS before a character shows —
|
|
1048
|
+
* and reports each change; a bound value is sent back only when it differs
|
|
1049
|
+
* from what the field last reported, so the caret never jumps.
|
|
1050
|
+
*/
|
|
1051
|
+
function inputElement(
|
|
1052
|
+
el: TElement,
|
|
1053
|
+
values: readonly unknown[],
|
|
1054
|
+
ctx: Ctx,
|
|
1055
|
+
attrs: Attrs,
|
|
1056
|
+
s: Styled,
|
|
1057
|
+
): NativeNode {
|
|
1058
|
+
const raw = new Map<string, unknown>();
|
|
1059
|
+
for (const attr of el.attrs) raw.set(attr.name, attrValue(attr, values));
|
|
1060
|
+
const type = str(raw.get("type") ?? "text").toLowerCase();
|
|
1061
|
+
const multiline = el.tag === "textarea";
|
|
1062
|
+
const rowsAttr = Number(str(raw.get("rows")));
|
|
1063
|
+
const maxLength = Number(str(raw.get("maxlength")));
|
|
1064
|
+
const inputmode = str(raw.get("inputmode")).toLowerCase();
|
|
1065
|
+
const node = new NativeNode("OrbitTextInput", {}, []);
|
|
1066
|
+
const style = bindStyle(node, () => {
|
|
1067
|
+
const c = s.computed();
|
|
1068
|
+
return textProps({
|
|
1069
|
+
placeholderColor: c.placeholder ?? PLACEHOLDER,
|
|
1070
|
+
...ctx.text(),
|
|
1071
|
+
...c.text,
|
|
1072
|
+
...boxOf(c, ctx),
|
|
1073
|
+
});
|
|
1074
|
+
});
|
|
1075
|
+
const bound = raw.get(".value") ?? raw.get("value");
|
|
1076
|
+
let lastNative = isGetter(bound) ? str(untrack(bound)) : str(bound);
|
|
1077
|
+
let version = 0;
|
|
1078
|
+
node.props = {
|
|
1079
|
+
...flatten(style),
|
|
1080
|
+
placeholder: str(raw.get("placeholder")),
|
|
1081
|
+
multiline,
|
|
1082
|
+
rows:
|
|
1083
|
+
Number.isFinite(rowsAttr) && rowsAttr > 0 ? rowsAttr : multiline ? 2 : 1,
|
|
1084
|
+
secure: type === "password",
|
|
1085
|
+
keyboard: KEYBOARDS[inputmode] ?? KEYBOARDS[type] ?? "text",
|
|
1086
|
+
maxLength: Number.isFinite(maxLength) && maxLength > 0 ? maxLength : 0,
|
|
1087
|
+
editable: !raw.has("disabled") || raw.get("disabled") === false,
|
|
1088
|
+
autocomplete: str(raw.get("autocomplete")),
|
|
1089
|
+
text: lastNative,
|
|
1090
|
+
textVersion: version,
|
|
1091
|
+
};
|
|
1092
|
+
if (isGetter(bound)) {
|
|
1093
|
+
let first = true;
|
|
1094
|
+
effect(() => {
|
|
1095
|
+
const next = str(bound());
|
|
1096
|
+
if (first) {
|
|
1097
|
+
first = false;
|
|
1098
|
+
return;
|
|
1099
|
+
}
|
|
1100
|
+
if (next === lastNative) return;
|
|
1101
|
+
lastNative = next;
|
|
1102
|
+
version += 1;
|
|
1103
|
+
// A field sized by its content is measured on what it now holds:
|
|
1104
|
+
// cleared after a send, it shrinks back.
|
|
1105
|
+
node.setProps(
|
|
1106
|
+
node.props.fieldSizing === "content"
|
|
1107
|
+
? { text: next, textVersion: version, measureText: next }
|
|
1108
|
+
: { text: next, textVersion: version },
|
|
1109
|
+
);
|
|
1110
|
+
});
|
|
1111
|
+
}
|
|
1112
|
+
let atFocus = lastNative;
|
|
1113
|
+
node.onEvent = (eventType, payload) => {
|
|
1114
|
+
const text: unknown =
|
|
1115
|
+
typeof payload === "object" && payload !== null
|
|
1116
|
+
? Reflect.get(payload, "text")
|
|
1117
|
+
: undefined;
|
|
1118
|
+
const value = typeof text === "string" ? text : lastNative;
|
|
1119
|
+
if (eventType === "topChange") {
|
|
1120
|
+
lastNative = value;
|
|
1121
|
+
// A field sized by its content grows with its lines: the host
|
|
1122
|
+
// measures this text (layout only — the field already shows it).
|
|
1123
|
+
if (node.props.fieldSizing === "content")
|
|
1124
|
+
node.setProps({ measureText: value });
|
|
1125
|
+
call(raw.get("@input"), fieldEvent("input", value));
|
|
1126
|
+
} else if (eventType === "topFocus") {
|
|
1127
|
+
atFocus = value;
|
|
1128
|
+
call(raw.get("@focus"), fieldEvent("focus", value));
|
|
1129
|
+
} else if (eventType === "topBlur") {
|
|
1130
|
+
if (value !== atFocus)
|
|
1131
|
+
call(raw.get("@change"), fieldEvent("change", value));
|
|
1132
|
+
call(raw.get("@blur"), fieldEvent("blur", value));
|
|
1133
|
+
} else if (eventType === "topSubmit") {
|
|
1134
|
+
call(raw.get("@keydown"), fieldEvent("keydown", value, "Enter"));
|
|
1135
|
+
}
|
|
1136
|
+
};
|
|
1137
|
+
return withMotion(node, attrs);
|
|
1138
|
+
}
|
|
1139
|
+
|
|
1140
|
+
// ─── Images ──────────────────────────────────────────────────────
|
|
1141
|
+
|
|
1142
|
+
// Bytes handed to an image are numbered so that new bytes reload the view
|
|
1143
|
+
// even under the same node.
|
|
1144
|
+
let bytesVersion = 0;
|
|
1145
|
+
|
|
1146
|
+
const SCHEME = /^([a-z][a-z0-9+.-]*):/i;
|
|
1147
|
+
|
|
1148
|
+
/**
|
|
1149
|
+
* Where `src` points, in the form the host loads: `https://…`, `file:<path>`,
|
|
1150
|
+
* `content://…` (what Android's pickers return), `asset:<name>` for a file
|
|
1151
|
+
* bundled with the app, or `bytes:<n>` for bytes the app hands over (they are
|
|
1152
|
+
* sent to the host's memory at the next commit, never written to disk).
|
|
1153
|
+
*/
|
|
1154
|
+
function imageSource(node: NativeNode, value: unknown): string {
|
|
1155
|
+
if (value instanceof Uint8Array || value instanceof ArrayBuffer) {
|
|
1156
|
+
node.pendingBytes =
|
|
1157
|
+
value instanceof Uint8Array ? value : new Uint8Array(value);
|
|
1158
|
+
bytesVersion += 1;
|
|
1159
|
+
return `bytes:${bytesVersion}`;
|
|
1160
|
+
}
|
|
1161
|
+
node.pendingBytes = null;
|
|
1162
|
+
const src = str(value).trim();
|
|
1163
|
+
if (src === "") return "";
|
|
1164
|
+
const scheme = SCHEME.exec(src)?.[1]?.toLowerCase();
|
|
1165
|
+
if (scheme === undefined) {
|
|
1166
|
+
return src.startsWith("/")
|
|
1167
|
+
? `file:${src}`
|
|
1168
|
+
: `asset:${src.replace(/^\.\//, "")}`;
|
|
1169
|
+
}
|
|
1170
|
+
if (scheme === "https" || scheme === "content") return src;
|
|
1171
|
+
if (scheme === "file") {
|
|
1172
|
+
// file:///data/x.jpg and file:/data/x.jpg name the same path.
|
|
1173
|
+
const path = src.slice(5).replace(/^\/\/(?=\/)/, "");
|
|
1174
|
+
try {
|
|
1175
|
+
return `file:${decodeURIComponent(path)}`;
|
|
1176
|
+
} catch {
|
|
1177
|
+
throw new OrbitError(
|
|
1178
|
+
"E_ORBIT_IMAGE_SOURCE",
|
|
1179
|
+
`[orbit] <img src="${src}">: not a valid file URL`,
|
|
1180
|
+
);
|
|
1181
|
+
}
|
|
1182
|
+
}
|
|
1183
|
+
if (scheme === "http")
|
|
1184
|
+
throw new OrbitError(
|
|
1185
|
+
"E_ORBIT_INSECURE_IMAGE_URL",
|
|
1186
|
+
`[orbit] <img src="${src}">: an image is fetched over https only`,
|
|
1187
|
+
);
|
|
1188
|
+
throw new OrbitError(
|
|
1189
|
+
"E_ORBIT_IMAGE_SOURCE",
|
|
1190
|
+
`[orbit] <img src="${src}">: ${scheme}: is not an image source (https:, file:, content:, a bundled asset or bytes)`,
|
|
1191
|
+
);
|
|
1192
|
+
}
|
|
1193
|
+
|
|
1194
|
+
/**
|
|
1195
|
+
* `<img>`: an image the host decodes off the UI thread, at the size its box
|
|
1196
|
+
* is laid out to, and keeps in a bounded memory cache. Sized by its classes
|
|
1197
|
+
* (`w-*`, `h-*`, `size-*`, `aspect-*`): the box does not wait for the image
|
|
1198
|
+
* to learn its size. `object-*` fits it, `rounded-*` clips it, `alt` is what
|
|
1199
|
+
* the screen reader says.
|
|
1200
|
+
*/
|
|
1201
|
+
function imgElement(
|
|
1202
|
+
el: TElement,
|
|
1203
|
+
values: readonly unknown[],
|
|
1204
|
+
attrs: Attrs,
|
|
1205
|
+
s: Styled,
|
|
1206
|
+
ctx: Ctx,
|
|
1207
|
+
): NativeNode {
|
|
1208
|
+
const raw = new Map<string, unknown>();
|
|
1209
|
+
for (const attr of el.attrs) raw.set(attr.name, attrValue(attr, values));
|
|
1210
|
+
const node = new NativeNode("OrbitImage", {}, []);
|
|
1211
|
+
const style = bindStyle(node, () => boxOf(s.computed(), ctx));
|
|
1212
|
+
const src = raw.get("src");
|
|
1213
|
+
const initial = isGetter(src) ? untrack(src) : src;
|
|
1214
|
+
node.props = {
|
|
1215
|
+
...flatten(style),
|
|
1216
|
+
src: imageSource(node, initial),
|
|
1217
|
+
alt: str(raw.get("alt")),
|
|
1218
|
+
};
|
|
1219
|
+
if (isGetter(src)) {
|
|
1220
|
+
let first = true;
|
|
1221
|
+
effect(() => {
|
|
1222
|
+
const next = src();
|
|
1223
|
+
if (first) {
|
|
1224
|
+
first = false;
|
|
1225
|
+
return;
|
|
1226
|
+
}
|
|
1227
|
+
node.setProps({ src: imageSource(node, next) });
|
|
1228
|
+
});
|
|
1229
|
+
}
|
|
1230
|
+
return withMotion(withEvents(node, attrs), attrs);
|
|
1231
|
+
}
|
|
1232
|
+
|
|
1233
|
+
// ─── SVG ─────────────────────────────────────────────────────────
|
|
1234
|
+
|
|
1235
|
+
/** What orbit-host draws (`crates/orbit-host/src/vector.rs`); anything else is refused. */
|
|
1236
|
+
const SVG_TAGS = new Set([
|
|
1237
|
+
"svg",
|
|
1238
|
+
"g",
|
|
1239
|
+
"path",
|
|
1240
|
+
"rect",
|
|
1241
|
+
"circle",
|
|
1242
|
+
"ellipse",
|
|
1243
|
+
"line",
|
|
1244
|
+
"polyline",
|
|
1245
|
+
"polygon",
|
|
1246
|
+
"title",
|
|
1247
|
+
"desc",
|
|
1248
|
+
"metadata",
|
|
1249
|
+
]);
|
|
1250
|
+
|
|
1251
|
+
/** The attributes the host reads; the rest (xmlns, aria-*, class) stay here. */
|
|
1252
|
+
const SVG_ATTRS = new Set([
|
|
1253
|
+
"viewBox",
|
|
1254
|
+
"preserveAspectRatio",
|
|
1255
|
+
"width",
|
|
1256
|
+
"height",
|
|
1257
|
+
"fill",
|
|
1258
|
+
"stroke",
|
|
1259
|
+
"stroke-width",
|
|
1260
|
+
"stroke-linecap",
|
|
1261
|
+
"stroke-linejoin",
|
|
1262
|
+
"stroke-miterlimit",
|
|
1263
|
+
"fill-rule",
|
|
1264
|
+
"fill-opacity",
|
|
1265
|
+
"stroke-opacity",
|
|
1266
|
+
"opacity",
|
|
1267
|
+
"shape-rendering",
|
|
1268
|
+
"transform",
|
|
1269
|
+
"d",
|
|
1270
|
+
"x",
|
|
1271
|
+
"y",
|
|
1272
|
+
"rx",
|
|
1273
|
+
"ry",
|
|
1274
|
+
"cx",
|
|
1275
|
+
"cy",
|
|
1276
|
+
"r",
|
|
1277
|
+
"x1",
|
|
1278
|
+
"y1",
|
|
1279
|
+
"x2",
|
|
1280
|
+
"y2",
|
|
1281
|
+
"points",
|
|
1282
|
+
]);
|
|
1283
|
+
|
|
1284
|
+
type SvgNode = {
|
|
1285
|
+
t: string;
|
|
1286
|
+
a: { [name: string]: string | number };
|
|
1287
|
+
c: SvgNode[];
|
|
1288
|
+
};
|
|
1289
|
+
|
|
1290
|
+
/** A paint attribute as the host reads it: ARGB, or `none` / `currentColor`. */
|
|
1291
|
+
function svgPaint(value: string): string | number {
|
|
1292
|
+
const v = value.trim();
|
|
1293
|
+
if (v === "none" || v === "currentColor") return v;
|
|
1294
|
+
return colorToArgb(v);
|
|
1295
|
+
}
|
|
1296
|
+
|
|
1297
|
+
/** An svg element and what it holds, read now (inside the drawing's effect). */
|
|
1298
|
+
function svgTree(el: TElement, values: readonly unknown[]): SvgNode {
|
|
1299
|
+
if (!SVG_TAGS.has(el.tag)) {
|
|
1300
|
+
throw new OrbitError(
|
|
1301
|
+
"E_ORBIT_UNSUPPORTED_ELEMENT",
|
|
1302
|
+
`[orbit] <${el.tag}> inside <svg> is not drawn: paths, basic shapes and g are`,
|
|
1303
|
+
);
|
|
1304
|
+
}
|
|
1305
|
+
const a: SvgNode["a"] = {};
|
|
1306
|
+
for (const attr of el.attrs) {
|
|
1307
|
+
if (!SVG_ATTRS.has(attr.name)) continue;
|
|
1308
|
+
const raw = attrValue(attr, values);
|
|
1309
|
+
const value = isGetter(raw) ? raw() : raw;
|
|
1310
|
+
if (value === null || value === undefined || value === false) continue;
|
|
1311
|
+
const text = str(value);
|
|
1312
|
+
a[attr.name] =
|
|
1313
|
+
attr.name === "fill" || attr.name === "stroke" ? svgPaint(text) : text;
|
|
1314
|
+
}
|
|
1315
|
+
return { t: el.tag, a, c: svgChildren(el.children, values) };
|
|
1316
|
+
}
|
|
1317
|
+
|
|
1318
|
+
/** Child elements, through the slots between them (`${body}`, lists, getters). */
|
|
1319
|
+
function svgChildren(nodes: TNode[], values: readonly unknown[]): SvgNode[] {
|
|
1320
|
+
const out: SvgNode[] = [];
|
|
1321
|
+
const visit = (v: unknown): void => {
|
|
1322
|
+
if (isGetter(v)) visit(v());
|
|
1323
|
+
else if (Array.isArray(v)) v.forEach(visit);
|
|
1324
|
+
else if (isTemplateResult(v))
|
|
1325
|
+
out.push(...svgChildren(parse(v.strings), slotValues(v)));
|
|
1326
|
+
};
|
|
1327
|
+
for (const n of nodes) {
|
|
1328
|
+
if (n.kind === "el") out.push(svgTree(n, values));
|
|
1329
|
+
else
|
|
1330
|
+
for (const part of n.parts)
|
|
1331
|
+
if (typeof part === "number") visit(values[part]);
|
|
1332
|
+
}
|
|
1333
|
+
return out;
|
|
1334
|
+
}
|
|
1335
|
+
|
|
1336
|
+
/** A length attribute in px (`24`, `24px`), or undefined for anything else (`100%`). */
|
|
1337
|
+
function svgLength(el: TElement, name: string, values: readonly unknown[]) {
|
|
1338
|
+
const attr = el.attrs.find((a) => a.name === name);
|
|
1339
|
+
if (!attr) return undefined;
|
|
1340
|
+
const raw = attrValue(attr, values);
|
|
1341
|
+
const n = Number(str(isGetter(raw) ? untrack(raw) : raw).replace(/px$/, ""));
|
|
1342
|
+
return Number.isFinite(n) && n > 0 ? n : undefined;
|
|
1343
|
+
}
|
|
1344
|
+
|
|
1345
|
+
/**
|
|
1346
|
+
* `<svg>`: a vector view orbit-host draws from its paths. The class gives its
|
|
1347
|
+
* box and its text colour (what `currentColor` paints with, else the colour
|
|
1348
|
+
* it inherits); `width` / `height` size it when the class does not, and the
|
|
1349
|
+
* viewBox keeps its proportions.
|
|
1350
|
+
*/
|
|
1351
|
+
function svgElement(
|
|
1352
|
+
el: TElement,
|
|
1353
|
+
values: readonly unknown[],
|
|
1354
|
+
ctx: Ctx,
|
|
1355
|
+
attrs: Attrs,
|
|
1356
|
+
s: Styled,
|
|
1357
|
+
): NativeNode {
|
|
1358
|
+
const node = new NativeNode("OrbitVector", {}, []);
|
|
1359
|
+
const box = bindStyle(node, () => boxOf(s.computed(), ctx));
|
|
1360
|
+
const sized: Style = {};
|
|
1361
|
+
const width = svgLength(el, "width", values);
|
|
1362
|
+
const height = svgLength(el, "height", values);
|
|
1363
|
+
if (width !== undefined && box.width === undefined) sized.width = width;
|
|
1364
|
+
if (height !== undefined && box.height === undefined) sized.height = height;
|
|
1365
|
+
const viewBox = el.attrs.find((a) => a.name === "viewBox");
|
|
1366
|
+
const numbers = viewBox
|
|
1367
|
+
? str(attrValue(viewBox, values))
|
|
1368
|
+
.trim()
|
|
1369
|
+
.split(/[\s,]+/)
|
|
1370
|
+
.map(Number)
|
|
1371
|
+
: [];
|
|
1372
|
+
const [, , vw, vh] = numbers;
|
|
1373
|
+
if (
|
|
1374
|
+
vw !== undefined &&
|
|
1375
|
+
vh !== undefined &&
|
|
1376
|
+
vw > 0 &&
|
|
1377
|
+
vh > 0 &&
|
|
1378
|
+
box.aspectRatio === undefined &&
|
|
1379
|
+
(sized.width === undefined || sized.height === undefined)
|
|
1380
|
+
) {
|
|
1381
|
+
sized.aspectRatio = vw / vh;
|
|
1382
|
+
}
|
|
1383
|
+
const color = (): string => {
|
|
1384
|
+
const value = s.computed().text.color ?? ctx.text().color ?? "#000000";
|
|
1385
|
+
return typeof value === "string" ? value : "#000000";
|
|
1386
|
+
};
|
|
1387
|
+
const describe = () => ({
|
|
1388
|
+
color: colorToArgb(color()),
|
|
1389
|
+
root: svgTree(el, values),
|
|
1390
|
+
});
|
|
1391
|
+
let first = true;
|
|
1392
|
+
let initial: ReturnType<typeof describe> | null = null;
|
|
1393
|
+
effect(() => {
|
|
1394
|
+
const next = describe();
|
|
1395
|
+
if (first) {
|
|
1396
|
+
first = false;
|
|
1397
|
+
initial = next;
|
|
1398
|
+
} else {
|
|
1399
|
+
node.setProps({ vector: next });
|
|
1400
|
+
}
|
|
1401
|
+
});
|
|
1402
|
+
node.props = { ...flatten({ ...sized, ...box }), vector: initial };
|
|
1403
|
+
return withMotion(withEvents(node, attrs), attrs);
|
|
1404
|
+
}
|
|
1405
|
+
|
|
1406
|
+
function element(
|
|
1407
|
+
el: TElement,
|
|
1408
|
+
values: readonly unknown[],
|
|
1409
|
+
ctx: Ctx,
|
|
1410
|
+
): NativeNode {
|
|
1411
|
+
if (UNSUPPORTED.has(el.tag)) {
|
|
1412
|
+
throw new OrbitError(
|
|
1413
|
+
"E_ORBIT_UNSUPPORTED_ELEMENT",
|
|
1414
|
+
`[orbit] <${el.tag}> has no native mapping yet`,
|
|
1415
|
+
);
|
|
1416
|
+
}
|
|
1417
|
+
const attrs = readAttrs(el, values);
|
|
1418
|
+
const s = styled(el.tag, attrs, ctx);
|
|
1419
|
+
if (el.tag === "input" || el.tag === "textarea") {
|
|
1420
|
+
return inputElement(el, values, ctx, attrs, s);
|
|
1421
|
+
}
|
|
1422
|
+
if (el.tag === "img") return imgElement(el, values, attrs, s, ctx);
|
|
1423
|
+
if (el.tag === "svg") return svgElement(el, values, ctx, attrs, s);
|
|
1424
|
+
const own = TAG_TEXT[el.tag] ?? {};
|
|
1425
|
+
if (TEXT_BLOCK.has(el.tag) || isInline(el)) {
|
|
1426
|
+
const node = new NativeNode(
|
|
1427
|
+
"RCTText",
|
|
1428
|
+
{},
|
|
1429
|
+
textChildren(el.children, values, childCtx(s, ctx, own)),
|
|
1430
|
+
);
|
|
1431
|
+
node.props = flatten(
|
|
1432
|
+
bindStyle(node, () => {
|
|
1433
|
+
const c = s.computed();
|
|
1434
|
+
return textProps({
|
|
1435
|
+
...ctx.text(),
|
|
1436
|
+
...own,
|
|
1437
|
+
...c.text,
|
|
1438
|
+
...boxOf(c, ctx),
|
|
1439
|
+
});
|
|
1440
|
+
}),
|
|
1441
|
+
);
|
|
1442
|
+
return withEvents(node, attrs);
|
|
1443
|
+
}
|
|
1444
|
+
// What the element is (a scroll view, a flex container) is read once.
|
|
1445
|
+
const first = untrack(s.computed);
|
|
1446
|
+
if (first.scroll) return scrollElement(el, values, ctx, attrs, s);
|
|
1447
|
+
const node = new NativeNode(
|
|
1448
|
+
"RCTView",
|
|
1449
|
+
{},
|
|
1450
|
+
viewChildren(el.children, values, childCtx(s, ctx, own), first.flex),
|
|
1451
|
+
);
|
|
1452
|
+
node.props = flatten(bindStyle(node, () => boxOf(s.computed(), ctx)));
|
|
1453
|
+
return withMotion(withEvents(node, attrs), attrs);
|
|
1454
|
+
}
|
|
1455
|
+
|
|
1456
|
+
function virtualText(
|
|
1457
|
+
el: TElement,
|
|
1458
|
+
values: readonly unknown[],
|
|
1459
|
+
ctx: Ctx,
|
|
1460
|
+
): NativeNode {
|
|
1461
|
+
if (!isInline(el) && !TEXT_BLOCK.has(el.tag)) {
|
|
1462
|
+
throw new OrbitError(
|
|
1463
|
+
"E_ORBIT_BLOCK_IN_TEXT",
|
|
1464
|
+
`[orbit] <${el.tag}> cannot sit inside text on native`,
|
|
1465
|
+
);
|
|
1466
|
+
}
|
|
1467
|
+
const attrs = readAttrs(el, values);
|
|
1468
|
+
const s = styled(el.tag, attrs, ctx);
|
|
1469
|
+
const own = TAG_TEXT[el.tag] ?? {};
|
|
1470
|
+
const node = new NativeNode(
|
|
1471
|
+
"RCTVirtualText",
|
|
1472
|
+
{},
|
|
1473
|
+
textChildren(el.children, values, childCtx(s, ctx, own)),
|
|
1474
|
+
);
|
|
1475
|
+
// Only its own style: the host cascades the rest from the text around it.
|
|
1476
|
+
node.props = flatten(
|
|
1477
|
+
bindStyle(node, () => {
|
|
1478
|
+
const c = s.computed();
|
|
1479
|
+
const mine = { ...own, ...c.text, ...c.box };
|
|
1480
|
+
const size = mine.fontSize ?? ctx.text().fontSize;
|
|
1481
|
+
const lh = textProps({
|
|
1482
|
+
fontSize: size ?? 16,
|
|
1483
|
+
lineHeight: mine.lineHeight ?? "",
|
|
1484
|
+
});
|
|
1485
|
+
return mine.lineHeight === undefined
|
|
1486
|
+
? mine
|
|
1487
|
+
: { ...mine, lineHeight: lh.lineHeight ?? "" };
|
|
1488
|
+
}),
|
|
1489
|
+
);
|
|
1490
|
+
return withEvents(node, attrs);
|
|
1491
|
+
}
|
|
1492
|
+
|
|
1493
|
+
// Children inside an RCTText: raw strings and nested virtual texts only.
|
|
1494
|
+
function textChildren(
|
|
1495
|
+
nodes: TNode[],
|
|
1496
|
+
values: readonly unknown[],
|
|
1497
|
+
ctx: Ctx,
|
|
1498
|
+
): Child[] {
|
|
1499
|
+
const out: Child[] = [];
|
|
1500
|
+
for (const n of nodes) {
|
|
1501
|
+
if (n.kind === "el") {
|
|
1502
|
+
out.push(virtualText(n, values, ctx));
|
|
1503
|
+
continue;
|
|
1504
|
+
}
|
|
1505
|
+
for (const part of n.parts) {
|
|
1506
|
+
if (typeof part === "string") out.push(rawText(part));
|
|
1507
|
+
else out.push(...textValue(values[part], ctx));
|
|
1508
|
+
}
|
|
1509
|
+
}
|
|
1510
|
+
return out;
|
|
1511
|
+
}
|
|
1512
|
+
|
|
1513
|
+
function textValue(v: unknown, ctx: Ctx): Child[] {
|
|
1514
|
+
const list = readEach(v);
|
|
1515
|
+
const again = (value: unknown) => textValue(value, ctx);
|
|
1516
|
+
if (list) return [keyedRegion(list, again)];
|
|
1517
|
+
if (isGetter(v)) return [reactiveSlot(v, again).child];
|
|
1518
|
+
if (isTemplateResult(v)) {
|
|
1519
|
+
ownLifecycle(v);
|
|
1520
|
+
return textChildren(parse(v.strings), slotValues(v), ctx);
|
|
1521
|
+
}
|
|
1522
|
+
if (Array.isArray(v)) return v.flatMap(again);
|
|
1523
|
+
return isPrimitive(v) && str(v) ? [rawText(str(v))] : [];
|
|
1524
|
+
}
|
|
1525
|
+
|
|
1526
|
+
// Children inside an RCTView: elements, plus text runs wrapped in RCTText.
|
|
1527
|
+
// In a CSS flex container every child is its own flex item, inline or not;
|
|
1528
|
+
// elsewhere inline content flows into a shared text run.
|
|
1529
|
+
function viewChildren(
|
|
1530
|
+
nodes: TNode[],
|
|
1531
|
+
values: readonly unknown[],
|
|
1532
|
+
ctx: Ctx,
|
|
1533
|
+
flexItems = false,
|
|
1534
|
+
): Child[] {
|
|
1535
|
+
const out: Child[] = [];
|
|
1536
|
+
let run: Child[] = [];
|
|
1537
|
+
const flushRun = () => {
|
|
1538
|
+
// A run of nothing but white space makes no box, as in CSS: the
|
|
1539
|
+
// spaces between `${…}` in a template are not flex items.
|
|
1540
|
+
const blank = run.every(
|
|
1541
|
+
(c) =>
|
|
1542
|
+
c instanceof NativeNode &&
|
|
1543
|
+
c.viewName === "RCTRawText" &&
|
|
1544
|
+
!/\S/.test(str(c.props.text)),
|
|
1545
|
+
);
|
|
1546
|
+
if (run.length > 0 && !blank) out.push(textRun(ctx, run));
|
|
1547
|
+
run = [];
|
|
1548
|
+
};
|
|
1549
|
+
for (const n of nodes) {
|
|
1550
|
+
if (flexItems) flushRun();
|
|
1551
|
+
if (n.kind === "el") {
|
|
1552
|
+
if (isInline(n) && !flexItems && !isLink(n)) {
|
|
1553
|
+
run.push(virtualText(n, values, ctx));
|
|
1554
|
+
} else {
|
|
1555
|
+
flushRun();
|
|
1556
|
+
out.push(element(n, values, ctx));
|
|
1557
|
+
}
|
|
1558
|
+
continue;
|
|
1559
|
+
}
|
|
1560
|
+
for (const part of n.parts) {
|
|
1561
|
+
if (typeof part === "string") {
|
|
1562
|
+
run.push(rawText(part));
|
|
1563
|
+
continue;
|
|
1564
|
+
}
|
|
1565
|
+
const v = values[part];
|
|
1566
|
+
if (isPrimitive(v)) {
|
|
1567
|
+
run.push(...textValue(v, ctx));
|
|
1568
|
+
} else if (isGetter(v) && !readEach(v)) {
|
|
1569
|
+
const slot = reactiveSlot(v, (value) => viewValue(value, ctx));
|
|
1570
|
+
if (slot.text) {
|
|
1571
|
+
run.push(slot.child);
|
|
1572
|
+
} else {
|
|
1573
|
+
flushRun();
|
|
1574
|
+
out.push(slot.child);
|
|
1575
|
+
}
|
|
1576
|
+
} else {
|
|
1577
|
+
flushRun();
|
|
1578
|
+
out.push(...viewValue(v, ctx));
|
|
1579
|
+
}
|
|
1580
|
+
}
|
|
1581
|
+
}
|
|
1582
|
+
flushRun();
|
|
1583
|
+
return out;
|
|
1584
|
+
}
|
|
1585
|
+
|
|
1586
|
+
function viewValue(v: unknown, ctx: Ctx): Child[] {
|
|
1587
|
+
if (v instanceof Stack) {
|
|
1588
|
+
return [
|
|
1589
|
+
v.attach(
|
|
1590
|
+
(result) => scoped(() => viewValue(result, ctx)),
|
|
1591
|
+
(end) => collecting?.push(end),
|
|
1592
|
+
),
|
|
1593
|
+
];
|
|
1594
|
+
}
|
|
1595
|
+
const list = readEach(v);
|
|
1596
|
+
const again = (value: unknown) => viewValue(value, ctx);
|
|
1597
|
+
if (list) return [keyedRegion(list, again)];
|
|
1598
|
+
if (isGetter(v)) return [region(v, again)];
|
|
1599
|
+
if (isTemplateResult(v)) {
|
|
1600
|
+
ownLifecycle(v);
|
|
1601
|
+
return viewChildren(parse(v.strings), slotValues(v), ctx, ctx.flexParent);
|
|
1602
|
+
}
|
|
1603
|
+
if (Array.isArray(v)) return v.flatMap(again);
|
|
1604
|
+
const text = str(v);
|
|
1605
|
+
return text ? [textRun(ctx, [rawText(text)])] : [];
|
|
1606
|
+
}
|
|
1607
|
+
|
|
1608
|
+
/**
|
|
1609
|
+
* A template's values, each function bound to the providers the template was
|
|
1610
|
+
* written under: a component a slot builds later (a row, a branch, a pushed
|
|
1611
|
+
* screen's child) still injects what was provided around it.
|
|
1612
|
+
*/
|
|
1613
|
+
function slotValues(result: TemplateResult): unknown[] {
|
|
1614
|
+
return result.values.map((value) => bindTemplateContext(result, value));
|
|
1615
|
+
}
|
|
1616
|
+
|
|
1617
|
+
// The root text: a browser's 16px black, and Tailwind's preflight line height
|
|
1618
|
+
// (`html { line-height: 1.5 }`), a multiple each text applies to its own size.
|
|
1619
|
+
const ROOT_TEXT: Style = { fontSize: 16, color: "#000000", lineHeight: "1.5x" };
|
|
1620
|
+
|
|
1621
|
+
/**
|
|
1622
|
+
* Render an aurora template as native views through the Rust host. Returns
|
|
1623
|
+
* the root: it commits on its own after each change (batched to a microtask);
|
|
1624
|
+
* `flush()` commits synchronously.
|
|
1625
|
+
*/
|
|
1626
|
+
export function mount(result: TemplateResult, options: MountOptions): Root {
|
|
1627
|
+
viewportSize(options.viewport);
|
|
1628
|
+
const top: Ctx = {
|
|
1629
|
+
text: () => ROOT_TEXT,
|
|
1630
|
+
parent: null,
|
|
1631
|
+
flexParent: false,
|
|
1632
|
+
siblings: { last: null },
|
|
1633
|
+
};
|
|
1634
|
+
// The root is the page's html and body: the base layer's `body { … }`
|
|
1635
|
+
// (nebula's background and text colour) styles it.
|
|
1636
|
+
const body = styled(
|
|
1637
|
+
"body",
|
|
1638
|
+
{
|
|
1639
|
+
className: "",
|
|
1640
|
+
gestures: new Map(),
|
|
1641
|
+
motion: null,
|
|
1642
|
+
scrollY: null,
|
|
1643
|
+
attributes: new Map(),
|
|
1644
|
+
},
|
|
1645
|
+
top,
|
|
1646
|
+
true,
|
|
1647
|
+
);
|
|
1648
|
+
const tree = new NativeNode(
|
|
1649
|
+
"RCTView",
|
|
1650
|
+
{},
|
|
1651
|
+
viewValue(result, childCtx(body, top)),
|
|
1652
|
+
);
|
|
1653
|
+
tree.props = flatten(
|
|
1654
|
+
bindStyle(tree, () => ({ flex: 1, ...boxOf(body.computed(), top) })),
|
|
1655
|
+
);
|
|
1656
|
+
return mountTree(tree, options);
|
|
1657
|
+
}
|