-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
44 lines (38 loc) · 1.16 KB
/
utils.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
const trigger = (e) => e.composedPath()[0];
const matchesTrigger = (e, selectorString) =>
trigger(e).matches(selectorString);
export const createListener =
(target) => (eventName, selectorString, event, args) => {
// focus doesn't work with this, focus doesn't bubble, need focusin
target.addEventListener(
eventName,
(e) => {
e.trigger = trigger(e); // Do I need this? e.target seems to work in many (all?) cases
if (selectorString === "" || matchesTrigger(e, selectorString))
event(e);
},
args ?? {}
);
};
// pipe ID utils
export const buildPipeID = (startTool, startPort, endTool, endPort) => {
return `${startTool}_${startPort}_${endTool}_${endPort}`;
};
export function selectElementContents(element) {
let range = document.createRange();
range.selectNodeContents(element);
let sel = window.getSelection();
sel.removeAllRanges();
sel.addRange(range);
}
export function blurTargetOnEnter(e) {
if (e.code === "Enter") {
e.preventDefault();
e.target.blur();
}
}
export function checkCharacterCount(element, max, e) {
if (element.textContent.length > max) {
e.preventDefault();
}
}