import { Terminal, ITerminalAddon } from 'xterm'; export class ScrollDownHelperAddon implements ITerminalAddon { private terminal: Terminal = new Terminal(); private element?: HTMLDivElement; activate (terminal: Terminal): void { this.terminal = terminal; this.terminal.onScroll(() => { if (this.isScrolledDown()) { this.hide(); } }); this.terminal.onLineFeed(() => { if (!this.isScrolledDown()) { this.show(); } }); this.show(); } dispose (): void { // ignore } show (): void { if (!this.terminal || !this.terminal.element) { return; } if (this.element) { this.element.style.visibility = 'visible'; return; } this.terminal.element.style.position = 'relative'; this.element = document.createElement('div'); this.element.innerHTML = ''; this.element.style.position = 'absolute'; this.element.style.right = '1.5rem'; this.element.style.bottom = '.5rem'; this.element.style.padding = '.5rem'; this.element.style.fontSize = '1.25em'; this.element.style.boxShadow = '0 2px 8px #000'; this.element.style.backgroundColor = '#252526'; this.element.style.zIndex = '999'; this.element.style.cursor = 'pointer'; this.element.addEventListener('click', () => { this.terminal.scrollToBottom(); }); this.terminal.element.appendChild(this.element); } hide (): void { if (this.element) { this.element.style.visibility = 'hidden'; } } isScrolledDown (): boolean { return this.terminal.buffer.active.viewportY === this.terminal.buffer.active.baseY; } }