刘光辉
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
import type { App, Directive, DirectiveBinding } from 'vue';
 
interface DraggableElement extends HTMLElement {
  __modal_draggable__?: {
    handleEl: HTMLElement;
    onMousedown: (e: MouseEvent) => void;
  };
}
 
const DRAGGABLE_KEY = '__modal_draggable__' as const;
 
function setupDrag(modalWrap: DraggableElement, handleSelector: string) {
  const dragDom = modalWrap.querySelector('.ant-modal') as HTMLElement;
  const handleEl = modalWrap.querySelector(handleSelector) as HTMLElement;
 
  if (!dragDom || !handleEl) return;
  if (modalWrap[DRAGGABLE_KEY]) return;
 
  handleEl.style.cursor = 'move';
  handleEl.style.userSelect = 'none';
 
  const onMousedown = (e: MouseEvent) => {
    if (!e || modalWrap.classList.contains('fullscreen-modal')) return;
    const disX = e.clientX;
    const disY = e.clientY;
    const screenWidth = document.body.clientWidth;
    const screenHeight = document.documentElement.clientHeight;
 
    const dragDomWidth = dragDom.offsetWidth;
    const dragDomHeight = dragDom.offsetHeight;
    const minDragDomLeft = dragDom.offsetLeft;
    const maxDragDomLeft = screenWidth - dragDom.offsetLeft - dragDomWidth;
    const minDragDomTop = dragDom.offsetTop;
    let maxDragDomTop = screenHeight - dragDom.offsetTop - dragDomHeight;
    if (maxDragDomTop < 0) maxDragDomTop = screenHeight - dragDom.offsetTop;
 
    const domLeft = getComputedStyle(dragDom).left;
    const domTop = getComputedStyle(dragDom).top;
    let styL = +domLeft;
    let styT = +domTop;
 
    if (domLeft.includes('%')) {
      styL = +document.body.clientWidth * (+domLeft.replaceAll('%', '') / 100);
      styT = +document.body.clientHeight * (+domTop.replaceAll('%', '') / 100);
    } else {
      styL = +domLeft.replaceAll('px', '');
      styT = +domTop.replaceAll('px', '');
    }
 
    document.body.style.cursor = 'move';
    document.body.style.userSelect = 'none';
 
    const onMousemove = (e: MouseEvent) => {
      let left = e.clientX - disX;
      let top = e.clientY - disY;
 
      if (-left > minDragDomLeft) {
        left = -minDragDomLeft;
      } else if (left > maxDragDomLeft) {
        left = maxDragDomLeft;
      }
 
      if (-top > minDragDomTop) {
        top = -minDragDomTop;
      } else if (top > maxDragDomTop) {
        top = maxDragDomTop;
      }
 
      dragDom.style.cssText += `;left:${left + styL}px;top:${top + styT}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 DraggableElement)[DRAGGABLE_KEY] = { handleEl, onMousedown };
}
 
function cleanupDrag(modalWrap: DraggableElement) {
  const dragData = modalWrap[DRAGGABLE_KEY];
  if (dragData) {
    dragData.handleEl.removeEventListener('mousedown', dragData.onMousedown);
    delete modalWrap[DRAGGABLE_KEY];
  }
}
 
/** 自动为 .common-container-modal 添加拖拽支持 */
export function setupGlobalDragObserver() {
  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')) {
            const modal = node.querySelector('.common-container-modal');
            if (modal) {
              setTimeout(() => setupDrag(node as DraggableElement, '.ant-modal-header'), 50);
            }
          }
          const wraps = node.querySelectorAll?.('.ant-modal-wrap');
          wraps?.forEach((wrap: HTMLElement) => {
            const modal = wrap.querySelector('.common-container-modal');
            if (modal) {
              setTimeout(() => setupDrag(wrap as DraggableElement, '.ant-modal-header'), 50);
            }
          });
        }
      }
      for (const node of mutation.removedNodes) {
        if (node instanceof HTMLElement) {
          if (node.classList.contains('ant-modal-wrap')) {
            cleanupDrag(node as DraggableElement);
          }
          const wraps = node.querySelectorAll?.('.ant-modal-wrap');
          wraps?.forEach((wrap: HTMLElement) => cleanupDrag(wrap as DraggableElement));
        }
      }
    }
  });
 
  observer.observe(document.body, { childList: true, subtree: true });
  return observer;
}
 
/** v-draggable 指令,用于显式标记需要拖拽的 modal */
export const vDraggable: Directive = {
  mounted(el: HTMLElement, binding: DirectiveBinding<string | undefined>) {
    const handleSelector = binding.value || '.ant-modal-header';
    const modalWrap = (el.classList.contains('ant-modal-wrap') ? el : el.closest('.ant-modal-wrap')) as DraggableElement;
    if (!modalWrap) return;
 
    const observer = new MutationObserver(() => {
      const dragDom = modalWrap.querySelector('.ant-modal');
      if (dragDom) {
        observer.disconnect();
        setupDrag(modalWrap, handleSelector);
      }
    });
 
    observer.observe(modalWrap, { childList: true, subtree: true });
 
    if (modalWrap.querySelector('.ant-modal')) {
      observer.disconnect();
      setupDrag(modalWrap, handleSelector);
    }
  },
 
  unmounted(el: HTMLElement) {
    const modalWrap = (el.classList.contains('ant-modal-wrap') ? el : el.closest('.ant-modal-wrap')) as DraggableElement;
    if (!modalWrap) return;
    cleanupDrag(modalWrap);
  },
};
 
/** 全局注册指令和自动拖拽 */
export function registerDraggable(app: App) {
  app.directive('draggable', vDraggable);
  setupGlobalDragObserver();
}