Fix for xtermjs

This commit is contained in:
piotrpekala7 2020-03-26 21:18:22 +01:00
parent 4fc87c4cda
commit 4402a53ae0
6 changed files with 44 additions and 18 deletions

View File

@ -24,7 +24,7 @@
</button> </button>
</ng-template> </ng-template>
place for log console place for log console component
</mat-tab> </mat-tab>
<mat-tab *ngFor="let node of nodes; let index = index" [label]="tab"> <mat-tab *ngFor="let node of nodes; let index = index" [label]="tab">

View File

@ -6,6 +6,7 @@ import { ResizeEvent } from 'angular-resizable-element';
import { ThemeService } from '../../../services/theme.service'; import { ThemeService } from '../../../services/theme.service';
import { FormControl } from '@angular/forms'; import { FormControl } from '@angular/forms';
import { NodeConsoleService } from '../../../services/nodeConsole.service'; import { NodeConsoleService } from '../../../services/nodeConsole.service';
import { Node } from '../../../cartography/models/node';
@Component({ @Component({
@ -13,7 +14,7 @@ import { NodeConsoleService } from '../../../services/nodeConsole.service';
templateUrl: './console-wrapper.component.html', templateUrl: './console-wrapper.component.html',
styleUrls: ['./console-wrapper.component.scss'] styleUrls: ['./console-wrapper.component.scss']
}) })
export class ConsoleWrapperComponent implements OnInit, AfterViewInit, OnDestroy { export class ConsoleWrapperComponent implements OnInit {
@Input() server: Server; @Input() server: Server;
@Input() project: Project; @Input() project: Project;
@Output() closeConsole = new EventEmitter<boolean>(); @Output() closeConsole = new EventEmitter<boolean>();
@ -37,19 +38,18 @@ export class ConsoleWrapperComponent implements OnInit, AfterViewInit, OnDestroy
ngOnInit() { ngOnInit() {
this.themeService.getActualTheme() === 'light' ? this.isLightThemeEnabled = true : this.isLightThemeEnabled = false; this.themeService.getActualTheme() === 'light' ? this.isLightThemeEnabled = true : this.isLightThemeEnabled = false;
this.style = { bottom: '20px', left: '20px', width: '600px', height: '180px'}; // style properties this.style = { bottom: '20px', left: '20px', width: '720px', height: '456px'};
this.consoleService.nodeConsoleTrigger.subscribe((node) => { this.consoleService.nodeConsoleTrigger.subscribe((node) => {
this.addTab(node, true); this.addTab(node, true);
}); });
}
ngAfterViewInit() { this.consoleService.closeNodeConsoleTrigger.subscribe((node) => {
// this.console.nativeElement.scrollTop = this.console.nativeElement.scrollHeight; let index = this.nodes.findIndex(n => n.node_id === node.node_id)
this.removeTab(index);
});
} }
ngOnDestroy() {}
addTab(node: Node, selectAfterAdding: boolean) { addTab(node: Node, selectAfterAdding: boolean) {
this.nodes.push(node); this.nodes.push(node);

View File

@ -1,3 +1,7 @@
.wrapper {
height: 600px;
}
app-root, app-root,
app-project-map, app-project-map,
.project-map, .project-map,

View File

@ -1 +1 @@
<div id="terminal"></div> <div #terminal id="terminal"></div>

View File

@ -1,12 +1,15 @@
import { Component, OnInit, Input, AfterViewInit } from '@angular/core'; import { Component, OnInit, Input, AfterViewInit, ViewEncapsulation, ViewChild, ElementRef } from '@angular/core';
import { Project } from '../../../models/project'; import { Project } from '../../../models/project';
import { Server } from '../../../models/server'; import { Server } from '../../../models/server';
import { Terminal } from 'xterm'; import { Terminal } from 'xterm';
import { AttachAddon } from 'xterm-addon-attach'; import { AttachAddon } from 'xterm-addon-attach';
import { Node } from '../../../cartography/models/node'; import { Node } from '../../../cartography/models/node';
import { FitAddon } from 'xterm-addon-fit';
import { NodeConsoleService } from '../../../services/nodeConsole.service';
@Component({ @Component({
encapsulation: ViewEncapsulation.None,
selector: 'app-web-console', selector: 'app-web-console',
templateUrl: './web-console.component.html', templateUrl: './web-console.component.html',
styleUrls: ['../../../../../node_modules/xterm/css/xterm.css'] styleUrls: ['../../../../../node_modules/xterm/css/xterm.css']
@ -15,22 +18,35 @@ export class WebConsoleComponent implements OnInit, AfterViewInit {
@Input() server: Server; @Input() server: Server;
@Input() project: Project; @Input() project: Project;
@Input() node: Node; @Input() node: Node;
public term: Terminal;
@ViewChild('terminal', {static: false}) terminal: ElementRef;
constructor() {} constructor(
private consoleService: NodeConsoleService
) {}
ngOnInit() {} ngOnInit() {}
ngAfterViewInit() { ngAfterViewInit() {
const term = new Terminal(); this.term = new Terminal();
setTimeout(() => { setTimeout(() => {
term.open(document.getElementById('terminal')); this.term.open(this.terminal.nativeElement);
term.write('\x1B[1;3;31mxterm.js\x1B[0m $ ')
const socket = new WebSocket(this.getUrl()); const socket = new WebSocket(this.getUrl());
socket.onerror = ((event) => {
this.term.write('Connection lost');
});
socket.onclose = ((event) => {
this.consoleService.closeConsoleForNode(this.node);
});
const attachAddon = new AttachAddon(socket); const attachAddon = new AttachAddon(socket);
term.loadAddon(attachAddon); this.term.loadAddon(attachAddon);
this.term.setOption('cursorBlink', true);
console.log('Console is running...');
const fitAddon = new FitAddon();
this.term.loadAddon(fitAddon);
fitAddon.activate(this.term);
}, 1000); }, 1000);
} }

View File

@ -1,13 +1,19 @@
import { Injectable, EventEmitter } from '@angular/core'; import { Injectable, EventEmitter } from '@angular/core';
import { Node } from '../cartography/models/node'; import { Node } from '../cartography/models/node';
import { Subject } from 'rxjs';
@Injectable() @Injectable()
export class NodeConsoleService { export class NodeConsoleService {
public nodeConsoleTrigger = new EventEmitter<Node>(); public nodeConsoleTrigger = new EventEmitter<Node>();
public closeNodeConsoleTrigger = new Subject<Node>();
constructor() {} constructor() {}
openConsoleForNode(node: Node) { openConsoleForNode(node: Node) {
this.nodeConsoleTrigger.emit(node); this.nodeConsoleTrigger.emit(node);
} }
closeConsoleForNode(node: Node) {
this.closeNodeConsoleTrigger.next(node)
}
} }