feat: Inject internal helpers into render function

This commit is contained in:
Sidharth Vinod 2024-08-21 17:22:21 +05:30
parent c5be9fd882
commit f6e1515f66
No known key found for this signature in database
GPG Key ID: FB5CCD378D3907CD
3 changed files with 861 additions and 854 deletions

View File

@ -1,9 +1,19 @@
import { curveLinear } from 'd3'; import { curveLinear } from 'd3';
import ELK from 'elkjs/lib/elk.bundled.js'; import ELK from 'elkjs/lib/elk.bundled.js';
import mermaid, { type LayoutData } from 'mermaid'; import type { InternalHelpers, LayoutData } from 'mermaid';
import { type TreeData, findCommonAncestor } from './find-common-ancestor.js'; import { type TreeData, findCommonAncestor } from './find-common-ancestor.js';
const { export const render = async (
data4Layout: LayoutData,
svg: {
insert: (arg0: string) => {
(): any;
new (): any;
attr: { (arg0: string, arg1: string): any; new (): any };
};
},
element: any,
{
common, common,
getConfig, getConfig,
insertCluster, insertCluster,
@ -15,8 +25,9 @@ const {
labelHelper, labelHelper,
log, log,
positionEdgeLabel, positionEdgeLabel,
} = mermaid.internalHelpers; }: InternalHelpers,
algorithm: any
) => {
const nodeDb: Record<string, any> = {}; const nodeDb: Record<string, any> = {};
const portPos: Record<string, any> = {}; const portPos: Record<string, any> = {};
const clusterDb: Record<string, any> = {}; const clusterDb: Record<string, any> = {};
@ -831,18 +842,6 @@ const cutPathAtIntersect = (
return points; return points;
}; };
export const render = async (
data4Layout: LayoutData,
svg: {
insert: (arg0: string) => {
(): any;
new (): any;
attr: { (arg0: string, arg1: string): any; new (): any };
};
},
element: any,
algorithm: any
) => {
// @ts-ignore - ELK is not typed // @ts-ignore - ELK is not typed
const elk = new ELK(); const elk = new ELK();

View File

@ -4,34 +4,35 @@
*/ */
import { dedent } from 'ts-dedent'; import { dedent } from 'ts-dedent';
import type { MermaidConfig } from './config.type.js'; import type { MermaidConfig } from './config.type.js';
import { log } from './logger.js'; import { detectType, registerLazyLoadedDiagrams } from './diagram-api/detectType.js';
import utils from './utils.js';
import type { ParseOptions, ParseResult, RenderResult } from './types.js';
import { mermaidAPI } from './mermaidAPI.js';
import { registerLazyLoadedDiagrams, detectType } from './diagram-api/detectType.js';
import { loadRegisteredDiagrams } from './diagram-api/loadDiagram.js';
import type { ParseErrorFunction } from './Diagram.js';
import { isDetailedError } from './utils.js';
import type { DetailedError } from './utils.js';
import type { ExternalDiagramDefinition } from './diagram-api/types.js';
import type { UnknownDiagramError } from './errors.js';
import { addDiagrams } from './diagram-api/diagram-orchestration.js'; import { addDiagrams } from './diagram-api/diagram-orchestration.js';
import { registerLayoutLoaders } from './rendering-util/render.js'; import { loadRegisteredDiagrams } from './diagram-api/loadDiagram.js';
import type { ExternalDiagramDefinition } from './diagram-api/types.js';
import type { ParseErrorFunction } from './Diagram.js';
import type { UnknownDiagramError } from './errors.js';
import type { internalHelpers } from './internals.js';
import { log } from './logger.js';
import { mermaidAPI } from './mermaidAPI.js';
import type { LayoutLoaderDefinition } from './rendering-util/render.js'; import type { LayoutLoaderDefinition } from './rendering-util/render.js';
import { internalHelpers } from './internals.js'; import { registerLayoutLoaders } from './rendering-util/render.js';
import type { LayoutData } from './rendering-util/types.js'; import type { LayoutData } from './rendering-util/types.js';
import type { ParseOptions, ParseResult, RenderResult } from './types.js';
import type { DetailedError } from './utils.js';
import utils, { isDetailedError } from './utils.js';
type InternalHelpers = typeof internalHelpers;
export type { export type {
MermaidConfig,
DetailedError, DetailedError,
ExternalDiagramDefinition, ExternalDiagramDefinition,
InternalHelpers,
LayoutData,
LayoutLoaderDefinition,
MermaidConfig,
ParseErrorFunction, ParseErrorFunction,
RenderResult,
ParseOptions, ParseOptions,
ParseResult, ParseResult,
RenderResult,
UnknownDiagramError, UnknownDiagramError,
LayoutLoaderDefinition,
LayoutData,
}; };
export interface RunOptions { export interface RunOptions {
@ -432,11 +433,6 @@ export interface Mermaid {
contentLoaded: typeof contentLoaded; contentLoaded: typeof contentLoaded;
setParseErrorHandler: typeof setParseErrorHandler; setParseErrorHandler: typeof setParseErrorHandler;
detectType: typeof detectType; detectType: typeof detectType;
/**
* Internal helpers for mermaid
* @deprecated - This should not be used by external packages, as the definitions will change without notice.
*/
internalHelpers: typeof internalHelpers;
} }
const mermaid: Mermaid = { const mermaid: Mermaid = {
@ -453,7 +449,6 @@ const mermaid: Mermaid = {
contentLoaded, contentLoaded,
setParseErrorHandler, setParseErrorHandler,
detectType, detectType,
internalHelpers,
}; };
export default mermaid; export default mermaid;

View File

@ -1,7 +1,14 @@
import { internalHelpers } from '$root/internals.js';
import { log } from '$root/logger.js'; import { log } from '$root/logger.js';
export interface LayoutAlgorithm { export interface LayoutAlgorithm {
render(data4Layout: any, svg: any, element: any, algorithm?: string): any; render(
data4Layout: any,
svg: any,
element: any,
helpers: typeof internalHelpers,
algorithm?: string
): any;
} }
export type LayoutLoader = () => Promise<LayoutAlgorithm>; export type LayoutLoader = () => Promise<LayoutAlgorithm>;
@ -38,7 +45,13 @@ export const render = async (data4Layout: any, svg: any, element: any) => {
const layoutDefinition = layoutAlgorithms[data4Layout.layoutAlgorithm]; const layoutDefinition = layoutAlgorithms[data4Layout.layoutAlgorithm];
const layoutRenderer = await layoutDefinition.loader(); const layoutRenderer = await layoutDefinition.loader();
return layoutRenderer.render(data4Layout, svg, element, layoutDefinition.algorithm); return layoutRenderer.render(
data4Layout,
svg,
element,
internalHelpers,
layoutDefinition.algorithm
);
}; };
/** /**