From 677dcbdbccf4d9203cc276744e083f29f4424ba9 Mon Sep 17 00:00:00 2001 From: AlexIchenskiy Date: Wed, 13 Mar 2024 10:53:18 +0100 Subject: [PATCH 1/4] New: Add removal functions --- src/models/graph.ts | 31 +++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/src/models/graph.ts b/src/models/graph.ts index a28c7bf..af351c0 100644 --- a/src/models/graph.ts +++ b/src/models/graph.ts @@ -263,7 +263,6 @@ export class Graph implements IGraph) { const nodeIds = data.nodeIds ?? []; const edgeIds = data.edgeIds ?? []; @@ -274,7 +273,35 @@ export class Graph implements IGraph node.id); + const edgeIds = this._edges.getAll().map((edge) => edge.id); + + this._removeNodes(nodeIds); + this._removeEdges(edgeIds); + + if (this._settings && this._settings.onRemoveData) { + this._settings.onRemoveData({ nodeIds, edgeIds }); + } + } + + removeAllEdges() { + const edgeIds = this._edges.getAll().map((edge) => edge.id); + + this._removeEdges(edgeIds); + + if (this._settings && this._settings.onRemoveData) { + this._settings.onRemoveData({ edgeIds }); + } + } + + removeAllNodes() { + this.removeAll(); } isEqual(graph: Graph): boolean { From 53c615d97c651b0591dda868df9f106d78444a6a Mon Sep 17 00:00:00 2001 From: AlexIchenskiy Date: Thu, 14 Mar 2024 10:54:09 +0100 Subject: [PATCH 2/4] Fix: Add missing callback data --- src/models/graph.ts | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/src/models/graph.ts b/src/models/graph.ts index af351c0..fba2764 100644 --- a/src/models/graph.ts +++ b/src/models/graph.ts @@ -47,7 +47,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<{ nodeIds: any[]; edgeIds: any[] }>) => void; } export class Graph implements IGraph { @@ -267,14 +267,19 @@ export class Graph implements IGraph removedData.edgeIds.indexOf(edgeId) < 0), + ); this._applyEdgeOffsets(); this._applyStyle(); if (this._settings && this._settings.onRemoveData) { - this._settings.onRemoveData(data); + this._settings.onRemoveData(removedData); } } @@ -282,22 +287,13 @@ export class Graph implements IGraph node.id); const edgeIds = this._edges.getAll().map((edge) => edge.id); - this._removeNodes(nodeIds); - this._removeEdges(edgeIds); - - if (this._settings && this._settings.onRemoveData) { - this._settings.onRemoveData({ nodeIds, edgeIds }); - } + this.remove({ nodeIds, edgeIds }); } removeAllEdges() { const edgeIds = this._edges.getAll().map((edge) => edge.id); - this._removeEdges(edgeIds); - - if (this._settings && this._settings.onRemoveData) { - this._settings.onRemoveData({ edgeIds }); - } + this.remove({ edgeIds }); } removeAllNodes() { @@ -501,7 +497,7 @@ export class Graph implements IGraph implements IGraph implements IGraph Date: Thu, 14 Mar 2024 20:27:13 +0100 Subject: [PATCH 3/4] Chore: Refactor remove return values --- src/models/graph.ts | 33 +++++++++++++++++++++------------ src/utils/array.utils.ts | 5 +++++ 2 files changed, 26 insertions(+), 12 deletions(-) diff --git a/src/models/graph.ts b/src/models/graph.ts index fba2764..0244771 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: any[]; edgeIds: any[] }>) => void; + onRemoveData?: (data: Partial) => void; } export class Graph implements IGraph { @@ -263,17 +272,17 @@ export class Graph implements IGraph) { + remove(data: Partial) { const nodeIds = data.nodeIds ?? []; const edgeIds = data.edgeIds ?? []; - const removedData = this._removeNodes(nodeIds); - const removedEdgeIds = this._removeEdges(edgeIds); + const removedNodesData = this._removeNodes(nodeIds); + const removedEdgesData = this._removeEdges(edgeIds); - // Merge edges removed by removing nodes and by removing edges, ensuring there are no duplicate edge IDs. - removedData.edgeIds = removedData.edgeIds.concat( - removedEdgeIds.filter((edgeId) => removedData.edgeIds.indexOf(edgeId) < 0), - ); + const removedData = { + nodeIds: dedupArrays(removedNodesData.nodeIds, removedEdgesData.nodeIds), + edgeIds: dedupArrays(removedNodesData.edgeIds, removedEdgesData.edgeIds), + }; this._applyEdgeOffsets(); this._applyStyle(); @@ -497,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)); +}; From 558c55210da2833ec4ee14645958b78550d42acf Mon Sep 17 00:00:00 2001 From: AlexIchenskiy Date: Thu, 14 Mar 2024 21:02:22 +0100 Subject: [PATCH 4/4] Chore: Refactor remove function type usage --- src/models/graph.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/models/graph.ts b/src/models/graph.ts index 0244771..31fc586 100644 --- a/src/models/graph.ts +++ b/src/models/graph.ts @@ -279,15 +279,15 @@ export class Graph implements IGraph