Add programmatically setting shadows (#3902)

* Add programmatically setting shadows
This commit is contained in:
Beka Westberg
2020-05-21 11:44:08 -07:00
committed by GitHub
parent 317834ff59
commit 3aa1963be8
4 changed files with 1069 additions and 18 deletions

View File

@@ -108,7 +108,7 @@ Blockly.Connection.prototype.connect_ = function(childConnection) {
var orphanBlock = parentConnection.targetBlock();
var shadowDom = parentConnection.getShadowDom();
// Temporarily set the shadow DOM to null so it does not respawn.
parentConnection.setShadowDom(null);
parentConnection.shadowDom_ = null;
// Displaced shadow blocks dissolve rather than reattaching or bumping.
if (orphanBlock.isShadow()) {
// Save the shadow block so that field values are preserved.
@@ -175,7 +175,7 @@ Blockly.Connection.prototype.connect_ = function(childConnection) {
}
}
// Restore the shadow DOM.
parentConnection.setShadowDom(shadowDom);
parentConnection.shadowDom_ = shadowDom;
}
var event;
@@ -200,12 +200,11 @@ Blockly.Connection.prototype.dispose = function() {
// isConnected returns true for shadows and non-shadows.
if (this.isConnected()) {
// Destroy the attached shadow block & its children (if it exists).
this.setShadowDom(null);
var targetBlock = this.targetBlock();
if (targetBlock.isShadow()) {
// Destroy the attached shadow block & its children.
targetBlock.dispose(false);
} else {
if (targetBlock) {
// Disconnect the attached normal block.
targetBlock.unplug();
}
@@ -672,15 +671,22 @@ Blockly.Connection.prototype.getCheck = function() {
};
/**
* Change a connection's shadow block.
* Changes the connection's shadow block.
* @param {Element} shadow DOM representation of a block or null.
*/
Blockly.Connection.prototype.setShadowDom = function(shadow) {
this.shadowDom_ = shadow;
var target = this.targetBlock();
if (!target) {
this.respawnShadow_();
} else if (target.isShadow()) {
// The disconnect from dispose will automatically generate the new shadow.
target.dispose(false);
}
};
/**
* Return a connection's shadow block.
* Returns the xml representation of the connection's shadow block.
* @return {Element} Shadow DOM representation of a block or null.
*/
Blockly.Connection.prototype.getShadowDom = function() {

View File

@@ -231,6 +231,30 @@ Blockly.Input.prototype.setAlign = function(align) {
return this;
};
/**
* Changes the connection's shadow block.
* @param {Element} shadow DOM representation of a block or null.
* @return {Blockly.Input} The input being modified (to allow chaining).
*/
Blockly.Input.prototype.setShadowDom = function(shadow) {
if (!this.connection) {
throw Error('This input does not have a connection.');
}
this.connection.setShadowDom(shadow);
return this;
};
/**
* Returns the xml representation of the connection's shadow block.
* @return {Element} Shadow DOM representation of a block or null.
*/
Blockly.Input.prototype.getShadowDom = function() {
if (!this.connection) {
throw Error('This input does not have a connection.');
}
return this.connection.getShadowDom();
};
/**
* Initialize the fields on this input.
*/

View File

@@ -661,10 +661,6 @@ Blockly.Xml.domToBlockHeadless_ = function(xmlBlock, workspace) {
}
}
}
// Use the shadow block if there is no child block.
if (!childBlockElement && childShadowElement) {
childBlockElement = childShadowElement;
}
var name = xmlChild.getAttribute('name');
var xmlChildElement = /** @type {!Element} */ (xmlChild);
@@ -719,9 +715,6 @@ Blockly.Xml.domToBlockHeadless_ = function(xmlBlock, workspace) {
prototypeName);
break;
}
if (childShadowElement) {
input.connection.setShadowDom(childShadowElement);
}
if (childBlockElement) {
blockChild = Blockly.Xml.domToBlockHeadless_(childBlockElement,
workspace);
@@ -734,11 +727,12 @@ Blockly.Xml.domToBlockHeadless_ = function(xmlBlock, workspace) {
'Child block does not have output or previous statement.');
}
}
// Set shadow after so we don't create a shadow we delete immediately.
if (childShadowElement) {
input.connection.setShadowDom(childShadowElement);
}
break;
case 'next':
if (childShadowElement && block.nextConnection) {
block.nextConnection.setShadowDom(childShadowElement);
}
if (childBlockElement) {
if (!block.nextConnection) {
throw TypeError('Next statement does not exist.');
@@ -754,6 +748,10 @@ Blockly.Xml.domToBlockHeadless_ = function(xmlBlock, workspace) {
}
block.nextConnection.connect(blockChild.previousConnection);
}
// Set shadow after so we don't create a shadow we delete immediately.
if (childShadowElement && block.nextConnection) {
block.nextConnection.setShadowDom(childShadowElement);
}
break;
default:
// Unknown tag; ignore. Same principle as HTML parsers.

File diff suppressed because it is too large Load Diff