Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add docs about using node.js api usage #177

Merged
merged 1 commit into from
Oct 23, 2023
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ demo/dist/
demo/cypress/videos/
.idea/

# platform
.DS_Store

# Created by https://www.gitignore.io/api/node

### Node ###
Expand Down
47 changes: 47 additions & 0 deletions packages/pintora-cli/examples/nodejs-render-example.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import * as fs from 'fs'
import { render, PintoraConfig } from '..'

const buildSVG = async (code: string, config?: Partial<PintoraConfig>) => {
const str = await render({
code: code,
pintoraConfig: config,
mimeType: 'image/svg+xml',
width: 1000,
backgroundColor: '#fff',
})
fs.writeFileSync('example.svg', str)
}

const buildPNG = async (code: string, config?: Partial<PintoraConfig>) => {
const buf = await render({
code: code,
pintoraConfig: config,
mimeType: 'image/png',
width: 800,
backgroundColor: '#fdfdfd', // use some other background color
})
fs.writeFileSync('example.png', buf)
}

const code = `
activityDiagram
start
:render functionl called;
if (is mimeType image/svg+xml ?) then
:renderer svg;
:render with jsdom;
:generate string;
else (no)
:renderer canvas;
:render with node-canvas;
:generate image buffer by mimeType;
endif

:return result;

end
`

buildSVG(code)

buildPNG(code)
11 changes: 11 additions & 0 deletions packages/pintora-cli/examples/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"extends": "../../../tsconfig.json",
"compilerOptions": {
"rootDir": "src",
"outDir": "lib",
"module": "commonjs"
},
"references": [
{ "path": ".." },
]
}
2 changes: 2 additions & 0 deletions packages/pintora-cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@ export { render } from './render'

export { pintoraStandalone }

export type { PintoraConfig } from '@pintora/standalone'

export const setConfig = pintoraStandalone.setConfig
export const getConfig = pintoraStandalone.getConfig
1 change: 1 addition & 0 deletions packages/pintora-cli/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"outDir": "lib",
"module": "commonjs"
},
"exclude": ["examples", "lib"],
"references": [
{ "path": "../pintora-core" },
{ "path": "../pintora-renderer" },
Expand Down
70 changes: 68 additions & 2 deletions website/docs/advanced/api-usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ title: API Usage
sidebar_position: 1
---

## pintora API
## pintora API in the browser

Here is the API of `@pintora/standalone`.

Expand Down Expand Up @@ -119,7 +119,7 @@ const diagramEventManager: DiagramEventManager
Only the renders that happen after the event binding will trigger the event handler.

In the example below, only the `click` handler will be triggered because it is bound before the render.

```ts
const disposeClickListener = pintora.diagramEventManager.on('click', (item) => {
console.log('diagramEvent click', item)
Expand All @@ -131,3 +131,69 @@ const disposeMouseenterListener = pintora.diagramEventManager.on('mouseenter', (
console.log('diagramEvent mouseenter', item)
})
```

## pintora API in Node.js (@pintora/cli)

It's a little bit more complicated in Node.js than in the browser, we need to borrow some power from `jsdom` or `node-canvas`. Some wrapper functions are exported from the `@pintora/cli` package - the same one described in the [Cli Usage Doc](../getting-started/usage.mdx#cli), but this time we use it as a Node.js library.

### render(options)

```ts
export type CLIRenderOptions = {
code: string
devicePixelRatio?: number | null
mimeType?: string
backgroundColor?: string
pintoraConfig?: Partial<PintoraConfig>
width?: number
}

export function render(options: CLIRenderOptions): Promise<string | buffer>
```

Current supported `mimeType`

- image/svg+xml
- image/jpeg
- image/png

Some example code:

```ts title=nodejs-render-example.ts
import { render, PintoraConfig } from '@pintora/cli'
import * as fs from 'fs'

const buildPNG = async (code: string, config?: Partial<PintoraConfig>) => {
const buf = await render({
code: code,
pintoraConfig: config,
mimeType: 'image/png',
width: 800,
backgroundColor: '#fdfdfd', // use some other background color
})
fs.writeFileSync('example.png', buf)
}

const code = `
activityDiagram
start
:render functionl called;
if (is mimeType image/svg+xml ?) then
:renderer svg;
:render with jsdom;
:generate string;
else (no)
:renderer canvas;
:render with node-canvas;
:generate image buffer by mimeType;
endif

:return result;

end
`

buildSVG(code)

buildPNG(code)
```
Loading