| b69ab31 | | | 1 | /** |
| b69ab31 | | | 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| b69ab31 | | | 3 | * |
| b69ab31 | | | 4 | * This source code is licensed under the MIT license found in the |
| b69ab31 | | | 5 | * LICENSE file in the root directory of this source tree. |
| b69ab31 | | | 6 | */ |
| b69ab31 | | | 7 | |
| b69ab31 | | | 8 | import type {Json} from 'shared/typeUtils'; |
| b69ab31 | | | 9 | import type {Platform} from '../platform'; |
| b69ab31 | | | 10 | import type {OneIndexedLineNumber, PlatformName, RepoRelativePath} from '../types'; |
| b69ab31 | | | 11 | |
| b69ab31 | | | 12 | import {LocalWebSocketEventBus} from '../LocalWebSocketEventBus'; |
| b69ab31 | | | 13 | import {computeInitialParams} from '../urlParams'; |
| b69ab31 | | | 14 | |
| b69ab31 | | | 15 | // important: this file should not try to import other code from 'isl', |
| b69ab31 | | | 16 | // since it will end up getting duplicated when bundling. |
| b69ab31 | | | 17 | |
| b69ab31 | | | 18 | export function browserClipboardCopy(text: string, html?: string) { |
| b69ab31 | | | 19 | if (html) { |
| b69ab31 | | | 20 | const htmlBlob = new Blob([html], {type: 'text/html'}); |
| b69ab31 | | | 21 | const textBlob = new Blob([text], {type: 'text/plain'}); |
| b69ab31 | | | 22 | const clipboardItem = new window.ClipboardItem({ |
| b69ab31 | | | 23 | 'text/html': htmlBlob, |
| b69ab31 | | | 24 | 'text/plain': textBlob, |
| b69ab31 | | | 25 | }); |
| b69ab31 | | | 26 | navigator.clipboard.write([clipboardItem]); |
| b69ab31 | | | 27 | } else { |
| b69ab31 | | | 28 | navigator.clipboard.writeText(text); |
| b69ab31 | | | 29 | } |
| b69ab31 | | | 30 | } |
| b69ab31 | | | 31 | |
| b69ab31 | | | 32 | export const makeBrowserLikePlatformImpl = (platformName: PlatformName): Platform => { |
| b69ab31 | | | 33 | const initialUrlParams = computeInitialParams(platformName === 'browser'); |
| b69ab31 | | | 34 | return { |
| b69ab31 | | | 35 | platformName, |
| b69ab31 | | | 36 | confirm: (message: string, details?: string) => { |
| b69ab31 | | | 37 | const ok = window.confirm(message + '\n' + (details ?? '')); |
| b69ab31 | | | 38 | return Promise.resolve(ok); |
| b69ab31 | | | 39 | }, |
| b69ab31 | | | 40 | |
| b69ab31 | | | 41 | openFile: (path: RepoRelativePath, options?: {line?: OneIndexedLineNumber}) => { |
| b69ab31 | | | 42 | window.clientToServerAPI?.postMessage({type: 'platform/openFile', path, options}); |
| b69ab31 | | | 43 | }, |
| b69ab31 | | | 44 | openFiles: (paths: Array<RepoRelativePath>, options?: {line?: OneIndexedLineNumber}) => { |
| b69ab31 | | | 45 | window.clientToServerAPI?.postMessage({type: 'platform/openFiles', paths, options}); |
| b69ab31 | | | 46 | }, |
| b69ab31 | | | 47 | canCustomizeFileOpener: true, |
| b69ab31 | | | 48 | upsellExternalMergeTool: true, |
| b69ab31 | | | 49 | |
| b69ab31 | | | 50 | openContainingFolder: (path: RepoRelativePath) => { |
| b69ab31 | | | 51 | window.clientToServerAPI?.postMessage({type: 'platform/openContainingFolder', path}); |
| b69ab31 | | | 52 | }, |
| b69ab31 | | | 53 | |
| b69ab31 | | | 54 | openExternalLink(url: string): void { |
| b69ab31 | | | 55 | window.open(url, '_blank'); |
| b69ab31 | | | 56 | }, |
| b69ab31 | | | 57 | |
| b69ab31 | | | 58 | getPersistedState<T>(key: string): T | null { |
| b69ab31 | | | 59 | try { |
| b69ab31 | | | 60 | const found = localStorage.getItem(key) as string | null; |
| b69ab31 | | | 61 | if (found == null) { |
| b69ab31 | | | 62 | return null; |
| b69ab31 | | | 63 | } |
| b69ab31 | | | 64 | return JSON.parse(found) as T; |
| b69ab31 | | | 65 | } catch { |
| b69ab31 | | | 66 | return null; |
| b69ab31 | | | 67 | } |
| b69ab31 | | | 68 | }, |
| b69ab31 | | | 69 | setPersistedState<T>(key: string, value: T | undefined): void { |
| b69ab31 | | | 70 | try { |
| b69ab31 | | | 71 | if (value === undefined) { |
| b69ab31 | | | 72 | localStorage.removeItem(key); |
| b69ab31 | | | 73 | } else { |
| b69ab31 | | | 74 | localStorage.setItem(key, JSON.stringify(value)); |
| b69ab31 | | | 75 | } |
| b69ab31 | | | 76 | } catch {} |
| b69ab31 | | | 77 | }, |
| b69ab31 | | | 78 | clearPersistedState(): void { |
| b69ab31 | | | 79 | try { |
| b69ab31 | | | 80 | localStorage.clear(); |
| b69ab31 | | | 81 | } catch {} |
| b69ab31 | | | 82 | }, |
| b69ab31 | | | 83 | getAllPersistedState(): Json | undefined { |
| b69ab31 | | | 84 | try { |
| b69ab31 | | | 85 | return Object.fromEntries( |
| b69ab31 | | | 86 | Object.entries({...localStorage}) |
| b69ab31 | | | 87 | .map(([key, value]: [string, unknown]) => { |
| b69ab31 | | | 88 | try { |
| b69ab31 | | | 89 | return [key, JSON.parse(value as string)]; |
| b69ab31 | | | 90 | } catch { |
| b69ab31 | | | 91 | return null; |
| b69ab31 | | | 92 | } |
| b69ab31 | | | 93 | }) |
| b69ab31 | | | 94 | .filter((e): e is [string, Json] => e != null), |
| b69ab31 | | | 95 | ); |
| b69ab31 | | | 96 | } catch { |
| b69ab31 | | | 97 | return undefined; |
| b69ab31 | | | 98 | } |
| b69ab31 | | | 99 | }, |
| b69ab31 | | | 100 | |
| b69ab31 | | | 101 | clipboardCopy: browserClipboardCopy, |
| b69ab31 | | | 102 | |
| b69ab31 | | | 103 | messageBus: new LocalWebSocketEventBus( |
| b69ab31 | | | 104 | process.env.NODE_ENV === 'development' |
| b69ab31 | | | 105 | ? // in dev mode, Vite hosts our files for hot-reloading. |
| b69ab31 | | | 106 | // This means we can't host the ws server on the same port as the page. |
| b69ab31 | | | 107 | 'localhost:3001' |
| b69ab31 | | | 108 | : // in production, we serve both the static files and ws from the same port |
| b69ab31 | | | 109 | location.host, |
| b69ab31 | | | 110 | WebSocket, |
| b69ab31 | | | 111 | { |
| b69ab31 | | | 112 | cwd: initialUrlParams.get('cwd'), |
| b69ab31 | | | 113 | sessionId: initialUrlParams.get('sessionId'), |
| b69ab31 | | | 114 | token: initialUrlParams.get('token'), |
| b69ab31 | | | 115 | platformName, |
| b69ab31 | | | 116 | }, |
| b69ab31 | | | 117 | ), |
| b69ab31 | | | 118 | |
| b69ab31 | | | 119 | initialUrlParams, |
| b69ab31 | | | 120 | }; |
| b69ab31 | | | 121 | }; |