fileHandle refactor & fixes (#2252)

This commit is contained in:
David Luzar 2020-10-19 10:53:37 +02:00 committed by GitHub
parent 4a26845395
commit 1484c5a63b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
17 changed files with 163 additions and 41 deletions

View file

@ -59,8 +59,6 @@ export const actionClearCanvas = register({
showAriaLabel={useIsMobile()}
onClick={() => {
if (window.confirm(t("alerts.clearReset"))) {
// TODO: Make this part of `AppState`.
(window as any).handle = null;
updateData(null);
}
}}

View file

@ -85,12 +85,16 @@ export const actionChangeShouldAddWatermark = register({
export const actionSaveScene = register({
name: "saveScene",
perform: (elements, appState, value) => {
// TODO: Make this part of `AppState`.
saveAsJSON(elements, appState, (window as any).handle)
.catch(muteFSAbortError)
.catch((error) => console.error(error));
return { commitToHistory: false };
perform: async (elements, appState, value) => {
try {
const { fileHandle } = await saveAsJSON(elements, appState);
return { commitToHistory: false, appState: { ...appState, fileHandle } };
} catch (error) {
if (error?.name !== "AbortError") {
console.error(error);
}
return { commitToHistory: false };
}
},
keyTest: (event) => {
return event.key === "s" && event[KEYS.CTRL_OR_CMD] && !event.shiftKey;
@ -109,11 +113,19 @@ export const actionSaveScene = register({
export const actionSaveAsScene = register({
name: "saveAsScene",
perform: (elements, appState, value) => {
saveAsJSON(elements, appState, null)
.catch(muteFSAbortError)
.catch((error) => console.error(error));
return { commitToHistory: false };
perform: async (elements, appState, value) => {
try {
const { fileHandle } = await saveAsJSON(elements, {
...appState,
fileHandle: null,
});
return { commitToHistory: false, appState: { ...appState, fileHandle } };
} catch (error) {
if (error?.name !== "AbortError") {
console.error(error);
}
return { commitToHistory: false };
}
},
keyTest: (event) => {
return event.key === "s" && event.shiftKey && event[KEYS.CTRL_OR_CMD];

View file

@ -5,6 +5,7 @@ import {
UpdaterFn,
ActionFilterFn,
ActionName,
ActionResult,
} from "./types";
import { ExcalidrawElement } from "../element/types";
import { AppState } from "../types";
@ -13,7 +14,7 @@ import { t } from "../i18n";
export class ActionManager implements ActionsManagerInterface {
actions = {} as ActionsManagerInterface["actions"];
updater: UpdaterFn;
updater: (actionResult: ActionResult | Promise<ActionResult>) => void;
getAppState: () => Readonly<AppState>;
@ -24,7 +25,15 @@ export class ActionManager implements ActionsManagerInterface {
getAppState: () => AppState,
getElementsIncludingDeleted: () => readonly ExcalidrawElement[],
) {
this.updater = updater;
this.updater = (actionResult) => {
if (actionResult && "then" in actionResult) {
actionResult.then((actionResult) => {
return updater(actionResult);
});
} else {
return updater(actionResult);
}
};
this.getAppState = getAppState;
this.getElementsIncludingDeleted = getElementsIncludingDeleted;
}

View file

@ -16,9 +16,9 @@ type ActionFn = (
elements: readonly ExcalidrawElement[],
appState: Readonly<AppState>,
formData: any,
) => ActionResult;
) => ActionResult | Promise<ActionResult>;
export type UpdaterFn = (res: ActionResult, commitToHistory?: boolean) => void;
export type UpdaterFn = (res: ActionResult) => void;
export type ActionFilterFn = (action: Action) => void;
export type ActionName =