wenay-react2 1.0.25 → 1.0.26
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -41,6 +41,12 @@ export class CResizeObserver {
|
|
|
41
41
|
}
|
|
42
42
|
const global_resizeObserver = new CResizeObserver();
|
|
43
43
|
const resizeableElementMap = new WeakMap();
|
|
44
|
+
function getWidth(el) {
|
|
45
|
+
return Math.ceil(el.clientWidth || el.getBoundingClientRect().width);
|
|
46
|
+
}
|
|
47
|
+
function applyWidth(el, state, width) {
|
|
48
|
+
el.style.width = width >= state.defaultWidth ? state.styleWidth : width + "px";
|
|
49
|
+
}
|
|
44
50
|
// Set automatic element resizing based on the parent element size
|
|
45
51
|
//
|
|
46
52
|
export function setResizeableElement(el) {
|
|
@@ -53,39 +59,59 @@ export function setResizeableElement(el) {
|
|
|
53
59
|
const lastEl = parent.lastElementChild;
|
|
54
60
|
if (!lastEl)
|
|
55
61
|
return;
|
|
56
|
-
let defaultWidth;
|
|
57
62
|
const existing = resizeableElementMap.get(el);
|
|
58
|
-
if (existing)
|
|
59
|
-
global_resizeObserver.delete(existing);
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
if (
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
63
|
+
if (existing) {
|
|
64
|
+
global_resizeObserver.delete(existing.observerId);
|
|
65
|
+
el.style.width = existing.styleWidth;
|
|
66
|
+
}
|
|
67
|
+
const state = {
|
|
68
|
+
observerId: {},
|
|
69
|
+
defaultWidth: existing?.defaultWidth ?? getWidth(el),
|
|
70
|
+
styleWidth: existing?.styleWidth ?? el.style.width,
|
|
71
|
+
resizing: false,
|
|
72
|
+
};
|
|
73
|
+
const resize = () => {
|
|
74
|
+
if (state.resizing)
|
|
75
|
+
return;
|
|
76
|
+
state.resizing = true;
|
|
77
|
+
try {
|
|
78
|
+
applyWidth(el, state, state.defaultWidth);
|
|
79
|
+
let rangeDelta = Math.floor(lastEl.getBoundingClientRect().right - parentParent.getBoundingClientRect().right);
|
|
80
|
+
if (rangeDelta <= 0)
|
|
81
|
+
return;
|
|
82
|
+
const parentWidth = parentParent.getBoundingClientRect().width;
|
|
83
|
+
const probeWidth = Math.max(10, Math.floor(state.defaultWidth * 0.8));
|
|
84
|
+
if (state.defaultWidth - probeWidth >= 2) {
|
|
85
|
+
el.style.width = probeWidth + "px";
|
|
86
|
+
const probedParentWidth = parentParent.getBoundingClientRect().width;
|
|
87
|
+
applyWidth(el, state, state.defaultWidth);
|
|
88
|
+
if (Math.abs(parentWidth - probedParentWidth) > 0.5)
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
for (let width = state.defaultWidth, i = 0; i < 8; i++) {
|
|
92
|
+
width = Math.max(10, Math.min(state.defaultWidth, width - rangeDelta));
|
|
93
|
+
applyWidth(el, state, width);
|
|
94
|
+
if (width == 10 || width == state.defaultWidth)
|
|
95
|
+
break;
|
|
96
|
+
rangeDelta = Math.floor(lastEl.getBoundingClientRect().right - parentParent.getBoundingClientRect().right);
|
|
97
|
+
if (rangeDelta <= 0)
|
|
98
|
+
break;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
finally {
|
|
102
|
+
state.resizing = false;
|
|
80
103
|
}
|
|
81
|
-
}
|
|
82
|
-
|
|
104
|
+
};
|
|
105
|
+
state.observerId = global_resizeObserver.add(parentParent, resize);
|
|
106
|
+
resizeableElementMap.set(el, state);
|
|
107
|
+
resize();
|
|
83
108
|
return el;
|
|
84
109
|
}
|
|
85
110
|
export function removeResizeableElement(el) {
|
|
86
|
-
const
|
|
87
|
-
if (!
|
|
111
|
+
const state = resizeableElementMap.get(el);
|
|
112
|
+
if (!state)
|
|
88
113
|
return;
|
|
89
|
-
global_resizeObserver.delete(
|
|
114
|
+
global_resizeObserver.delete(state.observerId);
|
|
115
|
+
el.style.width = state.styleWidth;
|
|
90
116
|
resizeableElementMap.delete(el);
|
|
91
117
|
}
|