bpmn-js

repository·develop·Indexed 27 days ago

https://github.com/bpmn-io/bpmn-js

A BPMN 2.0 compliant modeling library and web modeler for the web, part of the bpmn-io ecosystem. It provides a toolkit to render BPMN 2.0 diagrams via the BpmnJS instance and importXML method, and includes a Viewer class for read-only embedding. The library includes utilities for managing BPMN element types, calculating SVG paths for shapes, handling label positioning, and managing BPMN categories and values.

Tokens
2.7K
Snippets
4
Records
30
Agent score
94%

What's inside bpmn-js

  1. Set up a bpmn-js development environment manually

    develop

    To set up a local development environment for bpmn-js that allows for immediate cross-project changes, follow these steps:

    1. Prerequisites: Ensure git, NodeJS, and npm are installed.
    2. Clone Repositories: Clone bpmn-js, diagram-js, and bpmn-moddle into a common directory.
    3. Link Projects: Link the dependent projects so bpmn-js uses your local versions of diagram-js and bpmn-moddle instead of npm versions.
      • OS X / Linux: Use npm link or ln -s <target> <link>.
      • Windows: Use mklink /d <link> <target>.
    4. Install Dependencies: Run npm install in each project directory.
    5. Verify: Run npm run all in each project to ensure everything is working correctly.
  2. Build and run bpmn-js development environment

    develop

    To set up the development environment, first install all dependencies using npm install.

    Depending on your goal, use one of the following commands:

    • npm run all: Builds the library and runs all tests.
    • npm start: Spins up a single local modeler instance.
    • npm run dev: Runs the full development setup.
    npm install
    
    # build the library and run all tests
    npm run all
    
    # spin up a single local modeler instance
    npm start
    
    # run the full development setup
    npm run dev
  3. Render BPMN 2.0 diagrams with BpmnJS

    develop

    To render a BPMN 2.0 diagram, create a new BpmnJS instance by providing a container (the ID of a DOM element or the element itself). Use the importXML method to load the XML content. The importXML method returns an object containing warnings and is asynchronous.

    const xml = '...'; // my BPMN 2.0 xml
    const viewer = new BpmnJS({
      container: 'body'
    });
    
    try {
      const { warnings } = await viewer.importXML(xml);
    
      console.log('rendered');
    } catch (err) {
      console.log('error rendering', err);
    }
  4. Check if an element is an Event Sub-Process using isEventSubProcess()

    develop
    Use isEventSubProcess(element) to determine if an element is an Event Sub-Process. It returns true if the element's business object has the triggeredByEvent property set to a truthy value.
  5. Generate SVG paths for shapes with getCirclePath, getRectPath, getDiamondPath, and getRoundRectPath

    develop

    Generate SVG path strings for various BPMN shape geometries based on a ShapeLike object (which must contain x, y, width, and height).

    • getCirclePath(shape): Returns a path for a circle.
    • getRectPath(shape): Returns a path for a rectangle.
    • getDiamondPath(shape): Returns a path for a diamond.
    • getRoundRectPath(shape, borderRadius): Returns a path for a rectangle with rounded corners using the provided borderRadius.
  6. Calculate element dimensions with getBounds, getWidth, and getHeight

    develop

    Utilities to extract dimensions from an element or a bounding box, with support for overrides.

    • getBounds(bounds, overrides): Returns an object { width, height }.
    • getWidth(bounds, overrides): Returns the width. If overrides.width is present, it is used; otherwise, it returns bounds.width.
    • getHeight(bounds, overrides): Returns the height. If overrides.height is present, it is used; otherwise, it returns bounds.height.
  7. Calculate label positions and bounds

    develop

    Use these utilities to calculate where labels should be positioned or how much space they occupy:

    • getFlowLabelPosition(waypoints): Calculates the optimal position for a label on a flow (like a Sequence Flow) based on its waypoints and an indentation constant.
    • getExternalLabelMid(element): Returns the center point of an element's external label.
    • getExternalLabelBounds(di, element): Returns the bounding box (Rect) of an element's label, using either the provided DI (Diagram Interchange) information or default sizes.
    • getWaypointsMid(waypoints): Returns the geometric midpoint of a set of waypoints.
  8. Get or set the text label of a BPMN element

    develop
    The getLabel(element) and setLabel(element, text) functions allow you to interact with the text content of a BPMN element. These functions automatically resolve the correct attribute based on the element type (e.g., name for FlowElements, text for TextAnnotations, or categoryValueRef for Groups).
  9. Check if a BPMN element is expanded using isExpanded()

    develop

    Use isExpanded(element, di) to determine if a BPMN element is currently expanded in the diagram.

    • For bpmn:CallActivity, it always returns false.
    • For bpmn:SubProcess, it checks the provided DI (Diagram Interchange) or retrieves it via getDi(element). It returns true if the DI is a bpmndi:BPMNPlane or if the DI object has a truthy isExpanded property.
    • For bpmn:Participant, it returns true if the associated business object has a processRef.
    • For other elements, it defaults to true.
  10. Check if a Participant or Lane is horizontal using isHorizontal()

    develop

    Use isHorizontal(element) to determine the orientation of a bpmn:Participant or bpmn:Lane.

    • Returns undefined if the element is neither a bpmn:Participant nor a bpmn:Lane.
    • Returns the isHorizontal property from the element's DI.
    • Defaults to true if the isHorizontal property is not defined on the DI.