Skip to content

Commit ddb6234

Browse files
authored
Merge pull request scratchfoundation#1028 from paulkaplan/fix-duplicate-on-duplicate
Add shadow dom ID uniquifier to 'Duplicate' code path
2 parents bdbe98e + 3efae4c commit ddb6234

File tree

3 files changed

+30
-13
lines changed

3 files changed

+30
-13
lines changed

core/block_svg.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -690,6 +690,10 @@ Blockly.BlockSvg.prototype.duplicateAndDragCallback_ = function() {
690690
// Using domToBlock instead of domToWorkspace means that the new block
691691
// will be placed at position (0, 0) in main workspace units.
692692
var newBlock = Blockly.Xml.domToBlock(xml, ws);
693+
694+
// Scratch-specific: Give shadow dom new IDs to prevent duplicating on paste
695+
Blockly.utils.changeObscuredShadowIds(newBlock);
696+
693697
var svgRootNew = newBlock.getSvgRoot();
694698
if (!svgRootNew) {
695699
throw new Error('newBlock is not rendered.');

core/utils.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -931,3 +931,26 @@ Blockly.utils.setCssTransform = function(node, transform) {
931931
node.style['transform'] = transform;
932932
node.style['-webkit-transform'] = transform;
933933
};
934+
935+
936+
/**
937+
* Re-assign obscured shadow blocks new IDs to prevent collisions
938+
* Scratch specific to help the VM handle deleting obscured shadows.
939+
* @param {Blockly.Block} block the root block to be processed.
940+
*/
941+
Blockly.utils.changeObscuredShadowIds = function(block) {
942+
var blocks = block.getDescendants();
943+
for (var i = blocks.length - 1; i >= 0; i--) {
944+
var descendant = blocks[i];
945+
for (var j = 0; j < descendant.inputList.length; j++) {
946+
var connection = descendant.inputList[j].connection;
947+
if (connection) {
948+
var shadowDom = connection.getShadowDom();
949+
if (shadowDom) {
950+
shadowDom.setAttribute('id', Blockly.utils.genUid());
951+
connection.setShadowDom(shadowDom);
952+
}
953+
}
954+
}
955+
}
956+
};

core/workspace_svg.js

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -945,22 +945,12 @@ Blockly.WorkspaceSvg.prototype.paste = function(xmlBlock) {
945945
try {
946946
var block = Blockly.Xml.domToBlock(xmlBlock, this);
947947

948+
// Scratch-specific: Give shadow dom new IDs to prevent duplicating on paste
949+
Blockly.utils.changeObscuredShadowIds(block);
950+
948951
var blocks = block.getDescendants();
949952
for (var i = blocks.length - 1; i >= 0; i--) {
950953
var descendant = blocks[i];
951-
952-
// Scratch-specific: Give shadow dom new IDs to prevent duplicating on paste
953-
for (var j = 0; j < descendant.inputList.length; j++) {
954-
var connection = descendant.inputList[j].connection;
955-
if (connection) {
956-
var shadowDom = connection.getShadowDom();
957-
if (shadowDom) {
958-
shadowDom.setAttribute('id', Blockly.utils.genUid());
959-
connection.setShadowDom(shadowDom);
960-
}
961-
}
962-
}
963-
964954
// Rerender to get around problem with IE and Edge not measuring text
965955
// correctly when it is hidden.
966956
if (goog.userAgent.IE || goog.userAgent.EDGE) {

0 commit comments

Comments
 (0)