Skip to content

Commit

Permalink
Added time calculation blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
klein0r committed Aug 4, 2023
1 parent 561d824 commit 8ff60bd
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 2 deletions.
82 changes: 80 additions & 2 deletions src/public/google-blockly/own/blocks_time.js
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,7 @@ Blockly.Blocks['time_get_special'] = {

this.setInputsInline(true);

this.setOutput(true);
this.setOutput(true, 'Number');

this.setColour(Blockly.Time.HUE);
this.setTooltip(Blockly.Translate('time_get_special_tooltip'));
Expand Down Expand Up @@ -545,4 +545,82 @@ Blockly.JavaScript['time_astro'] = function(block) {
const offset = parseFloat(block.getFieldValue('OFFSET'));

return [`getAstroDate('${type}', undefined, ${offset})`, Blockly.JavaScript.ORDER_ATOMIC];
};
};

// --- time calculation --------------------------------------------------
Blockly.Time.blocks['time_calculation'] =
'<block type="time_calculation">'
+ ' <value name="DATE_TIME">'
+ ' <shadow type="time_get">'
+ ' <field name="OPTION">object</field>'
+ ' </shadow>'
+ ' </value>'
+ ' <value name="OPERATION">'
+ ' </value>'
+ ' <value name="VALUE">'
+ ' <shadow type="math_number">'
+ ' <field name="NUM">1</field>'
+ ' </shadow>'
+ ' </value>'
+ ' <value name="UNIT">'
+ ' </value>'
+ '</block>';

Blockly.Blocks['time_calculation'] = {
init: function() {
this.appendDummyInput('NAME')
.appendField(Blockly.Translate('time_calculation'));

this.appendValueInput('DATE_TIME')
.appendField(Blockly.Translate('time_calculation_on'))
.setCheck(null);

this.appendDummyInput('OPERATION')
.appendField(new Blockly.FieldDropdown([
['+', '+'],
['-', '-'],
]), 'OPERATION');

this.appendValueInput('VALUE');

this.appendDummyInput('UNIT')
.appendField(new Blockly.FieldDropdown([
[Blockly.Translate('time_calculation_ms'), 'ms'],
[Blockly.Translate('time_calculation_sec'), 'sec'],
[Blockly.Translate('time_calculation_min'), 'min'],
[Blockly.Translate('time_calculation_hour'), 'hour'],
[Blockly.Translate('time_calculation_day'), 'day'],
[Blockly.Translate('time_calculation_week'), 'week'],
]), 'UNIT');

this.setInputsInline(true);

this.setOutput(true, 'Number');

this.setColour(Blockly.Time.HUE);
this.setTooltip(Blockly.Translate('time_calculation_tooltip'));
this.setHelpUrl(Blockly.Translate('time_calculation_help'));
},
};

Blockly.JavaScript['time_calculation'] = function(block) {
const dateTime = Blockly.JavaScript.valueToCode(block, 'DATE_TIME', Blockly.JavaScript.ORDER_ATOMIC);
const operation = block.getFieldValue('OPERATION');
const value = Blockly.JavaScript.valueToCode(block, 'VALUE', Blockly.JavaScript.ORDER_ATOMIC);
const unit = block.getFieldValue('UNIT');

let step = 1;
if (unit === 'sec') {
step = 1000;
} else if (unit === 'min') {
step = 60 * 1000;
} else if (unit === 'hour') {
step = 60 * 60 * 1000;
} else if (unit === 'day') {
step = 24 * 60 * 60 * 1000;
} else if (unit === 'week') {
step = 7 * 24 * 60 * 60 * 1000;
}

return [`/* time calculation */ ((dateTime) => { const ts = (typeof dateTime === 'object' ? dateTime.getTime() : dateTime); return ts ${operation} ((${value}) * ${step}); })(${dateTime})`, Blockly.JavaScript.ORDER_ATOMIC];
};
10 changes: 10 additions & 0 deletions src/public/google-blockly/own/blocks_words.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 8ff60bd

Please sign in to comment.