tabby/terminus-terminal/src/pathDrop.ts
2019-05-07 14:11:26 +02:00

40 lines
1.2 KiB
TypeScript

import { Subscription } from 'rxjs'
import { Injectable } from '@angular/core'
import { TerminalDecorator } from './api'
import { TerminalTabComponent } from './components/terminalTab.component'
/** @hidden */
@Injectable()
export class PathDropDecorator extends TerminalDecorator {
private subscriptions: Subscription[] = []
attach (terminal: TerminalTabComponent): void {
setTimeout(() => {
this.subscriptions = [
terminal.frontend.dragOver$.subscribe(event => {
event.preventDefault()
}),
terminal.frontend.drop$.subscribe(event => {
for (let file of event.dataTransfer.files as any) {
this.injectPath(terminal, file.path)
}
event.preventDefault()
}),
]
})
}
injectPath (terminal: TerminalTabComponent, path: string) {
if (path.indexOf(' ') >= 0) {
path = `"${path}"`
}
path = path.replace(/\\/g, '\\\\')
terminal.sendInput(path + ' ')
}
detach (terminal: TerminalTabComponent): void {
for (let s of this.subscriptions) {
s.unsubscribe()
}
}
}