Merge pull request from paulkaplan/fix-duplicate-on-duplicate

Add shadow dom ID uniquifier to 'Duplicate' code path
This commit is contained in:
Paul Kaplan 2017-08-09 09:33:43 -04:00 committed by GitHub
commit ddb623455f
3 changed files with 30 additions and 13 deletions

View file

@ -690,6 +690,10 @@ Blockly.BlockSvg.prototype.duplicateAndDragCallback_ = function() {
// Using domToBlock instead of domToWorkspace means that the new block
// will be placed at position (0, 0) in main workspace units.
var newBlock = Blockly.Xml.domToBlock(xml, ws);
// Scratch-specific: Give shadow dom new IDs to prevent duplicating on paste
Blockly.utils.changeObscuredShadowIds(newBlock);
var svgRootNew = newBlock.getSvgRoot();
if (!svgRootNew) {
throw new Error('newBlock is not rendered.');

View file

@ -931,3 +931,26 @@ Blockly.utils.setCssTransform = function(node, transform) {
node.style['transform'] = transform;
node.style['-webkit-transform'] = transform;
};
/**
* Re-assign obscured shadow blocks new IDs to prevent collisions
* Scratch specific to help the VM handle deleting obscured shadows.
* @param {Blockly.Block} block the root block to be processed.
*/
Blockly.utils.changeObscuredShadowIds = function(block) {
var blocks = block.getDescendants();
for (var i = blocks.length - 1; i >= 0; i--) {
var descendant = blocks[i];
for (var j = 0; j < descendant.inputList.length; j++) {
var connection = descendant.inputList[j].connection;
if (connection) {
var shadowDom = connection.getShadowDom();
if (shadowDom) {
shadowDom.setAttribute('id', Blockly.utils.genUid());
connection.setShadowDom(shadowDom);
}
}
}
}
};

View file

@ -945,22 +945,12 @@ Blockly.WorkspaceSvg.prototype.paste = function(xmlBlock) {
try {
var block = Blockly.Xml.domToBlock(xmlBlock, this);
// Scratch-specific: Give shadow dom new IDs to prevent duplicating on paste
Blockly.utils.changeObscuredShadowIds(block);
var blocks = block.getDescendants();
for (var i = blocks.length - 1; i >= 0; i--) {
var descendant = blocks[i];
// Scratch-specific: Give shadow dom new IDs to prevent duplicating on paste
for (var j = 0; j < descendant.inputList.length; j++) {
var connection = descendant.inputList[j].connection;
if (connection) {
var shadowDom = connection.getShadowDom();
if (shadowDom) {
shadowDom.setAttribute('id', Blockly.utils.genUid());
connection.setShadowDom(shadowDom);
}
}
}
// Rerender to get around problem with IE and Edge not measuring text
// correctly when it is hidden.
if (goog.userAgent.IE || goog.userAgent.EDGE) {