Skip to content

Commit 495ff5b

Browse files
committed
fix: add workaround for missing use calls
Creates a function that hte user of the library can call to register with their cytoscape. Ignores errors in registration, incase has multiple calls. Not an idea fix for #41 but good enough for now to work around the issue. Also exports the CytoscapeGraphLayoutAdapter as a backup. see #41
1 parent 354062f commit 495ff5b

File tree

4 files changed

+33
-3
lines changed

4 files changed

+33
-3
lines changed

example/index.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,13 @@ import {
1818
Graph,
1919
GraphModel,
2020
GraphToolbar,
21+
initializeCytoscape,
2122
ModelNode,
2223
NodeViewer,
2324
} from '../dist/committed-components-graph.cjs.js'
25+
import { use } from 'cytoscape'
26+
27+
initializeCytoscape(use)
2428

2529
const App: React.FC = () => {
2630
const [model, setModel] = React.useState(

src/graph/renderer/CytoscapeRenderer.tsx

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
2+
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
3+
/* eslint-disable @typescript-eslint/no-unsafe-call */
14
import { useTheme } from '@committed/components'
25
import {
36
Css,
@@ -45,6 +48,26 @@ import {
4548
} from './CytoscapeGraphLayoutAdapter'
4649
import { useCyListener } from './useCyListener'
4750

51+
/**
52+
* Call to initialize the additional modules.
53+
*/
54+
/*
55+
* This is a work-around - the modules do not seem to be registered when using as a library so must be called by the lib user.
56+
* Should be able to remove but may be a bundling issue.
57+
*/
58+
export function initializeCytoscape(
59+
cyuse: (module: cytoscape.Ext) => void
60+
): void {
61+
try {
62+
cyuse(CytoscapeGraphLayoutAdapter.register)
63+
cyuse(ccola)
64+
} catch {
65+
// Ignore multiple attempts to initialize
66+
}
67+
}
68+
69+
initializeCytoscape(use)
70+
4871
export interface CyGraphRendererOptions extends GraphRendererOptions {
4972
renderOptions: Pick<
5073
React.ComponentProps<typeof CytoscapeComponent>,
@@ -61,9 +84,6 @@ export interface CyGraphRendererOptions extends GraphRendererOptions {
6184
>
6285
}
6386

64-
use(CytoscapeGraphLayoutAdapter.register)
65-
use(ccola)
66-
6787
const dblClick = () => {
6888
// eslint-disable-next-line @typescript-eslint/no-explicit-any
6989
let clicked: any | null = null

src/graph/renderer/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
export * from './CytoscapeRenderer'
2+
export * from './CytoscapeGraphLayoutAdapter'

src/introduction.stories.mdx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,14 @@ import {
8181
GraphModel,
8282
GraphToolbar,
8383
cytoscapeRenderer,
84+
initializeCytoscape,
8485
} from '@committed/components-graph'
8586
import { Row, ThemeProvider } from '@committed/components'
8687

88+
// If using the cytoscape renderer, just once in your application add
89+
import { use } from 'cytoscape'
90+
initializeCytoscape(use)
91+
8792
const App: React.FC = () => {
8893
const [model, setModel] = React.useState(
8994
addRandomEdge(addRandomNode(GraphModel.createEmpty(), 20), 15)

0 commit comments

Comments
 (0)