From 46d256c5536faa50b2fea930daf68f5fae2adf0d Mon Sep 17 00:00:00 2001 From: stefan-hoehn Date: Sun, 7 May 2023 14:02:29 +0200 Subject: [PATCH] [blockly] Add pattern to text of date block (#1869) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Closes #1636. Allows the user to define a custom pattern for the text of date block. Also-by: Florian Hotze Signed-off-by: Stefan Höhn --- .../definitions/blockly/blocks-dateoffsets.js | 22 ++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/bundles/org.openhab.ui/web/src/assets/definitions/blockly/blocks-dateoffsets.js b/bundles/org.openhab.ui/web/src/assets/definitions/blockly/blocks-dateoffsets.js index 946e9d198..a1487e423 100644 --- a/bundles/org.openhab.ui/web/src/assets/definitions/blockly/blocks-dateoffsets.js +++ b/bundles/org.openhab.ui/web/src/assets/definitions/blockly/blocks-dateoffsets.js @@ -608,16 +608,33 @@ export default function (f7, isGraalJs) { */ Blockly.Blocks['oh_zdt_toText'] = { init: function () { + const block = this this.appendValueInput('date') .appendField('text of') .setCheck('ZonedDateTime') + + const dropDown = new Blockly.FieldDropdown( + [['without time', 'without'], ['with time', 'with'], ['as OH-Time', 'asOHTime'], ['with pattern', 'withPattern']], + function (newMode) { + block._updateType(newMode) + }) this.appendDummyInput() - .appendField(new Blockly.FieldDropdown([['without time', 'without'], ['with time', 'with'], ['as OH-Time', 'asOHTime']]), 'withtime') + .appendField(dropDown, 'withtime') this.setOutput(true, 'String') this.setColour(160) this.setTooltip('converts an ZonedDateTime into a date string') this.setHelpUrl('https://www.openhab.org/docs/configuration/blockly/rules-blockly-date-handling.html#get-string-representation-of-date') + }, + _updateType: function (type) { + if (type === 'withPattern') { + if (this.getInput('pattern')) return + this.appendValueInput('pattern') + .setCheck('String') + .setShadowDom(Blockly.Xml.textToDom('yyyy-MM-dd')) + } else if (this.getInput('pattern')) { + this.removeInput('pattern') + } } } @@ -635,6 +652,9 @@ export default function (f7, isGraalJs) { code = `${date}.format(${dtf}.ofPattern('yyyy-MM-dd HH:mm:ss'))` } else if (withtime === 'without') { code = `${date}.format(${dtf}.ofPattern('yyyy-MM-dd'))` + } else if (withtime === 'withPattern') { + const pattern = javascriptGenerator.valueToCode(block, 'pattern', javascriptGenerator.ORDER_ATOMIC) + code = `${date}.format(${dtf}.ofPattern(${pattern}))` } else { code = `${date}.format(${dtf}.ofPattern('yyyy-MM-dd\\'T\\'HH:mm:ss.SSSZ'))` }