From c0bfd73b7cd9a7e53d3267f2be93031d9ff26287 Mon Sep 17 00:00:00 2001 From: madhavilosetty-intel Date: Wed, 25 Sep 2024 12:22:52 -0700 Subject: [PATCH] fix(sol): emits data now onKey --- sol/src/terminal/terminal.component.ts | 50 ++++++++++++-------------- 1 file changed, 22 insertions(+), 28 deletions(-) diff --git a/sol/src/terminal/terminal.component.ts b/sol/src/terminal/terminal.component.ts index 1b923508..64be441b 100644 --- a/sol/src/terminal/terminal.component.ts +++ b/sol/src/terminal/terminal.component.ts @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 **********************************************************************/ import { Component, Input, OnInit, Output, EventEmitter } from '@angular/core' -import { C, V } from '@angular/cdk/keycodes' import { Terminal } from '@xterm/xterm' @Component({ selector: 'amt-terminal', @@ -15,44 +14,39 @@ export class TerminalComponent implements OnInit { @Output() handleKeyPress: EventEmitter = new EventEmitter() container!: HTMLElement | null ngOnInit(): void { + if (this.term == null) throw new Error('Terminal not found') this.container = document.getElementById('terminal') if (this.container == null) throw new Error('Container not found') this.term.open(this.container) - this.term.onData((data: any) => { - this.handleKeyPress.emit(data) - }) - this.term.attachCustomKeyEventHandler((e: KeyboardEvent): boolean => { - e.stopPropagation() - // Due to a new 'HACK' in xtermjs, calling e.preventDefault() here - // results in all upper case characters being dropped, - // so only call it if we 'consume' the keydown here. - // Note: this function can be called for 'keypress' and 'keyup' events as well as 'keydown' events - if (e.type === 'keydown') { - if (e.ctrlKey && e.shiftKey && e.keyCode === C) { - e.preventDefault() + this.term.onKey(({ key, domEvent }) => { + domEvent.preventDefault() + if (domEvent.ctrlKey && domEvent.key === 'c') { + if (this.term.hasSelection()) { navigator.clipboard .writeText(this.term.getSelection()) .then(() => {}) .catch((err) => { console.error('Failed to copy to clipboard', err) }) - } else if (e.ctrlKey && e.shiftKey && e.keyCode === V) { - e.preventDefault() - navigator.clipboard - .readText() - .then((text) => { - this.handleKeyPress.emit(text) - }) - .catch((err) => { - console.error('Failed to read clipboard', err) - }) - } else if (e.code === 'Space') { - // or e.keyCode === SPACE - e.preventDefault() - this.handleKeyPress.emit(e.key) + } else { + // Send interrupt signal (SIGINT) to terminal + this.term.write('\x03') // Sends Ctrl + C (ASCII code 03) } + // Prevent the default browser behavior (like opening menus) + return false + } + if (domEvent.ctrlKey && domEvent.key === 'v') { + navigator.clipboard + .readText() + .then((text) => { + this.handleKeyPress.emit(text) + }) + .catch((err) => { + console.error('Failed to read clipboard', err) + }) + return false } - return false + this.handleKeyPress.emit(key) }) } }