Git Product home page Git Product logo

parse-sb3-blocks's Introduction

parse-sb3-blocks

parse-sb3-blocks parses Scratch 3.0 block formats, and convert it to scratchblocks format.

Usage

Example

import {toScratchblocks} from 'parse-sb3-blocks';

const sb3blocks = {
    'ND,(]G?KLIy(IZrd2sl.': {
        opcode: 'event_whenflagclicked',
        ...
    }
};

console.log(toScratchblocks('ND,(]G?KLIy(IZrd2sl.', sb3blocks, 'en', {tabs: ' '.repeat(4)}));

toScratchblocks

toScratchblocks is a function. This can take three to four arguments:

  • scriptStart: The block ID to start parsing from. This must be a block ID of Connectable (which includes hat blocks.)
  • blocks: serialized SB3 format (project.json format) of blocks.
  • locale: Locale to use. en should always be available.
  • opts: Optional. It can be an object (see below)

Options

opts can have these properties.

  • tabs: Tab characters used by C/E blocks for indenting. Note that while parse-sb3-blocks can use non-space/tab indent, scratchblocks only accepts tab or spaces. Pass empty string to remove indents. Defaults to four spaces.
  • variableStyle: If set to none (default), variables will never have ::variables at the end. If set to always, it will always have ::variables. If set to as-needed, it will have ::variables if block name conflicts.

Internal Functions and Parsers

It also exports several classes used internally by the parsers. These are not APIs and is subject to changes at any time.

Inputtable and Connectable

Connectable is an instance that can be connected to a stack block via next-parent, including:

  • Block (stack, hat, cap)
  • CBlock
  • EBlock (if-else)
  • Definition
  • ProcedureCall

Inputtable can be used as an argument. Note that Icon is not technically an argument, but is Inputtable.

  • Stack (for arguments to C/E blocks)
  • Menu (includes fields menu and menu block)
  • Variable (variable reporters and custom block arguments)
  • Icon (greenFlag, turnLeft, turnRight icon)
  • BooleanBlock
  • ReporterBlock
  • Input and its subclasses NumberInput, StringInput, ColorPickerInput, BroadcastMenuInput, EmptyBooleanInput

Both Connectable and Inputtable implement toScratchblocks method. It accepts locale and opts, similar to the exported toScratchblocks.

Instances with class name ending with "Block" have these attributes:

  • id for block ID
  • opcode for block opcode
  • inputtables for object of input key to Inputtable

Block Mapping

block-enum.js provides an enum for block types.

translations.js is an auto-generated file which includes all translations. options.js is also auto-generated, and contains which block needs options (because of conflicting names).

Only blocks and menu items in all-blocks.js are supported.

all-blocks.js

all-blocks.js default-exports allBlocks, which is an object with opcode as key and the object (see below) as value.

The object has these keys and values:

  • noTranslation: If set to true, it will be ignored from the translation generator.
  • defaultMessage: Message in default language (English).
  • type: The block type. Defaults to BlockEnum.BLOCK.
  • defaultOptions: The block option for the block when using defaultMessage, in case duplicates exist. It can have category key and the category as a value.
  • translationKey: Translation key for scratch-l10n. Defaults to the block opcode, upper-cased.
  • boolArg: Array of arguments which must be boolean. Used to fill empty arguments with EmptyBooleanInput. If omitted, empty array is used.

Building

Node 16+ is required for building. Type npm run build, which generates locale files.

env

  • mode: Unless set to dev, the result is minified. Add docs to update the JS file used in demo.

parse-sb3-blocks's People

Contributors

apple502j avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.