11import * as Blockly from 'blockly' ;
22import { workspace } from './blocklyinit.js' ;
33import { flock } from '../flock.js' ;
4+ import { scrollToBlockTopParentLeft } from '../ui/blocklyutil.js' ;
45
56export const isNarrowScreen = ( ) => {
67 return window . innerWidth <= 1024 ;
@@ -10,6 +11,8 @@ const isMobile = () => {
1011 return / M o b i | A n d r o i d | i P a d / i. test ( navigator . userAgent ) ;
1112} ;
1213
14+ let pendingScrollBlockId = null ;
15+
1316export function onResize ( mode ) {
1417 // First handle canvas and engine
1518 resizeCanvas ( ) ;
@@ -29,6 +32,10 @@ export function onResize(mode) {
2932 if ( workspace ) {
3033 Blockly . svgResize ( workspace ) ;
3134 if ( mode === 'reset' ) workspace . scroll ( scrollX , scrollY ) ;
35+ if ( mode === 'reset' && pendingScrollBlockId ) {
36+ scrollToBlockTopParentLeft ( workspace , pendingScrollBlockId ) ;
37+ pendingScrollBlockId = null ;
38+ }
3239 }
3340 } ) ;
3441}
@@ -320,12 +327,6 @@ function showCodeView() {
320327 if ( canvasToggleBtn ) canvasToggleBtn . setAttribute ( 'aria-pressed' , 'false' ) ;
321328 if ( codeToggleBtn ) codeToggleBtn . setAttribute ( 'aria-pressed' , 'true' ) ;
322329
323- // Blockly resize after DOM changes
324- requestAnimationFrame ( ( ) => {
325- if ( workspace ) {
326- Blockly . svgResize ( workspace ) ;
327- }
328- } ) ;
329330 }
330331
331332 onResize ( 'reset' ) ;
@@ -343,6 +344,9 @@ export function showCanvasView() {
343344 currentView = 'canvas' ;
344345
345346 if ( isNarrowScreen ( ) ) {
347+ // Save the current block before hiding the panel — currentBlock may be cleared on hide.
348+ pendingScrollBlockId = window . currentBlock ?. id || null ;
349+
346350 // Instead of CSS transform, change the layout directly
347351 const canvasArea = document . getElementById ( 'canvasArea' ) ;
348352 const blocklyArea = document . getElementById ( 'codePanel' ) ;
0 commit comments