Skip to content

Commit 5df4b8e

Browse files
committed
Added lodash-es to clone
1 parent ad646c1 commit 5df4b8e

File tree

4 files changed

+128
-86
lines changed

4 files changed

+128
-86
lines changed

package-lock.json

+23-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
"@semantic-release/changelog": "^6.0.3",
4141
"@semantic-release/git": "^10.0.1",
4242
"@senojs/rollup-plugin-style-inject": "^0.2.3",
43+
"@types/lodash-es": "^4.17.12",
4344
"@types/node": "^22.10.1",
4445
"@types/postcss-preset-env": "^7.7.0",
4546
"@vitejs/plugin-vue": "^5.2.1",
@@ -93,6 +94,7 @@
9394
},
9495
"dependencies": {
9596
"@tsconfig/node22": "^22.0.0",
97+
"lodash-es": "^4.17.21",
9698
"uuid": "^11.0.5"
9799
}
98100
}

src/components/GGanttChart.vue

+12-6
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,16 @@ const updateRangeBackground = () => {
424424
}
425425
}
426426
427+
const redo = () => {
428+
rowManager.redo()
429+
updateBarPositions()
430+
}
431+
432+
const undo = () => {
433+
rowManager.undo()
434+
updateBarPositions()
435+
}
436+
427437
// ResizeObserver instance
428438
let resizeObserver: ResizeObserver
429439
@@ -695,17 +705,13 @@ provide(GANTT_ID_KEY, id.value)
695705
</div>
696706
<div class="g-gantt-command-history">
697707
<button
698-
@click="rowManager.undo()"
708+
@click="undo"
699709
:disabled="!rowManager.canUndo.value"
700710
aria-label="Annulla ultima azione"
701711
>
702712
<FontAwesomeIcon :icon="faUndo" class="command-icon" />
703713
</button>
704-
<button
705-
@click="rowManager.redo()"
706-
:disabled="!rowManager.canRedo.value"
707-
aria-label="Ripeti azione"
708-
>
714+
<button @click="redo" :disabled="!rowManager.canRedo.value" aria-label="Ripeti azione">
709715
<FontAwesomeIcon :icon="faRedo" class="command-icon" />
710716
</button>
711717
</div>

src/composables/useRows.ts

+91-77
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,17 @@
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"
112
import type {
123
ChartRow,
134
GanttBarObject,
145
LabelColumnConfig,
156
LabelColumnField,
167
SortDirection,
178
SortState,
18-
HistoryState
9+
HistoryState,
10+
BaseConnection,
11+
GanttBarConfig
1912
} from "../types"
2013
import dayjs from "dayjs"
14+
import { cloneDeep } from "lodash-es"
2115

2216
/**
2317
* Interface defining the return object from the useRows composable
@@ -59,79 +53,94 @@ export interface UseRowsProps {
5953
onGroupExpansion: (rowId: string | number) => void
6054
}
6155

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+
}
7560

76-
return clonedBar as GanttBarObject
61+
interface CleanRow {
62+
id?: string | number
63+
label: string
64+
bars: CleanBar[]
65+
connections?: BaseConnection[]
66+
children?: CleanRow[]
7767
}
7868

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 = {
8276
id: row.id,
8377
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)
8798
}
88-
return clonedRow
89-
})
90-
}
9199

92-
function restoreBarFromHistory(
93-
historicBar: GanttBarObject,
94-
originalBar: GanttBarObject
95-
): GanttBarObject {
100+
return cleanRow
101+
}
102+
103+
const preparedRows = rows.map(prepareRowForCloning)
104+
96105
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()
104110
}
105111
}
106112

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
121126
}
122-
})
123-
}
124127

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+
130137
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)
135144
}
136145
}
137146

@@ -195,6 +204,7 @@ export function useRows(
195204
historyStates.value.push(
196205
createHistoryState(reorderedRows.value, expandedGroups.value, customOrder.value)
197206
)
207+
198208
currentHistoryIndex.value++
199209

200210
if (historyStates.value.length > MAX_HISTORY_STATES) {
@@ -210,8 +220,11 @@ export function useRows(
210220
currentHistoryIndex.value--
211221
const previousState = historyStates.value[currentHistoryIndex.value]!
212222

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
215228
}
216229

217230
const redo = () => {
@@ -220,14 +233,15 @@ export function useRows(
220233
currentHistoryIndex.value++
221234
const nextState = historyStates.value[currentHistoryIndex.value]!
222235

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
225241
}
226242

227243
const onBarMove = () => {
228-
nextTick(() => {
229-
addHistoryState()
230-
})
244+
addHistoryState()
231245
}
232246

233247
const clearHistory = () => {

0 commit comments

Comments
 (0)