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-03-17 20:55:40 +01:00
|
|
|
import { isLinearElement } from "./element/typeChecks";
|
2020-05-23 12:07:11 -07:00
|
|
|
import { deepCopyElement } from "./element/newElement";
|
2020-01-06 21:58:48 +04:00
|
|
|
|
2020-05-23 12:07:11 -07:00
|
|
|
export interface HistoryEntry {
|
2020-05-23 07:26:59 +02:00
|
|
|
appState: ReturnType<typeof clearAppStatePropertiesForHistory>;
|
2020-03-01 14:39:03 -05:00
|
|
|
elements: ExcalidrawElement[];
|
2020-05-23 12:07:11 -07:00
|
|
|
}
|
2020-03-01 14:39:03 -05:00
|
|
|
|
2020-05-23 12:07:11 -07:00
|
|
|
interface DehydratedExcalidrawElement {
|
|
|
|
id: string;
|
|
|
|
versionNonce: number;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface DehydratedHistoryEntry {
|
2020-05-25 00:17:14 +02:00
|
|
|
appState: string;
|
2020-05-23 12:07:11 -07:00
|
|
|
elements: DehydratedExcalidrawElement[];
|
|
|
|
}
|
2020-05-23 07:26:59 +02:00
|
|
|
|
|
|
|
const clearAppStatePropertiesForHistory = (appState: AppState) => {
|
|
|
|
return {
|
|
|
|
selectedElementIds: appState.selectedElementIds,
|
2021-05-29 21:35:03 +02:00
|
|
|
selectedGroupIds: appState.selectedGroupIds,
|
2020-05-23 07:26:59 +02:00
|
|
|
viewBackgroundColor: appState.viewBackgroundColor,
|
2020-06-01 11:35:44 +02:00
|
|
|
editingLinearElement: appState.editingLinearElement,
|
2020-05-30 22:48:57 +02:00
|
|
|
editingGroupId: appState.editingGroupId,
|
2020-12-01 14:00:13 +01:00
|
|
|
name: appState.name,
|
2020-05-23 07:26:59 +02:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2021-04-24 18:21:02 +05:30
|
|
|
class History {
|
2020-05-25 00:17:14 +02:00
|
|
|
private elementCache = new Map<string, Map<number, ExcalidrawElement>>();
|
2020-01-06 21:58:48 +04:00
|
|
|
private recording: boolean = true;
|
2020-05-23 12:07:11 -07:00
|
|
|
private stateHistory: DehydratedHistoryEntry[] = [];
|
|
|
|
private redoStack: DehydratedHistoryEntry[] = [];
|
2020-05-23 07:26:59 +02:00
|
|
|
private lastEntry: HistoryEntry | null = null;
|
2020-01-06 21:58:48 +04:00
|
|
|
|
2020-05-23 12:07:11 -07:00
|
|
|
private hydrateHistoryEntry({
|
|
|
|
appState,
|
|
|
|
elements,
|
|
|
|
}: DehydratedHistoryEntry): HistoryEntry {
|
|
|
|
return {
|
2020-05-25 00:17:14 +02:00
|
|
|
appState: JSON.parse(appState),
|
2020-05-23 12:07:11 -07:00
|
|
|
elements: elements.map((dehydratedExcalidrawElement) => {
|
|
|
|
const element = this.elementCache
|
|
|
|
.get(dehydratedExcalidrawElement.id)
|
|
|
|
?.get(dehydratedExcalidrawElement.versionNonce);
|
|
|
|
if (!element) {
|
|
|
|
throw new Error(
|
2020-05-25 00:17:14 +02:00
|
|
|
`Element not found: ${dehydratedExcalidrawElement.id}:${dehydratedExcalidrawElement.versionNonce}`,
|
2020-05-23 12:07:11 -07:00
|
|
|
);
|
|
|
|
}
|
|
|
|
return element;
|
|
|
|
}),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
private dehydrateHistoryEntry({
|
|
|
|
appState,
|
|
|
|
elements,
|
|
|
|
}: HistoryEntry): DehydratedHistoryEntry {
|
|
|
|
return {
|
2020-05-25 00:17:14 +02:00
|
|
|
appState: JSON.stringify(appState),
|
|
|
|
elements: elements.map((element: ExcalidrawElement) => {
|
2020-05-23 12:07:11 -07:00
|
|
|
if (!this.elementCache.has(element.id)) {
|
|
|
|
this.elementCache.set(element.id, new Map());
|
|
|
|
}
|
|
|
|
const versions = this.elementCache.get(element.id)!;
|
2020-05-25 00:17:14 +02:00
|
|
|
if (!versions.has(element.versionNonce)) {
|
|
|
|
versions.set(element.versionNonce, deepCopyElement(element));
|
2020-05-23 12:07:11 -07:00
|
|
|
}
|
|
|
|
return {
|
|
|
|
id: element.id,
|
|
|
|
versionNonce: element.versionNonce,
|
|
|
|
};
|
|
|
|
}),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-03-23 16:38:41 -07:00
|
|
|
getSnapshotForTest() {
|
|
|
|
return {
|
|
|
|
recording: this.recording,
|
2020-05-23 12:07:11 -07:00
|
|
|
stateHistory: this.stateHistory.map((dehydratedHistoryEntry) =>
|
|
|
|
this.hydrateHistoryEntry(dehydratedHistoryEntry),
|
|
|
|
),
|
|
|
|
redoStack: this.redoStack.map((dehydratedHistoryEntry) =>
|
|
|
|
this.hydrateHistoryEntry(dehydratedHistoryEntry),
|
|
|
|
),
|
2020-03-23 16:38:41 -07:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-03-14 20:46:57 -07:00
|
|
|
clear() {
|
|
|
|
this.stateHistory.length = 0;
|
|
|
|
this.redoStack.length = 0;
|
2020-05-23 07:26:59 +02:00
|
|
|
this.lastEntry = null;
|
2020-05-23 12:07:11 -07:00
|
|
|
this.elementCache.clear();
|
2020-03-14 20:46:57 -07:00
|
|
|
}
|
|
|
|
|
2020-05-20 16:21:37 +03:00
|
|
|
private generateEntry = (
|
2020-02-05 22:47:10 +04:00
|
|
|
appState: AppState,
|
2020-01-24 12:04:54 +02:00
|
|
|
elements: readonly ExcalidrawElement[],
|
2020-05-23 12:07:11 -07:00
|
|
|
): DehydratedHistoryEntry =>
|
|
|
|
this.dehydrateHistoryEntry({
|
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;
|
|
|
|
}
|
|
|
|
|
2020-05-23 07:26:59 +02:00
|
|
|
elements.push({
|
|
|
|
...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,
|
|
|
|
});
|
2020-03-19 19:41:32 +01:00
|
|
|
} else {
|
2020-05-23 07:26:59 +02:00
|
|
|
elements.push(element);
|
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-05-23 07:26:59 +02:00
|
|
|
shouldCreateEntry(nextEntry: HistoryEntry): boolean {
|
|
|
|
const { lastEntry } = this;
|
|
|
|
|
|
|
|
if (!lastEntry) {
|
|
|
|
return true;
|
2020-01-06 21:58:48 +04:00
|
|
|
}
|
2020-01-12 12:19:24 +01:00
|
|
|
|
2020-05-23 07:26:59 +02:00
|
|
|
if (nextEntry.elements.length !== lastEntry.elements.length) {
|
|
|
|
return true;
|
|
|
|
}
|
2020-01-12 12:19:24 +01:00
|
|
|
|
2020-05-23 07:26:59 +02:00
|
|
|
// loop from right to left as changes are likelier to happen on new elements
|
|
|
|
for (let i = nextEntry.elements.length - 1; i > -1; i--) {
|
|
|
|
const prev = nextEntry.elements[i];
|
|
|
|
const next = lastEntry.elements[i];
|
|
|
|
if (
|
|
|
|
!prev ||
|
|
|
|
!next ||
|
|
|
|
prev.id !== next.id ||
|
|
|
|
prev.versionNonce !== next.versionNonce
|
|
|
|
) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// note: this is safe because entry's appState is guaranteed no excess props
|
|
|
|
let key: keyof typeof nextEntry.appState;
|
|
|
|
for (key in nextEntry.appState) {
|
2020-06-01 11:35:44 +02:00
|
|
|
if (key === "editingLinearElement") {
|
|
|
|
if (
|
|
|
|
nextEntry.appState[key]?.elementId ===
|
|
|
|
lastEntry.appState[key]?.elementId
|
|
|
|
) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2021-05-29 21:35:03 +02:00
|
|
|
if (key === "selectedElementIds" || key === "selectedGroupIds") {
|
2020-05-23 07:26:59 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (nextEntry.appState[key] !== lastEntry.appState[key]) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2020-01-06 21:58:48 +04:00
|
|
|
}
|
|
|
|
|
2020-05-23 07:26:59 +02:00
|
|
|
pushEntry(appState: AppState, elements: readonly ExcalidrawElement[]) {
|
2020-05-23 12:07:11 -07:00
|
|
|
const newEntryDehydrated = this.generateEntry(appState, elements);
|
|
|
|
const newEntry: HistoryEntry = this.hydrateHistoryEntry(newEntryDehydrated);
|
2020-05-23 07:26:59 +02:00
|
|
|
|
|
|
|
if (newEntry) {
|
|
|
|
if (!this.shouldCreateEntry(newEntry)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-05-23 12:07:11 -07:00
|
|
|
this.stateHistory.push(newEntryDehydrated);
|
2020-05-23 07:26:59 +02:00
|
|
|
this.lastEntry = newEntry;
|
|
|
|
// As a new entry was pushed, we invalidate the redo stack
|
|
|
|
this.clearRedoStack();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-06 21:58:48 +04:00
|
|
|
clearRedoStack() {
|
|
|
|
this.redoStack.splice(0, this.redoStack.length);
|
|
|
|
}
|
|
|
|
|
2020-05-23 07:26:59 +02:00
|
|
|
redoOnce(): HistoryEntry | 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-05-25 00:17:14 +02:00
|
|
|
return this.hydrateHistoryEntry(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-05-23 07:26:59 +02:00
|
|
|
undoOnce(): HistoryEntry | 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-05-25 00:17:14 +02:00
|
|
|
return this.hydrateHistoryEntry(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-05-23 07:26:59 +02:00
|
|
|
/**
|
|
|
|
* Updates history's `lastEntry` to latest app state. This is necessary
|
|
|
|
* when doing undo/redo which itself doesn't commit to history, but updates
|
|
|
|
* app state in a way that would break `shouldCreateEntry` which relies on
|
|
|
|
* `lastEntry` to reflect last comittable history state.
|
|
|
|
* We can't update `lastEntry` from within history when calling undo/redo
|
|
|
|
* because the action potentially mutates appState/elements before storing
|
|
|
|
* it.
|
|
|
|
*/
|
|
|
|
setCurrentState(appState: AppState, elements: readonly ExcalidrawElement[]) {
|
2020-05-23 12:07:11 -07:00
|
|
|
this.lastEntry = this.hydrateHistoryEntry(
|
|
|
|
this.generateEntry(appState, elements),
|
|
|
|
);
|
2020-05-23 07:26:59 +02: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
|
|
|
}
|
|
|
|
|
2021-04-24 18:21:02 +05:30
|
|
|
export default History;
|