gterm/main.js

60 lines
2.1 KiB
JavaScript
Raw Normal View History

2021-11-16 15:57:32 +00:00
import { Terminal } from 'xterm'
import { AttachAddon } from 'xterm-addon-attach';
import { FitAddon } from 'xterm-addon-fit';
import { SerializeAddon } from "xterm-addon-serialize";
import { Unicode11Addon } from 'xterm-addon-unicode11';
import { WebLinksAddon } from 'xterm-addon-web-links';
import 'xterm/css/xterm.css'
(function() {
2021-11-16 18:04:21 +00:00
const terminal = new Terminal({
2021-11-18 02:48:48 +00:00
// screenKeys: true,
// useStyle: true,
// cursorBlink: true,
// fullscreenWin: true,
// maximizeWin: true,
// screenReaderMode: true,
2021-11-16 15:57:32 +00:00
});
2021-11-16 18:04:21 +00:00
const fitAddon = new FitAddon();
terminal.loadAddon(fitAddon);
2021-11-16 15:57:32 +00:00
var protocol = (location.protocol === "https:") ? "wss://" : "ws://";
2021-11-16 18:16:35 +00:00
var url = protocol + location.host + location.pathname + "ws"
2021-11-16 18:04:21 +00:00
const ws = new WebSocket(url);
const attachAddon = new AttachAddon(ws);
const webLinksAddon = new WebLinksAddon();
2021-11-16 15:57:32 +00:00
terminal.loadAddon(webLinksAddon);
2021-11-16 18:04:21 +00:00
const unicode11Addon = new Unicode11Addon();
2021-11-16 15:57:32 +00:00
terminal.loadAddon(unicode11Addon);
2021-11-16 18:04:21 +00:00
const serializeAddon = new SerializeAddon();
2021-11-16 15:57:32 +00:00
terminal.loadAddon(serializeAddon);
2021-11-16 18:04:21 +00:00
terminal.open(document.getElementById("terminal"));
2021-11-16 15:57:32 +00:00
ws.onclose = function(event) {
console.log(event);
terminal.write('\r\n\nconnection has been terminated from the server-side (hit refresh to restart)\n')
};
ws.onopen = function() {
terminal.loadAddon(attachAddon);
terminal._initialized = true;
terminal.focus();
setTimeout(function() {fitAddon.fit()});
2021-11-17 04:28:47 +00:00
document.addEventListener('keypress',(e)=>{
e.preventDefault();
})
2021-11-16 15:57:32 +00:00
terminal.onResize(function(event) {
var rows = event.rows;
var cols = event.cols;
var size = JSON.stringify({cols: cols, rows: rows + 1});
var send = new TextEncoder().encode("\x01" + size);
console.log('resizing to', size);
ws.send(send);
});
terminal.onTitleChange(function(event) {
console.log(event);
});
window.onresize = function() {
2021-11-16 18:04:21 +00:00
console.log("resize")
2021-11-16 15:57:32 +00:00
fitAddon.fit();
};
2021-11-17 04:28:47 +00:00
fitAddon.fit();
2021-11-16 15:57:32 +00:00
};
})();