@window-splitter/state 0.5.8 → 0.6.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.
@@ -1,32 +1,8 @@
1
1
  "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
- Object.defineProperty(o, "default", { enumerable: true, value: v });
15
- }) : function(o, v) {
16
- o["default"] = v;
17
- });
18
- var __importStar = (this && this.__importStar) || function (mod) {
19
- if (mod && mod.__esModule) return mod;
20
- var result = {};
21
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
- __setModuleDefault(result, mod);
23
- return result;
24
- };
25
2
  var __importDefault = (this && this.__importDefault) || function (mod) {
26
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
27
4
  };
28
5
  Object.defineProperty(exports, "__esModule", { value: true });
29
- exports.groupMachine = void 0;
30
6
  exports.makePercentUnit = makePercentUnit;
31
7
  exports.makePixelUnit = makePixelUnit;
32
8
  exports.getCursor = getCursor;
@@ -49,9 +25,8 @@ exports.getPanelPercentageSize = getPanelPercentageSize;
49
25
  exports.buildTemplate = buildTemplate;
50
26
  exports.prepareItems = prepareItems;
51
27
  exports.dragHandlePayload = dragHandlePayload;
52
- const Cookies = __importStar(require("tiny-cookie"));
28
+ exports.groupMachine = groupMachine;
53
29
  const rafz_1 = require("@react-spring/rafz");
54
- const xstate_1 = require("xstate");
55
30
  const tiny_invariant_1 = __importDefault(require("tiny-invariant"));
56
31
  const big_js_1 = __importDefault(require("big.js"));
57
32
  // #region Constants
@@ -122,9 +97,15 @@ function getCursor(context) {
122
97
  }
123
98
  }
124
99
  function prepareSnapshot(snapshot) {
125
- const snapshotContext = snapshot.context;
126
- snapshotContext.dragOvershoot = new big_js_1.default(snapshotContext.dragOvershoot);
127
- for (const item of snapshotContext.items) {
100
+ // convert from old format
101
+ if ("context" in snapshot) {
102
+ snapshot = snapshot.context;
103
+ }
104
+ if (!("items" in snapshot)) {
105
+ return;
106
+ }
107
+ snapshot.dragOvershoot = new big_js_1.default(snapshot.dragOvershoot || 0);
108
+ for (const item of snapshot.items) {
128
109
  if (isPanelData(item)) {
129
110
  item.currentValue.value = new big_js_1.default(item.currentValue.value);
130
111
  item.collapsedSize.value = new big_js_1.default(item.collapsedSize.value);
@@ -888,6 +869,9 @@ function handleOverflow(context) {
888
869
  function clearLastKnownSize(items) {
889
870
  return items.map((i) => ({ ...i, lastKnownSize: undefined }));
890
871
  }
872
+ function setCookie(name, jsonData) {
873
+ document.cookie = `${name}=${encodeURIComponent(`${jsonData}`)};path=/;max-age=31536000`;
874
+ }
891
875
  function getDeltaForEvent(context, event) {
892
876
  const panel = getPanelWithId(context, event.panelId);
893
877
  if (event.type === "expandPanel") {
@@ -896,193 +880,168 @@ function getDeltaForEvent(context, event) {
896
880
  const collapsedSize = getUnitPixelValue(context, panel.collapsedSize);
897
881
  return panel.currentValue.value.minus(collapsedSize);
898
882
  }
899
- const animationActor = (0, xstate_1.fromPromise)(({ input: { send, context, event } }) => new Promise((resolve) => {
883
+ function animationActor(context, event, send, abortController) {
900
884
  const panel = getPanelWithId(context, event.panelId);
901
885
  const handle = getHandleForPanelId(context, event.panelId);
902
886
  let direction = new big_js_1.default(handle.direction);
903
887
  const fullDelta = getDeltaForEvent(context, event);
904
- if (event.type === "collapsePanel") {
905
- panel.sizeBeforeCollapse = panel.currentValue.value.toNumber();
906
- direction = direction.mul(new big_js_1.default(-1));
907
- }
908
- const fps = 60;
909
- const { duration, ease } = getCollapseAnimation(panel);
910
- const totalFrames = Math.ceil(panel.collapseAnimation ? duration / (1000 / fps) : 1);
911
- let frame = 0;
912
- let appliedDelta = new big_js_1.default(0);
913
- function renderFrame() {
914
- const progress = ++frame / totalFrames;
915
- const e = new big_js_1.default(panel.collapseAnimation ? ease(progress) : 1);
916
- const delta = e.mul(fullDelta).sub(appliedDelta).mul(direction);
917
- send({
918
- type: "applyDelta",
919
- handleId: handle.item.id,
920
- delta: delta.toNumber(),
888
+ return new Promise((resolve, reject) => {
889
+ abortController.signal.addEventListener("abort", () => {
890
+ reject(new Error("Operation was canceled"));
921
891
  });
922
- appliedDelta = appliedDelta.add(delta
923
- .abs()
924
- .mul((delta.gt(0) && direction.lt(0)) ||
925
- (delta.lt(0) && direction.gt(0))
926
- ? -1
927
- : 1));
928
- if (e.eq(1)) {
929
- const action = event.type === "expandPanel" ? "expand" : "collapse";
930
- resolve({ panelId: panel.id, action });
931
- return false;
892
+ if (event.type === "collapsePanel") {
893
+ panel.sizeBeforeCollapse = panel.currentValue.value.toNumber();
894
+ direction = direction.mul(new big_js_1.default(-1));
932
895
  }
933
- return true;
896
+ const fps = 60;
897
+ const { duration, ease } = getCollapseAnimation(panel);
898
+ const totalFrames = Math.ceil(panel.collapseAnimation ? duration / (1000 / fps) : 1);
899
+ let frame = 0;
900
+ let appliedDelta = new big_js_1.default(0);
901
+ function renderFrame() {
902
+ const progress = ++frame / totalFrames;
903
+ const e = new big_js_1.default(panel.collapseAnimation ? ease(progress) : 1);
904
+ const delta = e.mul(fullDelta).sub(appliedDelta).mul(direction);
905
+ send({
906
+ type: "applyDelta",
907
+ handleId: handle.item.id,
908
+ delta: delta.toNumber(),
909
+ });
910
+ appliedDelta = appliedDelta.add(delta
911
+ .abs()
912
+ .mul((delta.gt(0) && direction.lt(0)) || (delta.lt(0) && direction.gt(0))
913
+ ? -1
914
+ : 1));
915
+ if (e.eq(1)) {
916
+ const action = event.type === "expandPanel" ? "expand" : "collapse";
917
+ resolve({ panelId: panel.id, action });
918
+ return false;
919
+ }
920
+ return true;
921
+ }
922
+ (0, rafz_1.raf)(renderFrame);
923
+ });
924
+ }
925
+ function assign(target, source) {
926
+ for (const key in source) {
927
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
928
+ target[key] = source[key];
934
929
  }
935
- (0, rafz_1.raf)(renderFrame);
936
- }));
937
- exports.groupMachine = (0, xstate_1.createMachine)({
938
- initial: "idle",
939
- types: {
940
- context: {},
941
- events: {},
942
- input: {},
943
- },
944
- context: ({ input }) => ({
930
+ }
931
+ let groupId = 0;
932
+ function groupMachine(input, onUpdate) {
933
+ const abortController = new AbortController();
934
+ const state = {
935
+ current: "idle",
936
+ };
937
+ let locked = false;
938
+ const context = {
945
939
  size: { width: 0, height: 0 },
946
- items: input.initialItems || [],
940
+ items: input.items || [],
947
941
  orientation: input.orientation || "horizontal",
948
942
  dragOvershoot: new big_js_1.default(0),
949
- groupId: input.groupId,
943
+ groupId: input.groupId || `group-${groupId++}`,
950
944
  autosaveStrategy: input.autosaveStrategy,
951
- }),
952
- states: {
953
- idle: {
954
- entry: ["onAutosave"],
955
- on: {
956
- dragHandleStart: {
957
- target: "dragging",
958
- actions: ["onDragHandleStart"],
959
- },
960
- setPanelPixelSize: {
961
- actions: [
962
- "prepare",
963
- "onClearLastKnownSize",
964
- "onSetPanelSize",
965
- "commit",
966
- "onResize",
967
- "onAutosave",
968
- ],
969
- },
970
- collapsePanel: [
971
- {
972
- actions: "notifyCollapseToggle",
973
- guard: "shouldNotifyCollapseToggle",
974
- },
975
- { target: "togglingCollapse" },
976
- ],
977
- expandPanel: [
978
- // This will match if we can't expand and the expansion won't happen
979
- { guard: "cannotExpandPanel" },
980
- {
981
- actions: "notifyCollapseToggle",
982
- guard: "shouldNotifyCollapseToggle",
983
- },
984
- { target: "togglingCollapse" },
985
- ],
986
- },
945
+ };
946
+ const actions = {
947
+ prepare: () => {
948
+ context.items = prepareItems(context);
987
949
  },
988
- dragging: {
989
- entry: ["prepare"],
990
- on: {
991
- dragHandle: {
992
- actions: ["onClearLastKnownSize", "onDragHandle", "onResize"],
993
- },
994
- dragHandleEnd: { target: "idle" },
995
- collapsePanel: {
996
- guard: "shouldCollapseToggle",
997
- actions: "runCollapseToggle",
998
- },
999
- expandPanel: {
1000
- guard: "shouldCollapseToggle",
1001
- actions: "runCollapseToggle",
1002
- },
1003
- },
1004
- exit: ["commit", "clearDragHandle"],
950
+ clearLastKnownSize: () => {
951
+ context.items = clearLastKnownSize(context.items);
1005
952
  },
1006
- togglingCollapse: {
1007
- entry: ["prepare", "onClearLastKnownSize"],
1008
- invoke: {
1009
- src: "animation",
1010
- input: (i) => ({ ...i, send: i.self.send }),
1011
- onDone: {
1012
- target: "idle",
1013
- actions: ["onToggleCollapseComplete", "commit"],
1014
- },
1015
- },
1016
- on: {
1017
- applyDelta: { actions: ["onApplyDelta", "onResize"] },
1018
- },
953
+ commit: () => {
954
+ context.dragOvershoot = new big_js_1.default(0);
955
+ context.items = commitLayout(context);
1019
956
  },
1020
- },
1021
- on: {
1022
- setActualItemsSize: { actions: ["recordActualItemSize", "onResize"] },
1023
- registerPanel: { actions: ["assignPanelData"] },
1024
- rebindPanelCallbacks: {
1025
- actions: ["rebindPanelCallbacks"],
957
+ onResize: () => {
958
+ for (const item of context.items) {
959
+ if (isPanelData(item)) {
960
+ const pixel = item.lastKnownSize
961
+ ? new big_js_1.default(context.orientation === "horizontal"
962
+ ? item.lastKnownSize.width
963
+ : item.lastKnownSize.height)
964
+ : clampUnit(context, item, getUnitPixelValue(context, item.currentValue));
965
+ const groupSize = getGroupSize(context);
966
+ item.onResize?.current?.({
967
+ pixel: pixel.toNumber(),
968
+ percentage: groupSize > 0 ? pixel.div(getGroupSize(context)).toNumber() : -1,
969
+ });
970
+ }
971
+ }
1026
972
  },
1027
- updateConstraints: {
1028
- actions: [
1029
- "prepare",
1030
- "updateConstraints",
1031
- "onClearLastKnownSize",
1032
- "commit",
1033
- "onResize",
1034
- "onAutosave",
1035
- ],
973
+ onAutosave: () => {
974
+ if (!context.autosaveStrategy || typeof window === "undefined") {
975
+ return;
976
+ }
977
+ const snapshot = { ...context };
978
+ snapshot.items = clearLastKnownSize(context.items);
979
+ snapshot.activeDragHandleId = context.activeDragHandleId;
980
+ const data = JSON.stringify(snapshot);
981
+ if (context.autosaveStrategy === "localStorage") {
982
+ localStorage.setItem(context.groupId, data);
983
+ }
984
+ else {
985
+ setCookie(context.groupId, data);
986
+ }
1036
987
  },
1037
- registerDynamicPanel: {
1038
- actions: [
1039
- "prepare",
1040
- "onRegisterDynamicPanel",
1041
- "onClearLastKnownSize",
1042
- "commit",
1043
- "onResize",
1044
- "onAutosave",
1045
- ],
988
+ removeItem: (id) => {
989
+ const itemIndex = context.items.findIndex((item) => item.id === id);
990
+ const item = context.items[itemIndex];
991
+ if (!item) {
992
+ return;
993
+ }
994
+ const newItems = context.items.filter((i) => i.id !== id);
995
+ const removedSize = isPanelData(item)
996
+ ? item.currentValue.value
997
+ : item.size.value;
998
+ applyDeltaInBothDirections(context, newItems, itemIndex, removedSize);
999
+ context.items = newItems;
1046
1000
  },
1047
- unregisterPanel: {
1048
- actions: [
1049
- "prepare",
1050
- "removeItem",
1051
- "onClearLastKnownSize",
1052
- "commit",
1053
- "onResize",
1054
- "onAutosave",
1055
- ],
1001
+ notifyCollapseToggle: (event) => {
1002
+ isEvent(event, ["collapsePanel", "expandPanel"]);
1003
+ const panel = getPanelWithId(context, event.panelId);
1004
+ panel.onCollapseChange?.current?.(!panel.collapsed);
1056
1005
  },
1057
- registerPanelHandle: { actions: ["assignPanelHandleData"] },
1058
- unregisterPanelHandle: {
1059
- actions: [
1060
- "prepare",
1061
- "removeItem",
1062
- "onClearLastKnownSize",
1063
- "commit",
1064
- "onResize",
1065
- "onAutosave",
1066
- ],
1006
+ runCollapseToggle: (event) => {
1007
+ isEvent(event, ["collapsePanel", "expandPanel"]);
1008
+ const handle = getHandleForPanelId(context, event.panelId);
1009
+ // When collapsing a panel it will be in the opposite direction
1010
+ // that handle assumes
1011
+ const delta = event.type === "collapsePanel"
1012
+ ? handle.direction * -1
1013
+ : handle.direction;
1014
+ const newContext = updateLayout(context, {
1015
+ handleId: handle.item.id,
1016
+ type: "dragHandle",
1017
+ controlled: event.controlled,
1018
+ value: dragHandlePayload({ delta, orientation: context.orientation }),
1019
+ });
1020
+ assign(context, newContext);
1067
1021
  },
1068
- setSize: { actions: ["updateSize", "onResize"] },
1069
- setOrientation: {
1070
- actions: ["updateOrientation", "onClearLastKnownSize", "onResize"],
1022
+ onAnimationEnd: (output) => {
1023
+ (0, tiny_invariant_1.default)(output, "Expected output from animation actor");
1024
+ const panel = getPanelWithId(context, output.panelId);
1025
+ panel.collapsed = output.action === "collapse";
1026
+ if (panel.collapsed) {
1027
+ panel.currentValue = panel.collapsedSize;
1028
+ }
1029
+ actions.commit();
1071
1030
  },
1072
- },
1073
- }, {
1074
- guards: {
1075
- shouldNotifyCollapseToggle: ({ context, event }) => {
1031
+ };
1032
+ actions.onAutosave();
1033
+ const guards = {
1034
+ shouldNotifyCollapseToggle: (event) => {
1076
1035
  isEvent(event, ["collapsePanel", "expandPanel"]);
1077
1036
  const panel = getPanelWithId(context, event.panelId);
1078
1037
  return !event.controlled && panel.collapseIsControlled === true;
1079
1038
  },
1080
- shouldCollapseToggle: ({ context, event }) => {
1039
+ shouldCollapseToggle: (event) => {
1081
1040
  isEvent(event, ["collapsePanel", "expandPanel"]);
1082
1041
  const panel = getPanelWithId(context, event.panelId);
1083
1042
  return panel.collapseIsControlled === true;
1084
1043
  },
1085
- cannotExpandPanel: ({ context, event }) => {
1044
+ cannotExpandPanel: (event) => {
1086
1045
  isEvent(event, ["expandPanel"]);
1087
1046
  const delta = getDeltaForEvent(context, event);
1088
1047
  const handle = getHandleForPanelId(context, event.panelId);
@@ -1105,166 +1064,82 @@ exports.groupMachine = (0, xstate_1.createMachine)({
1105
1064
  !updatedPanel.currentValue.value.eq(updatedPanel.collapsedSize.value);
1106
1065
  return totalSize.gt(getGroupSize(context)) || !didExpand;
1107
1066
  },
1108
- },
1109
- actors: {
1110
- animation: animationActor,
1111
- },
1112
- actions: {
1113
- onDragHandleStart: (0, xstate_1.assign)({
1114
- activeDragHandleId: ({ event }) => {
1115
- isEvent(event, ["dragHandleStart"]);
1116
- return event.handleId;
1117
- },
1118
- }),
1119
- clearDragHandle: (0, xstate_1.assign)({
1120
- activeDragHandleId: undefined,
1121
- }),
1122
- onAutosave: ({ context, self }) => {
1123
- if (!context.autosaveStrategy || typeof window === "undefined") {
1124
- return;
1125
- }
1126
- const snapshot = self.getPersistedSnapshot();
1127
- snapshot.context.items = clearLastKnownSize(context.items);
1128
- snapshot.context.activeDragHandleId = context.activeDragHandleId;
1129
- snapshot.value = "idle";
1130
- const data = JSON.stringify(snapshot);
1131
- if (context.autosaveStrategy === "localStorage") {
1132
- localStorage.setItem(context.groupId, data);
1133
- }
1134
- else {
1135
- Cookies.set(context.groupId, data, {
1136
- path: "/",
1137
- "max-age": 31536000,
1138
- });
1139
- }
1140
- },
1141
- notifyCollapseToggle: ({ context, event }) => {
1142
- isEvent(event, ["collapsePanel", "expandPanel"]);
1143
- const panel = getPanelWithId(context, event.panelId);
1144
- panel.onCollapseChange?.current?.(!panel.collapsed);
1145
- },
1146
- runCollapseToggle: (0, xstate_1.enqueueActions)(({ context, event, enqueue }) => {
1147
- isEvent(event, ["collapsePanel", "expandPanel"]);
1148
- const handle = getHandleForPanelId(context, event.panelId);
1149
- // When collapsing a panel it will be in the opposite direction
1150
- // that handle assumes
1151
- const delta = event.type === "collapsePanel"
1152
- ? handle.direction * -1
1153
- : handle.direction;
1154
- const newContext = updateLayout(context, {
1155
- handleId: handle.item.id,
1156
- type: "dragHandle",
1157
- controlled: event.controlled,
1158
- value: dragHandlePayload({ delta, orientation: context.orientation }),
1159
- });
1160
- enqueue.assign(newContext);
1161
- }),
1162
- onToggleCollapseComplete: (0, xstate_1.assign)({
1163
- items: ({ context, event: e }) => {
1164
- const { output } = e;
1165
- (0, tiny_invariant_1.default)(output, "Expected output from animation actor");
1166
- const panel = getPanelWithId(context, output.panelId);
1167
- panel.collapsed = output.action === "collapse";
1168
- if (panel.collapsed) {
1169
- panel.currentValue = panel.collapsedSize;
1170
- }
1171
- return context.items;
1172
- },
1173
- }),
1174
- updateSize: (0, xstate_1.assign)({
1175
- size: ({ event }) => {
1176
- isEvent(event, ["setSize"]);
1177
- return event.size;
1178
- },
1179
- }),
1180
- recordActualItemSize: (0, xstate_1.assign)({
1181
- items: ({ context, event }) => {
1182
- isEvent(event, ["setActualItemsSize"]);
1183
- const withLastKnownSize = context.items.map((i) => {
1184
- if (!isPanelData(i))
1185
- return i;
1186
- const lastKnownSize = event.childrenSizes[i.id] || i.lastKnownSize;
1187
- return { ...i, lastKnownSize };
1188
- });
1189
- let totalSize = 0;
1190
- for (const item of withLastKnownSize) {
1191
- if (isPanelData(item)) {
1192
- const size = item.lastKnownSize?.[context.orientation === "horizontal" ? "width" : "height"];
1193
- // If any size is 0 don't handle overflow
1194
- if (!size) {
1195
- return withLastKnownSize;
1196
- }
1197
- totalSize += size;
1198
- }
1199
- else {
1200
- totalSize += item.size.value.toNumber();
1201
- }
1202
- }
1203
- if (totalSize > getGroupSize(context)) {
1204
- return handleOverflow({
1205
- ...context,
1206
- items: prepareItems({ ...context, items: withLastKnownSize }),
1207
- }).items;
1208
- }
1209
- return withLastKnownSize;
1210
- },
1211
- }),
1212
- updateOrientation: (0, xstate_1.assign)({
1213
- orientation: ({ event }) => {
1214
- isEvent(event, ["setOrientation"]);
1215
- return event.orientation;
1216
- },
1217
- }),
1218
- onClearLastKnownSize: (0, xstate_1.assign)({
1219
- items: ({ context }) => clearLastKnownSize(context.items),
1220
- }),
1221
- assignPanelData: (0, xstate_1.assign)({
1222
- items: ({ context, event }) => {
1223
- isEvent(event, ["registerPanel"]);
1224
- return addDeDuplicatedItems(context.items, {
1067
+ };
1068
+ function transition(to) {
1069
+ // exit
1070
+ switch (state.current) {
1071
+ case "dragging":
1072
+ actions.commit();
1073
+ context.activeDragHandleId = undefined;
1074
+ break;
1075
+ }
1076
+ // enter
1077
+ switch (to) {
1078
+ case "idle":
1079
+ actions.onAutosave();
1080
+ break;
1081
+ case "dragging":
1082
+ actions.prepare();
1083
+ break;
1084
+ case "togglingCollapse":
1085
+ actions.prepare();
1086
+ actions.clearLastKnownSize();
1087
+ break;
1088
+ }
1089
+ state.current = to;
1090
+ }
1091
+ function send(event) {
1092
+ switch (event.type) {
1093
+ case "lockGroup":
1094
+ locked = true;
1095
+ break;
1096
+ case "unlockGroup":
1097
+ locked = false;
1098
+ break;
1099
+ }
1100
+ if (locked) {
1101
+ return;
1102
+ }
1103
+ switch (event.type) {
1104
+ case "registerPanel":
1105
+ context.items = addDeDuplicatedItems(context.items, {
1225
1106
  type: "panel",
1226
1107
  currentValue: makePixelUnit(-1),
1227
1108
  ...event.data,
1228
1109
  });
1229
- },
1230
- }),
1231
- rebindPanelCallbacks: (0, xstate_1.assign)({
1232
- items: ({ context, event }) => {
1233
- isEvent(event, ["rebindPanelCallbacks"]);
1234
- for (const item of context.items) {
1235
- if (isPanelData(item) && item.id === event.data.id) {
1236
- item.onCollapseChange = event.data.onCollapseChange;
1237
- item.onResize = event.data.onResize;
1238
- }
1239
- }
1240
- return context.items;
1241
- },
1242
- }),
1243
- updateConstraints: (0, xstate_1.assign)({
1244
- items: ({ context, event }) => {
1245
- isEvent(event, ["updateConstraints"]);
1246
- for (const item of context.items) {
1247
- if (isPanelData(item) && item.id === event.data.id) {
1248
- const panel = event.data;
1249
- item.min = panel.min;
1250
- item.max = panel.max;
1251
- item.default = panel.default;
1252
- item.collapsedSize = panel.collapsedSize;
1253
- item.isStaticAtRest = panel.isStaticAtRest;
1254
- item.collapseAnimation = panel.collapseAnimation;
1255
- item.collapsible = panel.collapsible;
1256
- }
1257
- else if (isPanelHandle(item) && item.id === event.data.id) {
1258
- const handle = event.data;
1259
- item.size = handle.size;
1260
- }
1261
- }
1262
- return context.items;
1263
- },
1264
- }),
1265
- onRegisterDynamicPanel: (0, xstate_1.assign)({
1266
- items: ({ context, event }) => {
1267
- isEvent(event, ["registerDynamicPanel"]);
1110
+ break;
1111
+ case "unregisterPanel":
1112
+ actions.prepare();
1113
+ actions.removeItem(event.id);
1114
+ actions.clearLastKnownSize();
1115
+ actions.commit();
1116
+ actions.onResize();
1117
+ actions.onAutosave();
1118
+ break;
1119
+ case "registerPanelHandle": {
1120
+ const unit = typeof event.data.size === "string"
1121
+ ? parseUnit(event.data.size)
1122
+ : event.data.size;
1123
+ context.items = addDeDuplicatedItems(context.items, {
1124
+ type: "handle",
1125
+ ...event.data,
1126
+ size: {
1127
+ type: "pixel",
1128
+ value: new big_js_1.default(unit.value),
1129
+ },
1130
+ });
1131
+ break;
1132
+ }
1133
+ case "unregisterPanelHandle":
1134
+ actions.prepare();
1135
+ actions.removeItem(event.id);
1136
+ actions.clearLastKnownSize();
1137
+ actions.commit();
1138
+ actions.onResize();
1139
+ actions.onAutosave();
1140
+ break;
1141
+ case "registerDynamicPanel": {
1142
+ actions.prepare();
1268
1143
  let currentValue = makePixelUnit(0);
1269
1144
  if (event.data.collapsible &&
1270
1145
  event.data.collapsed &&
@@ -1293,100 +1168,196 @@ exports.groupMachine = (0, xstate_1.createMachine)({
1293
1168
  }, new big_js_1.default(0))
1294
1169
  .minus(getGroupSize(context));
1295
1170
  applyDeltaInBothDirections(newContext, newItems, itemIndex, currentValue.value.add(overflowDueToHandles).neg());
1296
- return newItems;
1297
- },
1298
- }),
1299
- assignPanelHandleData: (0, xstate_1.assign)({
1300
- items: ({ context, event }) => {
1301
- isEvent(event, ["registerPanelHandle"]);
1302
- const unit = typeof event.data.size === "string"
1303
- ? parseUnit(event.data.size)
1304
- : event.data.size;
1305
- return addDeDuplicatedItems(context.items, {
1306
- type: "handle",
1307
- ...event.data,
1308
- size: {
1309
- type: "pixel",
1310
- value: new big_js_1.default(unit.value),
1311
- },
1171
+ context.items = newItems;
1172
+ actions.clearLastKnownSize();
1173
+ actions.commit();
1174
+ actions.onResize();
1175
+ actions.onAutosave();
1176
+ break;
1177
+ }
1178
+ case "rebindPanelCallbacks":
1179
+ for (const item of context.items) {
1180
+ if (isPanelData(item) && item.id === event.data.id) {
1181
+ item.onCollapseChange = event.data.onCollapseChange;
1182
+ item.onResize = event.data.onResize;
1183
+ }
1184
+ }
1185
+ break;
1186
+ case "updateConstraints":
1187
+ actions.prepare();
1188
+ for (const item of context.items) {
1189
+ if (isPanelData(item) && item.id === event.data.id) {
1190
+ const panel = event.data;
1191
+ item.min = panel.min;
1192
+ item.max = panel.max;
1193
+ item.default = panel.default;
1194
+ item.collapsedSize = panel.collapsedSize;
1195
+ item.isStaticAtRest = panel.isStaticAtRest;
1196
+ item.collapseAnimation = panel.collapseAnimation;
1197
+ item.collapsible = panel.collapsible;
1198
+ }
1199
+ else if (isPanelHandle(item) && item.id === event.data.id) {
1200
+ const handle = event.data;
1201
+ item.size = handle.size;
1202
+ }
1203
+ }
1204
+ actions.clearLastKnownSize();
1205
+ actions.commit();
1206
+ actions.onResize();
1207
+ actions.onAutosave();
1208
+ break;
1209
+ case "setActualItemsSize": {
1210
+ const withLastKnownSize = context.items.map((i) => {
1211
+ if (!isPanelData(i))
1212
+ return i;
1213
+ const lastKnownSize = event.childrenSizes[i.id] || i.lastKnownSize;
1214
+ return { ...i, lastKnownSize };
1312
1215
  });
1313
- },
1314
- }),
1315
- removeItem: (0, xstate_1.assign)({
1316
- items: ({ context, event }) => {
1317
- isEvent(event, ["unregisterPanel", "unregisterPanelHandle"]);
1318
- const itemIndex = context.items.findIndex((item) => item.id === event.id);
1319
- const item = context.items[itemIndex];
1320
- if (!item) {
1321
- return context.items;
1216
+ let totalSize = 0;
1217
+ let useLastKnownSize = false;
1218
+ for (const item of withLastKnownSize) {
1219
+ if (isPanelData(item)) {
1220
+ const size = item.lastKnownSize?.[context.orientation === "horizontal" ? "width" : "height"];
1221
+ // If any size is 0 don't handle overflow
1222
+ if (!size) {
1223
+ context.items = withLastKnownSize;
1224
+ useLastKnownSize = true;
1225
+ break;
1226
+ }
1227
+ totalSize += size;
1228
+ }
1229
+ else {
1230
+ totalSize += item.size.value.toNumber();
1231
+ }
1322
1232
  }
1323
- const newItems = context.items.filter((i) => i.id !== event.id);
1324
- const removedSize = isPanelData(item)
1325
- ? item.currentValue.value
1326
- : item.size.value;
1327
- applyDeltaInBothDirections(context, newItems, itemIndex, removedSize);
1328
- return newItems;
1329
- },
1330
- }),
1331
- prepare: (0, xstate_1.assign)({
1332
- items: ({ context }) => prepareItems(context),
1333
- }),
1334
- onDragHandle: (0, xstate_1.enqueueActions)(({ context, event, enqueue }) => {
1335
- isEvent(event, ["dragHandle"]);
1336
- enqueue.assign(updateLayout(context, event));
1337
- }),
1338
- commit: (0, xstate_1.assign)({
1339
- dragOvershoot: new big_js_1.default(0),
1340
- items: ({ context }) => commitLayout(context),
1341
- }),
1342
- onApplyDelta: (0, xstate_1.assign)(({ context, event }) => {
1343
- isEvent(event, ["applyDelta"]);
1344
- return updateLayout(context, {
1345
- handleId: event.handleId,
1346
- type: "collapsePanel",
1347
- disregardCollapseBuffer: true,
1348
- value: dragHandlePayload({
1349
- delta: event.delta,
1350
- orientation: context.orientation,
1351
- }),
1352
- });
1353
- }),
1354
- onSetPanelSize: (0, xstate_1.enqueueActions)(({ context, event, enqueue }) => {
1355
- isEvent(event, ["setPanelPixelSize"]);
1356
- const panel = getPanelWithId(context, event.panelId);
1357
- const handle = getHandleForPanelId(context, event.panelId);
1358
- const current = panel.currentValue.value;
1359
- const newSize = clampUnit(context, panel, getUnitPixelValue(context, parseUnit(event.size)));
1360
- const isBigger = newSize > current;
1361
- const delta = isBigger
1362
- ? newSize.minus(current)
1363
- : current.minus(newSize);
1364
- enqueue.assign(iterativelyUpdateLayout({
1365
- context,
1366
- direction: (handle.direction * (isBigger ? 1 : -1)),
1367
- handleId: handle.item.id,
1368
- delta,
1369
- }));
1370
- }),
1371
- onResize: ({ context }) => {
1372
- for (const item of context.items) {
1373
- if (isPanelData(item)) {
1374
- const pixel = item.lastKnownSize
1375
- ? new big_js_1.default(context.orientation === "horizontal"
1376
- ? item.lastKnownSize.width
1377
- : item.lastKnownSize.height)
1378
- : clampUnit(context, item, getUnitPixelValue(context, item.currentValue));
1379
- const groupSize = getGroupSize(context);
1380
- item.onResize?.current?.({
1381
- pixel: pixel.toNumber(),
1382
- percentage: groupSize > 0
1383
- ? pixel.div(getGroupSize(context)).toNumber()
1384
- : -1,
1385
- });
1233
+ if (useLastKnownSize) {
1234
+ context.items = withLastKnownSize;
1235
+ }
1236
+ else if (totalSize > getGroupSize(context)) {
1237
+ context.items = handleOverflow({
1238
+ ...context,
1239
+ items: prepareItems({ ...context, items: withLastKnownSize }),
1240
+ }).items;
1241
+ }
1242
+ else {
1243
+ context.items = withLastKnownSize;
1386
1244
  }
1245
+ actions.onResize();
1246
+ break;
1387
1247
  }
1388
- },
1389
- },
1390
- });
1248
+ case "setSize": {
1249
+ context.size = event.size;
1250
+ actions.onResize();
1251
+ break;
1252
+ }
1253
+ case "setOrientation":
1254
+ context.orientation = event.orientation;
1255
+ actions.clearLastKnownSize();
1256
+ actions.onResize();
1257
+ break;
1258
+ default:
1259
+ break;
1260
+ }
1261
+ if (state.current === "idle") {
1262
+ switch (event.type) {
1263
+ case "dragHandleStart":
1264
+ transition("dragging");
1265
+ context.activeDragHandleId = event.handleId;
1266
+ break;
1267
+ case "setPanelPixelSize": {
1268
+ actions.prepare();
1269
+ actions.clearLastKnownSize();
1270
+ const panel = getPanelWithId(context, event.panelId);
1271
+ const handle = getHandleForPanelId(context, event.panelId);
1272
+ const current = panel.currentValue.value;
1273
+ const newSize = clampUnit(context, panel, getUnitPixelValue(context, parseUnit(event.size)));
1274
+ const isBigger = newSize > current;
1275
+ const delta = isBigger
1276
+ ? newSize.minus(current)
1277
+ : current.minus(newSize);
1278
+ Object.assign(context, iterativelyUpdateLayout({
1279
+ context,
1280
+ direction: (handle.direction * (isBigger ? 1 : -1)),
1281
+ handleId: handle.item.id,
1282
+ delta,
1283
+ }));
1284
+ actions.commit();
1285
+ actions.onResize();
1286
+ actions.onAutosave();
1287
+ break;
1288
+ }
1289
+ case "collapsePanel":
1290
+ if (guards.shouldNotifyCollapseToggle(event)) {
1291
+ actions.notifyCollapseToggle(event);
1292
+ }
1293
+ else {
1294
+ transition("togglingCollapse");
1295
+ abortController.abort();
1296
+ animationActor(context, event, send, abortController).then((output) => {
1297
+ actions.onAnimationEnd(output);
1298
+ transition("idle");
1299
+ });
1300
+ }
1301
+ break;
1302
+ case "expandPanel":
1303
+ if (guards.cannotExpandPanel(event)) {
1304
+ break;
1305
+ }
1306
+ else {
1307
+ if (guards.shouldNotifyCollapseToggle(event)) {
1308
+ actions.notifyCollapseToggle(event);
1309
+ }
1310
+ else {
1311
+ transition("togglingCollapse");
1312
+ abortController.abort();
1313
+ animationActor(context, event, send, abortController).then((output) => {
1314
+ actions.onAnimationEnd(output);
1315
+ transition("idle");
1316
+ });
1317
+ }
1318
+ }
1319
+ break;
1320
+ default:
1321
+ break;
1322
+ }
1323
+ }
1324
+ else if (state.current === "dragging") {
1325
+ switch (event.type) {
1326
+ case "dragHandle":
1327
+ actions.clearLastKnownSize();
1328
+ assign(context, updateLayout(context, event));
1329
+ actions.onResize();
1330
+ break;
1331
+ case "dragHandleEnd":
1332
+ transition("idle");
1333
+ break;
1334
+ case "expandPanel":
1335
+ case "collapsePanel":
1336
+ if (guards.shouldCollapseToggle(event)) {
1337
+ actions.runCollapseToggle(event);
1338
+ }
1339
+ break;
1340
+ }
1341
+ }
1342
+ else if (state.current === "togglingCollapse") {
1343
+ switch (event.type) {
1344
+ case "applyDelta":
1345
+ assign(context, updateLayout(context, {
1346
+ handleId: event.handleId,
1347
+ type: "collapsePanel",
1348
+ disregardCollapseBuffer: true,
1349
+ value: dragHandlePayload({
1350
+ delta: event.delta,
1351
+ orientation: context.orientation,
1352
+ }),
1353
+ }));
1354
+ actions.onResize();
1355
+ break;
1356
+ }
1357
+ }
1358
+ onUpdate?.(context);
1359
+ }
1360
+ return [context, send, state];
1361
+ }
1391
1362
  // #endregion
1392
1363
  //# sourceMappingURL=index.js.map