Skip to content

Commit

Permalink
#3005 - Fix undo edgecase
Browse files Browse the repository at this point in the history
  • Loading branch information
nanoblit committed Sep 1, 2023
1 parent d40575f commit 73580b3
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 13 deletions.
22 changes: 15 additions & 7 deletions packages/ketcher-react/src/script/editor/Editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,7 @@ class Editor implements KetcherEditor {
this.historyStack.shift();
}
this.historyPtr = this.historyStack.length;
this.event.change.dispatch(action); // TODO: stoppable here
this.event.change.dispatch({ action }); // TODO: stoppable here
}
this.render.update(false, null, options);
}
Expand All @@ -525,7 +525,11 @@ class Editor implements KetcherEditor {
};
}

undo() {
private async resolveDispatch() {
return new Promise((resolve) => setTimeout(resolve, 0));
}

async undo() {
if (this.historyPtr === 0) {
throw new Error('Undo stack is empty');
}
Expand All @@ -536,21 +540,23 @@ class Editor implements KetcherEditor {
this.selection(null);

if (this._tool instanceof toolsMap.paste) {
this.event.change.dispatch();
this.event.change.dispatch({ isUndoOrRedo: true });
}

await this.resolveDispatch();

this._tool?.updatePreview?.();

this.historyPtr--;
const stack = this.historyStack[this.historyPtr];
const action = stack.perform(this.render.ctab);

this.historyStack[this.historyPtr] = action;
this.event.change.dispatch(action);
this.event.change.dispatch({ action, isUndoOrRedo: true });
this.render.update();
}

redo() {
async redo() {
if (this.historyPtr === this.historyStack.length) {
throw new Error('Redo stack is empty');
}
Expand All @@ -562,15 +568,17 @@ class Editor implements KetcherEditor {
this.selection(null);

if (this._tool instanceof toolsMap.paste) {
this.event.change.dispatch();
this.event.change.dispatch({ isUndoOrRedo: true });
}

await this.resolveDispatch();

this._tool?.updatePreview?.();

const action = this.historyStack[this.historyPtr].perform(this.render.ctab);
this.historyStack[this.historyPtr] = action;
this.historyPtr++;
this.event.change.dispatch(action);
this.event.change.dispatch({ action, isUndoOrRedo: true });
this.render.update();
}

Expand Down
26 changes: 20 additions & 6 deletions packages/ketcher-react/src/script/ui/state/editor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,23 +46,37 @@ export default function initEditor(dispatch, getState) {
// eslint-disable-line no-shadow
const state = global.currentState;
const activeTool = state.actionState.activeTool.tool;
if (activeTool === 'select') return;

if (activeTool === 'select') {
return;
}

const selectMode = state.toolbar.visibleTools.select;
const resetOption = state.options.settings.resetToSelect;
if (resetOption === true || resetOption === activeTool)

if (resetOption === true || resetOption === activeTool) {
// example: 'paste'
dispatch({ type: 'ACTION', action: acts[selectMode].action });
else updateAction();
} else {
updateAction();
}
}

return {
onInit: (editor) => {
dispatch({ type: 'INIT', editor });
},
onChange: (action) => {
if (action === undefined) sleep(0).then(() => dispatch(resetToSelect));
onChange: ({ action, isUndoOrRedo }) => {
if (!action) {
return;
}

if (isUndoOrRedo) {
return updateAction();
}

// new tool in reducer
else dispatch(resetToSelect);
dispatch(resetToSelect);
},
onSelectionChange: () => {
updateAction();
Expand Down

0 comments on commit 73580b3

Please sign in to comment.