insomnia/app/ui/components/codemirror/extensions/clickable.js
Gregory Schier 1d45367aa1 Added eslint and fixed all problems (#101)
* Fixed duplication kve bug

* Added semistandard and updated code

* Actually got it working

* Even better

* I think it should work on Windows now
2017-03-03 12:09:08 -08:00

29 lines
751 B
JavaScript

import CodeMirror from 'codemirror';
import 'codemirror/addon/mode/overlay';
CodeMirror.defineExtension('makeLinksClickable', function (handleClick) {
// Only add the click mode if we have links to click
const regexUrl = /^https?:\/\/([\da-z\-.]+)\.([a-z.]{2,6})([/\w .\-+=;]*)*\/?/;
this.addOverlay({
token: function (stream, state) {
if (stream.match(regexUrl, true)) {
return 'clickable';
}
while (stream.next() != null) {
if (stream.match(regexUrl, false)) break;
}
return null;
}
});
this.getWrapperElement().addEventListener('click', e => {
const cls = e.target.className;
if (cls.indexOf('cm-clickable') >= 0) {
handleClick(e.target.innerHTML);
}
});
});