|
1 | 1 | import type {ReactElement, MouseEvent} from "react"; |
2 | | -import {memo, useCallback, useLayoutEffect, useRef} from "react"; |
| 2 | +import {memo, useCallback, useRef} from "react"; |
3 | 3 | import type {Node, NodeProps} from "@xyflow/react"; |
4 | 4 | import {Handle, Position} from "@xyflow/react"; |
5 | 5 | import cc from "classcat"; |
6 | 6 | import type {TreeNode} from "../tree-description"; |
7 | 7 | import {NodeIcon} from "./NodeIcon"; |
8 | 8 | import "./QueryNode.css"; |
9 | 9 | import {useGraphRenderingStore} from "./store"; |
10 | | -import {animationStartTime, graphAnimationProgress} from "./animation-timing"; |
| 10 | +import {useGraphAnimationController} from "./useAnimatedGraphLayout"; |
11 | 11 |
|
12 | 12 | export type QueryGraphNode = Node<TreeNode, "querynode">; |
13 | 13 |
|
14 | 14 | function QueryNode({data, id}: NodeProps<QueryGraphNode>) { |
15 | 15 | const expanded = useGraphRenderingStore((s) => s.expandedNodes[id]); |
16 | 16 | const toggleNode = useGraphRenderingStore((s) => s.toggleExpandedNode); |
17 | | - const finishNodeAnimation = useGraphRenderingStore((s) => s.finishNodeAnimation); |
18 | | - const sizeAnimation = useGraphRenderingStore((s) => s.nodeSizeAnimations.get(id)); |
19 | 17 | const subtreeExpanded = useGraphRenderingStore((s) => s.expandedSubtrees[id]); |
20 | 18 | const toggleSubtree = useGraphRenderingStore((s) => s.toggleExpandedSubtree); |
| 19 | + const animationController = useGraphAnimationController(); |
21 | 20 |
|
22 | 21 | const hasProperties = data.properties?.size; |
23 | 22 | const hasSubtree = data.collapsedChildren && data.collapsedChildren.length > 0; |
@@ -52,65 +51,40 @@ function QueryNode({data, id}: NodeProps<QueryGraphNode>) { |
52 | 51 | return measurements; |
53 | 52 | }, []); |
54 | 53 |
|
55 | | - useLayoutEffect(() => { |
56 | | - const element = bodyWrapperRef.current; |
57 | | - if (element === null) return; |
58 | | - if (sizeAnimation === undefined) { |
59 | | - element.style.removeProperty("width"); |
60 | | - element.style.removeProperty("height"); |
61 | | - element.style.removeProperty("max-width"); |
62 | | - element.style.removeProperty("max-height"); |
63 | | - return; |
64 | | - } |
65 | | - |
66 | | - element.style.maxWidth = "none"; |
67 | | - element.style.maxHeight = "none"; |
68 | | - let animationFrame: number | undefined; |
69 | | - const step = (now: number) => { |
70 | | - const progress = graphAnimationProgress(sizeAnimation.startedAt, now); |
71 | | - const width = sizeAnimation.from.width + (sizeAnimation.to.width - sizeAnimation.from.width) * progress; |
72 | | - const height = sizeAnimation.from.height + (sizeAnimation.to.height - sizeAnimation.from.height) * progress; |
73 | | - element.style.width = `${width}px`; |
74 | | - element.style.height = `${height}px`; |
75 | | - if (progress < 1) animationFrame = requestAnimationFrame(step); |
76 | | - else finishNodeAnimation(id); |
77 | | - }; |
78 | | - step(sizeAnimation.startedAt); |
79 | | - return () => { |
80 | | - if (animationFrame !== undefined) cancelAnimationFrame(animationFrame); |
81 | | - }; |
82 | | - }, [finishNodeAnimation, id, sizeAnimation]); |
83 | | - |
84 | 54 | const onClick = useCallback( |
85 | 55 | (e: MouseEvent) => { |
86 | 56 | if (e.shiftKey) { |
87 | | - if (hasSubtree) toggleSubtree(id, animationStartTime()); |
| 57 | + if (hasSubtree) animationController.animateSubtreeChange(id, () => toggleSubtree(id)); |
88 | 58 | } else { |
89 | 59 | if (hasProperties) { |
90 | 60 | const target = measureTargetDimensions(!expanded); |
91 | 61 | const bodyWrapper = bodyWrapperRef.current; |
92 | | - const startedAt = animationStartTime(); |
93 | | - const sizeAnimation = |
94 | | - startedAt === undefined || bodyWrapper === null |
95 | | - ? undefined |
96 | | - : { |
97 | | - from: {width: bodyWrapper.offsetWidth, height: bodyWrapper.offsetHeight}, |
98 | | - to: target.body, |
99 | | - startedAt, |
100 | | - }; |
101 | | - toggleNode(id, target.node, sizeAnimation); |
| 62 | + if (bodyWrapper === null) { |
| 63 | + toggleNode(id); |
| 64 | + } else { |
| 65 | + animationController.animateNodeResize( |
| 66 | + { |
| 67 | + nodeId: id, |
| 68 | + targetDimensions: target.node, |
| 69 | + bodyElement: bodyWrapper, |
| 70 | + bodyFrom: {width: bodyWrapper.offsetWidth, height: bodyWrapper.offsetHeight}, |
| 71 | + bodyTo: target.body, |
| 72 | + }, |
| 73 | + () => toggleNode(id), |
| 74 | + ); |
| 75 | + } |
102 | 76 | } |
103 | 77 | } |
104 | 78 | e.stopPropagation(); |
105 | 79 | }, |
106 | | - [toggleNode, toggleSubtree, hasProperties, hasSubtree, expanded, id, measureTargetDimensions], |
| 80 | + [animationController, toggleNode, toggleSubtree, hasProperties, hasSubtree, expanded, id, measureTargetDimensions], |
107 | 81 | ); |
108 | 82 | const onSubtreeHandleClick = useCallback( |
109 | 83 | (e: MouseEvent) => { |
110 | | - if (hasSubtree) toggleSubtree(id, animationStartTime()); |
| 84 | + if (hasSubtree) animationController.animateSubtreeChange(id, () => toggleSubtree(id)); |
111 | 85 | e.stopPropagation(); |
112 | 86 | }, |
113 | | - [toggleSubtree, hasSubtree, id], |
| 87 | + [animationController, toggleSubtree, hasSubtree, id], |
114 | 88 | ); |
115 | 89 | const children = [] as ReactElement[]; |
116 | 90 | for (const [key, value] of (data.properties || []).entries()) { |
|
0 commit comments