Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pterodactyl
GitHub Repository: pterodactyl/panel
Path: blob/1.0-develop/resources/scripts/plugins/XtermScrollDownHelperAddon.ts
7458 views
1
import { Terminal, ITerminalAddon } from 'xterm';
2
3
export class ScrollDownHelperAddon implements ITerminalAddon {
4
private terminal: Terminal = new Terminal();
5
private element?: HTMLDivElement;
6
7
activate(terminal: Terminal): void {
8
this.terminal = terminal;
9
10
this.terminal.onScroll(() => {
11
if (this.isScrolledDown()) {
12
this.hide();
13
}
14
});
15
16
this.terminal.onLineFeed(() => {
17
if (!this.isScrolledDown()) {
18
this.show();
19
}
20
});
21
22
this.show();
23
}
24
25
dispose(): void {
26
// ignore
27
}
28
29
show(): void {
30
if (!this.terminal || !this.terminal.element) {
31
return;
32
}
33
if (this.element) {
34
this.element.style.visibility = 'visible';
35
return;
36
}
37
38
this.terminal.element.style.position = 'relative';
39
40
this.element = document.createElement('div');
41
this.element.innerHTML =
42
'<svg aria-hidden="true" focusable="false" data-prefix="fas" data-icon="arrow-down" class="svg-inline--fa fa-bell fa-w-14" role="img" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 384 512"><path fill="currentColor" d="M374.6 310.6l-160 160C208.4 476.9 200.2 480 192 480s-16.38-3.125-22.62-9.375l-160-160c-12.5-12.5-12.5-32.75 0-45.25s32.75-12.5 45.25 0L160 370.8V64c0-17.69 14.33-31.1 31.1-31.1S224 46.31 224 64v306.8l105.4-105.4c12.5-12.5 32.75-12.5 45.25 0S387.1 298.1 374.6 310.6z"/></svg>';
43
this.element.style.position = 'absolute';
44
this.element.style.right = '1.5rem';
45
this.element.style.bottom = '.5rem';
46
this.element.style.padding = '.5rem';
47
this.element.style.fontSize = '1.25em';
48
this.element.style.boxShadow = '0 2px 8px #000';
49
this.element.style.backgroundColor = '#252526';
50
this.element.style.zIndex = '999';
51
this.element.style.cursor = 'pointer';
52
53
this.element.addEventListener('click', () => {
54
this.terminal.scrollToBottom();
55
});
56
57
this.terminal.element.appendChild(this.element);
58
}
59
60
hide(): void {
61
if (this.element) {
62
this.element.style.visibility = 'hidden';
63
}
64
}
65
66
isScrolledDown(): boolean {
67
return this.terminal.buffer.active.viewportY === this.terminal.buffer.active.baseY;
68
}
69
}
70
71