Skip to content

Commit

Permalink
Add arguments handling
Browse files Browse the repository at this point in the history
  • Loading branch information
simosathan9 committed Oct 2, 2024
1 parent 19b3806 commit 7ed9111
Show file tree
Hide file tree
Showing 8 changed files with 257 additions and 83 deletions.
5 changes: 2 additions & 3 deletions public/blocks/argumentBlock.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@ var argumentBlock = {
args0: [
{
type: 'field_input',
name: 'argument',
name: 'ARGUMENT',
text: 'argument' // default text for the input
}
],

extensions: ['restrict_argumentsCreate_to_argument'],
output: 'String',
style: 'Function inputs',
tooltip: '%{BKY_ARGUMENT_TOOLTIP}',
helpUrl: '%{BKY_ARGUMENT_HELPURL}' // URL για περαιτέρω πληροφορίες ή τεκμηρίωση.
};

Blockly.defineBlocksWithJsonArray([argumentBlock]);
3 changes: 1 addition & 2 deletions public/blocks/fileEndStartBlock.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,10 @@ var fileEndStartBlock = {
text: '....' // empty text for user to define filename
}
],

extensions: ['restrict_fileEndStart_to_filenamesCreate'],
output: 'fileWildcard',
style: 'Function inputs',
tooltip: '%{BKY_FILE_END_START_WILDCHARS_TOOLTIP}',
helpUrl: '%{BKY_FILE_END_START_WILDCHARS_HELPURL} ' // URL to further information or documentation.
};

Blockly.defineBlocksWithJsonArray([fileEndStartBlock]);
2 changes: 1 addition & 1 deletion public/blocks/filenameBlock.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ var filenameBlock = {
text: 'default.txt' // default text for the input
}
],
extensions: ['restrict_filename_to_filenamesCreate'],
output: 'filename',
style: 'Function inputs',
tooltip: '%{BKY_FILENAME_TOOLTIP}',
helpUrl: '%{BKY_FILENAME_HELPURL}' // URL to further information or documentation.
};

Blockly.defineBlocksWithJsonArray([filenameBlock]);
4 changes: 2 additions & 2 deletions public/blocks/mvBlock.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ var mvBlock = {
message4: '%{BKY_MV_SOURCE}: %1 %{BKY_MV_DEST}: %2',
args4: [
{
type: 'field_input',
type: 'input_value',
name: 'SOURCE',
text: 'source'
},
{
type: 'field_input',
type: 'input_value',
name: 'DEST',
text: 'destination'
}
Expand Down
9 changes: 8 additions & 1 deletion public/blocks/rmBlock.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
var rmBlock = {
type: 'rm',
message0: '%{BKY_RM}',
category: 'Filesystem Operations',
unix_description: [
{
Expand All @@ -11,6 +10,14 @@ var rmBlock = {
undelete: '-W'
}
],
message0: '%{BKY_RM} %1',
args0: [
{
type: 'input_value',
name: 'ARGUMENT',
check: 'String'
}
],
message1: '%{BKY_RM_FORCE}',
args1: [
{
Expand Down
2 changes: 1 addition & 1 deletion public/blocks/sedBlock.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ var sedBlock = {
message2: '%{BKY_SED_PATTERN}',
args2: [
{
type: 'input_value',
type: 'input_statement',
name: 'regPattern',
check: 'String'
}
Expand Down
70 changes: 69 additions & 1 deletion public/blocks/touchBlock.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,78 @@ var touchBlock = {
}
],

extensions: ['validate_touch_time_d'],
extensions: ['validate_touch_time_d', 'restrict_touch_to_argumentsCreate'],
style: 'Filesystem Operations',
tooltip: '%{BKY_TOUCH_TOOLTIP}',
helpUrl: '%{BKY_TOUCH_HELPURL}' // URL to further information or documentation.
};

Blockly.defineBlocksWithJsonArray([touchBlock]);

Blockly.Extensions.register('validate_touch_time_d', function () {
this.getField('change_time_d').setValidator(function (input) {
// Define regex patterns to match different valid formats
var patterns = [
/^$/, // Empty string pattern
/^(\d{4})-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])\s(0[0-9]|1[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])(Z)?$/, // YYYY-MM-DD hh:mm:SS[tz]
/^(\d{4})-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])T(0[0-9]|1[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])(Z)?$/ // YYYY-MM-DDThh:mm:SS[tz]
];

// Check if input matches one of the valid patterns
var isValid = patterns.some(function (pattern) {
return pattern.test(input);
});

if (!isValid) {
return null; // Invalid input, reject
}

// Extract components from input (no replacement here)
var parts = input.split('T');
var datePart = parts[0];
var timePart = (parts[1] || '').split('.')[0]; // Remove fractional seconds part
var tzPart = (parts[1] || '').includes('Z') ? 'Z' : '';

// Handle date part
var dateParts = datePart.split('-');
var year = dateParts[0];
var month = dateParts[1];
var day = dateParts[2];

// Handle time part
var timeParts = timePart.split(':');
var hour = timeParts[0] || '00';
var minute = timeParts[1] || '00';
var second = timeParts[2] || '00';

// Validate ranges for year, month, day, hour, minute, and second
if (
month < '01' ||
month > '12' ||
day < '01' ||
day > '31' ||
hour < '00' ||
hour > '23' ||
minute < '00' ||
minute > '59' ||
second < '00' ||
second > '59'
) {
return null; // Invalid input: reject
}

// Check day validity for the given month and year (leap year handling)
var daysInMonth = new Date(year, parseInt(month, 10), 0).getDate();
if (parseInt(day, 10) > daysInMonth) {
return null; // Invalid day for the given month
}

// Optional time zone part validation
if (tzPart && tzPart !== 'Z') {
return null; // Invalid time zone designator, should be 'Z' if present
}

// Replace space with 'T' only when returning the valid input
return input;
});
});
Loading

0 comments on commit 7ed9111

Please sign in to comment.