@buffcore/app-shell 0.8.44 → 0.8.46
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.
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { prototypeNavigation, prototypeStudioOrigin } from './home-bridge.js';
|
|
2
|
+
import { partAnswer, partAt, prototypePartAsk } from './home-part.js';
|
|
2
3
|
import { prototypeShow } from './home-show.js';
|
|
3
4
|
/** Strict, small request schema; no actions or credentials, only an echoed document challenge. */
|
|
4
5
|
export function prototypeChallenge(event, expected) {
|
|
@@ -70,6 +71,12 @@ export function prototypeInbox() {
|
|
|
70
71
|
next.reply?.(nonce);
|
|
71
72
|
};
|
|
72
73
|
channel.port1.onmessage = (event) => {
|
|
74
|
+
const ask = prototypePartAsk(event.data, next);
|
|
75
|
+
if (ask && next.nonce) {
|
|
76
|
+
const expected = { navigation: next.navigation, nonce: next.nonce };
|
|
77
|
+
next.port.postMessage(partAnswer(expected, ask, partAt(ask.x, ask.y)));
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
73
80
|
const choice = prototypeShow(event.data, next);
|
|
74
81
|
if (choice)
|
|
75
82
|
next.show?.(choice);
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
export type PrototypePartAsk = {
|
|
2
|
+
ask: number;
|
|
3
|
+
x: number;
|
|
4
|
+
y: number;
|
|
5
|
+
};
|
|
6
|
+
export type PrototypePart = {
|
|
7
|
+
name?: string;
|
|
8
|
+
box: {
|
|
9
|
+
x: number;
|
|
10
|
+
y: number;
|
|
11
|
+
w: number;
|
|
12
|
+
h: number;
|
|
13
|
+
};
|
|
14
|
+
};
|
|
15
|
+
type Point = {
|
|
16
|
+
x: number;
|
|
17
|
+
y: number;
|
|
18
|
+
};
|
|
19
|
+
/**
|
|
20
|
+
* How far the page and its tallest scroller (the box a screen's content scrolls in) are scrolled,
|
|
21
|
+
* so a picture of this screen can be scrolled the same way. The studio's picture finds the scroller
|
|
22
|
+
* by the same rule.
|
|
23
|
+
*/
|
|
24
|
+
export declare function scrolled(page?: Document): {
|
|
25
|
+
page: Point;
|
|
26
|
+
inner: Point;
|
|
27
|
+
};
|
|
28
|
+
/** A name is one short line: a label longer than this is not a part's name. */
|
|
29
|
+
export declare const PART_NAME = 80;
|
|
30
|
+
/**
|
|
31
|
+
* One part ask, read strictly: it arrives only on this document's private port (the studio's end
|
|
32
|
+
* of the hello), names the current navigation and document nonce, and carries an ask number and a
|
|
33
|
+
* point in the page. It grants nothing: the app only reads which element is drawn there.
|
|
34
|
+
*/
|
|
35
|
+
export declare function prototypePartAsk(data: unknown, expected: {
|
|
36
|
+
navigation: string;
|
|
37
|
+
nonce?: string;
|
|
38
|
+
}): PrototypePartAsk | undefined;
|
|
39
|
+
/** The nearest element at the point with a name, with its box; the element itself without one. */
|
|
40
|
+
export declare function partAt(x: number, y: number, page?: Document): PrototypePart | undefined;
|
|
41
|
+
/** The answer to one ask: the ask's number, the part's name and box, and how far it is scrolled. */
|
|
42
|
+
export declare function partAnswer(expected: {
|
|
43
|
+
navigation: string;
|
|
44
|
+
nonce: string;
|
|
45
|
+
}, ask: PrototypePartAsk, part: PrototypePart | undefined, scroll?: {
|
|
46
|
+
page: Point;
|
|
47
|
+
inner: Point;
|
|
48
|
+
}): string;
|
|
49
|
+
export {};
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
// The studio's marking asks which part of the app is at a point, to name a comment ("Comment on
|
|
2
|
+
// Price"). Development only, like the rest of the prototype home. The answer carries the part's
|
|
3
|
+
// name (its accessible label or test id), its box and the scroll, never the app's data.
|
|
4
|
+
/**
|
|
5
|
+
* How far the page and its tallest scroller (the box a screen's content scrolls in) are scrolled,
|
|
6
|
+
* so a picture of this screen can be scrolled the same way. The studio's picture finds the scroller
|
|
7
|
+
* by the same rule.
|
|
8
|
+
*/
|
|
9
|
+
export function scrolled(page = document) {
|
|
10
|
+
const view = page.defaultView;
|
|
11
|
+
let inner;
|
|
12
|
+
let most = 0;
|
|
13
|
+
const all = page.body?.querySelectorAll('*') ?? [];
|
|
14
|
+
for (let i = 0; i < all.length && i < 20_000; i++) {
|
|
15
|
+
const el = all[i];
|
|
16
|
+
const room = el.scrollHeight - el.clientHeight;
|
|
17
|
+
if (room > most && /auto|scroll|overlay/.test(view?.getComputedStyle(el).overflowY ?? '')) {
|
|
18
|
+
most = room;
|
|
19
|
+
inner = el;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
const whole = (value = 0) => Math.max(0, Math.round(value));
|
|
23
|
+
return {
|
|
24
|
+
page: { x: whole(view?.scrollX), y: whole(view?.scrollY) },
|
|
25
|
+
inner: { x: whole(inner?.scrollLeft), y: whole(inner?.scrollTop) },
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
const allowed = new Set(['type', 'version', 'navigation', 'document', 'ask', 'x', 'y']);
|
|
29
|
+
/** A name is one short line: a label longer than this is not a part's name. */
|
|
30
|
+
export const PART_NAME = 80;
|
|
31
|
+
const coordinate = (value) => typeof value === 'number' && Number.isFinite(value) && value >= 0 && value <= 100_000;
|
|
32
|
+
/**
|
|
33
|
+
* One part ask, read strictly: it arrives only on this document's private port (the studio's end
|
|
34
|
+
* of the hello), names the current navigation and document nonce, and carries an ask number and a
|
|
35
|
+
* point in the page. It grants nothing: the app only reads which element is drawn there.
|
|
36
|
+
*/
|
|
37
|
+
export function prototypePartAsk(data, expected) {
|
|
38
|
+
if (typeof data !== 'string' || data.length > 512 || !expected.nonce)
|
|
39
|
+
return;
|
|
40
|
+
let value;
|
|
41
|
+
try {
|
|
42
|
+
value = JSON.parse(data);
|
|
43
|
+
}
|
|
44
|
+
catch {
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
if (!value ||
|
|
48
|
+
typeof value !== 'object' ||
|
|
49
|
+
Array.isArray(value) ||
|
|
50
|
+
Object.keys(value).some((key) => !allowed.has(key)) ||
|
|
51
|
+
value.type !== 'ape:prototype:part' ||
|
|
52
|
+
value.version !== 2 ||
|
|
53
|
+
value.navigation !== expected.navigation ||
|
|
54
|
+
value.document !== expected.nonce)
|
|
55
|
+
return;
|
|
56
|
+
const { ask, x, y } = value;
|
|
57
|
+
if (typeof ask !== 'number' || !Number.isInteger(ask) || ask < 1 || ask > 1_000_000)
|
|
58
|
+
return;
|
|
59
|
+
if (!coordinate(x) || !coordinate(y))
|
|
60
|
+
return;
|
|
61
|
+
return { ask, x, y };
|
|
62
|
+
}
|
|
63
|
+
/** The part's own name: its accessible label, else its test id in words; else none. */
|
|
64
|
+
function nameOf(element) {
|
|
65
|
+
const label = element.getAttribute('aria-label') ?? '';
|
|
66
|
+
const id = element.getAttribute('data-testid') ?? '';
|
|
67
|
+
const words = (label || id.replace(/[-_.]+/g, ' ')).replace(/\s+/g, ' ').trim();
|
|
68
|
+
return words && words.length <= PART_NAME ? words : undefined;
|
|
69
|
+
}
|
|
70
|
+
/** The nearest element at the point with a name, with its box; the element itself without one. */
|
|
71
|
+
export function partAt(x, y, page = document) {
|
|
72
|
+
const hit = page.elementFromPoint(x, y);
|
|
73
|
+
if (!hit)
|
|
74
|
+
return;
|
|
75
|
+
let named = hit;
|
|
76
|
+
for (let depth = 0; named && depth < 12; depth++, named = named.parentElement) {
|
|
77
|
+
const name = nameOf(named);
|
|
78
|
+
if (name)
|
|
79
|
+
return { name, box: boxOf(named) };
|
|
80
|
+
}
|
|
81
|
+
return { box: boxOf(hit) };
|
|
82
|
+
}
|
|
83
|
+
function boxOf(element) {
|
|
84
|
+
const r = element.getBoundingClientRect();
|
|
85
|
+
const round = (value) => Math.max(0, Math.round(value));
|
|
86
|
+
return { x: round(r.left), y: round(r.top), w: round(r.width), h: round(r.height) };
|
|
87
|
+
}
|
|
88
|
+
/** The answer to one ask: the ask's number, the part's name and box, and how far it is scrolled. */
|
|
89
|
+
export function partAnswer(expected, ask, part, scroll = scrolled()) {
|
|
90
|
+
return JSON.stringify({
|
|
91
|
+
type: 'ape:prototype:spot',
|
|
92
|
+
version: 2,
|
|
93
|
+
navigation: expected.navigation,
|
|
94
|
+
document: expected.nonce,
|
|
95
|
+
ask: ask.ask,
|
|
96
|
+
...(part?.name ? { name: part.name } : {}),
|
|
97
|
+
box: part?.box ?? { x: Math.round(ask.x), y: Math.round(ask.y), w: 0, h: 0 },
|
|
98
|
+
scroll,
|
|
99
|
+
});
|
|
100
|
+
}
|
|
@@ -25,4 +25,4 @@ export interface SceneProps {
|
|
|
25
25
|
* The component for the app's `/_scene` route. Development only: the route loads it behind
|
|
26
26
|
* `__DEV__`, so production builds leave it and the sample data out.
|
|
27
27
|
*/
|
|
28
|
-
export declare function createScene({ scenes, views, catalogs, extensions }: SceneOptions): (props: SceneProps) => import("react").JSX.Element
|
|
28
|
+
export declare function createScene({ scenes, views, catalogs, extensions }: SceneOptions): (props: SceneProps) => import("react").JSX.Element;
|
package/dist/prototype/scene.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { SCENE_LOOK_ATTR, SCENE_LOOK_CHANGE, SCENE_LOOK_REUSABLE, } from '@buffcore/contracts/prototype';
|
|
2
3
|
import { createI18n, isRtl } from '@buffcore/i18n';
|
|
3
4
|
import { I18nProvider } from '@buffcore/i18n/react';
|
|
4
5
|
import { DirectionProvider } from '@buffcore/ui';
|
|
@@ -28,7 +29,7 @@ export function createScene({ scenes, views, catalogs = [], extensions = [] }) {
|
|
|
28
29
|
const names = missing.map((s) => `"${s.key}" (${s.view})`).join(', ');
|
|
29
30
|
throw new Error(`No app view for the scenes ${names}: add them to createScene's views.`);
|
|
30
31
|
}
|
|
31
|
-
|
|
32
|
+
function SceneLook({ props, search }) {
|
|
32
33
|
const { scene: key, theme, mode = 'light', locale = 'en', scale: asked } = props;
|
|
33
34
|
const [themeReady, setThemeReady] = useState(false);
|
|
34
35
|
const translator = useMemo(() => createI18n({ catalogs: [shellCatalog, ...strings], pseudo: true, locale }), [locale]);
|
|
@@ -37,6 +38,7 @@ export function createScene({ scenes, views, catalogs = [], extensions = [] }) {
|
|
|
37
38
|
const name = themeName(look.key, resolveThemeMode(mode, look.modes));
|
|
38
39
|
const dir = isRtl(locale) ? 'rtl' : 'ltr';
|
|
39
40
|
const found = key ? own.find((s) => s.key === key) : preview;
|
|
41
|
+
const View = found && views[found.view];
|
|
40
42
|
// The scale the URL asks for, else the scene's own, else 1.
|
|
41
43
|
const scale = Number(asked) > 0 ? Number(asked) : (found?.scale ?? 1);
|
|
42
44
|
// Apply the theme before mounting the view, and before paint on later switches.
|
|
@@ -52,13 +54,50 @@ export function createScene({ scenes, views, catalogs = [], extensions = [] }) {
|
|
|
52
54
|
document.documentElement.lang = locale;
|
|
53
55
|
document.documentElement.dir = dir;
|
|
54
56
|
}, [locale, dir]);
|
|
55
|
-
|
|
57
|
+
useLayoutEffect(() => {
|
|
58
|
+
if (!themeReady || Platform.OS !== 'web')
|
|
59
|
+
return;
|
|
60
|
+
const html = document.documentElement;
|
|
61
|
+
html.setAttribute(SCENE_LOOK_ATTR, search ?? window.location.search);
|
|
62
|
+
// Any view can move in place: the snapshot checks each scene against a fresh load.
|
|
63
|
+
html.setAttribute(SCENE_LOOK_REUSABLE, '');
|
|
64
|
+
return () => {
|
|
65
|
+
html.removeAttribute(SCENE_LOOK_ATTR);
|
|
66
|
+
html.removeAttribute(SCENE_LOOK_REUSABLE);
|
|
67
|
+
};
|
|
68
|
+
}, [themeReady, search]);
|
|
69
|
+
if (!found || !View)
|
|
56
70
|
throw new Error(key
|
|
57
71
|
? `No app scene "${key}" in prototype/scenes.ts.`
|
|
58
72
|
: 'No app scene to preview in prototype/scenes.ts.');
|
|
59
73
|
if (!themeReady)
|
|
60
74
|
return null;
|
|
61
|
-
const View = views[found.view];
|
|
62
75
|
return (_jsx(I18nProvider, { translator: translator, children: _jsx(DirectionProvider, { dir: dir, children: _jsx(View, { ...found.props }) }) }));
|
|
76
|
+
}
|
|
77
|
+
return function Scene(props) {
|
|
78
|
+
// The router supplies the initial props (also on native). A browser snapshot moves the URL
|
|
79
|
+
// directly so the router and the rest of the app stay mounted.
|
|
80
|
+
const [search, setSearch] = useState();
|
|
81
|
+
useLayoutEffect(() => {
|
|
82
|
+
if (Platform.OS !== 'web')
|
|
83
|
+
return;
|
|
84
|
+
const read = () => {
|
|
85
|
+
window.scrollTo({ top: 0, left: 0, behavior: 'instant' });
|
|
86
|
+
setSearch(window.location.search);
|
|
87
|
+
};
|
|
88
|
+
window.addEventListener(SCENE_LOOK_CHANGE, read);
|
|
89
|
+
return () => window.removeEventListener(SCENE_LOOK_CHANGE, read);
|
|
90
|
+
}, []);
|
|
91
|
+
return (_jsx(SceneLook, { props: search === undefined ? props : urlProps(search), search: search }, search));
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
function urlProps(search) {
|
|
95
|
+
const params = new URLSearchParams(search);
|
|
96
|
+
return {
|
|
97
|
+
scene: params.get('key') ?? undefined,
|
|
98
|
+
theme: params.get('theme') ?? undefined,
|
|
99
|
+
mode: params.get('mode') ?? undefined,
|
|
100
|
+
locale: params.get('locale') ?? undefined,
|
|
101
|
+
scale: params.get('scale') ?? undefined,
|
|
63
102
|
};
|
|
64
103
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@buffcore/app-shell",
|
|
3
|
-
"version": "0.8.
|
|
3
|
+
"version": "0.8.46",
|
|
4
4
|
"description": "The Expo app shell for a BuffCore product: sign-in, settings, themes, live sessions and push.",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"author": "BuffApe",
|
|
@@ -33,12 +33,12 @@
|
|
|
33
33
|
"dependencies": {
|
|
34
34
|
"@noble/hashes": "2.4.0",
|
|
35
35
|
"firebase": "12.19.0",
|
|
36
|
-
"@buffcore/
|
|
37
|
-
"@buffcore/
|
|
38
|
-
"@buffcore/
|
|
39
|
-
"@buffcore/sdk": "0.8.
|
|
40
|
-
"@buffcore/ui": "0.8.
|
|
41
|
-
"@buffcore/tokens": "0.8.
|
|
36
|
+
"@buffcore/i18n": "0.8.46",
|
|
37
|
+
"@buffcore/config": "0.8.46",
|
|
38
|
+
"@buffcore/contracts": "0.8.46",
|
|
39
|
+
"@buffcore/sdk": "0.8.46",
|
|
40
|
+
"@buffcore/ui": "0.8.46",
|
|
41
|
+
"@buffcore/tokens": "0.8.46"
|
|
42
42
|
},
|
|
43
43
|
"peerDependencies": {
|
|
44
44
|
"@react-native-firebase/app": ">=26",
|
|
@@ -116,8 +116,8 @@
|
|
|
116
116
|
"react-native-web": "0.21.4",
|
|
117
117
|
"react-native-webview": "13.16.1",
|
|
118
118
|
"zod": "4.6.5",
|
|
119
|
-
"@buffcore/modules": "0.8.
|
|
120
|
-
"@buffcore/server": "0.8.
|
|
119
|
+
"@buffcore/modules": "0.8.46",
|
|
120
|
+
"@buffcore/server": "0.8.46"
|
|
121
121
|
},
|
|
122
122
|
"scripts": {
|
|
123
123
|
"build": "node ../../scripts/build-package.mjs",
|