Commit 216270e5 authored by Kostis Trantzas's avatar Kostis Trantzas
Browse files

Merge branch '45-enhacement-proposal-lcm-text-replace-operator' into 'develop'

Resolve "Enhacement proposal: LCM text replace operator"

See merge request !41
parents 0144dcf1 d8d69a2b
Loading
Loading
Loading
Loading
Loading
+17 −0
Original line number Diff line number Diff line
@@ -198,6 +198,23 @@
                  </shadow>
                </value>
              </block>
              <block type="text_replace">
                <value name="STRING">
                  <block type="variables_get">
                    <field name="VAR">textVariable</field>
                  </block>
                </value>
                <value name="SEARCH">
                  <shadow type="text">
                    <field name="TEXT">before</field>
                  </shadow>
                </value>
                <value name="REPLACE">
                  <shadow type="text">
                    <field name="TEXT">after</field>
                  </shadow>
                </value>
              </block>


          </category> 
+12 −0
Original line number Diff line number Diff line
@@ -804,6 +804,18 @@ export class BlocklyJavaService {
      return [argument0 + operator, Blockly.Java.ORDER_FUNCTION_CALL];
    };

    Blockly.Java['text_replace'] = function (block: { getFieldValue: (arg0: string) => string; }) {
      // Search the text for a substring.
      var argument0 = Blockly.Java.valueToCode(block, 'SEARCH',
        Blockly.Java.ORDER_NONE) || '""';
      var argument1 = Blockly.Java.valueToCode(block, 'REPLACE',
        Blockly.Java.ORDER_NONE) || '""';
      var argument2 = Blockly.Java.valueToCode(block, 'STRING',
        Blockly.Java.ORDER_MEMBER) || '""';
      var code = argument2 + '.replace(' + argument0 + ',' + argument1 + ')';
      return [code, Blockly.Java.ORDER_MEMBER];
    };

    Blockly.Java['text_print'] = function (block: any) {
      // Print statement.
      var argument0 = Blockly.Java.valueToCode(block, 'TEXT',
+26 −0
Original line number Diff line number Diff line
@@ -706,6 +706,32 @@ Blockly.Blocks['variable_set_string'] = {
  customContextMenu: Blockly.Blocks['variables_get'].customContextMenu
};

Blockly.Blocks['text_replace'] = {
  /**
   * Block for string replacement.
   * @this Blockly.Block
   */
  init: function() {
    this.setStyle('text_blocks');

    this.appendValueInput("STRING")
      .setCheck("String")
      .appendField("in text");

    this.appendValueInput("SEARCH")
      .setCheck("String")
      .appendField("replace");

    this.appendValueInput('REPLACE')
      .setCheck("String")
      .appendField("with");

    this.setInputsInline(true);
    this.setOutput(true,'String');
    this.setTooltip("Replaces all occurrences of a substring in a string with another");
  },
};