@window-splitter/state 0.2.5 → 0.3.1

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.
package/dist/esm/index.js CHANGED
@@ -18,8 +18,8 @@ function getCollapseAnimation(panel) {
18
18
  if (typeof panel.collapseAnimation === "string") {
19
19
  easeFn = collapseAnimations[panel.collapseAnimation];
20
20
  }
21
- else if ("duration" in panel.collapseAnimation) {
22
- duration = panel.collapseAnimation.duration ?? duration;
21
+ else {
22
+ duration = panel.collapseAnimation.duration;
23
23
  easeFn =
24
24
  typeof panel.collapseAnimation.easing === "function"
25
25
  ? panel.collapseAnimation.easing
@@ -45,6 +45,30 @@ const collapseAnimations = {
45
45
  };
46
46
  // #endregion
47
47
  // #region Helpers
48
+ export function getCursor(context) {
49
+ if (context.orientation === "horizontal") {
50
+ if (context.dragOvershoot.gt(0)) {
51
+ return "w-resize";
52
+ }
53
+ else if (context.dragOvershoot.lt(0)) {
54
+ return "e-resize";
55
+ }
56
+ else {
57
+ return "ew-resize";
58
+ }
59
+ }
60
+ else {
61
+ if (context.dragOvershoot.gt(0)) {
62
+ return "n-resize";
63
+ }
64
+ else if (context.dragOvershoot.lt(0)) {
65
+ return "s-resize";
66
+ }
67
+ else {
68
+ return "ns-resize";
69
+ }
70
+ }
71
+ }
48
72
  export function prepareSnapshot(snapshot) {
49
73
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
50
74
  const snapshotContext = snapshot
@@ -53,12 +77,8 @@ export function prepareSnapshot(snapshot) {
53
77
  for (const item of snapshotContext.items) {
54
78
  if (isPanelData(item)) {
55
79
  item.currentValue.value = new Big(item.currentValue.value);
56
- if (item.collapsedSize) {
57
- item.collapsedSize.value = new Big(item.collapsedSize.value);
58
- }
59
- if (item.min) {
60
- item.min.value = new Big(item.min.value);
61
- }
80
+ item.collapsedSize.value = new Big(item.collapsedSize.value);
81
+ item.min.value = new Big(item.min.value);
62
82
  if (item.max && item.max !== "1fr") {
63
83
  item.max.value = new Big(item.max.value);
64
84
  }
@@ -88,6 +108,20 @@ export function isPanelHandle(value) {
88
108
  value.type === "handle");
89
109
  }
90
110
  export function initializePanel(item) {
111
+ const onResize = () => {
112
+ let lastCall = null;
113
+ // Memo-ize so we only call the callback once per size
114
+ return ((size) => {
115
+ if (!lastCall ||
116
+ (lastCall.pixel === size.pixel &&
117
+ lastCall.percentage === size.percentage)) {
118
+ lastCall = size;
119
+ return;
120
+ }
121
+ lastCall = size;
122
+ item.onResize?.current?.(size);
123
+ });
124
+ };
91
125
  const data = {
92
126
  type: "panel",
93
127
  min: parseUnit(item.min || "0px"),
@@ -98,6 +132,7 @@ export function initializePanel(item) {
98
132
  collapsible: item.collapsible,
99
133
  collapsedSize: parseUnit(item.collapsedSize ?? "0px"),
100
134
  onCollapseChange: item.onCollapseChange,
135
+ onResize: { current: onResize() },
101
136
  collapseIsControlled: typeof item.collapsed !== "undefined",
102
137
  sizeBeforeCollapse: undefined,
103
138
  id: item.id,
@@ -141,7 +176,7 @@ export function getGroupSize(context) {
141
176
  : context.size.height;
142
177
  }
143
178
  /** Get the size of a panel in pixels */
144
- export function getUnitPixelValue(context, unit) {
179
+ function getUnitPixelValue(context, unit) {
145
180
  const parsed = unit === "1fr" ? parseUnit(unit) : unit;
146
181
  return parsed.type === "pixel"
147
182
  ? parsed.value
@@ -284,6 +319,32 @@ function formatUnit(unit) {
284
319
  }
285
320
  return `${unit.value.toNumber()}%`;
286
321
  }
322
+ export function getPanelGroupPixelSizes(context) {
323
+ return prepareItems(context).map((i) => isPanelData(i)
324
+ ? i.currentValue.value.toNumber()
325
+ : getUnitPixelValue(context, i.size).toNumber());
326
+ }
327
+ export function getPanelPixelSize(context, panelId) {
328
+ const p = getPanelWithId({ ...context, items: prepareItems(context) }, panelId);
329
+ return p.currentValue.value.toNumber();
330
+ }
331
+ export function getPanelGroupPercentageSizes(context) {
332
+ const clamped = commitLayout({
333
+ ...context,
334
+ items: prepareItems(context),
335
+ });
336
+ return clamped.map((i) => {
337
+ if (isPanelHandle(i)) {
338
+ return getUnitPercentageValue(getGroupSize(context), i.size);
339
+ }
340
+ return getUnitPercentageValue(getGroupSize(context), i.currentValue);
341
+ });
342
+ }
343
+ export function getPanelPercentageSize(context, panelId) {
344
+ const items = prepareItems(context);
345
+ const p = getPanelWithId({ ...context, items }, panelId);
346
+ return getUnitPercentageValue(getGroupSize(context), p.currentValue);
347
+ }
287
348
  /** Build the grid template from the item values. */
288
349
  export function buildTemplate(context) {
289
350
  const staticWidth = getStaticWidth(context);
@@ -293,9 +354,6 @@ export function buildTemplate(context) {
293
354
  const min = formatUnit(item.min);
294
355
  if (item.currentValue.type === "pixel" &&
295
356
  item.currentValue.value.toNumber() !== -1) {
296
- if (item.currentValue.value.toNumber() < 0) {
297
- return "0px";
298
- }
299
357
  return formatUnit(item.currentValue);
300
358
  }
301
359
  else if (item.currentValue.type === "percent") {
@@ -383,19 +441,24 @@ function createUnrestrainedPanel(_, data) {
383
441
  */
384
442
  /** Converts the items to pixels */
385
443
  export function prepareItems(context) {
386
- const newItems = [...context.items];
387
444
  const staticWidth = getStaticWidth(context);
388
- for (const item of newItems) {
445
+ const newItems = [];
446
+ for (const item of context.items) {
389
447
  if (!item || !isPanelData(item)) {
448
+ newItems.push(item);
390
449
  continue;
391
450
  }
392
451
  if (item.currentValue.type === "pixel") {
452
+ newItems.push(item);
393
453
  continue;
394
454
  }
395
455
  const pixel = new Big(getGroupSize(context))
396
456
  .minus(staticWidth)
397
457
  .mul(item.currentValue.value);
398
- item.currentValue = makePixelUnit(pixel.toNumber());
458
+ newItems.push({
459
+ ...item,
460
+ currentValue: makePixelUnit(pixel.toNumber()),
461
+ });
399
462
  }
400
463
  return newItems;
401
464
  }
@@ -543,14 +606,14 @@ function updateLayout(context, dragEvent) {
543
606
  panelBefore.currentValue = { type: "pixel", value: panelBeforeNewValue };
544
607
  panelAfter.currentValue = { type: "pixel", value: panelAfterNewValue };
545
608
  const leftoverSpace = new Big(getGroupSize(context)).minus(newItems.reduce((acc, b) => acc.add(isPanelData(b) ? b.currentValue.value : b.size.value), new Big(0)));
546
- if (leftoverSpace.gt(0)) {
609
+ if (!leftoverSpace.eq(0)) {
547
610
  panelBefore.currentValue.value =
548
611
  panelBefore.currentValue.value.add(leftoverSpace);
549
612
  }
550
613
  return { items: newItems };
551
614
  }
552
615
  /** Converts the items to percentages */
553
- export function commitLayout(context) {
616
+ function commitLayout(context) {
554
617
  const newItems = [...context.items];
555
618
  // First set all the static width
556
619
  newItems.forEach((item, index) => {
@@ -699,7 +762,13 @@ export const groupMachine = createMachine({
699
762
  setActualItemsSize: { actions: ["recordActualItemSize"] },
700
763
  dragHandleStart: { target: "dragging" },
701
764
  setPanelPixelSize: {
702
- actions: ["prepare", "onSetPanelSize", "commit"],
765
+ actions: [
766
+ "prepare",
767
+ "onSetPanelSize",
768
+ "commit",
769
+ "onResize",
770
+ "onAutosave",
771
+ ],
703
772
  },
704
773
  collapsePanel: [
705
774
  {
@@ -720,7 +789,7 @@ export const groupMachine = createMachine({
720
789
  dragging: {
721
790
  entry: ["prepare"],
722
791
  on: {
723
- dragHandle: { actions: ["onDragHandle"] },
792
+ dragHandle: { actions: ["onDragHandle", "onResize"] },
724
793
  dragHandleEnd: { target: "idle" },
725
794
  collapsePanel: {
726
795
  guard: "shouldCollapseToggle",
@@ -744,24 +813,30 @@ export const groupMachine = createMachine({
744
813
  },
745
814
  },
746
815
  on: {
747
- applyDelta: { actions: ["onApplyDelta"] },
816
+ applyDelta: { actions: ["onApplyDelta", "onResize"] },
748
817
  },
749
818
  },
750
819
  },
751
820
  on: {
752
821
  registerPanel: { actions: ["assignPanelData"] },
753
822
  registerDynamicPanel: {
754
- actions: ["prepare", "onRegisterDynamicPanel", "commit", "onAutosave"],
823
+ actions: [
824
+ "prepare",
825
+ "onRegisterDynamicPanel",
826
+ "commit",
827
+ "onResize",
828
+ "onAutosave",
829
+ ],
755
830
  },
756
831
  unregisterPanel: {
757
- actions: ["prepare", "removeItem", "commit", "onAutosave"],
832
+ actions: ["prepare", "removeItem", "commit", "onResize", "onAutosave"],
758
833
  },
759
834
  registerPanelHandle: { actions: ["assignPanelHandleData"] },
760
835
  unregisterPanelHandle: {
761
- actions: ["prepare", "removeItem", "commit", "onAutosave"],
836
+ actions: ["prepare", "removeItem", "commit", "onResize", "onAutosave"],
762
837
  },
763
- setSize: { actions: ["updateSize"] },
764
- setOrientation: { actions: ["updateOrientation"] },
838
+ setSize: { actions: ["updateSize", "onResize"] },
839
+ setOrientation: { actions: ["updateOrientation", "onResize"] },
765
840
  },
766
841
  }, {
767
842
  guards: {
@@ -956,6 +1031,16 @@ export const groupMachine = createMachine({
956
1031
  delta,
957
1032
  }));
958
1033
  }),
1034
+ onResize: ({ context }) => {
1035
+ for (const item of context.items) {
1036
+ if (isPanelData(item)) {
1037
+ item.onResize?.current?.({
1038
+ pixel: getUnitPixelValue(context, item.currentValue).toNumber(),
1039
+ percentage: getUnitPercentageValue(getGroupSize(context), item.currentValue),
1040
+ });
1041
+ }
1042
+ }
1043
+ },
959
1044
  },
960
1045
  });
961
1046
  // #endregion