Skip to content

Commit

Permalink
Merge pull request #425 from jackgriffiths/clear-history
Browse files Browse the repository at this point in the history
Add command to clear history
  • Loading branch information
newcat authored Oct 19, 2024
2 parents 1760310 + 73fd54d commit 9e2f24e
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
5 changes: 5 additions & 0 deletions packages/renderer-vue/playground/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<button @click="createSubgraph">Create Subgraph</button>
<button @click="saveAndLoad">Save and Load</button>
<button @click="changeSidebarWidth">SidebarWidth</button>
<button @click="clearHistory">Clear History</button>
</div>
</template>

Expand Down Expand Up @@ -149,6 +150,10 @@ const changeSidebarWidth = () => {
baklavaView.settings.sidebar.width = Math.round(Math.random() * 500) + 300;
baklavaView.settings.sidebar.resizable = !baklavaView.settings.sidebar.resizable;
};
const clearHistory = () => {
baklavaView.commandHandler.executeCommand<Commands.ClearHistoryCommand>(Commands.CLEAR_HISTORY_COMMAND);
};
</script>

<style>
Expand Down
4 changes: 2 additions & 2 deletions packages/renderer-vue/src/commandList.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export type { CreateSubgraphCommand, DeleteNodesCommand, SaveSubgraphCommand, SwitchToMainGraphCommand } from "./graph";
export type { CommitTransactionCommand, StartTransactionCommand, UndoCommand, RedoCommand } from "./history";
export type { ClearHistoryCommand, CommitTransactionCommand, StartTransactionCommand, UndoCommand, RedoCommand } from "./history";
export type { ClearClipboardCommand, CopyCommand, PasteCommand } from "./clipboard";
export type { OpenSidebarCommand } from "./sidebar";
export type { StartSelectionBoxCommand } from "./editor/selectionBox";
Expand All @@ -10,7 +10,7 @@ export {
SAVE_SUBGRAPH_COMMAND,
SWITCH_TO_MAIN_GRAPH_COMMAND,
} from "./graph";
export { COMMIT_TRANSACTION_COMMAND, START_TRANSACTION_COMMAND, UNDO_COMMAND, REDO_COMMAND } from "./history";
export { CLEAR_HISTORY_COMMAND, COMMIT_TRANSACTION_COMMAND, START_TRANSACTION_COMMAND, UNDO_COMMAND, REDO_COMMAND } from "./history";
export { CLEAR_CLIPBOARD_COMMAND, COPY_COMMAND, PASTE_COMMAND } from "./clipboard";
export { OPEN_SIDEBAR_COMMAND } from "./sidebar";
export { START_SELECTION_BOX_COMMAND } from "./editor/selectionBox";
11 changes: 11 additions & 0 deletions packages/renderer-vue/src/history/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ export const UNDO_COMMAND = "UNDO";
export const REDO_COMMAND = "REDO";
export const START_TRANSACTION_COMMAND = "START_TRANSACTION";
export const COMMIT_TRANSACTION_COMMAND = "COMMIT_TRANSACTION";
export const CLEAR_HISTORY_COMMAND = "CLEAR_HISTORY";

export type UndoCommand = ICommand<void>;
export type RedoCommand = ICommand<void>;
export type StartTransactionCommand = ICommand<void>;
export type CommitTransactionCommand = ICommand<void>;
export type ClearHistoryCommand = ICommand<void>;

export interface IHistory {
maxSteps: number;
Expand Down Expand Up @@ -87,6 +89,11 @@ export function useHistory(graph: Ref<Graph>, commandHandler: ICommandHandler):
changeBySelf.value = false;
};

const clear = () => {
steps.value = [];
currentIndex.value = -1;
};

watch(
graph,
(newGraph, oldGraph) => {
Expand Down Expand Up @@ -131,6 +138,10 @@ export function useHistory(graph: Ref<Graph>, commandHandler: ICommandHandler):
canExecute: () => activeTransaction.value,
execute: commitTransaction,
});
commandHandler.registerCommand<ClearHistoryCommand>(CLEAR_HISTORY_COMMAND, {
canExecute: () => steps.value.length > 0,
execute: clear,
});

commandHandler.registerHotkey(["Control", "z"], UNDO_COMMAND);
commandHandler.registerHotkey(["Control", "y"], REDO_COMMAND);
Expand Down

0 comments on commit 9e2f24e

Please sign in to comment.