|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
goog.provide('Blockly.FieldTextInput');
|
|
|
|
goog.require('Blockly.BlockSvg.render');
|
|
goog.require('Blockly.Colours');
|
|
goog.require('Blockly.Field');
|
|
goog.require('Blockly.Msg');
|
|
goog.require('Blockly.scratchBlocksUtils');
|
|
goog.require('Blockly.utils');
|
|
|
|
goog.require('goog.asserts');
|
|
goog.require('goog.dom');
|
|
goog.require('goog.dom.TagName');
|
|
goog.require('goog.userAgent');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.FieldTextInput = function(text, opt_validator, opt_restrictor) {
|
|
Blockly.FieldTextInput.superClass_.constructor.call(this, text,
|
|
opt_validator);
|
|
this.setRestrictor(opt_restrictor);
|
|
this.addArgType('text');
|
|
};
|
|
goog.inherits(Blockly.FieldTextInput, Blockly.Field);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.FieldTextInput.fromJson = function(options) {
|
|
var text = Blockly.utils.replaceMessageReferences(options['text']);
|
|
var field = new Blockly.FieldTextInput(text, options['class']);
|
|
if (typeof options['spellcheck'] === 'boolean') {
|
|
field.setSpellcheck(options['spellcheck']);
|
|
}
|
|
return field;
|
|
};
|
|
|
|
|
|
|
|
|
|
Blockly.FieldTextInput.ANIMATION_TIME = 0.25;
|
|
|
|
|
|
|
|
|
|
Blockly.FieldTextInput.TEXT_MEASURE_PADDING_MAGIC = 45;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.FieldTextInput.htmlInput_ = null;
|
|
|
|
|
|
|
|
|
|
Blockly.FieldTextInput.prototype.CURSOR = 'text';
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.FieldTextInput.prototype.spellcheck_ = true;
|
|
|
|
|
|
|
|
|
|
Blockly.FieldTextInput.prototype.init = function() {
|
|
if (this.fieldGroup_) {
|
|
|
|
return;
|
|
}
|
|
|
|
var notInShadow = !this.sourceBlock_.isShadow();
|
|
|
|
if (notInShadow) {
|
|
this.className_ += ' blocklyEditableLabel';
|
|
}
|
|
|
|
Blockly.FieldTextInput.superClass_.init.call(this);
|
|
|
|
|
|
if (notInShadow) {
|
|
this.box_ = Blockly.utils.createSvgElement('rect',
|
|
{
|
|
'x': 0,
|
|
'y': 0,
|
|
'width': this.size_.width,
|
|
'height': this.size_.height,
|
|
'fill': this.sourceBlock_.getColourTertiary()
|
|
}
|
|
);
|
|
this.fieldGroup_.insertBefore(this.box_, this.textElement_);
|
|
}
|
|
};
|
|
|
|
|
|
|
|
|
|
Blockly.FieldTextInput.prototype.dispose = function() {
|
|
Blockly.WidgetDiv.hideIfOwner(this);
|
|
Blockly.FieldTextInput.superClass_.dispose.call(this);
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.FieldTextInput.prototype.setValue = function(newValue) {
|
|
if (newValue === null) {
|
|
return;
|
|
}
|
|
if (this.sourceBlock_) {
|
|
var validated = this.callValidator(newValue);
|
|
|
|
|
|
if (validated !== null) {
|
|
newValue = validated;
|
|
}
|
|
}
|
|
Blockly.Field.prototype.setValue.call(this, newValue);
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.FieldTextInput.prototype.setText = function(newText) {
|
|
if (newText === null) {
|
|
|
|
return;
|
|
}
|
|
newText = String(newText);
|
|
if (newText === this.text_) {
|
|
|
|
return;
|
|
}
|
|
if (this.sourceBlock_ && Blockly.Events.isEnabled()) {
|
|
Blockly.Events.fire(new Blockly.Events.BlockChange(
|
|
this.sourceBlock_, 'field', this.name, this.text_, newText));
|
|
}
|
|
Blockly.Field.prototype.setText.call(this, newText);
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.FieldTextInput.prototype.setSpellcheck = function(check) {
|
|
this.spellcheck_ = check;
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.FieldTextInput.prototype.setRestrictor = function(restrictor) {
|
|
this.restrictor_ = restrictor;
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.FieldTextInput.prototype.showEditor_ = function(
|
|
opt_quietInput, opt_readOnly, opt_withArrow, opt_arrowCallback) {
|
|
this.workspace_ = this.sourceBlock_.workspace;
|
|
var quietInput = opt_quietInput || false;
|
|
var readOnly = opt_readOnly || false;
|
|
Blockly.WidgetDiv.show(this, this.sourceBlock_.RTL,
|
|
this.widgetDispose_(), this.widgetDisposeAnimationFinished_(),
|
|
Blockly.FieldTextInput.ANIMATION_TIME);
|
|
var div = Blockly.WidgetDiv.DIV;
|
|
|
|
div.className += ' fieldTextInput';
|
|
|
|
var htmlInput =
|
|
goog.dom.createDom(goog.dom.TagName.INPUT, 'blocklyHtmlInput');
|
|
htmlInput.setAttribute('spellcheck', this.spellcheck_);
|
|
if (readOnly) {
|
|
htmlInput.setAttribute('readonly', 'true');
|
|
}
|
|
|
|
Blockly.FieldTextInput.htmlInput_ = htmlInput;
|
|
div.appendChild(htmlInput);
|
|
|
|
if (opt_withArrow) {
|
|
|
|
if (this.sourceBlock_.RTL) {
|
|
htmlInput.style.paddingLeft = (this.arrowSize_ + Blockly.BlockSvg.DROPDOWN_ARROW_PADDING) + 'px';
|
|
} else {
|
|
htmlInput.style.paddingRight = (this.arrowSize_ + Blockly.BlockSvg.DROPDOWN_ARROW_PADDING) + 'px';
|
|
}
|
|
|
|
var dropDownArrow =
|
|
goog.dom.createDom(goog.dom.TagName.IMG, 'blocklyTextDropDownArrow');
|
|
dropDownArrow.setAttribute('src',
|
|
Blockly.mainWorkspace.options.pathToMedia + 'dropdown-arrow-dark.svg');
|
|
dropDownArrow.style.width = this.arrowSize_ + 'px';
|
|
dropDownArrow.style.height = this.arrowSize_ + 'px';
|
|
dropDownArrow.style.top = this.arrowY_ + 'px';
|
|
dropDownArrow.style.cursor = 'pointer';
|
|
|
|
var dropdownArrowMagic = '11px';
|
|
if (this.sourceBlock_.RTL) {
|
|
dropDownArrow.style.left = dropdownArrowMagic;
|
|
} else {
|
|
dropDownArrow.style.right = dropdownArrowMagic;
|
|
}
|
|
if (opt_arrowCallback) {
|
|
htmlInput.dropDownArrowMouseWrapper_ = Blockly.bindEvent_(dropDownArrow,
|
|
'mousedown', this, opt_arrowCallback);
|
|
}
|
|
div.appendChild(dropDownArrow);
|
|
}
|
|
|
|
htmlInput.value = htmlInput.defaultValue = this.text_;
|
|
htmlInput.oldValue_ = null;
|
|
this.validate_();
|
|
this.resizeEditor_();
|
|
if (!quietInput) {
|
|
htmlInput.focus();
|
|
htmlInput.select();
|
|
|
|
htmlInput.setSelectionRange(0, 99999);
|
|
}
|
|
|
|
this.bindEvents_(htmlInput, quietInput || readOnly);
|
|
|
|
|
|
var transitionProperties = 'box-shadow ' + Blockly.FieldTextInput.ANIMATION_TIME + 's';
|
|
if (Blockly.BlockSvg.FIELD_TEXTINPUT_ANIMATE_POSITIONING) {
|
|
div.style.transition += ',padding ' + Blockly.FieldTextInput.ANIMATION_TIME + 's,' +
|
|
'width ' + Blockly.FieldTextInput.ANIMATION_TIME + 's,' +
|
|
'height ' + Blockly.FieldTextInput.ANIMATION_TIME + 's,' +
|
|
'margin-left ' + Blockly.FieldTextInput.ANIMATION_TIME + 's';
|
|
}
|
|
div.style.transition = transitionProperties;
|
|
htmlInput.style.transition = 'font-size ' + Blockly.FieldTextInput.ANIMATION_TIME + 's';
|
|
|
|
htmlInput.style.fontSize = Blockly.BlockSvg.FIELD_TEXTINPUT_FONTSIZE_FINAL + 'pt';
|
|
div.style.boxShadow = '0px 0px 0px 4px ' + Blockly.Colours.fieldShadow;
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.FieldTextInput.prototype.bindEvents_ = function(
|
|
htmlInput, bindGlobalKeypress) {
|
|
|
|
htmlInput.onKeyDownWrapper_ =
|
|
Blockly.bindEventWithChecks_(htmlInput, 'keydown', this,
|
|
this.onHtmlInputKeyDown_);
|
|
|
|
htmlInput.onKeyUpWrapper_ =
|
|
Blockly.bindEventWithChecks_(htmlInput, 'keyup', this,
|
|
this.onHtmlInputChange_);
|
|
|
|
htmlInput.onKeyPressWrapper_ =
|
|
Blockly.bindEventWithChecks_(htmlInput, 'keypress', this,
|
|
this.onHtmlInputChange_);
|
|
|
|
|
|
|
|
|
|
htmlInput.onInputWrapper_ =
|
|
Blockly.bindEvent_(htmlInput, 'input', this, this.onHtmlInputChange_);
|
|
htmlInput.onWorkspaceChangeWrapper_ = this.resizeEditor_.bind(this);
|
|
this.workspace_.addChangeListener(htmlInput.onWorkspaceChangeWrapper_);
|
|
|
|
if (bindGlobalKeypress) {
|
|
htmlInput.onDocumentKeyDownWrapper_ =
|
|
Blockly.bindEventWithChecks_(document, 'keydown', this,
|
|
this.onDocumentKeyDown_);
|
|
}
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.FieldTextInput.prototype.unbindEvents_ = function(htmlInput) {
|
|
Blockly.unbindEvent_(htmlInput.onKeyDownWrapper_);
|
|
Blockly.unbindEvent_(htmlInput.onKeyUpWrapper_);
|
|
Blockly.unbindEvent_(htmlInput.onKeyPressWrapper_);
|
|
Blockly.unbindEvent_(htmlInput.onInputWrapper_);
|
|
this.workspace_.removeChangeListener(
|
|
htmlInput.onWorkspaceChangeWrapper_);
|
|
|
|
|
|
if (htmlInput.onDocumentKeyDownWrapper_) {
|
|
Blockly.unbindEvent_(htmlInput.onDocumentKeyDownWrapper_);
|
|
}
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.FieldTextInput.prototype.onHtmlInputKeyDown_ = function(e) {
|
|
var htmlInput = Blockly.FieldTextInput.htmlInput_;
|
|
var tabKey = 9, enterKey = 13, escKey = 27;
|
|
if (e.keyCode == enterKey) {
|
|
Blockly.WidgetDiv.hide();
|
|
Blockly.DropDownDiv.hideWithoutAnimation();
|
|
} else if (e.keyCode == escKey) {
|
|
htmlInput.value = htmlInput.defaultValue;
|
|
Blockly.WidgetDiv.hide();
|
|
Blockly.DropDownDiv.hideWithoutAnimation();
|
|
} else if (e.keyCode == tabKey) {
|
|
Blockly.WidgetDiv.hide();
|
|
Blockly.DropDownDiv.hideWithoutAnimation();
|
|
this.sourceBlock_.tab(this, !e.shiftKey);
|
|
e.preventDefault();
|
|
}
|
|
};
|
|
|
|
Blockly.FieldTextInput.prototype.onDocumentKeyDown_ = function(e) {
|
|
var htmlInput = Blockly.FieldTextInput.htmlInput_;
|
|
var targetMatches = e.target === htmlInput;
|
|
var targetIsInput = e.target.tagName === 'INPUT' || e.target.tagName === 'TEXTAREA';
|
|
if (targetMatches || !targetIsInput) {
|
|
htmlInput.removeAttribute('readonly');
|
|
htmlInput.value = '';
|
|
htmlInput.focus();
|
|
Blockly.unbindEvent_(htmlInput.onDocumentKeyDownWrapper_);
|
|
htmlInput.onDocumentKeyDownWrapper_ = null;
|
|
}
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.FieldTextInput.GECKO_KEYCODE_WHITELIST = [
|
|
97,
|
|
99,
|
|
118,
|
|
120
|
|
];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.FieldTextInput.prototype.onHtmlInputChange_ = function(e) {
|
|
|
|
if (e.type === 'keypress' && this.restrictor_) {
|
|
var keyCode;
|
|
var isWhitelisted = false;
|
|
if (goog.userAgent.GECKO) {
|
|
|
|
keyCode = e.charCode;
|
|
|
|
|
|
|
|
|
|
if (keyCode < 32 || keyCode == 127) {
|
|
isWhitelisted = true;
|
|
} else if (e.metaKey || e.ctrlKey) {
|
|
|
|
|
|
isWhitelisted = Blockly.FieldTextInput.GECKO_KEYCODE_WHITELIST.indexOf(keyCode) > -1;
|
|
}
|
|
} else {
|
|
keyCode = e.keyCode;
|
|
}
|
|
var char = String.fromCharCode(keyCode);
|
|
if (!isWhitelisted && !this.restrictor_.test(char) && e.preventDefault) {
|
|
|
|
e.preventDefault();
|
|
return;
|
|
}
|
|
}
|
|
var htmlInput = Blockly.FieldTextInput.htmlInput_;
|
|
|
|
var text = htmlInput.value;
|
|
if (text !== htmlInput.oldValue_) {
|
|
htmlInput.oldValue_ = text;
|
|
this.setText(text);
|
|
this.validate_();
|
|
} else if (goog.userAgent.WEBKIT) {
|
|
|
|
|
|
this.sourceBlock_.render();
|
|
}
|
|
this.resizeEditor_();
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.FieldTextInput.prototype.validate_ = function() {
|
|
var valid = true;
|
|
goog.asserts.assertObject(Blockly.FieldTextInput.htmlInput_);
|
|
var htmlInput = Blockly.FieldTextInput.htmlInput_;
|
|
if (this.sourceBlock_) {
|
|
valid = this.callValidator(htmlInput.value);
|
|
}
|
|
if (valid === null) {
|
|
Blockly.utils.addClass(htmlInput, 'blocklyInvalidInput');
|
|
} else {
|
|
Blockly.utils.removeClass(htmlInput, 'blocklyInvalidInput');
|
|
}
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.FieldTextInput.prototype.resizeEditor_ = function() {
|
|
var scale = this.sourceBlock_.workspace.scale;
|
|
var div = Blockly.WidgetDiv.DIV;
|
|
|
|
var initialWidth;
|
|
if (this.sourceBlock_.isShadow()) {
|
|
initialWidth = this.sourceBlock_.getHeightWidth().width * scale;
|
|
} else {
|
|
initialWidth = this.size_.width * scale;
|
|
}
|
|
|
|
var width;
|
|
if (Blockly.BlockSvg.FIELD_TEXTINPUT_EXPAND_PAST_TRUNCATION) {
|
|
|
|
var textWidth = Blockly.scratchBlocksUtils.measureText(
|
|
Blockly.FieldTextInput.htmlInput_.style.fontSize,
|
|
Blockly.FieldTextInput.htmlInput_.style.fontFamily,
|
|
Blockly.FieldTextInput.htmlInput_.style.fontWeight,
|
|
Blockly.FieldTextInput.htmlInput_.value
|
|
);
|
|
|
|
textWidth += Blockly.FieldTextInput.TEXT_MEASURE_PADDING_MAGIC;
|
|
textWidth *= scale;
|
|
width = textWidth;
|
|
} else {
|
|
|
|
width = initialWidth;
|
|
}
|
|
|
|
width = Math.max(width, Blockly.BlockSvg.FIELD_WIDTH_MIN_EDIT * scale);
|
|
width = Math.min(width, Blockly.BlockSvg.FIELD_WIDTH_MAX_EDIT * scale);
|
|
|
|
div.style.width = (width / scale + 1) + 'px';
|
|
div.style.height = (Blockly.BlockSvg.FIELD_HEIGHT_MAX_EDIT + 1) + 'px';
|
|
div.style.transform = 'scale(' + scale + ')';
|
|
|
|
|
|
|
|
|
|
div.style.marginLeft = -0.5 * (width - initialWidth) + 'px';
|
|
|
|
|
|
var borderRadius = this.getBorderRadius() + 0.5;
|
|
div.style.borderRadius = borderRadius + 'px';
|
|
Blockly.FieldTextInput.htmlInput_.style.borderRadius = borderRadius + 'px';
|
|
|
|
var strokeColour = this.sourceBlock_.getColourTertiary();
|
|
div.style.borderColor = strokeColour;
|
|
|
|
var xy = this.getAbsoluteXY_();
|
|
|
|
xy.x -= scale / 2;
|
|
xy.y -= scale / 2;
|
|
|
|
|
|
if (this.sourceBlock_.RTL) {
|
|
xy.x += width;
|
|
xy.x -= div.offsetWidth * scale;
|
|
xy.x += 1 * scale;
|
|
}
|
|
|
|
xy.y += 1 * scale;
|
|
if (goog.userAgent.GECKO && Blockly.WidgetDiv.DIV.style.top) {
|
|
|
|
|
|
xy.x += 2 * scale;
|
|
xy.y += 1 * scale;
|
|
}
|
|
if (goog.userAgent.WEBKIT) {
|
|
xy.y -= 1 * scale;
|
|
}
|
|
|
|
div.style.left = xy.x + 'px';
|
|
div.style.top = xy.y + 'px';
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.FieldTextInput.prototype.getBorderRadius = function() {
|
|
if (this.sourceBlock_.getOutputShape() == Blockly.OUTPUT_SHAPE_ROUND) {
|
|
return Blockly.BlockSvg.NUMBER_FIELD_CORNER_RADIUS;
|
|
}
|
|
return Blockly.BlockSvg.TEXT_FIELD_CORNER_RADIUS;
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.FieldTextInput.prototype.widgetDispose_ = function() {
|
|
var thisField = this;
|
|
return function() {
|
|
var div = Blockly.WidgetDiv.DIV;
|
|
var htmlInput = Blockly.FieldTextInput.htmlInput_;
|
|
|
|
thisField.maybeSaveEdit_();
|
|
|
|
thisField.unbindEvents_(htmlInput);
|
|
if (htmlInput.dropDownArrowMouseWrapper_) {
|
|
Blockly.unbindEvent_(htmlInput.dropDownArrowMouseWrapper_);
|
|
}
|
|
Blockly.Events.setGroup(false);
|
|
|
|
|
|
htmlInput.style.fontSize = Blockly.BlockSvg.FIELD_TEXTINPUT_FONTSIZE_INITIAL + 'pt';
|
|
div.style.boxShadow = '';
|
|
|
|
if (thisField.sourceBlock_) {
|
|
if (thisField.sourceBlock_.isShadow()) {
|
|
var size = thisField.sourceBlock_.getHeightWidth();
|
|
div.style.width = (size.width + 1) + 'px';
|
|
div.style.height = (size.height + 1) + 'px';
|
|
} else {
|
|
div.style.width = (thisField.size_.width + 1) + 'px';
|
|
div.style.height = (Blockly.BlockSvg.FIELD_HEIGHT_MAX_EDIT + 1) + 'px';
|
|
}
|
|
}
|
|
div.style.marginLeft = 0;
|
|
};
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.FieldTextInput.prototype.widgetDisposeAnimationFinished_ = function() {
|
|
return function() {
|
|
|
|
var style = Blockly.WidgetDiv.DIV.style;
|
|
style.width = 'auto';
|
|
style.height = 'auto';
|
|
style.fontSize = '';
|
|
|
|
Blockly.WidgetDiv.DIV.className = 'blocklyWidgetDiv';
|
|
|
|
Blockly.WidgetDiv.DIV.removeAttribute('style');
|
|
Blockly.FieldTextInput.htmlInput_.style.transition = '';
|
|
Blockly.FieldTextInput.htmlInput_ = null;
|
|
};
|
|
};
|
|
|
|
Blockly.FieldTextInput.prototype.maybeSaveEdit_ = function() {
|
|
var htmlInput = Blockly.FieldTextInput.htmlInput_;
|
|
|
|
var text = htmlInput.value;
|
|
if (this.sourceBlock_) {
|
|
var text1 = this.callValidator(text);
|
|
if (text1 === null) {
|
|
|
|
text = htmlInput.defaultValue;
|
|
} else {
|
|
|
|
text = text1;
|
|
if (this.onFinishEditing_) {
|
|
this.onFinishEditing_(text);
|
|
}
|
|
}
|
|
}
|
|
this.setText(text);
|
|
this.sourceBlock_.rendered && this.sourceBlock_.render();
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.FieldTextInput.numberValidator = function(text) {
|
|
console.warn('Blockly.FieldTextInput.numberValidator is deprecated. ' +
|
|
'Use Blockly.FieldNumber instead.');
|
|
if (text === null) {
|
|
return null;
|
|
}
|
|
text = String(text);
|
|
|
|
|
|
text = text.replace(/O/ig, '0');
|
|
|
|
text = text.replace(/,/g, '');
|
|
var n = parseFloat(text || 0);
|
|
return isNaN(n) ? null : String(n);
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.FieldTextInput.nonnegativeIntegerValidator = function(text) {
|
|
var n = Blockly.FieldTextInput.numberValidator(text);
|
|
if (n) {
|
|
n = String(Math.max(0, Math.floor(n)));
|
|
}
|
|
return n;
|
|
};
|
|
|
|
Blockly.Field.register('field_input', Blockly.FieldTextInput);
|
|
|