@@ -228,7 +228,14 @@ 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+
231235 Blockly . svgResize ( workspaceRef . current ) ;
236+
237+ // Restore scroll position after resize
238+ workspaceRef . current . scroll ( scrollX , scrollY ) ;
232239 }
233240 }
234241 } ;
@@ -259,43 +266,31 @@ export default function BlocklyComponent(props: BlocklyComponentProps): React.JS
259266 const setActive = ( active : boolean ) : void => {
260267 if ( workspaceRef . current ) {
261268 if ( ! active ) {
262- // Save the scroll position before making this workspace invisible.
263- if ( isScrollPositionValid ( workspaceRef . current ) ) {
264- savedScrollX . current = workspaceRef . current . scrollX ;
265- savedScrollY . current = workspaceRef . current . scrollY ;
266- } else {
267- savedScrollX . current = 0 ;
268- savedScrollY . current = 0 ;
269- }
269+ // Always save the scroll position before making this workspace invisible
270+ savedScrollX . current = workspaceRef . current . scrollX ;
271+ savedScrollY . current = workspaceRef . current . scrollY ;
272+ workspaceRef . current . setVisible ( active ) ;
273+ } else {
274+ // Make visible first
275+ workspaceRef . current . setVisible ( active ) ;
270276 }
271- workspaceRef . current . setVisible ( active ) ;
272277 }
273278 if ( parentDiv . current ) {
274279 parentDiv . current . hidden = ! active ;
275280 }
276- if ( workspaceRef . current ) {
277- if ( active ) {
278- workspaceRef . current . markFocused ( ) ;
279-
280- const needScroll = ! isScrollPositionValid ( workspaceRef . current ) ;
281- if ( Blockly . getMainWorkspace ( ) . id === workspaceRef . current . id ) {
282- Blockly . svgResize ( workspaceRef . current ) ;
283- if ( needScroll ) {
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 ) {
284287 workspaceRef . current . scroll ( savedScrollX . current , savedScrollY . current ) ;
285288 }
286- }
287- }
289+ } ) ;
290+ } ) ;
288291 }
289292 } ;
290293
291- const isScrollPositionValid = ( workspace : Blockly . WorkspaceSvg ) : boolean => {
292- return ! (
293- Math . round ( workspace . getMetrics ( ) . svgWidth ) === 0 &&
294- Math . round ( workspace . getMetrics ( ) . svgHeight ) === 0 &&
295- Math . round ( workspace . scrollX ) === - 10 &&
296- Math . round ( workspace . scrollY ) === - 10 ) ;
297- } ;
298-
299294 // Initialize Blockly workspace
300295 React . useEffect ( ( ) => {
301296 if ( props . onBlocklyComponentCreated ) {
0 commit comments