Skip to content

Commit

Permalink
#834 Fix zIndex out of bound in upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
thekingofcity committed Sep 22, 2024
1 parent 55bec3c commit f6f98c5
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/util/save.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -441,12 +441,12 @@ describe('Unit tests for param upgrade function', () => {
it('33 -> 34', () => {
// Bump save version to support parallel lines.
const oldParam =
'{"svgViewBoxZoom":100,"svgViewBoxMin":{"x":0,"y":0},"graph":{"options":{"type":"directed","multi":true,"allowSelfLoops":true},"attributes":{},"nodes":[{"key":"misc_node__8CmQwl2e2","attributes":{"visible":true,"zIndex":-1,"x":0,"y":0,"type":"virtual","virtual":{}}},{"key":"misc_node_966GIPcDUU","attributes":{"visible":true,"zIndex":0,"x":50,"y":50,"type":"virtual","virtual":{}}}],"edges":[{"key":"line_39qw6f0ehK","source":"misc_node__8CmQwl2e2","target":"misc_node_966GIPcDUU","attributes":{"visible":true,"zIndex":1,"type":"diagonal","diagonal":{"startFrom":"from","offsetFrom":0,"offsetTo":0,"roundCornerFactor":10},"style":"single-color","single-color":{"color":["shanghai","sh1","#E3002B","#fff"]},"reconcileId":""}}]},"version":33}';
'{"svgViewBoxZoom":100,"svgViewBoxMin":{"x":0,"y":0},"graph":{"options":{"type":"directed","multi":true,"allowSelfLoops":true},"attributes":{},"nodes":[{"key":"misc_node__8CmQwl2e2","attributes":{"visible":true,"zIndex":-1,"x":0,"y":0,"type":"virtual","virtual":{}}},{"key":"misc_node_966GIPcDUU","attributes":{"visible":true,"zIndex":0,"x":50,"y":50,"type":"virtual","virtual":{}}}],"edges":[{"key":"line_39qw6f0ehK","source":"misc_node__8CmQwl2e2","target":"misc_node_966GIPcDUU","attributes":{"visible":true,"zIndex":-10,"type":"diagonal","diagonal":{"startFrom":"from","offsetFrom":0,"offsetTo":0,"roundCornerFactor":10},"style":"single-color","single-color":{"color":["shanghai","sh1","#E3002B","#fff"]},"reconcileId":""}}]},"version":33}';
const newParam = UPGRADE_COLLECTION[33](oldParam);
const graph = new MultiDirectedGraph() as MultiDirectedGraph<NodeAttributes, EdgeAttributes, GraphAttributes>;
expect(() => graph.import(JSON.parse(newParam))).not.toThrow();
const expectParam =
'{"svgViewBoxZoom":100,"svgViewBoxMin":{"x":0,"y":0},"graph":{"options":{"type":"directed","multi":true,"allowSelfLoops":true},"attributes":{},"nodes":[{"key":"misc_node__8CmQwl2e2","attributes":{"visible":true,"zIndex":4,"x":0,"y":0,"type":"virtual","virtual":{}}},{"key":"misc_node_966GIPcDUU","attributes":{"visible":true,"zIndex":5,"x":50,"y":50,"type":"virtual","virtual":{}}}],"edges":[{"key":"line_39qw6f0ehK","source":"misc_node__8CmQwl2e2","target":"misc_node_966GIPcDUU","attributes":{"visible":true,"zIndex":-4,"type":"diagonal","diagonal":{"startFrom":"from","offsetFrom":0,"offsetTo":0,"roundCornerFactor":10},"style":"single-color","single-color":{"color":["shanghai","sh1","#E3002B","#fff"]},"reconcileId":"","parallelIndex":-1}}]},"version":34}';
'{"svgViewBoxZoom":100,"svgViewBoxMin":{"x":0,"y":0},"graph":{"options":{"type":"directed","multi":true,"allowSelfLoops":true},"attributes":{},"nodes":[{"key":"misc_node__8CmQwl2e2","attributes":{"visible":true,"zIndex":4,"x":0,"y":0,"type":"virtual","virtual":{}}},{"key":"misc_node_966GIPcDUU","attributes":{"visible":true,"zIndex":5,"x":50,"y":50,"type":"virtual","virtual":{}}}],"edges":[{"key":"line_39qw6f0ehK","source":"misc_node__8CmQwl2e2","target":"misc_node_966GIPcDUU","attributes":{"visible":true,"zIndex":-10,"type":"diagonal","diagonal":{"startFrom":"from","offsetFrom":0,"offsetTo":0,"roundCornerFactor":10},"style":"single-color","single-color":{"color":["shanghai","sh1","#E3002B","#fff"]},"reconcileId":"","parallelIndex":-1}}]},"version":34}';
expect(newParam).toEqual(expectParam);
});

Expand Down
6 changes: 3 additions & 3 deletions src/util/save.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export const upgrade: (originalParam: string | null) => Promise<string> = async
}

if (changed) {
console.warn(`Upgrade save to version: ${version}`);
console.warn(`Upgrade save from version: ${originalSave.version} to version: ${version}`);
// Backup original param in case of bugs in the upgrades.
localStorage.setItem(LocalStorageKey.PARAM_BACKUP, originalParam);
}
Expand Down Expand Up @@ -434,10 +434,10 @@ export const UPGRADE_COLLECTION: { [version: number]: (param: string) => string
graph.import(p?.graph);
graph.forEachDirectedEdge(edge => {
graph.setEdgeAttribute(edge, 'parallelIndex', -1);
graph.updateEdgeAttribute(edge, 'zIndex', zIndex => (zIndex ?? 0) - 5);
graph.updateEdgeAttribute(edge, 'zIndex', zIndex => Math.max(-10, (zIndex ?? 0) - 5));
});
graph.forEachNode(node => {
graph.updateNodeAttribute(node, 'zIndex', zIndex => (zIndex ?? 0) + 5);
graph.updateNodeAttribute(node, 'zIndex', zIndex => Math.min(10, (zIndex ?? 0) + 5));
});
return JSON.stringify({ ...p, version: 34, graph: graph.export() });
},
Expand Down

0 comments on commit f6f98c5

Please sign in to comment.