Skip to content

Commit 800fe78

Browse files
authored
Merge pull request #5 from chengcyber/feat-change-direction
Feat change direction
2 parents 3369415 + 4126c25 commit 800fe78

File tree

8 files changed

+63
-2
lines changed

8 files changed

+63
-2
lines changed

.changeset/cold-badgers-develop.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"pnpm-workspace-graph": minor
3+
---
4+
5+
Support change direction: Top to Bottom or Left to Right

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,18 @@ Visualize project relationships in your [PNPM](https://pnpm.io/) workspace
6666
<p align="center"><i>An example graph made by cloning the <a href="https://github.com/pnpm/pnpm">https://github.com/pnpm/pnpm</a> workspace</i></p>
6767

6868

69+
## Features
70+
71+
1. Layout the graph with different directions: Top to Bottom or Left to Right.
72+
73+
Top to Bottom | Left to Right
74+
:-------------------------:|:-------------------------:
75+
<kbd><img src="assets/direction-tb.png" /></kbd> | <kbd><img src="assets/direction-lr.png" /></kbd>
76+
77+
2. Save the graph to PNG for later use.
78+
79+
<kbd><img src="assets/save-graph.png" /></kbd>
80+
6981
## CLI parameters
7082

7183
```shell

assets/direction-lr.png

79.7 KB
Loading

assets/direction-tb.png

96.7 KB
Loading

assets/example-settings.png

-8.84 KB
Loading

assets/save-graph.png

21.5 KB
Loading

client/ControlPanel.tsx

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import {
99
Toolbar,
1010
} from "@fluentui/react-components";
1111
import {
12+
ArrowDown20Regular,
13+
ArrowRight20Filled,
1214
ChevronLeft20Regular,
1315
ChevronRight20Regular,
1416
DataFunnel20Regular,
@@ -21,7 +23,7 @@ import { FieldValues, FormProvider, useForm } from "react-hook-form";
2123
import { saveAs } from "file-saver";
2224
import { ControlledTextFieldArray } from "./ControlledFormComponent/ControlledTextFieldArray";
2325
import { GraphService } from "./service/Graph";
24-
import { useState } from "react";
26+
import { useEffect, useState } from "react";
2527

2628
const gs = GraphService.getInstance();
2729

@@ -37,6 +39,28 @@ export const ControlPanel = () => {
3739
const [isExpandActions, { toggle: toggleExpandActions }] = useBoolean(true);
3840
const [isExportingImage, setIsExportImage] = useState(false);
3941

42+
const [direction, setDirection] = useState<'TB' | 'LR'>(gs.getLayoutConfig().rankDir || 'TB');
43+
let directionIcon = null;
44+
if (direction === 'TB') {
45+
directionIcon = <ArrowDown20Regular />
46+
} else if (direction === 'LR') {
47+
directionIcon = <ArrowRight20Filled />
48+
}
49+
const directionText: string = direction === 'LR' ? 'Left to Right' : 'Top to Bottom'
50+
const toggleDirection = () => {
51+
if (direction === 'TB') {
52+
setDirection('LR')
53+
gs.setLayoutConfig({
54+
rankDir: 'LR',
55+
})
56+
} else {
57+
setDirection('TB')
58+
gs.setLayoutConfig({
59+
rankDir: 'TB',
60+
})
61+
}
62+
}
63+
4064
const onSave = () => {
4165
console.log("on save");
4266
setIsExportImage(true);
@@ -121,6 +145,14 @@ export const ControlPanel = () => {
121145
>
122146
{isExpandActions ? "Filter" : ""}
123147
</Button>
148+
<Button
149+
appearance="subtle"
150+
icon={directionIcon}
151+
title="Direction"
152+
onClick={toggleDirection}
153+
>
154+
{isExpandActions ? directionText : ''}
155+
</Button>
124156
<Button
125157
appearance="subtle"
126158
icon={

client/service/Graph.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import cy from "cytoscape";
2-
import dagre from "cytoscape-dagre";
2+
import dagre, { DagreLayoutOptions } from "cytoscape-dagre";
33
import { cyGraphStyle } from "../style/graph";
44
import { ViewerData } from "../types";
55
import { BridgeService } from "./Bridge";
@@ -114,6 +114,18 @@ class GraphService {
114114
);
115115
};
116116

117+
public setLayoutConfig(config: Partial<CytoscapeDagreConfig>): void {
118+
this._dagreLayoutConfig = {
119+
...this._dagreLayoutConfig,
120+
...config,
121+
}
122+
this.showAllProjects();
123+
}
124+
125+
public getLayoutConfig(): Partial<CytoscapeDagreConfig> {
126+
return this._dagreLayoutConfig;
127+
}
128+
117129
public exportToPNG(): Promise<Blob> | undefined {
118130
if (this._fgGraph) {
119131
return this._fgGraph.png({

0 commit comments

Comments
 (0)