Skip to content

Commit c375696

Browse files
committed
Make demo use strict TS compiler mode
1 parent 8eed7b4 commit c375696

13 files changed

Lines changed: 160 additions & 154 deletions

demo/client/client.ts

Lines changed: 70 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,11 @@ export interface IWindowWithTerminal extends Window {
5858
}
5959
declare let window: IWindowWithTerminal;
6060

61-
let term;
62-
let protocol;
63-
let socketURL;
64-
let socket;
65-
let pid;
61+
let term: Terminal | null;
62+
let protocol: string;
63+
let socketURL: string;
64+
let socket: WebSocket | null;
65+
let pid: string;
6666
let controlBar: ControlBar;
6767
let addonsWindow: AddonsWindow;
6868
let addonSearchWindow: AddonSearchWindow;
@@ -73,7 +73,7 @@ const addons: AddonCollection = {
7373
attach: { name: 'attach', ctor: AttachAddon, canChange: false },
7474
clipboard: { name: 'clipboard', ctor: ClipboardAddon, canChange: true },
7575
fit: { name: 'fit', ctor: FitAddon, canChange: false },
76-
image: { name: 'image', ctor: ImageAddon, canChange: true },
76+
image: { name: 'image', ctor: ImageAddon!, canChange: true },
7777
progress: { name: 'progress', ctor: ProgressAddon, canChange: true },
7878
search: { name: 'search', ctor: SearchAddon, canChange: true },
7979
serialize: { name: 'serialize', ctor: SerializeAddon, canChange: true },
@@ -116,8 +116,8 @@ const xtermjsTheme = {
116116
brightWhite: '#FFFFFF'
117117
} satisfies ITheme;
118118
function setPadding(): void {
119-
term.element.style.padding = parseInt(paddingElement.value, 10).toString() + 'px';
120-
addons.fit.instance.fit();
119+
term!.element!.style.padding = parseInt(paddingElement.value, 10).toString() + 'px';
120+
addons.fit.instance!.fit();
121121
}
122122

123123
function getSearchOptions(): ISearchOptions {
@@ -141,7 +141,7 @@ const disposeRecreateButtonHandler: () => void = () => {
141141
if (term) {
142142
term.dispose();
143143
term = null;
144-
window.term = null;
144+
(window as any).term = null;
145145
socket = null;
146146
addons.attach.instance = undefined;
147147
addons.clipboard.instance = undefined;
@@ -154,18 +154,18 @@ const disposeRecreateButtonHandler: () => void = () => {
154154
addons.ligatures.instance = undefined;
155155
addons.webLinks.instance = undefined;
156156
addons.webgl.instance = undefined;
157-
document.getElementById('dispose').innerHTML = 'Recreate Terminal';
157+
document.getElementById('dispose')!.innerHTML = 'Recreate Terminal';
158158
} else {
159159
createTerminal();
160-
document.getElementById('dispose').innerHTML = 'Dispose terminal';
160+
document.getElementById('dispose')!.innerHTML = 'Dispose terminal';
161161
}
162162
};
163163

164164
const createNewWindowButtonHandler: () => void = () => {
165165
if (term) {
166166
disposeRecreateButtonHandler();
167167
}
168-
const win = window.open();
168+
const win = window.open()!;
169169
terminalContainer = win.document.createElement('div');
170170
terminalContainer.id = 'terminal-container';
171171
win.document.body.appendChild(terminalContainer);
@@ -207,7 +207,7 @@ if (document.location.pathname === '/test') {
207207
} else {
208208
const typedTerm = createTerminal();
209209

210-
controlBar = new ControlBar(document.getElementById('sidebar'), document.querySelector('.banner-tabs'), []);
210+
controlBar = new ControlBar(document.getElementById('sidebar')!, document.querySelector('.banner-tabs')!, []);
211211
optionsWindow = controlBar.registerWindow(new OptionsWindow(typedTerm, addons, { updateTerminalSize, updateTerminalContainerBackground }));
212212
const styleWindow = controlBar.registerWindow(new StyleWindow(typedTerm, addons));
213213
controlBar.registerWindow(new CellInspectorWindow(typedTerm, addons));
@@ -234,43 +234,43 @@ if (document.location.pathname === '/test') {
234234
controlBar.setTabVisible('addon-serialize', true);
235235
controlBar.setTabVisible('addon-image', true);
236236
controlBar.setTabVisible('addon-web-fonts', true);
237-
addonWebglWindow.setTextureAtlas(addons.webgl.instance.textureAtlas);
238-
addons.webgl.instance.onChangeTextureAtlas(e => addonWebglWindow.setTextureAtlas(e));
239-
addons.webgl.instance.onAddTextureAtlasCanvas(e => addonWebglWindow.appendTextureAtlas(e));
240-
addons.webgl.instance.onRemoveTextureAtlasCanvas(e => addonWebglWindow.removeTextureAtlas(e));
237+
addonWebglWindow.setTextureAtlas(addons.webgl.instance!.textureAtlas!);
238+
addons.webgl.instance!.onChangeTextureAtlas(e => addonWebglWindow.setTextureAtlas(e));
239+
addons.webgl.instance!.onAddTextureAtlasCanvas(e => addonWebglWindow.appendTextureAtlas(e));
240+
addons.webgl.instance!.onRemoveTextureAtlasCanvas(e => addonWebglWindow.removeTextureAtlas(e));
241241

242242
paddingElement.value = '0';
243243
addDomListener(paddingElement, 'change', setPadding);
244244
addDomListener(actionElements.findNext, 'keydown', (e) => {
245245
if (e.key === 'Enter') {
246-
addons.search.instance.findNext(actionElements.findNext.value, getSearchOptions());
246+
addons.search.instance!.findNext(actionElements.findNext.value, getSearchOptions());
247247
e.preventDefault();
248248
}
249249
});
250250
addDomListener(actionElements.findNext, 'input', (e) => {
251-
addons.search.instance.findNext(actionElements.findNext.value, getSearchOptions());
251+
addons.search.instance!.findNext(actionElements.findNext.value, getSearchOptions());
252252
});
253253
addDomListener(actionElements.findPrevious, 'keydown', (e) => {
254254
if (e.key === 'Enter') {
255-
addons.search.instance.findPrevious(actionElements.findPrevious.value, getSearchOptions());
255+
addons.search.instance!.findPrevious(actionElements.findPrevious.value, getSearchOptions());
256256
e.preventDefault();
257257
}
258258
});
259259
addDomListener(actionElements.findPrevious, 'input', (e) => {
260-
addons.search.instance.findPrevious(actionElements.findPrevious.value, getSearchOptions());
260+
addons.search.instance!.findPrevious(actionElements.findPrevious.value, getSearchOptions());
261261
});
262262
addDomListener(actionElements.findNext, 'blur', (e) => {
263-
addons.search.instance.clearActiveDecoration();
263+
addons.search.instance!.clearActiveDecoration();
264264
});
265265
addDomListener(actionElements.findPrevious, 'blur', (e) => {
266-
addons.search.instance.clearActiveDecoration();
266+
addons.search.instance!.clearActiveDecoration();
267267
});
268268
}
269269

270270
function createTerminal(): Terminal {
271271
// Clean terminal
272-
while (terminalContainer.children.length) {
273-
terminalContainer.removeChild(terminalContainer.children[0]);
272+
while (terminalContainer!.children.length) {
273+
terminalContainer!.removeChild(terminalContainer!.children[0]);
274274
}
275275

276276
const isWindows = ['Windows', 'Win16', 'Win32', 'WinCE'].indexOf(navigator.platform) >= 0;
@@ -290,7 +290,7 @@ function createTerminal(): Terminal {
290290
addons.search.instance = new SearchAddon();
291291
addons.serialize.instance = new SerializeAddon();
292292
addons.fit.instance = new FitAddon();
293-
addons.image.instance = new ImageAddon();
293+
addons.image.instance = new ImageAddon!();
294294
addons.progress.instance = new ProgressAddon();
295295
addons.unicodeGraphemes.instance = new UnicodeGraphemesAddon();
296296
addons.clipboard.instance = new ClipboardAddon();
@@ -311,7 +311,7 @@ function createTerminal(): Terminal {
311311
typedTerm.loadAddon(addons.webFonts.instance);
312312
typedTerm.loadAddon(addons.clipboard.instance);
313313

314-
window.term = term; // Expose `term` to window for debugging purposes
314+
(window as any).term = term; // Expose `term` to window for debugging purposes
315315
term.onResize((size: { cols: number, rows: number }) => {
316316
if (!pid) {
317317
return;
@@ -330,7 +330,7 @@ function createTerminal(): Terminal {
330330
if (addons.webgl.instance) {
331331
try {
332332
typedTerm.loadAddon(addons.webgl.instance);
333-
term.open(terminalContainer);
333+
term.open(terminalContainer!);
334334
} catch (e) {
335335
console.warn('error during loading webgl addon:', e);
336336
addons.webgl.instance.dispose();
@@ -339,7 +339,7 @@ function createTerminal(): Terminal {
339339
}
340340
if (!typedTerm.element) {
341341
// webgl loading failed for some reason, attach with DOM renderer
342-
term.open(terminalContainer);
342+
term.open(terminalContainer!);
343343
}
344344

345345
term.focus();
@@ -349,13 +349,13 @@ function createTerminal(): Terminal {
349349
if (optionsWindow.autoResize) {
350350
// In general this should be debounced to avoid excessive work on the main
351351
// thread by firing the expensive resize action repeatedly
352-
addons.fit.instance.fit();
352+
addons.fit.instance!.fit();
353353
}
354354
});
355-
resizeObserver.observe(terminalContainer);
355+
resizeObserver.observe(terminalContainer!);
356356

357357
window.addEventListener('resize', () => {
358-
terminalContainer.style.width = document.body.clientWidth + 'px';
358+
terminalContainer!.style.width = document.body.clientWidth + 'px';
359359
});
360360

361361
// fit is called within a setTimeout, cols and rows need this.
@@ -369,7 +369,7 @@ function createTerminal(): Terminal {
369369
if (useRealTerminal instanceof HTMLInputElement && !useRealTerminal.checked) {
370370
runFakeTerminal();
371371
} else {
372-
const res = await fetch('/terminals?cols=' + term.cols + '&rows=' + term.rows, { method: 'POST' });
372+
const res = await fetch('/terminals?cols=' + term!.cols + '&rows=' + term!.rows, { method: 'POST' });
373373
const processId = await res.text();
374374
pid = processId;
375375
socketURL += processId;
@@ -384,52 +384,52 @@ function createTerminal(): Terminal {
384384
}
385385

386386
function runRealTerminal(): void {
387-
addons.attach.instance = new AttachAddon(socket);
388-
term.loadAddon(addons.attach.instance);
389-
term._initialized = true;
390-
initAddons(term);
387+
addons.attach.instance = new AttachAddon(socket!);
388+
term!.loadAddon(addons.attach.instance);
389+
(term as any)._initialized = true;
390+
initAddons(term!);
391391
}
392392

393393
function runFakeTerminal(): void {
394-
if (term._initialized) {
394+
if ((term as any)._initialized) {
395395
return;
396396
}
397397

398-
term._initialized = true;
399-
initAddons(term);
398+
(term as any)._initialized = true;
399+
initAddons(term!);
400400

401-
term.prompt = () => {
402-
term.write('\r\n$ ');
401+
(term as any).prompt = () => {
402+
term!.write('\r\n$ ');
403403
};
404404

405-
term.writeln('Welcome to xterm.js');
406-
term.writeln('This is a local terminal emulation, without a real terminal in the back-end.');
407-
term.writeln('Type some keys and commands to play around.');
408-
term.writeln('');
409-
term.prompt();
405+
term!.writeln('Welcome to xterm.js');
406+
term!.writeln('This is a local terminal emulation, without a real terminal in the back-end.');
407+
term!.writeln('Type some keys and commands to play around.');
408+
term!.writeln('');
409+
(term as any).prompt();
410410

411-
term.onKey((e: { key: string, domEvent: KeyboardEvent }) => {
411+
term!.onKey((e: { key: string, domEvent: KeyboardEvent }) => {
412412
const ev = e.domEvent;
413413
const printable = !ev.altKey && !ev.ctrlKey && !ev.metaKey;
414414

415415
if (ev.keyCode === 13) {
416-
term.prompt();
416+
(term as any).prompt();
417417
} else if (ev.keyCode === 8) {
418418
// Do not delete the prompt
419-
if (term._core.buffer.x > 2) {
420-
term.write('\b \b');
419+
if ((term as any)._core.buffer.x > 2) {
420+
term!.write('\b \b');
421421
}
422422
} else if (printable) {
423-
term.write(e.key);
423+
term!.write(e.key);
424424
}
425425
});
426426
}
427427

428428
function updateTerminalContainerBackground(): void {
429-
if (term.options.allowTransparency) {
430-
terminalContainer.style.background = 'repeating-conic-gradient(#000000 0% 25%, #101010 0% 50%) 50% / 20px 20px';
429+
if (term!.options.allowTransparency) {
430+
terminalContainer!.style.background = 'repeating-conic-gradient(#000000 0% 25%, #101010 0% 50%) 50% / 20px 20px';
431431
} else {
432-
terminalContainer.style.background = term.options.theme?.background ?? '#000000';
432+
terminalContainer!.style.background = term!.options.theme?.background ?? '#000000';
433433
}
434434
}
435435

@@ -439,19 +439,19 @@ function initAddons(term: Terminal): void {
439439
function postInitWebgl(): void {
440440
controlBar.setTabVisible('addon-webgl', true);
441441
setTimeout(() => {
442-
addonWebglWindow.setTextureAtlas(addons.webgl.instance.textureAtlas);
443-
addons.webgl.instance.onChangeTextureAtlas(e => addonWebglWindow.setTextureAtlas(e));
444-
addons.webgl.instance.onAddTextureAtlasCanvas(e => addonWebglWindow.appendTextureAtlas(e));
442+
addonWebglWindow.setTextureAtlas(addons.webgl.instance!.textureAtlas!);
443+
addons.webgl.instance!.onChangeTextureAtlas(e => addonWebglWindow.setTextureAtlas(e));
444+
addons.webgl.instance!.onAddTextureAtlasCanvas(e => addonWebglWindow.appendTextureAtlas(e));
445445
}, 500);
446446
}
447447
function preDisposeWebgl(): void {
448448
controlBar.setTabVisible('addon-webgl', false);
449-
if (addons.webgl.instance.textureAtlas) {
450-
addons.webgl.instance.textureAtlas.remove();
449+
if (addons.webgl.instance!.textureAtlas) {
450+
addons.webgl.instance!.textureAtlas.remove();
451451
}
452452
}
453453

454-
Object.keys(addons).forEach((name: AddonType) => {
454+
(Object.keys(addons) as AddonType[]).forEach(name => {
455455
const addon = addons[name];
456456
const checkbox = document.createElement('input') as HTMLInputElement;
457457
checkbox.type = 'checkbox';
@@ -466,12 +466,12 @@ function initAddons(term: Terminal): void {
466466
term.unicode.activeVersion = '15-graphemes';
467467
}
468468
if (name === 'search' && checkbox.checked) {
469-
addons[name].instance.onDidChangeResults(e => updateFindResults(e));
469+
addons[name].instance!.onDidChangeResults(e => updateFindResults(e));
470470
}
471471
addDomListener(checkbox, 'change', () => {
472472
if (name === 'image') {
473473
if (checkbox.checked) {
474-
const ctorOptionsJson = document.querySelector<HTMLTextAreaElement>('#image-options').value;
474+
const ctorOptionsJson = document.querySelector<HTMLTextAreaElement>('#image-options')!.value;
475475
addon.instance = ctorOptionsJson
476476
? new addons[name].ctor(JSON.parse(ctorOptionsJson))
477477
: new addons[name].ctor();
@@ -497,7 +497,7 @@ function initAddons(term: Terminal): void {
497497
term.unicode.activeVersion = '15-graphemes';
498498
} else if (name === 'search') {
499499
controlBar.setTabVisible('addon-search', true);
500-
addons[name].instance.onDidChangeResults(e => updateFindResults(e));
500+
addons[name].instance!.onDidChangeResults(e => updateFindResults(e));
501501
} else if (name === 'serialize') {
502502
controlBar.setTabVisible('addon-serialize', true);
503503
}
@@ -587,17 +587,17 @@ function updateFindResults(e: { resultIndex: number, resultCount: number } | und
587587

588588
function addDomListener(element: HTMLElement, type: string, handler: (...args: any[]) => any): void {
589589
element.addEventListener(type, handler);
590-
term._core._register({ dispose: () => element.removeEventListener(type, handler) });
590+
(term as any)._core._register({ dispose: () => element.removeEventListener(type, handler) });
591591
}
592592

593593
function updateTerminalSize(): void {
594594
const width = optionsWindow.autoResize ? '100%'
595-
: (term.dimensions.css.canvas.width + term._core.viewport.scrollBarWidth).toString() + 'px';
595+
: ((term as any).dimensions.css.canvas.width + (term as any)._core.viewport.scrollBarWidth).toString() + 'px';
596596
const height = optionsWindow.autoResize ? '100%'
597-
: (term.dimensions.css.canvas.height).toString() + 'px';
598-
terminalContainer.style.width = width;
599-
terminalContainer.style.height = height;
600-
addons.fit.instance.fit();
597+
: ((term as any).dimensions.css.canvas.height).toString() + 'px';
598+
terminalContainer!.style.width = width;
599+
terminalContainer!.style.height = height;
600+
addons.fit.instance!.fit();
601601
}
602602

603603
(console as any).image = (source: ImageData | HTMLCanvasElement, scale: number = 1) => {

demo/client/components/window/addonImageWindow.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ export class AddonImageWindow extends BaseWindow implements IControlWindow {
1010
public readonly id = 'addon-image';
1111
public readonly label = 'image';
1212

13-
private _imageStorageLimitInput: HTMLInputElement;
14-
private _imageShowPlaceholderCheckbox: HTMLInputElement;
15-
private _imageOptionsTextarea: HTMLTextAreaElement;
13+
private _imageStorageLimitInput!: HTMLInputElement;
14+
private _imageShowPlaceholderCheckbox!: HTMLInputElement;
15+
private _imageOptionsTextarea!: HTMLTextAreaElement;
1616

1717
public build(container: HTMLElement): void {
1818
// Storage limit

demo/client/components/window/addonSearchWindow.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ export class AddonSearchWindow extends BaseWindow implements IControlWindow {
1010
public readonly id = 'addon-search';
1111
public readonly label = 'search';
1212

13-
private _findNextInput: HTMLInputElement;
14-
private _findPreviousInput: HTMLInputElement;
15-
private _findResultsSpan: HTMLElement;
16-
private _regexCheckbox: HTMLInputElement;
17-
private _caseSensitiveCheckbox: HTMLInputElement;
18-
private _wholeWordCheckbox: HTMLInputElement;
19-
private _highlightAllMatchesCheckbox: HTMLInputElement;
13+
private _findNextInput!: HTMLInputElement;
14+
private _findPreviousInput!: HTMLInputElement;
15+
private _findResultsSpan!: HTMLElement;
16+
private _regexCheckbox!: HTMLInputElement;
17+
private _caseSensitiveCheckbox!: HTMLInputElement;
18+
private _wholeWordCheckbox!: HTMLInputElement;
19+
private _highlightAllMatchesCheckbox!: HTMLInputElement;
2020

2121
public build(container: HTMLElement): void {
2222
const wrapper = document.createElement('div');

0 commit comments

Comments
 (0)