Skip to content

Commit 2936f80

Browse files
authored
Merge pull request #3233 from marvinthepa/3056_hover_callback_link_providers
Add hover and leave callbacks for linkprovider
2 parents d9309d3 + dec9f6d commit 2936f80

3 files changed

Lines changed: 54 additions & 10 deletions

File tree

addons/xterm-addon-web-links/src/WebLinkProvider.ts

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,40 @@
33
* @license MIT
44
*/
55

6-
import { ILinkProvider, IBufferCellPosition, ILink, Terminal } from 'xterm';
6+
import { ILinkProvider, ILink, Terminal, IViewportRange } from 'xterm';
7+
8+
interface ILinkProviderOptions {
9+
hover?(event: MouseEvent, text: string, location: IViewportRange): void;
10+
leave?(event: MouseEvent, text: string): void;
11+
}
712

813
export class WebLinkProvider implements ILinkProvider {
914

1015
constructor(
1116
private readonly _terminal: Terminal,
1217
private readonly _regex: RegExp,
13-
private readonly _handler: (event: MouseEvent, uri: string) => void
18+
private readonly _handler: (event: MouseEvent, uri: string) => void,
19+
private readonly _options: ILinkProviderOptions = {}
1420
) {
1521

1622
}
1723

1824
public provideLinks(y: number, callback: (links: ILink[] | undefined) => void): void {
19-
callback(LinkComputer.computeLink(y, this._regex, this._terminal, this._handler));
25+
const links = LinkComputer.computeLink(y, this._regex, this._terminal, this._handler);
26+
callback(this._addCallbacks(links));
27+
}
28+
29+
private _addCallbacks(links: ILink[]): ILink[] {
30+
return links.map(link => {
31+
link.leave = this._options.leave;
32+
link.hover = (event: MouseEvent, uri: string): void => {
33+
if (this._options.hover) {
34+
const { range } = link;
35+
this._options.hover(event, uri, range);
36+
}
37+
};
38+
return link;
39+
});
2040
}
2141
}
2242

addons/xterm-addon-web-links/src/WebLinksAddon.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* @license MIT
44
*/
55

6-
import { Terminal, ILinkMatcherOptions, ITerminalAddon, IDisposable } from 'xterm';
6+
import { Terminal, ILinkMatcherOptions, ITerminalAddon, IDisposable, IViewportRange } from 'xterm';
77
import { WebLinkProvider } from './WebLinkProvider';
88

99
const protocolClause = '(https?:\\/\\/)';
@@ -36,27 +36,34 @@ function handleLink(event: MouseEvent, uri: string): void {
3636
}
3737
}
3838

39+
interface ILinkProviderOptions {
40+
hover?(event: MouseEvent, text: string, location: IViewportRange): void;
41+
leave?(event: MouseEvent, text: string): void;
42+
}
43+
3944
export class WebLinksAddon implements ITerminalAddon {
4045
private _linkMatcherId: number | undefined;
4146
private _terminal: Terminal | undefined;
4247
private _linkProvider: IDisposable | undefined;
4348

4449
constructor(
4550
private _handler: (event: MouseEvent, uri: string) => void = handleLink,
46-
private _options: ILinkMatcherOptions = {},
51+
private _options: ILinkMatcherOptions | ILinkProviderOptions = {},
4752
private _useLinkProvider: boolean = false
4853
) {
49-
this._options.matchIndex = 1;
5054
}
5155

5256
public activate(terminal: Terminal): void {
5357
this._terminal = terminal;
5458

5559
if (this._useLinkProvider && 'registerLinkProvider' in this._terminal) {
56-
this._linkProvider = this._terminal.registerLinkProvider(new WebLinkProvider(this._terminal, strictUrlRegex, this._handler));
60+
const options = this._options as ILinkProviderOptions;
61+
this._linkProvider = this._terminal.registerLinkProvider(new WebLinkProvider(this._terminal, strictUrlRegex, this._handler, options));
5762
} else {
5863
// TODO: This should be removed eventually
59-
this._linkMatcherId = (this._terminal as Terminal).registerLinkMatcher(strictUrlRegex, this._handler, this._options);
64+
const options = this._options as ILinkMatcherOptions;
65+
options.matchIndex = 1;
66+
this._linkMatcherId = (this._terminal as Terminal).registerLinkMatcher(strictUrlRegex, this._handler, options);
6067
}
6168
}
6269

addons/xterm-addon-web-links/typings/xterm-addon-web-links.d.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*/
55

66

7-
import { Terminal, ILinkMatcherOptions, ITerminalAddon } from 'xterm';
7+
import { Terminal, ILinkMatcherOptions, ITerminalAddon, IViewportRange } from 'xterm';
88

99
declare module 'xterm-addon-web-links' {
1010
/**
@@ -20,7 +20,7 @@ declare module 'xterm-addon-web-links' {
2020
* link provider (new) may cause issues. Link provider will eventually be
2121
* the default and only option.
2222
*/
23-
constructor(handler?: (event: MouseEvent, uri: string) => void, options?: ILinkMatcherOptions, useLinkProvider?: boolean);
23+
constructor(handler?: (event: MouseEvent, uri: string) => void, options?: ILinkMatcherOptions | ILinkProviderOptions, useLinkProvider?: boolean);
2424

2525
/**
2626
* Activates the addon
@@ -33,4 +33,21 @@ declare module 'xterm-addon-web-links' {
3333
*/
3434
public dispose(): void;
3535
}
36+
37+
/**
38+
* An object containing options for a link provider.
39+
*/
40+
export interface ILinkProviderOptions {
41+
/**
42+
* A callback that fires when the mouse hovers over a link for a period of
43+
* time (defined by {@link ITerminalOptions.linkTooltipHoverDuration}).
44+
*/
45+
hover?(event: MouseEvent, text: string, location: IViewportRange): void;
46+
47+
/**
48+
* A callback that fires when the mouse leaves a link. Note that this can
49+
* happen even when tooltipCallback hasn't fired for the link yet.
50+
*/
51+
leave?(event: MouseEvent, text: string): void;
52+
}
3653
}

0 commit comments

Comments
 (0)