High-Performance UI Component Library for Enterprise Applications Forums Gantt How can I implement undo and redo for task edits in Smart Gantt while keeping se Reply To: How can I implement undo and redo for task edits in Smart Gantt while keeping se

#113631
admin
Keymaster

Hi,

The cleanest way to implement undo/redo for Smart Gantt task edits is to treat every edit as a command with a before/after state, rather than trying to restore the entire chart state.

Keep a history stack containing the changed task data:

– Before a task edit starts, store the original task state.
– After the edit completes, store the new state.
– Undo applies the old state.
– Redo applies the new state.
– Keep server persistence separate: when an undo/redo action changes a task, send the resulting task state back to the API so the backend stays synchronized.

A typical structure looks like:

const undoStack = [];
const redoStack = [];

function saveTaskChange(oldTask, newTask) {
    undoStack.push({
        oldValue: { ...oldTask },
        newValue: { ...newTask }
    });

    redoStack.length = 0;
}

function undo() {
    const action = undoStack.pop();

    if (!action) {
        return;
    }

    gantt.updateTask(action.oldValue.id, action.oldValue);

    redoStack.push(action);
}

function redo() {
    const action = redoStack.pop();

    if (!action) {
        return;
    }

    gantt.updateTask(action.newValue.id, action.newValue);

    undoStack.push(action);
}

Regards,
Markov