Skip to content

Commit ca2f958

Browse files
committed
feat: better paste, resolves #346
1 parent a1698cf commit ca2f958

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed

js/browser/graph.ts

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { timer } from "../timer";
77
import { NODE } from "../node";
88
import { progress } from "./progress";
99
import { View } from "./view";
10-
import type { Core, Collection, NodeCollection, EdgeCollection, NodeSingular } from "cytoscape";
10+
import type { Core, Collection, NodeCollection, EdgeCollection, NodeSingular, EdgeSingular } from "cytoscape";
1111
import cytoscape from "cytoscape"; //eslint-disable-line no-duplicate-imports
1212
import type { Menu } from "./menu";
1313
import log from "loglevel";
@@ -318,7 +318,8 @@ export class Graph {
318318
return this.getElementsByIds(nodes.map((node) => node.id())) as unknown as NodeCollection;
319319
}
320320

321-
/** @param ids - iterable of cytoscape ids
321+
/** Get a collection of nodes/edges using a list of their IDs. Works only in this graph.
322+
* @param ids - iterable of cytoscape ids
322323
* @returns cytoscape collection of elements with those ids */
323324
getElementsByIds(ids: Iterable<string>): Collection {
324325
const own = this.cy.collection();
@@ -328,6 +329,32 @@ export class Graph {
328329
}
329330
return own;
330331
}
332+
/** Get a collection of nodes/edges using a list of their IDs.
333+
* If not existant in this graph, then all others are scanned.
334+
* @param ids - iterable of cytoscape ids
335+
* @returns cytoscape collection of elements with those ids */
336+
getElementsByAllMeansNecessary(ids: Iterable<string>): Collection {
337+
const own = this.cy.collection();
338+
for (const id of ids) {
339+
let ele = this.cy.getElementById(id);
340+
if (ele.length === 0) {
341+
// get out the big guns
342+
// search every view for this id
343+
for (const view of View.views()) {
344+
ele = view.state.cy.getElementById(id);
345+
// if the query does not return an empty collection, we found the element
346+
if (ele.length !== 0) {
347+
console.debug("Found!");
348+
// add it to our graph!
349+
this.cy.add(ele);
350+
break;
351+
}
352+
}
353+
}
354+
own.merge(ele);
355+
}
356+
return own;
357+
}
331358
/** Returns the start node for all path operations
332359
@returns the start node for all path operations, or null if none exists. */
333360
getSource(): NodeSingular | null {

js/browser/main.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ function initKeyListener(): void {
2929
}
3030
// Copy
3131
if (e.code === "KeyS" || e.code === "KeyC") {
32-
const selected = layoutState.cy.nodes(":selected");
32+
const selected = layoutState.cy.elements(":selected");
3333
if (selected.size() === 0) {
3434
return;
3535
} // do nothing when nothing selected
@@ -42,7 +42,7 @@ function initKeyListener(): void {
4242
if (e.code === "KeyP" || e.code === "KeyV") {
4343
layoutState.cy.startBatch();
4444
layoutState.cy.elements().unselect();
45-
const nodes = layoutState.graph.getElementsByIds(clipboard) as unknown as NodeCollection;
45+
const nodes = layoutState.graph.getElementsByAllMeansNecessary(clipboard) as unknown as NodeCollection;
4646
Graph.setVisible(nodes, true);
4747

4848
layoutState.cy.endBatch();

0 commit comments

Comments
 (0)