Skip to content

Commit

Permalink
Better aproach, check i a selection is being performed
Browse files Browse the repository at this point in the history
  • Loading branch information
bmf-ribeiro committed Mar 11, 2019
1 parent c16e3e5 commit 62cfa64
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
11 changes: 3 additions & 8 deletions src/addons/webLinks/webLinks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ const start = '(?:^|' + negatedDomainCharacterSet + ')(';
const end = ')($|' + negatedPathCharacterSet + ')';
const strictUrlRegex = new RegExp(start + protocolClause + bodyClause + end);

let handleLink: (event: MouseEvent, uri: string) => void;
function handleLink(event: MouseEvent, uri: string): void {
window.open(uri, '_blank');
}

/**
* Initialize the web links addon, registering the link matcher.
Expand All @@ -34,18 +36,11 @@ let handleLink: (event: MouseEvent, uri: string) => void;
*/
export function webLinksInit(term: Terminal, handler: (event: MouseEvent, uri: string) => void = handleLink, options: ILinkMatcherOptions = {}): void {
options.matchIndex = 1;

term.registerLinkMatcher(strictUrlRegex, handler, options);
}

export function apply(terminalConstructor: typeof Terminal): void {
(<any>terminalConstructor.prototype).webLinksInit = function (handler?: (event: MouseEvent, uri: string) => void, options?: ILinkMatcherOptions): void {
handleLink = (event, uri) => {
if (!this.hasSelection()) {
window.open(uri, '_blank');
}
};

webLinksInit(this, handler, options);
};
}
12 changes: 10 additions & 2 deletions src/ui/MouseZoneManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export class MouseZoneManager extends Disposable implements IMouseZoneManager {
private _tooltipTimeout: number = null;
private _currentZone: IMouseZone = null;
private _lastHoverCoords: [number, number] = [null, null];
private _initialSelectionLenght: number;

constructor(
private _terminal: ITerminal
Expand Down Expand Up @@ -157,6 +158,10 @@ export class MouseZoneManager extends Disposable implements IMouseZoneManager {
}

private _onMouseDown(e: MouseEvent): void {
// Store current terminal selection length, to check if we're performing
// a selection operation
this._initialSelectionLenght = this._terminal.getSelection().length;

// Ignore the event if there are no zones active
if (!this._areZonesActive) {
return;
Expand Down Expand Up @@ -186,9 +191,12 @@ export class MouseZoneManager extends Disposable implements IMouseZoneManager {
}

private _onClick(e: MouseEvent): void {
// Find the active zone and click it if found
// Find the active zone and click it if found and no selection was
// being performed
const zone = this._findZoneEventAt(e);
if (zone) {
const currentSelectionLength = this._terminal.getSelection().length;

if (zone && currentSelectionLength === this._initialSelectionLenght) {
zone.clickCallback(e);
e.preventDefault();
e.stopImmediatePropagation();
Expand Down

0 comments on commit 62cfa64

Please sign in to comment.