2020-01-22 21:32:43 +01:00
|
|
|
import { AppState } from "./types";
|
2020-01-06 21:58:48 +04:00
|
|
|
import { ExcalidrawElement } from "./element/types";
|
2020-02-05 22:47:10 +04:00
|
|
|
import { clearAppStatePropertiesForHistory } from "./appState";
|
2020-01-06 21:58:48 +04:00
|
|
|
|
2020-03-01 14:39:03 -05:00
|
|
|
type Result = {
|
|
|
|
appState: AppState;
|
|
|
|
elements: ExcalidrawElement[];
|
|
|
|
};
|
|
|
|
|
|
|
|
export class SceneHistory {
|
2020-01-06 21:58:48 +04:00
|
|
|
private recording: boolean = true;
|
|
|
|
private stateHistory: string[] = [];
|
|
|
|
private redoStack: string[] = [];
|
|
|
|
|
2020-02-05 22:47:10 +04:00
|
|
|
private generateEntry(
|
|
|
|
appState: AppState,
|
2020-01-24 12:04:54 +02:00
|
|
|
elements: readonly ExcalidrawElement[],
|
2020-01-22 21:32:43 +01:00
|
|
|
) {
|
|
|
|
return JSON.stringify({
|
2020-02-05 22:47:10 +04:00
|
|
|
appState: clearAppStatePropertiesForHistory(appState),
|
2020-03-08 10:20:55 -07:00
|
|
|
elements: elements.map(element => ({
|
2020-01-12 04:00:00 +04:00
|
|
|
...element,
|
2020-02-05 22:47:10 +04:00
|
|
|
points:
|
|
|
|
appState.multiElement && appState.multiElement.id === element.id
|
|
|
|
? element.points.slice(0, -1)
|
|
|
|
: element.points,
|
2020-01-24 12:04:54 +02:00
|
|
|
})),
|
2020-01-22 21:32:43 +01:00
|
|
|
});
|
2020-01-06 21:58:48 +04:00
|
|
|
}
|
|
|
|
|
2020-02-05 22:47:10 +04:00
|
|
|
pushEntry(appState: AppState, elements: readonly ExcalidrawElement[]) {
|
|
|
|
const newEntry = this.generateEntry(appState, elements);
|
2020-01-06 21:58:48 +04:00
|
|
|
if (
|
|
|
|
this.stateHistory.length > 0 &&
|
|
|
|
this.stateHistory[this.stateHistory.length - 1] === newEntry
|
|
|
|
) {
|
|
|
|
// If the last entry is the same as this one, ignore it
|
|
|
|
return;
|
|
|
|
}
|
2020-01-12 12:19:24 +01:00
|
|
|
|
2020-01-06 21:58:48 +04:00
|
|
|
this.stateHistory.push(newEntry);
|
2020-01-12 12:19:24 +01:00
|
|
|
|
|
|
|
// As a new entry was pushed, we invalidate the redo stack
|
|
|
|
this.clearRedoStack();
|
2020-01-06 21:58:48 +04:00
|
|
|
}
|
|
|
|
|
2020-01-09 19:22:04 +04:00
|
|
|
restoreEntry(entry: string) {
|
|
|
|
try {
|
|
|
|
return JSON.parse(entry);
|
|
|
|
} catch {
|
|
|
|
return null;
|
|
|
|
}
|
2020-01-06 21:58:48 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
clearRedoStack() {
|
|
|
|
this.redoStack.splice(0, this.redoStack.length);
|
|
|
|
}
|
|
|
|
|
2020-03-01 14:39:03 -05:00
|
|
|
redoOnce(): Result | null {
|
2020-01-12 12:19:24 +01:00
|
|
|
if (this.redoStack.length === 0) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-01-06 21:58:48 +04:00
|
|
|
const entryToRestore = this.redoStack.pop();
|
2020-01-12 12:19:24 +01:00
|
|
|
|
2020-01-06 21:58:48 +04:00
|
|
|
if (entryToRestore !== undefined) {
|
2020-01-12 12:19:24 +01:00
|
|
|
this.stateHistory.push(entryToRestore);
|
2020-01-09 19:22:04 +04:00
|
|
|
return this.restoreEntry(entryToRestore);
|
2020-01-06 21:58:48 +04:00
|
|
|
}
|
2020-01-09 19:22:04 +04:00
|
|
|
|
|
|
|
return null;
|
2020-01-06 21:58:48 +04:00
|
|
|
}
|
|
|
|
|
2020-03-01 14:39:03 -05:00
|
|
|
undoOnce(): Result | null {
|
2020-01-11 20:45:56 -08:00
|
|
|
if (this.stateHistory.length === 0) {
|
|
|
|
return null;
|
2020-01-06 21:58:48 +04:00
|
|
|
}
|
2020-01-11 20:45:56 -08:00
|
|
|
|
|
|
|
const currentEntry = this.stateHistory.pop();
|
2020-02-05 22:47:10 +04:00
|
|
|
|
2020-01-11 20:45:56 -08:00
|
|
|
const entryToRestore = this.stateHistory[this.stateHistory.length - 1];
|
|
|
|
|
|
|
|
if (currentEntry !== undefined) {
|
2020-01-06 21:58:48 +04:00
|
|
|
this.redoStack.push(currentEntry);
|
2020-01-09 19:22:04 +04:00
|
|
|
return this.restoreEntry(entryToRestore);
|
2020-01-06 21:58:48 +04:00
|
|
|
}
|
2020-01-09 19:22:04 +04:00
|
|
|
|
|
|
|
return null;
|
2020-01-06 21:58:48 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
isRecording() {
|
|
|
|
return this.recording;
|
|
|
|
}
|
|
|
|
|
|
|
|
skipRecording() {
|
|
|
|
this.recording = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
resumeRecording() {
|
|
|
|
this.recording = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export const createHistory: () => { history: SceneHistory } = () => {
|
|
|
|
const history = new SceneHistory();
|
|
|
|
return { history };
|
|
|
|
};
|