@diplodoc/mermaid-extension 1.1.0 → 1.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +2 -2
- package/react/index.d.ts +6 -0
- package/react/index.js +52 -0
- package/react/index.js.map +7 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@diplodoc/mermaid-extension",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.1",
|
|
4
4
|
"description": "Mermaid plugin for Diplodoc transformer and builder",
|
|
5
5
|
"main": "plugin/index.js",
|
|
6
6
|
"types": "plugin/index.d.ts",
|
|
@@ -27,7 +27,7 @@
|
|
|
27
27
|
"files": [
|
|
28
28
|
"plugin",
|
|
29
29
|
"runtime",
|
|
30
|
-
"
|
|
30
|
+
"react"
|
|
31
31
|
],
|
|
32
32
|
"peerDependencies": {
|
|
33
33
|
"react": "^16.8.0 || ^17.0.0 || ^18.0.0"
|
package/react/index.d.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { InitConfig, RunOptions } from '../types';
|
|
2
|
+
export type RuntimeOptions = {
|
|
3
|
+
onError?: (error: any) => void;
|
|
4
|
+
};
|
|
5
|
+
export declare function MermaidRuntime(props: InitConfig & RunOptions & RuntimeOptions): null;
|
|
6
|
+
export declare function useMermaid(): (config: InitConfig, options?: RunOptions) => Promise<void>;
|
package/react/index.js
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
// src/react/index.ts
|
|
2
|
+
import { useEffect, useState, useCallback } from "react";
|
|
3
|
+
function pick(object, props) {
|
|
4
|
+
return Object.keys(object).reduce((acc, key) => {
|
|
5
|
+
if (props.includes(key)) {
|
|
6
|
+
acc[key] = object[key];
|
|
7
|
+
}
|
|
8
|
+
return acc;
|
|
9
|
+
}, {});
|
|
10
|
+
}
|
|
11
|
+
function omit(object, props) {
|
|
12
|
+
return Object.keys(object).reduce((acc, key) => {
|
|
13
|
+
if (!props.includes(key)) {
|
|
14
|
+
acc[key] = object[key];
|
|
15
|
+
}
|
|
16
|
+
return acc;
|
|
17
|
+
}, {});
|
|
18
|
+
}
|
|
19
|
+
function MermaidRuntime(props) {
|
|
20
|
+
const renderMermaid = useMermaid();
|
|
21
|
+
const config = omit(props, ["querySelector", "nodes", "onError"]);
|
|
22
|
+
const options = pick(props, ["querySelector", "nodes"]);
|
|
23
|
+
useEffect(() => {
|
|
24
|
+
renderMermaid(config, options).catch(props.onError || (() => {
|
|
25
|
+
}));
|
|
26
|
+
});
|
|
27
|
+
return null;
|
|
28
|
+
}
|
|
29
|
+
function useMermaid() {
|
|
30
|
+
const [mermaid, setMermaid] = useState(null);
|
|
31
|
+
const render = useCallback(async (config, options) => {
|
|
32
|
+
if (mermaid) {
|
|
33
|
+
mermaid.initialize(config);
|
|
34
|
+
return mermaid.run(options);
|
|
35
|
+
}
|
|
36
|
+
}, [mermaid]);
|
|
37
|
+
useEffect(() => {
|
|
38
|
+
(window.mermaidJsonp = window.mermaidJsonp || []).push(setMermaid);
|
|
39
|
+
return () => {
|
|
40
|
+
const index = window.mermaidJsonp.indexOf(setMermaid);
|
|
41
|
+
if (index > -1) {
|
|
42
|
+
window.mermaidJsonp.splice(index, 1);
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
}, []);
|
|
46
|
+
return render;
|
|
47
|
+
}
|
|
48
|
+
export {
|
|
49
|
+
MermaidRuntime,
|
|
50
|
+
useMermaid
|
|
51
|
+
};
|
|
52
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../src/react/index.ts"],
|
|
4
|
+
"sourcesContent": ["import type { InitConfig, RunOptions, ZoomOptions } from '../types';\nimport { useEffect, useState, useCallback } from 'react';\n\nexport type RuntimeOptions = {\n onError?: (error: any) => void\n}\n\nfunction pick<O extends Record<string, any>, P extends string, R extends {\n [K in P]: O[K];\n}>(object: O, props: readonly P[]): R {\n return Object.keys(object).reduce((acc, key) => {\n if ((props as readonly string[]).includes(key)) {\n acc[key] = object[key];\n }\n\n return acc;\n }, {} as Record<string, any>) as R;\n}\n\nfunction omit<O extends Record<string, any>, P extends string, R extends {\n [K in keyof Omit<O, P>]: O[K];\n}>(object: O, props: readonly P[]): R {\n return Object.keys(object).reduce((acc, key) => {\n if (!(props as readonly string[]).includes(key)) {\n acc[key] = object[key];\n }\n\n return acc;\n }, {} as Record<string, any>) as R;\n}\n\nexport function MermaidRuntime(props: InitConfig & RunOptions & RuntimeOptions) {\n const renderMermaid = useMermaid();\n const config = omit(props, [ 'querySelector', 'nodes', 'onError' ]);\n const options = pick(props, [ 'querySelector', 'nodes' ]);\n\n useEffect(() => {\n renderMermaid(config, options).catch(props.onError || (() => {}));\n });\n\n return null;\n}\n\nexport function useMermaid() {\n const [ mermaid, setMermaid ] = useState<Parameters<Callback>[0] | null>(null);\n const render = useCallback(async (config: InitConfig, options?: RunOptions) => {\n if (mermaid) {\n mermaid.initialize(config);\n return mermaid.run(options);\n }\n }, [ mermaid ]);\n\n useEffect(() => {\n (window.mermaidJsonp = window.mermaidJsonp || []).push(setMermaid);\n\n return () => {\n const index = window.mermaidJsonp.indexOf(setMermaid);\n if (index > -1) {\n window.mermaidJsonp.splice(index, 1);\n }\n };\n }, []);\n\n return render;\n}\n"],
|
|
5
|
+
"mappings": ";AACA,SAAS,WAAW,UAAU,mBAAmB;AAMjD,SAAS,KAEN,QAAW,OAAwB;AAClC,SAAO,OAAO,KAAK,MAAM,EAAE,OAAO,CAAC,KAAK,QAAQ;AAC5C,QAAK,MAA4B,SAAS,GAAG,GAAG;AAC5C,UAAI,GAAG,IAAI,OAAO,GAAG;AAAA,IACzB;AAEA,WAAO;AAAA,EACX,GAAG,CAAC,CAAwB;AAChC;AAEA,SAAS,KAEN,QAAW,OAAwB;AAClC,SAAO,OAAO,KAAK,MAAM,EAAE,OAAO,CA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