From 57077f57545f0e2e88048610bc878e08e66aa513 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20H=C3=B6hn?= Date: Wed, 2 Aug 2023 18:43:45 +0200 Subject: [PATCH 1/2] [blockly] Add Quantity support to round block MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Stefan Höhn --- .../assets/definitions/blockly/blocks-math.js | 30 +++++++++++++++++-- .../src/assets/definitions/blockly/utils.js | 1 + 2 files changed, 28 insertions(+), 3 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 7171a62637..0756241d4b 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 53df72e677..e61cc8600d 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,6 +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 + if (!block.getInput(inputName).connection.targetBlock()) return '' const connectedBlock = block.getInput(inputName).connection.targetBlock().outputConnection.getCheck() // Skip if no checks are available if (!thisBlock || !connectedBlock) return '' From 03abcd0cf8d0bcfb27b63ada722d46167b6b3e79 Mon Sep 17 00:00:00 2001 From: Florian Hotze Date: Sun, 6 Aug 2023 18:51:55 +0200 Subject: [PATCH 2/2] Minor code improvement Signed-off-by: Florian Hotze --- .../org.openhab.ui/web/src/assets/definitions/blockly/utils.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) 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 e61cc8600d..0f5ac2e57e 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,8 +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 - if (!block.getInput(inputName).connection.targetBlock()) return '' - 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