feat(actions): simplify action corner api through the toolbarService (#5033)
This commit is contained in:
1 parent
f71b88e639
commit
7c15bb8901
246 files changed
+7930
-4270
No files matched your search
@@ -2,13 +2,68 @@ import { CommandsManager } from '../../classes';
|
||||
import { ExtensionManager } from '../../extensions';
|
||||
import { PubSubService } from '../_shared/pubSubServiceInterface';
|
||||
import type { RunCommand } from '../../types/Command';
|
||||
import { Button, ButtonProps, EvaluateFunction, EvaluatePublic, NestedButtonProps } from './types';
|
||||
import { Button, ButtonProps, EvaluateFunction, EvaluatePublic } from './types';
|
||||
|
||||
const EVENTS = {
|
||||
TOOL_BAR_MODIFIED: 'event::toolBarService:toolBarModified',
|
||||
TOOL_BAR_STATE_MODIFIED: 'event::toolBarService:toolBarStateModified',
|
||||
};
|
||||
|
||||
/**
|
||||
* Predefined toolbar sections used throughout the application
|
||||
*/
|
||||
export const TOOLBAR_SECTIONS = {
|
||||
/**
|
||||
* Main toolbar
|
||||
*/
|
||||
primary: 'primary',
|
||||
|
||||
/**
|
||||
* Secondary toolbar
|
||||
*/
|
||||
secondary: 'secondary',
|
||||
|
||||
/**
|
||||
* Viewport action menu sections
|
||||
*/
|
||||
viewportActionMenu: {
|
||||
topLeft: 'viewportActionMenu.topLeft',
|
||||
topRight: 'viewportActionMenu.topRight',
|
||||
bottomLeft: 'viewportActionMenu.bottomLeft',
|
||||
bottomRight: 'viewportActionMenu.bottomRight',
|
||||
topMiddle: 'viewportActionMenu.topMiddle',
|
||||
bottomMiddle: 'viewportActionMenu.bottomMiddle',
|
||||
leftMiddle: 'viewportActionMenu.leftMiddle',
|
||||
rightMiddle: 'viewportActionMenu.rightMiddle',
|
||||
},
|
||||
|
||||
/**
|
||||
* Measurement tools section
|
||||
*/
|
||||
measurementSection: 'measurementSection',
|
||||
|
||||
/**
|
||||
* More tools section
|
||||
*/
|
||||
moreToolsSection: 'moreToolsSection',
|
||||
|
||||
/**
|
||||
* Advanced rendering controls section
|
||||
*/
|
||||
advancedRenderingControlsSection: 'advancedRenderingControlsSection',
|
||||
};
|
||||
|
||||
export enum ButtonLocation {
|
||||
TopLeft = 0,
|
||||
TopMiddle = 1,
|
||||
TopRight = 2,
|
||||
LeftMiddle = 3,
|
||||
RightMiddle = 4,
|
||||
BottomLeft = 5,
|
||||
BottomMiddle = 6,
|
||||
BottomRight = 7,
|
||||
}
|
||||
|
||||
export default class ToolbarService extends PubSubService {
|
||||
public static REGISTRATION = {
|
||||
name: 'toolbarService',
|
||||
@@ -18,6 +73,13 @@ export default class ToolbarService extends PubSubService {
|
||||
},
|
||||
};
|
||||
|
||||
/**
|
||||
* Access to predefined toolbar sections for autocomplete support
|
||||
*/
|
||||
public get sections() {
|
||||
return TOOLBAR_SECTIONS;
|
||||
}
|
||||
|
||||
public static createButton(options: {
|
||||
id: string;
|
||||
label: string;
|
||||
@@ -130,7 +192,7 @@ export default class ToolbarService extends PubSubService {
|
||||
* @param buttons - The buttons to be added.
|
||||
* @param replace - Flag indicating if any existing button with the same id as one being added should be replaced
|
||||
*/
|
||||
public addButtons(buttons: Button[], replace: boolean = false): void {
|
||||
public register(buttons: Button[], replace: boolean = false): void {
|
||||
buttons.forEach(button => {
|
||||
if (replace || !this.state.buttons[button.id]) {
|
||||
if (!button.props) {
|
||||
@@ -231,7 +293,8 @@ export default class ToolbarService extends PubSubService {
|
||||
* which buttons to evaluate based on the props
|
||||
*/
|
||||
public refreshToolbarState(refreshProps) {
|
||||
const buttons = this.state.buttons;
|
||||
const originalButtons = this.state.buttons;
|
||||
const updatedButtons = { ...originalButtons };
|
||||
const evaluationResults = new Map();
|
||||
|
||||
const evaluateButtonProps = (button, props, refreshProps) => {
|
||||
@@ -239,6 +302,7 @@ export default class ToolbarService extends PubSubService {
|
||||
const { disabled, disabledText, className, isActive } = evaluationResults.get(button.id);
|
||||
return { ...props, disabled, disabledText, className, isActive };
|
||||
} else {
|
||||
const evaluateProps = props.evaluateProps;
|
||||
const evaluated =
|
||||
typeof props.evaluate === 'function'
|
||||
? props.evaluate({ ...refreshProps, button })
|
||||
@@ -247,6 +311,7 @@ export default class ToolbarService extends PubSubService {
|
||||
...props,
|
||||
...evaluated,
|
||||
disabled: evaluated?.disabled || false,
|
||||
visible: evaluateProps?.hideWhenDisabled && evaluated?.disabled ? false : true,
|
||||
className: evaluated?.className || '',
|
||||
isActive: evaluated?.isActive, // isActive will be undefined for buttons without this prop
|
||||
};
|
||||
@@ -255,7 +320,14 @@ export default class ToolbarService extends PubSubService {
|
||||
}
|
||||
};
|
||||
|
||||
Object.values(buttons).forEach(button => {
|
||||
const updatedIds = new Set();
|
||||
Object.values(originalButtons).forEach(button => {
|
||||
// Note: do not re-evaluate buttons that have already been evaluated
|
||||
// this will result in inconsistencies in the toolbar state
|
||||
if (updatedIds.has(button.id)) {
|
||||
return;
|
||||
}
|
||||
|
||||
const hasSection = (button.props as NestedButtonProps)?.buttonSection;
|
||||
|
||||
if (!hasSection) {
|
||||
@@ -263,10 +335,12 @@ export default class ToolbarService extends PubSubService {
|
||||
const buttonProps = button.props as ButtonProps;
|
||||
|
||||
const updatedProps = evaluateButtonProps(button, buttonProps, refreshProps);
|
||||
buttons[button.id] = {
|
||||
updatedButtons[button.id] = {
|
||||
...button,
|
||||
props: updatedProps,
|
||||
};
|
||||
|
||||
updatedIds.add(button.id);
|
||||
} else {
|
||||
let buttonProps = button.props as NestedButtonProps;
|
||||
const { evaluate: groupEvaluate } = buttonProps;
|
||||
@@ -288,17 +362,27 @@ export default class ToolbarService extends PubSubService {
|
||||
}
|
||||
|
||||
toolButtonIds.forEach(buttonId => {
|
||||
const button = buttons[buttonId];
|
||||
const button = originalButtons[buttonId];
|
||||
if (!button) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (updatedIds.has(buttonId)) {
|
||||
return;
|
||||
}
|
||||
|
||||
const updatedProps = evaluateButtonProps(button, button.props, refreshProps);
|
||||
buttons[buttonId] = {
|
||||
updatedButtons[buttonId] = {
|
||||
...button,
|
||||
props: updatedProps,
|
||||
};
|
||||
|
||||
updatedIds.add(buttonId);
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
this.setButtons(buttons);
|
||||
this.setButtons(updatedButtons);
|
||||
return this.state;
|
||||
}
|
||||
|
||||
@@ -373,7 +457,7 @@ export default class ToolbarService extends PubSubService {
|
||||
* @param {string} key - The key of the button section.
|
||||
* @param {Array} buttons - The buttons to be added to the section.
|
||||
*/
|
||||
createButtonSection(key, buttons) {
|
||||
updateSection(key, buttons) {
|
||||
if (this.state.buttonSections[key]) {
|
||||
this.state.buttonSections[key].push(
|
||||
...buttons.filter(
|
||||
@@ -443,7 +527,7 @@ export default class ToolbarService extends PubSubService {
|
||||
}
|
||||
|
||||
const { id, uiType } = btn;
|
||||
const { groupId } = btn.props as NestedButtonProps;
|
||||
const { buttonSection } = btn.props;
|
||||
|
||||
const buttonTypes = this._getButtonUITypes();
|
||||
|
||||
@@ -457,7 +541,7 @@ export default class ToolbarService extends PubSubService {
|
||||
return;
|
||||
}
|
||||
|
||||
!groupId ? this.handleEvaluate(btn.props) : this.handleEvaluateNested(btn.props);
|
||||
!buttonSection ? this.handleEvaluate(btn.props) : this.handleEvaluateNested(btn.props);
|
||||
|
||||
const { id: buttonId, props: componentProps } = btn;
|
||||
|
||||
@@ -587,6 +671,7 @@ export default class ToolbarService extends PubSubService {
|
||||
return evaluateFunction;
|
||||
});
|
||||
|
||||
const evaluateProps = props.evaluate;
|
||||
props.evaluate = args => {
|
||||
const results = evaluators.map(evaluator => evaluator(args)).filter(Boolean);
|
||||
|
||||
@@ -607,6 +692,8 @@ export default class ToolbarService extends PubSubService {
|
||||
return mergedResult;
|
||||
};
|
||||
|
||||
props.evaluateProps = evaluateProps;
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -627,7 +714,9 @@ export default class ToolbarService extends PubSubService {
|
||||
const { name, ...options } = evaluate;
|
||||
const evaluateFunction = this._evaluateFunction[name];
|
||||
if (evaluateFunction) {
|
||||
const evaluateProps = props.evaluate;
|
||||
props.evaluate = args => evaluateFunction({ ...args, ...options });
|
||||
props.evaluateProps = evaluateProps;
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -645,4 +734,56 @@ export default class ToolbarService extends PubSubService {
|
||||
this.state.buttonSections[buttonSection] = [];
|
||||
this._broadcastEvent(this.EVENTS.TOOL_BAR_MODIFIED, { ...this.state });
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if a button exists in any toolbar section.
|
||||
*
|
||||
* @param buttonId - The button ID to check for
|
||||
* @returns True if the button exists in any section, false otherwise
|
||||
*/
|
||||
isInAnySection(buttonId: string): boolean {
|
||||
if (!buttonId) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Check all sections to see if the button ID exists in any of them
|
||||
return Object.values(this.state.buttonSections).some(
|
||||
section => Array.isArray(section) && section.includes(buttonId)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the alignment and side for a specific viewport corner location.
|
||||
* Used for menu positioning based on the corner location.
|
||||
*
|
||||
* @param location - The viewport corner location
|
||||
* @returns An object with align and side properties
|
||||
*/
|
||||
public getAlignAndSide(location: ButtonLocation | string): {
|
||||
align: 'start' | 'end' | 'center';
|
||||
side: 'top' | 'bottom' | 'left' | 'right';
|
||||
} {
|
||||
const locationNumber = Number(location);
|
||||
switch (locationNumber) {
|
||||
case ButtonLocation.TopLeft: // Enum 0, Original 0 (topLeft)
|
||||
return { align: 'start', side: 'bottom' };
|
||||
case ButtonLocation.TopMiddle: // Enum 1, Original 4 (topMiddle)
|
||||
return { align: 'center', side: 'bottom' };
|
||||
case ButtonLocation.TopRight: // Enum 2, Original 1 (topRight)
|
||||
return { align: 'end', side: 'bottom' };
|
||||
case ButtonLocation.LeftMiddle: // Enum 3, Original 6 (leftMiddle)
|
||||
return { align: 'start', side: 'right' };
|
||||
case ButtonLocation.RightMiddle: // Enum 4, Original 7 (rightMiddle)
|
||||
return { align: 'end', side: 'left' };
|
||||
case ButtonLocation.BottomLeft: // Enum 5, Original 2 (bottomLeft)
|
||||
return { align: 'start', side: 'top' };
|
||||
case ButtonLocation.BottomMiddle: // Enum 6, Original 5 (bottomMiddle)
|
||||
return { align: 'center', side: 'top' };
|
||||
case ButtonLocation.BottomRight: // Enum 7, Original 3 (bottomRight)
|
||||
return { align: 'end', side: 'top' };
|
||||
default:
|
||||
// Default to TopLeft behavior if an unexpected value is passed.
|
||||
return { align: 'start', side: 'bottom' };
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,3 +1,3 @@
|
||||
import ToolbarService from './ToolbarService';
|
||||
|
||||
import ToolbarService, { TOOLBAR_SECTIONS } from './ToolbarService';
|
||||
export default ToolbarService;
|
||||
export { TOOLBAR_SECTIONS };
|
||||
@@ -1,6 +1,22 @@
|
||||
import type { RunCommand } from '../../types/Command';
|
||||
import React from 'react';
|
||||
|
||||
/**
|
||||
* Type definitions for toolbar sections
|
||||
*/
|
||||
export type ToolbarSections = {
|
||||
primary: string;
|
||||
secondary: string;
|
||||
viewportActionMenu: {
|
||||
topLeft: string;
|
||||
topRight: string;
|
||||
bottomLeft: string;
|
||||
bottomRight: string;
|
||||
};
|
||||
measurementSection: string;
|
||||
moreToolsSection: string;
|
||||
};
|
||||
|
||||
export type EvaluatePublic =
|
||||
| string
|
||||
| EvaluateFunction
|
||||
|
||||
@@ -137,7 +137,7 @@ class WorkflowStepsService extends PubSubService {
|
||||
|
||||
toUse.forEach(({ buttonSection, buttons }) => {
|
||||
toolbarService.clearButtonSection(buttonSection);
|
||||
toolbarService.createButtonSection(buttonSection, buttons);
|
||||
toolbarService.updateSection(buttonSection, buttons);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user