From e9a46329cb0868ef157ad502e7ced2074f771a91 Mon Sep 17 00:00:00 2001 From: Oleksandr Ichenskyi <55350107+AlexIchenskiy@users.noreply.github.com> Date: Mon, 18 Mar 2024 14:30:51 +0100 Subject: [PATCH] New: Add removal functions (#96) * New: Add removal functions * Fix: Add missing callback data * Chore: Refactor remove return values * Chore: Refactor remove function type usage --- src/models/graph.ts | 54 +++++++++++++++++++++++++++++++++------- src/utils/array.utils.ts | 5 ++++ 2 files changed, 50 insertions(+), 9 deletions(-) diff --git a/src/models/graph.ts b/src/models/graph.ts index a28c7bf..31fc586 100644 --- a/src/models/graph.ts +++ b/src/models/graph.ts @@ -5,12 +5,18 @@ import { IGraphStyle } from './style'; import { ImageHandler } from '../services/images'; import { getEdgeOffsets } from './topology'; import { IEntityState, EntityState } from '../utils/entity.utils'; +import { dedupArrays } from '../utils/array.utils'; export interface IGraphData { nodes: N[]; edges: E[]; } +export interface IGraphObjectsIds { + nodeIds: any[]; + edgeIds: any[]; +} + export type IEdgeFilter = (edge: IEdge) => boolean; export type INodeFilter = (node: INode) => boolean; @@ -33,7 +39,10 @@ export interface IGraph { setup(data: Partial>): void; clearPositions(): void; merge(data: Partial>): void; - remove(data: Partial<{ nodeIds: number[]; edgeIds: number[] }>): void; + remove(data: Partial): void; + removeAll(): void; + removeAllNodes(): void; + removeAllEdges(): void; isEqual(graph: Graph): boolean; getBoundingBox(): IRectangle; getNearestNode(point: IPosition): INode | undefined; @@ -47,7 +56,7 @@ export interface IGraphSettings { onLoadedImages?: () => void; onSetupData?: (data: Partial>) => void; onMergeData?: (data: Partial>) => void; - onRemoveData?: (data: Partial<{ nodeIds: number[]; edgeIds: number[] }>) => void; + onRemoveData?: (data: Partial) => void; } export class Graph implements IGraph { @@ -263,18 +272,41 @@ export class Graph implements IGraph) { + remove(data: Partial) { const nodeIds = data.nodeIds ?? []; const edgeIds = data.edgeIds ?? []; - this._removeNodes(nodeIds); - this._removeEdges(edgeIds); + const removedNodesData = this._removeNodes(nodeIds); + const removedEdgesData = this._removeEdges(edgeIds); this._applyEdgeOffsets(); this._applyStyle(); - this._settings?.onRemoveData?.(data); + if (this._settings && this._settings.onRemoveData) { + const removedData: IGraphObjectsIds = { + nodeIds: dedupArrays(removedNodesData.nodeIds, removedEdgesData.nodeIds), + edgeIds: dedupArrays(removedNodesData.edgeIds, removedEdgesData.edgeIds), + }; + + this._settings.onRemoveData(removedData); + } + } + + removeAll() { + const nodeIds = this._nodes.getAll().map((node) => node.id); + const edgeIds = this._edges.getAll().map((edge) => edge.id); + + this.remove({ nodeIds, edgeIds }); + } + + removeAllEdges() { + const edgeIds = this._edges.getAll().map((edge) => edge.id); + + this.remove({ edgeIds }); + } + + removeAllNodes() { + this.removeAll(); } isEqual(graph: Graph): boolean { @@ -474,7 +506,7 @@ export class Graph implements IGraph implements IGraph implements IGraph(array: Array): Array => { } return newArray; }; + +export const dedupArrays = (...arrays: T[][]): T[] => { + const combinedArray = arrays.reduce((acc, curr) => acc.concat(curr), []); + return Array.from(new Set(combinedArray)); +};