Enable custom http link handlers
This commit is contained in:
+13
-17
@@ -28,7 +28,7 @@ export type LinkHandler = (uri: string) => void;
|
|||||||
export class Linkifier {
|
export class Linkifier {
|
||||||
private _rows: HTMLElement[];
|
private _rows: HTMLElement[];
|
||||||
private _rowTimeoutIds: number[];
|
private _rowTimeoutIds: number[];
|
||||||
private _webLinkHandler: LinkHandler;
|
private _hypertextLinkHandler: LinkHandler;
|
||||||
|
|
||||||
constructor(rows: HTMLElement[]) {
|
constructor(rows: HTMLElement[]) {
|
||||||
this._rows = rows;
|
this._rows = rows;
|
||||||
@@ -48,9 +48,8 @@ export class Linkifier {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Support local links
|
// TODO: Support local links
|
||||||
public attachWebLinkHandler(handler: LinkHandler): void {
|
public attachHypertextLinkHandler(handler: LinkHandler): void {
|
||||||
this._webLinkHandler = handler;
|
this._hypertextLinkHandler = handler;
|
||||||
// TODO: Refresh links if a handler is attached?
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -96,10 +95,6 @@ export class Linkifier {
|
|||||||
this._replaceNodeSubstringWithNode(node, linkElement, uri, searchIndex);
|
this._replaceNodeSubstringWithNode(node, linkElement, uri, searchIndex);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Continue searching in case multiple URIs exist on a single
|
|
||||||
// const link = '<a href="' + uri + '">' + uri + '</a>';
|
|
||||||
// const newHtml = rowHtml.replace(uri, link);
|
|
||||||
// this._rows[rowIndex].innerHTML = newHtml;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -124,13 +119,12 @@ export class Linkifier {
|
|||||||
private _createAnchorElement(uri: string): HTMLAnchorElement {
|
private _createAnchorElement(uri: string): HTMLAnchorElement {
|
||||||
const element = document.createElement('a');
|
const element = document.createElement('a');
|
||||||
element.textContent = uri;
|
element.textContent = uri;
|
||||||
// Force link on another tab so work is not lost
|
if (this._hypertextLinkHandler) {
|
||||||
element.target = '_blank';
|
element.addEventListener('click', () => this._hypertextLinkHandler(uri));
|
||||||
if (this._webLinkHandler) {
|
|
||||||
element.href = '#';
|
|
||||||
element.addEventListener('click', () => this._webLinkHandler(uri));
|
|
||||||
} else {
|
} else {
|
||||||
element.href = uri;
|
element.href = uri;
|
||||||
|
// Force link on another tab so work is not lost
|
||||||
|
element.target = '_blank';
|
||||||
}
|
}
|
||||||
return element;
|
return element;
|
||||||
}
|
}
|
||||||
@@ -150,7 +144,8 @@ export class Linkifier {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Replace a substring within a node with a new node.
|
* Replace a substring within a node with a new node.
|
||||||
* @param {Node} targetNode The target node.
|
* @param {Node} targetNode The target node; either a text node or a <span>
|
||||||
|
* containing a single text node.
|
||||||
* @param {Node} newNode The new node to insert.
|
* @param {Node} newNode The new node to insert.
|
||||||
* @param {string} substring The substring to replace.
|
* @param {string} substring The substring to replace.
|
||||||
* @param {number} substringIndex The index of the substring within the string.
|
* @param {number} substringIndex The index of the substring within the string.
|
||||||
@@ -160,9 +155,10 @@ export class Linkifier {
|
|||||||
if (node.nodeType !== Node.TEXT_NODE) {
|
if (node.nodeType !== Node.TEXT_NODE) {
|
||||||
node = node.childNodes[0];
|
node = node.childNodes[0];
|
||||||
}
|
}
|
||||||
// The targetNode will be either a text node or a <span>. The targetNode is
|
|
||||||
// assumed to have no children. In either case, the targetNode's text node
|
// The targetNode will be either a text node or a <span>. The text node
|
||||||
// must be split into 2 text nodes surrounding the newNode.
|
// (targetNode or its only-child) needs to be replaced with newNode plus new
|
||||||
|
// text nodes potentially on either side.
|
||||||
if (node.childNodes.length === 0 && node.nodeType !== Node.TEXT_NODE) {
|
if (node.childNodes.length === 0 && node.nodeType !== Node.TEXT_NODE) {
|
||||||
throw new Error('targetNode must be a text node or only contain a single text node');
|
throw new Error('targetNode must be a text node or only contain a single text node');
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -73,6 +73,12 @@
|
|||||||
|
|
||||||
.terminal a {
|
.terminal a {
|
||||||
color: inherit;
|
color: inherit;
|
||||||
|
/* Feature underline on links even if they have no href */
|
||||||
|
text-decoration: underline;
|
||||||
|
}
|
||||||
|
|
||||||
|
.terminal a:hover {
|
||||||
|
cursor: pointer;
|
||||||
}
|
}
|
||||||
|
|
||||||
.terminal:not(.xterm-cursor-style-underline):not(.xterm-cursor-style-bar) .terminal-cursor {
|
.terminal:not(.xterm-cursor-style-underline):not(.xterm-cursor-style-bar) .terminal-cursor {
|
||||||
|
|||||||
@@ -1274,6 +1274,16 @@ Terminal.prototype.attachCustomKeydownHandler = function(customKeydownHandler) {
|
|||||||
this.customKeydownHandler = customKeydownHandler;
|
this.customKeydownHandler = customKeydownHandler;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: Doc
|
||||||
|
Terminal.prototype.attachHypertextLinkHandler = function(handler) {
|
||||||
|
if (!this.linkifier) {
|
||||||
|
throw new Error('Cannot attach a hypertext link handler before Terminal.open is called');
|
||||||
|
}
|
||||||
|
this.linkifier.attachHypertextLinkHandler(handler);
|
||||||
|
// Refresh to force links to refresh
|
||||||
|
this.refresh(0, this.rows - 1);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Handle a keydown event
|
* Handle a keydown event
|
||||||
* Key Resources:
|
* Key Resources:
|
||||||
|
|||||||
Reference in New Issue
Block a user