mirror of
https://github.com/mermaid-js/mermaid.git
synced 2025-08-21 17:26:45 +02:00
refactor: improve types of shapes
Instead of being a `Record<string, ShapeHandler>`, the type now knows every key in the variable.
This commit is contained in:
@@ -84,7 +84,7 @@ describe('Test Alias for shapes', function () {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('should support alias for shadedProcess shape ', function () {
|
it('should support alias for shadedProcess shape ', function () {
|
||||||
const aliases = ['lined-process', 'lined-rectangle', 'lin-proc', 'lin-rect'];
|
const aliases = ['lined-process', 'lined-rectangle', 'lin-proc', 'lin-rect'] as const;
|
||||||
for (const alias of aliases) {
|
for (const alias of aliases) {
|
||||||
expect(shapes[alias]).toBe(shapes['shaded-process']);
|
expect(shapes[alias]).toBe(shapes['shaded-process']);
|
||||||
}
|
}
|
||||||
|
@@ -1,3 +1,4 @@
|
|||||||
|
import type { Entries } from 'type-fest';
|
||||||
import type { Node, ShapeRenderOptions } from '../types.js';
|
import type { Node, ShapeRenderOptions } from '../types.js';
|
||||||
import { anchor } from './shapes/anchor.js';
|
import { anchor } from './shapes/anchor.js';
|
||||||
import { bowTieRect } from './shapes/bowTieRect.js';
|
import { bowTieRect } from './shapes/bowTieRect.js';
|
||||||
@@ -75,7 +76,7 @@ export interface ShapeDefinition {
|
|||||||
handler: ShapeHandler;
|
handler: ShapeHandler;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const shapesDefs: ShapeDefinition[] = [
|
export const shapesDefs = [
|
||||||
{
|
{
|
||||||
semanticName: 'Process',
|
semanticName: 'Process',
|
||||||
name: 'Rectangle',
|
name: 'Rectangle',
|
||||||
@@ -442,11 +443,11 @@ export const shapesDefs: ShapeDefinition[] = [
|
|||||||
aliases: ['lined-document'],
|
aliases: ['lined-document'],
|
||||||
handler: linedWaveEdgedRect,
|
handler: linedWaveEdgedRect,
|
||||||
},
|
},
|
||||||
];
|
] as const satisfies ShapeDefinition[];
|
||||||
|
|
||||||
const generateShapeMap = () => {
|
const generateShapeMap = () => {
|
||||||
// These are the shapes that didn't have documentation present
|
// These are the shapes that didn't have documentation present
|
||||||
const shapeMap: Record<string, ShapeHandler> = {
|
const undocumentedShapes = {
|
||||||
// States
|
// States
|
||||||
state,
|
state,
|
||||||
choice,
|
choice,
|
||||||
@@ -464,18 +465,25 @@ const generateShapeMap = () => {
|
|||||||
imageSquare,
|
imageSquare,
|
||||||
|
|
||||||
anchor,
|
anchor,
|
||||||
};
|
} as const;
|
||||||
|
|
||||||
for (const shape of shapesDefs) {
|
const entries = [
|
||||||
for (const alias of [
|
...(Object.entries(undocumentedShapes) as Entries<typeof undocumentedShapes>),
|
||||||
shape.shortName,
|
...shapesDefs.flatMap((shape) => {
|
||||||
...(shape.aliases ?? []),
|
const aliases = [
|
||||||
...(shape.internalAliases ?? []),
|
shape.shortName,
|
||||||
]) {
|
...('aliases' in shape ? shape.aliases : []),
|
||||||
shapeMap[alias] = shape.handler;
|
...('internalAliases' in shape ? shape.internalAliases : []),
|
||||||
}
|
];
|
||||||
}
|
return aliases.map((alias) => [alias, shape.handler] as const);
|
||||||
return shapeMap;
|
}),
|
||||||
|
];
|
||||||
|
return Object.fromEntries(entries) as Record<
|
||||||
|
(typeof entries)[number][0],
|
||||||
|
(typeof entries)[number][1]
|
||||||
|
> satisfies Record<string, ShapeHandler>;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const shapes = generateShapeMap();
|
export const shapes = generateShapeMap();
|
||||||
|
|
||||||
|
export type ShapeID = keyof typeof shapes;
|
||||||
|
Reference in New Issue
Block a user