|
| 1 | +import $ from 'blingblingjs' |
| 2 | +import { HandleStyles } from '../styles.store' |
| 3 | +import { clamp } from '../../utilities/numbers' |
| 4 | + |
| 5 | +export class Handle extends HTMLElement { |
| 6 | + |
| 7 | + constructor() { |
| 8 | + super() |
| 9 | + this.$shadow = this.attachShadow({mode: 'closed'}) |
| 10 | + this.styles = [HandleStyles] |
| 11 | + } |
| 12 | + |
| 13 | + connectedCallback() { |
| 14 | + this.$shadow.adoptedStyleSheets = this.styles |
| 15 | + this.$shadow.innerHTML = this.render() |
| 16 | + |
| 17 | + this.button = this.$shadow.querySelector('button') |
| 18 | + this.button.addEventListener('pointerdown', this.on_element_resize_start.bind(this)) |
| 19 | + |
| 20 | + this.placement = this.getAttribute('placement') |
| 21 | + } |
| 22 | + |
| 23 | + static get observedAttributes() { |
| 24 | + return ['placement'] |
| 25 | + } |
| 26 | + |
| 27 | + attributeChangedCallback(name, oldValue, newValue) { |
| 28 | + if (name === 'placement') { |
| 29 | + this.placement = newValue |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + on_element_resize_start(e) { |
| 34 | + e.preventDefault() |
| 35 | + e.stopPropagation() |
| 36 | + |
| 37 | + if (e.button !== 0) return |
| 38 | + |
| 39 | + const placement = this.placement |
| 40 | + const handlesEl = e.path.find(el => el.tagName === 'VISBUG-HANDLES') |
| 41 | + const nodeLabelId = handlesEl.getAttribute('data-label-id') |
| 42 | + const [sourceEl] = $(`[data-label-id="${nodeLabelId}"]`) |
| 43 | + |
| 44 | + if (!sourceEl) return |
| 45 | + |
| 46 | + const { x: initialX, y: initialY } = e |
| 47 | + const initialStyle = getComputedStyle(sourceEl) |
| 48 | + const initialWidth = parseFloat(initialStyle.width) |
| 49 | + const initialHeight = parseFloat(initialStyle.height) |
| 50 | + const initialTransform = new DOMMatrix(initialStyle.transform) |
| 51 | + |
| 52 | + const originalElTransition = sourceEl.style.transition |
| 53 | + const originalDocumentCursor = document.body.style.cursor |
| 54 | + const originalDocumentUserSelect = document.body.style.userSelect |
| 55 | + sourceEl.style.transition = 'none' |
| 56 | + document.body.style.cursor = getComputedStyle(this).getPropertyValue('--cursor') |
| 57 | + document.body.style.userSelect = 'none' |
| 58 | + |
| 59 | + document.addEventListener('pointermove', on_element_resize_move) |
| 60 | + |
| 61 | + function on_element_resize_move(e) { |
| 62 | + e.preventDefault() |
| 63 | + e.stopPropagation() |
| 64 | + |
| 65 | + const newX = clamp(0, e.clientX, document.documentElement.clientWidth) |
| 66 | + const newY = clamp(0, e.clientY, document.documentElement.clientHeight) |
| 67 | + |
| 68 | + const diffX = newX - initialX |
| 69 | + const diffY = newY - initialY |
| 70 | + |
| 71 | + switch (placement) { |
| 72 | + case 'top-start': { |
| 73 | + const newWidth = initialWidth - diffX |
| 74 | + const newHeight = initialHeight - diffY |
| 75 | + const newTranslate = initialTransform.translate(diffX, diffY).transformPoint() |
| 76 | + |
| 77 | + requestAnimationFrame(() => { |
| 78 | + sourceEl.style.width = `${newWidth}px` |
| 79 | + sourceEl.style.height = `${newHeight}px` |
| 80 | + sourceEl.style.transform = `translate(${newTranslate.x}px, ${newTranslate.y}px)` |
| 81 | + }) |
| 82 | + break |
| 83 | + } |
| 84 | + case 'top-center': { |
| 85 | + const newHeight = initialHeight - diffY |
| 86 | + const newTranslate = initialTransform.translate(0, diffY).transformPoint() |
| 87 | + |
| 88 | + requestAnimationFrame(() => { |
| 89 | + sourceEl.style.height = `${newHeight}px` |
| 90 | + sourceEl.style.transform = `translate(${newTranslate.x}px, ${newTranslate.y}px)` |
| 91 | + }) |
| 92 | + break |
| 93 | + } |
| 94 | + case 'top-end': { |
| 95 | + const newWidth = initialWidth + diffX |
| 96 | + const newHeight = initialHeight - diffY |
| 97 | + const newTranslate = initialTransform.translate(0, diffY).transformPoint() |
| 98 | + |
| 99 | + requestAnimationFrame(() => { |
| 100 | + sourceEl.style.width = `${newWidth}px` |
| 101 | + sourceEl.style.height = `${newHeight}px` |
| 102 | + sourceEl.style.transform = `translate(${newTranslate.x}px, ${newTranslate.y}px)` |
| 103 | + }) |
| 104 | + break |
| 105 | + } |
| 106 | + case 'middle-start': { |
| 107 | + const newWidth = initialWidth - diffX |
| 108 | + const newTranslate = initialTransform.translate(diffX).transformPoint() |
| 109 | + |
| 110 | + requestAnimationFrame(() => { |
| 111 | + sourceEl.style.width = `${newWidth}px` |
| 112 | + sourceEl.style.transform = `translate(${newTranslate.x}px, ${newTranslate.y}px)` |
| 113 | + }) |
| 114 | + break |
| 115 | + } |
| 116 | + case 'middle-end': { |
| 117 | + const newWidth = initialWidth + diffX |
| 118 | + |
| 119 | + requestAnimationFrame(() => { |
| 120 | + sourceEl.style.width = `${newWidth}px` |
| 121 | + }) |
| 122 | + break |
| 123 | + } |
| 124 | + case 'bottom-start': { |
| 125 | + const newWidth = initialWidth - diffX |
| 126 | + const newHeight = initialHeight + diffY |
| 127 | + const newTranslate = initialTransform.translate(diffX, 0).transformPoint() |
| 128 | + |
| 129 | + requestAnimationFrame(() => { |
| 130 | + sourceEl.style.width = `${newWidth}px` |
| 131 | + sourceEl.style.height = `${newHeight}px` |
| 132 | + sourceEl.style.transform = `translate(${newTranslate.x}px, ${newTranslate.y}px)` |
| 133 | + }) |
| 134 | + break |
| 135 | + } |
| 136 | + case 'bottom-center': { |
| 137 | + const newHeight = initialHeight + diffY |
| 138 | + |
| 139 | + requestAnimationFrame(() => { |
| 140 | + sourceEl.style.height = `${newHeight}px` |
| 141 | + }) |
| 142 | + break |
| 143 | + } |
| 144 | + case 'bottom-end': { |
| 145 | + const newWidth = initialWidth + diffX |
| 146 | + const newHeight = initialHeight + diffY |
| 147 | + |
| 148 | + requestAnimationFrame(() => { |
| 149 | + sourceEl.style.width = `${newWidth}px` |
| 150 | + sourceEl.style.height = `${newHeight}px` |
| 151 | + }) |
| 152 | + break |
| 153 | + } |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + document.addEventListener('pointerup', on_element_resize_end, { once: true }) |
| 158 | + document.addEventListener('mouseleave', on_element_resize_end, { once: true }) |
| 159 | + |
| 160 | + function on_element_resize_end() { |
| 161 | + document.removeEventListener('pointermove', on_element_resize_move) |
| 162 | + document.body.style.cursor = originalDocumentCursor |
| 163 | + document.body.style.userSelect = originalDocumentUserSelect |
| 164 | + sourceEl.style.transition = originalElTransition |
| 165 | + } |
| 166 | + } |
| 167 | + |
| 168 | + disconnectedCallback() { |
| 169 | + this.button.removeEventListener('pointerdown', this.on_element_resize_start.bind(this)) |
| 170 | + } |
| 171 | + |
| 172 | + render() { |
| 173 | + return ` |
| 174 | + <button type="button" aria-label="Resize"></button> |
| 175 | + ` |
| 176 | + } |
| 177 | +} |
| 178 | + |
| 179 | +customElements.define('visbug-handle', Handle) |
0 commit comments