2020-01-21 17:52:40 +00:00
|
|
|
import { env } from 'consul-ui/env';
|
2019-05-15 13:48:16 +00:00
|
|
|
|
|
|
|
const SECONDARY_BUTTON = 2;
|
2022-09-15 08:43:17 +00:00
|
|
|
const isSelecting = function (win = window) {
|
2019-05-15 13:48:16 +00:00
|
|
|
const selection = win.getSelection();
|
|
|
|
let selecting = false;
|
|
|
|
try {
|
2019-07-16 16:05:09 +00:00
|
|
|
selecting =
|
|
|
|
'isCollapsed' in selection && !selection.isCollapsed && selection.toString().length > 1;
|
2019-05-15 13:48:16 +00:00
|
|
|
} catch (e) {
|
|
|
|
// passthrough
|
|
|
|
}
|
|
|
|
return selecting;
|
|
|
|
};
|
|
|
|
export default {
|
|
|
|
name: 'selection',
|
|
|
|
initialize(container) {
|
|
|
|
if (env('CONSUL_UI_DISABLE_ANCHOR_SELECTION')) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const dom = container.lookup('service:dom');
|
2021-03-17 15:58:17 +00:00
|
|
|
const doc = dom.document();
|
|
|
|
const $html = doc.getElementsByTagName('html')[0];
|
2022-09-15 08:43:17 +00:00
|
|
|
const findAnchor = function (el) {
|
2019-05-15 13:48:16 +00:00
|
|
|
return el.tagName === 'A' ? el : dom.closest('a', el);
|
|
|
|
};
|
2022-09-15 08:43:17 +00:00
|
|
|
const mousedown = function (e) {
|
2021-03-17 15:58:17 +00:00
|
|
|
if ($html.classList.contains('is-debug')) {
|
|
|
|
return;
|
|
|
|
}
|
2019-05-15 13:48:16 +00:00
|
|
|
const $a = findAnchor(e.target);
|
|
|
|
if ($a) {
|
|
|
|
if (typeof e.button !== 'undefined' && e.button === SECONDARY_BUTTON) {
|
|
|
|
const dataHref = $a.dataset.href;
|
|
|
|
if (dataHref) {
|
|
|
|
$a.setAttribute('href', dataHref);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const href = $a.getAttribute('href');
|
|
|
|
if (href) {
|
|
|
|
$a.dataset.href = href;
|
|
|
|
$a.removeAttribute('href');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2022-09-15 08:43:17 +00:00
|
|
|
const mouseup = function (e) {
|
2021-03-17 15:58:17 +00:00
|
|
|
if ($html.classList.contains('is-debug')) {
|
|
|
|
return;
|
|
|
|
}
|
2019-05-15 13:48:16 +00:00
|
|
|
const $a = findAnchor(e.target);
|
|
|
|
if ($a) {
|
|
|
|
const href = $a.dataset.href;
|
|
|
|
if (!isSelecting() && href) {
|
|
|
|
$a.setAttribute('href', href);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-03-17 15:58:17 +00:00
|
|
|
doc.body.addEventListener('mousedown', mousedown);
|
|
|
|
doc.body.addEventListener('mouseup', mouseup);
|
2019-05-15 13:48:16 +00:00
|
|
|
|
|
|
|
container.reopen({
|
2022-09-15 08:43:17 +00:00
|
|
|
willDestroy: function () {
|
2021-03-17 15:58:17 +00:00
|
|
|
doc.body.removeEventListener('mousedown', mousedown);
|
|
|
|
doc.body.removeEventListener('mouseup', mouseup);
|
2019-05-15 13:48:16 +00:00
|
|
|
return this._super(...arguments);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
},
|
|
|
|
};
|