insomnia/app/ui/components/codemirror/extensions/clickable.js

31 lines
858 B
JavaScript
Raw Normal View History

import CodeMirror from 'codemirror';
import 'codemirror/addon/mode/overlay';
import {AllHtmlEntities} from 'html-entities';
2017-04-05 02:33:09 +00:00
import {FLEXIBLE_URL_REGEX} from '../../../../common/constants';
const entities = new AllHtmlEntities();
CodeMirror.defineExtension('makeLinksClickable', function (handleClick) {
// Only add the click mode if we have links to click
this.addOverlay({
token: function (stream, state) {
2017-04-05 02:33:09 +00:00
if (stream.match(FLEXIBLE_URL_REGEX, true)) {
return 'clickable';
}
while (stream.next() != null) {
2017-04-05 02:33:09 +00:00
if (stream.match(FLEXIBLE_URL_REGEX, false)) break;
}
return null;
}
});
this.getWrapperElement().addEventListener('click', e => {
const cls = e.target.className;
if (cls.indexOf('cm-clickable') >= 0) {
handleClick(entities.decode(e.target.innerHTML));
}
});
});