Skip to content

Commit 679e85a

Browse files
authored
Fix sidebar width not persisting when un-collapsing (#267)
* Do not persist collapsed width to db * remove console.logs * one more * one more * remove setTemp from getWidth and add to the startResize call
1 parent da69a0f commit 679e85a

2 files changed

Lines changed: 33 additions & 35 deletions

File tree

src/app/common/common.tsx

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1304,21 +1304,23 @@ class ResizableSidebar extends React.Component<ResizableSidebarProps> {
13041304

13051305
document.body.style.cursor = "col-resize";
13061306
mobx.action(() => {
1307-
GlobalModel.mainSidebarModel.isDragging.set(true);
1307+
const sbm = GlobalModel.mainSidebarModel;
1308+
sbm.setTempWidthAndTempCollapsed(this.resizeStartWidth, sbm.getCollapsed());
1309+
sbm.isDragging.set(true);
13081310
})();
13091311
}
13101312

13111313
@boundMethod
13121314
onMouseMove(event: MouseEvent) {
13131315
event.preventDefault();
13141316

1315-
let { parentRef, enableSnap, position } = this.props;
1316-
let parentRect = parentRef.current?.getBoundingClientRect();
1317-
let mainSidebarModel = GlobalModel.mainSidebarModel;
1317+
const { parentRef, enableSnap, position } = this.props;
1318+
const parentRect = parentRef.current?.getBoundingClientRect();
1319+
const mainSidebarModel = GlobalModel.mainSidebarModel;
13181320

13191321
if (!mainSidebarModel.isDragging.get() || !parentRect) return;
13201322

1321-
let delta, newWidth;
1323+
let delta: number, newWidth: number;
13221324

13231325
if (position === "right") {
13241326
delta = parentRect.right - event.clientX - this.startX;
@@ -1329,10 +1331,10 @@ class ResizableSidebar extends React.Component<ResizableSidebarProps> {
13291331
newWidth = this.resizeStartWidth + delta;
13301332

13311333
if (enableSnap) {
1332-
let minWidth = MagicLayout.MainSidebarMinWidth;
1333-
let snapPoint = minWidth + MagicLayout.MainSidebarSnapThreshold;
1334-
let dragResistance = MagicLayout.MainSidebarDragResistance;
1335-
let dragDirection;
1334+
const minWidth = MagicLayout.MainSidebarMinWidth;
1335+
const snapPoint = minWidth + MagicLayout.MainSidebarSnapThreshold;
1336+
const dragResistance = MagicLayout.MainSidebarDragResistance;
1337+
let dragDirection: string;
13361338

13371339
if (delta - this.prevDelta > 0) {
13381340
dragDirection = "+";
@@ -1387,26 +1389,19 @@ class ResizableSidebar extends React.Component<ResizableSidebarProps> {
13871389

13881390
@boundMethod
13891391
toggleCollapsed() {
1390-
let mainSidebarModel = GlobalModel.mainSidebarModel;
1391-
1392-
let tempCollapsed = mainSidebarModel.getCollapsed();
1393-
let width = MagicLayout.MainSidebarDefaultWidth;
1394-
let newWidth;
1395-
if (tempCollapsed) {
1396-
newWidth = width;
1397-
} else {
1398-
newWidth = MagicLayout.MainSidebarMinWidth;
1399-
}
1392+
const mainSidebarModel = GlobalModel.mainSidebarModel;
14001393

1401-
mainSidebarModel.setTempWidthAndTempCollapsed(newWidth, !tempCollapsed);
1402-
GlobalCommandRunner.clientSetSidebar(newWidth, !tempCollapsed);
1394+
const tempCollapsed = mainSidebarModel.getCollapsed();
1395+
const width = mainSidebarModel.getWidth(true);
1396+
mainSidebarModel.setTempWidthAndTempCollapsed(width, !tempCollapsed);
1397+
GlobalCommandRunner.clientSetSidebar(width, !tempCollapsed);
14031398
}
14041399

14051400
render() {
1406-
let { className, children } = this.props;
1407-
let mainSidebarModel = GlobalModel.mainSidebarModel;
1408-
let width = mainSidebarModel.getWidth();
1409-
let isCollapsed = mainSidebarModel.getCollapsed();
1401+
const { className, children } = this.props;
1402+
const mainSidebarModel = GlobalModel.mainSidebarModel;
1403+
const width = mainSidebarModel.getWidth();
1404+
const isCollapsed = mainSidebarModel.getCollapsed();
14101405

14111406
return (
14121407
<div className={cn("sidebar", className, { collapsed: isCollapsed })} style={{ width }}>

src/model/model.ts

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2638,7 +2638,12 @@ class MainSidebarModel {
26382638
})();
26392639
}
26402640

2641-
getWidth(): number {
2641+
/**
2642+
* Gets the intended width for the sidebar. If the sidebar is being dragged, returns the tempWidth. If the sidebar is collapsed, returns the default width.
2643+
* @param ignoreCollapse If true, returns the persisted width even if the sidebar is collapsed.
2644+
* @returns The intended width for the sidebar or the default width if the sidebar is collapsed. Can be overridden using ignoreCollapse.
2645+
*/
2646+
getWidth(ignoreCollapse: boolean = false): number {
26422647
const clientData = GlobalModel.clientData.get();
26432648
let width = clientData?.clientopts?.mainsidebar?.width ?? MagicLayout.MainSidebarDefaultWidth;
26442649
if (this.isDragging.get()) {
@@ -2651,23 +2656,21 @@ class MainSidebarModel {
26512656
return this.tempWidth.get();
26522657
}
26532658
// Set by CLI and collapsed
2654-
if (this.getCollapsed() && width != MagicLayout.MainSidebarMinWidth) {
2655-
this.setTempWidthAndTempCollapsed(MagicLayout.MainSidebarMinWidth, true);
2656-
return MagicLayout.MainSidebarMinWidth;
2657-
}
2658-
// Set by CLI and not collapsed
2659-
if (!this.getCollapsed()) {
2659+
if (this.getCollapsed()) {
2660+
if (ignoreCollapse) {
2661+
return width;
2662+
} else {
2663+
return MagicLayout.MainSidebarMinWidth;
2664+
}
2665+
} else {
26602666
if (width <= MagicLayout.MainSidebarMinWidth) {
26612667
width = MagicLayout.MainSidebarDefaultWidth;
26622668
}
26632669
const snapPoint = MagicLayout.MainSidebarMinWidth + MagicLayout.MainSidebarSnapThreshold;
26642670
if (width < snapPoint || width > MagicLayout.MainSidebarMaxWidth) {
26652671
width = MagicLayout.MainSidebarDefaultWidth;
26662672
}
2667-
this.setTempWidthAndTempCollapsed(width, false);
2668-
return width;
26692673
}
2670-
this.setTempWidthAndTempCollapsed(width, this.getCollapsed());
26712674
return width;
26722675
}
26732676

0 commit comments

Comments
 (0)