@@ -48,6 +48,7 @@ import { mutatorOpenListener } from './blocks/mrc_param_container'
4848import { TOOLBOX_UPDATE_EVENT } from './blocks/mrc_mechanism_component_holder' ;
4949import { antdThemeFromString } from './reactComponents/ThemeModal' ;
5050import { useTranslation } from 'react-i18next' ;
51+ import { setup } from './blocks/mrc_event_handler' ;
5152
5253/** Storage key for shown toolbox categories. */
5354const SHOWN_TOOLBOX_CATEGORIES_KEY = 'shownPythonToolboxCategories' ;
@@ -87,7 +88,7 @@ const LAYOUT_BACKGROUND_COLOR = '#0F0';
8788 * project management, and user interface layout.
8889 */
8990const App : React . FC = ( ) : React . JSX . Element => {
90- const { t } = useTranslation ( ) ;
91+ const { t, i18n } = useTranslation ( ) ;
9192
9293 const [ alertErrorMessage , setAlertErrorMessage ] = React . useState ( '' ) ;
9394 const [ storage , setStorage ] = React . useState < commonStorage . Storage | null > ( null ) ;
@@ -263,7 +264,7 @@ const App: React.FC = (): React.JSX.Element => {
263264 if ( blocksEditor . current && currentModule ) {
264265 blocksEditor . current . updateToolbox ( shownPythonToolboxCategories ) ;
265266 }
266- } , [ currentModule , shownPythonToolboxCategories ] ) ;
267+ } , [ currentModule , shownPythonToolboxCategories , i18n . language ] ) ;
267268
268269 // Add event listener for toolbox updates
269270 React . useEffect ( ( ) => {
@@ -294,6 +295,23 @@ const App: React.FC = (): React.JSX.Element => {
294295 }
295296 } , [ currentModule ] ) ;
296297
298+ const setupWorkspace = ( newWorkspace : Blockly . WorkspaceSvg ) => {
299+ if ( ! blocklyComponent . current || ! storage ) {
300+ return ;
301+ }
302+ // Recreate workspace when Blockly component is ready
303+ ChangeFramework . setup ( newWorkspace ) ;
304+ newWorkspace . addChangeListener ( mutatorOpenListener ) ;
305+ newWorkspace . addChangeListener ( handleBlocksChanged ) ;
306+ generatorContext . current = createGeneratorContext ( ) ;
307+
308+ if ( currentModule ) {
309+ generatorContext . current . setModule ( currentModule ) ;
310+ }
311+
312+ blocksEditor . current = new editor . Editor ( newWorkspace , generatorContext . current , storage ) ;
313+ blocksEditor . current . updateToolbox ( shownPythonToolboxCategories ) ;
314+ } ;
297315
298316 // Initialize Blockly workspace and editor when component and storage are ready
299317 React . useEffect ( ( ) => {
@@ -303,17 +321,8 @@ const App: React.FC = (): React.JSX.Element => {
303321
304322 const blocklyWorkspace = blocklyComponent . current . getBlocklyWorkspace ( ) ;
305323 if ( blocklyWorkspace ) {
306- ChangeFramework . setup ( blocklyWorkspace ) ;
307- blocklyWorkspace . addChangeListener ( mutatorOpenListener ) ;
308- blocklyWorkspace . addChangeListener ( handleBlocksChanged ) ;
309- }
310-
311- generatorContext . current = createGeneratorContext ( ) ;
312- if ( currentModule ) {
313- generatorContext . current . setModule ( currentModule ) ;
324+ setupWorkspace ( blocklyWorkspace ) ;
314325 }
315-
316- blocksEditor . current = new editor . Editor ( blocklyWorkspace , generatorContext . current , storage ) ;
317326 } , [ blocklyComponent , storage ] ) ;
318327
319328 // Generate code when module or regeneration trigger changes
@@ -395,6 +404,7 @@ const App: React.FC = (): React.JSX.Element => {
395404 < Content >
396405 < BlocklyComponent
397406 theme = { theme }
407+ onWorkspaceRecreated = { setupWorkspace }
398408 ref = { blocklyComponent }
399409 />
400410 </ Content >
0 commit comments