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
186 lines (168 loc) • 5.52 kB
JavaScript
import { BoxModelStyles } from '../styles.store'
export class BoxModel extends HTMLElement {
constructor() {
super()
this.$shadow = this.attachShadow({mode: 'closed'})
this.drawable = {}
}
connectedCallback() {
this.$shadow.adoptedStyleSheets = [BoxModelStyles]
}
disconnectedCallback() {}
set position(payload) {
this.$shadow.innerHTML = this.render(payload)
if (!this.drawable.measurements) // && payload.color === 'pink'
this.createMeasurements(payload)
}
render({mode, bounds, sides, color = 'pink'}) {
const total_height = bounds.height + sides.bottom + sides.top
const total_width = bounds.width + sides.right + sides.left
if (mode === 'padding') {
this.drawable = {
height: bounds.height,
width: bounds.width,
top: bounds.top + window.scrollY,
left: bounds.left + window.scrollX,
rotation: 'rotate(-45)',
}
}
else if (mode === 'margin') {
this.drawable = {
height: total_height,
width: total_width,
top: bounds.top + window.scrollY - sides.top,
left: bounds.left + window.scrollX - sides.left,
rotation: 'rotate(45)',
}
}
if (color === 'pink') {
this.drawable.bg = 'hsla(330, 100%, 71%, 15%)'
this.drawable.stripe = 'hsla(330, 100%, 71%, 80%)'
}
else {
this.drawable.bg = 'hsla(267, 100%, 58%, 15%)'
this.drawable.stripe = 'hsla(267, 100%, 58%, 80%)'
}
this.styles({sides})
return `
<div mask>
<svg xmlns="http://www.w3.org/2000/svg" width="100%" height="100%">
<defs>
<pattern id="pinstripe" patternUnits="userSpaceOnUse" width="10" height="10" patternTransform="${this.drawable.rotation}" class="pattern">
<line x1="0" y="0" x2="0" y2="10" stroke="${this.drawable.stripe}" stroke-width="1"></line>
</pattern>
</defs>
<rect width="100%" height="100%" fill="url(#pinstripe)"></rect>
</svg>
</div>
`
}
styles({sides}) {
this.style.setProperty('--width', `${this.drawable.width}px`)
this.style.setProperty('--height', `${this.drawable.height}px`)
this.style.setProperty('--top', `${this.drawable.top}px`)
this.style.setProperty('--left', `${this.drawable.left}px`)
this.style.setProperty('--bg', `${this.drawable.bg}`)
this.style.setProperty('--target-left', `${sides.left}px`)
this.style.setProperty('--target-top', `${sides.top}px`)
this.style.setProperty('--target-right', `${sides.right}px`)
this.style.setProperty('--target-bottom', `${sides.bottom}px`)
this.style.setProperty('--offset-right', `${this.drawable.width - sides.right}px`)
this.style.setProperty('--offset-bottom', `${this.drawable.height - sides.bottom}px`)
}
createMeasurements({mode, bounds, sides, color}) {
const win_width = window.innerWidth
const pill_height = 18
const offset = 3
if (mode === 'margin') {
if (sides.top) {
this.createMeasurement({
x: bounds.left + (bounds.width / 2) - offset,
y: bounds.top - sides.top - (sides.top < pill_height ? pill_height - sides.top : 0),
d: sides.top,
q: 'top',
v: true,
color,
})
}
if (sides.bottom) {
this.createMeasurement({
x: bounds.left + (bounds.width / 2) - offset,
y: bounds.bottom,
d: sides.bottom,
q: 'bottom',
v: true,
color,
})
}
if (sides.right) {
this.createMeasurement({
x: bounds.right,
y: bounds.top + (bounds.height / 2) - offset,
d: sides.right,
q: 'right',
v: false,
color,
})
}
if (sides.left) {
this.createMeasurement({
x: win_width - bounds.left,
y: bounds.top + (bounds.height / 2) - offset,
d: sides.left,
q: 'left',
v: false,
color,
})
}
}
else if (mode === 'padding') {
if (sides.top) {
this.createMeasurement({
x: bounds.left + (bounds.width / 2) - offset,
y: bounds.top - (sides.top < pill_height ? pill_height - sides.top : 0),
d: sides.top,
q: 'top',
v: true,
color,
})
}
if (sides.bottom) {
this.createMeasurement({
x: bounds.left + (bounds.width / 2) - offset,
y: bounds.bottom - sides.bottom,
d: sides.bottom,
q: 'bottom',
v: true,
color,
})
}
if (sides.right) {
this.createMeasurement({
x: bounds.right - sides.right,
y: bounds.top + (bounds.height / 2) - offset,
d: sides.right,
q: 'right',
v: false,
color,
})
}
if (sides.left) {
this.createMeasurement({
x: win_width - bounds.left - sides.left,
y: bounds.top + (bounds.height / 2) - offset,
d: sides.left,
q: 'left',
v: false,
color,
})
}
}
}
createMeasurement(line_model, node_label_id=0) {
const measurement = document.createElement('visbug-distance')
measurement.position = { line_model, node_label_id }
this.$shadow.appendChild(measurement)
}
}
customElements.define('visbug-boxmodel', BoxModel)