visbug-lib
Version:
<p align="center"> <img src="./assets/visbug.png" width="300" height="300" alt="visbug"> <br> <a href="https://www.npmjs.org/package/visbug"><img src="https://img.shields.io/npm/v/visbug.svg?style=flat" alt="npm latest version number"></a> <a href
84 lines (67 loc) • 2.54 kB
JavaScript
import $ from 'blingblingjs'
import { HandleStyles } from '../styles.store'
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,
}
})
}
set position({el, node_label_id}) {
this.$shadow.innerHTML = this.render(el.getBoundingClientRect(), node_label_id)
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) {
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`)
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)