-
Notifications
You must be signed in to change notification settings - Fork 320
Expand file tree
/
Copy pathcommon.js
More file actions
134 lines (108 loc) · 3.38 KB
/
common.js
File metadata and controls
134 lines (108 loc) · 3.38 KB
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import $ from 'blingblingjs'
import { nodeKey } from './strings'
export const deepElementFromPoint = (x, y) => {
const el = document.elementFromPoint(x, y)
const crawlShadows = node => {
if (node.shadowRoot) {
const potential = node.shadowRoot.elementFromPoint(x, y)
if (potential == node) return node
else if (potential.shadowRoot) return crawlShadows(potential)
else return potential
}
else return node
}
const nested_shadow = crawlShadows(el)
return nested_shadow || el
}
export const getSide = direction => {
let start = direction.split('+').pop().replace(/^\w/, c => c.toUpperCase())
if (start == 'Up') start = 'Top'
if (start == 'Down') start = 'Bottom'
return start
}
export const getNodeIndex = el => {
return [...el.parentElement.parentElement.children]
.indexOf(el.parentElement)
}
export function showEdge(el) {
return el.animate([
{ outline: '1px solid transparent' },
{ outline: '1px solid hsla(330, 100%, 71%, 80%)' },
{ outline: '1px solid transparent' },
], 600)
}
let timeoutMap = {}
export const showHideSelected = (el, duration = 750) => {
el.setAttribute('data-selected-hide', true)
showHideNodeLabel(el, true)
if (timeoutMap[nodeKey(el)])
clearTimeout(timeoutMap[nodeKey(el)])
timeoutMap[nodeKey(el)] = setTimeout(_ => {
el.removeAttribute('data-selected-hide')
showHideNodeLabel(el, false)
}, duration)
return el
}
export const showHideNodeLabel = (el, show = false) => {
if (!el.hasAttribute('data-label-id'))
return
const label_id = el.getAttribute('data-label-id')
const nodes = $(`
visbug-label[data-label-id="${label_id}"],
visbug-handles[data-label-id="${label_id}"]
`)
nodes.length && show
? nodes.forEach(el =>
el.style.display = 'none')
: nodes.forEach(el =>
el.style.display = null)
}
export const htmlStringToDom = (htmlString = "") =>
(new DOMParser().parseFromString(htmlString, 'text/html'))
.body.firstChild
export const isOffBounds = node =>
node.closest && (
node.closest('vis-bug')
|| node.closest('hotkey-map')
|| node.closest('visbug-metatip')
|| node.closest('visbug-ally')
|| node.closest('visbug-label')
|| node.closest('visbug-handles')
|| node.closest('visbug-corners')
|| node.closest('visbug-grip')
|| node.closest('visbug-gridlines')
|| node.closest('visbug-rotation')
|| node.closest('visbug-delete')
)
export const isSelectorValid = (qs => (
selector => {
try { qs(selector) } catch (e) { return false }
return true
}
))(s => document.createDocumentFragment().querySelector(s))
export const swapElements = (src, target) => {
var temp = document.createElement("div")
src.parentNode.insertBefore(temp, src)
target.parentNode.insertBefore(src, target)
temp.parentNode.insertBefore(target, temp)
temp.parentNode.removeChild(temp)
}
export const onRemove = (element, callback) => {
const parent = element.parentNode
? element.parentNode
: document.body
if (!parent) throw new Error("The node must already be attached")
const obs = new MutationObserver(mutations => {
for (const mutation of mutations) {
for (const el of mutation.removedNodes) {
if (el === element) {
obs.disconnect()
callback()
}
}
}
})
obs.observe(parent, {
childList: true,
})
}