@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,122 @@
|
|
|
1
|
+
import { ClipboardTextIcon, TrashIcon } from '@phosphor-icons/react'
|
|
2
|
+
import { Button, CodeBlock, defineShellPlugin, Field, IconButton, type PluginContext, Row, TextField, ImagePreview, Toggle } from 'web-sim/shell'
|
|
3
|
+
import { type ClipboardContent, type ClipboardState, contentTypes, imageSize, INITIAL_CLIPBOARD, write } from './state.ts'
|
|
4
|
+
|
|
5
|
+
function blobToDataUri(blob: Blob): Promise<string> {
|
|
6
|
+
return new Promise((resolve, reject) => {
|
|
7
|
+
const reader = new FileReader()
|
|
8
|
+
reader.onload = () => resolve(reader.result as string)
|
|
9
|
+
reader.onerror = () => reject(reader.error)
|
|
10
|
+
reader.readAsDataURL(blob)
|
|
11
|
+
})
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/** What is on the computer's clipboard. Reading it needs the user's click, which the button gives. */
|
|
15
|
+
async function readComputer(): Promise<Partial<ClipboardContent>> {
|
|
16
|
+
try {
|
|
17
|
+
const items = await navigator.clipboard.read()
|
|
18
|
+
const content: Partial<ClipboardContent> = {}
|
|
19
|
+
for (const item of items) {
|
|
20
|
+
const imageType = item.types.find((type) => type.startsWith('image/'))
|
|
21
|
+
if (imageType !== undefined && content.image === undefined) {
|
|
22
|
+
const data = await blobToDataUri(await item.getType(imageType))
|
|
23
|
+
content.image = { data, ...(await imageSize(data)) }
|
|
24
|
+
}
|
|
25
|
+
if (item.types.includes('text/plain') && content.text === undefined) {
|
|
26
|
+
content.text = await (await item.getType('text/plain')).text()
|
|
27
|
+
}
|
|
28
|
+
if (item.types.includes('text/html') && content.html === undefined) {
|
|
29
|
+
content.html = await (await item.getType('text/html')).text()
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
return content
|
|
33
|
+
} catch {
|
|
34
|
+
return { text: await navigator.clipboard.readText() }
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
async function writeComputer(state: ClipboardState): Promise<void> {
|
|
39
|
+
if (state.image !== null) {
|
|
40
|
+
const png = await (await fetch(state.image.data)).blob()
|
|
41
|
+
const blob = png.type === 'image/png' ? png : await toPng(state.image.data)
|
|
42
|
+
await navigator.clipboard.write([new ClipboardItem({ 'image/png': blob })])
|
|
43
|
+
return
|
|
44
|
+
}
|
|
45
|
+
const text = state.text ?? state.url ?? ''
|
|
46
|
+
if (state.html !== null) {
|
|
47
|
+
await navigator.clipboard.write([
|
|
48
|
+
new ClipboardItem({ 'text/plain': new Blob([text], { type: 'text/plain' }), 'text/html': new Blob([state.html], { type: 'text/html' }) }),
|
|
49
|
+
])
|
|
50
|
+
} else {
|
|
51
|
+
await navigator.clipboard.writeText(text)
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
async function toPng(data: string): Promise<Blob> {
|
|
56
|
+
const image = new Image()
|
|
57
|
+
image.src = data
|
|
58
|
+
await image.decode()
|
|
59
|
+
const canvas = document.createElement('canvas')
|
|
60
|
+
canvas.width = image.naturalWidth
|
|
61
|
+
canvas.height = image.naturalHeight
|
|
62
|
+
canvas.getContext('2d')!.drawImage(image, 0, 0)
|
|
63
|
+
return new Promise((resolve) => canvas.toBlob((blob) => resolve(blob!), 'image/png'))
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** Puts content on the phone's pasteboard as the user would from outside the app. */
|
|
67
|
+
function copy(context: PluginContext<ClipboardState>, content: Partial<ClipboardContent>) {
|
|
68
|
+
context.set(write(context.state(), content, 'outside'))
|
|
69
|
+
return context.state()
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export default defineShellPlugin<ClipboardState>({
|
|
73
|
+
id: 'clipboard',
|
|
74
|
+
title: 'Clipboard',
|
|
75
|
+
icon: ClipboardTextIcon,
|
|
76
|
+
initialState: INITIAL_CLIPBOARD,
|
|
77
|
+
actions: {
|
|
78
|
+
/** `{text?, html?, url?, image?: {data, width, height}}`: what the user copies in another app. */
|
|
79
|
+
copy: (context, args) => copy(context, (args ?? {}) as Partial<ClipboardContent>),
|
|
80
|
+
clear: (context) => copy(context, {}),
|
|
81
|
+
pasteFromComputer: async (context) => copy(context, await readComputer()),
|
|
82
|
+
copyToComputer: async (context) => {
|
|
83
|
+
await writeComputer(context.state())
|
|
84
|
+
return context.state()
|
|
85
|
+
},
|
|
86
|
+
},
|
|
87
|
+
Panel: ({ state, set, run, sim }) => {
|
|
88
|
+
const replace = (content: Partial<ClipboardContent>) => set(write(state, content, 'outside'))
|
|
89
|
+
return (
|
|
90
|
+
<>
|
|
91
|
+
<Field label="Copied by" value={contentTypes(state).length === 0 ? '—' : state.source === 'app' ? 'The app' : 'The user'} />
|
|
92
|
+
<Field label="Text">
|
|
93
|
+
<TextField label="Clipboard text" multiline value={state.text ?? ''} onChange={(text) => replace({ text: text === '' ? null : text, url: state.url })} />
|
|
94
|
+
</Field>
|
|
95
|
+
{sim.device.platform === 'ios' && (
|
|
96
|
+
<Field label="URL">
|
|
97
|
+
<TextField label="Clipboard URL" value={state.url ?? ''} onChange={(url) => replace({ url: url === '' ? null : url, text: state.text })} />
|
|
98
|
+
</Field>
|
|
99
|
+
)}
|
|
100
|
+
{state.html !== null && (
|
|
101
|
+
<Field label="HTML">
|
|
102
|
+
<CodeBlock>{state.html}</CodeBlock>
|
|
103
|
+
</Field>
|
|
104
|
+
)}
|
|
105
|
+
{state.image !== null && (
|
|
106
|
+
<Field label="Image" value={`${state.image.width} × ${state.image.height}`}>
|
|
107
|
+
<Row>
|
|
108
|
+
<ImagePreview src={state.image.data} label="Clipboard image" />
|
|
109
|
+
<IconButton icon={TrashIcon} title="Remove image" onClick={() => replace({ text: state.text })} />
|
|
110
|
+
</Row>
|
|
111
|
+
</Field>
|
|
112
|
+
)}
|
|
113
|
+
<Row>
|
|
114
|
+
<Button onClick={() => void run('pasteFromComputer')}>Paste from computer</Button>
|
|
115
|
+
<Button onClick={() => void run('copyToComputer')}>Copy to computer</Button>
|
|
116
|
+
<Button onClick={() => replace({})}>Clear</Button>
|
|
117
|
+
</Row>
|
|
118
|
+
{sim.device.platform === 'ios' && <Toggle label="Ask before pasting" checked={state.pastePrompt} onChange={(pastePrompt) => set({ pastePrompt })} />}
|
|
119
|
+
</>
|
|
120
|
+
)
|
|
121
|
+
},
|
|
122
|
+
})
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The phone's own pasteboard, apart from the computer's. `expo-clipboard` reads and writes it; the panel shows and
|
|
3
|
+
* edits it, and moves it to and from the computer's clipboard.
|
|
4
|
+
*/
|
|
5
|
+
export type ClipboardState = {
|
|
6
|
+
text: string | null
|
|
7
|
+
/** Set with the text when the app copies HTML. */
|
|
8
|
+
html: string | null
|
|
9
|
+
/** A data URI. */
|
|
10
|
+
image: { data: string; width: number; height: number } | null
|
|
11
|
+
/** iOS's URL item. */
|
|
12
|
+
url: string | null
|
|
13
|
+
/** Who put the contents there: the app itself, or the user from outside it (the panel, the computer). */
|
|
14
|
+
source: 'app' | 'outside'
|
|
15
|
+
/** iOS: a read of what came from outside the app asks "Allow Paste" first. */
|
|
16
|
+
pastePrompt: boolean
|
|
17
|
+
/** The user allowed the app to paste the current contents. */
|
|
18
|
+
pasteAllowed: boolean
|
|
19
|
+
/** Bumped on every write, as `UIPasteboard.changeCount`, so copying the same text again is a change too. */
|
|
20
|
+
changeCount: number
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export const INITIAL_CLIPBOARD: ClipboardState = {
|
|
24
|
+
text: null,
|
|
25
|
+
html: null,
|
|
26
|
+
image: null,
|
|
27
|
+
url: null,
|
|
28
|
+
source: 'outside',
|
|
29
|
+
pastePrompt: false,
|
|
30
|
+
pasteAllowed: false,
|
|
31
|
+
changeCount: 0,
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export type ClipboardContent = Pick<ClipboardState, 'text' | 'html' | 'image' | 'url'>
|
|
35
|
+
|
|
36
|
+
/** A write replaces the whole pasteboard, as on the devices. */
|
|
37
|
+
export function write(state: ClipboardState, content: Partial<ClipboardContent>, source: ClipboardState['source']): Partial<ClipboardState> {
|
|
38
|
+
return {
|
|
39
|
+
text: null,
|
|
40
|
+
html: null,
|
|
41
|
+
image: null,
|
|
42
|
+
url: null,
|
|
43
|
+
...content,
|
|
44
|
+
source,
|
|
45
|
+
pasteAllowed: source === 'app',
|
|
46
|
+
changeCount: state.changeCount + 1,
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export type ContentType = 'plain-text' | 'html' | 'image' | 'url'
|
|
51
|
+
|
|
52
|
+
export function contentTypes(state: ClipboardState): ContentType[] {
|
|
53
|
+
return [
|
|
54
|
+
...(state.text !== null && state.text !== '' ? (['plain-text'] as const) : []),
|
|
55
|
+
...(state.html !== null ? (['html'] as const) : []),
|
|
56
|
+
...(state.image !== null ? (['image'] as const) : []),
|
|
57
|
+
...(state.url !== null ? (['url'] as const) : []),
|
|
58
|
+
]
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export function contentChanged(a: ClipboardState, b: ClipboardState): boolean {
|
|
62
|
+
return a.changeCount !== b.changeCount || a.text !== b.text || a.html !== b.html || a.url !== b.url || a.image?.data !== b.image?.data
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export function isUrl(text: string | null): boolean {
|
|
66
|
+
if (text === null) {
|
|
67
|
+
return false
|
|
68
|
+
}
|
|
69
|
+
try {
|
|
70
|
+
return /^[a-z][a-z0-9+.-]*:\S+$/i.test(text) && new URL(text).href !== ''
|
|
71
|
+
} catch {
|
|
72
|
+
return false
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** A data URI's size in pixels. */
|
|
77
|
+
export function imageSize(data: string): Promise<{ width: number; height: number }> {
|
|
78
|
+
return new Promise((resolve, reject) => {
|
|
79
|
+
const image = new Image()
|
|
80
|
+
image.onload = () => resolve({ width: image.naturalWidth, height: image.naturalHeight })
|
|
81
|
+
image.onerror = () => reject(new Error('Not an image'))
|
|
82
|
+
image.src = data
|
|
83
|
+
})
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** Base64 with or without its `data:` prefix, as a data URI. */
|
|
87
|
+
export function toDataUri(base64: string): string {
|
|
88
|
+
if (base64.startsWith('data:')) {
|
|
89
|
+
return base64
|
|
90
|
+
}
|
|
91
|
+
const mime = base64.startsWith('/9j/') ? 'image/jpeg' : base64.startsWith('R0lGOD') ? 'image/gif' : base64.startsWith('UklGR') ? 'image/webp' : 'image/png'
|
|
92
|
+
return `data:${mime};base64,${base64}`
|
|
93
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { phoneNow } from 'web-sim/runtime'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The app's window reads the phone's clock: `Date.now()`, `new Date()` and `Date()` give the time the clock panel
|
|
5
|
+
* set, frozen or running at its speed. Timers, `performance.now` and animation frames keep real time, as they do
|
|
6
|
+
* on a phone whose clock is changed.
|
|
7
|
+
*
|
|
8
|
+
* This wraps whatever `Date` is in place, and the locale plugin wraps whatever it finds, so the phone's time zone and
|
|
9
|
+
* its clock apply together whichever loads first.
|
|
10
|
+
*/
|
|
11
|
+
const Inner = window.Date
|
|
12
|
+
const realNow = Inner.now
|
|
13
|
+
window.__webSimRealNow = () => realNow.call(Inner)
|
|
14
|
+
|
|
15
|
+
// Through the locale plugin's Proxy, if it's in place, onto the real Date.
|
|
16
|
+
Inner.now = () => phoneNow()
|
|
17
|
+
|
|
18
|
+
const ClockDate: DateConstructor = new Proxy(Inner, {
|
|
19
|
+
construct: (target, args, newTarget) => Reflect.construct(target, args.length === 0 ? [phoneNow()] : args, newTarget) as Date,
|
|
20
|
+
// `Date()` called as a function: the time now, as a string, in the phone's zone when the locale plugin sets one.
|
|
21
|
+
apply: () => Inner.prototype.toString.call(new Inner(phoneNow())),
|
|
22
|
+
})
|
|
23
|
+
|
|
24
|
+
window.Date = ClockDate
|
|
25
|
+
Object.defineProperty(Inner.prototype, 'constructor', { value: ClockDate, writable: true, configurable: true, enumerable: false })
|
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
import { ClockIcon } from '@phosphor-icons/react'
|
|
2
|
+
import { useEffect, useReducer } from 'react'
|
|
3
|
+
import {
|
|
4
|
+
clockTime,
|
|
5
|
+
fromWallTime,
|
|
6
|
+
phoneLanguage,
|
|
7
|
+
phoneTimeZone,
|
|
8
|
+
phoneUses24HourClock,
|
|
9
|
+
REAL_CLOCK,
|
|
10
|
+
wallTimeInput,
|
|
11
|
+
} from 'web-sim/runtime'
|
|
12
|
+
import { Button, defineShellPlugin, Field, Readout, Row, Select, TextField, type PluginContext } from 'web-sim/shell'
|
|
13
|
+
import { type ClockState, INITIAL_CLOCK, RATES } from './state.ts'
|
|
14
|
+
|
|
15
|
+
type Context = PluginContext<ClockState>
|
|
16
|
+
|
|
17
|
+
const UNITS = { ms: 1, seconds: 1000, minutes: 60_000, hours: 3_600_000, days: 86_400_000 } as const
|
|
18
|
+
|
|
19
|
+
/** A wall-clock time with no offset: `2026-12-25`, `2026-12-25T08:00`, `2026-12-25 08:00:30.5`. */
|
|
20
|
+
const WALL_TIME = /^\s*(\d{4,6})-(\d{2})-(\d{2})(?:[T ](\d{2}):(\d{2})(?::(\d{2})(?:\.(\d{1,3}))?)?)?\s*$/
|
|
21
|
+
|
|
22
|
+
/** A time as `set` takes it: ms since the epoch, an ISO string with an offset, or one without, read in the phone's zone. */
|
|
23
|
+
export function parseTime(time: unknown, timeZone: string): number {
|
|
24
|
+
if (typeof time === 'number' && Number.isFinite(time)) {
|
|
25
|
+
return time
|
|
26
|
+
}
|
|
27
|
+
if (typeof time === 'string') {
|
|
28
|
+
const wall = WALL_TIME.exec(time)
|
|
29
|
+
if (wall !== null) {
|
|
30
|
+
const [, year, month, day, hours = '0', minutes = '0', seconds = '0', fraction = '0'] = wall
|
|
31
|
+
const ms = Number(fraction.padEnd(3, '0'))
|
|
32
|
+
return fromWallTime(timeZone, Date.UTC(Number(year), Number(month) - 1, Number(day), Number(hours), Number(minutes), Number(seconds), ms))
|
|
33
|
+
}
|
|
34
|
+
const parsed = Date.parse(time)
|
|
35
|
+
if (!Number.isNaN(parsed)) {
|
|
36
|
+
return parsed
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
throw new Error(`Can't read the time ${JSON.stringify(time)}: give ms since the epoch or an ISO string, such as "2026-12-25T08:00"`)
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** `advance`'s amount: `{hours: 1}`, `{days: 1, minutes: 30}`, or a number of ms. */
|
|
43
|
+
function amount(args: unknown): number {
|
|
44
|
+
if (typeof args === 'number') {
|
|
45
|
+
return args
|
|
46
|
+
}
|
|
47
|
+
const given = (args ?? {}) as Record<string, unknown>
|
|
48
|
+
let total = 0
|
|
49
|
+
for (const [unit, size] of Object.entries(UNITS)) {
|
|
50
|
+
const value = given[unit]
|
|
51
|
+
if (value !== undefined) {
|
|
52
|
+
if (typeof value !== 'number' || !Number.isFinite(value)) {
|
|
53
|
+
throw new Error(`advance: ${unit} must be a number`)
|
|
54
|
+
}
|
|
55
|
+
total += value * size
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
if (Object.keys(given).some((key) => !(key in UNITS))) {
|
|
59
|
+
throw new Error(`advance takes ${Object.keys(UNITS).join(', ')}`)
|
|
60
|
+
}
|
|
61
|
+
return total
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** Moves the clock to `time`, keeping its speed and whether it's frozen. */
|
|
65
|
+
function moveTo(context: Context, time: number) {
|
|
66
|
+
context.set({ anchorReal: Date.now(), anchorPhone: time })
|
|
67
|
+
return report(context)
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function report(context: Context) {
|
|
71
|
+
const state = context.state()
|
|
72
|
+
return { time: new Date(clockTime(state, Date.now())).toISOString(), frozen: state.frozen, rate: state.rate }
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function isReal(state: ClockState): boolean {
|
|
76
|
+
return !state.frozen && state.rate === 1 && state.anchorPhone === state.anchorReal
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function formatted(time: number, options: Intl.DateTimeFormatOptions): string {
|
|
80
|
+
try {
|
|
81
|
+
return new Intl.DateTimeFormat(phoneLanguage(), { timeZone: phoneTimeZone(), ...options }).format(time)
|
|
82
|
+
} catch {
|
|
83
|
+
return new Date(time).toISOString()
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** How far the phone's clock is from the real time: `2 d 3 h ahead`. */
|
|
88
|
+
function drift(ms: number): string {
|
|
89
|
+
const total = Math.round(Math.abs(ms) / 1000)
|
|
90
|
+
if (total < 1) {
|
|
91
|
+
return 'In step'
|
|
92
|
+
}
|
|
93
|
+
const parts: [number, string][] = [
|
|
94
|
+
[Math.floor(total / 86_400), 'd'],
|
|
95
|
+
[Math.floor((total % 86_400) / 3600), 'h'],
|
|
96
|
+
[Math.floor((total % 3600) / 60), 'min'],
|
|
97
|
+
[total % 60, 's'],
|
|
98
|
+
]
|
|
99
|
+
const shown = parts.filter(([value]) => value > 0).slice(0, 2)
|
|
100
|
+
return `${shown.map(([value, unit]) => `${value} ${unit}`).join(' ')} ${ms > 0 ? 'ahead' : 'behind'}`
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/** Re-renders while the clock runs: often enough for its seconds. */
|
|
104
|
+
function useTick(running: boolean) {
|
|
105
|
+
const [, tick] = useReducer((count: number) => count + 1, 0)
|
|
106
|
+
useEffect(() => {
|
|
107
|
+
if (!running) {
|
|
108
|
+
return
|
|
109
|
+
}
|
|
110
|
+
const timer = setInterval(tick, 250)
|
|
111
|
+
return () => clearInterval(timer)
|
|
112
|
+
}, [running])
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function rateLabel(rate: number): string {
|
|
116
|
+
return rate === 1 ? 'Real speed (1×)' : `${rate}×`
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
export default defineShellPlugin<ClockState>({
|
|
120
|
+
id: 'clock',
|
|
121
|
+
title: 'Clock',
|
|
122
|
+
icon: ClockIcon,
|
|
123
|
+
initialState: INITIAL_CLOCK,
|
|
124
|
+
actions: {
|
|
125
|
+
/** Sets the time: `{time}` as ms or an ISO string; without an offset, it's read in the phone's time zone. */
|
|
126
|
+
set: (context, args) => {
|
|
127
|
+
const time = typeof args === 'object' && args !== null ? (args as { time?: unknown }).time : args
|
|
128
|
+
return moveTo(context, parseTime(time, phoneTimeZone()))
|
|
129
|
+
},
|
|
130
|
+
/** Moves the clock on (or back, with negative amounts): `{ms?, seconds?, minutes?, hours?, days?}`. */
|
|
131
|
+
advance: (context, args) => moveTo(context, clockTime(context.state(), Date.now()) + amount(args)),
|
|
132
|
+
freeze: (context) => {
|
|
133
|
+
const state = context.state()
|
|
134
|
+
if (!state.frozen) {
|
|
135
|
+
const real = Date.now()
|
|
136
|
+
context.set({ frozen: true, anchorReal: real, anchorPhone: clockTime(state, real) })
|
|
137
|
+
}
|
|
138
|
+
return report(context)
|
|
139
|
+
},
|
|
140
|
+
resume: (context) => {
|
|
141
|
+
if (context.state().frozen) {
|
|
142
|
+
context.set({ frozen: false, anchorReal: Date.now() })
|
|
143
|
+
}
|
|
144
|
+
return report(context)
|
|
145
|
+
},
|
|
146
|
+
/** Runs the clock at `{rate}` phone seconds per real second. */
|
|
147
|
+
rate: (context, args) => {
|
|
148
|
+
const rate = typeof args === 'number' ? args : (args as { rate?: unknown } | null)?.rate
|
|
149
|
+
if (typeof rate !== 'number' || !Number.isFinite(rate) || rate <= 0) {
|
|
150
|
+
throw new Error('rate takes {rate}: a number above 0')
|
|
151
|
+
}
|
|
152
|
+
const state = context.state()
|
|
153
|
+
const real = Date.now()
|
|
154
|
+
context.set({ rate, anchorReal: real, anchorPhone: clockTime(state, real) })
|
|
155
|
+
return report(context)
|
|
156
|
+
},
|
|
157
|
+
/** Back to the real time, running at real speed. */
|
|
158
|
+
reset: (context) => {
|
|
159
|
+
context.set({ ...REAL_CLOCK })
|
|
160
|
+
return report(context)
|
|
161
|
+
},
|
|
162
|
+
},
|
|
163
|
+
/**
|
|
164
|
+
* `web-sim set clock frozen=true` or `rate=60` changes the clock from now, as the actions do: the anchor moves to
|
|
165
|
+
* the time it read at the change, so the time doesn't jump.
|
|
166
|
+
*/
|
|
167
|
+
setup: (context) => {
|
|
168
|
+
let previous = context.state()
|
|
169
|
+
return context.subscribe(() => {
|
|
170
|
+
const next = context.state()
|
|
171
|
+
const last = previous
|
|
172
|
+
previous = next
|
|
173
|
+
const anchored = next.anchorReal !== last.anchorReal || next.anchorPhone !== last.anchorPhone
|
|
174
|
+
if (!anchored && (next.frozen !== last.frozen || next.rate !== last.rate)) {
|
|
175
|
+
const real = Date.now()
|
|
176
|
+
context.set({ anchorReal: real, anchorPhone: clockTime(last, real) })
|
|
177
|
+
}
|
|
178
|
+
})
|
|
179
|
+
},
|
|
180
|
+
Panel: ({ state, run }) => {
|
|
181
|
+
useTick(!state.frozen)
|
|
182
|
+
const now = clockTime(state, Date.now())
|
|
183
|
+
const zone = phoneTimeZone()
|
|
184
|
+
const hourCycle = phoneUses24HourClock() ? 'h23' : 'h12'
|
|
185
|
+
const rates = RATES.includes(state.rate) ? RATES : [...RATES, state.rate].sort((a, b) => a - b)
|
|
186
|
+
const status = state.frozen ? 'Frozen' : isReal(state) ? 'Real time' : state.rate === 1 ? 'Running' : `Running at ${state.rate}×`
|
|
187
|
+
return (
|
|
188
|
+
<>
|
|
189
|
+
<Field label="Phone time" value={status}>
|
|
190
|
+
<Readout
|
|
191
|
+
rows={[
|
|
192
|
+
['Date', formatted(now, { weekday: 'short', year: 'numeric', month: 'short', day: 'numeric' })],
|
|
193
|
+
['Time', formatted(now, { hour: 'numeric', minute: '2-digit', second: '2-digit', hourCycle })],
|
|
194
|
+
['Time zone', zone.replace(/_/g, ' ')],
|
|
195
|
+
['Real time', drift(now - Date.now())],
|
|
196
|
+
]}
|
|
197
|
+
/>
|
|
198
|
+
</Field>
|
|
199
|
+
<Field label="Set date and time">
|
|
200
|
+
<TextField label="Set date and time" type="datetime-local" value={wallTimeInput(now, zone)} onChange={(value) => value !== '' && run('set', { time: value })} />
|
|
201
|
+
</Field>
|
|
202
|
+
<Row>
|
|
203
|
+
<Button pressed={state.frozen} onClick={() => run(state.frozen ? 'resume' : 'freeze')}>
|
|
204
|
+
{state.frozen ? 'Resume' : 'Freeze'}
|
|
205
|
+
</Button>
|
|
206
|
+
<Button onClick={() => run('advance', { hours: 1 })}>+1 hour</Button>
|
|
207
|
+
<Button onClick={() => run('advance', { days: 1 })}>+1 day</Button>
|
|
208
|
+
</Row>
|
|
209
|
+
<Field label="Speed">
|
|
210
|
+
<Select
|
|
211
|
+
label="Speed"
|
|
212
|
+
value={String(state.rate)}
|
|
213
|
+
options={rates.map((rate) => ({ value: String(rate), label: rateLabel(rate) }))}
|
|
214
|
+
onChange={(rate) => run('rate', { rate: Number(rate) })}
|
|
215
|
+
/>
|
|
216
|
+
</Field>
|
|
217
|
+
<Row>
|
|
218
|
+
<Button disabled={isReal(state)} onClick={() => run('reset')}>
|
|
219
|
+
Reset to real time
|
|
220
|
+
</Button>
|
|
221
|
+
</Row>
|
|
222
|
+
</>
|
|
223
|
+
)
|
|
224
|
+
},
|
|
225
|
+
})
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { type ClockState, REAL_CLOCK } from 'web-sim/runtime'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The phone's clock, set in the panel: the real time by default, or a time set, frozen or run faster. Its math is in
|
|
5
|
+
* `web-sim/runtime` (`phoneNow`, `clockTime`), which the app's `Date`, the status bar and other plugins share.
|
|
6
|
+
*/
|
|
7
|
+
export type { ClockState }
|
|
8
|
+
|
|
9
|
+
export const INITIAL_CLOCK: ClockState = { ...REAL_CLOCK }
|
|
10
|
+
|
|
11
|
+
/** The speeds the panel offers. */
|
|
12
|
+
export const RATES = [1, 10, 60, 3600]
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import { type LogEntry, noRecordChanges, type Platform, type RecordChanges, simRecords, type SimRecords } from 'web-sim/runtime'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The address book: a few made-up people, plus the changes the app and the panel make, kept per platform in the
|
|
5
|
+
* plugin's state (iOS and Android give their contacts different ids).
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
export type Item = { id: string; label?: string; [key: string]: unknown }
|
|
9
|
+
export type ContactDate = { year?: number; month: number; day: number }
|
|
10
|
+
|
|
11
|
+
export type ContactRecord = {
|
|
12
|
+
id: string
|
|
13
|
+
isFavourite: boolean
|
|
14
|
+
givenName: string | null
|
|
15
|
+
middleName: string | null
|
|
16
|
+
familyName: string | null
|
|
17
|
+
maidenName: string | null
|
|
18
|
+
nickname: string | null
|
|
19
|
+
prefix: string | null
|
|
20
|
+
suffix: string | null
|
|
21
|
+
phoneticGivenName: string | null
|
|
22
|
+
phoneticMiddleName: string | null
|
|
23
|
+
phoneticFamilyName: string | null
|
|
24
|
+
company: string | null
|
|
25
|
+
department: string | null
|
|
26
|
+
jobTitle: string | null
|
|
27
|
+
phoneticCompanyName: string | null
|
|
28
|
+
note: string | null
|
|
29
|
+
image: string | null
|
|
30
|
+
birthday: ContactDate | null
|
|
31
|
+
nonGregorianBirthday: unknown
|
|
32
|
+
emails: Item[]
|
|
33
|
+
phones: Item[]
|
|
34
|
+
dates: Item[]
|
|
35
|
+
extraNames: Item[]
|
|
36
|
+
addresses: Item[]
|
|
37
|
+
relations: Item[]
|
|
38
|
+
urlAddresses: Item[]
|
|
39
|
+
socialProfiles: Item[]
|
|
40
|
+
imAddresses: Item[]
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export function empty(id: string): ContactRecord {
|
|
44
|
+
return {
|
|
45
|
+
id, isFavourite: false, givenName: null, middleName: null, familyName: null, maidenName: null, nickname: null,
|
|
46
|
+
prefix: null, suffix: null, phoneticGivenName: null, phoneticMiddleName: null, phoneticFamilyName: null,
|
|
47
|
+
company: null, department: null, jobTitle: null, phoneticCompanyName: null, note: null, image: null,
|
|
48
|
+
birthday: null, nonGregorianBirthday: null, emails: [], phones: [], dates: [], extraNames: [], addresses: [],
|
|
49
|
+
relations: [], urlAddresses: [], socialProfiles: [], imAddresses: [],
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
type Seed = [given: string, family: string, phone: string, email: string | null, extra?: Partial<ContactRecord>]
|
|
54
|
+
|
|
55
|
+
const PEOPLE: Seed[] = [
|
|
56
|
+
['Ava', 'Chen', '(415) 555-0132', 'ava.chen@example.com', { isFavourite: true, birthday: { year: 1991, month: 4, day: 12 } }],
|
|
57
|
+
['Mateo', 'Alvarez', '(408) 555-0187', 'mateo@example.com', { company: 'Alvarez Bakery', jobTitle: 'Owner' }],
|
|
58
|
+
['Priya', 'Natarajan', '(650) 555-0144', 'priya.n@example.org', { isFavourite: true }],
|
|
59
|
+
['Jonas', 'Becker', '+49 30 5550 1234', null, { nickname: 'Joe' }],
|
|
60
|
+
['Sofia', 'Rossi', '(212) 555-0199', 'sofia.rossi@example.com', { birthday: { month: 11, day: 3 } }],
|
|
61
|
+
['Kenji', 'Watanabe', '(510) 555-0176', 'kenji@example.net', { company: 'Northwind Foods', department: 'Purchasing', jobTitle: 'Buyer' }],
|
|
62
|
+
['Amara', 'Okafor', '(628) 555-0121', 'amara.okafor@example.com', {}],
|
|
63
|
+
['Liam', 'Murphy', '(925) 555-0163', null, { note: 'Brings dessert' }],
|
|
64
|
+
]
|
|
65
|
+
|
|
66
|
+
function builtIn(os: Platform): ContactRecord[] {
|
|
67
|
+
const ios = os === 'ios'
|
|
68
|
+
return PEOPLE.map(([givenName, familyName, phone, email, extra], index) => {
|
|
69
|
+
const id = ios ? `C0FFEE${String(index + 1).padStart(2, '0')}-5A1B-4C3D-9E8F-ADDRE55B00C${index}:ABPerson` : String(index + 1)
|
|
70
|
+
return {
|
|
71
|
+
...empty(id),
|
|
72
|
+
givenName,
|
|
73
|
+
familyName,
|
|
74
|
+
...extra,
|
|
75
|
+
phones: [{ id: `${id}-phone`, label: 'mobile', number: phone }],
|
|
76
|
+
emails: email === null ? [] : [{ id: `${id}-email`, label: index % 3 === 1 ? 'work' : 'home', address: email }],
|
|
77
|
+
}
|
|
78
|
+
})
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export function fullName(record: ContactRecord): string {
|
|
82
|
+
const name = [record.prefix, record.givenName, record.middleName, record.familyName, record.suffix].filter(Boolean).join(' ')
|
|
83
|
+
return name || record.company || ''
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export type ContactsState = {
|
|
87
|
+
ios: RecordChanges<ContactRecord>
|
|
88
|
+
android: RecordChanges<ContactRecord>
|
|
89
|
+
/** What the app added, changed and deleted. */
|
|
90
|
+
log: LogEntry[]
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export const INITIAL_CONTACTS: ContactsState = { ios: noRecordChanges(), android: noRecordChanges(), log: [] }
|
|
94
|
+
|
|
95
|
+
export type ContactsHandle = { get(): ContactsState; set(patch: Partial<ContactsState>): void }
|
|
96
|
+
|
|
97
|
+
/** The address book for a platform. `onWrite` hears each change, with a line for a log. */
|
|
98
|
+
export function contactBook(handle: ContactsHandle, os: Platform, onWrite?: (text: string) => void): SimRecords<ContactRecord> {
|
|
99
|
+
const records = simRecords(() => builtIn(os), {
|
|
100
|
+
read: () => handle.get()[os],
|
|
101
|
+
write: (changes) => handle.set({ [os]: changes }),
|
|
102
|
+
})
|
|
103
|
+
if (onWrite === undefined) {
|
|
104
|
+
return records
|
|
105
|
+
}
|
|
106
|
+
return {
|
|
107
|
+
...records,
|
|
108
|
+
put(record) {
|
|
109
|
+
const existed = records.get(record.id) !== null
|
|
110
|
+
records.put(record)
|
|
111
|
+
onWrite(`${existed ? 'Changed' : 'Added'} ${fullName(record) || 'a contact'}`)
|
|
112
|
+
return record
|
|
113
|
+
},
|
|
114
|
+
remove(id) {
|
|
115
|
+
const record = records.get(id)
|
|
116
|
+
records.remove(id)
|
|
117
|
+
onWrite(`Deleted ${record === null ? 'a contact' : fullName(record) || 'a contact'}`)
|
|
118
|
+
},
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/** A contact's first phone number and email, as the panel edits them. */
|
|
123
|
+
export function primary(record: ContactRecord): { phone: string; email: string } {
|
|
124
|
+
return { phone: String(record.phones[0]?.number ?? ''), email: String(record.emails[0]?.address ?? '') }
|
|
125
|
+
}
|