From 8e1a3bab9c40f621f9bf1587b8656f75252f7ffc Mon Sep 17 00:00:00 2001 From: Arpit Date: Wed, 21 Dec 2022 18:49:05 +0530 Subject: [PATCH] [improvement] Multipages : sync undo redo to page level (#5010) * undo/redo is page level * resolves: auto save definitions and double redo/undo actions --- frontend/src/Editor/Editor.jsx | 43 +++++++++++++++++++++++++--------- 1 file changed, 32 insertions(+), 11 deletions(-) diff --git a/frontend/src/Editor/Editor.jsx b/frontend/src/Editor/Editor.jsx index d03a7e4485..1a1d686119 100644 --- a/frontend/src/Editor/Editor.jsx +++ b/frontend/src/Editor/Editor.jsx @@ -268,7 +268,9 @@ class EditorComponent extends React.Component { // 2. Whenever you perform an undo – you can always redo and keep doing undo as long as we have a patch for it. // 3. Whenever you redo – you can always undo and keep doing redo as long as we have a patch for it. initComponentVersioning = () => { - this.currentVersion = -1; + this.currentVersion = { + [this.state.currentPageId]: -1, + }; this.currentVersionChanges = {}; this.noOfVersionsSupported = 100; this.canUndo = false; @@ -512,39 +514,52 @@ class EditorComponent extends React.Component { handleAddPatch = (patches, inversePatches) => { if (isEmpty(patches) && isEmpty(inversePatches)) return; if (isEqual(patches, inversePatches)) return; - this.currentVersion++; - this.currentVersionChanges[this.currentVersion] = { + + const currentPage = this.state.currentPageId; + const currentVersion = this.currentVersion[currentPage] ?? -1; + + this.currentVersionChanges[currentPage] = this.currentVersionChanges[currentPage] ?? {}; + + this.currentVersionChanges[currentPage][currentVersion] = { redo: patches, undo: inversePatches, }; - this.canUndo = this.currentVersionChanges.hasOwnProperty(this.currentVersion); - this.canRedo = this.currentVersionChanges.hasOwnProperty(this.currentVersion + 1); + this.canUndo = this.currentVersionChanges[currentPage].hasOwnProperty(currentVersion); + this.canRedo = this.currentVersionChanges[currentPage].hasOwnProperty(currentVersion + 1); - delete this.currentVersionChanges[this.currentVersion + 1]; - delete this.currentVersionChanges[this.currentVersion - this.noOfVersionsSupported]; + this.currentVersion[currentPage] = currentVersion + 1; + + delete this.currentVersionChanges[currentPage][currentVersion + 1]; + delete this.currentVersionChanges[currentPage][currentVersion - this.noOfVersionsSupported]; }; handleUndo = () => { if (this.canUndo) { + let currentVersion = this.currentVersion[this.state.currentPageId]; + const appDefinition = applyPatches( this.state.appDefinition, - this.currentVersionChanges[this.currentVersion--].undo + this.currentVersionChanges[this.state.currentPageId][currentVersion - 1].undo ); - this.canUndo = this.currentVersionChanges.hasOwnProperty(this.currentVersion); + this.canUndo = this.currentVersionChanges[this.state.currentPageId].hasOwnProperty(currentVersion - 1); this.canRedo = true; + this.currentVersion[this.state.currentPageId] = currentVersion - 1; if (!appDefinition) return; this.setState( { appDefinition, + isSaving: true, }, () => { this.props.ymap?.set('appDef', { newDefinition: appDefinition, editingVersionId: this.state.editingVersion?.id, }); + + this.autoSave(); } ); } @@ -552,24 +567,30 @@ class EditorComponent extends React.Component { handleRedo = () => { if (this.canRedo) { + let currentVersion = this.currentVersion[this.state.currentPageId]; + const appDefinition = applyPatches( this.state.appDefinition, - this.currentVersionChanges[++this.currentVersion].redo + this.currentVersionChanges[this.state.currentPageId][currentVersion].redo ); this.canUndo = true; - this.canRedo = this.currentVersionChanges.hasOwnProperty(this.currentVersion + 1); + this.canRedo = this.currentVersionChanges[this.state.currentPageId].hasOwnProperty(currentVersion + 1); + this.currentVersion[this.state.currentPageId] = currentVersion + 1; if (!appDefinition) return; this.setState( { appDefinition, + isSaving: true, }, () => { this.props.ymap?.set('appDef', { newDefinition: appDefinition, editingVersionId: this.state.editingVersion?.id, }); + + this.autoSave(); } ); }