* Bump prettier from 1.19.1 to 2.0.1 Bumps [prettier](https://github.com/prettier/prettier) from 1.19.1 to 2.0.1. - [Release notes](https://github.com/prettier/prettier/releases) - [Changelog](https://github.com/prettier/prettier/blob/master/CHANGELOG.md) - [Commits](https://github.com/prettier/prettier/compare/1.19.1...2.0.1) Signed-off-by: dependabot-preview[bot] <support@dependabot.com> * Update formatting Co-authored-by: dependabot-preview[bot] <27856297+dependabot-preview[bot]@users.noreply.github.com> Co-authored-by: Panayiotis Lipiridis <lipiridis@gmail.com>
96 lines
2.7 KiB
TypeScript
96 lines
2.7 KiB
TypeScript
import { Action, ActionResult } 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";
|
|
import { getElementMap } from "../element";
|
|
import { newElementWith } from "../element/mutateElement";
|
|
|
|
const writeData = (
|
|
prevElements: readonly ExcalidrawElement[],
|
|
appState: AppState,
|
|
updater: () => {
|
|
elements: ExcalidrawElement[];
|
|
appState: AppState;
|
|
} | null,
|
|
): ActionResult => {
|
|
const commitToHistory = false;
|
|
if (
|
|
!appState.multiElement &&
|
|
!appState.resizingElement &&
|
|
!appState.editingElement &&
|
|
!appState.draggingElement
|
|
) {
|
|
const data = updater();
|
|
if (data === null) {
|
|
return { commitToHistory };
|
|
}
|
|
|
|
const prevElementMap = getElementMap(prevElements);
|
|
const nextElements = data.elements;
|
|
const nextElementMap = getElementMap(nextElements);
|
|
return {
|
|
elements: nextElements
|
|
.map((nextElement) =>
|
|
newElementWith(
|
|
prevElementMap[nextElement.id] || nextElement,
|
|
nextElement,
|
|
),
|
|
)
|
|
.concat(
|
|
prevElements
|
|
.filter(
|
|
(prevElement) => !nextElementMap.hasOwnProperty(prevElement.id),
|
|
)
|
|
.map((prevElement) =>
|
|
newElementWith(prevElement, { isDeleted: true }),
|
|
),
|
|
),
|
|
appState: { ...appState, ...data.appState },
|
|
commitToHistory,
|
|
};
|
|
}
|
|
return { commitToHistory };
|
|
};
|
|
|
|
const testUndo = (shift: boolean) => (event: KeyboardEvent) =>
|
|
event[KEYS.CTRL_OR_CMD] && /z/i.test(event.key) && event.shiftKey === shift;
|
|
|
|
type ActionCreator = (history: SceneHistory) => Action;
|
|
|
|
export const createUndoAction: ActionCreator = (history) => ({
|
|
name: "undo",
|
|
perform: (elements, appState) =>
|
|
writeData(elements, appState, () => history.undoOnce()),
|
|
keyTest: testUndo(false),
|
|
PanelComponent: ({ updateData }) => (
|
|
<ToolButton
|
|
type="button"
|
|
icon={undo}
|
|
aria-label={t("buttons.undo")}
|
|
onClick={updateData}
|
|
/>
|
|
),
|
|
commitToHistory: () => false,
|
|
});
|
|
|
|
export const createRedoAction: ActionCreator = (history) => ({
|
|
name: "redo",
|
|
perform: (elements, appState) =>
|
|
writeData(elements, appState, () => history.redoOnce()),
|
|
keyTest: testUndo(true),
|
|
PanelComponent: ({ updateData }) => (
|
|
<ToolButton
|
|
type="button"
|
|
icon={redo}
|
|
aria-label={t("buttons.redo")}
|
|
onClick={updateData}
|
|
/>
|
|
),
|
|
commitToHistory: () => false,
|
|
});
|