mirror of
https://github.com/scratchfoundation/scratch-blocks.git
synced 2025-08-28 22:10:31 -04:00
Set field.sourceBlock in headless Blockly.
This commit is contained in:
parent
601ea15b75
commit
4ac1204550
8 changed files with 25 additions and 23 deletions
|
@ -118,15 +118,22 @@ Blockly.Field.NBSP = '\u00A0';
|
|||
Blockly.Field.prototype.EDITABLE = true;
|
||||
|
||||
/**
|
||||
* Install this field on a block.
|
||||
* Attach this field to a block.
|
||||
* @param {!Blockly.Block} block The block containing this field.
|
||||
*/
|
||||
Blockly.Field.prototype.init = function(block) {
|
||||
if (this.sourceBlock_) {
|
||||
Blockly.Field.prototype.setSourceBlock = function(block) {
|
||||
goog.asserts.assert(!this.sourceBlock_, 'Field already bound to a block.');
|
||||
this.sourceBlock_ = block;
|
||||
};
|
||||
|
||||
/**
|
||||
* Install this field on a block.
|
||||
*/
|
||||
Blockly.Field.prototype.init = function() {
|
||||
if (this.fieldGroup_) {
|
||||
// Field has already been initialized once.
|
||||
return;
|
||||
}
|
||||
this.sourceBlock_ = block;
|
||||
// Build the DOM.
|
||||
this.fieldGroup_ = Blockly.createSvgElement('g', {}, null);
|
||||
if (!this.visible_) {
|
||||
|
@ -144,7 +151,7 @@ Blockly.Field.prototype.init = function(block) {
|
|||
this.fieldGroup_);
|
||||
|
||||
this.updateEditable();
|
||||
block.getSvgRoot().appendChild(this.fieldGroup_);
|
||||
this.sourceBlock_.getSvgRoot().appendChild(this.fieldGroup_);
|
||||
this.mouseUpWrapper_ =
|
||||
Blockly.bindEvent_(this.fieldGroup_, 'mouseup', this, this.onMouseUp_);
|
||||
// Force a render.
|
||||
|
|
|
@ -61,7 +61,7 @@ Blockly.FieldCheckbox.prototype.CURSOR = 'default';
|
|||
* @param {!Blockly.Block} block The block containing this text.
|
||||
*/
|
||||
Blockly.FieldCheckbox.prototype.init = function(block) {
|
||||
if (this.sourceBlock_) {
|
||||
if (this.fieldGroup_) {
|
||||
// Checkbox has already been initialized once.
|
||||
return;
|
||||
}
|
||||
|
|
|
@ -80,11 +80,10 @@ Blockly.FieldDropdown.prototype.CURSOR = 'default';
|
|||
* @param {!Blockly.Block} block The block containing this text.
|
||||
*/
|
||||
Blockly.FieldDropdown.prototype.init = function(block) {
|
||||
if (this.sourceBlock_) {
|
||||
if (this.fieldGroup_) {
|
||||
// Dropdown has already been initialized once.
|
||||
return;
|
||||
}
|
||||
|
||||
// Add dropdown arrow: "option ▾" (LTR) or "▾ אופציה" (RTL)
|
||||
this.arrow_ = Blockly.createSvgElement('tspan', {}, null);
|
||||
this.arrow_.appendChild(document.createTextNode(
|
||||
|
|
|
@ -67,14 +67,12 @@ Blockly.FieldImage.prototype.EDITABLE = false;
|
|||
|
||||
/**
|
||||
* Install this image on a block.
|
||||
* @param {!Blockly.Block} block The block containing this text.
|
||||
*/
|
||||
Blockly.FieldImage.prototype.init = function(block) {
|
||||
if (this.sourceBlock_) {
|
||||
Blockly.FieldImage.prototype.init = function() {
|
||||
if (this.fieldGroup_) {
|
||||
// Image has already been initialized once.
|
||||
return;
|
||||
}
|
||||
this.sourceBlock_ = block;
|
||||
// Build the DOM.
|
||||
/** @type {SVGElement} */
|
||||
this.fieldGroup_ = Blockly.createSvgElement('g', {}, null);
|
||||
|
@ -97,7 +95,7 @@ Blockly.FieldImage.prototype.init = function(block) {
|
|||
'width': this.width_ + 'px',
|
||||
'fill-opacity': 0}, this.fieldGroup_);
|
||||
}
|
||||
block.getSvgRoot().appendChild(this.fieldGroup_);
|
||||
this.sourceBlock_.getSvgRoot().appendChild(this.fieldGroup_);
|
||||
|
||||
// Configure the field to be transparent with respect to tooltips.
|
||||
var topElement = this.rectElement_ || this.imageElement_;
|
||||
|
|
|
@ -53,15 +53,12 @@ Blockly.FieldLabel.prototype.EDITABLE = false;
|
|||
|
||||
/**
|
||||
* Install this text on a block.
|
||||
* @param {!Blockly.Block} block The block containing this text.
|
||||
*/
|
||||
Blockly.FieldLabel.prototype.init = function(block) {
|
||||
if (this.sourceBlock_) {
|
||||
Blockly.FieldLabel.prototype.init = function() {
|
||||
if (this.textElement_) {
|
||||
// Text has already been initialized once.
|
||||
return;
|
||||
}
|
||||
this.sourceBlock_ = block;
|
||||
|
||||
// Build the DOM.
|
||||
this.textElement_ = Blockly.createSvgElement('text',
|
||||
{'class': 'blocklyText', 'y': this.size_.height - 5}, null);
|
||||
|
@ -71,7 +68,7 @@ Blockly.FieldLabel.prototype.init = function(block) {
|
|||
if (!this.visible_) {
|
||||
this.textElement_.style.display = 'none';
|
||||
}
|
||||
block.getSvgRoot().appendChild(this.textElement_);
|
||||
this.sourceBlock_.getSvgRoot().appendChild(this.textElement_);
|
||||
|
||||
// Configure the field to be transparent with respect to tooltips.
|
||||
this.textElement_.tooltip = this.sourceBlock_;
|
||||
|
|
|
@ -82,7 +82,7 @@ Blockly.FieldVariable.prototype.setValidator = function(handler) {
|
|||
* @param {!Blockly.Block} block The block containing this text.
|
||||
*/
|
||||
Blockly.FieldVariable.prototype.init = function(block) {
|
||||
if (this.sourceBlock_) {
|
||||
if (this.fieldGroup_) {
|
||||
// Dropdown has already been initialized once.
|
||||
return;
|
||||
}
|
||||
|
|
|
@ -326,8 +326,8 @@ Blockly.inject.bindDocumentEvents_ = function() {
|
|||
|
||||
/**
|
||||
* Load sounds for the given workspace.
|
||||
* @param options {string} The path to the media directory.
|
||||
* @param workspace {!Blockly.Workspace} The workspace to load sounds for.
|
||||
* @param {string} options The path to the media directory.
|
||||
* @param {!Blockly.Workspace} workspace The workspace to load sounds for.
|
||||
* @private
|
||||
*/
|
||||
Blockly.inject.loadSounds_ = function(pathToMedia, workspace) {
|
||||
|
|
|
@ -84,8 +84,9 @@ Blockly.Input.prototype.appendField = function(field, opt_name) {
|
|||
if (goog.isString(field)) {
|
||||
field = new Blockly.FieldLabel(/** @type {string} */ (field));
|
||||
}
|
||||
field.setSourceBlock(this.sourceBlock_);
|
||||
if (this.sourceBlock_.rendered) {
|
||||
field.init(this.sourceBlock_);
|
||||
field.init();
|
||||
}
|
||||
field.name = opt_name;
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue