Skip to content

Commit

Permalink
feat: better paste, resolves #346
Browse files Browse the repository at this point in the history
  • Loading branch information
Yagnap committed Sep 2, 2024
1 parent a1698cf commit ca2f958
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
31 changes: 29 additions & 2 deletions js/browser/graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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<string>): Collection {
const own = this.cy.collection();
Expand All @@ -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<string>): 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 {
Expand Down
4 changes: 2 additions & 2 deletions js/browser/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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();
Expand Down

0 comments on commit ca2f958

Please sign in to comment.