@relo-ui/web 0.0.0-stage → 0.1.0-rc.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +27 -2
- package/dist/LICENSE +202 -0
- package/dist/NOTICE +5 -0
- package/dist/core/contracts.d.ts +72 -0
- package/dist/core/contracts.js +76 -0
- package/dist/core/document.d.ts +16 -0
- package/dist/core/document.js +21 -0
- package/dist/core/engine.d.ts +8 -0
- package/dist/core/engine.js +28 -0
- package/dist/core/index.d.ts +7 -0
- package/dist/core/index.js +7 -0
- package/dist/core/runtime.d.ts +23 -0
- package/dist/core/runtime.js +46 -0
- package/dist/core/schema.d.ts +56 -0
- package/dist/core/schema.js +72 -0
- package/dist/core/session.d.ts +88 -0
- package/dist/core/session.js +520 -0
- package/dist/core/value.d.ts +18 -0
- package/dist/core/value.js +118 -0
- package/dist/dom/accessibility.d.ts +2 -0
- package/dist/dom/accessibility.js +69 -0
- package/dist/dom/icons.d.ts +1 -0
- package/dist/dom/icons.js +21 -0
- package/dist/dom/images.d.ts +59 -0
- package/dist/dom/images.js +406 -0
- package/dist/dom/index.d.ts +3 -0
- package/dist/dom/index.js +2 -0
- package/dist/dom/inputs.d.ts +5 -0
- package/dist/dom/inputs.js +85 -0
- package/dist/dom/lists.d.ts +7 -0
- package/dist/dom/lists.js +48 -0
- package/dist/dom/measurement.d.ts +16 -0
- package/dist/dom/measurement.js +67 -0
- package/dist/dom/motion-lifecycle.d.ts +17 -0
- package/dist/dom/motion-lifecycle.js +28 -0
- package/dist/dom/motion-plan.d.ts +44 -0
- package/dist/dom/motion-plan.js +31 -0
- package/dist/dom/motion.d.ts +73 -0
- package/dist/dom/motion.js +817 -0
- package/dist/dom/mount-plan.d.ts +15 -0
- package/dist/dom/mount-plan.js +58 -0
- package/dist/dom/presentation.d.ts +11 -0
- package/dist/dom/presentation.js +331 -0
- package/dist/dom/primitives.d.ts +16 -0
- package/dist/dom/primitives.js +378 -0
- package/dist/dom/range.d.ts +9 -0
- package/dist/dom/range.js +120 -0
- package/dist/dom/renderer.d.ts +99 -0
- package/dist/dom/renderer.js +1041 -0
- package/dist/dom/scroll.d.ts +7 -0
- package/dist/dom/scroll.js +167 -0
- package/dist/dom/shared-layout.d.ts +40 -0
- package/dist/dom/shared-layout.js +440 -0
- package/dist/dom/style.d.ts +35 -0
- package/dist/dom/style.js +337 -0
- package/dist/dom/text-motion.d.ts +17 -0
- package/dist/dom/text-motion.js +185 -0
- package/dist/dom/theme.d.ts +13 -0
- package/dist/dom/theme.js +65 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.js +18 -0
- package/dist/licenses/Rust-COPYRIGHT-library.html +29357 -0
- package/dist/licenses/archery-1.2.3/LICENSE.md +21 -0
- package/dist/licenses/bytemuck-1.25.2/LICENSE-APACHE +61 -0
- package/dist/licenses/bytemuck-1.25.2/LICENSE-MIT +9 -0
- package/dist/licenses/bytemuck-1.25.2/LICENSE-ZLIB +11 -0
- package/dist/licenses/cargo.json +340 -0
- package/dist/licenses/cfg-if-1.0.5/LICENSE-APACHE +201 -0
- package/dist/licenses/cfg-if-1.0.5/LICENSE-MIT +25 -0
- package/dist/licenses/equivalent-1.0.2/LICENSE-APACHE +201 -0
- package/dist/licenses/equivalent-1.0.2/LICENSE-MIT +25 -0
- package/dist/licenses/futures-core-0.3.34/LICENSE-APACHE +202 -0
- package/dist/licenses/futures-core-0.3.34/LICENSE-MIT +26 -0
- package/dist/licenses/futures-task-0.3.34/LICENSE-APACHE +202 -0
- package/dist/licenses/futures-task-0.3.34/LICENSE-MIT +26 -0
- package/dist/licenses/futures-util-0.3.34/LICENSE-APACHE +202 -0
- package/dist/licenses/futures-util-0.3.34/LICENSE-MIT +26 -0
- package/dist/licenses/imbl-7.0.2/LICENCE.md +355 -0
- package/dist/licenses/imbl-sized-chunks-0.2.0/LICENCE.md +355 -0
- package/dist/licenses/itoa-1.0.18/LICENSE-APACHE +176 -0
- package/dist/licenses/itoa-1.0.18/LICENSE-MIT +23 -0
- package/dist/licenses/js-sys-0.3.106/LICENSE-APACHE +201 -0
- package/dist/licenses/js-sys-0.3.106/LICENSE-MIT +25 -0
- package/dist/licenses/memchr-2.8.3/COPYING +3 -0
- package/dist/licenses/memchr-2.8.3/LICENSE-MIT +21 -0
- package/dist/licenses/once_cell-1.21.4/LICENSE-APACHE +201 -0
- package/dist/licenses/once_cell-1.21.4/LICENSE-MIT +23 -0
- package/dist/licenses/pin-project-lite-0.2.17/LICENSE-APACHE +177 -0
- package/dist/licenses/pin-project-lite-0.2.17/LICENSE-MIT +23 -0
- package/dist/licenses/rand_core-0.9.5/COPYRIGHT +12 -0
- package/dist/licenses/rand_core-0.9.5/LICENSE-APACHE +187 -0
- package/dist/licenses/rand_core-0.9.5/LICENSE-MIT +26 -0
- package/dist/licenses/rand_xoshiro-0.7.0/COPYRIGHT +12 -0
- package/dist/licenses/rand_xoshiro-0.7.0/LICENSE-APACHE +176 -0
- package/dist/licenses/rand_xoshiro-0.7.0/LICENSE-MIT +25 -0
- package/dist/licenses/roxmltree-0.21.1/LICENSE-APACHE +201 -0
- package/dist/licenses/roxmltree-0.21.1/LICENSE-MIT +21 -0
- package/dist/licenses/safe_arch-0.7.4/LICENSE-APACHE.md +61 -0
- package/dist/licenses/safe_arch-0.7.4/LICENSE-MIT.md +9 -0
- package/dist/licenses/safe_arch-0.7.4/LICENSE-ZLIB.md +11 -0
- package/dist/licenses/serde-1.0.229/LICENSE-APACHE +176 -0
- package/dist/licenses/serde-1.0.229/LICENSE-MIT +23 -0
- package/dist/licenses/serde_core-1.0.229/LICENSE-APACHE +176 -0
- package/dist/licenses/serde_core-1.0.229/LICENSE-MIT +23 -0
- package/dist/licenses/serde_json-1.0.151/LICENSE-APACHE +176 -0
- package/dist/licenses/serde_json-1.0.151/LICENSE-MIT +23 -0
- package/dist/licenses/slab-0.4.12/LICENSE +25 -0
- package/dist/licenses/slotmap-1.1.1/LICENSE +19 -0
- package/dist/licenses/smallvec-1.16.3/LICENSE-APACHE +201 -0
- package/dist/licenses/smallvec-1.16.3/LICENSE-MIT +25 -0
- package/dist/licenses/taffy-0.15.0/taffy-0.15.0-LICENSE +22 -0
- package/dist/licenses/unicode-ident-1.0.26/LICENSE-APACHE +176 -0
- package/dist/licenses/unicode-ident-1.0.26/LICENSE-MIT +23 -0
- package/dist/licenses/unicode-ident-1.0.26/LICENSE-UNICODE +39 -0
- package/dist/licenses/wasm-bindgen-0.2.129/LICENSE-APACHE +201 -0
- package/dist/licenses/wasm-bindgen-0.2.129/LICENSE-MIT +25 -0
- package/dist/licenses/wasm-bindgen-shared-0.2.129/LICENSE-APACHE +201 -0
- package/dist/licenses/wasm-bindgen-shared-0.2.129/LICENSE-MIT +25 -0
- package/dist/licenses/web-time-1.1.0/LICENSE-APACHE +201 -0
- package/dist/licenses/web-time-1.1.0/LICENSE-MIT +21 -0
- package/dist/licenses/wide-0.7.33/LICENSE-ZLIB.md +11 -0
- package/dist/licenses/zmij-1.0.23/LICENSE-MIT +23 -0
- package/dist/wasm/relo_wasm.d.ts +179 -0
- package/dist/wasm/relo_wasm.js +1228 -0
- package/dist/wasm/relo_wasm_bg.wasm +0 -0
- package/dist/wasm/relo_wasm_bg.wasm.d.ts +65 -0
- package/package.json +25 -4
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
export class ActionError extends Error {
|
|
2
|
+
code;
|
|
3
|
+
location;
|
|
4
|
+
constructor(code, message, location) {
|
|
5
|
+
super(message);
|
|
6
|
+
this.code = code;
|
|
7
|
+
this.location = location;
|
|
8
|
+
this.name = 'ActionError';
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
export function asError(error) {
|
|
12
|
+
if (error instanceof ActionError)
|
|
13
|
+
return error;
|
|
14
|
+
if (typeof error === 'string') {
|
|
15
|
+
try {
|
|
16
|
+
const value = JSON.parse(error);
|
|
17
|
+
if (typeof value.code === 'string' && typeof value.message === 'string')
|
|
18
|
+
return new ActionError(value.code, value.message, value.location ?? undefined);
|
|
19
|
+
}
|
|
20
|
+
catch {
|
|
21
|
+
/* Host exceptions do not expose arbitrary messages. */
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
return new ActionError('unavailable', 'Host operation failed');
|
|
25
|
+
}
|
|
26
|
+
export function fail(message, code = 'invalidArguments') {
|
|
27
|
+
throw new ActionError(code, message);
|
|
28
|
+
}
|
|
29
|
+
export function call(operation) {
|
|
30
|
+
try {
|
|
31
|
+
return operation();
|
|
32
|
+
}
|
|
33
|
+
catch (error) {
|
|
34
|
+
throw asError(error);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
const encoder = new TextEncoder();
|
|
38
|
+
function scalarString(value) {
|
|
39
|
+
for (let i = 0; i < value.length; i++) {
|
|
40
|
+
const unit = value.charCodeAt(i);
|
|
41
|
+
if (unit >= 0xd800 && unit <= 0xdbff) {
|
|
42
|
+
const next = value.charCodeAt(++i);
|
|
43
|
+
if (!(next >= 0xdc00 && next <= 0xdfff))
|
|
44
|
+
fail('Ill-formed Unicode');
|
|
45
|
+
}
|
|
46
|
+
else if (unit >= 0xdc00 && unit <= 0xdfff)
|
|
47
|
+
fail('Ill-formed Unicode');
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
/** Snapshot before crossing the engine boundary; never invoke getters or toJSON. */
|
|
51
|
+
export function snapshot(value) {
|
|
52
|
+
const ancestors = new Set();
|
|
53
|
+
let work = 0;
|
|
54
|
+
function visit(item, depth) {
|
|
55
|
+
if (++work > 100_000 || depth > 64)
|
|
56
|
+
fail('JSON value limit', 'limitExceeded');
|
|
57
|
+
if (item === null || typeof item === 'boolean')
|
|
58
|
+
return item;
|
|
59
|
+
if (typeof item === 'string') {
|
|
60
|
+
scalarString(item);
|
|
61
|
+
return item;
|
|
62
|
+
}
|
|
63
|
+
if (typeof item === 'number') {
|
|
64
|
+
if (!Number.isFinite(item))
|
|
65
|
+
fail('Expected finite number');
|
|
66
|
+
return item;
|
|
67
|
+
}
|
|
68
|
+
if (typeof item !== 'object' || ancestors.has(item))
|
|
69
|
+
fail('Expected acyclic JSON value');
|
|
70
|
+
const array = Array.isArray(item);
|
|
71
|
+
if (!array &&
|
|
72
|
+
Object.getPrototypeOf(item) !== Object.prototype &&
|
|
73
|
+
Object.getPrototypeOf(item) !== null)
|
|
74
|
+
fail('Expected plain JSON record');
|
|
75
|
+
if (Object.getOwnPropertySymbols(item).length)
|
|
76
|
+
fail('Symbol properties are not JSON');
|
|
77
|
+
ancestors.add(item);
|
|
78
|
+
const result = array ? [] : Object.create(null);
|
|
79
|
+
for (const key of Object.keys(item)) {
|
|
80
|
+
scalarString(key);
|
|
81
|
+
const descriptor = Object.getOwnPropertyDescriptor(item, key);
|
|
82
|
+
if (!('value' in descriptor))
|
|
83
|
+
fail('JSON accessors are unsupported');
|
|
84
|
+
if (array && (!/^(0|[1-9]\d*)$/.test(key) || Number(key) >= item.length))
|
|
85
|
+
fail('Unexpected array property');
|
|
86
|
+
result[key] = visit(descriptor.value, depth + 1);
|
|
87
|
+
}
|
|
88
|
+
if (array && result.length !== item.length)
|
|
89
|
+
fail('Sparse arrays are unsupported');
|
|
90
|
+
if (array && Object.keys(result).length !== item.length)
|
|
91
|
+
fail('Sparse arrays are unsupported');
|
|
92
|
+
ancestors.delete(item);
|
|
93
|
+
return Object.freeze(result);
|
|
94
|
+
}
|
|
95
|
+
const result = visit(value, 0);
|
|
96
|
+
if (encoder.encode(JSON.stringify(result)).length > 8 * 1024 * 1024)
|
|
97
|
+
fail('JSON size limit', 'limitExceeded');
|
|
98
|
+
return result;
|
|
99
|
+
}
|
|
100
|
+
export function record(value) {
|
|
101
|
+
const result = snapshot(value);
|
|
102
|
+
if (result === null || typeof result !== 'object' || Array.isArray(result))
|
|
103
|
+
fail('Expected record');
|
|
104
|
+
return result;
|
|
105
|
+
}
|
|
106
|
+
export function equal(a, b) {
|
|
107
|
+
if (a === b)
|
|
108
|
+
return true;
|
|
109
|
+
if (!a ||
|
|
110
|
+
!b ||
|
|
111
|
+
typeof a !== 'object' ||
|
|
112
|
+
typeof b !== 'object' ||
|
|
113
|
+
Array.isArray(a) !== Array.isArray(b))
|
|
114
|
+
return false;
|
|
115
|
+
const keys = Object.keys(a);
|
|
116
|
+
return (keys.length === Object.keys(b).length &&
|
|
117
|
+
keys.every((key) => Object.hasOwn(b, key) && equal(a[key], b[key])));
|
|
118
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
let sequence = 0;
|
|
2
|
+
const id = (element) => (element.id ||= `relo-semantics-${++sequence}`);
|
|
3
|
+
const target = (value) => value.node.props.semantics && value.node.props.semantics !== 'none'
|
|
4
|
+
? value.element
|
|
5
|
+
: (value.content ?? value.element);
|
|
6
|
+
export function coordinateTables(nodes) {
|
|
7
|
+
const all = [...nodes];
|
|
8
|
+
for (const value of all)
|
|
9
|
+
for (const name of [
|
|
10
|
+
'aria-rowcount',
|
|
11
|
+
'aria-colcount',
|
|
12
|
+
'aria-rowindex',
|
|
13
|
+
'aria-colindex',
|
|
14
|
+
'aria-owns',
|
|
15
|
+
])
|
|
16
|
+
target(value).removeAttribute(name);
|
|
17
|
+
for (const table of all.filter((value) => value.node.props.semantics === 'table')) {
|
|
18
|
+
const rows = [];
|
|
19
|
+
const walk = (value) => {
|
|
20
|
+
if (value !== table && value.node.props.semantics === 'table')
|
|
21
|
+
return;
|
|
22
|
+
if (value.node.props.semantics === 'row')
|
|
23
|
+
rows.push(value);
|
|
24
|
+
else
|
|
25
|
+
value.children.forEach(walk);
|
|
26
|
+
};
|
|
27
|
+
walk(table);
|
|
28
|
+
const cells = rows.map((row) => {
|
|
29
|
+
const result = [];
|
|
30
|
+
const collect = (value) => {
|
|
31
|
+
if (value !== row && ['table', 'row'].includes(value.node.props.semantics))
|
|
32
|
+
return;
|
|
33
|
+
if (['cell', 'columnHeader', 'rowHeader'].includes(value.node.props.semantics))
|
|
34
|
+
result.push(value);
|
|
35
|
+
else
|
|
36
|
+
value.children.forEach(collect);
|
|
37
|
+
};
|
|
38
|
+
collect(row);
|
|
39
|
+
return result;
|
|
40
|
+
});
|
|
41
|
+
const element = target(table);
|
|
42
|
+
element.setAttribute('aria-rowcount', String(rows.length));
|
|
43
|
+
element.setAttribute('aria-colcount', String(table.node.props.columns ?? Math.max(0, ...cells.map((row) => row.length))));
|
|
44
|
+
if (rows.length)
|
|
45
|
+
element.setAttribute('aria-owns', rows.map((row) => id(target(row))).join(' '));
|
|
46
|
+
rows.forEach((row, r) => {
|
|
47
|
+
target(row).setAttribute('aria-rowindex', String(r + 1));
|
|
48
|
+
if (cells[r].length)
|
|
49
|
+
target(row).setAttribute('aria-owns', cells[r].map((cell) => id(target(cell))).join(' '));
|
|
50
|
+
cells[r].forEach((cell, c) => {
|
|
51
|
+
const el = target(cell);
|
|
52
|
+
el.setAttribute('aria-rowindex', String(r + 1));
|
|
53
|
+
el.setAttribute('aria-colindex', String(c + 1));
|
|
54
|
+
const headers = [
|
|
55
|
+
...cells.flatMap((row) => row[c]?.node.props.semantics === 'columnHeader' ? [row[c]] : []),
|
|
56
|
+
...cells[r].filter((cell) => cell.node.props.semantics === 'rowHeader'),
|
|
57
|
+
].filter((header) => header !== cell);
|
|
58
|
+
const own = el
|
|
59
|
+
.getAttribute('aria-describedby')
|
|
60
|
+
?.split(' ')
|
|
61
|
+
.filter((id) => !id.startsWith('relo-semantics-')) ?? [];
|
|
62
|
+
if (headers.length || own.length)
|
|
63
|
+
el.setAttribute('aria-describedby', [...own, ...headers.map((header) => id(target(header)))].join(' '));
|
|
64
|
+
else
|
|
65
|
+
el.removeAttribute('aria-describedby');
|
|
66
|
+
});
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const namedIcons: Readonly<Record<string, string>>;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
// Portable symbol names use deterministic vector geometry instead of font glyphs.
|
|
2
|
+
// Coordinates are SDK-authored, not downloaded SVG/HTML.
|
|
3
|
+
export const namedIcons = {
|
|
4
|
+
search: 'M21 21l-5-5 M19 10.5a8.5 8.5 0 1 1-17 0 8.5 8.5 0 0 1 17 0',
|
|
5
|
+
heart: 'M20.8 4.6a5.5 5.5 0 0 0-7.8 0L12 5.7l-1.1-1.1a5.5 5.5 0 0 0-7.8 7.8L12 21l8.8-8.6a5.5 5.5 0 0 0 0-7.8z',
|
|
6
|
+
'heart.fill': 'M20.8 4.6a5.5 5.5 0 0 0-7.8 0L12 5.7l-1.1-1.1a5.5 5.5 0 0 0-7.8 7.8L12 21l8.8-8.6a5.5 5.5 0 0 0 0-7.8z',
|
|
7
|
+
suitcase: 'M7 5V3h10v2 M4 5h16v16H4z M8 5v16 M16 5v16',
|
|
8
|
+
ski: 'M13 2a1.5 1.5 0 1 0 0 3 1.5 1.5 0 0 0 0-3 M11 6l-3 6 5 2 2 5 M11 6l4 5h5 M3 21h17l2-2 M6 10l-3 8 M18 10l3 8',
|
|
9
|
+
gift: 'M3 8h18v4H3z M5 12v9h14v-9 M12 8v13 M12 8H7a3 3 0 1 1 3-3z M12 8h5a3 3 0 1 0-3-3z',
|
|
10
|
+
tag: 'M3 3h8l10 10-8 8L3 11z M7 7h.01',
|
|
11
|
+
news: 'M3 3h16v18H3z M19 7h3v14 M7 7h8 M7 11h8 M7 15h3 M7 18h8',
|
|
12
|
+
shield: 'M12 2l9 4v6c0 5-9 10-9 10S3 17 3 12V6z',
|
|
13
|
+
bell: 'M18 8a6 6 0 0 0-12 0c0 7-3 7-3 9h18c0-2-3-2-3-9 M9 21h6 M12 2V1',
|
|
14
|
+
menu: 'M3 6h18 M3 12h18 M3 18h18',
|
|
15
|
+
back: 'M15 4l-8 8 8 8',
|
|
16
|
+
more: 'M4 12h.01 M12 12h.01 M20 12h.01',
|
|
17
|
+
check: 'M4 12l5 5L20 6',
|
|
18
|
+
star: 'M12 2l3 6.2 7 .9-5 4.9 1.2 7L12 17.7 5.8 21 7 14 2 9.1l7-.9z',
|
|
19
|
+
circle: 'M22 12a10 10 0 1 1-20 0 10 10 0 0 1 20 0',
|
|
20
|
+
person: 'M16 6a4 4 0 1 1-8 0 4 4 0 0 1 8 0 M4 22v-3a8 8 0 0 1 16 0v3',
|
|
21
|
+
};
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import type { Mounted } from './renderer.js';
|
|
2
|
+
export interface RemoteImageRequest {
|
|
3
|
+
readonly source: string;
|
|
4
|
+
readonly width: number;
|
|
5
|
+
readonly height: number;
|
|
6
|
+
readonly signal: AbortSignal;
|
|
7
|
+
}
|
|
8
|
+
export interface RemoteImageResource {
|
|
9
|
+
readonly url: string;
|
|
10
|
+
readonly width: number;
|
|
11
|
+
readonly height: number;
|
|
12
|
+
release(): void;
|
|
13
|
+
}
|
|
14
|
+
export type RemoteImageLoader = (request: RemoteImageRequest) => Promise<RemoteImageResource>;
|
|
15
|
+
export interface ImageLoaderOptions {
|
|
16
|
+
maxResponseBytes?: number;
|
|
17
|
+
maxSourcePixels?: number;
|
|
18
|
+
maxDecodedPixels?: number;
|
|
19
|
+
maxTargetDimension?: number;
|
|
20
|
+
cacheBytes?: number;
|
|
21
|
+
concurrency?: number;
|
|
22
|
+
timeoutMs?: number;
|
|
23
|
+
}
|
|
24
|
+
export declare class DefaultImageLoader {
|
|
25
|
+
static readonly shared: DefaultImageLoader;
|
|
26
|
+
private readonly options;
|
|
27
|
+
private readonly cache;
|
|
28
|
+
private bytes;
|
|
29
|
+
private active;
|
|
30
|
+
private readonly waiting;
|
|
31
|
+
constructor(options?: ImageLoaderOptions);
|
|
32
|
+
private acquire;
|
|
33
|
+
private resource;
|
|
34
|
+
private remove;
|
|
35
|
+
clearMemoryCache(): void;
|
|
36
|
+
readonly load: RemoteImageLoader;
|
|
37
|
+
}
|
|
38
|
+
export declare class MountedImage {
|
|
39
|
+
private readonly value;
|
|
40
|
+
private readonly changed;
|
|
41
|
+
private request?;
|
|
42
|
+
private resource?;
|
|
43
|
+
private generation;
|
|
44
|
+
private key;
|
|
45
|
+
private intersecting;
|
|
46
|
+
private visible;
|
|
47
|
+
private loader?;
|
|
48
|
+
private readonly observer;
|
|
49
|
+
private readonly status;
|
|
50
|
+
constructor(value: Mounted, changed: (success: boolean) => void);
|
|
51
|
+
private source;
|
|
52
|
+
private width;
|
|
53
|
+
private height;
|
|
54
|
+
update(source: string, loader: RemoteImageLoader, width: number, height: number, visible: boolean): void;
|
|
55
|
+
private cancel;
|
|
56
|
+
private sync;
|
|
57
|
+
dispose(): void;
|
|
58
|
+
}
|
|
59
|
+
export declare const imageCSS = "\n.relo-surface .relo-image-status{position:absolute;inset:0;display:flex;align-items:center;justify-content:center;background:var(--relo-input-background);color:var(--relo-input-border);font-size:.8em;pointer-events:none}\n.relo-surface .relo-image-status[hidden],.relo-surface img[hidden]{display:none!important}\n";
|
|
@@ -0,0 +1,406 @@
|
|
|
1
|
+
import { fail } from '../core/value.js';
|
|
2
|
+
function abort() {
|
|
3
|
+
return new DOMException('Image request cancelled', 'AbortError');
|
|
4
|
+
}
|
|
5
|
+
function releaseResource(resource) {
|
|
6
|
+
try {
|
|
7
|
+
resource?.release();
|
|
8
|
+
}
|
|
9
|
+
catch {
|
|
10
|
+
// Host cleanup must not interrupt renderer disposal or the next request.
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
function raster(bytes) {
|
|
14
|
+
const ascii = (a, b) => String.fromCharCode(...bytes.subarray(a, b));
|
|
15
|
+
return ((bytes[0] === 137 && ascii(1, 4) === 'PNG') ||
|
|
16
|
+
(bytes[0] === 255 && bytes[1] === 216 && bytes[2] === 255) ||
|
|
17
|
+
/^GIF8[79]a$/.test(ascii(0, 6)) ||
|
|
18
|
+
(ascii(0, 4) === 'RIFF' && ascii(8, 12) === 'WEBP') ||
|
|
19
|
+
(ascii(4, 8) === 'ftyp' && /avif|avis/.test(ascii(8, 32))) ||
|
|
20
|
+
ascii(0, 2) === 'BM' ||
|
|
21
|
+
(bytes[0] === 0 && bytes[1] === 0 && [1, 2].includes(bytes[2]) && bytes[3] === 0));
|
|
22
|
+
}
|
|
23
|
+
export class DefaultImageLoader {
|
|
24
|
+
static shared = new DefaultImageLoader();
|
|
25
|
+
options;
|
|
26
|
+
cache = new Map();
|
|
27
|
+
bytes = 0;
|
|
28
|
+
active = 0;
|
|
29
|
+
waiting = new Set();
|
|
30
|
+
constructor(options = {}) {
|
|
31
|
+
if (Object.keys(options).some((key) => ![
|
|
32
|
+
'maxResponseBytes',
|
|
33
|
+
'maxSourcePixels',
|
|
34
|
+
'maxDecodedPixels',
|
|
35
|
+
'maxTargetDimension',
|
|
36
|
+
'cacheBytes',
|
|
37
|
+
'concurrency',
|
|
38
|
+
'timeoutMs',
|
|
39
|
+
].includes(key)))
|
|
40
|
+
fail('Unknown image loader limit');
|
|
41
|
+
this.options = {
|
|
42
|
+
maxResponseBytes: 8 * 1024 * 1024,
|
|
43
|
+
maxSourcePixels: 64_000_000,
|
|
44
|
+
maxDecodedPixels: 4_000_000,
|
|
45
|
+
maxTargetDimension: 2048,
|
|
46
|
+
cacheBytes: 16 * 1024 * 1024,
|
|
47
|
+
concurrency: 4,
|
|
48
|
+
timeoutMs: 5000,
|
|
49
|
+
...options,
|
|
50
|
+
};
|
|
51
|
+
for (const [key, value] of Object.entries(this.options))
|
|
52
|
+
if (!Number.isSafeInteger(value) ||
|
|
53
|
+
value < (key === 'cacheBytes' ? 0 : 1) ||
|
|
54
|
+
value >
|
|
55
|
+
(key === 'concurrency'
|
|
56
|
+
? 16
|
|
57
|
+
: key === 'maxTargetDimension'
|
|
58
|
+
? 8192
|
|
59
|
+
: key === 'timeoutMs'
|
|
60
|
+
? 60_000
|
|
61
|
+
: 128 * 1024 * 1024))
|
|
62
|
+
fail('Invalid image loader limit');
|
|
63
|
+
}
|
|
64
|
+
acquire(signal) {
|
|
65
|
+
return new Promise((resolve, reject) => {
|
|
66
|
+
const cancel = () => {
|
|
67
|
+
this.waiting.delete(ready);
|
|
68
|
+
reject(abort());
|
|
69
|
+
};
|
|
70
|
+
const ready = () => {
|
|
71
|
+
if (signal.aborted) {
|
|
72
|
+
cancel();
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
if (this.active >= this.options.concurrency)
|
|
76
|
+
return;
|
|
77
|
+
this.waiting.delete(ready);
|
|
78
|
+
signal.removeEventListener('abort', cancel);
|
|
79
|
+
this.active++;
|
|
80
|
+
let released = false;
|
|
81
|
+
resolve(() => {
|
|
82
|
+
if (released)
|
|
83
|
+
return;
|
|
84
|
+
released = true;
|
|
85
|
+
this.active--;
|
|
86
|
+
for (const next of this.waiting)
|
|
87
|
+
next();
|
|
88
|
+
});
|
|
89
|
+
};
|
|
90
|
+
signal.addEventListener('abort', cancel, { once: true });
|
|
91
|
+
this.waiting.add(ready);
|
|
92
|
+
ready();
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
resource(entry) {
|
|
96
|
+
entry.references++;
|
|
97
|
+
let released = false;
|
|
98
|
+
return {
|
|
99
|
+
url: entry.url,
|
|
100
|
+
width: entry.width,
|
|
101
|
+
height: entry.height,
|
|
102
|
+
release: () => {
|
|
103
|
+
if (released)
|
|
104
|
+
return;
|
|
105
|
+
released = true;
|
|
106
|
+
entry.references--;
|
|
107
|
+
if (!entry.cached && !entry.references)
|
|
108
|
+
URL.revokeObjectURL(entry.url);
|
|
109
|
+
},
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
remove(key, entry) {
|
|
113
|
+
this.cache.delete(key);
|
|
114
|
+
this.bytes -= entry.bytes;
|
|
115
|
+
entry.cached = false;
|
|
116
|
+
if (!entry.references)
|
|
117
|
+
URL.revokeObjectURL(entry.url);
|
|
118
|
+
}
|
|
119
|
+
clearMemoryCache() {
|
|
120
|
+
for (const [key, entry] of this.cache)
|
|
121
|
+
this.remove(key, entry);
|
|
122
|
+
}
|
|
123
|
+
load = async (request) => {
|
|
124
|
+
const { source, width, height } = request;
|
|
125
|
+
if (!Number.isFinite(width) || !Number.isFinite(height) || width <= 0 || height <= 0)
|
|
126
|
+
fail('Invalid image size');
|
|
127
|
+
const original = new URL(source);
|
|
128
|
+
if (!['http:', 'https:'].includes(original.protocol))
|
|
129
|
+
fail('Invalid image protocol');
|
|
130
|
+
const targetWidth = Math.min(this.options.maxTargetDimension, Math.max(1, Math.ceil(width))), targetHeight = Math.min(this.options.maxTargetDimension, Math.max(1, Math.ceil(height))), key = `${source}|${targetWidth}|${targetHeight}`;
|
|
131
|
+
request.signal.throwIfAborted();
|
|
132
|
+
const cached = this.cache.get(key);
|
|
133
|
+
if (cached) {
|
|
134
|
+
this.cache.delete(key);
|
|
135
|
+
this.cache.set(key, cached);
|
|
136
|
+
return this.resource(cached);
|
|
137
|
+
}
|
|
138
|
+
const signal = AbortSignal.any([request.signal, AbortSignal.timeout(this.options.timeoutMs)]), release = await this.acquire(signal);
|
|
139
|
+
try {
|
|
140
|
+
const response = await fetch(original, { signal, credentials: 'omit', redirect: 'follow' });
|
|
141
|
+
if (!response.ok ||
|
|
142
|
+
response.type === 'opaque' ||
|
|
143
|
+
(original.protocol === 'https:' && new URL(response.url).protocol !== 'https:'))
|
|
144
|
+
fail('Image unavailable');
|
|
145
|
+
const declared = Number(response.headers.get('content-length'));
|
|
146
|
+
if (declared > this.options.maxResponseBytes)
|
|
147
|
+
fail('Image response limit');
|
|
148
|
+
const reader = response.body?.getReader();
|
|
149
|
+
if (!reader)
|
|
150
|
+
fail('Image body unavailable');
|
|
151
|
+
const chunks = [];
|
|
152
|
+
let count = 0;
|
|
153
|
+
try {
|
|
154
|
+
while (true) {
|
|
155
|
+
const { done, value } = await reader.read();
|
|
156
|
+
if (done)
|
|
157
|
+
break;
|
|
158
|
+
count += value.length;
|
|
159
|
+
if (count > this.options.maxResponseBytes) {
|
|
160
|
+
await reader.cancel();
|
|
161
|
+
fail('Image response limit');
|
|
162
|
+
}
|
|
163
|
+
chunks.push(value);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
finally {
|
|
167
|
+
reader.releaseLock();
|
|
168
|
+
}
|
|
169
|
+
const first = new Uint8Array(Math.min(count, 32));
|
|
170
|
+
let cursor = 0;
|
|
171
|
+
for (const chunk of chunks) {
|
|
172
|
+
const piece = chunk.subarray(0, first.length - cursor);
|
|
173
|
+
first.set(piece, cursor);
|
|
174
|
+
cursor += piece.length;
|
|
175
|
+
if (cursor === first.length)
|
|
176
|
+
break;
|
|
177
|
+
}
|
|
178
|
+
if (!raster(first))
|
|
179
|
+
fail('Unsupported raster image');
|
|
180
|
+
const blob = new Blob(chunks, {
|
|
181
|
+
type: response.headers.get('content-type') ?? 'application/octet-stream',
|
|
182
|
+
}), temporary = URL.createObjectURL(blob), image = new Image();
|
|
183
|
+
try {
|
|
184
|
+
await new Promise((resolve, reject) => {
|
|
185
|
+
const cancel = () => {
|
|
186
|
+
image.src = '';
|
|
187
|
+
reject(abort());
|
|
188
|
+
};
|
|
189
|
+
signal.addEventListener('abort', cancel, { once: true });
|
|
190
|
+
image.onload = () => {
|
|
191
|
+
signal.removeEventListener('abort', cancel);
|
|
192
|
+
resolve();
|
|
193
|
+
};
|
|
194
|
+
image.onerror = () => {
|
|
195
|
+
signal.removeEventListener('abort', cancel);
|
|
196
|
+
reject(new Error('Image decode failed'));
|
|
197
|
+
};
|
|
198
|
+
image.src = temporary;
|
|
199
|
+
if (signal.aborted)
|
|
200
|
+
cancel();
|
|
201
|
+
});
|
|
202
|
+
signal.throwIfAborted();
|
|
203
|
+
const sw = image.naturalWidth, sh = image.naturalHeight;
|
|
204
|
+
if (sw < 1 || sh < 1 || sw > 32768 || sh > 32768 || sw * sh > this.options.maxSourcePixels)
|
|
205
|
+
fail('Image dimension limit');
|
|
206
|
+
const scale = Math.min(1, Math.max(targetWidth / sw, targetHeight / sh), this.options.maxTargetDimension / sw, this.options.maxTargetDimension / sh, Math.sqrt(this.options.maxDecodedPixels / (sw * sh))), w = Math.max(1, Math.floor(sw * scale)), h = Math.max(1, Math.floor(sh * scale));
|
|
207
|
+
const canvas = document.createElement('canvas');
|
|
208
|
+
canvas.width = w;
|
|
209
|
+
canvas.height = h;
|
|
210
|
+
const context = canvas.getContext('2d');
|
|
211
|
+
if (!context)
|
|
212
|
+
fail('Image canvas unavailable');
|
|
213
|
+
const bitmap = typeof createImageBitmap === 'function'
|
|
214
|
+
? await createImageBitmap(blob, {
|
|
215
|
+
resizeWidth: w,
|
|
216
|
+
resizeHeight: h,
|
|
217
|
+
resizeQuality: 'high',
|
|
218
|
+
})
|
|
219
|
+
: undefined;
|
|
220
|
+
try {
|
|
221
|
+
context.drawImage(bitmap ?? image, 0, 0, w, h);
|
|
222
|
+
}
|
|
223
|
+
finally {
|
|
224
|
+
bitmap?.close();
|
|
225
|
+
}
|
|
226
|
+
const decoded = await new Promise((resolve, reject) => canvas.toBlob((blob) => (blob ? resolve(blob) : reject(new Error('Image encode failed'))), 'image/png'));
|
|
227
|
+
canvas.width = canvas.height = 0;
|
|
228
|
+
signal.throwIfAborted();
|
|
229
|
+
const entry = {
|
|
230
|
+
url: URL.createObjectURL(decoded),
|
|
231
|
+
width: w,
|
|
232
|
+
height: h,
|
|
233
|
+
bytes: w * h * 4,
|
|
234
|
+
references: 0,
|
|
235
|
+
cached: false,
|
|
236
|
+
};
|
|
237
|
+
if (entry.bytes <= this.options.cacheBytes) {
|
|
238
|
+
while ((this.bytes + entry.bytes > this.options.cacheBytes || this.cache.size >= 256) &&
|
|
239
|
+
this.cache.size) {
|
|
240
|
+
const key = this.cache.keys().next().value, old = this.cache.get(key);
|
|
241
|
+
this.remove(key, old);
|
|
242
|
+
}
|
|
243
|
+
const replaced = this.cache.get(key);
|
|
244
|
+
if (replaced)
|
|
245
|
+
this.remove(key, replaced);
|
|
246
|
+
entry.cached = true;
|
|
247
|
+
this.cache.set(key, entry);
|
|
248
|
+
this.bytes += entry.bytes;
|
|
249
|
+
}
|
|
250
|
+
return this.resource(entry);
|
|
251
|
+
}
|
|
252
|
+
finally {
|
|
253
|
+
image.src = '';
|
|
254
|
+
URL.revokeObjectURL(temporary);
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
finally {
|
|
258
|
+
release();
|
|
259
|
+
}
|
|
260
|
+
};
|
|
261
|
+
}
|
|
262
|
+
export class MountedImage {
|
|
263
|
+
value;
|
|
264
|
+
changed;
|
|
265
|
+
request;
|
|
266
|
+
resource;
|
|
267
|
+
generation = 0;
|
|
268
|
+
key = '';
|
|
269
|
+
intersecting = false;
|
|
270
|
+
visible = true;
|
|
271
|
+
loader;
|
|
272
|
+
observer;
|
|
273
|
+
status;
|
|
274
|
+
constructor(value, changed) {
|
|
275
|
+
this.value = value;
|
|
276
|
+
this.changed = changed;
|
|
277
|
+
const image = value.content;
|
|
278
|
+
image.loading = 'eager';
|
|
279
|
+
image.hidden = true;
|
|
280
|
+
this.status = document.createElement('div');
|
|
281
|
+
this.status.className = 'relo-image-status';
|
|
282
|
+
this.status.setAttribute('aria-hidden', 'true');
|
|
283
|
+
this.status.dataset.phase = 'loading';
|
|
284
|
+
value.element.append(this.status);
|
|
285
|
+
this.observer = new IntersectionObserver((entries) => {
|
|
286
|
+
this.intersecting = entries[0]?.isIntersecting ?? false;
|
|
287
|
+
this.sync();
|
|
288
|
+
});
|
|
289
|
+
this.observer.observe(value.element);
|
|
290
|
+
value.controller.signal.addEventListener('abort', () => this.dispose(), { once: true });
|
|
291
|
+
}
|
|
292
|
+
source = '';
|
|
293
|
+
width = 0;
|
|
294
|
+
height = 0;
|
|
295
|
+
update(source, loader, width, height, visible) {
|
|
296
|
+
this.source = source;
|
|
297
|
+
this.loader = loader;
|
|
298
|
+
this.width = Math.ceil(width * devicePixelRatio);
|
|
299
|
+
this.height = Math.ceil(height * devicePixelRatio);
|
|
300
|
+
this.visible = visible;
|
|
301
|
+
this.status.hidden =
|
|
302
|
+
this.value.node.props.placeholder === 'none' ||
|
|
303
|
+
!this.value.content.hidden;
|
|
304
|
+
this.sync();
|
|
305
|
+
}
|
|
306
|
+
cancel() {
|
|
307
|
+
this.request?.abort();
|
|
308
|
+
this.request = undefined;
|
|
309
|
+
this.generation++;
|
|
310
|
+
releaseResource(this.resource);
|
|
311
|
+
this.resource = undefined;
|
|
312
|
+
const image = this.value.content;
|
|
313
|
+
image.removeAttribute('src');
|
|
314
|
+
image.hidden = true;
|
|
315
|
+
this.key = '';
|
|
316
|
+
}
|
|
317
|
+
sync() {
|
|
318
|
+
if (this.value.controller.signal.aborted)
|
|
319
|
+
return;
|
|
320
|
+
if (!this.visible || !this.intersecting || document.hidden || !this.width || !this.height) {
|
|
321
|
+
this.cancel();
|
|
322
|
+
return;
|
|
323
|
+
}
|
|
324
|
+
const key = `${this.source}|${this.width}|${this.height}`;
|
|
325
|
+
if (key === this.key && this.loader === this.value.extra.imageLoader)
|
|
326
|
+
return;
|
|
327
|
+
this.cancel();
|
|
328
|
+
this.key = key;
|
|
329
|
+
this.value.extra.imageLoader = this.loader;
|
|
330
|
+
const request = new AbortController();
|
|
331
|
+
this.request = request;
|
|
332
|
+
const generation = this.generation;
|
|
333
|
+
this.status.hidden = this.value.node.props.placeholder === 'none';
|
|
334
|
+
this.status.dataset.phase = 'loading';
|
|
335
|
+
this.status.textContent = '';
|
|
336
|
+
void (async () => {
|
|
337
|
+
try {
|
|
338
|
+
const resource = await this.loader({
|
|
339
|
+
source: this.source,
|
|
340
|
+
width: this.width,
|
|
341
|
+
height: this.height,
|
|
342
|
+
signal: request.signal,
|
|
343
|
+
});
|
|
344
|
+
if (request.signal.aborted || generation !== this.generation) {
|
|
345
|
+
releaseResource(resource);
|
|
346
|
+
return;
|
|
347
|
+
}
|
|
348
|
+
if (resource && typeof resource.release === 'function')
|
|
349
|
+
this.resource = resource;
|
|
350
|
+
if (!resource ||
|
|
351
|
+
typeof resource.release !== 'function' ||
|
|
352
|
+
!Number.isFinite(resource.width) ||
|
|
353
|
+
!Number.isFinite(resource.height) ||
|
|
354
|
+
resource.width <= 0 ||
|
|
355
|
+
resource.height <= 0 ||
|
|
356
|
+
!['blob:', 'http:', 'https:', 'data:'].includes(new URL(resource.url, document.baseURI).protocol))
|
|
357
|
+
throw new Error('Invalid image resource');
|
|
358
|
+
this.resource = resource;
|
|
359
|
+
const image = this.value.content;
|
|
360
|
+
await new Promise((resolve, reject) => {
|
|
361
|
+
const cancel = () => {
|
|
362
|
+
image.onload = image.onerror = null;
|
|
363
|
+
reject(abort());
|
|
364
|
+
};
|
|
365
|
+
request.signal.addEventListener('abort', cancel, { once: true });
|
|
366
|
+
image.onload = () => {
|
|
367
|
+
request.signal.removeEventListener('abort', cancel);
|
|
368
|
+
resolve();
|
|
369
|
+
};
|
|
370
|
+
image.onerror = () => {
|
|
371
|
+
request.signal.removeEventListener('abort', cancel);
|
|
372
|
+
reject(new Error('Image unavailable'));
|
|
373
|
+
};
|
|
374
|
+
image.src = resource.url;
|
|
375
|
+
});
|
|
376
|
+
if (request.signal.aborted || generation !== this.generation)
|
|
377
|
+
return;
|
|
378
|
+
image.hidden = false;
|
|
379
|
+
this.status.hidden = true;
|
|
380
|
+
this.changed(true);
|
|
381
|
+
}
|
|
382
|
+
catch {
|
|
383
|
+
if (request.signal.aborted || generation !== this.generation)
|
|
384
|
+
return;
|
|
385
|
+
releaseResource(this.resource);
|
|
386
|
+
this.resource = undefined;
|
|
387
|
+
const image = this.value.content;
|
|
388
|
+
image.hidden = true;
|
|
389
|
+
image.removeAttribute('src');
|
|
390
|
+
this.status.hidden = this.value.node.props.placeholder === 'none';
|
|
391
|
+
this.status.dataset.phase = 'error';
|
|
392
|
+
this.status.textContent = 'Image unavailable';
|
|
393
|
+
this.changed(false);
|
|
394
|
+
}
|
|
395
|
+
})();
|
|
396
|
+
}
|
|
397
|
+
dispose() {
|
|
398
|
+
this.observer.disconnect();
|
|
399
|
+
this.cancel();
|
|
400
|
+
this.status.remove();
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
export const imageCSS = `
|
|
404
|
+
.relo-surface .relo-image-status{position:absolute;inset:0;display:flex;align-items:center;justify-content:center;background:var(--relo-input-background);color:var(--relo-input-border);font-size:.8em;pointer-events:none}
|
|
405
|
+
.relo-surface .relo-image-status[hidden],.relo-surface img[hidden]{display:none!important}
|
|
406
|
+
`;
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
export { RemoteSurface, supportedBuiltins, type MountOptions, type DomAdapter, } from './renderer.js';
|
|
2
|
+
export { type RemoteTheme } from './theme.js';
|
|
3
|
+
export { DefaultImageLoader, type RemoteImageLoader, type RemoteImageRequest, type RemoteImageResource, type ImageLoaderOptions, } from './images.js';
|