There are several ways to load the diagramming library, depending on your project's needs. This guide covers the most common approaches:
Choose the method that best fits your development setup.
The library is organized into six main modules or namespaces: Collections, Controls, Drawing, Diagramming, Animations and Graphs. In the UMD distribution, these are accessed via the global `MindFusion` object (e.g., `MindFusion.Diagramming`). When using ES modules or npm packages, you'll typically import these modules directly. You will see these concepts in action in the examples below.
For more information on JavaScript modules see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules.
UMD is a pattern of universal module definition for JavaScript modules. These modules are capable of working irrespective of the executing environment. In web browsers, it exposes the module functionality to the global scope.
In order to load the UMD scripts, add references to files from diagram distrbution's /umd folder using <script> tags. Note that the order of the scripts matters: controls.js must be loaded after collections and drawing, and diagramming.js must be loaded after animations and graphs. After these scripts are loaded, you can access the global MindFusion namespace object from your code.
HTML
Copy Code
|
|---|
<!DOCTYPE html> |
Diagram's ES6 modules conform to modern JavaScript module definition that supports native import and export statements. You could import these modules directly, without using any additional module/package libraries or tools, by defining your script file as a module via <script type="module"> tag:
HTML
Copy Code
|
|---|
<script src="app.js" type="module"></script> |
In order to make the diagram's ES6 modules available for importing, add the following importmap, referencing the scripts, contained in distribution's /esm folder. The purpose of the importmap is to define a map of module names which allow using import specifiers such as import {x} from "@mindfusion/diagramming" (instead of a relative path to the corresponding script file), both internally and in in your code.
HTML
Copy Code
|
|---|
<script type="importmap"> |
Now you can consume the modules you need by importing them into your app.js:
JavaScript
Copy Code
|
|---|
import * as Diagramming from '@mindfusion/diagramming'; |
As of late 2024, importmap is now supported by all major modern browsers, including Chrome, Firefox, Edge, and Safari. For compatibility with older browsers, you can still use a polyfill like the https://github.com/guybedford/es-module-shims library.You can always check the latest support details at https://caniuse.com/import-maps.
There are six npm packages that provide the functionality of MindFusion.Diagramming, each containing the corresponding module. To use JsDiagram in a project, you only need to install the @mindfusion/diagramming package, the rest will be automatically added as dependencies:
npm install @mindfusion/diagramming
Each of the packages contains three distributions - ES module, CommonJs module and UMD, that you can use depending on your project setup.
To consume a module as a CommonJS module, use the 'require' statement. The CommonJs version of the module will be loaded from the /dist/cjs folder, as specified in the package.json's main field.
JavaScript
Copy Code
|
|---|
const Diagramming = require('@mindfusion/diagramming'); |
To consume a module as an ES6 module, use the 'import' statement. The ES6 version of the module will be loaded from the /dist/esm folder, as specified in the package.json's module field.
JavaScript
Copy Code
|
|---|
import * as Diagramming from '@mindfusion/diagramming'; |
A sample webpack bundle configuration is shown in Samples\MinApp\TypeScript\WebPack folder of distribution:
package.json
Copy Code
|
|---|
... |