Newer
Older
import { graph, state, renderer } from "../editor";
import * as Graph from "../graph";
import jquery from "jquery";
/**
* Only one instance of this should exist, since box-delete has to work on a global scale.
*/
var deleteToolInstance = undefined; // Used for box delete
export default class DeleteTool extends Tool {
super("Delete", "delete", key, new ToolMenu());
this.setupBoxSelect();
if (deleteToolInstance === undefined) {
deleteToolInstance = this;
}
}
onBoxSelect(left, bottom, top, right) {
// Filter out selected nodes
const hitNodes = [];
const tl = renderer.screen2GraphCoords(left, top);
const br = renderer.screen2GraphCoords(right, bottom);
graph.data[Graph.GRAPH_NODES].forEach((node) => {
if (
tl.x < node.x &&
node.x < br.x &&
br.y > node.y &&
node.y > tl.y
) {
hitNodes.push(node);
}
});
// Delete selected items after confirmation
console.log("DELETE");
console.log(hitNodes);
graph.deleteNode(node[Graph.NODE_ID]);
if (state.selectedItem == node) {
state.setSelectedItem(undefined);
}
graph.deleteLink(
link[Graph.LINK_SOURCE][Graph.NODE_ID],
link[Graph.LINK_TARGET][Graph.NODE_ID]
);
if (state.selectedItem == link) {
state.setSelectedItem(undefined);
}
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
setupBoxSelect() {
window.addEventListener("load", () => {
// Source: https://github.com/vasturiano/force-graph/issues/151#issuecomment-735850938
// forceGraph element is the element provided to the Force Graph Library
jquery("#2d-graph").on(
"pointerdown",
this.boxSelectOnPointerDown
);
jquery("#2d-graph").on(
"pointermove",
this.boxSelectOnPointerMove
);
jquery("#2d-graph").on(
"pointerup",
this.boxSelectOnPointerUp
);
});
}
boxSelectOnPointerDown(e) {
if (!e.shiftKey) {
return;
}
e.preventDefault();
this.boxSelect = document.createElement("div");
this.boxSelect.id = "box-select";
this.boxSelect.style.left = e.offsetX.toString() + "px";
this.boxSelect.style.top = e.offsetY.toString() + "px";
this.boxSelectStart = {
x: e.offsetX,
y: e.offsetY,
};
// app element is the element just above the forceGraph element.
jquery("#box-select-layer").append(this.boxSelect);
}
boxSelectOnPointerMove(e) {
if (!this.boxSelect) {
return;
}
if (!e.shiftKey) {
this.boxSelect.remove();
return;
}
e.preventDefault();
if (e.offsetX < this.boxSelectStart.x) {
this.boxSelect.style.left = e.offsetX.toString() + "px";
this.boxSelect.style.width =
(this.boxSelectStart.x - e.offsetX).toString() + "px";
} else {
this.boxSelect.style.left = this.boxSelectStart.x.toString() + "px";
this.boxSelect.style.width =
(e.offsetX - this.boxSelectStart.x).toString() + "px";
}
if (e.offsetY < this.boxSelectStart.y) {
this.boxSelect.style.top = e.offsetY.toString() + "px";
this.boxSelect.style.height =
(this.boxSelectStart.y - e.offsetY).toString() + "px";
} else {
this.boxSelect.style.top = this.boxSelectStart.y.toString() + "px";
this.boxSelect.style.height =
(e.offsetY - this.boxSelectStart.y).toString() + "px";
}
}
boxSelectOnPointerUp(e) {
if (!this.boxSelect) {
return;
}
if (!e.shiftKey) {
this.boxSelect.remove();
return;
}
e.preventDefault();
let left, bottom, top, right;
if (e.offsetX < this.boxSelectStart.x) {
left = e.offsetX;
right = this.boxSelectStart.x;
} else {
left = this.boxSelectStart.x;
right = e.offsetX;
}
if (e.offsetY < this.boxSelectStart.y) {
top = e.offsetY;
bottom = this.boxSelectStart.y;
} else {
top = this.boxSelectStart.y;
bottom = e.offsetY;
}
this.boxSelect.remove();
deleteToolInstance.onBoxSelect(left, bottom, top, right);
}