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

@@ -44,8 +44,10 @@ export const detectType = function (text: string, config?: MermaidConfig): strin
throw new Error(`No diagram type detected for text: ${text}`);
};
export const addDiagram = ({ id, detector, loader }: ExternalDiagramDefinition) => {
addDetector(id, detector, loader);
export const registerLazyLoadedDiagrams = (...diagrams: ExternalDiagramDefinition[]) => {
for (const { id, detector, loader } of diagrams) {
addDetector(id, detector, loader);
}
};
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 journey from '../diagrams/user-journey/journeyDetector';
import error from '../diagrams/error/errorDetector';
import { addDiagram } from './detectType';
import { registerLazyLoadedDiagrams } from './detectType';
let hasLoadedDiagrams = false;
export const addDiagrams = () => {
@@ -24,20 +24,22 @@ export const addDiagrams = () => {
// This is added here to avoid race-conditions.
// We could optimize the loading logic somehow.
hasLoadedDiagrams = true;
addDiagram(error);
addDiagram(c4);
addDiagram(classDiagram);
addDiagram(classDiagramV2);
addDiagram(er);
addDiagram(gantt);
addDiagram(info);
addDiagram(pie);
addDiagram(requirement);
addDiagram(sequence);
addDiagram(flowchart);
addDiagram(flowchartV2);
addDiagram(git);
addDiagram(state);
addDiagram(stateV2);
addDiagram(journey);
registerLazyLoadedDiagrams(
error,
c4,
classDiagram,
classDiagramV2,
er,
gantt,
info,
pie,
requirement,
sequence,
flowchart,
flowchartV2,
git,
state,
stateV2,
journey
);
};

View File

@@ -6,7 +6,7 @@ import type { MermaidConfig } from './config.type';
import { log } from './logger';
import utils from './utils';
import { mermaidAPI } from './mermaidAPI';
import { addDetector } from './diagram-api/detectType';
import { registerLazyLoadedDiagrams } from './diagram-api/detectType';
import { isDetailedError, type DetailedError } from './utils';
import { registerDiagram } from './diagram-api/diagramAPI';
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.
* @internal
@@ -333,7 +322,7 @@ const registerExternalDiagrams = async (
} = {}
) => {
if (lazyLoad) {
registerLazyLoadedDiagrams(diagrams);
registerLazyLoadedDiagrams(...diagrams);
} else {
await loadExternalDiagrams(diagrams);
}