Skip to content

Commit 20f1280

Browse files
authored
Merge pull request #152 from Linkurious/develop
v5.1.4
2 parents 396932c + dd68600 commit 20f1280

File tree

14 files changed

+1127
-529
lines changed

14 files changed

+1127
-529
lines changed

.bumpversion.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[bumpversion]
2-
current_version = 5.1.3
2+
current_version = 5.1.4
33
commit = False
44
tag = False
55
serialize =

.version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
5.1.3
1+
5.1.4

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,9 @@ Main visualisation component. You can use `onReady` or `ref` prop to get a refer
230230
| `graph?` | `Ogma.RawGraph` | `null` | The graph to render |
231231
| `onReady?` | `(ogma: Ogma) => void` | `null` | Callback when the Ogma instance is ready |
232232
| `ref?` | `React.Ref<Ogma>` | `null` | Reference to the Ogma instance |
233-
| `children` | `React.ReactNode` | `null` | The children of the component, such as `<Popup>` or `<Tooltip>` or your custom component. Ogma instance is avalable to the children components through `useOgma()` hook |
233+
| `children?` | `React.ReactNode` | `null` | The children of the component, such as `<Popup>` or `<Tooltip>` or your custom component. Ogma instance is avalable to the children components through `useOgma()` hook |
234+
| `theme?` | `Ogma.Themes` | `null` | The theme of the graph. Keep in mind that adding `<NodeStyle>` and `<EdgeStyle>` components will overwrite the theme's styles |
235+
| `onEventName` | `(event: EventTypes<ND, ED>[K]) => void` | `null` | The handler for an [event](https://doc.linkurious.com/ogma/latest/api/events.html). The passed in function should always be the result of the `useEvent` hook to have a stable function identity and avoid reassigning the same handler at every render. |
234236

235237
### `<NodeStyle />`
236238

demo/src/App.tsx

Lines changed: 49 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import OgmaLib, {
55
RawGraph,
66
NodeGrouping as NodeGroupingTransformation
77
} from "@linkurious/ogma";
8+
import { morningBreeze } from "@linkurious/ogma-styles";
89
import { useEffect, useState, createRef, useCallback } from "react";
910
import { LoadingOverlay } from "./components/LoadingOverlay";
1011
// for geo mode
@@ -18,10 +19,13 @@ import {
1819
NodeGrouping,
1920
Popup,
2021
Geo,
21-
NodeGroupingProps
22+
NodeGroupingProps,
23+
useEvent
2224
} from "../../src";
2325

24-
// cusotm components:
26+
import { Theme } from "../../src/types";
27+
28+
// custom components:
2529
// layout component, to be applied on certain events
2630
import { LayoutService } from "./components/Layout";
2731
// outlines canvas layer with halos
@@ -47,7 +51,7 @@ export default function App() {
4751
const [clickedNode, setClickedNode] = useState<Node>();
4852

4953
// ogma instance and grouping references
50-
const ref = createRef<OgmaLib>();
54+
const ogmaInstanceRef = createRef<OgmaLib>();
5155
const groupingRef = createRef<NodeGroupingTransformation<ND, ED>>();
5256

5357
// grouping and geo states
@@ -97,40 +101,59 @@ export default function App() {
97101
});
98102
}, []);
99103

104+
const onClick = useEvent("click", ({ target }) => {
105+
if (target && target.isNode) {
106+
setClickedNode(target);
107+
setPopupOpen(true);
108+
}
109+
});
110+
111+
const onMousemove = useEvent("mousemove", () => {
112+
if (!ogmaInstanceRef.current) return;
113+
const ptr = ogmaInstanceRef.current.getPointerInformation();
114+
requestSetTooltipPosition(
115+
ogmaInstanceRef.current.view.screenToGraphCoordinates({
116+
x: ptr.x,
117+
y: ptr.y
118+
})
119+
);
120+
setTarget(ptr.target);
121+
});
122+
123+
const onAddNodes = useEvent("addNodes", () => {
124+
if (!ogmaInstanceRef.current) return;
125+
ogmaInstanceRef.current.view.locateGraph({ duration: 250, padding: 50 });
126+
});
127+
128+
const onReady = useCallback((instance: OgmaLib<ND, ED>) => {
129+
ogmaInstanceRef.current = instance;
130+
}, []);
131+
132+
const addNode = useCallback(() => {
133+
if (!ogmaInstanceRef.current) return;
134+
ogmaInstanceRef.current.addNode({
135+
id: ogmaInstanceRef.current.getNodes().size
136+
});
137+
}, []);
138+
100139
// nothing to render yet
101140
if (loading) return <LoadingOverlay />;
102141

103142
return (
104143
<div className="App">
105144
<Logo />
106145
<Ogma
107-
ref={ref}
146+
ref={ogmaInstanceRef}
108147
graph={graph}
109-
onReady={(ogma) => {
110-
ogma.events
111-
.on("click", ({ target }) => {
112-
if (target && target.isNode) {
113-
setClickedNode(target);
114-
setPopupOpen(true);
115-
}
116-
})
117-
.on("mousemove", () => {
118-
const ptr = ogma.getPointerInformation();
119-
requestSetTooltipPosition(
120-
ogma.view.screenToGraphCoordinates({ x: ptr.x, y: ptr.y })
121-
);
122-
setTarget(ptr.target);
123-
})
124-
// locate graph when the nodes are added
125-
.on("addNodes", () =>
126-
ogma.view.locateGraph({ duration: 250, padding: 50 })
127-
);
128-
}}
148+
onClick={onClick}
149+
onMousemove={onMousemove}
150+
onAddNodes={onAddNodes}
151+
onReady={onReady}
152+
theme={morningBreeze as Theme<ND, ED>}
129153
>
130154
{/* Styling */}
131155
<NodeStyleRule
132156
attributes={{
133-
color: "#247BA0",
134157
radius: (n) => (n?.getData("multiplier") || 1) * nodeSize, // the label is the value os the property name.
135158
text: {
136159
content: (node) => node?.getData("properties.name"),
@@ -150,7 +173,6 @@ export default function App() {
150173
disabled={!nodeGrouping && !geoEnabled}
151174
groupIdFunction={groupingOptions.groupIdFunction}
152175
nodeGenerator={groupingOptions.nodeGenerator}
153-
duration={500}
154176
/>
155177

156178
{/* context-aware UI */}
@@ -193,6 +215,7 @@ export default function App() {
193215
setOutlines={setOutlines}
194216
geoEnabled={geoEnabled}
195217
setGeoEnabled={setGeoEnabled}
218+
addNode={addNode}
196219
/>
197220
</div>
198221
);

demo/src/components/Controls.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ interface ControlsProps {
1313
setOutlines: (value: boolean) => void;
1414
geoEnabled: boolean;
1515
setGeoEnabled: (value: boolean) => void;
16+
addNode: () => void;
1617
}
1718

1819
export function Controls({
@@ -24,6 +25,7 @@ export function Controls({
2425
setGeoEnabled,
2526
outlines,
2627
setOutlines,
28+
addNode
2729
}: ControlsProps) {
2830
//const ogma = useOgma();
2931
const [drawerShown, setDrawerShown] = useState(false);
@@ -94,6 +96,11 @@ export function Controls({
9496
label="Geo mode"
9597
/>
9698
</div>
99+
<div className="controls-section">
100+
<div className="link-button" onClick={() => addNode()}>
101+
Add node
102+
</div>
103+
</div>
97104
</Drawer>
98105
</>
99106
);

demo/src/index.css

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ body {
88
--overlay-background-color: #282c34;
99
--text-color: #222222;
1010
--brand-color: #0094ff;
11+
--brand-color-darker: #0074cc;
1112
--overlay-text-color: var(--brand-color);
1213
--fontFamily: "IBM Plex Sans", sans-serif;
1314
--border-radius: 5px;
@@ -189,3 +190,16 @@ code {
189190
.gh-link {
190191
margin-left: 1em;
191192
}
193+
194+
.link-button {
195+
cursor: pointer;
196+
color: var(--brand-color);
197+
text-decoration: underline;
198+
text-align: left;
199+
}
200+
201+
.link-button:hover,
202+
.link-button:focus,
203+
.link-button:active {
204+
color: var(--brand-color-darker);
205+
}

0 commit comments

Comments
 (0)