@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.
- 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 +157 -0
- package/overrides/react-native-web/useElementLayout.ts +152 -0
- package/package.json +80 -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 +129 -0
- package/src/client/app/appFocus.ts +97 -0
- package/src/client/app/console.ts +118 -0
- package/src/client/app/fonts.ts +76 -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 +23 -0
- package/src/devices/catalog.ts +223 -0
- package/src/devices/types.ts +83 -0
- package/src/node/appInfo.ts +188 -0
- package/src/node/appSource.ts +140 -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/fonts.ts +377 -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 +169 -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 +254 -0
- package/src/node/worklets.ts +86 -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 +519 -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,384 @@
|
|
|
1
|
+
import type { PluginContext, SimState } from 'web-sim/shell'
|
|
2
|
+
import { buildLayout, type KeyboardLayout, type KeySpec, type LayoutInput } from './layouts/index.ts'
|
|
3
|
+
import { currentWord, type Suggestion } from './suggestions.ts'
|
|
4
|
+
import { type AutoCapitalize, type Edit, type FieldConfig, type KeyboardFrame, type KeyboardState, type Shift, TIMING, TRANSIENT } from './state.ts'
|
|
5
|
+
|
|
6
|
+
/** What changes with every key: kept out of the plugin's state, which is saved on every change. */
|
|
7
|
+
export type Live = { pressed: string | null; textBefore: string }
|
|
8
|
+
|
|
9
|
+
const DOUBLE_TAP_MS = 350
|
|
10
|
+
const DOUBLE_SPACE_MS = 600
|
|
11
|
+
const DELETE_REPEAT_DELAY_MS = 500
|
|
12
|
+
const DELETE_REPEAT_INTERVAL_MS = 90
|
|
13
|
+
const PHYSICAL_KEY_FLASH_MS = 90
|
|
14
|
+
|
|
15
|
+
const DEFAULT_CONFIG: FieldConfig = { keyboardType: 'default', returnKeyType: 'default', autoCapitalize: 'sentences', suggestions: true, multiline: false, appearance: 'default' }
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* The keyboard's behaviour, in the shell: which keys show, what a key does, shift and caps lock, a double space for a
|
|
19
|
+
* full stop, delete repeating while held, keys lighting up as the computer's keyboard types, and the keyboard
|
|
20
|
+
* sliding in and out with React Native's events. Edits go to the app's half, which makes them in the field.
|
|
21
|
+
*/
|
|
22
|
+
export class KeyboardController {
|
|
23
|
+
private readonly context: PluginContext<KeyboardState>
|
|
24
|
+
private live: Live = { pressed: null, textBefore: '' }
|
|
25
|
+
private listeners = new Set<() => void>()
|
|
26
|
+
/** The last field's config, so the keys don't change while the keyboard slides away. */
|
|
27
|
+
private config: FieldConfig = DEFAULT_CONFIG
|
|
28
|
+
private lastShiftTap = 0
|
|
29
|
+
private lastSpaceTap = 0
|
|
30
|
+
private deleteRepeat: ReturnType<typeof setTimeout> | undefined
|
|
31
|
+
private flashTimer: ReturnType<typeof setTimeout> | undefined
|
|
32
|
+
private settleTimer: ReturnType<typeof setTimeout> | undefined
|
|
33
|
+
private layoutCache: { key: string; layout: KeyboardLayout } | null = null
|
|
34
|
+
/** When the keyboard last started sliding in. */
|
|
35
|
+
shownAt = 0
|
|
36
|
+
/** Typing waiting for the next field to focus, as the panel's Type button asks with no field focused. */
|
|
37
|
+
private armed: ((keyboard: KeyboardController) => void) | null = null
|
|
38
|
+
|
|
39
|
+
constructor(context: PluginContext<KeyboardState>) {
|
|
40
|
+
this.context = context
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** Listens to the app's half. Returns the cleanup. */
|
|
44
|
+
attach(): () => void {
|
|
45
|
+
const { context } = this
|
|
46
|
+
context.set(TRANSIENT)
|
|
47
|
+
const removals = [
|
|
48
|
+
context.on('focus', (payload) => {
|
|
49
|
+
const { config, textBefore } = payload as { config: FieldConfig; textBefore: string }
|
|
50
|
+
this.focus(config, textBefore)
|
|
51
|
+
}),
|
|
52
|
+
context.on('blur', () => this.blur()),
|
|
53
|
+
context.on('text', (payload) => this.text((payload as { textBefore: string }).textBefore)),
|
|
54
|
+
context.on('physical-key', (payload) => this.flash((payload as { key: string }).key)),
|
|
55
|
+
]
|
|
56
|
+
// A field the app focused before the shell listened.
|
|
57
|
+
context.emit('sync')
|
|
58
|
+
return () => removals.forEach((remove) => remove())
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
getLive = (): Live => this.live
|
|
62
|
+
|
|
63
|
+
/** Runs `start` when the next field focuses; null cancels. */
|
|
64
|
+
arm(start: ((keyboard: KeyboardController) => void) | null): void {
|
|
65
|
+
this.armed = start
|
|
66
|
+
this.notify()
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
isArmed(): boolean {
|
|
70
|
+
return this.armed !== null
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
subscribe = (listener: () => void): (() => void) => {
|
|
74
|
+
this.listeners.add(listener)
|
|
75
|
+
return () => this.listeners.delete(listener)
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
platform(): 'ios' | 'android' {
|
|
79
|
+
return this.context.sim().device.platform
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
state(): KeyboardState {
|
|
83
|
+
return this.context.state()
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
fieldConfig(): FieldConfig {
|
|
87
|
+
return this.state().field ?? this.config
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/** What the layout is built from now. */
|
|
91
|
+
layoutInput(sim: SimState = this.context.sim()): LayoutInput {
|
|
92
|
+
const state = this.state()
|
|
93
|
+
const config = this.fieldConfig()
|
|
94
|
+
const { device, orientation } = sim
|
|
95
|
+
const insets = device.insets[orientation]
|
|
96
|
+
return {
|
|
97
|
+
platform: device.platform,
|
|
98
|
+
width: orientation === 'portrait' ? device.screen.width : device.screen.height,
|
|
99
|
+
orientation,
|
|
100
|
+
insets,
|
|
101
|
+
homeButton: device.navigation === 'home-button',
|
|
102
|
+
navigationHeight: device.platform === 'android' ? insets.bottom : 0,
|
|
103
|
+
keyboardType: config.keyboardType,
|
|
104
|
+
layer: state.layer,
|
|
105
|
+
suggestions: state.suggestions && (device.platform === 'android' || config.suggestions),
|
|
106
|
+
numberRow: state.numberRow,
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
layout(sim: SimState = this.context.sim()): KeyboardLayout {
|
|
111
|
+
const input = this.layoutInput(sim)
|
|
112
|
+
const key = JSON.stringify(input)
|
|
113
|
+
if (this.layoutCache?.key !== key) {
|
|
114
|
+
this.layoutCache = { key, layout: buildLayout(input) }
|
|
115
|
+
}
|
|
116
|
+
return this.layoutCache.layout
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
findKey(id: string): KeySpec | undefined {
|
|
120
|
+
return this.layout().keys.find((key) => key.id === id)
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Brings the keyboard in line with the focused field and the settings: shown when a field is focused and the
|
|
125
|
+
* keyboard shows on focus, and at the height the current layout needs.
|
|
126
|
+
*/
|
|
127
|
+
sync(): void {
|
|
128
|
+
const state = this.state()
|
|
129
|
+
this.slide(state.field !== null && state.showOnFocus)
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
dismiss(): void {
|
|
133
|
+
this.context.emit('dismiss')
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
press(id: string): void {
|
|
137
|
+
const key = this.findKey(id)
|
|
138
|
+
if (key === undefined) {
|
|
139
|
+
return
|
|
140
|
+
}
|
|
141
|
+
this.setLive({ pressed: id })
|
|
142
|
+
if (key.action.kind === 'delete') {
|
|
143
|
+
this.edit({ type: 'delete' })
|
|
144
|
+
this.deleteRepeat = setTimeout(() => this.repeatDelete(), DELETE_REPEAT_DELAY_MS)
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
release(id: string): void {
|
|
149
|
+
clearTimeout(this.deleteRepeat)
|
|
150
|
+
if (this.live.pressed !== id) {
|
|
151
|
+
return
|
|
152
|
+
}
|
|
153
|
+
this.setLive({ pressed: null })
|
|
154
|
+
const key = this.findKey(id)
|
|
155
|
+
if (key !== undefined) {
|
|
156
|
+
this.perform(key)
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
cancel(): void {
|
|
161
|
+
clearTimeout(this.deleteRepeat)
|
|
162
|
+
this.setLive({ pressed: null })
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
async tap(id: string, holdMs: number): Promise<void> {
|
|
166
|
+
this.press(id)
|
|
167
|
+
await sleep(holdMs)
|
|
168
|
+
this.release(id)
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
applySuggestion(suggestion: Suggestion): void {
|
|
172
|
+
const word = currentWord(this.live.textBefore)
|
|
173
|
+
this.edit({ type: 'replace', length: word.length, text: `${suggestion.replacement} ` })
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/** Types a character no key makes, as a long press or the emoji keyboard would. */
|
|
177
|
+
insertRaw(text: string): void {
|
|
178
|
+
this.edit({ type: 'insert', text })
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
private focus(config: FieldConfig, textBefore: string): void {
|
|
182
|
+
this.config = config
|
|
183
|
+
this.live = { pressed: null, textBefore }
|
|
184
|
+
this.context.set({ field: config, layer: 'letters', shift: autoShift(textBefore, config.autoCapitalize) ? 'on' : 'off' })
|
|
185
|
+
this.notify()
|
|
186
|
+
this.sync()
|
|
187
|
+
const start = this.armed
|
|
188
|
+
if (start !== null) {
|
|
189
|
+
this.armed = null
|
|
190
|
+
this.notify()
|
|
191
|
+
start(this)
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
private blur(): void {
|
|
196
|
+
clearTimeout(this.deleteRepeat)
|
|
197
|
+
this.context.set({ field: null })
|
|
198
|
+
this.setLive({ pressed: null })
|
|
199
|
+
this.sync()
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
private text(textBefore: string): void {
|
|
203
|
+
if (textBefore === this.live.textBefore) {
|
|
204
|
+
return
|
|
205
|
+
}
|
|
206
|
+
this.setLive({ textBefore })
|
|
207
|
+
const state = this.state()
|
|
208
|
+
if (state.shift !== 'locked' && state.field !== null) {
|
|
209
|
+
this.context.set({ shift: autoShift(textBefore, state.field.autoCapitalize) ? 'on' : 'off' })
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
private edit(edit: Edit): void {
|
|
214
|
+
if (this.state().field !== null) {
|
|
215
|
+
this.context.emit('edit', edit)
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
private perform(key: KeySpec): void {
|
|
220
|
+
const { action } = key
|
|
221
|
+
const state = this.state()
|
|
222
|
+
switch (action.kind) {
|
|
223
|
+
case 'letter': {
|
|
224
|
+
// The text that comes back sets the shift again, from the field's autocapitalisation.
|
|
225
|
+
this.edit({ type: 'insert', text: state.shift !== 'off' ? action.char.toUpperCase() : action.char })
|
|
226
|
+
break
|
|
227
|
+
}
|
|
228
|
+
case 'char':
|
|
229
|
+
this.edit({ type: 'insert', text: action.char })
|
|
230
|
+
if (action.char === "'" && state.layer !== 'letters') {
|
|
231
|
+
this.context.set({ layer: 'letters' })
|
|
232
|
+
}
|
|
233
|
+
break
|
|
234
|
+
case 'space':
|
|
235
|
+
this.typeSpace()
|
|
236
|
+
if (state.layer !== 'letters') {
|
|
237
|
+
this.context.set({ layer: 'letters' })
|
|
238
|
+
}
|
|
239
|
+
break
|
|
240
|
+
case 'return':
|
|
241
|
+
this.edit({ type: 'return' })
|
|
242
|
+
break
|
|
243
|
+
case 'shift':
|
|
244
|
+
this.tapShift()
|
|
245
|
+
break
|
|
246
|
+
case 'layer':
|
|
247
|
+
this.context.set({ layer: action.layer })
|
|
248
|
+
break
|
|
249
|
+
case 'delete':
|
|
250
|
+
case 'emoji':
|
|
251
|
+
case 'dictation':
|
|
252
|
+
break
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
private typeSpace(): void {
|
|
257
|
+
const now = performance.now()
|
|
258
|
+
const doubleSpace = now - this.lastSpaceTap < DOUBLE_SPACE_MS && /[A-Za-z0-9] $/.test(this.live.textBefore)
|
|
259
|
+
this.lastSpaceTap = doubleSpace ? 0 : now
|
|
260
|
+
if (doubleSpace) {
|
|
261
|
+
this.edit({ type: 'replace', length: 1, text: '. ' })
|
|
262
|
+
return
|
|
263
|
+
}
|
|
264
|
+
this.edit({ type: 'insert', text: ' ' })
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
private tapShift(): void {
|
|
268
|
+
const now = performance.now()
|
|
269
|
+
const doubleTap = now - this.lastShiftTap < DOUBLE_TAP_MS
|
|
270
|
+
this.lastShiftTap = now
|
|
271
|
+
const next: Record<Shift, Shift> = { off: doubleTap ? 'locked' : 'on', on: doubleTap ? 'locked' : 'off', locked: 'off' }
|
|
272
|
+
this.context.set({ shift: next[this.state().shift] })
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
private repeatDelete(): void {
|
|
276
|
+
this.edit({ type: 'delete' })
|
|
277
|
+
this.deleteRepeat = setTimeout(() => this.repeatDelete(), DELETE_REPEAT_INTERVAL_MS)
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
private flash(eventKey: string): void {
|
|
281
|
+
const id = physicalKeyId(eventKey)
|
|
282
|
+
if (id === null || !this.state().shown || this.findKey(id) === undefined) {
|
|
283
|
+
return
|
|
284
|
+
}
|
|
285
|
+
clearTimeout(this.flashTimer)
|
|
286
|
+
this.setLive({ pressed: id })
|
|
287
|
+
this.flashTimer = setTimeout(() => {
|
|
288
|
+
if (this.live.pressed === id) {
|
|
289
|
+
this.setLive({ pressed: null })
|
|
290
|
+
}
|
|
291
|
+
}, PHYSICAL_KEY_FLASH_MS)
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
/**
|
|
295
|
+
* Slides the keyboard in or out, or to a new height, with React Native's events: iOS sends `keyboardWillShow` as it
|
|
296
|
+
* starts and `keyboardDidShow` as it settles; Android only the `Did` events.
|
|
297
|
+
*/
|
|
298
|
+
private slide(show: boolean): void {
|
|
299
|
+
const state = this.state()
|
|
300
|
+
const sim = this.context.sim()
|
|
301
|
+
const platform = sim.device.platform
|
|
302
|
+
const timing = TIMING[platform]
|
|
303
|
+
const screen = sim.orientation === 'portrait' ? sim.device.screen : { width: sim.device.screen.height, height: sim.device.screen.width }
|
|
304
|
+
const height = this.layout(sim).height
|
|
305
|
+
const at = (keyboardHeight: number, visible: boolean): KeyboardFrame => ({
|
|
306
|
+
screenX: 0,
|
|
307
|
+
screenY: visible ? screen.height - keyboardHeight : screen.height,
|
|
308
|
+
width: screen.width,
|
|
309
|
+
height: keyboardHeight,
|
|
310
|
+
})
|
|
311
|
+
if (show && state.shown && state.height === height) {
|
|
312
|
+
return
|
|
313
|
+
}
|
|
314
|
+
if (!show && !state.shown) {
|
|
315
|
+
return
|
|
316
|
+
}
|
|
317
|
+
clearTimeout(this.settleTimer)
|
|
318
|
+
const start = state.shown ? at(state.height, true) : at(height, false)
|
|
319
|
+
const end = show ? at(height, true) : platform === 'ios' ? at(state.height, false) : at(0, false)
|
|
320
|
+
const payload =
|
|
321
|
+
platform === 'ios'
|
|
322
|
+
? { startCoordinates: start, endCoordinates: end, duration: timing.reportedMs, easing: 'keyboard', isEventFromThisApp: true }
|
|
323
|
+
: { endCoordinates: end, duration: timing.reportedMs, easing: 'keyboard' }
|
|
324
|
+
if (show) {
|
|
325
|
+
this.shownAt = performance.now()
|
|
326
|
+
this.context.set({ shown: true, height })
|
|
327
|
+
} else {
|
|
328
|
+
this.context.set({ shown: false })
|
|
329
|
+
this.cancel()
|
|
330
|
+
}
|
|
331
|
+
if (platform === 'ios') {
|
|
332
|
+
this.context.emit(show ? 'keyboardWillShow' : 'keyboardWillHide', payload)
|
|
333
|
+
}
|
|
334
|
+
this.settleTimer = setTimeout(() => {
|
|
335
|
+
this.context.set({ frame: show ? end : null })
|
|
336
|
+
this.context.emit(show ? 'keyboardDidShow' : 'keyboardDidHide', payload)
|
|
337
|
+
}, timing.drawnMs)
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
private setLive(patch: Partial<Live>): void {
|
|
341
|
+
this.live = { ...this.live, ...patch }
|
|
342
|
+
this.notify()
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
private notify(): void {
|
|
346
|
+
for (const listener of this.listeners) {
|
|
347
|
+
listener()
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
function autoShift(textBefore: string, mode: AutoCapitalize): boolean {
|
|
353
|
+
switch (mode) {
|
|
354
|
+
case 'none':
|
|
355
|
+
return false
|
|
356
|
+
case 'characters':
|
|
357
|
+
return true
|
|
358
|
+
case 'words':
|
|
359
|
+
return textBefore === '' || /\s$/.test(textBefore)
|
|
360
|
+
case 'sentences':
|
|
361
|
+
return textBefore.trim() === '' || /[.!?]\s+$/.test(textBefore)
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
function physicalKeyId(eventKey: string): string | null {
|
|
366
|
+
switch (eventKey) {
|
|
367
|
+
case ' ':
|
|
368
|
+
return 'space'
|
|
369
|
+
case 'Backspace':
|
|
370
|
+
return 'delete'
|
|
371
|
+
case 'Enter':
|
|
372
|
+
return 'return'
|
|
373
|
+
case 'Shift':
|
|
374
|
+
return 'shift'
|
|
375
|
+
}
|
|
376
|
+
if (eventKey.length !== 1) {
|
|
377
|
+
return null
|
|
378
|
+
}
|
|
379
|
+
return /[a-z]/i.test(eventKey) ? `letter:${eventKey.toLowerCase()}` : `char:${eventKey}`
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
export function sleep(ms: number): Promise<void> {
|
|
383
|
+
return new Promise((resolve) => setTimeout(resolve, ms))
|
|
384
|
+
}
|
|
@@ -0,0 +1,244 @@
|
|
|
1
|
+
import type { AutoCapitalize, FieldConfig, KeyboardAppearance, KeyboardType, ReturnKeyType } from './state.ts'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Text fields in the app's document: which elements are fields, what keyboard they ask for, and editing them so
|
|
5
|
+
* React sees the change. React Native Web renders `TextInput` as an `input` or `textarea` and turns its props into
|
|
6
|
+
* attributes: `keyboardType` into `inputmode`, `returnKeyType` into `enterkeyhint`, `autoCapitalize`, `autoCorrect`,
|
|
7
|
+
* `secureTextEntry` into `type="password"`. The react-native-web override adds `keyboardAppearance` as
|
|
8
|
+
* `data-keyboard-appearance`.
|
|
9
|
+
*
|
|
10
|
+
* Elements are recognised by tag name, not `instanceof`, so this works on a document from another window.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
export type Editable = HTMLInputElement | HTMLTextAreaElement
|
|
14
|
+
|
|
15
|
+
const TEXT_INPUT_TYPES = new Set(['text', 'search', 'email', 'tel', 'url', 'number', 'password'])
|
|
16
|
+
|
|
17
|
+
function isTextArea(element: Element): element is HTMLTextAreaElement {
|
|
18
|
+
return element.tagName === 'TEXTAREA'
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function isInput(element: Element): element is HTMLInputElement {
|
|
22
|
+
return element.tagName === 'INPUT'
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function asEditable(element: Element | null | undefined): Editable | null {
|
|
26
|
+
if (element === null || element === undefined) {
|
|
27
|
+
return null
|
|
28
|
+
}
|
|
29
|
+
if (isTextArea(element)) {
|
|
30
|
+
return element.readOnly ? null : element
|
|
31
|
+
}
|
|
32
|
+
if (isInput(element) && TEXT_INPUT_TYPES.has(element.type)) {
|
|
33
|
+
return element.readOnly ? null : element
|
|
34
|
+
}
|
|
35
|
+
return null
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** Inside a field, or anything the user can type in. */
|
|
39
|
+
export function isEditable(element: Element): boolean {
|
|
40
|
+
return (
|
|
41
|
+
element.closest('input, textarea, [contenteditable="true"], [contenteditable=""]') !== null ||
|
|
42
|
+
(element as HTMLElement).isContentEditable === true
|
|
43
|
+
)
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export function focusedField(document: Document): Editable | null {
|
|
47
|
+
return asEditable(document.activeElement)
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export function readFieldConfig(field: Editable): FieldConfig {
|
|
51
|
+
const keyboardType = readKeyboardType(field)
|
|
52
|
+
return {
|
|
53
|
+
keyboardType,
|
|
54
|
+
returnKeyType: readReturnKeyType(field),
|
|
55
|
+
autoCapitalize: readAutoCapitalize(field, keyboardType),
|
|
56
|
+
suggestions: keyboardType === 'default' && readAutoCorrect(field),
|
|
57
|
+
multiline: isTextArea(field),
|
|
58
|
+
appearance: readAppearance(field),
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function readAppearance(field: Editable): KeyboardAppearance {
|
|
63
|
+
const value = field.dataset.keyboardAppearance
|
|
64
|
+
return value === 'light' || value === 'dark' ? value : 'default'
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function readKeyboardType(field: Editable): KeyboardType {
|
|
68
|
+
const mode = field.getAttribute('inputmode') ?? (isInput(field) ? field.type : '')
|
|
69
|
+
switch (mode) {
|
|
70
|
+
case 'email':
|
|
71
|
+
return 'email'
|
|
72
|
+
case 'url':
|
|
73
|
+
return 'url'
|
|
74
|
+
case 'decimal':
|
|
75
|
+
return 'decimal'
|
|
76
|
+
case 'numeric':
|
|
77
|
+
case 'number':
|
|
78
|
+
return 'number'
|
|
79
|
+
case 'tel':
|
|
80
|
+
return 'phone'
|
|
81
|
+
default:
|
|
82
|
+
return 'default'
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function readReturnKeyType(field: Editable): ReturnKeyType {
|
|
87
|
+
switch (field.getAttribute('enterkeyhint')) {
|
|
88
|
+
case 'search':
|
|
89
|
+
return 'search'
|
|
90
|
+
case 'go':
|
|
91
|
+
return 'go'
|
|
92
|
+
case 'send':
|
|
93
|
+
return 'send'
|
|
94
|
+
case 'done':
|
|
95
|
+
return 'done'
|
|
96
|
+
case 'next':
|
|
97
|
+
return 'next'
|
|
98
|
+
default:
|
|
99
|
+
return isInput(field) && field.type === 'search' ? 'search' : 'default'
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
function readAutoCapitalize(field: Editable, keyboardType: KeyboardType): AutoCapitalize {
|
|
104
|
+
switch (field.getAttribute('autocapitalize')) {
|
|
105
|
+
case 'none':
|
|
106
|
+
case 'off':
|
|
107
|
+
return 'none'
|
|
108
|
+
case 'words':
|
|
109
|
+
return 'words'
|
|
110
|
+
case 'characters':
|
|
111
|
+
return 'characters'
|
|
112
|
+
case 'sentences':
|
|
113
|
+
case 'on':
|
|
114
|
+
return 'sentences'
|
|
115
|
+
default:
|
|
116
|
+
return keyboardType === 'default' ? 'sentences' : 'none'
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
function readAutoCorrect(field: Editable): boolean {
|
|
121
|
+
if (isInput(field) && field.type === 'password') {
|
|
122
|
+
return false
|
|
123
|
+
}
|
|
124
|
+
return field.getAttribute('autocorrect') !== 'off'
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
export function textBeforeCaret(field: Editable): string {
|
|
128
|
+
// Inputs of type `email` and `number` have no selection API.
|
|
129
|
+
let caret: number | null = null
|
|
130
|
+
try {
|
|
131
|
+
caret = field.selectionStart
|
|
132
|
+
} catch {
|
|
133
|
+
caret = null
|
|
134
|
+
}
|
|
135
|
+
return field.value.slice(0, caret ?? field.value.length)
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/** The native `value` setter, which React's change tracking does not see, so the `input` event that follows counts. */
|
|
139
|
+
function setValue(field: Editable, value: string): void {
|
|
140
|
+
const prototype = Object.getPrototypeOf(field) as object
|
|
141
|
+
const setter = Object.getOwnPropertyDescriptor(prototype, 'value')?.set
|
|
142
|
+
if (setter !== undefined) {
|
|
143
|
+
setter.call(field, value)
|
|
144
|
+
} else {
|
|
145
|
+
field.value = value
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
function dispatchInput(field: Editable, inputType: string, data: string | null): void {
|
|
150
|
+
const view = field.ownerDocument.defaultView
|
|
151
|
+
const FieldInputEvent = view?.InputEvent ?? InputEvent
|
|
152
|
+
field.dispatchEvent(new FieldInputEvent('input', { bubbles: true, inputType, data }))
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
function caretRange(field: Editable): [number, number] {
|
|
156
|
+
try {
|
|
157
|
+
if (field.selectionStart !== null && field.selectionEnd !== null) {
|
|
158
|
+
return [field.selectionStart, field.selectionEnd]
|
|
159
|
+
}
|
|
160
|
+
} catch {
|
|
161
|
+
// No selection API on this input type.
|
|
162
|
+
}
|
|
163
|
+
return [field.value.length, field.value.length]
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* Types `text` at the caret. `execCommand` edits as typing does, with undo and an `input` event React handles; a
|
|
168
|
+
* document that refuses it (its window is in the background) gets the value set and the event sent by hand.
|
|
169
|
+
*/
|
|
170
|
+
export function insertText(field: Editable, text: string): void {
|
|
171
|
+
const document = field.ownerDocument
|
|
172
|
+
if (document.activeElement !== field) {
|
|
173
|
+
field.focus()
|
|
174
|
+
}
|
|
175
|
+
if (field.maxLength >= 0 && field.value.length >= field.maxLength) {
|
|
176
|
+
return
|
|
177
|
+
}
|
|
178
|
+
if (document.execCommand('insertText', false, text)) {
|
|
179
|
+
return
|
|
180
|
+
}
|
|
181
|
+
const [start, end] = caretRange(field)
|
|
182
|
+
setValue(field, field.value.slice(0, start) + text + field.value.slice(end))
|
|
183
|
+
try {
|
|
184
|
+
field.setSelectionRange(start + text.length, start + text.length)
|
|
185
|
+
} catch {
|
|
186
|
+
// No selection API on this input type.
|
|
187
|
+
}
|
|
188
|
+
dispatchInput(field, 'insertText', text)
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
export function deleteBackward(field: Editable): void {
|
|
192
|
+
const document = field.ownerDocument
|
|
193
|
+
if (document.activeElement !== field) {
|
|
194
|
+
field.focus()
|
|
195
|
+
}
|
|
196
|
+
if (document.execCommand('delete')) {
|
|
197
|
+
return
|
|
198
|
+
}
|
|
199
|
+
const [start, end] = caretRange(field)
|
|
200
|
+
if (start === end && start === 0) {
|
|
201
|
+
return
|
|
202
|
+
}
|
|
203
|
+
const from = start === end ? start - 1 : start
|
|
204
|
+
setValue(field, field.value.slice(0, from) + field.value.slice(end))
|
|
205
|
+
try {
|
|
206
|
+
field.setSelectionRange(from, from)
|
|
207
|
+
} catch {
|
|
208
|
+
// No selection API on this input type.
|
|
209
|
+
}
|
|
210
|
+
dispatchInput(field, 'deleteContentBackward', null)
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
export function replaceBeforeCaret(field: Editable, length: number, text: string): void {
|
|
214
|
+
const [caret] = caretRange(field)
|
|
215
|
+
try {
|
|
216
|
+
field.setSelectionRange(caret - length, caret)
|
|
217
|
+
} catch {
|
|
218
|
+
// No selection API on this input type.
|
|
219
|
+
}
|
|
220
|
+
insertText(field, text)
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/**
|
|
224
|
+
* The return key: a new line in a multiline field, else Enter, which React Native Web turns into `onSubmitEditing`
|
|
225
|
+
* (and a blur, unless `blurOnSubmit` / `submitBehavior` keep the focus).
|
|
226
|
+
*/
|
|
227
|
+
export function pressReturn(field: Editable): void {
|
|
228
|
+
if (isTextArea(field)) {
|
|
229
|
+
const init: KeyboardEventInit = { key: 'Enter', code: 'Enter', bubbles: true, cancelable: true }
|
|
230
|
+
const FieldKeyboardEvent = field.ownerDocument.defaultView?.KeyboardEvent ?? KeyboardEvent
|
|
231
|
+
if (field.dispatchEvent(new FieldKeyboardEvent('keydown', init))) {
|
|
232
|
+
insertText(field, '\n')
|
|
233
|
+
}
|
|
234
|
+
field.dispatchEvent(new FieldKeyboardEvent('keyup', init))
|
|
235
|
+
return
|
|
236
|
+
}
|
|
237
|
+
const FieldKeyboardEvent = field.ownerDocument.defaultView?.KeyboardEvent ?? KeyboardEvent
|
|
238
|
+
const init: KeyboardEventInit = { key: 'Enter', code: 'Enter', bubbles: true, cancelable: true }
|
|
239
|
+
const handled = !field.dispatchEvent(new FieldKeyboardEvent('keydown', init))
|
|
240
|
+
field.dispatchEvent(new FieldKeyboardEvent('keyup', init))
|
|
241
|
+
if (!handled && field.form !== null) {
|
|
242
|
+
field.form.requestSubmit()
|
|
243
|
+
}
|
|
244
|
+
}
|