@@ -228,14 +228,7 @@ export default function BlocklyComponent(props: BlocklyComponentProps): React.JS
228228 if ( workspaceRef . current ) {
229229 if ( workspaceRef . current . isVisible ( ) &&
230230 Blockly . getMainWorkspace ( ) . id === workspaceRef . current . id ) {
231- // Save scroll position before resize
232- const scrollX = workspaceRef . current . scrollX ;
233- const scrollY = workspaceRef . current . scrollY ;
234-
235231 Blockly . svgResize ( workspaceRef . current ) ;
236-
237- // Restore scroll position after resize
238- workspaceRef . current . scroll ( scrollX , scrollY ) ;
239232 }
240233 }
241234 } ;
@@ -266,28 +259,34 @@ export default function BlocklyComponent(props: BlocklyComponentProps): React.JS
266259 const setActive = ( active : boolean ) : void => {
267260 if ( workspaceRef . current ) {
268261 if ( ! active ) {
269- // Always save the scroll position before making this workspace invisible
262+ // Save the scroll position before making this workspace invisible.
270263 savedScrollX . current = workspaceRef . current . scrollX ;
271264 savedScrollY . current = workspaceRef . current . scrollY ;
272- workspaceRef . current . setVisible ( active ) ;
273- } else {
274- // Make visible first
275- workspaceRef . current . setVisible ( active ) ;
276265 }
266+ workspaceRef . current . setVisible ( active ) ;
277267 }
278268 if ( parentDiv . current ) {
279269 parentDiv . current . hidden = ! active ;
280270 }
281271 if ( workspaceRef . current && active ) {
282272 workspaceRef . current . markFocused ( ) ;
283- // Restore scroll position after making visible, with double RAF for proper rendering
284- requestAnimationFrame ( ( ) => {
285- requestAnimationFrame ( ( ) => {
273+
274+ if ( Blockly . getMainWorkspace ( ) . id === workspaceRef . current . id ) {
275+ Blockly . svgResize ( workspaceRef . current ) ;
276+ // We need to call Workspace.scroll, but it is not effective if we call it now.
277+ // I tried requestAnimationFrame, nested requestAnimationFrame, and setTimeout.
278+ // The requestAnimationFrame callback was called first, and calling Workspace.scroll was
279+ // not effective.
280+ // The setTimeout callback was called second, and calling Workspace.scroll was effective.
281+ // The nested requestAnimationFrame callback was called after the setTimeout callback.
282+ // I chose to use setTimeout because it was the earliest callback where calling
283+ // Workspace.scroll was effective.
284+ setTimeout ( ( ) => {
286285 if ( workspaceRef . current ) {
287286 workspaceRef . current . scroll ( savedScrollX . current , savedScrollY . current ) ;
288287 }
289288 } ) ;
290- } ) ;
289+ }
291290 }
292291 } ;
293292
0 commit comments