From 4492c5ed4e29e3ef05d2f938cbebc7c884f8b4d4 Mon Sep 17 00:00:00 2001 From: Sidharth Vinod Date: Fri, 18 Nov 2022 16:09:36 +0530 Subject: [PATCH] chore(split): classDiagram --- .../src/diagrams/class/classDetector-V2.ts | 19 ++++++++++++++++-- .../src/diagrams/class/classDetector.ts | 19 ++++++++++++++++-- .../src/diagrams/class/classDiagram-v2.ts | 20 +++++++++++++++++++ .../src/diagrams/class/classDiagram.ts | 20 +++++++++++++++++++ 4 files changed, 74 insertions(+), 4 deletions(-) create mode 100644 packages/mermaid/src/diagrams/class/classDiagram-v2.ts create mode 100644 packages/mermaid/src/diagrams/class/classDiagram.ts diff --git a/packages/mermaid/src/diagrams/class/classDetector-V2.ts b/packages/mermaid/src/diagrams/class/classDetector-V2.ts index d65caf9a8..51b93fb0d 100644 --- a/packages/mermaid/src/diagrams/class/classDetector-V2.ts +++ b/packages/mermaid/src/diagrams/class/classDetector-V2.ts @@ -1,9 +1,24 @@ -import type { DiagramDetector } from '../../diagram-api/types'; +import type { DiagramDetector, ExternalDiagramDefinition } from '../../diagram-api/types'; -export const classDetectorV2: DiagramDetector = (txt, config) => { +const id = 'classDiagram'; + +const detector: DiagramDetector = (txt, config) => { // If we have confgured to use dagre-wrapper then we should return true in this function for classDiagram code thus making it use the new class diagram if (txt.match(/^\s*classDiagram/) !== null && config?.class?.defaultRenderer === 'dagre-wrapper') return true; // We have not opted to use the new renderer so we should return true if we detect a class diagram return txt.match(/^\s*classDiagram-v2/) !== null; }; + +const loader = async () => { + const { diagram } = await import('./classDiagram-v2'); + return { id, diagram }; +}; + +const plugin: ExternalDiagramDefinition = { + id, + detector, + loader, +}; + +export default plugin; diff --git a/packages/mermaid/src/diagrams/class/classDetector.ts b/packages/mermaid/src/diagrams/class/classDetector.ts index ef6389a60..dc2f4261f 100644 --- a/packages/mermaid/src/diagrams/class/classDetector.ts +++ b/packages/mermaid/src/diagrams/class/classDetector.ts @@ -1,8 +1,23 @@ -import type { DiagramDetector } from '../../diagram-api/types'; +import type { DiagramDetector, ExternalDiagramDefinition } from '../../diagram-api/types'; -export const classDetector: DiagramDetector = (txt, config) => { +const id = 'class'; + +const detector: DiagramDetector = (txt, config) => { // If we have confgured to use dagre-wrapper then we should never return true in this function if (config?.class?.defaultRenderer === 'dagre-wrapper') return false; // We have not opted to use the new renderer so we should return true if we detect a class diagram return txt.match(/^\s*classDiagram/) !== null; }; + +const loader = async () => { + const { diagram } = await import('./classDiagram'); + return { id, diagram }; +}; + +const plugin: ExternalDiagramDefinition = { + id, + detector, + loader, +}; + +export default plugin; diff --git a/packages/mermaid/src/diagrams/class/classDiagram-v2.ts b/packages/mermaid/src/diagrams/class/classDiagram-v2.ts new file mode 100644 index 000000000..2f0bbf371 --- /dev/null +++ b/packages/mermaid/src/diagrams/class/classDiagram-v2.ts @@ -0,0 +1,20 @@ +import { DiagramDefinition } from '../../diagram-api/types'; +// @ts-ignore: TODO Fix ts errors +import parser from './parser/classDiagram'; +import db from './classDb'; +import styles from './styles'; +import renderer from './classRenderer-v2'; + +export const diagram: DiagramDefinition = { + parser, + db, + renderer, + styles, + init: (cnf) => { + if (!cnf.class) { + cnf.class = {}; + } + cnf.class.arrowMarkerAbsolute = cnf.arrowMarkerAbsolute; + db.clear(); + }, +}; diff --git a/packages/mermaid/src/diagrams/class/classDiagram.ts b/packages/mermaid/src/diagrams/class/classDiagram.ts new file mode 100644 index 000000000..250d04176 --- /dev/null +++ b/packages/mermaid/src/diagrams/class/classDiagram.ts @@ -0,0 +1,20 @@ +import { DiagramDefinition } from '../../diagram-api/types'; +// @ts-ignore: TODO Fix ts errors +import parser from './parser/classDiagram'; +import db from './classDb'; +import styles from './styles'; +import renderer from './classRenderer'; + +export const diagram: DiagramDefinition = { + parser, + db, + renderer, + styles, + init: (cnf) => { + if (!cnf.class) { + cnf.class = {}; + } + cnf.class.arrowMarkerAbsolute = cnf.arrowMarkerAbsolute; + db.clear(); + }, +};