2017-02-27 21:00:13 +00:00
|
|
|
import CodeMirror from 'codemirror';
|
|
|
|
import 'codemirror/addon/mode/overlay';
|
2017-04-07 18:10:15 +00:00
|
|
|
import {AllHtmlEntities} from 'html-entities';
|
2017-04-05 02:33:09 +00:00
|
|
|
import {FLEXIBLE_URL_REGEX} from '../../../../common/constants';
|
2017-02-27 21:00:13 +00:00
|
|
|
|
2017-04-07 18:10:15 +00:00
|
|
|
const entities = new AllHtmlEntities();
|
|
|
|
|
2017-02-27 21:00:13 +00:00
|
|
|
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)) {
|
2017-02-27 21:00:13 +00:00
|
|
|
return 'clickable';
|
|
|
|
}
|
|
|
|
|
|
|
|
while (stream.next() != null) {
|
2017-04-05 02:33:09 +00:00
|
|
|
if (stream.match(FLEXIBLE_URL_REGEX, false)) break;
|
2017-02-27 21:00:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
this.getWrapperElement().addEventListener('click', e => {
|
|
|
|
const cls = e.target.className;
|
|
|
|
if (cls.indexOf('cm-clickable') >= 0) {
|
2017-04-07 18:10:15 +00:00
|
|
|
handleClick(entities.decode(e.target.innerHTML));
|
2017-02-27 21:00:13 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|