Skip to content

Commit 9e75892

Browse files
authored
Migrate snap to grid (#41)
* rename * migrate
1 parent 584051b commit 9e75892

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

src/extensions/core/snapToGrid.js renamed to src/extensions/core/snapToGrid.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,9 @@ app.registerExtension({
5858
};
5959

6060
// Draw a preview of where the node will go if holding shift and the node is selected
61+
// @ts-ignore
6162
const origDrawNode = LGraphCanvas.prototype.drawNode;
63+
// @ts-ignore
6264
LGraphCanvas.prototype.drawNode = function (node, ctx) {
6365
if (app.shiftDown && this.node_dragged && node.id in this.selected_nodes) {
6466
const [x, y] = roundVectorToGrid([...node.pos]);
@@ -88,8 +90,6 @@ app.registerExtension({
8890
return origDrawNode.apply(this, arguments);
8991
};
9092

91-
92-
9393
/**
9494
* The currently moving, selected group only. Set after the `selected_group` has actually started
9595
* moving.
@@ -100,7 +100,9 @@ app.registerExtension({
100100
* Handles moving a group; tracking when a group has been moved (to show the ghost in `drawGroups`
101101
* below) as well as handle the last move call from LiteGraph's `processMouseUp`.
102102
*/
103+
// @ts-ignore
103104
const groupMove = LGraphGroup.prototype.move;
105+
// @ts-ignore
104106
LGraphGroup.prototype.move = function(deltax, deltay, ignore_nodes) {
105107
const v = groupMove.apply(this, arguments);
106108
// When we've started moving, set `selectedAndMovingGroup` as LiteGraph sets `selected_group`
@@ -120,6 +122,7 @@ app.registerExtension({
120122
for (const node of this._nodes) {
121123
node.alignToGrid();
122124
}
125+
// @ts-ignore
123126
LGraphNode.prototype.alignToGrid.apply(this);
124127
}
125128
return v;
@@ -130,7 +133,9 @@ app.registerExtension({
130133
* drawing a ghost box when one is actively being moved. This mimics the node snapping behavior for
131134
* both.
132135
*/
136+
// @ts-ignore
133137
const drawGroups = LGraphCanvas.prototype.drawGroups;
138+
// @ts-ignore
134139
LGraphCanvas.prototype.drawGroups = function (canvas, ctx) {
135140
if (this.selected_group && app.shiftDown) {
136141
if (this.selected_group_resizing) {
@@ -155,13 +160,18 @@ app.registerExtension({
155160

156161

157162
/** Handles adding a group in a snapping-enabled state. */
163+
// @ts-ignore
158164
const onGroupAdd = LGraphCanvas.onGroupAdd;
165+
// @ts-ignore
159166
LGraphCanvas.onGroupAdd = function() {
160167
const v = onGroupAdd.apply(app.canvas, arguments);
161168
if (app.shiftDown) {
169+
// @ts-ignore
162170
const lastGroup = app.graph._groups[app.graph._groups.length - 1];
163171
if (lastGroup) {
172+
// @ts-ignore
164173
roundVectorToGrid(lastGroup.pos);
174+
// @ts-ignore
165175
roundVectorToGrid(lastGroup.size);
166176
}
167177
}

src/types/litegraph.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,8 @@ export type SerializedLGraphNode<T extends LGraphNode = LGraphNode> = {
411411

412412
/** https://github.com/jagenjo/litegraph.js/blob/master/guides/README.md#lgraphnode */
413413
export declare class LGraphNode {
414+
onResize?: Function;
415+
414416
// Used in group node
415417
setInnerNodes(nodes: any) {
416418
throw new Error("Method not implemented.");
@@ -1028,6 +1030,8 @@ export declare class LGraphCanvas {
10281030
node_over: LGraphNode | null;
10291031
node_title_color: string;
10301032
node_widget: [LGraphNode, IWidget] | null;
1033+
last_mouse_dragging: boolean;
1034+
10311035
/** Called by `LGraphCanvas.drawBackCanvas` */
10321036
onDrawBackground:
10331037
| ((ctx: CanvasRenderingContext2D, visibleArea: Vector4) => void)

0 commit comments

Comments
 (0)