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
- const observerId = global_resizeObserver.add(parentParent, () => {
61
- // Jump directly by the mismatch amount (previously 1px with reflow on each step);
62
- // a few iterations are only for final adjustment, the upper bound prevents an infinite loop
63
- let lastRangeDelta = 0;
64
- for (let width = el.clientWidth, i = 0; i < 8; i++) {
65
- const rangeDelta = Math.floor(lastEl.getBoundingClientRect().right - parentParent.getBoundingClientRect().right);
66
- if (rangeDelta == 0)
67
- break;
68
- if (lastRangeDelta && rangeDelta * lastRangeDelta < 0)
69
- break;
70
- lastRangeDelta = rangeDelta;
71
- defaultWidth ??= el.clientWidth;
72
- width -= rangeDelta;
73
- if (width < 10)
74
- width = 10;
75
- if (width > defaultWidth)
76
- width = defaultWidth;
77
- el.style.width = width + "px";
78
- if (width == 10 || width == defaultWidth)
79
- break;
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
- resizeableElementMap.set(el, observerId);
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 id = resizeableElementMap.get(el);
87
- if (!id)
111
+ const state = resizeableElementMap.get(el);
112
+ if (!state)
88
113
  return;
89
- global_resizeObserver.delete(id);
114
+ global_resizeObserver.delete(state.observerId);
115
+ el.style.width = state.styleWidth;
90
116
  resizeableElementMap.delete(el);
91
117
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wenay-react2",
3
- "version": "1.0.25",
3
+ "version": "1.0.26",
4
4
  "description": "Common react",
5
5
  "strict": true,
6
6
  "main": "dist/index.js",