PromucFlow_constructor/app/client/src/workers/ReplayDSL.ts

155 lines
3.6 KiB
TypeScript
Raw Normal View History

feat: Undo/Redo (#6654) * Scaffolding for undo-redo * undo redo working Poc commit * memory performance improvements by diffing * dont run update on undo/redo" * merging widget postion update and canvas bottom row update into one dsl update. * fix tabs widget * Visible updates per undo redo action (#6838) Co-authored-by: Rahul R <rahulramesha@Rahuls-MacBook-Pro.local> * resize atomic operation * fix switch control state issue * disallow undo/redo for snipping and comment mode * disallow undo/redo for snipping and comment mode * fix color picker issue in undo/redo * add test for replayDSL * option control fix, adding logs * minor position change undo redo updates * add test cases for replayHelpers * property Upade visual change * remove unused code * global hot key jest test for undo redo * Fixing batch updates on property change.. * add tests for toggle control in property pane * unwanted utils. * add tests for text control * add tests for deletion * add tests for dropping a new widget * adding jest test for replayUtils * add move widget tests * add tests for color picker control * add analytics for undo/redo * add analytics for undo/redo * tab addition atomic * cypress tests for propertyPane, toasts and radiowidget optionControl * replayDSL end of redo stack fix * property update changes * menu option control debounce input * color picker empty undo fix * fix cypress tests * widget add/remove atomic * revert alternative approach to handle atomic operations * update replayDSL test * add some comments * addressing review comments * flash color for property pane controls * Fixing adding of tabs widget as well. * code review comments. * merging widget postion update and canvas bottom row update into one dsl update. * fix ordering of tabs property control * meta property update canvas min height. * fixing failed specs. * Fixing entity explorer update on deleting tab from entity explorer. * address review comments and minor property update changes * fixing failing tests * merge conflicts * changes to cater widget api. * fix suggested widget table issue * draggable list for undo redo * fix widget name focus * excluding canvas updates. * fixing codeEditor update on propertySection collapse * fixed failing test case Co-authored-by: Abhinav Jha <abhinav@appsmith.com> Co-authored-by: Rahul R <rahulramesha@Rahuls-MacBook-Pro.local> Co-authored-by: root <root@DESKTOP-9GENCK0.localdomain> Co-authored-by: Ashok Kumar M <35134347+marks0351@users.noreply.github.com> Co-authored-by: Pawan Kumar <pawankumar@Pawans-MacBook-Pro.local>
2021-09-21 07:55:56 +00:00
import { Doc, Map, UndoManager } from "yjs";
import { captureException } from "@sentry/react";
import { diff as deepDiff, applyChange, revertChange } from "deep-diff";
import { processDiff, DSLDiff, getPathsFromDiff } from "./replayUtils";
import { CanvasWidgetsReduxState } from "reducers/entityReducers/canvasWidgetsReducer";
const _DIFF_ = "diff";
type ReplayType = "UNDO" | "REDO";
export default class ReplayDSL {
private diffMap: any;
private undoManager: UndoManager;
private dsl: CanvasWidgetsReduxState;
private prevRedoDiff: Array<DSLDiff> | undefined;
logs: any[] = [];
constructor(widgets: CanvasWidgetsReduxState) {
const doc = new Doc();
this.diffMap = doc.get("map", Map);
this.dsl = widgets;
this.diffMap.set(_DIFF_, []);
this.undoManager = new UndoManager(this.diffMap, { captureTimeout: 100 });
}
/**
* checks if there is anything in the redoStack or undoStack
*
* @return boolean
*/
canReplay(replayType: ReplayType) {
switch (replayType) {
case "UNDO":
return this.undoManager.undoStack.length > 0;
case "REDO":
return this.undoManager.redoStack.length > 0;
default:
return false;
}
}
/**
* get the diffs from yMap
*
* @returns
*/
private getDiffs() {
return this.diffMap.get(_DIFF_);
}
/**
* replay actions ( undo redo )
*
* Note:
* important thing to note is that for redo we redo first, then
* get the diff map and undo, we get diff first, then undo
*
* @param replayType
*/
replay(replayType: ReplayType) {
const start = performance.now();
if (this.canReplay(replayType)) {
let diffs;
switch (replayType) {
case "UNDO":
diffs = this.getDiffs();
this.undoManager.undo();
break;
case "REDO":
this.undoManager.redo();
diffs = this.getDiffs();
break;
}
const replay = this.applyDiffs(diffs, replayType);
const stop = performance.now();
this.logs.push({
log: `replay ${replayType}`,
undoTime: `${stop - start} ms`,
replay: replay,
diffs: diffs,
});
return {
replayWidgetDSL: this.dsl,
replay,
logs: this.logs,
event: `REPLAY_${replayType}`,
timeTaken: stop - start,
paths: getPathsFromDiff(diffs),
};
}
return null;
}
/**
* saves the changes (diff) in yMap
* only if there is a deep diff
*
* @param widgets
*/
update(widgets: CanvasWidgetsReduxState) {
const startTime = performance.now();
const diffs = deepDiff(this.dsl, widgets);
if (diffs && diffs.length) {
this.dsl = widgets;
this.diffMap.set(_DIFF_, diffs);
}
const endTime = performance.now();
this.logs.push({
log: "replay updating",
updateTime: `${endTime - startTime} ms`,
});
}
clearLogs() {
this.logs = [];
}
/**
* apply the diff on the current dsl
*
* @param diffs
* @param isUndo
*/
applyDiffs(diffs: Array<DSLDiff>, replayType: ReplayType) {
const replay = {};
const isUndo = replayType === "UNDO";
const applyDiff = isUndo ? revertChange : applyChange;
for (const diff of diffs) {
if (!Array.isArray(diff.path) || diff.path.length === 0) {
continue;
}
try {
processDiff(this.dsl, diff, replay, isUndo);
applyDiff(this.dsl, true, diff);
} catch (e) {
captureException(e, {
extra: {
diff,
updateLength: diffs.length,
},
});
}
}
return replay;
}
}