refactor(mermaid): remove registerDiagram cb func

Remove the callback function parameter from registerDiagram.
Instead, we can just load the callback function from the `injectUtils`
diagram definition, if it exists.
This commit is contained in:
Alois Klink
2022-10-23 14:49:50 +01:00
parent 41249fd064
commit 89d3d297b7
2 changed files with 11 additions and 11 deletions

View File

@@ -94,7 +94,7 @@ export const getDiagramFromText = async (txt: string, parseError?: Function): Pr
} }
// Diagram not available, loading it // Diagram not available, loading it
const { diagram } = await loader(); const { diagram } = await loader();
registerDiagram(type, diagram, undefined, diagram.injectUtils); registerDiagram(type, diagram, undefined);
// new diagram will try getDiagram again and if fails then it is a valid throw // 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 // If either of the above worked, we have the diagram

View File

@@ -22,17 +22,16 @@ export interface Detectors {
[key: string]: DiagramDetector; [key: string]: DiagramDetector;
} }
/**
*
* @param id
* @param diagram
* @param detector
*/
export const registerDiagram = ( export const registerDiagram = (
id: string, id: string,
diagram: DiagramDefinition, diagram: DiagramDefinition,
detector?: DiagramDetector, detector?: DiagramDetector
callback?: (
_log: any,
_setLogLevel: any,
_getConfig: any,
_sanitizeText: any,
_setupGraphViewbox: any
) => void
) => { ) => {
if (diagrams[id]) { if (diagrams[id]) {
throw new Error(`Diagram ${id} already registered.`); throw new Error(`Diagram ${id} already registered.`);
@@ -42,8 +41,9 @@ export const registerDiagram = (
addDetector(id, detector); addDetector(id, detector);
} }
addStylesForDiagram(id, diagram.styles); addStylesForDiagram(id, diagram.styles);
if (typeof callback !== 'undefined') {
callback(log, setLogLevel, getConfig, sanitizeText, setupGraphViewbox); if (diagram.injectUtils) {
diagram.injectUtils(log, setLogLevel, getConfig, sanitizeText, setupGraphViewbox);
} }
}; };