|
| 1 | +import { HandlesStyles, RotationStyles } from '../styles.store' |
| 2 | + |
| 3 | +export class Rotation extends HTMLElement { |
| 4 | + constructor() { |
| 5 | + super() |
| 6 | + this.$shadow = this.attachShadow({mode: 'closed'}) |
| 7 | + this.styles = [HandlesStyles, RotationStyles] |
| 8 | + this.startAngle = 0 |
| 9 | + this.currentAngle = 0 |
| 10 | + } |
| 11 | + |
| 12 | + connectedCallback() { |
| 13 | + this.$shadow.adoptedStyleSheets = this.styles |
| 14 | + } |
| 15 | + |
| 16 | + set position({el}) { |
| 17 | + this.targetElement = el |
| 18 | + const {left, top, width, height} = el.getBoundingClientRect() |
| 19 | + const isFixed = getComputedStyle(el).position === 'fixed' |
| 20 | + |
| 21 | + this.style.setProperty('--top', `${top + (isFixed ? 0 : window.scrollY)}px`) |
| 22 | + this.style.setProperty('--left', `${left}px`) |
| 23 | + this.style.setProperty('--position', isFixed ? 'fixed' : 'absolute') |
| 24 | + this.style.setProperty('--width', `${width}px`) |
| 25 | + |
| 26 | + this.$shadow.innerHTML = this.render() |
| 27 | + this.setupRotationHandlers() |
| 28 | + } |
| 29 | + |
| 30 | + setupRotationHandlers() { |
| 31 | + const handle = this.$shadow.querySelector('.rotation-handle') |
| 32 | + |
| 33 | + const onMouseDown = e => { |
| 34 | + e.preventDefault() |
| 35 | + const {left, top, width, height} = this.targetElement.getBoundingClientRect() |
| 36 | + const center = { |
| 37 | + x: left + width / 2, |
| 38 | + y: top + height / 2 |
| 39 | + } |
| 40 | + this.startAngle = Math.atan2( |
| 41 | + e.clientY - center.y, |
| 42 | + e.clientX - center.x |
| 43 | + ) |
| 44 | + |
| 45 | + document.addEventListener('mousemove', onMouseMove) |
| 46 | + document.addEventListener('mouseup', onMouseUp) |
| 47 | + } |
| 48 | + |
| 49 | + const onMouseMove = e => { |
| 50 | + const {left, top, width, height} = this.targetElement.getBoundingClientRect() |
| 51 | + const center = { |
| 52 | + x: left + width / 2, |
| 53 | + y: top + height / 2 |
| 54 | + } |
| 55 | + |
| 56 | + const angle = Math.atan2( |
| 57 | + e.clientY - center.y, |
| 58 | + e.clientX - center.x |
| 59 | + ) |
| 60 | + |
| 61 | + const rotation = angle - this.startAngle |
| 62 | + this.currentAngle += rotation |
| 63 | + this.startAngle = angle |
| 64 | + |
| 65 | + this.targetElement.style.transform = `rotate(${this.currentAngle * (180 / Math.PI)}deg)` |
| 66 | + } |
| 67 | + |
| 68 | + const onMouseUp = () => { |
| 69 | + document.removeEventListener('mousemove', onMouseMove) |
| 70 | + document.removeEventListener('mouseup', onMouseUp) |
| 71 | + } |
| 72 | + |
| 73 | + handle.addEventListener('mousedown', onMouseDown) |
| 74 | + |
| 75 | + this.cleanup = () => { |
| 76 | + handle.removeEventListener('mousedown', onMouseDown) |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + disconnectedCallback() { |
| 81 | + this.cleanup && this.cleanup() |
| 82 | + } |
| 83 | + |
| 84 | + render() { |
| 85 | + return ` |
| 86 | + <div class="rotation-handle"> |
| 87 | + <svg class="rotation-icon" viewBox="0 0 24 24"> |
| 88 | + <path d="M12 5V1L7 6l5 5V7c3.31 0 6 2.69 6 6s-2.69 6-6 6-6-2.69-6-6H4c0 4.42 3.58 8 8 8s8-3.58 8-8-3.58-8-8-8z"/> |
| 89 | + </svg> |
| 90 | + </div> |
| 91 | + ` |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +customElements.define('visbug-rotation', Rotation) |
0 commit comments