@window-splitter/state 0.2.4 → 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,
@@ -110,7 +145,9 @@ export function initializePanelHandleData(item) {
110
145
  return {
111
146
  type: "handle",
112
147
  ...item,
113
- size: typeof item.size === "string" ? parseUnit(item.size) : item.size,
148
+ size: typeof item.size === "string"
149
+ ? parseUnit(item.size)
150
+ : item.size,
114
151
  };
115
152
  }
116
153
  /** Parse a `Unit` string or `clamp` value */
@@ -139,7 +176,7 @@ export function getGroupSize(context) {
139
176
  : context.size.height;
140
177
  }
141
178
  /** Get the size of a panel in pixels */
142
- export function getUnitPixelValue(context, unit) {
179
+ function getUnitPixelValue(context, unit) {
143
180
  const parsed = unit === "1fr" ? parseUnit(unit) : unit;
144
181
  return parsed.type === "pixel"
145
182
  ? parsed.value
@@ -282,6 +319,32 @@ function formatUnit(unit) {
282
319
  }
283
320
  return `${unit.value.toNumber()}%`;
284
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
+ }
285
348
  /** Build the grid template from the item values. */
286
349
  export function buildTemplate(context) {
287
350
  const staticWidth = getStaticWidth(context);
@@ -291,9 +354,6 @@ export function buildTemplate(context) {
291
354
  const min = formatUnit(item.min);
292
355
  if (item.currentValue.type === "pixel" &&
293
356
  item.currentValue.value.toNumber() !== -1) {
294
- if (item.currentValue.value.toNumber() < 0) {
295
- return "0px";
296
- }
297
357
  return formatUnit(item.currentValue);
298
358
  }
299
359
  else if (item.currentValue.type === "percent") {
@@ -541,14 +601,14 @@ function updateLayout(context, dragEvent) {
541
601
  panelBefore.currentValue = { type: "pixel", value: panelBeforeNewValue };
542
602
  panelAfter.currentValue = { type: "pixel", value: panelAfterNewValue };
543
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)));
544
- if (leftoverSpace.gt(0)) {
604
+ if (!leftoverSpace.eq(0)) {
545
605
  panelBefore.currentValue.value =
546
606
  panelBefore.currentValue.value.add(leftoverSpace);
547
607
  }
548
608
  return { items: newItems };
549
609
  }
550
610
  /** Converts the items to percentages */
551
- export function commitLayout(context) {
611
+ function commitLayout(context) {
552
612
  const newItems = [...context.items];
553
613
  // First set all the static width
554
614
  newItems.forEach((item, index) => {
@@ -577,7 +637,7 @@ export function commitLayout(context) {
577
637
  });
578
638
  return newItems;
579
639
  }
580
- export function dragHandlePayload({ delta, orientation, shiftKey = false, }) {
640
+ export function dragHandlePayload({ delta, orientation = "horizontal", shiftKey = false, }) {
581
641
  return {
582
642
  type: "move",
583
643
  pointerType: "keyboard",
@@ -689,7 +749,6 @@ export const groupMachine = createMachine({
689
749
  items: input.initialItems || [],
690
750
  orientation: input.orientation || "horizontal",
691
751
  dragOvershoot: new Big(0),
692
- autosaveId: input.autosaveId,
693
752
  groupId: input.groupId,
694
753
  }),
695
754
  states: {
@@ -698,7 +757,13 @@ export const groupMachine = createMachine({
698
757
  setActualItemsSize: { actions: ["recordActualItemSize"] },
699
758
  dragHandleStart: { target: "dragging" },
700
759
  setPanelPixelSize: {
701
- actions: ["prepare", "onSetPanelSize", "commit"],
760
+ actions: [
761
+ "prepare",
762
+ "onSetPanelSize",
763
+ "commit",
764
+ "onResize",
765
+ "onAutosave",
766
+ ],
702
767
  },
703
768
  collapsePanel: [
704
769
  {
@@ -719,7 +784,7 @@ export const groupMachine = createMachine({
719
784
  dragging: {
720
785
  entry: ["prepare"],
721
786
  on: {
722
- dragHandle: { actions: ["onDragHandle"] },
787
+ dragHandle: { actions: ["onDragHandle", "onResize"] },
723
788
  dragHandleEnd: { target: "idle" },
724
789
  collapsePanel: {
725
790
  guard: "shouldCollapseToggle",
@@ -743,24 +808,30 @@ export const groupMachine = createMachine({
743
808
  },
744
809
  },
745
810
  on: {
746
- applyDelta: { actions: ["onApplyDelta"] },
811
+ applyDelta: { actions: ["onApplyDelta", "onResize"] },
747
812
  },
748
813
  },
749
814
  },
750
815
  on: {
751
816
  registerPanel: { actions: ["assignPanelData"] },
752
817
  registerDynamicPanel: {
753
- actions: ["prepare", "onRegisterDynamicPanel", "commit", "onAutosave"],
818
+ actions: [
819
+ "prepare",
820
+ "onRegisterDynamicPanel",
821
+ "commit",
822
+ "onResize",
823
+ "onAutosave",
824
+ ],
754
825
  },
755
826
  unregisterPanel: {
756
- actions: ["prepare", "removeItem", "commit", "onAutosave"],
827
+ actions: ["prepare", "removeItem", "commit", "onResize", "onAutosave"],
757
828
  },
758
829
  registerPanelHandle: { actions: ["assignPanelHandleData"] },
759
830
  unregisterPanelHandle: {
760
- actions: ["prepare", "removeItem", "commit", "onAutosave"],
831
+ actions: ["prepare", "removeItem", "commit", "onResize", "onAutosave"],
761
832
  },
762
- setSize: { actions: ["updateSize"] },
763
- setOrientation: { actions: ["updateOrientation"] },
833
+ setSize: { actions: ["updateSize", "onResize"] },
834
+ setOrientation: { actions: ["updateOrientation", "onResize"] },
764
835
  },
765
836
  }, {
766
837
  guards: {
@@ -955,6 +1026,16 @@ export const groupMachine = createMachine({
955
1026
  delta,
956
1027
  }));
957
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
+ },
958
1039
  },
959
1040
  });
960
1041
  // #endregion