刘光辉
10 小时以前 0dfe84494048ce27ba8449831782128412d3eb13
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
import type { ComputedRef, Ref } from 'vue';
 
import { onBeforeUnmount, ref, watch } from 'vue';
 
export function useModalResizable(
  targetRef: Ref<HTMLElement | undefined>,
  resizable: ComputedRef<boolean>,
  containerSelector?: ComputedRef<string | undefined>,
) {
  const resizing = ref(false);
  const handleRef = ref<HTMLElement>();
 
  function onMousedown(e: MouseEvent) {
    e.preventDefault();
    e.stopPropagation();
 
    const target = targetRef.value;
    if (!target) return;
 
    const startX = e.clientX;
    const startY = e.clientY;
    const startWidth = target.offsetWidth;
    const startHeight = target.offsetHeight;
    const startLeft = target.getBoundingClientRect().left;
    const startTop = target.getBoundingClientRect().top;
 
    resizing.value = true;
    document.body.style.cursor = 'nwse-resize';
    document.body.style.userSelect = 'none';
 
    const onMousemove = (e: MouseEvent) => {
      const deltaX = e.clientX - startX;
      const deltaY = e.clientY - startY;
 
      let newWidth = Math.max(startWidth, startWidth + deltaX);
      let newHeight = Math.max(startHeight, startHeight + deltaY);
 
      if (containerSelector?.value) {
        const container = document.querySelector(containerSelector.value);
        if (container) {
          const containerRect = container.getBoundingClientRect();
          newWidth = Math.min(newWidth, containerRect.right - startLeft);
          newHeight = Math.min(newHeight, containerRect.bottom - startTop);
        }
      } else {
        const docElement = document.documentElement;
        newWidth = Math.min(newWidth, docElement.clientWidth - startLeft);
        newHeight = Math.min(newHeight, docElement.clientHeight - startTop);
      }
 
      target.style.width = `${newWidth}px`;
      target.style.height = `${newHeight}px`;
 
      const currentLeft = target.getBoundingClientRect().left;
      const currentTop = target.getBoundingClientRect().top;
      if (currentLeft !== startLeft || currentTop !== startTop) {
        const offsetX = currentLeft - startLeft;
        const offsetY = currentTop - startTop;
        const transform = target.style.transform || '';
        const matchX = transform.match(/translate\(([-\d.]+)px/);
        const matchY = transform.match(/translate\([-\d.]+px,\s*([-\d.]+)px/);
        const currentTranslateX = matchX && matchX[1] ? Number.parseFloat(matchX[1]) : 0;
        const currentTranslateY = matchY && matchY[1] ? Number.parseFloat(matchY[1]) : 0;
        const newTranslateX = currentTranslateX - offsetX;
        const newTranslateY = currentTranslateY - offsetY;
        target.style.transform = `translate(${newTranslateX}px, ${newTranslateY}px)`;
      }
    };
 
    const onMouseup = () => {
      resizing.value = false;
      document.body.style.cursor = '';
      document.body.style.userSelect = '';
      document.removeEventListener('mousemove', onMousemove);
      document.removeEventListener('mouseup', onMouseup);
    };
 
    document.addEventListener('mousemove', onMousemove);
    document.addEventListener('mouseup', onMouseup);
  }
 
  function addHandle() {
    const target = targetRef.value;
    if (!target || handleRef.value) return;
 
    const handle = document.createElement('div');
    handle.className = 'modal-resize-handle';
    Object.assign(handle.style, {
      position: 'absolute',
      right: '2px',
      bottom: '2px',
      width: '18px',
      height: '18px',
      cursor: 'nwse-resize',
      zIndex: '10',
    });
 
    handle.addEventListener('mousedown', onMousedown);
    target.append(handle);
    handleRef.value = handle;
  }
 
  function removeHandle() {
    if (handleRef.value) {
      handleRef.value.removeEventListener('mousedown', onMousedown);
      handleRef.value.remove();
      handleRef.value = undefined;
    }
  }
 
  function resetSize() {
    const target = targetRef.value;
    if (target) {
      target.style.width = '';
      target.style.height = '';
      target.style.transform = '';
    }
  }
 
  watch(
    () => targetRef.value,
    (target) => {
      if (target && resizable.value) {
        addHandle();
      } else {
        removeHandle();
      }
    },
    { immediate: true },
  );
 
  watch(resizable, (val) => {
    if (val && targetRef.value) {
      addHandle();
    } else {
      removeHandle();
    }
  });
 
  onBeforeUnmount(() => {
    removeHandle();
  });
 
  return {
    resizing,
    resetSize,
  };
}