fix: change sankey config types to be unions

Replace the TypeScript `enum {a = "a", b = "b"}` types with
TypeScript's literal types (e.g. `"a" | "b"`).

This is because TypeScript enums are
[_not_ a type-level addition to JavaScript][1], and even the official
TypeScript docs say to be careful when using.

[1]: https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#enums
This commit is contained in:
Alois Klink 2023-07-02 23:44:19 +01:00
parent 9c011abbd4
commit b989ff5362
3 changed files with 15 additions and 25 deletions

View File

@ -412,18 +412,8 @@ export interface FlowchartDiagramConfig extends BaseDiagramConfig {
wrappingWidth?: number;
}
export enum SankeyLinkColor {
source = 'source',
target = 'target',
gradient = 'gradient',
}
export enum SankeyNodeAlignment {
left = 'left',
right = 'right',
center = 'center',
justify = 'justify',
}
export type SankeyLinkColor = 'source' | 'target' | 'gradient';
export type SankeyNodeAlignment = 'left' | 'right' | 'center' | 'justify';
export interface SankeyDiagramConfig extends BaseDiagramConfig {
width?: number;

View File

@ -1,5 +1,5 @@
import theme from './themes/index.js';
import { MermaidConfig, SankeyLinkColor, SankeyNodeAlignment } from './config.type.js';
import { type MermaidConfig } from './config.type.js';
/**
* **Configuration methods in Mermaid version 8.6.0 have been updated, to learn more[[click
* here](8.6.0_docs.md)].**
@ -2273,8 +2273,8 @@ const config: Partial<MermaidConfig> = {
sankey: {
width: 800,
height: 400,
linkColor: SankeyLinkColor.gradient,
nodeAlignment: SankeyNodeAlignment.justify,
linkColor: 'gradient',
nodeAlignment: 'justify',
useMaxWidth: false,
},
fontSize: 16,

View File

@ -18,17 +18,17 @@ import {
} from 'd3-sankey';
import { configureSvgSize } from '../../setupGraphViewbox.js';
import { Uid } from '../../rendering-util/uid.js';
import { SankeyLinkColor, SankeyNodeAlignment } from '../../config.type.js';
import type { SankeyLinkColor, SankeyNodeAlignment } from '../../config.type.js';
// Map config options to alignment functions
const alignmentsMap: Record<
SankeyNodeAlignment,
(node: d3SankeyNode<object, object>, n: number) => number
> = {
[SankeyNodeAlignment.left]: d3SankeyLeft,
[SankeyNodeAlignment.right]: d3SankeyRight,
[SankeyNodeAlignment.center]: d3SankeyCenter,
[SankeyNodeAlignment.justify]: d3SankeyJustify,
left: d3SankeyLeft,
right: d3SankeyRight,
center: d3SankeyCenter,
justify: d3SankeyJustify,
};
/**
@ -157,9 +157,9 @@ export const draw = function (text: string, id: string, _version: string, diagOb
.attr('class', 'link')
.style('mix-blend-mode', 'multiply');
const linkColor = conf?.linkColor || SankeyLinkColor.gradient;
const linkColor = conf?.linkColor || 'gradient';
if (linkColor === SankeyLinkColor.gradient) {
if (linkColor === 'gradient') {
const gradient = link
.append('linearGradient')
.attr('id', (d: any) => (d.uid = Uid.next('linearGradient-')).id)
@ -180,13 +180,13 @@ export const draw = function (text: string, id: string, _version: string, diagOb
let coloring: any;
switch (linkColor) {
case SankeyLinkColor.gradient:
case 'gradient':
coloring = (d: any) => d.uid;
break;
case SankeyLinkColor.source:
case 'source':
coloring = (d: any) => colorScheme(d.source.id);
break;
case SankeyLinkColor.target:
case 'target':
coloring = (d: any) => colorScheme(d.target.id);
break;
default: