UNPKG

@gravity-ui/graph

Version:

Modern graph editor component

144 lines (143 loc) 6.12 kB
import { GraphComponent } from "../../components/canvas/GraphComponent"; import { ESelectionStrategy, ISelectionBucket, TMultiEntitySelection, TSelectionEntityId } from "./types"; /** * Service responsible for managing selection across different entity types * Acts as a central registry and coordinator for ISelectionBucket instances * * The SelectionService manages selection state for all graph elements. * During selection operations, the strategy determines how the selection will change: * - REPLACE: replaces current selection with new one, affects all entity types * - APPEND: adds new elements to current selection * - SUBTRACT: removes elements from current selection * - TOGGLE: toggles selection state of elements * * Supports two APIs: * 1. Single-type selection: select(entityType, ids, strategy) * 2. Multi-type selection: select({ type1: ids1, type2: ids2 }, strategy) * * @example * ```typescript * const selectionService = new SelectionService(); * * // Single entity type selection * selectionService.registerBucket(new MultipleSelectionBucket<string>("block")); * selectionService.select("block", ["1", "2", "3"], ESelectionStrategy.REPLACE); * selectionService.$selection.value; // { block: ["1", "2", "3"] } * * // Multi-entity type selection * selectionService.registerBucket(new MultipleSelectionBucket<string>("connection")); * selectionService.select({ * block: ["1", "2"], * connection: ["4", "5"] * }, ESelectionStrategy.REPLACE); * selectionService.$selection.value; // { block: ["1", "2"], connection: ["4", "5"] } * ``` */ export declare class SelectionService { /** * Map of entity types to their corresponding selection buckets */ private buckets; /** * Computed signal aggregating selection from all buckets */ readonly $selection: import("@preact/signals-core").ReadonlySignal<Map<string, Set<TSelectionEntityId>>>; /** * Computed signal aggregating resolved entities from all buckets * Returns a flat array of all selected entities that have resolvers */ readonly $selectedEntities: import("@preact/signals-core").ReadonlySignal<unknown[]>; /** * Computed signal aggregating GraphComponent instances from all buckets * Returns a flat array of all selected GraphComponent instances that have resolvers and return GraphComponent */ readonly $selectedComponents: import("@preact/signals-core").ReadonlySignal<GraphComponent<import("../../components/canvas/GraphComponent").TGraphComponentProps, import("../..").TComponentState, import("../../components/canvas/GraphComponent").GraphComponentContext>[]>; /** * Registers a selection bucket for a specific entity type * @param bucket The selection bucket to register * @returns void */ registerBucket(bucket: ISelectionBucket): void; /** * Unregisters a selection bucket for a specific entity type * * @param bucket The selection bucket to unregister * @returns void */ unregisterBucket(bucket: ISelectionBucket): void; /** * Retrieves the selection bucket for a specific entity type * * @param entityType The entity type to get the bucket for * @returns {ISelectionBucket | undefined} The selection bucket or undefined if not found */ getBucket(entityType: string): ISelectionBucket | undefined; getBucketByElement(element: GraphComponent): ISelectionBucket | undefined; selectRelatedElements(elements: GraphComponent[], strategy: ESelectionStrategy): void; /** * Selects entities of a specific type according to the specified strategy * * @param entityType The type of entities to select * @param ids The IDs of the entities to select * @param strategy The selection strategy to apply * @returns void */ select(entityType: string, ids: TSelectionEntityId[], strategy: ESelectionStrategy): void; /** * Selects entities across multiple entity types according to the specified strategy * * @param selection Object mapping entity types to arrays of IDs to select * @param strategy The selection strategy to apply * @returns void */ select(selection: TMultiEntitySelection, strategy: ESelectionStrategy): void; /** * Deselects entities of a specific type * * @param entityType The type of entities to deselect * @param ids The IDs of the entities to deselect * @returns void */ deselect(entityType: string, ids: TSelectionEntityId[]): void; /** * Deselects entities across multiple entity types * * @param selection Object mapping entity types to arrays of IDs to deselect * @returns void */ deselect(selection: TMultiEntitySelection): void; /** * Checks if an entity is currently selected * @param entityType The type of the entity * @param id The ID of the entity * @returns true if selected, false otherwise */ isSelected(entityType: string, id: TSelectionEntityId): boolean; /** * Checks if an entity is currently selected * @param entityType The type of the entity * @param id The ID of the entity * @returns true if selected, false otherwise */ isSelected(entityType: string, id: TSelectionEntityId): boolean; /** * Checks selection status for multiple entities across different types * @param queries Object mapping entity types to IDs to check * @returns Object mapping entity types to boolean selection status */ isSelected(queries: TMultiEntitySelection): Record<string, boolean>; /** * Resets the selection for a specific entity type * @param entityType The type of entities to reset selection for */ resetSelection(entityType: string): void; /** * Resets the selection for multiple entity types * @param entityTypes Array of entity types to reset selection for */ resetSelection(entityTypes: string[]): void; /** * Resets the selection for all registered buckets */ resetAllSelections(): void; }