Blocks for Java script comments

473 views
Skip to first unread message

Siman007

unread,
Oct 31, 2013, 12:14:07 PM10/31/13
to blo...@googlegroups.com
Hi I created the following blocks to add javascript comments and to inject pure code into the javascript file (i.e. whatever you type is injected into the javascript verbatim)

Both are very similar blocks BUT both suffer from the same problem i.e. the JavaScript produced always has a ';' appended at the end of the line of code - is there a way to prevent this?

Thanks

Si 


1) Comment Block

Blockly.Blocks['sds_comment'] = {
  // Text value.
  init: function() {
    this.setHelpUrl(Blockly.Msg.TEXT_TEXT_HELPURL);
    this.setColour(160);
    this.appendDummyInput()
        .appendTitle('Comment:')
        .appendTitle(new Blockly.FieldTextInput(''), 'TEXT');
    this.setPreviousStatement(true);
    this.setNextStatement(true);
    this.setTooltip(Blockly.Msg.TEXT_TEXT_TOOLTIP);
  }
};



And the JavaScript generator

Blockly.JavaScript['sds_comment'] = function(block) {
  // Text value.
  var code = block.getTitleValue('TEXT');
  return '//        ' + code+'\n';
};


2) Direct code inject block

Blockly.Blocks['sds_jsscript'] = {
  // Text value.
  init: function() {
    this.setHelpUrl(Blockly.Msg.TEXT_TEXT_HELPURL);
    this.setColour(160);
    this.appendDummyInput()
        .appendTitle('')
        .appendTitle(new Blockly.FieldTextInput(''), 'TEXT')
         .appendTitle(';');
    this.setOutput(true, 'String');
    this.setTooltip(Blockly.Msg.TEXT_TEXT_TOOLTIP);
  }
};

Blockly.JavaScript['sds_jsscript'] = function(block) {
  // Text value.
  var code = block.getTitleValue('TEXT');
  return [code, Blockly.JavaScript.ORDER_ATOMIC];
};

Ellen Spertus

unread,
Oct 31, 2013, 5:04:32 PM10/31/13
to blo...@googlegroups.com
There's already support for comments, although you have to right-click on a block to find it.  If you add a comment in Blockly, it will appear (without semicolon) in the generated code.

Ellen


--
You received this message because you are subscribed to the Google Groups "Blockly" group.
To unsubscribe from this group and stop receiving emails from it, send an email to blockly+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Siman007

unread,
Nov 1, 2013, 7:27:16 AM11/1/13
to blo...@googlegroups.com
Thanks again - I wanted a block for the comment so it shows up on the screen so people can see it without having to click on it to pick it up.
I guess there is noway to generate the code without a ; being added ?

Thanks

Simon

Ellen Spertus

unread,
Nov 1, 2013, 12:28:14 PM11/1/13
to blo...@googlegroups.com
On Fri, Nov 1, 2013 at 4:27 AM, Siman007 <willc...@googlemail.com> wrote:
Thanks again - I wanted a block for the comment so it shows up on the screen so people can see it without having to click on it to pick it up.

Fair enough.
 
I guess there is noway to generate the code without a ; being added ?

I don't believe so. 

Neil Fraser

unread,
Nov 1, 2013, 8:34:18 PM11/1/13
to blo...@googlegroups.com
On 31 October 2013 09:14, Siman007 <willc...@googlemail.com> wrote:
> Hi I created the following blocks to add javascript comments

Very nice. I think this is superior to the current comment bubbles.
If it were up to me I'd switch, but when we tried, there was strong
push-back from existing users. I'm still deciding whether this is a
battle that's worth fighting.

> and to inject pure code into the javascript file (i.e. whatever you type is injected into the javascript verbatim)

Hmm. This is an idea that's been discussed for years. The giant
stumbling block is that variables defined in Blockly are often renamed
when code is generated. Thus one is not sure what the variable and
function names are. Consider that Blockly allows a variable named
'+'. Solving this properly requires some interesting parsers.

> Both are very similar blocks BUT both suffer from the same problem i.e. the JavaScript produced
> always has a ';' appended at the end of the line of code - is there a way to prevent this?

Yes. The offending code is Blockly.JavaScript.scrubNakedValue. It
should only kick in if one is generating code from a top-level block
(not part of a program), that has a left output tab
(Blockly.Generator.prototype.workspaceToCode).

This should not apply to your comment block, so there is no reason you
should see a semicolon on code generated from this block. On the
other hand, if your code injection block does have a left output. If
it is not connected to other blocks and is orphaned on the workspace,
then it will have a semicolon appended (which is a good thing).

Please take another look at your code and verify what you are seeing. Thanks!

> return [code, Blockly.JavaScript.ORDER_ATOMIC];

Please change this to Blockly.JavaScript.ORDER_NONE. Otherwise
enclosing code may rip apart your code due to order of operations.
You have no idea what the minimum bond strength of your user's code
is, so to be safe you have to state that it has no cohesion.

--
Neil Fraser
http://neil.fraser.name
Reply all
Reply to author
Forward
0 new messages