fix(toolbox): Preserve user-specified tool state and streamline command execution (#4063)

This commit is contained in:
Alireza authored and GitHub committed 2024-04-29 11:58:03 -04:00
1 parent 4784638717
commit f1a736d193
34 files changed
+332 -181

No files matched your search

+7 -2
View File
@@ -204,7 +204,8 @@ export class CommandsManager {
// Execute each command in the array
let result: unknown;
commands.forEach(({ commandName, commandOptions, context }) => {
commands.forEach(command => {
const { commandName, commandOptions, context } = command;
if (commandName) {
result = this.runCommand(
commandName,
@@ -215,7 +216,11 @@ export class CommandsManager {
context
);
} else {
console.warn('No command name supplied in', toRun);
if (typeof command === 'function') {
result = command();
} else {
console.warn('No command name supplied in', toRun);
}
}
});
@@ -156,7 +156,7 @@ export default class ToolbarService extends PubSubService {
const itemId = interaction.itemId ?? interaction.id;
interaction.itemId = itemId;
const commands = Array.isArray(interaction.commands)
let commands = Array.isArray(interaction.commands)
? interaction.commands
: [interaction.commands];
@@ -170,6 +170,27 @@ export default class ToolbarService extends PubSubService {
const commandOptions = { ...options, ...interaction };
commands = commands.map(command => {
if (typeof command === 'function') {
return () => {
command({
...commandOptions,
commandsManager: this._commandsManager,
servicesManager: this._servicesManager,
});
};
}
return command;
});
// if still no commands, return
commands = commands.filter(Boolean);
if (!commands.length) {
return;
}
// Loop through commands and run them with the combined options
this._commandsManager.run(commands, commandOptions);
@@ -377,6 +398,26 @@ export default class ToolbarService extends PubSubService {
);
}
/**
* Retrieves the tool name for a given button.
* @param button - The button object.
* @returns The tool name associated with the button.
*/
getToolNameForButton(button) {
const { props } = button;
const commands = props?.commands || button.commands;
const commandsArray = Array.isArray(commands) ? commands : [commands];
const firstCommand = commandsArray[0];
if (firstCommand?.commandOptions) {
return firstCommand.commandOptions.toolName ?? props?.id ?? button.id;
}
// use id as a fallback for toolName
return props?.id ?? button.id;
}
/**
*
* @param {*} btn