@@ -1181,22 +1181,73 @@ export class Editor extends toolboxeditor.ToolboxEditor {
11811181 if ( brk ) {
11821182 const b = this . editor . getBlockById ( bid ) as Blockly . BlockSvg ;
11831183 b . setWarningText ( brk ? brk . exceptionMessage : undefined , pxtblockly . PXT_WARNING_ID ) ;
1184- // ensure highlight is in the screen when a breakpoint info is available
1185- // TODO: make warning mode look good
1186- // b.setHighlightWarning(brk && !!brk.exceptionMessage);
1187- const p = b . getRelativeToSurfaceXY ( ) ;
1188- const c = b . getHeightWidth ( ) ;
1189- const s = this . editor . scale ;
1190- const m = this . editor . getMetrics ( ) ;
1191- // don't center if block is still on the screen
1192- const marginx = 4 ;
1193- const marginy = 4 ;
1194- if ( p . x * s < m . viewLeft + marginx
1195- || ( p . x + c . width ) * s > m . viewLeft + m . viewWidth - marginx
1196- || p . y * s < m . viewTop + marginy
1197- || ( p . y + c . height ) * s > m . viewTop + m . viewHeight - marginy ) {
1198- // move the block towards the center
1199- this . editor . centerOnBlock ( bid ) ;
1184+
1185+ // scroll the workspace so that the block is visible. if the block is too tall or too wide
1186+ // to fit in the workspace view, then try to align it with the left/top edge of the block
1187+ const xy = b . getRelativeToSurfaceXY ( ) ;
1188+ const scale = this . editor . scale ;
1189+ const metrics = this . editor . getMetrics ( ) ;
1190+
1191+ // this margin is in screen pixels
1192+ const margin = 20 ;
1193+
1194+ let scrollX = metrics . viewLeft ;
1195+ let scrollY = metrics . viewTop ;
1196+
1197+ const blockWidth = b . width * scale + margin * 2 ;
1198+ const blockHeight = b . height * scale + margin * 2 ;
1199+
1200+ // in RTL workspaces, the x coordinate for the block is the right side
1201+ const blockLeftEdge = this . editor . RTL ? ( xy . x - b . width ) * scale - margin : xy . x * scale - margin ;
1202+ const blockRightEdge = blockLeftEdge + blockWidth ;
1203+
1204+ const blockTopEdge = xy . y * scale - margin ;
1205+ const blockBottomEdge = blockTopEdge + blockHeight ;
1206+
1207+ const viewBottom = metrics . viewTop + metrics . viewHeight ;
1208+ const viewRight = metrics . viewLeft + metrics . viewWidth ;
1209+
1210+ if ( metrics . viewTop > blockTopEdge ) {
1211+ scrollY = blockTopEdge ;
1212+ }
1213+ else if ( viewBottom < blockBottomEdge ) {
1214+ if ( blockHeight > metrics . viewHeight ) {
1215+ scrollY = blockTopEdge ;
1216+ }
1217+ else {
1218+ scrollY = blockBottomEdge - metrics . viewHeight ;
1219+ }
1220+ }
1221+
1222+ if ( this . editor . RTL ) {
1223+ // for RTL, we want to align to the right edge
1224+ if ( viewRight < blockRightEdge ) {
1225+ scrollX = blockRightEdge - metrics . viewWidth ;
1226+ }
1227+ else if ( metrics . viewLeft > blockLeftEdge ) {
1228+ if ( blockWidth > metrics . viewWidth ) {
1229+ scrollX = blockRightEdge - metrics . viewWidth ;
1230+ }
1231+ else {
1232+ scrollX = blockLeftEdge ;
1233+ }
1234+ }
1235+ }
1236+ else if ( metrics . viewLeft > blockLeftEdge ) {
1237+ scrollX = blockLeftEdge ;
1238+ }
1239+ else if ( viewRight < blockRightEdge ) {
1240+ if ( blockWidth > metrics . viewWidth ) {
1241+ scrollX = blockLeftEdge ;
1242+ }
1243+ else {
1244+ scrollX = blockRightEdge - metrics . viewWidth ;
1245+ }
1246+ }
1247+
1248+ if ( scrollX !== metrics . viewLeft || scrollY !== metrics . scrollTop ) {
1249+ // scroll coordinates are negative
1250+ this . editor . scroll ( - scrollX , - scrollY ) ;
12001251 }
12011252 }
12021253 return true ;
0 commit comments