afield 0.0.0-stage → 0.1.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/LICENSE +15 -0
- package/README.md +165 -2
- package/afield +214 -0
- package/package.json +42 -4
- package/pi-extensions/afield-session.ts +59 -0
- package/pi-extensions/elastic-footer.ts +167 -0
- package/server/attach.js +264 -0
- package/server/auth.js +169 -0
- package/server/client.js +64 -0
- package/server/control.js +65 -0
- package/server/identity.js +45 -0
- package/server/index.js +375 -0
- package/server/mirror.js +159 -0
- package/server/origin.js +51 -0
- package/server/picker.js +187 -0
- package/server/places.js +348 -0
- package/server/protocol.js +51 -0
- package/server/quick-tunnel.js +40 -0
- package/server/registry.js +190 -0
- package/server/serve.js +100 -0
- package/server/session-main.js +27 -0
- package/server/session.js +112 -0
- package/server/supervisor.js +432 -0
- package/server/viewer.js +65 -0
- package/src/app.js +1683 -0
- package/src/apple-touch-icon.png +0 -0
- package/src/index.html +96 -0
- package/src/keys.js +59 -0
- package/src/pi-icon.svg +13 -0
- package/src/style.css +559 -0
- package/src/transport.js +153 -0
- package/src/ttyd.js +51 -0
- package/src/viewport.js +168 -0
- package/tsconfig.json +14 -0
- package/vendor/wterm/LICENSE +191 -0
- package/vendor/wterm/README.md +7 -0
- package/vendor/wterm/packages/@wterm/dom/src/debug.ts +347 -0
- package/vendor/wterm/packages/@wterm/dom/src/hyperlink.ts +8 -0
- package/vendor/wterm/packages/@wterm/dom/src/index.ts +13 -0
- package/vendor/wterm/packages/@wterm/dom/src/input.ts +662 -0
- package/vendor/wterm/packages/@wterm/dom/src/renderer.ts +725 -0
- package/vendor/wterm/packages/@wterm/dom/src/terminal.css +195 -0
- package/vendor/wterm/packages/@wterm/dom/src/wterm.ts +609 -0
|
@@ -0,0 +1,609 @@
|
|
|
1
|
+
// Modified for afield from upstream wterm; see vendor/wterm/README.md.
|
|
2
|
+
import { WasmBridge, type TerminalCore } from "@wterm/core";
|
|
3
|
+
import { Renderer } from "./renderer.js";
|
|
4
|
+
import { InputHandler } from "./input.js";
|
|
5
|
+
import { DebugAdapter } from "./debug.js";
|
|
6
|
+
import { isLinkActivationModifier } from "./hyperlink.js";
|
|
7
|
+
|
|
8
|
+
const SYNCHRONIZED_OUTPUT_TIMEOUT_MS = 1000;
|
|
9
|
+
const CPR = /^\x1b\[\d+;\d+R$/;
|
|
10
|
+
|
|
11
|
+
export interface WTermOptions {
|
|
12
|
+
cols?: number;
|
|
13
|
+
rows?: number;
|
|
14
|
+
/**
|
|
15
|
+
* A pre-constructed terminal core. When provided, `wasmUrl` is ignored and
|
|
16
|
+
* this core is used directly instead of loading the built-in Zig WASM binary.
|
|
17
|
+
*/
|
|
18
|
+
core?: TerminalCore;
|
|
19
|
+
wasmUrl?: string;
|
|
20
|
+
autoResize?: boolean;
|
|
21
|
+
cursorBlink?: boolean;
|
|
22
|
+
debug?: boolean;
|
|
23
|
+
onData?: (data: string) => void;
|
|
24
|
+
onTitle?: (title: string) => void;
|
|
25
|
+
onResize?: (cols: number, rows: number) => void;
|
|
26
|
+
/**
|
|
27
|
+
* Called after the renderer has painted a frame and before wterm applies
|
|
28
|
+
* its own scroll pinning, so a consumer can adjust the painted geometry --
|
|
29
|
+
* ending the scroller at the last written row, for one -- and have the pin
|
|
30
|
+
* read the corrected heights.
|
|
31
|
+
*/
|
|
32
|
+
onAfterPaint?: () => void;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export class WTerm {
|
|
36
|
+
element: HTMLElement;
|
|
37
|
+
cols: number;
|
|
38
|
+
rows: number;
|
|
39
|
+
bridge: TerminalCore | null = null;
|
|
40
|
+
autoResize: boolean;
|
|
41
|
+
debug: DebugAdapter | null = null;
|
|
42
|
+
|
|
43
|
+
private _coreOption: TerminalCore | undefined;
|
|
44
|
+
private wasmUrl: string | undefined;
|
|
45
|
+
private _debugEnabled: boolean;
|
|
46
|
+
private renderer: Renderer | null = null;
|
|
47
|
+
private input: InputHandler | null = null;
|
|
48
|
+
private rafId: number | null = null;
|
|
49
|
+
private _synchronizedOutputTimer: ReturnType<typeof setTimeout> | null = null;
|
|
50
|
+
private _synchronizedOutputState: "idle" | "held" | "passthrough" = "idle";
|
|
51
|
+
private _synchronizedOutputGeneration = 0;
|
|
52
|
+
private _rendererNeedsSetup = false;
|
|
53
|
+
private resizeObserver: ResizeObserver | null = null;
|
|
54
|
+
private _destroyed = false;
|
|
55
|
+
private _shouldScrollToBottom = false;
|
|
56
|
+
private _scrollbackDiscardedCount = 0;
|
|
57
|
+
private _programmaticScrollTop: number | null = null;
|
|
58
|
+
private _pendingResizeScrollTop: number | null = null;
|
|
59
|
+
private _rowHeight = 0;
|
|
60
|
+
private _charWidth = 0;
|
|
61
|
+
private _onClickFocus: (event: MouseEvent) => void;
|
|
62
|
+
private _onScroll: () => void;
|
|
63
|
+
private _onModifierChange: (event: KeyboardEvent) => void;
|
|
64
|
+
private _onWindowBlur: () => void;
|
|
65
|
+
|
|
66
|
+
onData: ((data: string) => void) | null;
|
|
67
|
+
onTitle: ((title: string) => void) | null;
|
|
68
|
+
onResize: ((cols: number, rows: number) => void) | null;
|
|
69
|
+
onAfterPaint: (() => void) | null;
|
|
70
|
+
|
|
71
|
+
private _container: HTMLDivElement;
|
|
72
|
+
|
|
73
|
+
constructor(element: HTMLElement, options: WTermOptions = {}) {
|
|
74
|
+
this.element = element;
|
|
75
|
+
this._coreOption = options.core;
|
|
76
|
+
this.wasmUrl = options.wasmUrl;
|
|
77
|
+
this.cols = options.cols || 80;
|
|
78
|
+
this.rows = options.rows || 24;
|
|
79
|
+
this.autoResize = options.autoResize !== false;
|
|
80
|
+
this._debugEnabled = options.debug ?? false;
|
|
81
|
+
|
|
82
|
+
this.onData = options.onData || null;
|
|
83
|
+
this.onTitle = options.onTitle || null;
|
|
84
|
+
this.onResize = options.onResize || null;
|
|
85
|
+
this.onAfterPaint = options.onAfterPaint || null;
|
|
86
|
+
|
|
87
|
+
this._container = document.createElement("div");
|
|
88
|
+
this._container.className = "term-grid";
|
|
89
|
+
this.element.appendChild(this._container);
|
|
90
|
+
this.element.classList.add("wterm");
|
|
91
|
+
if (options.cursorBlink) this.element.classList.add("cursor-blink");
|
|
92
|
+
|
|
93
|
+
this._onClickFocus = (event) => {
|
|
94
|
+
const target = event.target;
|
|
95
|
+
if (target instanceof Element && target.closest(".term-link")) {
|
|
96
|
+
if (
|
|
97
|
+
isLinkActivationModifier(
|
|
98
|
+
event,
|
|
99
|
+
this.element.ownerDocument.defaultView?.navigator ?? navigator,
|
|
100
|
+
) ||
|
|
101
|
+
event.detail === 0
|
|
102
|
+
) {
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
event.preventDefault();
|
|
106
|
+
}
|
|
107
|
+
const sel = window.getSelection();
|
|
108
|
+
if (!sel || sel.isCollapsed) this.input?.focus();
|
|
109
|
+
};
|
|
110
|
+
this.element.addEventListener("click", this._onClickFocus);
|
|
111
|
+
this._onModifierChange = (event) => {
|
|
112
|
+
this.element.classList.toggle(
|
|
113
|
+
"link-modifier-active",
|
|
114
|
+
isLinkActivationModifier(
|
|
115
|
+
event,
|
|
116
|
+
this.element.ownerDocument.defaultView?.navigator ?? navigator,
|
|
117
|
+
),
|
|
118
|
+
);
|
|
119
|
+
};
|
|
120
|
+
this._onWindowBlur = () => {
|
|
121
|
+
this.element.classList.remove("link-modifier-active");
|
|
122
|
+
};
|
|
123
|
+
this.element.ownerDocument.addEventListener(
|
|
124
|
+
"keydown",
|
|
125
|
+
this._onModifierChange,
|
|
126
|
+
);
|
|
127
|
+
this.element.ownerDocument.addEventListener(
|
|
128
|
+
"keyup",
|
|
129
|
+
this._onModifierChange,
|
|
130
|
+
);
|
|
131
|
+
this.element.ownerDocument.defaultView?.addEventListener(
|
|
132
|
+
"blur",
|
|
133
|
+
this._onWindowBlur,
|
|
134
|
+
);
|
|
135
|
+
this._onScroll = () => {
|
|
136
|
+
if (this._pendingResizeScrollTop !== null) return;
|
|
137
|
+
if (
|
|
138
|
+
this._programmaticScrollTop !== null &&
|
|
139
|
+
this.element.scrollTop === this._programmaticScrollTop
|
|
140
|
+
) {
|
|
141
|
+
this._programmaticScrollTop = null;
|
|
142
|
+
return;
|
|
143
|
+
}
|
|
144
|
+
this._programmaticScrollTop = null;
|
|
145
|
+
this._shouldScrollToBottom = false;
|
|
146
|
+
this._scheduleRender();
|
|
147
|
+
};
|
|
148
|
+
this.element.addEventListener("scroll", this._onScroll, { passive: true });
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
async init(): Promise<this> {
|
|
152
|
+
try {
|
|
153
|
+
if (this._coreOption) {
|
|
154
|
+
this.bridge = this._coreOption;
|
|
155
|
+
} else {
|
|
156
|
+
this.bridge = await WasmBridge.load(this.wasmUrl);
|
|
157
|
+
}
|
|
158
|
+
if (this._destroyed) return this;
|
|
159
|
+
this.bridge.init(this.cols, this.rows);
|
|
160
|
+
|
|
161
|
+
if (this._debugEnabled) {
|
|
162
|
+
this.debug = new DebugAdapter();
|
|
163
|
+
this.debug.setBridge(this.bridge);
|
|
164
|
+
(globalThis as Record<string, unknown>).__wterm = this;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
this._setRowHeight();
|
|
168
|
+
this._measureCharSize();
|
|
169
|
+
|
|
170
|
+
this.renderer = new Renderer(this._container);
|
|
171
|
+
this.renderer.setup(this.cols, this.rows);
|
|
172
|
+
|
|
173
|
+
this.input = new InputHandler(
|
|
174
|
+
this.element,
|
|
175
|
+
(data) => {
|
|
176
|
+
this._scrollToBottom();
|
|
177
|
+
if (this.onData) {
|
|
178
|
+
this.onData(data);
|
|
179
|
+
} else {
|
|
180
|
+
this.write(data);
|
|
181
|
+
}
|
|
182
|
+
},
|
|
183
|
+
() => this.bridge,
|
|
184
|
+
() =>
|
|
185
|
+
this._charWidth > 0 && this._rowHeight > 0
|
|
186
|
+
? { charWidth: this._charWidth, rowHeight: this._rowHeight }
|
|
187
|
+
: null,
|
|
188
|
+
);
|
|
189
|
+
|
|
190
|
+
if (this.autoResize) {
|
|
191
|
+
this._setupResizeObserver();
|
|
192
|
+
} else {
|
|
193
|
+
this._lockHeight();
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
this.input.focus();
|
|
197
|
+
this._initialRender();
|
|
198
|
+
} catch (err) {
|
|
199
|
+
this.destroy();
|
|
200
|
+
throw new Error(
|
|
201
|
+
`wterm: failed to initialize: ${err instanceof Error ? err.message : err}`,
|
|
202
|
+
);
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
return this;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
private _isScrolledToBottom(): boolean {
|
|
209
|
+
const el = this.element;
|
|
210
|
+
return el.scrollHeight - el.scrollTop - el.clientHeight < 5;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
/** Re-engage (or drop) follow-output. wterm's scroll handler lowers the
|
|
214
|
+
* follow latch on every scroll it does not recognize as its own,
|
|
215
|
+
* including the one that lands the reader at the live edge -- and with
|
|
216
|
+
* the latch down, rotation compensation at a saturated ring walks the box
|
|
217
|
+
* off the bottom a row at a time. A consumer that requires follow-output
|
|
218
|
+
* once its reader is back at the bottom calls this with true. */
|
|
219
|
+
followOutput(on: boolean): void {
|
|
220
|
+
this._shouldScrollToBottom = on;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
private _scrollToBottom(): void {
|
|
224
|
+
const el = this.element;
|
|
225
|
+
const maxScroll = el.scrollHeight - el.clientHeight;
|
|
226
|
+
if (maxScroll <= 0) {
|
|
227
|
+
this._setScrollTop(0);
|
|
228
|
+
return;
|
|
229
|
+
}
|
|
230
|
+
this._setScrollTop(maxScroll);
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
private _setScrollTop(value: number): void {
|
|
234
|
+
if (this.element.scrollTop === value) return;
|
|
235
|
+
this._programmaticScrollTop = value;
|
|
236
|
+
this.element.scrollTop = value;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
write(data: string | Uint8Array): void {
|
|
240
|
+
if (!this.bridge) return;
|
|
241
|
+
if (this.debug) this.debug.traceWrite(data);
|
|
242
|
+
this._shouldScrollToBottom = this._isScrolledToBottom();
|
|
243
|
+
let deliveryError: unknown;
|
|
244
|
+
let hasDeliveryError = false;
|
|
245
|
+
const drain = () => {
|
|
246
|
+
const result = this._drainResponses();
|
|
247
|
+
if (!hasDeliveryError && result.hasError) {
|
|
248
|
+
hasDeliveryError = true;
|
|
249
|
+
deliveryError = result.error;
|
|
250
|
+
}
|
|
251
|
+
};
|
|
252
|
+
if (typeof data === "string") {
|
|
253
|
+
this.bridge.writeString(data, drain);
|
|
254
|
+
} else {
|
|
255
|
+
this.bridge.writeRaw(data, drain);
|
|
256
|
+
}
|
|
257
|
+
const synchronized = this.bridge.synchronizedOutput?.() ?? false;
|
|
258
|
+
const generation = this.bridge.synchronizedOutputGeneration?.() ?? 0;
|
|
259
|
+
this._updateSynchronizedOutput(synchronized, generation);
|
|
260
|
+
if (this._synchronizedOutputState !== "held") {
|
|
261
|
+
this._setupRendererIfNeeded();
|
|
262
|
+
this._scheduleRender();
|
|
263
|
+
}
|
|
264
|
+
drain();
|
|
265
|
+
if (hasDeliveryError) throw deliveryError;
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
resize(cols: number, rows: number): void {
|
|
269
|
+
if (!this.bridge) return;
|
|
270
|
+
this._shouldScrollToBottom =
|
|
271
|
+
this._pendingResizeScrollTop === null && this._isScrolledToBottom();
|
|
272
|
+
this.cols = cols;
|
|
273
|
+
this.rows = rows;
|
|
274
|
+
this.bridge.resize(cols, rows);
|
|
275
|
+
const synchronized = this.bridge.synchronizedOutput?.() ?? false;
|
|
276
|
+
const generation = this.bridge.synchronizedOutputGeneration?.() ?? 0;
|
|
277
|
+
if (this._updateSynchronizedOutput(synchronized, generation)) {
|
|
278
|
+
this._rendererNeedsSetup = true;
|
|
279
|
+
} else {
|
|
280
|
+
this._setupRenderer(cols, rows);
|
|
281
|
+
this._scheduleRender();
|
|
282
|
+
}
|
|
283
|
+
if (this.onResize) this.onResize(cols, rows);
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
/** Re-measure cell geometry after the host changes its terminal font. */
|
|
287
|
+
refreshCellSize(): void {
|
|
288
|
+
this._measureCharSize();
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
focus(): void {
|
|
292
|
+
if (this.input) {
|
|
293
|
+
this.input.focus();
|
|
294
|
+
} else {
|
|
295
|
+
this.element.focus();
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
private _scheduleRender(): void {
|
|
300
|
+
if (this.rafId != null) return;
|
|
301
|
+
this.rafId = requestAnimationFrame(() => {
|
|
302
|
+
this.rafId = null;
|
|
303
|
+
this._doRender();
|
|
304
|
+
});
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
private _cancelScheduledRender(): void {
|
|
308
|
+
if (this.rafId != null) {
|
|
309
|
+
cancelAnimationFrame(this.rafId);
|
|
310
|
+
this.rafId = null;
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
private _updateSynchronizedOutput(
|
|
315
|
+
synchronized: boolean,
|
|
316
|
+
generation: number,
|
|
317
|
+
): boolean {
|
|
318
|
+
if (!synchronized) {
|
|
319
|
+
if (this._synchronizedOutputState === "held") {
|
|
320
|
+
this._cancelSynchronizedOutputFallback();
|
|
321
|
+
}
|
|
322
|
+
this._synchronizedOutputState = "idle";
|
|
323
|
+
return false;
|
|
324
|
+
}
|
|
325
|
+
if (
|
|
326
|
+
this._synchronizedOutputState === "held" &&
|
|
327
|
+
generation !== this._synchronizedOutputGeneration
|
|
328
|
+
) {
|
|
329
|
+
this._armSynchronizedOutputFallback(generation);
|
|
330
|
+
return true;
|
|
331
|
+
} else if (
|
|
332
|
+
this._synchronizedOutputState === "passthrough" &&
|
|
333
|
+
generation !== this._synchronizedOutputGeneration
|
|
334
|
+
) {
|
|
335
|
+
this._synchronizedOutputState = "idle";
|
|
336
|
+
}
|
|
337
|
+
if (this._synchronizedOutputState !== "idle") {
|
|
338
|
+
return this._synchronizedOutputState === "held";
|
|
339
|
+
}
|
|
340
|
+
this._synchronizedOutputState = "held";
|
|
341
|
+
this._cancelScheduledRender();
|
|
342
|
+
this._armSynchronizedOutputFallback(generation);
|
|
343
|
+
return true;
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
private _armSynchronizedOutputFallback(generation: number): void {
|
|
347
|
+
this._cancelSynchronizedOutputFallback();
|
|
348
|
+
this._synchronizedOutputGeneration = generation;
|
|
349
|
+
this._synchronizedOutputTimer = setTimeout(() => {
|
|
350
|
+
if (
|
|
351
|
+
this._synchronizedOutputState !== "held" ||
|
|
352
|
+
this._synchronizedOutputGeneration !== generation
|
|
353
|
+
) {
|
|
354
|
+
return;
|
|
355
|
+
}
|
|
356
|
+
this._synchronizedOutputTimer = null;
|
|
357
|
+
this._synchronizedOutputState = "passthrough";
|
|
358
|
+
this._setupRendererIfNeeded();
|
|
359
|
+
this._cancelScheduledRender();
|
|
360
|
+
this._doRender();
|
|
361
|
+
}, SYNCHRONIZED_OUTPUT_TIMEOUT_MS);
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
private _cancelSynchronizedOutputFallback(): void {
|
|
365
|
+
if (this._synchronizedOutputTimer == null) return;
|
|
366
|
+
clearTimeout(this._synchronizedOutputTimer);
|
|
367
|
+
this._synchronizedOutputTimer = null;
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
private _setupRendererIfNeeded(): void {
|
|
371
|
+
if (!this._rendererNeedsSetup) return;
|
|
372
|
+
this._setupRenderer(this.cols, this.rows);
|
|
373
|
+
this._rendererNeedsSetup = false;
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
private _setupRenderer(cols: number, rows: number): void {
|
|
377
|
+
if (!this._shouldScrollToBottom && this._pendingResizeScrollTop === null) {
|
|
378
|
+
this._pendingResizeScrollTop = this.element.scrollTop;
|
|
379
|
+
}
|
|
380
|
+
this.renderer?.setup(cols, rows);
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
private _initialRender(): void {
|
|
384
|
+
this._doRender();
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
private _doRender(): void {
|
|
388
|
+
if (!this.bridge || !this.renderer) return;
|
|
389
|
+
|
|
390
|
+
// The follow latch was decided when the bytes arrived, but the render
|
|
391
|
+
// runs a frame later: a reader who scrolled up in between must not be
|
|
392
|
+
// snapped back down by a decision their scroll can no longer reach (the
|
|
393
|
+
// queued render can beat the scroll event's dispatch). Re-decide from
|
|
394
|
+
// where the box actually is.
|
|
395
|
+
if (this._shouldScrollToBottom && !this._isScrolledToBottom()) {
|
|
396
|
+
this._shouldScrollToBottom = false;
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
let dirtyCount = 0;
|
|
400
|
+
const t0 = this.debug ? performance.now() : 0;
|
|
401
|
+
if (this.debug) {
|
|
402
|
+
for (let r = 0; r < this.rows; r++) {
|
|
403
|
+
if (this.bridge.isDirtyRow(r)) dirtyCount++;
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
const rowHeight = this._rowHeight || 17;
|
|
408
|
+
const scrollbackCount = this.bridge.getScrollbackCount();
|
|
409
|
+
const discardedCount = this.bridge.getScrollbackDiscardedCount?.();
|
|
410
|
+
const discardedDelta =
|
|
411
|
+
discardedCount !== undefined &&
|
|
412
|
+
discardedCount >= this._scrollbackDiscardedCount
|
|
413
|
+
? discardedCount - this._scrollbackDiscardedCount
|
|
414
|
+
: 0;
|
|
415
|
+
if (discardedCount !== undefined) {
|
|
416
|
+
this._scrollbackDiscardedCount = discardedCount;
|
|
417
|
+
}
|
|
418
|
+
let scrollTop =
|
|
419
|
+
this._pendingResizeScrollTop !== null
|
|
420
|
+
? this._pendingResizeScrollTop
|
|
421
|
+
: this.element.scrollTop;
|
|
422
|
+
if (!this._shouldScrollToBottom && discardedDelta > 0) {
|
|
423
|
+
scrollTop = Math.max(0, scrollTop - discardedDelta * rowHeight);
|
|
424
|
+
if (this._pendingResizeScrollTop !== null) {
|
|
425
|
+
this._pendingResizeScrollTop = scrollTop;
|
|
426
|
+
} else {
|
|
427
|
+
this._setScrollTop(scrollTop);
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
this.renderer.render(this.bridge, {
|
|
432
|
+
// Following output, the box is about to be pinned to a bottom this
|
|
433
|
+
// render has yet to lay out. Window the history as if no grid row
|
|
434
|
+
// showed: a consumer may hide grid rows in onAfterPaint, and the view
|
|
435
|
+
// then reaches that much further up into history.
|
|
436
|
+
scrollTop: this._shouldScrollToBottom
|
|
437
|
+
? Math.max(
|
|
438
|
+
0,
|
|
439
|
+
scrollbackCount * rowHeight - this.element.clientHeight,
|
|
440
|
+
)
|
|
441
|
+
: scrollTop,
|
|
442
|
+
clientHeight: this.element.clientHeight,
|
|
443
|
+
rowHeight,
|
|
444
|
+
scrollbackDiscardedCount: discardedCount,
|
|
445
|
+
});
|
|
446
|
+
|
|
447
|
+
// Between the paint and the pin below: the hook may change the painted
|
|
448
|
+
// geometry, and the pin must read what it decided.
|
|
449
|
+
if (this.onAfterPaint) this.onAfterPaint();
|
|
450
|
+
|
|
451
|
+
if (this.debug) {
|
|
452
|
+
this.debug.recordRender(performance.now() - t0, dirtyCount);
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
const hasScrollback = scrollbackCount > 0;
|
|
456
|
+
this.element.classList.toggle("has-scrollback", hasScrollback);
|
|
457
|
+
|
|
458
|
+
if (this._shouldScrollToBottom) {
|
|
459
|
+
this._scrollToBottom();
|
|
460
|
+
} else if (this._pendingResizeScrollTop !== null) {
|
|
461
|
+
const pendingScrollTop = this._pendingResizeScrollTop;
|
|
462
|
+
this._pendingResizeScrollTop = null;
|
|
463
|
+
this._setScrollTop(pendingScrollTop);
|
|
464
|
+
} else if (!hasScrollback && this.element.scrollTop !== 0) {
|
|
465
|
+
this.element.scrollTop = 0;
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
const title = this.bridge.getTitle();
|
|
469
|
+
if (title !== null && this.onTitle) {
|
|
470
|
+
this.onTitle(title);
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
this._drainResponses();
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
private _drainResponses(): { hasError: boolean; error?: unknown } {
|
|
477
|
+
if (!this.bridge) return { hasError: false };
|
|
478
|
+
let response: string | null;
|
|
479
|
+
let firstError: unknown;
|
|
480
|
+
let hasError = false;
|
|
481
|
+
while ((response = this.bridge.getResponse()) !== null) {
|
|
482
|
+
// Only cursor-position reports reach the PTY. The core also answers
|
|
483
|
+
// capability queries (kitty keyboard, DA1, OSC 10/11, DECRQM), and pi
|
|
484
|
+
// would act on those: a kitty reply puts it in a key mode the input
|
|
485
|
+
// path does not encode for, and a viewer is not always attached to
|
|
486
|
+
// answer, so pi's mode would depend on whether one was.
|
|
487
|
+
if (!CPR.test(response)) continue;
|
|
488
|
+
try {
|
|
489
|
+
if (this.onData) this.onData(response);
|
|
490
|
+
} catch (error) {
|
|
491
|
+
if (!hasError) {
|
|
492
|
+
hasError = true;
|
|
493
|
+
firstError = error;
|
|
494
|
+
}
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
return { hasError, error: firstError };
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
private _lockHeight(): void {
|
|
501
|
+
const rh = this._rowHeight || 17;
|
|
502
|
+
const gridHeight = this.rows * rh;
|
|
503
|
+
const cs = getComputedStyle(this.element);
|
|
504
|
+
let extra =
|
|
505
|
+
(parseFloat(cs.paddingTop) || 0) + (parseFloat(cs.paddingBottom) || 0);
|
|
506
|
+
if (cs.boxSizing === "border-box") {
|
|
507
|
+
extra +=
|
|
508
|
+
(parseFloat(cs.borderTopWidth) || 0) +
|
|
509
|
+
(parseFloat(cs.borderBottomWidth) || 0);
|
|
510
|
+
}
|
|
511
|
+
this.element.style.height = `${gridHeight + extra}px`;
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
private _setRowHeight(): void {
|
|
515
|
+
const probe = document.createElement("div");
|
|
516
|
+
probe.className = "term-row";
|
|
517
|
+
probe.style.visibility = "hidden";
|
|
518
|
+
probe.style.position = "absolute";
|
|
519
|
+
probe.textContent = "W";
|
|
520
|
+
this._container.appendChild(probe);
|
|
521
|
+
const h = probe.getBoundingClientRect().height;
|
|
522
|
+
probe.remove();
|
|
523
|
+
if (h > 0) {
|
|
524
|
+
const rh = Math.ceil(h);
|
|
525
|
+
this._rowHeight = rh;
|
|
526
|
+
this.element.style.setProperty("--term-row-height", `${rh}px`);
|
|
527
|
+
}
|
|
528
|
+
}
|
|
529
|
+
|
|
530
|
+
private _measureCharSize(): {
|
|
531
|
+
charWidth: number;
|
|
532
|
+
rowHeight: number;
|
|
533
|
+
} | null {
|
|
534
|
+
const row = document.createElement("div");
|
|
535
|
+
row.className = "term-row";
|
|
536
|
+
row.style.visibility = "hidden";
|
|
537
|
+
row.style.position = "absolute";
|
|
538
|
+
|
|
539
|
+
const probe = document.createElement("span");
|
|
540
|
+
probe.textContent = "W";
|
|
541
|
+
row.appendChild(probe);
|
|
542
|
+
|
|
543
|
+
this._container.appendChild(row);
|
|
544
|
+
const charWidth = probe.getBoundingClientRect().width;
|
|
545
|
+
const rowHeight = row.getBoundingClientRect().height;
|
|
546
|
+
row.remove();
|
|
547
|
+
|
|
548
|
+
if (charWidth === 0 || rowHeight === 0) return null;
|
|
549
|
+
this._charWidth = charWidth;
|
|
550
|
+
this._rowHeight = rowHeight;
|
|
551
|
+
return { charWidth, rowHeight };
|
|
552
|
+
}
|
|
553
|
+
|
|
554
|
+
private _setupResizeObserver(): void {
|
|
555
|
+
const initial = this._measureCharSize();
|
|
556
|
+
if (!initial) return;
|
|
557
|
+
|
|
558
|
+
let { charWidth, rowHeight } = initial;
|
|
559
|
+
|
|
560
|
+
this.resizeObserver = new ResizeObserver((entries) => {
|
|
561
|
+
const measured = this._measureCharSize();
|
|
562
|
+
if (measured) {
|
|
563
|
+
charWidth = measured.charWidth;
|
|
564
|
+
rowHeight = measured.rowHeight;
|
|
565
|
+
}
|
|
566
|
+
|
|
567
|
+
for (const entry of entries) {
|
|
568
|
+
const { width, height } = entry.contentRect;
|
|
569
|
+
const newCols = Math.max(1, Math.floor(width / charWidth));
|
|
570
|
+
const newRows = Math.max(1, Math.floor(height / rowHeight));
|
|
571
|
+
if (newCols !== this.cols || newRows !== this.rows) {
|
|
572
|
+
this.resize(newCols, newRows);
|
|
573
|
+
}
|
|
574
|
+
}
|
|
575
|
+
});
|
|
576
|
+
this.resizeObserver.observe(this.element);
|
|
577
|
+
}
|
|
578
|
+
|
|
579
|
+
destroy(): void {
|
|
580
|
+
this._destroyed = true;
|
|
581
|
+
this._cancelScheduledRender();
|
|
582
|
+
this._cancelSynchronizedOutputFallback();
|
|
583
|
+
if (this.resizeObserver) this.resizeObserver.disconnect();
|
|
584
|
+
if (this.input) this.input.destroy();
|
|
585
|
+
this.element.removeEventListener("click", this._onClickFocus);
|
|
586
|
+
this.element.removeEventListener("scroll", this._onScroll);
|
|
587
|
+
this.element.ownerDocument.removeEventListener(
|
|
588
|
+
"keydown",
|
|
589
|
+
this._onModifierChange,
|
|
590
|
+
);
|
|
591
|
+
this.element.ownerDocument.removeEventListener(
|
|
592
|
+
"keyup",
|
|
593
|
+
this._onModifierChange,
|
|
594
|
+
);
|
|
595
|
+
this.element.ownerDocument.defaultView?.removeEventListener(
|
|
596
|
+
"blur",
|
|
597
|
+
this._onWindowBlur,
|
|
598
|
+
);
|
|
599
|
+
this.element.classList.remove("link-modifier-active");
|
|
600
|
+
this.element.innerHTML = "";
|
|
601
|
+
if (
|
|
602
|
+
this.debug &&
|
|
603
|
+
(globalThis as Record<string, unknown>).__wterm === this
|
|
604
|
+
) {
|
|
605
|
+
delete (globalThis as Record<string, unknown>).__wterm;
|
|
606
|
+
}
|
|
607
|
+
this.debug = null;
|
|
608
|
+
}
|
|
609
|
+
}
|