Skip to content

Commit 2bbf25d

Browse files
authored
fix: visual fixes on load, viewport position enhancement and dropdown close logic (#207)
# Pull Request Overview This pull request initially aimed to address a bug where the layer did not update correctly on load. The actual fix turned out to be quite simple—we just needed to set the current layer value explicitly in the load function. This step is essential because, although the canvas defaulted to the "link" layer, both the right sidebar and the layer dropdown retained the previous layer state. Rather than forcing all components to switch to the "link" layer, updating the graph's layer alone is a cleaner solution. It also preserves the user's original settings, which might be more relevant depending on the context. We can discuss whether this or a more aggressive sync approach is preferable. ## Viewport Save/Load Improvements Some inconsistencies the team encountered when saving and loading the viewport's zoom level and position were also tackled. The updated logic now performs more thorough validation of the position values and handles edge cases more gracefully. In addition, logs have been added to help debug if similar issues arise in the future. ## UI Enhancement: Dropdown Behavior Lastly, the Dropdown component has been updated to close automatically when the user touches or drags outside of the options box. Thanks to the recent refactor of the visual structure, this improvement was straightforward to implement—and surprisingly satisfying! closes #197
1 parent 25f1fea commit 2bbf25d

File tree

4 files changed

+43
-7
lines changed

4 files changed

+43
-7
lines changed

src/graphics/basic_components/dropdown.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,17 @@ export class Dropdown {
3535
CSS_CLASSES.OPTIONS_CONTAINER_NOT_PUSH,
3636
);
3737
}
38+
3839
this.initializeDropdown();
40+
41+
// Handle all outside interactions to close the dropdown
42+
["click", "dragstart", "touchstart", "tap"].forEach((eventType) => {
43+
document.addEventListener(eventType, (event) => {
44+
if (!this.container.contains(event.target as Node)) {
45+
this.close();
46+
}
47+
});
48+
});
3949
}
4050

4151
private initializeDropdown(): void {
@@ -136,6 +146,10 @@ export class Dropdown {
136146
this.optionsContainer.classList.remove(CSS_CLASSES.SHOW); // Ensure the dropdown is closed
137147
}
138148

149+
private close(): void {
150+
this.optionsContainer.classList.remove(CSS_CLASSES.SHOW);
151+
}
152+
139153
// Public method to render the dropdown
140154
toHTML(): HTMLElement {
141155
return this.container;

src/graphics/viewport.ts

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,15 +78,35 @@ export class Viewport extends pixi_viewport.Viewport {
7878
const savedZoom = localStorage.getItem("viewportZoom");
7979

8080
if (savedPosition) {
81-
const { x, y } = JSON.parse(savedPosition);
82-
this.position.set(x, y);
81+
try {
82+
const { x, y } = JSON.parse(savedPosition);
83+
if (typeof x === "number" && typeof y === "number") {
84+
this.position.set(x, y);
85+
} else {
86+
console.warn("Invalid saved position, resetting to center.");
87+
this.setCenter();
88+
}
89+
} catch (e) {
90+
console.error("Error parsing viewportPosition:", e);
91+
this.setCenter();
92+
}
8393
} else {
84-
this.position.set(WORLD_WIDTH / 2, WORLD_HEIGHT / 2);
94+
this.setCenter();
8595
}
8696

8797
if (savedZoom) {
88-
const { x, y } = JSON.parse(savedZoom);
89-
this.scale.set(x, y);
98+
try {
99+
const { x, y } = JSON.parse(savedZoom);
100+
if (typeof x === "number" && typeof y === "number") {
101+
this.scale.set(x, y);
102+
} else {
103+
console.warn("Invalid saved zoom, resetting to 1.");
104+
this.scale.set(1);
105+
}
106+
} catch (e) {
107+
console.error("Error parsing viewportZoom:", e);
108+
this.scale.set(1);
109+
}
90110
} else {
91111
this.scale.set(1);
92112
}
@@ -95,6 +115,8 @@ export class Viewport extends pixi_viewport.Viewport {
95115
clear() {
96116
this.removeChildren();
97117
this.addChild(new Background());
118+
this.setCenter();
119+
this.scale.set(1);
98120
localStorage.removeItem("viewportPosition");
99121
localStorage.removeItem("viewportZoom");
100122
}

src/types/viewportManager.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ export function loadFromFile(ctx: GlobalContext) {
185185
showError(ALERT_MESSAGES.FAILED_TO_LOAD_GRAPH);
186186
return;
187187
}
188-
ctx.load(dataGraph);
188+
ctx.load(dataGraph, ctx.getCurrentLayer());
189189
ctx.centerView();
190190

191191
showSuccess(ALERT_MESSAGES.GRAPH_LOADED_SUCCESSFULLY);

src/utils/constants/alert_constants.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@ export const ALERT_MESSAGES = {
1515
ROUTING_TABLE_REGENERATED: "Routing table regenerated successfully.",
1616
LAYER_CHANGED:
1717
"Layer changed successfully. Programs that do not belong to the selected layer will be hidden but will continue running.",
18-
GRAPH_LOADED_SUCCESSFULLY: "network loaded successfully.",
18+
GRAPH_LOADED_SUCCESSFULLY: "Network loaded successfully.",
1919
FAILED_TO_LOAD_GRAPH: "Failed to load the network.",
2020
};

0 commit comments

Comments
 (0)