File tree Expand file tree Collapse file tree 2 files changed +35
-0
lines changed
Expand file tree Collapse file tree 2 files changed +35
-0
lines changed Original file line number Diff line number Diff line change @@ -42,6 +42,7 @@ import * as CustomBlocks from './blocks/setup_custom_blocks';
4242import { initialize as initializePythonBlocks } from './blocks/utils/python' ;
4343import * as ChangeFramework from './blocks/utils/change_framework'
4444import { 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. */
4748const 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 ( ) ;
Original file line number Diff line number Diff line change @@ -37,6 +37,8 @@ export const BLOCK_NAME = 'mrc_mechanism_component_holder';
3737export const MECHANISM = 'mechanism' ;
3838export const COMPONENT = 'component' ;
3939
40+ export const TOOLBOX_UPDATE_EVENT = 'toolbox-update-requested' ;
41+
4042type 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+
129147export const setup = function ( ) {
130148 Blockly . Blocks [ BLOCK_NAME ] = MECHANISM_COMPONENT_HOLDER ;
131149}
You can’t perform that action at this time.
0 commit comments