From 60720dbb72b856c9ec57e8f424ccd1c011b6266b Mon Sep 17 00:00:00 2001 From: rupato-deriv Date: Mon, 29 Jul 2024 11:36:44 +0800 Subject: [PATCH 01/23] fix: refactored bot functions --- .../src/scratch/hooks/workspace.js | 85 ++++ .../bot-skeleton/src/scratch/utils/index.js | 6 + .../components/load-modal/local-footer.tsx | 6 +- .../load-modal/recent-workspace.tsx | 6 +- .../bot-web-ui/src/constants/load-modal.ts | 6 - .../bot-web-ui/src/stores/dashboard-store.ts | 5 +- .../bot-web-ui/src/stores/load-modal-store.ts | 478 +++++++++--------- 7 files changed, 353 insertions(+), 239 deletions(-) diff --git a/packages/bot-skeleton/src/scratch/hooks/workspace.js b/packages/bot-skeleton/src/scratch/hooks/workspace.js index 9ce860d3a2e1..b9712146ab32 100644 --- a/packages/bot-skeleton/src/scratch/hooks/workspace.js +++ b/packages/bot-skeleton/src/scratch/hooks/workspace.js @@ -136,3 +136,88 @@ Blockly.createVirtualWorkspace_ = function (fragment, options, blockDragSurface, return mainWorkspace; }; + +// Blockly.Xml.domToWorkspace = function (xml, workspace) { +// if (xml instanceof Blockly.Workspace) { +// var swap = xml; +// xml = workspace; +// workspace = swap; +// console.warn('Deprecated call to Blockly.Xml.domToWorkspace, ' + +// 'swap the arguments.'); +// } +// var width; // Not used in LTR. +// if (workspace.RTL) { +// width = workspace.getWidth(); +// } +// var newBlockIds = []; // A list of block IDs added by this call. +// //Blockly.Field.startCache(); +// // Safari 7.1.3 is known to provide node lists with extra references to +// // children beyond the lists' length. Trust the length, do not use the +// // looping pattern of checking the index for an object. +// var childCount = xml.childNodes.length; +// var existingGroup = Blockly.Events.getGroup(); +// if (!existingGroup) { +// Blockly.Events.setGroup(true); +// } + +// // Disable workspace resizes as an optimization. +// if (workspace.setResizesEnabled) { +// workspace.setResizesEnabled(false); +// } +// var variablesFirst = true; +// try { +// for (var i = 0; i < childCount; i++) { +// var xmlChild = xml.childNodes[i]; +// var name = xmlChild.nodeName.toLowerCase(); +// if (name == 'block' || +// (name == 'shadow' && !Blockly.Events.recordUndo)) { +// // Allow top-level shadow blocks if recordUndo is disabled since +// // that means an undo is in progress. Such a block is expected +// // to be moved to a nested destination in the next operation. +// var block = Blockly.Xml.domToBlock(xmlChild, workspace); +// newBlockIds.push(block.id); +// var blockX = xmlChild.hasAttribute('x') ? +// parseInt(xmlChild.getAttribute('x'), 10) : 10; +// var blockY = xmlChild.hasAttribute('y') ? +// parseInt(xmlChild.getAttribute('y'), 10) : 10; +// if (!isNaN(blockX) && !isNaN(blockY)) { +// block.moveBy(workspace.RTL ? width - blockX : blockX, blockY); +// if (block.comment && typeof block.comment === 'object') { +// var commentXY = block.comment.getXY(); +// var commentWidth = block.comment.getBubbleSize().width; +// block.comment.moveTo(block.workspace.RTL ? width - commentXY.x - commentWidth : commentXY.x, commentXY.y); +// } +// } +// variablesFirst = false; +// } else if (name == 'shadow') { +// goog.asserts.fail('Shadow block cannot be a top-level block.'); +// variablesFirst = false; +// } else if (name == 'comment') { +// if (workspace.rendered) { +// Blockly.WorkspaceCommentSvg.fromXml(xmlChild, workspace, width); +// } else { +// Blockly.WorkspaceComment.fromXml(xmlChild, workspace); +// } +// } else if (name == 'variables') { +// if (variablesFirst) { +// Blockly.Xml.domToVariables(xmlChild, workspace); +// } else { +// throw Error('\'variables\' tag must exist once before block and ' + +// 'shadow tag elements in the workspace XML, but it was found in ' + +// 'another location.'); +// } +// variablesFirst = false; +// } +// } +// } finally { +// if (!existingGroup) { +// Blockly.Events.setGroup(false); +// } +// // Blockly.Field.stopCache(); +// } +// // Re-enable workspace resizing. +// if (workspace.setResizesEnabled) { +// //workspace.setResizesEnabled(true); +// } +// return newBlockIds; +// }; diff --git a/packages/bot-skeleton/src/scratch/utils/index.js b/packages/bot-skeleton/src/scratch/utils/index.js index 01e2e7710064..12634c77d27e 100644 --- a/packages/bot-skeleton/src/scratch/utils/index.js +++ b/packages/bot-skeleton/src/scratch/utils/index.js @@ -179,6 +179,12 @@ export const load = async ({ workspace, Array.from(blockly_xml).map(xml_block => xml_block.getAttribute('type')) ); + window.Blockly.readFileXml = { + file_name, + strategy_id, + from, + xml: window.Blockly.Xml.domToText(xml), + }; if (is_collection) { loadBlocks(xml, drop_event, event_group, workspace); diff --git a/packages/bot-web-ui/src/components/load-modal/local-footer.tsx b/packages/bot-web-ui/src/components/load-modal/local-footer.tsx index f022fd2ddf6e..b596ad181690 100644 --- a/packages/bot-web-ui/src/components/load-modal/local-footer.tsx +++ b/packages/bot-web-ui/src/components/load-modal/local-footer.tsx @@ -8,7 +8,8 @@ import { useDBotStore } from 'Stores/useDBotStore'; const LocalFooter = observer(() => { const { ui } = useStore(); const { load_modal, dashboard } = useDBotStore(); - const { is_open_button_loading, loadFileFromLocal, setLoadedLocalFile, toggleLoadModal } = load_modal; + const { is_open_button_loading, loadStrategyOnBotBuilder, loadFileFromLocal, setLoadedLocalFile, toggleLoadModal } = + load_modal; const { setOpenSettings, setPreviewOnPopup } = dashboard; const { is_desktop } = ui; @@ -22,7 +23,8 @@ const LocalFooter = observer(() => {