diff --git a/docs/config/setup/interfaces/mermaid.LayoutData.md b/docs/config/setup/interfaces/mermaid.LayoutData.md index 5616e1c9a..552a16a8d 100644 --- a/docs/config/setup/interfaces/mermaid.LayoutData.md +++ b/docs/config/setup/interfaces/mermaid.LayoutData.md @@ -20,7 +20,7 @@ #### Defined in -[packages/mermaid/src/rendering-util/types.ts:144](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/rendering-util/types.ts#L144) +[packages/mermaid/src/rendering-util/types.ts:147](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/rendering-util/types.ts#L147) --- @@ -30,7 +30,7 @@ #### Defined in -[packages/mermaid/src/rendering-util/types.ts:143](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/rendering-util/types.ts#L143) +[packages/mermaid/src/rendering-util/types.ts:146](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/rendering-util/types.ts#L146) --- @@ -40,4 +40,4 @@ #### Defined in -[packages/mermaid/src/rendering-util/types.ts:142](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/rendering-util/types.ts#L142) +[packages/mermaid/src/rendering-util/types.ts:145](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/rendering-util/types.ts#L145) diff --git a/docs/config/setup/interfaces/mermaid.ParseOptions.md b/docs/config/setup/interfaces/mermaid.ParseOptions.md index 717e35565..bac54b8ca 100644 --- a/docs/config/setup/interfaces/mermaid.ParseOptions.md +++ b/docs/config/setup/interfaces/mermaid.ParseOptions.md @@ -19,4 +19,4 @@ The `parseError` function will not be called. #### Defined in -[packages/mermaid/src/types.ts:59](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/types.ts#L59) +[packages/mermaid/src/types.ts:64](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/types.ts#L64) diff --git a/docs/config/setup/interfaces/mermaid.ParseResult.md b/docs/config/setup/interfaces/mermaid.ParseResult.md index 9f90b6dd4..e2eb5df50 100644 --- a/docs/config/setup/interfaces/mermaid.ParseResult.md +++ b/docs/config/setup/interfaces/mermaid.ParseResult.md @@ -18,7 +18,7 @@ The config passed as YAML frontmatter or directives #### Defined in -[packages/mermaid/src/types.ts:70](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/types.ts#L70) +[packages/mermaid/src/types.ts:75](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/types.ts#L75) --- @@ -30,4 +30,4 @@ The diagram type, e.g. 'flowchart', 'sequence', etc. #### Defined in -[packages/mermaid/src/types.ts:66](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/types.ts#L66) +[packages/mermaid/src/types.ts:71](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/types.ts#L71) diff --git a/docs/config/setup/interfaces/mermaid.RenderResult.md b/docs/config/setup/interfaces/mermaid.RenderResult.md index f882b7af4..cce7f6928 100644 --- a/docs/config/setup/interfaces/mermaid.RenderResult.md +++ b/docs/config/setup/interfaces/mermaid.RenderResult.md @@ -39,7 +39,7 @@ bindFunctions?.(div); // To call bindFunctions only if it's present. #### Defined in -[packages/mermaid/src/types.ts:98](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/types.ts#L98) +[packages/mermaid/src/types.ts:103](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/types.ts#L103) --- @@ -51,7 +51,7 @@ The diagram type, e.g. 'flowchart', 'sequence', etc. #### Defined in -[packages/mermaid/src/types.ts:88](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/types.ts#L88) +[packages/mermaid/src/types.ts:93](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/types.ts#L93) --- @@ -63,4 +63,4 @@ The svg code for the rendered graph. #### Defined in -[packages/mermaid/src/types.ts:84](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/types.ts#L84) +[packages/mermaid/src/types.ts:89](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/types.ts#L89) diff --git a/docs/syntax/flowchart.md b/docs/syntax/flowchart.md index 3837e77de..fefa12e02 100644 --- a/docs/syntax/flowchart.md +++ b/docs/syntax/flowchart.md @@ -1183,6 +1183,91 @@ flowchart TB B --> D ``` +### Attaching an ID to Edges + +Mermaid now supports assigning IDs to edges, similar to how IDs and metadata can be attached to nodes. This feature lays the groundwork for more advanced styling, classes, and animation capabilities on edges. + +**Syntax:** + +To give an edge an ID, prepend the edge syntax with the ID followed by an `@` character. For example: + +```mermaid-example +flowchart LR + A e1@–> B +``` + +```mermaid +flowchart LR + A e1@–> B +``` + +In this example, `e1` is the ID of the edge connecting `A` to `B`. You can then use this ID in later definitions or style statements, just like with nodes. + +### Turning an Animation On + +Once you have assigned an ID to an edge, you can turn on animations for that edge by defining the edge’s properties: + +```mermaid-example +flowchart LR + A e1@==> B + e1@{ animate: true } +``` + +```mermaid +flowchart LR + A e1@==> B + e1@{ animate: true } +``` + +This tells Mermaid that the edge `e1` should be animated. + +### Selecting Type of Animation + +In the initial version, two animation speeds are supported: `fast` and `slow`. Selecting a specific animation type is a shorthand for enabling animation and setting the animation speed in one go. + +**Examples:** + +```mermaid-example +flowchart LR + A e1@–> B + e1@{ animation: fast } +``` + +```mermaid +flowchart LR + A e1@–> B + e1@{ animation: fast } +``` + +This is equivalent to `{ animate: true, animation: fast }`. + +### Using classDef Statements for Animations + +You can also animate edges by assigning a class to them and then defining animation properties in a `classDef` statement. For example: + +```mermaid-example +flowchart LR + A e1@–> B + classDef animate stroke-dasharray: 9,5,stroke-dashoffset: 900,animation: dash 25s linear infinite; + class e1 animate +``` + +```mermaid +flowchart LR + A e1@–> B + classDef animate stroke-dasharray: 9,5,stroke-dashoffset: 900,animation: dash 25s linear infinite; + class e1 animate +``` + +In this snippet: + +- `e1@-->` creates an edge with ID `e1`. +- `classDef animate` defines a class named `animate` with styling and animation properties. +- `class e1 animate` applies the `animate` class to the edge `e1`. + +**Note on Escaping Commas:** +When setting the `stroke-dasharray` property, remember to escape commas as `\,` since commas are used as delimiters in Mermaid’s style definitions. + ## New arrow types There are new types of arrows supported: diff --git a/packages/mermaid/src/diagrams/flowchart/flowDb.ts b/packages/mermaid/src/diagrams/flowchart/flowDb.ts index ffe46d398..931347a4d 100644 --- a/packages/mermaid/src/diagrams/flowchart/flowDb.ts +++ b/packages/mermaid/src/diagrams/flowchart/flowDb.ts @@ -249,11 +249,21 @@ You have to call mermaid.initialize.` } }; +interface LinkData { + id: string; +} + +function isLinkData(value: unknown): value is LinkData { + return ( + value !== null && + typeof value === 'object' && + 'id' in value && + typeof (value as LinkData).id === 'string' + ); +} + export const addLink = function (_start: string[], _end: string[], linkData: unknown) { - const id = - linkData && typeof linkData === 'object' && 'id' in linkData - ? linkData.id?.replace('@', '') - : undefined; + const id = isLinkData(linkData) ? linkData.id.replace('@', '') : undefined; log.info('addLink', _start, _end, id); diff --git a/packages/mermaid/src/docs/syntax/flowchart.md b/packages/mermaid/src/docs/syntax/flowchart.md index 829b71c2d..6c9db197a 100644 --- a/packages/mermaid/src/docs/syntax/flowchart.md +++ b/packages/mermaid/src/docs/syntax/flowchart.md @@ -711,6 +711,67 @@ flowchart TB B --> D ``` +### Attaching an ID to Edges + +Mermaid now supports assigning IDs to edges, similar to how IDs and metadata can be attached to nodes. This feature lays the groundwork for more advanced styling, classes, and animation capabilities on edges. + +**Syntax:** + +To give an edge an ID, prepend the edge syntax with the ID followed by an `@` character. For example: + +```mermaid +flowchart LR + A e1@–> B +``` + +In this example, `e1` is the ID of the edge connecting `A` to `B`. You can then use this ID in later definitions or style statements, just like with nodes. + +### Turning an Animation On + +Once you have assigned an ID to an edge, you can turn on animations for that edge by defining the edge’s properties: + +```mermaid +flowchart LR + A e1@==> B + e1@{ animate: true } +``` + +This tells Mermaid that the edge `e1` should be animated. + +### Selecting Type of Animation + +In the initial version, two animation speeds are supported: `fast` and `slow`. Selecting a specific animation type is a shorthand for enabling animation and setting the animation speed in one go. + +**Examples:** + +```mermaid +flowchart LR + A e1@–> B + e1@{ animation: fast } +``` + +This is equivalent to `{ animate: true, animation: fast }`. + +### Using classDef Statements for Animations + +You can also animate edges by assigning a class to them and then defining animation properties in a `classDef` statement. For example: + +```mermaid +flowchart LR + A e1@–> B + classDef animate stroke-dasharray: 9,5,stroke-dashoffset: 900,animation: dash 25s linear infinite; + class e1 animate +``` + +In this snippet: + +- `e1@-->` creates an edge with ID `e1`. +- `classDef animate` defines a class named `animate` with styling and animation properties. +- `class e1 animate` applies the `animate` class to the edge `e1`. + +**Note on Escaping Commas:** +When setting the `stroke-dasharray` property, remember to escape commas as `\,` since commas are used as delimiters in Mermaid’s style definitions. + ## New arrow types There are new types of arrows supported: diff --git a/packages/mermaid/src/rendering-util/types.ts b/packages/mermaid/src/rendering-util/types.ts index d64594218..1f84c66c3 100644 --- a/packages/mermaid/src/rendering-util/types.ts +++ b/packages/mermaid/src/rendering-util/types.ts @@ -96,6 +96,8 @@ export interface Edge { label?: string; classes?: string; style?: string[]; + animate?: boolean; + animation?: 'fast' | 'slow'; // Properties common to both Flowchart and State Diagram edges arrowhead?: string; arrowheadStyle?: string;