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-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 {
|
|
|
|
actions: { [keyProp: string]: Action } = {};
|
|
|
|
|
2020-02-05 22:47:10 +04:00
|
|
|
updater: UpdaterFn;
|
2020-01-12 02:22:03 +04:00
|
|
|
|
2020-02-05 22:47:10 +04:00
|
|
|
resumeHistoryRecording: () => void;
|
|
|
|
|
|
|
|
getAppState: () => AppState;
|
|
|
|
|
|
|
|
getElements: () => readonly ExcalidrawElement[];
|
|
|
|
|
|
|
|
constructor(
|
|
|
|
updater: UpdaterFn,
|
|
|
|
resumeHistoryRecording: () => void,
|
|
|
|
getAppState: () => AppState,
|
|
|
|
getElements: () => readonly ExcalidrawElement[],
|
2020-01-12 02:22:03 +04:00
|
|
|
) {
|
|
|
|
this.updater = updater;
|
2020-02-05 22:47:10 +04:00
|
|
|
this.resumeHistoryRecording = resumeHistoryRecording;
|
|
|
|
this.getAppState = getAppState;
|
|
|
|
this.getElements = getElements;
|
2020-01-12 02:22:03 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
registerAction(action: Action) {
|
|
|
|
this.actions[action.name] = action;
|
|
|
|
}
|
|
|
|
|
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-02-05 22:47:10 +04:00
|
|
|
action =>
|
|
|
|
action.keyTest &&
|
|
|
|
action.keyTest(event, this.getAppState(), this.getElements()),
|
2020-01-12 02:22:03 +04:00
|
|
|
);
|
|
|
|
|
2020-02-02 20:04:35 +02:00
|
|
|
if (data.length === 0) {
|
|
|
|
return null;
|
|
|
|
}
|
2020-01-12 02:22:03 +04:00
|
|
|
|
|
|
|
event.preventDefault();
|
2020-02-05 22:47:10 +04:00
|
|
|
if (
|
|
|
|
data[0].commitToHistory &&
|
|
|
|
data[0].commitToHistory(this.getAppState(), this.getElements())
|
|
|
|
) {
|
|
|
|
this.resumeHistoryRecording();
|
|
|
|
}
|
|
|
|
return data[0].perform(this.getElements(), this.getAppState(), null);
|
2020-01-12 02:22:03 +04:00
|
|
|
}
|
|
|
|
|
2020-02-05 22:47:10 +04: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-01-12 02:22:03 +04:00
|
|
|
.filter(action => "contextItemLabel" in action)
|
|
|
|
.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
|
|
|
)
|
|
|
|
.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
|
|
|
if (
|
|
|
|
action.commitToHistory &&
|
|
|
|
action.commitToHistory(this.getAppState(), this.getElements())
|
|
|
|
) {
|
|
|
|
this.resumeHistoryRecording();
|
|
|
|
}
|
|
|
|
this.updater(
|
|
|
|
action.perform(this.getElements(), this.getAppState(), null),
|
|
|
|
);
|
2020-01-24 12:04:54 +02:00
|
|
|
},
|
2020-01-12 02:22:03 +04:00
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
2020-02-05 22:47:10 +04:00
|
|
|
renderAction(name: 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!;
|
|
|
|
const updateData = (formState: any) => {
|
2020-02-05 22:47:10 +04:00
|
|
|
if (
|
|
|
|
action.commitToHistory &&
|
|
|
|
action.commitToHistory(this.getAppState(), this.getElements()) ===
|
|
|
|
true
|
|
|
|
) {
|
|
|
|
this.resumeHistoryRecording();
|
|
|
|
}
|
|
|
|
this.updater(
|
|
|
|
action.perform(this.getElements(), this.getAppState(), formState),
|
|
|
|
);
|
2020-01-12 02:22:03 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<PanelComponent
|
2020-02-05 22:47:10 +04:00
|
|
|
elements={this.getElements()}
|
|
|
|
appState={this.getAppState()}
|
2020-01-12 02:22:03 +04:00
|
|
|
updateData={updateData}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|