Skip to content

Commit 7f53299

Browse files
committed
add saving of blocks before changing language
1 parent 7d201d5 commit 7f53299

File tree

2 files changed

+53
-17
lines changed

2 files changed

+53
-17
lines changed

src/App.tsx

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,12 @@ const App: React.FC = (): React.JSX.Element => {
309309
}
310310

311311
blocksEditor.current = new editor.Editor(newWorkspace, generatorContext.current, storage);
312+
313+
// Set the current module in the editor after creating it
314+
if (currentModule) {
315+
blocksEditor.current.loadModuleBlocks(currentModule);
316+
}
317+
312318
blocksEditor.current.updateToolbox(shownPythonToolboxCategories);
313319
};
314320

@@ -353,6 +359,31 @@ const App: React.FC = (): React.JSX.Element => {
353359
}
354360
}, [project]);
355361

362+
// Handle language changes with automatic saving
363+
React.useEffect(() => {
364+
const handleLanguageChange = async () => {
365+
// Save current blocks before language change
366+
if (currentModule && areBlocksModified()) {
367+
try {
368+
await saveBlocks();
369+
} catch (e) {
370+
console.error('Failed to save blocks before language change:', e);
371+
}
372+
}
373+
374+
// Update toolbox after language change
375+
if (blocksEditor.current) {
376+
blocksEditor.current.updateToolbox(shownPythonToolboxCategories);
377+
}
378+
};
379+
380+
i18n.on('languageChanged', handleLanguageChange);
381+
382+
return () => {
383+
i18n.off('languageChanged', handleLanguageChange);
384+
};
385+
}, [currentModule, shownPythonToolboxCategories, i18n]);
386+
356387
const { Sider, Content } = Antd.Layout;
357388

358389
return (

src/reactComponents/BlocklyComponent.tsx

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,8 @@ const BlocklyComponent = React.forwardRef<BlocklyComponentType | null, BlocklyCo
140140
if (!workspaceRef.current) {
141141
return;
142142
}
143+
// Save workspace state
144+
const workspaceXml = Blockly.Xml.workspaceToDom(workspaceRef.current);
143145

144146
// Set new locale
145147
switch (i18n.language) {
@@ -153,30 +155,33 @@ const BlocklyComponent = React.forwardRef<BlocklyComponentType | null, BlocklyCo
153155
Blockly.setLocale(En as any);
154156
break;
155157
}
156-
157158
// Apply custom tokens
158159
Blockly.setLocale(customTokens(t));
159160

160-
// Save workspace state
161-
const workspaceXml = Blockly.Xml.workspaceToDom(workspaceRef.current);
162-
163161
// Clear the workspace
164162
workspaceRef.current.clear();
165163

166-
// Restore workspace with new locale (blocks will be recreated with new text)
167-
if (workspaceXml.hasChildNodes()) {
168-
Blockly.Xml.domToWorkspace(workspaceXml, workspaceRef.current);
169-
}
170-
171-
// Refresh the toolbox
172-
const toolbox = workspaceRef.current.getToolbox();
173-
if (toolbox) {
174-
// Force toolbox to rebuild with new locale
175-
toolbox.refreshSelection();
164+
// Force complete toolbox rebuild by calling onWorkspaceRecreated AFTER locale is set
165+
if (props.onWorkspaceRecreated) {
166+
props.onWorkspaceRecreated(workspaceRef.current);
176167
}
177-
178-
// Trigger workspace refresh
179-
Blockly.svgResize(workspaceRef.current);
168+
169+
// Small delay to ensure toolbox is rebuilt before restoring blocks
170+
setTimeout(() => {
171+
if (workspaceRef.current && workspaceXml.hasChildNodes()) {
172+
Blockly.Xml.domToWorkspace(workspaceXml, workspaceRef.current);
173+
}
174+
175+
// Final refresh
176+
const toolbox = workspaceRef.current!.getToolbox();
177+
if (toolbox && toolbox.refreshSelection) {
178+
toolbox.refreshSelection();
179+
}
180+
181+
if (workspaceRef.current) {
182+
Blockly.svgResize(workspaceRef.current);
183+
}
184+
}, 10);
180185
};
181186

182187
/** Initializes the Blockly workspace. */

0 commit comments

Comments
 (0)