1
1
import { NodePositions } from "../components/getNodePositionsFromCy" ;
2
2
import { useDoc } from "./useDoc" ;
3
+ import { addToUndoStack } from "./undoStack" ;
3
4
4
5
/**
5
6
* This function tries to align nodes vertical and horiontal on their center
@@ -15,6 +16,9 @@ export function alignNodes() {
15
16
const threshold = 40 ; // Adjust this value to change the alignment sensitivity
16
17
const alignedPositions : NodePositions = { } ;
17
18
19
+ // Store the original positions for undo
20
+ const originalPositions = { ...nodePositions } ;
21
+
18
22
// Iterate through all nodes
19
23
Object . entries ( nodePositions ) . forEach ( ( [ nodeId , position ] ) => {
20
24
let closestHorizontal : cytoscape . Position | null = null ;
@@ -56,6 +60,26 @@ export function alignNodes() {
56
60
nodePositions : alignedPositions ,
57
61
} ,
58
62
} ) ) ;
63
+
64
+ // Add the action to the undo stack
65
+ addToUndoStack ( {
66
+ undo : ( ) => {
67
+ useDoc . setState ( ( state ) => ( {
68
+ meta : {
69
+ ...state . meta ,
70
+ nodePositions : originalPositions ,
71
+ } ,
72
+ } ) ) ;
73
+ } ,
74
+ redo : ( ) => {
75
+ useDoc . setState ( ( state ) => ( {
76
+ meta : {
77
+ ...state . meta ,
78
+ nodePositions : alignedPositions ,
79
+ } ,
80
+ } ) ) ;
81
+ } ,
82
+ } ) ;
59
83
}
60
84
61
85
/**
@@ -68,6 +92,9 @@ export function alignNodesHorizontally(nodeIds: string[]) {
68
92
const nodePositions = meta . nodePositions as NodePositions ;
69
93
if ( ! nodePositions ) return ;
70
94
95
+ // Store the original positions for undo
96
+ const originalPositions = { ...nodePositions } ;
97
+
71
98
// Calculate the average x position
72
99
let sumX = 0 ;
73
100
let count = 0 ;
@@ -99,6 +126,26 @@ export function alignNodesHorizontally(nodeIds: string[]) {
99
126
nodePositions : alignedPositions ,
100
127
} ,
101
128
} ) ) ;
129
+
130
+ // Add the action to the undo stack
131
+ addToUndoStack ( {
132
+ undo : ( ) => {
133
+ useDoc . setState ( ( state ) => ( {
134
+ meta : {
135
+ ...state . meta ,
136
+ nodePositions : originalPositions ,
137
+ } ,
138
+ } ) ) ;
139
+ } ,
140
+ redo : ( ) => {
141
+ useDoc . setState ( ( state ) => ( {
142
+ meta : {
143
+ ...state . meta ,
144
+ nodePositions : alignedPositions ,
145
+ } ,
146
+ } ) ) ;
147
+ } ,
148
+ } ) ;
102
149
}
103
150
104
151
/**
@@ -111,6 +158,9 @@ export function alignNodesVertically(nodeIds: string[]) {
111
158
const nodePositions = meta . nodePositions as NodePositions ;
112
159
if ( ! nodePositions ) return ;
113
160
161
+ // Store the original positions for undo
162
+ const originalPositions = { ...nodePositions } ;
163
+
114
164
// Calculate the average y position
115
165
let sumY = 0 ;
116
166
let count = 0 ;
@@ -142,4 +192,24 @@ export function alignNodesVertically(nodeIds: string[]) {
142
192
nodePositions : alignedPositions ,
143
193
} ,
144
194
} ) ) ;
195
+
196
+ // Add the action to the undo stack
197
+ addToUndoStack ( {
198
+ undo : ( ) => {
199
+ useDoc . setState ( ( state ) => ( {
200
+ meta : {
201
+ ...state . meta ,
202
+ nodePositions : originalPositions ,
203
+ } ,
204
+ } ) ) ;
205
+ } ,
206
+ redo : ( ) => {
207
+ useDoc . setState ( ( state ) => ( {
208
+ meta : {
209
+ ...state . meta ,
210
+ nodePositions : alignedPositions ,
211
+ } ,
212
+ } ) ) ;
213
+ } ,
214
+ } ) ;
145
215
}
0 commit comments