@ng-native/device 0.0.1 → 0.1.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/LICENSE +21 -0
- package/README.md +72 -1
- package/dist/accessibility.d.ts +60 -0
- package/dist/accessibility.js +101 -0
- package/dist/accessibility.js.map +1 -0
- package/dist/android-permissions.d.ts +31 -0
- package/dist/android-permissions.js +51 -0
- package/dist/android-permissions.js.map +1 -0
- package/dist/app-state.d.ts +27 -0
- package/dist/app-state.js +45 -0
- package/dist/app-state.js.map +1 -0
- package/dist/color-scheme.d.ts +33 -0
- package/dist/color-scheme.js +50 -0
- package/dist/color-scheme.js.map +1 -0
- package/dist/conditions.d.ts +48 -0
- package/dist/conditions.js +91 -0
- package/dist/conditions.js.map +1 -0
- package/dist/deep-links.d.ts +35 -0
- package/dist/deep-links.js +119 -0
- package/dist/deep-links.js.map +1 -0
- package/dist/dev-menu.d.ts +48 -0
- package/dist/dev-menu.js +52 -0
- package/dist/dev-menu.js.map +1 -0
- package/dist/dialogs.d.ts +111 -0
- package/dist/dialogs.js +186 -0
- package/dist/dialogs.js.map +1 -0
- package/dist/direction.d.ts +49 -0
- package/dist/direction.js +57 -0
- package/dist/direction.js.map +1 -0
- package/dist/hardware-back.d.ts +27 -0
- package/dist/hardware-back.js +41 -0
- package/dist/hardware-back.js.map +1 -0
- package/dist/index.d.ts +26 -0
- package/dist/index.js +27 -0
- package/dist/index.js.map +1 -0
- package/dist/keyboard.d.ts +67 -0
- package/dist/keyboard.js +103 -0
- package/dist/keyboard.js.map +1 -0
- package/dist/layout-animation.d.ts +54 -0
- package/dist/layout-animation.js +75 -0
- package/dist/layout-animation.js.map +1 -0
- package/dist/react-native.d.ts +176 -0
- package/dist/react-native.js +38 -0
- package/dist/react-native.js.map +1 -0
- package/dist/safe-area.d.ts +52 -0
- package/dist/safe-area.js +51 -0
- package/dist/safe-area.js.map +1 -0
- package/dist/screen-in-front.d.ts +10 -0
- package/dist/screen-in-front.js +14 -0
- package/dist/screen-in-front.js.map +1 -0
- package/dist/screen.d.ts +78 -0
- package/dist/screen.js +90 -0
- package/dist/screen.js.map +1 -0
- package/dist/sharing.d.ts +37 -0
- package/dist/sharing.js +61 -0
- package/dist/sharing.js.map +1 -0
- package/dist/status-bar.d.ts +69 -0
- package/dist/status-bar.js +101 -0
- package/dist/status-bar.js.map +1 -0
- package/dist/vibration.d.ts +38 -0
- package/dist/vibration.js +43 -0
- package/dist/vibration.js.map +1 -0
- package/package.json +59 -3
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { accessibilitySource } from "./accessibility.js";
|
|
2
|
+
import { colorSchemeSource } from "./color-scheme.js";
|
|
3
|
+
import { screenSource } from "./screen.js";
|
|
4
|
+
import { reactNative } from "./react-native.js";
|
|
5
|
+
/** The system text size, as a multiplier. One off a device. */
|
|
6
|
+
function fontScale() {
|
|
7
|
+
return reactNative()?.PixelRatio.getFontScale() ?? 1;
|
|
8
|
+
}
|
|
9
|
+
export function currentConditions() {
|
|
10
|
+
const { window } = screenSource().current();
|
|
11
|
+
return {
|
|
12
|
+
width: window.width,
|
|
13
|
+
height: window.height,
|
|
14
|
+
colorScheme: colorSchemeSource().current(),
|
|
15
|
+
// Asynchronous on both platforms, so the first paint says no preference and `watchConditions`
|
|
16
|
+
// corrects it. Reduced motion is a preference about what happens next, not about the frame
|
|
17
|
+
// already on screen, so nothing is lost by learning it a tick late.
|
|
18
|
+
reducedMotion: false,
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Custom properties the device settles before the first frame, for `mount`'s `tokens` option.
|
|
23
|
+
*
|
|
24
|
+
* Only the hairline so far: the thinnest line the screen can draw, which is a third of a point on
|
|
25
|
+
* a 3x phone and half of one on a 2x. `1px` is what a browser would give and is a visibly fat
|
|
26
|
+
* divider on a modern screen, so a native-looking rule has to ask. React Native works the number
|
|
27
|
+
* out from the pixel ratio; off a device there is no answer, and inventing one would be a line of
|
|
28
|
+
* the wrong weight in every test that draws one.
|
|
29
|
+
*
|
|
30
|
+
* Not watched, unlike the conditions: the pixel density of a screen does not change while an app
|
|
31
|
+
* is open. The safe-area insets are the opposite case and arrive through `updateTokens`.
|
|
32
|
+
*/
|
|
33
|
+
export function deviceTokens() {
|
|
34
|
+
const native = reactNative();
|
|
35
|
+
return native ? { '--hairline': { length: native.StyleSheet.hairlineWidth } } : {};
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Keep the engine's conditions current. Returns an unsubscribe, though an app that calls this at
|
|
39
|
+
* bootstrap will never use it.
|
|
40
|
+
*/
|
|
41
|
+
export function watchConditions(engine, options = {}) {
|
|
42
|
+
const screen = screenSource();
|
|
43
|
+
const colors = colorSchemeSource();
|
|
44
|
+
const accessibility = accessibilitySource();
|
|
45
|
+
let conditions = currentConditions();
|
|
46
|
+
const push = (change) => {
|
|
47
|
+
conditions = { ...conditions, ...change };
|
|
48
|
+
if (options.darkClass !== false) {
|
|
49
|
+
if (conditions.colorScheme === 'dark')
|
|
50
|
+
engine.addClass(engine.root, 'dark');
|
|
51
|
+
else
|
|
52
|
+
engine.removeClass(engine.root, 'dark');
|
|
53
|
+
}
|
|
54
|
+
engine.updateConditions(conditions);
|
|
55
|
+
};
|
|
56
|
+
// The class for the scheme the app started in, committed at once. Light needs nothing: an app
|
|
57
|
+
// starting light is exactly what it already rendered.
|
|
58
|
+
if (options.darkClass !== false && conditions.colorScheme === 'dark')
|
|
59
|
+
push({});
|
|
60
|
+
// The system text size. Every text on screen was measured at the old one, and keeps that box
|
|
61
|
+
// until it is measured again, so a change re-measures them all before anything else commits.
|
|
62
|
+
// A change arrives as a `Dimensions` change on both platforms; the app coming back to the
|
|
63
|
+
// foreground is checked as well, and the same change reported by both is re-measured once.
|
|
64
|
+
let scale = fontScale();
|
|
65
|
+
const rescale = (next) => {
|
|
66
|
+
if (next === scale)
|
|
67
|
+
return;
|
|
68
|
+
scale = next;
|
|
69
|
+
engine.remeasureText?.();
|
|
70
|
+
};
|
|
71
|
+
const sizes = screen.subscribe(({ window }) => {
|
|
72
|
+
rescale(fontScale());
|
|
73
|
+
push({ width: window.width, height: window.height });
|
|
74
|
+
});
|
|
75
|
+
const scheme = colors.subscribe((colorScheme) => push({ colorScheme }));
|
|
76
|
+
const settings = accessibility.subscribe((change) => {
|
|
77
|
+
if (change.fontScale !== undefined)
|
|
78
|
+
rescale(change.fontScale);
|
|
79
|
+
if (change.reduceMotion !== undefined)
|
|
80
|
+
push({ reducedMotion: change.reduceMotion });
|
|
81
|
+
});
|
|
82
|
+
void accessibility
|
|
83
|
+
.current()
|
|
84
|
+
.then(({ reduceMotion }) => reduceMotion && push({ reducedMotion: true }));
|
|
85
|
+
return () => {
|
|
86
|
+
sizes();
|
|
87
|
+
scheme();
|
|
88
|
+
settings();
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
//# sourceMappingURL=conditions.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"conditions.js","sourceRoot":"","sources":["../src/conditions.ts"],"names":[],"mappings":"AAWA,OAAO,EAAE,mBAAmB,EAAE,MAAM,oBAAoB,CAAC;AACzD,OAAO,EAAE,iBAAiB,EAAE,MAAM,mBAAmB,CAAC;AACtD,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAC3C,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAYhD,+DAA+D;AAC/D,SAAS,SAAS;IAChB,OAAO,WAAW,EAAE,EAAE,UAAU,CAAC,YAAY,EAAE,IAAI,CAAC,CAAC;AACvD,CAAC;AAWD,MAAM,UAAU,iBAAiB;IAC/B,MAAM,EAAE,MAAM,EAAE,GAAG,YAAY,EAAE,CAAC,OAAO,EAAE,CAAC;IAC5C,OAAO;QACL,KAAK,EAAE,MAAM,CAAC,KAAK;QACnB,MAAM,EAAE,MAAM,CAAC,MAAM;QACrB,WAAW,EAAE,iBAAiB,EAAE,CAAC,OAAO,EAAE;QAC1C,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ries against, kept in step with the device.\n *\n * Not a service, because the engine is not injectable and has no reason to be: it is handed to\n * `mount` and handed back, and this connects the two. An app that wants `@media` to keep working\n * after a rotation or a theme change calls `watchConditions` once and never thinks about it again.\n *\n * A rotation changes no component and no binding, so nothing would be dirty and nothing would\n * re-render. The engine re-resolves and commits on its own when the conditions change.\n */\nimport type { Conditions, TokenValue } from '@ng-native/fabric';\nimport { accessibilitySource } from './accessibility.ts';\nimport { colorSchemeSource } from './color-scheme.ts';\nimport { screenSource } from './screen.ts';\nimport { reactNative } from './react-native.ts';\n\n/** Just enough of the engine to be told. */\ninterface Resolves<Node = unknown> {\n readonly root: Node;\n updateConditions(conditions: Conditions): void;\n addClass(node: Node, name: string): void;\n removeClass(node: Node, name: string): void;\n /** Measure every text again, after the system text size changed. */\n remeasureText?(): void;\n}\n\n/** The system text size, as a multiplier. One off a device. */\nfunction fontScale(): number {\n return reactNative()?.PixelRatio.getFontScale() ?? 1;\n}\n\nexport interface WatchOptions {\n /**\n * Keep a `dark` class on the root in step with the system scheme, which is what\n * `@ng-native/tailwind`'s `dark:` variant matches. On by default; an app with its own theme\n * switch turns it off and puts `dark` on its own root view instead.\n */\n readonly darkClass?: boolean;\n}\n\nexport function currentConditions(): Conditions {\n const { window } = screenSource().current();\n return {\n width: window.width,\n height: window.height,\n colorScheme: colorSchemeSource().current(),\n // Asynchronous on both platforms, so the first paint says no preference and `watchConditions`\n // corrects it. Reduced motion is a preference about what happens next, not about the frame\n // already on screen, so nothing is lost by learning it a tick late.\n reducedMotion: false,\n };\n}\n\n/**\n * Custom properties the device settles before the first frame, for `mount`'s `tokens` option.\n *\n * Only the hairline so far: the thinnest line the screen can draw, which is a third of a point on\n * a 3x phone and half of one on a 2x. `1px` is what a browser would give and is a visibly fat\n * divider on a modern screen, so a native-looking rule has to ask. React Native works the number\n * out from the pixel ratio; off a device there is no answer, and inventing one would be a line of\n * the wrong weight in every test that draws one.\n *\n * Not watched, unlike the conditions: the pixel density of a screen does not change while an app\n * is open. The safe-area insets are the opposite case and arrive through `updateTokens`.\n */\nexport function deviceTokens(): Record<string, TokenValue> {\n const native = reactNative();\n return native ? { '--hairline': { length: native.StyleSheet.hairlineWidth } } : {};\n}\n\n/**\n * Keep the engine's conditions current. Returns an unsubscribe, though an app that calls this at\n * bootstrap will never use it.\n */\nexport function watchConditions<Node>(\n engine: Resolves<Node>,\n options: WatchOptions = {},\n): () => void {\n const screen = screenSource();\n const colors = colorSchemeSource();\n const accessibility = accessibilitySource();\n\n let conditions = currentConditions();\n const push = (change: Partial<Conditions>) => {\n conditions = { ...conditions, ...change };\n if (options.darkClass !== false) {\n if (conditions.colorScheme === 'dark') engine.addClass(engine.root, 'dark');\n else engine.removeClass(engine.root, 'dark');\n }\n engine.updateConditions(conditions);\n };\n // The class for the scheme the app started in, committed at once. Light needs nothing: an app\n // starting light is exactly what it already rendered.\n if (options.darkClass !== false && conditions.colorScheme === 'dark') push({});\n\n // The system text size. Every text on screen was measured at the old one, and keeps that box\n // until it is measured again, so a change re-measures them all before anything else commits.\n // A change arrives as a `Dimensions` change on both platforms; the app coming back to the\n // foreground is checked as well, and the same change reported by both is re-measured once.\n let scale = fontScale();\n const rescale = (next: number) => {\n if (next === scale) return;\n scale = next;\n engine.remeasureText?.();\n };\n\n const sizes = screen.subscribe(({ window }) => {\n rescale(fontScale());\n push({ width: window.width, height: window.height });\n });\n const scheme = colors.subscribe((colorScheme) => push({ colorScheme }));\n const settings = accessibility.subscribe((change) => {\n if (change.fontScale !== undefined) rescale(change.fontScale);\n if (change.reduceMotion !== undefined) push({ reducedMotion: change.reduceMotion });\n });\n void accessibility\n .current()\n .then(({ reduceMotion }) => reduceMotion && push({ reducedMotion: true }));\n\n return () => {\n sizes();\n scheme();\n settings();\n };\n}\n"]}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Links that arrive from outside the app: a `canary://` url, a universal link, a notification.
|
|
3
|
+
*/
|
|
4
|
+
import { InjectionToken } from '@angular/core';
|
|
5
|
+
import * as i0 from "@angular/core";
|
|
6
|
+
export interface DeepLinkSource {
|
|
7
|
+
/** The url the app was launched with. A promise, and nothing says it settles before mount. */
|
|
8
|
+
launchUrl(): Promise<string | null>;
|
|
9
|
+
subscribe(listener: (url: string) => void): () => void;
|
|
10
|
+
open(url: string): void;
|
|
11
|
+
}
|
|
12
|
+
export declare function deepLinkSource(): DeepLinkSource;
|
|
13
|
+
export declare function pathOf(url: string | null): string | null;
|
|
14
|
+
/**
|
|
15
|
+
* The launch url is delivered whichever way is true at the time: as the initial url if it is
|
|
16
|
+
* known before anything asks, and as an ordinary link if the app is already up. Both happen -
|
|
17
|
+
* the promise races the first navigation and neither order is guaranteed - and a link that
|
|
18
|
+
* arrives late is still a link, so nothing is dropped either way.
|
|
19
|
+
*/
|
|
20
|
+
export declare class DeepLinks {
|
|
21
|
+
/** Overridden in a test to deliver a link with no platform under it. */
|
|
22
|
+
static readonly SOURCE: InjectionToken<DeepLinkSource>;
|
|
23
|
+
private readonly source;
|
|
24
|
+
private launch;
|
|
25
|
+
private listener;
|
|
26
|
+
constructor();
|
|
27
|
+
/** The path the app was launched with, if it is known yet. */
|
|
28
|
+
initialUrl(): string | null;
|
|
29
|
+
/** Links that arrive while the app is running, as paths. Returns an unsubscribe. */
|
|
30
|
+
subscribe(listener: (path: string) => void): () => void;
|
|
31
|
+
/** Hand a url to whatever else on the device handles it: a browser, Maps, another app. */
|
|
32
|
+
open(url: string): void;
|
|
33
|
+
static ɵfac: i0.ɵɵFactoryDeclaration<DeepLinks, never>;
|
|
34
|
+
static ɵprov: i0.ɵɵInjectableDeclaration<any>;
|
|
35
|
+
}
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Links that arrive from outside the app: a `canary://` url, a universal link, a notification.
|
|
3
|
+
*/
|
|
4
|
+
import { InjectionToken, Service, inject } from '@angular/core';
|
|
5
|
+
import { reactNative } from "./react-native.js";
|
|
6
|
+
import * as i0 from "@angular/core";
|
|
7
|
+
export function deepLinkSource() {
|
|
8
|
+
const native = reactNative();
|
|
9
|
+
if (!native) {
|
|
10
|
+
return { launchUrl: () => Promise.resolve(null), subscribe: () => () => { }, open: () => { } };
|
|
11
|
+
}
|
|
12
|
+
return {
|
|
13
|
+
launchUrl: () => native.Linking.getInitialURL(),
|
|
14
|
+
subscribe: (listener) => {
|
|
15
|
+
const subscription = native.Linking.addEventListener('url', ({ url }) => listener(url));
|
|
16
|
+
return () => subscription.remove();
|
|
17
|
+
},
|
|
18
|
+
open: (url) => void native.Linking.openURL(url).catch(() => { }),
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* The path a url means to this app: everything after the scheme and host.
|
|
23
|
+
*
|
|
24
|
+
* Prefixes are app configuration and every app's are different, but stripping one is not: what a
|
|
25
|
+
* router can navigate to is a path. `canary://primitives` and `https://example.com/primitives`
|
|
26
|
+
* both mean `/primitives` - which takes noticing that only the second one has a host.
|
|
27
|
+
*
|
|
28
|
+
* Expo Go is the exception worth handling here rather than in every app: it serves every project
|
|
29
|
+
* from one development url, puts the app's own path after `/--`, and has a host like a web url
|
|
30
|
+
* rather than a path like an app's own scheme.
|
|
31
|
+
*
|
|
32
|
+
* The development client is the other. `expo run:ios` launches the app through
|
|
33
|
+
* `<scheme>://expo-development-client/?url=...`, which is Expo telling its own launcher which
|
|
34
|
+
* bundle to load and means nothing to the app. Left alone it reaches the router as a path, matches
|
|
35
|
+
* no route, and the app shows a blank screen on every launch during development - which looks
|
|
36
|
+
* exactly like a bug in the app.
|
|
37
|
+
*/
|
|
38
|
+
/** Expo's own launch hosts. Not paths: instructions to the launcher, which the app never routes. */
|
|
39
|
+
const EXPO_INTERNAL = new Set(['expo-development-client']);
|
|
40
|
+
/** Whether the first segment names one of Expo's launchers rather than something in the app. */
|
|
41
|
+
function isLauncher(rest) {
|
|
42
|
+
return EXPO_INTERNAL.has(rest.split(/[/?#]/, 1)[0] ?? '');
|
|
43
|
+
}
|
|
44
|
+
/** Schemes that carry a host to skip before the path starts. */
|
|
45
|
+
const HAS_HOST = new Set(['http', 'https', 'exp', 'exps']);
|
|
46
|
+
export function pathOf(url) {
|
|
47
|
+
if (!url)
|
|
48
|
+
return null;
|
|
49
|
+
const scheme = /^([a-z][a-z0-9+.-]*):\/\//i.exec(url);
|
|
50
|
+
const rest = scheme ? url.slice(scheme[0].length) : url;
|
|
51
|
+
const expoGo = rest.indexOf('/--/');
|
|
52
|
+
if (expoGo !== -1)
|
|
53
|
+
return rest.slice(expoGo + 3);
|
|
54
|
+
if (isLauncher(rest))
|
|
55
|
+
return null;
|
|
56
|
+
// A url with a host has one to skip. A custom scheme does not: `canary://settings` names a
|
|
57
|
+
// path, not a machine, and treating the two alike is how it quietly becomes `/`.
|
|
58
|
+
if (HAS_HOST.has(scheme?.[1]?.toLowerCase() ?? '')) {
|
|
59
|
+
// The host ends at the path, or at a query or fragment when there is no path.
|
|
60
|
+
const end = rest.search(/[/?#]/);
|
|
61
|
+
if (end === -1)
|
|
62
|
+
return '/';
|
|
63
|
+
return rest[end] === '/' ? rest.slice(end) : `/${rest.slice(end)}`;
|
|
64
|
+
}
|
|
65
|
+
return rest.startsWith('/') ? rest : `/${rest}`;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* The launch url is delivered whichever way is true at the time: as the initial url if it is
|
|
69
|
+
* known before anything asks, and as an ordinary link if the app is already up. Both happen -
|
|
70
|
+
* the promise races the first navigation and neither order is guaranteed - and a link that
|
|
71
|
+
* arrives late is still a link, so nothing is dropped either way.
|
|
72
|
+
*/
|
|
73
|
+
export class DeepLinks {
|
|
74
|
+
/** Overridden in a test to deliver a link with no platform under it. */
|
|
75
|
+
static SOURCE = new InjectionToken('angular-native.deepLinkSource', {
|
|
76
|
+
factory: deepLinkSource,
|
|
77
|
+
});
|
|
78
|
+
source = inject(DeepLinks.SOURCE);
|
|
79
|
+
launch = null;
|
|
80
|
+
listener = null;
|
|
81
|
+
constructor() {
|
|
82
|
+
void this.source.launchUrl().then((url) => {
|
|
83
|
+
const path = pathOf(url);
|
|
84
|
+
if (!path || path === '/')
|
|
85
|
+
return;
|
|
86
|
+
if (this.listener)
|
|
87
|
+
this.listener(path);
|
|
88
|
+
else
|
|
89
|
+
this.launch = path;
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
/** The path the app was launched with, if it is known yet. */
|
|
93
|
+
initialUrl() {
|
|
94
|
+
return this.launch;
|
|
95
|
+
}
|
|
96
|
+
/** Links that arrive while the app is running, as paths. Returns an unsubscribe. */
|
|
97
|
+
subscribe(listener) {
|
|
98
|
+
this.listener = listener;
|
|
99
|
+
const unsubscribe = this.source.subscribe((url) => {
|
|
100
|
+
const path = pathOf(url);
|
|
101
|
+
if (path)
|
|
102
|
+
listener(path);
|
|
103
|
+
});
|
|
104
|
+
return () => {
|
|
105
|
+
this.listener = null;
|
|
106
|
+
unsubscribe();
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
/** Hand a url to whatever else on the device handles it: a browser, Maps, another app. */
|
|
110
|
+
open(url) {
|
|
111
|
+
this.source.open(url);
|
|
112
|
+
}
|
|
113
|
+
static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "22.2.0", ngImport: i0, type: DeepLinks, deps: [], target: i0.ɵɵFactoryTarget.Service });
|
|
114
|
+
static ɵprov = i0.ɵɵngDeclareService({ minVersion: "22.0.0", version: "22.2.0", ngImport: i0, type: DeepLinks });
|
|
115
|
+
}
|
|
116
|
+
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "22.2.0", ngImport: i0, type: DeepLinks, decorators: [{
|
|
117
|
+
type: Service
|
|
118
|
+
}], ctorParameters: () => [] });
|
|
119
|
+
//# sourceMappingURL=deep-links.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"deep-links.js","sourceRoot":"","sources":["../src/deep-links.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,OAAO,EAAE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GAAW;QACd,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;IACxB,CAAC;uGAxCU,SAAS;wGAAT,SAAS;;2FAAT,SAAS;kBADrB,OAAO","sourcesContent":["/**\n * Links that arrive from outside the app: a `canary://` url, a universal link, a notification.\n */\nimport { InjectionToken, Service, inject } from '@angular/core';\nimport { reactNative } from './react-native.ts';\n\nexport interface DeepLinkSource {\n /** The url the app was launched with. A promise, and nothing says it settles before mount. */\n launchUrl(): Promise<string | null>;\n subscribe(listener: (url: string) => void): () => void;\n open(url: string): void;\n}\n\nexport function deepLinkSource(): DeepLinkSource {\n const native = reactNative();\n if (!native) {\n return { launchUrl: () => Promise.resolve(null), subscribe: () => () => {}, open: () => {} };\n }\n\n return {\n launchUrl: () => native.Linking.getInitialURL(),\n subscribe: (listener) => {\n const subscription = native.Linking.addEventListener('url', ({ url }) => listener(url));\n return () => subscription.remove();\n },\n open: (url) => void native.Linking.openURL(url).catch(() => {}),\n };\n}\n\n/**\n * The path a url means to this app: everything after the scheme and host.\n *\n * Prefixes are app configuration and every app's are different, but stripping one is not: what a\n * router can navigate to is a path. `canary://primitives` and `https://example.com/primitives`\n * both mean `/primitives` - which takes noticing that only the second one has a host.\n *\n * Expo Go is the exception worth handling here rather than in every app: it serves every project\n * from one development url, puts the app's own path after `/--`, and has a host like a web url\n * rather than a path like an app's own scheme.\n *\n * The development client is the other. `expo run:ios` launches the app through\n * `<scheme>://expo-development-client/?url=...`, which is Expo telling its own launcher which\n * bundle to load and means nothing to the app. Left alone it reaches the router as a path, matches\n * no route, and the app shows a blank screen on every launch during development - which looks\n * exactly like a bug in the app.\n */\n\n/** Expo's own launch hosts. Not paths: instructions to the launcher, which the app never routes. */\nconst EXPO_INTERNAL = new Set(['expo-development-client']);\n\n/** Whether the first segment names one of Expo's launchers rather than something in the app. */\nfunction isLauncher(rest: string): boolean {\n return EXPO_INTERNAL.has(rest.split(/[/?#]/, 1)[0] ?? '');\n}\n\n/** Schemes that carry a host to skip before the path starts. */\nconst HAS_HOST = new Set(['http', 'https', 'exp', 'exps']);\n\nexport function pathOf(url: string | null): string | null {\n if (!url) return null;\n const scheme = /^([a-z][a-z0-9+.-]*):\\/\\//i.exec(url);\n const rest = scheme ? url.slice(scheme[0].length) : url;\n\n const expoGo = rest.indexOf('/--/');\n if (expoGo !== -1) return rest.slice(expoGo + 3);\n if (isLauncher(rest)) return null;\n\n // A url with a host has one to skip. A custom scheme does not: `canary://settings` names a\n // path, not a machine, and treating the two alike is how it quietly becomes `/`.\n if (HAS_HOST.has(scheme?.[1]?.toLowerCase() ?? '')) {\n // The host ends at the path, or at a query or fragment when there is no path.\n const end = rest.search(/[/?#]/);\n if (end === -1) return '/';\n return rest[end] === '/' ? rest.slice(end) : `/${rest.slice(end)}`;\n }\n return rest.startsWith('/') ? rest : `/${rest}`;\n}\n\n/**\n * The launch url is delivered whichever way is true at the time: as the initial url if it is\n * known before anything asks, and as an ordinary link if the app is already up. Both happen -\n * the promise races the first navigation and neither order is guaranteed - and a link that\n * arrives late is still a link, so nothing is dropped either way.\n */\n@Service()\nexport class DeepLinks {\n /** Overridden in a test to deliver a link with no platform under it. */\n static readonly SOURCE = new InjectionToken<DeepLinkSource>('angular-native.deepLinkSource', {\n factory: deepLinkSource,\n });\n\n private readonly source = inject(DeepLinks.SOURCE);\n private launch: string | null = null;\n private listener: ((path: string) => void) | null = null;\n\n constructor() {\n void this.source.launchUrl().then((url) => {\n const path = pathOf(url);\n if (!path || path === '/') return;\n if (this.listener) this.listener(path);\n else this.launch = path;\n });\n }\n\n /** The path the app was launched with, if it is known yet. */\n initialUrl(): string | null {\n return this.launch;\n }\n\n /** Links that arrive while the app is running, as paths. Returns an unsubscribe. */\n subscribe(listener: (path: string) => void): () => void {\n this.listener = listener;\n const unsubscribe = this.source.subscribe((url) => {\n const path = pathOf(url);\n if (path) listener(path);\n });\n return () => {\n this.listener = null;\n unsubscribe();\n };\n }\n\n /** Hand a url to whatever else on the device handles it: a browser, Maps, another app. */\n open(url: string): void {\n this.source.open(url);\n }\n}\n"]}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The developer menu, for the switches an app wants while it is being built.
|
|
3
|
+
*
|
|
4
|
+
* A feature flag, a fake slow network, "log the current state", "clear the database" - the things
|
|
5
|
+
* a team otherwise builds a hidden settings screen for and then has to remember not to ship. These
|
|
6
|
+
* go in the shake menu, which does not exist in a release build, so neither do they.
|
|
7
|
+
*
|
|
8
|
+
* `__DEV__` is checked rather than assumed: `DevSettings` exists in a release bundle and its
|
|
9
|
+
* `addMenuItem` is a no-op there, but an app that registers twenty of them is still doing twenty
|
|
10
|
+
* things at startup for no reason.
|
|
11
|
+
*/
|
|
12
|
+
import { InjectionToken } from '@angular/core';
|
|
13
|
+
import * as i0 from "@angular/core";
|
|
14
|
+
export interface NativeDevMenu {
|
|
15
|
+
addMenuItem(title: string, handler: () => unknown): void;
|
|
16
|
+
reload(reason?: string): void;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* The menu, and whether there is one to add to.
|
|
20
|
+
*
|
|
21
|
+
* `__DEV__` travels with the module rather than being read from the global inside the class, so a
|
|
22
|
+
* test can say "this is a release build" without writing to `globalThis` and putting it back.
|
|
23
|
+
*/
|
|
24
|
+
export interface DevMenuSource {
|
|
25
|
+
readonly menu: NativeDevMenu | null;
|
|
26
|
+
readonly development: boolean;
|
|
27
|
+
}
|
|
28
|
+
export declare class DevMenu {
|
|
29
|
+
/** Overridden in a test to register items without a shake menu. */
|
|
30
|
+
static readonly SOURCE: InjectionToken<DevMenuSource>;
|
|
31
|
+
private readonly source;
|
|
32
|
+
/** Whether anything registered here will appear. False in a release build. */
|
|
33
|
+
get available(): boolean;
|
|
34
|
+
/**
|
|
35
|
+
* Add an item to the shake menu.
|
|
36
|
+
*
|
|
37
|
+
* `DevSettings.addMenuItem` already keys on `title` and swaps in the new handler without adding
|
|
38
|
+
* a second native entry - it removes its own previous listener for that title before adding the
|
|
39
|
+
* one just given - so registering the same title again, as a hot reload does when it re-runs the
|
|
40
|
+
* constructor that calls `add()`, is exactly how the fresh handler replaces the stale one rather
|
|
41
|
+
* than being silently dropped.
|
|
42
|
+
*/
|
|
43
|
+
add(title: string, handler: () => unknown): void;
|
|
44
|
+
/** Reload the bundle, as the menu's own item does. */
|
|
45
|
+
reload(reason?: string): void;
|
|
46
|
+
static ɵfac: i0.ɵɵFactoryDeclaration<DevMenu, never>;
|
|
47
|
+
static ɵprov: i0.ɵɵInjectableDeclaration<any>;
|
|
48
|
+
}
|
package/dist/dev-menu.js
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The developer menu, for the switches an app wants while it is being built.
|
|
3
|
+
*
|
|
4
|
+
* A feature flag, a fake slow network, "log the current state", "clear the database" - the things
|
|
5
|
+
* a team otherwise builds a hidden settings screen for and then has to remember not to ship. These
|
|
6
|
+
* go in the shake menu, which does not exist in a release build, so neither do they.
|
|
7
|
+
*
|
|
8
|
+
* `__DEV__` is checked rather than assumed: `DevSettings` exists in a release bundle and its
|
|
9
|
+
* `addMenuItem` is a no-op there, but an app that registers twenty of them is still doing twenty
|
|
10
|
+
* things at startup for no reason.
|
|
11
|
+
*/
|
|
12
|
+
import { InjectionToken, Service, inject } from '@angular/core';
|
|
13
|
+
import { reactNative } from "./react-native.js";
|
|
14
|
+
import * as i0 from "@angular/core";
|
|
15
|
+
export class DevMenu {
|
|
16
|
+
/** Overridden in a test to register items without a shake menu. */
|
|
17
|
+
static SOURCE = new InjectionToken('angular-native.devMenuSource', {
|
|
18
|
+
factory: () => ({
|
|
19
|
+
menu: reactNative()?.DevSettings ?? null,
|
|
20
|
+
development: globalThis.__DEV__ === true,
|
|
21
|
+
}),
|
|
22
|
+
});
|
|
23
|
+
source = inject(DevMenu.SOURCE);
|
|
24
|
+
/** Whether anything registered here will appear. False in a release build. */
|
|
25
|
+
get available() {
|
|
26
|
+
return this.source.development && this.source.menu !== null;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Add an item to the shake menu.
|
|
30
|
+
*
|
|
31
|
+
* `DevSettings.addMenuItem` already keys on `title` and swaps in the new handler without adding
|
|
32
|
+
* a second native entry - it removes its own previous listener for that title before adding the
|
|
33
|
+
* one just given - so registering the same title again, as a hot reload does when it re-runs the
|
|
34
|
+
* constructor that calls `add()`, is exactly how the fresh handler replaces the stale one rather
|
|
35
|
+
* than being silently dropped.
|
|
36
|
+
*/
|
|
37
|
+
add(title, handler) {
|
|
38
|
+
if (!this.available)
|
|
39
|
+
return;
|
|
40
|
+
this.source.menu.addMenuItem(title, handler);
|
|
41
|
+
}
|
|
42
|
+
/** Reload the bundle, as the menu's own item does. */
|
|
43
|
+
reload(reason = 'requested by the app') {
|
|
44
|
+
this.source.menu?.reload(reason);
|
|
45
|
+
}
|
|
46
|
+
static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "22.2.0", ngImport: i0, type: DevMenu, deps: [], target: i0.ɵɵFactoryTarget.Service });
|
|
47
|
+
static ɵprov = i0.ɵɵngDeclareService({ minVersion: "22.0.0", version: "22.2.0", ngImport: i0, type: DevMenu });
|
|
48
|
+
}
|
|
49
|
+
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "22.2.0", ngImport: i0, type: DevMenu, decorators: [{
|
|
50
|
+
type: Service
|
|
51
|
+
}] });
|
|
52
|
+
//# sourceMappingURL=dev-menu.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"dev-menu.js","sourceRoot":"","sources":["../src/dev-menu.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAEH,OAAO,EAAE,cAAc,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,eAAe,CAAC;AAChE,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;;AAmBhD,MAAM,OAAO,OAAO;IAClB,mEAAmE;IACnE,MAAM,CAAU,MAAM,GAAG,IAAI,cAAc,CAAgB,8BAA8B,EAAE;QACzF,OAAO,EAAE,GAAG,EAAE,CAAC,CAAC;YACd,IAAI,EAAE,WAAW,EAAE,EAAE,WAAW,IAAI,IAAI;YACxC,WAAW,EAAG,UAAoC,CAAC,OAAO,KAAK,IAAI;SACpE,CAAC;KACH,CAAC,CAAC;IAEc,MAAM,GAAG,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC;IAEjD,8EAA8E;IAC9E,IAAI,SAAS;QACX,OAAO,IAAI,CAAC,MAAM,CAAC,WAAW,IAAI,IAAI,CAAC,MAAM,CAAC,IAAI,KAAK,IAAI,CAAC;IAC9D,CAAC;IAED;;;;;;;;OAQG;IACH,GAAG,CAAC,KAAa,EAAE,OAAsB;QACvC,IAAI,CAAC,IAAI,CAAC,SAAS;YAAE,OAAO;QAC5B,IAAI,CAAC,MAAM,CAAC,IAAK,CAAC,WAAW,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC;IAChD,CAAC;IAED,sDAAsD;IACtD,MAAM,CAAC,MAAM,GAAG,sBAAsB;QACpC,IAAI,CAAC,MAAM,CAAC,IAAI,EAAE,MAAM,CAAC,MAAM,CAAC,CAAC;IACnC,CAAC;uGAjCU,OAAO;wGAAP,OAAO;;2FAAP,OAAO;kBADnB,OAAO","sourcesContent":["/**\n * The developer menu, for the switches an app wants while it is being built.\n *\n * A feature flag, a fake slow network, \"log the current state\", \"clear the database\" - the things\n * a team otherwise builds a hidden settings screen for and then has to remember not to ship. These\n * go in the shake menu, which does not exist in a release build, so neither do they.\n *\n * `__DEV__` is checked rather than assumed: `DevSettings` exists in a release bundle and its\n * `addMenuItem` is a no-op there, but an app that registers twenty of them is still doing twenty\n * things at startup for no reason.\n */\n\nimport { InjectionToken, Service, inject } from '@angular/core';\nimport { reactNative } from './react-native.ts';\n\nexport interface NativeDevMenu {\n addMenuItem(title: string, handler: () => unknown): void;\n reload(reason?: string): void;\n}\n\n/**\n * The menu, and whether there is one to add to.\n *\n * `__DEV__` travels with the module rather than being read from the global inside the class, so a\n * test can say \"this is a release build\" without writing to `globalThis` and putting it back.\n */\nexport interface DevMenuSource {\n readonly menu: NativeDevMenu | null;\n readonly development: boolean;\n}\n\n@Service()\nexport class DevMenu {\n /** Overridden in a test to register items without a shake menu. */\n static readonly SOURCE = new InjectionToken<DevMenuSource>('angular-native.devMenuSource', {\n factory: () => ({\n menu: reactNative()?.DevSettings ?? null,\n development: (globalThis as { __DEV__?: boolean }).__DEV__ === true,\n }),\n });\n\n private readonly source = inject(DevMenu.SOURCE);\n\n /** Whether anything registered here will appear. False in a release build. */\n get available(): boolean {\n return this.source.development && this.source.menu !== null;\n }\n\n /**\n * Add an item to the shake menu.\n *\n * `DevSettings.addMenuItem` already keys on `title` and swaps in the new handler without adding\n * a second native entry - it removes its own previous listener for that title before adding the\n * one just given - so registering the same title again, as a hot reload does when it re-runs the\n * constructor that calls `add()`, is exactly how the fresh handler replaces the stale one rather\n * than being silently dropped.\n */\n add(title: string, handler: () => unknown): void {\n if (!this.available) return;\n this.source.menu!.addMenuItem(title, handler);\n }\n\n /** Reload the bundle, as the menu's own item does. */\n reload(reason = 'requested by the app'): void {\n this.source.menu?.reload(reason);\n }\n}\n"]}
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The platform's own dialogs: an alert, a confirmation, a prompt, an action sheet, a toast.
|
|
3
|
+
*
|
|
4
|
+
* All of these are callback APIs, and the reason to wrap them is that **a dialog is a question**.
|
|
5
|
+
* `Alert.alert` hands its answer to one of several `onPress` callbacks, so asking something and
|
|
6
|
+
* acting on the answer means splitting one thought across three functions. A promise puts it back
|
|
7
|
+
* together, and `await` at the call site reads like what it is.
|
|
8
|
+
*
|
|
9
|
+
* The platform split is the other half. An action sheet is iOS's way of offering a short list of
|
|
10
|
+
* choices and Android's is a dialog; a toast is Android's way of saying something small and iOS
|
|
11
|
+
* has none. `choose` and `notify` pick per platform, so an app writes the intent rather than the
|
|
12
|
+
* platform - and the one thing neither can do is fail silently on the platform it is not for.
|
|
13
|
+
*/
|
|
14
|
+
import { InjectionToken } from '@angular/core';
|
|
15
|
+
import { type ReactNative } from './react-native.ts';
|
|
16
|
+
import * as i0 from "@angular/core";
|
|
17
|
+
/** One choice in a dialog or an action sheet. */
|
|
18
|
+
export interface Choice {
|
|
19
|
+
readonly label: string;
|
|
20
|
+
/** `cancel` is dismissal; `destructive` is the one the platform paints red. */
|
|
21
|
+
readonly style?: 'default' | 'cancel' | 'destructive';
|
|
22
|
+
}
|
|
23
|
+
export interface NativeDialogs {
|
|
24
|
+
readonly platform: 'ios' | 'android' | string;
|
|
25
|
+
alert(title: string, message: string | undefined, buttons: readonly {
|
|
26
|
+
text: string;
|
|
27
|
+
style?: string;
|
|
28
|
+
onPress?: () => void;
|
|
29
|
+
}[]): void;
|
|
30
|
+
prompt?(title: string, message: string | undefined, onResult: (value: string | null) => void, defaultValue?: string): void;
|
|
31
|
+
actionSheet?(options: {
|
|
32
|
+
title?: string;
|
|
33
|
+
options: string[];
|
|
34
|
+
cancelButtonIndex: number;
|
|
35
|
+
destructiveButtonIndex?: number;
|
|
36
|
+
}, onSelect: (index: number) => void): void;
|
|
37
|
+
toast?(message: string, long: boolean): void;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* `inject(Dialogs).confirm('Delete this?', { destructive: true })`.
|
|
41
|
+
*
|
|
42
|
+
* A dialog is a question, so every method here resolves to the answer rather than handing it to
|
|
43
|
+
* one of several callbacks.
|
|
44
|
+
*/
|
|
45
|
+
export declare class Dialogs {
|
|
46
|
+
/** Overridden in a test to answer a dialog without one being shown. */
|
|
47
|
+
static readonly SOURCE: InjectionToken<NativeDialogs | null>;
|
|
48
|
+
private readonly native;
|
|
49
|
+
/** Say something, and wait until it has been read. Resolves when the dialog is dismissed. */
|
|
50
|
+
tell(title: string, message?: string, dismiss?: string): Promise<void>;
|
|
51
|
+
/**
|
|
52
|
+
* Ask something with two answers. Resolves to whether they agreed.
|
|
53
|
+
*
|
|
54
|
+
* `destructive` paints the confirm button as the platform paints a delete, and puts cancel where
|
|
55
|
+
* the platform puts it - which on iOS is the *right*, so a confirmation dialog does not train
|
|
56
|
+
* people to tap the wrong thing.
|
|
57
|
+
*/
|
|
58
|
+
confirm(title: string, options?: {
|
|
59
|
+
message?: string;
|
|
60
|
+
confirm?: string;
|
|
61
|
+
cancel?: string;
|
|
62
|
+
destructive?: boolean;
|
|
63
|
+
}): Promise<boolean>;
|
|
64
|
+
/**
|
|
65
|
+
* Ask for a line of text. Resolves to null if they cancelled.
|
|
66
|
+
*
|
|
67
|
+
* iOS only: `Alert.prompt` has no Android equivalent, and inventing one out of a modal and a
|
|
68
|
+
* text input would be a dialog that looks nothing like the platform's. On Android this resolves
|
|
69
|
+
* to null so a caller can fall back to a screen of its own.
|
|
70
|
+
*/
|
|
71
|
+
ask(title: string, options?: {
|
|
72
|
+
message?: string;
|
|
73
|
+
value?: string;
|
|
74
|
+
}): Promise<string | null>;
|
|
75
|
+
/**
|
|
76
|
+
* Offer a short list of choices. Resolves to the index chosen, or null if dismissed.
|
|
77
|
+
*
|
|
78
|
+
* An action sheet on iOS and a dialog on Android, because that is what each platform means by
|
|
79
|
+
* this. A `cancel` choice is where the platform puts cancel rather than in the list.
|
|
80
|
+
*/
|
|
81
|
+
choose(title: string, choices: readonly Choice[]): Promise<number | null>;
|
|
82
|
+
/**
|
|
83
|
+
* Say something small and transient.
|
|
84
|
+
*
|
|
85
|
+
* A toast on Android. iOS has no such thing and never has, so this does nothing there rather
|
|
86
|
+
* than putting up a modal alert for something that did not warrant interrupting anyone - and an
|
|
87
|
+
* app that must be seen should use `tell`.
|
|
88
|
+
*/
|
|
89
|
+
notify(message: string, options?: {
|
|
90
|
+
long?: boolean;
|
|
91
|
+
}): void;
|
|
92
|
+
static ɵfac: i0.ɵɵFactoryDeclaration<Dialogs, never>;
|
|
93
|
+
static ɵprov: i0.ɵɵInjectableDeclaration<any>;
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* The platform's dialogs, from React Native's own modules.
|
|
97
|
+
*
|
|
98
|
+
* `Alert.prompt` and `ActionSheetIOS` are iOS-only and `ToastAndroid` is Android-only; each is
|
|
99
|
+
* left absent rather than shimmed, which is what lets `ask`, `choose` and `notify` above decide
|
|
100
|
+
* what to do about it.
|
|
101
|
+
*
|
|
102
|
+
* Which platform it is has to be asked outright, because React Native's own modules do not answer
|
|
103
|
+
* it. `react-native` exports `ActionSheetIOS` on Android too - the object is there and so is
|
|
104
|
+
* `showActionSheetWithOptions`; only the TurboModule underneath is null, and the invariant that
|
|
105
|
+
* catches that throws out of the promise `choose` returns, so a press did nothing at all and said
|
|
106
|
+
* nothing about it. `Alert.prompt` is worse: it exists on Android and its body is one
|
|
107
|
+
* `Platform.OS === 'ios'` check, so it returns having called no callback and the promise `ask`
|
|
108
|
+
* returns never settles. `ToastAndroid` is the mirror image on iOS. Presence is not capability
|
|
109
|
+
* here; only the platform name is.
|
|
110
|
+
*/
|
|
111
|
+
export declare function dialogSource(native: Pick<ReactNative, 'Platform' | 'Alert' | 'ActionSheetIOS' | 'ToastAndroid'> | null): NativeDialogs | null;
|