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

feat(1624): first draft, prototype of context map grammar and diagram #5353

Draft
wants to merge 2 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .vite/jsonSchemaPlugin.ts
Expand Up @@ -21,6 +21,7 @@ const MERMAID_CONFIG_DIAGRAM_KEYS = [
'xyChart',
'requirement',
'mindmap',
'contextMap',
'timeline',
'gitGraph',
'c4',
Expand Down
74 changes: 74 additions & 0 deletions demos/contextMap.html
@@ -0,0 +1,74 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title>Context Map Language Quick Test Page</title>
<link rel="icon" type="image/png" href="data:image/png;base64,iVBORw0KGgo=" />
<style>
div.mermaid {
/* font-family: 'trebuchet ms', verdana, arial; */
font-family: 'Courier New', Courier, monospace !important;
}
</style>
</head>

<body>
<h1>Context Map demo</h1>
<pre class="mermaid">
ContextMap DDDSampleMap {
contains CargoBookingContext
contains VoyagePlanningContext
contains LocationContext

CargoBookingContext [SK]<->[SK] VoyagePlanningContext
CargoBookingContext [D]<-[U,OHS,PL] LocationContext
VoyagePlanningContext [D]<-[U,OHS,PL] LocationContext
}
</pre
>

<pre class="mermaid">
ContextMap InsuranceContextMap {
contains CustomerManagementContext
contains CustomerSelfServiceContext
contains PrintingContext
contains PolicyManagementContext
contains RiskManagementContext
contains DebtCollection

CustomerSelfServiceContext [D,C]<-[U,S] CustomerManagementContext
CustomerManagementContext [D,ACL]<-[U,OHS,PL] PrintingContext
PrintingContext [U,OHS,PL]->[D,ACL] PolicyManagementContext
RiskManagementContext [P]<->[P] PolicyManagementContext
PolicyManagementContext [D,CF]<-[U,OHS,PL] CustomerManagementContext
DebtCollection [D,ACL]<-[U,OHS,PL] PrintingContext
PolicyManagementContext [SK]<->[SK] DebtCollection
}


</pre
>

<script type="module">
import mermaid from './mermaid.esm.mjs';

mermaid.parseError = function (err, hash) {
// console.error('Mermaid error: ', err);
};
mermaid.initialize({
theme: 'base',
startOnLoad: true,
logLevel: 0,
useMaxWidth: false,
});
function callback() {
alert('It worked');
}
mermaid.parseError = function (err, hash) {
console.error('In parse error:');
console.error(err);
};
</script>
</body>
</html>
3 changes: 3 additions & 0 deletions demos/index.html
Expand Up @@ -54,6 +54,9 @@ <h2><a href="./journey.html">Journey</a></h2>
<li>
<h2><a href="./mindmap.html">Mindmap</a></h2>
</li>
<li>
<h2><a href="./contextMap.html">Context Map</a></h2>
</li>
<li>
<h2><a href="./pie.html">Pie</a></h2>
</li>
Expand Down
2 changes: 1 addition & 1 deletion docs/config/setup/modules/defaultConfig.md
Expand Up @@ -14,7 +14,7 @@

#### Defined in

[defaultConfig.ts:272](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/defaultConfig.ts#L272)
[defaultConfig.ts:276](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/defaultConfig.ts#L276)

---

Expand Down
1 change: 1 addition & 0 deletions packages/mermaid/scripts/create-types-from-json-schema.mts
Expand Up @@ -50,6 +50,7 @@
'xyChart',
'requirement',
'mindmap',
'contextMap',
'timeline',
'gitGraph',
'c4',
Expand Down Expand Up @@ -120,7 +121,7 @@
* @param schema - The input schema.
* @returns The schema with `allOf` replaced with `extends`.
*/
function replaceAllOfWithExtends(schema: JSONSchemaType<Record<string, any>>) {

Check warning on line 124 in packages/mermaid/scripts/create-types-from-json-schema.mts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
if (schema['allOf']) {
const { allOf, ...schemaWithoutAllOf } = schema;
return {
Expand All @@ -144,7 +145,7 @@
* @param schema - The input schema.
* @returns The schema with all required values removed.
*/
function removeRequired(schema: JSONSchemaType<Record<string, any>>) {

Check warning on line 148 in packages/mermaid/scripts/create-types-from-json-schema.mts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
return { ...schema, required: [] };
}

Expand Down
36 changes: 36 additions & 0 deletions packages/mermaid/src/config.type.ts
Expand Up @@ -166,6 +166,7 @@ export interface MermaidConfig {
xyChart?: XYChartConfig;
requirement?: RequirementDiagramConfig;
mindmap?: MindmapDiagramConfig;
contextMap?: MiniContextMapLanguageDiagramConfig;
gitGraph?: GitGraphDiagramConfig;
c4?: C4DiagramConfig;
sankey?: SankeyDiagramConfig;
Expand Down Expand Up @@ -639,6 +640,41 @@ export interface MindmapDiagramConfig extends BaseDiagramConfig {
padding?: number;
maxNodeWidth?: number;
}
/**
* The object containing configurations specific for mini context map language diagrams
*
* This interface was referenced by `MermaidConfig`'s JSON-Schema
* via the `definition` "MiniContextMapLanguageDiagramConfig".
*/
export interface MiniContextMapLanguageDiagramConfig extends BaseDiagramConfig {
width?: number;
height?: number;
nodeMargin?: ContextMapNodeMargin;
nodePadding?: ContextMapNodePadding;
font?: ContextMapFont;
}
/**
* margins of nodes
*/
export interface ContextMapNodeMargin {
horizontal?: number;
vertical?: number;
}
/**
* padding of nodes
*/
export interface ContextMapNodePadding {
horizontal?: number;
vertical?: number;
}
/**
* Font of all Context Map texts
*/
export interface ContextMapFont {
fontFamily?: string;
fontSize?: number;
fontWeight?: number;
}
/**
* This interface was referenced by `MermaidConfig`'s JSON-Schema
* via the `definition` "PieDiagramConfig".
Expand Down
4 changes: 4 additions & 0 deletions packages/mermaid/src/defaultConfig.ts
Expand Up @@ -257,6 +257,10 @@ const config: RequiredDeep<MermaidConfig> = {
// TODO: can we make this default to `true` instead?
useMaxWidth: false,
},
contextMap: {
...defaultConfigJson.contextMap,
useMaxWidth: false,
},
};

const keyify = (obj: any, prefix = ''): string[] =>
Expand Down
Expand Up @@ -31,6 +31,7 @@ describe('diagram-orchestration', () => {
{ text: 'info', expected: 'info' },
{ text: 'sequenceDiagram', expected: 'sequence' },
{ text: 'mindmap', expected: 'mindmap' },
{ text: 'ContextMap', expected: 'contextMap' },
{ text: 'timeline', expected: 'timeline' },
{ text: 'gitGraph', expected: 'gitGraph' },
{ text: 'stateDiagram', expected: 'state' },
Expand Down
2 changes: 2 additions & 0 deletions packages/mermaid/src/diagram-api/diagram-orchestration.ts
Expand Up @@ -19,6 +19,7 @@ import errorDiagram from '../diagrams/error/errorDiagram.js';
import flowchartElk from '../diagrams/flowchart/elk/detector.js';
import timeline from '../diagrams/timeline/detector.js';
import mindmap from '../diagrams/mindmap/detector.js';
import contextMap from '../diagrams/context-map/detector.js';
import sankey from '../diagrams/sankey/sankeyDetector.js';
import block from '../diagrams/block/blockDetector.js';
import { registerLazyLoadedDiagrams } from './detectType.js';
Expand Down Expand Up @@ -81,6 +82,7 @@ export const addDiagrams = () => {
flowchartV2,
flowchart,
mindmap,
contextMap,
timeline,
git,
stateV2,
Expand Down
11 changes: 11 additions & 0 deletions packages/mermaid/src/diagrams/context-map/contextMap-definition.ts
@@ -0,0 +1,11 @@
// @ts-ignore: JISON doesn't support types
import contextMapParser from './parser/contextMap.jison';
import * as contextMapDb from './contextMapDb.js';
import contextMapRenderer from './contextMapRenderer.js';

export const diagram = {
db: contextMapDb,
renderer: contextMapRenderer,
parser: contextMapParser,
styles: undefined,
};
116 changes: 116 additions & 0 deletions packages/mermaid/src/diagrams/context-map/contextMap.spec.ts
@@ -0,0 +1,116 @@
import { describe, test, expect } from 'vitest';
import type { Link, RawLink } from './contextMap.js';
import { mapEdgeLabels } from './contextMap.js';

describe('graph construction', () => {
test.each([
{
rawLink: {
source: { id: 'CargoBookingContext', type: ['SK'] },
target: {
id: 'VoyagePlanningContextVoyagePlanningContextVoyagePlanningContext',
type: ['SK'],
},
arrow: ['left', 'right'],
},
link: {
source: { id: 'CargoBookingContext', boxText: undefined, bodyText: undefined },
target: {
id: 'VoyagePlanningContextVoyagePlanningContextVoyagePlanningContext',
boxText: undefined,
bodyText: undefined,
},
middleText: 'Shared Kernel',
},
},
{
rawLink: {
source: { id: 'CustomerSelfServiceContext', type: ['D', 'C'] },
target: { id: 'CustomerManagementContext', type: ['U', 'S'] },
arrow: ['right'],
},
link: {
source: { id: 'CustomerSelfServiceContext', boxText: 'D', bodyText: undefined },
target: { id: 'CustomerManagementContext', boxText: 'U', bodyText: undefined },
middleText: 'Customer/Supplier',
},
},
{
rawLink: {
source: { id: 'CustomerManagementContext', type: ['D', 'ACL'] },
target: { id: 'PrintingContext', type: ['U', 'OHS', 'PL'] },
arrow: ['right'],
},
link: {
source: { id: 'CustomerManagementContext', boxText: 'D', bodyText: 'ACL' },
target: { id: 'PrintingContext', boxText: 'U', bodyText: 'OHS, PL' },
middleText: undefined,
},
},
{
rawLink: {
source: { id: 'PrintingContext', type: ['U', 'OHS', 'PL'] },
target: { id: 'PolicyManagementContext', type: ['D', 'ACL'] },
arrow: ['right'],
},
link: {
source: { id: 'PrintingContext', boxText: 'U', bodyText: 'OHS, PL' },
target: { id: 'PolicyManagementContext', boxText: 'D', bodyText: 'ACL' },
middleText: undefined,
},
},
{
rawLink: {
source: { id: 'RiskManagementContext', type: ['P'] },
target: { id: 'PolicyManagementContext', type: ['P'] },
arrow: ['left', 'right'],
},
link: {
source: { id: 'RiskManagementContext', boxText: undefined, bodyText: undefined },
target: { id: 'PolicyManagementContext', boxText: undefined, bodyText: undefined },
middleText: 'Partnership',
},
},
{
rawLink: {
source: { id: 'PolicyManagementContext', type: ['D', 'CF'] },
target: { id: 'CustomerManagementContext', type: ['U', 'OHS', 'PL'] },
arrow: ['right'],
},
link: {
source: { id: 'PolicyManagementContext', boxText: 'D', bodyText: 'CF' },
target: { id: 'CustomerManagementContext', boxText: 'U', bodyText: 'OHS, PL' },
middleText: undefined,
},
},
{
rawLink: {
source: { id: 'DebtCollection', type: ['D', 'ACL'] },
target: { id: 'PrintingContext', type: ['U', 'OHS', 'PL'] },
arrow: ['right'],
},
link: {
source: { id: 'DebtCollection', boxText: 'D', bodyText: 'ACL' },
target: { id: 'PrintingContext', boxText: 'U', bodyText: 'OHS, PL' },
middleText: undefined,
},
},
{
rawLink: {
source: { id: 'CustomersBackofficeTeam', type: ['U', 'S'] },
target: { id: 'CustomersFrontofficeTeam', type: ['D', 'C'] },
arrow: ['right'],
},
link: {
source: { id: 'CustomersBackofficeTeam', boxText: 'U', bodyText: undefined },
target: { id: 'CustomersFrontofficeTeam', boxText: 'D', bodyText: undefined },
middleText: 'Customer/Supplier',
},
},
] as { rawLink: RawLink; link: Link }[])(
'map labels for source: $rawLink.source.type, and target: $rawLink.target.type',
({ rawLink, link }: { rawLink: RawLink; link: Link }) => {
expect(mapEdgeLabels(rawLink)).toStrictEqual(link);
}
);
});