mirror of
https://github.com/mermaid-js/mermaid.git
synced 2025-01-21 06:53:17 +08:00
add Required
and RequiredDeep
to pieDb, add config functions
This commit is contained in:
parent
a92571d588
commit
c894c1f5b5
@ -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<PieDiagramConfig> = {
|
||||
useMaxWidth: true,
|
||||
useWidth: 1200,
|
||||
textPosition: 0.75,
|
||||
} as const;
|
||||
|
||||
export const DEFAULT_PIE_DB: PieFields = {
|
||||
export const DEFAULT_PIE_DB: RequiredDeep<PieFields> = {
|
||||
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<PieDiagramConfig> = {
|
||||
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<PieDiagramConfig> => 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,
|
||||
|
@ -47,18 +47,25 @@ export interface D3Sections {
|
||||
}
|
||||
|
||||
export interface PieDb extends DiagramDB {
|
||||
clear: () => void;
|
||||
getConfig: () => PieDiagramConfig | undefined;
|
||||
// config
|
||||
setConfig: (config: PieDiagramConfig) => void;
|
||||
getConfig: () => Required<PieDiagramConfig>;
|
||||
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;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user