@@ -20,7 +20,7 @@ Blockly.Action.blocks['exec'] =
2020 '<block type="exec">'
2121 + ' <value name="COMMAND">'
2222 + ' <shadow type="text">'
23- + ' <field name="TEXT">text </field>'
23+ + ' <field name="TEXT">pwd </field>'
2424 + ' </shadow>'
2525 + ' </value>'
2626 + ' <value name="LOG">'
@@ -33,7 +33,7 @@ Blockly.Action.blocks['exec'] =
3333Blockly . Blocks [ 'exec' ] = {
3434 init : function ( ) {
3535 this . appendDummyInput ( 'TEXT' )
36- . appendField ( Blockly . Translate ( 'exec' ) ) ;
36+ . appendField ( '» ' + Blockly . Translate ( 'exec' ) ) ;
3737
3838 this . appendValueInput ( 'COMMAND' )
3939 . appendField ( Blockly . Translate ( 'exec_command' ) ) ;
@@ -96,26 +96,91 @@ Blockly.JavaScript['exec'] = function(block) {
9696
9797 let logText ;
9898 if ( logLevel ) {
99- logText = ' console.' + logLevel + '(" exec: " + ' + value_command + ' );\n'
99+ logText = ` console.${ logLevel } (' exec: ' + ${ value_command } );\n` ;
100100 } else {
101101 logText = '' ;
102102 }
103103
104104 if ( withStatement === 'TRUE' || withStatement === 'true' || withStatement === true ) {
105105 const statement = Blockly . JavaScript . statementToCode ( block , 'STATEMENT' ) ;
106106 if ( statement ) {
107- return 'exec(' + value_command + ', async (error, result, stderr) => {\n' + statement + '});\n' +
108- logText ;
107+ return `exec(${ value_command } , async (error, result, stderr) => {\n` +
108+ statement +
109+ `});\n${ logText } ` ;
109110 } else {
110- return 'exec(' + value_command + ');\n' +
111- logText ;
111+ return `exec(${ value_command } );\n${ logText } ` ;
112112 }
113113 } else {
114- return 'exec(' + value_command + ');\n' +
115- logText ;
114+ return `exec(${ value_command } );\n${ logText } ` ;
116115 }
117116} ;
118117
118+ // --- exec_result -----------------------------------------------------------
119+ Blockly . Action . blocks [ 'exec_result' ] =
120+ '<block type="exec_result">'
121+ + ' <value name="ATTR">'
122+ + ' </value>'
123+ + '</block>' ;
124+
125+ Blockly . Blocks [ 'exec_result' ] = {
126+ /**
127+ * Block for conditionally returning a value from a procedure.
128+ * @this Blockly.Block
129+ */
130+ init : function ( ) {
131+ this . appendDummyInput ( )
132+ . appendField ( '»' ) ;
133+
134+ this . appendDummyInput ( 'ATTR' )
135+ . appendField ( new Blockly . FieldDropdown ( [
136+ [ Blockly . Translate ( 'exec_result_result' ) , 'result' ] ,
137+ [ Blockly . Translate ( 'exec_result_stderr' ) , 'stderr' ] ,
138+ [ Blockly . Translate ( 'exec_result_error' ) , 'error' ] ,
139+ ] ) , 'ATTR' ) ;
140+
141+ this . setInputsInline ( true ) ;
142+ this . setOutput ( true ) ;
143+ this . setColour ( Blockly . Action . HUE ) ;
144+ this . setTooltip ( Blockly . Translate ( 'exec_result_tooltip' ) ) ;
145+ //this.setHelpUrl(getHelp('exec'));
146+ } ,
147+ /**
148+ * Called whenever anything on the workspace changes.
149+ * Add warning if this flow block is not nested inside a loop.
150+ * @param {!Blockly.Events.Abstract } e Change event.
151+ * @this Blockly.Block
152+ */
153+ onchange : function ( e ) {
154+ let legal = false ;
155+ // Is the block nested in an exec?
156+ let block = this ;
157+ do {
158+ if ( this . FUNCTION_TYPES . includes ( block . type ) ) {
159+ legal = true ;
160+ break ;
161+ }
162+ block = block . getSurroundParent ( ) ;
163+ } while ( block ) ;
164+
165+ if ( legal ) {
166+ this . setWarningText ( null , this . id ) ;
167+ } else {
168+ this . setWarningText ( Blockly . Translate ( 'exec_result_warning' ) , this . id ) ;
169+ }
170+ } ,
171+ /**
172+ * List of block types that are functions and thus do not need warnings.
173+ * To add a new function type add this to your code:
174+ * Blockly.Blocks['procedures_ifreturn'].FUNCTION_TYPES.push('custom_func');
175+ */
176+ FUNCTION_TYPES : [ 'exec' ] ,
177+ } ;
178+ Blockly . JavaScript [ 'exec_result' ] = function ( block ) {
179+ const attr = block . getFieldValue ( 'ATTR' ) ;
180+
181+ return [ attr , Blockly . JavaScript . ORDER_ATOMIC ] ;
182+ } ;
183+
119184// --- action http_get --------------------------------------------------
120185Blockly . Action . blocks [ 'http_get' ] =
121186 '<block type="http_get">'
0 commit comments