@samatawy/diagrams
    Preparing search index...

    Class DiagramView

    A class representing a diagram in 'view' mode. This class extends the base Diagram model and adds properties and methods specific to rendering and interacting with the diagram.

    It includes a coordinate system for managing transformations and a cache for storing precomputed values related to nodes for efficient rendering.

    Hierarchy (View Summary)

    Implements

    Index

    Constructors

    Properties

    Accessors

    Methods

    Constructors

    • Creates a new DiagramView instance.

      Parameters

      • id: string

        The unique identifier for the diagram.

      • target: HTMLElement | HTMLCanvasElement

        The HTML element or canvas where the diagram will be rendered.

      • Optionalinitial: Partial<Omit<IDiagram, "id">>

        Initial properties for the diagram.

      • Optionaloptions: DiagramViewOptions

        Configuration options for the diagram view.

      Returns DiagramView

    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.

    ownsCanvas: boolean
    contextMenu?: ContextMenu

    Optional context menu invoked when the user right-clicks on the canvas. Assign a DiagramContextMenu or ContextMenu instance here.

    view.contextMenu = new DiagramContextMenu(view);
    
    coordinates: CoordinateSystem
    cache: ViewCache
    host: HTMLElement
    canvas: HTMLCanvasElement
    context: CanvasRenderingContext2D
    pixelRatio: number = 1
    canvasBackgroundColor: string = DiagramConstants.CANVAS_BACKGROUND_COLOR
    event_dispatcher: EventDispatcher
    resizeObserver?: ResizeObserver
    selected_nodes: INode[] = []
    selectionOptions: DiagramSelectionOptions = ...
    fitViewport: FitViewport
    animations: DiagramAnimations
    grid: IGrid = ...
    guideOptions: DiagramGuideOptions = ...
    guides: DiagramLineGuide[] = []
    keyboardFlags: KeyboardFlags = {}
    dragStartOnNode: boolean = false
    dragPanningWithSpace: boolean = false
    hover_listener?: DiagramPointerListener
    click_listener?: DiagramPointerListener
    double_click_listener?: DiagramPointerListener

    Accessors

    • get image_assets(): Record<string, string>

      Returns Record<string, string>

    • get sheetRepository(): SheetRepository

      The diagram's sheet repository, for managing and applying spec sheets to the diagram and its nodes.

      Returns SheetRepository

      The SheetRepository instance associated with the diagram.

    • set sheetRepository(repo: SheetRepository): void

      Sets the diagram's sheet repository, allowing for the management and application of spec sheets to the diagram and its nodes.

      Parameters

      • repo: SheetRepository

        The SheetRepository instance to set for the diagram. Cannot be null or undefined.

      Returns void

    • get assetStore(): AssetStore

      The diagram's asset store, for registering and resolving image/SVG asset sources.

      Returns AssetStore

      The AssetStore instance associated with the diagram.

    • 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

    • get render_mode(): RenderMode

      Gets the active render mode for this view.

      • 'view': optimized for static viewing of diagrams, with limited interactivity and simplified rendering.
      • 'editing': optimized for user interaction and mutation of the diagram.

      Returns RenderMode

      The current render mode, either 'view' or 'editing'.

    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.

    • 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 group from the diagram. Effectively ungrouping nodes.

      Parameters

      • groupId: string

        The ID of the group to delete.

      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

    • 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.

    • 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.

    • Cleans up resources used by the DiagramView, including event listeners and the canvas element if it was created by the view.

      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

    • Returns the canvas element used by the DiagramView.

      Returns HTMLCanvasElement

    • Selects a node by its ID or node object.

      Parameters

      • node: string | INode

        The ID of the node or the node object to select.

      Returns INode | undefined

      The selected node, if any.

    • Selects a node.

      Parameters

      • node: INode

        The node to select.

      • option: "in_group" | "isolated"

        Determines whether to select related nodes or isolate the selection.

      Returns void

    • Deselects a node.

      Parameters

      • node: INode

        The node to deselect.

      • option: "in_group" | "isolated"

        Determines whether to deselect related nodes or isolate the deselection.

      Returns void

    • Selects all nodes in the diagram. Honors the enable_multi selection option to determine whether multiple nodes can be selected at once.

      Returns void

    • Toggles the selection state of a node.

      Parameters

      • node: INode

        The node to toggle.

      • option: "in_group" | "isolated"

      Returns void

      void

    • Fits the diagram content to the width of the viewport, with optional padding and alignment.

      Parameters

      • Optionaloptions: { padding?: number; alignment?: FitAlign }

        Optional configuration for padding and alignment.

      Returns void

    • Fits the diagram content to the viewport, with optional padding and alignment.

      Parameters

      • Optionaloptions: { padding?: number; alignment?: FitAlign }

        Optional configuration for padding and alignment.

      Returns void

    • Set the zoom level directly, optionally around a canvas-space center point.

      Parameters

      • zoom: number

        The desired zoom level.

      • OptionalcenterX: number

        The x coordinate (canvas space) to keep visually stable during zoom.

      • OptionalcenterY: number

        The y coordinate (canvas space) to keep visually stable during zoom.

      • mode: AnimationMode = 'instant'

        The animation mode for the zoom operation.

      Returns void

    • Zoom the diagram by multiplying current zoom by a factor, optionally around a canvas-space center point.

      Parameters

      • factor: number

        The zoom multiplier. Values greater than 1 zoom in and values between 0 and 1 zoom out.

      • OptionalcenterX: number

        The x coordinate (canvas space) to keep visually stable during zoom.

      • OptionalcenterY: number

        The y coordinate (canvas space) to keep visually stable during zoom.

      • mode: AnimationMode = 'instant'

        The animation mode for the zoom operation.

      Returns void

    • Pan the diagram by the specified delta values.

      Parameters

      • deltaX: number

        The amount to pan in the x direction.

      • deltaY: number

        The amount to pan in the y direction.

      • mode: AnimationMode = 'instant'

        The animation mode for the pan operation.

      Returns void

    • Renders the diagram onto the canvas, including any selection markers. By default, both nodes and selection markers are rendered, but this can be controlled with the what parameter. Accepts the following:

      • 'nodes' to render only nodes,
      • 'selection' to render only selection markers,
      • 'grid' to render only the grid,
      • 'guides' to render only guides, or
      • 'all' to render everything.

      Parameters

      • what: RenderScope = 'all'

        A RenderScope indicating what to render.

      Returns void

    • Parameters

      • context: CanvasRenderingContext2D

      Returns void

    • Updates the grid settings for the diagram and re-renders it to reflect the changes.

      Parameters

      • json: Partial<IGrid>

        A partial object containing the grid properties to update. Only the provided properties will be updated, while the others will remain unchanged.

      Returns void

    • Updates the guides settings for the diagram.

      Parameters

      • options: Partial<DiagramGuideOptions>

        A partial object containing the guide properties to update. Only the provided properties will be updated, while the others will remain unchanged.

      Returns void

    • Resets the viewport to the default position and zoom level, and clears the selection.

      Returns void

    • Exports the full visible diagram content as an image blob. This export is uncropped and ignores the current viewport pan/zoom. The exported image will be sized to fit the bounds of all visible nodes, plus optional padding.

      Parameters

      • options: ImageWriteOptions = {}

        The options for writing the image, including padding and MIME type.

      • serializer: ImageSerializer = ...

        The serializer to use for writing the image.

      Returns Promise<Blob>

      A promise that resolves to a Blob containing the exported image.

    • Calculate the bounding rectangle that encompasses all visible nodes in the diagram.

      Returns IRect | undefined

      The bounding rectangle of all visible nodes, or undefined if there are no visible nodes.

    • Parameters

      • target: { zoom?: number; pan?: { x: number; y: number } }
      • Optionalfunc: () => void

      Returns void

    • Type Parameters

      • T

      Parameters

      • event: string
      • listener: (event: CustomEvent<T>) => void

      Returns void

    • Type Parameters

      • T

      Parameters

      • event: string
      • listener: (event: CustomEvent<T>) => void

      Returns void

    • Returns the first node found at the specified coordinates, if any.

      Parameters

      • x: number

        The x-coordinate to test.

      • y: number

        The y-coordinate to test.

      Returns INode | undefined

    • Returns all nodes at the specified coordinates. Nodes are returned in order, so the topmost node is first in the array.

      Parameters

      • x: number

        The x-coordinate to test.

      • y: number

        The y-coordinate to test.

      Returns INode[]

      An ordered array of nodes at the specified coordinates.

    • Returns the handle at the specified coordinates, if any.

      Parameters

      • x: number

        The x-coordinate to test.

      • y: number

        The y-coordinate to test.

      • Optionaltarget: INode

        The target node to test, if any.

      Returns NodeHandle

      The handle at the specified coordinates, or NodeHandle.NONE if none is found.

    • Returns the connection-enabled handle at the specified coordinates, if any. Each node type can define its own connection handles, which may differ from the standard resize/move handles.

      Parameters

      • x: number

        The x-coordinate to test.

      • y: number

        The y-coordinate to test.

      • Optionaltarget: INode

        The target node to test, if any.

      Returns IHandlePoint

      The connection-enabled handle at the specified coordinates, or { handle: NodeHandle.NONE, point: { x: 0, y: 0 } } if none is found.

    • Select the cursor style reflecting a node handle.

      Parameters

      • handle: NodeHandle

        The node handle to determine the cursor style for.

      Returns string | undefined

      The CSS cursor style for the specified node handle.

    • Parameters

      • event: KeyboardEvent
      • isDown: boolean

      Returns void

    • Parameters

      • isDown: boolean
      • activeHint: string

      Returns void

    • Respond to pointer down events on the canvas, handling selection and panning based on the event properties and current selection options.

      Parameters

      • event: PointerEvent

        The pointer event.

      Returns void

    • Respond to pointer move events on the canvas, updating the cursor style based on hit testing and handling panning if the primary button is pressed.

      Parameters

      • event: PointerEvent

        The pointer event.

      Returns void

    • Respond to pointer up events on the canvas, releasing pointer capture and re-rendering the diagram if necessary.

      Parameters

      • event: PointerEvent

        The pointer event.

      Returns void

    • Respond to wheel events on the canvas, handling zooming when the Ctrl or Meta key is pressed and panning otherwise.

      Parameters

      • event: WheelEvent

        The wheel event.

      Returns void

    • Respond to double-click events on the canvas, invoking the double-click listener if one is set.

      Parameters

      • event: MouseEvent

        The pointer event.

      Returns void

    • Respond to keydown events on the window, currently does nothing but can be used for keyboard shortcuts in the future.

      Parameters

      • event: KeyboardEvent

        The keyboard event.

      Returns void

    • Respond to keyup events on the window.

      Parameters

      • event: KeyboardEvent

        The keyboard event.

      Returns void

    • Respond to context menu events on the canvas, preventing the default context menu from appearing.

      Parameters

      • event: MouseEvent

        The pointer event.

      Returns void