From 07f6358a804900073fee7e69c8430b7ea6f69ca7 Mon Sep 17 00:00:00 2001 From: Yokozuna59 Date: Sat, 24 Jun 2023 22:18:00 +0300 Subject: [PATCH 1/6] fix not rendered style when style is optional --- packages/mermaid/src/diagram-api/diagramAPI.ts | 5 +---- packages/mermaid/src/styles.ts | 6 ++++-- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/packages/mermaid/src/diagram-api/diagramAPI.ts b/packages/mermaid/src/diagram-api/diagramAPI.ts index 457dd673ba..7e89d9cd79 100644 --- a/packages/mermaid/src/diagram-api/diagramAPI.ts +++ b/packages/mermaid/src/diagram-api/diagramAPI.ts @@ -7,7 +7,6 @@ import { addStylesForDiagram } from '../styles.js'; import { DiagramDefinition, DiagramDetector } from './types.js'; import * as _commonDb from '../commonDb.js'; import { parseDirective as _parseDirective } from '../directiveUtils.js'; -import isEmpty from 'lodash-es/isEmpty.js'; /* Packaging and exposing resources for external diagrams so that they can import @@ -51,9 +50,7 @@ export const registerDiagram = ( if (detector) { addDetector(id, detector); } - if (!isEmpty(diagram.styles)) { - addStylesForDiagram(id, diagram.styles); - } + addStylesForDiagram(id, diagram.styles); if (diagram.injectUtils) { diagram.injectUtils( diff --git a/packages/mermaid/src/styles.ts b/packages/mermaid/src/styles.ts index a6e7524756..74322f678a 100644 --- a/packages/mermaid/src/styles.ts +++ b/packages/mermaid/src/styles.ts @@ -73,8 +73,10 @@ const getStyles = ( `; }; -export const addStylesForDiagram = (type: string, diagramTheme: unknown): void => { - themes[type] = diagramTheme; +export const addStylesForDiagram = (type: string, diagramTheme?: unknown): void => { + if (diagramTheme !== undefined) { + themes[type] = diagramTheme; + } }; export default getStyles; From f2c40271b27d4d44fb16d4bfc907ee08fda50baa Mon Sep 17 00:00:00 2001 From: Yokozuna59 Date: Mon, 26 Jun 2023 18:02:20 +0300 Subject: [PATCH 2/6] change `diagramTheme` and `themes` to return `string` --- packages/mermaid/src/styles.ts | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/packages/mermaid/src/styles.ts b/packages/mermaid/src/styles.ts index 74322f678a..f924a4f1fb 100644 --- a/packages/mermaid/src/styles.ts +++ b/packages/mermaid/src/styles.ts @@ -1,7 +1,7 @@ import type { FlowChartStyleOptions } from './diagrams/flowchart/styles.js'; import { log } from './logger.js'; -const themes: Record = {}; +const themes: Record string> = {}; const getStyles = ( type: string, @@ -73,9 +73,12 @@ const getStyles = ( `; }; -export const addStylesForDiagram = (type: string, diagramTheme?: unknown): void => { +export const addStylesForDiagram = ( + type: string, + diagramTheme?: (options?: any) => string +): void => { if (diagramTheme !== undefined) { - themes[type] = diagramTheme; + themes[type] = (options) => diagramTheme(options); } }; From f020f4a292cccf570127c2ed236f5453149262b2 Mon Sep 17 00:00:00 2001 From: Yokozuna59 Date: Mon, 26 Jun 2023 19:12:04 +0300 Subject: [PATCH 3/6] remove `diagramTheme` arrow function return Co-authored-by: Sidharth Vinod --- packages/mermaid/src/styles.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/mermaid/src/styles.ts b/packages/mermaid/src/styles.ts index f924a4f1fb..2f19884198 100644 --- a/packages/mermaid/src/styles.ts +++ b/packages/mermaid/src/styles.ts @@ -78,7 +78,7 @@ export const addStylesForDiagram = ( diagramTheme?: (options?: any) => string ): void => { if (diagramTheme !== undefined) { - themes[type] = (options) => diagramTheme(options); + themes[type] = diagramTheme; } }; From 3106ddbd1ec0d8c065479cf700e06e47b65f81cd Mon Sep 17 00:00:00 2001 From: Yokozuna59 Date: Mon, 26 Jun 2023 19:26:56 +0300 Subject: [PATCH 4/6] add `getDiagramStyles` type and use it in `style.ts` --- packages/mermaid/src/diagram-api/types.ts | 2 ++ packages/mermaid/src/styles.ts | 12 +++++------- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/packages/mermaid/src/diagram-api/types.ts b/packages/mermaid/src/diagram-api/types.ts index 265af65874..ab82e7e74e 100644 --- a/packages/mermaid/src/diagram-api/types.ts +++ b/packages/mermaid/src/diagram-api/types.ts @@ -82,3 +82,5 @@ export type ParseDirectiveDefinition = (statement: string, context: string, type export type HTML = d3.Selection; export type SVG = d3.Selection; + +export type getDiagramStyles = (options?: unknown) => string; diff --git a/packages/mermaid/src/styles.ts b/packages/mermaid/src/styles.ts index 2f19884198..001bd1794d 100644 --- a/packages/mermaid/src/styles.ts +++ b/packages/mermaid/src/styles.ts @@ -1,7 +1,8 @@ import type { FlowChartStyleOptions } from './diagrams/flowchart/styles.js'; import { log } from './logger.js'; +import { getDiagramStyles } from './diagram-api/types.js'; -const themes: Record string> = {}; +const themes: Record = {}; const getStyles = ( type: string, @@ -73,12 +74,9 @@ const getStyles = ( `; }; -export const addStylesForDiagram = ( - type: string, - diagramTheme?: (options?: any) => string -): void => { - if (diagramTheme !== undefined) { - themes[type] = diagramTheme; +export const addStylesForDiagram = (type: string, getDiagramStyles?: getDiagramStyles): void => { + if (getDiagramStyles !== undefined) { + themes[type] = getDiagramStyles; } }; From 8b67200717607451b03e2357ca72c9577a5121b2 Mon Sep 17 00:00:00 2001 From: Yokozuna59 Date: Mon, 26 Jun 2023 19:31:53 +0300 Subject: [PATCH 5/6] change `options` paramter to any instead of `unknown` --- packages/mermaid/src/diagram-api/types.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/mermaid/src/diagram-api/types.ts b/packages/mermaid/src/diagram-api/types.ts index ab82e7e74e..67730e33ca 100644 --- a/packages/mermaid/src/diagram-api/types.ts +++ b/packages/mermaid/src/diagram-api/types.ts @@ -83,4 +83,4 @@ export type HTML = d3.Selection; export type SVG = d3.Selection; -export type getDiagramStyles = (options?: unknown) => string; +export type getDiagramStyles = (options?: any) => string; From 64a28be5e3930d59bc35a1efb857194ecdd91813 Mon Sep 17 00:00:00 2001 From: Yokozuna59 Date: Mon, 26 Jun 2023 20:34:21 +0300 Subject: [PATCH 6/6] rename `getDiagramStyles` type into `DiagramStylesProvider` --- packages/mermaid/src/diagram-api/types.ts | 2 +- packages/mermaid/src/styles.ts | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/packages/mermaid/src/diagram-api/types.ts b/packages/mermaid/src/diagram-api/types.ts index 67730e33ca..3b8d004563 100644 --- a/packages/mermaid/src/diagram-api/types.ts +++ b/packages/mermaid/src/diagram-api/types.ts @@ -83,4 +83,4 @@ export type HTML = d3.Selection; export type SVG = d3.Selection; -export type getDiagramStyles = (options?: any) => string; +export type DiagramStylesProvider = (options?: any) => string; diff --git a/packages/mermaid/src/styles.ts b/packages/mermaid/src/styles.ts index 001bd1794d..fde079450c 100644 --- a/packages/mermaid/src/styles.ts +++ b/packages/mermaid/src/styles.ts @@ -1,8 +1,8 @@ import type { FlowChartStyleOptions } from './diagrams/flowchart/styles.js'; import { log } from './logger.js'; -import { getDiagramStyles } from './diagram-api/types.js'; +import type { DiagramStylesProvider } from './diagram-api/types.js'; -const themes: Record = {}; +const themes: Record = {}; const getStyles = ( type: string, @@ -74,9 +74,9 @@ const getStyles = ( `; }; -export const addStylesForDiagram = (type: string, getDiagramStyles?: getDiagramStyles): void => { - if (getDiagramStyles !== undefined) { - themes[type] = getDiagramStyles; +export const addStylesForDiagram = (type: string, diagramTheme?: DiagramStylesProvider): void => { + if (diagramTheme !== undefined) { + themes[type] = diagramTheme; } };