forked from Eugeny/tabby
-
Notifications
You must be signed in to change notification settings - Fork 0
/
decorator.ts
59 lines (52 loc) · 1.86 KB
/
decorator.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import { Inject, Injectable } from '@angular/core'
import { ConfigService, PlatformService } from 'tabby-core'
import { TerminalDecorator, BaseTerminalTabComponent, XTermFrontend } from 'tabby-terminal'
import { WebLinksAddon } from 'xterm-addon-web-links'
import { LinkHandler } from './api'
@Injectable()
export class LinkHighlighterDecorator extends TerminalDecorator {
constructor (
private config: ConfigService,
private platform: PlatformService,
@Inject(LinkHandler) private handlers: LinkHandler[],
) {
super()
}
attach (tab: BaseTerminalTabComponent): void {
if (!(tab.frontend instanceof XTermFrontend)) {
// not xterm
return
}
tab.frontend.xterm.options.linkHandler = {
activate: (event, uri) => {
if (!this.willHandleEvent(event)) {
return
}
this.platform.openExternal(uri)
},
}
for (const handler of this.handlers) {
const getLink = async uri => handler.convert(uri, tab)
const openLink = async uri => handler.handle(await getLink(uri), tab)
const addon = new WebLinksAddon(
async (event, uri) => {
if (!this.willHandleEvent(event)) {
return
}
if (!await handler.verify(await handler.convert(uri, tab), tab)) {
return
}
openLink(uri)
},
{
urlRegex: handler.regex,
},
)
tab.frontend.xterm.loadAddon(addon)
}
}
private willHandleEvent (event: MouseEvent) {
const modifier = this.config.store.clickableLinks.modifier
return !modifier || event[modifier]
}
}