@window-splitter/state 0.4.2 → 0.5.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/src/index.ts CHANGED
@@ -182,6 +182,17 @@ interface RebindPanelCallbacksEvent {
182
182
  data: Pick<PanelData, "id" | "onCollapseChange" | "onResize">;
183
183
  }
184
184
 
185
+ interface UpdateConstraintsEvent {
186
+ /** Update the constraints of a panel */
187
+ type: "updateConstraints";
188
+ data:
189
+ | Pick<
190
+ PanelData,
191
+ "id" | "min" | "max" | "default" | "collapsedSize" | "isStaticAtRest"
192
+ >
193
+ | Pick<PanelHandleData, "id" | "size">;
194
+ }
195
+
185
196
  interface RegisterDynamicPanelEvent extends Omit<RegisterPanelEvent, "type"> {
186
197
  /** Register a new panel with the state machine */
187
198
  type: "registerDynamicPanel";
@@ -308,6 +319,8 @@ export interface GroupMachineContextValue {
308
319
  orientation: Orientation;
309
320
  /** How much the drag has overshot the handle */
310
321
  dragOvershoot: Big.Big;
322
+ /** The id of the handle that is currently being dragged */
323
+ activeDragHandleId?: string;
311
324
  groupId: string;
312
325
  /**
313
326
  * How to save the persisted state
@@ -336,7 +349,9 @@ export type GroupMachineEvent =
336
349
  | SetPanelPixelSizeEvent
337
350
  | ApplyDeltaEvent
338
351
  | SetActualItemsSizeEvent
339
- | RebindPanelCallbacksEvent;
352
+ | RebindPanelCallbacksEvent
353
+ | UpdateConstraintsEvent;
354
+
340
355
  type EventForType<T extends GroupMachineEvent["type"]> = Extract<
341
356
  GroupMachineEvent,
342
357
  { type: T }
@@ -500,6 +515,64 @@ export function initializePanel(
500
515
  >;
501
516
  }
502
517
 
518
+ function eq(a: ParsedUnit, b: ParsedUnit) {
519
+ return a.type === b.type && a.value.eq(b.value);
520
+ }
521
+
522
+ export function haveConstraintsChangedForPanel(
523
+ a: Omit<PanelData, "id" | "currentValue">,
524
+ b: Omit<PanelData, "id" | "currentValue">
525
+ ) {
526
+ if (!eq(a.min, b.min)) {
527
+ return true;
528
+ }
529
+
530
+ if (
531
+ (a.max === "1fr" && b.max !== "1fr") ||
532
+ (a.max !== "1fr" && b.max === "1fr") ||
533
+ (a.max !== "1fr" && b.max !== "1fr" && !eq(a.max, b.max))
534
+ ) {
535
+ return true;
536
+ }
537
+
538
+ if (
539
+ (a.default && !b.default) ||
540
+ (!a.default && b.default) ||
541
+ (a.default && b.default && !eq(a.default, b.default))
542
+ ) {
543
+ return true;
544
+ }
545
+
546
+ if (!eq(a.collapsedSize, b.collapsedSize)) {
547
+ return true;
548
+ }
549
+
550
+ if (a.isStaticAtRest !== b.isStaticAtRest) {
551
+ return true;
552
+ }
553
+
554
+ if (a.collapseAnimation !== b.collapseAnimation) {
555
+ return true;
556
+ }
557
+
558
+ if (a.collapsible !== b.collapsible) {
559
+ return true;
560
+ }
561
+
562
+ return false;
563
+ }
564
+
565
+ export function haveConstraintsChangedForPanelHandle(
566
+ a: Omit<PanelHandleData, "id">,
567
+ b: Omit<PanelHandleData, "id">
568
+ ) {
569
+ if (!eq(a.size, b.size)) {
570
+ return true;
571
+ }
572
+
573
+ return false;
574
+ }
575
+
503
576
  export function initializePanelHandleData(item: InitializePanelHandleData) {
504
577
  return {
505
578
  type: "handle" as const,
@@ -1587,7 +1660,10 @@ export const groupMachine = createMachine(
1587
1660
  idle: {
1588
1661
  entry: ["onAutosave"],
1589
1662
  on: {
1590
- dragHandleStart: { target: "dragging" },
1663
+ dragHandleStart: {
1664
+ target: "dragging",
1665
+ actions: ["onDragHandleStart"],
1666
+ },
1591
1667
  setPanelPixelSize: {
1592
1668
  actions: [
1593
1669
  "prepare",
@@ -1632,7 +1708,7 @@ export const groupMachine = createMachine(
1632
1708
  actions: "runCollapseToggle",
1633
1709
  },
1634
1710
  },
1635
- exit: ["commit"],
1711
+ exit: ["commit", "clearDragHandle"],
1636
1712
  },
1637
1713
  togglingCollapse: {
1638
1714
  entry: ["prepare", "onClearLastKnownSize"],
@@ -1652,7 +1728,19 @@ export const groupMachine = createMachine(
1652
1728
  on: {
1653
1729
  setActualItemsSize: { actions: ["recordActualItemSize", "onResize"] },
1654
1730
  registerPanel: { actions: ["assignPanelData"] },
1655
- rebindPanelCallbacks: { actions: ["rebindPanelCallbacks"] },
1731
+ rebindPanelCallbacks: {
1732
+ actions: ["rebindPanelCallbacks"],
1733
+ },
1734
+ updateConstraints: {
1735
+ actions: [
1736
+ "prepare",
1737
+ "updateConstraints",
1738
+ "onClearLastKnownSize",
1739
+ "commit",
1740
+ "onResize",
1741
+ "onAutosave",
1742
+ ],
1743
+ },
1656
1744
  registerDynamicPanel: {
1657
1745
  actions: [
1658
1746
  "prepare",
@@ -1739,6 +1827,15 @@ export const groupMachine = createMachine(
1739
1827
  animation: animationActor,
1740
1828
  },
1741
1829
  actions: {
1830
+ onDragHandleStart: assign({
1831
+ activeDragHandleId: ({ event }) => {
1832
+ isEvent(event, ["dragHandleStart"]);
1833
+ return event.handleId;
1834
+ },
1835
+ }),
1836
+ clearDragHandle: assign({
1837
+ activeDragHandleId: undefined,
1838
+ }),
1742
1839
  onAutosave: ({ context, self }) => {
1743
1840
  if (!context.autosaveStrategy || typeof window === "undefined") {
1744
1841
  return;
@@ -1746,6 +1843,7 @@ export const groupMachine = createMachine(
1746
1843
 
1747
1844
  const snapshot = self.getPersistedSnapshot() as GroupMachineSnapshot;
1748
1845
  snapshot.context.items = clearLastKnownSize(context.items);
1846
+ snapshot.context.activeDragHandleId = context.activeDragHandleId;
1749
1847
  snapshot.value = "idle";
1750
1848
  const data = JSON.stringify(snapshot);
1751
1849
 
@@ -1878,6 +1976,29 @@ export const groupMachine = createMachine(
1878
1976
  return context.items;
1879
1977
  },
1880
1978
  }),
1979
+ updateConstraints: assign({
1980
+ items: ({ context, event }) => {
1981
+ isEvent(event, ["updateConstraints"]);
1982
+
1983
+ for (const item of context.items) {
1984
+ if (isPanelData(item) && item.id === event.data.id) {
1985
+ const panel = event.data as PanelData;
1986
+ item.min = panel.min;
1987
+ item.max = panel.max;
1988
+ item.default = panel.default;
1989
+ item.collapsedSize = panel.collapsedSize;
1990
+ item.isStaticAtRest = panel.isStaticAtRest;
1991
+ item.collapseAnimation = panel.collapseAnimation;
1992
+ item.collapsible = panel.collapsible;
1993
+ } else if (isPanelHandle(item) && item.id === event.data.id) {
1994
+ const handle = event.data as PanelHandleData;
1995
+ item.size = handle.size;
1996
+ }
1997
+ }
1998
+
1999
+ return context.items;
2000
+ },
2001
+ }),
1881
2002
  onRegisterDynamicPanel: assign({
1882
2003
  items: ({ context, event }) => {
1883
2004
  isEvent(event, ["registerDynamicPanel"]);