Make filtering of custom actions optional.

This commit is contained in:
Daniel J. Geiger 2023-01-08 19:30:01 -06:00
parent 9e5948ac28
commit ddc393bd9d
2 changed files with 20 additions and 14 deletions

View File

@ -117,10 +117,16 @@ export class ActionManager {
if (this === undefined) { if (this === undefined) {
return []; return [];
} }
const filter =
opts !== undefined &&
("elements" in opts || "data" in opts || "guardsOnly" in opts);
const customActions: Action[] = []; const customActions: Action[] = [];
for (const key in this.actions) { for (const key in this.actions) {
const action = this.actions[key]; const action = this.actions[key];
if (!isActionName(action.name) && this.isActionEnabled(action, opts)) { if (
!isActionName(action.name) &&
(!filter || this.isActionEnabled(action, opts))
) {
customActions.push(action); customActions.push(action);
} }
} }

View File

@ -6241,29 +6241,29 @@ class App extends React.Component<AppProps, AppState> {
type: "canvas" | "element" | "custom", type: "canvas" | "element" | "custom",
source?: string, source?: string,
): ContextMenuItems => { ): ContextMenuItems => {
const options = this._getContextMenuItems(type, source); const custom: ContextMenuItems = [];
return options.filter( 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);
}
const standard: ContextMenuItems = this._getContextMenuItems(type).filter(
(item) => (item) =>
!item || !item ||
item === CONTEXT_MENU_SEPARATOR || item === CONTEXT_MENU_SEPARATOR ||
this.actionManager.isActionEnabled(item, { guardsOnly: true }), this.actionManager.isActionEnabled(item, { guardsOnly: true }),
); );
return [...custom, ...standard];
}; };
private _getContextMenuItems = ( private _getContextMenuItems = (
type: "canvas" | "element" | "custom", type: "canvas" | "element",
source?: string,
): ContextMenuItems => { ): ContextMenuItems => {
const options: ContextMenuItems = []; const options: ContextMenuItems = [];
this.actionManager
.getCustomActions({ data: { source: source ?? "" } })
.forEach((action) => options.push(action));
if (type === "custom") {
return options;
}
if (options.length > 0) {
options.push(CONTEXT_MENU_SEPARATOR);
}
options.push(actionCopyAsPng, actionCopyAsSvg); options.push(actionCopyAsPng, actionCopyAsSvg);