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-03-10 20:11:02 -07:00
|
|
|
import { newElementWith } from "./element/mutateElement";
|
2020-03-17 20:55:40 +01:00
|
|
|
import { isLinearElement } from "./element/typeChecks";
|
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-03-23 16:38:41 -07:00
|
|
|
getSnapshotForTest() {
|
|
|
|
return {
|
|
|
|
recording: this.recording,
|
|
|
|
stateHistory: this.stateHistory.map((s) => JSON.parse(s)),
|
|
|
|
redoStack: this.redoStack.map((s) => JSON.parse(s)),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-03-14 20:46:57 -07:00
|
|
|
clear() {
|
|
|
|
this.stateHistory.length = 0;
|
|
|
|
this.redoStack.length = 0;
|
|
|
|
}
|
|
|
|
|
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-19 19:41:32 +01:00
|
|
|
elements: elements.reduce((elements, element) => {
|
|
|
|
if (
|
|
|
|
isLinearElement(element) &&
|
|
|
|
appState.multiElement &&
|
|
|
|
appState.multiElement.id === element.id
|
|
|
|
) {
|
|
|
|
// don't store multi-point arrow if still has only one point
|
|
|
|
if (
|
|
|
|
appState.multiElement &&
|
|
|
|
appState.multiElement.id === element.id &&
|
|
|
|
element.points.length < 2
|
|
|
|
) {
|
|
|
|
return elements;
|
|
|
|
}
|
|
|
|
|
|
|
|
elements.push(
|
|
|
|
newElementWith(element, {
|
|
|
|
// don't store last point if not committed
|
|
|
|
points:
|
|
|
|
element.lastCommittedPoint !==
|
|
|
|
element.points[element.points.length - 1]
|
|
|
|
? element.points.slice(0, -1)
|
|
|
|
: element.points,
|
|
|
|
// don't regenerate versionNonce else this will short-circuit our
|
|
|
|
// bail-on-no-change logic in pushEntry()
|
|
|
|
versionNonce: element.versionNonce,
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
elements.push(
|
|
|
|
newElementWith(element, { versionNonce: element.versionNonce }),
|
|
|
|
);
|
2020-03-17 20:55:40 +01:00
|
|
|
}
|
2020-03-19 19:41:32 +01:00
|
|
|
return elements;
|
|
|
|
}, [] as Mutable<typeof elements>),
|
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-03-19 14:51:05 +01:00
|
|
|
if (this.stateHistory.length === 1) {
|
2020-01-11 20:45:56 -08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-03-20 10:45:30 -07:00
|
|
|
// Suspicious that this is called so many places. Seems error-prone.
|
2020-01-06 21:58:48 +04:00
|
|
|
resumeRecording() {
|
|
|
|
this.recording = true;
|
|
|
|
}
|
2020-03-20 10:45:30 -07:00
|
|
|
|
|
|
|
record(state: AppState, elements: readonly ExcalidrawElement[]) {
|
|
|
|
if (this.recording) {
|
|
|
|
this.pushEntry(state, elements);
|
|
|
|
this.recording = false;
|
|
|
|
}
|
|
|
|
}
|
2020-01-06 21:58:48 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
export const createHistory: () => { history: SceneHistory } = () => {
|
|
|
|
const history = new SceneHistory();
|
|
|
|
return { history };
|
|
|
|
};
|