刘光辉
14 小时以前 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import type { App, Directive } from 'vue';
 
interface ResizableElement extends HTMLElement {
  __modal_resizable__?: {
    handleEl: HTMLElement;
    initialHeight?: number;
    initialWidth?: number;
    onMousedown: (e: MouseEvent) => void;
    styleObserver: MutationObserver;
  };
}
 
const RESIZABLE_KEY = '__modal_resizable__' as const;
 
function resetModalSize(modalWrap: ResizableElement) {
  const dragDom = modalWrap.querySelector('.ant-modal') as HTMLElement;
  const modalContent = modalWrap.querySelector('.ant-modal-content') as HTMLElement;
  if (dragDom) {
    dragDom.style.width = '';
    dragDom.style.left = '';
    dragDom.style.top = '';
  }
  if (modalContent) {
    modalContent.style.height = '';
    modalContent.classList.remove('is-modal-resized');
  }
  const resizeData = modalWrap[RESIZABLE_KEY];
  if (resizeData) {
    resizeData.initialWidth = undefined;
    resizeData.initialHeight = undefined;
  }
}
 
function setupResize(modalWrap: ResizableElement) {
  const dragDom = modalWrap.querySelector('.ant-modal') as HTMLElement;
  const modalContent = modalWrap.querySelector('.ant-modal-content') as HTMLElement;
 
  if (!dragDom || !modalContent) return;
  if (modalWrap[RESIZABLE_KEY]) return;
  if (modalWrap.classList.contains('fullscreen-modal')) return;
  if (dragDom.classList.contains('ant-modal-confirm')) return;
 
  const handleEl = document.createElement('div');
  handleEl.className = 'modal-resize-handle';
  modalContent.append(handleEl);
 
  const styleObserver = new MutationObserver(() => {
    if (modalWrap.style.display === 'none') {
      resetModalSize(modalWrap);
    }
  });
  styleObserver.observe(modalWrap, { attributes: true, attributeFilter: ['style'] });
 
  const onMousedown = (e: MouseEvent) => {
    if (!e) return;
    e.preventDefault();
    e.stopPropagation();
 
    const resizeData = modalWrap[RESIZABLE_KEY];
    if (!resizeData) return;
    if (!resizeData.initialWidth || !resizeData.initialHeight) {
      resizeData.initialWidth = dragDom.offsetWidth;
      resizeData.initialHeight = dragDom.offsetHeight;
    }
 
    const startX = e.clientX;
    const startY = e.clientY;
    const startWidth = dragDom.offsetWidth;
    const startHeight = dragDom.offsetHeight;
    const minWidth = resizeData.initialWidth;
    const minHeight = resizeData.initialHeight;
    const startLeft = dragDom.getBoundingClientRect().left;
    const startTop = dragDom.getBoundingClientRect().top;
 
    const screenWidth = document.body.clientWidth;
    const screenHeight = document.documentElement.clientHeight;
 
    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(minWidth, startWidth + deltaX);
      let newHeight = Math.max(minHeight, startHeight + deltaY);
 
      newWidth = Math.min(newWidth, screenWidth - startLeft);
      newHeight = Math.min(newHeight, screenHeight - startTop);
 
      modalContent.classList.add('is-modal-resized');
      dragDom.style.width = `${newWidth}px`;
      modalContent.style.height = `${newHeight}px`;
 
      const currentLeft = dragDom.getBoundingClientRect().left;
      if (currentLeft !== startLeft) {
        const offset = currentLeft - startLeft;
        const currentStyleLeft = Number.parseFloat(getComputedStyle(dragDom).left) || 0;
        dragDom.style.left = `${currentStyleLeft - offset}px`;
      }
 
      const currentTop = dragDom.getBoundingClientRect().top;
      if (currentTop !== startTop) {
        const offset = currentTop - startTop;
        const currentStyleTop = Number.parseFloat(getComputedStyle(dragDom).top) || 0;
        dragDom.style.top = `${currentStyleTop - offset}px`;
      }
    };
 
    const onMouseup = () => {
      document.body.style.cursor = '';
      document.body.style.userSelect = '';
      document.removeEventListener('mousemove', onMousemove);
      document.removeEventListener('mouseup', onMouseup);
    };
 
    document.addEventListener('mousemove', onMousemove);
    document.addEventListener('mouseup', onMouseup);
  };
 
  handleEl.addEventListener('mousedown', onMousedown);
  (modalWrap as ResizableElement)[RESIZABLE_KEY] = { handleEl, onMousedown, styleObserver };
}
 
function cleanupResize(modalWrap: ResizableElement) {
  const resizeData = modalWrap[RESIZABLE_KEY];
  if (resizeData) {
    resizeData.handleEl.removeEventListener('mousedown', resizeData.onMousedown);
    resizeData.handleEl.remove();
    resizeData.styleObserver.disconnect();
    resetModalSize(modalWrap);
    delete modalWrap[RESIZABLE_KEY];
  }
}
 
/** 自动为所有 .ant-modal-wrap 添加 resize 支持 */
export function setupGlobalResizeObserver() {
  const observer = new MutationObserver((mutations) => {
    for (const mutation of mutations) {
      for (const node of mutation.addedNodes) {
        if (node instanceof HTMLElement) {
          if (node.classList.contains('ant-modal-wrap')) {
            setTimeout(() => setupResize(node as ResizableElement), 50);
          }
          const wraps = node.querySelectorAll?.('.ant-modal-wrap');
          wraps?.forEach((wrap) => {
            setTimeout(() => setupResize(wrap as ResizableElement), 50);
          });
        }
      }
      for (const node of mutation.removedNodes) {
        if (node instanceof HTMLElement) {
          if (node.classList.contains('ant-modal-wrap')) {
            cleanupResize(node as ResizableElement);
          }
          const wraps = node.querySelectorAll?.('.ant-modal-wrap');
          wraps?.forEach((wrap) => cleanupResize(wrap as ResizableElement));
        }
      }
    }
  });
 
  observer.observe(document.body, { childList: true, subtree: true });
  return observer;
}
 
/** v-resizable 指令,用于显式标记需要 resize 的 modal */
export const vResizable: Directive = {
  mounted(el: HTMLElement) {
    const modalWrap = (el.classList.contains('ant-modal-wrap') ? el : el.closest('.ant-modal-wrap')) as ResizableElement;
    if (!modalWrap) return;
 
    const observer = new MutationObserver(() => {
      const dragDom = modalWrap.querySelector('.ant-modal');
      if (dragDom) {
        observer.disconnect();
        setupResize(modalWrap);
      }
    });
 
    observer.observe(modalWrap, { childList: true, subtree: true });
 
    if (modalWrap.querySelector('.ant-modal')) {
      observer.disconnect();
      setupResize(modalWrap);
    }
  },
 
  unmounted(el: HTMLElement) {
    const modalWrap = (el.classList.contains('ant-modal-wrap') ? el : el.closest('.ant-modal-wrap')) as ResizableElement;
    if (!modalWrap) return;
    cleanupResize(modalWrap);
  },
};
 
/** 全局注册指令和自动 resize */
export function registerResizable(app: App) {
  app.directive('resizable', vResizable);
  setupGlobalResizeObserver();
}