move footer into layerUI & refactor ActionManager (#729)

This commit is contained in:
David Luzar
2020-02-07 23:46:19 +01:00
committed by GitHub
parent 88eacc9da7
commit d79293de06
7 changed files with 164 additions and 179 deletions

View File

@ -14,20 +14,16 @@ export class ActionManager implements ActionsManagerInterface {
updater: UpdaterFn;
resumeHistoryRecording: () => void;
getAppState: () => AppState;
getElements: () => readonly ExcalidrawElement[];
constructor(
updater: UpdaterFn,
resumeHistoryRecording: () => void,
getAppState: () => AppState,
getElements: () => readonly ExcalidrawElement[],
) {
this.updater = updater;
this.resumeHistoryRecording = resumeHistoryRecording;
this.getAppState = getAppState;
this.getElements = getElements;
}
@ -46,17 +42,18 @@ export class ActionManager implements ActionsManagerInterface {
);
if (data.length === 0) {
return null;
return false;
}
event.preventDefault();
if (
const commitToHistory =
data[0].commitToHistory &&
data[0].commitToHistory(this.getAppState(), this.getElements())
) {
this.resumeHistoryRecording();
}
return data[0].perform(this.getElements(), this.getAppState(), null);
data[0].commitToHistory(this.getAppState(), this.getElements());
this.updater(
data[0].perform(this.getElements(), this.getAppState(), null),
commitToHistory,
);
return true;
}
getContextMenuItems(actionFilter: ActionFilterFn = action => action) {
@ -71,14 +68,12 @@ export class ActionManager implements ActionsManagerInterface {
.map(action => ({
label: action.contextItemLabel ? t(action.contextItemLabel) : "",
action: () => {
if (
const commitToHistory =
action.commitToHistory &&
action.commitToHistory(this.getAppState(), this.getElements())
) {
this.resumeHistoryRecording();
}
action.commitToHistory(this.getAppState(), this.getElements());
this.updater(
action.perform(this.getElements(), this.getAppState(), null),
commitToHistory,
);
},
}));
@ -89,15 +84,12 @@ export class ActionManager implements ActionsManagerInterface {
const action = this.actions[name];
const PanelComponent = action.PanelComponent!;
const updateData = (formState: any) => {
if (
const commitToHistory =
action.commitToHistory &&
action.commitToHistory(this.getAppState(), this.getElements()) ===
true
) {
this.resumeHistoryRecording();
}
action.commitToHistory(this.getAppState(), this.getElements());
this.updater(
action.perform(this.getElements(), this.getAppState(), formState),
commitToHistory,
);
};

View File

@ -3,8 +3,8 @@ import { ExcalidrawElement } from "../element/types";
import { AppState } from "../types";
export type ActionResult = {
elements?: readonly ExcalidrawElement[];
appState?: AppState;
elements?: readonly ExcalidrawElement[] | null;
appState?: AppState | null;
};
type ActionFn = (
@ -13,7 +13,7 @@ type ActionFn = (
formData: any,
) => ActionResult;
export type UpdaterFn = (res: ActionResult) => void;
export type UpdaterFn = (res: ActionResult, commitToHistory?: boolean) => void;
export type ActionFilterFn = (action: Action) => void;
export interface Action {
@ -43,7 +43,7 @@ export interface ActionsManagerInterface {
[keyProp: string]: Action;
};
registerAction: (action: Action) => void;
handleKeyDown: (event: KeyboardEvent) => ActionResult | null;
handleKeyDown: (event: KeyboardEvent) => boolean;
getContextMenuItems: (
actionFilter: ActionFilterFn,
) => { label: string; action: () => void }[];