Skip to content

Commit a554185

Browse files
committed
Changed toolbox update event to include the workspace id in the event details.
1 parent fdf3ba2 commit a554185

File tree

3 files changed

+19
-8
lines changed

3 files changed

+19
-8
lines changed

src/App.tsx

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -391,11 +391,13 @@ const AppContent: React.FC<AppContentProps> = ({ project, setProject }): React.J
391391
};
392392

393393
/** Handles toolbox update requests from blocks */
394-
const handleToolboxUpdateRequest = React.useCallback(() => {
395-
if (blocksEditor.current && currentModule) {
396-
blocksEditor.current.updateToolbox(shownPythonToolboxCategories);
394+
const handleToolboxUpdateRequest = React.useCallback((e: Event) => {
395+
const workspaceId = (e as CustomEvent).detail.workspaceId;
396+
const correspondingEditor = editor.Editor.getEditorForBlocklyWorkspaceId(workspaceId);
397+
if (correspondingEditor) {
398+
correspondingEditor.updateToolbox(shownPythonToolboxCategories);
397399
}
398-
}, [currentModule, shownPythonToolboxCategories, i18n.language]);
400+
}, [shownPythonToolboxCategories, i18n.language]);
399401

400402
// Add event listener for toolbox updates
401403
React.useEffect(() => {

src/blocks/mrc_mechanism_component_holder.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -142,13 +142,13 @@ const MECHANISM_COMPONENT_HOLDER = {
142142
let blockMoveEvent = blockEvent as Blockly.Events.BlockMove;
143143
if (blockMoveEvent.reason?.includes('connect')) {
144144
setName(block);
145-
updateToolboxAfterDelay();
145+
updateToolboxAfterDelay(block);
146146
}
147147
}
148148
else {
149149
if (blockEvent.type == Blockly.Events.BLOCK_CHANGE) {
150150
setName(block);
151-
updateToolboxAfterDelay();
151+
updateToolboxAfterDelay(block);
152152
}
153153
}
154154
},
@@ -243,13 +243,17 @@ const MECHANISM_COMPONENT_HOLDER = {
243243
}
244244

245245
let toolboxUpdateTimeout: NodeJS.Timeout | null = null;
246-
export function updateToolboxAfterDelay(){
246+
247+
function updateToolboxAfterDelay(block: Blockly.BlockSvg) {
247248
if (toolboxUpdateTimeout) {
248249
clearTimeout(toolboxUpdateTimeout);
249250
}
250251
toolboxUpdateTimeout = setTimeout(() => {
251252
const event = new CustomEvent(TOOLBOX_UPDATE_EVENT, {
252-
detail: { timestamp: Date.now() }
253+
detail: {
254+
timestamp: Date.now(),
255+
workspaceId: block.workspace.id,
256+
}
253257
});
254258
window.dispatchEvent(event);
255259
toolboxUpdateTimeout = null;

src/editor/editor.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,11 @@ export class Editor {
495495
return null;
496496
}
497497

498+
public static getEditorForBlocklyWorkspaceId(workspaceId: string): Editor | null {
499+
const workspace = Blockly.Workspace.getById(workspaceId);
500+
return workspace ? Editor.getEditorForBlocklyWorkspace(workspace) : null;
501+
}
502+
498503
public static getCurrentEditor(): Editor | null {
499504
return Editor.currentEditor;
500505
}

0 commit comments

Comments
 (0)