Skip to content

Commit

Permalink
Add hover and leave callbacks for linkprovider
Browse files Browse the repository at this point in the history
  • Loading branch information
equada authored and marvinthepa committed Feb 3, 2021
1 parent 36136a4 commit eec804d
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 5 deletions.
21 changes: 18 additions & 3 deletions addons/xterm-addon-web-links/src/WebLinkProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,35 @@
* @license MIT
*/

import { ILinkProvider, IBufferCellPosition, ILink, Terminal } from 'xterm';
import { ILinkProvider, ILink, Terminal, IViewportRange, ILinkProviderOptions } from 'xterm';

export class WebLinkProvider implements ILinkProvider {

constructor(
private readonly _terminal: Terminal,
private readonly _regex: RegExp,
private readonly _handler: (event: MouseEvent, uri: string) => void
private readonly _handler: (event: MouseEvent, uri: string) => void,
private readonly _options: ILinkProviderOptions = {}
) {

}

public provideLinks(y: number, callback: (links: ILink[] | undefined) => void): void {
callback(LinkComputer.computeLink(y, this._regex, this._terminal, this._handler));
const links = LinkComputer.computeLink(y, this._regex, this._terminal, this._handler);
callback(this._addCallbacks(links));
}

private _addCallbacks(links: ILink[]): ILink[] {
return links.map(link => {
link.leave = this._options.leave;
link.hover = (event: MouseEvent, uri: string): void => {
if (this._options.hover) {
const { range } = link;
this._options.hover(event, uri, range);
}
};
return link;
});
}
}

Expand Down
8 changes: 6 additions & 2 deletions addons/xterm-addon-web-links/src/WebLinksAddon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* @license MIT
*/

import { Terminal, ILinkMatcherOptions, ITerminalAddon, IDisposable } from 'xterm';
import { Terminal, ILinkMatcherOptions, ITerminalAddon, IDisposable, ILinkProviderOptions } from 'xterm';
import { WebLinkProvider } from './WebLinkProvider';

const protocolClause = '(https?:\\/\\/)';
Expand Down Expand Up @@ -53,7 +53,11 @@ export class WebLinksAddon implements ITerminalAddon {
this._terminal = terminal;

if (this._useLinkProvider && 'registerLinkProvider' in this._terminal) {
this._linkProvider = this._terminal.registerLinkProvider(new WebLinkProvider(this._terminal, strictUrlRegex, this._handler));
const options = {
hover: this._options.tooltipCallback,
leave: this._options.leaveCallback
};
this._linkProvider = this._terminal.registerLinkProvider(new WebLinkProvider(this._terminal, strictUrlRegex, this._handler, options));
} else {
// TODO: This should be removed eventually
this._linkMatcherId = (this._terminal as Terminal).registerLinkMatcher(strictUrlRegex, this._handler, this._options);
Expand Down
17 changes: 17 additions & 0 deletions typings/xterm.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,23 @@ declare module 'xterm' {
willLinkActivate?: (event: MouseEvent, uri: string) => boolean;
}

/**
* An object containing options for a link provider.
*/
export interface ILinkProviderOptions {
/**
* A callback that fires when the mouse hovers over a link for a period of
* time (defined by {@link ITerminalOptions.linkTooltipHoverDuration}).
*/
hover?(event: MouseEvent, text: string, location: IViewportRange): void;

/**
* A callback that fires when the mouse leaves a link. Note that this can
* happen even when tooltipCallback hasn't fired for the link yet.
*/
leave?(event: MouseEvent, text: string): void;
}

/**
* An object that can be disposed via a dispose function.
*/
Expand Down

0 comments on commit eec804d

Please sign in to comment.