debian-mirror-gitlab/app/assets/javascripts/terminal/terminal.js

129 lines
3.2 KiB
JavaScript
Raw Normal View History

2018-05-09 12:01:36 +05:30
import $ from 'jquery';
2021-03-11 19:13:27 +05:30
import { throttle } from 'lodash';
2018-11-18 11:00:15 +05:30
import { Terminal } from 'xterm';
import * as fit from 'xterm/lib/addons/fit/fit';
2019-03-02 22:35:43 +05:30
import * as webLinks from 'xterm/lib/addons/webLinks/webLinks';
2019-02-15 15:39:39 +05:30
import { canScrollUp, canScrollDown } from '~/lib/utils/dom_utils';
2019-07-31 22:56:46 +05:30
import { __ } from '~/locale';
2019-02-15 15:39:39 +05:30
const SCROLL_MARGIN = 5;
Terminal.applyAddon(fit);
2019-03-02 22:35:43 +05:30
Terminal.applyAddon(webLinks);
2018-05-09 12:01:36 +05:30
2018-11-18 11:00:15 +05:30
export default class GLTerminal {
2019-02-15 15:39:39 +05:30
constructor(element, options = {}) {
2020-05-24 23:13:21 +05:30
this.options = {
cursorBlink: true,
screenKeys: true,
...options,
};
2018-03-27 19:54:05 +05:30
2019-02-15 15:39:39 +05:30
this.container = element;
this.onDispose = [];
2018-03-27 19:54:05 +05:30
2018-11-18 11:00:15 +05:30
this.setSocketUrl();
this.createTerminal();
2017-08-17 22:00:37 +05:30
2021-02-22 17:27:13 +05:30
// eslint-disable-next-line @gitlab/no-global-event-off
2018-11-18 11:00:15 +05:30
$(window)
.off('resize.terminal')
.on('resize.terminal', () => {
2017-08-17 22:00:37 +05:30
this.terminal.fit();
});
2018-11-18 11:00:15 +05:30
}
setSocketUrl() {
const { protocol, hostname, port } = window.location;
const wsProtocol = protocol === 'https:' ? 'wss://' : 'ws://';
const path = this.container.dataset.projectPath;
2017-08-17 22:00:37 +05:30
2018-11-18 11:00:15 +05:30
this.socketUrl = `${wsProtocol}${hostname}:${port}${path}`;
}
2017-08-17 22:00:37 +05:30
2018-11-18 11:00:15 +05:30
createTerminal() {
this.terminal = new Terminal(this.options);
2017-08-17 22:00:37 +05:30
2018-11-18 11:00:15 +05:30
this.socket = new WebSocket(this.socketUrl, ['terminal.gitlab.com']);
this.socket.binaryType = 'arraybuffer';
2017-08-17 22:00:37 +05:30
2018-11-18 11:00:15 +05:30
this.terminal.open(this.container);
this.terminal.fit();
2019-03-02 22:35:43 +05:30
this.terminal.webLinksInit();
2018-11-18 11:00:15 +05:30
this.terminal.focus();
2017-08-17 22:00:37 +05:30
2018-11-18 11:00:15 +05:30
this.socket.onopen = () => {
this.runTerminal();
};
this.socket.onerror = () => {
this.handleSocketFailure();
};
}
2017-08-17 22:00:37 +05:30
2018-11-18 11:00:15 +05:30
runTerminal() {
const decoder = new TextDecoder('utf-8');
const encoder = new TextEncoder('utf-8');
2017-08-17 22:00:37 +05:30
2021-03-08 18:12:59 +05:30
this.terminal.on('data', (data) => {
2018-11-18 11:00:15 +05:30
this.socket.send(encoder.encode(data));
});
2017-08-17 22:00:37 +05:30
2021-03-08 18:12:59 +05:30
this.socket.addEventListener('message', (ev) => {
2018-11-18 11:00:15 +05:30
this.terminal.write(decoder.decode(ev.data));
});
2017-08-17 22:00:37 +05:30
2018-11-18 11:00:15 +05:30
this.isTerminalInitialized = true;
this.terminal.fit();
2017-08-17 22:00:37 +05:30
}
2018-11-18 11:00:15 +05:30
handleSocketFailure() {
2019-07-31 22:56:46 +05:30
this.terminal.write('\r\n');
this.terminal.write(__('Connection failure'));
2018-11-18 11:00:15 +05:30
}
2019-02-15 15:39:39 +05:30
addScrollListener(onScrollLimit) {
const viewport = this.container.querySelector('.xterm-viewport');
2020-04-08 14:13:33 +05:30
const listener = throttle(() => {
2019-02-15 15:39:39 +05:30
onScrollLimit({
canScrollUp: canScrollUp(viewport, SCROLL_MARGIN),
canScrollDown: canScrollDown(viewport, SCROLL_MARGIN),
});
});
this.onDispose.push(() => viewport.removeEventListener('scroll', listener));
viewport.addEventListener('scroll', listener);
// don't forget to initialize value before scroll!
listener({ target: viewport });
}
disable() {
this.terminal.setOption('cursorBlink', false);
this.terminal.setOption('theme', { foreground: '#707070' });
this.terminal.setOption('disableStdin', true);
this.socket.close();
}
dispose() {
2021-02-22 17:27:13 +05:30
// eslint-disable-next-line @gitlab/no-global-event-off
2019-02-15 15:39:39 +05:30
this.terminal.off('data');
this.terminal.dispose();
this.socket.close();
2021-03-08 18:12:59 +05:30
this.onDispose.forEach((fn) => fn());
2019-02-15 15:39:39 +05:30
this.onDispose.length = 0;
}
scrollToTop() {
this.terminal.scrollToTop();
}
scrollToBottom() {
this.terminal.scrollToBottom();
}
fit() {
this.terminal.fit();
}
2018-11-18 11:00:15 +05:30
}