2020-01-12 02:22:03 +04:00
|
|
|
import React from "react";
|
2020-01-12 07:10:15 -08:00
|
|
|
import {
|
|
|
|
Action,
|
|
|
|
ActionsManagerInterface,
|
|
|
|
UpdaterFn,
|
|
|
|
ActionFilterFn
|
|
|
|
} from "./types";
|
2020-01-12 02:22:03 +04:00
|
|
|
import { ExcalidrawElement } from "../element/types";
|
|
|
|
import { AppState } from "../types";
|
2020-01-21 01:14:10 +02:00
|
|
|
import { TFunction } from "i18next";
|
2020-01-12 02:22:03 +04:00
|
|
|
|
|
|
|
export class ActionManager implements ActionsManagerInterface {
|
|
|
|
actions: { [keyProp: string]: Action } = {};
|
|
|
|
|
|
|
|
updater:
|
|
|
|
| ((elements: ExcalidrawElement[], appState: AppState) => void)
|
|
|
|
| null = null;
|
|
|
|
|
|
|
|
setUpdater(
|
|
|
|
updater: (elements: ExcalidrawElement[], appState: AppState) => void
|
|
|
|
) {
|
|
|
|
this.updater = updater;
|
|
|
|
}
|
|
|
|
|
|
|
|
registerAction(action: Action) {
|
|
|
|
this.actions[action.name] = action;
|
|
|
|
}
|
|
|
|
|
|
|
|
handleKeyDown(
|
|
|
|
event: KeyboardEvent,
|
|
|
|
elements: readonly ExcalidrawElement[],
|
|
|
|
appState: AppState
|
|
|
|
) {
|
|
|
|
const data = Object.values(this.actions)
|
|
|
|
.sort((a, b) => (b.keyPriority || 0) - (a.keyPriority || 0))
|
|
|
|
.filter(
|
|
|
|
action => action.keyTest && action.keyTest(event, elements, appState)
|
|
|
|
);
|
|
|
|
|
|
|
|
if (data.length === 0) return {};
|
|
|
|
|
|
|
|
event.preventDefault();
|
|
|
|
return data[0].perform(elements, appState, null);
|
|
|
|
}
|
|
|
|
|
|
|
|
getContextMenuItems(
|
|
|
|
elements: readonly ExcalidrawElement[],
|
|
|
|
appState: AppState,
|
2020-01-12 07:10:15 -08:00
|
|
|
updater: UpdaterFn,
|
2020-01-21 01:14:10 +02:00
|
|
|
actionFilter: ActionFilterFn = action => action,
|
|
|
|
t?: TFunction
|
2020-01-12 02:22:03 +04:00
|
|
|
) {
|
|
|
|
return Object.values(this.actions)
|
2020-01-12 07:10:15 -08:00
|
|
|
.filter(actionFilter)
|
2020-01-12 02:22:03 +04:00
|
|
|
.filter(action => "contextItemLabel" in action)
|
|
|
|
.sort(
|
|
|
|
(a, b) =>
|
|
|
|
(a.contextMenuOrder !== undefined ? a.contextMenuOrder : 999) -
|
|
|
|
(b.contextMenuOrder !== undefined ? b.contextMenuOrder : 999)
|
|
|
|
)
|
|
|
|
.map(action => ({
|
2020-01-21 01:14:10 +02:00
|
|
|
label:
|
|
|
|
t && action.contextItemLabel
|
|
|
|
? t(action.contextItemLabel)
|
|
|
|
: action.contextItemLabel!,
|
2020-01-12 02:22:03 +04:00
|
|
|
action: () => {
|
|
|
|
updater(action.perform(elements, appState, null));
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
renderAction(
|
|
|
|
name: string,
|
|
|
|
elements: readonly ExcalidrawElement[],
|
|
|
|
appState: AppState,
|
2020-01-21 01:14:10 +02:00
|
|
|
updater: UpdaterFn,
|
|
|
|
t: TFunction
|
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!;
|
|
|
|
const updateData = (formState: any) => {
|
|
|
|
updater(action.perform(elements, appState, formState));
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<PanelComponent
|
|
|
|
elements={elements}
|
|
|
|
appState={appState}
|
|
|
|
updateData={updateData}
|
2020-01-21 01:14:10 +02:00
|
|
|
t={t}
|
2020-01-12 02:22:03 +04:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|