forked from folio-org/stripes-components
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPaneResizeContainer.js
251 lines (228 loc) · 8.61 KB
/
PaneResizeContainer.js
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
import React, { useState, useRef, useEffect, useCallback } from 'react';
import cloneDeep from 'lodash/cloneDeep';
import isEqual from 'lodash/isEqual';
import insertByClientRect from './insertByClientRect';
import PaneResizeHandle from './PaneResizeHandle';
import PaneResizeCursor from './PaneResizeCursor';
import { ResizeProvider } from './ResizeContext';
import css from './PaneResize.css';
const PaneResizeContainer = ({ isRoot, children, onElementResize, parentElement, resizeContainerRef }) => {
const [dragging, setDragging] = useState(false);
const [cursorX, setCursorX] = useState(0);
const [handles, setHandles] = useState([]);
const [activeHandle, setActiveHandle] = useState(null);
const [update, setUpdate] = useState(0); // eslint-disable-line no-unused-vars
const [blocking, setBlocking] = useState(null);
let container = useRef();
if (resizeContainerRef) {
container = resizeContainerRef;
}
const handleData = useRef([]);
const containerRect = useRef();
const activeRef = useRef();
const observer = useRef();
const observerCallback = useRef(
(mutationList) => {
setBlocking(curBlocker => {
let blocker = curBlocker;
mutationList.forEach((m) => {
if (m.type === 'childList') {
// check against full container bounds
const blockingNode = Array.from(m.addedNodes).find((n) => {
const newBox = n?.getBoundingClientRect();
return (
newBox && containerRect.current &&
newBox.left <= containerRect.current.left &&
newBox.width >= containerRect.current.width
);
});
if (blockingNode) {
blocker = {
id: blockingNode.id,
class: blockingNode.className,
style: blockingNode.getAttribute('style')
};
}
if (typeof curBlocker === 'object') {
if (Array.from(m.removedNodes).find((n) => { // eslint-disable-line consistent-return
// we look for the removal of a node matching the blocker...
const candidate = {
id: n.id,
class: n.className,
style: n.getAttribute('style')
};
return isEqual(blocker, candidate);
})) {
blocker = null;
}
}
}
});
return blocker;
});
}
).current;
const onWindowResize = useCallback(() => {
containerRect.current = container.current?.getBoundingClientRect();
setUpdate(u => u + 1);
}, []);
// set up the mutation observer for overlapping element once we get a usable parentElement prop.
useEffect(() => {
if (isRoot) {
const observerOptions = {
childList: true,
};
if (parentElement) {
observer.current = new MutationObserver(observerCallback);
observer.current.observe(parentElement, observerOptions);
}
}
return () => {
if (observer.current) observer.current.disconnect();
};
}, [parentElement, isRoot, observerCallback]);
useEffect(() => {
if (isRoot) {
if (!dragging && activeHandle !== null) {
const { current: handleList } = handleData;
const resizedIndex = handleList.findIndex(h => h.id === activeHandle.id);
if (resizedIndex !== -1) {
handleList[resizedIndex].x = cursorX;
onElementResize({ positions: handleList, activeHandle, cursorX, containerRect: containerRect.current });
}
setActiveHandle(null);
}
}
}, [dragging, activeHandle, onElementResize, cursorX, isRoot]);
useEffect(() => { // eslint-disable-line consistent-return
if (isRoot) {
window.addEventListener('resize', onWindowResize);
return function cleanup() {
if (observer.current) observer.current.disconnect();
window.removeEventListener('resize', onWindowResize);
document.removeEventListener('mousemove', updateCursor); // eslint-disable-line no-use-before-define
document.removeEventListener('mouseup', handleMouseUp); // eslint-disable-line no-use-before-define
};
}
}, []); // eslint-disable-line
const updateCursor = useCallback((e) => {
if (containerRect.current) {
let nextX = e.clientX - containerRect.current.left;
const hdata = handleData.current;
const handleOffset = 20; // some margin on the bounds of a resize handle
const prevHandle = hdata[activeRef.current - 1] || { x: 0 };
const nextHandle = hdata[activeRef.current + 1] || { x: containerRect.current.width };
if (
nextX < prevHandle.x + handleOffset
) {
nextX = prevHandle.x + handleOffset;
} else if (
nextX > nextHandle.x - handleOffset
) {
nextX = nextHandle.x - handleOffset;
}
setCursorX(nextX);
}
}, []);
const handleMouseUp = useCallback(() => {
document.removeEventListener('mousemove', updateCursor);
document.removeEventListener('mouseup', handleMouseUp);
document.body.style.userSelect = 'auto';
document.body.style.webkitUserSelect = 'auto';
document.body.style.msUserSelect = 'auto';
document.body.style.msUserSelect = 'auto';
setDragging(false);
}, [updateCursor]);
const handleHandleMouseDown = useCallback((e, id) => {
setCursorX(e.clientX);
setDragging(true);
const handleIndex = handles.findIndex((h) => h.id === id);
const handleDataIndex = handleData.current.findIndex((h) => h.id === id);
setActiveHandle(handles[handleIndex]);
activeRef.current = handleDataIndex;
document.addEventListener('mousemove', updateCursor);
document.addEventListener('mouseup', handleMouseUp);
document.body.style.userSelect = 'none';
document.body.style.webkitUserSelect = 'none';
document.body.style.msUserSelect = 'none';
document.body.style.mozUserSelect = 'none';
}, [handleMouseUp, handles, updateCursor]);
const registerHandle = ({ id, elementId, getRef }) => {
setHandles(curHandles => {
if (curHandles.findIndex(h => h.id === id) === -1) {
return insertByClientRect(curHandles, { id, elementId, getRef });
}
return curHandles;
});
};
const removeHandle = (id) => {
setHandles(curHandles => {
const indexToRemove = curHandles.findIndex(h => h.id === id);
if (indexToRemove !== -1) {
const temp = cloneDeep(curHandles);
temp.splice(indexToRemove, 1);
return temp;
}
return curHandles;
});
};
const renderHandles = useCallback(() => {
handleData.current = [];
const handleElements = handles.map((h) => {
const rect = h.getRef()?.current?.getBoundingClientRect();
if (rect) {
if (rect.left === 0) {
handleData.current.push({ id: h.id, elementId: h.elementId, x: 0, y: 50 });
return false;
}
if (!containerRect.current) {
containerRect.current = container.current?.getBoundingClientRect();
}
const newLeft = rect.left - (containerRect.current ? containerRect.current.left : 0);
const top = rect.top - (containerRect.current ? containerRect.current.top : 0);
handleData.current.push({ id: h.id, elementId: h.elementId, x: newLeft, y: top });
return (
<PaneResizeHandle
key={h.id}
id={h.id}
xpos={newLeft - 3}
active={(activeHandle && h.id === activeHandle.id)}
onMouseDown={handleHandleMouseDown}
/>
);
}
return null;
});
return handleElements;
}, [activeHandle, handleHandleMouseDown, handles]);
// this is the equivalent of `forceUpdate` for functional components.
const updateHandle = () => {
if (containerRect.current) {
setUpdate(u => u + 1);
}
};
const suspend = () => {
setBlocking({ setByLayer: true });
};
const resume = () => {
setBlocking(null);
};
if (isRoot) {
return (
<>
<ResizeProvider value={{ registerHandle, updateHandle, removeHandle, suspend, resume }}>
{children}
</ResizeProvider>
<div
ref={container}
className={css.container}
>
{!blocking && renderHandles()}
<PaneResizeCursor visible={dragging} activeId={activeHandle} xpos={cursorX} />
</div>
</>
);
}
return children;
};
export default PaneResizeContainer;