@@ -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 }
281- if ( workspaceRef . current && active ) {
282- workspaceRef . current . markFocused ( ) ;
283- // Restore scroll position after making visible, with double RAF for proper rendering
284- requestAnimationFrame ( ( ) => {
285- requestAnimationFrame ( ( ) => {
286- if ( workspaceRef . current ) {
271+ if ( workspaceRef . current ) {
272+ if ( active ) {
273+ workspaceRef . current . markFocused ( ) ;
274+
275+ if ( Blockly . getMainWorkspace ( ) . id === workspaceRef . current . id ) {
276+ Blockly . svgResize ( workspaceRef . current ) ;
277+ // We need to call Workspace.scroll, but it is not effective if we call it now.
278+ // I tried requestAnimationFrame, nested requestAnimationFrame, and setTimeout.
279+ // The requestAnimationFrame callback was called first, and calling Workspace.scroll was
280+ // not effective.
281+ // The setTimeout callback was called second, and calling Workspace.scroll was effective.
282+ // The nested requestAnimationFrame callback was called after the setTimeout callback.
283+ // I chose to use setTimeout because it was the earliest callback where calling
284+ // Workspace.scroll was effective.
285+ setTimeout ( ( ) => {
287286 workspaceRef . current . scroll ( savedScrollX . current , savedScrollY . current ) ;
288- }
289- } ) ;
290- } ) ;
287+ } ) ;
288+ }
289+ }
291290 }
292291 } ;
293292
0 commit comments