-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
104 lines (88 loc) · 2.75 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
const cssImp = require('./popupCss');
let actions;
/**
* @typedef _options - options
* @prop {string} [style = ''] - custom style
*/
const _options = {
style: '',
};
/**
* add popup menu to text selections
* @param {string} items - menu items
* @param {function} callbacks - click callbacks for items
* @param {_options} [options] - options
*/
function selectionPopup(items, callbacks, options) {
let css = cssImp.cssPopup + cssImp.elseCss;
if (options && options.style) css = _parseCss(cssImp.cssPopup, options.style) + cssImp.elseCss;
const head = document.head || document.getElementsByTagName('head')[0];
const style = document.createElement('style');
style.type = 'text/css';
if (style.styleSheet) {
style.styleSheet.cssText += css;
} else {
style.appendChild(document.createTextNode(css));
}
head.appendChild(style);
const parsedItems = _parseItems(items, callbacks);
document.body.innerHTML += `<popup class='popup'>
<div class='popupContentWrapper'>
${parsedItems}
</div>
<div class='pointer'></div></div>`;
if (!callbacks.push) callbacks = [callbacks];
actions = callbacks;
document.onmouseup = _selectionEndText;
document.onmousedown = _processSelection;
}
function _parseItems(items, actions) {
if (!items.push) items = [items];
let parsed = '';
for (let i = 0; i < items.length; i++) {
const item = ` <div class='popupItem' data-action= "${i}">
${items[i]}
</div>`;
parsed += item;
}
return parsed;
}
function _parseCss(main, newOne) {
const parsed = `${main.slice(0, main.length - 1)};${newOne}}`;
return parsed;
}
function _processSelection(e) {
if (e.target.classList.contains('popupItem')) {
actions[e.target.dataset.action](document.getSelection());
_hidePopup();
return;
}
const parent = _parentNodeCheck(e.target, 'popupItem');
if (parent) actions[parent.dataset.action](document.getSelection());
_hidePopup();
}
function _parentNodeCheck(node, className) {
const parent = node.parentNode;
if (!parent) return undefined;
if (parent.tagName !== 'BODY') {
if (parent.classList.contains(className)) return parent;
_parentNodeCheck(parent, className);
} else if (parent.tagName === 'BODY') {
return undefined;
}
}
function _hidePopup() {
document.querySelector('popup').classList.remove('popupVisible');
}
function _selectionEndText(e) {
const t = document.getSelection();
if (t.toString().length !== 0) {
const popup = document.querySelector('popup');
const rangeT = t.getRangeAt(0);
const rectT = rangeT.getBoundingClientRect();
popup.style.top = `${rectT.y}px`;
popup.style.left = `${rectT.x - popup.clientWidth / 2 + rectT.width / 2}px`;
popup.classList.add('popupVisible');
}
}
module.exports = selectionPopup;