Profectus/src/components/Context.vue

90 lines
2.5 KiB
Vue
Raw Normal View History

2022-01-14 04:25:47 +00:00
<template>
<slot />
<div ref="resizeListener" class="resize-listener" />
2022-01-14 04:25:47 +00:00
</template>
<script setup lang="ts">
import {
RegisterNodeInjectionKey,
UnregisterNodeInjectionKey,
NodesInjectionKey,
BoundsInjectionKey
} from "game/layers";
2022-06-27 00:17:22 +00:00
import type { FeatureNode } from "game/layers";
import { nextTick, onMounted, provide, ref } from "vue";
2022-01-25 04:23:30 +00:00
const emit = defineEmits<{
(e: "updateNodes", nodes: Record<string, FeatureNode | undefined>): void;
}>();
const nodes = ref<Record<string, FeatureNode | undefined>>({});
2022-01-25 04:23:30 +00:00
const resizeObserver = new ResizeObserver(updateBounds);
const resizeListener = ref<Element | null>(null);
onMounted(() => {
// ResizeListener exists because ResizeObserver's don't work when told to observe an SVG element
const resListener = resizeListener.value;
if (resListener != null) {
resizeObserver.observe(resListener);
}
});
let isDirty = true;
let boundingRect = ref(resizeListener.value?.getBoundingClientRect());
function updateBounds() {
if (resizeListener.value != null && isDirty) {
isDirty = false;
nextTick(() => {
boundingRect.value = resizeListener.value?.getBoundingClientRect();
(Object.values(nodes.value) as FeatureNode[])
.filter(n => n) // Sometimes the values become undefined
.forEach(node => (node.rect = node.element.getBoundingClientRect()));
emit("updateNodes", nodes.value);
isDirty = true;
});
}
}
document.fonts.ready.then(updateBounds);
2022-01-14 04:25:47 +00:00
const observerOptions = {
attributes: false,
2022-01-14 04:25:47 +00:00
childList: true,
subtree: false
2022-01-14 04:25:47 +00:00
};
provide(RegisterNodeInjectionKey, (id, element) => {
2022-03-23 03:55:48 +00:00
const observer = new MutationObserver(() => updateNode(id));
2022-01-14 04:25:47 +00:00
observer.observe(element, observerOptions);
nodes.value[id] = { element, observer, rect: element.getBoundingClientRect() };
emit("updateNodes", nodes.value);
2022-03-23 03:55:48 +00:00
nextTick(() => updateNode(id));
2022-01-14 04:25:47 +00:00
});
provide(UnregisterNodeInjectionKey, id => {
nodes.value[id]?.observer.disconnect();
2022-01-25 04:23:30 +00:00
nodes.value[id] = undefined;
emit("updateNodes", nodes.value);
2022-01-14 04:25:47 +00:00
});
provide(NodesInjectionKey, nodes);
provide(BoundsInjectionKey, boundingRect);
2022-01-14 04:25:47 +00:00
function updateNode(id: string) {
2022-01-25 04:23:30 +00:00
const node = nodes.value[id];
2022-03-23 03:55:48 +00:00
if (node == null) {
2022-01-14 04:25:47 +00:00
return;
}
2022-03-23 03:55:48 +00:00
node.rect = node.element.getBoundingClientRect();
emit("updateNodes", nodes.value);
2022-01-14 04:25:47 +00:00
}
</script>
<style scoped>
.resize-listener {
position: absolute;
top: 0px;
left: 0;
right: -4px;
bottom: 5px;
z-index: -10;
pointer-events: none;
}
</style>