From f16cdf912d876a7774524c77b5380ecc5472232a Mon Sep 17 00:00:00 2001 From: Beka Westberg Date: Sat, 15 Dec 2018 15:52:34 -0800 Subject: [PATCH 1/3] Changed Warnings to Interact Properly with Collapsing/Expanding. --- core/block_svg.js | 41 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 39 insertions(+), 2 deletions(-) diff --git a/core/block_svg.js b/core/block_svg.js index 4cd15dde5..abb17c159 100644 --- a/core/block_svg.js +++ b/core/block_svg.js @@ -140,6 +140,15 @@ Blockly.BlockSvg.prototype.warningTextDb_ = null; */ Blockly.BlockSvg.INLINE = -1; +/** + * Prefix to add to warnings that bubble up when the parent block is + * collapse. Allows us to remove the inherited warnings without removing the + * ones that belong to the block. + * @type {string} + * @const + */ +Blockly.BlockSvg.COLLAPSED_WARNING = 'TEMP_COLLAPSED_WARNING_'; + /** * Create and initialize the SVG representation of the block. * May be called more than once. @@ -509,10 +518,37 @@ Blockly.BlockSvg.prototype.setCollapsed = function(collapsed) { } var text = this.toString(Blockly.COLLAPSE_CHARS); this.appendDummyInput(COLLAPSED_INPUT_NAME).appendField(text).init(); + + // Add any warnings on enclosed blocks to this block. + var descendants = this.getDescendants(true); + var nextBlock = this.getNextBlock(); + if (nextBlock) { + var index = descendants.indexOf(nextBlock); + descendants.splice(index, descendants.length - index); + } + for (var i = 1, block; block = descendants[i]; i++) { + if (block.warning) { + this.setWarningText(block.warning.getText(), + Blockly.BlockSvg.COLLAPSED_WARNING + i); + } + } } else { this.removeInput(COLLAPSED_INPUT_NAME); // Clear any warnings inherited from enclosed blocks. - this.setWarningText(null); + if (this.warning) { + var keys = Object.keys(this.warning.text_); + var warningsDeleted = 0; + for (var i = 0; i < keys.length; i++) { + var key = keys[i]; + if (key.indexOf(Blockly.BlockSvg.COLLAPSED_WARNING) != -1) { + this.warning.setText('', key); + warningsDeleted++; + } + } + if (warningsDeleted == keys.length) { + this.setWarningText(null); + } + } } Blockly.BlockSvg.superClass_.setCollapsed.call(this, collapsed); @@ -1035,7 +1071,8 @@ Blockly.BlockSvg.prototype.setWarningText = function(text, opt_id) { parent = parent.getSurroundParent(); } if (collapsedParent) { - collapsedParent.setWarningText(text, 'collapsed ' + this.id + ' ' + id); + collapsedParent.setWarningText(text, + Blockly.BlockSvg.COLLAPSED_WARNING + this.id + ' ' + id); } var changedState = false; From 44df2d4680e083c3740277f0fa64bd86d050f5b4 Mon Sep 17 00:00:00 2001 From: Beka Westberg Date: Mon, 17 Dec 2018 14:54:56 -0800 Subject: [PATCH 2/3] Changed parent blocks to display "Collapsed blocks contain warnings." instead of having warnings "bubble up" from children. Added message to msg/messages.js . --- core/block_svg.js | 23 ++++++++--------------- msg/messages.js | 5 +++++ 2 files changed, 13 insertions(+), 15 deletions(-) diff --git a/core/block_svg.js b/core/block_svg.js index abb17c159..c467aa5bf 100644 --- a/core/block_svg.js +++ b/core/block_svg.js @@ -147,7 +147,7 @@ Blockly.BlockSvg.INLINE = -1; * @type {string} * @const */ -Blockly.BlockSvg.COLLAPSED_WARNING = 'TEMP_COLLAPSED_WARNING_'; +Blockly.BlockSvg.COLLAPSED_WARNING_ID = 'TEMP_COLLAPSED_WARNING_'; /** * Create and initialize the SVG representation of the block. @@ -528,24 +528,17 @@ Blockly.BlockSvg.prototype.setCollapsed = function(collapsed) { } for (var i = 1, block; block = descendants[i]; i++) { if (block.warning) { - this.setWarningText(block.warning.getText(), - Blockly.BlockSvg.COLLAPSED_WARNING + i); + this.setWarningText(Blockly.Msg['COLLAPSED_WARNINGS_WARNING'], + Blockly.BlockSvg.COLLAPSED_WARNING_ID); + break; } } } else { this.removeInput(COLLAPSED_INPUT_NAME); // Clear any warnings inherited from enclosed blocks. if (this.warning) { - var keys = Object.keys(this.warning.text_); - var warningsDeleted = 0; - for (var i = 0; i < keys.length; i++) { - var key = keys[i]; - if (key.indexOf(Blockly.BlockSvg.COLLAPSED_WARNING) != -1) { - this.warning.setText('', key); - warningsDeleted++; - } - } - if (warningsDeleted == keys.length) { + this.warning.setText('', Blockly.BlockSvg.COLLAPSED_WARNING_ID); + if (!Object.keys(this.warning.text_).length) { this.setWarningText(null); } } @@ -1071,8 +1064,8 @@ Blockly.BlockSvg.prototype.setWarningText = function(text, opt_id) { parent = parent.getSurroundParent(); } if (collapsedParent) { - collapsedParent.setWarningText(text, - Blockly.BlockSvg.COLLAPSED_WARNING + this.id + ' ' + id); + collapsedParent.setWarningText(Blockly.Msg['COLLAPSED_WARNINGS_WARNING'], + Blockly.BlockSvg.COLLAPSED_WARNING_ID); } var changedState = false; diff --git a/msg/messages.js b/msg/messages.js index d3fea6ce9..9974c6dfa 100644 --- a/msg/messages.js +++ b/msg/messages.js @@ -1206,3 +1206,8 @@ Blockly.Msg.PROCEDURES_IFRETURN_WARNING = 'Warning: This block may be used only /// comment text - This text appears in a new workspace comment, to hint that /// the user can type here. Blockly.Msg.WORKSPACE_COMMENT_DEFAULT_TEXT = 'Say something...'; + +/// warning - This appears if the user collapses a block, and blocks inside +// that block have warnings attached to them. It should inform the user that the +// block they collapsed contains blocks that have warnings. +Blockly.Msg.COLLAPSED_WARNINGS_WARNING = 'Collapsed blocks contain warnings.'; From 8241068630e5586afca4d22b37ad131d5f34895b Mon Sep 17 00:00:00 2001 From: Beka Westberg Date: Mon, 17 Dec 2018 15:29:25 -0800 Subject: [PATCH 3/3] Updated JSDoc. --- core/block_svg.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/core/block_svg.js b/core/block_svg.js index c467aa5bf..37ef35803 100644 --- a/core/block_svg.js +++ b/core/block_svg.js @@ -141,9 +141,9 @@ Blockly.BlockSvg.prototype.warningTextDb_ = null; Blockly.BlockSvg.INLINE = -1; /** - * Prefix to add to warnings that bubble up when the parent block is - * collapse. Allows us to remove the inherited warnings without removing the - * ones that belong to the block. + * ID to give the "collapsed warnings" warning. Allows us to remove the + * "collapsed warnings" warning without removing any warnings that belong to + * the block. * @type {string} * @const */