@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,470 @@
|
|
|
1
|
+
import { type ComponentProps, type ComponentRef, createContext, forwardRef, type PropsWithChildren, type Ref, useContext, useEffect, useRef } from 'react'
|
|
2
|
+
import { createPortal } from 'react-dom'
|
|
3
|
+
import { Animated as RNAnimated, Easing as RNEasing, Pressable, ScrollView, type StyleProp, Text, useWindowDimensions, View, type ViewStyle } from 'react-native'
|
|
4
|
+
import Animated, { Easing, useAnimatedStyle, useSharedValue, withTiming } from 'react-native-reanimated'
|
|
5
|
+
import { platform, plugin } from 'web-sim/runtime'
|
|
6
|
+
import { INITIAL_KEYBOARD, type KeyboardState as PluginKeyboardState, TIMING } from '../../state.ts'
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* react-native-keyboard-controller, driven by the keyboard the `keyboard` plugin draws. Its web build does nothing;
|
|
10
|
+
* this one follows the plugin's state: `shown` and `height` as the keyboard starts to slide, `frame` once it has
|
|
11
|
+
* settled. Values animate with the drawn keyboard's own duration and curve, so views that follow it move with it.
|
|
12
|
+
*
|
|
13
|
+
* The library's files are not run through the worklets Babel plugin, so animated styles list their dependencies, shared values among them.
|
|
14
|
+
*
|
|
15
|
+
* `OverKeyboardView` draws in the app, which sits under the keyboard; on a device it draws over the keyboard.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
const keyboard = () => plugin<PluginKeyboardState>('keyboard', INITIAL_KEYBOARD)
|
|
19
|
+
|
|
20
|
+
function timing() {
|
|
21
|
+
return TIMING[platform()]
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function reanimatedTiming() {
|
|
25
|
+
const { drawnMs, bezier } = timing()
|
|
26
|
+
return { duration: drawnMs, easing: Easing.bezier(bezier[0], bezier[1], bezier[2], bezier[3]) }
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** The CSS-style cubic Bézier `timing().bezier`, as a function of time from 0 to 1. */
|
|
30
|
+
function curve(t: number): number {
|
|
31
|
+
const [x1, y1, x2, y2] = timing().bezier
|
|
32
|
+
const at = (a: number, b: number, s: number) => 3 * a * s * (1 - s) ** 2 + 3 * b * s ** 2 * (1 - s) + s ** 3
|
|
33
|
+
let low = 0
|
|
34
|
+
let high = 1
|
|
35
|
+
let s = t
|
|
36
|
+
for (let i = 0; i < 20; i++) {
|
|
37
|
+
s = (low + high) / 2
|
|
38
|
+
if (at(x1, x2, s) < t) {
|
|
39
|
+
low = s
|
|
40
|
+
} else {
|
|
41
|
+
high = s
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
return at(y1, y2, s)
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
type Motion = { height: number; progress: number; duration: number; target: number }
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Calls `onStart`, `onMove` on every frame of the keyboard's slide, and `onEnd`, as the library's keyboard handlers
|
|
51
|
+
* are called.
|
|
52
|
+
*/
|
|
53
|
+
function useKeyboardMotion(handlers: { onStart?: (event: Motion) => void; onMove?: (event: Motion) => void; onEnd?: (event: Motion) => void }) {
|
|
54
|
+
const latest = useRef(handlers)
|
|
55
|
+
latest.current = handlers
|
|
56
|
+
useEffect(() => {
|
|
57
|
+
let frame = 0
|
|
58
|
+
const stop = keyboard().watch((next, previous) => {
|
|
59
|
+
const from = previous.shown ? previous.height : 0
|
|
60
|
+
const to = next.shown ? next.height : 0
|
|
61
|
+
if (from === to) {
|
|
62
|
+
return
|
|
63
|
+
}
|
|
64
|
+
cancelAnimationFrame(frame)
|
|
65
|
+
const { drawnMs } = timing()
|
|
66
|
+
const event = (height: number): Motion => ({ height, progress: next.height > 0 ? height / next.height : 0, duration: drawnMs, target: 1 })
|
|
67
|
+
latest.current.onStart?.(event(to))
|
|
68
|
+
const started = performance.now()
|
|
69
|
+
const step = () => {
|
|
70
|
+
const t = Math.min(1, (performance.now() - started) / drawnMs)
|
|
71
|
+
const height = from + (to - from) * curve(t)
|
|
72
|
+
latest.current.onMove?.(event(height))
|
|
73
|
+
if (t < 1) {
|
|
74
|
+
frame = requestAnimationFrame(step)
|
|
75
|
+
} else {
|
|
76
|
+
latest.current.onEnd?.(event(to))
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
frame = requestAnimationFrame(step)
|
|
80
|
+
})
|
|
81
|
+
return () => {
|
|
82
|
+
stop()
|
|
83
|
+
cancelAnimationFrame(frame)
|
|
84
|
+
}
|
|
85
|
+
}, [])
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function blurFocusedField() {
|
|
89
|
+
const active = document.activeElement as HTMLElement | null
|
|
90
|
+
if (active !== null && (active.tagName === 'INPUT' || active.tagName === 'TEXTAREA' || active.isContentEditable)) {
|
|
91
|
+
active.blur()
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
type KeyboardEventData = { height: number; duration: number; timestamp: number; target: number; type: string; appearance: string }
|
|
96
|
+
|
|
97
|
+
function eventData(state: PluginKeyboardState, height: number): KeyboardEventData {
|
|
98
|
+
return {
|
|
99
|
+
height,
|
|
100
|
+
duration: timing().drawnMs,
|
|
101
|
+
timestamp: Date.now(),
|
|
102
|
+
target: 1,
|
|
103
|
+
type: state.field?.keyboardType ?? 'default',
|
|
104
|
+
appearance: 'default',
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* The library's events on both platforms, as it sends them: `Will` as the keyboard starts to move, `Did` when it
|
|
110
|
+
* stops.
|
|
111
|
+
*/
|
|
112
|
+
export const KeyboardEvents = {
|
|
113
|
+
addListener(name: string, listener: (event: KeyboardEventData) => void) {
|
|
114
|
+
const remove = keyboard().watch((next, previous) => {
|
|
115
|
+
if (document.defaultView === null) {
|
|
116
|
+
remove()
|
|
117
|
+
return
|
|
118
|
+
}
|
|
119
|
+
if (name === 'keyboardWillShow' && next.shown && (!previous.shown || next.height !== previous.height)) {
|
|
120
|
+
listener(eventData(next, next.height))
|
|
121
|
+
} else if (name === 'keyboardWillHide' && !next.shown && previous.shown) {
|
|
122
|
+
listener(eventData(next, 0))
|
|
123
|
+
} else if (name === 'keyboardDidShow' && next.frame !== null && next.frame !== previous.frame) {
|
|
124
|
+
listener(eventData(next, next.frame.height))
|
|
125
|
+
} else if (name === 'keyboardDidHide' && next.frame === null && previous.frame !== null) {
|
|
126
|
+
listener(eventData(next, 0))
|
|
127
|
+
}
|
|
128
|
+
})
|
|
129
|
+
return { remove }
|
|
130
|
+
},
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
const NOOP = () => {}
|
|
134
|
+
const NOOP_EVENTS = { addListener: () => ({ remove: NOOP }) }
|
|
135
|
+
|
|
136
|
+
export const FocusedInputEvents = NOOP_EVENTS
|
|
137
|
+
export const WindowDimensionsEvents = NOOP_EVENTS
|
|
138
|
+
|
|
139
|
+
export const KeyboardControllerNative = {
|
|
140
|
+
setDefaultMode: NOOP,
|
|
141
|
+
setInputMode: NOOP,
|
|
142
|
+
preload: NOOP,
|
|
143
|
+
setTranslucent: NOOP,
|
|
144
|
+
dismiss: blurFocusedField,
|
|
145
|
+
setFocusTo: NOOP,
|
|
146
|
+
viewPositionInWindow: () => Promise.resolve({ x: 0, y: 0, width: 0, height: 0 }),
|
|
147
|
+
addListener: NOOP,
|
|
148
|
+
removeListeners: NOOP,
|
|
149
|
+
getConstants: () => ({ keyboardBorderRadius: 0 }),
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
export const KeyboardController = {
|
|
153
|
+
/** Blurs the field and resolves once the keyboard has gone, or at once if it was not up. */
|
|
154
|
+
dismiss: (options?: { keepFocus?: boolean }) =>
|
|
155
|
+
new Promise<void>((resolve) => {
|
|
156
|
+
if (keyboard().get().frame === null && !keyboard().get().shown) {
|
|
157
|
+
if (options?.keepFocus !== true) {
|
|
158
|
+
blurFocusedField()
|
|
159
|
+
}
|
|
160
|
+
resolve()
|
|
161
|
+
return
|
|
162
|
+
}
|
|
163
|
+
const subscription = KeyboardEvents.addListener('keyboardDidHide', () => {
|
|
164
|
+
subscription.remove()
|
|
165
|
+
resolve()
|
|
166
|
+
})
|
|
167
|
+
blurFocusedField()
|
|
168
|
+
}),
|
|
169
|
+
isVisible: () => keyboard().get().frame !== null,
|
|
170
|
+
state: () => {
|
|
171
|
+
const state = keyboard().get()
|
|
172
|
+
return eventData(state, state.frame?.height ?? 0)
|
|
173
|
+
},
|
|
174
|
+
setInputMode: NOOP,
|
|
175
|
+
setDefaultMode: NOOP,
|
|
176
|
+
setFocusTo: NOOP,
|
|
177
|
+
preload: NOOP,
|
|
178
|
+
setTranslucent: NOOP,
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
export const AndroidSoftInputModes = {
|
|
182
|
+
SOFT_INPUT_ADJUST_NOTHING: 48,
|
|
183
|
+
SOFT_INPUT_ADJUST_PAN: 32,
|
|
184
|
+
SOFT_INPUT_ADJUST_RESIZE: 16,
|
|
185
|
+
SOFT_INPUT_ADJUST_UNSPECIFIED: 0,
|
|
186
|
+
SOFT_INPUT_IS_FORWARD_NAVIGATION: 256,
|
|
187
|
+
SOFT_INPUT_MASK_ADJUST: 240,
|
|
188
|
+
SOFT_INPUT_MASK_STATE: 15,
|
|
189
|
+
SOFT_INPUT_MODE_CHANGED: 512,
|
|
190
|
+
SOFT_INPUT_STATE_ALWAYS_HIDDEN: 3,
|
|
191
|
+
SOFT_INPUT_STATE_ALWAYS_VISIBLE: 5,
|
|
192
|
+
SOFT_INPUT_STATE_HIDDEN: 2,
|
|
193
|
+
SOFT_INPUT_STATE_UNCHANGED: 1,
|
|
194
|
+
SOFT_INPUT_STATE_UNSPECIFIED: 0,
|
|
195
|
+
SOFT_INPUT_STATE_VISIBLE: 4,
|
|
196
|
+
} as const
|
|
197
|
+
|
|
198
|
+
export const KEYBOARD_BORDER_RADIUS = 0
|
|
199
|
+
export const KEYBOARD_HAS_ROUNDED_CORNERS = false
|
|
200
|
+
|
|
201
|
+
export const KeyboardControllerView = View
|
|
202
|
+
export const KeyboardControllerViewCommands = { synchronizeFocusedInputLayout: NOOP }
|
|
203
|
+
export const KeyboardGestureArea = View
|
|
204
|
+
export const RCTOverKeyboardView = View
|
|
205
|
+
export const KeyboardBackgroundView = View
|
|
206
|
+
export const RCTKeyboardExtender = View
|
|
207
|
+
export const ClippingScrollView = View
|
|
208
|
+
export const RCTKeyboardToolbarGroupView = View
|
|
209
|
+
|
|
210
|
+
export const KeyboardContext = createContext({ enabled: true })
|
|
211
|
+
export const useKeyboardContext = () => useContext(KeyboardContext)
|
|
212
|
+
|
|
213
|
+
export function KeyboardProvider({ children }: PropsWithChildren) {
|
|
214
|
+
return children
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
export function useResizeMode() {}
|
|
218
|
+
|
|
219
|
+
export function useKeyboardController() {
|
|
220
|
+
return { setEnabled: NOOP, enabled: true }
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
export function useKeyboardHandler(handler: { onStart?: (event: Motion) => void; onMove?: (event: Motion) => void; onEnd?: (event: Motion) => void }, _deps?: unknown[]) {
|
|
224
|
+
useKeyboardMotion(handler)
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
export const useGenericKeyboardHandler = useKeyboardHandler
|
|
228
|
+
|
|
229
|
+
export function useReanimatedFocusedInput() {
|
|
230
|
+
const input = useSharedValue(null)
|
|
231
|
+
return { input, update: async () => {} }
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
export function useFocusedInputHandler() {}
|
|
235
|
+
|
|
236
|
+
export { useWindowDimensions }
|
|
237
|
+
|
|
238
|
+
/** `height` runs from 0 to minus the keyboard's height and `progress` from 0 to 1, with the keyboard's slide. */
|
|
239
|
+
export function useReanimatedKeyboardAnimation() {
|
|
240
|
+
const state = keyboard().use()
|
|
241
|
+
const animatedHeight = useSharedValue(state.shown ? -state.height : 0)
|
|
242
|
+
const progress = useSharedValue(state.shown ? 1 : 0)
|
|
243
|
+
useEffect(() => {
|
|
244
|
+
animatedHeight.value = withTiming(state.shown ? -state.height : 0, reanimatedTiming())
|
|
245
|
+
progress.value = withTiming(state.shown ? 1 : 0, reanimatedTiming())
|
|
246
|
+
}, [state.shown, state.height, animatedHeight, progress])
|
|
247
|
+
return { height: animatedHeight, progress }
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/** The same, as `Animated` values. */
|
|
251
|
+
export function useKeyboardAnimation() {
|
|
252
|
+
const state = keyboard().use()
|
|
253
|
+
const height = useRef(new RNAnimated.Value(state.shown ? -state.height : 0)).current
|
|
254
|
+
const progress = useRef(new RNAnimated.Value(state.shown ? 1 : 0)).current
|
|
255
|
+
useEffect(() => {
|
|
256
|
+
const { drawnMs, bezier } = timing()
|
|
257
|
+
const easing = RNEasing.bezier(bezier[0], bezier[1], bezier[2], bezier[3])
|
|
258
|
+
RNAnimated.parallel([
|
|
259
|
+
RNAnimated.timing(height, { toValue: state.shown ? -state.height : 0, duration: drawnMs, easing, useNativeDriver: false }),
|
|
260
|
+
RNAnimated.timing(progress, { toValue: state.shown ? 1 : 0, duration: drawnMs, easing, useNativeDriver: false }),
|
|
261
|
+
]).start()
|
|
262
|
+
}, [state.shown, state.height, height, progress])
|
|
263
|
+
return { height, progress }
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
export function useKeyboardState<T = KeyboardEventData & { isVisible: boolean }>(selector?: (state: KeyboardEventData & { isVisible: boolean }) => T): T {
|
|
267
|
+
const state = keyboard().use()
|
|
268
|
+
const value = { ...eventData(state, state.frame?.height ?? 0), isVisible: state.frame !== null }
|
|
269
|
+
return selector === undefined ? (value as T) : selector(value)
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
export const KeyboardState = { UNKNOWN: 0, OPENING: 1, OPEN: 2, CLOSING: 3, CLOSED: 4 } as const
|
|
273
|
+
|
|
274
|
+
export function useAnimatedKeyboard() {
|
|
275
|
+
const height = useSharedValue(0)
|
|
276
|
+
const state = useSharedValue<number>(KeyboardState.UNKNOWN)
|
|
277
|
+
useKeyboardMotion({
|
|
278
|
+
onStart: (event) => {
|
|
279
|
+
state.value = event.height > 0 ? KeyboardState.OPENING : KeyboardState.CLOSING
|
|
280
|
+
},
|
|
281
|
+
onMove: (event) => {
|
|
282
|
+
height.value = event.height
|
|
283
|
+
},
|
|
284
|
+
onEnd: (event) => {
|
|
285
|
+
state.value = event.height > 0 ? KeyboardState.OPEN : KeyboardState.CLOSED
|
|
286
|
+
height.value = event.height
|
|
287
|
+
},
|
|
288
|
+
})
|
|
289
|
+
return { height, state }
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
type KeyboardAvoidingViewProps = ComponentProps<typeof View> & {
|
|
293
|
+
behavior?: 'padding' | 'height' | 'position' | 'translate-with-padding'
|
|
294
|
+
keyboardVerticalOffset?: number
|
|
295
|
+
enabled?: boolean
|
|
296
|
+
contentContainerStyle?: StyleProp<ViewStyle>
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
/**
|
|
300
|
+
* Takes off as much of its bottom as the keyboard covers, measured in the window, which is the screen, so
|
|
301
|
+
* `keyboardVerticalOffset` is not needed to make up for a header.
|
|
302
|
+
*/
|
|
303
|
+
export const KeyboardAvoidingView = forwardRef(function KeyboardAvoidingView(
|
|
304
|
+
{ behavior = 'padding', keyboardVerticalOffset: _offset, enabled = true, contentContainerStyle, style, children, onLayout, ...props }: KeyboardAvoidingViewProps,
|
|
305
|
+
ref: Ref<ComponentRef<typeof View>>,
|
|
306
|
+
) {
|
|
307
|
+
const { height } = useReanimatedKeyboardAnimation()
|
|
308
|
+
const gap = useSharedValue(0)
|
|
309
|
+
const element = useRef<HTMLElement | null>(null)
|
|
310
|
+
const measure = () => {
|
|
311
|
+
const rect = element.current?.getBoundingClientRect?.()
|
|
312
|
+
if (rect !== undefined) {
|
|
313
|
+
gap.value = Math.max(0, window.innerHeight - rect.bottom)
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
const avoidStyle = useAnimatedStyle(() => {
|
|
317
|
+
const overlap = enabled ? Math.max(0, -height.value - gap.value) : 0
|
|
318
|
+
return behavior === 'position' ? { transform: [{ translateY: -overlap }] } : { paddingBottom: overlap }
|
|
319
|
+
}, [enabled, behavior, height, gap])
|
|
320
|
+
const setRef = (node: ComponentRef<typeof View> | null) => {
|
|
321
|
+
element.current = node as unknown as HTMLElement | null
|
|
322
|
+
if (typeof ref === 'function') {
|
|
323
|
+
ref(node)
|
|
324
|
+
} else if (ref !== null) {
|
|
325
|
+
ref.current = node
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
return (
|
|
329
|
+
<Animated.View
|
|
330
|
+
ref={setRef as never}
|
|
331
|
+
style={[style, behavior === 'position' ? null : avoidStyle]}
|
|
332
|
+
onLayout={(event) => {
|
|
333
|
+
measure()
|
|
334
|
+
onLayout?.(event)
|
|
335
|
+
}}
|
|
336
|
+
{...props}
|
|
337
|
+
>
|
|
338
|
+
{behavior === 'position' ? <Animated.View style={[contentContainerStyle, avoidStyle]}>{children}</Animated.View> : children}
|
|
339
|
+
</Animated.View>
|
|
340
|
+
)
|
|
341
|
+
})
|
|
342
|
+
|
|
343
|
+
type KeyboardStickyViewProps = ComponentProps<typeof View> & { offset?: { closed?: number; opened?: number }; enabled?: boolean }
|
|
344
|
+
|
|
345
|
+
export function KeyboardStickyView({ offset = {}, enabled = true, style, children, ...props }: KeyboardStickyViewProps) {
|
|
346
|
+
const { height, progress } = useReanimatedKeyboardAnimation()
|
|
347
|
+
const closed = offset.closed ?? 0
|
|
348
|
+
const opened = offset.opened ?? 0
|
|
349
|
+
const stickyStyle = useAnimatedStyle(() => ({
|
|
350
|
+
transform: [{ translateY: enabled ? height.value + closed + (opened - closed) * progress.value : closed }],
|
|
351
|
+
}), [enabled, closed, opened, height, progress])
|
|
352
|
+
return (
|
|
353
|
+
<Animated.View style={[style, stickyStyle]} {...props}>
|
|
354
|
+
{children}
|
|
355
|
+
</Animated.View>
|
|
356
|
+
)
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
const OVER_KEYBOARD_STYLE = { position: 'fixed', top: 0, right: 0, bottom: 0, left: 0, zIndex: 2147483647 } as unknown as ViewStyle
|
|
360
|
+
|
|
361
|
+
export function OverKeyboardView({ visible, children }: PropsWithChildren<{ visible: boolean }>) {
|
|
362
|
+
if (!visible) {
|
|
363
|
+
return null
|
|
364
|
+
}
|
|
365
|
+
return createPortal(
|
|
366
|
+
<View pointerEvents="box-none" style={OVER_KEYBOARD_STYLE}>
|
|
367
|
+
{children}
|
|
368
|
+
</View>,
|
|
369
|
+
document.body,
|
|
370
|
+
)
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
export function KeyboardExtender({ enabled = true, children }: PropsWithChildren<{ enabled?: boolean }>) {
|
|
374
|
+
return enabled ? <KeyboardStickyView style={{ position: 'absolute', left: 0, right: 0, bottom: 0 }}>{children}</KeyboardStickyView> : null
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
export const DefaultKeyboardToolbarTheme = {
|
|
378
|
+
light: { primary: '#007aff', disabled: '#b0bec5', background: '#f2f2f2', ripple: '#bcbcbc' },
|
|
379
|
+
dark: { primary: '#ffffff', disabled: '#707070', background: '#2c2c2e', ripple: '#F8F8F888' },
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
type KeyboardToolbarProps = { content?: React.ReactNode; doneText?: string; showArrows?: boolean; onDoneCallback?: () => void; offset?: { closed?: number; opened?: number }; enabled?: boolean }
|
|
383
|
+
|
|
384
|
+
/** The toolbar over the keyboard: what the app puts in it, and Done. */
|
|
385
|
+
export function KeyboardToolbar({ content, doneText = 'Done', onDoneCallback, offset, enabled = true }: KeyboardToolbarProps) {
|
|
386
|
+
const state = keyboard().use()
|
|
387
|
+
if (!enabled || !state.shown) {
|
|
388
|
+
return null
|
|
389
|
+
}
|
|
390
|
+
return (
|
|
391
|
+
<KeyboardStickyView offset={offset} style={TOOLBAR_STYLE.bar}>
|
|
392
|
+
<View style={TOOLBAR_STYLE.content}>{content}</View>
|
|
393
|
+
<Pressable
|
|
394
|
+
onPress={() => {
|
|
395
|
+
onDoneCallback?.()
|
|
396
|
+
blurFocusedField()
|
|
397
|
+
}}
|
|
398
|
+
>
|
|
399
|
+
<Text style={TOOLBAR_STYLE.done}>{doneText}</Text>
|
|
400
|
+
</Pressable>
|
|
401
|
+
</KeyboardStickyView>
|
|
402
|
+
)
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
const TOOLBAR_STYLE = {
|
|
406
|
+
bar: { position: 'absolute', left: 0, right: 0, bottom: 0, height: 42, flexDirection: 'row', alignItems: 'center', paddingHorizontal: 16, backgroundColor: '#f2f2f2' } as ViewStyle,
|
|
407
|
+
content: { flex: 1 } as ViewStyle,
|
|
408
|
+
done: { color: '#007aff', fontWeight: '600' as const, fontSize: 15 },
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
KeyboardToolbar.Background = () => null
|
|
412
|
+
KeyboardToolbar.Content = ({ children }: PropsWithChildren) => children
|
|
413
|
+
KeyboardToolbar.Prev = () => null
|
|
414
|
+
KeyboardToolbar.Next = () => null
|
|
415
|
+
KeyboardToolbar.Done = () => null
|
|
416
|
+
|
|
417
|
+
export function KeyboardEffects() {
|
|
418
|
+
return null
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
type KeyboardScrollViewProps = ComponentProps<typeof ScrollView> & {
|
|
422
|
+
bottomOffset?: number
|
|
423
|
+
disableScrollOnKeyboardHide?: boolean
|
|
424
|
+
enabled?: boolean
|
|
425
|
+
extraKeyboardSpace?: number
|
|
426
|
+
keyboardLiftBehavior?: string
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
function KeyboardSpacer({ lift, extra }: { lift: boolean; extra: number }) {
|
|
430
|
+
const { height } = useReanimatedKeyboardAnimation()
|
|
431
|
+
const spacerStyle = useAnimatedStyle(() => ({ height: lift ? Math.max(0, -height.value + extra) : 0 }), [lift, extra, height])
|
|
432
|
+
return <Animated.View style={spacerStyle} />
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
/** Leaves room under its content for the keyboard and scrolls the focused field into view once it has opened. */
|
|
436
|
+
function KeyboardScrollView(
|
|
437
|
+
{ bottomOffset = 0, disableScrollOnKeyboardHide: _disable, enabled = true, extraKeyboardSpace = 0, keyboardLiftBehavior, children, ...props }: KeyboardScrollViewProps,
|
|
438
|
+
ref: Ref<ComponentRef<typeof ScrollView>>,
|
|
439
|
+
) {
|
|
440
|
+
const { shown } = keyboard().use()
|
|
441
|
+
const lift = enabled && keyboardLiftBehavior !== 'never'
|
|
442
|
+
useEffect(() => {
|
|
443
|
+
if (!shown || !lift) {
|
|
444
|
+
return
|
|
445
|
+
}
|
|
446
|
+
const timer = setTimeout(() => {
|
|
447
|
+
const active = document.activeElement as HTMLElement | null
|
|
448
|
+
const { height } = keyboard().get()
|
|
449
|
+
if (active === null || active === document.body) {
|
|
450
|
+
return
|
|
451
|
+
}
|
|
452
|
+
const rect = active.getBoundingClientRect()
|
|
453
|
+
const limit = window.innerHeight - height - bottomOffset
|
|
454
|
+
const scroller = active.closest('[data-keyboard-should-persist-taps]') as HTMLElement | null
|
|
455
|
+
if (rect.bottom > limit && scroller !== null) {
|
|
456
|
+
scroller.scrollBy({ top: rect.bottom - limit, behavior: 'smooth' })
|
|
457
|
+
}
|
|
458
|
+
}, timing().drawnMs)
|
|
459
|
+
return () => clearTimeout(timer)
|
|
460
|
+
}, [shown, lift, bottomOffset])
|
|
461
|
+
return (
|
|
462
|
+
<ScrollView ref={ref} {...props}>
|
|
463
|
+
{children}
|
|
464
|
+
<KeyboardSpacer lift={lift} extra={extraKeyboardSpace} />
|
|
465
|
+
</ScrollView>
|
|
466
|
+
)
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
export const KeyboardAwareScrollView = forwardRef(KeyboardScrollView)
|
|
470
|
+
export const KeyboardChatScrollView = forwardRef(KeyboardScrollView)
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { defineOverrides } from '../../../../src/node/overrides/define.ts'
|
|
2
|
+
|
|
3
|
+
export default defineOverrides('react-native-keyboard-controller', [
|
|
4
|
+
{
|
|
5
|
+
versions: '>=1.22 <1.23',
|
|
6
|
+
note: 'follows the drawn keyboard: events, animated values, avoiding, sticky and aware scroll views',
|
|
7
|
+
replace: './KeyboardController.tsx',
|
|
8
|
+
},
|
|
9
|
+
])
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { definePlugin } from '../../src/node/plugins.ts'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The on-screen keyboard: drawn on the screen when the app focuses a text field, typed on by tapping it, and reported
|
|
5
|
+
* to the app through React Native's `Keyboard` and react-native-keyboard-controller. Every app with a text field
|
|
6
|
+
* needs it, so it is always active.
|
|
7
|
+
*/
|
|
8
|
+
export default definePlugin({
|
|
9
|
+
name: 'keyboard',
|
|
10
|
+
overrides: './overrides',
|
|
11
|
+
shell: './shell.tsx',
|
|
12
|
+
app: './app.ts',
|
|
13
|
+
})
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import { KeyboardIcon } from '@phosphor-icons/react'
|
|
2
|
+
import { useEffect, useState, useSyncExternalStore } from 'react'
|
|
3
|
+
import { Button, defineShellPlugin, Field, type OverlayProps, type PanelProps, type PluginContext, Row, Slider, TextField, Toggle } from 'web-sim/shell'
|
|
4
|
+
import { KeyboardController, sleep } from './controller.ts'
|
|
5
|
+
import { KeyboardView } from './Keyboard.tsx'
|
|
6
|
+
import { INITIAL_KEYBOARD, type KeyboardState, TIMING } from './state.ts'
|
|
7
|
+
import { typeText } from './typing.ts'
|
|
8
|
+
|
|
9
|
+
/** The shell keeps one keyboard; it is made when the shell starts (`setup`). */
|
|
10
|
+
let controller: KeyboardController | null = null
|
|
11
|
+
|
|
12
|
+
function keyboardFor(context: PluginContext<KeyboardState>): KeyboardController {
|
|
13
|
+
controller ??= new KeyboardController(context)
|
|
14
|
+
return controller
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
async function typeInto(keyboard: KeyboardController, text: string, speed?: number): Promise<KeyboardState> {
|
|
18
|
+
const state = keyboard.state()
|
|
19
|
+
// Typing starts once the keyboard has slid in.
|
|
20
|
+
const elapsed = performance.now() - keyboard.shownAt
|
|
21
|
+
const drawn = TIMING[keyboard.platform()].drawnMs
|
|
22
|
+
if (state.shown && elapsed < drawn) {
|
|
23
|
+
await sleep(drawn - elapsed + 80)
|
|
24
|
+
}
|
|
25
|
+
await typeText(keyboard, text, { wordsPerMinute: speed ?? state.wordsPerMinute, stopped: () => keyboard.state().field === null })
|
|
26
|
+
return keyboard.state()
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const KEY_NAMES: Record<string, string> = {
|
|
30
|
+
enter: 'return',
|
|
31
|
+
backspace: 'delete',
|
|
32
|
+
' ': 'space',
|
|
33
|
+
'123': 'layer:numbers',
|
|
34
|
+
numbers: 'layer:numbers',
|
|
35
|
+
symbols: 'layer:symbols',
|
|
36
|
+
abc: 'layer:letters',
|
|
37
|
+
letters: 'layer:letters',
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export default defineShellPlugin<KeyboardState>({
|
|
41
|
+
id: 'keyboard',
|
|
42
|
+
title: 'Keyboard',
|
|
43
|
+
icon: KeyboardIcon,
|
|
44
|
+
initialState: INITIAL_KEYBOARD,
|
|
45
|
+
setup: (context) => keyboardFor(context).attach(),
|
|
46
|
+
actions: {
|
|
47
|
+
/** Types `text` into the focused field through the keyboard, at `speed` words per minute. */
|
|
48
|
+
type: (context, args) => {
|
|
49
|
+
const { text, speed } = args as { text: string; speed?: number }
|
|
50
|
+
if (context.state().field === null) {
|
|
51
|
+
throw new Error('No text field is focused')
|
|
52
|
+
}
|
|
53
|
+
return typeInto(keyboardFor(context), String(text), speed)
|
|
54
|
+
},
|
|
55
|
+
/** Blurs the focused field, which puts the keyboard away. */
|
|
56
|
+
dismiss: (context) => {
|
|
57
|
+
keyboardFor(context).dismiss()
|
|
58
|
+
},
|
|
59
|
+
/** Presses one key: `return`, `delete`, `shift`, `space`, `numbers`, `symbols`, `letters`, or a character. */
|
|
60
|
+
press: async (context, args) => {
|
|
61
|
+
const { key } = args as { key: string }
|
|
62
|
+
const keyboard = keyboardFor(context)
|
|
63
|
+
const name = KEY_NAMES[key.toLowerCase()] ?? key
|
|
64
|
+
const id = [name, `letter:${name.toLowerCase()}`, `char:${name}`].find((candidate) => keyboard.findKey(candidate) !== undefined)
|
|
65
|
+
if (id === undefined) {
|
|
66
|
+
const keys = keyboard.layout().keys.map((spec) => spec.id)
|
|
67
|
+
throw new Error(`No key '${key}' on the keyboard now. Keys: ${keys.join(', ')}`)
|
|
68
|
+
}
|
|
69
|
+
await keyboard.tap(id, 80)
|
|
70
|
+
return context.state()
|
|
71
|
+
},
|
|
72
|
+
},
|
|
73
|
+
Overlay: KeyboardOverlay,
|
|
74
|
+
Panel: KeyboardPanel,
|
|
75
|
+
})
|
|
76
|
+
|
|
77
|
+
function KeyboardOverlay({ state, context, sim }: OverlayProps<KeyboardState>) {
|
|
78
|
+
const keyboard = keyboardFor(context)
|
|
79
|
+
const layout = keyboard.layout(sim)
|
|
80
|
+
// A rotation, or a setting that changes the keyboard's height, moves it as the device does.
|
|
81
|
+
useEffect(() => keyboard.sync(), [keyboard, layout.height, state.showOnFocus])
|
|
82
|
+
return <KeyboardView controller={keyboard} state={state} layout={layout} appearance={{ device: sim.appearance, app: sim.appAppearance }} />
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** The panel's buttons keep the app's field focused, as the keyboard's own keys do. */
|
|
86
|
+
const keepFocus = (event: { preventDefault(): void }) => event.preventDefault()
|
|
87
|
+
|
|
88
|
+
function KeyboardPanel({ state, set, run, sim }: PanelProps<KeyboardState>) {
|
|
89
|
+
const [text, setText] = useState('Hello from web-sim')
|
|
90
|
+
const keyboard = controller
|
|
91
|
+
const armed = useSyncExternalStore(keyboard?.subscribe ?? noSubscribe, () => keyboard?.isArmed() ?? false)
|
|
92
|
+
const field = state.field
|
|
93
|
+
const type = () => {
|
|
94
|
+
if (keyboard === null) {
|
|
95
|
+
return
|
|
96
|
+
}
|
|
97
|
+
if (armed) {
|
|
98
|
+
keyboard.arm(null)
|
|
99
|
+
} else if (field === null) {
|
|
100
|
+
// Typing waits for the next field the app focuses: the text box above took the focus from the app.
|
|
101
|
+
keyboard.arm((focused) => void typeInto(focused, text))
|
|
102
|
+
} else {
|
|
103
|
+
void Promise.resolve(run('type', { text })).catch(() => undefined)
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
return (
|
|
107
|
+
<>
|
|
108
|
+
<Field label="Field" value={field === null ? 'None' : `${field.keyboardType} · ${field.returnKeyType}`} />
|
|
109
|
+
<Field label="Keyboard" value={state.shown ? `${Math.round(state.height)} pt · ${state.layer}` : 'Hidden'} />
|
|
110
|
+
<Toggle label="Show on focus" checked={state.showOnFocus} onChange={(showOnFocus) => set({ showOnFocus })} />
|
|
111
|
+
<Toggle label="Suggestions" checked={state.suggestions} onChange={(suggestions) => set({ suggestions })} />
|
|
112
|
+
{sim.device.platform === 'android' && <Toggle label="Number row" checked={state.numberRow} onChange={(numberRow) => set({ numberRow })} />}
|
|
113
|
+
<Field label="Typing speed" value={`${state.wordsPerMinute} wpm`}>
|
|
114
|
+
<Slider label="Typing speed" value={state.wordsPerMinute} min={15} max={150} step={5} onChange={(wordsPerMinute) => set({ wordsPerMinute })} />
|
|
115
|
+
</Field>
|
|
116
|
+
<Field label="Type">
|
|
117
|
+
<TextField label="Text to type" value={text} onChange={setText} />
|
|
118
|
+
</Field>
|
|
119
|
+
<div onMouseDown={keepFocus}>
|
|
120
|
+
<Row>
|
|
121
|
+
<Button title={armed ? 'Stop waiting for a field' : 'Type into the focused field, or the next one focused'} onClick={type}>
|
|
122
|
+
{armed ? 'Cancel' : 'Type'}
|
|
123
|
+
</Button>
|
|
124
|
+
<Button title="Dismiss the keyboard" onClick={() => run('dismiss')}>
|
|
125
|
+
Dismiss
|
|
126
|
+
</Button>
|
|
127
|
+
</Row>
|
|
128
|
+
</div>
|
|
129
|
+
</>
|
|
130
|
+
)
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
const noSubscribe = () => () => undefined
|