import Blockly from "blockly/core" ; import "blockly/javascript_compressed" ; export interface BlocklyJSDef { block : string ; f : { ( block ) : void ; } ; } export enum ArgType { Value , Variable , Field } export interface TypedArg { type : ArgType ; arg : string ; } export function constructCodeFromParams ( block , method , ... args ) { let result = method + "(" ; args . forEach ( ( arg , index ) => { if ( index !== 0 ) { result += ", " ; } let resultAddition = undefined ; if ( typeof arg === "string" ) { resultAddition = Blockly . JavaScript . valueToCode ( block , arg , Blockly . JavaScript . ORDER_ATOMIC ) ; } else { let typedArg = arg as TypedArg ; switch ( typedArg . type ) { case ArgType . Value : { resultAddition = Blockly . JavaScript . valueToCode ( block , typedArg . arg , Blockly . JavaScript . ORDER_ATOMIC ) ; break ; } case ArgType . Variable : { resultAddition = Blockly . JavaScript . variableDB_ . getName ( block . getFieldValue ( typedArg . arg ) , Blockly . VARIABLE_CATEGORY_NAME ) ; break ; } case ArgType . Field : { resultAddition = '"' + block . getFieldValue ( typedArg . arg ) . replace ( '"' , '\\"' ) + '"' ; break ; } } } result += resultAddition && resultAddition !== "" ? resultAddition : "undefined" ; } ) ; result += ")" ; return result ; } export function valuePkg ( code ) { return [ code , Blockly . JavaScript . ORDER_NONE ] ; } export function statementPkg ( code ) { return code + ";" ; } export const makeJSArray = ( array ) => "[" + array . reduce ( ( acc , cur , index ) => acc + ", " + cur ) + "]" ; export class KobraError extends Error { error : string ; explanation : string ; constructor ( error , explanation ) { super ( error + " - " + explanation ) ; this . error = error ; this . explanation = explanation ; this . name = "KobraError" ; } } export class UnconnectedParameterError extends KobraError { constructor ( connectionName , explanation ? ) { const error = `${ connectionName } is not connected` ; explanation ??= "Make sure you connect a block to the input, or if a variable is connected, make sure the variable is set" ; super ( error , explanation ) ; this . name = "UnconnectedParameterError" ; } } export class UndefinedVariableError extends KobraError { constructor ( variableName , explanation ? ) { const error = `${ variableName } variable is not defined` ; explanation ??= "Make sure you have set it to a value before this block" ; super ( error , explanation ) ; this . name = "UndefinedVariableError" ; } }