LiveFlow.State (LiveFlow v0.2.3)

Copy Markdown View Source

Main state container for LiveFlow.

The state holds all nodes, edges, viewport information, and selection state. It provides CRUD operations for nodes and edges, as well as selection management.

Fields

  • :nodes - Map of node ID to LiveFlow.Node structs
  • :edges - Map of edge ID to LiveFlow.Edge structs
  • :viewport - Current LiveFlow.Viewport state
  • :selected_nodes - Set of selected node IDs
  • :selected_edges - Set of selected edge IDs

Examples

iex> state = LiveFlow.State.new()
iex> node = LiveFlow.Node.new("1", %{x: 0, y: 0}, %{label: "Start"})
iex> state = LiveFlow.State.add_node(state, node)
iex> Map.keys(state.nodes)
["1"]

Summary

Functions

Adds an edge to the state.

Adds multiple edges to the state.

Adds a node to the state.

Adds multiple nodes to the state.

Calculates bounds of all nodes.

Clears all selection (nodes and edges).

Deletes all selected nodes and edges.

Deselects a node by ID.

Checks if an edge already exists between two nodes.

Gets all edges connected to a node.

Gets all edges as a list.

Gets an edge by ID.

Gets a node by ID.

Creates a new empty state.

Gets all nodes as a list.

Removes an edge by ID.

Removes multiple edges by ID.

Removes a node by ID. Also removes all edges connected to this node.

Removes multiple nodes by ID.

Selects all nodes and edges.

Selects an edge by ID.

Selects a node by ID.

Selects multiple nodes by ID.

Gets all selected edges.

Gets all selected nodes.

Sets the viewport.

Updates an edge by ID with the given attributes.

Updates a node by ID with the given attributes or function.

Updates the viewport with the given values.

Types

t()

@type t() :: %LiveFlow.State{
  edges: %{required(String.t()) => LiveFlow.Edge.t()},
  nodes: %{required(String.t()) => LiveFlow.Node.t()},
  selected_edges: MapSet.t(String.t()),
  selected_nodes: MapSet.t(String.t()),
  viewport: LiveFlow.Viewport.t()
}

Functions

add_edge(state, edge)

@spec add_edge(t(), LiveFlow.Edge.t()) :: t()

Adds an edge to the state.

add_edges(state, edges)

@spec add_edges(t(), [LiveFlow.Edge.t()]) :: t()

Adds multiple edges to the state.

add_node(state, node)

@spec add_node(t(), LiveFlow.Node.t()) :: t()

Adds a node to the state.

add_nodes(state, nodes)

@spec add_nodes(t(), [LiveFlow.Node.t()]) :: t()

Adds multiple nodes to the state.

bounds(state)

@spec bounds(t()) :: map() | nil

Calculates bounds of all nodes.

clear_selection(state)

@spec clear_selection(t()) :: t()

Clears all selection (nodes and edges).

delete_selected(state)

@spec delete_selected(t()) :: t()

Deletes all selected nodes and edges.

deselect_node(state, id)

@spec deselect_node(t(), String.t()) :: t()

Deselects a node by ID.

edge_exists?(state, source, target, source_handle \\ nil, target_handle \\ nil)

@spec edge_exists?(t(), String.t(), String.t(), String.t() | nil, String.t() | nil) ::
  boolean()

Checks if an edge already exists between two nodes.

edges_for_node(state, node_id)

@spec edges_for_node(t(), String.t()) :: [LiveFlow.Edge.t()]

Gets all edges connected to a node.

edges_list(state)

@spec edges_list(t()) :: [LiveFlow.Edge.t()]

Gets all edges as a list.

get_edge(state, id)

@spec get_edge(t(), String.t()) :: LiveFlow.Edge.t() | nil

Gets an edge by ID.

get_node(state, id)

@spec get_node(t(), String.t()) :: LiveFlow.Node.t() | nil

Gets a node by ID.

new(opts \\ [])

@spec new(keyword()) :: t()

Creates a new empty state.

Options

  • :nodes - Initial nodes (list or map)
  • :edges - Initial edges (list or map)
  • :viewport - Initial viewport

Examples

iex> LiveFlow.State.new()
%LiveFlow.State{nodes: %{}, edges: %{}}

iex> nodes = [LiveFlow.Node.new("1", %{x: 0, y: 0}, %{})]
iex> state = LiveFlow.State.new(nodes: nodes)
iex> Map.keys(state.nodes)
["1"]

nodes_list(state)

@spec nodes_list(t()) :: [LiveFlow.Node.t()]

Gets all nodes as a list.

remove_edge(state, id)

@spec remove_edge(t(), String.t()) :: t()

Removes an edge by ID.

remove_edges(state, ids)

@spec remove_edges(t(), [String.t()]) :: t()

Removes multiple edges by ID.

remove_node(state, id)

@spec remove_node(t(), String.t()) :: t()

Removes a node by ID. Also removes all edges connected to this node.

remove_nodes(state, ids)

@spec remove_nodes(t(), [String.t()]) :: t()

Removes multiple nodes by ID.

select_all(state)

@spec select_all(t()) :: t()

Selects all nodes and edges.

select_edge(state, id, opts \\ [])

@spec select_edge(t(), String.t(), keyword()) :: t()

Selects an edge by ID.

select_node(state, id, opts \\ [])

@spec select_node(t(), String.t(), keyword()) :: t()

Selects a node by ID.

Options

  • :multi - If true, adds to selection. If false, replaces selection (default: false)

select_nodes(state, ids)

@spec select_nodes(t(), [String.t()]) :: t()

Selects multiple nodes by ID.

selected_edges_list(state)

@spec selected_edges_list(t()) :: [LiveFlow.Edge.t()]

Gets all selected edges.

selected_nodes_list(state)

@spec selected_nodes_list(t()) :: [LiveFlow.Node.t()]

Gets all selected nodes.

set_viewport(state, viewport)

@spec set_viewport(t(), LiveFlow.Viewport.t()) :: t()

Sets the viewport.

update_edge(state, id, attrs)

@spec update_edge(t(), String.t(), keyword()) :: t()

Updates an edge by ID with the given attributes.

update_node(state, id, fun)

@spec update_node(
  t(),
  String.t(),
  keyword() | (LiveFlow.Node.t() -> LiveFlow.Node.t())
) :: t()

Updates a node by ID with the given attributes or function.

When passed a keyword list, updates the node with those attributes. When passed a function, applies the function to the node.

update_viewport(state, attrs)

@spec update_viewport(t(), map()) :: t()

Updates the viewport with the given values.