Use ES6 for code in extension docs

This commit is contained in:
Christopher Willis-Ford 2019-04-05 15:00:09 -07:00
parent 2a5abe028f
commit 18f3307ed0

View file

@ -9,8 +9,9 @@ wiki](https://github.com/LLK/docs/wiki/Extensions).
## Types of Extensions
There are four types of extensions that can define everything from the Scratch's core library (such as the "Looks" and
"Operators" categories) to unofficial extensions that can be loaded from a remote URL. Unofficial extensions are not
yet supported.
"Operators" categories) to unofficial extensions that can be loaded from a remote URL.
**Scratch 3.0 does not yet support unofficial extensions.**
For more details, see [this Extensions page on the wiki](https://github.com/LLK/docs/wiki/Extensions).
@ -22,6 +23,15 @@ For more details, see [this Extensions page on the wiki](https://github.com/LLK/
| Sandboxed | X | X | √ | √ |
| Can save projects to community | √ | √ | √ | X |
## JavaScript Environment
Most Scratch 3.0 is written using JavaScript features not yet commonly supported by browsers. For compatibility we
transpile the code to ES5 before publishing or deploying. Any extension included in the `scratch-vm` repository may
use ES6+ features and may use `require` to reference other code within the `scratch-vm` repository.
Unofficial extensions must be self-contained. Authors of unofficial extensions are responsible for ensuring browser
compatibility for those extensions, including transpiling if necessary.
## Translation
Scratch extensions use the [ICU message format](http://userguide.icu-project.org/formatparse/messages) to handle
@ -46,12 +56,17 @@ Scratch extensions are defined as a single Javascript class which accepts either
across a well defined worker boundary (i.e. the sandbox).
```js
var SomeBlocks = function (runtimeProxy) {
class SomeBlocks {
constructor (runtime) {
/**
* A proxy to communicate with the Scratch 3.0 runtime across a worker boundary.
* Store this for later communication with the Scratch VM runtime.
* If this extension is running in a sandbox then `runtime` is an async proxy object.
* @type {Runtime}
*/
this.runtime = runtimeProxy;
this.runtime = runtime;
}
// ...
}
```
@ -59,7 +74,9 @@ All extensions must define a function called `getInfo` which returns an object t
render both the blocks and the extension itself.
```js
SomeBlocks.prototype.getInfo = function () {
class SomeBlocks {
// ...
getInfo () {
return {
id: 'someBlocks',
name: 'Some Blocks',
@ -81,32 +98,40 @@ SomeBlocks.prototype.getInfo = function () {
}
]
};
};
}
// ...
}
```
Finally the extension must define a function for any "opcode" defined in the blocks. For example:
```js
SomeBlocks.prototype.myReporter = function (args) {
class SomeBlocks {
// ...
myReporter (args) {
return args.TEXT.charAt(args.LETTER_NUM);
};
// ...
}
```
## Annotated Example
```js
var SomeBlocks = function (runtimeProxy) {
class SomeBlocks {
constructor (runtime) {
/**
* A proxy to communicate with the Scratch 3.0 runtime across a worker boundary.
* Store this for later communication with the Scratch VM runtime.
* If this extension is running in a sandbox then `runtime` is an async proxy object.
* @type {Runtime}
*/
this.runtime = runtimeProxy;
};
this.runtime = runtime;
}
/**
* @return {object} This extension's metadata.
*/
SomeBlocks.prototype.getInfo = function () {
getInfo () {
return {
// Required: the machine-readable name of this extension.
// Will be used as the extension's namespace.
@ -298,7 +323,7 @@ SomeBlocks.prototype.getInfo = function () {
* @property {string} MY_ARG - the string value of the argument.
* @returns {string} a string which includes the block argument value.
*/
SomeBlocks.prototype.myReporter = function (args) {
myReporter (args) {
// This message contains ICU placeholders, not Scratch placeholders
const message = formatMessage({
id: 'myReporter.result',
@ -315,4 +340,5 @@ SomeBlocks.prototype.myReporter = function (args) {
LETTER: result
});
};
}
```