From 07d662462585bcb089d4f8b82bfb4d385a0a1bc2 Mon Sep 17 00:00:00 2001 From: Karishma Chadha Date: Wed, 22 Nov 2017 12:50:00 -0500 Subject: [PATCH 01/12] Fixing bug where VarCreate event does not fire when adding a block with pre-existing variables from the flyout into the workspace. --- core/flyout_base.js | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/core/flyout_base.js b/core/flyout_base.js index 1ec79c31b..7e01a6b10 100644 --- a/core/flyout_base.js +++ b/core/flyout_base.js @@ -606,6 +606,7 @@ Blockly.Flyout.prototype.onMouseDown_ = function(e) { Blockly.Flyout.prototype.createBlock = function(originalBlock) { var newBlock = null; Blockly.Events.disable(); + var variablesBeforeCreation = this.workspace_.getAllVariables(); this.targetWorkspace_.setResizesEnabled(false); try { newBlock = this.placeNewBlock_(originalBlock); @@ -615,9 +616,28 @@ Blockly.Flyout.prototype.createBlock = function(originalBlock) { Blockly.Events.enable(); } + var variablesAfterCreation = this.workspace_.getAllVariables(); + var variablesToFireVarCreate = []; + if (variablesBeforeCreation.length != variablesAfterCreation.length) { + for (var i = 0; i < variablesAfterCreation.length; i++) { + var variable = variablesAfterCreation[i]; + // for any variable that is present in the list of variables + // after creation but is not present in the list of variables before + // creation, add the variable to the list we will traverse to + // fire the VarCreate event + if (!variablesBeforeCreation.includes(variable)) { + variablesToFireVarCreate.push(variable); + } + } + } + if (Blockly.Events.isEnabled()) { Blockly.Events.setGroup(true); Blockly.Events.fire(new Blockly.Events.Create(newBlock)); + for(var i = 0; i < variablesToFireVarCreate.length; i++) { + var thisVariable = variablesToFireVarCreate[i]; + Blockly.Events.fire(new Blockly.Events.VarCreate(thisVariable)); + } } if (this.autoClose) { this.hide(); From 71205de2efa94f996d80adec1c5d3a96365a1ed2 Mon Sep 17 00:00:00 2001 From: Karishma Chadha Date: Wed, 22 Nov 2017 13:27:29 -0500 Subject: [PATCH 02/12] Target workspace is the one that carries the variables we want. VarCreate gets fired when a block with a new default variable is dragged out into the workspace. --- core/flyout_base.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core/flyout_base.js b/core/flyout_base.js index 7e01a6b10..84919a7e4 100644 --- a/core/flyout_base.js +++ b/core/flyout_base.js @@ -606,7 +606,7 @@ Blockly.Flyout.prototype.onMouseDown_ = function(e) { Blockly.Flyout.prototype.createBlock = function(originalBlock) { var newBlock = null; Blockly.Events.disable(); - var variablesBeforeCreation = this.workspace_.getAllVariables(); + var variablesBeforeCreation = this.targetWorkspace_.getAllVariables(); this.targetWorkspace_.setResizesEnabled(false); try { newBlock = this.placeNewBlock_(originalBlock); @@ -616,7 +616,7 @@ Blockly.Flyout.prototype.createBlock = function(originalBlock) { Blockly.Events.enable(); } - var variablesAfterCreation = this.workspace_.getAllVariables(); + var variablesAfterCreation = this.targetWorkspace_.getAllVariables(); var variablesToFireVarCreate = []; if (variablesBeforeCreation.length != variablesAfterCreation.length) { for (var i = 0; i < variablesAfterCreation.length; i++) { From c950225897135c0d3e284e493d29baa1737c14c5 Mon Sep 17 00:00:00 2001 From: Karishma Chadha Date: Wed, 22 Nov 2017 13:37:30 -0500 Subject: [PATCH 03/12] Minor refactoring --- core/flyout_base.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/core/flyout_base.js b/core/flyout_base.js index 84919a7e4..b05b30134 100644 --- a/core/flyout_base.js +++ b/core/flyout_base.js @@ -617,7 +617,7 @@ Blockly.Flyout.prototype.createBlock = function(originalBlock) { } var variablesAfterCreation = this.targetWorkspace_.getAllVariables(); - var variablesToFireVarCreate = []; + var newVariables = []; if (variablesBeforeCreation.length != variablesAfterCreation.length) { for (var i = 0; i < variablesAfterCreation.length; i++) { var variable = variablesAfterCreation[i]; @@ -626,7 +626,7 @@ Blockly.Flyout.prototype.createBlock = function(originalBlock) { // creation, add the variable to the list we will traverse to // fire the VarCreate event if (!variablesBeforeCreation.includes(variable)) { - variablesToFireVarCreate.push(variable); + newVariables.push(variable); } } } @@ -634,8 +634,8 @@ Blockly.Flyout.prototype.createBlock = function(originalBlock) { if (Blockly.Events.isEnabled()) { Blockly.Events.setGroup(true); Blockly.Events.fire(new Blockly.Events.Create(newBlock)); - for(var i = 0; i < variablesToFireVarCreate.length; i++) { - var thisVariable = variablesToFireVarCreate[i]; + for(var i = 0; i < newVariables.length; i++) { + var thisVariable = newVariables[i]; Blockly.Events.fire(new Blockly.Events.VarCreate(thisVariable)); } } From ee142e92bbf0437eb9a261723cd2b4c1efb1bfad Mon Sep 17 00:00:00 2001 From: Karishma Chadha Date: Wed, 22 Nov 2017 13:43:25 -0500 Subject: [PATCH 04/12] Fixing comment style. --- core/flyout_base.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core/flyout_base.js b/core/flyout_base.js index b05b30134..fe9a17bcf 100644 --- a/core/flyout_base.js +++ b/core/flyout_base.js @@ -621,10 +621,10 @@ Blockly.Flyout.prototype.createBlock = function(originalBlock) { if (variablesBeforeCreation.length != variablesAfterCreation.length) { for (var i = 0; i < variablesAfterCreation.length; i++) { var variable = variablesAfterCreation[i]; - // for any variable that is present in the list of variables + // For any variable that is present in the list of variables // after creation but is not present in the list of variables before // creation, add the variable to the list we will traverse to - // fire the VarCreate event + // fire the VarCreate event. if (!variablesBeforeCreation.includes(variable)) { newVariables.push(variable); } From aae1b1bb74c8dfd658cfce3cfb10c390353390a3 Mon Sep 17 00:00:00 2001 From: Karishma Chadha Date: Wed, 22 Nov 2017 14:51:11 -0500 Subject: [PATCH 05/12] Addressing PR comment. Moving functionality to get newly added variables into a helper function. --- core/flyout_base.js | 43 +++++++++++++++++++++++++++++-------------- 1 file changed, 29 insertions(+), 14 deletions(-) diff --git a/core/flyout_base.js b/core/flyout_base.js index fe9a17bcf..305be33e0 100644 --- a/core/flyout_base.js +++ b/core/flyout_base.js @@ -596,6 +596,33 @@ Blockly.Flyout.prototype.onMouseDown_ = function(e) { } }; +/** + * Helper function to get the list of variables that have been added to the + * workspace after adding a new block, using the given list of variables that + * were in the workspace before the new block was added. + * @param {!Array.} originalVariables The array of + * variables that existed in the workspace before adding the new block. + * @return {!Array.} The new array of variables that were + * freshly added to the workspace after creating the new block, or [] if no + * new variables were added to the workspace. + * @private + */ +Blockly.Flyout.prototype.getAddedVariables_ = function(originalVariables) { + var allCurrentVariables = this.targetWorkspace_.getAllVariables(); + var addedVariables = []; + if (originalVariables.length != allCurrentVariables.length) { + for (var i = 0; i < allCurrentVariables.length; i++) { + var variable = allCurrentVariables[i]; + // For any variable that is present in allCurrentVariables but not + // present in originalVariables, add the variable to addedVariables. + if (!originalVariables.includes(variable)) { + addedVariables.push(variable); + } + } + } + return addedVariables; +}; + /** * Create a copy of this block on the workspace. * @param {!Blockly.BlockSvg} originalBlock The block to copy from the flyout. @@ -616,24 +643,12 @@ Blockly.Flyout.prototype.createBlock = function(originalBlock) { Blockly.Events.enable(); } - var variablesAfterCreation = this.targetWorkspace_.getAllVariables(); - var newVariables = []; - if (variablesBeforeCreation.length != variablesAfterCreation.length) { - for (var i = 0; i < variablesAfterCreation.length; i++) { - var variable = variablesAfterCreation[i]; - // For any variable that is present in the list of variables - // after creation but is not present in the list of variables before - // creation, add the variable to the list we will traverse to - // fire the VarCreate event. - if (!variablesBeforeCreation.includes(variable)) { - newVariables.push(variable); - } - } - } + var newVariables = this.getAddedVariables_(variablesBeforeCreation); if (Blockly.Events.isEnabled()) { Blockly.Events.setGroup(true); Blockly.Events.fire(new Blockly.Events.Create(newBlock)); + // Fire a VarCreate event for each (if any) new variable created. for(var i = 0; i < newVariables.length; i++) { var thisVariable = newVariables[i]; Blockly.Events.fire(new Blockly.Events.VarCreate(thisVariable)); From 143d95222c66267f4290ba7395e18225116d2fe5 Mon Sep 17 00:00:00 2001 From: Karishma Chadha Date: Wed, 22 Nov 2017 15:22:34 -0500 Subject: [PATCH 06/12] Fixing type annotation. --- core/flyout_base.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core/flyout_base.js b/core/flyout_base.js index 305be33e0..c56ea6253 100644 --- a/core/flyout_base.js +++ b/core/flyout_base.js @@ -600,9 +600,9 @@ Blockly.Flyout.prototype.onMouseDown_ = function(e) { * Helper function to get the list of variables that have been added to the * workspace after adding a new block, using the given list of variables that * were in the workspace before the new block was added. - * @param {!Array.} originalVariables The array of + * @param {!Array.} originalVariables The array of * variables that existed in the workspace before adding the new block. - * @return {!Array.} The new array of variables that were + * @return {!Array.} The new array of variables that were * freshly added to the workspace after creating the new block, or [] if no * new variables were added to the workspace. * @private From d73337ad7383596983c426f7292abd73bdaeaede Mon Sep 17 00:00:00 2001 From: Rachel Fenichel Date: Thu, 30 Nov 2017 11:24:57 -0800 Subject: [PATCH 07/12] Extract fieldToDom into its own function --- core/xml.js | 65 +++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 46 insertions(+), 19 deletions(-) diff --git a/core/xml.js b/core/xml.js index 82e4040b4..7ca622662 100644 --- a/core/xml.js +++ b/core/xml.js @@ -86,6 +86,50 @@ Blockly.Xml.blockToDomWithXY = function(block, opt_noId) { return element; }; +/** + * Encode a field as XML. + * @param {!Blockly.Field} field The field to encode. + * @param {!Blockly.Workspace} workspace The workspace that the field is in. + * @return {?Element} XML element, or null if the field did not need to be + * serialized. + * @private + */ +Blockly.Xml.fieldToDom_ = function(field, workspace) { + if (field.name && field.EDITABLE) { + var container = goog.dom.createDom('field', null, field.getValue()); + container.setAttribute('name', field.name); + if (field instanceof Blockly.FieldVariable) { + var variable = workspace.getVariable(field.getValue()); + if (variable) { + container.setAttribute('id', variable.getId()); + container.setAttribute('variabletype', variable.type); + } + } + return container; + } + return null; +}; + +/** + * Encode all of a block's fields as XML and attach them to the given tree of + * XML elements. + * @param {!Blockly.Block} block A block with fields to be encoded. + * @param {!Element} element The XML element to which the field DOM should be + * attached. + * @private + */ +Blockly.Xml.allFieldsToDom_ = function(block, element) { + var workspace = block.workspace; + for (var i = 0, input; input = block.inputList[i]; i++) { + for (var j = 0, field; field = input.fieldRow[j]; j++) { + var fieldDom = Blockly.Xml.fieldToDom_(field, workspace); + if (fieldDom) { + element.appendChild(fieldDom); + } + } + } +}; + /** * Encode a block subtree as XML. * @param {!Blockly.Block} block The root block to encode. @@ -105,25 +149,8 @@ Blockly.Xml.blockToDom = function(block, opt_noId) { element.appendChild(mutation); } } - function fieldToDom(field) { - if (field.name && field.EDITABLE) { - var container = goog.dom.createDom('field', null, field.getValue()); - container.setAttribute('name', field.name); - if (field instanceof Blockly.FieldVariable) { - var variable = block.workspace.getVariable(field.getValue()); - if (variable) { - container.setAttribute('id', variable.getId()); - container.setAttribute('variabletype', variable.type); - } - } - element.appendChild(container); - } - } - for (var i = 0, input; input = block.inputList[i]; i++) { - for (var j = 0, field; field = input.fieldRow[j]; j++) { - fieldToDom(field); - } - } + + Blockly.Xml.allFieldsToDom_(block, element); var commentText = block.getCommentText(); if (commentText) { From 222407c6ce5a07453fed7f5986e1a6aa033239d5 Mon Sep 17 00:00:00 2001 From: Rachel Fenichel Date: Thu, 30 Nov 2017 12:08:01 -0800 Subject: [PATCH 08/12] Extract code for domToField_ --- core/xml.js | 62 ++++++++++++++++++++++++++++++++--------------------- 1 file changed, 37 insertions(+), 25 deletions(-) diff --git a/core/xml.js b/core/xml.js index 7ca622662..bf0d9e321 100644 --- a/core/xml.js +++ b/core/xml.js @@ -610,31 +610,7 @@ Blockly.Xml.domToBlockHeadless_ = function(xmlBlock, workspace) { // Titles were renamed to field in December 2013. // Fall through. case 'field': - var field = block.getField(name); - var text = xmlChild.textContent; - if (field instanceof Blockly.FieldVariable) { - // TODO (marisaleung): When we change setValue and getValue to - // interact with IDs instead of names, update this so that we get - // the variable based on ID instead of textContent. - var type = xmlChild.getAttribute('variabletype') || ''; - var variable = workspace.getVariable(text); - if (!variable) { - variable = workspace.createVariable(text, type, - xmlChild.getAttribute(id)); - } - if (type != null && type !== variable.type) { - throw Error('Serialized variable type with id \'' + - variable.getId() + '\' had type ' + variable.type + ', and ' + - 'does not match variable field that references it: ' + - Blockly.Xml.domToText(xmlChild) + '.'); - } - } - if (!field) { - console.warn('Ignoring non-existent field ' + name + ' in block ' + - prototypeName); - break; - } - field.setValue(text); + Blockly.Xml.domToField_(block, name, xmlChild); break; case 'value': case 'statement': @@ -722,6 +698,42 @@ Blockly.Xml.domToBlockHeadless_ = function(xmlBlock, workspace) { return block; }; +/** + * Decode an XML field tag and set the value of that field on the given block. + * @param {!Blockly.Block} block The block that is currently being deserialized. + * @param {string} fieldName The name of the field on the block. + * @param {!Element} xml The field tag to decode. + * @private + */ +Blockly.Xml.domToField_ = function(block, fieldName, xml) { + var field = block.getField(fieldName); + if (!field) { + console.warn('Ignoring non-existent field ' + fieldName + ' in block ' + + block.type); + return; + } + + var text = xml.textContent; + if (field instanceof Blockly.FieldVariable) { + // TODO (#1199): When we change setValue and getValue to + // interact with IDs instead of names, update this so that we get + // the variable based on ID instead of textContent. + var type = xml.getAttribute('variabletype') || ''; + var variable = block.workspace.getVariable(text); + if (!variable) { + variable = block.workspace.createVariable(text, type, + xml.getAttribute('id')); + } + if (type != null && type !== variable.type) { + throw Error('Serialized variable type with id \'' + + variable.getId() + '\' had type ' + variable.type + ', and ' + + 'does not match variable field that references it: ' + + Blockly.Xml.domToText(xml) + '.'); + } + } + field.setValue(text); +}; + /** * Remove any 'next' block (statements in a stack). * @param {!Element} xmlBlock XML block element. From 2dd8c1f669c9941709556cc233d8435d827f8149 Mon Sep 17 00:00:00 2001 From: Sandeep Dubey Date: Sun, 3 Dec 2017 02:40:11 +0530 Subject: [PATCH 09/12] Made the changes asked by the reviewer --- core/css.js | 3 +++ core/field_dropdown.js | 10 ++++++++++ 2 files changed, 13 insertions(+) diff --git a/core/css.js b/core/css.js index d5074b0e3..134b31dea 100644 --- a/core/css.js +++ b/core/css.js @@ -499,6 +499,9 @@ Blockly.Css.CONTENT = [ '.blocklyDropdownMenu {', 'padding: 0 !important;', + /* max-height value is same as the constant + * Blockly.FieldDropdown.MAX_MENU_HEIGHT defined in field_dropdown.js. */ + 'max-height: 300px !important;', '}', /* Override the default Closure URL. */ diff --git a/core/field_dropdown.js b/core/field_dropdown.js index d70212186..2f9c11e61 100644 --- a/core/field_dropdown.js +++ b/core/field_dropdown.js @@ -68,6 +68,12 @@ goog.inherits(Blockly.FieldDropdown, Blockly.Field); */ Blockly.FieldDropdown.CHECKMARK_OVERHANG = 25; +/** + * Maximum height of the dropdown menu,it's also referenced in css.js as + * part of .blocklyDropdownMenu. + */ +Blockly.FieldDropdown.MAX_MENU_HEIGHT = 300; + /** * Android can't (in 2014) display "▾", so use "▼" instead. */ @@ -239,6 +245,10 @@ Blockly.FieldDropdown.prototype.positionMenu_ = function(menu) { this.createWidget_(menu); var menuSize = Blockly.utils.uiMenu.getSize(menu); + if (menuSize.height > Blockly.FieldDropdown.MAX_MENU_HEIGHT) { + menuSize.height = Blockly.FieldDropdown.MAX_MENU_HEIGHT; + } + if (this.sourceBlock_.RTL) { Blockly.utils.uiMenu.adjustBBoxesForRTL(viewportBBox, anchorBBox, menuSize); } From d1886572d087a47deb6e3668b2f15dc014c5bdb1 Mon Sep 17 00:00:00 2001 From: picklesrus Date: Mon, 4 Dec 2017 10:36:47 -0800 Subject: [PATCH 10/12] =?UTF-8?q?Fix=20#1487=20and=20only=20add=20the=20de?= =?UTF-8?q?lete=20cursor=20if=20the=20block=20is=20not=20deletabl=E2=80=A6?= =?UTF-8?q?=20(#1491)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Fix #1487 and only add the delete cursor if the block is not deletable. Also add a grab cursor when it is not deletable so the cursor keeps the grab hand when over the toolbox. This changes the toolbox api slightly, but the methods it touches are @package so it should be okay. --- core/block_dragger.js | 8 ++++++-- core/css.js | 6 ++++++ core/toolbox.js | 14 ++++++++------ 3 files changed, 20 insertions(+), 8 deletions(-) diff --git a/core/block_dragger.js b/core/block_dragger.js index 0ba1749f0..c6a0a3b1a 100644 --- a/core/block_dragger.js +++ b/core/block_dragger.js @@ -168,7 +168,9 @@ Blockly.BlockDragger.prototype.startBlockDrag = function(currentDragDeltaXY) { this.draggingBlock_.moveToDragSurface_(); if (this.workspace_.toolbox_) { - this.workspace_.toolbox_.addDeleteStyle(); + var style = this.draggingBlock_.isDeletable() ? 'blocklyToolboxDelete' : + 'blocklyToolboxGrab'; + this.workspace_.toolbox_.addStyle(style); } }; @@ -224,7 +226,9 @@ Blockly.BlockDragger.prototype.endBlockDrag = function(e, currentDragDeltaXY) { this.workspace_.setResizesEnabled(true); if (this.workspace_.toolbox_) { - this.workspace_.toolbox_.removeDeleteStyle(); + var style = this.draggingBlock_.isDeletable() ? 'blocklyToolboxDelete' : + 'blocklyToolboxGrab'; + this.workspace_.toolbox_.removeStyle(style); } Blockly.Events.setGroup(false); }; diff --git a/core/css.js b/core/css.js index d5074b0e3..21fd9805e 100644 --- a/core/css.js +++ b/core/css.js @@ -253,6 +253,12 @@ Blockly.Css.CONTENT = [ 'cursor: url("<<>>/handdelete.cur"), auto;', '}', + '.blocklyToolboxGrab {', + 'cursor: url("<<>>/handclosed.cur"), auto;', + 'cursor: grabbing;', + 'cursor: -webkit-grabbing;', + '}', + '.blocklyDragging>.blocklyPath,', '.blocklyDragging>.blocklyPathLight {', 'fill-opacity: .8;', diff --git a/core/toolbox.js b/core/toolbox.js index 5531f1c4a..0a07fddba 100644 --- a/core/toolbox.js +++ b/core/toolbox.js @@ -422,21 +422,23 @@ Blockly.Toolbox.prototype.clearSelection = function() { }; /** - * Adds styles on the toolbox indicating blocks will be deleted. + * Adds a style on the toolbox. Usually used to change the cursor. + * @param {string} style The name of the class to add. * @package */ -Blockly.Toolbox.prototype.addDeleteStyle = function() { +Blockly.Toolbox.prototype.addStyle = function(style) { Blockly.utils.addClass(/** @type {!Element} */ (this.HtmlDiv), - 'blocklyToolboxDelete'); + style); }; /** - * Remove styles from the toolbox that indicate blocks will be deleted. + * Removes a style from the toolbox. Usually used to change the cursor. + * @param {string} style The name of the class to remove. * @package */ -Blockly.Toolbox.prototype.removeDeleteStyle = function() { +Blockly.Toolbox.prototype.removeStyle = function(style) { Blockly.utils.removeClass(/** @type {!Element} */ (this.HtmlDiv), - 'blocklyToolboxDelete'); + style); }; /** From 7ef19b6851227217b2e8e73b932d0a18c1766ae1 Mon Sep 17 00:00:00 2001 From: Rachel Fenichel Date: Wed, 6 Dec 2017 17:00:22 -0800 Subject: [PATCH 11/12] Fix #1467 --- core/field_image.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/core/field_image.js b/core/field_image.js index 8ee57bc6a..6c2c11793 100644 --- a/core/field_image.js +++ b/core/field_image.js @@ -175,6 +175,13 @@ Blockly.FieldImage.prototype.render_ = function() { // NOP }; +/** + * Images are fixed width, no need to render even if forced. + */ +Blockly.FieldImage.prototype.forceRerender = function() { + // NOP +}; + /** * Images are fixed width, no need to update. * @private From 492462e1d4ebcdb405594434ddbfac2000fe7c90 Mon Sep 17 00:00:00 2001 From: Tianwei Du Date: Sat, 9 Dec 2017 00:53:24 +0800 Subject: [PATCH 12/12] Develop (#1481) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * variables_dynamic * make test , and i18n * Issue #1470 * Blockly.Constants.VariablesDynamic.CUSTOM_CONTEXT_MENU_VARIABLE_MUTATION to make sure setCheck before event trigger * Update built files from develop. * fix copyright ,quotes , comments , and optimize some flow. * quotes,change UI , change category custom name * single setter , remove mutator , colour typo , typed variables create button message * undo the indent edits. --- blocks/variables.js | 3 + blocks/variables_dynamic.js | 138 ++++++++++++++++++++++++++++++++++++ core/constants.js | 7 ++ core/field_variable.js | 12 ++-- core/variables_dynamic.js | 116 ++++++++++++++++++++++++++++++ core/workspace_svg.js | 2 + msg/messages.js | 10 +++ tests/playground.html | 2 + 8 files changed, 284 insertions(+), 6 deletions(-) create mode 100644 blocks/variables_dynamic.js create mode 100644 core/variables_dynamic.js diff --git a/blocks/variables.js b/blocks/variables.js index 1ec3825a4..1caf14834 100644 --- a/blocks/variables.js +++ b/blocks/variables.js @@ -103,6 +103,9 @@ Blockly.Constants.Variables.CUSTOM_CONTEXT_MENU_VARIABLE_GETTER_SETTER_MIXIN = { * @this Blockly.Block */ customContextMenu: function(options) { + if(this.isInFlyout){ + return; + } // Getter blocks have the option to create a setter block, and vice versa. if (this.type == 'variables_get') { var opposite_type = 'variables_set'; diff --git a/blocks/variables_dynamic.js b/blocks/variables_dynamic.js new file mode 100644 index 000000000..5d523f7e3 --- /dev/null +++ b/blocks/variables_dynamic.js @@ -0,0 +1,138 @@ +/** + * @license + * Visual Blocks Editor + * + * Copyright 2017 Google Inc. + * https://developers.google.com/blockly/ + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * @fileoverview Variable blocks for Blockly. + + * This file is scraped to extract a .json file of block definitions. The array + * passed to defineBlocksWithJsonArray(..) must be strict JSON: double quotes + * only, no outside references, no functions, no trailing commas, etc. The one + * exception is end-of-line comments, which the scraper will remove. + * @author duzc2dtw@gmail.com (Du Tian Wei) + */ +'use strict'; + +goog.provide('Blockly.Constants.VariablesDynamic'); + +goog.require('Blockly.Blocks'); +goog.require('Blockly'); + + +/** + * Common HSV hue for all blocks in this category. + * Should be the same as Blockly.Msg.VARIABLES_DYNAMIC_HUE. + * @readonly + */ +Blockly.Constants.VariablesDynamic.HUE = 310; + +Blockly.defineBlocksWithJsonArray([ // BEGIN JSON EXTRACT + // Block for variable getter. + { + "type": "variables_get_dynamic", + "message0": "%1", + "args0": [{ + "type": "field_variable", + "name": "VAR", + "variable": "%{BKY_VARIABLES_DEFAULT_NAME}" + }], + "output": null, + "colour": "%{BKY_VARIABLES_DYNAMIC_HUE}", + "helpUrl": "%{BKY_VARIABLES_GET_HELPURL}", + "tooltip": "%{BKY_VARIABLES_GET_TOOLTIP}", + "extensions": ["contextMenu_variableDynamicSetterGetter"] + }, + // Block for variable setter. + { + "type": "variables_set_dynamic", + "message0": "%{BKY_VARIABLES_SET}", + "args0": [{ + "type": "field_variable", + "name": "VAR", + "variable": "%{BKY_VARIABLES_DEFAULT_NAME}" + }, + { + "type": "input_value", + "name": "VALUE" + } + ], + "previousStatement": null, + "nextStatement": null, + "colour": "%{BKY_VARIABLES_DYNAMIC_HUE}", + "tooltip": "%{BKY_VARIABLES_SET_TOOLTIP}", + "helpUrl": "%{BKY_VARIABLES_SET_HELPURL}", + "extensions": ["contextMenu_variableDynamicSetterGetter"] + } +]); // END JSON EXTRACT (Do not delete this comment.) + +/** + * Mixin to add context menu items to create getter/setter blocks for this + * setter/getter. + * Used by blocks 'variables_set_dynamic' and 'variables_get_dynamic'. + * @mixin + * @augments Blockly.Block + * @package + * @readonly + */ +Blockly.Constants.VariablesDynamic.CUSTOM_CONTEXT_MENU_VARIABLE_GETTER_SETTER_MIXIN = { + /** + * Add menu option to create getter/setter block for this setter/getter. + * @param {!Array} options List of menu options to add to. + * @this Blockly.Block + */ + customContextMenu: function(options) { + // Getter blocks have the option to create a setter block, and vice versa. + if(this.isInFlyout){ + return; + } + var opposite_type ; + var contextMenuMsg ; + if (this.type == 'variables_get_dynamic') { + opposite_type = 'variables_set_dynamic'; + contextMenuMsg = Blockly.Msg.VARIABLES_GET_CREATE_SET; + } else { + opposite_type = 'variables_get_dynamic'; + contextMenuMsg = Blockly.Msg.VARIABLES_SET_CREATE_GET; + } + + var option = { enabled: this.workspace.remainingCapacity() > 0 }; + var name = this.getFieldValue('VAR'); + option.text = contextMenuMsg.replace('%1', name); + var xmlField = goog.dom.createDom('field', null, name); + xmlField.setAttribute('name', 'VAR'); + var variableModel = this.workspace.getVariable(name); + xmlField.setAttribute('variabletype', variableModel.type); + var xmlBlock = goog.dom.createDom('block', null, xmlField); + xmlBlock.setAttribute('type', opposite_type); + option.callback = Blockly.ContextMenu.callbackFactory(this, xmlBlock); + options.push(option); + }, + onchange: function() { + var name = this.getFieldValue('VAR'); + var variableModel = this.workspace.getVariable(name); + if (this.type == 'variables_get_dynamic') { + this.outputConnection.setCheck(variableModel.type); + } else { + this.getInput('VALUE').connection.setCheck(variableModel.type); + } + } +}; + +Blockly.Extensions.registerMixin('contextMenu_variableDynamicSetterGetter', + Blockly.Constants.VariablesDynamic.CUSTOM_CONTEXT_MENU_VARIABLE_GETTER_SETTER_MIXIN); diff --git a/core/constants.js b/core/constants.js index f5428ff74..54ed1fb49 100644 --- a/core/constants.js +++ b/core/constants.js @@ -241,6 +241,13 @@ Blockly.DELETE_AREA_TOOLBOX = 2; * @const {string} */ Blockly.VARIABLE_CATEGORY_NAME = 'VARIABLE'; +/** + * String for use in the "custom" attribute of a category in toolbox xml. + * This string indicates that the category should be dynamically populated with + * variable blocks. + * @const {string} + */ +Blockly.VARIABLE_DYNAMIC_CATEGORY_NAME = 'VARIABLE_DYNAMIC'; /** * String for use in the "custom" attribute of a category in toolbox xml. diff --git a/core/field_variable.js b/core/field_variable.js index 3fa227c39..4dc071ec0 100644 --- a/core/field_variable.js +++ b/core/field_variable.js @@ -73,8 +73,8 @@ Blockly.FieldVariable.prototype.initModel = function() { // Variables without names get uniquely named for this workspace. var workspace = this.sourceBlock_.isInFlyout ? - this.sourceBlock_.workspace.targetWorkspace : - this.sourceBlock_.workspace; + this.sourceBlock_.workspace.targetWorkspace : + this.sourceBlock_.workspace; this.setValue(Blockly.Variables.generateUniqueName(workspace)); } // If the selected variable doesn't exist yet, create it. @@ -91,7 +91,7 @@ Blockly.FieldVariable.prototype.initModel = function() { */ Blockly.FieldVariable.prototype.setSourceBlock = function(block) { goog.asserts.assert(!block.isShadow(), - 'Variable fields are not allowed to exist on shadow blocks.'); + 'Variable fields are not allowed to exist on shadow blocks.'); Blockly.FieldVariable.superClass_.setSourceBlock.call(this, block); }; @@ -124,7 +124,7 @@ Blockly.FieldVariable.prototype.setValue = function(value) { } if (Blockly.Events.isEnabled()) { Blockly.Events.fire(new Blockly.Events.BlockChange( - this.sourceBlock_, 'field', this.name, this.value_, newValue)); + this.sourceBlock_, 'field', this.name, this.value_, newValue)); } } this.value_ = newValue; @@ -139,7 +139,7 @@ Blockly.FieldVariable.prototype.setValue = function(value) { */ Blockly.FieldVariable.prototype.getVariableTypes_ = function() { var variableTypes = this.variableTypes; - if (variableTypes === null) { + if (variableTypes === null || variableTypes === undefined) { // If variableTypes is null, return all variable types. if (this.sourceBlock_) { var workspace = this.sourceBlock_.workspace; @@ -203,7 +203,7 @@ Blockly.FieldVariable.dropdownCreate = function() { options.push([Blockly.Msg.RENAME_VARIABLE, Blockly.RENAME_VARIABLE_ID]); if (Blockly.Msg.DELETE_VARIABLE) { options.push([Blockly.Msg.DELETE_VARIABLE.replace('%1', name), - Blockly.DELETE_VARIABLE_ID]); + Blockly.DELETE_VARIABLE_ID]); } return options; }; diff --git a/core/variables_dynamic.js b/core/variables_dynamic.js new file mode 100644 index 000000000..fb114fae0 --- /dev/null +++ b/core/variables_dynamic.js @@ -0,0 +1,116 @@ +/** + * @license + * Visual Blocks Editor + * + * Copyright 2017 Google Inc. + * https://developers.google.com/blockly/ + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * @fileoverview Utility functions for handling variables dynamic. + * + * @author duzc2dtw@gmail.com (Du Tian Wei) + */ +'use strict'; + +goog.provide('Blockly.VariablesDynamic'); + +goog.require('Blockly.Variables'); +goog.require('Blockly.Blocks'); +goog.require('Blockly.constants'); +goog.require('Blockly.VariableModel'); +goog.require('Blockly.Workspace'); +goog.require('goog.string'); + + +Blockly.VariablesDynamic.onCreateVariableButtonClick_String = function(button) { + Blockly.Variables.createVariable(button.getTargetWorkspace(), null, 'String'); +}; +Blockly.VariablesDynamic.onCreateVariableButtonClick_Number = function(button) { + Blockly.Variables.createVariable(button.getTargetWorkspace(), null, 'Number'); +}; +Blockly.VariablesDynamic.onCreateVariableButtonClick_Colour = function(button) { + Blockly.Variables.createVariable(button.getTargetWorkspace(), null, 'Colour'); +}; +/** + * Construct the elements (blocks and button) required by the flyout for the + * variable category. + * @param {!Blockly.Workspace} workspace The workspace contianing variables. + * @return {!Array.} Array of XML elements. + */ +Blockly.VariablesDynamic.flyoutCategory = function(workspace) { + var xmlList = []; + var button = goog.dom.createDom('button'); + button.setAttribute('text', Blockly.Msg.NEW_STRING_VARIABLE); + button.setAttribute('callbackKey', 'CREATE_VARIABLE_STRING'); + xmlList.push(button); + button = goog.dom.createDom('button'); + button.setAttribute('text', Blockly.Msg.NEW_NUMBER_VARIABLE); + button.setAttribute('callbackKey', 'CREATE_VARIABLE_NUMBER'); + xmlList.push(button);button = goog.dom.createDom('button'); + button.setAttribute('text', Blockly.Msg.NEW_COLOUR_VARIABLE); + button.setAttribute('callbackKey', 'CREATE_VARIABLE_COLOUR'); + xmlList.push(button); + + workspace.registerButtonCallback('CREATE_VARIABLE_STRING', + Blockly.VariablesDynamic.onCreateVariableButtonClick_String); + workspace.registerButtonCallback('CREATE_VARIABLE_NUMBER', + Blockly.VariablesDynamic.onCreateVariableButtonClick_Number); + workspace.registerButtonCallback('CREATE_VARIABLE_COLOUR', + Blockly.VariablesDynamic.onCreateVariableButtonClick_Colour); + + + var blockList = Blockly.VariablesDynamic.flyoutCategoryBlocks(workspace); + xmlList = xmlList.concat(blockList); + return xmlList; +}; + +/** + * Construct the blocks required by the flyout for the variable category. + * @param {!Blockly.Workspace} workspace The workspace contianing variables. + * @return {!Array.} Array of XML block elements. + */ +Blockly.VariablesDynamic.flyoutCategoryBlocks = function(workspace) { + var variableModelList = workspace.getAllVariables(); + variableModelList.sort(Blockly.VariableModel.compareByName); + + var xmlList = []; + if (variableModelList.length > 0) { + + if (Blockly.Blocks['variables_set_dynamic']) { + var firstVariable = variableModelList[0]; + var gap = 24; + var blockText = '' + + '' + + Blockly.Variables.generateVariableFieldXml_(firstVariable) + + '' + + ''; + var block = Blockly.Xml.textToDom(blockText).firstChild; + xmlList.push(block); + } + for (var i = 0, variable; variable = variableModelList[i]; i++) { + if (Blockly.Blocks['variables_get_dynamic']) { + var blockText = '' + + '' + + Blockly.Variables.generateVariableFieldXml_(variable) + + '' + + ''; + var block = Blockly.Xml.textToDom(blockText).firstChild; + xmlList.push(block); + } + } + } + return xmlList; +}; diff --git a/core/workspace_svg.js b/core/workspace_svg.js index b5954b1f1..35510628b 100644 --- a/core/workspace_svg.js +++ b/core/workspace_svg.js @@ -104,6 +104,8 @@ Blockly.WorkspaceSvg = function(options, opt_blockDragSurface, opt_wsDragSurface this.registerToolboxCategoryCallback(Blockly.VARIABLE_CATEGORY_NAME, Blockly.Variables.flyoutCategory); + this.registerToolboxCategoryCallback(Blockly.VARIABLE_DYNAMIC_CATEGORY_NAME, + Blockly.VariablesDynamic.flyoutCategory); this.registerToolboxCategoryCallback(Blockly.PROCEDURE_CATEGORY_NAME, Blockly.Procedures.flyoutCategory); }; diff --git a/msg/messages.js b/msg/messages.js index c4a2c2d54..1cd48b8fb 100644 --- a/msg/messages.js +++ b/msg/messages.js @@ -62,6 +62,8 @@ Blockly.Msg.LISTS_HUE = '260'; Blockly.Msg.COLOUR_HUE = '20'; /// {{Notranslate}} Hue value for all variable blocks. Blockly.Msg.VARIABLES_HUE = '330'; +/// {{Notranslate}} Hue value for all variable dynamic blocks. +Blockly.Msg.VARIABLES_DYNAMIC_HUE = '310'; /// {{Notranslate}} Hue value for all procedure blocks. Blockly.Msg.PROCEDURES_HUE = '290'; @@ -121,6 +123,14 @@ Blockly.Msg.RENAME_VARIABLE_TITLE = 'Rename all "%1" variables to:'; // Variable creation /// button text - Text on the button used to launch the variable creation dialogue. Blockly.Msg.NEW_VARIABLE = 'Create variable...'; +/// button text - Text on the button used to launch the variable creation dialogue. +Blockly.Msg.NEW_STRING_VARIABLE = 'Create string variable...'; +/// button text - Text on the button used to launch the variable creation dialogue. +Blockly.Msg.NEW_NUMBER_VARIABLE = 'Create number variable...'; +/// button text - Text on the button used to launch the variable creation dialogue. +Blockly.Msg.NEW_COLOUR_VARIABLE = 'Create colour variable...'; +/// prompt - Prompts the user to enter the type for a variable. +Blockly.Msg.NEW_VARIABLE_TYPE_TITLE = 'New variable type:'; /// prompt - Prompts the user to enter the name for a new variable. See [https://github.com/google/blockly/wiki/Variables#dropdown-menu https://github.com/google/blockly/wiki/Variables#dropdown-menu]. Blockly.Msg.NEW_VARIABLE_TITLE = 'New variable name:'; /// alert - Tells the user that the name they entered is already in use. diff --git a/tests/playground.html b/tests/playground.html index f062bf542..1b7599717 100644 --- a/tests/playground.html +++ b/tests/playground.html @@ -57,6 +57,7 @@ +