cytoscape-js-node-resizer 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Gabriel Xará
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,2 @@
1
+ A zero dependency extension for Cytoscape.js that adds node resizing functionality.
2
+ ```
package/package.json ADDED
@@ -0,0 +1,19 @@
1
+ {
2
+ "name": "cytoscape-js-node-resizer",
3
+ "version": "1.0.0",
4
+ "description": "A zero dependency extension for Cytoscape.js that adds node resizing functionality.",
5
+ "main": "resizeHandler.js",
6
+ "scripts": {
7
+ "test": "echo \"Error: no test specified\" && exit 1"
8
+ },
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "git+https://github.com/gxara/cytoscape-js-node-resizer.git"
12
+ },
13
+ "author": "gxara",
14
+ "license": "MIT",
15
+ "bugs": {
16
+ "url": "https://github.com/gxara/cytoscape-js-node-resizer/issues"
17
+ },
18
+ "homepage": "https://github.com/gxara/cytoscape-js-node-resizer#readme"
19
+ }
@@ -0,0 +1,268 @@
1
+ (function () {
2
+ "use strict";
3
+
4
+ let activeNode = null;
5
+
6
+ const createResizeHandle = (node, cy) => {
7
+ let resizeBox = document.getElementById("resize-box");
8
+
9
+ activeNode = node;
10
+
11
+ const styles = {
12
+ grapperColor: "#7711C0",
13
+ };
14
+
15
+ if (!resizeBox) {
16
+ // Resizer Outline
17
+ resizeBox = document.createElement("div");
18
+ resizeBox.id = "resize-box";
19
+ resizeBox.style.width = node.style("width");
20
+ resizeBox.style.height = node.style("height");
21
+ resizeBox.style.position = "absolute";
22
+ resizeBox.style.border = "1px dashed #999";
23
+ cy.container().appendChild(resizeBox);
24
+
25
+ const parentDiv = resizeBox;
26
+
27
+ // Grapper Handle - Bottom Right
28
+ let resizeHandleBottomRight = document.createElement("div");
29
+ resizeHandleBottomRight.id = "resize-handle-bottom-right";
30
+ resizeHandleBottomRight.style.width = "8px";
31
+ resizeHandleBottomRight.style.height = "8px";
32
+ resizeHandleBottomRight.style.border = `"4px solid ${styles.grapperColor}"`;
33
+ resizeHandleBottomRight.style.position = "absolute";
34
+ resizeHandleBottomRight.style.cursor = "nwse-resize";
35
+ parentDiv.appendChild(resizeHandleBottomRight);
36
+
37
+ // Grapper Handle - Bottom Left
38
+ let resizeHandleBottomLeft = document.createElement("div");
39
+ resizeHandleBottomLeft.id = "resize-handle-bottom-left";
40
+ resizeHandleBottomLeft.style.width = "8px";
41
+ resizeHandleBottomLeft.style.height = "8px";
42
+ resizeHandleBottomLeft.style.border = `"4px solid ${styles.grapperColor}"`;
43
+ resizeHandleBottomLeft.style.position = "absolute";
44
+ resizeHandleBottomLeft.style.cursor = "nesw-resize";
45
+ parentDiv.appendChild(resizeHandleBottomLeft);
46
+
47
+ // Grapper Handle - Top Right
48
+ let resizeHandleTopRight = document.createElement("div");
49
+ resizeHandleTopRight.id = "resize-handle-top-right";
50
+ resizeHandleTopRight.style.width = "8px";
51
+ resizeHandleTopRight.style.height = "8px";
52
+ resizeHandleTopRight.style.border = `"4px solid ${styles.grapperColor}"`;
53
+ resizeHandleTopRight.style.position = "absolute";
54
+ resizeHandleTopRight.style.cursor = "nesw-resize";
55
+ parentDiv.appendChild(resizeHandleTopRight);
56
+
57
+ // Grapper Handle - Top Left
58
+ let resizeHandleTopLeft = document.createElement("div");
59
+ resizeHandleTopLeft.id = "resize-handle-top-left";
60
+ resizeHandleTopLeft.style.width = "8px";
61
+ resizeHandleTopLeft.style.height = "8px";
62
+ resizeHandleTopLeft.style.border = `"4px solid ${styles.grapperColor}"`;
63
+ resizeHandleTopLeft.style.position = "absolute";
64
+ resizeHandleTopLeft.style.cursor = "nwse-resize";
65
+ parentDiv.appendChild(resizeHandleTopLeft);
66
+ }
67
+
68
+ let isResizing = false;
69
+ let initialX = 0;
70
+ let initialY = 0;
71
+ let initialWidth = 0;
72
+ let initialHeight = 0;
73
+ let initialPositionX = 0;
74
+ let initialPositionY = 0;
75
+ let activeCorner = null;
76
+
77
+ for (const pos of [
78
+ "bottom-right",
79
+ "bottom-left",
80
+ "top-right",
81
+ "top-left",
82
+ ]) {
83
+ const resizeHandler = document.getElementById(`resize-handle-${pos}`);
84
+
85
+ resizeHandler.onmousedown = function (e) {
86
+ e.stopPropagation();
87
+ isResizing = true;
88
+ activeCorner = pos;
89
+ document.body.classList.add("no-select"); // Disable text selection
90
+ initialX = e.clientX;
91
+ initialY = e.clientY;
92
+ initialWidth = parseFloat(node.style("width"));
93
+ initialHeight = parseFloat(node.style("height"));
94
+ initialPositionX = node.position("x");
95
+ initialPositionY = node.position("y");
96
+ };
97
+
98
+ resizeHandler.onmouseup = function () {
99
+ if (isResizing) {
100
+ document.body.classList.remove("no-select"); // Re-enable text selection
101
+ isResizing = false;
102
+ }
103
+ };
104
+ }
105
+
106
+ document.onmousemove = function (e) {
107
+ if (isResizing) {
108
+ // Calculate deltas adjusted for zoom level
109
+ let deltaX = (e.clientX - initialX) / cy.zoom();
110
+ let deltaY = (e.clientY - initialY) / cy.zoom();
111
+
112
+ // Initialize new position and dimensions
113
+ let newNodePosition = { x: initialPositionX, y: initialPositionY };
114
+ let newDimensions = { width: initialWidth, height: initialHeight };
115
+
116
+ // Adjust position and dimensions based on the active corner
117
+ switch (activeCorner) {
118
+ case "top-left":
119
+ // Move top-left corner, fix bottom-right
120
+ newNodePosition.x = initialPositionX + deltaX / 2;
121
+ newNodePosition.y = initialPositionY + deltaY / 2;
122
+ newDimensions.width = Math.max(10, initialWidth - deltaX);
123
+ newDimensions.height = Math.max(10, initialHeight - deltaY);
124
+ break;
125
+
126
+ case "top-right":
127
+ // Move top-right corner, fix bottom-left
128
+ newNodePosition.x = initialPositionX + deltaX / 2;
129
+ newNodePosition.y = initialPositionY + deltaY / 2;
130
+ newDimensions.width = Math.max(10, initialWidth + deltaX);
131
+ newDimensions.height = Math.max(10, initialHeight - deltaY);
132
+ break;
133
+
134
+ case "bottom-left":
135
+ // Move bottom-left corner, fix top-right
136
+ newNodePosition.x = initialPositionX + deltaX / 2;
137
+ newNodePosition.y = initialPositionY + deltaY / 2;
138
+ newDimensions.width = Math.max(10, initialWidth - deltaX);
139
+ newDimensions.height = Math.max(10, initialHeight + deltaY);
140
+ break;
141
+
142
+ case "bottom-right":
143
+ // Move bottom-right corner, fix top-left
144
+ newNodePosition.x = initialPositionX + deltaX / 2;
145
+ newNodePosition.y = initialPositionY + deltaY / 2;
146
+ newDimensions.width = Math.max(10, initialWidth + deltaX);
147
+ newDimensions.height = Math.max(10, initialHeight + deltaY);
148
+ break;
149
+ }
150
+ // Apply new dimensions and position to the node
151
+ node.position(newNodePosition);
152
+ node.style(newDimensions);
153
+
154
+ positionHandle(node);
155
+ cy.trigger("resizeHandler.nodeUpdated", [
156
+ node,
157
+ newDimensions,
158
+ newNodePosition,
159
+ ]);
160
+ }
161
+ };
162
+
163
+ document.onmouseup = function () {
164
+ if (isResizing) {
165
+ document.body.classList.remove("no-select"); // Re-enable text selection
166
+ isResizing = false;
167
+ }
168
+ };
169
+
170
+ return;
171
+ };
172
+
173
+ const positionHandle = () => {
174
+ const resizeBox = document.getElementById("resize-box");
175
+
176
+ if (!resizeBox) return;
177
+
178
+ if (!activeNode) return;
179
+
180
+ const pos = activeNode.renderedPosition();
181
+
182
+ const width = parseFloat(activeNode.renderedStyle("width"));
183
+ const height = parseFloat(activeNode.renderedStyle("height"));
184
+
185
+ resizeBox.style.left = `${pos.x}px`;
186
+ resizeBox.style.top = `${pos.y}px`;
187
+ resizeBox.style.width = `${width}px`;
188
+ resizeBox.style.height = `${height}px`;
189
+ resizeBox.style.transform = `translate(-${width / 2}px, -${height / 2}px)`;
190
+
191
+ const resizeHandleBottomRight = document.getElementById(
192
+ "resize-handle-bottom-right"
193
+ );
194
+ resizeHandleBottomRight.style.left = `${width - 4}px`;
195
+ resizeHandleBottomRight.style.top = `${height - 4}px`;
196
+
197
+ const resizeHandleBottomLeft = document.getElementById(
198
+ "resize-handle-bottom-left"
199
+ );
200
+ resizeHandleBottomLeft.style.left = `-4px`;
201
+ resizeHandleBottomLeft.style.top = `${height - 4}px`;
202
+
203
+ const resizeHandleTopRight = document.getElementById(
204
+ "resize-handle-top-right"
205
+ );
206
+ resizeHandleTopRight.style.left = `${width - 4}px`;
207
+ resizeHandleTopRight.style.top = `-4px`;
208
+
209
+ const resizeHandleTopLeft = document.getElementById(
210
+ "resize-handle-top-left"
211
+ );
212
+ resizeHandleTopLeft.style.left = `-4px`;
213
+ resizeHandleTopLeft.style.top = `-4px`;
214
+ };
215
+
216
+ const destroyResizeHandle = () => {
217
+ const resizeBox = document.getElementById("resize-box");
218
+
219
+ if (resizeBox) {
220
+ resizeBox.remove();
221
+ }
222
+ };
223
+
224
+ var register = function (cytoscape) {
225
+ if (!cytoscape) {
226
+ return;
227
+ }
228
+
229
+ cytoscape("core", "resizeHandler", function (opts) {
230
+ var cy = this;
231
+
232
+ cy.on("pan", (event) => positionHandle());
233
+ cy.on("drag", (event) => positionHandle());
234
+
235
+ cy.on("vclick", function (event) {
236
+ if (typeof event.target.group !== "function") {
237
+ destroyResizeHandle();
238
+ }
239
+ });
240
+
241
+ cy.on("tap", "node", function (evt) {
242
+ const node = evt.target;
243
+
244
+ createResizeHandle(node, cy);
245
+ positionHandle(node);
246
+ });
247
+
248
+ return this;
249
+ });
250
+ };
251
+
252
+ if (typeof module !== "undefined" && module.exports) {
253
+ // expose as a commonjs module
254
+ module.exports = register;
255
+ }
256
+
257
+ if (typeof define !== "undefined" && define.amd) {
258
+ // expose as an amd/requirejs module
259
+ define("cytoscape-node-resizer", function () {
260
+ return register;
261
+ });
262
+ }
263
+
264
+ if (typeof cytoscape !== "undefined") {
265
+ // expose to global cytoscape (i.e. window.cytoscape)
266
+ register(cytoscape);
267
+ }
268
+ })();