2021-06-08 09:00:54 +02:00
|
|
|
<script>
|
|
|
|
import { onMount, onDestroy } from "svelte"
|
|
|
|
import SettingsButton from "./SettingsButton.svelte"
|
2021-06-10 19:42:41 +02:00
|
|
|
import { builderStore } from "../../store"
|
|
|
|
import { domDebounce } from "../../utils/domDebounce"
|
2021-06-08 09:00:54 +02:00
|
|
|
|
2021-06-08 15:19:03 +02:00
|
|
|
const verticalOffset = 28
|
|
|
|
const horizontalOffset = 2
|
|
|
|
|
2021-06-08 09:00:54 +02:00
|
|
|
let top = 0
|
|
|
|
let left = 0
|
|
|
|
let interval
|
|
|
|
let self
|
|
|
|
let measured = false
|
2021-06-08 15:19:03 +02:00
|
|
|
|
2021-06-08 09:00:54 +02:00
|
|
|
$: definition = $builderStore.selectedComponentDefinition
|
|
|
|
$: showBar = definition?.showSettingsBar
|
|
|
|
$: settings = definition?.settings?.filter(setting => setting.showInBar) ?? []
|
|
|
|
|
|
|
|
const updatePosition = () => {
|
|
|
|
if (!showBar) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
const id = $builderStore.selectedComponentId
|
|
|
|
const parent = document.getElementsByClassName(id)?.[0]
|
|
|
|
const element = parent?.childNodes?.[0]
|
|
|
|
if (element && self) {
|
2021-06-08 15:19:03 +02:00
|
|
|
// Batch reads to minimize reflow
|
2021-06-08 09:00:54 +02:00
|
|
|
const elBounds = element.getBoundingClientRect()
|
|
|
|
const width = self.offsetWidth
|
|
|
|
const height = self.offsetHeight
|
2021-06-08 15:19:03 +02:00
|
|
|
const { scrollX, scrollY, innerWidth } = window
|
2021-06-08 09:00:54 +02:00
|
|
|
|
|
|
|
// Vertically, always render above unless no room, then render inside
|
2021-06-08 15:19:03 +02:00
|
|
|
let newTop = elBounds.top + scrollY - verticalOffset - height
|
2021-06-08 09:00:54 +02:00
|
|
|
if (newTop < 0) {
|
2021-06-08 15:19:03 +02:00
|
|
|
newTop = elBounds.top + scrollY + verticalOffset
|
2021-06-08 09:00:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Horizontally, try to center first.
|
|
|
|
// Failing that, render to left edge of component.
|
|
|
|
// Failing that, render to right edge of component,
|
|
|
|
// Failing that, render to window left edge and accept defeat.
|
2021-06-08 15:19:03 +02:00
|
|
|
let elCenter = elBounds.left + scrollX + elBounds.width / 2
|
2021-06-08 09:00:54 +02:00
|
|
|
let newLeft = elCenter - width / 2
|
2021-06-08 15:19:03 +02:00
|
|
|
if (newLeft < 0 || newLeft + width > innerWidth) {
|
|
|
|
newLeft = elBounds.left + scrollX - horizontalOffset
|
|
|
|
if (newLeft < 0 || newLeft + width > innerWidth) {
|
2021-06-08 16:16:37 +02:00
|
|
|
newLeft = elBounds.right + scrollX - width + horizontalOffset
|
2021-06-08 15:19:03 +02:00
|
|
|
if (newLeft < 0 || newLeft + width > innerWidth) {
|
2021-06-08 16:16:37 +02:00
|
|
|
newLeft = horizontalOffset
|
2021-06-08 09:00:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-08 15:19:03 +02:00
|
|
|
// Only update state when things changes to minimize renders
|
2021-06-08 09:00:54 +02:00
|
|
|
if (Math.round(newTop) !== Math.round(top)) {
|
|
|
|
top = newTop
|
|
|
|
}
|
|
|
|
if (Math.round(newLeft) !== Math.round(left)) {
|
|
|
|
left = newLeft
|
|
|
|
}
|
|
|
|
|
|
|
|
measured = true
|
|
|
|
}
|
|
|
|
}
|
2021-06-10 10:05:08 +02:00
|
|
|
const debouncedUpdate = domDebounce(updatePosition)
|
2021-06-08 09:00:54 +02:00
|
|
|
|
|
|
|
onMount(() => {
|
2021-06-10 10:05:08 +02:00
|
|
|
debouncedUpdate()
|
|
|
|
interval = setInterval(debouncedUpdate, 100)
|
|
|
|
document.addEventListener("scroll", debouncedUpdate, true)
|
2021-06-08 09:00:54 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
onDestroy(() => {
|
2021-06-08 15:19:03 +02:00
|
|
|
clearInterval(interval)
|
2021-06-10 10:05:08 +02:00
|
|
|
document.removeEventListener("scroll", debouncedUpdate, true)
|
2021-06-08 09:00:54 +02:00
|
|
|
})
|
|
|
|
</script>
|
|
|
|
|
|
|
|
{#if showBar}
|
|
|
|
<div
|
|
|
|
class="bar"
|
|
|
|
style="top: {top}px; left: {left}px;"
|
|
|
|
bind:this={self}
|
|
|
|
class:visible={measured}
|
|
|
|
>
|
|
|
|
{#each settings as setting, idx}
|
|
|
|
{#if setting.type === "select"}
|
|
|
|
{#each setting.options as option}
|
|
|
|
<SettingsButton
|
|
|
|
prop={setting.key}
|
|
|
|
value={option.value}
|
|
|
|
icon={option.barIcon}
|
2021-06-08 09:14:50 +02:00
|
|
|
title={option.barTitle}
|
2021-06-08 09:00:54 +02:00
|
|
|
/>
|
|
|
|
{/each}
|
|
|
|
{/if}
|
|
|
|
{#if idx < settings.length - 1}
|
|
|
|
<div class="divider" />
|
|
|
|
{/if}
|
|
|
|
{/each}
|
|
|
|
</div>
|
|
|
|
{/if}
|
|
|
|
|
|
|
|
<style>
|
|
|
|
.bar {
|
|
|
|
display: flex;
|
|
|
|
position: absolute;
|
2021-06-10 19:42:41 +02:00
|
|
|
z-index: 930;
|
2021-06-08 15:19:03 +02:00
|
|
|
padding: 6px 8px;
|
2021-06-08 09:00:54 +02:00
|
|
|
opacity: 0;
|
|
|
|
flex-direction: row;
|
2021-06-08 09:14:50 +02:00
|
|
|
background: var(--background);
|
2021-06-08 09:00:54 +02:00
|
|
|
justify-content: center;
|
|
|
|
align-items: center;
|
|
|
|
border-radius: 4px;
|
|
|
|
box-shadow: 2px 2px 8px rgba(0, 0, 0, 0.2);
|
|
|
|
gap: 2px;
|
|
|
|
transition: opacity 0.13s ease-in-out;
|
|
|
|
}
|
|
|
|
.visible {
|
|
|
|
opacity: 1;
|
|
|
|
}
|
|
|
|
.divider {
|
|
|
|
flex: 0 0 1px;
|
|
|
|
align-self: stretch;
|
|
|
|
margin: 0 4px;
|
2021-06-08 09:14:50 +02:00
|
|
|
background-color: var(--spectrum-global-color-gray-300);
|
2021-06-08 09:00:54 +02:00
|
|
|
}
|
|
|
|
</style>
|