mirror of
https://github.com/scratchfoundation/scratch-vm.git
synced 2025-07-11 21:43:58 -04:00
Use ES6 for code in extension docs
This commit is contained in:
parent
2a5abe028f
commit
18f3307ed0
1 changed files with 250 additions and 224 deletions
|
@ -9,8 +9,9 @@ wiki](https://github.com/LLK/docs/wiki/Extensions).
|
||||||
## Types of 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
|
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
|
"Operators" categories) to unofficial extensions that can be loaded from a remote URL.
|
||||||
yet supported.
|
|
||||||
|
**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).
|
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 | √ | √ |
|
| Sandboxed | X | X | √ | √ |
|
||||||
| Can save projects to community | √ | √ | √ | 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
|
## Translation
|
||||||
|
|
||||||
Scratch extensions use the [ICU message format](http://userguide.icu-project.org/formatparse/messages) to handle
|
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).
|
across a well defined worker boundary (i.e. the sandbox).
|
||||||
|
|
||||||
```js
|
```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}
|
* @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.
|
render both the blocks and the extension itself.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
SomeBlocks.prototype.getInfo = function () {
|
class SomeBlocks {
|
||||||
|
// ...
|
||||||
|
getInfo () {
|
||||||
return {
|
return {
|
||||||
id: 'someBlocks',
|
id: 'someBlocks',
|
||||||
name: 'Some Blocks',
|
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:
|
Finally the extension must define a function for any "opcode" defined in the blocks. For example:
|
||||||
|
|
||||||
```js
|
```js
|
||||||
SomeBlocks.prototype.myReporter = function (args) {
|
class SomeBlocks {
|
||||||
|
// ...
|
||||||
|
myReporter (args) {
|
||||||
return args.TEXT.charAt(args.LETTER_NUM);
|
return args.TEXT.charAt(args.LETTER_NUM);
|
||||||
};
|
};
|
||||||
|
// ...
|
||||||
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
## Annotated Example
|
## Annotated Example
|
||||||
|
|
||||||
```js
|
```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}
|
* @type {Runtime}
|
||||||
*/
|
*/
|
||||||
this.runtime = runtimeProxy;
|
this.runtime = runtime;
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return {object} This extension's metadata.
|
* @return {object} This extension's metadata.
|
||||||
*/
|
*/
|
||||||
SomeBlocks.prototype.getInfo = function () {
|
getInfo () {
|
||||||
return {
|
return {
|
||||||
// Required: the machine-readable name of this extension.
|
// Required: the machine-readable name of this extension.
|
||||||
// Will be used as the extension's namespace.
|
// 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.
|
* @property {string} MY_ARG - the string value of the argument.
|
||||||
* @returns {string} a string which includes the block argument value.
|
* @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
|
// This message contains ICU placeholders, not Scratch placeholders
|
||||||
const message = formatMessage({
|
const message = formatMessage({
|
||||||
id: 'myReporter.result',
|
id: 'myReporter.result',
|
||||||
|
@ -315,4 +340,5 @@ SomeBlocks.prototype.myReporter = function (args) {
|
||||||
LETTER: result
|
LETTER: result
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
}
|
||||||
```
|
```
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue