@xterm/addon-web-links 0.10.0-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +19 -0
- package/README.md +21 -0
- package/lib/addon-web-links.js +2 -0
- package/lib/addon-web-links.js.map +1 -0
- package/package.json +26 -0
- package/src/WebLinkProvider.ts +198 -0
- package/src/WebLinksAddon.ts +57 -0
- package/typings/addon-web-links.d.ts +53 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
Copyright (c) 2017, The xterm.js authors (https://github.com/xtermjs/xterm.js)
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
4
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
5
|
+
in the Software without restriction, including without limitation the rights
|
|
6
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
7
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
8
|
+
furnished to do so, subject to the following conditions:
|
|
9
|
+
|
|
10
|
+
The above copyright notice and this permission notice shall be included in
|
|
11
|
+
all copies or substantial portions of the Software.
|
|
12
|
+
|
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
19
|
+
THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
## @xterm/addon-web-links
|
|
2
|
+
|
|
3
|
+
An addon for [xterm.js](https://github.com/xtermjs/xterm.js) that enables web links. This addon requires xterm.js v4+.
|
|
4
|
+
|
|
5
|
+
### Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install --save @xterm/addon-web-links
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
### Usage
|
|
12
|
+
|
|
13
|
+
```ts
|
|
14
|
+
import { Terminal } from '@xterm/xterm';
|
|
15
|
+
import { WebLinksAddon } from '@xterm/addon-web-links';
|
|
16
|
+
|
|
17
|
+
const terminal = new Terminal();
|
|
18
|
+
terminal.loadAddon(new WebLinksAddon());
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
See the full [API](https://github.com/xtermjs/xterm.js/blob/master/addons/addon-web-links/typings/addon-web-links.d.ts) for more advanced usage.
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.WebLinksAddon=t():e.WebLinksAddon=t()}(self,(()=>(()=>{"use strict";var e={6:(e,t)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.LinkComputer=t.WebLinkProvider=void 0,t.WebLinkProvider=class{constructor(e,t,n,i={}){this._terminal=e,this._regex=t,this._handler=n,this._options=i}provideLinks(e,t){const i=n.computeLink(e,this._regex,this._terminal,this._handler);t(this._addCallbacks(i))}_addCallbacks(e){return e.map((e=>(e.leave=this._options.leave,e.hover=(t,n)=>{if(this._options.hover){const{range:i}=e;this._options.hover(t,n,i)}},e)))}};class n{static computeLink(e,t,i,r){const o=new RegExp(t.source,(t.flags||"")+"g"),[s,a]=n._getWindowedLineStrings(e-1,i),c=s.join("");let d;const l=[];for(;d=o.exec(c);){const e=d[0];try{const t=new URL(e),n=decodeURI(t.toString());if(e!==n&&e+"/"!==n)continue}catch(e){continue}const[t,o]=n._mapStrIdx(i,a,0,d.index),[s,c]=n._mapStrIdx(i,t,o,e.length);if(-1===t||-1===o||-1===s||-1===c)continue;const p={start:{x:o+1,y:t+1},end:{x:c,y:s+1}};l.push({range:p,text:e,activate:r})}return l}static _getWindowedLineStrings(e,t){let n,i=e,r=e,o=0,s="";const a=[];if(n=t.buffer.active.getLine(e)){const e=n.translateToString(!0);if(n.isWrapped&&" "!==e[0]){for(o=0;(n=t.buffer.active.getLine(--i))&&o<2048&&(s=n.translateToString(!0),o+=s.length,a.push(s),n.isWrapped&&-1===s.indexOf(" ")););a.reverse()}for(a.push(e),o=0;(n=t.buffer.active.getLine(++r))&&n.isWrapped&&o<2048&&(s=n.translateToString(!0),o+=s.length,a.push(s),-1===s.indexOf(" ")););}return[a,i]}static _mapStrIdx(e,t,n,i){const r=e.buffer.active,o=r.getNullCell();let s=n;for(;i;){const e=r.getLine(t);if(!e)return[-1,-1];for(let n=s;n<e.length;++n){e.getCell(n,o);const s=o.getChars();if(o.getWidth()&&(i-=s.length||1,n===e.length-1&&""===s)){const e=r.getLine(t+1);e&&e.isWrapped&&(e.getCell(0,o),2===o.getWidth()&&(i+=1))}if(i<0)return[t,n]}t++,s=0}return[t,s]}}t.LinkComputer=n}},t={};function n(i){var r=t[i];if(void 0!==r)return r.exports;var o=t[i]={exports:{}};return e[i](o,o.exports,n),o.exports}var i={};return(()=>{var e=i;Object.defineProperty(e,"__esModule",{value:!0}),e.WebLinksAddon=void 0;const t=n(6),r=/https?:[/]{2}[^\s"'!*(){}|\\\^<>`]*[^\s"':,.!?{}|\\\^~\[\]`()<>]/;function o(e,t){const n=window.open();if(n){try{n.opener=null}catch{}n.location.href=t}else console.warn("Opening link blocked as opener could not be cleared")}e.WebLinksAddon=class{constructor(e=o,t={}){this._handler=e,this._options=t}activate(e){this._terminal=e;const n=this._options,i=n.urlRegex||r;this._linkProvider=this._terminal.registerLinkProvider(new t.WebLinkProvider(this._terminal,i,this._handler,n))}dispose(){this._linkProvider?.dispose()}}})(),i})()));
|
|
2
|
+
//# sour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C,SAAAA,CAEV,CAEO,QAAAkB,CAASD,GACdV,KAAKV,UAAYoB,EACjB,MAAMsE,EAAUhF,KAAKP,SACfgB,EAAQuE,EAAQC,UAAYX,EAClCtE,KAAKkF,cAAgBlF,KAAKV,UAAU6F,qBAAqB,IAAI,EAAAC,gBAAgBpF,KAAKV,UAAWmB,EAAOT,KAAKR,SAAUwF,GACrH,CAEO,OAAAK,GACLrF,KAAKkF,eAAeG,SACtB,E","sources":["webpack://WebLinksAddon/webpack/universalModuleDefinition","webpack://WebLinksAddon/./src/WebLinkProvider.ts","webpack://WebLinksAddon/webpack/bootstrap","webpack://WebLinksAddon/./src/WebLinksAddon.ts"],"sourcesContent":["(function webpackUniversalModuleDefinition(root, factory) {\n\tif(typeof exports === 'object' && typeof module === 'object')\n\t\tmodule.exports = factory();\n\telse if(typeof define === 'function' && define.amd)\n\t\tdefine([], factory);\n\telse if(typeof exports === 'object')\n\t\texports[\"WebLinksAddon\"] = factory();\n\telse\n\t\troot[\"WebLinksAddon\"] = factory();\n})(self, () => {\nreturn ","/**\n * Copyright (c) 2019 The xterm.js authors. All rights reserved.\n * @license MIT\n */\n\nimport { ILinkProvider, ILink, Terminal, IViewportRange, IBufferLine } from '@xterm/xterm';\n\nexport interface ILinkProviderOptions {\n hover?(event: MouseEvent, text: string, location: IViewportRange): void;\n leave?(event: MouseEvent, text: string): void;\n urlRegex?: RegExp;\n}\n\nexport class WebLinkProvider implements ILinkProvider {\n\n constructor(\n private readonly _terminal: Terminal,\n private readonly _regex: RegExp,\n private readonly _handler: (event: MouseEvent, uri: string) => void,\n private readonly _options: ILinkProviderOptions = {}\n ) {\n\n }\n\n public provideLinks(y: number, callback: (links: ILink[] | undefined) => void): void {\n const links = LinkComputer.computeLink(y, this._regex, this._terminal, this._handler);\n callback(this._addCallbacks(links));\n }\n\n private _addCallbacks(links: ILink[]): ILink[] {\n return links.map(link => {\n link.leave = this._options.leave;\n link.hover = (event: MouseEvent, uri: string): void => {\n if (this._options.hover) {\n const { range } = link;\n this._options.hover(event, uri, range);\n }\n };\n return link;\n });\n }\n}\n\nexport class LinkComputer {\n public static computeLink(y: number, regex: RegExp, terminal: Terminal, activate: (event: MouseEvent, uri: string) => void): ILink[] {\n const rex = new RegExp(regex.source, (regex.flags || '') + 'g');\n\n const [lines, startLineIndex] = LinkComputer._getWindowedLineStrings(y - 1, terminal);\n const line = lines.join('');\n\n let match;\n const result: ILink[] = [];\n\n while (match = rex.exec(line)) {\n const text = match[0];\n\n // check via URL if the matched text would form a proper url\n // NOTE: This outsources the ugly url parsing to the browser.\n // To avoid surprising auto expansion from URL we additionally\n // check afterwards if the provided string resembles the parsed\n // one close enough:\n // - decodeURI decode path segement back to byte repr\n // to detect unicode auto conversion correctly\n // - append / also match domain urls w'o any path notion\n try {\n const url = new URL(text);\n const urlText = decodeURI(url.toString());\n if (text !== urlText && text + '/' !== urlText) {\n continue;\n }\n } catch (e) {\n continue;\n }\n\n // map string positions back to buffer positions\n // values are 0-based right side excluding\n const [startY, startX] = LinkComputer._mapStrIdx(terminal, startLineIndex, 0, match.index);\n const [endY, endX] = LinkComputer._mapStrIdx(terminal, startY, startX, text.length);\n\n if (startY === -1 || startX === -1 || endY === -1 || endX === -1) {\n continue;\n }\n\n // range expects values 1-based right side including, thus +1 except for endX\n const range = {\n start: {\n x: startX + 1,\n y: startY + 1\n },\n end: {\n x: endX,\n y: endY + 1\n }\n };\n\n result.push({ range, text, activate });\n }\n\n return result;\n }\n\n /**\n * Get wrapped content lines for the current line index.\n * The top/bottom line expansion stops at whitespaces or length > 2048.\n * Returns an array with line strings and the top line index.\n *\n * NOTE: We pull line strings with trimRight=true on purpose to make sure\n * to correctly match urls with early wrapped wide chars. This corrupts the string index\n * for 1:1 backmapping to buffer positions, thus needs an additional correction in _mapStrIdx.\n */\n private static _getWindowedLineStrings(lineIndex: number, terminal: Terminal): [string[], number] {\n let line: IBufferLine | undefined;\n let topIdx = lineIndex;\n let bottomIdx = lineIndex;\n let length = 0;\n let content = '';\n const lines: string[] = [];\n\n if ((line = terminal.buffer.active.getLine(lineIndex))) {\n const currentContent = line.translateToString(true);\n\n // expand top, stop on whitespaces or length > 2048\n if (line.isWrapped && currentContent[0] !== ' ') {\n length = 0;\n while ((line = terminal.buffer.active.getLine(--topIdx)) && length < 2048) {\n content = line.translateToString(true);\n length += content.length;\n lines.push(content);\n if (!line.isWrapped || content.indexOf(' ') !== -1) {\n break;\n }\n }\n lines.reverse();\n }\n\n // append current line\n lines.push(currentContent);\n\n // expand bottom, stop on whitespaces or length > 2048\n length = 0;\n while ((line = terminal.buffer.active.getLine(++bottomIdx)) && line.isWrapped && length < 2048) {\n content = line.translateToString(true);\n length += content.length;\n lines.push(content);\n if (content.indexOf(' ') !== -1) {\n break;\n }\n }\n }\n return [lines, topIdx];\n }\n\n /**\n * Map a string index back to buffer positions.\n * Returns buffer position as [lineIndex, columnIndex] 0-based,\n * or [-1, -1] in case the lookup ran into a non-existing line.\n */\n private static _mapStrIdx(terminal: Terminal, lineIndex: number, rowIndex: number, stringIndex: number): [number, number] {\n const buf = terminal.buffer.active;\n const cell = buf.getNullCell();\n let start = rowIndex;\n while (stringIndex) {\n const line = buf.getLine(lineIndex);\n if (!line) {\n return [-1, -1];\n }\n for (let i = start; i < line.length; ++i) {\n line.getCell(i, cell);\n const chars = cell.getChars();\n const width = cell.getWidth();\n if (width) {\n stringIndex -= chars.length || 1;\n\n // correct stringIndex for early wrapped wide chars:\n // - currently only happens at last cell\n // - cells to the right are reset with chars='' and width=1 in InputHandler.print\n // - follow-up line must be wrapped and contain wide char at first cell\n // --> if all these conditions are met, correct stringIndex by +1\n if (i === line.length - 1 && chars === '') {\n const line = buf.getLine(lineIndex + 1);\n if (line && line.isWrapped) {\n line.getCell(0, cell);\n if (cell.getWidth() === 2) {\n stringIndex += 1;\n }\n }\n }\n }\n if (stringIndex < 0) {\n return [lineIndex, i];\n }\n }\n lineIndex++;\n start = 0;\n }\n return [lineIndex, start];\n }\n}\n","// The module cache\nvar __webpack_module_cache__ = {};\n\n// The require function\nfunction __webpack_require__(moduleId) {\n\t// Check if module is in cache\n\tvar cachedModule = __webpack_module_cache__[moduleId];\n\tif (cachedModule !== undefined) {\n\t\treturn cachedModule.exports;\n\t}\n\t// Create a new module (and put it into the cache)\n\tvar module = __webpack_module_cache__[moduleId] = {\n\t\t// no module.id needed\n\t\t// no module.loaded needed\n\t\texports: {}\n\t};\n\n\t// Execute the module function\n\t__webpack_modules__[moduleId](module, module.exports, __webpack_require__);\n\n\t// Return the exports of the module\n\treturn module.exports;\n}\n\n","/**\n * Copyright (c) 2019 The xterm.js authors. All rights reserved.\n * @license MIT\n */\n\nimport { Terminal, ITerminalAddon, IDisposable } from '@xterm/xterm';\nimport { ILinkProviderOptions, WebLinkProvider } from './WebLinkProvider';\n\n// consider everthing starting with http:// or https://\n// up to first whitespace, `\"` or `'` as url\n// NOTE: The repeated end clause is needed to not match a dangling `:`\n// resembling the old (...)*([^:\"\\'\\\\s]) final path clause\n// additionally exclude early + final:\n// - unsafe from rfc3986: !*'()\n// - unsafe chars from rfc1738: {}|\\^~[]` (minus [] as we need them for ipv6 adresses, also allow ~)\n// also exclude as finals:\n// - final interpunction like ,.!?\n// - any sort of brackets <>()[]{} (not spec conform, but often used to enclose urls)\n// - unsafe chars from rfc1738: {}|\\^~[]`\nconst strictUrlRegex = /https?:[/]{2}[^\\s\"'!*(){}|\\\\\\^<>`]*[^\\s\"':,.!?{}|\\\\\\^~\\[\\]`()<>]/;\n\n\nfunction handleLink(event: MouseEvent, uri: string): void {\n const newWindow = window.open();\n if (newWindow) {\n try {\n newWindow.opener = null;\n } catch {\n // no-op, Electron can throw\n }\n newWindow.location.href = uri;\n } else {\n console.warn('Opening link blocked as opener could not be cleared');\n }\n}\n\nexport class WebLinksAddon implements ITerminalAddon {\n private _terminal: Terminal | undefined;\n private _linkProvider: IDisposable | undefined;\n\n constructor(\n private _handler: (event: MouseEvent, uri: string) => void = handleLink,\n private _options: ILinkProviderOptions = {}\n ) {\n }\n\n public activate(terminal: Terminal): void {\n this._terminal = terminal;\n const options = this._options as ILinkProviderOptions;\n const regex = options.urlRegex || strictUrlRegex;\n this._linkProvider = this._terminal.registerLinkProvider(new WebLinkProvider(this._terminal, regex, this._handler, options));\n }\n\n public dispose(): void {\n this._linkProvider?.dispose();\n }\n}\n"],"names":["root","factory","exports","module","define","amd","self","constructor","_terminal","_regex","_handler","_options","provideLinks","y","callback","links","LinkComputer","computeLink","this","_addCallbacks","map","link","leave","hover","event","uri","range","regex","terminal","activate","rex","RegExp","source","flags","lines","startLineIndex","_getWindowedLineStrings","line","join","match","result","exec","text","url","URL","urlText","decodeURI","toString","e","startY","startX","_mapStrIdx","index","endY","endX","length","start","x","end","push","lineIndex","topIdx","bottomIdx","content","buffer","active","getLine","currentContent","translateToString","isWrapped","indexOf","reverse","rowIndex","stringIndex","buf","cell","getNullCell","i","getCell","chars","getChars","getWidth","__webpack_module_cache__","__webpack_require__","moduleId","cachedModule","undefined","__webpack_modules__","strictUrlRegex","handleLink","newWindow","window","open","opener","location","href","console","warn","options","urlRegex","_linkProvider","registerLinkProvider","WebLinkProvider","dispose"],"sourceRoot":""}
|
package/package.json
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@xterm/addon-web-links",
|
|
3
|
+
"version": "0.10.0-beta.1",
|
|
4
|
+
"author": {
|
|
5
|
+
"name": "The xterm.js authors",
|
|
6
|
+
"url": "https://xtermjs.org/"
|
|
7
|
+
},
|
|
8
|
+
"main": "lib/addon-web-links.js",
|
|
9
|
+
"types": "typings/addon-web-links.d.ts",
|
|
10
|
+
"repository": "https://github.com/xtermjs/xterm.js/tree/master/addons/addon-web-links",
|
|
11
|
+
"license": "MIT",
|
|
12
|
+
"keywords": [
|
|
13
|
+
"terminal",
|
|
14
|
+
"xterm",
|
|
15
|
+
"xterm.js"
|
|
16
|
+
],
|
|
17
|
+
"scripts": {
|
|
18
|
+
"build": "../../node_modules/.bin/tsc -p .",
|
|
19
|
+
"prepackage": "npm run build",
|
|
20
|
+
"package": "../../node_modules/.bin/webpack",
|
|
21
|
+
"prepublishOnly": "npm run package"
|
|
22
|
+
},
|
|
23
|
+
"peerDependencies": {
|
|
24
|
+
"xterm": "^5.0.0"
|
|
25
|
+
}
|
|
26
|
+
}
|
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) 2019 The xterm.js authors. All rights reserved.
|
|
3
|
+
* @license MIT
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { ILinkProvider, ILink, Terminal, IViewportRange, IBufferLine } from '@xterm/xterm';
|
|
7
|
+
|
|
8
|
+
export interface ILinkProviderOptions {
|
|
9
|
+
hover?(event: MouseEvent, text: string, location: IViewportRange): void;
|
|
10
|
+
leave?(event: MouseEvent, text: string): void;
|
|
11
|
+
urlRegex?: RegExp;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export class WebLinkProvider implements ILinkProvider {
|
|
15
|
+
|
|
16
|
+
constructor(
|
|
17
|
+
private readonly _terminal: Terminal,
|
|
18
|
+
private readonly _regex: RegExp,
|
|
19
|
+
private readonly _handler: (event: MouseEvent, uri: string) => void,
|
|
20
|
+
private readonly _options: ILinkProviderOptions = {}
|
|
21
|
+
) {
|
|
22
|
+
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
public provideLinks(y: number, callback: (links: ILink[] | undefined) => void): void {
|
|
26
|
+
const links = LinkComputer.computeLink(y, this._regex, this._terminal, this._handler);
|
|
27
|
+
callback(this._addCallbacks(links));
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
private _addCallbacks(links: ILink[]): ILink[] {
|
|
31
|
+
return links.map(link => {
|
|
32
|
+
link.leave = this._options.leave;
|
|
33
|
+
link.hover = (event: MouseEvent, uri: string): void => {
|
|
34
|
+
if (this._options.hover) {
|
|
35
|
+
const { range } = link;
|
|
36
|
+
this._options.hover(event, uri, range);
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
return link;
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export class LinkComputer {
|
|
45
|
+
public static computeLink(y: number, regex: RegExp, terminal: Terminal, activate: (event: MouseEvent, uri: string) => void): ILink[] {
|
|
46
|
+
const rex = new RegExp(regex.source, (regex.flags || '') + 'g');
|
|
47
|
+
|
|
48
|
+
const [lines, startLineIndex] = LinkComputer._getWindowedLineStrings(y - 1, terminal);
|
|
49
|
+
const line = lines.join('');
|
|
50
|
+
|
|
51
|
+
let match;
|
|
52
|
+
const result: ILink[] = [];
|
|
53
|
+
|
|
54
|
+
while (match = rex.exec(line)) {
|
|
55
|
+
const text = match[0];
|
|
56
|
+
|
|
57
|
+
// check via URL if the matched text would form a proper url
|
|
58
|
+
// NOTE: This outsources the ugly url parsing to the browser.
|
|
59
|
+
// To avoid surprising auto expansion from URL we additionally
|
|
60
|
+
// check afterwards if the provided string resembles the parsed
|
|
61
|
+
// one close enough:
|
|
62
|
+
// - decodeURI decode path segement back to byte repr
|
|
63
|
+
// to detect unicode auto conversion correctly
|
|
64
|
+
// - append / also match domain urls w'o any path notion
|
|
65
|
+
try {
|
|
66
|
+
const url = new URL(text);
|
|
67
|
+
const urlText = decodeURI(url.toString());
|
|
68
|
+
if (text !== urlText && text + '/' !== urlText) {
|
|
69
|
+
continue;
|
|
70
|
+
}
|
|
71
|
+
} catch (e) {
|
|
72
|
+
continue;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// map string positions back to buffer positions
|
|
76
|
+
// values are 0-based right side excluding
|
|
77
|
+
const [startY, startX] = LinkComputer._mapStrIdx(terminal, startLineIndex, 0, match.index);
|
|
78
|
+
const [endY, endX] = LinkComputer._mapStrIdx(terminal, startY, startX, text.length);
|
|
79
|
+
|
|
80
|
+
if (startY === -1 || startX === -1 || endY === -1 || endX === -1) {
|
|
81
|
+
continue;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// range expects values 1-based right side including, thus +1 except for endX
|
|
85
|
+
const range = {
|
|
86
|
+
start: {
|
|
87
|
+
x: startX + 1,
|
|
88
|
+
y: startY + 1
|
|
89
|
+
},
|
|
90
|
+
end: {
|
|
91
|
+
x: endX,
|
|
92
|
+
y: endY + 1
|
|
93
|
+
}
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
result.push({ range, text, activate });
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
return result;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Get wrapped content lines for the current line index.
|
|
104
|
+
* The top/bottom line expansion stops at whitespaces or length > 2048.
|
|
105
|
+
* Returns an array with line strings and the top line index.
|
|
106
|
+
*
|
|
107
|
+
* NOTE: We pull line strings with trimRight=true on purpose to make sure
|
|
108
|
+
* to correctly match urls with early wrapped wide chars. This corrupts the string index
|
|
109
|
+
* for 1:1 backmapping to buffer positions, thus needs an additional correction in _mapStrIdx.
|
|
110
|
+
*/
|
|
111
|
+
private static _getWindowedLineStrings(lineIndex: number, terminal: Terminal): [string[], number] {
|
|
112
|
+
let line: IBufferLine | undefined;
|
|
113
|
+
let topIdx = lineIndex;
|
|
114
|
+
let bottomIdx = lineIndex;
|
|
115
|
+
let length = 0;
|
|
116
|
+
let content = '';
|
|
117
|
+
const lines: string[] = [];
|
|
118
|
+
|
|
119
|
+
if ((line = terminal.buffer.active.getLine(lineIndex))) {
|
|
120
|
+
const currentContent = line.translateToString(true);
|
|
121
|
+
|
|
122
|
+
// expand top, stop on whitespaces or length > 2048
|
|
123
|
+
if (line.isWrapped && currentContent[0] !== ' ') {
|
|
124
|
+
length = 0;
|
|
125
|
+
while ((line = terminal.buffer.active.getLine(--topIdx)) && length < 2048) {
|
|
126
|
+
content = line.translateToString(true);
|
|
127
|
+
length += content.length;
|
|
128
|
+
lines.push(content);
|
|
129
|
+
if (!line.isWrapped || content.indexOf(' ') !== -1) {
|
|
130
|
+
break;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
lines.reverse();
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
// append current line
|
|
137
|
+
lines.push(currentContent);
|
|
138
|
+
|
|
139
|
+
// expand bottom, stop on whitespaces or length > 2048
|
|
140
|
+
length = 0;
|
|
141
|
+
while ((line = terminal.buffer.active.getLine(++bottomIdx)) && line.isWrapped && length < 2048) {
|
|
142
|
+
content = line.translateToString(true);
|
|
143
|
+
length += content.length;
|
|
144
|
+
lines.push(content);
|
|
145
|
+
if (content.indexOf(' ') !== -1) {
|
|
146
|
+
break;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
return [lines, topIdx];
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* Map a string index back to buffer positions.
|
|
155
|
+
* Returns buffer position as [lineIndex, columnIndex] 0-based,
|
|
156
|
+
* or [-1, -1] in case the lookup ran into a non-existing line.
|
|
157
|
+
*/
|
|
158
|
+
private static _mapStrIdx(terminal: Terminal, lineIndex: number, rowIndex: number, stringIndex: number): [number, number] {
|
|
159
|
+
const buf = terminal.buffer.active;
|
|
160
|
+
const cell = buf.getNullCell();
|
|
161
|
+
let start = rowIndex;
|
|
162
|
+
while (stringIndex) {
|
|
163
|
+
const line = buf.getLine(lineIndex);
|
|
164
|
+
if (!line) {
|
|
165
|
+
return [-1, -1];
|
|
166
|
+
}
|
|
167
|
+
for (let i = start; i < line.length; ++i) {
|
|
168
|
+
line.getCell(i, cell);
|
|
169
|
+
const chars = cell.getChars();
|
|
170
|
+
const width = cell.getWidth();
|
|
171
|
+
if (width) {
|
|
172
|
+
stringIndex -= chars.length || 1;
|
|
173
|
+
|
|
174
|
+
// correct stringIndex for early wrapped wide chars:
|
|
175
|
+
// - currently only happens at last cell
|
|
176
|
+
// - cells to the right are reset with chars='' and width=1 in InputHandler.print
|
|
177
|
+
// - follow-up line must be wrapped and contain wide char at first cell
|
|
178
|
+
// --> if all these conditions are met, correct stringIndex by +1
|
|
179
|
+
if (i === line.length - 1 && chars === '') {
|
|
180
|
+
const line = buf.getLine(lineIndex + 1);
|
|
181
|
+
if (line && line.isWrapped) {
|
|
182
|
+
line.getCell(0, cell);
|
|
183
|
+
if (cell.getWidth() === 2) {
|
|
184
|
+
stringIndex += 1;
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
if (stringIndex < 0) {
|
|
190
|
+
return [lineIndex, i];
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
lineIndex++;
|
|
194
|
+
start = 0;
|
|
195
|
+
}
|
|
196
|
+
return [lineIndex, start];
|
|
197
|
+
}
|
|
198
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) 2019 The xterm.js authors. All rights reserved.
|
|
3
|
+
* @license MIT
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { Terminal, ITerminalAddon, IDisposable } from '@xterm/xterm';
|
|
7
|
+
import { ILinkProviderOptions, WebLinkProvider } from './WebLinkProvider';
|
|
8
|
+
|
|
9
|
+
// consider everthing starting with http:// or https://
|
|
10
|
+
// up to first whitespace, `"` or `'` as url
|
|
11
|
+
// NOTE: The repeated end clause is needed to not match a dangling `:`
|
|
12
|
+
// resembling the old (...)*([^:"\'\\s]) final path clause
|
|
13
|
+
// additionally exclude early + final:
|
|
14
|
+
// - unsafe from rfc3986: !*'()
|
|
15
|
+
// - unsafe chars from rfc1738: {}|\^~[]` (minus [] as we need them for ipv6 adresses, also allow ~)
|
|
16
|
+
// also exclude as finals:
|
|
17
|
+
// - final interpunction like ,.!?
|
|
18
|
+
// - any sort of brackets <>()[]{} (not spec conform, but often used to enclose urls)
|
|
19
|
+
// - unsafe chars from rfc1738: {}|\^~[]`
|
|
20
|
+
const strictUrlRegex = /https?:[/]{2}[^\s"'!*(){}|\\\^<>`]*[^\s"':,.!?{}|\\\^~\[\]`()<>]/;
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
function handleLink(event: MouseEvent, uri: string): void {
|
|
24
|
+
const newWindow = window.open();
|
|
25
|
+
if (newWindow) {
|
|
26
|
+
try {
|
|
27
|
+
newWindow.opener = null;
|
|
28
|
+
} catch {
|
|
29
|
+
// no-op, Electron can throw
|
|
30
|
+
}
|
|
31
|
+
newWindow.location.href = uri;
|
|
32
|
+
} else {
|
|
33
|
+
console.warn('Opening link blocked as opener could not be cleared');
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export class WebLinksAddon implements ITerminalAddon {
|
|
38
|
+
private _terminal: Terminal | undefined;
|
|
39
|
+
private _linkProvider: IDisposable | undefined;
|
|
40
|
+
|
|
41
|
+
constructor(
|
|
42
|
+
private _handler: (event: MouseEvent, uri: string) => void = handleLink,
|
|
43
|
+
private _options: ILinkProviderOptions = {}
|
|
44
|
+
) {
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
public activate(terminal: Terminal): void {
|
|
48
|
+
this._terminal = terminal;
|
|
49
|
+
const options = this._options as ILinkProviderOptions;
|
|
50
|
+
const regex = options.urlRegex || strictUrlRegex;
|
|
51
|
+
this._linkProvider = this._terminal.registerLinkProvider(new WebLinkProvider(this._terminal, regex, this._handler, options));
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
public dispose(): void {
|
|
55
|
+
this._linkProvider?.dispose();
|
|
56
|
+
}
|
|
57
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) 2017 The xterm.js authors. All rights reserved.
|
|
3
|
+
* @license MIT
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
import { Terminal, ITerminalAddon, IViewportRange } from '@xterm/xterm';
|
|
8
|
+
|
|
9
|
+
declare module '@xterm/addon-web-links' {
|
|
10
|
+
/**
|
|
11
|
+
* An xterm.js addon that enables web links.
|
|
12
|
+
*/
|
|
13
|
+
export class WebLinksAddon implements ITerminalAddon {
|
|
14
|
+
/**
|
|
15
|
+
* Creates a new web links addon.
|
|
16
|
+
* @param handler The callback when the link is called.
|
|
17
|
+
* @param options Options for the link provider.
|
|
18
|
+
*/
|
|
19
|
+
constructor(handler?: (event: MouseEvent, uri: string) => void, options?: ILinkProviderOptions);
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Activates the addon
|
|
23
|
+
* @param terminal The terminal the addon is being loaded in.
|
|
24
|
+
*/
|
|
25
|
+
public activate(terminal: Terminal): void;
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Disposes the addon.
|
|
29
|
+
*/
|
|
30
|
+
public dispose(): void;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* An object containing options for a link provider.
|
|
35
|
+
*/
|
|
36
|
+
export interface ILinkProviderOptions {
|
|
37
|
+
/**
|
|
38
|
+
* A callback that fires when the mouse hovers over a link.
|
|
39
|
+
*/
|
|
40
|
+
hover?(event: MouseEvent, text: string, location: IViewportRange): void;
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* A callback that fires when the mouse leaves a link. Note that this can
|
|
44
|
+
* happen even when tooltipCallback hasn't fired for the link yet.
|
|
45
|
+
*/
|
|
46
|
+
leave?(event: MouseEvent, text: string): void;
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* A callback to use instead of the default one.
|
|
50
|
+
*/
|
|
51
|
+
urlRegex?: RegExp;
|
|
52
|
+
}
|
|
53
|
+
}
|