Merge pull request #3233 from marvinthepa/3056_hover_callback_link_providers

Add hover and leave callbacks for linkprovider
This commit is contained in:
Daniel Imms
2021-02-03 05:35:15 -08:00
committed by GitHub
3 changed files with 54 additions and 10 deletions
@@ -3,20 +3,40 @@
* @license MIT
*/
import { ILinkProvider, IBufferCellPosition, ILink, Terminal } from 'xterm';
import { ILinkProvider, ILink, Terminal, IViewportRange } from 'xterm';
interface ILinkProviderOptions {
hover?(event: MouseEvent, text: string, location: IViewportRange): void;
leave?(event: MouseEvent, text: string): void;
}
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;
});
}
}
@@ -3,7 +3,7 @@
* @license MIT
*/
import { Terminal, ILinkMatcherOptions, ITerminalAddon, IDisposable } from 'xterm';
import { Terminal, ILinkMatcherOptions, ITerminalAddon, IDisposable, IViewportRange } from 'xterm';
import { WebLinkProvider } from './WebLinkProvider';
const protocolClause = '(https?:\\/\\/)';
@@ -36,6 +36,11 @@ function handleLink(event: MouseEvent, uri: string): void {
}
}
interface ILinkProviderOptions {
hover?(event: MouseEvent, text: string, location: IViewportRange): void;
leave?(event: MouseEvent, text: string): void;
}
export class WebLinksAddon implements ITerminalAddon {
private _linkMatcherId: number | undefined;
private _terminal: Terminal | undefined;
@@ -43,20 +48,22 @@ export class WebLinksAddon implements ITerminalAddon {
constructor(
private _handler: (event: MouseEvent, uri: string) => void = handleLink,
private _options: ILinkMatcherOptions = {},
private _options: ILinkMatcherOptions | ILinkProviderOptions = {},
private _useLinkProvider: boolean = false
) {
this._options.matchIndex = 1;
}
public activate(terminal: Terminal): void {
this._terminal = terminal;
if (this._useLinkProvider && 'registerLinkProvider' in this._terminal) {
this._linkProvider = this._terminal.registerLinkProvider(new WebLinkProvider(this._terminal, strictUrlRegex, this._handler));
const options = this._options as ILinkProviderOptions;
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);
const options = this._options as ILinkMatcherOptions;
options.matchIndex = 1;
this._linkMatcherId = (this._terminal as Terminal).registerLinkMatcher(strictUrlRegex, this._handler, options);
}
}
@@ -4,7 +4,7 @@
*/
import { Terminal, ILinkMatcherOptions, ITerminalAddon } from 'xterm';
import { Terminal, ILinkMatcherOptions, ITerminalAddon, IViewportRange } from 'xterm';
declare module 'xterm-addon-web-links' {
/**
@@ -20,7 +20,7 @@ declare module 'xterm-addon-web-links' {
* link provider (new) may cause issues. Link provider will eventually be
* the default and only option.
*/
constructor(handler?: (event: MouseEvent, uri: string) => void, options?: ILinkMatcherOptions, useLinkProvider?: boolean);
constructor(handler?: (event: MouseEvent, uri: string) => void, options?: ILinkMatcherOptions | ILinkProviderOptions, useLinkProvider?: boolean);
/**
* Activates the addon
@@ -33,4 +33,21 @@ declare module 'xterm-addon-web-links' {
*/
public dispose(): void;
}
/**
* 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;
}
}