Add tabbing between text fields.

This commit is contained in:
Neil Fraser 2015-10-14 16:23:23 -07:00
parent c83faa3314
commit ded524ff3a
3 changed files with 49 additions and 3 deletions

View file

@ -1076,7 +1076,7 @@ Blockly.BlockSvg.prototype.getRelativeToSurfaceXY=function(){var a=0,b=0,c=this.
Blockly.BlockSvg.prototype.snapToGrid=function(){if(this.workspace&&0==Blockly.dragMode_&&!this.getParent()&&!this.isInFlyout&&this.workspace.options.gridOptions&&this.workspace.options.gridOptions.snap){var a=this.workspace.options.gridOptions.spacing,b=a/2,c=this.getRelativeToSurfaceXY(),d=Math.round((c.x-b)/a)*a+b-c.x,a=Math.round((c.y-b)/a)*a+b-c.y,d=Math.round(d),a=Math.round(a);0==d&&0==a||this.moveBy(d,a)}};
Blockly.BlockSvg.prototype.getHeightWidth=function(){var a=this.height,b=this.width,c=this.getNextBlock();c?(c=c.getHeightWidth(),a+=c.height-4,b=Math.max(b,c.width)):this.nextConnection||this.outputConnection||(a+=2);return{height:a,width:b}};
Blockly.BlockSvg.prototype.setCollapsed=function(a){if(this.collapsed_!=a){for(var b=[],c=0,d;d=this.inputList[c];c++)b.push.apply(b,d.setVisible(!a));if(a){d=this.getIcons();for(c=0;c<d.length;c++)d[c].setVisible(!1);c=this.toString(Blockly.COLLAPSE_CHARS);this.appendDummyInput("_TEMP_COLLAPSED_INPUT").appendField(c).init()}else this.removeInput("_TEMP_COLLAPSED_INPUT"),this.setWarningText(null);Blockly.BlockSvg.superClass_.setCollapsed.call(this,a);b.length||(b[0]=this);if(this.rendered)for(c=0;a=
b[c];c++)a.render();this.workspace.fireChangeEvent()}};
b[c];c++)a.render();this.workspace.fireChangeEvent()}};Blockly.BlockSvg.prototype.tab=function(a,b){for(var c=[],d=0,e;e=this.inputList[d];d++){for(var f=0,g;g=e.fieldRow[f];f++)g instanceof Blockly.FieldTextInput&&c.push(g);e.connection&&(e=e.connection.targetBlock())&&c.push(e)}d=c.indexOf(a);-1==d&&(d=b?-1:c.length);(c=c[b?d+1:d-1])?c instanceof Blockly.Field?c.showEditor_():c.tab(null,b):(c=this.getParent())&&c.tab(this,b)};
Blockly.BlockSvg.prototype.onMouseDown_=function(a){if(!this.isInFlyout){this.workspace.markFocused();Blockly.svgResize(this.workspace);Blockly.terminateDrag_();this.select();Blockly.hideChaff();if(Blockly.isRightButton(a))this.showContextMenu_(a);else if(this.isMovable()){Blockly.removeAllRanges();Blockly.Css.setCursor(Blockly.Css.Cursor.CLOSED);this.dragStartXY_=this.getRelativeToSurfaceXY();this.workspace.startDrag(a,this.dragStartXY_.x,this.dragStartXY_.y);Blockly.dragMode_=1;Blockly.BlockSvg.onMouseUpWrapper_=
Blockly.bindEvent_(document,"mouseup",this,this.onMouseUp_);Blockly.BlockSvg.onMouseMoveWrapper_=Blockly.bindEvent_(document,"mousemove",this,this.onMouseMove_);this.draggedBubbles_=[];for(var b=this.getDescendants(),c=0,d;d=b[c];c++){d=d.getIcons();for(var e=0;e<d.length;e++){var f=d[e].getIconLocation();f.bubble=d[e];this.draggedBubbles_.push(f)}}}else return;a.stopPropagation()}};
Blockly.BlockSvg.prototype.onMouseUp_=function(a){var b=this;Blockly.doCommand(function(){Blockly.terminateDrag_();if(Blockly.selected&&Blockly.highlightedConnection_)Blockly.localConnection_.connect(Blockly.highlightedConnection_),b.rendered&&(Blockly.localConnection_.isSuperior()?Blockly.highlightedConnection_:Blockly.localConnection_).sourceBlock_.connectionUiEffect(),b.workspace.trashcan&&b.workspace.trashcan.close();else if(!b.getParent()&&Blockly.selected.isDeletable()&&b.workspace.isDeleteArea(a)){var c=
@ -1142,7 +1142,7 @@ Blockly.BlockSvg.prototype.renderDrawLeft_=function(a,b,c,d){this.outputConnecti
Blockly.FieldTextInput.prototype.setText=function(a){if(null!==a){if(this.sourceBlock_&&this.changeHandler_){var b=this.changeHandler_(a);null!==b&&void 0!==b&&(a=b)}Blockly.Field.prototype.setText.call(this,a)}};Blockly.FieldTextInput.prototype.setSpellcheck=function(a){this.spellcheck_=a};
Blockly.FieldTextInput.prototype.showEditor_=function(a){var b=a||!1;if(!b&&(goog.userAgent.MOBILE||goog.userAgent.ANDROID||goog.userAgent.IPAD))a=window.prompt(Blockly.Msg.CHANGE_VALUE_TITLE,this.text_),this.sourceBlock_&&this.changeHandler_&&(b=this.changeHandler_(a),void 0!==b&&(a=b)),null!==a&&this.setText(a);else{Blockly.WidgetDiv.show(this,this.sourceBlock_.RTL,this.widgetDispose_());var c=Blockly.WidgetDiv.DIV;a=goog.dom.createDom("input","blocklyHtmlInput");a.setAttribute("spellcheck",this.spellcheck_);
var d=Blockly.FieldTextInput.FONTSIZE*this.sourceBlock_.workspace.scale+"pt";c.style.fontSize=d;a.style.fontSize=d;Blockly.FieldTextInput.htmlInput_=a;c.appendChild(a);a.value=a.defaultValue=this.text_;a.oldValue_=null;this.validate_();this.resizeEditor_();b||(a.focus(),a.select());a.onKeyDownWrapper_=Blockly.bindEvent_(a,"keydown",this,this.onHtmlInputKeyDown_);a.onKeyUpWrapper_=Blockly.bindEvent_(a,"keyup",this,this.onHtmlInputChange_);a.onKeyPressWrapper_=Blockly.bindEvent_(a,"keypress",this,this.onHtmlInputChange_);
b=this.sourceBlock_.workspace.getCanvas();a.onWorkspaceChangeWrapper_=Blockly.bindEvent_(b,"blocklyWorkspaceChange",this,this.resizeEditor_)}};Blockly.FieldTextInput.prototype.onHtmlInputKeyDown_=function(a){var b=Blockly.FieldTextInput.htmlInput_;13==a.keyCode?Blockly.WidgetDiv.hide():27==a.keyCode&&(this.setText(b.defaultValue),Blockly.WidgetDiv.hide())};
b=this.sourceBlock_.workspace.getCanvas();a.onWorkspaceChangeWrapper_=Blockly.bindEvent_(b,"blocklyWorkspaceChange",this,this.resizeEditor_)}};Blockly.FieldTextInput.prototype.onHtmlInputKeyDown_=function(a){var b=Blockly.FieldTextInput.htmlInput_;13==a.keyCode?Blockly.WidgetDiv.hide():27==a.keyCode?(this.setText(b.defaultValue),Blockly.WidgetDiv.hide()):9==a.keyCode&&(Blockly.WidgetDiv.hide(),this.sourceBlock_.tab(this,!a.shiftKey),a.preventDefault())};
Blockly.FieldTextInput.prototype.onHtmlInputChange_=function(a){var b=Blockly.FieldTextInput.htmlInput_;27!=a.keyCode&&(a=b.value,a!==b.oldValue_?(b.oldValue_=a,this.setText(a),this.validate_()):goog.userAgent.WEBKIT&&this.sourceBlock_.render())};
Blockly.FieldTextInput.prototype.validate_=function(){var a=!0;goog.asserts.assertObject(Blockly.FieldTextInput.htmlInput_);var b=Blockly.FieldTextInput.htmlInput_;this.sourceBlock_&&this.changeHandler_&&(a=this.changeHandler_(b.value));null===a?Blockly.addClass_(b,"blocklyInvalidInput"):Blockly.removeClass_(b,"blocklyInvalidInput")};
Blockly.FieldTextInput.prototype.resizeEditor_=function(){var a=Blockly.WidgetDiv.DIV,b=this.fieldGroup_.getBBox();a.style.width=b.width*this.sourceBlock_.workspace.scale+"px";a.style.height=b.height*this.sourceBlock_.workspace.scale+"px";b=this.getAbsoluteXY_();if(this.sourceBlock_.RTL){var c=this.getScaledBBox_();b.x+=c.width;b.x-=a.offsetWidth}b.y+=1;goog.userAgent.GECKO&&Blockly.WidgetDiv.DIV.style.top&&(--b.x,--b.y);goog.userAgent.WEBKIT&&(b.y-=3);a.style.left=b.x+"px";a.style.top=b.y+"px"};

View file

@ -378,6 +378,48 @@ Blockly.BlockSvg.prototype.setCollapsed = function(collapsed) {
this.workspace.fireChangeEvent();
};
/**
* Open the next (or previous) FieldTextInput.
* @param {Blockly.Field|Blockly.Block} start Current location.
* @param {boolean} forward If true go forward, otherwise backward.
*/
Blockly.BlockSvg.prototype.tab = function(start, forward) {
// This function need not be efficient since it runs once on a keypress.
// Create an ordered list of all text fields and connected inputs.
var list = [];
for (var i = 0, input; input = this.inputList[i]; i++) {
for (var j = 0, field; field = input.fieldRow[j]; j++) {
if (field instanceof Blockly.FieldTextInput) {
// TODO: Also support dropdown fields.
list.push(field);
}
}
if (input.connection) {
var block = input.connection.targetBlock();
if (block) {
list.push(block);
}
}
}
var i = list.indexOf(start);
if (i == -1) {
// No start location, start at the beginning or end.
i = forward ? -1 : list.length;
}
var target = list[forward ? i + 1 : i - 1];
if (!target) {
// Ran off of list.
var parent = this.getParent();
if (parent) {
parent.tab(this, forward);
}
} else if (target instanceof Blockly.Field) {
target.showEditor_();
} else {
target.tab(null, forward);
}
};
/**
* Handle a mouse-down on an SVG block.
* @param {!Event} e Mouse down event.

View file

@ -170,12 +170,16 @@ Blockly.FieldTextInput.prototype.showEditor_ = function(opt_quietInput) {
*/
Blockly.FieldTextInput.prototype.onHtmlInputKeyDown_ = function(e) {
var htmlInput = Blockly.FieldTextInput.htmlInput_;
var enterKey = 13, escKey = 27;
var tabKey = 9, enterKey = 13, escKey = 27;
if (e.keyCode == enterKey) {
Blockly.WidgetDiv.hide();
} else if (e.keyCode == escKey) {
this.setText(htmlInput.defaultValue);
Blockly.WidgetDiv.hide();
} else if (e.keyCode == tabKey) {
Blockly.WidgetDiv.hide();
this.sourceBlock_.tab(this, !e.shiftKey);
e.preventDefault();
}
};