@celljs/ta-workspace 4.8.0 → 4.9.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 +2 -2
- package/README.zh-cn.md +2 -2
- package/package.json +13 -4
- package/web/index.js +916 -295
- package/web-src/browser-agent.ts +0 -440
- package/web-src/browser-common.tsx +0 -278
- package/web-src/browser-import.tsx +0 -126
- package/web-src/browser-webview.tsx +0 -270
- package/web-src/browser.tsx +0 -80
- package/web-src/common.tsx +0 -296
- package/web-src/editors.tsx +0 -471
- package/web-src/explorer.tsx +0 -717
- package/web-src/file-icon.tsx +0 -21
- package/web-src/host.ts +0 -121
- package/web-src/i18n.ts +0 -395
- package/web-src/index.tsx +0 -71
- package/web-src/open-files.tsx +0 -80
- package/web-src/preview.tsx +0 -132
- package/web-src/react.ts +0 -8
- package/web-src/scm.tsx +0 -388
- package/web-src/search.tsx +0 -485
- package/web-src/style.ts +0 -254
- package/web-src/terminal.tsx +0 -269
- package/web-src/util/browser-snapshot.spec.ts +0 -96
- package/web-src/util/browser-snapshot.ts +0 -225
- package/web-src/util/file-icon.spec.ts +0 -35
- package/web-src/util/file-icon.ts +0 -23
- package/web-src/util/git.ts +0 -203
- package/web-src/util/preview.spec.ts +0 -48
- package/web-src/util/preview.ts +0 -90
- package/web-src/util/search.spec.ts +0 -164
- package/web-src/util/search.ts +0 -274
- package/web-src/util/seti-font.ts +0 -6
- package/web-src/util/seti.ts +0 -761
- package/web-src/util/upload.spec.ts +0 -66
- package/web-src/util/upload.ts +0 -123
- package/web-src/util/workspace.spec.ts +0 -220
- package/web-src/util/workspace.ts +0 -396
- package/web-src/util/zip.spec.ts +0 -60
- package/web-src/util/zip.ts +0 -137
- package/web-src/workspace-client.ts +0 -221
|
@@ -1,278 +0,0 @@
|
|
|
1
|
-
import { Action, Glyph } from './common';
|
|
2
|
-
import { Channel, Host, pi } from './host';
|
|
3
|
-
import { t } from './i18n';
|
|
4
|
-
import { React } from './react';
|
|
5
|
-
import { WorkspaceUtil } from './util/workspace';
|
|
6
|
-
|
|
7
|
-
const RECENT_KEY = 'recent';
|
|
8
|
-
const MAX_RECENT = 12;
|
|
9
|
-
|
|
10
|
-
/** The addresses opened lately, kept for the user on the server (pi.storage), in every browser. */
|
|
11
|
-
export namespace Recent {
|
|
12
|
-
let list: string[] = [];
|
|
13
|
-
let loaded = false;
|
|
14
|
-
const listeners = new Set<() => void>();
|
|
15
|
-
|
|
16
|
-
export function all(): string[] {
|
|
17
|
-
return list;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
export function subscribe(listener: () => void): () => void {
|
|
21
|
-
listeners.add(listener);
|
|
22
|
-
if (!loaded) {
|
|
23
|
-
loaded = true;
|
|
24
|
-
void load();
|
|
25
|
-
}
|
|
26
|
-
return () => listeners.delete(listener);
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
async function load(): Promise<void> {
|
|
30
|
-
try {
|
|
31
|
-
const kept = await pi.storage.get<unknown>(RECENT_KEY);
|
|
32
|
-
list = Array.isArray(kept) ? kept.filter((url): url is string => typeof url === 'string') : [];
|
|
33
|
-
} catch {
|
|
34
|
-
list = [];
|
|
35
|
-
}
|
|
36
|
-
for (const listener of listeners) {
|
|
37
|
-
listener();
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
export function add(url: string): void {
|
|
42
|
-
save([url, ...list.filter(other => other !== url)].slice(0, MAX_RECENT));
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
export function remove(url: string): void {
|
|
46
|
-
save(list.filter(other => other !== url));
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
function save(next: string[]): void {
|
|
50
|
-
list = next;
|
|
51
|
-
pi.storage.set(RECENT_KEY, list).catch(() => undefined);
|
|
52
|
-
for (const listener of listeners) {
|
|
53
|
-
listener();
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
/** Addresses to try on an empty start page: the ports dev servers take by default. */
|
|
59
|
-
const SUGGESTIONS = ['localhost:5173', 'localhost:3000', 'localhost:8080'];
|
|
60
|
-
|
|
61
|
-
/** The address without the scheme, split for show: the host stands out, the rest is quieter. */
|
|
62
|
-
function parts(url: string): { host: string; rest: string } {
|
|
63
|
-
try {
|
|
64
|
-
const parsed = new URL(url);
|
|
65
|
-
const rest = `${parsed.pathname === '/' ? '' : parsed.pathname}${parsed.search}${parsed.hash}`;
|
|
66
|
-
return { host: parsed.host || url, rest };
|
|
67
|
-
} catch {
|
|
68
|
-
return { host: url, rest: '' };
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
/** The icon of the address: the page loading, its own icon when it gave one, else a globe. */
|
|
73
|
-
function AddressIcon({ value, loading, favicon }: { value: string; loading: boolean; favicon?: string }) {
|
|
74
|
-
const [broken, setBroken] = React.useState<string>();
|
|
75
|
-
if (loading) {
|
|
76
|
-
return (
|
|
77
|
-
<span className="taw-spin">
|
|
78
|
-
<Glyph name="LoaderCircle" size={14} />
|
|
79
|
-
</span>
|
|
80
|
-
);
|
|
81
|
-
}
|
|
82
|
-
if (favicon && broken !== favicon) {
|
|
83
|
-
return <img className="taw-address-favicon" src={favicon} alt="" referrerPolicy="no-referrer" onError={() => setBroken(favicon)} />;
|
|
84
|
-
}
|
|
85
|
-
return <Glyph name={value ? 'Globe' : 'Search'} size={14} />;
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
function AddressBar({
|
|
89
|
-
value,
|
|
90
|
-
title,
|
|
91
|
-
loading,
|
|
92
|
-
favicon,
|
|
93
|
-
onOpen,
|
|
94
|
-
}: {
|
|
95
|
-
value: string;
|
|
96
|
-
title?: string;
|
|
97
|
-
loading: boolean;
|
|
98
|
-
favicon?: string;
|
|
99
|
-
onOpen(url: string): void;
|
|
100
|
-
}) {
|
|
101
|
-
const [text, setText] = React.useState(value);
|
|
102
|
-
React.useEffect(() => setText(value), [value]);
|
|
103
|
-
return (
|
|
104
|
-
<form
|
|
105
|
-
className="taw-address"
|
|
106
|
-
onSubmit={event => {
|
|
107
|
-
event.preventDefault();
|
|
108
|
-
const url = WorkspaceUtil.toUrl(text);
|
|
109
|
-
if (url) {
|
|
110
|
-
onOpen(url);
|
|
111
|
-
(document.activeElement as HTMLElement | null)?.blur();
|
|
112
|
-
}
|
|
113
|
-
}}
|
|
114
|
-
>
|
|
115
|
-
<span className="taw-address-icon">
|
|
116
|
-
<AddressIcon value={value} loading={loading} favicon={favicon} />
|
|
117
|
-
</span>
|
|
118
|
-
<input
|
|
119
|
-
value={text}
|
|
120
|
-
placeholder={t('address')}
|
|
121
|
-
aria-label={t('address')}
|
|
122
|
-
title={title || undefined}
|
|
123
|
-
spellCheck={false}
|
|
124
|
-
autoComplete="off"
|
|
125
|
-
onFocus={event => event.target.select()}
|
|
126
|
-
onChange={event => setText(event.target.value)}
|
|
127
|
-
onKeyDown={event => {
|
|
128
|
-
if (event.key === 'Escape') {
|
|
129
|
-
setText(value);
|
|
130
|
-
event.currentTarget.blur();
|
|
131
|
-
}
|
|
132
|
-
}}
|
|
133
|
-
/>
|
|
134
|
-
{text.trim() && text !== value ? (
|
|
135
|
-
<button type="submit" className="taw-action taw-address-go" title={t('open')} aria-label={t('open')}>
|
|
136
|
-
<Glyph name="ArrowRight" size={14} />
|
|
137
|
-
</button>
|
|
138
|
-
) : undefined}
|
|
139
|
-
</form>
|
|
140
|
-
);
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
export interface BrowserBarProps {
|
|
144
|
-
/** The address shown, '' on the start page. */
|
|
145
|
-
url: string;
|
|
146
|
-
/** The title of the page, on hover over its address. */
|
|
147
|
-
title?: string;
|
|
148
|
-
favicon?: string;
|
|
149
|
-
loading: boolean;
|
|
150
|
-
canGoBack: boolean;
|
|
151
|
-
canGoForward: boolean;
|
|
152
|
-
onBack(): void;
|
|
153
|
-
onForward(): void;
|
|
154
|
-
onReload(): void;
|
|
155
|
-
/** Stops loading: the button of reload while a page loads, when given. */
|
|
156
|
-
onStop?(): void;
|
|
157
|
-
onOpen(url: string): void;
|
|
158
|
-
onHome(): void;
|
|
159
|
-
/** Opens the page out of Ta; `undefined` while there is none. */
|
|
160
|
-
onOpenOutside?(): void;
|
|
161
|
-
outsideLabel: string;
|
|
162
|
-
/** Beside the address: what goes on with the page. */
|
|
163
|
-
badge?: React.ReactNode;
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
/** The bar of the browser: back, forward, reload or stop, the address, the start page, out of Ta. */
|
|
167
|
-
export function BrowserBar(props: BrowserBarProps) {
|
|
168
|
-
const { url, loading, onStop } = props;
|
|
169
|
-
return (
|
|
170
|
-
<header className="taw-browser-bar">
|
|
171
|
-
<Action icon="ArrowLeft" label={t('back')} disabled={!props.canGoBack} onClick={props.onBack} />
|
|
172
|
-
<Action icon="ArrowRight" label={t('forward')} disabled={!props.canGoForward} onClick={props.onForward} />
|
|
173
|
-
{loading && onStop ? (
|
|
174
|
-
<Action icon="X" label={t('stop')} onClick={onStop} />
|
|
175
|
-
) : (
|
|
176
|
-
<Action icon="RefreshCw" label={t('refresh')} disabled={!url} onClick={props.onReload} />
|
|
177
|
-
)}
|
|
178
|
-
<AddressBar value={url} title={props.title} loading={loading} favicon={props.favicon} onOpen={props.onOpen} />
|
|
179
|
-
{props.badge}
|
|
180
|
-
<Action icon="House" label={t('home')} disabled={!url} onClick={props.onHome} />
|
|
181
|
-
<Action icon="ExternalLink" label={props.outsideLabel} disabled={!props.onOpenOutside} onClick={() => props.onOpenOutside?.()} />
|
|
182
|
-
{loading ? <span className="taw-browser-loading" /> : undefined}
|
|
183
|
-
</header>
|
|
184
|
-
);
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
/** The start page: what the browser is for, `extra` under it, then the addresses opened lately; `foot`, a line under them. */
|
|
188
|
-
export function Home({ onOpen, foot, extra }: { onOpen(url: string): void; foot?: string; extra?: React.ReactNode }) {
|
|
189
|
-
const recent = React.useSyncExternalStore(Recent.subscribe, Recent.all);
|
|
190
|
-
return (
|
|
191
|
-
<div className={`taw-browser-home ${recent.length === 0 ? 'taw-browser-blank' : ''}`}>
|
|
192
|
-
<div className="taw-browser-hero">
|
|
193
|
-
<span className="taw-browser-mark">
|
|
194
|
-
<Glyph name="Globe" size={22} />
|
|
195
|
-
</span>
|
|
196
|
-
<p className="taw-browser-title">{t('browserTitle')}</p>
|
|
197
|
-
<p className="taw-browser-sub">{t('browserHint')}</p>
|
|
198
|
-
{recent.length === 0 ? (
|
|
199
|
-
<div className="taw-browser-chips">
|
|
200
|
-
{SUGGESTIONS.map(address => (
|
|
201
|
-
<button type="button" key={address} className="taw-browser-chip" onClick={() => onOpen(`http://${address}`)}>
|
|
202
|
-
{address}
|
|
203
|
-
</button>
|
|
204
|
-
))}
|
|
205
|
-
</div>
|
|
206
|
-
) : undefined}
|
|
207
|
-
{extra}
|
|
208
|
-
</div>
|
|
209
|
-
{recent.length > 0 ? (
|
|
210
|
-
<section className="taw-browser-recent">
|
|
211
|
-
<h3 className="taw-browser-heading">{t('recent')}</h3>
|
|
212
|
-
{recent.map(address => {
|
|
213
|
-
const { host, rest } = parts(address);
|
|
214
|
-
return (
|
|
215
|
-
<div key={address} className="taw-browser-site">
|
|
216
|
-
<button type="button" className="taw-browser-site-main" title={address} onClick={() => onOpen(address)}>
|
|
217
|
-
<span className="taw-browser-letter">
|
|
218
|
-
{host
|
|
219
|
-
.replace(/^www\./, '')
|
|
220
|
-
.charAt(0)
|
|
221
|
-
.toUpperCase()}
|
|
222
|
-
</span>
|
|
223
|
-
<span className="taw-browser-host">{host}</span>
|
|
224
|
-
<span className="taw-browser-path">{rest}</span>
|
|
225
|
-
</button>
|
|
226
|
-
<button
|
|
227
|
-
type="button"
|
|
228
|
-
className="taw-action taw-browser-remove"
|
|
229
|
-
title={t('removeRecent')}
|
|
230
|
-
aria-label={t('removeRecent')}
|
|
231
|
-
onClick={() => Recent.remove(address)}
|
|
232
|
-
>
|
|
233
|
-
<Glyph name="X" size={13} />
|
|
234
|
-
</button>
|
|
235
|
-
</div>
|
|
236
|
-
);
|
|
237
|
-
})}
|
|
238
|
-
</section>
|
|
239
|
-
) : undefined}
|
|
240
|
-
{foot ? <p className="taw-browser-foot">{foot}</p> : undefined}
|
|
241
|
-
</div>
|
|
242
|
-
);
|
|
243
|
-
}
|
|
244
|
-
|
|
245
|
-
/** A page to show, from a link of a reply. */
|
|
246
|
-
export const ShowPage = new Channel<string>();
|
|
247
|
-
|
|
248
|
-
/**
|
|
249
|
-
* The address to load for `url`: in a directory of another machine, a page of its localhost is a port of
|
|
250
|
-
* that machine, opened here (TaRemoteServer.forwardPort); `undefined` until it is.
|
|
251
|
-
*/
|
|
252
|
-
export function useForwarded(url: string, cwd: string): string | undefined {
|
|
253
|
-
const port = WorkspaceUtil.localPort(url);
|
|
254
|
-
const remote = cwd.startsWith('ssh://') && port !== undefined;
|
|
255
|
-
const [forwarded, setForwarded] = React.useState<{ url: string; to: string }>();
|
|
256
|
-
React.useEffect(() => {
|
|
257
|
-
if (!remote || port === undefined) {
|
|
258
|
-
return;
|
|
259
|
-
}
|
|
260
|
-
let current = true;
|
|
261
|
-
pi.remote.forwardPort(port, cwd).then(
|
|
262
|
-
({ localPort }) => current && setForwarded({ url, to: WorkspaceUtil.withPort(url, localPort) }),
|
|
263
|
-
error => current && pi.ui.notify(Host.message(error), 'error'),
|
|
264
|
-
);
|
|
265
|
-
return () => {
|
|
266
|
-
current = false;
|
|
267
|
-
};
|
|
268
|
-
}, [remote, port, cwd, url]);
|
|
269
|
-
if (!remote) {
|
|
270
|
-
return url;
|
|
271
|
-
}
|
|
272
|
-
return forwarded?.url === url ? forwarded.to : undefined;
|
|
273
|
-
}
|
|
274
|
-
|
|
275
|
-
/** Opens an address out of Ta: in a new tab of the browser, or the system's browser in Ta Desktop. */
|
|
276
|
-
export function openOutside(url: string): void {
|
|
277
|
-
window.open(url, '_blank', 'noopener,noreferrer');
|
|
278
|
-
}
|
|
@@ -1,126 +0,0 @@
|
|
|
1
|
-
import type { WebBrowserData, WebBrowserImport, WebBrowserProfile } from '@celljs/ta-web/lib/browser/extension/web-extension-api';
|
|
2
|
-
import { Glyph } from './common';
|
|
3
|
-
import { Host, pi } from './host';
|
|
4
|
-
import { t } from './i18n';
|
|
5
|
-
import { React } from './react';
|
|
6
|
-
|
|
7
|
-
type Step = { kind: 'pick' } | { kind: 'working' } | { kind: 'done'; report: WebBrowserImport } | { kind: 'cleared' };
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* Takes the sign-ins of a profile of the user's browser into the browser of Ta (Ta Desktop): pick the
|
|
11
|
-
* profile here, then the app asks in its own dialog. `onChanged` when the cookies of the browser changed.
|
|
12
|
-
*/
|
|
13
|
-
function ImportDialog({ data, onClose, onChanged }: { data: WebBrowserData; onClose(): void; onChanged(): void }) {
|
|
14
|
-
const [profiles, setProfiles] = React.useState<WebBrowserProfile[]>();
|
|
15
|
-
const [chosen, setChosen] = React.useState<string>();
|
|
16
|
-
const [step, setStep] = React.useState<Step>({ kind: 'pick' });
|
|
17
|
-
const [error, setError] = React.useState<string>();
|
|
18
|
-
React.useEffect(() => {
|
|
19
|
-
data.profiles().then(
|
|
20
|
-
list => {
|
|
21
|
-
setProfiles(list);
|
|
22
|
-
setChosen(list[0]?.id);
|
|
23
|
-
},
|
|
24
|
-
failure => setError(Host.message(failure)),
|
|
25
|
-
);
|
|
26
|
-
}, [data]);
|
|
27
|
-
const run = async (action: () => Promise<Step | undefined>) => {
|
|
28
|
-
setError(undefined);
|
|
29
|
-
setStep({ kind: 'working' });
|
|
30
|
-
try {
|
|
31
|
-
setStep((await action()) ?? { kind: 'pick' });
|
|
32
|
-
} catch (failure) {
|
|
33
|
-
setError(Host.message(failure));
|
|
34
|
-
setStep({ kind: 'pick' });
|
|
35
|
-
}
|
|
36
|
-
};
|
|
37
|
-
const take = () =>
|
|
38
|
-
run(async () => {
|
|
39
|
-
const report = chosen ? await data.import(chosen) : undefined;
|
|
40
|
-
if (report) {
|
|
41
|
-
onChanged();
|
|
42
|
-
}
|
|
43
|
-
return report && { kind: 'done', report };
|
|
44
|
-
});
|
|
45
|
-
const clear = async () => {
|
|
46
|
-
if (await pi.ui.confirm(t('clearBrowserData'), t('clearBrowserDataDetail'), { confirmLabel: t('clear'), danger: true })) {
|
|
47
|
-
await run(async () => {
|
|
48
|
-
await data.clear();
|
|
49
|
-
onChanged();
|
|
50
|
-
return { kind: 'cleared' };
|
|
51
|
-
});
|
|
52
|
-
}
|
|
53
|
-
};
|
|
54
|
-
const working = step.kind === 'working';
|
|
55
|
-
let body: React.ReactNode;
|
|
56
|
-
if (step.kind === 'done' || step.kind === 'cleared') {
|
|
57
|
-
const report = step.kind === 'done' ? step.report : undefined;
|
|
58
|
-
body = (
|
|
59
|
-
<div className="taw-import-result">
|
|
60
|
-
<span className="taw-browser-mark">
|
|
61
|
-
<Glyph name="Check" size={22} />
|
|
62
|
-
</span>
|
|
63
|
-
<p className="taw-browser-title">{report ? t('imported', { count: report.imported }) : t('cleared')}</p>
|
|
64
|
-
{report && report.skipped > 0 ? <p className="taw-browser-sub">{t('importSkipped', { count: report.skipped })}</p> : undefined}
|
|
65
|
-
{report && report.appBound > 0 ? <p className="taw-browser-sub">{t('importAppBound', { count: report.appBound })}</p> : undefined}
|
|
66
|
-
<div className="taw-import-actions">
|
|
67
|
-
<button type="button" className="taw-button taw-primary" onClick={onClose}>
|
|
68
|
-
{t('done')}
|
|
69
|
-
</button>
|
|
70
|
-
</div>
|
|
71
|
-
</div>
|
|
72
|
-
);
|
|
73
|
-
} else {
|
|
74
|
-
body = (
|
|
75
|
-
<React.Fragment>
|
|
76
|
-
<p className="taw-import-hint">{t('importHint')}</p>
|
|
77
|
-
{profiles === undefined && !error ? <p className="taw-import-hint">{t('loading')}</p> : undefined}
|
|
78
|
-
{profiles?.length === 0 ? <p className="taw-import-hint">{t('noProfiles')}</p> : undefined}
|
|
79
|
-
{profiles && profiles.length > 0 ? (
|
|
80
|
-
<div className="taw-import-profiles" role="radiogroup" aria-label={t('importFrom')}>
|
|
81
|
-
{profiles.map(profile => (
|
|
82
|
-
<label key={profile.id} className={`taw-import-profile ${chosen === profile.id ? 'taw-on' : ''}`}>
|
|
83
|
-
<input type="radio" name="taw-import-profile" checked={chosen === profile.id} onChange={() => setChosen(profile.id)} />
|
|
84
|
-
<span className="taw-browser-letter">{profile.browser.replace(/^(Google|Microsoft) /, '').charAt(0)}</span>
|
|
85
|
-
<span className="taw-import-browser">{profile.browser}</span>
|
|
86
|
-
<span className="taw-import-name">{profile.name}</span>
|
|
87
|
-
</label>
|
|
88
|
-
))}
|
|
89
|
-
</div>
|
|
90
|
-
) : undefined}
|
|
91
|
-
{error ? <p className="taw-import-error">{error}</p> : undefined}
|
|
92
|
-
<div className="taw-import-actions">
|
|
93
|
-
<button type="button" className="taw-link" disabled={working} onClick={() => void clear()}>
|
|
94
|
-
{t('clearBrowserData')}
|
|
95
|
-
</button>
|
|
96
|
-
<button type="button" className="taw-button taw-primary" disabled={working || !chosen} onClick={() => void take()}>
|
|
97
|
-
{working ? t('importing') : t('importNow')}
|
|
98
|
-
</button>
|
|
99
|
-
</div>
|
|
100
|
-
</React.Fragment>
|
|
101
|
-
);
|
|
102
|
-
}
|
|
103
|
-
return (
|
|
104
|
-
<pi.ui.Dialog title={t('importTitle')} onClose={onClose}>
|
|
105
|
-
<div className="taw-import">{body}</div>
|
|
106
|
-
</pi.ui.Dialog>
|
|
107
|
-
);
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
/** On the start page of Ta Desktop's browser: the way to the sign-ins of the user's browser. */
|
|
111
|
-
export function ImportSignIns({ onChanged }: { onChanged(): void }) {
|
|
112
|
-
const data = pi.ui.desktop?.browserData;
|
|
113
|
-
const [open, setOpen] = React.useState(false);
|
|
114
|
-
if (!data) {
|
|
115
|
-
return null;
|
|
116
|
-
}
|
|
117
|
-
return (
|
|
118
|
-
<React.Fragment>
|
|
119
|
-
<button type="button" className="taw-browser-import" onClick={() => setOpen(true)}>
|
|
120
|
-
<Glyph name="Download" size={13} />
|
|
121
|
-
{t('importSignIns')}
|
|
122
|
-
</button>
|
|
123
|
-
{open ? <ImportDialog data={data} onClose={() => setOpen(false)} onChanged={onChanged} /> : undefined}
|
|
124
|
-
</React.Fragment>
|
|
125
|
-
);
|
|
126
|
-
}
|
|
@@ -1,270 +0,0 @@
|
|
|
1
|
-
import { AgentBrowser, type WebviewElement } from './browser-agent';
|
|
2
|
-
import { BrowserBar, Home, openOutside, Recent, ShowPage, useForwarded } from './browser-common';
|
|
3
|
-
import { ImportSignIns } from './browser-import';
|
|
4
|
-
import { Glyph } from './common';
|
|
5
|
-
import { pi } from './host';
|
|
6
|
-
import { t } from './i18n';
|
|
7
|
-
import { React } from './react';
|
|
8
|
-
import { WorkspaceUtil } from './util/workspace';
|
|
9
|
-
|
|
10
|
-
/** Electron's code of a load that another one replaced: not a failure. */
|
|
11
|
-
const ERR_ABORTED = -3;
|
|
12
|
-
|
|
13
|
-
interface Page {
|
|
14
|
-
url: string;
|
|
15
|
-
title: string;
|
|
16
|
-
favicon?: string;
|
|
17
|
-
loading: boolean;
|
|
18
|
-
canGoBack: boolean;
|
|
19
|
-
canGoForward: boolean;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
/** Why the page is not shown: a load that failed, or the process of the page that went away. */
|
|
23
|
-
type Failure = { url: string; reason: string } | { crashed: true };
|
|
24
|
-
|
|
25
|
-
const NO_PAGE: Page = { url: '', title: '', loading: false, canGoBack: false, canGoForward: false };
|
|
26
|
-
|
|
27
|
-
/** The page of the webview as its events tell it; `ready` once its methods can be called. */
|
|
28
|
-
function usePage(view: React.RefObject<WebviewElement | null>, mounted: boolean, onFailure: (failure: Failure | undefined) => void) {
|
|
29
|
-
const [page, setPage] = React.useState<Page>(NO_PAGE);
|
|
30
|
-
const ready = React.useRef(false);
|
|
31
|
-
const failed = React.useRef(onFailure);
|
|
32
|
-
failed.current = onFailure;
|
|
33
|
-
React.useEffect(() => {
|
|
34
|
-
const element = view.current;
|
|
35
|
-
if (!element) {
|
|
36
|
-
return;
|
|
37
|
-
}
|
|
38
|
-
const update = (next: Partial<Page>) =>
|
|
39
|
-
setPage(previous => ({
|
|
40
|
-
...previous,
|
|
41
|
-
...next,
|
|
42
|
-
...(ready.current ? { canGoBack: element.canGoBack(), canGoForward: element.canGoForward() } : {}),
|
|
43
|
-
}));
|
|
44
|
-
type Detail = Event & {
|
|
45
|
-
url: string;
|
|
46
|
-
isMainFrame: boolean;
|
|
47
|
-
title: string;
|
|
48
|
-
favicons: string[];
|
|
49
|
-
errorCode: number;
|
|
50
|
-
errorDescription: string;
|
|
51
|
-
validatedURL: string;
|
|
52
|
-
};
|
|
53
|
-
const listeners: [string, (event: Detail) => void][] = [
|
|
54
|
-
[
|
|
55
|
-
'dom-ready',
|
|
56
|
-
() => {
|
|
57
|
-
ready.current = true;
|
|
58
|
-
update({});
|
|
59
|
-
},
|
|
60
|
-
],
|
|
61
|
-
[
|
|
62
|
-
'did-start-loading',
|
|
63
|
-
() => {
|
|
64
|
-
failed.current(undefined);
|
|
65
|
-
update({ loading: true });
|
|
66
|
-
},
|
|
67
|
-
],
|
|
68
|
-
['did-stop-loading', () => update({ loading: false })],
|
|
69
|
-
['did-navigate', event => update({ url: event.url, favicon: undefined })],
|
|
70
|
-
['did-navigate-in-page', event => event.isMainFrame && update({ url: event.url })],
|
|
71
|
-
['page-title-updated', event => update({ title: event.title })],
|
|
72
|
-
['page-favicon-updated', event => update({ favicon: event.favicons.find(icon => /^(https?:|data:image\/)/i.test(icon)) })],
|
|
73
|
-
[
|
|
74
|
-
'did-fail-load',
|
|
75
|
-
event => {
|
|
76
|
-
if (event.isMainFrame && event.errorCode !== ERR_ABORTED) {
|
|
77
|
-
failed.current({ url: event.validatedURL, reason: event.errorDescription || String(event.errorCode) });
|
|
78
|
-
update({ url: event.validatedURL, title: '', favicon: undefined, loading: false });
|
|
79
|
-
}
|
|
80
|
-
},
|
|
81
|
-
],
|
|
82
|
-
[
|
|
83
|
-
'render-process-gone',
|
|
84
|
-
() => {
|
|
85
|
-
failed.current({ crashed: true });
|
|
86
|
-
update({ loading: false });
|
|
87
|
-
},
|
|
88
|
-
],
|
|
89
|
-
];
|
|
90
|
-
for (const [name, listener] of listeners) {
|
|
91
|
-
element.addEventListener(name, listener as EventListener);
|
|
92
|
-
}
|
|
93
|
-
return () => {
|
|
94
|
-
ready.current = false;
|
|
95
|
-
for (const [name, listener] of listeners) {
|
|
96
|
-
element.removeEventListener(name, listener as EventListener);
|
|
97
|
-
}
|
|
98
|
-
};
|
|
99
|
-
}, [mounted]);
|
|
100
|
-
/** The address being opened, shown until the page has one. */
|
|
101
|
-
const expect = React.useCallback((url: string) => setPage(previous => ({ ...previous, url, title: '', favicon: undefined })), []);
|
|
102
|
-
return { page, ready, expect };
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
function FailurePanel({ failure, onRetry, onOutside }: { failure: Failure; onRetry(): void; onOutside?(): void }) {
|
|
106
|
-
const crashed = 'crashed' in failure;
|
|
107
|
-
return (
|
|
108
|
-
<div className="taw-browser-failure" role="alert">
|
|
109
|
-
<span className="taw-browser-mark">
|
|
110
|
-
<Glyph name="CircleAlert" size={22} />
|
|
111
|
-
</span>
|
|
112
|
-
<p className="taw-browser-title">{t(crashed ? 'pageCrashed' : 'loadFailed')}</p>
|
|
113
|
-
{crashed ? undefined : <p className="taw-browser-sub">{failure.reason}</p>}
|
|
114
|
-
<div className="taw-browser-chips">
|
|
115
|
-
<button type="button" className="taw-browser-chip" onClick={onRetry}>
|
|
116
|
-
{t('retry')}
|
|
117
|
-
</button>
|
|
118
|
-
{onOutside ? (
|
|
119
|
-
<button type="button" className="taw-browser-chip" onClick={onOutside}>
|
|
120
|
-
{t('openBrowser')}
|
|
121
|
-
</button>
|
|
122
|
-
) : undefined}
|
|
123
|
-
</div>
|
|
124
|
-
</div>
|
|
125
|
-
);
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
/** How long after the agent's last action the bar still says it works the page. */
|
|
129
|
-
const DRIVEN_MS = 4000;
|
|
130
|
-
|
|
131
|
-
/**
|
|
132
|
-
* Gives the agent the page (AgentBrowser) while the view is open; whether it worked it lately. `show`
|
|
133
|
-
* puts the page in front of the start page.
|
|
134
|
-
*/
|
|
135
|
-
function useAgent(
|
|
136
|
-
go: (url: string) => void,
|
|
137
|
-
view: React.RefObject<WebviewElement | null>,
|
|
138
|
-
ready: React.RefObject<boolean>,
|
|
139
|
-
hasPage: boolean,
|
|
140
|
-
show: () => void,
|
|
141
|
-
stage: React.RefObject<HTMLDivElement | null>,
|
|
142
|
-
): boolean {
|
|
143
|
-
const [driven, setDriven] = React.useState(false);
|
|
144
|
-
const latest = React.useRef({ go, hasPage, show });
|
|
145
|
-
latest.current = { go, hasPage, show };
|
|
146
|
-
React.useEffect(() => {
|
|
147
|
-
let timer: ReturnType<typeof setTimeout> | undefined;
|
|
148
|
-
const detach = AgentBrowser.attach({
|
|
149
|
-
go: url => latest.current.go(url),
|
|
150
|
-
view: () => (latest.current.hasPage && ready.current ? (view.current ?? undefined) : undefined),
|
|
151
|
-
shown: () => (stage.current?.getClientRects().length ?? 0) > 0,
|
|
152
|
-
drive: () => {
|
|
153
|
-
if (latest.current.hasPage) {
|
|
154
|
-
latest.current.show();
|
|
155
|
-
}
|
|
156
|
-
setDriven(true);
|
|
157
|
-
clearTimeout(timer);
|
|
158
|
-
timer = setTimeout(() => setDriven(false), DRIVEN_MS);
|
|
159
|
-
},
|
|
160
|
-
});
|
|
161
|
-
return () => {
|
|
162
|
-
clearTimeout(timer);
|
|
163
|
-
detach();
|
|
164
|
-
};
|
|
165
|
-
}, []);
|
|
166
|
-
return driven;
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
/**
|
|
170
|
-
* The browser in Ta Desktop: a `<webview>`, a page of its own that sites cannot refuse, where signing in
|
|
171
|
-
* stays; back, forward, the address, the title and the icon follow the page. The webview stays while the
|
|
172
|
-
* start page is shown, so that back returns to the page as it was.
|
|
173
|
-
*/
|
|
174
|
-
export function WebviewBrowser() {
|
|
175
|
-
const cwd = pi.ui.useCwd();
|
|
176
|
-
/** The address asked for last; `id` tells apart two requests of the same address. */
|
|
177
|
-
const [request, setRequest] = React.useState({ url: '', id: 0 });
|
|
178
|
-
const [home, setHome] = React.useState(true);
|
|
179
|
-
/** The first address of the webview: later ones go through loadURL, which keeps its history. */
|
|
180
|
-
const [src, setSrc] = React.useState<string>();
|
|
181
|
-
const [failure, setFailure] = React.useState<Failure>();
|
|
182
|
-
const view = React.useRef<WebviewElement | null>(null);
|
|
183
|
-
const stage = React.useRef<HTMLDivElement>(null);
|
|
184
|
-
const { page, ready, expect } = usePage(view, Boolean(src), setFailure);
|
|
185
|
-
const shown = useForwarded(request.url, cwd);
|
|
186
|
-
React.useEffect(() => {
|
|
187
|
-
if (!request.url || !shown) {
|
|
188
|
-
return;
|
|
189
|
-
}
|
|
190
|
-
setFailure(undefined);
|
|
191
|
-
expect(shown);
|
|
192
|
-
if (view.current && ready.current) {
|
|
193
|
-
view.current.loadURL(shown).catch(() => undefined);
|
|
194
|
-
} else {
|
|
195
|
-
setSrc(shown);
|
|
196
|
-
}
|
|
197
|
-
}, [shown, request.id]);
|
|
198
|
-
const go = (url: string) => {
|
|
199
|
-
if (!url) {
|
|
200
|
-
setHome(true);
|
|
201
|
-
return;
|
|
202
|
-
}
|
|
203
|
-
Recent.add(url);
|
|
204
|
-
setHome(false);
|
|
205
|
-
setRequest(previous => ({ url, id: previous.id + 1 }));
|
|
206
|
-
};
|
|
207
|
-
ShowPage.useReceive(go);
|
|
208
|
-
const driven = useAgent(go, view, ready, Boolean(src), () => setHome(false), stage);
|
|
209
|
-
const call = (run: (element: WebviewElement) => void) => {
|
|
210
|
-
if (view.current && ready.current) {
|
|
211
|
-
run(view.current);
|
|
212
|
-
}
|
|
213
|
-
};
|
|
214
|
-
const retry = () => {
|
|
215
|
-
if (failure && 'url' in failure && failure.url) {
|
|
216
|
-
setFailure(undefined);
|
|
217
|
-
call(element => element.loadURL(failure.url).catch(() => undefined));
|
|
218
|
-
} else {
|
|
219
|
-
call(element => element.reload());
|
|
220
|
-
}
|
|
221
|
-
};
|
|
222
|
-
// A page of another machine's localhost shows at the address asked for, not the port forwarded to.
|
|
223
|
-
const address = home ? '' : WorkspaceUtil.unforward(page.url || request.url, request.url, shown);
|
|
224
|
-
const current = page.url || shown;
|
|
225
|
-
return (
|
|
226
|
-
<div className="taw-browser">
|
|
227
|
-
<BrowserBar
|
|
228
|
-
url={address}
|
|
229
|
-
title={home ? undefined : page.title}
|
|
230
|
-
favicon={home ? undefined : page.favicon}
|
|
231
|
-
loading={!home && page.loading}
|
|
232
|
-
canGoBack={home ? Boolean(src) : page.canGoBack}
|
|
233
|
-
canGoForward={!home && page.canGoForward}
|
|
234
|
-
onBack={() => (home ? setHome(false) : call(element => element.goBack()))}
|
|
235
|
-
onForward={() => call(element => element.goForward())}
|
|
236
|
-
onReload={retry}
|
|
237
|
-
onStop={() => call(element => element.stop())}
|
|
238
|
-
onOpen={go}
|
|
239
|
-
onHome={() => setHome(true)}
|
|
240
|
-
onOpenOutside={!home && current ? () => openOutside(current) : undefined}
|
|
241
|
-
outsideLabel={t('openBrowser')}
|
|
242
|
-
badge={
|
|
243
|
-
driven ? (
|
|
244
|
-
<span className="taw-browser-driven" role="status">
|
|
245
|
-
<span className="taw-browser-driven-dot" />
|
|
246
|
-
{t('agentDriving')}
|
|
247
|
-
</span>
|
|
248
|
-
) : undefined
|
|
249
|
-
}
|
|
250
|
-
/>
|
|
251
|
-
<div className="taw-browser-stage" ref={stage}>
|
|
252
|
-
{src
|
|
253
|
-
? React.createElement('webview', {
|
|
254
|
-
ref: view,
|
|
255
|
-
src,
|
|
256
|
-
className: 'taw-webview',
|
|
257
|
-
// Windows a page opens (a sign-in) are the app's to allow (embedded-browser.ts).
|
|
258
|
-
allowpopups: 'true',
|
|
259
|
-
// On the element itself: hidden by its parent, a webview may still be drawn.
|
|
260
|
-
style: { display: home ? 'none' : 'flex' },
|
|
261
|
-
})
|
|
262
|
-
: undefined}
|
|
263
|
-
{home ? <Home onOpen={go} extra={<ImportSignIns onChanged={() => call(element => element.reload())} />} /> : undefined}
|
|
264
|
-
{!home && failure ? (
|
|
265
|
-
<FailurePanel failure={failure} onRetry={retry} onOutside={'url' in failure ? () => openOutside(failure.url) : undefined} />
|
|
266
|
-
) : undefined}
|
|
267
|
-
</div>
|
|
268
|
-
</div>
|
|
269
|
-
);
|
|
270
|
-
}
|