Merge pull request #3683 from mermaid-js/sidv/TSMindmap

fix: Converts mindmapDB to TS
This commit is contained in:
Knut Sveidqvist
2022-10-19 15:47:08 +02:00
committed by GitHub
8 changed files with 47 additions and 34 deletions

View File

@@ -28,7 +28,7 @@
"scripts": { "scripts": {
"build:mermaid": "ts-node-esm --transpileOnly --project=.vite/tsconfig.json .vite/build.ts --mermaid", "build:mermaid": "ts-node-esm --transpileOnly --project=.vite/tsconfig.json .vite/build.ts --mermaid",
"build:vite": "ts-node-esm --transpileOnly --project=.vite/tsconfig.json .vite/build.ts", "build:vite": "ts-node-esm --transpileOnly --project=.vite/tsconfig.json .vite/build.ts",
"build:types": "concurrently \"tsc -p ./packages/mermaid/tsconfig.json --emitDeclarationOnly\" \"tsc -p ./packages/mermaid-mindmap/tsconfig.json --emitDeclarationOnly\"", "build:types": "tsc -p ./packages/mermaid/tsconfig.json --emitDeclarationOnly && tsc -p ./packages/mermaid-mindmap/tsconfig.json --emitDeclarationOnly",
"build:watch": "pnpm build:vite --watch", "build:watch": "pnpm build:vite --watch",
"build": "pnpm run -r clean && concurrently \"pnpm build:vite\" \"pnpm build:types\"", "build": "pnpm run -r clean && concurrently \"pnpm build:vite\" \"pnpm build:types\"",
"dev": "concurrently \"pnpm build:vite --watch\" \"ts-node-esm .vite/server.ts\"", "dev": "concurrently \"pnpm build:vite --watch\" \"ts-node-esm .vite/server.ts\"",

View File

@@ -58,6 +58,7 @@
}, },
"devDependencies": { "devDependencies": {
"concurrently": "^7.4.0", "concurrently": "^7.4.0",
"mermaid": "workspace:*",
"rimraf": "^3.0.2" "rimraf": "^3.0.2"
}, },
"resolutions": { "resolutions": {

View File

@@ -1,3 +1,5 @@
import type { MermaidConfig } from 'mermaid';
const warning = (s: string) => { const warning = (s: string) => {
// Todo remove debug code // Todo remove debug code
console.error('Log function was called before initialization', s); // eslint-disable-line console.error('Log function was called before initialization', s); // eslint-disable-line
@@ -24,7 +26,7 @@ export const log: Record<keyof typeof LEVELS, typeof console.log> = {
}; };
export let setLogLevel: (level: keyof typeof LEVELS | number | string) => void; export let setLogLevel: (level: keyof typeof LEVELS | number | string) => void;
export let getConfig: () => object; export let getConfig: () => MermaidConfig;
export let sanitizeText: (str: string) => string; export let sanitizeText: (str: string) => string;
// eslint-disable @typescript-eslint/no-explicit-any // eslint-disable @typescript-eslint/no-explicit-any
export let setupGraphViewbox: ( export let setupGraphViewbox: (

View File

@@ -1,15 +1,28 @@
/** Created by knut on 15-01-14. */ /** Created by knut on 23-07-2022. */
import { sanitizeText, getConfig, log } from './mermaidUtils'; import { sanitizeText, getConfig, log } from './mermaidUtils';
import type { DetailedError } from 'mermaid';
let nodes = []; interface Node {
id: number;
nodeId: string;
level: number;
descr: string;
type: number;
children: Node[];
width: number;
padding: number;
icon?: string;
class?: string;
}
let nodes: Node[] = [];
let cnt = 0; let cnt = 0;
export const clear = () => { export const clear = () => {
nodes = []; nodes = [];
cnt = 0; cnt = 0;
}; };
const getParent = function (level) { const getParent = function (level: number) {
for (let i = nodes.length - 1; i >= 0; i--) { for (let i = nodes.length - 1; i >= 0; i--) {
if (nodes[i].level < level) { if (nodes[i].level < level) {
return nodes[i]; return nodes[i];
@@ -22,28 +35,21 @@ const getParent = function (level) {
export const getMindmap = () => { export const getMindmap = () => {
return nodes.length > 0 ? nodes[0] : null; return nodes.length > 0 ? nodes[0] : null;
}; };
export const addNode = (level, id, descr, type) => {
export const addNode = (level: number, id: string, descr: string, type: number) => {
log.info('addNode', level, id, descr, type); log.info('addNode', level, id, descr, type);
const conf = getConfig(); const conf = getConfig();
const node = { const padding = conf.mindmap?.padding ?? 15;
id: `id-${cnt++}`, const node: Node = {
id: cnt++,
nodeId: sanitizeText(id), nodeId: sanitizeText(id),
level, level,
descr: sanitizeText(descr), descr: sanitizeText(descr),
type, type,
children: [], children: [],
width: getConfig().mindmap.maxNodeWidth, width: getConfig().mindmap?.maxNodeWidth ?? 200,
padding: type === nodeType.ROUNDED_RECT || type === nodeType.RECT ? 2 * padding : padding,
}; };
switch (node.type) {
case nodeType.ROUNDED_RECT:
node.padding = 2 * conf.mindmap.padding;
break;
case nodeType.RECT:
node.padding = 2 * conf.mindmap.padding;
break;
default:
node.padding = conf.mindmap.padding;
}
const parent = getParent(level); const parent = getParent(level);
if (parent) { if (parent) {
parent.children.push(node); parent.children.push(node);
@@ -55,9 +61,10 @@ export const addNode = (level, id, descr, type) => {
nodes.push(node); nodes.push(node);
} else { } else {
// Syntax error ... there can only bee one root // Syntax error ... there can only bee one root
let error = new Error( const error = new Error(
'There can be only one root. No parent could be found for ("' + node.descr + '")' 'There can be only one root. No parent could be found for ("' + node.descr + '")'
); );
// @ts-ignore TODO: Add mermaid error
error.hash = { error.hash = {
text: 'branch ' + name, text: 'branch ' + name,
token: 'branch ' + name, token: 'branch ' + name,
@@ -80,7 +87,7 @@ export const nodeType = {
BANG: 5, BANG: 5,
}; };
export const getType = (startStr, endStr) => { export const getType = (startStr: string, endStr: string): number => {
log.debug('In get type', startStr, endStr); log.debug('In get type', startStr, endStr);
switch (startStr) { switch (startStr) {
case '[': case '[':
@@ -98,7 +105,7 @@ export const getType = (startStr, endStr) => {
} }
}; };
export const decorateNode = (decoration) => { export const decorateNode = (decoration: { icon: string; class: string }) => {
const node = nodes[nodes.length - 1]; const node = nodes[nodes.length - 1];
if (decoration && decoration.icon) { if (decoration && decoration.icon) {
node.icon = sanitizeText(decoration.icon); node.icon = sanitizeText(decoration.icon);
@@ -108,7 +115,7 @@ export const decorateNode = (decoration) => {
} }
}; };
export const type2Str = (type) => { export const type2Str = (type: number) => {
switch (type) { switch (type) {
case nodeType.DEFAULT: case nodeType.DEFAULT:
return 'no-border'; return 'no-border';
@@ -127,12 +134,13 @@ export const type2Str = (type) => {
} }
}; };
export let parseError; export type ParseErrorFunction = (err: string | DetailedError, hash?: any) => void;
export const setErrorHandler = (handler) => { export let parseError: ParseErrorFunction;
export const setErrorHandler = (handler: ParseErrorFunction) => {
parseError = handler; parseError = handler;
}; };
// Expose logger to grammar // Expose logger to grammar
export const getLogger = () => log; export const getLogger = () => log;
export const getNodeById = (id) => nodes[id]; export const getNodeById = (id: number): Node => nodes[id];

View File

@@ -1,10 +1,9 @@
/** Created by knut on 14-12-11. */ /** Created by knut on 23-07-2022. */
import { select } from 'd3'; import { select } from 'd3';
import { log, getConfig, setupGraphViewbox } from './mermaidUtils'; import { log, getConfig, setupGraphViewbox } from './mermaidUtils';
import svgDraw, { getElementById, clearElementRefs } from './svgDraw'; import svgDraw, { getElementById, clearElementRefs } from './svgDraw';
import cytoscape from 'cytoscape'; import cytoscape from 'cytoscape';
import coseBilkent from 'cytoscape-cose-bilkent'; import coseBilkent from 'cytoscape-cose-bilkent';
import * as db from './mindmapDb';
// Inject the layout algorithm into cytoscape // Inject the layout algorithm into cytoscape
cytoscape.use(coseBilkent); cytoscape.use(coseBilkent);

View File

@@ -294,11 +294,9 @@ export const positionNode = function (node) {
nodeElem.attr('transform', 'translate(' + x + ',' + y + ')'); nodeElem.attr('transform', 'translate(' + x + ',' + y + ')');
}; };
export default { drawNode, positionNode, drawEdge };
let elements = {}; let elements = {};
export const setElementById = (id, element) => { const setElementById = (id, element) => {
elements[id] = element; elements[id] = element;
}; };
@@ -309,3 +307,5 @@ export const getElementById = (id) => {
export const clearElementRefs = () => { export const clearElementRefs = () => {
elements = {}; elements = {};
}; };
export default { drawNode, positionNode, drawEdge };

View File

@@ -2,13 +2,14 @@
* Web page integration module for the mermaid framework. It uses the mermaidAPI for mermaid * Web page integration module for the mermaid framework. It uses the mermaidAPI for mermaid
* functionality and to render the diagrams to svg code! * functionality and to render the diagrams to svg code!
*/ */
import { MermaidConfig } from './config.type'; import type { MermaidConfig } from './config.type';
import { log } from './logger'; import { log } from './logger';
import utils from './utils'; import utils from './utils';
import { mermaidAPI } from './mermaidAPI'; import { mermaidAPI } from './mermaidAPI';
import { addDetector } from './diagram-api/detectType'; import { addDetector } from './diagram-api/detectType';
import { isDetailedError } from './utils'; import { isDetailedError, DetailedError } from './utils';
export type { MermaidConfig, DetailedError };
/** /**
* ## init * ## init
* *

2
pnpm-lock.yaml generated
View File

@@ -285,6 +285,7 @@ importers:
cytoscape-cose-bilkent: ^4.1.0 cytoscape-cose-bilkent: ^4.1.0
cytoscape-fcose: ^2.1.0 cytoscape-fcose: ^2.1.0
d3: ^7.0.0 d3: ^7.0.0
mermaid: workspace:*
non-layered-tidy-tree-layout: ^2.0.2 non-layered-tidy-tree-layout: ^2.0.2
rimraf: ^3.0.2 rimraf: ^3.0.2
dependencies: dependencies:
@@ -296,6 +297,7 @@ importers:
non-layered-tidy-tree-layout: 2.0.2 non-layered-tidy-tree-layout: 2.0.2
devDependencies: devDependencies:
concurrently: 7.4.0 concurrently: 7.4.0
mermaid: link:../mermaid
rimraf: 3.0.2 rimraf: 3.0.2
packages: packages: