-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathViewportAdjuster.svelte
63 lines (55 loc) · 1.52 KB
/
ViewportAdjuster.svelte
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<script>
import {
useSvelteFlow
} from '@xyflow/svelte';
let svelteFlowElement;
export let selection;
export let nodes;
export let focusOn = true;
function calculateBoundingBox(selectedNodeIds, nodes) {
if (!selectedNodeIds || selectedNodeIds.length === 0) return null;
let minX = Infinity;
let minY = Infinity;
let maxX = -Infinity;
let maxY = -Infinity;
selectedNodeIds.forEach((nodeId) => {
const node = nodes.find((n) => n.id === nodeId);
if (node) {
const { x, y, width, height } = node;
minX = Math.min(minX, x);
minY = Math.min(minY, y);
maxX = Math.max(maxX, x + width);
maxY = Math.max(maxY, y + height);
}
});
return {
x: minX,
y: minY,
width: maxX - minX,
height: maxY - minY,
};
}
function fitViewToSelection(selectedNodeIds, nodes) {
// const boundingBox = calculateBoundingBox(selectedNodeIds, nodes);
// if (boundingBox) {
if (selectedNodeIds.length > 0) {
const nd = nodes.filter(node => selectedNodeIds.includes(node.id));
// console.log(svelteFlowElement.getViewport())
//TODO: Don't change view if node is already in the view
svelteFlowElement.fitView({
nodes: nd,
maxZoom: 1,
duration: 200,
// includeHiddenNodes: true
});
}
}
$: {
if (selection && selection.nodeIds && focusOn) {
fitViewToSelection(selection.nodeIds, nodes);
}
}
$: {
svelteFlowElement = useSvelteFlow();
}
</script>