react-resizable-panels 2.0.10 → 2.0.12

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.
@@ -1,28 +1,31 @@
1
1
  import { PanelConstraints } from "../Panel";
2
2
  import { assert } from "./assert";
3
3
  import { fuzzyCompareNumbers } from "./numbers/fuzzyCompareNumbers";
4
+ import { fuzzyLayoutsEqual } from "./numbers/fuzzyLayoutsEqual";
4
5
  import { fuzzyNumbersEqual } from "./numbers/fuzzyNumbersEqual";
5
6
  import { resizePanel } from "./resizePanel";
6
7
 
7
8
  // All units must be in percentages; pixel values should be pre-converted
8
9
  export function adjustLayoutByDelta({
9
10
  delta,
10
- layout: prevLayout,
11
+ initialLayout,
11
12
  panelConstraints: panelConstraintsArray,
12
13
  pivotIndices,
14
+ prevLayout,
13
15
  trigger,
14
16
  }: {
15
17
  delta: number;
16
- layout: number[];
18
+ initialLayout: number[];
17
19
  panelConstraints: PanelConstraints[];
18
20
  pivotIndices: number[];
21
+ prevLayout: number[];
19
22
  trigger: "imperative-api" | "keyboard" | "mouse-or-touch";
20
23
  }): number[] {
21
24
  if (fuzzyNumbersEqual(delta, 0)) {
22
- return prevLayout;
25
+ return initialLayout;
23
26
  }
24
27
 
25
- const nextLayout = [...prevLayout];
28
+ const nextLayout = [...initialLayout];
26
29
 
27
30
  const [firstPivotIndex, secondPivotIndex] = pivotIndices;
28
31
  assert(firstPivotIndex != null, "Invalid first pivot index");
@@ -30,12 +33,14 @@ export function adjustLayoutByDelta({
30
33
 
31
34
  let deltaApplied = 0;
32
35
 
33
- //const DEBUG = [];
34
- //DEBUG.push(`adjustLayoutByDelta() ${prevLayout.join(", ")}`);
35
- //DEBUG.push(` delta: ${delta}`);
36
- //DEBUG.push(` pivotIndices: ${pivotIndices.join(", ")}`);
37
- //DEBUG.push(` trigger: ${trigger}`);
38
- //DEBUG.push("");
36
+ // const DEBUG = [];
37
+ // DEBUG.push(`adjustLayoutByDelta()`);
38
+ // DEBUG.push(` initialLayout: ${initialLayout.join(", ")}`);
39
+ // DEBUG.push(` prevLayout: ${prevLayout.join(", ")}`);
40
+ // DEBUG.push(` delta: ${delta}`);
41
+ // DEBUG.push(` pivotIndices: ${pivotIndices.join(", ")}`);
42
+ // DEBUG.push(` trigger: ${trigger}`);
43
+ // DEBUG.push("");
39
44
 
40
45
  // A resizing panel affects the panels before or after it.
41
46
  //
@@ -64,10 +69,10 @@ export function adjustLayoutByDelta({
64
69
  minSize = 0,
65
70
  } = panelConstraints;
66
71
 
67
- //DEBUG.push(`edge case check 1: ${index}`);
68
- //DEBUG.push(` -> collapsible? ${constraints.collapsible}`);
72
+ // DEBUG.push(`edge case check 1: ${index}`);
73
+ // DEBUG.push(` -> collapsible? ${collapsible}`);
69
74
  if (collapsible) {
70
- const prevSize = prevLayout[index];
75
+ const prevSize = initialLayout[index];
71
76
  assert(
72
77
  prevSize != null,
73
78
  `Previous layout not found for panel index ${index}`
@@ -75,11 +80,11 @@ export function adjustLayoutByDelta({
75
80
 
76
81
  if (fuzzyNumbersEqual(prevSize, collapsedSize)) {
77
82
  const localDelta = minSize - prevSize;
78
- //DEBUG.push(` -> expand delta: ${localDelta}`);
83
+ // DEBUG.push(` -> expand delta: ${localDelta}`);
79
84
 
80
85
  if (fuzzyCompareNumbers(localDelta, Math.abs(delta)) > 0) {
81
86
  delta = delta < 0 ? 0 - localDelta : localDelta;
82
- //DEBUG.push(` -> delta: ${delta}`);
87
+ // DEBUG.push(` -> delta: ${delta}`);
83
88
  }
84
89
  }
85
90
  }
@@ -100,10 +105,10 @@ export function adjustLayoutByDelta({
100
105
  minSize = 0,
101
106
  } = panelConstraints;
102
107
 
103
- //DEBUG.push(`edge case check 2: ${index}`);
104
- //DEBUG.push(` -> collapsible? ${collapsible}`);
108
+ // DEBUG.push(`edge case check 2: ${index}`);
109
+ // DEBUG.push(` -> collapsible? ${collapsible}`);
105
110
  if (collapsible) {
106
- const prevSize = prevLayout[index];
111
+ const prevSize = initialLayout[index];
107
112
  assert(
108
113
  prevSize != null,
109
114
  `Previous layout not found for panel index ${index}`
@@ -111,17 +116,17 @@ export function adjustLayoutByDelta({
111
116
 
112
117
  if (fuzzyNumbersEqual(prevSize, minSize)) {
113
118
  const localDelta = prevSize - collapsedSize;
114
- //DEBUG.push(` -> expand delta: ${localDelta}`);
119
+ // DEBUG.push(` -> expand delta: ${localDelta}`);
115
120
 
116
121
  if (fuzzyCompareNumbers(localDelta, Math.abs(delta)) > 0) {
117
122
  delta = delta < 0 ? 0 - localDelta : localDelta;
118
- //DEBUG.push(` -> delta: ${delta}`);
123
+ // DEBUG.push(` -> delta: ${delta}`);
119
124
  }
120
125
  }
121
126
  }
122
127
  }
123
128
  }
124
- //DEBUG.push("");
129
+ // DEBUG.push("");
125
130
  }
126
131
 
127
132
  {
@@ -136,9 +141,9 @@ export function adjustLayoutByDelta({
136
141
  let index = delta < 0 ? secondPivotIndex : firstPivotIndex;
137
142
  let maxAvailableDelta = 0;
138
143
 
139
- //DEBUG.push("pre calc...");
144
+ // DEBUG.push("pre calc...");
140
145
  while (true) {
141
- const prevSize = prevLayout[index];
146
+ const prevSize = initialLayout[index];
142
147
  assert(
143
148
  prevSize != null,
144
149
  `Previous layout not found for panel index ${index}`
@@ -150,7 +155,7 @@ export function adjustLayoutByDelta({
150
155
  size: 100,
151
156
  });
152
157
  const delta = maxSafeSize - prevSize;
153
- //DEBUG.push(` ${index}: ${prevSize} -> ${maxSafeSize}`);
158
+ // DEBUG.push(` ${index}: ${prevSize} -> ${maxSafeSize}`);
154
159
 
155
160
  maxAvailableDelta += delta;
156
161
  index += increment;
@@ -160,11 +165,11 @@ export function adjustLayoutByDelta({
160
165
  }
161
166
  }
162
167
 
163
- //DEBUG.push(` -> max available delta: ${maxAvailableDelta}`);
168
+ // DEBUG.push(` -> max available delta: ${maxAvailableDelta}`);
164
169
  const minAbsDelta = Math.min(Math.abs(delta), Math.abs(maxAvailableDelta));
165
170
  delta = delta < 0 ? 0 - minAbsDelta : minAbsDelta;
166
- //DEBUG.push(` -> adjusted delta: ${delta}`);
167
- //DEBUG.push("");
171
+ // DEBUG.push(` -> adjusted delta: ${delta}`);
172
+ // DEBUG.push("");
168
173
  }
169
174
 
170
175
  {
@@ -175,7 +180,7 @@ export function adjustLayoutByDelta({
175
180
  while (index >= 0 && index < panelConstraintsArray.length) {
176
181
  const deltaRemaining = Math.abs(delta) - Math.abs(deltaApplied);
177
182
 
178
- const prevSize = prevLayout[index];
183
+ const prevSize = initialLayout[index];
179
184
  assert(
180
185
  prevSize != null,
181
186
  `Previous layout not found for panel index ${index}`
@@ -211,14 +216,16 @@ export function adjustLayoutByDelta({
211
216
  }
212
217
  }
213
218
  }
214
- //DEBUG.push(`after 1: ${nextLayout.join(", ")}`);
215
- //DEBUG.push(` deltaApplied: ${deltaApplied}`);
216
- //DEBUG.push("");
219
+ // DEBUG.push(`after 1: ${nextLayout.join(", ")}`);
220
+ // DEBUG.push(` deltaApplied: ${deltaApplied}`);
221
+ // DEBUG.push("");
217
222
 
218
223
  // If we were unable to resize any of the panels panels, return the previous state.
219
224
  // This will essentially bailout and ignore e.g. drags past a panel's boundaries
220
- if (fuzzyNumbersEqual(deltaApplied, 0)) {
221
- //console.log(DEBUG.join("\n"));
225
+ if (fuzzyLayoutsEqual(prevLayout, nextLayout)) {
226
+ // DEBUG.push(`bailout to previous layout: ${prevLayout.join(", ")}`);
227
+ // console.log(DEBUG.join("\n"));
228
+
222
229
  return prevLayout;
223
230
  }
224
231
 
@@ -226,7 +233,7 @@ export function adjustLayoutByDelta({
226
233
  // Now distribute the applied delta to the panels in the other direction
227
234
  const pivotIndex = delta < 0 ? secondPivotIndex : firstPivotIndex;
228
235
 
229
- const prevSize = prevLayout[pivotIndex];
236
+ const prevSize = initialLayout[pivotIndex];
230
237
  assert(
231
238
  prevSize != null,
232
239
  `Previous layout not found for panel index ${pivotIndex}`
@@ -280,17 +287,22 @@ export function adjustLayoutByDelta({
280
287
  }
281
288
  }
282
289
  }
283
- //DEBUG.push(`after 2: ${nextLayout.join(", ")}`);
284
- //DEBUG.push(` deltaApplied: ${deltaApplied}`);
285
- //DEBUG.push("");
290
+ // DEBUG.push(`after 2: ${nextLayout.join(", ")}`);
291
+ // DEBUG.push(` deltaApplied: ${deltaApplied}`);
292
+ // DEBUG.push("");
286
293
 
287
294
  const totalSize = nextLayout.reduce((total, size) => size + total, 0);
288
- //DEBUG.push(`total size: ${totalSize}`);
289
- //console.log(DEBUG.join("\n"));
295
+ // DEBUG.push(`total size: ${totalSize}`);
290
296
 
297
+ // If our new layout doesn't add up to 100%, that means the requested delta can't be applied
298
+ // In that case, fall back to our most recent valid layout
291
299
  if (!fuzzyNumbersEqual(totalSize, 100)) {
300
+ // DEBUG.push(`bailout to previous layout: ${prevLayout.join(", ")}`);
301
+ // console.log(DEBUG.join("\n"));
302
+
292
303
  return prevLayout;
293
304
  }
294
305
 
306
+ // console.log(DEBUG.join("\n"));
295
307
  return nextLayout;
296
308
  }
@@ -4,15 +4,15 @@ import { isMouseEvent, isTouchEvent } from ".";
4
4
  export function getResizeEventCoordinates(event: ResizeEvent) {
5
5
  if (isMouseEvent(event)) {
6
6
  return {
7
- x: event.pageX,
8
- y: event.pageY,
7
+ x: event.clientX,
8
+ y: event.clientY,
9
9
  };
10
10
  } else if (isTouchEvent(event)) {
11
11
  const touch = event.touches[0];
12
- if (touch && touch.pageX && touch.pageY) {
12
+ if (touch && touch.clientX && touch.clientY) {
13
13
  return {
14
- x: touch.pageX,
15
- y: touch.pageY,
14
+ x: touch.clientX,
15
+ y: touch.clientY,
16
16
  };
17
17
  }
18
18
  }
@@ -0,0 +1,22 @@
1
+ import { fuzzyNumbersEqual } from "./fuzzyNumbersEqual";
2
+
3
+ export function fuzzyLayoutsEqual(
4
+ actual: number[],
5
+ expected: number[],
6
+ fractionDigits?: number
7
+ ): boolean {
8
+ if (actual.length !== expected.length) {
9
+ return false;
10
+ }
11
+
12
+ for (let index = 0; index < actual.length; index++) {
13
+ const actualSize = actual[index] as number;
14
+ const expectedSize = expected[index] as number;
15
+
16
+ if (!fuzzyNumbersEqual(actualSize, expectedSize, fractionDigits)) {
17
+ return false;
18
+ }
19
+ }
20
+
21
+ return true;
22
+ }