From ffb4ce6767c247dfa6b8bc61cf6b2734df57b660 Mon Sep 17 00:00:00 2001 From: stefan-hoehn Date: Sun, 6 Aug 2023 19:08:46 +0200 Subject: [PATCH] [blockly] Add Quantity support to round block (#2000) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Round block now also takes a Qty block as input. Output type changes according to input. Also-by: Florian Hotze Signed-off-by: Stefan Höhn --- .../assets/definitions/blockly/blocks-math.js | 30 +++++++++++++++++-- .../src/assets/definitions/blockly/utils.js | 2 +- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/bundles/org.openhab.ui/web/src/assets/definitions/blockly/blocks-math.js b/bundles/org.openhab.ui/web/src/assets/definitions/blockly/blocks-math.js index 7171a6263..0756241d4 100644 --- a/bundles/org.openhab.ui/web/src/assets/definitions/blockly/blocks-math.js +++ b/bundles/org.openhab.ui/web/src/assets/definitions/blockly/blocks-math.js @@ -4,6 +4,7 @@ import Blockly from 'blockly' import { javascriptGenerator } from 'blockly/javascript' +import { blockGetCheckedInputType } from './utils' export default function (f7, isGraalJs) { Blockly.Blocks['oh_bit_not'] = { @@ -88,13 +89,25 @@ export default function (f7, isGraalJs) { block.updateType(operation) }) this.appendValueInput('NUM') - .setCheck('Number') + .setCheck(['Number', 'oh_quantity']) .appendField(dropDown, 'op') + this.setColour('%{BKY_MATH_HUE}') this.setInputsInline(false) this.setTooltip('Round a number up or down') this.setHelpUrl('https://www.openhab.org/docs/configuration/blockly/rules-blockly-math.html#round') - this.setOutput(true, 'Number') + this.setOutput(true, null) + }, + updateShape_: function () { + if (this.getInput('NUM')) { + let type = blockGetCheckedInputType(this, 'NUM') + if (type) { + this.setOutput(true, type) + } + } + }, + onchange: function () { + this.updateShape_() }, updateType: function (type) { if (type === 'toFixed') { @@ -111,13 +124,20 @@ export default function (f7, isGraalJs) { this.removeInput('declabel') this.setInputsInline(false) } + this.updateShape_() } } javascriptGenerator['math_round'] = function (block) { - const math_number = javascriptGenerator.valueToCode(block, 'NUM', javascriptGenerator.ORDER_FUNCTION_CALL) + const inputType = blockGetCheckedInputType(block, 'NUM') + const math_number_input = javascriptGenerator.valueToCode(block, 'NUM', javascriptGenerator.ORDER_FUNCTION_CALL) + let math_number = math_number_input + if (inputType === 'oh_quantity') { + math_number = math_number_input + '.float' + } const decimals = javascriptGenerator.valueToCode(block, 'DECIMALS', javascriptGenerator.ORDER_NONE) const operand = block.getFieldValue('op') + let code = '' if (operand !== 'toFixed') { let method = '' @@ -136,6 +156,10 @@ export default function (f7, isGraalJs) { } else { code = `(${math_number}).toFixed(${decimals})` } + + if (inputType === 'oh_quantity') { + code = `Quantity((${code}).toString() + ' ' + ${math_number_input}.symbol)` + } return [code, 0] } } diff --git a/bundles/org.openhab.ui/web/src/assets/definitions/blockly/utils.js b/bundles/org.openhab.ui/web/src/assets/definitions/blockly/utils.js index 53df72e67..0f5ac2e57 100644 --- a/bundles/org.openhab.ui/web/src/assets/definitions/blockly/utils.js +++ b/bundles/org.openhab.ui/web/src/assets/definitions/blockly/utils.js @@ -230,7 +230,7 @@ export function blockGetCheckedInputType (block, inputName) { // Get the input type checks for this block const thisBlock = block.getInput(inputName).connection.getCheck() // Get the output type checks for the connected block - const connectedBlock = block.getInput(inputName).connection.targetBlock().outputConnection.getCheck() + const connectedBlock = block.getInput(inputName).connection.targetBlock()?.outputConnection.getCheck() // Skip if no checks are available if (!thisBlock || !connectedBlock) return '' // Find any intersection in the checklist