2020-03-19 14:51:05 +01:00
|
|
|
import { Action, ActionResult } from "./types";
|
2020-03-01 14:39:03 -05:00
|
|
|
import React from "react";
|
|
|
|
import { undo, redo } from "../components/icons";
|
|
|
|
import { ToolButton } from "../components/ToolButton";
|
|
|
|
import { t } from "../i18n";
|
2020-05-23 07:26:59 +02:00
|
|
|
import { SceneHistory, HistoryEntry } from "../history";
|
2020-03-01 14:39:03 -05:00
|
|
|
import { ExcalidrawElement } from "../element/types";
|
|
|
|
import { AppState } from "../types";
|
|
|
|
import { KEYS } from "../keys";
|
2020-03-14 20:46:57 -07:00
|
|
|
import { getElementMap } from "../element";
|
|
|
|
import { newElementWith } from "../element/mutateElement";
|
2020-08-08 21:04:15 -07:00
|
|
|
import { fixBindingsAfterDeletion } from "../element/binding";
|
2020-03-01 14:39:03 -05:00
|
|
|
|
|
|
|
const writeData = (
|
2020-03-14 20:46:57 -07:00
|
|
|
prevElements: readonly ExcalidrawElement[],
|
2020-03-01 14:39:03 -05:00
|
|
|
appState: AppState,
|
2020-05-23 07:26:59 +02:00
|
|
|
updater: () => HistoryEntry | null,
|
2020-03-19 14:51:05 +01:00
|
|
|
): ActionResult => {
|
|
|
|
const commitToHistory = false;
|
2020-03-07 10:20:38 -05:00
|
|
|
if (
|
2020-03-07 20:37:35 -05:00
|
|
|
!appState.multiElement &&
|
|
|
|
!appState.resizingElement &&
|
|
|
|
!appState.editingElement &&
|
|
|
|
!appState.draggingElement
|
2020-03-07 10:20:38 -05:00
|
|
|
) {
|
|
|
|
const data = updater();
|
2020-03-14 20:46:57 -07:00
|
|
|
if (data === null) {
|
2020-03-19 14:51:05 +01:00
|
|
|
return { commitToHistory };
|
2020-03-14 20:46:57 -07:00
|
|
|
}
|
2020-03-07 10:20:38 -05:00
|
|
|
|
2020-03-14 20:46:57 -07:00
|
|
|
const prevElementMap = getElementMap(prevElements);
|
|
|
|
const nextElements = data.elements;
|
|
|
|
const nextElementMap = getElementMap(nextElements);
|
2020-06-01 11:35:44 +02:00
|
|
|
|
2020-08-08 21:04:15 -07:00
|
|
|
const deletedElements = prevElements.filter(
|
|
|
|
(prevElement) => !nextElementMap.hasOwnProperty(prevElement.id),
|
|
|
|
);
|
2020-06-01 11:35:44 +02:00
|
|
|
const elements = nextElements
|
|
|
|
.map((nextElement) =>
|
|
|
|
newElementWith(
|
|
|
|
prevElementMap[nextElement.id] || nextElement,
|
|
|
|
nextElement,
|
2020-03-14 20:46:57 -07:00
|
|
|
),
|
2020-06-01 11:35:44 +02:00
|
|
|
)
|
|
|
|
.concat(
|
2020-08-08 21:04:15 -07:00
|
|
|
deletedElements.map((prevElement) =>
|
|
|
|
newElementWith(prevElement, { isDeleted: true }),
|
|
|
|
),
|
2020-06-01 11:35:44 +02:00
|
|
|
);
|
2020-08-08 21:04:15 -07:00
|
|
|
fixBindingsAfterDeletion(elements, deletedElements);
|
2020-06-01 11:35:44 +02:00
|
|
|
|
|
|
|
return {
|
|
|
|
elements,
|
2020-03-14 20:46:57 -07:00
|
|
|
appState: { ...appState, ...data.appState },
|
2020-03-19 14:51:05 +01:00
|
|
|
commitToHistory,
|
2020-05-23 07:26:59 +02:00
|
|
|
syncHistory: true,
|
2020-03-14 20:46:57 -07:00
|
|
|
};
|
2020-03-01 14:39:03 -05:00
|
|
|
}
|
2020-03-19 14:51:05 +01:00
|
|
|
return { commitToHistory };
|
2020-03-01 14:39:03 -05:00
|
|
|
};
|
|
|
|
|
2020-03-07 10:20:38 -05:00
|
|
|
const testUndo = (shift: boolean) => (event: KeyboardEvent) =>
|
2020-03-09 15:06:35 +02:00
|
|
|
event[KEYS.CTRL_OR_CMD] && /z/i.test(event.key) && event.shiftKey === shift;
|
2020-03-01 14:39:03 -05:00
|
|
|
|
2020-03-07 10:20:38 -05:00
|
|
|
type ActionCreator = (history: SceneHistory) => Action;
|
|
|
|
|
2020-03-23 13:05:07 +02:00
|
|
|
export const createUndoAction: ActionCreator = (history) => ({
|
2020-03-01 14:39:03 -05:00
|
|
|
name: "undo",
|
2020-03-14 20:46:57 -07:00
|
|
|
perform: (elements, appState) =>
|
|
|
|
writeData(elements, appState, () => history.undoOnce()),
|
2020-03-01 14:39:03 -05:00
|
|
|
keyTest: testUndo(false),
|
|
|
|
PanelComponent: ({ updateData }) => (
|
|
|
|
<ToolButton
|
|
|
|
type="button"
|
|
|
|
icon={undo}
|
|
|
|
aria-label={t("buttons.undo")}
|
|
|
|
onClick={updateData}
|
|
|
|
/>
|
|
|
|
),
|
|
|
|
commitToHistory: () => false,
|
|
|
|
});
|
|
|
|
|
2020-03-23 13:05:07 +02:00
|
|
|
export const createRedoAction: ActionCreator = (history) => ({
|
2020-03-01 14:39:03 -05:00
|
|
|
name: "redo",
|
2020-03-14 20:46:57 -07:00
|
|
|
perform: (elements, appState) =>
|
|
|
|
writeData(elements, appState, () => history.redoOnce()),
|
2020-03-01 14:39:03 -05:00
|
|
|
keyTest: testUndo(true),
|
|
|
|
PanelComponent: ({ updateData }) => (
|
|
|
|
<ToolButton
|
|
|
|
type="button"
|
|
|
|
icon={redo}
|
|
|
|
aria-label={t("buttons.redo")}
|
|
|
|
onClick={updateData}
|
|
|
|
/>
|
|
|
|
),
|
|
|
|
commitToHistory: () => false,
|
|
|
|
});
|