Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add TreeView Diagram #5665

Open
wants to merge 9 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .build/jsonSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const MERMAID_CONFIG_DIAGRAM_KEYS = [
'sankey',
'block',
'packet',
'treeView',
] as const;

/**
Expand Down
20 changes: 15 additions & 5 deletions docs/config/setup/interfaces/mermaid.MermaidConfig.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ should not change unless content is changed.

#### Defined in

[packages/mermaid/src/config.type.ts:163](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/config.type.ts#L163)
[packages/mermaid/src/config.type.ts:164](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/config.type.ts#L164)

---

Expand Down Expand Up @@ -155,7 +155,7 @@ See <https://developer.mozilla.org/en-US/docs/Web/CSS/font-family>

#### Defined in

[packages/mermaid/src/config.type.ts:165](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/config.type.ts#L165)
[packages/mermaid/src/config.type.ts:166](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/config.type.ts#L166)

---

Expand Down Expand Up @@ -246,7 +246,7 @@ This option decides the amount of logging to be used by mermaid.

#### Defined in

[packages/mermaid/src/config.type.ts:166](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/config.type.ts#L166)
[packages/mermaid/src/config.type.ts:167](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/config.type.ts#L167)

---

Expand Down Expand Up @@ -401,7 +401,7 @@ This is useful when you want to control how to handle syntax errors in your appl

#### Defined in

[packages/mermaid/src/config.type.ts:172](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/config.type.ts#L172)
[packages/mermaid/src/config.type.ts:173](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/config.type.ts#L173)

---

Expand Down Expand Up @@ -448,13 +448,23 @@ You may also use `themeCSS` to override this value.

---

### treeView

• `Optional` **treeView**: `TreeViewDiagramConfig`

#### Defined in

[packages/mermaid/src/config.type.ts:163](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/config.type.ts#L163)

---

### wrap

• `Optional` **wrap**: `boolean`

#### Defined in

[packages/mermaid/src/config.type.ts:164](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/config.type.ts#L164)
[packages/mermaid/src/config.type.ts:165](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/config.type.ts#L165)

---

Expand Down
2 changes: 1 addition & 1 deletion docs/config/setup/modules/defaultConfig.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

#### Defined in

[packages/mermaid/src/defaultConfig.ts:275](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/defaultConfig.ts#L275)
[packages/mermaid/src/defaultConfig.ts:279](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/defaultConfig.ts#L279)

---

Expand Down
25 changes: 25 additions & 0 deletions packages/mermaid/src/config.type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ export interface MermaidConfig {
sankey?: SankeyDiagramConfig;
packet?: PacketDiagramConfig;
block?: BlockDiagramConfig;
treeView?: TreeViewDiagramConfig;
dompurifyConfig?: DOMPurifyConfiguration;
wrap?: boolean;
fontSize?: number;
Expand Down Expand Up @@ -1460,6 +1461,30 @@ export interface PacketDiagramConfig extends BaseDiagramConfig {
export interface BlockDiagramConfig extends BaseDiagramConfig {
padding?: number;
}
/**
* The object containing configurations specific for treeView diagrams.
*
* This interface was referenced by `MermaidConfig`'s JSON-Schema
* via the `definition` "TreeViewDiagramConfig".
*/
export interface TreeViewDiagramConfig extends BaseDiagramConfig {
/**
* Horizontal distance between rows differing by one level
*/
rowIndent?: number;
/**
* Horizontal padding of label
*/
paddingX?: number;
/**
* Vertical padding of label
*/
paddingY?: number;
/**
* Thickness of the line
*/
lineThickness?: number;
}
/**
* This interface was referenced by `MermaidConfig`'s JSON-Schema
* via the `definition` "FontConfig".
Expand Down
4 changes: 4 additions & 0 deletions packages/mermaid/src/defaultConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,10 @@ const config: RequiredDeep<MermaidConfig> = {
packet: {
...defaultConfigJson.packet,
},
treeView: {
...defaultConfigJson.treeView,
useWidth: undefined,
},
};

const keyify = (obj: any, prefix = ''): string[] =>
Expand Down
4 changes: 3 additions & 1 deletion packages/mermaid/src/diagram-api/diagram-orchestration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import mindmap from '../diagrams/mindmap/detector.js';
import sankey from '../diagrams/sankey/sankeyDetector.js';
import { packet } from '../diagrams/packet/detector.js';
import block from '../diagrams/block/blockDetector.js';
import treeView from '../diagrams/treeView/detector.js';
import { registerLazyLoadedDiagrams } from './detectType.js';
import { registerDiagram } from './diagramAPI.js';

Expand Down Expand Up @@ -90,6 +91,7 @@ export const addDiagrams = () => {
sankey,
packet,
xychart,
block
block,
treeView
);
};
79 changes: 79 additions & 0 deletions packages/mermaid/src/diagrams/treeView/db.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import type { TreeViewDiagramConfig } from '../../config.type.js';
import type { TreeViewDB, Node } from './types.js';
import { getConfig as getCommonConfig } from '../../config.js';
import DEFAULT_CONFIG from '../../defaultConfig.js';
import {
clear as commonClear,
getAccDescription,
getAccTitle,
getDiagramTitle,
setAccDescription,
setAccTitle,
setDiagramTitle,
} from '../common/commonDb.js';
import { cleanAndMerge } from '../../utils.js';
import { ImperativeState } from '../../utils/imperativeState.js';

interface TreeViewState {
cnt: number;
stack: Node[];
}

const state = new ImperativeState<TreeViewState>(() => ({
cnt: 1,
stack: [
{
id: 0,
level: -1,
name: '/',
children: [],
},
],
}));

const clear = () => {
state.reset();
commonClear();
};

const getRoot = () => {
return state.records.stack[0];
};

const getCount = () => state.records.cnt;

const defaultConfig: Required<TreeViewDiagramConfig> = DEFAULT_CONFIG.treeView;

const getConfig = (): Required<TreeViewDiagramConfig> => {
return cleanAndMerge(defaultConfig, getCommonConfig().treeView);
};

const addNode = (level: number, name: string) => {
while (level <= state.records.stack[state.records.stack.length - 1].level) {
state.records.stack.pop();
}
const node = {
id: state.records.cnt++,
level,
name,
children: [],
};
state.records.stack[state.records.stack.length - 1].children.push(node);
state.records.stack.push(node);
};

const db: TreeViewDB = {
clear,
addNode,
getRoot,
getCount,
getConfig,
getAccTitle,
getAccDescription,
getDiagramTitle,
setAccDescription,
setAccTitle,
setDiagramTitle,
};

export default db;
21 changes: 21 additions & 0 deletions packages/mermaid/src/diagrams/treeView/detector.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import type { DiagramDetector, DiagramLoader } from '../../diagram-api/types.js';
import type { ExternalDiagramDefinition } from '../../diagram-api/types.js';

const id = 'treeView';

const detector: DiagramDetector = (txt) => {
return /^\s*treeView-beta/.test(txt);
};

const loader: DiagramLoader = async () => {
const { diagram } = await import('./diagram.js');
return { id, diagram };
};

const plugin: ExternalDiagramDefinition = {
id,
detector,
loader,
};

export default plugin;
12 changes: 12 additions & 0 deletions packages/mermaid/src/diagrams/treeView/diagram.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import type { DiagramDefinition } from '../../diagram-api/types.js';
import { parser } from './parser.js';
import db from './db.js';
import renderer from './renderer.js';
import styles from './styles.js';

export const diagram: DiagramDefinition = {
db,
renderer,
parser,
styles,
};
18 changes: 18 additions & 0 deletions packages/mermaid/src/diagrams/treeView/parser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import type { ParserDefinition } from '../../diagram-api/types.js';
import { log } from '../../logger.js';
import { populateCommonDb } from '../common/populateCommonDb.js';
import db from './db.js';
import { parse, type TreeView } from '@mermaid-js/parser';

const populate = (ast: TreeView) => {
populateCommonDb(ast, db);
ast.nodes.map((node) => db.addNode(node.indent.length, node.name));
};

export const parser: ParserDefinition = {
parse: async (input: string): Promise<void> => {
const ast = await parse('treeView', input);
log.debug(ast);
populate(ast);
},
};
Loading
Loading