@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,640 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Scrolls the app's lists by dragging, as a finger does on a phone. Browsers only scroll with a wheel or trackpad,
|
|
3
|
+
* so without this a drag over a list does nothing, or presses whatever it started on. It also answers the made-up
|
|
4
|
+
* pointer events a script sends, so a swipe from `web-sim` scrolls too.
|
|
5
|
+
*
|
|
6
|
+
* A horizontal drag scrolls a horizontal list under it (a carousel, a row of chips), settling on a page when the list
|
|
7
|
+
* has `pagingEnabled`. Where there is none, it is left to the app, for swipeable rows and pagers.
|
|
8
|
+
*
|
|
9
|
+
* On iOS a list goes past its ends as UIScrollView does: it pulls with growing resistance, a fling carries into the
|
|
10
|
+
* edge and bounces, and it springs back when let go. While it is past an end, `onScroll` reports the offset iOS
|
|
11
|
+
* would, below zero or past the end of the content. A RefreshControl refreshes when pulled far enough, and holds its
|
|
12
|
+
* spinner open while `refreshing`. Android stops at the ends.
|
|
13
|
+
*
|
|
14
|
+
* The overscroll is drawn as space before or after the content (the scroll view's `::before` and `::after`), not
|
|
15
|
+
* by moving the content, which would change how far the browser lets the element scroll. The refresh spinner is
|
|
16
|
+
* that space's background.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
/** How far a press moves before it counts as a drag, as a finger's tap tolerates. */
|
|
20
|
+
const DRAG_SLOP = 10
|
|
21
|
+
/** The finger's speed is measured over this much of the end of a drag, for the fling. */
|
|
22
|
+
const VELOCITY_WINDOW_MS = 100
|
|
23
|
+
/** Per millisecond, how much of its speed a fling keeps, which is close to iOS's normal deceleration. */
|
|
24
|
+
const FLING_DECAY_PER_MS = 0.998
|
|
25
|
+
const FLING_MIN_SPEED = 0.02
|
|
26
|
+
/** UIScrollView's rubber band: past an end, the content moves this much of the way the finger does, less the further it goes. */
|
|
27
|
+
const RUBBER_BAND = 0.55
|
|
28
|
+
/** The spring that brings an overscrolled list back, critically damped: per millisecond, settled in about 400ms. */
|
|
29
|
+
const SPRING_OMEGA = 0.012
|
|
30
|
+
/** How far past the top a RefreshControl is pulled before it refreshes, in points the content has moved. */
|
|
31
|
+
const REFRESH_PULL = 90
|
|
32
|
+
/** The space a refreshing RefreshControl holds open above the content. */
|
|
33
|
+
const REFRESH_HEIGHT = 60
|
|
34
|
+
/** After a pull starts a refresh, how long the space stays open for the app to set `refreshing`, as React Native allows a render. */
|
|
35
|
+
const REFRESH_PENDING_MS = 300
|
|
36
|
+
|
|
37
|
+
type Drag = {
|
|
38
|
+
pointerId: number
|
|
39
|
+
target: Element
|
|
40
|
+
startX: number
|
|
41
|
+
startY: number
|
|
42
|
+
scroller: HTMLElement | null
|
|
43
|
+
anchorY: number
|
|
44
|
+
/** The unbanded position the drag started from: where the list would be with no resistance. */
|
|
45
|
+
anchorRaw: number
|
|
46
|
+
/** The top end of the drag's rubber band, fixed for the drag so a refresh starting under the finger doesn't jump. */
|
|
47
|
+
low: number
|
|
48
|
+
samples: { t: number; y: number }[]
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** What a scroll view has beyond the browser's own scrolling: the space drawn past its ends, and its RefreshControl. */
|
|
52
|
+
type Scroller = {
|
|
53
|
+
padTop: number
|
|
54
|
+
padBottom: number
|
|
55
|
+
animation: number
|
|
56
|
+
/** `pulling` shows the spinner's ticks as the list is pulled, `refreshing` spins it, `ending` hides it as the space closes. */
|
|
57
|
+
refresh: 'pulling' | 'refreshing' | 'ending'
|
|
58
|
+
/** Until when a refresh started by a pull holds its space open without `refreshing` set. */
|
|
59
|
+
pendingUntil: number
|
|
60
|
+
image: string
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
declare global {
|
|
64
|
+
interface HTMLElement {
|
|
65
|
+
/** The space drawn before and after a scroll view's content, which its `onScroll` leaves out (overrides/react-native-web). */
|
|
66
|
+
__webSimPadTop?: number
|
|
67
|
+
__webSimPadBottom?: number
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export function enableDragScroll(document: Document): () => void {
|
|
72
|
+
const view = document.defaultView
|
|
73
|
+
if (view === null) {
|
|
74
|
+
return () => undefined
|
|
75
|
+
}
|
|
76
|
+
const style = document.createElement('style')
|
|
77
|
+
style.textContent = `
|
|
78
|
+
[data-web-sim-overscroll] { overflow-anchor: none; }
|
|
79
|
+
[data-web-sim-overscroll]::before, [data-web-sim-overscroll]::after { content: ''; display: block; flex-shrink: 0; pointer-events: none; }
|
|
80
|
+
[data-web-sim-overscroll]::before { height: var(--web-sim-pad-top, 0px); background: var(--web-sim-refresh-image, none) center / auto no-repeat; }
|
|
81
|
+
[data-web-sim-overscroll]::after { height: var(--web-sim-pad-bottom, 0px); }`
|
|
82
|
+
document.head.append(style)
|
|
83
|
+
|
|
84
|
+
const scrollers = new WeakMap<HTMLElement, Scroller>()
|
|
85
|
+
const stateOf = (scroller: HTMLElement): Scroller => {
|
|
86
|
+
let state = scrollers.get(scroller)
|
|
87
|
+
if (state === undefined) {
|
|
88
|
+
state = { padTop: 0, padBottom: 0, animation: 0, refresh: 'pulling', pendingUntil: 0, image: '' }
|
|
89
|
+
scrollers.set(scroller, state)
|
|
90
|
+
}
|
|
91
|
+
return state
|
|
92
|
+
}
|
|
93
|
+
const isIos = () => document.documentElement.dataset.webSimPlatform === 'ios'
|
|
94
|
+
let drag: Drag | null = null
|
|
95
|
+
let sideways: SidewaysDrag | null = null
|
|
96
|
+
/** Lists a finger stopped mid-animation, which carry on settling if the finger lets go without dragging them. */
|
|
97
|
+
let held: HTMLElement[] = []
|
|
98
|
+
|
|
99
|
+
/** The RefreshControl around a scroll view, which react-native-web renders as its parent. */
|
|
100
|
+
const refreshControl = (scroller: HTMLElement): HTMLElement | null => {
|
|
101
|
+
const parent = scroller.parentElement
|
|
102
|
+
return isIos() && parent?.dataset.webSimRefresh === 'enabled' ? parent : null
|
|
103
|
+
}
|
|
104
|
+
const refreshHeld = (scroller: HTMLElement): boolean => {
|
|
105
|
+
const control = refreshControl(scroller)
|
|
106
|
+
return control !== null && (control.dataset.webSimRefreshing === 'true' || view.performance.now() < stateOf(scroller).pendingUntil)
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/** How far the list goes each way at rest, as iOS's content offset: below zero while a refresh holds space above it. */
|
|
110
|
+
const bounds = (scroller: HTMLElement) => {
|
|
111
|
+
const state = stateOf(scroller)
|
|
112
|
+
const content = scroller.scrollHeight - state.padTop - state.padBottom
|
|
113
|
+
return { low: refreshHeld(scroller) ? -REFRESH_HEIGHT : 0, high: Math.max(0, content - scroller.clientHeight) }
|
|
114
|
+
}
|
|
115
|
+
const bounces = (scroller: HTMLElement): boolean => {
|
|
116
|
+
if (!isIos()) {
|
|
117
|
+
return false
|
|
118
|
+
}
|
|
119
|
+
const mode = scroller.dataset.webSimBounce
|
|
120
|
+
return mode === 'content' ? bounds(scroller).high > 0 || refreshControl(scroller) !== null : mode !== 'none'
|
|
121
|
+
}
|
|
122
|
+
/** Where the list is, as iOS's content offset. */
|
|
123
|
+
const positionOf = (scroller: HTMLElement) => scroller.scrollTop - stateOf(scroller).padTop
|
|
124
|
+
|
|
125
|
+
const moveTo = (scroller: HTMLElement, y: number) => {
|
|
126
|
+
const state = stateOf(scroller)
|
|
127
|
+
const { high } = bounds(scroller)
|
|
128
|
+
const inset = refreshHeld(scroller) ? REFRESH_HEIGHT : 0
|
|
129
|
+
const padTop = Math.max(inset, -y, 0)
|
|
130
|
+
const padBottom = Math.max(0, y - high)
|
|
131
|
+
if (padTop !== state.padTop || padBottom !== state.padBottom) {
|
|
132
|
+
state.padTop = padTop
|
|
133
|
+
state.padBottom = padBottom
|
|
134
|
+
scroller.__webSimPadTop = padTop
|
|
135
|
+
scroller.__webSimPadBottom = padBottom
|
|
136
|
+
scroller.style.setProperty('--web-sim-pad-top', `${padTop}px`)
|
|
137
|
+
scroller.style.setProperty('--web-sim-pad-bottom', `${padBottom}px`)
|
|
138
|
+
}
|
|
139
|
+
if (padTop === 0 && state.refresh === 'ending') {
|
|
140
|
+
state.refresh = 'pulling'
|
|
141
|
+
}
|
|
142
|
+
showSpinner(scroller, state)
|
|
143
|
+
if (padTop > 0 || padBottom > 0) {
|
|
144
|
+
scroller.dataset.webSimOverscroll = ''
|
|
145
|
+
} else {
|
|
146
|
+
delete scroller.dataset.webSimOverscroll
|
|
147
|
+
}
|
|
148
|
+
const before = scroller.scrollTop
|
|
149
|
+
scroller.scrollTop = y + padTop
|
|
150
|
+
// Moving into the space past an end leaves scrollTop alone, so the browser sends no scroll event of its own.
|
|
151
|
+
if (scroller.scrollTop === before) {
|
|
152
|
+
scroller.dispatchEvent(new view.Event('scroll'))
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
const showSpinner = (scroller: HTMLElement, state: Scroller) => {
|
|
157
|
+
const control = refreshControl(scroller)
|
|
158
|
+
let image = ''
|
|
159
|
+
if (control !== null && state.padTop > 0 && state.refresh !== 'ending') {
|
|
160
|
+
const color = control.dataset.webSimRefreshTint || 'rgb(142, 142, 147)'
|
|
161
|
+
const title = control.dataset.webSimRefreshTitle
|
|
162
|
+
const titleColor = control.dataset.webSimRefreshTitleColor || color
|
|
163
|
+
const spinning = state.refresh === 'refreshing'
|
|
164
|
+
const ticks = spinning ? 8 : Math.ceil(8 * Math.min(1, state.padTop / REFRESH_PULL))
|
|
165
|
+
image = ticks === 0 ? '' : spinnerImage(color, ticks, spinning, title, titleColor)
|
|
166
|
+
}
|
|
167
|
+
if (image !== state.image) {
|
|
168
|
+
state.image = image
|
|
169
|
+
if (image === '') {
|
|
170
|
+
scroller.style.removeProperty('--web-sim-refresh-image')
|
|
171
|
+
} else {
|
|
172
|
+
scroller.style.setProperty('--web-sim-refresh-image', image)
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
const stop = (scroller: HTMLElement) => {
|
|
178
|
+
const state = stateOf(scroller)
|
|
179
|
+
view.cancelAnimationFrame(state.animation)
|
|
180
|
+
state.animation = 0
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* Moves a list on from where it is at a speed (points per millisecond): a fling that slows inside the content, and a
|
|
185
|
+
* spring past an end, which a fling runs into and bounces off, and which brings a list let go past an end back.
|
|
186
|
+
*/
|
|
187
|
+
const animate = (scroller: HTMLElement, speed: number, target?: number) => {
|
|
188
|
+
stop(scroller)
|
|
189
|
+
const state = stateOf(scroller)
|
|
190
|
+
let y = positionOf(scroller)
|
|
191
|
+
let v = speed
|
|
192
|
+
let previous = view.performance.now()
|
|
193
|
+
const step = (now: number) => {
|
|
194
|
+
const elapsed = Math.min(now - previous, 64)
|
|
195
|
+
previous = now
|
|
196
|
+
const { low, high } = bounds(scroller)
|
|
197
|
+
const bouncy = bounces(scroller)
|
|
198
|
+
let settled = false
|
|
199
|
+
for (let t = 0; t < elapsed && !settled; t += 1) {
|
|
200
|
+
const dt = Math.min(1, elapsed - t)
|
|
201
|
+
if (target !== undefined || y < low || y > high) {
|
|
202
|
+
const edge = target ?? (y < low ? low : high)
|
|
203
|
+
if (!bouncy && target === undefined) {
|
|
204
|
+
y = edge
|
|
205
|
+
settled = true
|
|
206
|
+
break
|
|
207
|
+
}
|
|
208
|
+
v += (-SPRING_OMEGA * SPRING_OMEGA * (y - edge) - 2 * SPRING_OMEGA * v) * dt
|
|
209
|
+
const next = y + v * dt
|
|
210
|
+
// A spring that brings the list back stops at the end, not past it.
|
|
211
|
+
if ((target === undefined && Math.sign(next - edge) !== Math.sign(y - edge)) || (Math.abs(next - edge) < 0.5 && Math.abs(v) < 0.02)) {
|
|
212
|
+
y = edge
|
|
213
|
+
settled = true
|
|
214
|
+
} else {
|
|
215
|
+
y = next
|
|
216
|
+
}
|
|
217
|
+
} else {
|
|
218
|
+
v *= FLING_DECAY_PER_MS ** dt
|
|
219
|
+
y += v * dt
|
|
220
|
+
if (!bouncy && (y < low || y > high)) {
|
|
221
|
+
y = Math.max(low, Math.min(high, y))
|
|
222
|
+
settled = true
|
|
223
|
+
} else if (y >= low && y <= high && Math.abs(v) < FLING_MIN_SPEED) {
|
|
224
|
+
settled = true
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
markUserScrolling(view)
|
|
229
|
+
moveTo(scroller, y)
|
|
230
|
+
state.animation = settled ? 0 : view.requestAnimationFrame(step)
|
|
231
|
+
}
|
|
232
|
+
state.animation = view.requestAnimationFrame(step)
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
/** Lets a list that is past its ends, or holding a refresh it no longer has, settle back. */
|
|
236
|
+
const settle = (scroller: HTMLElement) => {
|
|
237
|
+
if (drag?.scroller === scroller || stateOf(scroller).animation !== 0) {
|
|
238
|
+
return
|
|
239
|
+
}
|
|
240
|
+
const { low, high } = bounds(scroller)
|
|
241
|
+
const y = positionOf(scroller)
|
|
242
|
+
if (y < low || y > high || stateOf(scroller).padTop > Math.max(0, -y)) {
|
|
243
|
+
animate(scroller, 0)
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
const onDown = (event: PointerEvent) => {
|
|
248
|
+
if (!event.isPrimary || event.button !== 0 || !(event.target instanceof view.Element)) {
|
|
249
|
+
return
|
|
250
|
+
}
|
|
251
|
+
drag = {
|
|
252
|
+
pointerId: event.pointerId,
|
|
253
|
+
target: event.target,
|
|
254
|
+
startX: event.clientX,
|
|
255
|
+
startY: event.clientY,
|
|
256
|
+
scroller: null,
|
|
257
|
+
anchorY: 0,
|
|
258
|
+
anchorRaw: 0,
|
|
259
|
+
low: 0,
|
|
260
|
+
samples: [],
|
|
261
|
+
}
|
|
262
|
+
// A finger on a moving list stops it.
|
|
263
|
+
held = []
|
|
264
|
+
for (let element: Element | null = event.target; element !== null; element = element.parentElement) {
|
|
265
|
+
if (element instanceof view.HTMLElement && scrollers.get(element)?.animation) {
|
|
266
|
+
stop(element)
|
|
267
|
+
held.push(element)
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
/** A horizontal list taking a drag: snapping is turned off while the finger moves it, and the start remembered. */
|
|
273
|
+
const startSideways = (row: HTMLElement, from: Drag, dx: number, event: PointerEvent): SidewaysDrag => {
|
|
274
|
+
view.cancelAnimationFrame(sidewaysAnimations.get(row) ?? 0)
|
|
275
|
+
const snap = row.style.scrollSnapType
|
|
276
|
+
const paging = /mandatory|proximity/.test(view.getComputedStyle(row).scrollSnapType)
|
|
277
|
+
row.style.scrollSnapType = 'none'
|
|
278
|
+
document.getSelection()?.removeAllRanges()
|
|
279
|
+
document.documentElement.style.userSelect = 'none'
|
|
280
|
+
// A press under the finger gives up once the list it is in scrolls.
|
|
281
|
+
row.dispatchEvent(new view.Event('scroll'))
|
|
282
|
+
const state: SidewaysDrag = {
|
|
283
|
+
pointerId: from.pointerId,
|
|
284
|
+
row,
|
|
285
|
+
snap,
|
|
286
|
+
paging,
|
|
287
|
+
anchorX: from.startX + Math.sign(dx) * DRAG_SLOP,
|
|
288
|
+
anchorLeft: row.scrollLeft,
|
|
289
|
+
samples: [],
|
|
290
|
+
}
|
|
291
|
+
moveSideways(state, event)
|
|
292
|
+
return state
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
const moveSideways = (state: SidewaysDrag, event: PointerEvent) => {
|
|
296
|
+
const { row } = state
|
|
297
|
+
markUserScrolling(view)
|
|
298
|
+
row.scrollLeft = Math.max(0, Math.min(row.scrollWidth - row.clientWidth, state.anchorLeft - (event.clientX - state.anchorX)))
|
|
299
|
+
state.samples.push({ t: event.timeStamp, x: event.clientX })
|
|
300
|
+
while (state.samples.length > 2 && event.timeStamp - state.samples[0].t > VELOCITY_WINDOW_MS) {
|
|
301
|
+
state.samples.shift()
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
/** Let go, a paging list settles on the nearest page or the next one when flung; any other list glides to a stop. */
|
|
306
|
+
const endSideways = (state: SidewaysDrag, cancelled: boolean) => {
|
|
307
|
+
document.documentElement.style.userSelect = ''
|
|
308
|
+
const { row, samples, paging } = state
|
|
309
|
+
const first = samples[0]
|
|
310
|
+
const last = samples.at(-1)
|
|
311
|
+
const speed = !cancelled && first !== undefined && last !== undefined && last.t > first.t ? -(last.x - first.x) / (last.t - first.t) : 0
|
|
312
|
+
const max = row.scrollWidth - row.clientWidth
|
|
313
|
+
const start = row.scrollLeft
|
|
314
|
+
let x = start
|
|
315
|
+
let v = speed
|
|
316
|
+
const began = view.performance.now()
|
|
317
|
+
let previous = began
|
|
318
|
+
const page = row.clientWidth
|
|
319
|
+
const target = paging && page > 0
|
|
320
|
+
? Math.max(0, Math.min(max, page * (Math.abs(speed) > PAGE_FLING_SPEED ? (speed > 0 ? Math.floor(start / page) + 1 : Math.ceil(start / page) - 1) : Math.round(start / page))))
|
|
321
|
+
: null
|
|
322
|
+
const step = (now: number) => {
|
|
323
|
+
let settled: boolean
|
|
324
|
+
if (target !== null) {
|
|
325
|
+
const t = Math.min(1, (now - began) / PAGE_SETTLE_MS)
|
|
326
|
+
x = start + (target - start) * (1 - (1 - t) ** 3)
|
|
327
|
+
settled = t === 1
|
|
328
|
+
} else {
|
|
329
|
+
const elapsed = Math.min(now - previous, 64)
|
|
330
|
+
v *= FLING_DECAY_PER_MS ** elapsed
|
|
331
|
+
x = Math.max(0, Math.min(max, x + v * elapsed))
|
|
332
|
+
settled = Math.abs(v) < FLING_MIN_SPEED || x === 0 || x === max
|
|
333
|
+
}
|
|
334
|
+
previous = now
|
|
335
|
+
markUserScrolling(view)
|
|
336
|
+
row.scrollLeft = x
|
|
337
|
+
if (settled) {
|
|
338
|
+
sidewaysAnimations.delete(row)
|
|
339
|
+
row.style.scrollSnapType = state.snap
|
|
340
|
+
} else {
|
|
341
|
+
sidewaysAnimations.set(row, view.requestAnimationFrame(step))
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
sidewaysAnimations.set(row, view.requestAnimationFrame(step))
|
|
345
|
+
}
|
|
346
|
+
const sidewaysAnimations = new WeakMap<HTMLElement, number>()
|
|
347
|
+
|
|
348
|
+
const onMove = (event: PointerEvent) => {
|
|
349
|
+
if (sideways !== null && event.pointerId === sideways.pointerId) {
|
|
350
|
+
moveSideways(sideways, event)
|
|
351
|
+
return
|
|
352
|
+
}
|
|
353
|
+
if (drag === null || event.pointerId !== drag.pointerId) {
|
|
354
|
+
return
|
|
355
|
+
}
|
|
356
|
+
if (drag.scroller === null) {
|
|
357
|
+
const dx = event.clientX - drag.startX
|
|
358
|
+
const dy = event.clientY - drag.startY
|
|
359
|
+
if (Math.hypot(dx, dy) < DRAG_SLOP) {
|
|
360
|
+
return
|
|
361
|
+
}
|
|
362
|
+
if (Math.abs(dx) >= Math.abs(dy)) {
|
|
363
|
+
const row = horizontalScroller(drag.target)
|
|
364
|
+
sideways = row === null ? null : startSideways(row, drag, dx, event)
|
|
365
|
+
drag = null
|
|
366
|
+
return
|
|
367
|
+
}
|
|
368
|
+
const scroller = verticalScroller(drag.target, scrollers)
|
|
369
|
+
if (scroller === null) {
|
|
370
|
+
drag = null
|
|
371
|
+
return
|
|
372
|
+
}
|
|
373
|
+
stop(scroller)
|
|
374
|
+
const state = stateOf(scroller)
|
|
375
|
+
if (state.refresh === 'ending') {
|
|
376
|
+
state.refresh = 'pulling'
|
|
377
|
+
}
|
|
378
|
+
drag.scroller = scroller
|
|
379
|
+
// The list follows the finger from where it crossed the slop, not from this move, which can be far past it:
|
|
380
|
+
// a replayed swipe's first move is one recorded sample, often 100 pt or more from the start.
|
|
381
|
+
drag.anchorY = drag.startY + Math.sign(dy) * DRAG_SLOP
|
|
382
|
+
const { low, high } = bounds(scroller)
|
|
383
|
+
drag.low = low
|
|
384
|
+
drag.anchorRaw = unband(positionOf(scroller), low, high, scroller.clientHeight)
|
|
385
|
+
document.getSelection()?.removeAllRanges()
|
|
386
|
+
document.documentElement.style.userSelect = 'none'
|
|
387
|
+
// The list takes the touch from the press it started as, even at its top or bottom where it can't move (a
|
|
388
|
+
// phone bounces it), so letting go back over the press doesn't press. react-native-web gives up a press
|
|
389
|
+
// when a scroll view around it scrolls.
|
|
390
|
+
scroller.dispatchEvent(new view.Event('scroll'))
|
|
391
|
+
}
|
|
392
|
+
const scroller = drag.scroller
|
|
393
|
+
const { high } = bounds(scroller)
|
|
394
|
+
const raw = drag.anchorRaw - (event.clientY - drag.anchorY)
|
|
395
|
+
const y = bounces(scroller) ? band(raw, drag.low, high, scroller.clientHeight) : Math.max(drag.low, Math.min(high, raw))
|
|
396
|
+
markUserScrolling(view)
|
|
397
|
+
moveTo(scroller, y)
|
|
398
|
+
pullToRefresh(scroller, y)
|
|
399
|
+
const now = event.timeStamp
|
|
400
|
+
drag.samples.push({ t: now, y: event.clientY })
|
|
401
|
+
while (drag.samples.length > 2 && now - drag.samples[0].t > VELOCITY_WINDOW_MS) {
|
|
402
|
+
drag.samples.shift()
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
/** A RefreshControl pulled far enough refreshes while the finger is still down, as UIRefreshControl does. */
|
|
407
|
+
const pullToRefresh = (scroller: HTMLElement, y: number) => {
|
|
408
|
+
const control = refreshControl(scroller)
|
|
409
|
+
const state = stateOf(scroller)
|
|
410
|
+
if (control === null || state.refresh !== 'pulling' || control.dataset.webSimRefreshing === 'true' || -y < REFRESH_PULL) {
|
|
411
|
+
return
|
|
412
|
+
}
|
|
413
|
+
state.refresh = 'refreshing'
|
|
414
|
+
state.pendingUntil = view.performance.now() + REFRESH_PENDING_MS
|
|
415
|
+
// If the app doesn't set `refreshing`, the space closes once the wait is over.
|
|
416
|
+
view.setTimeout(() => {
|
|
417
|
+
if (control.dataset.webSimRefreshing !== 'true') {
|
|
418
|
+
endRefresh(scroller)
|
|
419
|
+
}
|
|
420
|
+
}, REFRESH_PENDING_MS + 16)
|
|
421
|
+
showSpinner(scroller, state)
|
|
422
|
+
control.dispatchEvent(new view.CustomEvent('websimrefresh'))
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
const endRefresh = (scroller: HTMLElement) => {
|
|
426
|
+
const state = stateOf(scroller)
|
|
427
|
+
state.pendingUntil = 0
|
|
428
|
+
if (state.refresh === 'refreshing') {
|
|
429
|
+
state.refresh = state.padTop > 0 ? 'ending' : 'pulling'
|
|
430
|
+
showSpinner(scroller, state)
|
|
431
|
+
}
|
|
432
|
+
settle(scroller)
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
/** The RefreshControl override says when `refreshing` changes, from a pull or from the app. */
|
|
436
|
+
const onRefreshing = (event: Event) => {
|
|
437
|
+
const control = event.target
|
|
438
|
+
const scroller = control instanceof view.HTMLElement ? control.firstElementChild : null
|
|
439
|
+
if (!(scroller instanceof view.HTMLElement) || refreshControl(scroller) === null) {
|
|
440
|
+
return
|
|
441
|
+
}
|
|
442
|
+
const state = stateOf(scroller)
|
|
443
|
+
if (control instanceof view.HTMLElement && control.dataset.webSimRefreshing === 'true') {
|
|
444
|
+
state.refresh = 'refreshing'
|
|
445
|
+
// Set by the app rather than a pull, the spinner shows if the list is at its top.
|
|
446
|
+
if (drag?.scroller !== scroller && state.animation === 0 && positionOf(scroller) <= 0) {
|
|
447
|
+
animate(scroller, 0, -REFRESH_HEIGHT)
|
|
448
|
+
} else {
|
|
449
|
+
showSpinner(scroller, state)
|
|
450
|
+
}
|
|
451
|
+
} else {
|
|
452
|
+
endRefresh(scroller)
|
|
453
|
+
}
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
const onUp = (event: PointerEvent) => {
|
|
457
|
+
if (sideways !== null && event.pointerId === sideways.pointerId) {
|
|
458
|
+
endSideways(sideways, event.type !== 'pointerup')
|
|
459
|
+
sideways = null
|
|
460
|
+
return
|
|
461
|
+
}
|
|
462
|
+
const stopped = held
|
|
463
|
+
held = []
|
|
464
|
+
for (const scroller of stopped) {
|
|
465
|
+
if (scroller !== drag?.scroller) {
|
|
466
|
+
settle(scroller)
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
if (drag === null || event.pointerId !== drag.pointerId) {
|
|
470
|
+
return
|
|
471
|
+
}
|
|
472
|
+
const { scroller, samples } = drag
|
|
473
|
+
drag = null
|
|
474
|
+
if (scroller === null) {
|
|
475
|
+
return
|
|
476
|
+
}
|
|
477
|
+
document.documentElement.style.userSelect = ''
|
|
478
|
+
const first = samples[0]
|
|
479
|
+
const last = samples.at(-1)
|
|
480
|
+
const { low, high } = bounds(scroller)
|
|
481
|
+
const y = positionOf(scroller)
|
|
482
|
+
if (y < low || y > high) {
|
|
483
|
+
// Let go past an end, the list springs back.
|
|
484
|
+
animate(scroller, 0)
|
|
485
|
+
} else if (event.type === 'pointerup' && first !== undefined && last !== undefined && last.t > first.t) {
|
|
486
|
+
animate(scroller, -(last.y - first.y) / (last.t - first.t))
|
|
487
|
+
}
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
// Dragging an image starts the browser's own drag and drop, which cancels the pointer partway through.
|
|
491
|
+
const onDragStart = (event: DragEvent) => event.preventDefault()
|
|
492
|
+
|
|
493
|
+
const onWheel = () => markUserScrolling(view)
|
|
494
|
+
|
|
495
|
+
const options = { capture: true, passive: true }
|
|
496
|
+
document.addEventListener('pointerdown', onDown, options)
|
|
497
|
+
document.addEventListener('pointermove', onMove, options)
|
|
498
|
+
document.addEventListener('pointerup', onUp, options)
|
|
499
|
+
document.addEventListener('pointercancel', onUp, options)
|
|
500
|
+
document.addEventListener('dragstart', onDragStart, { capture: true })
|
|
501
|
+
document.addEventListener('wheel', onWheel, options)
|
|
502
|
+
document.addEventListener('websimrefreshing', onRefreshing, { capture: true })
|
|
503
|
+
return () => {
|
|
504
|
+
document.removeEventListener('wheel', onWheel, options)
|
|
505
|
+
document.removeEventListener('pointerdown', onDown, options)
|
|
506
|
+
document.removeEventListener('pointermove', onMove, options)
|
|
507
|
+
document.removeEventListener('pointerup', onUp, options)
|
|
508
|
+
document.removeEventListener('pointercancel', onUp, options)
|
|
509
|
+
document.removeEventListener('dragstart', onDragStart, { capture: true })
|
|
510
|
+
document.removeEventListener('websimrefreshing', onRefreshing, { capture: true })
|
|
511
|
+
style.remove()
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
/** UIScrollView's resistance: `distance` past an end moves the content this far, never as far as `size`. */
|
|
516
|
+
function rubberBand(distance: number, size: number): number {
|
|
517
|
+
return (1 - 1 / ((distance * RUBBER_BAND) / size + 1)) * size
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
function unrubberBand(moved: number, size: number): number {
|
|
521
|
+
return (size / RUBBER_BAND) * (1 / (1 - Math.min(moved, size * 0.99) / size) - 1)
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
type SidewaysDrag = {
|
|
525
|
+
pointerId: number
|
|
526
|
+
row: HTMLElement
|
|
527
|
+
/** The list's own `scroll-snap-type`, put back once it settles. */
|
|
528
|
+
snap: string
|
|
529
|
+
paging: boolean
|
|
530
|
+
anchorX: number
|
|
531
|
+
anchorLeft: number
|
|
532
|
+
samples: { t: number; x: number }[]
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
/** A release this fast (points per millisecond) turns a paging list's page even short of halfway, as UIScrollView does. */
|
|
536
|
+
const PAGE_FLING_SPEED = 0.3
|
|
537
|
+
const PAGE_SETTLE_MS = 300
|
|
538
|
+
|
|
539
|
+
/** The horizontal list a sideways drag moves: the nearest one with more to show across. */
|
|
540
|
+
function horizontalScroller(start: Element): HTMLElement | null {
|
|
541
|
+
const view = start.ownerDocument.defaultView
|
|
542
|
+
for (let element: Element | null = start; element !== null && view !== null; element = element.parentElement) {
|
|
543
|
+
if (element instanceof view.HTMLElement && /(auto|scroll)/.test(view.getComputedStyle(element).overflowX) && element.scrollWidth > element.clientWidth) {
|
|
544
|
+
return element
|
|
545
|
+
}
|
|
546
|
+
}
|
|
547
|
+
return null
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
/** Where a list is with the finger at `raw`, past an end by less than the finger is. */
|
|
551
|
+
function band(raw: number, low: number, high: number, size: number): number {
|
|
552
|
+
if (raw < low) {
|
|
553
|
+
return low - rubberBand(low - raw, size)
|
|
554
|
+
}
|
|
555
|
+
if (raw > high) {
|
|
556
|
+
return high + rubberBand(raw - high, size)
|
|
557
|
+
}
|
|
558
|
+
return raw
|
|
559
|
+
}
|
|
560
|
+
|
|
561
|
+
function unband(y: number, low: number, high: number, size: number): number {
|
|
562
|
+
if (y < low) {
|
|
563
|
+
return low - unrubberBand(low - y, size)
|
|
564
|
+
}
|
|
565
|
+
if (y > high) {
|
|
566
|
+
return high + unrubberBand(y - high, size)
|
|
567
|
+
}
|
|
568
|
+
return y
|
|
569
|
+
}
|
|
570
|
+
|
|
571
|
+
/**
|
|
572
|
+
* The list a vertical drag moves: the nearest one with more to show, or else the nearest scroll view at all, which
|
|
573
|
+
* on a phone bounces under the finger even when its content fits.
|
|
574
|
+
*/
|
|
575
|
+
function verticalScroller(start: Element, scrollers: WeakMap<HTMLElement, Scroller>): HTMLElement | null {
|
|
576
|
+
const view = start.ownerDocument.defaultView
|
|
577
|
+
let fits: HTMLElement | null = null
|
|
578
|
+
for (let element: Element | null = start; element !== null && view !== null; element = element.parentElement) {
|
|
579
|
+
if (element instanceof view.HTMLElement && /(auto|scroll)/.test(view.getComputedStyle(element).overflowY)) {
|
|
580
|
+
const state = scrollers.get(element)
|
|
581
|
+
const pads = state === undefined ? 0 : state.padTop + state.padBottom
|
|
582
|
+
if (element.scrollHeight - pads > element.clientHeight) {
|
|
583
|
+
return element
|
|
584
|
+
}
|
|
585
|
+
fits ??= element
|
|
586
|
+
}
|
|
587
|
+
}
|
|
588
|
+
return fits
|
|
589
|
+
}
|
|
590
|
+
|
|
591
|
+
/**
|
|
592
|
+
* iOS's activity indicator as an image: eight ticks round a circle, drawn one by one from the top as a list is
|
|
593
|
+
* pulled, then turning.
|
|
594
|
+
*/
|
|
595
|
+
function spinnerImage(color: string, ticks: number, spinning: boolean, title: string | undefined, titleColor: string): string {
|
|
596
|
+
const escape = (text: string) => text.replace(/[&<>"']/g, (c) => `&#${c.charCodeAt(0)};`)
|
|
597
|
+
const lines = Array.from({ length: ticks }, (_, i) => {
|
|
598
|
+
const opacity = spinning ? 1 - i * 0.11 : 1
|
|
599
|
+
// Spinning, the brightest tick leads clockwise and the rest trail behind it.
|
|
600
|
+
const angle = spinning ? -i * 45 : i * 45
|
|
601
|
+
return `<line x1='14' y1='5' x2='14' y2='9.5' stroke='${escape(color)}' stroke-width='2.4' stroke-linecap='round' opacity='${opacity.toFixed(2)}' transform='rotate(${angle} 14 14)'/>`
|
|
602
|
+
}).join('')
|
|
603
|
+
const turn = spinning
|
|
604
|
+
? `<animateTransform attributeName='transform' type='rotate' calcMode='discrete' dur='0.8s' repeatCount='indefinite' values='${Array.from({ length: 8 }, (_, i) => `${i * 45} 14 14`).join(';')}'/>`
|
|
605
|
+
: ''
|
|
606
|
+
const height = title ? 50 : 28
|
|
607
|
+
const width = title ? 240 : 28
|
|
608
|
+
const label = title
|
|
609
|
+
? `<text x='${width / 2}' y='45' text-anchor='middle' font-family='-apple-system, system-ui, sans-serif' font-size='13' fill='${escape(titleColor)}'>${escape(title)}</text>`
|
|
610
|
+
: ''
|
|
611
|
+
const svg = `<svg xmlns='http://www.w3.org/2000/svg' width='${width}' height='${height}'><g transform='translate(${width / 2 - 14} 0)'><g>${lines}${turn}</g></g>${label}</svg>`
|
|
612
|
+
return `url("data:image/svg+xml,${encodeURIComponent(svg)}")`
|
|
613
|
+
}
|
|
614
|
+
|
|
615
|
+
/** How long a mark lasts, which covers the scroll events a browser sends just after the last movement. */
|
|
616
|
+
const MARK_MS = 250
|
|
617
|
+
|
|
618
|
+
declare global {
|
|
619
|
+
interface Window {
|
|
620
|
+
__webSimUserScrollingUntil?: number
|
|
621
|
+
}
|
|
622
|
+
}
|
|
623
|
+
|
|
624
|
+
/**
|
|
625
|
+
* Marks the window while the user scrolls, by dragging, flinging, or with a wheel, so code that must tell a user's
|
|
626
|
+
* scroll from the app scrolling itself can.
|
|
627
|
+
*/
|
|
628
|
+
function markUserScrolling(view: Window): void {
|
|
629
|
+
view.__webSimUserScrollingUntil = view.performance.now() + MARK_MS
|
|
630
|
+
}
|
|
631
|
+
|
|
632
|
+
/**
|
|
633
|
+
* The pointer over the screen is a fingertip: a soft circle, as simulators draw a touch.
|
|
634
|
+
*/
|
|
635
|
+
export function showFingerCursor(document: Document): void {
|
|
636
|
+
const svg = `<svg xmlns='http://www.w3.org/2000/svg' width='28' height='28'><circle cx='14' cy='14' r='11' fill='rgba(128,128,128,0.35)' stroke='rgba(255,255,255,0.8)' stroke-width='1.5'/></svg>`
|
|
637
|
+
const style = document.createElement('style')
|
|
638
|
+
style.textContent = `html, html * { cursor: url("data:image/svg+xml,${encodeURIComponent(svg)}") 14 14, pointer !important; }`
|
|
639
|
+
document.head.append(style)
|
|
640
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The shell and the app load the same page. Framed, it is the app; on its own, the shell that frames it. `?app`
|
|
3
|
+
* opens the app alone, on the default device.
|
|
4
|
+
*/
|
|
5
|
+
const isApp = window.self !== window.top || new URLSearchParams(window.location.search).has('app')
|
|
6
|
+
|
|
7
|
+
if (isApp) {
|
|
8
|
+
// What fails before the app's own crash screen exists (a plugin's app module, say) still shows, not a blank screen.
|
|
9
|
+
import('./app/main.tsx').catch((error: unknown) => {
|
|
10
|
+
const screen = document.createElement('pre')
|
|
11
|
+
screen.id = 'web-sim-crash'
|
|
12
|
+
screen.style.cssText =
|
|
13
|
+
'position:fixed;inset:0;margin:0;padding:72px 16px 16px;overflow:auto;font-size:11px;white-space:pre-wrap;background:#b3261e;color:white'
|
|
14
|
+
screen.textContent = `The app failed to load.\n\n${error instanceof Error ? `${error.message}\n\n${error.stack ?? ''}` : String(error)}`
|
|
15
|
+
document.body.append(screen)
|
|
16
|
+
throw error
|
|
17
|
+
})
|
|
18
|
+
} else {
|
|
19
|
+
void import('./shell/main.tsx')
|
|
20
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
declare module 'react-native-web'
|
|
2
|
+
declare module 'expo-router' {
|
|
3
|
+
export const ExpoRoot: React.ComponentType<{ context: unknown }>
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
declare module 'react-native-web/dist/exports/StyleSheet/compiler/hash' {
|
|
7
|
+
export default function hash(text: string): string
|
|
8
|
+
}
|