Add fail handler

This commit is contained in:
Daniel Imms
2017-03-04 14:01:34 -08:00
parent 15a867d2b7
commit 1ecc5674ac
3 changed files with 21 additions and 6 deletions
+6
View File
@@ -85,6 +85,12 @@ export interface LinkMatcherOptions {
* false if invalid.
*/
validationCallback?: LinkMatcherValidationCallback;
/**
* A callback that fires when a link click fails due to ctrl or cmd (mac) not
* being pressed. This allows hinting to the user after a likely failed
* click.
*/
clickFailCallback?: (event: MouseEvent) => any;
/**
* The priority of the link matcher, this defines the order in which the link
* matcher is evaluated relative to others, from highest to lowest. The
+14 -6
View File
@@ -100,6 +100,7 @@ export class Linkifier {
handler,
matchIndex: options.matchIndex,
validationCallback: options.validationCallback,
clickFailCallback: options.clickFailCallback,
priority: options.priority || 0
};
this._addLinkMatcherToList(matcher);
@@ -158,7 +159,7 @@ export class Linkifier {
const matcher = this._linkMatchers[i];
const uri = this._findLinkMatch(text, matcher.regex, matcher.matchIndex);
if (uri) {
const linkElement = this._doLinkifyRow(rowIndex, uri, matcher.handler);
const linkElement = this._doLinkifyRow(rowIndex, uri, matcher.handler, matcher.clickFailCallback);
// Fire validation callback
if (linkElement && matcher.validationCallback) {
matcher.validationCallback(uri, isValid => {
@@ -180,14 +181,14 @@ export class Linkifier {
* @param {handler} handler The handler to trigger when the link is triggered.
* @return The link element if it was added, otherwise undefined.
*/
private _doLinkifyRow(rowIndex: number, uri: string, handler?: LinkMatcherHandler): HTMLElement {
private _doLinkifyRow(rowIndex: number, uri: string, handler?: LinkMatcherHandler, clickFailHandler?: (event: MouseEvent) => any): HTMLElement {
// Iterate over nodes as we want to consider text nodes
const nodes = this._rows[rowIndex].childNodes;
for (let i = 0; i < nodes.length; i++) {
const node = nodes[i];
const searchIndex = node.textContent.indexOf(uri);
if (searchIndex >= 0) {
const linkElement = this._createAnchorElement(uri, handler);
const linkElement = this._createAnchorElement(uri, handler, clickFailHandler);
if (node.textContent.length === uri.length) {
// Matches entire string
@@ -230,11 +231,11 @@ export class Linkifier {
* @param {string} uri The uri of the link.
* @return {HTMLAnchorElement} The link.
*/
private _createAnchorElement(uri: string, handler: LinkMatcherHandler): HTMLAnchorElement {
private _createAnchorElement(uri: string, handler: LinkMatcherHandler, failHandler: (event: MouseEvent) => any): HTMLAnchorElement {
const element = this._document.createElement('a');
element.textContent = uri;
if (handler) {
element.addEventListener('click', (event: KeyboardEvent) => {
element.addEventListener('click', (event: MouseEvent) => {
// Don't execute the handler if the link is flagged as invalid
if (element.classList.contains(INVALID_LINK_CLASS)) {
return;
@@ -242,15 +243,22 @@ export class Linkifier {
// Require ctrl on click
if (isMac ? event.metaKey : event.ctrlKey) {
handler(uri);
} else {
if (failHandler) {
failHandler(event);
}
}
});
} else {
element.href = uri;
// Force link on another tab so work is not lost
element.target = '_blank';
element.addEventListener('click', (event: KeyboardEvent) => {
element.addEventListener('click', (event: MouseEvent) => {
// Require ctrl on click
if (isMac ? !event.metaKey : !event.ctrlKey) {
if (failHandler) {
failHandler(event);
}
event.preventDefault();
return false;
}
+1
View File
@@ -8,6 +8,7 @@ export type LinkMatcher = {
handler: LinkMatcherHandler,
matchIndex?: number,
validationCallback?: LinkMatcherValidationCallback,
clickFailCallback?: (event: MouseEvent) => any,
priority?: number
};
export type LinkMatcherHandler = (uri: string) => void;