This is FieldButton, which holds a button you can click to do something. I based it off of FieldTextInput and FieldLabel. Instead of showing an editor, it clicks a button on my page.
// lets try adding in a button, which will be a text element that when you click on it,
// the "editor" is to do the file choose menu.
goog.provide('Blockly.FieldButton');
goog.require('Blockly.Field');
goog.require('Blockly.Msg');
goog.require('goog.asserts');
goog.require('goog.dom');
goog.require('goog.userAgent');
/**
* Class for an editable text field.
* @param {string} text The initial content of the field.
* @param {Function=} opt_changeHandler An optional function that is called
* to validate any constraints on what the user entered. Takes the new
* text as an argument and returns either the accepted text, a replacement
* text, or null to abort the change.
* @extends {Blockly.Field}
* @constructor
*/
Blockly.FieldButton = function(text, opt_changeHandler) {
Blockly.FieldButton.superClass_.constructor.call(this, text);
this.setChangeHandler(opt_changeHandler);
};
goog.inherits(Blockly.FieldButton, Blockly.Field);
/**
* Mouse cursor style when over the hotspot that initiates the editor.
*/
Blockly.FieldButton.prototype.CURSOR = 'default';
/**
* Editable fields are saved by the XML renderer, non-editable fields are not.
*/
Blockly.FieldButton.prototype.EDITABLE = true;
/**
* Close the input widget if this input is being deleted.
*/
Blockly.FieldButton.prototype.dispose = function() {
Blockly.WidgetDiv.hideIfOwner(this);
Blockly.FieldButton.superClass_.dispose.call(this);
};
/**
* Set the text in this field.
* @param {?string} text New text.
* @override
*/
Blockly.FieldButton.prototype.setText = function(text) {
if (text === null) {
// No change if null.
return;
}
if (this.sourceBlock_ && this.changeHandler_) {
var validated = this.changeHandler_(text);
// If the new text is invalid, validation returns null.
// In this case we still want to display the illegal result.
if (validated !== null && validated !== undefined) {
text = validated;
}
}
Blockly.Field.prototype.setText.call(this, text);
};
/**
* Show the inline free-text editor on top of the text.
* @param {boolean=} opt_quietInput True if editor should be created without
* focus. Defaults to false.
* @private
*/
Blockly.FieldButton.prototype.showEditor_ = function(opt_quietInput) {
// console.log("editor activated");
Blockscad.currentInterestingBlock = this.sourceBlock_;
$('#importStl').click();
};
/**
* Close the editor, save the results, and dispose of the editable
* text field's elements.
* @return {!Function} Closure to call on destruction of the WidgetDiv.
* @private
*/
Blockly.FieldButton.prototype.widgetDispose_ = function() {
var thisField = this;
return function() {
var htmlInput = Blockly.FieldButton.htmlInput_;
// Save the edit (if it validates).
var text = htmlInput.value;
if (thisField.sourceBlock_ && thisField.changeHandler_) {
var text1 = thisField.changeHandler_(text);
if (text1 === null) {
// Invalid edit.
text = htmlInput.defaultValue;
} else if (text1 !== undefined) {
// Change handler has changed the text.
text = text1;
}
}
thisField.setText(text);
thisField.sourceBlock_.rendered && thisField.sourceBlock_.render();
Blockly.unbindEvent_(htmlInput.onKeyDownWrapper_);
Blockly.unbindEvent_(htmlInput.onKeyUpWrapper_);
Blockly.unbindEvent_(htmlInput.onKeyPressWrapper_);
Blockly.unbindEvent_(htmlInput.onWorkspaceChangeWrapper_);
Blockly.FieldButton.htmlInput_ = null;
// Delete the width property.
Blockly.WidgetDiv.DIV.style.width = 'auto';
};
};