@@ -429,6 +429,79 @@ suite('Keyboard Shortcut Items', function () {
429429 sinon . assert . calledWith ( toastSpy , this . workspace , 'copiedHint' ) ;
430430 toastSpy . restore ( ) ;
431431 } ) ;
432+
433+ test ( 'Pastes near focused block instead of copy origin' , function ( ) {
434+ this . workspace . clear ( ) ;
435+ const blockA = setSelectedBlock ( this . workspace ) ;
436+
437+ this . injectionDiv . dispatchEvent (
438+ createKeyDownEvent ( Blockly . utils . KeyCodes . C , [
439+ Blockly . utils . KeyCodes . CTRL_CMD ,
440+ ] ) ,
441+ ) ;
442+
443+ const blockB = Blockly . serialization . blocks . append (
444+ { type : 'stack_block' , x : 300 , y : 300 } ,
445+ this . workspace ,
446+ ) ;
447+ Blockly . getFocusManager ( ) . focusNode ( blockB ) ;
448+
449+ this . injectionDiv . dispatchEvent (
450+ createKeyDownEvent ( Blockly . utils . KeyCodes . V , [
451+ Blockly . utils . KeyCodes . CTRL_CMD ,
452+ ] ) ,
453+ ) ;
454+
455+ const pastedBlock = this . workspace
456+ . getAllBlocks ( false )
457+ . find ( ( b ) => ! [ blockA , blockB ] . includes ( b ) ) ;
458+ assert . isDefined ( pastedBlock ) ;
459+
460+ const pastedXY = pastedBlock . getRelativeToSurfaceXY ( ) ;
461+ // Check that the pasted block is closer to blockB than blockA, which means
462+ // it used the focus location instead of the copy origin.
463+ assert . isBelow (
464+ Blockly . utils . Coordinate . distance (
465+ pastedXY ,
466+ blockB . getRelativeToSurfaceXY ( ) ,
467+ ) ,
468+ Blockly . utils . Coordinate . distance (
469+ pastedXY ,
470+ blockA . getRelativeToSurfaceXY ( ) ,
471+ ) ,
472+ ) ;
473+ } ) ;
474+
475+ test ( 'Uses copy origin when workspace has focus' , function ( ) {
476+ const blockA = setSelectedBlock ( this . workspace ) ;
477+ this . injectionDiv . dispatchEvent (
478+ createKeyDownEvent ( Blockly . utils . KeyCodes . C , [
479+ Blockly . utils . KeyCodes . CTRL_CMD ,
480+ ] ) ,
481+ ) ;
482+
483+ Blockly . getFocusManager ( ) . focusNode ( this . workspace ) ;
484+ this . injectionDiv . dispatchEvent (
485+ createKeyDownEvent ( Blockly . utils . KeyCodes . V , [
486+ Blockly . utils . KeyCodes . CTRL_CMD ,
487+ ] ) ,
488+ ) ;
489+
490+ const pastedBlock = this . workspace
491+ . getAllBlocks ( false )
492+ . find ( ( b ) => b . id !== blockA . id ) ;
493+ assert . isDefined ( pastedBlock ) ;
494+
495+ const copyOrigin = blockA . getRelativeToSurfaceXY ( ) ;
496+ const pastedXY = pastedBlock . getRelativeToSurfaceXY ( ) ;
497+ assert . isBelow (
498+ Blockly . utils . Coordinate . distance ( pastedXY , copyOrigin ) ,
499+ Blockly . utils . Coordinate . distance (
500+ pastedXY ,
501+ new Blockly . utils . Coordinate ( 300 , 300 ) ,
502+ ) ,
503+ ) ;
504+ } ) ;
432505 } ) ;
433506
434507 suite ( 'Undo' , function ( ) {
0 commit comments