2018-03-09 20:18:59 +00:00
|
|
|
import $ from 'jquery';
|
2021-02-14 18:09:20 +00:00
|
|
|
import { throttle } from 'lodash';
|
2018-07-12 10:44:09 +00:00
|
|
|
import { Terminal } from 'xterm';
|
|
|
|
import * as fit from 'xterm/lib/addons/fit/fit';
|
2019-01-08 18:32:37 +00:00
|
|
|
import * as webLinks from 'xterm/lib/addons/webLinks/webLinks';
|
2018-12-07 16:29:32 +00:00
|
|
|
import { canScrollUp, canScrollDown } from '~/lib/utils/dom_utils';
|
2019-05-02 13:05:48 +00:00
|
|
|
import { __ } from '~/locale';
|
2018-12-07 16:29:32 +00:00
|
|
|
|
|
|
|
const SCROLL_MARGIN = 5;
|
|
|
|
|
|
|
|
Terminal.applyAddon(fit);
|
2019-01-08 18:32:37 +00:00
|
|
|
Terminal.applyAddon(webLinks);
|
2018-03-09 20:18:59 +00:00
|
|
|
|
2018-07-12 10:44:09 +00:00
|
|
|
export default class GLTerminal {
|
2018-12-07 16:29:32 +00:00
|
|
|
constructor(element, options = {}) {
|
2020-05-07 21:09:26 +00:00
|
|
|
this.options = {
|
|
|
|
cursorBlink: true,
|
|
|
|
screenKeys: true,
|
|
|
|
...options,
|
|
|
|
};
|
2018-03-01 17:53:47 +00:00
|
|
|
|
2018-12-07 16:29:32 +00:00
|
|
|
this.container = element;
|
|
|
|
this.onDispose = [];
|
2016-11-24 13:01:25 +00:00
|
|
|
|
2018-07-12 10:44:09 +00:00
|
|
|
this.setSocketUrl();
|
|
|
|
this.createTerminal();
|
2018-08-03 12:41:33 +00:00
|
|
|
|
2020-12-08 21:10:06 +00:00
|
|
|
// eslint-disable-next-line @gitlab/no-global-event-off
|
2018-07-12 10:44:09 +00:00
|
|
|
$(window)
|
|
|
|
.off('resize.terminal')
|
|
|
|
.on('resize.terminal', () => {
|
2016-11-24 13:01:25 +00:00
|
|
|
this.terminal.fit();
|
|
|
|
});
|
2018-07-12 10:44:09 +00:00
|
|
|
}
|
2016-11-24 13:01:25 +00:00
|
|
|
|
2018-07-12 10:44:09 +00:00
|
|
|
setSocketUrl() {
|
|
|
|
const { protocol, hostname, port } = window.location;
|
|
|
|
const wsProtocol = protocol === 'https:' ? 'wss://' : 'ws://';
|
|
|
|
const path = this.container.dataset.projectPath;
|
2016-11-24 13:01:25 +00:00
|
|
|
|
2018-07-12 10:44:09 +00:00
|
|
|
this.socketUrl = `${wsProtocol}${hostname}:${port}${path}`;
|
|
|
|
}
|
2016-11-24 13:01:25 +00:00
|
|
|
|
2018-07-12 10:44:09 +00:00
|
|
|
createTerminal() {
|
|
|
|
this.terminal = new Terminal(this.options);
|
2016-11-24 13:01:25 +00:00
|
|
|
|
2018-07-12 10:44:09 +00:00
|
|
|
this.socket = new WebSocket(this.socketUrl, ['terminal.gitlab.com']);
|
|
|
|
this.socket.binaryType = 'arraybuffer';
|
2016-11-24 13:01:25 +00:00
|
|
|
|
2018-07-12 10:44:09 +00:00
|
|
|
this.terminal.open(this.container);
|
2018-08-03 12:41:33 +00:00
|
|
|
this.terminal.fit();
|
2019-01-08 18:32:37 +00:00
|
|
|
this.terminal.webLinksInit();
|
2018-08-03 13:15:04 +00:00
|
|
|
this.terminal.focus();
|
2016-11-24 13:01:25 +00:00
|
|
|
|
2018-07-12 10:44:09 +00:00
|
|
|
this.socket.onopen = () => {
|
|
|
|
this.runTerminal();
|
|
|
|
};
|
|
|
|
this.socket.onerror = () => {
|
|
|
|
this.handleSocketFailure();
|
|
|
|
};
|
|
|
|
}
|
2016-11-24 13:01:25 +00:00
|
|
|
|
2018-07-12 10:44:09 +00:00
|
|
|
runTerminal() {
|
|
|
|
const decoder = new TextDecoder('utf-8');
|
|
|
|
const encoder = new TextEncoder('utf-8');
|
2016-11-24 13:01:25 +00:00
|
|
|
|
2020-12-23 21:10:24 +00:00
|
|
|
this.terminal.on('data', (data) => {
|
2018-07-12 10:44:09 +00:00
|
|
|
this.socket.send(encoder.encode(data));
|
|
|
|
});
|
2016-11-24 13:01:25 +00:00
|
|
|
|
2020-12-23 21:10:24 +00:00
|
|
|
this.socket.addEventListener('message', (ev) => {
|
2018-07-12 10:44:09 +00:00
|
|
|
this.terminal.write(decoder.decode(ev.data));
|
|
|
|
});
|
|
|
|
|
|
|
|
this.isTerminalInitialized = true;
|
|
|
|
this.terminal.fit();
|
2016-11-24 13:01:25 +00:00
|
|
|
}
|
|
|
|
|
2018-07-12 10:44:09 +00:00
|
|
|
handleSocketFailure() {
|
2019-05-02 13:05:48 +00:00
|
|
|
this.terminal.write('\r\n');
|
|
|
|
this.terminal.write(__('Connection failure'));
|
2018-07-12 10:44:09 +00:00
|
|
|
}
|
2018-12-07 16:29:32 +00:00
|
|
|
|
|
|
|
addScrollListener(onScrollLimit) {
|
|
|
|
const viewport = this.container.querySelector('.xterm-viewport');
|
2020-03-03 09:07:54 +00:00
|
|
|
const listener = throttle(() => {
|
2018-12-07 16:29:32 +00:00
|
|
|
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() {
|
2020-12-08 21:10:06 +00:00
|
|
|
// eslint-disable-next-line @gitlab/no-global-event-off
|
2018-12-07 16:29:32 +00:00
|
|
|
this.terminal.off('data');
|
|
|
|
this.terminal.dispose();
|
|
|
|
this.socket.close();
|
|
|
|
|
2020-12-23 21:10:24 +00:00
|
|
|
this.onDispose.forEach((fn) => fn());
|
2018-12-07 16:29:32 +00:00
|
|
|
this.onDispose.length = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
scrollToTop() {
|
|
|
|
this.terminal.scrollToTop();
|
|
|
|
}
|
|
|
|
|
|
|
|
scrollToBottom() {
|
|
|
|
this.terminal.scrollToBottom();
|
|
|
|
}
|
|
|
|
|
|
|
|
fit() {
|
|
|
|
this.terminal.fit();
|
|
|
|
}
|
2018-07-12 10:44:09 +00:00
|
|
|
}
|