@celljs/ta-workspace 0.0.0-stage → 4.6.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 +28 -2
- package/package.json +39 -4
- package/web/index.js +7289 -0
- package/web-src/browser.tsx +240 -0
- package/web-src/common.tsx +296 -0
- package/web-src/editors.tsx +471 -0
- package/web-src/explorer.tsx +479 -0
- package/web-src/file-icon.tsx +21 -0
- package/web-src/host.ts +121 -0
- package/web-src/i18n.ts +320 -0
- package/web-src/index.tsx +65 -0
- package/web-src/open-files.tsx +80 -0
- package/web-src/preview.tsx +132 -0
- package/web-src/react.ts +8 -0
- package/web-src/scm.tsx +388 -0
- package/web-src/search.tsx +485 -0
- package/web-src/style.ts +225 -0
- package/web-src/terminal.tsx +350 -0
- package/web-src/util/ansi.spec.ts +66 -0
- package/web-src/util/ansi.ts +438 -0
- package/web-src/util/file-icon.spec.ts +35 -0
- package/web-src/util/file-icon.ts +23 -0
- package/web-src/util/git.ts +203 -0
- package/web-src/util/keys.ts +56 -0
- package/web-src/util/preview.spec.ts +48 -0
- package/web-src/util/preview.ts +90 -0
- package/web-src/util/search.spec.ts +164 -0
- package/web-src/util/search.ts +274 -0
- package/web-src/util/seti-font.ts +6 -0
- package/web-src/util/seti.ts +761 -0
- package/web-src/util/workspace.spec.ts +191 -0
- package/web-src/util/workspace.ts +331 -0
- package/web-src/workspace-client.ts +119 -0
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
import { Action, Glyph } from './common';
|
|
2
|
+
import { Channel, 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
|
+
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
|
+
function AddressBar({ value, loading, onOpen }: { value: string; loading: boolean; onOpen(url: string): void }) {
|
|
73
|
+
const [text, setText] = React.useState(value);
|
|
74
|
+
React.useEffect(() => setText(value), [value]);
|
|
75
|
+
return (
|
|
76
|
+
<form
|
|
77
|
+
className="taw-address"
|
|
78
|
+
onSubmit={event => {
|
|
79
|
+
event.preventDefault();
|
|
80
|
+
const url = WorkspaceUtil.toUrl(text);
|
|
81
|
+
if (url) {
|
|
82
|
+
onOpen(url);
|
|
83
|
+
(document.activeElement as HTMLElement | null)?.blur();
|
|
84
|
+
}
|
|
85
|
+
}}
|
|
86
|
+
>
|
|
87
|
+
<span className="taw-address-icon">
|
|
88
|
+
{loading ? (
|
|
89
|
+
<span className="taw-spin">
|
|
90
|
+
<Glyph name="LoaderCircle" size={14} />
|
|
91
|
+
</span>
|
|
92
|
+
) : (
|
|
93
|
+
<Glyph name={value ? 'Globe' : 'Search'} size={14} />
|
|
94
|
+
)}
|
|
95
|
+
</span>
|
|
96
|
+
<input
|
|
97
|
+
value={text}
|
|
98
|
+
placeholder={t('address')}
|
|
99
|
+
aria-label={t('address')}
|
|
100
|
+
spellCheck={false}
|
|
101
|
+
autoComplete="off"
|
|
102
|
+
onFocus={event => event.target.select()}
|
|
103
|
+
onChange={event => setText(event.target.value)}
|
|
104
|
+
onKeyDown={event => {
|
|
105
|
+
if (event.key === 'Escape') {
|
|
106
|
+
setText(value);
|
|
107
|
+
event.currentTarget.blur();
|
|
108
|
+
}
|
|
109
|
+
}}
|
|
110
|
+
/>
|
|
111
|
+
{text.trim() && text !== value ? (
|
|
112
|
+
<button type="submit" className="taw-action taw-address-go" title={t('open')} aria-label={t('open')}>
|
|
113
|
+
<Glyph name="ArrowRight" size={14} />
|
|
114
|
+
</button>
|
|
115
|
+
) : undefined}
|
|
116
|
+
</form>
|
|
117
|
+
);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/** The start page: what the browser is for, then the addresses opened lately. */
|
|
121
|
+
function Home({ onOpen }: { onOpen(url: string): void }) {
|
|
122
|
+
const recent = React.useSyncExternalStore(Recent.subscribe, Recent.all);
|
|
123
|
+
return (
|
|
124
|
+
<div className={`taw-browser-home ${recent.length === 0 ? 'taw-browser-blank' : ''}`}>
|
|
125
|
+
<div className="taw-browser-hero">
|
|
126
|
+
<span className="taw-browser-mark">
|
|
127
|
+
<Glyph name="Globe" size={22} />
|
|
128
|
+
</span>
|
|
129
|
+
<p className="taw-browser-title">{t('browserTitle')}</p>
|
|
130
|
+
<p className="taw-browser-sub">{t('browserHint')}</p>
|
|
131
|
+
{recent.length === 0 ? (
|
|
132
|
+
<div className="taw-browser-chips">
|
|
133
|
+
{SUGGESTIONS.map(address => (
|
|
134
|
+
<button type="button" key={address} className="taw-browser-chip" onClick={() => onOpen(`http://${address}`)}>
|
|
135
|
+
{address}
|
|
136
|
+
</button>
|
|
137
|
+
))}
|
|
138
|
+
</div>
|
|
139
|
+
) : undefined}
|
|
140
|
+
</div>
|
|
141
|
+
{recent.length > 0 ? (
|
|
142
|
+
<section className="taw-browser-recent">
|
|
143
|
+
<h3 className="taw-browser-heading">{t('recent')}</h3>
|
|
144
|
+
{recent.map(address => {
|
|
145
|
+
const { host, rest } = parts(address);
|
|
146
|
+
return (
|
|
147
|
+
<div key={address} className="taw-browser-site">
|
|
148
|
+
<button type="button" className="taw-browser-site-main" title={address} onClick={() => onOpen(address)}>
|
|
149
|
+
<span className="taw-browser-letter">
|
|
150
|
+
{host
|
|
151
|
+
.replace(/^www\./, '')
|
|
152
|
+
.charAt(0)
|
|
153
|
+
.toUpperCase()}
|
|
154
|
+
</span>
|
|
155
|
+
<span className="taw-browser-host">{host}</span>
|
|
156
|
+
<span className="taw-browser-path">{rest}</span>
|
|
157
|
+
</button>
|
|
158
|
+
<button
|
|
159
|
+
type="button"
|
|
160
|
+
className="taw-action taw-browser-remove"
|
|
161
|
+
title={t('removeRecent')}
|
|
162
|
+
aria-label={t('removeRecent')}
|
|
163
|
+
onClick={() => Recent.remove(address)}
|
|
164
|
+
>
|
|
165
|
+
<Glyph name="X" size={13} />
|
|
166
|
+
</button>
|
|
167
|
+
</div>
|
|
168
|
+
);
|
|
169
|
+
})}
|
|
170
|
+
</section>
|
|
171
|
+
) : undefined}
|
|
172
|
+
<p className="taw-browser-foot">{t('frameHint')}</p>
|
|
173
|
+
</div>
|
|
174
|
+
);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/** A page to show, from a link of a reply. */
|
|
178
|
+
export const ShowPage = new Channel<string>();
|
|
179
|
+
|
|
180
|
+
interface History {
|
|
181
|
+
/** The addresses opened here, '' for the start page. */
|
|
182
|
+
list: string[];
|
|
183
|
+
index: number;
|
|
184
|
+
reloads: number;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* A browser in one tab of the workbench: a bar always there, the start page or the page opened under it.
|
|
189
|
+
* Back and forward go through the addresses opened here: a frame of another site hides its own history.
|
|
190
|
+
*/
|
|
191
|
+
export function BrowserView() {
|
|
192
|
+
const [history, setHistory] = React.useState<History>({ list: [''], index: 0, reloads: 0 });
|
|
193
|
+
const [loading, setLoading] = React.useState(false);
|
|
194
|
+
const url = history.list[history.index];
|
|
195
|
+
const go = (next: string) => {
|
|
196
|
+
if (next) {
|
|
197
|
+
Recent.add(next);
|
|
198
|
+
}
|
|
199
|
+
setLoading(Boolean(next));
|
|
200
|
+
setHistory(previous => ({ list: [...previous.list.slice(0, previous.index + 1), next], index: previous.index + 1, reloads: 0 }));
|
|
201
|
+
};
|
|
202
|
+
const step = (delta: number) => {
|
|
203
|
+
const index = Math.min(history.list.length - 1, Math.max(0, history.index + delta));
|
|
204
|
+
if (index !== history.index) {
|
|
205
|
+
setLoading(Boolean(history.list[index]));
|
|
206
|
+
setHistory({ ...history, index });
|
|
207
|
+
}
|
|
208
|
+
};
|
|
209
|
+
ShowPage.useReceive(go);
|
|
210
|
+
const reload = () => {
|
|
211
|
+
setLoading(true);
|
|
212
|
+
setHistory(previous => ({ ...previous, reloads: previous.reloads + 1 }));
|
|
213
|
+
};
|
|
214
|
+
return (
|
|
215
|
+
<div className="taw-browser">
|
|
216
|
+
<header className="taw-browser-bar">
|
|
217
|
+
<Action icon="ArrowLeft" label={t('back')} disabled={history.index === 0} onClick={() => step(-1)} />
|
|
218
|
+
<Action icon="ArrowRight" label={t('forward')} disabled={history.index === history.list.length - 1} onClick={() => step(1)} />
|
|
219
|
+
<Action icon="RefreshCw" label={t('refresh')} disabled={!url} onClick={reload} />
|
|
220
|
+
<AddressBar value={url} loading={loading} onOpen={go} />
|
|
221
|
+
<Action icon="House" label={t('home')} disabled={!url} onClick={() => go('')} />
|
|
222
|
+
<Action icon="ExternalLink" label={t('openExternal')} disabled={!url} onClick={() => window.open(url, '_blank', 'noopener,noreferrer')} />
|
|
223
|
+
{loading ? <span className="taw-browser-loading" /> : undefined}
|
|
224
|
+
</header>
|
|
225
|
+
{url ? (
|
|
226
|
+
<iframe
|
|
227
|
+
key={`${history.index}:${history.reloads}`}
|
|
228
|
+
className="taw-frame"
|
|
229
|
+
title={url}
|
|
230
|
+
src={url}
|
|
231
|
+
referrerPolicy="no-referrer"
|
|
232
|
+
sandbox={WorkspaceUtil.frameSandbox(url, location.origin)}
|
|
233
|
+
onLoad={() => setLoading(false)}
|
|
234
|
+
/>
|
|
235
|
+
) : (
|
|
236
|
+
<Home onOpen={go} />
|
|
237
|
+
)}
|
|
238
|
+
</div>
|
|
239
|
+
);
|
|
240
|
+
}
|
|
@@ -0,0 +1,296 @@
|
|
|
1
|
+
import type { WebIcon } from '@celljs/ta-web/lib/browser/extension/web-extension-api';
|
|
2
|
+
import { Host, pi } from './host';
|
|
3
|
+
import { React } from './react';
|
|
4
|
+
|
|
5
|
+
/** The row under the title the page gives a view: what it is about (`hint` on hover, else the title), and its actions. */
|
|
6
|
+
export function ViewHeader({ title, hint, children }: { title?: React.ReactNode; hint?: string; children?: React.ReactNode }) {
|
|
7
|
+
if (!title && !children) {
|
|
8
|
+
return null;
|
|
9
|
+
}
|
|
10
|
+
return (
|
|
11
|
+
<header className="taw-view-head">
|
|
12
|
+
<span className="taw-view-title" title={hint ?? (typeof title === 'string' ? title : undefined)}>
|
|
13
|
+
{title}
|
|
14
|
+
</span>
|
|
15
|
+
<div className="taw-actions">{children}</div>
|
|
16
|
+
</header>
|
|
17
|
+
);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function Action({
|
|
21
|
+
icon,
|
|
22
|
+
label,
|
|
23
|
+
onClick,
|
|
24
|
+
active,
|
|
25
|
+
disabled,
|
|
26
|
+
}: {
|
|
27
|
+
icon: string;
|
|
28
|
+
label: string;
|
|
29
|
+
onClick(event: React.MouseEvent): void;
|
|
30
|
+
active?: boolean;
|
|
31
|
+
disabled?: boolean;
|
|
32
|
+
}) {
|
|
33
|
+
const Glyph: WebIcon = Host.icon(icon);
|
|
34
|
+
return (
|
|
35
|
+
<button
|
|
36
|
+
type="button"
|
|
37
|
+
className={`taw-action ${active ? 'taw-on' : ''}`}
|
|
38
|
+
title={label}
|
|
39
|
+
aria-label={label}
|
|
40
|
+
aria-pressed={active === undefined ? undefined : active}
|
|
41
|
+
disabled={disabled}
|
|
42
|
+
onClick={event => {
|
|
43
|
+
event.stopPropagation();
|
|
44
|
+
onClick(event);
|
|
45
|
+
}}
|
|
46
|
+
onDoubleClick={event => event.stopPropagation()}
|
|
47
|
+
>
|
|
48
|
+
<Glyph size={15} strokeWidth={1.7} />
|
|
49
|
+
</button>
|
|
50
|
+
);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export function Empty({ children, tone }: { children: React.ReactNode; tone?: 'error' }) {
|
|
54
|
+
return <div className={`taw-empty ${tone === 'error' ? 'taw-error' : ''}`}>{children}</div>;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export function Glyph({ name, size = 15 }: { name: string; size?: number }) {
|
|
58
|
+
const Icon = Host.icon(name);
|
|
59
|
+
return <Icon size={size} strokeWidth={1.7} />;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** The value kept for one directory while the view comes and goes. */
|
|
63
|
+
export function useKept<T>(store: Map<string, T>, key: string, initial: () => T): [T, (value: T | ((previous: T) => T)) => void] {
|
|
64
|
+
const [value, setValue] = React.useState<T>(() => store.get(key) ?? initial());
|
|
65
|
+
const keyRef = React.useRef(key);
|
|
66
|
+
if (keyRef.current !== key) {
|
|
67
|
+
keyRef.current = key;
|
|
68
|
+
const next = store.get(key) ?? initial();
|
|
69
|
+
setValue(next);
|
|
70
|
+
}
|
|
71
|
+
const set = React.useCallback(
|
|
72
|
+
(next: T | ((previous: T) => T)) =>
|
|
73
|
+
setValue(previous => {
|
|
74
|
+
const value = typeof next === 'function' ? (next as (previous: T) => T)(previous) : next;
|
|
75
|
+
store.set(keyRef.current, value);
|
|
76
|
+
return value;
|
|
77
|
+
}),
|
|
78
|
+
[store],
|
|
79
|
+
);
|
|
80
|
+
return [value, set];
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** An entry of a context menu; `'-'` is a line between groups. */
|
|
84
|
+
export type MenuItem =
|
|
85
|
+
| '-'
|
|
86
|
+
| {
|
|
87
|
+
label: string;
|
|
88
|
+
run(): void;
|
|
89
|
+
/** Shown on the right, as the keys of an IDE. */
|
|
90
|
+
keys?: string;
|
|
91
|
+
disabled?: boolean;
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
interface MenuState {
|
|
95
|
+
x: number;
|
|
96
|
+
y: number;
|
|
97
|
+
items: MenuItem[];
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* A context menu at the pointer, as an IDE's: arrows move, Enter runs, Escape or a click elsewhere
|
|
102
|
+
* closes. `open` takes the event of a right click (or of a button that opens it) and the entries.
|
|
103
|
+
*/
|
|
104
|
+
export function useContextMenu(): [React.ReactNode, (event: React.MouseEvent | React.KeyboardEvent, items: MenuItem[]) => void] {
|
|
105
|
+
const [menu, setMenu] = React.useState<MenuState>();
|
|
106
|
+
const open = React.useCallback((event: React.MouseEvent | React.KeyboardEvent, items: MenuItem[]) => {
|
|
107
|
+
event.preventDefault();
|
|
108
|
+
event.stopPropagation();
|
|
109
|
+
const target = event.currentTarget as HTMLElement;
|
|
110
|
+
const box = target.getBoundingClientRect();
|
|
111
|
+
const pointer = 'clientX' in event && (event.clientX || event.clientY);
|
|
112
|
+
setMenu({ x: pointer ? event.clientX : box.left + 16, y: pointer ? event.clientY : box.bottom, items });
|
|
113
|
+
}, []);
|
|
114
|
+
const element = menu ? <ContextMenu {...menu} onClose={() => setMenu(undefined)} /> : null;
|
|
115
|
+
return [element, open];
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
function ContextMenu({ x, y, items, onClose }: MenuState & { onClose(): void }) {
|
|
119
|
+
const root = React.useRef<HTMLDivElement>(null);
|
|
120
|
+
const [place, setPlace] = React.useState({ left: x, top: y, ready: false });
|
|
121
|
+
React.useLayoutEffect(() => {
|
|
122
|
+
const box = root.current?.getBoundingClientRect();
|
|
123
|
+
if (box) {
|
|
124
|
+
const left = Math.max(4, Math.min(x, window.innerWidth - box.width - 4));
|
|
125
|
+
const top = y + box.height > window.innerHeight - 4 ? Math.max(4, y - box.height) : y;
|
|
126
|
+
setPlace({ left, top, ready: true });
|
|
127
|
+
}
|
|
128
|
+
root.current?.querySelector<HTMLButtonElement>('button:not(:disabled)')?.focus();
|
|
129
|
+
}, [x, y]);
|
|
130
|
+
React.useEffect(() => {
|
|
131
|
+
const close = (event: Event) => {
|
|
132
|
+
if (!root.current?.contains(event.target as Node)) {
|
|
133
|
+
onClose();
|
|
134
|
+
}
|
|
135
|
+
};
|
|
136
|
+
const onKey = (event: KeyboardEvent) => event.key === 'Escape' && onClose();
|
|
137
|
+
document.addEventListener('pointerdown', close, true);
|
|
138
|
+
document.addEventListener('keydown', onKey);
|
|
139
|
+
window.addEventListener('blur', onClose);
|
|
140
|
+
window.addEventListener('resize', onClose);
|
|
141
|
+
return () => {
|
|
142
|
+
document.removeEventListener('pointerdown', close, true);
|
|
143
|
+
document.removeEventListener('keydown', onKey);
|
|
144
|
+
window.removeEventListener('blur', onClose);
|
|
145
|
+
window.removeEventListener('resize', onClose);
|
|
146
|
+
};
|
|
147
|
+
}, [onClose]);
|
|
148
|
+
const onKeyDown = (event: React.KeyboardEvent) => {
|
|
149
|
+
if (event.key !== 'ArrowDown' && event.key !== 'ArrowUp') {
|
|
150
|
+
return;
|
|
151
|
+
}
|
|
152
|
+
event.preventDefault();
|
|
153
|
+
const buttons = [...(root.current?.querySelectorAll<HTMLButtonElement>('button:not(:disabled)') ?? [])];
|
|
154
|
+
const at = buttons.indexOf(document.activeElement as HTMLButtonElement);
|
|
155
|
+
buttons[(at + (event.key === 'ArrowDown' ? 1 : buttons.length - 1)) % buttons.length]?.focus();
|
|
156
|
+
};
|
|
157
|
+
return (
|
|
158
|
+
<div
|
|
159
|
+
ref={root}
|
|
160
|
+
className="taw-menu"
|
|
161
|
+
role="menu"
|
|
162
|
+
style={{ left: place.left, top: place.top, visibility: place.ready ? 'visible' : 'hidden' }}
|
|
163
|
+
onKeyDown={onKeyDown}
|
|
164
|
+
onContextMenu={event => event.preventDefault()}
|
|
165
|
+
>
|
|
166
|
+
{items.map((item, index) =>
|
|
167
|
+
item === '-' ? (
|
|
168
|
+
<hr key={`-${index}`} className="taw-menu-line" />
|
|
169
|
+
) : (
|
|
170
|
+
<button
|
|
171
|
+
key={item.label}
|
|
172
|
+
type="button"
|
|
173
|
+
role="menuitem"
|
|
174
|
+
className="taw-menu-item"
|
|
175
|
+
disabled={item.disabled}
|
|
176
|
+
onClick={() => {
|
|
177
|
+
onClose();
|
|
178
|
+
item.run();
|
|
179
|
+
}}
|
|
180
|
+
>
|
|
181
|
+
<span>{item.label}</span>
|
|
182
|
+
{item.keys && <kbd>{item.keys}</kbd>}
|
|
183
|
+
</button>
|
|
184
|
+
),
|
|
185
|
+
)}
|
|
186
|
+
</div>
|
|
187
|
+
);
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* A name typed in place, as the explorer of an IDE asks for one: Enter or leaving it accepts, Escape
|
|
192
|
+
* cancels. `validate` tells what is wrong (an error) or odd (a warning) while it is typed.
|
|
193
|
+
*/
|
|
194
|
+
export function NameInput({
|
|
195
|
+
initial,
|
|
196
|
+
validate,
|
|
197
|
+
onDone,
|
|
198
|
+
selectBase,
|
|
199
|
+
}: {
|
|
200
|
+
initial: string;
|
|
201
|
+
validate(name: string): { message: string; severity: 'error' | 'warning' } | undefined;
|
|
202
|
+
/** undefined: cancelled. */
|
|
203
|
+
onDone(name: string | undefined): void;
|
|
204
|
+
/** Selects the name without its extension, as a rename does. */
|
|
205
|
+
selectBase?: boolean;
|
|
206
|
+
}) {
|
|
207
|
+
const [value, setValue] = React.useState(initial);
|
|
208
|
+
const input = React.useRef<HTMLInputElement>(null);
|
|
209
|
+
const done = React.useRef(false);
|
|
210
|
+
React.useEffect(() => {
|
|
211
|
+
const element = input.current;
|
|
212
|
+
if (element) {
|
|
213
|
+
element.focus();
|
|
214
|
+
const dot = initial.lastIndexOf('.');
|
|
215
|
+
element.setSelectionRange(0, selectBase && dot > 0 ? dot : initial.length);
|
|
216
|
+
}
|
|
217
|
+
}, [initial, selectBase]);
|
|
218
|
+
// Nothing typed yet is not a mistake: the box only says what is wrong once there is a name.
|
|
219
|
+
const problem = value ? validate(value) : undefined;
|
|
220
|
+
const finish = (accept: boolean) => {
|
|
221
|
+
if (done.current) {
|
|
222
|
+
return;
|
|
223
|
+
}
|
|
224
|
+
if (accept && problem?.severity === 'error' && value.trim()) {
|
|
225
|
+
return;
|
|
226
|
+
}
|
|
227
|
+
done.current = true;
|
|
228
|
+
onDone(accept && value.trim() && value !== initial ? value : undefined);
|
|
229
|
+
};
|
|
230
|
+
return (
|
|
231
|
+
<span className="taw-name-input">
|
|
232
|
+
<input
|
|
233
|
+
ref={input}
|
|
234
|
+
className={`taw-input taw-inline ${problem ? `taw-${problem.severity}` : ''}`}
|
|
235
|
+
value={value}
|
|
236
|
+
spellCheck={false}
|
|
237
|
+
aria-invalid={problem?.severity === 'error'}
|
|
238
|
+
onChange={event => setValue(event.target.value)}
|
|
239
|
+
onClick={event => event.stopPropagation()}
|
|
240
|
+
onKeyDown={event => {
|
|
241
|
+
event.stopPropagation();
|
|
242
|
+
if (event.key === 'Enter') {
|
|
243
|
+
event.preventDefault();
|
|
244
|
+
finish(true);
|
|
245
|
+
} else if (event.key === 'Escape') {
|
|
246
|
+
event.preventDefault();
|
|
247
|
+
finish(false);
|
|
248
|
+
}
|
|
249
|
+
}}
|
|
250
|
+
onBlur={() => finish(problem?.severity !== 'error')}
|
|
251
|
+
/>
|
|
252
|
+
{problem && <span className={`taw-input-message taw-${problem.severity}`}>{problem.message}</span>}
|
|
253
|
+
</span>
|
|
254
|
+
);
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
/** A part of a view with a header that folds it, as the groups of source control: a title, a count, actions on hover. */
|
|
258
|
+
export function Section({
|
|
259
|
+
title,
|
|
260
|
+
count,
|
|
261
|
+
open,
|
|
262
|
+
onToggle,
|
|
263
|
+
actions,
|
|
264
|
+
children,
|
|
265
|
+
}: {
|
|
266
|
+
title: string;
|
|
267
|
+
count?: number;
|
|
268
|
+
open: boolean;
|
|
269
|
+
onToggle(): void;
|
|
270
|
+
actions?: React.ReactNode;
|
|
271
|
+
children?: React.ReactNode;
|
|
272
|
+
}) {
|
|
273
|
+
return (
|
|
274
|
+
<section className="taw-section">
|
|
275
|
+
<div className="taw-section-head">
|
|
276
|
+
<button type="button" className="taw-section-toggle" aria-expanded={open} onClick={onToggle}>
|
|
277
|
+
<span className="taw-twisty">
|
|
278
|
+
<Glyph name={open ? 'ChevronDown' : 'ChevronRight'} size={14} />
|
|
279
|
+
</span>
|
|
280
|
+
<span className="taw-section-title">{title}</span>
|
|
281
|
+
</button>
|
|
282
|
+
<div className="taw-row-actions">{actions}</div>
|
|
283
|
+
{count !== undefined && <span className="taw-badge">{count}</span>}
|
|
284
|
+
</div>
|
|
285
|
+
{open && children}
|
|
286
|
+
</section>
|
|
287
|
+
);
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
/** Writes to the clipboard, and says so. */
|
|
291
|
+
export function copyText(text: string, done: string): void {
|
|
292
|
+
navigator.clipboard.writeText(text).then(
|
|
293
|
+
() => pi.ui.notify(done),
|
|
294
|
+
error => pi.ui.notify(Host.message(error), 'error'),
|
|
295
|
+
);
|
|
296
|
+
}
|