@samatawy/diagrams
    Preparing search index...

    Class Diagram

    A diagram is the core data structure that holds all nodes, layers, and metadata. It provides methods for managing these elements and supports serialization and deserialization for persistence and data exchange.

    It does not support viewing. For that, use DiagramView which extends this base model and adds rendering, selection, and interaction capabilities. It does not support editing. For that, use DiagramController which manages user interactions and updates the Diagram model accordingly.

    Hierarchy (View Summary)

    Implements

    Index

    Constructors

    Properties

    id: string

    The unique identifier of the diagram, which is used to reference the diagram within the application and for serialization purposes.

    nodes: INode[]

    An array of nodes that belong to the diagram, which represent the entities and elements that can be connected and manipulated within the diagram.

    groups: IGroup[]

    An optional array of groups that belong to the diagram, which can be used to organize nodes into visual collections. Grouped nodes maintain their relative positions.

    layers: ILayer[] = []

    An array of layers that belong to the diagram, which can be used to organize nodes into different visual layers.

    sheet_id?: string

    An optional identifier for the sheet associated with the diagram, which can be used to apply specific styles and configurations to the diagram's nodes and connections.

    background?: FillStyle

    An optional background fill style for the diagram, which can be used to set the background color or gradient of the diagram area.

    meta?: Record<string, any>

    An optional metadata object for the diagram, which can be used to store additional information about the diagram that is not part of its core structure. This can include custom properties, annotations, or any other relevant data that is not related to diagram rendering.

    Accessors

    • get metadata(): Record<string, any>

      Gets the metadata object for the diagram. If no metadata exists, an empty object is returned.

      Returns Record<string, any>

      The metadata object for the diagram.

    • set metadata(data: Record<string, any>): void

      Sets the metadata object for the diagram. If a null or undefined value is provided, the metadata is cleared.

      Parameters

      • data: Record<string, any>

        The metadata object to set for the diagram.

      Returns void

    Methods

    • Retrieves a node by its ID.

      Parameters

      • id: string

        The ID of the node to retrieve.

      Returns INode | undefined

      The node with the specified ID, or undefined if not found.

    • Returns a group by its ID.

      Parameters

      • id: string

        The ID of the group to return.

      Returns IGroup | undefined

      The group with the specified ID, or undefined if not found.

    • Retrieves a layer by its ID.

      Parameters

      • id: string

        The ID of the layer to retrieve.

      Returns ILayer | undefined

      The layer with the specified ID, or undefined if not found.

    • Inserts a new group or updates an existing group in the diagram.

      Parameters

      • group: string | IGroup

        The group to upsert, or the ID of the group to create.

      Returns IGroup

      The upserted group.

    • Inserts a new layer or updates an existing layer in the diagram.

      Parameters

      • layer: string | ILayer

        The layer to upsert, or the ID of the layer to create.

      Returns ILayer

      The upserted layer.

    • Add or remove a node from a group. If the groupId is undefined, the node is removed from any group it belongs to. A node can only belong to one group at a time. If the node is already in a different group, it will be removed from that group before being added to the new group.

      Parameters

      • node: string | INode

        The node or node ID to add to the group.

      • groupId: string | undefined

        The ID of the group to add the node to, or undefined to remove the node from any group.

      Returns void

    • Add or remove a node from a layer. If the layerId is undefined, the node is removed from any layer it belongs to. A node can only belong to one layer at a time. If the node is already in a different layer, it will be removed from that layer before being added to the new layer.

      Parameters

      • node: string | INode

        The node or node ID to add to the layer.

      • layerId: string | undefined

        The ID of the layer to add the node to, or undefined to remove the node from any layer.

      Returns void

    • Deletes a node from the diagram.

      Parameters

      • nodeId: string

        The ID of the node to delete.

      Returns void

    • Deletes a group from the diagram. Effectively ungrouping nodes.

      Parameters

      • groupId: string

        The ID of the group to delete.

      Returns void

    • Deletes a layer from the diagram.

      Parameters

      • layerId: string

        The ID of the layer to delete.

      Returns void

    • Assigns an image source to a node for background/content rendering.

      Parameters

      • node: string | INode
      • imageSrc: string
      • mode: ImageMode = 'contain'
      • OptionalimageId: string

      Returns INode | undefined

    • Assigns SVG markup or SVG source URL/data URL to a node. Raw SVG markup is converted to a data URL.

      Parameters

      • node: string | INode
      • svgOrSrc: string
      • mode: ImageMode = 'contain'
      • OptionalimageId: string

      Returns INode | undefined

    • Resolves the concrete image source for a node image reference.

      Parameters

      Returns string | undefined

    • Clears all in-memory image assets. Intended for teardown paths.

      Returns void

    • Clears all nodes, layers, metadata, and assets from the diagram, effectively resetting it to an empty state while retaining the same ID. Intended for reuse scenarios where the diagram instance should be preserved but its content should be cleared.

      Returns void

    • Gets the value of a specific metadata key for the diagram.

      Parameters

      • Optionalkey: string

        The key of the metadata to retrieve.

      Returns any

      The value of the specified metadata key, or undefined if the key does not exist.

    • Sets the value of a specific metadata key for the diagram.

      Parameters

      • key: string

        The key of the metadata to set.

      • value: any

        The value to set for the specified metadata key.

      Returns void

    • Deletes a specific metadata key from the diagram.

      Parameters

      • Optionalkey: string

        The key of the metadata to delete. If not provided, all metadata for the diagram is deleted.

      Returns void

    • Gets the value of a specific metadata key for a node.

      Parameters

      • node: string | INode

        The node or node ID to retrieve metadata from.

      • key: string

        The key of the metadata to retrieve.

      Returns any

      The value of the specified metadata key, or undefined if the key does not exist.

    • Sets the value of a specific metadata key for a node.

      Parameters

      • node: string | INode

        The node or node ID to set metadata for.

      • key: string

        The key of the metadata to set.

      • value: any

        The value to set for the specified metadata key.

      Returns void

    • Deletes a specific metadata key from a node.

      Parameters

      • node: string | INode

        The node or node ID to delete metadata from.

      • Optionalkey: string

        The key of the metadata to delete. If not provided, all metadata for the node is deleted.

      Returns void

    • Loads the diagram data from a serialized JSON object.

      Parameters

      • source: string | ISerializedDiagram

        The serialized diagram data.

      • Optionalserializer: ISerializer

        The serializer to use for reading the data (optional if source is already an object).

      Returns Promise<Diagram>

      The diagram instance.

      An error if the input is an invalid JSON string or if the parsed JSON is null or undefined.

    • Exports the current diagram in a native format. Supported formats are JSON text, UTF-8 bytes, and Blob (browser runtime).

      Parameters

      • format: DiagramExportFormat = 'json'

        The format to export the diagram in ('json', 'bytes', or 'blob'). (defaults to 'json')

      • pretty: boolean = true

        Whether to pretty-print the output (applicable to JSON format). (defaults to true)

      • serializer: ISerializer = jsonSerializer

        The serializer to use for exporting the diagram. (optional)

      Returns string | Blob | Uint8Array<ArrayBufferLike>

      The exported diagram data in the specified format.

    • Saves the current diagram directly from the model. In Node.js this writes to the file system; in browsers this triggers a file download.

      Parameters

      • options: DiagramSaveOptions = {}

        The options for saving the diagram, including path, file name, serializer, pretty-printing, and MIME type.

      Returns Promise<string>

      A promise that resolves to the path or download URL of the saved file.

    • Parameters

      • target: INode
      • imageSrc: string
      • mode: ImageMode = 'contain'
      • OptionalimageId: string

      Returns void

    • Returns all nodes within the specified layer.

      Parameters

      • layer: ILayer

        The layer whose nodes should be returned.

      Returns INode[]

      An array of nodes within the specified layer.

    • Creates a new layer with the specified properties.

      Parameters

      • id: string

        The ID of the layer.

      • name: string = id

        The name of the layer.

      • visible: boolean = true

        Whether the layer is visible.

      • nodes: string[] = []

        The nodes within the layer.

      Returns ILayer

      The created layer.