From ca2f9581de03397d5c52aa0b0bd75bd8e04b64fd Mon Sep 17 00:00:00 2001 From: "Hannes R. Brunsch" Date: Mon, 2 Sep 2024 14:22:39 +0200 Subject: [PATCH] feat: better paste, resolves #346 --- js/browser/graph.ts | 31 +++++++++++++++++++++++++++++-- js/browser/main.ts | 4 ++-- 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/js/browser/graph.ts b/js/browser/graph.ts index 949154cc..7f77153e 100644 --- a/js/browser/graph.ts +++ b/js/browser/graph.ts @@ -7,7 +7,7 @@ import { timer } from "../timer"; import { NODE } from "../node"; import { progress } from "./progress"; import { View } from "./view"; -import type { Core, Collection, NodeCollection, EdgeCollection, NodeSingular } from "cytoscape"; +import type { Core, Collection, NodeCollection, EdgeCollection, NodeSingular, EdgeSingular } from "cytoscape"; import cytoscape from "cytoscape"; //eslint-disable-line no-duplicate-imports import type { Menu } from "./menu"; import log from "loglevel"; @@ -318,7 +318,8 @@ export class Graph { return this.getElementsByIds(nodes.map((node) => node.id())) as unknown as NodeCollection; } - /** @param ids - iterable of cytoscape ids + /** Get a collection of nodes/edges using a list of their IDs. Works only in this graph. + * @param ids - iterable of cytoscape ids * @returns cytoscape collection of elements with those ids */ getElementsByIds(ids: Iterable): Collection { const own = this.cy.collection(); @@ -328,6 +329,32 @@ export class Graph { } return own; } + /** Get a collection of nodes/edges using a list of their IDs. + * If not existant in this graph, then all others are scanned. + * @param ids - iterable of cytoscape ids + * @returns cytoscape collection of elements with those ids */ + getElementsByAllMeansNecessary(ids: Iterable): Collection { + const own = this.cy.collection(); + for (const id of ids) { + let ele = this.cy.getElementById(id); + if (ele.length === 0) { + // get out the big guns + // search every view for this id + for (const view of View.views()) { + ele = view.state.cy.getElementById(id); + // if the query does not return an empty collection, we found the element + if (ele.length !== 0) { + console.debug("Found!"); + // add it to our graph! + this.cy.add(ele); + break; + } + } + } + own.merge(ele); + } + return own; + } /** Returns the start node for all path operations @returns the start node for all path operations, or null if none exists. */ getSource(): NodeSingular | null { diff --git a/js/browser/main.ts b/js/browser/main.ts index 67d297cb..82a9c030 100644 --- a/js/browser/main.ts +++ b/js/browser/main.ts @@ -29,7 +29,7 @@ function initKeyListener(): void { } // Copy if (e.code === "KeyS" || e.code === "KeyC") { - const selected = layoutState.cy.nodes(":selected"); + const selected = layoutState.cy.elements(":selected"); if (selected.size() === 0) { return; } // do nothing when nothing selected @@ -42,7 +42,7 @@ function initKeyListener(): void { if (e.code === "KeyP" || e.code === "KeyV") { layoutState.cy.startBatch(); layoutState.cy.elements().unselect(); - const nodes = layoutState.graph.getElementsByIds(clipboard) as unknown as NodeCollection; + const nodes = layoutState.graph.getElementsByAllMeansNecessary(clipboard) as unknown as NodeCollection; Graph.setVisible(nodes, true); layoutState.cy.endBatch();