2021-06-08 15:19:03 +02:00
|
|
|
<script>
|
|
|
|
import { onMount, onDestroy } from "svelte"
|
|
|
|
import { builderStore } from "../store"
|
|
|
|
import Indicator from "./Indicator.svelte"
|
2021-06-10 10:05:08 +02:00
|
|
|
import { domDebounce } from "../utils/domDebounce"
|
2021-06-08 15:19:03 +02:00
|
|
|
|
|
|
|
let indicators = []
|
|
|
|
let interval
|
|
|
|
|
|
|
|
const updatePosition = () => {
|
|
|
|
const id = $builderStore.selectedComponentId
|
|
|
|
const parents = document.getElementsByClassName(id)
|
|
|
|
|
|
|
|
// Batch reads to minimize reflow
|
|
|
|
const scrollX = window.scrollX
|
|
|
|
const scrollY = window.scrollY
|
|
|
|
|
|
|
|
let newIndicators = []
|
|
|
|
for (let i = 0; i < parents.length; i++) {
|
|
|
|
const child = parents[i]?.childNodes?.[0]
|
|
|
|
if (child) {
|
|
|
|
const elBounds = child.getBoundingClientRect()
|
|
|
|
newIndicators.push({
|
2021-06-08 16:16:37 +02:00
|
|
|
top: elBounds.top + scrollY - 2,
|
|
|
|
left: elBounds.left + scrollX - 2,
|
|
|
|
width: elBounds.width + 4,
|
|
|
|
height: elBounds.height + 4,
|
2021-06-08 15:19:03 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
indicators = newIndicators
|
|
|
|
}
|
2021-06-10 10:05:08 +02:00
|
|
|
const debouncedUpdate = domDebounce(updatePosition)
|
2021-06-08 15:19:03 +02:00
|
|
|
|
|
|
|
onMount(() => {
|
2021-06-10 10:05:08 +02:00
|
|
|
debouncedUpdate()
|
|
|
|
interval = setInterval(debouncedUpdate, 100)
|
|
|
|
document.addEventListener("scroll", debouncedUpdate, true)
|
2021-06-08 15:19:03 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
onDestroy(() => {
|
|
|
|
clearInterval(interval)
|
2021-06-10 10:05:08 +02:00
|
|
|
document.removeEventListener("scroll", debouncedUpdate, true)
|
2021-06-08 15:19:03 +02:00
|
|
|
})
|
|
|
|
</script>
|
|
|
|
|
|
|
|
{#each indicators as indicator, idx}
|
|
|
|
<Indicator
|
|
|
|
top={indicator.top}
|
|
|
|
left={indicator.left}
|
|
|
|
width={indicator.width}
|
|
|
|
height={indicator.height}
|
|
|
|
text={idx === 0 ? $builderStore.selectedComponent._instanceName : null}
|
|
|
|
color="rgb(66, 133, 244)"
|
|
|
|
/>
|
|
|
|
{/each}
|