@a16n/web-sim 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/README.md +13 -2
- package/bin/cli.ts +255 -0
- package/bin/web-sim.js +39 -0
- package/overrides/@backpackapp-io/react-native-toast/overrides.ts +15 -0
- package/overrides/@react-native-firebase/app/overrides.ts +34 -0
- package/overrides/@tiktok-for-business/react-native-sdk/NativeTiktokSdk.ts +29 -0
- package/overrides/@tiktok-for-business/react-native-sdk/overrides.ts +9 -0
- package/overrides/expo-application/ExpoApplication.ts +81 -0
- package/overrides/expo-application/overrides.ts +16 -0
- package/overrides/expo-background-task/ExpoBackgroundTaskModule.ts +30 -0
- package/overrides/expo-background-task/overrides.ts +9 -0
- package/overrides/expo-constants/ExponentConstants.ts +66 -0
- package/overrides/expo-constants/overrides.ts +9 -0
- package/overrides/expo-device/ExpoDevice.ts +104 -0
- package/overrides/expo-device/overrides.ts +9 -0
- package/overrides/expo-glass-effect/GlassContainer.tsx +9 -0
- package/overrides/expo-glass-effect/GlassView.tsx +95 -0
- package/overrides/expo-glass-effect/availability.ts +14 -0
- package/overrides/expo-glass-effect/overrides.ts +14 -0
- package/overrides/expo-haptics/Haptics.ts +67 -0
- package/overrides/expo-haptics/overrides.ts +5 -0
- package/overrides/expo-mail-composer/ExpoMailComposer.ts +40 -0
- package/overrides/expo-mail-composer/overrides.ts +9 -0
- package/overrides/expo-mesh-gradient/MeshGradientView.tsx +44 -0
- package/overrides/expo-mesh-gradient/overrides.ts +9 -0
- package/overrides/expo-modules-core/overrides.ts +12 -0
- package/overrides/expo-navigation-bar/ExpoNavigationBar.ts +29 -0
- package/overrides/expo-navigation-bar/NavigationBar.ts +43 -0
- package/overrides/expo-navigation-bar/overrides.ts +12 -0
- package/overrides/expo-router/NativeStackView.tsx +74 -0
- package/overrides/expo-router/NativeTabsView.tsx +263 -0
- package/overrides/expo-router/SafeAreaProvider.ts +6 -0
- package/overrides/expo-router/overrides.ts +23 -0
- package/overrides/expo-router/statusBarRequests.ts +51 -0
- package/overrides/expo-screen-orientation/ExpoScreenOrientation.ts +173 -0
- package/overrides/expo-screen-orientation/overrides.ts +21 -0
- package/overrides/expo-sharing/SharingNativeModule.ts +21 -0
- package/overrides/expo-sharing/overrides.ts +9 -0
- package/overrides/expo-sms/ExpoSMS.ts +24 -0
- package/overrides/expo-sms/overrides.ts +9 -0
- package/overrides/expo-splash-screen/SplashScreen.ts +25 -0
- package/overrides/expo-splash-screen/overrides.ts +5 -0
- package/overrides/expo-status-bar/StatusBar.tsx +44 -0
- package/overrides/expo-status-bar/overrides.ts +5 -0
- package/overrides/expo-store-review/ExpoStoreReview.ts +18 -0
- package/overrides/expo-store-review/overrides.ts +9 -0
- package/overrides/expo-symbols/SymbolView.tsx +192 -0
- package/overrides/expo-symbols/overrides.ts +9 -0
- package/overrides/expo-system-ui/ExpoSystemUI.ts +34 -0
- package/overrides/expo-system-ui/overrides.ts +9 -0
- package/overrides/expo-task-manager/ExpoTaskManager.ts +39 -0
- package/overrides/expo-task-manager/overrides.ts +9 -0
- package/overrides/expo-tracking-transparency/ExpoTrackingTransparency.ts +30 -0
- package/overrides/expo-tracking-transparency/overrides.ts +11 -0
- package/overrides/expo-video/ExpoVideo.ts +16 -0
- package/overrides/expo-video/overrides.ts +9 -0
- package/overrides/lottie-react-native/LottieView.tsx +87 -0
- package/overrides/lottie-react-native/overrides.ts +9 -0
- package/overrides/react-native-pager-view/PagerViewNativeComponent.tsx +254 -0
- package/overrides/react-native-pager-view/overrides.ts +9 -0
- package/overrides/react-native-reanimated/overrides.ts +28 -0
- package/overrides/react-native-safe-area-context/NativeSafeAreaProvider.tsx +40 -0
- package/overrides/react-native-safe-area-context/overrides.ts +12 -0
- package/overrides/react-native-share/NativeRNShare.ts +22 -0
- package/overrides/react-native-share/overrides.ts +12 -0
- package/overrides/react-native-share/platform.ts +14 -0
- package/overrides/react-native-turbo-image/TurboImage.tsx +101 -0
- package/overrides/react-native-turbo-image/overrides.ts +9 -0
- package/overrides/react-native-web/RefreshControl.tsx +57 -0
- package/overrides/react-native-web/overrides.ts +153 -0
- package/overrides/react-native-web/useElementLayout.ts +140 -0
- package/package.json +78 -4
- package/plugins/battery/overrides/expo-battery/ExpoBattery.ts +57 -0
- package/plugins/battery/overrides/expo-battery/overrides.ts +9 -0
- package/plugins/battery/plugin.ts +8 -0
- package/plugins/battery/shell.tsx +56 -0
- package/plugins/battery/state.ts +11 -0
- package/plugins/biometrics/overrides/expo-local-authentication/ExpoLocalAuthentication.ts +90 -0
- package/plugins/biometrics/overrides/expo-local-authentication/overrides.ts +11 -0
- package/plugins/biometrics/plugin.ts +8 -0
- package/plugins/biometrics/shell.tsx +88 -0
- package/plugins/biometrics/state.ts +41 -0
- package/plugins/brightness/app.ts +5 -0
- package/plugins/brightness/overrides/expo-brightness/Brightness.ts +98 -0
- package/plugins/brightness/overrides/expo-brightness/overrides.ts +10 -0
- package/plugins/brightness/plugin.ts +9 -0
- package/plugins/brightness/shell.tsx +55 -0
- package/plugins/brightness/state.ts +19 -0
- package/plugins/calendar/calendar.ts +299 -0
- package/plugins/calendar/overrides/expo-calendar/ExpoCalendar.ts +358 -0
- package/plugins/calendar/overrides/expo-calendar/LegacyExpoCalendar.ts +163 -0
- package/plugins/calendar/overrides/expo-calendar/overrides.ts +14 -0
- package/plugins/calendar/plugin.ts +8 -0
- package/plugins/calendar/shell.tsx +123 -0
- package/plugins/camera/overrides/expo-camera/ExpoCamera.tsx +269 -0
- package/plugins/camera/overrides/expo-camera/ExpoCameraManager.ts +56 -0
- package/plugins/camera/overrides/expo-camera/overrides.ts +12 -0
- package/plugins/camera/plugin.ts +9 -0
- package/plugins/camera/shell.tsx +107 -0
- package/plugins/camera/state.ts +61 -0
- package/plugins/clipboard/overrides/expo-clipboard/ExpoClipboard.ts +138 -0
- package/plugins/clipboard/overrides/expo-clipboard/overrides.ts +9 -0
- package/plugins/clipboard/plugin.ts +8 -0
- package/plugins/clipboard/shell.tsx +122 -0
- package/plugins/clipboard/state.ts +93 -0
- package/plugins/clock/app.ts +25 -0
- package/plugins/clock/plugin.ts +8 -0
- package/plugins/clock/shell.tsx +225 -0
- package/plugins/clock/state.ts +12 -0
- package/plugins/contacts/contacts.ts +125 -0
- package/plugins/contacts/overrides/expo-contacts/ExpoContacts.ts +153 -0
- package/plugins/contacts/overrides/expo-contacts/ExpoContactsNext.ts +232 -0
- package/plugins/contacts/overrides/expo-contacts/overrides.ts +12 -0
- package/plugins/contacts/plugin.ts +8 -0
- package/plugins/contacts/shell.tsx +109 -0
- package/plugins/home-indicator/app.ts +5 -0
- package/plugins/home-indicator/plugin.ts +9 -0
- package/plugins/home-indicator/requests.ts +48 -0
- package/plugins/home-indicator/shell.tsx +68 -0
- package/plugins/home-indicator/state.ts +31 -0
- package/plugins/keep-awake/app.ts +5 -0
- package/plugins/keep-awake/overrides/expo-keep-awake/ExpoKeepAwake.ts +27 -0
- package/plugins/keep-awake/overrides/expo-keep-awake/overrides.ts +9 -0
- package/plugins/keep-awake/plugin.ts +9 -0
- package/plugins/keep-awake/shell.tsx +80 -0
- package/plugins/keep-awake/state.ts +12 -0
- package/plugins/keyboard/Keyboard.tsx +354 -0
- package/plugins/keyboard/app.ts +90 -0
- package/plugins/keyboard/controller.ts +384 -0
- package/plugins/keyboard/fields.ts +244 -0
- package/plugins/keyboard/icons.tsx +283 -0
- package/plugins/keyboard/keyboard.css +362 -0
- package/plugins/keyboard/layouts/android.ts +175 -0
- package/plugins/keyboard/layouts/index.ts +10 -0
- package/plugins/keyboard/layouts/ios.ts +219 -0
- package/plugins/keyboard/layouts/types.ts +80 -0
- package/plugins/keyboard/overrides/react-native-keyboard-controller/KeyboardController.tsx +470 -0
- package/plugins/keyboard/overrides/react-native-keyboard-controller/overrides.ts +9 -0
- package/plugins/keyboard/plugin.ts +13 -0
- package/plugins/keyboard/shell.tsx +133 -0
- package/plugins/keyboard/state.ts +92 -0
- package/plugins/keyboard/suggestions.ts +63 -0
- package/plugins/keyboard/taps.ts +77 -0
- package/plugins/keyboard/typing.ts +112 -0
- package/plugins/locale/app.ts +232 -0
- package/plugins/locale/locale.ts +143 -0
- package/plugins/locale/overrides/expo-localization/ExpoLocalization.ts +41 -0
- package/plugins/locale/overrides/expo-localization/overrides.ts +9 -0
- package/plugins/locale/plugin.ts +9 -0
- package/plugins/locale/shell.tsx +147 -0
- package/plugins/locale/state.ts +36 -0
- package/plugins/location/geo.ts +56 -0
- package/plugins/location/overrides/expo-location/ExpoLocation.ts +235 -0
- package/plugins/location/overrides/expo-location/overrides.ts +11 -0
- package/plugins/location/places.ts +38 -0
- package/plugins/location/plugin.ts +8 -0
- package/plugins/location/shell.tsx +188 -0
- package/plugins/location/state.ts +51 -0
- package/plugins/motion/overrides/expo-sensors/DeviceSensor.ts +169 -0
- package/plugins/motion/overrides/expo-sensors/ExponentPedometer.ts +54 -0
- package/plugins/motion/overrides/expo-sensors/overrides.ts +14 -0
- package/plugins/motion/plugin.ts +8 -0
- package/plugins/motion/sensors.ts +116 -0
- package/plugins/motion/shell.tsx +136 -0
- package/plugins/motion/state.ts +46 -0
- package/plugins/network/app.ts +400 -0
- package/plugins/network/overrides/expo-network/ExpoNetwork.ts +51 -0
- package/plugins/network/overrides/expo-network/overrides.ts +9 -0
- package/plugins/network/plugin.ts +8 -0
- package/plugins/network/shell.tsx +67 -0
- package/plugins/network/state.ts +29 -0
- package/plugins/notifications/center.ts +255 -0
- package/plugins/notifications/content.ts +36 -0
- package/plugins/notifications/overrides/expo-notifications/NotificationsModule.ts +175 -0
- package/plugins/notifications/overrides/expo-notifications/channels.ts +108 -0
- package/plugins/notifications/overrides/expo-notifications/getDevicePushTokenAsync.ts +19 -0
- package/plugins/notifications/overrides/expo-notifications/getExpoPushTokenAsync.ts +16 -0
- package/plugins/notifications/overrides/expo-notifications/overrides.ts +36 -0
- package/plugins/notifications/plugin.ts +8 -0
- package/plugins/notifications/shell.tsx +192 -0
- package/plugins/notifications/state.ts +56 -0
- package/plugins/notifications/tokens.ts +47 -0
- package/plugins/notifications/trigger.ts +107 -0
- package/plugins/permissions/plugin.ts +6 -0
- package/plugins/permissions/shell.tsx +50 -0
- package/plugins/permissions/state.ts +38 -0
- package/plugins/photos/appLibrary.ts +114 -0
- package/plugins/photos/library.ts +322 -0
- package/plugins/photos/overrides/expo-image-picker/ExponentImagePicker.ts +153 -0
- package/plugins/photos/overrides/expo-image-picker/overrides.ts +13 -0
- package/plugins/photos/overrides/expo-media-library/ExpoMediaLibrary.ts +235 -0
- package/plugins/photos/overrides/expo-media-library/NativeMediaLibraryModule.ts +318 -0
- package/plugins/photos/overrides/expo-media-library/overrides.ts +19 -0
- package/plugins/photos/plugin.ts +8 -0
- package/plugins/photos/samples.ts +367 -0
- package/plugins/photos/shell.tsx +164 -0
- package/plugins/photos/state.ts +88 -0
- package/plugins/storage/files.ts +351 -0
- package/plugins/storage/overrides/expo-file-system/ExpoFileSystem.ts +770 -0
- package/plugins/storage/overrides/expo-file-system/ExponentFileSystem.ts +196 -0
- package/plugins/storage/overrides/expo-file-system/overrides.ts +13 -0
- package/plugins/storage/overrides/expo-secure-store/ExpoSecureStore.ts +75 -0
- package/plugins/storage/overrides/expo-secure-store/overrides.ts +9 -0
- package/plugins/storage/plugin.ts +8 -0
- package/plugins/storage/reset.ts +136 -0
- package/plugins/storage/secure.ts +71 -0
- package/plugins/storage/shell.tsx +365 -0
- package/plugins/storage/state.ts +13 -0
- package/src/client/app/appDocument.ts +113 -0
- package/src/client/app/appFocus.ts +97 -0
- package/src/client/app/console.ts +118 -0
- package/src/client/app/fonts.ts +59 -0
- package/src/client/app/main.tsx +99 -0
- package/src/client/app/routeContext.ts +45 -0
- package/src/client/app/touch.ts +640 -0
- package/src/client/entry.ts +20 -0
- package/src/client/modules.d.ts +8 -0
- package/src/client/react-native/app.ts +57 -0
- package/src/client/react-native/deep.ts +13 -0
- package/src/client/react-native/keyboard.tsx +192 -0
- package/src/client/react-native/library.ts +256 -0
- package/src/client/shell/Cookies.tsx +95 -0
- package/src/client/shell/LogDrawer.tsx +415 -0
- package/src/client/shell/PhotoPicker.tsx +174 -0
- package/src/client/shell/PluginOverlays.tsx +42 -0
- package/src/client/shell/PluginPanels.tsx +82 -0
- package/src/client/shell/ResetApp.tsx +87 -0
- package/src/client/shell/Resizer.tsx +99 -0
- package/src/client/shell/Shell.tsx +531 -0
- package/src/client/shell/SystemBars.tsx +376 -0
- package/src/client/shell/SystemCamera.tsx +181 -0
- package/src/client/shell/SystemOverlay.tsx +422 -0
- package/src/client/shell/SystemSheets.tsx +462 -0
- package/src/client/shell/backdrop.ts +229 -0
- package/src/client/shell/control.ts +91 -0
- package/src/client/shell/events.ts +226 -0
- package/src/client/shell/geometry.ts +68 -0
- package/src/client/shell/host.ts +492 -0
- package/src/client/shell/icons.tsx +34 -0
- package/src/client/shell/logFeed.ts +102 -0
- package/src/client/shell/main.tsx +22 -0
- package/src/client/shell/permissionCopy.ts +169 -0
- package/src/client/shell/plugins.ts +266 -0
- package/src/client/shell/popout.ts +185 -0
- package/src/client/shell/popover.ts +27 -0
- package/src/client/shell/shell.css +878 -0
- package/src/client/shell/statusTime.ts +31 -0
- package/src/client/shell/tooltips.ts +104 -0
- package/src/client/virtual.d.ts +21 -0
- package/src/devices/catalog.ts +223 -0
- package/src/devices/types.ts +83 -0
- package/src/node/appInfo.ts +186 -0
- package/src/node/appSource.ts +138 -0
- package/src/node/config.ts +180 -0
- package/src/node/control.ts +144 -0
- package/src/node/doctor.ts +199 -0
- package/src/node/events/cli.ts +206 -0
- package/src/node/events/shared.ts +238 -0
- package/src/node/events/store.ts +266 -0
- package/src/node/index.ts +5 -0
- package/src/node/overrides/define.ts +56 -0
- package/src/node/overrides/packages.ts +136 -0
- package/src/node/overrides/platformPatch.ts +56 -0
- package/src/node/overrides/plugin.ts +166 -0
- package/src/node/overrides/registry.ts +258 -0
- package/src/node/paths.ts +18 -0
- package/src/node/platform.ts +171 -0
- package/src/node/plugins.ts +98 -0
- package/src/node/proxy.ts +421 -0
- package/src/node/server.ts +253 -0
- package/src/plugin/index.ts +33 -0
- package/src/plugin/types.ts +127 -0
- package/src/plugin/ui.tsx +562 -0
- package/src/runtime/clock.ts +112 -0
- package/src/runtime/draw.ts +196 -0
- package/src/runtime/index.ts +513 -0
- package/src/runtime/media.ts +379 -0
- package/src/runtime/records.ts +76 -0
- package/src/runtime/timezone.ts +81 -0
|
@@ -0,0 +1,365 @@
|
|
|
1
|
+
import { HardDrivesIcon, LockIcon, PlusIcon, TrashIcon } from '@phosphor-icons/react'
|
|
2
|
+
import { useState } from 'react'
|
|
3
|
+
import { app, getHost } from 'web-sim/runtime'
|
|
4
|
+
import {
|
|
5
|
+
CodeBlock,
|
|
6
|
+
defineShellPlugin,
|
|
7
|
+
Field,
|
|
8
|
+
FileButton,
|
|
9
|
+
IconButton,
|
|
10
|
+
List,
|
|
11
|
+
ListItem,
|
|
12
|
+
Row,
|
|
13
|
+
Section,
|
|
14
|
+
TextField,
|
|
15
|
+
ImagePreview,
|
|
16
|
+
Tree,
|
|
17
|
+
type TreeNode,
|
|
18
|
+
} from 'web-sim/shell'
|
|
19
|
+
import {
|
|
20
|
+
children,
|
|
21
|
+
clearFiles,
|
|
22
|
+
descendants,
|
|
23
|
+
exists,
|
|
24
|
+
fromBase64,
|
|
25
|
+
isDirectory,
|
|
26
|
+
isFile,
|
|
27
|
+
isSystemDirectory,
|
|
28
|
+
makeDirectory,
|
|
29
|
+
mimeType,
|
|
30
|
+
nameOf,
|
|
31
|
+
node,
|
|
32
|
+
parentOf,
|
|
33
|
+
readFile,
|
|
34
|
+
remove,
|
|
35
|
+
roots,
|
|
36
|
+
size,
|
|
37
|
+
store,
|
|
38
|
+
toBase64,
|
|
39
|
+
writeFile,
|
|
40
|
+
} from './files.ts'
|
|
41
|
+
import { clearItems, deleteItem, listItems, readItem, writeItem } from './secure.ts'
|
|
42
|
+
import { appKeys, describeReset, localStorageChanged, parseStores, resetApp } from './reset.ts'
|
|
43
|
+
import { INITIAL_STORAGE, type StorageState } from './state.ts'
|
|
44
|
+
|
|
45
|
+
function bytes(count: number): string {
|
|
46
|
+
if (count < 1024) {
|
|
47
|
+
return `${count} B`
|
|
48
|
+
}
|
|
49
|
+
const units = ['KB', 'MB', 'GB']
|
|
50
|
+
let value = count / 1024
|
|
51
|
+
let unit = 0
|
|
52
|
+
while (value >= 1024 && unit < units.length - 1) {
|
|
53
|
+
value /= 1024
|
|
54
|
+
unit += 1
|
|
55
|
+
}
|
|
56
|
+
return `${value < 10 ? value.toFixed(1) : Math.round(value)} ${units[unit]}`
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** A path from the CLI: absolute, or inside Documents. */
|
|
60
|
+
function resolvePath(path: string): string {
|
|
61
|
+
return path.startsWith('/') ? path.replace(/\/+$/, '') || '/' : `${roots().document}/${path}`.replace(/\/+$/, '')
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function asText(data: Uint8Array): string | null {
|
|
65
|
+
try {
|
|
66
|
+
const text = new TextDecoder('utf-8', { fatal: true }).decode(data)
|
|
67
|
+
return /[\0-\x08\x0e-\x1f]/.test(text.slice(0, 4096)) ? null : text
|
|
68
|
+
} catch {
|
|
69
|
+
return null
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** The app's container: its documents and caches, as the device names them. */
|
|
74
|
+
function tree(): TreeNode[] {
|
|
75
|
+
const build = (path: string, label: string): TreeNode => {
|
|
76
|
+
const folder = isDirectory(path)
|
|
77
|
+
const total = size(path)
|
|
78
|
+
return {
|
|
79
|
+
id: path,
|
|
80
|
+
label,
|
|
81
|
+
detail: folder ? `${children(path).length} items · ${bytes(total)}` : bytes(total),
|
|
82
|
+
children: folder ? children(path).map((child) => build(child, nameOf(child))) : undefined,
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
const { document, cache } = roots()
|
|
86
|
+
return [build(document, nameOf(document)), build(cache, nameOf(cache))]
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function Preview({ path }: { path: string }) {
|
|
90
|
+
if (!isFile(path)) {
|
|
91
|
+
return null
|
|
92
|
+
}
|
|
93
|
+
const data = readFile(path)
|
|
94
|
+
const mime = mimeType(path)
|
|
95
|
+
const timeZone = getHost().plugins.get('locale')?.timeZone
|
|
96
|
+
const modified = new Date(node(path)?.modified ?? 0).toLocaleString('en-US', {
|
|
97
|
+
dateStyle: 'medium',
|
|
98
|
+
timeStyle: 'short',
|
|
99
|
+
...(typeof timeZone === 'string' ? { timeZone } : {}),
|
|
100
|
+
})
|
|
101
|
+
const text = mime.startsWith('image/') ? null : asText(data)
|
|
102
|
+
return (
|
|
103
|
+
<Field label={nameOf(path)} value={`${bytes(data.length)} · ${modified}`}>
|
|
104
|
+
{mime.startsWith('image/') ? (
|
|
105
|
+
<ImagePreview src={`data:${mime};base64,${toBase64(data)}`} label={nameOf(path)} />
|
|
106
|
+
) : text !== null ? (
|
|
107
|
+
<CodeBlock>{text.length > 20000 ? `${text.slice(0, 20000)}\n…` : text}</CodeBlock>
|
|
108
|
+
) : (
|
|
109
|
+
<CodeBlock>{`${mime}, binary`}</CodeBlock>
|
|
110
|
+
)}
|
|
111
|
+
</Field>
|
|
112
|
+
)
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
export default defineShellPlugin<StorageState>({
|
|
116
|
+
id: 'storage',
|
|
117
|
+
title: 'Storage',
|
|
118
|
+
icon: HardDrivesIcon,
|
|
119
|
+
initialState: INITIAL_STORAGE,
|
|
120
|
+
persist: false,
|
|
121
|
+
logIgnore: ['localStorage'],
|
|
122
|
+
setup: (context) => {
|
|
123
|
+
// Loads the stored files, so the panel shows them before the app asks for any.
|
|
124
|
+
void store()
|
|
125
|
+
// The app's writes to localStorage, from its frame, reach the shell's window as `storage` events.
|
|
126
|
+
const onStorage = (event: StorageEvent) => {
|
|
127
|
+
if (event.storageArea === localStorage && (event.key === null || !/^web-sim[:-]/.test(event.key))) {
|
|
128
|
+
context.set({ localStorage: context.state().localStorage + 1 })
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
window.addEventListener('storage', onStorage)
|
|
132
|
+
return () => window.removeEventListener('storage', onStorage)
|
|
133
|
+
},
|
|
134
|
+
actions: {
|
|
135
|
+
/**
|
|
136
|
+
* Deletes and reinstalls the app, as far as its data goes: stops it, clears its stores, and launches it again.
|
|
137
|
+
* `{"only": [...]}` picks stores from secureStore, files, localStorage (also asyncStorage), databases, cookies
|
|
138
|
+
* and permissions; without it, all of them.
|
|
139
|
+
*/
|
|
140
|
+
reset: async (context, args) => {
|
|
141
|
+
const stores = parseStores(args)
|
|
142
|
+
const result = await resetApp(stores)
|
|
143
|
+
context.log('reset', describeReset(result), { stores, removed: result }, { level: 'info' })
|
|
144
|
+
return result
|
|
145
|
+
},
|
|
146
|
+
/** The app's localStorage (AsyncStorage, MMKV on web), web-sim's own keys left out. */
|
|
147
|
+
localStorage: () => Object.fromEntries(appKeys(localStorage).map((key) => [key, localStorage.getItem(key)])),
|
|
148
|
+
/** `{key, value}` */
|
|
149
|
+
setLocalStorage: (_context, args) => {
|
|
150
|
+
const { key, value } = args as { key: string; value: string }
|
|
151
|
+
localStorage.setItem(key, value)
|
|
152
|
+
localStorageChanged()
|
|
153
|
+
},
|
|
154
|
+
/** `{key}` */
|
|
155
|
+
deleteLocalStorage: (_context, args) => {
|
|
156
|
+
localStorage.removeItem((args as { key: string }).key)
|
|
157
|
+
localStorageChanged()
|
|
158
|
+
},
|
|
159
|
+
secureItems: () => listItems(),
|
|
160
|
+
/** `{key, value, service?, requireAuthentication?}` */
|
|
161
|
+
setSecureItem: (_context, args) => {
|
|
162
|
+
const { key, value, service, requireAuthentication } = args as { key: string; value: string; service?: string; requireAuthentication?: boolean }
|
|
163
|
+
writeItem(key, value, { service, requireAuthentication })
|
|
164
|
+
return readItem(key, service)
|
|
165
|
+
},
|
|
166
|
+
/** `{key, service?}` */
|
|
167
|
+
deleteSecureItem: (_context, args) => {
|
|
168
|
+
const { key, service } = args as { key: string; service?: string }
|
|
169
|
+
deleteItem(key, service)
|
|
170
|
+
},
|
|
171
|
+
/** Every file and folder the app made, with sizes. */
|
|
172
|
+
files: async () => {
|
|
173
|
+
await store().ready
|
|
174
|
+
const all = [...descendants(roots().document), ...descendants(roots().cache)]
|
|
175
|
+
return all.map((path) => ({ path, kind: node(path)?.kind, size: size(path) }))
|
|
176
|
+
},
|
|
177
|
+
/** `{path}`: absolute, or inside Documents. Text as it is; anything else as base64. */
|
|
178
|
+
readFile: async (_context, args) => {
|
|
179
|
+
await store().ready
|
|
180
|
+
const data = readFile(resolvePath((args as { path: string }).path))
|
|
181
|
+
const text = asText(data)
|
|
182
|
+
return text !== null ? { text } : { base64: toBase64(data) }
|
|
183
|
+
},
|
|
184
|
+
/** `{path, text}` or `{path, base64}`: absolute, or inside Documents; folders made as needed. */
|
|
185
|
+
writeFile: async (_context, args) => {
|
|
186
|
+
await store().ready
|
|
187
|
+
const { path, text, base64 } = args as { path: string; text?: string; base64?: string }
|
|
188
|
+
const target = resolvePath(path)
|
|
189
|
+
makeDirectory(parentOf(target), true)
|
|
190
|
+
writeFile(target, base64 !== undefined ? fromBase64(base64) : new TextEncoder().encode(text ?? ''))
|
|
191
|
+
return { path: target, size: size(target) }
|
|
192
|
+
},
|
|
193
|
+
/** `{path}` */
|
|
194
|
+
deleteFile: async (_context, args) => {
|
|
195
|
+
await store().ready
|
|
196
|
+
const target = resolvePath((args as { path: string }).path)
|
|
197
|
+
if (exists(target)) {
|
|
198
|
+
remove(target)
|
|
199
|
+
}
|
|
200
|
+
},
|
|
201
|
+
/** Wipes the secure store and the files. `reset` clears the rest of the app's data too. */
|
|
202
|
+
clear: () => {
|
|
203
|
+
clearItems()
|
|
204
|
+
clearFiles()
|
|
205
|
+
},
|
|
206
|
+
},
|
|
207
|
+
Panel: ({ run }) => {
|
|
208
|
+
const [selectedItem, setSelectedItem] = useState<{ key: string; service: string | null } | null>(null)
|
|
209
|
+
const [selectedPath, setSelectedPath] = useState<string | null>(null)
|
|
210
|
+
const [selectedKey, setSelectedKey] = useState<string | null>(null)
|
|
211
|
+
const dependencies = app().dependencies ?? []
|
|
212
|
+
const localKeys = appKeys(localStorage)
|
|
213
|
+
const usesLocalStorage = dependencies.length === 0 || dependencies.includes('@react-native-async-storage/async-storage') || dependencies.includes('react-native-mmkv')
|
|
214
|
+
const showLocal = usesLocalStorage || localKeys.length > 0
|
|
215
|
+
const selectedValue = selectedKey === null ? null : localStorage.getItem(selectedKey)
|
|
216
|
+
const showSecure = dependencies.length === 0 || dependencies.includes('expo-secure-store')
|
|
217
|
+
const showFiles = dependencies.length === 0 || dependencies.includes('expo-file-system')
|
|
218
|
+
const items = listItems()
|
|
219
|
+
const editing = selectedItem === null ? null : readItem(selectedItem.key, selectedItem.service)
|
|
220
|
+
const rename = (key: string) => {
|
|
221
|
+
if (editing === null || key === editing.key || !/^[\w.-]+$/.test(key)) {
|
|
222
|
+
return
|
|
223
|
+
}
|
|
224
|
+
deleteItem(editing.key, editing.service)
|
|
225
|
+
writeItem(key, editing.value, { service: editing.service, requireAuthentication: editing.requireAuthentication })
|
|
226
|
+
setSelectedItem({ key, service: editing.service })
|
|
227
|
+
}
|
|
228
|
+
const upload = async (files: File[]) => {
|
|
229
|
+
for (const file of files) {
|
|
230
|
+
const path = `${roots().document}/${file.name}`
|
|
231
|
+
makeDirectory(roots().document, true)
|
|
232
|
+
writeFile(path, new Uint8Array(await file.arrayBuffer()))
|
|
233
|
+
setSelectedPath(path)
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
return (
|
|
237
|
+
<>
|
|
238
|
+
{showSecure && (
|
|
239
|
+
<Section
|
|
240
|
+
title={`Secure store · ${items.length}`}
|
|
241
|
+
actions={
|
|
242
|
+
<IconButton
|
|
243
|
+
icon={PlusIcon}
|
|
244
|
+
title="Add item"
|
|
245
|
+
onClick={() => {
|
|
246
|
+
let key = 'new_item'
|
|
247
|
+
for (let n = 2; readItem(key) !== null; n++) {
|
|
248
|
+
key = `new_item_${n}`
|
|
249
|
+
}
|
|
250
|
+
writeItem(key, '')
|
|
251
|
+
setSelectedItem({ key, service: null })
|
|
252
|
+
}}
|
|
253
|
+
/>
|
|
254
|
+
}
|
|
255
|
+
>
|
|
256
|
+
<List empty="No items">
|
|
257
|
+
{items.map((item) => {
|
|
258
|
+
const id = `${item.service ?? ''}:${item.key}`
|
|
259
|
+
return (
|
|
260
|
+
<ListItem
|
|
261
|
+
key={id}
|
|
262
|
+
mono
|
|
263
|
+
title={item.service ? `${item.service} · ${item.key}` : item.key}
|
|
264
|
+
detail={item.value === '' ? '""' : item.value}
|
|
265
|
+
lead={item.requireAuthentication ? <LockIcon size={11} /> : undefined}
|
|
266
|
+
selected={editing !== null && `${editing.service ?? ''}:${editing.key}` === id}
|
|
267
|
+
onClick={() => setSelectedItem({ key: item.key, service: item.service })}
|
|
268
|
+
actions={
|
|
269
|
+
<IconButton
|
|
270
|
+
icon={TrashIcon}
|
|
271
|
+
title={`Delete ${item.key}`}
|
|
272
|
+
onClick={() => {
|
|
273
|
+
deleteItem(item.key, item.service)
|
|
274
|
+
setSelectedItem(null)
|
|
275
|
+
}}
|
|
276
|
+
/>
|
|
277
|
+
}
|
|
278
|
+
/>
|
|
279
|
+
)
|
|
280
|
+
})}
|
|
281
|
+
</List>
|
|
282
|
+
{editing !== null && (
|
|
283
|
+
<>
|
|
284
|
+
<Field label="Key">
|
|
285
|
+
<TextField label="Key" value={editing.key} onChange={rename} />
|
|
286
|
+
</Field>
|
|
287
|
+
<Field label="Value">
|
|
288
|
+
<TextField
|
|
289
|
+
label="Value"
|
|
290
|
+
multiline
|
|
291
|
+
value={editing.value}
|
|
292
|
+
onChange={(value) => writeItem(editing.key, value, { service: editing.service, requireAuthentication: editing.requireAuthentication })}
|
|
293
|
+
/>
|
|
294
|
+
</Field>
|
|
295
|
+
</>
|
|
296
|
+
)}
|
|
297
|
+
</Section>
|
|
298
|
+
)}
|
|
299
|
+
{showFiles && (
|
|
300
|
+
<Section title="Files">
|
|
301
|
+
<Tree
|
|
302
|
+
nodes={tree()}
|
|
303
|
+
selected={selectedPath}
|
|
304
|
+
defaultExpanded={[roots().document]}
|
|
305
|
+
onSelect={(target) => setSelectedPath(target.id)}
|
|
306
|
+
actions={(target) =>
|
|
307
|
+
isSystemDirectory(target.id) ? undefined : (
|
|
308
|
+
<IconButton
|
|
309
|
+
icon={TrashIcon}
|
|
310
|
+
title={`Delete ${target.label}`}
|
|
311
|
+
onClick={() => {
|
|
312
|
+
remove(target.id)
|
|
313
|
+
if (selectedPath !== null && (selectedPath === target.id || selectedPath.startsWith(`${target.id}/`))) {
|
|
314
|
+
setSelectedPath(null)
|
|
315
|
+
}
|
|
316
|
+
}}
|
|
317
|
+
/>
|
|
318
|
+
)
|
|
319
|
+
}
|
|
320
|
+
/>
|
|
321
|
+
<Row>
|
|
322
|
+
<FileButton multiple onFiles={(files) => void upload(files)}>
|
|
323
|
+
Upload to Documents
|
|
324
|
+
</FileButton>
|
|
325
|
+
</Row>
|
|
326
|
+
{selectedPath !== null && <Preview path={selectedPath} />}
|
|
327
|
+
</Section>
|
|
328
|
+
)}
|
|
329
|
+
{showLocal && (
|
|
330
|
+
<Section title={`AsyncStorage · localStorage · ${localKeys.length}`}>
|
|
331
|
+
<List empty="No keys">
|
|
332
|
+
{localKeys.map((key) => (
|
|
333
|
+
<ListItem
|
|
334
|
+
key={key}
|
|
335
|
+
mono
|
|
336
|
+
title={key}
|
|
337
|
+
detail={(localStorage.getItem(key) ?? '').slice(0, 200)}
|
|
338
|
+
selected={key === selectedKey}
|
|
339
|
+
onClick={() => setSelectedKey(key === selectedKey ? null : key)}
|
|
340
|
+
actions={
|
|
341
|
+
<IconButton
|
|
342
|
+
icon={TrashIcon}
|
|
343
|
+
title={`Delete ${key}`}
|
|
344
|
+
onClick={() => {
|
|
345
|
+
void run('deleteLocalStorage', { key })
|
|
346
|
+
if (key === selectedKey) {
|
|
347
|
+
setSelectedKey(null)
|
|
348
|
+
}
|
|
349
|
+
}}
|
|
350
|
+
/>
|
|
351
|
+
}
|
|
352
|
+
/>
|
|
353
|
+
))}
|
|
354
|
+
</List>
|
|
355
|
+
{selectedKey !== null && selectedValue !== null && (
|
|
356
|
+
<Field label="Value">
|
|
357
|
+
<TextField label="Value" multiline value={selectedValue} onChange={(value) => void run('setLocalStorage', { key: selectedKey, value })} />
|
|
358
|
+
</Field>
|
|
359
|
+
)}
|
|
360
|
+
</Section>
|
|
361
|
+
)}
|
|
362
|
+
</>
|
|
363
|
+
)
|
|
364
|
+
},
|
|
365
|
+
})
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The storage panel's view of the app's data. The data itself is too big for state and stays where the overrides
|
|
3
|
+
* keep it (the secure store and the app's own keys in localStorage, files in memory and IndexedDB); the state counts changes, so the panel
|
|
4
|
+
* redraws when the app or the CLI changes either.
|
|
5
|
+
*/
|
|
6
|
+
export type StorageState = {
|
|
7
|
+
secureStore: number
|
|
8
|
+
files: number
|
|
9
|
+
/** The app's localStorage: AsyncStorage's and MMKV's web builds keep their keys there. */
|
|
10
|
+
localStorage: number
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export const INITIAL_STORAGE: StorageState = { secureStore: 0, files: 0, localStorage: 0 }
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import { getState, subscribe } from '../../runtime/index.ts'
|
|
2
|
+
import rnwHash from 'react-native-web/dist/exports/StyleSheet/compiler/hash'
|
|
3
|
+
import { ANDROID_FONTS, installFonts } from './fonts.ts'
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* The app's document is the device's screen: the frame is exactly the screen's size, so the window, the visual
|
|
7
|
+
* viewport and `Dimensions` all report it. Text without a font family, or with one that has no glyph, falls back to
|
|
8
|
+
* the platform's system font, as it does on the device (the react-native-web override points its font stack at
|
|
9
|
+
* `--web-sim-system-font`). As on a phone, text can't be selected outside text fields (React Native's `selectable`
|
|
10
|
+
* sets it back per Text), there are no focus rings, and a long press shows no callout.
|
|
11
|
+
*/
|
|
12
|
+
const { device } = getState()
|
|
13
|
+
|
|
14
|
+
if (device.platform === 'android') {
|
|
15
|
+
const fonts = document.createElement('link')
|
|
16
|
+
fonts.rel = 'stylesheet'
|
|
17
|
+
fonts.href = ANDROID_FONTS
|
|
18
|
+
document.head.append(fonts)
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** react-native-web's class for a style value, as its compiler names it in development. */
|
|
22
|
+
const rnwClass = (property: string, value: string) => `r-${property}-${rnwHash(property + value)}`
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Yoga measures a row's children against the width the row has, so text in a non-shrinking child wraps at the
|
|
26
|
+
* screen's edge. CSS sizes a flex item at its longest unwrapped line, so the same child overflows the row, and a
|
|
27
|
+
* centred row pushes it off the screen's left edge. `fit-content` is Yoga's measure: as wide as the content, at most
|
|
28
|
+
* the space there is. `:where` keeps it below any width the app sets. An absolutely positioned child is left out: it
|
|
29
|
+
* isn't a flex item, and with `left` and `right` set it spans the row, as it does in Yoga. Its position may be a class
|
|
30
|
+
* or, from an animated style (Reanimated's), inline.
|
|
31
|
+
*/
|
|
32
|
+
const rows = [rnwClass('flexDirection', 'row'), rnwClass('flexDirection', 'row-reverse')].map((name) => `.${name}`).join(', ')
|
|
33
|
+
const absolute = `.${rnwClass('position', 'absolute')}, [style*="position: absolute"]`
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Yoga also measures a column's children against the height the column has (when the column doesn't scroll), so a
|
|
37
|
+
* view with `flexGrow` and no height, such as a screen's page container, is never taller than its parent, and a
|
|
38
|
+
* ScrollView inside it is bounded and scrolls. CSS sizes that view at its content's full height (a `flex: 1` inside
|
|
39
|
+
* it resolves its 0% basis to its content), and with React Native's `flexShrink: 0` it stays that tall: the
|
|
40
|
+
* ScrollView grows to fit its content and never scrolls. `max-height: 100%` is Yoga's bound. Views with a height or
|
|
41
|
+
* minimum height of their own, and absolutely positioned ones, keep theirs.
|
|
42
|
+
*/
|
|
43
|
+
const notColumns = [...rows.split(', '), `.${rnwClass('overflowY', 'auto')}`, `.${rnwClass('overflowY', 'scroll')}`].map((name) => `:not(${name})`).join('')
|
|
44
|
+
const growing = `[class*="r-flexGrow-"]:not(${absolute}):not([class*="r-height-"]):not([class*="r-minHeight-"]):not([style*="height"])`
|
|
45
|
+
|
|
46
|
+
const style = document.createElement('style')
|
|
47
|
+
style.textContent = `
|
|
48
|
+
html, body, #root { width: 100%; height: 100%; margin: 0; padding: 0; overflow: hidden; }
|
|
49
|
+
body { font-family: var(--web-sim-system-font); -webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent; overscroll-behavior: none; }
|
|
50
|
+
#root { display: flex; flex-direction: column; }
|
|
51
|
+
::-webkit-scrollbar { display: none; }
|
|
52
|
+
* {
|
|
53
|
+
scrollbar-width: none;
|
|
54
|
+
user-select: none;
|
|
55
|
+
-webkit-user-select: none;
|
|
56
|
+
-webkit-touch-callout: none;
|
|
57
|
+
-webkit-tap-highlight-color: transparent;
|
|
58
|
+
}
|
|
59
|
+
*:focus, *:focus-visible { outline: none !important; }
|
|
60
|
+
input, textarea, [contenteditable='true'], [contenteditable=''] { user-select: text; -webkit-user-select: text; }
|
|
61
|
+
input, textarea, button, select { font-family: inherit; }
|
|
62
|
+
:where(${rows}) > :where(:not(${absolute})) { width: fit-content; }
|
|
63
|
+
:where(${notColumns}) > :where(${growing}) { max-height: 100%; }
|
|
64
|
+
`
|
|
65
|
+
document.head.append(style)
|
|
66
|
+
installFonts(device.platform)
|
|
67
|
+
document.documentElement.dataset.webSimPlatform = device.platform
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* `prefers-color-scheme` answers with the app's appearance (the device's, unless the app pins one), so react-native-web's `Appearance` and
|
|
71
|
+
* `useColorScheme`, and any library that asks `matchMedia`, see it and hear when it changes. Other queries go to the
|
|
72
|
+
* browser.
|
|
73
|
+
*/
|
|
74
|
+
function simulateColorScheme() {
|
|
75
|
+
const browserMatchMedia = window.matchMedia.bind(window)
|
|
76
|
+
window.matchMedia = (query: string): MediaQueryList => {
|
|
77
|
+
const scheme = /prefers-color-scheme:\s*(dark|light)/.exec(query)?.[1]
|
|
78
|
+
if (scheme === undefined) {
|
|
79
|
+
return browserMatchMedia(query)
|
|
80
|
+
}
|
|
81
|
+
const listeners = new Set<(event: MediaQueryListEvent) => void>()
|
|
82
|
+
const list = {
|
|
83
|
+
media: query,
|
|
84
|
+
onchange: null as ((event: MediaQueryListEvent) => void) | null,
|
|
85
|
+
get matches() {
|
|
86
|
+
return getState().appAppearance === scheme
|
|
87
|
+
},
|
|
88
|
+
addListener: (listener: (event: MediaQueryListEvent) => void) => listeners.add(listener),
|
|
89
|
+
removeListener: (listener: (event: MediaQueryListEvent) => void) => listeners.delete(listener),
|
|
90
|
+
addEventListener: (_: string, listener: (event: MediaQueryListEvent) => void) => listeners.add(listener),
|
|
91
|
+
removeEventListener: (_: string, listener: (event: MediaQueryListEvent) => void) => listeners.delete(listener),
|
|
92
|
+
dispatchEvent: () => true,
|
|
93
|
+
}
|
|
94
|
+
let last = list.matches
|
|
95
|
+
subscribe(() => {
|
|
96
|
+
if (list.matches === last) {
|
|
97
|
+
return
|
|
98
|
+
}
|
|
99
|
+
last = list.matches
|
|
100
|
+
const event = { matches: last, media: query } as MediaQueryListEvent
|
|
101
|
+
listeners.forEach((listener) => listener(event))
|
|
102
|
+
list.onchange?.(event)
|
|
103
|
+
})
|
|
104
|
+
return list as unknown as MediaQueryList
|
|
105
|
+
}
|
|
106
|
+
const applyScheme = () => {
|
|
107
|
+
document.documentElement.style.colorScheme = getState().appAppearance
|
|
108
|
+
}
|
|
109
|
+
applyScheme()
|
|
110
|
+
subscribe(applyScheme)
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
simulateColorScheme()
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Focus in the app follows the app alone, as on a phone, not whether the browser window or the app's frame has the
|
|
3
|
+
* system focus. Browsers tie the two together:
|
|
4
|
+
*
|
|
5
|
+
* - `focus()` while the window is in the background (a script or the CLI driving the tab while the person is in
|
|
6
|
+
* their terminal) moves `document.activeElement` but fires no focus events, so neither the app's `onFocus` nor the
|
|
7
|
+
* shell's keyboard sees the field focus. The events come later, when the person returns to the window.
|
|
8
|
+
* - Leaving the window, or clicking the shell around the phone (its toolbar, a panel), blurs the focused field;
|
|
9
|
+
* returning focuses it again.
|
|
10
|
+
*
|
|
11
|
+
* So: focus events that only say the window lost or got back the system focus are stopped before the app sees them,
|
|
12
|
+
* and while the window is in the background, `focus()` and `blur()` announce the change themselves.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
let announced: Element | null = activeIn(document)
|
|
16
|
+
let announcing = false
|
|
17
|
+
/** The element that just got focus and the first of its `focus` and `focusin`: the other belongs to the same change. */
|
|
18
|
+
let arriving: { target: Element; type: string } | null = null
|
|
19
|
+
|
|
20
|
+
function activeIn(doc: Document): Element | null {
|
|
21
|
+
const active = doc.activeElement
|
|
22
|
+
return active === null || active === doc.body || active === doc.documentElement ? null : active
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function onFocusEvent(event: FocusEvent): void {
|
|
26
|
+
if (announcing || !(event.target instanceof Element)) {
|
|
27
|
+
return
|
|
28
|
+
}
|
|
29
|
+
const isFocus = event.type === 'focus' || event.type === 'focusin'
|
|
30
|
+
if (event.target === document.activeElement) {
|
|
31
|
+
if (isFocus && arriving !== null && arriving.target === event.target && arriving.type !== event.type) {
|
|
32
|
+
arriving = null
|
|
33
|
+
return
|
|
34
|
+
}
|
|
35
|
+
// The element keeps focus while its window loses it, and gets it back when its window does.
|
|
36
|
+
if (!isFocus || event.target === announced) {
|
|
37
|
+
event.stopImmediatePropagation()
|
|
38
|
+
return
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
if (isFocus) {
|
|
42
|
+
announced = event.target
|
|
43
|
+
arriving = { target: event.target, type: event.type }
|
|
44
|
+
} else if (event.type === 'focusout' && announced === event.target) {
|
|
45
|
+
announced = null
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function dispatchFocus(target: Element, type: string, bubbles: boolean, relatedTarget: Element | null): void {
|
|
50
|
+
// A frame being torn down (the device switched) loses its globals while React still restores focus in it.
|
|
51
|
+
if (typeof FocusEvent !== 'function') {
|
|
52
|
+
return
|
|
53
|
+
}
|
|
54
|
+
target.dispatchEvent(new FocusEvent(type, { bubbles, composed: true, relatedTarget }))
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** Fires the focus events the browser held back for the change from `announced` to the active element. */
|
|
58
|
+
function announce(): void {
|
|
59
|
+
if (document.hasFocus()) {
|
|
60
|
+
return
|
|
61
|
+
}
|
|
62
|
+
const next = activeIn(document)
|
|
63
|
+
const previous = announced?.isConnected ? announced : null
|
|
64
|
+
if (next === previous) {
|
|
65
|
+
return
|
|
66
|
+
}
|
|
67
|
+
announced = next
|
|
68
|
+
announcing = true
|
|
69
|
+
try {
|
|
70
|
+
if (previous !== null) {
|
|
71
|
+
dispatchFocus(previous, 'blur', false, next)
|
|
72
|
+
dispatchFocus(previous, 'focusout', true, next)
|
|
73
|
+
}
|
|
74
|
+
if (next !== null) {
|
|
75
|
+
dispatchFocus(next, 'focus', false, previous)
|
|
76
|
+
dispatchFocus(next, 'focusin', true, previous)
|
|
77
|
+
}
|
|
78
|
+
} finally {
|
|
79
|
+
announcing = false
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
for (const type of ['focus', 'blur', 'focusin', 'focusout']) {
|
|
84
|
+
window.addEventListener(type, onFocusEvent as EventListener, true)
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
for (const prototype of [HTMLElement.prototype, SVGElement.prototype]) {
|
|
88
|
+
const { focus, blur } = prototype
|
|
89
|
+
prototype.focus = function (this: HTMLElement, options?: FocusOptions) {
|
|
90
|
+
focus.call(this, options)
|
|
91
|
+
announce()
|
|
92
|
+
}
|
|
93
|
+
prototype.blur = function (this: HTMLElement) {
|
|
94
|
+
blur.call(this)
|
|
95
|
+
announce()
|
|
96
|
+
}
|
|
97
|
+
}
|