feat(flowchart): add inheritDir option for subgraph direction

This commit is contained in:
nour kouider
2025-04-09 10:56:30 +01:00
parent cd8d74bb96
commit 36fe04bd46
5 changed files with 64 additions and 3 deletions

View File

@@ -226,6 +226,12 @@ export interface FlowchartDiagramConfig extends BaseDiagramConfig {
* Defines a top/bottom margin for subgraph titles
*
*/
/**
* If true, subgraphs without explicit direction will inherit the global graph direction (e.g., LR, TB, RL, BT).
* Defaults to `false` to preserve legacy layout behavior.
*/
inheritDir?: boolean;
subGraphTitleMargin?: {
top?: number;
bottom?: number;
@@ -300,6 +306,7 @@ export interface FlowchartDiagramConfig extends BaseDiagramConfig {
* This interface was referenced by `MermaidConfig`'s JSON-Schema
* via the `definition` "BaseDiagramConfig".
*/
export interface BaseDiagramConfig {
useWidth?: number;
/**

View File

@@ -71,6 +71,10 @@ const config: RequiredDeep<MermaidConfig> = {
fontWeight: this.personFontWeight,
};
},
flowchart: {
...defaultConfigJson.flowchart,
inheritDir: false, // default to legacy behavior
},
external_personFont: function () {
return {

View File

@@ -651,7 +651,8 @@ You have to call mermaid.initialize.`
const prims: any = { boolean: {}, number: {}, string: {} };
const objs: any[] = [];
let dir; // = undefined; direction.trim();
let dir: string | undefined;
const nodeList = a.filter(function (item) {
const type = typeof item;
if (item.stmt && item.stmt === 'dir') {
@@ -670,7 +671,16 @@ You have to call mermaid.initialize.`
return { nodeList, dir };
};
const { nodeList, dir } = uniq(list.flat());
const result = uniq(list.flat());
const nodeList = result.nodeList;
let dir = result.dir;
const flowchartConfig = getConfig().flowchart ?? {};
dir =
dir ??
(flowchartConfig.inheritDir
? (this.getDirection() ?? (getConfig() as any).direction ?? undefined)
: undefined);
if (this.version === 'gen-1') {
for (let i = 0; i < nodeList.length; i++) {
nodeList[i] = this.lookUpDomId(nodeList[i]);
@@ -681,6 +691,7 @@ You have to call mermaid.initialize.`
title = title || '';
title = this.sanitizeText(title);
this.subCount = this.subCount + 1;
const subGraph = {
id: id,
nodes: nodeList,