diff --git a/src/Linkifier.ts b/src/Linkifier.ts index bfac87abb..f92badcf7 100644 --- a/src/Linkifier.ts +++ b/src/Linkifier.ts @@ -217,7 +217,7 @@ export class Linkifier { // Iterate over nodes as we want to consider text nodes let result = []; const isHttpLinkMatcher = matcher.id === HYPERTEXT_LINK_MATCHER_ID; -console.log('linkifyRow: ', text); + // Find the first match let match = text.match(matcher.regex); if (!match || match.length === 0) { @@ -228,29 +228,32 @@ console.log('linkifyRow: ', text); // TODO: Match more than one link per row // Set the next searches start index // let rowStartIndex = match.index + uri.length; -console.log('matcher', matcher); + + // Get index, match.index is for the outer match which includes negated chars + const index = text.indexOf(uri); + // Ensure the link is valid before registering if (matcher.validationCallback) { matcher.validationCallback(text, isValid => { if (isValid) { // TODO: Discard link if the line has already changed? - this._addLink(match.index, match.length, uri, matcher); + this._addLink(index, rowIndex, uri, matcher); } }); } else { - this._addLink(match.index, match.length, uri, matcher); + this._addLink(index, rowIndex, uri, matcher); } } - private _addLink(x: number, length: number, uri: string, matcher: LinkMatcher): void { + private _addLink(x: number, y: number, uri: string, matcher: LinkMatcher): void { this._mouseZoneManager.add(new MouseZone( - x, - x + length, + x + 1, + x + 1 + uri.length, + y + 1, e => { if (matcher.hoverCallback) { return matcher.hoverCallback(e, uri); } - console.log('hover', uri); }, e => { if (matcher.handler) { diff --git a/src/input/Interfaces.ts b/src/input/Interfaces.ts index 9bb90fed6..d0c0e4804 100644 --- a/src/input/Interfaces.ts +++ b/src/input/Interfaces.ts @@ -6,6 +6,7 @@ export interface IMouseZoneManager { export interface IMouseZone { x1: number; x2: number; + y: number; hoverCallback: (e: MouseEvent) => any; clickCallback: (e: MouseEvent) => any; } diff --git a/src/input/MouseZoneManager.ts b/src/input/MouseZoneManager.ts index 7e63ee0bb..6d8f017a4 100644 --- a/src/input/MouseZoneManager.ts +++ b/src/input/MouseZoneManager.ts @@ -1,5 +1,6 @@ import { IMouseZoneManager, IMouseZone } from './Interfaces'; import { ITerminal } from '../Interfaces'; +import { getCoords } from '../utils/Mouse'; /** * The MouseZoneManager allows components to register zones within the terminal @@ -26,7 +27,6 @@ export class MouseZoneManager implements IMouseZoneManager { } public add(zone: IMouseZone): void { - console.log('add zone', zone); this._zones.push(zone); if (this._zones.length === 1) { this._activate(); @@ -40,7 +40,6 @@ export class MouseZoneManager implements IMouseZoneManager { private _activate(): void { if (!this._areZonesActive) { - console.log('_addMoveListener'); this._areZonesActive = true; this._terminal.element.addEventListener('mousemove', this._mouseMoveListener); this._terminal.element.addEventListener('click', this._clickListener); @@ -49,7 +48,6 @@ export class MouseZoneManager implements IMouseZoneManager { private _deactivate(): void { if (this._areZonesActive) { - console.log('_removeMoveListener'); this._areZonesActive = false; this._terminal.element.removeEventListener('mousemove', this._mouseMoveListener); this._terminal.element.removeEventListener('click', this._clickListener); @@ -57,7 +55,6 @@ export class MouseZoneManager implements IMouseZoneManager { } private _onMouseMove(e: MouseEvent): void { - console.log('move'); // TODO: Handle hover } @@ -70,7 +67,14 @@ export class MouseZoneManager implements IMouseZoneManager { } private _findZoneEventAt(e: MouseEvent): IMouseZone { - return this._zones[0]; + const coords = getCoords(e, this._terminal.element, this._terminal.charMeasure,this._terminal.options.lineHeight, this._terminal.cols, this._terminal.rows); + for (let i = 0; i < this._zones.length; i++) { + const zone = this._zones[i]; + if (zone.y === coords[1] && zone.x1 <= coords[0] && zone.x2 > coords[0]) { + return zone; + } + }; + return null; } } @@ -78,6 +82,7 @@ export class MouseZone implements IMouseZone { constructor( public x1: number, public x2: number, + public y: number, public hoverCallback: (e: MouseEvent) => any, public clickCallback: (e: MouseEvent) => any ) { diff --git a/src/utils/Mouse.ts b/src/utils/Mouse.ts index 77c422d5e..0d433cd6f 100644 --- a/src/utils/Mouse.ts +++ b/src/utils/Mouse.ts @@ -2,7 +2,7 @@ * @license MIT */ -import { CharMeasure } from './CharMeasure'; +import { ICharMeasure } from '../Interfaces'; export function getCoordsRelativeToElement(event: MouseEvent, element: HTMLElement): [number, number] { // Ignore browsers that don't support MouseEvent.pageX @@ -36,7 +36,7 @@ export function getCoordsRelativeToElement(event: MouseEvent, element: HTMLEleme * apply an offset to the x value such that the left half of the cell will * select that cell and the right half will select the next cell. */ -export function getCoords(event: MouseEvent, element: HTMLElement, charMeasure: CharMeasure, lineHeight: number, colCount: number, rowCount: number, isSelection?: boolean): [number, number] { +export function getCoords(event: MouseEvent, element: HTMLElement, charMeasure: ICharMeasure, lineHeight: number, colCount: number, rowCount: number, isSelection?: boolean): [number, number] { // Coordinates cannot be measured if charMeasure has not been initialized if (!charMeasure.width || !charMeasure.height) { return null; @@ -68,7 +68,7 @@ export function getCoords(event: MouseEvent, element: HTMLElement, charMeasure: * @param colCount The number of columns in the terminal. * @param rowCount The number of rows in the terminal. */ -export function getRawByteCoords(event: MouseEvent, element: HTMLElement, charMeasure: CharMeasure, lineHeight: number, colCount: number, rowCount: number): { x: number, y: number } { +export function getRawByteCoords(event: MouseEvent, element: HTMLElement, charMeasure: ICharMeasure, lineHeight: number, colCount: number, rowCount: number): { x: number, y: number } { const coords = getCoords(event, element, charMeasure, lineHeight, colCount, rowCount); let x = coords[0]; let y = coords[1];