Duplicate should spread blocks out like copy and paste do.

This commit is contained in:
Neil Fraser
2015-09-18 18:46:19 -07:00
parent 2b345bbcb6
commit ad1d1261c8
3 changed files with 20 additions and 25 deletions

View File

@@ -260,30 +260,6 @@ Blockly.Block.prototype.unplug = function(healStack, bump) {
}
};
/**
* Duplicate this block and its children.
* @return {!Blockly.Block} The duplicate.
* @private
*/
Blockly.Block.prototype.duplicate_ = function() {
// Create a duplicate via XML.
var xmlBlock = Blockly.Xml.blockToDom_(this);
Blockly.Xml.deleteNext(xmlBlock);
var newBlock = Blockly.Xml.domToBlock(
/** @type {!Blockly.Workspace} */ (this.workspace), xmlBlock);
// Move the duplicate next to the old block.
var xy = this.getRelativeToSurfaceXY();
if (this.RTL) {
xy.x -= Blockly.SNAP_RADIUS;
} else {
xy.x += Blockly.SNAP_RADIUS;
}
xy.y += Blockly.SNAP_RADIUS * 2;
newBlock.moveBy(xy.x, xy.y);
newBlock.select();
return newBlock;
};
/**
* Returns all connections originating from this block.
* @param {boolean} all If true, return all connections even hidden ones.

View File

@@ -508,7 +508,7 @@ Blockly.BlockSvg.prototype.showContextMenu_ = function(e) {
text: Blockly.Msg.DUPLICATE_BLOCK,
enabled: true,
callback: function() {
block.duplicate_();
Blockly.duplicate_(block);
}
};
if (this.getDescendants().length > this.workspace.remainingCapacity()) {

View File

@@ -434,6 +434,25 @@ Blockly.copy_ = function(block) {
Blockly.clipboardSource_ = block.workspace;
};
/**
* Duplicate this block and its children.
* @param {!Blockly.Block} block Block to be copied.
* @private
*/
Blockly.duplicate_ = function(block) {
// Save the clipboard.
var clipboardXml = Blockly.clipboardXml_;
var clipboardSource = Blockly.clipboardSource_;
// Create a duplicate via a copy/paste operation.
Blockly.copy_(block);
block.workspace.paste(Blockly.clipboardXml_);
// Restore the clipboard.
Blockly.clipboardXml_ = clipboardXml;
Blockly.clipboardSource_ = clipboardSource;
};
/**
* Cancel the native context menu, unless the focus is on an HTML input widget.
* @param {!Event} e Mouse down event.