@@ -3,20 +3,35 @@
|
|||||||
* @license MIT
|
* @license MIT
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { ILinkProvider, IBufferCellPosition, ILink, Terminal } from 'xterm';
|
import { ILinkProvider, ILink, Terminal, IViewportRange, ILinkProviderOptions } from 'xterm';
|
||||||
|
|
||||||
export class WebLinkProvider implements ILinkProvider {
|
export class WebLinkProvider implements ILinkProvider {
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
private readonly _terminal: Terminal,
|
private readonly _terminal: Terminal,
|
||||||
private readonly _regex: RegExp,
|
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 {
|
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
|
* @license MIT
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { Terminal, ILinkMatcherOptions, ITerminalAddon, IDisposable } from 'xterm';
|
import { Terminal, ILinkMatcherOptions, ITerminalAddon, IDisposable, ILinkProviderOptions } from 'xterm';
|
||||||
import { WebLinkProvider } from './WebLinkProvider';
|
import { WebLinkProvider } from './WebLinkProvider';
|
||||||
|
|
||||||
const protocolClause = '(https?:\\/\\/)';
|
const protocolClause = '(https?:\\/\\/)';
|
||||||
@@ -53,7 +53,11 @@ export class WebLinksAddon implements ITerminalAddon {
|
|||||||
this._terminal = terminal;
|
this._terminal = terminal;
|
||||||
|
|
||||||
if (this._useLinkProvider && 'registerLinkProvider' in this._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 {
|
} else {
|
||||||
// TODO: This should be removed eventually
|
// TODO: This should be removed eventually
|
||||||
this._linkMatcherId = (this._terminal as Terminal).registerLinkMatcher(strictUrlRegex, this._handler, this._options);
|
this._linkMatcherId = (this._terminal as Terminal).registerLinkMatcher(strictUrlRegex, this._handler, this._options);
|
||||||
|
|||||||
Vendored
+17
@@ -352,6 +352,23 @@ declare module 'xterm' {
|
|||||||
willLinkActivate?: (event: MouseEvent, uri: string) => boolean;
|
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.
|
* An object that can be disposed via a dispose function.
|
||||||
*/
|
*/
|
||||||
|
|||||||
Reference in New Issue
Block a user