From c9be0ca4133d5b27e75219c62e50b292c07560ce Mon Sep 17 00:00:00 2001 From: Rodrigo Quelhas Date: Sat, 16 Jul 2022 22:37:01 +0100 Subject: [PATCH] feat(): Adds expression for adding/subtracting seconds, minutes and hours to timestamps --- package.json | 2 +- src/blocks/enums/BlockKind.ts | 3 ++ src/blocks/expression/index.ts | 1 + src/blocks/expression/string/ends_with.ts | 5 +-- src/blocks/expression/string/starts_with.ts | 5 +-- src/blocks/expression/ticket/create_ticket.ts | 3 +- src/blocks/expression/ticket/join_ticket.ts | 3 +- src/blocks/expression/ticket/read_ticket.ts | 3 +- src/blocks/expression/ticket/split_ticket.ts | 3 +- src/blocks/expression/timestamp/add_hours.ts | 32 +++++++++++++++++++ .../expression/timestamp/add_minutes.ts | 32 +++++++++++++++++++ .../expression/timestamp/add_seconds.ts | 32 +++++++++++++++++++ src/blocks/expression/timestamp/index.ts | 3 ++ .../blockly/blocks/expressions/index.tsx | 1 + .../blockly/blocks/expressions/timestamp.tsx | 30 +++++++++++++++++ src/pages/editor/view.tsx | 13 ++++++++ 16 files changed, 162 insertions(+), 9 deletions(-) create mode 100644 src/blocks/expression/timestamp/add_hours.ts create mode 100644 src/blocks/expression/timestamp/add_minutes.ts create mode 100644 src/blocks/expression/timestamp/add_seconds.ts create mode 100644 src/blocks/expression/timestamp/index.ts create mode 100644 src/components/blockly/blocks/expressions/timestamp.tsx diff --git a/package.json b/package.json index a7c6aec..cac30b3 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@tezwell/visual-tez", - "version": "1.2.1", + "version": "1.2.2", "description": "A visual programming tool for writing smart contracts in the Tezos blockchain", "private": true, "author": { diff --git a/src/blocks/enums/BlockKind.ts b/src/blocks/enums/BlockKind.ts index a5b9e32..31f0bf3 100644 --- a/src/blocks/enums/BlockKind.ts +++ b/src/blocks/enums/BlockKind.ts @@ -147,6 +147,9 @@ export enum BlockKind { read_ticket_expression = 'read_ticket_expression', join_ticket_expression = 'join_ticket_expression', split_ticket_expression = 'split_ticket_expression', + add_seconds = 'add_seconds', + add_minutes = 'add_minutes', + add_hours = 'add_hours', // Statements return_statement_block = 'return_statement_block', diff --git a/src/blocks/expression/index.ts b/src/blocks/expression/index.ts index 237c787..8f7a8b6 100644 --- a/src/blocks/expression/index.ts +++ b/src/blocks/expression/index.ts @@ -23,3 +23,4 @@ import './variant'; import './string'; import './address'; import './ticket'; +import './timestamp'; diff --git a/src/blocks/expression/string/ends_with.ts b/src/blocks/expression/string/ends_with.ts index 405b5c2..684082a 100644 --- a/src/blocks/expression/string/ends_with.ts +++ b/src/blocks/expression/string/ends_with.ts @@ -1,10 +1,11 @@ import Blockly from 'blockly'; -import type { Block } from 'src/typings/blockly'; import type { IExpression } from '@tezwell/smartts-sdk/typings/expression'; import { StringHelpers } from '@tezwell/smartts-sdk/lib'; +import type { Block } from 'src/typings/blockly'; import BlockKind from '../../enums/BlockKind'; import SmartML from '../../generators/SmartML'; +import { buildErrorInfo } from 'src/blocks/utils/errorHandling'; Blockly.Blocks[BlockKind.string_ends_with] = { init: function () { @@ -26,6 +27,6 @@ SmartML.addBlock(BlockKind.string_ends_with, { toValue: (block: Block) => { const text: IExpression = SmartML.toValue(block, 'TEXT'); const postfix: IExpression = SmartML.toValue(block, 'POSTFIX'); - return StringHelpers.EndsWith(text, postfix); + return StringHelpers.EndsWith(text, postfix, buildErrorInfo(block)); }, }); diff --git a/src/blocks/expression/string/starts_with.ts b/src/blocks/expression/string/starts_with.ts index fef7971..755fedb 100644 --- a/src/blocks/expression/string/starts_with.ts +++ b/src/blocks/expression/string/starts_with.ts @@ -1,10 +1,11 @@ import Blockly from 'blockly'; -import type { Block } from 'src/typings/blockly'; import type { IExpression } from '@tezwell/smartts-sdk/typings/expression'; import { StringHelpers } from '@tezwell/smartts-sdk/lib'; +import type { Block } from 'src/typings/blockly'; import BlockKind from '../../enums/BlockKind'; import SmartML from '../../generators/SmartML'; +import { buildErrorInfo } from 'src/blocks/utils/errorHandling'; Blockly.Blocks[BlockKind.string_starts_with] = { init: function () { @@ -26,6 +27,6 @@ SmartML.addBlock(BlockKind.string_starts_with, { toValue: (block: Block) => { const text: IExpression = SmartML.toValue(block, 'TEXT'); const prefix: IExpression = SmartML.toValue(block, 'PREFIX'); - return StringHelpers.StartsWith(text, prefix); + return StringHelpers.StartsWith(text, prefix, buildErrorInfo(block)); }, }); diff --git a/src/blocks/expression/ticket/create_ticket.ts b/src/blocks/expression/ticket/create_ticket.ts index 031c1ee..2ca6a37 100644 --- a/src/blocks/expression/ticket/create_ticket.ts +++ b/src/blocks/expression/ticket/create_ticket.ts @@ -5,6 +5,7 @@ import type { IExpression } from '@tezwell/smartts-sdk/typings/expression'; import BlockKind from '../../enums/BlockKind'; import SmartML from '../../generators/SmartML'; import { CreateTicket } from '@tezwell/smartts-sdk'; +import { buildErrorInfo } from 'src/blocks/utils/errorHandling'; Blockly.Blocks[BlockKind.create_ticket_expression] = { init: function () { @@ -27,6 +28,6 @@ SmartML.addBlock(BlockKind.create_ticket_expression, { toValue: (block: Block) => { const content: IExpression = SmartML.toValue(block, 'CONTENT'); const quantity: IExpression = SmartML.toValue(block, 'QUANTITY'); - return CreateTicket(content, quantity); + return CreateTicket(content, quantity, buildErrorInfo(block)); }, }); diff --git a/src/blocks/expression/ticket/join_ticket.ts b/src/blocks/expression/ticket/join_ticket.ts index 8c26e27..7bd1b8d 100644 --- a/src/blocks/expression/ticket/join_ticket.ts +++ b/src/blocks/expression/ticket/join_ticket.ts @@ -5,6 +5,7 @@ import type { IExpression } from '@tezwell/smartts-sdk/typings/expression'; import BlockKind from '../../enums/BlockKind'; import SmartML from '../../generators/SmartML'; import { JoinTicket } from '@tezwell/smartts-sdk'; +import { buildErrorInfo } from 'src/blocks/utils/errorHandling'; Blockly.Blocks[BlockKind.join_ticket_expression] = { init: function () { @@ -27,6 +28,6 @@ SmartML.addBlock(BlockKind.join_ticket_expression, { toValue: (block: Block) => { const ticket1: IExpression = SmartML.toValue(block, 'TICKET1'); const ticket2: IExpression = SmartML.toValue(block, 'TICKET2'); - return JoinTicket(ticket1, ticket2); + return JoinTicket(ticket1, ticket2, buildErrorInfo(block)); }, }); diff --git a/src/blocks/expression/ticket/read_ticket.ts b/src/blocks/expression/ticket/read_ticket.ts index 4a80418..2ebb496 100644 --- a/src/blocks/expression/ticket/read_ticket.ts +++ b/src/blocks/expression/ticket/read_ticket.ts @@ -5,6 +5,7 @@ import type { IExpression } from '@tezwell/smartts-sdk/typings/expression'; import BlockKind from '../../enums/BlockKind'; import SmartML from '../../generators/SmartML'; import { ReadTicket } from '@tezwell/smartts-sdk'; +import { buildErrorInfo } from 'src/blocks/utils/errorHandling'; Blockly.Blocks[BlockKind.read_ticket_expression] = { init: function () { @@ -25,6 +26,6 @@ Blockly.Blocks[BlockKind.read_ticket_expression] = { SmartML.addBlock(BlockKind.read_ticket_expression, { toValue: (block: Block) => { const ticket: IExpression = SmartML.toValue(block, 'TICKET'); - return ReadTicket(ticket); + return ReadTicket(ticket, buildErrorInfo(block)); }, }); diff --git a/src/blocks/expression/ticket/split_ticket.ts b/src/blocks/expression/ticket/split_ticket.ts index 6106c6e..81830d7 100644 --- a/src/blocks/expression/ticket/split_ticket.ts +++ b/src/blocks/expression/ticket/split_ticket.ts @@ -5,6 +5,7 @@ import type { IExpression } from '@tezwell/smartts-sdk/typings/expression'; import BlockKind from '../../enums/BlockKind'; import SmartML from '../../generators/SmartML'; import { SplitTicket } from '@tezwell/smartts-sdk'; +import { buildErrorInfo } from 'src/blocks/utils/errorHandling'; Blockly.Blocks[BlockKind.split_ticket_expression] = { init: function () { @@ -29,6 +30,6 @@ SmartML.addBlock(BlockKind.split_ticket_expression, { const ticket: IExpression = SmartML.toValue(block, 'TICKET'); const quantity1: IExpression = SmartML.toValue(block, 'QUANTITY1'); const quantity2: IExpression = SmartML.toValue(block, 'QUANTITY2'); - return SplitTicket(ticket, quantity1, quantity2); + return SplitTicket(ticket, quantity1, quantity2, buildErrorInfo(block)); }, }); diff --git a/src/blocks/expression/timestamp/add_hours.ts b/src/blocks/expression/timestamp/add_hours.ts new file mode 100644 index 0000000..be2d54f --- /dev/null +++ b/src/blocks/expression/timestamp/add_hours.ts @@ -0,0 +1,32 @@ +import Blockly from 'blockly'; +import type { Block } from 'src/typings/blockly'; +import type { IExpression } from '@tezwell/smartts-sdk/typings/expression'; + +import BlockKind from '../../enums/BlockKind'; +import SmartML from '../../generators/SmartML'; +import { AddHours } from '@tezwell/smartts-sdk'; +import { buildErrorInfo } from 'src/blocks/utils/errorHandling'; + +Blockly.Blocks[BlockKind.add_hours] = { + init: function () { + this.appendValueInput('HOURS').setCheck(['Int', 'Expression']).appendField('Add hours'); + this.appendValueInput('TIMESTAMP').setCheck(['Timescript', 'Expression']).appendField('to timestamp'); + + this.setInputsInline(true); + this.setOutput(true, ['Expression']); + + this.setTooltip(`Add seconds to timestamp.\n-\n(TTimestamp(), TInt()) => TTimestamp()\n`); + + this.setColour(200); + this.setPreviousStatement(false); + this.setNextStatement(false); + }, +}; + +SmartML.addBlock(BlockKind.add_hours, { + toValue: (block: Block) => { + const timestamp: IExpression = SmartML.toValue(block, 'TIMESTAMP'); + const hours: IExpression = SmartML.toValue(block, 'HOURS'); + return AddHours(timestamp, hours, buildErrorInfo(block)); + }, +}); diff --git a/src/blocks/expression/timestamp/add_minutes.ts b/src/blocks/expression/timestamp/add_minutes.ts new file mode 100644 index 0000000..7f98252 --- /dev/null +++ b/src/blocks/expression/timestamp/add_minutes.ts @@ -0,0 +1,32 @@ +import Blockly from 'blockly'; +import type { Block } from 'src/typings/blockly'; +import type { IExpression } from '@tezwell/smartts-sdk/typings/expression'; + +import BlockKind from '../../enums/BlockKind'; +import SmartML from '../../generators/SmartML'; +import { AddMinutes } from '@tezwell/smartts-sdk'; +import { buildErrorInfo } from 'src/blocks/utils/errorHandling'; + +Blockly.Blocks[BlockKind.add_minutes] = { + init: function () { + this.appendValueInput('MINUTES').setCheck(['Int', 'Expression']).appendField('Add minutes'); + this.appendValueInput('TIMESTAMP').setCheck(['Timescript', 'Expression']).appendField('to timestamp'); + + this.setInputsInline(true); + this.setOutput(true, ['Expression']); + + this.setTooltip(`Add seconds to timestamp.\n-\n(TTimestamp(), TInt()) => TTimestamp()\n`); + + this.setColour(200); + this.setPreviousStatement(false); + this.setNextStatement(false); + }, +}; + +SmartML.addBlock(BlockKind.add_minutes, { + toValue: (block: Block) => { + const timestamp: IExpression = SmartML.toValue(block, 'TIMESTAMP'); + const minutes: IExpression = SmartML.toValue(block, 'MINUTES'); + return AddMinutes(timestamp, minutes, buildErrorInfo(block)); + }, +}); diff --git a/src/blocks/expression/timestamp/add_seconds.ts b/src/blocks/expression/timestamp/add_seconds.ts new file mode 100644 index 0000000..f8e23d4 --- /dev/null +++ b/src/blocks/expression/timestamp/add_seconds.ts @@ -0,0 +1,32 @@ +import Blockly from 'blockly'; +import type { Block } from 'src/typings/blockly'; +import type { IExpression } from '@tezwell/smartts-sdk/typings/expression'; + +import BlockKind from '../../enums/BlockKind'; +import SmartML from '../../generators/SmartML'; +import { AddSeconds } from '@tezwell/smartts-sdk'; +import { buildErrorInfo } from 'src/blocks/utils/errorHandling'; + +Blockly.Blocks[BlockKind.add_seconds] = { + init: function () { + this.appendValueInput('SECONDS').setCheck(['Int', 'Expression']).appendField('Add seconds'); + this.appendValueInput('TIMESTAMP').setCheck(['Timescript', 'Expression']).appendField('to timestamp'); + + this.setInputsInline(true); + this.setOutput(true, ['Expression']); + + this.setTooltip(`Add seconds to timestamp.\n-\n(TTimestamp(), TInt()) => TTimestamp()\n`); + + this.setColour(200); + this.setPreviousStatement(false); + this.setNextStatement(false); + }, +}; + +SmartML.addBlock(BlockKind.add_seconds, { + toValue: (block: Block) => { + const timestamp: IExpression = SmartML.toValue(block, 'TIMESTAMP'); + const seconds: IExpression = SmartML.toValue(block, 'SECONDS'); + return AddSeconds(timestamp, seconds, buildErrorInfo(block)); + }, +}); diff --git a/src/blocks/expression/timestamp/index.ts b/src/blocks/expression/timestamp/index.ts new file mode 100644 index 0000000..76be165 --- /dev/null +++ b/src/blocks/expression/timestamp/index.ts @@ -0,0 +1,3 @@ +import './add_seconds'; +import './add_minutes'; +import './add_hours'; diff --git a/src/components/blockly/blocks/expressions/index.tsx b/src/components/blockly/blocks/expressions/index.tsx index bb05ddf..93a3bdb 100644 --- a/src/components/blockly/blocks/expressions/index.tsx +++ b/src/components/blockly/blocks/expressions/index.tsx @@ -20,3 +20,4 @@ export * from './record'; export * from './variables'; export * from './variant'; export * from './ticket'; +export * from './timestamp'; diff --git a/src/components/blockly/blocks/expressions/timestamp.tsx b/src/components/blockly/blocks/expressions/timestamp.tsx new file mode 100644 index 0000000..427723b --- /dev/null +++ b/src/components/blockly/blocks/expressions/timestamp.tsx @@ -0,0 +1,30 @@ +import React from 'react'; + +import BlockKind from 'src/blocks/enums/BlockKind'; +import Block from 'src/components/blockly/Block'; +import Shadow from '../../Shadow'; +import Value from '../../Value'; + +export const AddSeconds = () => ( + + + + + +); + +export const AddMinutes = () => ( + + + + + +); + +export const AddHours = () => ( + + + + + +); diff --git a/src/pages/editor/view.tsx b/src/pages/editor/view.tsx index 04bc5b9..2268735 100644 --- a/src/pages/editor/view.tsx +++ b/src/pages/editor/view.tsx @@ -179,6 +179,9 @@ import { ReadTicket, JoinTicket, SplitTicket, + AddSeconds, + AddHours, + AddMinutes, } from 'src/components/blockly/blocks/expressions'; import Logger from 'src/utils/logger'; import { validateBlockLocation } from 'src/blocks/utils/workspace'; @@ -460,6 +463,10 @@ const EditorView: React.FC = ({ workspaceRef, compile, onError + + + + {/* Statements */} @@ -626,6 +633,12 @@ const EditorView: React.FC = ({ workspaceRef, compile, onError + + + + + +