Skip to content

Commit a7cf997

Browse files
committed
Make toolbox update when mechanism_component holder changes
1 parent 38f6fa6 commit a7cf997

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

src/App.tsx

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ import * as CustomBlocks from './blocks/setup_custom_blocks';
4242
import { initialize as initializePythonBlocks } from './blocks/utils/python';
4343
import * as ChangeFramework from './blocks/utils/change_framework'
4444
import { mutatorOpenListener } from './blocks/mrc_param_container'
45+
import { TOOLBOX_UPDATE_EVENT } from './blocks/mrc_mechanism_component_holder';
4546

4647
/** Storage key for shown toolbox categories. */
4748
const SHOWN_TOOLBOX_CATEGORIES_KEY = 'shownPythonToolboxCategories';
@@ -253,6 +254,22 @@ const App: React.FC = (): React.JSX.Element => {
253254
return tabs;
254255
};
255256

257+
/** Handles toolbox update requests from blocks */
258+
const handleToolboxUpdateRequest = React.useCallback(() => {
259+
if (blocksEditor.current && currentModule) {
260+
blocksEditor.current.updateToolbox(shownPythonToolboxCategories);
261+
}
262+
}, [currentModule, shownPythonToolboxCategories]);
263+
264+
// Add event listener for toolbox updates
265+
React.useEffect(() => {
266+
window.addEventListener(TOOLBOX_UPDATE_EVENT, handleToolboxUpdateRequest);
267+
268+
return () => {
269+
window.removeEventListener(TOOLBOX_UPDATE_EVENT, handleToolboxUpdateRequest);
270+
};
271+
}, [handleToolboxUpdateRequest]);
272+
256273
// Initialize storage and blocks when app loads
257274
React.useEffect(() => {
258275
openStorage();

src/blocks/mrc_mechanism_component_holder.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ export const BLOCK_NAME = 'mrc_mechanism_component_holder';
3737
export const MECHANISM = 'mechanism';
3838
export const COMPONENT = 'component';
3939

40+
export const TOOLBOX_UPDATE_EVENT = 'toolbox-update-requested';
41+
4042
type MechanismComponentHolderExtraState = {
4143
hideMechanisms?: boolean;
4244
}
@@ -115,17 +117,33 @@ const MECHANISM_COMPONENT_HOLDER = {
115117
let blockMoveEvent = blockEvent as Blockly.Events.BlockMove;
116118
if (blockMoveEvent.reason?.includes('connect')) {
117119
setName(block);
120+
updateToolboxAfterDelay();
118121
}
119122
}
120123
else {
121124
if (blockEvent.type == Blockly.Events.BLOCK_CHANGE) {
122125
setName(block);
126+
updateToolboxAfterDelay();
123127
}
124128
}
125129
},
126130

127131
}
128132

133+
let toolboxUpdateTimeout: NodeJS.Timeout | null = null;
134+
export function updateToolboxAfterDelay(){
135+
if (toolboxUpdateTimeout) {
136+
clearTimeout(toolboxUpdateTimeout);
137+
}
138+
toolboxUpdateTimeout = setTimeout(() => {
139+
const event = new CustomEvent(TOOLBOX_UPDATE_EVENT, {
140+
detail: { timestamp: Date.now() }
141+
});
142+
window.dispatchEvent(event);
143+
toolboxUpdateTimeout = null;
144+
}, 100);
145+
}
146+
129147
export const setup = function () {
130148
Blockly.Blocks[BLOCK_NAME] = MECHANISM_COMPONENT_HOLDER;
131149
}

0 commit comments

Comments
 (0)