Skip to content

Conversation

@pumpkinhasapatch
Copy link
Contributor

@pumpkinhasapatch pumpkinhasapatch commented Aug 23, 2024

Resolves #2336
Closes #7988

Changes

This addon adds a right-click option to blocks in the Scratch Project Editor to convert them into scratchblocks text pseudocode. It finds the JSON code of the selected blocks loaded in the Scratch editor and uses parse-sb3-blocks by @apple502j to convert them into scratchblocks text, then copies it to the user's clipboard for easy use on other websites or text editors.

image

Features:

  • Fast - Tested on large stacks of blocks, quickly converted them with little performance impact.
  • Accurate - parse-sb3-blocks adds special formatting to some code like < [] \> [] > and turn @turnRight (15) degrees::motion so your scratchblocks are always rendered correctly.
  • Supports all blocks and extensions in vanilla Scratch. (See TurboWarp discussion for potential issues with porting the addon)
  • Multiple-language support (scratchblocks output follows the Scratch editor Language setting)
    Screenshot from 2024-10-16 23-17-12
  • Optionally add code formatting tags like [scratchblocks] around generated text to easily paste it on Discussion Forums or Scratch Wiki.

Reason for changes

It would be convenient for users to be able to copy and paste blocks from the Project Editor in places where only scratchblocks text is supported, like the Scratch Forums using BBCode tags or Scratch Wiki pages using the Block Plugin. There is also the scratchblocks playground for testing the output with 2.0 and 3.0 style blocks in different languages.

Tests

Tested on latest Chromium with Scratch Addons v1.39.0-pre and v1.40.0-pre.

Download addon fork - Installation instructions

Known issues:

  • Block input serialization errors (resolved)
  • Right-clicking reporters or boolean blocks and using the addon throws console error from parse-sb3-blocks or gives blank output (minor)
  • Blocks in languages other than English do not render correctly on the Forums. (work around with "forceEnglish" addon setting)

@Samq64 Samq64 added type: enhancement New feature for the project new addon Related to new addons to this extension. `scope: addon` should still be added. scope: addon Related to one or multiple addons labels Aug 23, 2024
@WorldLanguages
Copy link
Member

@TheColaber Do you know how to fix the bugs mentioned in this PR?

@TheColaber
Copy link
Member

i am not familiar with this library, maybe @apple502j can help since they made it. otherwise, the best solution to make a library that converts ScratchBlocks to bbcode.

@mxmou
Copy link
Member

mxmou commented Aug 24, 2024

I think the right way to implement this is to serialize the sprite to JSON, then pass the result to the parse-sb3-blocks library.

@Samq64
Copy link
Member

Samq64 commented Oct 3, 2024

The library should be minified and stored in /libraries/thirdparty/cs but even then it's 1.7MB which is quite large considering the whole extension is 20MB uncompressed.

@pumpkinhasapatch
Copy link
Contributor Author

pumpkinhasapatch commented Oct 15, 2024

The library should be minified and stored in /libraries/thirdparty/cs but even then it's 1.7MB which is quite large considering the whole extension is 20MB uncompressed.

The parse-sb3-blocks library was already minified. When I uploaded it here the GitHub Actions code bot tried to format the entire minified file and slightly increased the file size. (Now fixed by moving it to /libraries/thirdparty which is ignored by .eslintrc.json.)

The library is also very big because it contains the text for all blocks in every language Scratch supports. Blocks in languages other than English do not render correctly on the Discussion Forums and I could remove other languages from parse-sb3-blocks, but they might be useful for some people on Scratch Wiki or other places where scratchblocks is supported.

@pumpkinhasapatch pumpkinhasapatch changed the title New addon: Copy scratchblocks text from editor blocks New addon: Copy scratchblocks text from editor Oct 16, 2024
@pumpkinhasapatch pumpkinhasapatch marked this pull request as ready for review October 16, 2024 17:18
@pumpkinhasapatch
Copy link
Contributor Author

Any more feedback?

@Samq64
Copy link
Member

Samq64 commented Nov 5, 2024

I'm not sure if these are bugs with the library but the stop block includes the text {STOP_OPTION} for some reason and copying the translate blocks throws the error Cannot read properties of undefined (reading 'toScratchblocks') in the library.

@pumpkinhasapatch
Copy link
Contributor Author

pumpkinhasapatch commented Nov 17, 2024

I'm not sure if these are bugs with the library but the stop block includes the text {STOP_OPTION} for some reason and copying the translate blocks throws the error Cannot read properties of undefined (reading 'toScratchblocks') in the library.

@Samq64 These are issues with the parse-sb3-blocks library. All Scratch blocks are defined in this block mapping: {STOP_OPTION} is a placeholder for the stop block menu, for some reason the menu text is added after {STOP_OPTION} instead of replacing it, giving the text stop {STOP_OPTION} [all v] on this one block. We should create an issue on Apple's repo for this.

The addon does not work on loose reporter or boolean blocks. The library only supports square-shaped parent blocks, so you have to put other types of blocks inside an input to properly recognise them and get the block text. This should not affect complete scripts but I could make a workaround in the addon's code that fixes this.

My addon just finds selected blocks in the Scratch editor and gives it to the library, the addon does not do the actual block-to-text conversion.

@NeuronPulse
Copy link

May I ask if this process can be reversed, i.e., from Scratchblocks text to blocks? For example, the graphical kittenN editor similar to scratch, they can convert blocks and text, which is a very useful function.

@Samq64
Copy link
Member

Samq64 commented Oct 3, 2025

No, but there's an issue for that: #8573

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

new addon Related to new addons to this extension. `scope: addon` should still be added. scope: addon Related to one or multiple addons type: enhancement New feature for the project

Projects

None yet

Development

Successfully merging this pull request may close these issues.

New editor addon: copy block/stack to clipboard (scratchblocks/BBCode)

6 participants