From 657087317c335a58244336e792a74dded3c25ecb Mon Sep 17 00:00:00 2001 From: shafin-deriv Date: Wed, 11 Dec 2024 18:18:41 +0800 Subject: [PATCH 1/2] fix: context menu issues --- src/external/bot-skeleton/scratch/dbot.js | 6 +-- .../bot-skeleton/scratch/utils/index.js | 30 +++++------ .../bot-skeleton/utils/error-config.js | 53 ++++++------------- .../bot-skeleton/utils/error-handling.js | 4 +- 4 files changed, 35 insertions(+), 58 deletions(-) diff --git a/src/external/bot-skeleton/scratch/dbot.js b/src/external/bot-skeleton/scratch/dbot.js index 9f3ea3ad..9c2f911d 100644 --- a/src/external/bot-skeleton/scratch/dbot.js +++ b/src/external/bot-skeleton/scratch/dbot.js @@ -402,15 +402,12 @@ 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; } @@ -418,6 +415,7 @@ class DBot { * Disable blocks and their optional children. */ disableBlocksRecursively(block) { + block.setDisabled(true); if (block.nextConnection?.targetConnection) { this.disableBlocksRecursively(block.nextConnection.targetConnection.sourceBlock_); } diff --git a/src/external/bot-skeleton/scratch/utils/index.js b/src/external/bot-skeleton/scratch/utils/index.js index 9d83f9c0..a7a194f5 100644 --- a/src/external/bot-skeleton/scratch/utils/index.js +++ b/src/external/bot-skeleton/scratch/utils/index.js @@ -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); } } }; @@ -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); }); } @@ -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++) { @@ -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'), @@ -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({ @@ -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; } } diff --git a/src/external/bot-skeleton/utils/error-config.js b/src/external/bot-skeleton/utils/error-config.js index 5e9ab581..c8a8a030 100644 --- a/src/external/bot-skeleton/utils/error-config.js +++ b/src/external/bot-skeleton/utils/error-config.js @@ -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: ( - - ), - misplaced: ( - - ), - - disabled: ( - - ), - default: ( - - ), + 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'), @@ -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'), -}; +}); diff --git a/src/external/bot-skeleton/utils/error-handling.js b/src/external/bot-skeleton/utils/error-handling.js index 4ede7f8c..e91e48d8 100644 --- a/src/external/bot-skeleton/utils/error-handling.js +++ b/src/external/bot-skeleton/utils/error-handling.js @@ -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]); } break; default: From bb05e22f601309611d756607d9634d8a377c0f8f Mon Sep 17 00:00:00 2001 From: shafin-deriv Date: Wed, 11 Dec 2024 18:33:31 +0800 Subject: [PATCH 2/2] fix: add default --- src/external/bot-skeleton/utils/error-handling.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/external/bot-skeleton/utils/error-handling.js b/src/external/bot-skeleton/utils/error-handling.js index e91e48d8..c566ca76 100644 --- a/src/external/bot-skeleton/utils/error-handling.js +++ b/src/external/bot-skeleton/utils/error-handling.js @@ -21,7 +21,7 @@ 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]); + observer.emit('ui.log.error', error_message_map?.()?.[window.Blockly.getSelected().type]?.default); } break; default: