@@ -899,8 +899,9 @@ class Blocks {
899899 * Block management: delete blocks and their associated scripts. Does nothing if a block
900900 * with the given ID does not exist.
901901 * @param {!string } blockId Id of block to delete
902+ * @param {boolean } preserve Should stack be kept intact
902903 */
903- deleteBlock ( blockId ) {
904+ deleteBlock ( blockId , preserve ) {
904905 // @todo In runtime, stop threads running on this script.
905906
906907 // Get block
@@ -911,9 +912,26 @@ class Blocks {
911912 }
912913
913914 // Delete children
914- if ( block . next !== null ) {
915+ if ( block . next !== null && ! preserve ) {
915916 this . deleteBlock ( block . next ) ;
916917 }
918+ // Preservation if needed
919+ if ( preserve ) {
920+ const parent = this . _blocks [ block . parent ] ;
921+ const next = this . _blocks [ block . next ] ;
922+ const input = parent ?. inputs
923+ ? Object . values ( parent . inputs ) . find ( input => input . block === blockId )
924+ : null ;
925+ if ( parent && ! input ) {
926+ parent . next = block . next ;
927+ }
928+ if ( next ) {
929+ next . parent = block . parent ;
930+ }
931+ if ( next && input ) {
932+ input . block = block . next ;
933+ }
934+ }
917935
918936 // Delete inputs (including branches)
919937 for ( const input in block . inputs ) {
@@ -928,8 +946,21 @@ class Blocks {
928946 }
929947 }
930948
931- // Delete any script starting with this block.
932- this . _deleteScript ( blockId ) ;
949+ // More preservation stuff
950+ if ( ! preserve ) {
951+ this . _deleteScript ( blockId ) ;
952+ }
953+ const index = this . _scripts . indexOf ( blockId ) ;
954+ if ( preserve && index > - 1 ) {
955+ const next = this . _blocks [ block . next ] ;
956+ if ( next ) {
957+ this . _scripts . push ( next . id ) ;
958+ next . topLevel = true ;
959+ next . x = block . x ;
960+ next . y = block . y ;
961+ }
962+ this . _scripts . splice ( index , 1 ) ;
963+ }
933964
934965 // Delete block itself.
935966 delete this . _blocks [ blockId ] ;
0 commit comments