Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
cangzhang committed Mar 25, 2024
1 parent 8f1cafd commit 40ff592
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 16 deletions.
2 changes: 1 addition & 1 deletion html/src/components/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,4 @@ export class App extends Component {
/>
);
}
}
}
21 changes: 15 additions & 6 deletions html/src/components/terminal/xterm/helper.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
export function extractQueryParams(param: string) {
return new URLSearchParams(window.location.search).get(param) as string;
}
import type { ClientOptions } from '.';

export const BOOLEAN_CLIENT_OPTION_PROPS: Array<keyof ClientOptions> = [
'disableLeaveAlert',
'disableResizeOverlay',
'enableZmodem',
'enableTrzsz',
'enableSixel',
'rendererType',
];

export const STRING_CLIENT_OPTION_PROPS: Array<keyof ClientOptions> = ['rendererType', 'unicodeVersion'];

const TRUE_PARAMS = ['true', '1'];
export const CLIENT_OPTION_PROPS = [...BOOLEAN_CLIENT_OPTION_PROPS, ...STRING_CLIENT_OPTION_PROPS];

export function isQueryOptionEnabled(param: string): boolean {
return TRUE_PARAMS.includes(extractQueryParams(param));
export function extractQueryParams(param: keyof ClientOptions) {
return new URLSearchParams(window.location.search).get(param) as ClientOptions[typeof param];
}
32 changes: 23 additions & 9 deletions html/src/components/terminal/xterm/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { bind } from 'decko';
import { IDisposable, ITerminalOptions, Terminal } from '@xterm/xterm';
import type { IDisposable, ITerminalOptions } from '@xterm/xterm';
import { Terminal } from '@xterm/xterm';
import { CanvasAddon } from '@xterm/addon-canvas';
import { WebglAddon } from '@xterm/addon-webgl';
import { FitAddon } from '@xterm/addon-fit';
Expand All @@ -8,7 +9,12 @@ import { ImageAddon } from '@xterm/addon-image';
import { Unicode11Addon } from '@xterm/addon-unicode11';
import { OverlayAddon } from './addons/overlay';
import { ZmodemAddon } from './addons/zmodem';
import { isQueryOptionEnabled } from './helper';
import {
BOOLEAN_CLIENT_OPTION_PROPS,
CLIENT_OPTION_PROPS,
STRING_CLIENT_OPTION_PROPS,
extractQueryParams,
} from './helper';

import '@xterm/xterm/css/xterm.css';

Expand All @@ -22,7 +28,7 @@ declare global {
}
}

const enum Command {
enum Command {
// server side
OUTPUT = '0',
SET_WINDOW_TITLE = '1',
Expand Down Expand Up @@ -340,11 +346,19 @@ export class Xterm {
this.writeFunc = data => this.zmodemAddon?.consume(data);
terminal.loadAddon(register(this.zmodemAddon));
}
const finalPrefs = {
...prefs,
disableLeaveAlert: isQueryOptionEnabled('disableLeaveAlert'),
};
Object.entries(finalPrefs).forEach(([key, value]) => {
const finalPrefs = { ...prefs };
for (const k of CLIENT_OPTION_PROPS) {
const queryVal = extractQueryParams(k);
if (queryVal !== null) {
if (BOOLEAN_CLIENT_OPTION_PROPS.includes(k)) {
(finalPrefs[k] as boolean) = queryVal === 'true' || queryVal === '1';
} else if (STRING_CLIENT_OPTION_PROPS.includes(k)) {
(finalPrefs[k] as string) = queryVal as string;
}
}
}

for (const [key, value] of Object.entries(finalPrefs)) {
switch (key) {
case 'rendererType':
this.setRendererType(value);
Expand Down Expand Up @@ -417,7 +431,7 @@ export class Xterm {
if (key.indexOf('font') === 0) fitAddon.fit();
break;
}
});
}
}

@bind
Expand Down

0 comments on commit 40ff592

Please sign in to comment.