1
- import {
2
- ref ,
3
- computed ,
4
- type Ref ,
5
- type Slots ,
6
- watch ,
7
- onMounted ,
8
- type ComputedRef ,
9
- nextTick
10
- } from "vue"
1
+ import { ref , computed , type Ref , type Slots , watch , onMounted , type ComputedRef } from "vue"
11
2
import type {
12
3
ChartRow ,
13
4
GanttBarObject ,
14
5
LabelColumnConfig ,
15
6
LabelColumnField ,
16
7
SortDirection ,
17
8
SortState ,
18
- HistoryState
9
+ HistoryState ,
10
+ BaseConnection ,
11
+ GanttBarConfig
19
12
} from "../types"
20
13
import dayjs from "dayjs"
14
+ import { cloneDeep } from "lodash-es"
21
15
22
16
/**
23
17
* Interface defining the return object from the useRows composable
@@ -59,79 +53,94 @@ export interface UseRowsProps {
59
53
onGroupExpansion : ( rowId : string | number ) => void
60
54
}
61
55
62
- function cloneBarForHistory ( bar : GanttBarObject ) {
63
- const dynamicKeys = Object . keys ( bar ) . filter ( ( key ) => key !== "ganttBarConfig" )
64
-
65
- const clonedBar : any = { }
66
- dynamicKeys . forEach ( ( key ) => {
67
- clonedBar [ key ] = bar [ key ]
68
- } )
69
-
70
- clonedBar . ganttBarConfig = {
71
- ...bar . ganttBarConfig ,
72
- style : bar . ganttBarConfig . style ? { ...bar . ganttBarConfig . style } : undefined ,
73
- connections : bar . ganttBarConfig . connections ?. map ( ( conn ) => ( { ...conn } ) )
74
- }
56
+ interface CleanBar {
57
+ ganttBarConfig : GanttBarConfig
58
+ [ key : string ] : any
59
+ }
75
60
76
- return clonedBar as GanttBarObject
61
+ interface CleanRow {
62
+ id ?: string | number
63
+ label : string
64
+ bars : CleanBar [ ]
65
+ connections ?: BaseConnection [ ]
66
+ children ?: CleanRow [ ]
77
67
}
78
68
79
- function cloneRowsForHistory ( rows : ChartRow [ ] ) : ChartRow [ ] {
80
- return rows . map ( ( row ) => {
81
- const clonedRow : ChartRow = {
69
+ function createHistoryState (
70
+ rows : ChartRow [ ] ,
71
+ expandedGroups : Set < string | number > ,
72
+ customOrder : Map < string | number , number >
73
+ ) : HistoryState {
74
+ const prepareRowForCloning = ( row : ChartRow ) : CleanRow => {
75
+ const cleanRow : CleanRow = {
82
76
id : row . id ,
83
77
label : row . label ,
84
- bars : row . bars . map ( cloneBarForHistory ) ,
85
- children : row . children ? cloneRowsForHistory ( row . children ) : undefined ,
86
- connections : row . connections ?. map ( ( conn ) => ( { ...conn } ) )
78
+ bars :
79
+ row . bars ?. map ( ( bar ) => ( {
80
+ ...bar ,
81
+ ganttBarConfig : {
82
+ id : bar . ganttBarConfig . id ,
83
+ label : bar . ganttBarConfig . label ,
84
+ html : bar . ganttBarConfig . html ,
85
+ hasHandles : bar . ganttBarConfig . hasHandles ,
86
+ immobile : bar . ganttBarConfig . immobile ,
87
+ bundle : bar . ganttBarConfig . bundle ,
88
+ pushOnOverlap : bar . ganttBarConfig . pushOnOverlap ,
89
+ pushOnConnect : bar . ganttBarConfig . pushOnConnect ,
90
+ style : bar . ganttBarConfig . style ,
91
+ class : bar . ganttBarConfig . class ,
92
+ connections : bar . ganttBarConfig . connections ,
93
+ milestoneId : bar . ganttBarConfig . milestoneId
94
+ }
95
+ } ) ) || [ ] ,
96
+ connections : row . connections ,
97
+ children : row . children ?. map ( prepareRowForCloning )
87
98
}
88
- return clonedRow
89
- } )
90
- }
91
99
92
- function restoreBarFromHistory (
93
- historicBar : GanttBarObject ,
94
- originalBar : GanttBarObject
95
- ) : GanttBarObject {
100
+ return cleanRow
101
+ }
102
+
103
+ const preparedRows = rows . map ( prepareRowForCloning )
104
+
96
105
return {
97
- ...historicBar ,
98
- ganttBarConfig : {
99
- ...historicBar . ganttBarConfig ,
100
- hasHandles : originalBar . ganttBarConfig . hasHandles ,
101
- style : historicBar . ganttBarConfig . style ? { ...historicBar . ganttBarConfig . style } : undefined ,
102
- connections : historicBar . ganttBarConfig . connections ?. map ( ( conn ) => ( { ...conn } ) )
103
- }
106
+ rows : cloneDeep ( preparedRows ) ,
107
+ expandedGroups : new Set ( expandedGroups ) ,
108
+ customOrder : new Map ( customOrder ) ,
109
+ timestamp : Date . now ( )
104
110
}
105
111
}
106
112
107
- function restoreRowsFromHistory ( historyRows : ChartRow [ ] , originalRows : ChartRow [ ] ) : ChartRow [ ] {
108
- return historyRows . map ( ( historyRow , index ) => {
109
- const originalRow = originalRows . find ( ( r ) => r . id === historyRow . id ) || originalRows [ index ]
110
-
111
- return {
112
- ...historyRow ,
113
- _originalNode : originalRow ?. _originalNode ,
114
- bars : historyRow . bars . map ( ( historyBar , barIndex ) =>
115
- restoreBarFromHistory ( historyBar , originalRow ?. bars [ barIndex ] || historyBar )
116
- ) ,
117
- children :
118
- historyRow . children && originalRow ?. children
119
- ? restoreRowsFromHistory ( historyRow . children , originalRow . children )
120
- : historyRow . children
113
+ function restoreState (
114
+ state : HistoryState ,
115
+ originalRows : ChartRow [ ]
116
+ ) : {
117
+ rows : ChartRow [ ]
118
+ expandedGroups : Set < string | number >
119
+ customOrder : Map < string | number , number >
120
+ } {
121
+ const restoreRow = ( historyRow : CleanRow , originalRow : ChartRow | undefined ) : ChartRow => {
122
+ const restored = cloneDeep ( historyRow ) as ChartRow
123
+
124
+ if ( originalRow ) {
125
+ restored . _originalNode = originalRow . _originalNode
121
126
}
122
- } )
123
- }
124
127
125
- function createHistoryState (
126
- rows : ChartRow [ ] ,
127
- expandedGroups : Set < string | number > ,
128
- customOrder : Map < string | number , number >
129
- ) : HistoryState {
128
+ if ( restored . children && originalRow ?. children ) {
129
+ restored . children = restored . children . map ( ( child , index ) =>
130
+ restoreRow ( child , originalRow . children ! [ index ] )
131
+ )
132
+ }
133
+
134
+ return restored
135
+ }
136
+
130
137
return {
131
- rows : cloneRowsForHistory ( rows ) ,
132
- expandedGroups : new Set ( Array . from ( expandedGroups ) ) ,
133
- customOrder : new Map ( customOrder ) ,
134
- timestamp : Date . now ( )
138
+ rows : state . rows . map ( ( historyRow ) => {
139
+ const originalRow = originalRows . find ( ( r ) => r . id === historyRow . id )
140
+ return restoreRow ( historyRow as CleanRow , originalRow )
141
+ } ) ,
142
+ expandedGroups : new Set ( state . expandedGroups ) ,
143
+ customOrder : new Map ( state . customOrder )
135
144
}
136
145
}
137
146
@@ -195,6 +204,7 @@ export function useRows(
195
204
historyStates . value . push (
196
205
createHistoryState ( reorderedRows . value , expandedGroups . value , customOrder . value )
197
206
)
207
+
198
208
currentHistoryIndex . value ++
199
209
200
210
if ( historyStates . value . length > MAX_HISTORY_STATES ) {
@@ -210,8 +220,11 @@ export function useRows(
210
220
currentHistoryIndex . value --
211
221
const previousState = historyStates . value [ currentHistoryIndex . value ] !
212
222
213
- reorderedRows . value = restoreRowsFromHistory ( previousState . rows , reorderedRows . value )
214
- customOrder . value = new Map ( previousState . customOrder )
223
+ // Ripristiniamo lo stato completo in un'unica operazione
224
+ const restored = restoreState ( previousState , reorderedRows . value )
225
+
226
+ reorderedRows . value = restored . rows
227
+ customOrder . value = restored . customOrder
215
228
}
216
229
217
230
const redo = ( ) => {
@@ -220,14 +233,15 @@ export function useRows(
220
233
currentHistoryIndex . value ++
221
234
const nextState = historyStates . value [ currentHistoryIndex . value ] !
222
235
223
- reorderedRows . value = restoreRowsFromHistory ( nextState . rows , reorderedRows . value )
224
- customOrder . value = new Map ( nextState . customOrder )
236
+ // Ripristiniamo lo stato completo in un'unica operazione
237
+ const restored = restoreState ( nextState , reorderedRows . value )
238
+
239
+ reorderedRows . value = restored . rows
240
+ customOrder . value = restored . customOrder
225
241
}
226
242
227
243
const onBarMove = ( ) => {
228
- nextTick ( ( ) => {
229
- addHistoryState ( )
230
- } )
244
+ addHistoryState ( )
231
245
}
232
246
233
247
const clearHistory = ( ) => {
0 commit comments