• Stars
    star
    253
  • Rank 160,776 (Top 4 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created over 5 years ago
  • Updated 5 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A React component that embeds a Blockly visual programming editor.

react-blockly

Built on Blockly

A React library for embedding Blockly, the visual programming editor from Google.

This is a continuation of PatientsLikeMe's react-blockly-component project. This new version utilizes Google's official Blockly npm packages in addition to other modernizations.

Features:

  • Provides a hook as well as a function component for ease of use + flexibility
  • Supports the JSON toolbox format
  • Automatically propagates prop updates to Blockly
  • Provides callbacks for workspace injection, disposal, changes, and XML import errors
  • Automatically generates workspace XML, debounced for performance

VERSION 7 IS A COMPLETE REWRITE

This is the README for react-blockly version 7. If you're using the old class-based components, you will want to look at the version 6 readme instead: https://github.com/nbudin/react-blockly/blob/v6-stable/README.md

The breaking changes in react-blockly 7 are:

  • All components except BlocklyWorkspace have been removed
  • A hook for embedding Blockly in custom DOM has been added
  • The custom JSON format for toolbox has been removed in favor of Blockly's official toolbox JSON format
  • React 16.8 or higher is required
  • immutable.js has been removed

Example usage

FernandoVazZ has made an example React app that shows how to set up react-blockly as well as how to create custom blocks for use in it: https://github.com/FernandoVazZ/reactblockly-customblocks/

Thanks so much Fernando!

Help wanted!

I wrote react-blockly for an internal tool when I worked at PatientsLikeMe, Inc. I no longer maintain this tool, and I don't even have access to it anymore. I also don't have any actual project that uses react-blockly anymore.

I'd love to find maintainers for this package who actually use it. In particular, some things I would love to have help with are:

  • A test suite
  • Better documentation
  • More examples

If you are interested in working on any of those things, please let me know! Additionally, if you're interested in taking over maintainership of this package entirely, I'd be happy to talk to you about that.

Installation

To add react-blockly to a React app that uses npm, run:

npm install --save react-blockly

To add react-blockly to a React app that uses yarn, run:

yarn add react-blockly

How to use

You can use react-blockly as either a component or a hook. Embedding a component is often more straightforward and plugs into your app easily. On the other hand, using the hook can give you more control over exactly how Blockly is rendered.

Embedding a component

Write import { BlocklyWorkspace } from 'react-blockly'; in your code and use BlocklyWorkspace as a component.

Example:

import { BlocklyWorkspace } from 'react-blockly';

function MyBlocklyEditor() {
  const [xml, setXml] = useState();

  return (
    <BlocklyWorkspace
      className="width-100" // you can use whatever classes are appropriate for your app's CSS
      toolboxConfiguration={MY_TOOLBOX} // this must be a JSON toolbox definition
      initialXml={xml}
      onXmlChange={setXml}
    />
  )
}

Using the hook

Write import { useBlocklyWorkspace } from 'react-blockly'; in your code and use this hook to inject a Blockly workspace into your rendered components.

Example:

import { useBlocklyWorkspace } from 'react-blockly';

function MyBlocklyHookEmbed() {
  const blocklyRef = useRef(null);
  const { workspace, xml } = useBlocklyWorkspace({
    ref: blocklyRef,
    toolboxConfiguration: MY_TOOLBOX, // this must be a JSON toolbox definition
    initialXml: xml,
  });

  return (
    <div ref={blocklyRef} /> // Blockly will be injected here
  )
}

API reference

BlocklyWorkspace component props

All properties are optional.

  • initialXml: The XML of the program to initially load in the editor.
  • workspaceConfiguration: Any configuration options to be passed into Blockly.inject (except for toolbox, which is a separate prop).
  • toolboxConfiguration: A JSON toolbox configuration (see the Blockly documentation for details on this format).
  • className: The value for the class attribute to be used on the <div> elements generated by this component. Typically you'll need to use this to set the height of the Blockly editor, using either an explicit height style, flexboxes, or some other means.
  • onWorkspaceChange: A function called every time the content of the workspace changes. It should take a single argument, which is the Blockly workspace object. (You can call methods such as Blockly.JavaScript.workspaceToCode on this object.)
  • onXmlChange: A function called every time the content of the workspace, debounced to be called at most once every 200 milliseconds. This function should take a single argument, which is the new XML generated from the workspace.
  • onImportXmlError: A function called if initialXml can't be imported. This function takes a single argument, which is the error thrown during XML import.
  • onInject: A function called after the Blockly workspace is injected. This function takes a single argument, which is the newly-injected Blockly workspace object. This is a good place to add Blockly plugins, if desired.
  • onDispose: A function called after the Blockly workspace is disposed and removed from the page. This function takes a single argument, which is the just-disposed Blockly workspace object. Some Blockly plugins need to use this to dispose their own resources.

useBlocklyWorkspace

useBlocklyWorkspace(options) -> { workspace, xml };

Options object

All properties are optional, except ref, which must be passed.

  • ref: REQUIRED A React ref object (created via useRef) pointing at an HTML element where Blockly will be injected.
  • initialXml: The XML of the program to initially load in the editor.
  • workspaceConfiguration: Any configuration options to be passed into Blockly.inject (except for toolbox, which is a separate prop).
  • toolboxConfiguration: A JSON toolbox configuration (see the Blockly documentation for details on this format).
  • onWorkspaceChange: A function called every time the content of the workspace changes. It should take a single argument, which is the Blockly workspace object. (You can call methods such as Blockly.JavaScript.workspaceToCode on this object.)
  • onImportXmlError: A function called if initialXml can't be imported. This function takes a single argument, which is the error thrown during XML import.
  • onInject: A function called after the Blockly workspace is injected. This function takes a single argument, which is the newly-injected Blockly workspace object. This is a good place to add Blockly plugins, if desired.
  • onDispose: A function called after the Blockly workspace is disposed and removed from the page. This function takes a single argument, which is the just-disposed Blockly workspace object. Some Blockly plugins need to use this to dispose their own resources.

Hook return value

useBlocklyWorkspace returns an object containing:

  • workspace: The Blockly workspace object
  • xml: XML generated from the content of the Blockly workspace, debounced to update at most once every 200 milliseconds

Developer setup for working on this package

Clone this repository, and then inside it, do:

yarn install
yarn run start

webpack-dev-server will start and will be serving a demo of react-blockly, which should automatically refresh if you change the code locally.

Example usage

See public/index.html and src/dev-index.jsx for a fairly full-fledged demo that shows off most of the features of this component.

Contributing

We accept pull requests and issues! You can file an issue on this project, or fork, modify, and file a pull request.

Please note that this project is released with a Contributor Code of Conduct. By participating in this project you agree to abide by its terms.

Licensing

react-blockly is Copyright Β© 2019-2021 Nat Budin. Distributed under the terms and conditions of the MIT License.

react-blockly is based on react-blockly-component, which is Copyright Β© 2015 PatientsLikeMe, Inc. Distributed under the terms and conditions of the MIT License.

More Repositories

1

devise_cas_authenticatable

CAS authentication support for Devise
Ruby
286
star
2

devise_openid_authenticatable

OpenID authentication for Devise
Ruby
99
star
3

heroku_external_db

Makes it easy to connect to external databases from a Heroku app
Ruby
40
star
4

google4r-checkout

Google Checkout library for Ruby
Ruby
39
star
5

react-bootstrap4-modal

A very simple Bootstrap 4 modal dialog component for React
TypeScript
20
star
6

html2pdf

A command-line HTML-to-PDF converter for Mac OS X
Objective-C
20
star
7

journey

An online questionnaire application
Ruby
13
star
8

authlogic_pam

PAM authentication support for authlogic
Ruby
13
star
9

blockly_interpreter

An interpreter for Blockly programs in Ruby
Ruby
10
star
10

videojs-rails

This gem is deprecated. Please use videojs_rails by Sean Behan instead.
JavaScript
8
star
11

agikit

A set of developer tools for the Sierra AGI adventure game engine
TypeScript
8
star
12

procon

Awesome registration for awesome events
Ruby
6
star
13

devise_openid_example

A version of devise_example using devise_openid_authenticatable
Ruby
5
star
14

ec2-delete-old-snapshots-ruby

Ruby port of ec2-delete-old-snapshots.php
Ruby
4
star
15

vellum

A collaborative writing and mind-mapping application for the web
Ruby
4
star
16

rpp

Ruby library for parsing REAPER project files
Ruby
4
star
17

ae_users

An authentication and authorization plugin for Rails
Ruby
3
star
18

illyan

A central user account management app
Ruby
3
star
19

jipe

Jester In-Place Editing controls
JavaScript
2
star
20

ponzi

A household financial management tool
Ruby
2
star
21

union_bug

Advertise your union membership in work Slack
TypeScript
2
star
22

agikit-project-template

2
star
23

minitrack

A simple HTML5 calorie tracker for mobile phones
JavaScript
2
star
24

google_code_issues_import

Import google code issues to github
Ruby
2
star
25

ae_forms

A simple CSS-based form generator plugin for Rails
2
star
26

ae_users_migrator

Migration tool for legacy ae_users database
Ruby
1
star
27

stranger-ways-middleman

The Stranger Ways web site
HTML
1
star
28

incant

A new command line
C#
1
star
29

vellum-ipad

Vellum app for iPad
Objective-C
1
star
30

say-it-20

Say It 2.0 theme for WordPress - Nat Budin's fork
PHP
1
star
31

traverse

A web application for creating decision tree-based games
Ruby
1
star
32

cantrip

Computer-assisted casting for LARPs using the Alleged Entertainment casting algorithm
Ruby
1
star
33

sublime-flog-highlighter

Ruby complexity analysis for Sublime Text
Python
1
star
34

radiant-ae_users-authenticator

ae_users authentication plugin for Radiant CMS
Ruby
1
star
35

rblineprof-browser

A console-based browser for rblineprof results
Ruby
1
star
36

home

Standard layout for Nat's home directory
1
star
37

illyan_client

A REST API client for Illyan
Ruby
1
star
38

FireEngine

Objective-C
1
star