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