Skip to content

Commit

Permalink
Merge pull request #191 from shafin-deriv/shafin/BOT-2608/fix-context…
Browse files Browse the repository at this point in the history
…-menu-issue

fix: context menu issues
  • Loading branch information
rupato-deriv authored Dec 11, 2024
2 parents b002f99 + bb05e22 commit 97cb288
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 58 deletions.
6 changes: 2 additions & 4 deletions src/external/bot-skeleton/scratch/dbot.js
Original file line number Diff line number Diff line change
Expand Up @@ -402,22 +402,20 @@ class DBot {
* Disable blocks outside of any main or independent blocks.
*/
disableStrayBlocks() {
const isMainBlock = block_type => config().mainBlocks.indexOf(block_type) >= 0;
const top_blocks = this.workspace.getTopBlocks();

top_blocks.forEach(block => {
if (!isMainBlock() && !block.isIndependentBlock()) {
if (!block.isMainBlock() && !block.isIndependentBlock()) {
this.disableBlocksRecursively(block);
}
});

return true;
}

/**
* Disable blocks and their optional children.
*/
disableBlocksRecursively(block) {
block.setDisabled(true);
if (block.nextConnection?.targetConnection) {
this.disableBlocksRecursively(block.nextConnection.targetConnection.sourceBlock_);
}
Expand Down
30 changes: 15 additions & 15 deletions src/external/bot-skeleton/scratch/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export const validateErrorOnBlockDelete = () => {
blockX >= translate_X - translate_offset &&
blockX <= translate_X + translate_offset
) {
globalObserver.emit('ui.log.error', error_message_map[window.Blockly?.getSelected()?.type]?.default);
globalObserver.emit('ui.log.error', error_message_map?.()?.[window.Blockly?.getSelected()?.type]?.default);
}
}
};
Expand Down Expand Up @@ -412,17 +412,18 @@ const getDisabledBlocks = required_blocks_check => {

const throwNewErrorMessage = (error_blocks, key) => {
return error_blocks.forEach(block => {
if (key === 'misplaced' && block) globalObserver.emit('ui.log.error', error_message_map[block?.type]?.[key]);
else if (key === 'missing' && block) globalObserver.emit('ui.log.error', error_message_map[block]?.[key]);
if (key === 'misplaced' && block)
globalObserver.emit('ui.log.error', error_message_map?.()?.[block?.type]?.[key]);
else if (key === 'missing' && block) globalObserver.emit('ui.log.error', error_message_map?.()?.[block]?.[key]);
else if (key === 'disabled' && block) {
let parent_block_error = false;
const parent_error_message = error_message_map[block.type]?.[key];
const parent_error_message = error_message_map?.()?.[block.type]?.[key];
if (block.disabled && parent_error_message) {
globalObserver.emit('ui.log.error', parent_error_message);
parent_block_error = true;
} else if (!parent_block_error && block.childBlocks_) {
block.childBlocks_.forEach(childBlock => {
const child_error_message = error_message_map[childBlock.type]?.[key];
const child_error_message = error_message_map?.()?.[childBlock.type]?.[key];
if (child_error_message) globalObserver.emit('ui.log.error', child_error_message);
});
}
Expand Down Expand Up @@ -624,11 +625,11 @@ const downloadBlock = () => {
saveAs({ data: xml_text, type: 'text/xml;charset=utf-8', filename: 'block.xml' });
};

const download_option = {
const download_option = () => ({
text: localize('Download Block'),
enabled: true,
callback: downloadBlock,
};
});

export const excludeOptionFromContextMenu = (menu, exclude_items) => {
for (let i = 0; i <= menu.length - 1; i++) {
Expand All @@ -641,9 +642,7 @@ export const excludeOptionFromContextMenu = (menu, exclude_items) => {
}
};

const common_included_items = [download_option];

const all_context_menu_options = [
const all_context_menu_options = () => [
localize('Duplicate'),
localize('Add Comment'),
localize('Remove Comment'),
Expand All @@ -654,10 +653,11 @@ const all_context_menu_options = [
localize('Download Block'),
];

const deleteBlocksLocaleText = localize('Delete Block');
const deleteAllBlocksLocaleText = localize('Delete All Blocks');
const deleteBlocksLocaleText = () => localize('Delete Block');
const deleteAllBlocksLocaleText = () => localize('Delete All Blocks');

export const modifyContextMenu = (menu, add_new_items = []) => {
const common_included_items = [download_option()];
const include_items = [...common_included_items, ...add_new_items];
include_items.forEach(item => {
menu.push({
Expand All @@ -671,13 +671,13 @@ export const modifyContextMenu = (menu, add_new_items = []) => {
const menu_text = menu[i]?.text?.toLowerCase();
if (menu_text?.includes('delete')) {
if (menu_text.includes('block') && !menu_text.includes('blocks')) {
menu[i].text = deleteBlocksLocaleText;
menu[i].text = deleteBlocksLocaleText();
} else {
menu[i].text = deleteAllBlocksLocaleText;
menu[i].text = deleteAllBlocksLocaleText();
}
} else {
const localized_text = localize(menu[i].text);
if (all_context_menu_options.includes(localized_text)) {
if (all_context_menu_options().includes(localized_text)) {
menu[i].text = localized_text;
}
}
Expand Down
53 changes: 16 additions & 37 deletions src/external/bot-skeleton/utils/error-config.js
Original file line number Diff line number Diff line change
@@ -1,45 +1,24 @@
import { Localize } from '@deriv-com/translations';
import { localize } from '@deriv-com/translations';

const generateErrorMessage = (block_type, missing_space = 'workspace') => {
return {
missing: (
<Localize
i18n_default_text={`The {{block_type}} block is missing.`}
values={{
block_type,
}}
/>
),
misplaced: (
<Localize
i18n_default_text={`The {{block_type}} block is misplaced from {{missing_space}}.`}
values={{
block_type,
missing_space,
}}
/>
),

disabled: (
<Localize
i18n_default_text={`The {{block_type}} block is mandatory and cannot be deleted/disabled.`}
values={{
block_type,
}}
/>
),
default: (
<Localize
i18n_default_text={`The {{block_type}} block is mandatory and cannot be deleted/disabled.`}
values={{
block_type,
}}
/>
),
missing: localize('The {{block_type}} block is mandatory and cannot be deleted/disabled.', {
block_type,
}),
misplaced: localize('The {{block_type}} block is misplaced from {{missing_space}}.', {
block_type,
missing_space,
}),
disabled: localize('The {{block_type}} block is mandatory and cannot be deleted/disabled.', {
block_type,
}),
default: localize('The {{block_type}} block is mandatory and cannot be deleted/disabled.', {
block_type,
}),
};
};

export const error_message_map = {
export const error_message_map = () => ({
trade_definition: generateErrorMessage('Trade parameters'),
trade_parameters: generateErrorMessage('Trade parameters'),
before_purchase: generateErrorMessage('Purchase conditions'),
Expand All @@ -48,4 +27,4 @@ export const error_message_map = {
trade_definition_tradeoptions: generateErrorMessage('Trade options', 'trade parameters'),
trade_definition_multiplier: generateErrorMessage('Trade options multipliers', 'trade parameters'),
trade_definition_accumulator: generateErrorMessage('Trade options accumulators', 'trade parameters'),
};
});
4 changes: 2 additions & 2 deletions src/external/bot-skeleton/utils/error-handling.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ export const initErrorHandlingListener = (type = 'keydown') => {
export const handleError = (errorCode, observer) => {
switch (errorCode) {
case 'BLOCK_DELETION':
if (error_message_map[window.Blockly.getSelected().type]) {
observer.emit('ui.log.error', error_message_map[window.Blockly.getSelected().type]?.default);
if (error_message_map?.()?.[window.Blockly.getSelected().type]) {
observer.emit('ui.log.error', error_message_map?.()?.[window.Blockly.getSelected().type]?.default);
}
break;
default:
Expand Down

0 comments on commit 97cb288

Please sign in to comment.