Refactor: Drop isActionName and convert getCustomActions to

`filterActions`.
This commit is contained in:
Daniel J. Geiger 2023-01-28 21:27:25 -06:00
parent 87aba3f619
commit 14c6ea938a
8 changed files with 145 additions and 133 deletions

View file

@ -3,7 +3,7 @@ import { ActionManager } from "../actions/manager";
import { getNonDeletedElements } from "../element";
import { ExcalidrawElement, PointerType } from "../element/types";
import { t } from "../i18n";
import { useDevice, useExcalidrawActionManager } from "../components/App";
import { useDevice } from "../components/App";
import {
canChangeRoundness,
canHaveArrowheads,
@ -23,7 +23,7 @@ import {
} from "../utils";
import Stack from "./Stack";
import { ToolButton } from "./ToolButton";
import { SubtypeToggles } from "./SubtypeButton";
import { SubtypeShapeActions, SubtypeToggles } from "./Subtypes";
import { hasStrokeColor } from "../scene/comparisons";
import { trackEvent } from "../analytics";
import { hasBoundTextElement } from "../element/typeChecks";
@ -93,9 +93,7 @@ export const SelectedShapeActions = ({
{showChangeBackgroundIcons && (
<div>{renderAction("changeBackgroundColor")}</div>
)}
{useExcalidrawActionManager()
.getCustomActions({ elements: targetElements })
.map((action) => renderAction(action.name))}
<SubtypeShapeActions elements={targetElements} />
{showFillIcons && renderAction("changeFillStyle")}
{(hasStrokeWidth(appState.activeTool.type) ||

View file

@ -242,6 +242,7 @@ import {
prepareSubtype,
selectSubtype,
subtypeActionPredicate,
isSubtypeAction,
} from "../subtypes";
import {
dataURLToFile,
@ -6229,26 +6230,26 @@ class App extends React.Component<AppProps, AppState> {
};
private getContextMenuItems = (
type: "canvas" | "element" | "custom",
source?: string,
type: "canvas" | "element",
): ContextMenuItems => {
const custom: ContextMenuItems = [];
const subtype: ContextMenuItems = [];
this.actionManager
.getCustomActions({ data: { source: source ?? "" } })
.forEach((action) => custom.push(action));
if (type === "custom") {
return custom;
}
if (custom.length > 0) {
custom.push(CONTEXT_MENU_SEPARATOR);
.filterActions(isSubtypeAction)
.forEach(
(action) =>
this.actionManager.isActionEnabled(action, { data: {} }) &&
subtype.push(action),
);
if (subtype.length > 0) {
subtype.push(CONTEXT_MENU_SEPARATOR);
}
const standard: ContextMenuItems = this._getContextMenuItems(type).filter(
(item) =>
!item ||
item === CONTEXT_MENU_SEPARATOR ||
this.actionManager.isActionEnabled(item, { guardsOnly: true }),
this.actionManager.isActionEnabled(item, { noPredicates: true }),
);
return [...custom, ...standard];
return [...subtype, ...standard];
};
private _getContextMenuItems = (

View file

@ -7,6 +7,7 @@ import {
Subtype,
getSubtypeNames,
hasAlwaysEnabledActions,
isSubtypeAction,
isValidSubtype,
subtypeCollides,
} from "../subtypes";
@ -30,7 +31,7 @@ export const SubtypeButton = (
const subtypeAction: Action = {
name: subtype,
trackEvent: false,
predicate: (...rest) => rest[4]?.source === subtype,
predicate: (...rest) => rest[4]?.subtype === subtype,
perform: (elements, appState) => {
const inactive = !appState.activeSubtypes?.includes(subtype) ?? true;
const activeSubtypes: Subtype[] = [];
@ -121,7 +122,7 @@ export const SubtypeToggles = () => {
const onContextMenu = (
event: React.MouseEvent<HTMLButtonElement>,
source: string,
subtype: string,
) => {
event.preventDefault();
@ -131,8 +132,9 @@ export const SubtypeToggles = () => {
const top = event.clientY - offsetTop;
const items: ContextMenuItems = [];
am.getCustomActions({ data: { source } }).forEach((action) =>
items.push(action),
am.filterActions(isSubtypeAction).forEach(
(action) =>
am.isActionEnabled(action, { data: { subtype } }) && items.push(action),
);
setAppState({}, () => {
setAppState({
@ -154,3 +156,18 @@ export const SubtypeToggles = () => {
};
SubtypeToggles.displayName = "SubtypeToggles";
export const SubtypeShapeActions = (props: {
elements: readonly ExcalidrawElement[];
}) => {
const am = useExcalidrawActionManager();
return (
<>
{am
.filterActions(isSubtypeAction, { elements: props.elements })
.map((action) => am.renderAction(action.name))}
</>
);
};
SubtypeShapeActions.displayName = "SubtypeShapeActions";