2020-03-01 14:39:03 -05:00
|
|
|
import { Action } from "./types";
|
|
|
|
import React from "react";
|
|
|
|
import { undo, redo } from "../components/icons";
|
|
|
|
import { ToolButton } from "../components/ToolButton";
|
|
|
|
import { t } from "../i18n";
|
|
|
|
import { SceneHistory } from "../history";
|
|
|
|
import { ExcalidrawElement } from "../element/types";
|
|
|
|
import { AppState } from "../types";
|
|
|
|
import { KEYS } from "../keys";
|
|
|
|
|
|
|
|
const writeData = (
|
|
|
|
appState: AppState,
|
2020-03-07 10:20:38 -05:00
|
|
|
updater: () => { elements: ExcalidrawElement[]; appState: AppState } | null,
|
2020-03-01 14:39:03 -05:00
|
|
|
) => {
|
2020-03-07 10:20:38 -05:00
|
|
|
if (
|
|
|
|
[
|
|
|
|
appState.multiElement,
|
|
|
|
appState.resizingElement,
|
|
|
|
appState.editingElement,
|
|
|
|
appState.draggingElement,
|
|
|
|
].some(Boolean)
|
|
|
|
) {
|
|
|
|
const data = updater();
|
|
|
|
|
|
|
|
return data === null
|
|
|
|
? {}
|
|
|
|
: {
|
|
|
|
elements: data.elements,
|
|
|
|
appState: { ...appState, ...data.appState },
|
|
|
|
};
|
2020-03-01 14:39:03 -05:00
|
|
|
}
|
|
|
|
return {};
|
|
|
|
};
|
|
|
|
|
2020-03-07 10:20:38 -05:00
|
|
|
const testUndo = (shift: boolean) => (event: KeyboardEvent) =>
|
|
|
|
event[KEYS.META] && /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;
|
|
|
|
|
|
|
|
export const createUndoAction: ActionCreator = history => ({
|
2020-03-01 14:39:03 -05:00
|
|
|
name: "undo",
|
2020-03-07 10:20:38 -05:00
|
|
|
perform: (_, appState) => writeData(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-07 10:20:38 -05:00
|
|
|
export const createRedoAction: ActionCreator = history => ({
|
2020-03-01 14:39:03 -05:00
|
|
|
name: "redo",
|
2020-03-07 10:20:38 -05:00
|
|
|
perform: (_, appState) => writeData(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,
|
|
|
|
});
|