AntdDraggableModal.js
6.05 KB
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
import React from 'react';
import { Modal } from 'antd';
import './AntdDraggableModal/antDraggle.less';
class AntDraggableModal extends React.Component {
constructor(props) {
super(props);
this.simpleClass = Math.random().toString(36).substring(2);
this.state = {
isDrop: false,
// eslint-disable-next-line react/no-unused-state
offsetLeft: 0,
// eslint-disable-next-line react/no-unused-state
offsetTop: 0,
realizeHeight: 0,
modalHeight: 0,
};
this.deltaX = 0;
this.deltaY = 0;
this.contain = {};
this.antModal = {};
}
// eslint-disable-next-line react/sort-comp
handleMove = (event) => {
if (this.state.isDrop) {
this.antModal.style.margin = 0;
this.antModal.style.padding = 0;
const left = event.pageX - this.deltaX;
const top = event.pageY - this.deltaY;
// if (left < 0) {
// left = 0;
// } else if (left > (document.offsetWidth - this.antModal.offsetWidth)) {
// left = document.offsetWidth - this.antModal.offsetWidth;
// }
// if (top < 0) {
// top = 0;
// } else if (top > (document.offsetHeight - this.antModal.offsetHeight)) {
// top = document.offsetHeight - this.antModal.offsetHeight;
// }
this.antModal.style.left = `${left}px`;
this.antModal.style.top = `${top}px`;
}
};
addResizeListener = () => {
document.addEventListener('mousedown', this.handleResize);
}
handleResize = (downEl) => {
const ableList = ['resize-s', 'resize-sw', 'resize-se'];
if (ableList.indexOf(downEl.target.className) === -1) {
return;
}
// const modalContent = document.getElementsByClassName('ant-modal-content')[0];
const modalContent = downEl.target.parentNode.parentNode;
const modalContentHeight = modalContent.offsetHeight;
if (!this.state.modalHeight) {
this.setState({
modalHeight: modalContentHeight,
});
}
const modalContentWidth = modalContent.offsetWidth;
const modalContentLeft = modalContent.offsetLeft;
let resizedHeight = 0;
let resizeYPx = 0;
let resizeXPx = 0;
document.onmousemove = (moveEvent) => {
resizeYPx = moveEvent.pageY - downEl.pageY;
resizeXPx = moveEvent.pageX - downEl.pageX;
resizedHeight = this.state.modalHeight + this.state.realizeHeight + resizeYPx;
if (resizedHeight <= this.state.modalHeight) {
return false;
}
switch (downEl.target.className) {
case 'resize-s':
modalContent.style.height = `${resizedHeight}px`;
break;
case 'resize-sw':
modalContent.style.height = `${resizedHeight}px`;
modalContent.style.width = `${modalContentWidth - resizeXPx}px`;
modalContent.style.left = `${modalContentLeft + resizeXPx}px`;
break;
case 'resize-se':
modalContent.style.height = `${resizedHeight}px`;
modalContent.style.width = `${modalContentWidth + resizeXPx}px`;
break;
default:
break;
}
};
document.onmouseup = () => {
document.onmousemove = null;
document.onmouseup = null;
setTimeout(() => {
// eslint-disable-next-line no-unused-expressions
window.onSetTableKey && window.onSetTableKey();
}, 1000);
if (this.state.realizeHeight + resizeYPx <= 0 && this.props.onSaveState) {
this.setState({
realizeHeight: 0,
});
this.props.onSaveState({ realizeHeight: this.state.realizeHeight });
return;
}
if (this.props.onSaveState && resizeYPx !== 0) {
this.setState((prev) => {
return {
realizeHeight: prev.realizeHeight + resizeYPx,
};
});
this.props.onSaveState({ realizeHeight: this.state.realizeHeight });
}
};
}
initialEvent = (visible) => {
const { title } = this.props;
if (title && visible) {
setTimeout(() => {
window.removeEventListener('mouseup', this.removeUp, false);
// eslint-disable-next-line prefer-destructuring
this.contain = document.getElementsByClassName(this.simpleClass)[0];
// eslint-disable-next-line prefer-destructuring
this.header = this.contain.getElementsByClassName('ant-modal-header')[0];
this.header.style.cursor = 'all-scroll';
// eslint-disable-next-line prefer-destructuring
this.antModal = this.contain.getElementsByClassName('ant-modal')[0];
this.header.onmousedown = (e) => {
const disx = e.pageX - this.antModal.offsetLeft;
const disy = e.pageY - this.antModal.offsetTop;
this.deltaX = disx;
this.deltaY = disy;
this.setState({
isDrop: true,
});
document.body.onselectstart = () => false;
window.addEventListener('mousemove', this.handleMove.bind(this), false);
};
window.addEventListener('mouseup', this.removeUp, false);
this.addResizeListener();
}, 0);
}
};
removeUp = () => {
this.setState({ isDrop: false });
document.body.onselectstart = () => true;
};
componentDidMount() {
const { visible = false, open = false } = this.props;
this.initialEvent(open || visible);
}
componentWillUnmount() {
window.removeEventListener('mousedown', this.handleResize, false);
window.removeEventListener('mouseup', this.removeUp, false);
if (this.props.onSaveState) {
this.props.onSaveState({ realizeHeight: 0 });
}
}
render() {
const {
children, wrapClassName, pageLoading, forbidResize, ...other
} = this.props;
const wrapModalClassName = wrapClassName ? `${wrapClassName} ${this.simpleClass}` : `${this.simpleClass}`;
return (
<Modal
{...other}
maskClosable={false}
keyboard
wrapClassName={wrapModalClassName}
>
{children}
{!forbidResize && <div className="resize-s" />}
{!forbidResize && <div className="resize-sw" />}
{!forbidResize && <div className="resize-se" />}
</Modal>
);
}
}
export default AntDraggableModal;