|
1 | | -import { computed, onUnmounted, ref } from 'vue' |
| 1 | +import { computed, onUnmounted } from 'vue' |
2 | 2 |
|
3 | 3 | import { useWindowSize } from '@vueuse/core' |
4 | 4 |
|
| 5 | +import useDragging from '/@/composables/dom/useDragging' |
5 | 6 | import { createAnimationFrameController } from '/@/lib/dom/animationFrame' |
6 | 7 | import { |
7 | 8 | MAX_NAVIGATION_WIDTH_RATIO, |
@@ -40,62 +41,37 @@ const useNavigationResizer = () => { |
40 | 41 | return clampWidth(navigationWidth.value) |
41 | 42 | }) |
42 | 43 |
|
43 | | - const isResizing = ref(false) |
44 | | - let pointerId: null | number = null |
| 44 | + const { isDragging, onDragStart, onDragging, onDragEnd } = useDragging({ |
| 45 | + targetRef: resizerRef, |
| 46 | + onDragStart: () => { |
| 47 | + document.body.style.cursor = 'e-resize' |
| 48 | + document.body.style.userSelect = 'none' |
| 49 | + if (!isNavigationClosed.value) updateNavigationLeft() |
| 50 | + }, |
| 51 | + onDragging: (e: PointerEvent) => { |
| 52 | + animationFrame.request(() => { |
| 53 | + const width = e.clientX - navigationLeft.value |
| 54 | + |
| 55 | + if (width <= NAVIGATION_CLOSING_THRESHOLD) { |
| 56 | + closeNavigation() |
| 57 | + } else { |
| 58 | + setNavigationWidth(width) |
| 59 | + } |
| 60 | + }) |
| 61 | + }, |
| 62 | + onDragEnd: () => { |
| 63 | + document.body.style.cursor = '' |
| 64 | + document.body.style.userSelect = '' |
| 65 | + if (navigationWidth.value > 0) saveNavigationWidth() |
| 66 | + } |
| 67 | + }) |
45 | 68 |
|
46 | | - const cleanup = () => { |
| 69 | + onUnmounted(() => { |
47 | 70 | animationFrame.cancel() |
48 | | - |
49 | | - document.body.style.cursor = '' |
50 | | - document.body.style.userSelect = '' |
51 | | - |
52 | | - if (!pointerId || !resizerRef.value?.hasPointerCapture(pointerId)) return |
53 | | - resizerRef.value?.releasePointerCapture(pointerId) |
54 | | - } |
55 | | - |
56 | | - const onDragStart = (e: PointerEvent) => { |
57 | | - isResizing.value = true |
58 | | - |
59 | | - if (!isNavigationClosed.value) updateNavigationLeft() |
60 | | - |
61 | | - document.body.style.cursor = 'e-resize' |
62 | | - document.body.style.userSelect = 'none' |
63 | | - |
64 | | - pointerId = e.pointerId |
65 | | - resizerRef.value?.setPointerCapture(pointerId) |
66 | | - |
67 | | - e.preventDefault() |
68 | | - } |
69 | | - |
70 | | - const onDragging = (e: PointerEvent) => { |
71 | | - if (!isResizing.value) return |
72 | | - |
73 | | - animationFrame.request(() => { |
74 | | - const width = e.clientX - navigationLeft.value |
75 | | - |
76 | | - if (width <= NAVIGATION_CLOSING_THRESHOLD) { |
77 | | - closeNavigation() |
78 | | - } else { |
79 | | - setNavigationWidth(width) |
80 | | - } |
81 | | - }) |
82 | | - |
83 | | - e.preventDefault() |
84 | | - } |
85 | | - |
86 | | - const onDragEnd = () => { |
87 | | - if (!isResizing.value) return |
88 | | - |
89 | | - if (navigationWidth.value > 0) saveNavigationWidth() |
90 | | - |
91 | | - isResizing.value = false |
92 | | - cleanup() |
93 | | - } |
94 | | - |
95 | | - onUnmounted(cleanup) |
| 71 | + }) |
96 | 72 |
|
97 | 73 | return { |
98 | | - isNavigationResizing: isResizing, |
| 74 | + isNavigationResizing: isDragging, |
99 | 75 | navigationWidth: clampedNavigationWidth, |
100 | 76 | onDragStart, |
101 | 77 | onDragging, |
|
0 commit comments