-
Notifications
You must be signed in to change notification settings - Fork 321
Expand file tree
/
Copy pathhandle.element.js
More file actions
187 lines (154 loc) · 6.08 KB
/
handle.element.js
File metadata and controls
187 lines (154 loc) · 6.08 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import $ from 'blingblingjs'
import { HandleStyles } from '../styles.store'
import { clamp } from '../../utilities/numbers'
export class Handle extends HTMLElement {
constructor() {
super()
this.$shadow = this.attachShadow({mode: 'closed'})
this.styles = [HandleStyles]
}
connectedCallback() {
this.$shadow.adoptedStyleSheets = this.styles
this.$shadow.innerHTML = this.render()
this.button = this.$shadow.querySelector('button')
this.button.addEventListener('pointerdown', this.on_element_resize_start.bind(this))
this.placement = this.getAttribute('placement')
}
static get observedAttributes() {
return ['placement']
}
attributeChangedCallback(name, oldValue, newValue) {
if (name === 'placement') {
this.placement = newValue
}
}
/**
* @param {PointerEvent} e
*/
on_element_resize_start(e) {
e.preventDefault()
e.stopPropagation()
if (e.button !== 0) return
const placement = this.placement
const handlesEl = e.composedPath().find(el => el.tagName === 'VISBUG-HANDLES')
const nodeLabelId = handlesEl.getAttribute('data-label-id')
/** @type {Element[]} */
const [sourceEl] = $(`[data-label-id="${nodeLabelId}"]`)
if (!sourceEl) return
const t = sourceEl.convertPointFromNode(e, document.body.parentElement);
const { x: initialX, y: initialY } = t
const initialStyle = getComputedStyle(sourceEl)
const initialWidth = parseFloat(initialStyle.width)
const initialHeight = parseFloat(initialStyle.height)
const initialTransform = new DOMMatrix(initialStyle.transform)
const originalElTransition = sourceEl.style.transition
const originalDocumentCursor = document.body.style.cursor
const originalDocumentUserSelect = document.body.style.userSelect
sourceEl.style.transition = 'none'
document.body.style.cursor = getComputedStyle(this).getPropertyValue('--cursor')
document.body.style.userSelect = 'none'
document.addEventListener('pointermove', on_element_resize_move)
function on_element_resize_move(e) {
e.preventDefault()
e.stopPropagation()
const t = sourceEl.convertPointFromNode({ x: e.clientX, y: e.clientY }, document.body.parentElement);
const newX = clamp(0, t.x, document.documentElement.clientWidth)
const newY = clamp(0, t.y, document.documentElement.clientHeight)
const diffX = newX - initialX
const diffY = newY - initialY
switch (placement) {
case 'top-start': {
const newWidth = initialWidth - diffX
const newHeight = initialHeight - diffY
const newTranslate = initialTransform.translate(diffX, diffY).transformPoint()
requestAnimationFrame(() => {
sourceEl.style.width = `${newWidth}px`
sourceEl.style.height = `${newHeight}px`
sourceEl.style.transform = `translate(${newTranslate.x}px, ${newTranslate.y}px)`
})
break
}
case 'top-center': {
const newHeight = initialHeight - diffY
const newTranslate = initialTransform.translate(0, diffY).transformPoint()
requestAnimationFrame(() => {
sourceEl.style.height = `${newHeight}px`
sourceEl.style.transform = `translate(${newTranslate.x}px, ${newTranslate.y}px)`
})
break
}
case 'top-end': {
const newWidth = initialWidth + diffX
const newHeight = initialHeight - diffY
const newTranslate = initialTransform.translate(0, diffY).transformPoint()
requestAnimationFrame(() => {
sourceEl.style.width = `${newWidth}px`
sourceEl.style.height = `${newHeight}px`
sourceEl.style.transform = `translate(${newTranslate.x}px, ${newTranslate.y}px)`
})
break
}
case 'middle-start': {
const newWidth = initialWidth - diffX
const newTranslate = initialTransform.translate(diffX).transformPoint()
requestAnimationFrame(() => {
sourceEl.style.width = `${newWidth}px`
sourceEl.style.transform = `translate(${newTranslate.x}px, ${newTranslate.y}px)`
})
break
}
case 'middle-end': {
const newWidth = initialWidth + diffX
requestAnimationFrame(() => {
sourceEl.style.width = `${newWidth}px`
})
break
}
case 'bottom-start': {
const newWidth = initialWidth - diffX
const newHeight = initialHeight + diffY
const newTranslate = initialTransform.translate(diffX, 0).transformPoint()
requestAnimationFrame(() => {
sourceEl.style.width = `${newWidth}px`
sourceEl.style.height = `${newHeight}px`
sourceEl.style.transform = `translate(${newTranslate.x}px, ${newTranslate.y}px)`
})
break
}
case 'bottom-center': {
const newHeight = initialHeight + diffY
requestAnimationFrame(() => {
sourceEl.style.height = `${newHeight}px`
})
break
}
case 'bottom-end': {
const newWidth = initialWidth + diffX
const newHeight = initialHeight + diffY
requestAnimationFrame(() => {
sourceEl.style.width = `${newWidth}px`
sourceEl.style.height = `${newHeight}px`
})
break
}
}
}
document.addEventListener('pointerup', on_element_resize_end, { once: true })
document.addEventListener('mouseleave', on_element_resize_end, { once: true })
function on_element_resize_end() {
document.removeEventListener('pointermove', on_element_resize_move)
document.body.style.cursor = originalDocumentCursor
document.body.style.userSelect = originalDocumentUserSelect
sourceEl.style.transition = originalElTransition
}
}
disconnectedCallback() {
this.button.removeEventListener('pointerdown', this.on_element_resize_start.bind(this))
}
render() {
return `
<button type="button" aria-label="Resize"></button>
`
}
}
customElements.define('visbug-handle', Handle)