@window-splitter/state 0.2.5 → 0.3.0

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") {
@@ -543,14 +601,14 @@ function updateLayout(context, dragEvent) {
543
601
  panelBefore.currentValue = { type: "pixel", value: panelBeforeNewValue };
544
602
  panelAfter.currentValue = { type: "pixel", value: panelAfterNewValue };
545
603
  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)) {
604
+ if (!leftoverSpace.eq(0)) {
547
605
  panelBefore.currentValue.value =
548
606
  panelBefore.currentValue.value.add(leftoverSpace);
549
607
  }
550
608
  return { items: newItems };
551
609
  }
552
610
  /** Converts the items to percentages */
553
- export function commitLayout(context) {
611
+ function commitLayout(context) {
554
612
  const newItems = [...context.items];
555
613
  // First set all the static width
556
614
  newItems.forEach((item, index) => {
@@ -699,7 +757,13 @@ export const groupMachine = createMachine({
699
757
  setActualItemsSize: { actions: ["recordActualItemSize"] },
700
758
  dragHandleStart: { target: "dragging" },
701
759
  setPanelPixelSize: {
702
- actions: ["prepare", "onSetPanelSize", "commit"],
760
+ actions: [
761
+ "prepare",
762
+ "onSetPanelSize",
763
+ "commit",
764
+ "onResize",
765
+ "onAutosave",
766
+ ],
703
767
  },
704
768
  collapsePanel: [
705
769
  {
@@ -720,7 +784,7 @@ export const groupMachine = createMachine({
720
784
  dragging: {
721
785
  entry: ["prepare"],
722
786
  on: {
723
- dragHandle: { actions: ["onDragHandle"] },
787
+ dragHandle: { actions: ["onDragHandle", "onResize"] },
724
788
  dragHandleEnd: { target: "idle" },
725
789
  collapsePanel: {
726
790
  guard: "shouldCollapseToggle",
@@ -744,24 +808,30 @@ export const groupMachine = createMachine({
744
808
  },
745
809
  },
746
810
  on: {
747
- applyDelta: { actions: ["onApplyDelta"] },
811
+ applyDelta: { actions: ["onApplyDelta", "onResize"] },
748
812
  },
749
813
  },
750
814
  },
751
815
  on: {
752
816
  registerPanel: { actions: ["assignPanelData"] },
753
817
  registerDynamicPanel: {
754
- actions: ["prepare", "onRegisterDynamicPanel", "commit", "onAutosave"],
818
+ actions: [
819
+ "prepare",
820
+ "onRegisterDynamicPanel",
821
+ "commit",
822
+ "onResize",
823
+ "onAutosave",
824
+ ],
755
825
  },
756
826
  unregisterPanel: {
757
- actions: ["prepare", "removeItem", "commit", "onAutosave"],
827
+ actions: ["prepare", "removeItem", "commit", "onResize", "onAutosave"],
758
828
  },
759
829
  registerPanelHandle: { actions: ["assignPanelHandleData"] },
760
830
  unregisterPanelHandle: {
761
- actions: ["prepare", "removeItem", "commit", "onAutosave"],
831
+ actions: ["prepare", "removeItem", "commit", "onResize", "onAutosave"],
762
832
  },
763
- setSize: { actions: ["updateSize"] },
764
- setOrientation: { actions: ["updateOrientation"] },
833
+ setSize: { actions: ["updateSize", "onResize"] },
834
+ setOrientation: { actions: ["updateOrientation", "onResize"] },
765
835
  },
766
836
  }, {
767
837
  guards: {
@@ -956,6 +1026,16 @@ export const groupMachine = createMachine({
956
1026
  delta,
957
1027
  }));
958
1028
  }),
1029
+ onResize: ({ context }) => {
1030
+ for (const item of context.items) {
1031
+ if (isPanelData(item)) {
1032
+ item.onResize?.current?.({
1033
+ pixel: getUnitPixelValue(context, item.currentValue).toNumber(),
1034
+ percentage: getUnitPercentageValue(getGroupSize(context), item.currentValue),
1035
+ });
1036
+ }
1037
+ }
1038
+ },
959
1039
  },
960
1040
  });
961
1041
  // #endregion