-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwterm-renderer.tsx
More file actions
389 lines (362 loc) · 11.7 KB
/
Copy pathwterm-renderer.tsx
File metadata and controls
389 lines (362 loc) · 11.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
import {
useCallback,
useEffect,
useRef,
useState,
type CSSProperties,
type MouseEvent as ReactMouseEvent,
type UIEvent as ReactUIEvent,
} from "react";
import { GhosttyCore } from "@wterm/ghostty";
import { Terminal, type TerminalHandle } from "@wterm/react";
import { z } from "zod";
import type { TerminalAttachment } from "./terminal-attachment.js";
// @ts-expect-error CSS side effects are resolved by the plugin bundler.
import "@wterm/react/css";
// @ts-expect-error CSS side effects are resolved by the plugin bundler.
import "./wterm-renderer.css";
const PLUGIN_ID = "wterm-terminal-preview";
export const GHOSTTY_WASM_URL = `/api/v1/plugins/${PLUGIN_ID}/http/ghostty-vt.wasm`;
export const NERD_FONT_URL = `/api/v1/plugins/${PLUGIN_ID}/http/symbols-nerd-font-mono-v3.5.0.woff2`;
export const NERD_FONT_FAMILY = "Wterm Symbols Nerd Font Mono";
const nerdFontLoads = new WeakMap<object, Promise<void>>();
/**
* Ghostty WASM 0.3.4 discards mode 1003 before `mouseTracking()` can expose it.
* Track that one DEC mode at the write boundary, then let Wterm DOM provide
* its supported click, wheel, and button-drag subset through mode 1002.
*/
export function supportAnyEventMouseMode(core: GhosttyCore): GhosttyCore {
const decodeMouseControl = new TextDecoder("latin1");
const writeRaw = core.writeRaw.bind(core);
const writeString = core.writeString.bind(core);
const supportedMode = core.mouseTracking.bind(core);
let anyEventMouse = false;
let controlTail = "";
const observeMouseMode = (text: string) => {
const control = controlTail + text;
for (const match of control.matchAll(/\x1b\[\?([0-9;]*)([hl])/g)) {
const modes = match[1]?.split(";") ?? [];
if (modes.includes("1003")) {
anyEventMouse = match[2] === "h";
}
if (
match[2] === "l" &&
modes.some(
(mode) => mode === "47" || mode === "1047" || mode === "1049",
)
) {
anyEventMouse = false;
}
}
controlTail = control.slice(-64);
};
core.writeRaw = (data) => {
observeMouseMode(decodeMouseControl.decode(data));
writeRaw(data);
};
core.writeString = (data) => {
observeMouseMode(data);
writeString(data);
};
core.mouseTracking = () => (anyEventMouse ? 1002 : supportedMode());
return core;
}
async function pluginToken(): Promise<string> {
const response = await fetch(`/api/v1/plugins/${PLUGIN_ID}/token`, {
method: "POST",
headers: { "content-type": "application/json" },
body: "{}",
signal: AbortSignal.timeout(10_000),
});
const json: unknown = await response.json().catch(() => null);
const parsed = z.object({ token: z.string() }).safeParse(json);
if (!response.ok || !parsed.success) {
throw new Error(`WASM token request failed (HTTP ${response.status})`);
}
return parsed.data.token;
}
export async function loadGhosttyCore(
wasmUrl = GHOSTTY_WASM_URL,
): Promise<GhosttyCore> {
if (wasmUrl !== GHOSTTY_WASM_URL) {
return supportAnyEventMouseMode(
await GhosttyCore.load({ wasmPath: wasmUrl }),
);
}
const response = await fetch(wasmUrl, {
headers: { "x-bb-plugin-token": await pluginToken() },
signal: AbortSignal.timeout(10_000),
});
if (!response.ok) {
throw new Error(`WASM request failed (HTTP ${response.status})`);
}
const objectUrl = URL.createObjectURL(
new Blob([await response.arrayBuffer()], { type: "application/wasm" }),
);
try {
return supportAnyEventMouseMode(
await GhosttyCore.load({ wasmPath: objectUrl }),
);
} finally {
URL.revokeObjectURL(objectUrl);
}
}
export async function loadNerdFont(
fontFaceSet: Pick<FontFaceSet, "add"> = document.fonts,
): Promise<void> {
const key = fontFaceSet as object;
const cached = nerdFontLoads.get(key);
if (cached) return cached;
const pending = (async () => {
const response = await fetch(NERD_FONT_URL, {
headers: { "x-bb-plugin-token": await pluginToken() },
signal: AbortSignal.timeout(10_000),
});
if (!response.ok) {
throw new Error(`font request failed (HTTP ${response.status})`);
}
const face = new FontFace(
NERD_FONT_FAMILY,
await response.arrayBuffer(),
{ style: "normal", weight: "400" },
);
fontFaceSet.add(await face.load());
})();
const retryable = pending.catch((error: unknown) => {
nerdFontLoads.delete(key);
throw error;
});
nerdFontLoads.set(key, retryable);
return retryable;
}
export function hasRenderedSize(element: HTMLElement): boolean {
const { width, height } = element.getBoundingClientRect();
return width > 0 && height > 0;
}
interface WtermFontMetricsBoundary {
cols: number;
element: HTMLElement;
rows: number;
resize(cols: number, rows: number): void;
_measureCharSize?: () => { charWidth: number; rowHeight: number } | null;
}
export function refitTerminalAfterFontChange(
instance: NonNullable<TerminalHandle["instance"]>,
): boolean {
const terminal = instance as unknown as WtermFontMetricsBoundary;
const metrics = terminal._measureCharSize?.();
if (!metrics || !hasRenderedSize(terminal.element)) return false;
const style = getComputedStyle(terminal.element);
const contentWidth =
terminal.element.clientWidth -
(Number.parseFloat(style.paddingLeft) || 0) -
(Number.parseFloat(style.paddingRight) || 0);
const contentHeight =
terminal.element.clientHeight -
(Number.parseFloat(style.paddingTop) || 0) -
(Number.parseFloat(style.paddingBottom) || 0);
const cols = Math.max(1, Math.floor(contentWidth / metrics.charWidth));
const rows = Math.max(1, Math.floor(contentHeight / metrics.rowHeight));
if (cols === terminal.cols && rows === terminal.rows) return false;
terminal.resize(cols, rows);
return true;
}
type TerminalFontStyle = CSSProperties & {
"--term-font-family": string;
"--term-font-size": string;
"--term-row-height": string;
};
export function WtermRenderer({
attachment,
fontSizePx = 14,
wasmUrl = GHOSTTY_WASM_URL,
}: {
attachment: TerminalAttachment;
fontSizePx?: number;
wasmUrl?: string;
}) {
const terminalRef = useRef<TerminalHandle>(null);
const [core, setCore] = useState<GhosttyCore | null>(null);
const [error, setError] = useState<unknown>(null);
const [ready, setReady] = useState(false);
const clearSelectionBoundaryRef = useRef<(() => void) | null>(null);
const followBottomRef = useRef(true);
const terminalFontStyle: TerminalFontStyle = {
"--term-font-family":
`Menlo, Consolas, "DejaVu Sans Mono", "Courier New", "${NERD_FONT_FAMILY}", monospace`,
"--term-font-size": `${fontSizePx}px`,
"--term-row-height": `${Math.ceil(fontSizePx * 1.2)}px`,
};
useEffect(() => {
let alive = true;
setCore(null);
setError(null);
setReady(false);
void loadNerdFont().catch(() => undefined);
void loadGhosttyCore(wasmUrl).then(
(loaded) => {
if (alive) setCore(loaded);
},
(loadError) => {
if (alive) setError(loadError);
},
);
return () => {
alive = false;
};
}, [wasmUrl]);
useEffect(() => {
if (!ready) return;
return attachment.subscribe(({ bytes }) =>
terminalRef.current?.write(bytes),
);
}, [attachment, ready]);
useEffect(() => {
if (!ready) return;
const shouldRestoreBottom = followBottomRef.current;
let settleFrame: number | null = null;
const frame = window.requestAnimationFrame(() => {
const instance = terminalRef.current?.instance;
if (!instance) return;
refitTerminalAfterFontChange(instance);
settleFrame = window.requestAnimationFrame(() => {
const element = terminalRef.current?.instance?.element;
if (element && shouldRestoreBottom) {
element.scrollTop = element.scrollHeight;
}
});
});
return () => {
window.cancelAnimationFrame(frame);
if (settleFrame !== null) window.cancelAnimationFrame(settleFrame);
};
}, [fontSizePx, ready]);
useEffect(
() => () => {
clearSelectionBoundaryRef.current?.();
},
[],
);
const handleMouseDown = useCallback(
(event: ReactMouseEvent<HTMLDivElement>) => {
// WTerm prevents this event when a TUI owns the mouse. Shift-drag and
// ordinary shell selection remain native and reach this boundary.
if (event.button !== 0 || event.defaultPrevented) {
return;
}
clearSelectionBoundaryRef.current?.();
const selectionRoot = event.currentTarget;
document.documentElement.dataset.wtermNativeSelection = "active";
selectionRoot.dataset.wtermSelectionActive = "true";
const clearBoundary = () => {
window.removeEventListener("mouseup", finishSelection);
window.removeEventListener("blur", clearBoundary);
if (clearSelectionBoundaryRef.current === clearBoundary) {
clearSelectionBoundaryRef.current = null;
delete document.documentElement.dataset.wtermNativeSelection;
delete selectionRoot.dataset.wtermSelectionActive;
}
};
const finishSelection = () => {
clearBoundary();
const terminal = terminalRef.current?.instance?.element;
const selection = window.getSelection();
if (
!terminal ||
!selection ||
selection.isCollapsed ||
selection.rangeCount === 0
) {
return;
}
const range = selection.getRangeAt(0);
if (
!terminal.contains(range.startContainer) ||
!terminal.contains(range.endContainer)
) {
return;
}
const text = selection.toString();
if (text.length > 0 && navigator.clipboard?.writeText) {
void navigator.clipboard.writeText(text).catch(() => undefined);
}
};
clearSelectionBoundaryRef.current = clearBoundary;
window.addEventListener("mouseup", finishSelection, { once: true });
window.addEventListener("blur", clearBoundary, { once: true });
},
[],
);
const handleResize = useCallback(
(cols: number, rows: number) => {
const element = terminalRef.current?.instance?.element;
if (element && hasRenderedSize(element)) {
attachment.sendResize(cols, rows);
}
},
[attachment],
);
const handleScroll = useCallback((event: ReactUIEvent<HTMLDivElement>) => {
const element = event.currentTarget;
followBottomRef.current =
element.scrollHeight - element.scrollTop - element.clientHeight <= 1;
}, []);
if (error) {
return (
<div className="wterm-renderer-diagnostic" role="alert">
Ghostty terminal renderer failed to initialize.
</div>
);
}
if (!core) {
return (
<div
className="wterm-renderer"
data-renderer="ghostty"
aria-busy="true"
/>
);
}
return (
<Terminal
ref={terminalRef}
core={core}
autoResize
onReady={() => setReady(true)}
onError={setError}
onData={(data) => attachment.sendInput(new TextEncoder().encode(data))}
onResize={handleResize}
onMouseDown={handleMouseDown}
onScroll={handleScroll}
style={terminalFontStyle}
className="wterm-renderer"
data-renderer="ghostty"
/>
);
}
export function TerminalRenderer({
terminalId,
attachment,
fontSizePx = 14,
}: {
terminalId: string;
attachment: TerminalAttachment | null;
fontSizePx?: number;
}) {
if (!attachment) {
return (
<div className="wterm-renderer" data-terminal-id={terminalId}>
Waiting for terminal attachment…
</div>
);
}
return (
<div className="wterm-renderer-panel" data-terminal-id={terminalId}>
<WtermRenderer
key={terminalId}
attachment={attachment}
fontSizePx={fontSizePx}
/>
</div>
);
}