Skip to content

Commit 0f8b54e

Browse files
fix(sol): emits data now onKey
1 parent 31431df commit 0f8b54e

File tree

1 file changed

+30
-33
lines changed

1 file changed

+30
-33
lines changed

sol/src/terminal/terminal.component.ts

Lines changed: 30 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
* SPDX-License-Identifier: Apache-2.0
44
**********************************************************************/
55
import { Component, Input, OnInit, Output, EventEmitter } from '@angular/core'
6-
import { C, V } from '@angular/cdk/keycodes'
76
import { Terminal } from '@xterm/xterm'
87
@Component({
98
selector: 'amt-terminal',
@@ -15,44 +14,42 @@ export class TerminalComponent implements OnInit {
1514
@Output() handleKeyPress: EventEmitter<any> = new EventEmitter<any>()
1615
container!: HTMLElement | null
1716
ngOnInit(): void {
17+
console.log('Terminal component ngOnInit')
18+
if (this.term == null) throw new Error('Terminal not found')
1819
this.container = document.getElementById('terminal')
1920
if (this.container == null) throw new Error('Container not found')
2021
this.term.open(this.container)
21-
this.term.onData((data: any) => {
22-
this.handleKeyPress.emit(data)
23-
})
24-
this.term.attachCustomKeyEventHandler((e: KeyboardEvent): boolean => {
25-
e.stopPropagation()
26-
// Due to a new 'HACK' in xtermjs, calling e.preventDefault() here
27-
// results in all upper case characters being dropped,
28-
// so only call it if we 'consume' the keydown here.
29-
// Note: this function can be called for 'keypress' and 'keyup' events as well as 'keydown' events
30-
if (e.type === 'keydown') {
31-
if (e.ctrlKey && e.shiftKey && e.keyCode === C) {
32-
e.preventDefault()
33-
navigator.clipboard
34-
.writeText(this.term.getSelection())
35-
.then(() => {})
36-
.catch((err) => {
37-
console.error('Failed to copy to clipboard', err)
38-
})
39-
} else if (e.ctrlKey && e.shiftKey && e.keyCode === V) {
40-
e.preventDefault()
22+
this.term.onKey(({ key, domEvent }) => {
23+
console.log('Key event onKey:', domEvent)
24+
domEvent.preventDefault()
25+
if (domEvent.ctrlKey && domEvent.key === 'c') {
26+
if (this.term.hasSelection()) {
4127
navigator.clipboard
42-
.readText()
43-
.then((text) => {
44-
this.handleKeyPress.emit(text)
45-
})
46-
.catch((err) => {
47-
console.error('Failed to read clipboard', err)
48-
})
49-
} else if (e.code === 'Space') {
50-
// or e.keyCode === SPACE
51-
e.preventDefault()
52-
this.handleKeyPress.emit(e.key)
28+
.writeText(this.term.getSelection())
29+
.then(() => {})
30+
.catch((err) => {
31+
console.error('Failed to copy to clipboard', err)
32+
})
33+
} else {
34+
console.log('Sending Ctrl + C (Interrupt Signal)');
35+
// Send interrupt signal (SIGINT) to terminal
36+
this.term.write('\x03') // Sends Ctrl + C (ASCII code 03)
5337
}
38+
// Prevent the default browser behavior (like opening menus)
39+
return false
40+
}
41+
if (domEvent.ctrlKey && domEvent.key === 'v') {
42+
navigator.clipboard
43+
.readText()
44+
.then((text) => {
45+
this.handleKeyPress.emit(text)
46+
})
47+
.catch((err) => {
48+
console.error('Failed to read clipboard', err)
49+
})
50+
return false
5451
}
55-
return false
52+
this.handleKeyPress.emit(key)
5653
})
5754
}
5855
}

0 commit comments

Comments
 (0)