chore: Unify registerLazyLoadedDiagrams

This commit is contained in:
Sidharth Vinod
2022-11-20 00:38:35 +05:30
parent 0854bab124
commit 2e028ce36d
4 changed files with 71 additions and 32 deletions

View File

@@ -0,0 +1,46 @@
<html>
<head>
<link href="https://fonts.googleapis.com/css?family=Montserrat&display=swap" rel="stylesheet" />
<style>
body {
font-family: 'trebuchet ms', verdana, arial;
}
</style>
</head>
<body>
<pre class="mermaid">
graph TB
subgraph One
a1-->a2-->a3
end
</pre>
<pre class="mermaid">
graph TB
a_a --> b_b:::apa --> c_c:::apa
classDef apa fill:#f9f,stroke:#333,stroke-width:4px;
class a_a apa;
</pre>
<pre class="mermaid">
graph TB
a_a(Aftonbladet) --> b_b[gorilla]:::apa --> c_c{chimp}:::apa -->a_a
a_a --> c --> d_d --> c_c
classDef apa fill:#f9f,stroke:#333,stroke-width:4px;
class a_a apa;
click a_a "http://www.aftonbladet.se" "apa"
</pre>
<script type="module">
import mermaid from '../../packages/mermaid/src/mermaid';
mermaid.initialize({
theme: 'forest',
// themeCSS: '.node rect { fill: red; }',
logLevel: 3,
flowchart: { curve: 'linear' },
gantt: { axisFormat: '%m/%d/%Y' },
sequence: { actorMargin: 50 },
// sequenceDiagram: { actorMargin: 300 } // deprecated
});
</script>
</body>
</html>

View File

@@ -44,8 +44,10 @@ export const detectType = function (text: string, config?: MermaidConfig): strin
throw new Error(`No diagram type detected for text: ${text}`); throw new Error(`No diagram type detected for text: ${text}`);
}; };
export const addDiagram = ({ id, detector, loader }: ExternalDiagramDefinition) => { export const registerLazyLoadedDiagrams = (...diagrams: ExternalDiagramDefinition[]) => {
addDetector(id, detector, loader); for (const { id, detector, loader } of diagrams) {
addDetector(id, detector, loader);
}
}; };
export const addDetector = (key: string, detector: DiagramDetector, loader?: DiagramLoader) => { export const addDetector = (key: string, detector: DiagramDetector, loader?: DiagramLoader) => {

View File

@@ -14,7 +14,7 @@ import state from '../diagrams/state/stateDetector';
import stateV2 from '../diagrams/state/stateDetector-V2'; import stateV2 from '../diagrams/state/stateDetector-V2';
import journey from '../diagrams/user-journey/journeyDetector'; import journey from '../diagrams/user-journey/journeyDetector';
import error from '../diagrams/error/errorDetector'; import error from '../diagrams/error/errorDetector';
import { addDiagram } from './detectType'; import { registerLazyLoadedDiagrams } from './detectType';
let hasLoadedDiagrams = false; let hasLoadedDiagrams = false;
export const addDiagrams = () => { export const addDiagrams = () => {
@@ -24,20 +24,22 @@ export const addDiagrams = () => {
// This is added here to avoid race-conditions. // This is added here to avoid race-conditions.
// We could optimize the loading logic somehow. // We could optimize the loading logic somehow.
hasLoadedDiagrams = true; hasLoadedDiagrams = true;
addDiagram(error); registerLazyLoadedDiagrams(
addDiagram(c4); error,
addDiagram(classDiagram); c4,
addDiagram(classDiagramV2); classDiagram,
addDiagram(er); classDiagramV2,
addDiagram(gantt); er,
addDiagram(info); gantt,
addDiagram(pie); info,
addDiagram(requirement); pie,
addDiagram(sequence); requirement,
addDiagram(flowchart); sequence,
addDiagram(flowchartV2); flowchart,
addDiagram(git); flowchartV2,
addDiagram(state); git,
addDiagram(stateV2); state,
addDiagram(journey); stateV2,
journey
);
}; };

View File

@@ -6,7 +6,7 @@ import type { MermaidConfig } from './config.type';
import { log } from './logger'; import { log } from './logger';
import utils from './utils'; import utils from './utils';
import { mermaidAPI } from './mermaidAPI'; import { mermaidAPI } from './mermaidAPI';
import { addDetector } from './diagram-api/detectType'; import { registerLazyLoadedDiagrams } from './diagram-api/detectType';
import { isDetailedError, type DetailedError } from './utils'; import { isDetailedError, type DetailedError } from './utils';
import { registerDiagram } from './diagram-api/diagramAPI'; import { registerDiagram } from './diagram-api/diagramAPI';
import { ExternalDiagramDefinition } from './diagram-api/types'; import { ExternalDiagramDefinition } from './diagram-api/types';
@@ -175,17 +175,6 @@ const initThrowsErrors = function (
} }
}; };
/**
* This is an internal function and should not be made public, as it will likely change.
* @internal
* @param diagrams - Array of {@link ExternalDiagramDefinition}.
*/
const registerLazyLoadedDiagrams = (diagrams: ExternalDiagramDefinition[]) => {
for (const { id, detector, loader } of diagrams) {
addDetector(id, detector, loader);
}
};
/** /**
* This is an internal function and should not be made public, as it will likely change. * This is an internal function and should not be made public, as it will likely change.
* @internal * @internal
@@ -333,7 +322,7 @@ const registerExternalDiagrams = async (
} = {} } = {}
) => { ) => {
if (lazyLoad) { if (lazyLoad) {
registerLazyLoadedDiagrams(diagrams); registerLazyLoadedDiagrams(...diagrams);
} else { } else {
await loadExternalDiagrams(diagrams); await loadExternalDiagrams(diagrams);
} }