Skip to content
This repository was archived by the owner on Mar 17, 2024. It is now read-only.

Commit 4313d84

Browse files
authored
Merge pull request #68 from ts-graphviz/web
Web Module
2 parents e2a875a + 4d49bd7 commit 4313d84

17 files changed

+6705
-146
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
name: Generate Doc and Deploy
2+
3+
on:
4+
push:
5+
branches:
6+
- master
7+
8+
jobs:
9+
deploy:
10+
name: Deploy to gh-pages
11+
runs-on: ubuntu-latest
12+
steps:
13+
- uses: actions/checkout@v1
14+
- name: Use Node.js
15+
uses: actions/setup-node@v1
16+
with:
17+
node-version: 10.x
18+
- name: Install dependencies
19+
run: yarn --frozen-lockfile
20+
env:
21+
CI: true
22+
- name: Generate Storybook
23+
run: yarn storybook:build
24+
env:
25+
CI: true
26+
- name: Deploy
27+
uses: peaceiris/actions-gh-pages@v2.5.0
28+
env:
29+
PERSONAL_TOKEN: ${{ secrets.PERSONAL_TOKEN }}
30+
PUBLISH_BRANCH: gh-pages
31+
PUBLISH_DIR: ./docs

.gitignore

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,4 +130,8 @@ Temporary Items
130130

131131
# End of https://www.gitignore.io/api/macos
132132

133-
lib
133+
# Output Modules
134+
core
135+
web
136+
docs
137+
*.d.ts

.npmignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@
77
.vscode
88
config
99
coverage
10+
docs
1011
jest.config.js
1112
LICENSE
12-
rollup.config.js
13+
rollup.build.ts
1314
src
1415
tsconfig.json

.storybook/config.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { wasmFolder } from '@hpcc-js/wasm';
2+
3+
wasmFolder('https://unpkg.com/@hpcc-js/wasm/dist/');

.storybook/main.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
module.exports = {
2+
stories: ['../src/**/*.stories.[tj]sx'],
3+
addons: [
4+
'@storybook/addon-storysource',
5+
'@storybook/addon-knobs/register',
6+
],
7+
webpackFinal: async config => {
8+
config.module.rules.push({
9+
test: /\.(ts|tsx)$/,
10+
use: [
11+
{
12+
loader: require.resolve('ts-loader'),
13+
},
14+
],
15+
});
16+
config.resolve.extensions.push('.ts', '.tsx');
17+
return config;
18+
},
19+
};

README.md

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,30 @@ $ npm install @ts-graphviz/react
1919

2020
- [React](https://github.com/facebook/react/)(>=16.8)
2121
- [ts-graphviz](https://github.com/ts-graphviz/ts-graphviz)
22+
- [@hpcc-js/wasm](https://www.npmjs.com/package/@hpcc-js/wasm) (Optional)
2223

2324
```bash
25+
# Peer Dependencies
2426
$ yarn add react ts-graphviz
27+
# Optional Peer Dependencies
28+
$ yarn add @hpcc-js/wasm
2529
```
2630

27-
## Usage
31+
## API
2832

29-
### Example
33+
### Core Module
34+
35+
The core is designed to be independent of the execution environment.
36+
37+
It is designed to work in both browsers and Node.js.
38+
39+
It provides core components and hooks of `@ts-graphviz/react` and render functions.
3040

3141
#### Script
3242

3343
```tsx
3444
import React, { FC } from 'react';
35-
import { Digraph, Node, Subgraph, renderToDot, Edge, DOT } from '@ts-graphviz/react';
45+
import { Digraph, Node, Subgraph, Edge, DOT, renderToDot } from '@ts-graphviz/react';
3646

3747
const Example: FC = () => (
3848
<Digraph
@@ -87,7 +97,6 @@ digraph {
8797
shape = "none",
8898
label = <<TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0"><TR><TD>left</TD><TD PORT="m">middle</TD><TD PORT="r">right</TD></TR></TABLE>>,
8999
];
90-
"nodeB";
91100
subgraph "cluster" {
92101
labeljust = "l";
93102
label = "Cluster";
@@ -104,6 +113,38 @@ digraph {
104113

105114
![dot](./example/example.svg)
106115

116+
### Web Module
117+
118+
The `Graphviz` component of `@ts-graphviz/react/web` can be rendered directly in the browser.
119+
120+
Since this component uses the function of `@hpcc-js/wasm` internally, it is necessary to host `@hpcc-js/wasm/dist/graphviz.wasm` and specify its directory with `wasmFolder`.
121+
122+
For development, I recommend using the one hosted by unpkg.
123+
124+
```tsx
125+
import React, { FC } from 'react';
126+
import ReactDOM from 'react-dom';
127+
import { Digraph, Node, Edge } from '@ts-graphviz/react';
128+
import { Graphviz } from '@ts-graphviz/react/web';
129+
import { wasmFolder } from '@hpcc-js/wasm';
130+
131+
wasmFolder('https://unpkg.com/@hpcc-js/wasm/dist/');
132+
133+
const App: FC = () => (
134+
<Graphviz>
135+
<Digraph>
136+
<Node id="n1" />
137+
<Node id="n2" />
138+
<Node id="n3" />
139+
<Edge targets={['n1', 'n2', 'n3']} />
140+
<Edge targets={['n1', 'n3']} />
141+
</Digraph>
142+
</Graphviz>
143+
);
144+
145+
ReactDOM.render(<App />, document.getElementById('root'));
146+
```
147+
107148
## See Also
108149

109150
Graphviz-dot Test and Integration

package.json

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,32 +23,47 @@
2323
"publishConfig": {
2424
"access": "public"
2525
},
26-
"main": "lib/index.js",
27-
"module": "lib/index.mjs",
28-
"types": "lib/index.d.ts",
26+
"main": "core/index.js",
27+
"module": "core/index.mjs",
28+
"types": "core/index.d.ts",
2929
"scripts": {
30-
"build": "rollup -c",
30+
"example": "ts-node example/example",
31+
"build": "ts-node rollup.build",
3132
"test": "jest",
32-
"lint": "eslint -c .eslintrc.json --ext ts,tsx src"
33+
"lint": "eslint -c .eslintrc.json --ext ts,tsx src",
34+
"storybook": "start-storybook -p 9000",
35+
"storybook:build": "build-storybook -c .storybook -o docs"
3336
},
3437
"peerDependencies": {
38+
"@hpcc-js/wasm": "^0.3.13",
3539
"react": ">=16.8.0",
3640
"react-dom": ">=16.8.0",
3741
"ts-graphviz": ">=0.10.0"
3842
},
43+
"peerDependenciesMeta": {
44+
"@hpcc-js/wasm": {
45+
"optional": true
46+
}
47+
},
3948
"dependencies": {
4049
"prop-types": "^15.7.2",
4150
"react-reconciler": "^0.24.0"
4251
},
4352
"devDependencies": {
53+
"@hpcc-js/wasm": "^0.3.13",
4454
"@rollup/plugin-commonjs": "^11.1.0",
55+
"@storybook/addon-knobs": "^5.3.18",
56+
"@storybook/addon-storysource": "^5.3.18",
57+
"@storybook/react": "^5.3.18",
4558
"@testing-library/react-hooks": "^3.2.1",
59+
"@types/fs-extra": "^8.1.0",
4660
"@types/prop-types": "^15.7.3",
4761
"@types/react": "^16.9.17",
4862
"@types/react-dom": "^16.9.4",
4963
"@types/react-reconciler": "^0.18.0",
5064
"@typescript-eslint/eslint-plugin": "^2.28.0",
5165
"@typescript-eslint/parser": "^2.28.0",
66+
"babel-loader": "^8.1.0",
5267
"eslint": "^6.8.0",
5368
"eslint-config-airbnb": "^18.1.0",
5469
"eslint-config-prettier": "^6.10.1",
@@ -58,6 +73,7 @@
5873
"eslint-plugin-prettier": "^3.1.3",
5974
"eslint-plugin-react": "^7.19.0",
6075
"eslint-plugin-react-hooks": "^3.0.0",
76+
"fs-extra": "^9.0.0",
6177
"jest": "^25.3.0",
6278
"jest-graphviz": "^0.3.1",
6379
"prettier": "^1.19.1",
@@ -69,6 +85,8 @@
6985
"rollup-plugin-typescript2": "^0.27.0",
7086
"ts-graphviz": "^0.10.0",
7187
"ts-jest": "^25.3.1",
88+
"ts-loader": "^7.0.2",
89+
"ts-node": "^8.9.1",
7290
"typescript": "^3.8.2"
7391
}
7492
}

rollup.build.ts

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import { rollup } from 'rollup';
2+
import typescript from 'rollup-plugin-typescript2';
3+
import { terser } from 'rollup-plugin-terser';
4+
import commonjs from '@rollup/plugin-commonjs';
5+
import { move, remove } from 'fs-extra';
6+
7+
async function build({
8+
input,
9+
output,
10+
declaration,
11+
external,
12+
}: {
13+
input: string;
14+
output: string;
15+
declaration: boolean;
16+
external: string[];
17+
}): Promise<void> {
18+
const bundle = await rollup({
19+
input,
20+
plugins: [
21+
commonjs(),
22+
typescript({
23+
tsconfigOverride: {
24+
compilerOptions: {
25+
declaration,
26+
},
27+
},
28+
}),
29+
terser(),
30+
],
31+
external,
32+
});
33+
await Promise.all([
34+
bundle.write({
35+
format: 'cjs',
36+
file: `${output}/index.js`,
37+
}),
38+
bundle.write({
39+
format: 'esm',
40+
file: `${output}/index.mjs`,
41+
}),
42+
]);
43+
}
44+
45+
(async (): Promise<void> => {
46+
await Promise.all([remove('core'), remove('web')]);
47+
await Promise.all([
48+
build({
49+
input: 'src/index.ts',
50+
output: 'core',
51+
declaration: true,
52+
external: ['react', 'react-dom/server', 'ts-graphviz', 'prop-types', 'react-reconciler'],
53+
}),
54+
build({
55+
input: 'src/web/index.ts',
56+
output: 'core/web',
57+
declaration: false,
58+
external: ['react', 'react-dom/server', 'ts-graphviz', 'react-reconciler', '@hpcc-js/wasm'],
59+
}),
60+
]);
61+
62+
await move('core/web', 'web');
63+
})();

rollup.config.js

Lines changed: 0 additions & 20 deletions
This file was deleted.

src/stories/animations.stories.tsx

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import React, { FC, useState, useEffect } from 'react';
2+
import { Graphviz } from '../web';
3+
import { Digraph, Node, Edge } from '../index';
4+
5+
export default { title: 'Graphviz/Animations' };
6+
7+
const shapes = [
8+
'box',
9+
'polygon',
10+
'ellipse',
11+
'oval',
12+
'circle',
13+
'egg',
14+
'triangle',
15+
'parallelogram',
16+
'house',
17+
'pentagon',
18+
'hexagon',
19+
];
20+
21+
export const CircoAnime: FC = () => {
22+
const [nodes, setNodes] = useState<string[]>(['n0', 'n1']);
23+
const [i, setI] = useState<number>(2);
24+
useEffect(() => {
25+
const interval = setInterval(() => {
26+
setNodes([...nodes, `n${i}`]);
27+
setI(i + 1);
28+
}, 2000);
29+
return (): void => {
30+
clearInterval(interval);
31+
};
32+
}, [nodes, i, setNodes, setI]);
33+
return (
34+
<Graphviz engine="circo">
35+
<Digraph>
36+
{nodes.map((n, j) => (
37+
<Node id={n} key={n} shape={shapes[j % shapes.length]} />
38+
))}
39+
<Edge targets={nodes} />
40+
<Edge targets={[`n${i - 1}`, 'n0']} />
41+
</Digraph>
42+
</Graphviz>
43+
);
44+
};

0 commit comments

Comments
 (0)