From 2e028ce36d78c857693becedb2cac2549308a3ca Mon Sep 17 00:00:00 2001 From: Sidharth Vinod Date: Sun, 20 Nov 2022 00:38:35 +0530 Subject: [PATCH] chore: Unify `registerLazyLoadedDiagrams` --- cypress/platform/flow2.html | 46 +++++++++++++++++++ .../mermaid/src/diagram-api/detectType.ts | 6 ++- .../src/diagram-api/diagram-orchestration.ts | 36 ++++++++------- packages/mermaid/src/mermaid.ts | 15 +----- 4 files changed, 71 insertions(+), 32 deletions(-) create mode 100644 cypress/platform/flow2.html diff --git a/cypress/platform/flow2.html b/cypress/platform/flow2.html new file mode 100644 index 000000000..b23f00be6 --- /dev/null +++ b/cypress/platform/flow2.html @@ -0,0 +1,46 @@ + + + + + + +
+      graph TB
+      subgraph One
+        a1-->a2-->a3
+      end
+    
+
+      graph TB
+        a_a --> b_b:::apa --> c_c:::apa
+        classDef apa fill:#f9f,stroke:#333,stroke-width:4px;
+        class a_a apa;
+    
+
+      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"
+    
+ + + + diff --git a/packages/mermaid/src/diagram-api/detectType.ts b/packages/mermaid/src/diagram-api/detectType.ts index 1ea4f2b4d..c27f79b6c 100644 --- a/packages/mermaid/src/diagram-api/detectType.ts +++ b/packages/mermaid/src/diagram-api/detectType.ts @@ -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) => { diff --git a/packages/mermaid/src/diagram-api/diagram-orchestration.ts b/packages/mermaid/src/diagram-api/diagram-orchestration.ts index 64a7e5ad8..c6b8b579a 100644 --- a/packages/mermaid/src/diagram-api/diagram-orchestration.ts +++ b/packages/mermaid/src/diagram-api/diagram-orchestration.ts @@ -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 + ); }; diff --git a/packages/mermaid/src/mermaid.ts b/packages/mermaid/src/mermaid.ts index 3a8da77ad..268a830ec 100644 --- a/packages/mermaid/src/mermaid.ts +++ b/packages/mermaid/src/mermaid.ts @@ -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); }