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

Fix Graphviz component api #69

Merged
merged 6 commits into from
May 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
lib
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ Temporary Items
# End of https://www.gitignore.io/api/macos

# Output Modules
core
web
lib
docs
!src/**/*
*.d.ts
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
lib
25 changes: 10 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# @ts-graphviz/react

Graphviz-dot Renderer for React.
Graphviz-dot Renderer using React.

## Installation

Expand All @@ -25,20 +25,16 @@ $ npm install @ts-graphviz/react
# Peer Dependencies
$ yarn add react ts-graphviz
# Optional Peer Dependencies
$ yarn add @hpcc-js/wasm
$ yarn add @hpcc-js/wasm react-dom
```

## API

### Core Module
## DEMO

The core is designed to be independent of the execution environment.
- [Storybook](https://ts-graphviz.github.io/react/)

It is designed to work in both browsers and Node.js.

It provides core components and hooks of `@ts-graphviz/react` and render functions.
## API

#### Script
### Script

```tsx
import React, { FC } from 'react';
Expand Down Expand Up @@ -81,7 +77,7 @@ const dot = renderToDot(<Example />);
console.log(dot);
```

#### Output dot
### Output dot

```dot
digraph {
Expand Down Expand Up @@ -113,9 +109,9 @@ digraph {

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

### Web Module
### Web

The `Graphviz` component of `@ts-graphviz/react/web` can be rendered directly in the browser.
The `Graphviz` component can be rendered directly in the browser.

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`.

Expand All @@ -124,8 +120,7 @@ For development, I recommend using the one hosted by unpkg.
```tsx
import React, { FC } from 'react';
import ReactDOM from 'react-dom';
import { Digraph, Node, Edge } from '@ts-graphviz/react';
import { Graphviz } from '@ts-graphviz/react/web';
import { Graphviz, Digraph, Node, Edge } from '@ts-graphviz/react';
import { wasmFolder } from '@hpcc-js/wasm';

wasmFolder('https://unpkg.com/@hpcc-js/wasm/dist/');
Expand Down
2 changes: 1 addition & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
testEnvironment: 'jsdom',
verbose: true,
collectCoverage: true,
setupFilesAfterEnv: ['<rootDir>/config/jest/setup-jest.ts'],
Expand Down
8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
"publishConfig": {
"access": "public"
},
"main": "core/index.js",
"module": "core/index.mjs",
"types": "core/index.d.ts",
"main": "lib/index.js",
"module": "lib/index.mjs",
"types": "lib/index.d.ts",
"scripts": {
"example": "ts-node example/example",
"build": "ts-node rollup.build",
Expand Down Expand Up @@ -55,6 +55,8 @@
"@storybook/addon-knobs": "^5.3.18",
"@storybook/addon-storysource": "^5.3.18",
"@storybook/react": "^5.3.18",
"@testing-library/jest-dom": "^5.5.0",
"@testing-library/react": "^10.0.4",
"@testing-library/react-hooks": "^3.2.1",
"@types/fs-extra": "^8.1.0",
"@types/prop-types": "^15.7.3",
Expand Down
26 changes: 8 additions & 18 deletions rollup.build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { rollup } from 'rollup';
import typescript from 'rollup-plugin-typescript2';
import { terser } from 'rollup-plugin-terser';
import commonjs from '@rollup/plugin-commonjs';
import { move, remove } from 'fs-extra';
import { remove } from 'fs-extra';

async function build({
input,
Expand Down Expand Up @@ -43,21 +43,11 @@ async function build({
}

(async (): Promise<void> => {
await Promise.all([remove('core'), remove('web')]);
await Promise.all([
build({
input: 'src/index.ts',
output: 'core',
declaration: true,
external: ['react', 'react-dom/server', 'ts-graphviz', 'prop-types', 'react-reconciler'],
}),
build({
input: 'src/web/index.ts',
output: 'core/web',
declaration: false,
external: ['react', 'react-dom/server', 'ts-graphviz', 'react-reconciler', '@hpcc-js/wasm'],
}),
]);

await move('core/web', 'web');
await remove('lib');
await build({
input: 'src/index.ts',
output: 'lib',
declaration: true,
external: ['react', 'react-dom/server', 'ts-graphviz', 'prop-types', 'react-reconciler', '@hpcc-js/wasm'],
});
})();
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/* eslint-disable react/no-danger */
/* eslint-disable react/prop-types */
import React, { FC, ReactElement, useMemo } from 'react';
import { renderToDot } from '../../renderer/render';
import { useRendered, Engine, Image, File } from '../hooks/rendered';
import React, { FC, useMemo, ReactElement } from 'react';
import { renderToDot } from '../renderer/render';
import { useRendered, Engine, Image, File } from '../hooks/use-rendered';

type Props = {
children: ReactElement;
Expand All @@ -11,6 +11,9 @@ type Props = {
files?: File[];
};

/**
* Web only
*/
export const Graphviz: FC<Props> = ({ children, engine, images, files }) => {
const dot = useMemo(() => renderToDot(children), [children]);
const rendered = useRendered(dot, engine, 'svg', { images, files });
Expand Down
38 changes: 38 additions & 0 deletions src/components/__tests__/Graphviz.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import React from 'react';
import { render } from '@testing-library/react';
import { Graphviz } from '../Graphviz';
import { Engine, Format, Image } from '../../hooks/use-rendered';
import { Digraph } from '../Digraph';
import { Node } from '../Node';
import { Edge } from '../Edge';

jest.mock('../../hooks/use-rendered', () => {
return {
useRendered: (
dot: string,
engine?: Engine,
format?: Format,
ext?: {
images?: Image[];
files?: File[];
},
): string => JSON.stringify({ dot, engine, format, ext }),
};
});

describe('Graphviz component', () => {
test('render dot element', () => {
const { container } = render(
<Graphviz>
<Digraph>
<Node id="n1" />
<Node id="n2" />
<Node id="n3" />
<Edge targets={['n1', 'n2', 'n3']} />
<Edge targets={['n1', 'n3']} />
</Digraph>
</Graphviz>,
);
expect(container).toMatchSnapshot();
});
});
9 changes: 9 additions & 0 deletions src/components/__tests__/__snapshots__/Graphviz.spec.tsx.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Graphviz component render dot element 1`] = `
<div>
<div>
{"dot":"digraph {\\n \\"n1\\";\\n \\"n2\\";\\n \\"n3\\";\\n \\"n1\\" -&gt; \\"n2\\" -&gt; \\"n3\\";\\n \\"n1\\" -&gt; \\"n3\\";\\n}","format":"svg","ext":{"images":[],"files":[]}}
</div>
</div>
`;
File renamed without changes.
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from './renderer/render';
export * from './hooks/use-rendered';
export * from './hooks/use-cluster';
export * from './hooks/use-graphviz-context';
export * from './hooks/use-digraph';
Expand All @@ -8,6 +9,7 @@ export * from './hooks/use-edge';
export * from './hooks/use-node';
export * from './hooks/use-rendered-id';
export * from './hooks/use-root-cluster';
export * from './components/Graphviz';
export * from './components/HtmlLike';
export * from './components/Graph';
export * from './components/Digraph';
Expand Down
3 changes: 1 addition & 2 deletions src/stories/animations.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import React, { FC, useState, useEffect } from 'react';
import { Graphviz } from '../web';
import { Digraph, Node, Edge } from '../index';
import { Graphviz, Digraph, Node, Edge } from '../index';

export default { title: 'Graphviz/Animations' };

Expand Down
3 changes: 1 addition & 2 deletions src/stories/components.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
/* eslint-disable @typescript-eslint/explicit-function-return-type */
import React, { FC } from 'react';
import { withKnobs, text, select, number } from '@storybook/addon-knobs';
import { Graphviz } from '../web';
import { Digraph, Graph, Node, Edge } from '../index';
import { Graphviz, Digraph, Graph, Node, Edge } from '../index';

export default { title: 'Graphviz/Components', decorators: [withKnobs] };

Expand Down
3 changes: 1 addition & 2 deletions src/stories/index.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import React, { FC } from 'react';
import { select, withKnobs } from '@storybook/addon-knobs';
import { Graphviz } from '../web';
import { Digraph, Node, Edge, Subgraph, DOT } from '../index';
import { Digraph, Node, Edge, Subgraph, Graphviz, DOT } from '../index';

export default { title: 'Graphviz', decorators: [withKnobs] };

Expand Down
2 changes: 1 addition & 1 deletion src/utils/errors.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export const EdgeTargetLengthErrorMessage = 'Edges must have at least 2 targets.';
export const NoGraphvizContextErrorMessage =
'Cannot call useGraphvizContext outside GraphvizContext.\nCannot call useGraphvizContext outside context.\nBasically, you need to use the render function provided by @ts-graphviz/react.';
'Cannot call useGraphvizContext outside GraphvizContext.\nBasically, you need to use the render function provided by @ts-graphviz/react.';
export const NoClusterErrorMessage = 'useCluster must be called within a cluster such as Digraph, Graph, Subgraph.';
export const DuplicatedRootClusterErrorMessage = 'RootCluster is duplicated.\nUse only one of Digraph and Graph.';
2 changes: 0 additions & 2 deletions src/web/index.ts

This file was deleted.

Loading