2022-01-14 04:25:47 +00:00
|
|
|
<template>
|
|
|
|
<slot />
|
|
|
|
<div ref="resizeListener" class="resize-listener" />
|
2022-01-25 04:25:34 +00:00
|
|
|
<svg v-if="validLinks" v-bind="$attrs">
|
2022-01-14 04:25:47 +00:00
|
|
|
<LinkVue
|
|
|
|
v-for="(link, index) in validLinks"
|
|
|
|
:key="index"
|
|
|
|
:link="link"
|
2022-01-25 04:23:30 +00:00
|
|
|
:startNode="nodes[link.startNode.id]!"
|
|
|
|
:endNode="nodes[link.endNode.id]!"
|
2022-01-14 04:25:47 +00:00
|
|
|
/>
|
|
|
|
</svg>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script setup lang="ts">
|
|
|
|
import {
|
|
|
|
Link,
|
|
|
|
LinkNode,
|
|
|
|
RegisterLinkNodeInjectionKey,
|
|
|
|
UnregisterLinkNodeInjectionKey
|
|
|
|
} from "@/features/links";
|
2022-02-27 19:49:34 +00:00
|
|
|
import { computed, nextTick, onMounted, provide, ref, toRef } from "vue";
|
2022-01-14 04:25:47 +00:00
|
|
|
import LinkVue from "./Link.vue";
|
|
|
|
|
2022-02-27 19:49:34 +00:00
|
|
|
const _props = defineProps<{ links?: Link[] }>();
|
|
|
|
const links = toRef(_props, "links");
|
2022-01-14 04:25:47 +00:00
|
|
|
|
2022-01-25 04:23:30 +00:00
|
|
|
const observer = new MutationObserver(updateNodes);
|
|
|
|
const resizeObserver = new ResizeObserver(updateBounds);
|
|
|
|
|
|
|
|
const nodes = ref<Record<string, LinkNode | undefined>>({});
|
|
|
|
const boundingRect = ref(new DOMRect());
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
updateNodes();
|
|
|
|
});
|
|
|
|
|
2022-02-27 19:49:34 +00:00
|
|
|
const validLinks = computed(
|
|
|
|
() =>
|
|
|
|
links.value?.filter(link => {
|
|
|
|
const n = nodes.value;
|
|
|
|
return (
|
|
|
|
n[link.startNode.id]?.x != undefined &&
|
|
|
|
n[link.startNode.id]?.y != undefined &&
|
|
|
|
n[link.endNode.id]?.x != undefined &&
|
|
|
|
n[link.endNode.id]?.y != undefined
|
|
|
|
);
|
|
|
|
}) ?? []
|
2022-01-14 04:25:47 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
const observerOptions = {
|
|
|
|
attributes: true,
|
|
|
|
childList: true,
|
|
|
|
subtree: true
|
|
|
|
};
|
|
|
|
|
|
|
|
provide(RegisterLinkNodeInjectionKey, (id, element) => {
|
|
|
|
nodes.value[id] = { element };
|
|
|
|
observer.observe(element, observerOptions);
|
|
|
|
nextTick(() => {
|
|
|
|
if (resizeListener.value != null) {
|
|
|
|
updateNode(id);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
provide(UnregisterLinkNodeInjectionKey, id => {
|
2022-01-25 04:23:30 +00:00
|
|
|
nodes.value[id] = undefined;
|
2022-01-14 04:25:47 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
function updateNodes() {
|
|
|
|
if (resizeListener.value != null) {
|
|
|
|
Object.keys(nodes.value).forEach(id => updateNode(id));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function updateNode(id: string) {
|
2022-01-25 04:23:30 +00:00
|
|
|
const node = nodes.value[id];
|
|
|
|
if (!node) {
|
2022-01-14 04:25:47 +00:00
|
|
|
return;
|
|
|
|
}
|
2022-01-25 04:23:30 +00:00
|
|
|
const linkStartRect = node.element.getBoundingClientRect();
|
|
|
|
node.x = linkStartRect.x + linkStartRect.width / 2 - boundingRect.value.x;
|
|
|
|
node.y = linkStartRect.y + linkStartRect.height / 2 - boundingRect.value.y;
|
2022-01-14 04:25:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function updateBounds() {
|
|
|
|
if (resizeListener.value != null) {
|
|
|
|
boundingRect.value = resizeListener.value.getBoundingClientRect();
|
|
|
|
updateNodes();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
</script>
|
|
|
|
|
|
|
|
<style scoped>
|
|
|
|
svg,
|
|
|
|
.resize-listener {
|
|
|
|
position: absolute;
|
|
|
|
top: 0;
|
|
|
|
left: 0;
|
|
|
|
width: 100%;
|
|
|
|
height: 100%;
|
|
|
|
z-index: -10;
|
|
|
|
pointer-events: none;
|
|
|
|
}
|
|
|
|
</style>
|