2020-01-12 02:22:03 +04:00
|
|
|
import React from "react";
|
2020-01-12 07:10:15 -08:00
|
|
|
import {
|
|
|
|
Action,
|
|
|
|
ActionsManagerInterface,
|
|
|
|
UpdaterFn,
|
2020-03-25 14:13:59 +01:00
|
|
|
ActionName,
|
2020-10-19 10:53:37 +02:00
|
|
|
ActionResult,
|
2020-01-12 07:10:15 -08:00
|
|
|
} from "./types";
|
2020-01-12 02:22:03 +04:00
|
|
|
import { ExcalidrawElement } from "../element/types";
|
2021-02-02 02:26:42 +05:30
|
|
|
import { AppState, ExcalidrawProps } from "../types";
|
2021-01-28 00:41:17 +05:30
|
|
|
|
|
|
|
// This is the <App> component, but for now we don't care about anything but its
|
|
|
|
// `canvas` state.
|
2021-02-02 02:26:42 +05:30
|
|
|
type App = { canvas: HTMLCanvasElement | null; props: ExcalidrawProps };
|
2020-01-12 02:22:03 +04:00
|
|
|
|
|
|
|
export class ActionManager implements ActionsManagerInterface {
|
2020-03-25 14:13:59 +01:00
|
|
|
actions = {} as ActionsManagerInterface["actions"];
|
2020-01-12 02:22:03 +04:00
|
|
|
|
2020-10-19 10:53:37 +02:00
|
|
|
updater: (actionResult: ActionResult | Promise<ActionResult>) => void;
|
2020-01-12 02:22:03 +04:00
|
|
|
|
2020-08-08 22:35:34 +02:00
|
|
|
getAppState: () => Readonly<AppState>;
|
2020-04-08 09:49:52 -07:00
|
|
|
getElementsIncludingDeleted: () => readonly ExcalidrawElement[];
|
2021-01-28 00:41:17 +05:30
|
|
|
app: App;
|
2020-02-05 22:47:10 +04:00
|
|
|
|
|
|
|
constructor(
|
|
|
|
updater: UpdaterFn,
|
|
|
|
getAppState: () => AppState,
|
2020-07-30 14:50:59 +05:30
|
|
|
getElementsIncludingDeleted: () => readonly ExcalidrawElement[],
|
2021-01-28 00:41:17 +05:30
|
|
|
app: App,
|
2020-01-12 02:22:03 +04:00
|
|
|
) {
|
2020-10-19 10:53:37 +02:00
|
|
|
this.updater = (actionResult) => {
|
|
|
|
if (actionResult && "then" in actionResult) {
|
|
|
|
actionResult.then((actionResult) => {
|
|
|
|
return updater(actionResult);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
return updater(actionResult);
|
|
|
|
}
|
|
|
|
};
|
2020-02-05 22:47:10 +04:00
|
|
|
this.getAppState = getAppState;
|
2020-04-08 09:49:52 -07:00
|
|
|
this.getElementsIncludingDeleted = getElementsIncludingDeleted;
|
2021-01-28 00:41:17 +05:30
|
|
|
this.app = app;
|
2020-01-12 02:22:03 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
registerAction(action: Action) {
|
|
|
|
this.actions[action.name] = action;
|
|
|
|
}
|
|
|
|
|
2020-03-07 10:20:38 -05:00
|
|
|
registerAll(actions: readonly Action[]) {
|
2020-03-23 13:05:07 +02:00
|
|
|
actions.forEach((action) => this.registerAction(action));
|
2020-03-07 10:20:38 -05:00
|
|
|
}
|
|
|
|
|
2020-02-05 22:47:10 +04:00
|
|
|
handleKeyDown(event: KeyboardEvent) {
|
2020-01-12 02:22:03 +04:00
|
|
|
const data = Object.values(this.actions)
|
|
|
|
.sort((a, b) => (b.keyPriority || 0) - (a.keyPriority || 0))
|
|
|
|
.filter(
|
2020-03-23 13:05:07 +02:00
|
|
|
(action) =>
|
2020-02-05 22:47:10 +04:00
|
|
|
action.keyTest &&
|
2020-04-08 09:49:52 -07:00
|
|
|
action.keyTest(
|
|
|
|
event,
|
|
|
|
this.getAppState(),
|
|
|
|
this.getElementsIncludingDeleted(),
|
|
|
|
),
|
2020-01-12 02:22:03 +04:00
|
|
|
);
|
|
|
|
|
2020-02-02 20:04:35 +02:00
|
|
|
if (data.length === 0) {
|
2020-02-07 23:46:19 +01:00
|
|
|
return false;
|
2020-02-02 20:04:35 +02:00
|
|
|
}
|
2021-02-02 02:26:42 +05:30
|
|
|
const { viewModeEnabled } = this.getAppState();
|
|
|
|
if (viewModeEnabled) {
|
|
|
|
if (data[0].name !== "viewMode") {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2020-01-12 02:22:03 +04:00
|
|
|
|
|
|
|
event.preventDefault();
|
2020-04-08 09:49:52 -07:00
|
|
|
this.updater(
|
|
|
|
data[0].perform(
|
|
|
|
this.getElementsIncludingDeleted(),
|
|
|
|
this.getAppState(),
|
|
|
|
null,
|
2021-01-28 00:41:17 +05:30
|
|
|
this.app,
|
2020-04-08 09:49:52 -07:00
|
|
|
),
|
|
|
|
);
|
2020-02-07 23:46:19 +01:00
|
|
|
return true;
|
2020-01-12 02:22:03 +04:00
|
|
|
}
|
|
|
|
|
2020-03-18 16:43:06 +01:00
|
|
|
executeAction(action: Action) {
|
2020-04-08 09:49:52 -07:00
|
|
|
this.updater(
|
|
|
|
action.perform(
|
|
|
|
this.getElementsIncludingDeleted(),
|
|
|
|
this.getAppState(),
|
|
|
|
null,
|
2021-01-28 00:41:17 +05:30
|
|
|
this.app,
|
2020-04-08 09:49:52 -07:00
|
|
|
),
|
|
|
|
);
|
2020-03-18 16:43:06 +01:00
|
|
|
}
|
|
|
|
|
2020-06-19 11:36:49 +01:00
|
|
|
// Id is an attribute that we can use to pass in data like keys.
|
|
|
|
// This is needed for dynamically generated action components
|
|
|
|
// like the user list. We can use this key to extract more
|
|
|
|
// data from app state. This is an alternative to generic prop hell!
|
|
|
|
renderAction = (name: ActionName, id?: string) => {
|
2020-01-12 02:22:03 +04:00
|
|
|
if (this.actions[name] && "PanelComponent" in this.actions[name]) {
|
|
|
|
const action = this.actions[name];
|
|
|
|
const PanelComponent = action.PanelComponent!;
|
2020-03-01 14:39:03 -05:00
|
|
|
const updateData = (formState?: any) => {
|
2020-02-05 22:47:10 +04:00
|
|
|
this.updater(
|
2020-04-08 09:49:52 -07:00
|
|
|
action.perform(
|
|
|
|
this.getElementsIncludingDeleted(),
|
|
|
|
this.getAppState(),
|
|
|
|
formState,
|
2021-01-28 00:41:17 +05:30
|
|
|
this.app,
|
2020-04-08 09:49:52 -07:00
|
|
|
),
|
2020-02-05 22:47:10 +04:00
|
|
|
);
|
2020-01-12 02:22:03 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<PanelComponent
|
2020-04-08 09:49:52 -07:00
|
|
|
elements={this.getElementsIncludingDeleted()}
|
2020-02-05 22:47:10 +04:00
|
|
|
appState={this.getAppState()}
|
2020-01-12 02:22:03 +04:00
|
|
|
updateData={updateData}
|
2020-06-19 11:36:49 +01:00
|
|
|
id={id}
|
2020-01-12 02:22:03 +04:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
2020-03-07 10:20:38 -05:00
|
|
|
};
|
2020-01-12 02:22:03 +04:00
|
|
|
}
|