mirror of
https://github.com/mermaid-js/mermaid.git
synced 2025-11-14 01:34:19 +01:00
#3061 Lazy loading auto derived path
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import * as configApi from './config';
|
||||
import { log } from './logger';
|
||||
import { getDiagram } from './diagram-api/diagramAPI';
|
||||
import { detectType } from './diagram-api/detectType';
|
||||
import { getDiagram, loadDiagram } from './diagram-api/diagramAPI';
|
||||
import { detectType, getPathForDiagram } from './diagram-api/detectType';
|
||||
import { isDetailedError } from './utils';
|
||||
export class Diagram {
|
||||
type = 'graph';
|
||||
@@ -67,3 +67,21 @@ export class Diagram {
|
||||
}
|
||||
|
||||
export default Diagram;
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/ban-types
|
||||
export const getDiagramFromText = async (txt: string, parseError?: Function) => {
|
||||
const type = detectType(txt, configApi.getConfig());
|
||||
try {
|
||||
// Trying to find the diagram
|
||||
getDiagram(type);
|
||||
} catch (error) {
|
||||
// Diagram not avaiable, loading it
|
||||
const path = getPathForDiagram(type);
|
||||
// await loadDiagram('./packages/mermaid-mindmap/dist/mermaid-mindmap.js');
|
||||
await loadDiagram(path + 'mermaid-' + type + '.js');
|
||||
// new diagram will try getDiagram again and if fails then it is a valid throw
|
||||
}
|
||||
// If either of the above worked, we have the diagram
|
||||
// logic and can continue
|
||||
return new Diagram(txt, parseError);
|
||||
};
|
||||
|
||||
@@ -39,8 +39,6 @@ const edgeInCluster = (edge, clusterId) => {
|
||||
log.debug('Tilt, ', clusterId, ',not in decendants');
|
||||
return false;
|
||||
}
|
||||
log.info('Here ');
|
||||
|
||||
if (decendants[clusterId].indexOf(edge.v) >= 0) return true;
|
||||
if (isDecendant(edge.v, clusterId)) return true;
|
||||
if (isDecendant(edge.w, clusterId)) return true;
|
||||
|
||||
@@ -1,12 +1,13 @@
|
||||
import { MermaidConfig } from '../config.type';
|
||||
|
||||
export type DiagramDetector = (text: string, config?: MermaidConfig) => boolean;
|
||||
export type DetectorRecord = { detector: DiagramDetector; path: string };
|
||||
|
||||
const directive =
|
||||
/[%]{2}[{]\s*(?:(?:(\w+)\s*:|(\w+))\s*(?:(?:(\w+))|((?:(?![}][%]{2}).|\r?\n)*))?\s*)(?:[}][%]{2})?/gi;
|
||||
const anyComment = /\s*%%.*\n/gm;
|
||||
|
||||
const detectors: Record<string, DiagramDetector> = {};
|
||||
const detectors: Record<string, DetectorRecord> = {};
|
||||
|
||||
/**
|
||||
* @function detectType Detects the type of the graph text. Takes into consideration the possible
|
||||
@@ -36,8 +37,8 @@ export const detectType = function (text: string, config?: MermaidConfig): strin
|
||||
|
||||
// console.log(detectors);
|
||||
|
||||
for (const [key, detector] of Object.entries(detectors)) {
|
||||
if (detector(text, config)) {
|
||||
for (const [key, detectorRecord] of Object.entries(detectors)) {
|
||||
if (detectorRecord.detector(text, config)) {
|
||||
return key;
|
||||
}
|
||||
}
|
||||
@@ -46,6 +47,13 @@ export const detectType = function (text: string, config?: MermaidConfig): strin
|
||||
return 'flowchart';
|
||||
};
|
||||
|
||||
export const addDetector = (key: string, detector: DiagramDetector) => {
|
||||
detectors[key] = detector;
|
||||
export const addDetector = (key: string, detector: DiagramDetector, path: string) => {
|
||||
detectors[key] = { detector, path };
|
||||
};
|
||||
|
||||
export const getPathForDiagram = (id: string) => {
|
||||
const detectorRecord = detectors[id];
|
||||
if (detectorRecord) {
|
||||
return detectorRecord.path;
|
||||
}
|
||||
};
|
||||
|
||||
@@ -112,7 +112,7 @@ const registerDiagramAndDetector = (
|
||||
detector: DiagramDetector
|
||||
) => {
|
||||
registerDiagram(id, diagram);
|
||||
registerDetector(id, detector);
|
||||
registerDetector(id, detector, '');
|
||||
};
|
||||
|
||||
export const addDiagrams = () => {
|
||||
@@ -136,9 +136,9 @@ export const addDiagrams = () => {
|
||||
init: () => {
|
||||
// no op
|
||||
},
|
||||
}
|
||||
},
|
||||
(text) => text.toLowerCase().trim() === 'error'
|
||||
);
|
||||
registerDetector('error', (text) => text.toLowerCase().trim() === 'error');
|
||||
|
||||
registerDiagramAndDetector(
|
||||
'c4',
|
||||
|
||||
@@ -19,7 +19,7 @@ describe('DiagramAPI', () => {
|
||||
const detector: DiagramDetector = (str: string) => {
|
||||
return str.match('loki') !== null;
|
||||
};
|
||||
registerDetector('loki', detector);
|
||||
registerDetector('loki', detector, '');
|
||||
registerDiagram(
|
||||
'loki',
|
||||
{
|
||||
|
||||
@@ -32,8 +32,8 @@ export interface Detectors {
|
||||
[key: string]: DiagramDetector;
|
||||
}
|
||||
|
||||
export const registerDetector = (id: string, detector: DiagramDetector) => {
|
||||
addDetector(id, detector);
|
||||
export const registerDetector = (id: string, detector: DiagramDetector, path: string) => {
|
||||
addDetector(id, detector, path);
|
||||
};
|
||||
|
||||
export const registerDiagram = (
|
||||
@@ -61,3 +61,19 @@ export const getDiagram = (name: string): DiagramDefinition => {
|
||||
}
|
||||
throw new Error(`Diagram ${name} not found.`);
|
||||
};
|
||||
|
||||
/**
|
||||
*
|
||||
* @param sScriptSrc
|
||||
*/
|
||||
export const loadDiagram = (sScriptSrc: string) =>
|
||||
new Promise((resolve) => {
|
||||
const oHead = document.getElementsByTagName('HEAD')[0];
|
||||
const oScript = document.createElement('script');
|
||||
oScript.type = 'text/javascript';
|
||||
oScript.src = sScriptSrc;
|
||||
oHead.appendChild(oScript);
|
||||
oScript.onload = () => {
|
||||
resolve(true);
|
||||
};
|
||||
});
|
||||
|
||||
@@ -54,8 +54,8 @@ const init = function (
|
||||
) {
|
||||
try {
|
||||
log.info('Detectors in init', mermaid.detectors); // eslint-disable-line
|
||||
mermaid.detectors.forEach(({ id, detector }) => {
|
||||
addDetector(id, detector);
|
||||
mermaid.detectors.forEach(({ id, detector, path }) => {
|
||||
addDetector(id, detector, path);
|
||||
});
|
||||
initThrowsErrors(config, nodes, callback);
|
||||
} catch (e) {
|
||||
|
||||
@@ -22,7 +22,7 @@ import classDb from './diagrams/class/classDb';
|
||||
import flowDb from './diagrams/flowchart/flowDb';
|
||||
import flowRenderer from './diagrams/flowchart/flowRenderer';
|
||||
import ganttDb from './diagrams/gantt/ganttDb';
|
||||
import Diagram from './Diagram';
|
||||
import Diagram, { getDiagramFromText } from './Diagram';
|
||||
import errorRenderer from './diagrams/error/errorRenderer';
|
||||
import { attachFunctions } from './interactionDb';
|
||||
import { log, setLogLevel } from './logger';
|
||||
@@ -115,12 +115,12 @@ export const decodeEntities = function (text: string): string {
|
||||
* element will be removed when rendering is completed.
|
||||
* @returns {void}
|
||||
*/
|
||||
const render = function (
|
||||
const render = async function (
|
||||
id: string,
|
||||
text: string,
|
||||
cb: (svgCode: string, bindFunctions?: (element: Element) => void) => void,
|
||||
container?: Element
|
||||
): void {
|
||||
): Promise<void> {
|
||||
if (!hasLoadedDiagrams) {
|
||||
addDiagrams();
|
||||
hasLoadedDiagrams = true;
|
||||
@@ -232,7 +232,8 @@ const render = function (
|
||||
let diag;
|
||||
let parseEncounteredException;
|
||||
try {
|
||||
diag = new Diagram(text);
|
||||
// diag = new Diagram(text);
|
||||
diag = await getDiagramFromText(text);
|
||||
} catch (error) {
|
||||
diag = new Diagram('error');
|
||||
parseEncounteredException = error;
|
||||
|
||||
Reference in New Issue
Block a user