Skip to content

How to run mermaid.js with vaadin-js-loader #1

Open
@martin-schaefer

Description

@martin-schaefer

I had a hard time making mermaid.js run with Vaadin. Neither @NpmPackage nor @JsModule worked but with vaadin-js-loader it works and that's why I just wantet to drop the solution here.

The code below will load and kick in mermaid so it will render all diagrams on <pre>/<div> elements with class: "mermaid".

Thanks for this little gem!

import com.vaadin.flow.component.AttachEvent;
import com.vaadin.flow.component.html.Div;
import com.vaadin.flow.component.html.Pre;
import com.vaadin.flow.router.PageTitle;
import com.vaadin.flow.router.Route;
import org.parttio.vaadinjsloader.JSLoader;

@Route("mermaid")
@PageTitle("MermaidViewer")
public class MermaidView extends Div {

    private String flowchart = "flowchart TB\n" +
                       "  ui([Angular App])-->|REST|bff(Spring Boot App)\n" +
                       "  subgraph Cluster\n" +
                       "    direction LR\n" +
                       "    bff-->|JDBC|db[(PostgreSQL)]\n" +
                       "  end";

    public MermaidView() {
        Pre pre = new Pre();
        pre.setClassName("mermaid");
        pre.setText(flowchart);
        add(pre);
    }

    @Override
    protected void onAttach(AttachEvent attachEvent) {
        super.onAttach(attachEvent);
        JSLoader.loadCdnjs(attachEvent.getUI(),"mermaid", "10.9.1");
        attachEvent.getUI().getPage().executeJs("mermaid.run();");
    }

}

Metadata

Metadata

Assignees

No one assigned

    Labels

    TipUsage tips

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions