Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 22 additions & 28 deletions sol/src/terminal/terminal.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -15,44 +14,39 @@ export class TerminalComponent implements OnInit {
@Output() handleKeyPress: EventEmitter<any> = new EventEmitter<any>()
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)
})
}
}
Loading