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-01-24 12:04:54 +02:00
|
|
|
ActionFilterFn,
|
2020-03-25 14:13:59 +01:00
|
|
|
ActionName,
|
2020-01-12 07:10:15 -08:00
|
|
|
} from "./types";
|
2020-01-12 02:22:03 +04:00
|
|
|
import { ExcalidrawElement } from "../element/types";
|
|
|
|
import { AppState } from "../types";
|
2020-01-31 21:06:06 +00:00
|
|
|
import { t } from "../i18n";
|
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-02-05 22:47:10 +04:00
|
|
|
updater: UpdaterFn;
|
2020-01-12 02:22:03 +04:00
|
|
|
|
2020-08-08 22:35:34 +02:00
|
|
|
getAppState: () => Readonly<AppState>;
|
2020-02-05 22:47:10 +04:00
|
|
|
|
2020-04-08 09:49:52 -07:00
|
|
|
getElementsIncludingDeleted: () => readonly ExcalidrawElement[];
|
2020-02-05 22:47:10 +04:00
|
|
|
|
|
|
|
constructor(
|
|
|
|
updater: UpdaterFn,
|
|
|
|
getAppState: () => AppState,
|
2020-07-30 14:50:59 +05:30
|
|
|
getElementsIncludingDeleted: () => readonly ExcalidrawElement[],
|
2020-01-12 02:22:03 +04:00
|
|
|
) {
|
|
|
|
this.updater = updater;
|
2020-02-05 22:47:10 +04:00
|
|
|
this.getAppState = getAppState;
|
2020-04-08 09:49:52 -07:00
|
|
|
this.getElementsIncludingDeleted = getElementsIncludingDeleted;
|
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
|
|
|
}
|
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,
|
|
|
|
),
|
|
|
|
);
|
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,
|
|
|
|
),
|
|
|
|
);
|
2020-03-18 16:43:06 +01:00
|
|
|
}
|
|
|
|
|
2020-03-23 13:05:07 +02:00
|
|
|
getContextMenuItems(actionFilter: ActionFilterFn = (action) => action) {
|
2020-01-12 02:22:03 +04:00
|
|
|
return Object.values(this.actions)
|
2020-01-12 07:10:15 -08:00
|
|
|
.filter(actionFilter)
|
2020-03-23 13:05:07 +02:00
|
|
|
.filter((action) => "contextItemLabel" in action)
|
2020-07-09 22:32:27 +02:00
|
|
|
.filter((action) =>
|
|
|
|
action.contextItemPredicate
|
|
|
|
? action.contextItemPredicate(
|
|
|
|
this.getElementsIncludingDeleted(),
|
|
|
|
this.getAppState(),
|
|
|
|
)
|
|
|
|
: true,
|
|
|
|
)
|
2020-01-12 02:22:03 +04:00
|
|
|
.sort(
|
|
|
|
(a, b) =>
|
|
|
|
(a.contextMenuOrder !== undefined ? a.contextMenuOrder : 999) -
|
2020-01-24 12:04:54 +02:00
|
|
|
(b.contextMenuOrder !== undefined ? b.contextMenuOrder : 999),
|
2020-01-12 02:22:03 +04:00
|
|
|
)
|
2020-03-23 13:05:07 +02:00
|
|
|
.map((action) => ({
|
2020-01-31 21:06:06 +00:00
|
|
|
label: action.contextItemLabel ? t(action.contextItemLabel) : "",
|
2020-01-12 02:22:03 +04:00
|
|
|
action: () => {
|
2020-02-05 22:47:10 +04:00
|
|
|
this.updater(
|
2020-04-08 09:49:52 -07:00
|
|
|
action.perform(
|
|
|
|
this.getElementsIncludingDeleted(),
|
|
|
|
this.getAppState(),
|
|
|
|
null,
|
|
|
|
),
|
2020-02-05 22:47:10 +04:00
|
|
|
);
|
2020-01-24 12:04:54 +02:00
|
|
|
},
|
2020-01-12 02:22:03 +04: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,
|
|
|
|
),
|
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
|
|
|
}
|