UNPKG

@gravity-ui/graph

Version:

Modern graph editor component

240 lines (239 loc) 8.29 kB
import { AnchorState } from "store/anchor/Anchor"; import { TAnchor, TAnchorId } from "../../components/canvas/anchors"; import { Block, TBlock } from "../../components/canvas/blocks/Block"; import { Graph } from "../../graph"; import { MultipleSelectionBucket } from "../../services/selection/MultipleSelectionBucket"; import { SingleSelectionBucket } from "../../services/selection/SingleSelectionBucket"; import { ESelectionStrategy } from "../../services/selection/types"; import { RootStore } from "../index"; import { BlockState, TBlockId } from "./Block"; declare module "../../graphEvents" { interface GraphEventsDefinitions { /** * * Emited when selection of blocks changes. * Preventing the event will prevent selection change. * */ "blocks-selection-change": (event: SelectionEvent<TBlockId>) => void; /** * Emited when selection of block's anchor changes. * Preventing the event will prevent selection change. * */ "block-anchor-selection-change": (event: CustomEvent<{ /** Block anchor */ anchor: TAnchor; /** Is anchor selected */ selected: boolean; }>) => void; /** * Emited when block changes. * * Preventing the event will prevent block change. */ "block-change": (event: CustomEvent<{ /** Changed block */ block: TBlock; }>) => void; } } /** * Storage for managing blocks state */ export declare class BlockListStore { rootStore: RootStore; protected graph: Graph; $blocksMap: import("@preact/signals-core").Signal<Map<TBlockId, BlockState<TBlock>>>; $blocks: import("@preact/signals-core").Signal<BlockState<TBlock>[]>; /** * This signal is used to store blocks in reactive state. * this signal fired for each change of the block state. * * NOTE: Please do not use it before you know what you are doing. */ $blocksReactiveState: import("@preact/signals-core").ReadonlySignal<BlockState<TBlock>[]>; /** * Bucket for managing block selection state * Resolves IDs to BlockState instances (not GraphComponent) */ readonly blockSelectionBucket: MultipleSelectionBucket<TBlockId, BlockState>; /** * Computed signal that returns selected blocks as Block GraphComponent instances * Automatically resolves BlockState to Block components via getViewComponent() * Use this when you need to work with rendered Block components */ $selectedBlockComponents: import("@preact/signals-core").ReadonlySignal<Block<TBlock<{}>, import("../../components/canvas/blocks/Block").TBlockProps>[]>; readonly anchorSelectionBucket: SingleSelectionBucket<TAnchorId, AnchorState>; /** * @deprecated Use blockSelectionBucket.$selectedEntities instead * Computed signal that returns the currently selected blocks */ $selectedBlocks: import("@preact/signals-core").ReadonlySignal<BlockState<TBlock>[]>; /** * @deprecated Use anchorSelectionBucket.$selectedEntities instead * Computed signal that returns the currently selected anchor */ $selectedAnchor: import("@preact/signals-core").ReadonlySignal<AnchorState>; constructor(rootStore: RootStore, graph: Graph); /** * Sets anchor selection * * @param blockId {BlockState["id"]} Block id * @param anchorId {AnchorState["id"]} Anchor id * @param selected {boolean} Selected * @returns void */ setAnchorSelection(blockId: BlockState["id"], anchorId: AnchorState["id"], selected: boolean): void; /** * Checks if a block is selected * * @param blockId {BlockState["id"]} Block id * @returns {boolean} Is selected */ isSelectedBlock(blockId: BlockState["id"]): boolean; protected unsetAnchorsSelection(): void; /** * Updates block position * * @event block-change * @param id {BlockState["id"]} Block id * @param nextState {{x: number, y: number}} Next state * @returns void */ updatePosition(id: BlockState["id"], nextState: Pick<TBlock, "x" | "y">): void; protected updateBlocksMap(blocks: Map<BlockState["id"], BlockState> | [BlockState["id"], BlockState][]): void; /** * Adds block * * If a block with this id already exists, it will be updated. * If id is not provided, a random id will be generated. * * @param block {Omit<TBlock, "id"> & { id?: TBlockId }} Block to add * @returns void */ addBlock(block: Omit<TBlock, "id"> & { id?: TBlockId; }): TBlockId; /** * Deletes blocks * * @param blocks {TBlock["id"] | TBlock} Blocks to delete * @returns void */ deleteBlocks(blocks: (TBlock["id"] | TBlock)[]): void; /** * Updates blocks state * * If block with this id already exists, it will be updated. * Otherwise, a new block will be created. * * @param blocks {TBlock[]} Blocks to update * @returns void */ updateBlocks(blocks: TBlock[]): void; /** * Sets blocks state * * @param blocks {TBlock[]} Blocks to set * @returns void */ setBlocks(blocks: TBlock[]): void; protected getOrCraeateBlockState(block: TBlock): BlockState<TBlock>; protected applyBlocksState(blocks: BlockState[]): void; /** * Updates block selection using the SelectionService * * @param ids Block IDs to update selection for * @param selected Whether to select or deselect * @param strategy The selection strategy to apply * * @returns void */ updateBlocksSelection(ids: TBlockId[], selected: boolean, strategy?: ESelectionStrategy): void; /** * Gets connections of a block * * Method search connection with source/target block id. * If you connect blocks via custom ports, this method will not work. * * @param blockId {TBlockId} Block id * @returns {ConnectionState[]} Connections */ getBlockConnections(blockId: TBlockId): import("../..").ConnectionState<{ id?: import("../..").TConnectionId; sourceBlockId?: TBlockId; targetBlockId?: TBlockId; sourceAnchorId?: string; targetAnchorId?: string; sourcePortId?: import("../connection/port/Port").TPortId; targetPortId?: import("../connection/port/Port").TPortId; label?: string; styles?: Partial<import("../../graphConfig").TConnectionColors> & { dashes?: number[]; }; dashed?: boolean; selected?: boolean; }>[]; /** * Resets block selection * * @returns void */ resetSelection(): void; /** * Deletes selected blocks * * @returns void */ deleteSelectedBlocks(): void; /** * Deletes all connections of a block * * Method search connection with source/target block id. * If you connect blocks via custom ports, this method will not work. * * @param blockId {TBlockId} Block id * @returns void */ deleteAllBlockConnections(blockId: TBlockId): void; reset(): void; /** * Gets blocks as JSON * * @returns {TBlock[]} Blocks */ toJSON(): TBlock[]; /** * Gets block state by id * * @param id {TBlockId} Block id * @returns {BlockState | undefined} Block state */ getBlockState(id: TBlockId): BlockState<TBlock>; /** * Gets block by id * * @param id {TBlockId} Block id * @returns {TBlock | undefined} Block */ getBlock(id: TBlockId): TBlock | undefined; /** * Gets blocks by ids * * If block with this id does not exist, it will filtered out. * * @param ids {BlockState["id"][]} Block ids * @returns {TBlock[]} Blocks */ getBlocks(ids: BlockState["id"][]): TBlock[]; /** * Gets block states by ids * * If block with this id does not exist, it will filtered out. * * @param ids {BlockState["id"][]} Block ids * @returns {BlockState[]} Block states */ getBlockStates(ids: BlockState["id"][]): BlockState<TBlock>[]; }