mirror of
https://github.com/mermaid-js/mermaid.git
synced 2025-09-18 14:59:53 +02:00
feat: Remove direct file dependency of mermaid from elk, by using peerDependency.
This commit is contained in:
@@ -22,14 +22,9 @@ export const packageOptions = {
|
|||||||
packageName: 'mermaid-zenuml',
|
packageName: 'mermaid-zenuml',
|
||||||
file: 'detector.ts',
|
file: 'detector.ts',
|
||||||
},
|
},
|
||||||
'mermaid-flowchart-elk': {
|
|
||||||
name: 'mermaid-flowchart-elk',
|
|
||||||
packageName: 'mermaid-flowchart-elk',
|
|
||||||
file: 'detector.ts',
|
|
||||||
},
|
|
||||||
'mermaid-layout-elk': {
|
'mermaid-layout-elk': {
|
||||||
name: 'mermaid-layout-elk',
|
name: 'mermaid-layout-elk',
|
||||||
packageName: 'mermaid-layout-elk',
|
packageName: 'mermaid-layout-elk',
|
||||||
file: 'index.ts',
|
file: 'layouts.ts',
|
||||||
},
|
},
|
||||||
} as const;
|
} as const;
|
||||||
|
@@ -508,6 +508,8 @@ mindmap
|
|||||||
// import mindmap from '../../packages/mermaid-mindmap/src/detector';
|
// import mindmap from '../../packages/mermaid-mindmap/src/detector';
|
||||||
// import example from '../../packages/mermaid-example-diagram/src/mermaid-example-diagram.core.mjs';
|
// import example from '../../packages/mermaid-example-diagram/src/mermaid-example-diagram.core.mjs';
|
||||||
import mermaid from './mermaid.esm.mjs';
|
import mermaid from './mermaid.esm.mjs';
|
||||||
|
import { layouts } from './mermaid-layout-elk.esm.mjs';
|
||||||
|
mermaid.registerLayoutLoaders(layouts);
|
||||||
// await mermaid.registerExternalDiagrams([example]);
|
// await mermaid.registerExternalDiagrams([example]);
|
||||||
mermaid.parseError = function (err, hash) {
|
mermaid.parseError = function (err, hash) {
|
||||||
// console.error('Mermaid error: ', err);
|
// console.error('Mermaid error: ', err);
|
||||||
|
@@ -34,7 +34,7 @@
|
|||||||
"elkjs": "^0.9.3",
|
"elkjs": "^0.9.3",
|
||||||
"d3": "^7.9.0"
|
"d3": "^7.9.0"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"peerDependencies": {
|
||||||
"mermaid": "workspace:^"
|
"mermaid": "workspace:^"
|
||||||
},
|
},
|
||||||
"files": [
|
"files": [
|
||||||
|
17
packages/mermaid-layout-elk/src/layouts.ts
Normal file
17
packages/mermaid-layout-elk/src/layouts.ts
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
import type { LayoutLoaderDefinition } from 'mermaid';
|
||||||
|
|
||||||
|
const loader = async () => await import(`./render.js`);
|
||||||
|
const algos = ['elk.stress', 'elk.force', 'elk.mrtree', 'elk.sporeOverlap'];
|
||||||
|
|
||||||
|
export const layouts: LayoutLoaderDefinition[] = [
|
||||||
|
{
|
||||||
|
name: 'elk',
|
||||||
|
loader,
|
||||||
|
algorithm: 'elk.layered',
|
||||||
|
},
|
||||||
|
...algos.map((algo) => ({
|
||||||
|
name: algo,
|
||||||
|
loader,
|
||||||
|
algorithm: algo,
|
||||||
|
})),
|
||||||
|
];
|
@@ -1,21 +1,22 @@
|
|||||||
// @ts-nocheck File not ready to check types
|
// @ts-nocheck File not ready to check types
|
||||||
|
import { curveLinear } from 'd3';
|
||||||
import ELK from 'elkjs/lib/elk.bundled.js';
|
import ELK from 'elkjs/lib/elk.bundled.js';
|
||||||
import { getConfig } from '../../mermaid/src/config.js';
|
import mermaid from 'mermaid';
|
||||||
import common from '../../mermaid/src/diagrams/common/common.js';
|
import { findCommonAncestor } from './find-common-ancestor.js';
|
||||||
import { log } from '../../mermaid/src/logger.js';
|
|
||||||
import { insertCluster } from '../../mermaid/src/rendering-util/rendering-elements/clusters.js';
|
const {
|
||||||
import {
|
common,
|
||||||
|
getConfig,
|
||||||
|
insertCluster,
|
||||||
insertEdge,
|
insertEdge,
|
||||||
insertEdgeLabel,
|
insertEdgeLabel,
|
||||||
|
insertMarkers,
|
||||||
|
insertNode,
|
||||||
|
interpolateToCurve,
|
||||||
|
labelHelper,
|
||||||
|
log,
|
||||||
positionEdgeLabel,
|
positionEdgeLabel,
|
||||||
} from '../../mermaid/src/rendering-util/rendering-elements/edges.js';
|
} = mermaid.internalHelpers;
|
||||||
import { curveLinear } from 'd3';
|
|
||||||
import { interpolateToCurve } from '../../mermaid/src/utils.js';
|
|
||||||
import insertMarkers from '../../mermaid/src/rendering-util/rendering-elements/markers.js';
|
|
||||||
import { insertNode } from '../../mermaid/src/rendering-util/rendering-elements/nodes.js';
|
|
||||||
import { labelHelper } from '../../mermaid/src/rendering-util/rendering-elements/shapes/util.js';
|
|
||||||
import { findCommonAncestor } from './find-common-ancestor.js';
|
|
||||||
|
|
||||||
const nodeDb = {};
|
const nodeDb = {};
|
||||||
const portPos = {};
|
const portPos = {};
|
@@ -1,7 +1,7 @@
|
|||||||
{
|
{
|
||||||
"extends": "../../tsconfig.json",
|
"extends": "../../tsconfig.json",
|
||||||
"compilerOptions": {
|
"compilerOptions": {
|
||||||
"rootDir": "../..",
|
"rootDir": "./src",
|
||||||
"outDir": "./dist",
|
"outDir": "./dist",
|
||||||
"types": ["vitest/importMeta", "vitest/globals"]
|
"types": ["vitest/importMeta", "vitest/globals"]
|
||||||
},
|
},
|
||||||
|
31
packages/mermaid/src/internals.ts
Normal file
31
packages/mermaid/src/internals.ts
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
import { getConfig } from './config.js';
|
||||||
|
import common from './diagrams/common/common.js';
|
||||||
|
import { log } from './logger.js';
|
||||||
|
import { insertCluster } from './rendering-util/rendering-elements/clusters.js';
|
||||||
|
import {
|
||||||
|
insertEdge,
|
||||||
|
insertEdgeLabel,
|
||||||
|
positionEdgeLabel,
|
||||||
|
} from './rendering-util/rendering-elements/edges.js';
|
||||||
|
import insertMarkers from './rendering-util/rendering-elements/markers.js';
|
||||||
|
import { insertNode } from './rendering-util/rendering-elements/nodes.js';
|
||||||
|
import { labelHelper } from './rendering-util/rendering-elements/shapes/util.js';
|
||||||
|
import { interpolateToCurve } from './utils.js';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Internal helpers for mermaid
|
||||||
|
* @deprecated - This should not be used by external packages, as the definitions will change without notice.
|
||||||
|
*/
|
||||||
|
export const internalHelpers = {
|
||||||
|
common,
|
||||||
|
getConfig,
|
||||||
|
insertCluster,
|
||||||
|
insertEdge,
|
||||||
|
insertEdgeLabel,
|
||||||
|
insertMarkers,
|
||||||
|
insertNode,
|
||||||
|
interpolateToCurve,
|
||||||
|
labelHelper,
|
||||||
|
log,
|
||||||
|
positionEdgeLabel,
|
||||||
|
};
|
@@ -16,6 +16,9 @@ import type { DetailedError } from './utils.js';
|
|||||||
import type { ExternalDiagramDefinition } from './diagram-api/types.js';
|
import type { ExternalDiagramDefinition } from './diagram-api/types.js';
|
||||||
import type { UnknownDiagramError } from './errors.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 type { LayoutLoaderDefinition } from './rendering-util/render.js';
|
||||||
|
import { internalHelpers } from './internals.js';
|
||||||
|
|
||||||
export type {
|
export type {
|
||||||
MermaidConfig,
|
MermaidConfig,
|
||||||
@@ -26,6 +29,7 @@ export type {
|
|||||||
ParseOptions,
|
ParseOptions,
|
||||||
ParseResult,
|
ParseResult,
|
||||||
UnknownDiagramError,
|
UnknownDiagramError,
|
||||||
|
LayoutLoaderDefinition,
|
||||||
};
|
};
|
||||||
|
|
||||||
export interface RunOptions {
|
export interface RunOptions {
|
||||||
@@ -413,11 +417,17 @@ export interface Mermaid {
|
|||||||
render: typeof render;
|
render: typeof render;
|
||||||
init: typeof init;
|
init: typeof init;
|
||||||
run: typeof run;
|
run: typeof run;
|
||||||
|
registerLayoutLoaders: typeof registerLayoutLoaders;
|
||||||
registerExternalDiagrams: typeof registerExternalDiagrams;
|
registerExternalDiagrams: typeof registerExternalDiagrams;
|
||||||
initialize: typeof initialize;
|
initialize: typeof initialize;
|
||||||
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 = {
|
||||||
@@ -428,11 +438,13 @@ const mermaid: Mermaid = {
|
|||||||
init,
|
init,
|
||||||
run,
|
run,
|
||||||
registerExternalDiagrams,
|
registerExternalDiagrams,
|
||||||
|
registerLayoutLoaders,
|
||||||
initialize,
|
initialize,
|
||||||
parseError: undefined,
|
parseError: undefined,
|
||||||
contentLoaded,
|
contentLoaded,
|
||||||
setParseErrorHandler,
|
setParseErrorHandler,
|
||||||
detectType,
|
detectType,
|
||||||
|
internalHelpers,
|
||||||
};
|
};
|
||||||
|
|
||||||
export default mermaid;
|
export default mermaid;
|
||||||
|
@@ -1,34 +0,0 @@
|
|||||||
export const render = async (data4Layout, svg, element) => {
|
|
||||||
switch (data4Layout.layoutAlgorithm) {
|
|
||||||
case 'dagre': {
|
|
||||||
const layoutRenderer = await import('./layout-algorithms/dagre/index.js');
|
|
||||||
return layoutRenderer.render(data4Layout, svg, element);
|
|
||||||
}
|
|
||||||
|
|
||||||
case 'elk': {
|
|
||||||
// TODO: Should fix this import path
|
|
||||||
const layoutRenderer = await import('../../../mermaid-layout-elk/src/index.js');
|
|
||||||
return layoutRenderer.render(data4Layout, svg, element, 'elk.layered');
|
|
||||||
}
|
|
||||||
case 'stress': {
|
|
||||||
// TODO: Should fix this import path
|
|
||||||
const layoutRenderer = await import('../../../mermaid-layout-elk/src/index.js');
|
|
||||||
return layoutRenderer.render(data4Layout, svg, element, 'elk.stress');
|
|
||||||
}
|
|
||||||
case 'force': {
|
|
||||||
// TODO: Should fix this import path
|
|
||||||
const layoutRenderer = await import('../../../mermaid-layout-elk/src/index.js');
|
|
||||||
return layoutRenderer.render(data4Layout, svg, element, 'elk.force');
|
|
||||||
}
|
|
||||||
case 'mrtree': {
|
|
||||||
// TODO: Should fix this import path
|
|
||||||
const layoutRenderer = await import('../../../mermaid-layout-elk/src/index.js');
|
|
||||||
return layoutRenderer.render(data4Layout, svg, element, 'elk.mrtree');
|
|
||||||
}
|
|
||||||
case 'sporeOverlap': {
|
|
||||||
// TODO: Should fix this import path
|
|
||||||
const layoutRenderer = await import('../../../mermaid-layout-elk/src/index.js');
|
|
||||||
return layoutRenderer.render(data4Layout, svg, element, 'elk.sporeOverlap');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
40
packages/mermaid/src/rendering-util/render.ts
Normal file
40
packages/mermaid/src/rendering-util/render.ts
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
export interface LayoutAlgorithm {
|
||||||
|
render(data4Layout: any, svg: any, element: any, algorithm?: string): any;
|
||||||
|
}
|
||||||
|
|
||||||
|
export type LayoutLoader = () => Promise<LayoutAlgorithm>;
|
||||||
|
export interface LayoutLoaderDefinition {
|
||||||
|
name: string;
|
||||||
|
loader: LayoutLoader;
|
||||||
|
algorithm?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
const layoutAlgorithms: Record<string, LayoutLoaderDefinition> = {};
|
||||||
|
|
||||||
|
export const registerLayoutLoaders = (loaders: LayoutLoaderDefinition[]) => {
|
||||||
|
for (const loader of loaders) {
|
||||||
|
layoutAlgorithms[loader.name] = loader;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// TODO: Should we load dagre without lazy loading?
|
||||||
|
const registerDefaultLayoutLoaders = () => {
|
||||||
|
registerLayoutLoaders([
|
||||||
|
{
|
||||||
|
name: 'dagre',
|
||||||
|
loader: async () => await import('./layout-algorithms/dagre/index.js'),
|
||||||
|
},
|
||||||
|
]);
|
||||||
|
};
|
||||||
|
|
||||||
|
registerDefaultLayoutLoaders();
|
||||||
|
|
||||||
|
export const render = async (data4Layout: any, svg: any, element: any) => {
|
||||||
|
if (!(data4Layout.layoutAlgorithm in layoutAlgorithms)) {
|
||||||
|
throw new Error(`Unknown layout algorithm: ${data4Layout.layoutAlgorithm}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
const layoutDefinition = layoutAlgorithms[data4Layout.layoutAlgorithm];
|
||||||
|
const layoutRenderer = await layoutDefinition.loader();
|
||||||
|
return layoutRenderer.render(data4Layout, svg, element, layoutDefinition.algorithm);
|
||||||
|
};
|
1
pnpm-lock.yaml
generated
1
pnpm-lock.yaml
generated
@@ -442,7 +442,6 @@ importers:
|
|||||||
elkjs:
|
elkjs:
|
||||||
specifier: ^0.9.3
|
specifier: ^0.9.3
|
||||||
version: 0.9.3
|
version: 0.9.3
|
||||||
devDependencies:
|
|
||||||
mermaid:
|
mermaid:
|
||||||
specifier: workspace:^
|
specifier: workspace:^
|
||||||
version: link:../mermaid
|
version: link:../mermaid
|
||||||
|
@@ -66,7 +66,7 @@
|
|||||||
// "newLine": "crlf", /* Set the newline character for emitting files. */
|
// "newLine": "crlf", /* Set the newline character for emitting files. */
|
||||||
// "stripInternal": true, /* Disable emitting declarations that have `@internal` in their JSDoc comments. */
|
// "stripInternal": true, /* Disable emitting declarations that have `@internal` in their JSDoc comments. */
|
||||||
// "noEmitHelpers": true, /* Disable generating custom helper functions like `__extends` in compiled output. */
|
// "noEmitHelpers": true, /* Disable generating custom helper functions like `__extends` in compiled output. */
|
||||||
// "noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */
|
"noEmitOnError": false /* Disable emitting files if any type checking errors are reported. */,
|
||||||
// "preserveConstEnums": true, /* Disable erasing `const enum` declarations in generated code. */
|
// "preserveConstEnums": true, /* Disable erasing `const enum` declarations in generated code. */
|
||||||
// "declarationDir": "./", /* Specify the output directory for generated declaration files. */
|
// "declarationDir": "./", /* Specify the output directory for generated declaration files. */
|
||||||
// "preserveValueImports": true, /* Preserve unused imported values in the JavaScript output that would otherwise be removed. */
|
// "preserveValueImports": true, /* Preserve unused imported values in the JavaScript output that would otherwise be removed. */
|
||||||
|
Reference in New Issue
Block a user