Skip to content

Commit dea883a

Browse files
cangzhangtsl0922
authored andcommitted
html: allow overriding client options with URL query
change: overwrite options from url query params rm console.log add support for xterm options
1 parent ed551d4 commit dea883a

File tree

4 files changed

+5097
-5035
lines changed

4 files changed

+5097
-5035
lines changed

html/src/components/app.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { h, Component } from 'preact';
22

3-
import { ITerminalOptions, ITheme } from '@xterm/xterm';
4-
import { ClientOptions, FlowControl } from './terminal/xterm';
53
import { Terminal } from './terminal';
64

5+
import type { ITerminalOptions, ITheme } from '@xterm/xterm';
6+
import type { ClientOptions, FlowControl } from './terminal/xterm';
7+
78
const protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:';
89
const path = window.location.pathname.replace(/[/]+$/, '');
910
const wsUrl = [protocol, '//', window.location.host, path, '/ws', window.location.search].join('');

html/src/components/terminal/xterm/index.ts

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { bind } from 'decko';
2-
import { IDisposable, ITerminalOptions, Terminal } from '@xterm/xterm';
2+
import type { IDisposable, ITerminalOptions } from '@xterm/xterm';
3+
import { Terminal } from '@xterm/xterm';
34
import { CanvasAddon } from '@xterm/addon-canvas';
45
import { WebglAddon } from '@xterm/addon-webgl';
56
import { FitAddon } from '@xterm/addon-fit';
@@ -21,7 +22,7 @@ declare global {
2122
}
2223
}
2324

24-
const enum Command {
25+
enum Command {
2526
// server side
2627
OUTPUT = '0',
2728
SET_WINDOW_TITLE = '1',
@@ -296,6 +297,40 @@ export class Xterm {
296297
}
297298
}
298299

300+
@bind
301+
private parseOptsFromUrlQuery(query: string): Preferences {
302+
const { terminal } = this;
303+
const { clientOptions } = this.options;
304+
const prefs = {} as Preferences;
305+
const queryObj = Array.from(new URLSearchParams(query) as unknown as Iterable<[string, string]>);
306+
307+
for (const [k, queryVal] of queryObj) {
308+
let v = clientOptions[k];
309+
if (v === undefined) v = terminal.options[k];
310+
switch (typeof v) {
311+
case 'boolean':
312+
prefs[k] = queryVal === 'true' || queryVal === '1';
313+
break;
314+
case 'number':
315+
case 'bigint':
316+
prefs[k] = Number.parseInt(queryVal, 10);
317+
break;
318+
case 'string':
319+
prefs[k] = queryVal;
320+
break;
321+
case 'object':
322+
prefs[k] = JSON.parse(queryVal);
323+
break;
324+
default:
325+
console.warn(`[ttyd] maybe unknown option: ${k}=${queryVal}, treating as string`);
326+
prefs[k] = queryVal;
327+
break;
328+
}
329+
}
330+
331+
return prefs;
332+
}
333+
299334
@bind
300335
private onSocketData(event: MessageEvent) {
301336
const { textDecoder } = this;
@@ -315,6 +350,7 @@ export class Xterm {
315350
this.applyPreferences({
316351
...this.options.clientOptions,
317352
...JSON.parse(textDecoder.decode(data)),
353+
...this.parseOptsFromUrlQuery(window.location.search),
318354
} as Preferences);
319355
break;
320356
default:
@@ -339,8 +375,8 @@ export class Xterm {
339375
this.writeFunc = data => this.zmodemAddon?.consume(data);
340376
terminal.loadAddon(register(this.zmodemAddon));
341377
}
342-
Object.keys(prefs).forEach(key => {
343-
const value = prefs[key];
378+
379+
for (const [key, value] of Object.entries(prefs)) {
344380
switch (key) {
345381
case 'rendererType':
346382
this.setRendererType(value);
@@ -413,7 +449,7 @@ export class Xterm {
413449
if (key.indexOf('font') === 0) fitAddon.fit();
414450
break;
415451
}
416-
});
452+
}
417453
}
418454

419455
@bind

html/tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"declaration": false,
1111
"experimentalDecorators": true,
1212
"strictPropertyInitialization": false,
13+
"lib": ["es2019", "dom"],
1314
},
1415
"include": [
1516
"src/**/*.tsx",

0 commit comments

Comments
 (0)