1515
1616import * as Blockly from 'blockly/core' ;
1717import { ASTNode , Marker } from 'blockly/core' ;
18+ import { scrollBoundsIntoView } from './workspace_utilities' ;
1819
1920/** Options object for LineCursor instances. */
2021export type CursorOptions = {
@@ -549,7 +550,11 @@ export class LineCursor extends Marker {
549550 this . drawMarker ( oldNode , newNode ) ;
550551 // Try to scroll cursor into view.
551552 if ( newNode ?. getType ( ) === ASTNode . types . BLOCK ) {
552- this . scrollBlockIntoView ( newNode . getLocation ( ) as Blockly . BlockSvg ) ;
553+ const block = newNode . getLocation ( ) as Blockly . BlockSvg ;
554+ scrollBoundsIntoView (
555+ block . getBoundingRectangleWithoutChildren ( ) ,
556+ block . workspace ,
557+ ) ;
553558 }
554559 }
555560
@@ -568,57 +573,6 @@ export class LineCursor extends Marker {
568573 this . drawMarker ( curNode , curNode ) ;
569574 }
570575
571- /**
572- * Scrolls the provided block into view.
573- *
574- * This is a basic implementation that scrolls just enough to get the block
575- * into bounds. For very small workspaces/high zoom levels the entire block
576- * may not fit, but a decent portion of it should still be visible at least.
577- *
578- * @param block The block to scroll into bounds.
579- */
580- private scrollBlockIntoView ( block : Blockly . BlockSvg ) {
581- const workspace = block . workspace ;
582- const scale = workspace . getScale ( ) ;
583- const bounds = block . getBoundingRectangleWithoutChildren ( ) ;
584- const rawViewport = workspace . getMetricsManager ( ) . getViewMetrics ( true ) ;
585- const viewport = new Blockly . utils . Rect (
586- rawViewport . top ,
587- rawViewport . top + rawViewport . height ,
588- rawViewport . left ,
589- rawViewport . left + rawViewport . width ,
590- ) ;
591-
592- if (
593- bounds . left >= viewport . left &&
594- bounds . top >= viewport . top &&
595- bounds . right <= viewport . right &&
596- bounds . bottom <= viewport . bottom
597- ) {
598- // Do nothing if the block is fully inside the viewport.
599- return ;
600- }
601-
602- let deltaX = 0 ;
603- let deltaY = 0 ;
604-
605- if ( bounds . left < viewport . left ) {
606- deltaX = viewport . left - bounds . left ;
607- } else if ( bounds . right > viewport . right ) {
608- deltaX = viewport . right - bounds . right ;
609- }
610-
611- if ( bounds . top < viewport . top ) {
612- deltaY = viewport . top - bounds . top ;
613- } else if ( bounds . bottom > viewport . bottom ) {
614- deltaY = viewport . bottom - bounds . bottom ;
615- }
616-
617- deltaX *= scale ;
618- deltaY *= scale ;
619- workspace . scroll ( workspace . scrollX + deltaX , workspace . scrollY + deltaY ) ;
620- }
621-
622576 /**
623577 * Draw this cursor's marker.
624578 *
0 commit comments