import type { Command } from "./command"; import type { EditorState } from "@editor/state"; import { commandIds } from "./ids"; export const historyUndoCommand: Command = { id: commandIds.historyUndo, name: "Undo", history: { mode: "ignore" }, execute({ state }) { const previous = state.history.past.at(-1); if (!previous) return state; return { ...state, document: previous.document, editor: preserveGenerationJobs(previous.editor, state.editor), history: { past: state.history.past.slice(0, -1), future: [{ document: state.document, editor: state.editor }, ...state.history.future], }, }; }, }; export const historyRedoCommand: Command = { id: commandIds.historyRedo, name: "Redo", history: { mode: "ignore" }, execute({ state }) { const next = state.history.future[0]; if (!next) return state; return { ...state, document: next.document, editor: preserveGenerationJobs(next.editor, state.editor), history: { past: [...state.history.past, { document: state.document, editor: state.editor }], future: state.history.future.slice(1), }, }; }, }; export const historyCommands = [historyUndoCommand, historyRedoCommand] satisfies Command[]; function preserveGenerationJobs(target: EditorState, current: EditorState): EditorState { return { ...target, generation: { ...target.generation, jobs: current.generation.jobs, }, }; }