@a16n/web-sim 0.0.0-stage → 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.
Files changed (281) hide show
  1. package/README.md +13 -2
  2. package/bin/cli.ts +255 -0
  3. package/bin/web-sim.js +39 -0
  4. package/overrides/@backpackapp-io/react-native-toast/overrides.ts +15 -0
  5. package/overrides/@react-native-firebase/app/overrides.ts +34 -0
  6. package/overrides/@tiktok-for-business/react-native-sdk/NativeTiktokSdk.ts +29 -0
  7. package/overrides/@tiktok-for-business/react-native-sdk/overrides.ts +9 -0
  8. package/overrides/expo-application/ExpoApplication.ts +81 -0
  9. package/overrides/expo-application/overrides.ts +16 -0
  10. package/overrides/expo-background-task/ExpoBackgroundTaskModule.ts +30 -0
  11. package/overrides/expo-background-task/overrides.ts +9 -0
  12. package/overrides/expo-constants/ExponentConstants.ts +66 -0
  13. package/overrides/expo-constants/overrides.ts +9 -0
  14. package/overrides/expo-device/ExpoDevice.ts +104 -0
  15. package/overrides/expo-device/overrides.ts +9 -0
  16. package/overrides/expo-glass-effect/GlassContainer.tsx +9 -0
  17. package/overrides/expo-glass-effect/GlassView.tsx +95 -0
  18. package/overrides/expo-glass-effect/availability.ts +14 -0
  19. package/overrides/expo-glass-effect/overrides.ts +14 -0
  20. package/overrides/expo-haptics/Haptics.ts +67 -0
  21. package/overrides/expo-haptics/overrides.ts +5 -0
  22. package/overrides/expo-mail-composer/ExpoMailComposer.ts +40 -0
  23. package/overrides/expo-mail-composer/overrides.ts +9 -0
  24. package/overrides/expo-mesh-gradient/MeshGradientView.tsx +44 -0
  25. package/overrides/expo-mesh-gradient/overrides.ts +9 -0
  26. package/overrides/expo-modules-core/overrides.ts +12 -0
  27. package/overrides/expo-navigation-bar/ExpoNavigationBar.ts +29 -0
  28. package/overrides/expo-navigation-bar/NavigationBar.ts +43 -0
  29. package/overrides/expo-navigation-bar/overrides.ts +12 -0
  30. package/overrides/expo-router/NativeStackView.tsx +74 -0
  31. package/overrides/expo-router/NativeTabsView.tsx +263 -0
  32. package/overrides/expo-router/SafeAreaProvider.ts +6 -0
  33. package/overrides/expo-router/overrides.ts +23 -0
  34. package/overrides/expo-router/statusBarRequests.ts +51 -0
  35. package/overrides/expo-screen-orientation/ExpoScreenOrientation.ts +173 -0
  36. package/overrides/expo-screen-orientation/overrides.ts +21 -0
  37. package/overrides/expo-sharing/SharingNativeModule.ts +21 -0
  38. package/overrides/expo-sharing/overrides.ts +9 -0
  39. package/overrides/expo-sms/ExpoSMS.ts +24 -0
  40. package/overrides/expo-sms/overrides.ts +9 -0
  41. package/overrides/expo-splash-screen/SplashScreen.ts +25 -0
  42. package/overrides/expo-splash-screen/overrides.ts +5 -0
  43. package/overrides/expo-status-bar/StatusBar.tsx +44 -0
  44. package/overrides/expo-status-bar/overrides.ts +5 -0
  45. package/overrides/expo-store-review/ExpoStoreReview.ts +18 -0
  46. package/overrides/expo-store-review/overrides.ts +9 -0
  47. package/overrides/expo-symbols/SymbolView.tsx +192 -0
  48. package/overrides/expo-symbols/overrides.ts +9 -0
  49. package/overrides/expo-system-ui/ExpoSystemUI.ts +34 -0
  50. package/overrides/expo-system-ui/overrides.ts +9 -0
  51. package/overrides/expo-task-manager/ExpoTaskManager.ts +39 -0
  52. package/overrides/expo-task-manager/overrides.ts +9 -0
  53. package/overrides/expo-tracking-transparency/ExpoTrackingTransparency.ts +30 -0
  54. package/overrides/expo-tracking-transparency/overrides.ts +11 -0
  55. package/overrides/expo-video/ExpoVideo.ts +16 -0
  56. package/overrides/expo-video/overrides.ts +9 -0
  57. package/overrides/lottie-react-native/LottieView.tsx +87 -0
  58. package/overrides/lottie-react-native/overrides.ts +9 -0
  59. package/overrides/react-native-pager-view/PagerViewNativeComponent.tsx +254 -0
  60. package/overrides/react-native-pager-view/overrides.ts +9 -0
  61. package/overrides/react-native-reanimated/overrides.ts +28 -0
  62. package/overrides/react-native-safe-area-context/NativeSafeAreaProvider.tsx +40 -0
  63. package/overrides/react-native-safe-area-context/overrides.ts +12 -0
  64. package/overrides/react-native-share/NativeRNShare.ts +22 -0
  65. package/overrides/react-native-share/overrides.ts +12 -0
  66. package/overrides/react-native-share/platform.ts +14 -0
  67. package/overrides/react-native-turbo-image/TurboImage.tsx +101 -0
  68. package/overrides/react-native-turbo-image/overrides.ts +9 -0
  69. package/overrides/react-native-web/RefreshControl.tsx +57 -0
  70. package/overrides/react-native-web/overrides.ts +157 -0
  71. package/overrides/react-native-web/useElementLayout.ts +152 -0
  72. package/package.json +80 -4
  73. package/plugins/battery/overrides/expo-battery/ExpoBattery.ts +57 -0
  74. package/plugins/battery/overrides/expo-battery/overrides.ts +9 -0
  75. package/plugins/battery/plugin.ts +8 -0
  76. package/plugins/battery/shell.tsx +56 -0
  77. package/plugins/battery/state.ts +11 -0
  78. package/plugins/biometrics/overrides/expo-local-authentication/ExpoLocalAuthentication.ts +90 -0
  79. package/plugins/biometrics/overrides/expo-local-authentication/overrides.ts +11 -0
  80. package/plugins/biometrics/plugin.ts +8 -0
  81. package/plugins/biometrics/shell.tsx +88 -0
  82. package/plugins/biometrics/state.ts +41 -0
  83. package/plugins/brightness/app.ts +5 -0
  84. package/plugins/brightness/overrides/expo-brightness/Brightness.ts +98 -0
  85. package/plugins/brightness/overrides/expo-brightness/overrides.ts +10 -0
  86. package/plugins/brightness/plugin.ts +9 -0
  87. package/plugins/brightness/shell.tsx +55 -0
  88. package/plugins/brightness/state.ts +19 -0
  89. package/plugins/calendar/calendar.ts +299 -0
  90. package/plugins/calendar/overrides/expo-calendar/ExpoCalendar.ts +358 -0
  91. package/plugins/calendar/overrides/expo-calendar/LegacyExpoCalendar.ts +163 -0
  92. package/plugins/calendar/overrides/expo-calendar/overrides.ts +14 -0
  93. package/plugins/calendar/plugin.ts +8 -0
  94. package/plugins/calendar/shell.tsx +123 -0
  95. package/plugins/camera/overrides/expo-camera/ExpoCamera.tsx +269 -0
  96. package/plugins/camera/overrides/expo-camera/ExpoCameraManager.ts +56 -0
  97. package/plugins/camera/overrides/expo-camera/overrides.ts +12 -0
  98. package/plugins/camera/plugin.ts +9 -0
  99. package/plugins/camera/shell.tsx +107 -0
  100. package/plugins/camera/state.ts +61 -0
  101. package/plugins/clipboard/overrides/expo-clipboard/ExpoClipboard.ts +138 -0
  102. package/plugins/clipboard/overrides/expo-clipboard/overrides.ts +9 -0
  103. package/plugins/clipboard/plugin.ts +8 -0
  104. package/plugins/clipboard/shell.tsx +122 -0
  105. package/plugins/clipboard/state.ts +93 -0
  106. package/plugins/clock/app.ts +25 -0
  107. package/plugins/clock/plugin.ts +8 -0
  108. package/plugins/clock/shell.tsx +225 -0
  109. package/plugins/clock/state.ts +12 -0
  110. package/plugins/contacts/contacts.ts +125 -0
  111. package/plugins/contacts/overrides/expo-contacts/ExpoContacts.ts +153 -0
  112. package/plugins/contacts/overrides/expo-contacts/ExpoContactsNext.ts +232 -0
  113. package/plugins/contacts/overrides/expo-contacts/overrides.ts +12 -0
  114. package/plugins/contacts/plugin.ts +8 -0
  115. package/plugins/contacts/shell.tsx +109 -0
  116. package/plugins/home-indicator/app.ts +5 -0
  117. package/plugins/home-indicator/plugin.ts +9 -0
  118. package/plugins/home-indicator/requests.ts +48 -0
  119. package/plugins/home-indicator/shell.tsx +68 -0
  120. package/plugins/home-indicator/state.ts +31 -0
  121. package/plugins/keep-awake/app.ts +5 -0
  122. package/plugins/keep-awake/overrides/expo-keep-awake/ExpoKeepAwake.ts +27 -0
  123. package/plugins/keep-awake/overrides/expo-keep-awake/overrides.ts +9 -0
  124. package/plugins/keep-awake/plugin.ts +9 -0
  125. package/plugins/keep-awake/shell.tsx +80 -0
  126. package/plugins/keep-awake/state.ts +12 -0
  127. package/plugins/keyboard/Keyboard.tsx +354 -0
  128. package/plugins/keyboard/app.ts +90 -0
  129. package/plugins/keyboard/controller.ts +384 -0
  130. package/plugins/keyboard/fields.ts +244 -0
  131. package/plugins/keyboard/icons.tsx +283 -0
  132. package/plugins/keyboard/keyboard.css +362 -0
  133. package/plugins/keyboard/layouts/android.ts +175 -0
  134. package/plugins/keyboard/layouts/index.ts +10 -0
  135. package/plugins/keyboard/layouts/ios.ts +219 -0
  136. package/plugins/keyboard/layouts/types.ts +80 -0
  137. package/plugins/keyboard/overrides/react-native-keyboard-controller/KeyboardController.tsx +470 -0
  138. package/plugins/keyboard/overrides/react-native-keyboard-controller/overrides.ts +9 -0
  139. package/plugins/keyboard/plugin.ts +13 -0
  140. package/plugins/keyboard/shell.tsx +133 -0
  141. package/plugins/keyboard/state.ts +92 -0
  142. package/plugins/keyboard/suggestions.ts +63 -0
  143. package/plugins/keyboard/taps.ts +77 -0
  144. package/plugins/keyboard/typing.ts +112 -0
  145. package/plugins/locale/app.ts +232 -0
  146. package/plugins/locale/locale.ts +143 -0
  147. package/plugins/locale/overrides/expo-localization/ExpoLocalization.ts +41 -0
  148. package/plugins/locale/overrides/expo-localization/overrides.ts +9 -0
  149. package/plugins/locale/plugin.ts +9 -0
  150. package/plugins/locale/shell.tsx +147 -0
  151. package/plugins/locale/state.ts +36 -0
  152. package/plugins/location/geo.ts +56 -0
  153. package/plugins/location/overrides/expo-location/ExpoLocation.ts +235 -0
  154. package/plugins/location/overrides/expo-location/overrides.ts +11 -0
  155. package/plugins/location/places.ts +38 -0
  156. package/plugins/location/plugin.ts +8 -0
  157. package/plugins/location/shell.tsx +188 -0
  158. package/plugins/location/state.ts +51 -0
  159. package/plugins/motion/overrides/expo-sensors/DeviceSensor.ts +169 -0
  160. package/plugins/motion/overrides/expo-sensors/ExponentPedometer.ts +54 -0
  161. package/plugins/motion/overrides/expo-sensors/overrides.ts +14 -0
  162. package/plugins/motion/plugin.ts +8 -0
  163. package/plugins/motion/sensors.ts +116 -0
  164. package/plugins/motion/shell.tsx +136 -0
  165. package/plugins/motion/state.ts +46 -0
  166. package/plugins/network/app.ts +400 -0
  167. package/plugins/network/overrides/expo-network/ExpoNetwork.ts +51 -0
  168. package/plugins/network/overrides/expo-network/overrides.ts +9 -0
  169. package/plugins/network/plugin.ts +8 -0
  170. package/plugins/network/shell.tsx +67 -0
  171. package/plugins/network/state.ts +29 -0
  172. package/plugins/notifications/center.ts +255 -0
  173. package/plugins/notifications/content.ts +36 -0
  174. package/plugins/notifications/overrides/expo-notifications/NotificationsModule.ts +175 -0
  175. package/plugins/notifications/overrides/expo-notifications/channels.ts +108 -0
  176. package/plugins/notifications/overrides/expo-notifications/getDevicePushTokenAsync.ts +19 -0
  177. package/plugins/notifications/overrides/expo-notifications/getExpoPushTokenAsync.ts +16 -0
  178. package/plugins/notifications/overrides/expo-notifications/overrides.ts +36 -0
  179. package/plugins/notifications/plugin.ts +8 -0
  180. package/plugins/notifications/shell.tsx +192 -0
  181. package/plugins/notifications/state.ts +56 -0
  182. package/plugins/notifications/tokens.ts +47 -0
  183. package/plugins/notifications/trigger.ts +107 -0
  184. package/plugins/permissions/plugin.ts +6 -0
  185. package/plugins/permissions/shell.tsx +50 -0
  186. package/plugins/permissions/state.ts +38 -0
  187. package/plugins/photos/appLibrary.ts +114 -0
  188. package/plugins/photos/library.ts +322 -0
  189. package/plugins/photos/overrides/expo-image-picker/ExponentImagePicker.ts +153 -0
  190. package/plugins/photos/overrides/expo-image-picker/overrides.ts +13 -0
  191. package/plugins/photos/overrides/expo-media-library/ExpoMediaLibrary.ts +235 -0
  192. package/plugins/photos/overrides/expo-media-library/NativeMediaLibraryModule.ts +318 -0
  193. package/plugins/photos/overrides/expo-media-library/overrides.ts +19 -0
  194. package/plugins/photos/plugin.ts +8 -0
  195. package/plugins/photos/samples.ts +367 -0
  196. package/plugins/photos/shell.tsx +164 -0
  197. package/plugins/photos/state.ts +88 -0
  198. package/plugins/storage/files.ts +351 -0
  199. package/plugins/storage/overrides/expo-file-system/ExpoFileSystem.ts +770 -0
  200. package/plugins/storage/overrides/expo-file-system/ExponentFileSystem.ts +196 -0
  201. package/plugins/storage/overrides/expo-file-system/overrides.ts +13 -0
  202. package/plugins/storage/overrides/expo-secure-store/ExpoSecureStore.ts +75 -0
  203. package/plugins/storage/overrides/expo-secure-store/overrides.ts +9 -0
  204. package/plugins/storage/plugin.ts +8 -0
  205. package/plugins/storage/reset.ts +136 -0
  206. package/plugins/storage/secure.ts +71 -0
  207. package/plugins/storage/shell.tsx +365 -0
  208. package/plugins/storage/state.ts +13 -0
  209. package/src/client/app/appDocument.ts +129 -0
  210. package/src/client/app/appFocus.ts +97 -0
  211. package/src/client/app/console.ts +118 -0
  212. package/src/client/app/fonts.ts +76 -0
  213. package/src/client/app/main.tsx +99 -0
  214. package/src/client/app/routeContext.ts +45 -0
  215. package/src/client/app/touch.ts +640 -0
  216. package/src/client/entry.ts +20 -0
  217. package/src/client/modules.d.ts +8 -0
  218. package/src/client/react-native/app.ts +57 -0
  219. package/src/client/react-native/deep.ts +13 -0
  220. package/src/client/react-native/keyboard.tsx +192 -0
  221. package/src/client/react-native/library.ts +256 -0
  222. package/src/client/shell/Cookies.tsx +95 -0
  223. package/src/client/shell/LogDrawer.tsx +415 -0
  224. package/src/client/shell/PhotoPicker.tsx +174 -0
  225. package/src/client/shell/PluginOverlays.tsx +42 -0
  226. package/src/client/shell/PluginPanels.tsx +82 -0
  227. package/src/client/shell/ResetApp.tsx +87 -0
  228. package/src/client/shell/Resizer.tsx +99 -0
  229. package/src/client/shell/Shell.tsx +531 -0
  230. package/src/client/shell/SystemBars.tsx +376 -0
  231. package/src/client/shell/SystemCamera.tsx +181 -0
  232. package/src/client/shell/SystemOverlay.tsx +422 -0
  233. package/src/client/shell/SystemSheets.tsx +462 -0
  234. package/src/client/shell/backdrop.ts +229 -0
  235. package/src/client/shell/control.ts +91 -0
  236. package/src/client/shell/events.ts +226 -0
  237. package/src/client/shell/geometry.ts +68 -0
  238. package/src/client/shell/host.ts +492 -0
  239. package/src/client/shell/icons.tsx +34 -0
  240. package/src/client/shell/logFeed.ts +102 -0
  241. package/src/client/shell/main.tsx +22 -0
  242. package/src/client/shell/permissionCopy.ts +169 -0
  243. package/src/client/shell/plugins.ts +266 -0
  244. package/src/client/shell/popout.ts +185 -0
  245. package/src/client/shell/popover.ts +27 -0
  246. package/src/client/shell/shell.css +878 -0
  247. package/src/client/shell/statusTime.ts +31 -0
  248. package/src/client/shell/tooltips.ts +104 -0
  249. package/src/client/virtual.d.ts +23 -0
  250. package/src/devices/catalog.ts +223 -0
  251. package/src/devices/types.ts +83 -0
  252. package/src/node/appInfo.ts +188 -0
  253. package/src/node/appSource.ts +140 -0
  254. package/src/node/config.ts +180 -0
  255. package/src/node/control.ts +144 -0
  256. package/src/node/doctor.ts +199 -0
  257. package/src/node/events/cli.ts +206 -0
  258. package/src/node/events/shared.ts +238 -0
  259. package/src/node/events/store.ts +266 -0
  260. package/src/node/fonts.ts +377 -0
  261. package/src/node/index.ts +5 -0
  262. package/src/node/overrides/define.ts +56 -0
  263. package/src/node/overrides/packages.ts +136 -0
  264. package/src/node/overrides/platformPatch.ts +56 -0
  265. package/src/node/overrides/plugin.ts +169 -0
  266. package/src/node/overrides/registry.ts +258 -0
  267. package/src/node/paths.ts +18 -0
  268. package/src/node/platform.ts +171 -0
  269. package/src/node/plugins.ts +98 -0
  270. package/src/node/proxy.ts +421 -0
  271. package/src/node/server.ts +254 -0
  272. package/src/node/worklets.ts +86 -0
  273. package/src/plugin/index.ts +33 -0
  274. package/src/plugin/types.ts +127 -0
  275. package/src/plugin/ui.tsx +562 -0
  276. package/src/runtime/clock.ts +112 -0
  277. package/src/runtime/draw.ts +196 -0
  278. package/src/runtime/index.ts +519 -0
  279. package/src/runtime/media.ts +379 -0
  280. package/src/runtime/records.ts +76 -0
  281. package/src/runtime/timezone.ts +81 -0
@@ -0,0 +1,92 @@
1
+ /**
2
+ * The on-screen keyboard's state, shared by the shell module, the app module and the overrides.
3
+ */
4
+
5
+ export type KeyboardType = 'default' | 'email' | 'url' | 'decimal' | 'number' | 'phone'
6
+ export type ReturnKeyType = 'default' | 'search' | 'go' | 'send' | 'done' | 'next'
7
+ export type AutoCapitalize = 'none' | 'sentences' | 'words' | 'characters'
8
+ export type Layer = 'letters' | 'numbers' | 'symbols'
9
+ export type Shift = 'off' | 'on' | 'locked'
10
+
11
+ /** What the focused field asks of the keyboard, read from its element's attributes. */
12
+ export type FieldConfig = {
13
+ keyboardType: KeyboardType
14
+ returnKeyType: ReturnKeyType
15
+ autoCapitalize: AutoCapitalize
16
+ /** Whether the field takes autocorrect suggestions: not a password, not `autoCorrect={false}`. */
17
+ suggestions: boolean
18
+ multiline: boolean
19
+ /** iOS's `keyboardAppearance`: `light` or `dark` pin the keyboard's look; `default` follows the app's appearance. */
20
+ appearance: KeyboardAppearance
21
+ }
22
+
23
+ export type KeyboardAppearance = 'default' | 'light' | 'dark'
24
+
25
+ /** Where the keyboard is on the screen, as React Native's `KeyboardMetrics` gives it. */
26
+ export type KeyboardFrame = { screenX: number; screenY: number; width: number; height: number }
27
+
28
+ export type KeyboardState = {
29
+ /** Drawn on the screen, or sliding in. */
30
+ shown: boolean
31
+ /** The drawn keyboard's height, in points, up from the screen's bottom. */
32
+ height: number
33
+ /** The keyboard's frame once it has finished showing, null once it has finished hiding: `Keyboard.metrics()`. */
34
+ frame: KeyboardFrame | null
35
+ /** The focused text field, or null. A field can be focused with the keyboard hidden. */
36
+ field: FieldConfig | null
37
+ layer: Layer
38
+ shift: Shift
39
+ /** Show the keyboard when a field focuses. Off is a hardware keyboard connected. */
40
+ showOnFocus: boolean
41
+ /** The suggestion strip over the keys. */
42
+ suggestions: boolean
43
+ /** Android: Gboard's row of digits over the letters. */
44
+ numberRow: boolean
45
+ /** How fast `type` types. */
46
+ wordsPerMinute: number
47
+ }
48
+
49
+ export const INITIAL_KEYBOARD: KeyboardState = {
50
+ shown: false,
51
+ height: 0,
52
+ frame: null,
53
+ field: null,
54
+ layer: 'letters',
55
+ shift: 'off',
56
+ showOnFocus: true,
57
+ suggestions: true,
58
+ numberRow: false,
59
+ wordsPerMinute: 60,
60
+ }
61
+
62
+ /** The fields of the state that describe what is on screen now, reset when the shell or the app starts. */
63
+ export const TRANSIENT: Pick<KeyboardState, 'shown' | 'height' | 'frame' | 'field' | 'layer' | 'shift'> = {
64
+ shown: false,
65
+ height: 0,
66
+ frame: null,
67
+ field: null,
68
+ layer: 'letters',
69
+ shift: 'off',
70
+ }
71
+
72
+ /**
73
+ * What the keyboard's slide looks like on each platform. iOS reports 250ms to the app, as the device does, though
74
+ * its spring visibly settles later; Android reports no duration.
75
+ */
76
+ export const TIMING = {
77
+ ios: { drawnMs: 420, reportedMs: 250, curve: 'cubic-bezier(0.32, 0.72, 0, 1)', bezier: [0.32, 0.72, 0, 1] },
78
+ android: { drawnMs: 300, reportedMs: 0, curve: 'cubic-bezier(0.2, 0, 0, 1)', bezier: [0.2, 0, 0, 1] },
79
+ } as const
80
+
81
+ /**
82
+ * What the shell's keys ask the app's half to do to the focused field (the `edit` event). The app's half sends
83
+ * `focus`, `blur`, `text` and `physical-key` back; the shell also sends `dismiss`, `sync`, and React Native's keyboard
84
+ * events (`keyboardWillShow` and the rest) with their payloads.
85
+ */
86
+ export type Edit =
87
+ | { type: 'insert'; text: string }
88
+ | { type: 'delete' }
89
+ | { type: 'return' }
90
+ /** Replaces the `length` characters before the caret: a suggestion taking the place of the word being typed. */
91
+ | { type: 'replace'; length: number; text: string }
92
+
@@ -0,0 +1,63 @@
1
+ /** Predictions for the word being typed, from a small list of common words, as the strip over the keys shows them. */
2
+ export type Suggestion = {
3
+ label: string
4
+ replacement: string
5
+ }
6
+
7
+ const WORDS = `
8
+ the and to of a in is it you that for on with this was are be have not at as or but from they we can
9
+ all one will so if about what there out up just like my your time get go when more no some would make
10
+ know see how think back good now very much need want new over also day well first even only after
11
+ here should because could people really something going thanks thank please sure maybe tomorrow tonight today
12
+ dinner lunch breakfast coffee tea water weekend week morning evening night meeting call message email phone
13
+ address name number password account login sign search settings profile photo photos picture video music
14
+ hello hi hey love great nice cool awesome okay yes yeah perfect home house work office school family friend
15
+ friends together where which who why place places restaurant order delivery store shopping list later soon
16
+ `
17
+ .trim()
18
+ .split(/\s+/)
19
+
20
+ const SENTENCE_START = ['I', 'The', "I'm"]
21
+ const MID_SENTENCE = ['the', 'and', 'to']
22
+
23
+ export function currentWord(textBefore: string): string {
24
+ return /[A-Za-z']+$/.exec(textBefore)?.[0] ?? ''
25
+ }
26
+
27
+ /**
28
+ * Three suggestions: the typed word, then the best guesses. iOS quotes the typed word on the left; Gboard leaves the
29
+ * quotes off and draws the middle one, its best guess, in bold.
30
+ */
31
+ export function suggestionsFor(textBefore: string, platform: 'ios' | 'android' = 'ios'): Suggestion[] {
32
+ const suggestions = iosOrder(textBefore)
33
+ if (platform === 'ios' || currentWord(textBefore) === '') {
34
+ return suggestions
35
+ }
36
+ return suggestions.map((suggestion, index) => (index === 0 ? { label: suggestion.replacement, replacement: suggestion.replacement } : suggestion))
37
+ }
38
+
39
+ function iosOrder(textBefore: string): Suggestion[] {
40
+ const word = currentWord(textBefore)
41
+ if (word === '') {
42
+ const atSentenceStart = textBefore.trim() === '' || /[.!?]\s*$/.test(textBefore)
43
+ return (atSentenceStart ? SENTENCE_START : MID_SENTENCE).map((label) => ({ label, replacement: label }))
44
+ }
45
+ const lower = word.toLowerCase()
46
+ const completions = WORDS.filter((candidate) => candidate.startsWith(lower) && candidate !== lower)
47
+ .slice(0, 2)
48
+ .map((candidate) => matchCase(candidate, word))
49
+ return [
50
+ { label: `"${word}"`, replacement: word },
51
+ ...completions.map((completion) => ({ label: completion, replacement: completion })),
52
+ ]
53
+ }
54
+
55
+ function matchCase(candidate: string, typed: string): string {
56
+ if (typed.length > 1 && typed === typed.toUpperCase()) {
57
+ return candidate.toUpperCase()
58
+ }
59
+ if (typed[0] === typed[0].toUpperCase()) {
60
+ return candidate[0].toUpperCase() + candidate.slice(1)
61
+ }
62
+ return candidate
63
+ }
@@ -0,0 +1,77 @@
1
+ import { focusedField, isEditable } from './fields.ts'
2
+
3
+ /**
4
+ * What a tap does to the keyboard while a field is focused. A browser moves focus on every press, so any tap would
5
+ * blur the field. On a phone the keyboard stays up unless a scroll view decides otherwise, by its
6
+ * `keyboardShouldPersistTaps` (the react-native-web override puts it on the scroll view's element):
7
+ *
8
+ * - `never`, the default: a tap in the scroll view only dismisses the keyboard; what it landed on is not pressed.
9
+ * - `handled`: a tap on something pressable presses it and keeps the keyboard; anywhere else dismisses it.
10
+ * - `always`: the keyboard stays and the tap goes through.
11
+ *
12
+ * Outside any scroll view a tap on something pressable presses it and keeps the keyboard, as on the device, and a
13
+ * tap on the background dismisses it, so there is always a way to put the keyboard away.
14
+ *
15
+ * react-native-web presses on `click`, so a tap is swallowed by stopping its click; its pointer events still go
16
+ * through, so drag-scrolling sees it as usual.
17
+ */
18
+
19
+ const SCROLL_VIEW_SELECTOR = '[data-keyboard-should-persist-taps]'
20
+ const PRESSABLE_SELECTOR = 'button, a, input, textarea, select, [role="button"], [role="link"], [role="switch"], [role="checkbox"], [role="tab"], [tabindex]'
21
+
22
+ export function enableKeyboardTaps(document: Document): () => void {
23
+ const view = document.defaultView
24
+ if (view === null) {
25
+ return () => undefined
26
+ }
27
+ let swallowClick = false
28
+
29
+ const onMouseDown = (event: MouseEvent) => {
30
+ swallowClick = false
31
+ const field = focusedField(document)
32
+ const target = event.target as Element | null
33
+ if (field === null || target === null || typeof target.closest !== 'function' || event.button !== 0) {
34
+ return
35
+ }
36
+ if (isEditable(target)) {
37
+ return
38
+ }
39
+ // Keep the focus where it is; this tap's rules decide whether the keyboard goes.
40
+ event.preventDefault()
41
+ const scrollView = target.closest<HTMLElement>(SCROLL_VIEW_SELECTOR)
42
+ const pressable = target.closest(PRESSABLE_SELECTOR)
43
+ switch (scrollView?.dataset.keyboardShouldPersistTaps) {
44
+ case undefined:
45
+ if (pressable === null) {
46
+ field.blur()
47
+ }
48
+ return
49
+ case 'always':
50
+ return
51
+ case 'handled':
52
+ if (pressable === null || pressable === scrollView || !scrollView.contains(pressable)) {
53
+ field.blur()
54
+ }
55
+ return
56
+ default:
57
+ field.blur()
58
+ swallowClick = true
59
+ }
60
+ }
61
+
62
+ const onClick = (event: MouseEvent) => {
63
+ if (swallowClick) {
64
+ swallowClick = false
65
+ event.stopPropagation()
66
+ event.preventDefault()
67
+ }
68
+ }
69
+
70
+ // On the window, ahead of the app's own listeners on the document and its root.
71
+ view.addEventListener('mousedown', onMouseDown, true)
72
+ view.addEventListener('click', onClick, true)
73
+ return () => {
74
+ view.removeEventListener('mousedown', onMouseDown, true)
75
+ view.removeEventListener('click', onClick, true)
76
+ }
77
+ }
@@ -0,0 +1,112 @@
1
+ import { type KeyboardController, sleep } from './controller.ts'
2
+ import { buildLayout, isNumberPad } from './layouts/index.ts'
3
+ import type { Layer } from './state.ts'
4
+
5
+ /**
6
+ * Types a string through the keyboard at a person's speed: each character's key is pressed and held, with a shift or
7
+ * a change of layer first when it needs one, pauses after spaces and punctuation, and some jitter. A character no
8
+ * key makes is typed as is. With the keyboard hidden (a hardware keyboard), the characters go in at the same pace.
9
+ */
10
+
11
+ export type TypingOptions = {
12
+ wordsPerMinute: number
13
+ /** Stops typing when it returns true: the field lost focus. */
14
+ stopped: () => boolean
15
+ }
16
+
17
+ const CHARACTERS_PER_WORD = 5
18
+ const MAX_KEYS_PER_CHARACTER = 4
19
+ /** How long a key is held at natural speeds. Faster typing holds keys for less, down to one frame. */
20
+ const KEY_HOLD_MS = 85
21
+ const MIN_KEY_HOLD_MS = 16
22
+ /** The gap between keys at which the hold and the pauses after spaces and punctuation are full length. */
23
+ const NATURAL_KEY_INTERVAL_MS = 200
24
+
25
+ export async function typeText(keyboard: KeyboardController, text: string, options: TypingOptions): Promise<void> {
26
+ const keyInterval = 60000 / (options.wordsPerMinute * CHARACTERS_PER_WORD)
27
+ const pace = Math.min(1, keyInterval / NATURAL_KEY_INTERVAL_MS)
28
+ const hold = Math.max(MIN_KEY_HOLD_MS, KEY_HOLD_MS * pace)
29
+ for (const char of text) {
30
+ for (let attempt = 0; attempt < MAX_KEYS_PER_CHARACTER; attempt++) {
31
+ if (options.stopped()) {
32
+ return
33
+ }
34
+ const step = keyboard.state().shown ? nextKeyFor(keyboard, char) : ({ kind: 'raw' } as const)
35
+ if (step.kind === 'raw') {
36
+ keyboard.insertRaw(char)
37
+ await sleep(jitter(keyInterval))
38
+ break
39
+ }
40
+ // The hold is part of the key's time, so the typing keeps to `wordsPerMinute`.
41
+ const held = jitter(hold, 0.25)
42
+ await keyboard.tap(step.id, held)
43
+ await sleep(Math.max(0, jitter(keyInterval) - held) + pauseAfter(char, step.done) * pace)
44
+ if (step.done) {
45
+ break
46
+ }
47
+ }
48
+ }
49
+ }
50
+
51
+ type Step = { kind: 'key'; id: string; done: boolean } | { kind: 'raw' }
52
+
53
+ function nextKeyFor(keyboard: KeyboardController, char: string): Step {
54
+ const { layer, shift } = keyboard.state()
55
+ const config = keyboard.fieldConfig()
56
+ if (char === ' ' && keyboard.findKey('space') !== undefined) {
57
+ return { kind: 'key', id: 'space', done: true }
58
+ }
59
+ if (char === '\n') {
60
+ return { kind: 'key', id: 'return', done: true }
61
+ }
62
+ if (isNumberPad(config.keyboardType)) {
63
+ return keyboard.findKey(`char:${char}`) === undefined ? { kind: 'raw' } : { kind: 'key', id: `char:${char}`, done: true }
64
+ }
65
+ const lower = char.toLowerCase()
66
+ if (/^[a-z]$/.test(lower)) {
67
+ if (layer !== 'letters') {
68
+ return { kind: 'key', id: 'layer:letters', done: false }
69
+ }
70
+ const wantUpper = char !== lower
71
+ if (wantUpper !== (shift !== 'off')) {
72
+ return { kind: 'key', id: 'shift', done: false }
73
+ }
74
+ return { kind: 'key', id: `letter:${lower}`, done: true }
75
+ }
76
+ if (keyboard.findKey(`char:${char}`) !== undefined) {
77
+ return { kind: 'key', id: `char:${char}`, done: true }
78
+ }
79
+ const target = layerContaining(keyboard, char)
80
+ if (target === null) {
81
+ return { kind: 'raw' }
82
+ }
83
+ if (layer === 'letters' || target === 'numbers') {
84
+ return { kind: 'key', id: 'layer:numbers', done: false }
85
+ }
86
+ return { kind: 'key', id: 'layer:symbols', done: false }
87
+ }
88
+
89
+ function layerContaining(keyboard: KeyboardController, char: string): Layer | null {
90
+ const current = keyboard.layout()
91
+ for (const layer of ['numbers', 'symbols'] as const) {
92
+ const input = { ...keyboard.layoutInput(), layer }
93
+ if (buildLayout(input).keys.some((key) => key.id === `char:${char}`)) {
94
+ return layer
95
+ }
96
+ }
97
+ return current.keys.some((key) => key.id === `char:${char}`) ? keyboard.state().layer : null
98
+ }
99
+
100
+ function jitter(ms: number, spread = 0.4): number {
101
+ return ms * (1 - spread + Math.random() * spread * 2)
102
+ }
103
+
104
+ function pauseAfter(char: string, typedCharacter: boolean): number {
105
+ if (!typedCharacter) {
106
+ return 0
107
+ }
108
+ if (/[.!?,]/.test(char)) {
109
+ return 180
110
+ }
111
+ return char === ' ' ? 40 : 0
112
+ }
@@ -0,0 +1,232 @@
1
+ import { fromWallTime, isTimeZone, plugin, toWallTime, zoneOffset } from 'web-sim/runtime'
2
+ import { formattingTag } from './locale.ts'
3
+ import { INITIAL_LOCALE, type LocaleState } from './state.ts'
4
+
5
+ /**
6
+ * The app's window keeps the phone's time and language, not the computer's:
7
+ *
8
+ * - `Intl`'s formatters, and `toLocaleString` and its kin on dates, numbers and strings, default to the panel's first
9
+ * language in its region (with its 12- or 24-hour clock when set), and dates to its time zone.
10
+ * - `Date`'s local-time methods (`getHours`, `setDate`, `getTimezoneOffset`, `toString`, ...), its constructor from
11
+ * parts or from a string without an offset, and `Date.parse` work in the panel's time zone.
12
+ *
13
+ * Each call reads the panel, so a change applies at once. Where the panel's zone is the computer's, `Date` is left
14
+ * to the browser.
15
+ */
16
+ const locale = plugin<LocaleState>('locale', INITIAL_LOCALE)
17
+
18
+ const OriginalDate = Date
19
+ const proto = OriginalDate.prototype
20
+ const originalOffset = proto.getTimezoneOffset
21
+ const originalParse = OriginalDate.parse
22
+ const browserZone = new Intl.DateTimeFormat().resolvedOptions().timeZone
23
+
24
+ function safeTag(tag: string): string {
25
+ try {
26
+ return Intl.getCanonicalLocales(tag)[0] ?? 'en-US'
27
+ } catch {
28
+ return 'en-US'
29
+ }
30
+ }
31
+
32
+ function defaultLocale(): string {
33
+ const state = locale.get()
34
+ const tag = safeTag(formattingTag(state))
35
+ return state.clock === 'auto' || tag.includes('-u-') ? tag : `${tag}-u-hc-${state.clock === '24' ? 'h23' : 'h12'}`
36
+ }
37
+
38
+ function zone(): string {
39
+ const { timeZone } = locale.get()
40
+ return isTimeZone(timeZone) ? timeZone : 'UTC'
41
+ }
42
+
43
+ const withLocale = (locales: unknown) => (locales === undefined || (Array.isArray(locales) && locales.length === 0) ? defaultLocale() : locales)
44
+
45
+ const withZone = (options: unknown) => {
46
+ const given = (options ?? {}) as Intl.DateTimeFormatOptions
47
+ return given.timeZone === undefined ? { ...given, timeZone: zone() } : given
48
+ }
49
+
50
+ // ---------------------------------------------------------------------------------------------------------------
51
+ // Intl and toLocaleString
52
+
53
+ type Constructor = new (...args: any[]) => object
54
+
55
+ function defaultingConstructor<T extends Constructor>(original: T, options: (value: unknown) => unknown = (value) => value): T {
56
+ const args = ([locales, given, ...rest]: unknown[]) => [withLocale(locales), options(given), ...rest]
57
+ return new Proxy(original, {
58
+ construct: (target, list, newTarget) => Reflect.construct(target, args(list), newTarget),
59
+ // Intl's constructors also work called as functions.
60
+ apply: (target, _this, list) => Reflect.construct(target, args(list)),
61
+ })
62
+ }
63
+
64
+ const intl = Intl as unknown as Record<string, Constructor>
65
+ intl.DateTimeFormat = defaultingConstructor(Intl.DateTimeFormat, withZone)
66
+ for (const name of ['NumberFormat', 'Collator', 'PluralRules', 'RelativeTimeFormat', 'ListFormat', 'Segmenter', 'DisplayNames']) {
67
+ if (intl[name] !== undefined) {
68
+ intl[name] = defaultingConstructor(intl[name])
69
+ }
70
+ }
71
+
72
+ type Method = (this: unknown, ...args: any[]) => any
73
+
74
+ function patch(target: object, name: string, wrap: (original: Method) => Method) {
75
+ const original = (target as Record<string, Method>)[name]
76
+ Object.defineProperty(target, name, { value: wrap(original), writable: true, configurable: true, enumerable: false })
77
+ }
78
+
79
+ for (const name of ['toLocaleString', 'toLocaleDateString', 'toLocaleTimeString']) {
80
+ patch(proto, name, (original) =>
81
+ function (this: unknown, locales?: unknown, options?: unknown) {
82
+ return original.call(this, withLocale(locales), withZone(options))
83
+ },
84
+ )
85
+ }
86
+ patch(Number.prototype, 'toLocaleString', (original) =>
87
+ function (this: unknown, locales?: unknown, options?: unknown) {
88
+ return original.call(this, withLocale(locales), options)
89
+ },
90
+ )
91
+ patch(BigInt.prototype, 'toLocaleString', (original) =>
92
+ function (this: unknown, locales?: unknown, options?: unknown) {
93
+ return original.call(this, withLocale(locales), options)
94
+ },
95
+ )
96
+ patch(String.prototype, 'localeCompare', (original) =>
97
+ function (this: unknown, that: string, locales?: unknown, options?: unknown) {
98
+ return original.call(this, that, withLocale(locales), options)
99
+ },
100
+ )
101
+ for (const name of ['toLocaleUpperCase', 'toLocaleLowerCase']) {
102
+ patch(String.prototype, name, (original) =>
103
+ function (this: unknown, locales?: unknown) {
104
+ return original.call(this, withLocale(locales))
105
+ },
106
+ )
107
+ }
108
+
109
+ // ---------------------------------------------------------------------------------------------------------------
110
+ // Date in the phone's time zone
111
+
112
+ /** Whether the phone's zone is the computer's, so the browser's own local time is right. */
113
+ const native = () => zone() === browserZone
114
+
115
+ const time = (date: unknown) => proto.getTime.call(date as Date)
116
+
117
+ /** The wall-clock time in the phone's zone, as a Date to read with `getUTC*`. */
118
+ const wall = (date: unknown) => new OriginalDate(toWallTime(zone(), time(date)))
119
+
120
+ const PARTS = ['FullYear', 'Month', 'Date', 'Day', 'Hours', 'Minutes', 'Seconds', 'Milliseconds'] as const
121
+ const methods = proto as unknown as Record<string, Method>
122
+
123
+ for (const part of PARTS) {
124
+ const universal = methods[`getUTC${part}`]
125
+ patch(proto, `get${part}`, (original) =>
126
+ function (this: unknown) {
127
+ return native() ? original.call(this) : universal.call(wall(this))
128
+ },
129
+ )
130
+ if (part === 'Day') {
131
+ continue
132
+ }
133
+ const setUniversal = methods[`setUTC${part}`]
134
+ patch(proto, `set${part}`, (original) =>
135
+ function (this: unknown, ...args: unknown[]) {
136
+ if (native()) {
137
+ return original.apply(this, args)
138
+ }
139
+ const current = time(this)
140
+ // setFullYear starts an invalid date from the epoch's wall time.
141
+ const local = new OriginalDate(Number.isNaN(current) ? (part === 'FullYear' ? 0 : NaN) : toWallTime(zone(), current))
142
+ setUniversal.apply(local, args)
143
+ return proto.setTime.call(this as Date, fromWallTime(zone(), local.getTime()))
144
+ },
145
+ )
146
+ }
147
+
148
+ patch(proto, 'getTimezoneOffset', (original) =>
149
+ function (this: unknown) {
150
+ const current = time(this)
151
+ return native() || Number.isNaN(current) ? original.call(this) : -zoneOffset(zone(), current) / 60000
152
+ },
153
+ )
154
+
155
+ const DAYS = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat']
156
+ const MONTHS = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
157
+ const pad = (value: number, length = 2) => String(Math.abs(value)).padStart(length, '0')
158
+
159
+ function dateString(date: unknown): string {
160
+ const local = wall(date)
161
+ const year = local.getUTCFullYear()
162
+ return `${DAYS[local.getUTCDay()]} ${MONTHS[local.getUTCMonth()]} ${pad(local.getUTCDate())} ${year < 0 ? '-' : ''}${pad(year, 4)}`
163
+ }
164
+
165
+ function zoneName(at: number): string {
166
+ try {
167
+ return (
168
+ new Intl.DateTimeFormat('en-US', { timeZone: zone(), timeZoneName: 'long' }).formatToParts(at).find((part) => part.type === 'timeZoneName')?.value ??
169
+ zone()
170
+ )
171
+ } catch {
172
+ return zone()
173
+ }
174
+ }
175
+
176
+ function timeString(date: unknown): string {
177
+ const local = wall(date)
178
+ const offset = zoneOffset(zone(), time(date)) / 60000
179
+ const sign = offset < 0 ? '-' : '+'
180
+ return `${pad(local.getUTCHours())}:${pad(local.getUTCMinutes())}:${pad(local.getUTCSeconds())} GMT${sign}${pad(Math.floor(Math.abs(offset) / 60))}${pad(Math.abs(offset) % 60)} (${zoneName(time(date))})`
181
+ }
182
+
183
+ patch(proto, 'toString', (original) =>
184
+ function (this: unknown) {
185
+ return native() || Number.isNaN(time(this)) ? original.call(this) : `${dateString(this)} ${timeString(this)}`
186
+ },
187
+ )
188
+ patch(proto, 'toDateString', (original) =>
189
+ function (this: unknown) {
190
+ return native() || Number.isNaN(time(this)) ? original.call(this) : dateString(this)
191
+ },
192
+ )
193
+ patch(proto, 'toTimeString', (original) =>
194
+ function (this: unknown) {
195
+ return native() || Number.isNaN(time(this)) ? original.call(this) : timeString(this)
196
+ },
197
+ )
198
+
199
+ /** ISO dates without a time are UTC; other strings without an offset or zone name are local time. */
200
+ const DATE_ONLY = /^\s*[+-]?\d{4,6}(-\d{2}(-\d{2})?)?\s*$/
201
+ const HAS_ZONE = /(Z|[+-]\d{2}:?\d{2}|\b(GMT|UTC|UT|[ECMP][SD]T)\b.*)\s*(\([^)]*\))?\s*$/i
202
+
203
+ function parse(text: string): number {
204
+ const parsed = originalParse(text)
205
+ if (native() || Number.isNaN(parsed) || DATE_ONLY.test(text) || HAS_ZONE.test(text)) {
206
+ return parsed
207
+ }
208
+ // The browser read it in the computer's zone: the same wall-clock time, in the phone's.
209
+ const browserWall = parsed - originalOffset.call(new OriginalDate(parsed)) * 60000
210
+ return fromWallTime(zone(), browserWall)
211
+ }
212
+
213
+ OriginalDate.parse = parse
214
+
215
+ const PhoneDate: DateConstructor = new Proxy(OriginalDate, {
216
+ construct(target, args, newTarget) {
217
+ if (!native()) {
218
+ if (args.length >= 2) {
219
+ const [year, month, ...rest] = args.map(Number) as [number, number, ...number[]]
220
+ args = [fromWallTime(zone(), OriginalDate.UTC(year, month, ...rest))]
221
+ } else if (args.length === 1 && typeof args[0] === 'string') {
222
+ args = [parse(args[0])]
223
+ }
224
+ }
225
+ return Reflect.construct(target, args, newTarget) as Date
226
+ },
227
+ // `Date()` called as a function: the time now, as a string.
228
+ apply: () => proto.toString.call(new OriginalDate()),
229
+ })
230
+
231
+ window.Date = PhoneDate
232
+ Object.defineProperty(proto, 'constructor', { value: PhoneDate, writable: true, configurable: true, enumerable: false })