Add app state to history (#309)
* Add app state to history. * Pick missing state keys. * Fix bug. * Remove force update.
This commit is contained in:
parent
3f1075cbcd
commit
dfb7c2b744
@ -1,3 +1,4 @@
|
|||||||
|
import { AppState } from "./types";
|
||||||
import { ExcalidrawElement } from "./element/types";
|
import { ExcalidrawElement } from "./element/types";
|
||||||
|
|
||||||
class SceneHistory {
|
class SceneHistory {
|
||||||
@ -5,13 +6,17 @@ class SceneHistory {
|
|||||||
private stateHistory: string[] = [];
|
private stateHistory: string[] = [];
|
||||||
private redoStack: string[] = [];
|
private redoStack: string[] = [];
|
||||||
|
|
||||||
generateCurrentEntry(elements: readonly ExcalidrawElement[]) {
|
generateCurrentEntry(
|
||||||
return JSON.stringify(
|
appState: Partial<AppState>,
|
||||||
elements.map(({ shape, ...element }) => ({
|
elements: readonly ExcalidrawElement[]
|
||||||
|
) {
|
||||||
|
return JSON.stringify({
|
||||||
|
appState,
|
||||||
|
elements: elements.map(({ shape, ...element }) => ({
|
||||||
...element,
|
...element,
|
||||||
isSelected: false
|
isSelected: false
|
||||||
}))
|
}))
|
||||||
);
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
pushEntry(newEntry: string) {
|
pushEntry(newEntry: string) {
|
||||||
|
@ -115,6 +115,19 @@ export function viewportCoordsToSceneCoords(
|
|||||||
return { x, y };
|
return { x, y };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function pickAppStatePropertiesForHistory(
|
||||||
|
appState: AppState
|
||||||
|
): Partial<AppState> {
|
||||||
|
return {
|
||||||
|
exportBackground: appState.exportBackground,
|
||||||
|
currentItemStrokeColor: appState.currentItemStrokeColor,
|
||||||
|
currentItemBackgroundColor: appState.currentItemBackgroundColor,
|
||||||
|
currentItemFont: appState.currentItemFont,
|
||||||
|
viewBackgroundColor: appState.viewBackgroundColor,
|
||||||
|
name: appState.name
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
export class App extends React.Component<any, AppState> {
|
export class App extends React.Component<any, AppState> {
|
||||||
canvas: HTMLCanvasElement | null = null;
|
canvas: HTMLCanvasElement | null = null;
|
||||||
rc: RoughCanvas | null = null;
|
rc: RoughCanvas | null = null;
|
||||||
@ -312,21 +325,23 @@ export class App extends React.Component<any, AppState> {
|
|||||||
}
|
}
|
||||||
this.setState({ elementType: shape });
|
this.setState({ elementType: shape });
|
||||||
} else if (event[KEYS.META] && event.code === "KeyZ") {
|
} else if (event[KEYS.META] && event.code === "KeyZ") {
|
||||||
|
event.preventDefault();
|
||||||
|
|
||||||
if (event.shiftKey) {
|
if (event.shiftKey) {
|
||||||
// Redo action
|
// Redo action
|
||||||
const data = history.redoOnce();
|
const data = history.redoOnce();
|
||||||
if (data !== null) {
|
if (data !== null) {
|
||||||
elements = data;
|
elements = data.elements;
|
||||||
|
this.setState(data.appState);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// undo action
|
// undo action
|
||||||
const data = history.undoOnce();
|
const data = history.undoOnce();
|
||||||
if (data !== null) {
|
if (data !== null) {
|
||||||
elements = data;
|
elements = data.elements;
|
||||||
|
this.setState(data.appState);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
this.forceUpdate();
|
|
||||||
event.preventDefault();
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -1387,7 +1402,12 @@ export class App extends React.Component<any, AppState> {
|
|||||||
});
|
});
|
||||||
this.saveDebounced();
|
this.saveDebounced();
|
||||||
if (history.isRecording()) {
|
if (history.isRecording()) {
|
||||||
history.pushEntry(history.generateCurrentEntry(elements));
|
history.pushEntry(
|
||||||
|
history.generateCurrentEntry(
|
||||||
|
pickAppStatePropertiesForHistory(this.state),
|
||||||
|
elements
|
||||||
|
)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user