Skip to content

Commit

Permalink
feat!: add block definition to multiline text field plugin (#2202)
Browse files Browse the repository at this point in the history
* chore: force-install blockly 10.4.0-beta.1 for development

* feat: add text_multiline block and associated generators

* feat: update test page to use new block and field

* feat: README

* chore: respond to PR feedback

BREAKING CHANGE: The multiline text input field no longer registers itself on load. The developer must either manually register the field or install blocks, which will install the field. This is part of a move to have no side effects in field and block definitions, so that tree-shaking can remove unwanted fields and blocks.
  • Loading branch information
rachel-fenichel authored Feb 23, 2024
1 parent 44d3671 commit c29c022
Show file tree
Hide file tree
Showing 9 changed files with 12,121 additions and 815 deletions.
45 changes: 42 additions & 3 deletions plugins/field-multilineinput/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# @blockly/field-multilineinput [![Built on Blockly](https://tinyurl.com/built-on-blockly)](https://github.com/google/blockly)

A [Blockly](https://www.npmjs.com/package/blockly) multilineinput field.
A [Blockly](https://www.npmjs.com/package/blockly) multilineinput field and associated block.

## Installation

Expand All @@ -18,6 +18,8 @@ npm install @blockly/field-multilineinput --save

## Usage

### Field

This field accepts up to 3 parameters:

- "text" to specify the default text. Defaults to `""`.
Expand All @@ -28,12 +30,18 @@ This field accepts up to 3 parameters:

The multiline input field is a subclass of Blockly.FieldInput

If you want to use only the field, you must register it with Blockly. You can
do this by calling `registerFieldMultilineInput` before instantiating your
blocks. If another field is registered under the same name, this field will
overwrite it.

### JavaScript

```js
import * as Blockly from 'blockly';
import {FieldMultilineInput} from '@blockly/field-multilineinput';
import {registerFieldMultilineInput} from '@blockly/field-multilineinput';

registerFieldMultilineInput();
Blockly.Blocks['test_field_multilineinput'] = {
init: function () {
this.appendDummyInput()
Expand All @@ -50,8 +58,9 @@ Blockly.Blocks['test_field_multilineinput'] = {

```js
import * as Blockly from 'blockly';
import '@blockly/field-multilineinput';
import {registerFieldMultilineInput} from '@blockly/field-multilineinput';

registerFieldMultilineInput();
Blockly.defineBlocksWithJsonArray([
{
"type": "test_field_multilineinput",
Expand All @@ -65,6 +74,36 @@ Blockly.defineBlocksWithJsonArray([
}]);
```

### Blocks

This package also provides a simple block containing a multiline input
field. It has generators in JavaScript, Python, PHP, Lua, and Dart.

You can install the block by calling `textMultiline.installBlock()`.
This will install the block and all of its dependencies, including the
multiline input field. When calling `installBlock` you can supply one or
more `CodeGenerator` instances (e.g. `javascriptGenerator`), and the install
function will also install the correct generator function for the
corresponding language(s).

```js
import {javascriptGenerator} from 'blockly/javascript';
import {dartGenerator} from 'blockly/dart';
import {phpGenerator} from 'blockly/php';
import {pythonGenerator} from 'blockly/python';
import {luaGenerator} from 'blockly/lua';
import {textMultiline} from '@blockly/field-multilineinput';

// Installs the block, the field, and all of the language generators.
textMultiline.installBlock({
javascript: javascriptGenerator,
dart: dartGenerator,
lua: luaGenerator,
python: pythonGenerator,
php: phpGenerator,
});
```

### API reference

- `setMaxLines`: Sets the maximum number of displayed lines before
Expand Down
Loading

0 comments on commit c29c022

Please # to comment.