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

Expose registerDiagram() for non-lazyLoadedDiagrams #3710

Closed
wants to merge 3 commits into from

Conversation

aloisklink
Copy link
Member

📑 Summary

Exposes the registerDiagram() function publicly as mermaid.mermaidAPI.registerDiagram so that users can add their own diagrams at bundle-time.

This is instead of using the lazyLoadedDiagrams config setting.

Resolves #3701

📏 Design Decisions

In order to get this working, I updated @mermaid-js/mermaid-mindmap so that you can now get both the id and detector by running import("@mermaid-js/mermaid-mindmap").

Example usage

import {
  diagram as mindmap,
  detector as mindmapDetector,
  id as mindmapId,
} from '@mermaid-js/mermaid-mindmap';
import {
  diagram as exampleDiagram,
  detector as exampleDiagramDetector,
  id as exampleDiagramId,
} from '@mermaid-js/mermaid-example-diagram';
import mermaid from 'mermaid';

mermaid.mermaidAPI.registerDiagram(mindmapId, mindmap, mindmapDetector);
mermaid.mermaidAPI.registerDiagram(exampleDiagramId, exampleDiagram, exampleDiagramDetector);
await mermaid.initialize({ logLevel: 0 });
await mermaid.initThrowsErrors();

Also, to make life slightly easier, registerDiagram no longer takes a callback. Instead, it checks whether the diagram contains an injectUtils function, and if it does, it uses that as the callback instead.

📋 Tasks

Make sure you

  • 📖 have read the contribution guidelines
  • 💻 have added unit/e2e tests (if appropriate)
  • 🔖 targeted develop branch
    • This PR also merges cleanly with the release_9.2.0_buggfixes branch, in case we want to back-port this to v9.2.0 as well.

The `injectUtils` function takes the utils as multiple parameters,
not an object.
Remove the callback function parameter from registerDiagram.
Instead, we can just load the callback function from the `injectUtils`
diagram definition, if it exists.
Exposes the registerDiagram() function publically as
`mermaid.mermaidAPI.registerDiagram` so that users can add their
own diagrams at bundle-time.

This is instead of using the lazyLoadedDiagrams config setting.
@aloisklink aloisklink force-pushed the feat/3701-expose-registerDiagram branch from 4cfacd1 to f41e34e Compare October 23, 2022 16:08
Copy link
Collaborator

@knsv knsv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work!

@sidharthv96
Copy link
Member

Incorporated in #3774

@Billiam
Copy link
Contributor

Billiam commented Nov 10, 2022

@sidharthv96 It seems like the change to actually export registerDiagram (https://github.com/mermaid-js/mermaid/pull/3710/files#diff-8f9cbf877a7595ee65c9cb40cbfd22234c40531a90ecccf1bb33c6ae0432edf1R484-R491) didn't make it in.

The spec for registerDiagram (using external-diagrams-mindmap.html) was added, but the html file is still using the original registerExternalDiagrams method instead: https://github.com/mermaid-js/mermaid/blob/master/cypress/platform/external-diagrams-mindmap.html#L44

Possibly failed conflict resolution?

@sidharthv96
Copy link
Member

@Billiam It was a deliberate choice not to export registerDiagram. registerExternalDiagrams was added to support the functionality.

@Billiam
Copy link
Contributor

Billiam commented Nov 10, 2022

Thanks! Sorry I missed that discussion

@aloisklink aloisklink deleted the feat/3701-expose-registerDiagram branch November 20, 2022 16:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Bundling external diagrams
4 participants