bricks/bricks/wterm.js
2024-03-08 10:54:16 +08:00

54 lines
962 B
JavaScript

var bricks = window.bricks || {};
/*
dependent on xterm.js
*/
bricks.Wterm = class extends bricks.JsWidget {
/*
{
ws_url:
host:
ssh_port:
user:
}
*/
constructor(opts){
super(opts);
schedule_once(this.open.bind(this), 0.5);
}
async open(){
var term = new Terminal();
this.term = term;
term.open(this.dom_element);
var ws = new WebSocket(this.opts.ws_url);
bricks.debug('FitAddon=', FitAddon);
this.fitAddon = new FitAddon.FitAddon()
term.loadAddon(this.fitAddon)
// this.fitAddon.fit()
this.bind('resize', this.term_resize.bind(this))
ws.onmessage = msg => {
term.write(JSON.parse(msg.data).data);
};
term.onData(function(key) {
//Enter
let msg = {
data:{data:key},
type:1
}
ws.send(key);
});
term.focus();
term.paste("ls -l\n");
}
term_resize(){
try {
this.fitAddon.fit();
} catch(e){
bricks.debug('resize error', e);
}
}
}
bricks.Factory.register('Wterm', bricks.Wterm);