From c894c1f5b5b5d7cbf21368ae118310ce239b5a78 Mon Sep 17 00:00:00 2001 From: Yokozuna59 Date: Mon, 19 Jun 2023 17:45:38 +0300 Subject: [PATCH] add `Required` and `RequiredDeep` to pieDb, add config functions --- packages/mermaid/src/diagrams/pie/pieDb.ts | 55 +++++++++++++------ packages/mermaid/src/diagrams/pie/pieTypes.ts | 13 ++++- 2 files changed, 47 insertions(+), 21 deletions(-) diff --git a/packages/mermaid/src/diagrams/pie/pieDb.ts b/packages/mermaid/src/diagrams/pie/pieDb.ts index 027356b21..7e8e39f06 100644 --- a/packages/mermaid/src/diagrams/pie/pieDb.ts +++ b/packages/mermaid/src/diagrams/pie/pieDb.ts @@ -1,6 +1,6 @@ import { log } from '../../logger.js'; import { parseDirective as _parseDirective } from '../../directiveUtils.js'; -import { getConfig } from '../../config.js'; +import { getConfig as commonGetConfig } from '../../config.js'; import { sanitizeText } from '../common/common.js'; import { setAccTitle, @@ -13,14 +13,15 @@ import { } from '../../commonDb.js'; import type { ParseDirectiveDefinition } from '../../diagram-api/types.js'; import type { PieFields, PieDb, Sections, PieDiagramConfig } from './pieTypes.js'; +import type { RequiredDeep } from 'type-fest'; -export const DEFAULT_PIE_CONFIG: PieDiagramConfig = { +export const DEFAULT_PIE_CONFIG: Required = { useMaxWidth: true, useWidth: 1200, textPosition: 0.75, } as const; -export const DEFAULT_PIE_DB: PieFields = { +export const DEFAULT_PIE_DB: RequiredDeep = { sections: {}, showData: false, config: DEFAULT_PIE_CONFIG, @@ -28,18 +29,38 @@ export const DEFAULT_PIE_DB: PieFields = { let sections: Sections = DEFAULT_PIE_DB.sections; let showData: boolean = DEFAULT_PIE_DB.showData; -const config: PieDiagramConfig = { +const config: Required = { useWidth: DEFAULT_PIE_DB.config.useWidth, useMaxWidth: DEFAULT_PIE_DB.config.useMaxWidth, textPosition: DEFAULT_PIE_DB.config.textPosition, }; -export const parseDirective: ParseDirectiveDefinition = (statement, context, type) => { +const setConfig = (conf: PieDiagramConfig): void => { + config.useWidth = conf.useWidth ?? DEFAULT_PIE_CONFIG.useWidth; + config.useMaxWidth = conf.useMaxWidth ?? DEFAULT_PIE_CONFIG.useMaxWidth; + config.textPosition = conf.textPosition ?? DEFAULT_PIE_CONFIG.textPosition; +}; + +const getConfig = (): Required => config; + +const reset = (): void => { + config.useWidth = DEFAULT_PIE_CONFIG.useWidth; + config.useMaxWidth = DEFAULT_PIE_CONFIG.useMaxWidth; + config.textPosition = DEFAULT_PIE_CONFIG.textPosition; +}; + +const parseDirective: ParseDirectiveDefinition = (statement, context, type) => { _parseDirective(this, statement, context, type); }; +const clear = (): void => { + sections = JSON.parse(JSON.stringify(DEFAULT_PIE_DB.sections)); + showData = DEFAULT_PIE_DB.showData; + commonClear(); +}; + const addSection = (label: string, value: number): void => { - label = sanitizeText(label, getConfig()); + label = sanitizeText(label, commonGetConfig()); if (sections[label] === undefined) { sections[label] = value; log.debug(`added new section: ${label}, with value: ${value}`); @@ -48,12 +69,6 @@ const addSection = (label: string, value: number): void => { const getSections = (): Sections => sections; -const setShowData = (toggle: boolean): void => { - showData = toggle; -}; - -const getShowData = (): boolean => showData; - const cleanupValue = (value: string): number => { if (value.substring(0, 1) === ':') { value = value.substring(1).trim(); @@ -63,22 +78,26 @@ const cleanupValue = (value: string): number => { } }; -const clear = (): void => { - sections = JSON.parse(JSON.stringify(DEFAULT_PIE_DB.sections)); - showData = DEFAULT_PIE_DB.showData; - commonClear(); +const setShowData = (toggle: boolean): void => { + showData = toggle; }; +const getShowData = (): boolean => showData; + export const db: PieDb = { - clear, - getConfig: () => getConfig().pie, + setConfig, + getConfig, + reset, + parseDirective, + clear, setDiagramTitle, getDiagramTitle, setAccTitle, getAccTitle, setAccDescription, getAccDescription, + addSection, getSections, cleanupValue, diff --git a/packages/mermaid/src/diagrams/pie/pieTypes.ts b/packages/mermaid/src/diagrams/pie/pieTypes.ts index 7c88e41c4..8e8826be3 100644 --- a/packages/mermaid/src/diagrams/pie/pieTypes.ts +++ b/packages/mermaid/src/diagrams/pie/pieTypes.ts @@ -47,18 +47,25 @@ export interface D3Sections { } export interface PieDb extends DiagramDB { - clear: () => void; - getConfig: () => PieDiagramConfig | undefined; + // config + setConfig: (config: PieDiagramConfig) => void; + getConfig: () => Required; + reset: () => void; + + // common db parseDirective: ParseDirectiveDefinition; + clear: () => void; setDiagramTitle: (title: string) => void; getDiagramTitle: () => string; setAccTitle: (title: string) => void; getAccTitle: () => string; setAccDescription: (describetion: string) => void; getAccDescription: () => string; + + // diagram db addSection: (label: string, value: number) => void; - cleanupValue: (value: string) => number; getSections: () => Sections; + cleanupValue: (value: string) => number; setShowData: (toggle: boolean) => void; getShowData: () => boolean; }