From f30bcbc0e7f680d527bdc0ae2a84f82c87f94896 Mon Sep 17 00:00:00 2001 From: Sean Lip Date: Fri, 1 Jul 2016 15:55:51 -0700 Subject: [PATCH] Fix connection compatibility rules. Use the previous connection when 'pasting above' a block that already has a previous block. --- accessible/clipboard.service.js | 47 +++++++++++++++++++++----- accessible/workspace-tree.component.js | 11 +++++- 2 files changed, 48 insertions(+), 10 deletions(-) diff --git a/accessible/clipboard.service.js b/accessible/clipboard.service.js index 41e5bdb14..db65e2d9f 100644 --- a/accessible/clipboard.service.js +++ b/accessible/clipboard.service.js @@ -63,12 +63,22 @@ blocklyApp.ClipboardService = ng.core return this.canBeCopiedToMarkedConnection(block); }, canBeCopiedToMarkedConnection: function(block) { - var blockConnection = block.outputConnection || block.previousConnection; - return Boolean( - this.markedConnection_ && - this.markedConnection_.sourceBlock_.workspace && - this.areConnectionsCompatible_( - blockConnection, this.markedConnection_)); + if (!this.markedConnection_ || + !this.markedConnection_.getSourceBlock().workspace) { + return false; + } + + var potentialConnections = [ + block.outputConnection, + block.previousConnection, + block.nextConnection + ]; + + var that = this; + return potentialConnections.some(function(connection) { + return that.areConnectionsCompatible_( + connection, that.markedConnection_); + }); }, markConnection: function(connection) { this.markedConnection_ = connection; @@ -110,9 +120,28 @@ blocklyApp.ClipboardService = ng.core var xml = Blockly.Xml.blockToDom(block); var reconstitutedBlock = Blockly.Xml.domToBlock( blocklyApp.workspace, xml); - this.markedConnection_.connect( - reconstitutedBlock.outputConnection || - reconstitutedBlock.previousConnection); + + var potentialConnections = [ + reconstitutedBlock.outputConnection, + reconstitutedBlock.previousConnection, + reconstitutedBlock.nextConnection + ]; + + var connectionSuccessful = false; + for (var i = 0; i < potentialConnections.length; i++) { + if (this.areConnectionsCompatible_( + this.markedConnection_, potentialConnections[i])) { + this.markedConnection_.connect(potentialConnections[i]); + connectionSuccessful = true; + break; + } + } + + if (!connectionSuccessful) { + console.error('ERROR: Could not connect block to marked spot.'); + return; + } + if (announce) { alert( Blockly.Msg.PASTED_BLOCK_TO_MARKED_SPOT_MSG + diff --git a/accessible/workspace-tree.component.js b/accessible/workspace-tree.component.js index 5e5a094ac..5c7f4dc5f 100644 --- a/accessible/workspace-tree.component.js +++ b/accessible/workspace-tree.component.js @@ -147,7 +147,16 @@ blocklyApp.WorkspaceTreeComponent = ng.core pasteToConnection_: function(connection) { var that = this; this.treeService.runWhilePreservingFocus(function() { - that.clipboardService.pasteFromClipboard(connection); + // If the connection is a 'previousConnection' and that connection is + // already joined to something, use the 'nextConnection' of the + // previous block instead in order to do an insertion. + if (connection.type == Blockly.PREVIOUS_STATEMENT && + connection.isConnected()) { + that.clipboardService.pasteFromClipboard( + connection.targetConnection); + } else { + that.clipboardService.pasteFromClipboard(connection); + } }, this.tree.id); }, sendToMarkedSpot_: function() {