Skip to content

Add shadow dom ID uniquifier to 'Duplicate' code path #1028

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions core/block_svg.js
Original file line number Diff line number Diff line change
Expand Up @@ -684,6 +684,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

This comment was marked as abuse.

Blockly.utils.changeObscuredShadowIds(newBlock);

var svgRootNew = newBlock.getSvgRoot();
if (!svgRootNew) {
throw new Error('newBlock is not rendered.');
Expand Down
23 changes: 23 additions & 0 deletions core/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
}
};
16 changes: 3 additions & 13 deletions core/workspace_svg.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down