[blockly] Add Quantity support to round block (#2000)
Round block now also takes a Qty block as input. Output type changes according to input. Also-by: Florian Hotze <florianh_dev@icloud.com> Signed-off-by: Stefan Höhn <mail@stefanhoehn.com>pull/2009/head
parent
edc124e4f8
commit
ffb4ce6767
|
@ -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]
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue