zhangwencui
6 天以前 92f48fea04ed14d188652afa6bf11f3d6f940b5c
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import { ref } from 'vue';
 
interface DragOptions {
  max: number;
  min: number;
}
 
interface DragElements {
  dragBar: HTMLElement | null;
  target: HTMLElement | null;
}
 
type DragCallback = (newWidth: number) => void;
 
export function useSidebarDrag() {
  const isDragging = ref(false);
  let cleanup: (() => void) | null = null;
  let dragOverlay: HTMLElement | null = null;
 
  const startDrag = (
    e: MouseEvent,
    options: DragOptions,
    elements: DragElements,
    onDrag: DragCallback,
  ) => {
    const { min, max } = options;
    const { dragBar, target } = elements;
 
    if (isDragging.value || !dragBar || !target) return;
 
    e.preventDefault();
    e.stopPropagation();
 
    isDragging.value = true;
 
    const startX = e.clientX;
    const startWidth = target.getBoundingClientRect().width;
    const startLeft = dragBar.offsetLeft;
 
    dragBar.classList.add('bg-primary');
    dragBar.classList.remove('bg-primary/30');
 
    const dragBarTransition = dragBar.style.transition;
    const targetTransition = target.style.transition;
 
    dragBar.style.transition = 'none';
    target.style.transition = 'none';
 
    dragOverlay = document.createElement('div');
    dragOverlay.style.position = 'fixed';
    dragOverlay.style.inset = '0';
    dragOverlay.style.zIndex = '9999';
    dragOverlay.style.cursor = 'col-resize';
    dragOverlay.style.userSelect = 'none';
    dragOverlay.style.outline = 'none';
    dragOverlay.tabIndex = -1;
    dragOverlay.style.background = 'rgba(0,0,0,0)';
    document.body.append(dragOverlay);
 
    const onMouseMove = (moveEvent: MouseEvent) => {
      if (!isDragging.value || !dragBar || !target) {
        endDrag();
        return;
      }
 
      const deltaX = moveEvent.clientX - startX;
      let currentWidth = startWidth + deltaX;
 
      const isOutOfMin = currentWidth < min;
      const isOutOfMax = currentWidth > max;
      const isOutOfBounds = isOutOfMin || isOutOfMax;
 
      if (isOutOfMin) currentWidth = min;
      if (isOutOfMax) currentWidth = max;
 
      const newLeft = startLeft + (currentWidth - startWidth);
 
      if (dragOverlay)
        dragOverlay.style.cursor = isOutOfBounds ? 'not-allowed' : 'col-resize';
 
      dragBar.style.left = `${newLeft}px`;
 
      if (isOutOfBounds) {
        dragBar.classList.add('bg-primary/30');
        dragBar.classList.remove('bg-primary');
      } else {
        dragBar.classList.add('bg-primary');
        dragBar.classList.remove('bg-primary/30');
      }
    };
 
    const onMouseUp = (upEvent: MouseEvent) => {
      if (!isDragging.value || !dragBar || !target) {
        endDrag();
        return;
      }
 
      const deltaX = upEvent.clientX - startX;
      let newWidth = startWidth + deltaX;
 
      newWidth = Math.min(max, Math.max(min, newWidth));
 
      dragBar.classList.remove('bg-primary', 'bg-primary/30');
 
      try {
        onDrag?.(Math.round(newWidth));
      } finally {
        endDrag();
      }
    };
 
    document.addEventListener('mousemove', onMouseMove);
    document.addEventListener('mouseup', onMouseUp);
 
    cleanup = () => {
      if (!cleanup) return;
 
      document.removeEventListener('mousemove', onMouseMove);
      document.removeEventListener('mouseup', onMouseUp);
 
      if (dragBar) {
        dragBar.style.transition = dragBarTransition;
        dragBar.style.left = '';
        dragBar.classList.remove('bg-primary', 'bg-primary/30');
      }
 
      if (target) {
        target.style.transition = targetTransition;
      }
 
      if (dragOverlay) {
        dragOverlay.remove();
        dragOverlay = null;
      }
 
      isDragging.value = false;
      cleanup = null;
    };
  };
 
  const endDrag = () => {
    cleanup?.();
  };
 
  return {
    startDrag,
    endDrag,
    get isDragging() {
      return isDragging.value;
    },
  };
}