-
Notifications
You must be signed in to change notification settings - Fork 321
Expand file tree
/
Copy pathhandles.element.js
More file actions
86 lines (70 loc) · 2.65 KB
/
handles.element.js
File metadata and controls
86 lines (70 loc) · 2.65 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
import $ from 'blingblingjs'
import { HandleStyles } from '../styles.store'
import { isFixed } from '../../utilities/';
export class Handles extends HTMLElement {
constructor() {
super()
this.$shadow = this.attachShadow({mode: 'closed'})
this.styles = [HandleStyles]
}
connectedCallback() {
this.$shadow.adoptedStyleSheets = this.styles
window.addEventListener('resize', this.on_resize.bind(this))
}
disconnectedCallback() {
window.removeEventListener('resize', this.on_resize)
}
on_resize() {
window.requestAnimationFrame(() => {
const node_label_id = this.$shadow.host.getAttribute('data-label-id')
const [source_el] = $(`[data-label-id="${node_label_id}"]`)
if (!source_el) return
this.position = {
node_label_id,
el: source_el,
isFixed: isFixed(source_el),
}
})
}
set position({el, node_label_id}) {
this.$shadow.innerHTML = this.render(el.getBoundingClientRect(), node_label_id, isFixed(el))
if (this._backdrop) {
this.backdrop = {
element: this._backdrop.update(el),
update: this._backdrop.update,
}
}
}
set backdrop(bd) {
this._backdrop = bd
const cur_child = this.$shadow.querySelector('visbug-boxmodel')
cur_child
? this.$shadow.replaceChild(bd.element, cur_child)
: this.$shadow.appendChild(bd.element)
}
render({ x, y, width, height, top, left }, node_label_id, isFixed) {
this.$shadow.host.setAttribute('data-label-id', node_label_id)
this.style.setProperty('--top', `${top+window.scrollY}px`)
this.style.setProperty('--left', `${left+window.scrollX}px`)
this.style.setProperty('--position', isFixed ? 'fixed' : 'absolute')
return `
<svg
class="visbug-handles"
width="${width}" height="${height}"
viewBox="0 0 ${width} ${height}"
version="1.1" xmlns="http://www.w3.org/2000/svg"
>
<rect stroke="hotpink" fill="none" width="100%" height="100%"></rect>
<circle stroke="hotpink" fill="white" cx="0" cy="0" r="2"></circle>
<circle stroke="hotpink" fill="white" cx="100%" cy="0" r="2"></circle>
<circle stroke="hotpink" fill="white" cx="100%" cy="100%" r="2"></circle>
<circle stroke="hotpink" fill="white" cx="0" cy="100%" r="2"></circle>
<circle fill="hotpink" cx="${width/2}" cy="0" r="2"></circle>
<circle fill="hotpink" cx="0" cy="${height/2}" r="2"></circle>
<circle fill="hotpink" cx="${width/2}" cy="${height}" r="2"></circle>
<circle fill="hotpink" cx="${width}" cy="${height/2}" r="2"></circle>
</svg>
`
}
}
customElements.define('visbug-handles', Handles)