Add support for render addons #1656

This commit is contained in:
iliajie
2023-06-29 16:35:01 +03:00
parent 9db9582f4a
commit 827c77c960
3 changed files with 54 additions and 21 deletions

View File

@ -43,7 +43,9 @@ my $termlinks =
{ 'css' => ["xterm.css?$wver"], { 'css' => ["xterm.css?$wver"],
'js' => ["xterm.js?$wver", 'js' => ["xterm.js?$wver",
"xterm-addon-attach.js?$wver", "xterm-addon-attach.js?$wver",
"xterm-addon-fit.js?$wver"] }; "xterm-addon-fit.js?$wver",
"xterm-addon-canvas.js?$wver",
"xterm-addon-webgl.js?$wver"] };
# Pre-process options # Pre-process options
my $conf_size_str = $config{'size'}; my $conf_size_str = $config{'size'};
@ -205,31 +207,60 @@ if ($http_host_conf) {
} }
my $http_host = $http_host_conf || "$ws_proto://$ENV{'HTTP_HOST'}"; my $http_host = $http_host_conf || "$ws_proto://$ENV{'HTTP_HOST'}";
my $url = "$http_host/$module_name/ws-$port"; my $url = "$http_host/$module_name/ws-$port";
my $canvasAddon = $termlinks->{'js'}[3];
my $webGLAddon = $termlinks->{'js'}[4];
my $term_script = <<EOF; my $term_script = <<EOF;
(function() { (function() {
var socket = new WebSocket('$url', 'binary'), const socket = new WebSocket('$url', 'binary'),
termcont = document.getElementById('terminal'), termcont = document.getElementById('terminal'),
err_conn_cannot = 'Cannot connect to the socket $url', err_conn_cannot = 'Cannot connect to the socket $url',
err_conn_lost = 'Connection to the socket $url lost'; err_conn_lost = 'Connection to the socket $url lost',
webGLAddonLink = '$webGLAddon',
canvasAddonLink = '$canvasAddon',
detectWebGLContext = (function() {
const canvas = document.createElement("canvas"),
gl = canvas.getContext("webgl") ||
canvas.getContext("experimental-webgl");
return gl instanceof WebGLRenderingContext ? true : false;
})();
socket.onopen = function() { socket.onopen = function() {
var term = new Terminal($termjs_opts{'Options'}), const term = new Terminal($termjs_opts{'Options'}),
attachAddon = new AttachAddon.AttachAddon(this), attachAddon = new AttachAddon.AttachAddon(this),
fitAddon = new FitAddon.FitAddon(); fitAddon = new FitAddon.FitAddon(),
term.loadAddon(attachAddon); renderScript = document.createElement('script');
term.loadAddon(fitAddon); renderScript.src = detectWebGLContext ? webGLAddonLink : canvasAddonLink;
term.open(termcont); renderScript.async = false;
term.focus(); document.body.appendChild(renderScript);
// On resize event triggered by fit()
term.onResize(function(e) {
socket.send('\\\\033[8;('+e.rows+');('+e.cols+')t');
});
// Observe on terminal container change // Wait to load requested render addon
new ResizeObserver(function() { renderScript.addEventListener('load', function() {
fitAddon.fit(); const rendererAddon = detectWebGLContext ?
}).observe(termcont); new WebglAddon.WebglAddon() :
new CanvasAddon.CanvasAddon();
term.loadAddon(attachAddon);
term.loadAddon(fitAddon);
term.loadAddon(rendererAddon);
term.open(termcont);
term.focus();
// Handle case of dropping WebGL context
if (typeof WebglAddon === 'object') {
rendererAddon.onContextLoss(function() {
rendererAddon.dispose();
});
}
// On resize event triggered by fit()
term.onResize(function(e) {
socket.send('\\\\033[8;(' + e.rows + ');(' + e.cols + ')t');
});
// Observe on terminal container change
new ResizeObserver(function() {
fitAddon.fit();
}).observe(termcont);
});
}; };
socket.onerror = function() { socket.onerror = function() {
termcont.innerHTML = '<tt style="color: \#ff0000">Error: ' + termcont.innerHTML = '<tt style="color: \#ff0000">Error: ' +

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long