···11# Other extensions
2233-... instructions on how to built on top of the JupyterLab-Blockly extension
33+The JupyterLab-Blockly extension is ready to be used as a base for other projects: you can register new Blocks, Toolboxes and Generators. It is a great tool for fast prototyping.
44+55+## Creating a new JupyterLab extension
66+You can easily create a new JupyterLab extension by using a `cookiecutter`. You can read more documentation about `cookiecutters` [here](https://cookiecutter.readthedocs.io/en/latest/), but the process is fairly straight-forward.
77+88+After running the following command:
99+```
1010+cookiecutter https://github.com/jupyterlab/extension-cookiecutter-ts
1111+```
1212+ the `cookiecutter` will ask for some basic information about your project. Once completed, it will create a directory containing several files, all forming the base of your project. You will mostly work in the `index.ts` file, located in the `src` folder.
1313+1414+An example of creating a simple JupyterLab extension, which also contains the instructions of how to fill the information asked by the `cookiecutter`, can be found [here](https://github.com/jupyterlab/extension-examples/tree/master/hello-world).
1515+1616+1717+## Importing JupyterLab-Blockly
1818+Firstly you need to install and add `jupyterlab-blockly` as a dependency for your extension:
1919+```
2020+jlpm add jupyterlab-blockly
2121+```
2222+2323+Once it is part of your project, all you need to do is import `IBlocklyRegisty`, as it follows:
2424+```typescript
2525+// src/index.ts
2626+2727+import { IBlocklyRegisty } from 'jupyterlab-blockly';
2828+```
2929+3030+The `BlocklyRegistry` is the class that the JupyterLab-Blockly extension exposes to other plugins. This registry allows other plugins to register new Toolboxes, Blocks and Generators that users can use in the Blockly editor.
3131+3232+### Registering new Blocks
3333+The `IBlocklyRegisty` offers a function `registerBlocks`, which allows you to include new Blocks in your project. Blockly offers a [tool](https://blockly-demo.appspot.com/static/demos/blockfactory/index.html) which helps you easily create new Blocks and get their JSON definition and generator code in all supported programming languages.
3434+3535+**NOTE** : Once you create a new block, it won't appear into your Blockly editor, unless you add it to a Toolbox.
3636+3737+```typescript
3838+ /**
3939+ * Register new blocks.
4040+ *
4141+ * @argument blocks Blocks to register.
4242+ */
4343+ registerBlocks(blocks: JSONObject[]): void {
4444+ Blockly.defineBlocksWithJsonArray(blocks);
4545+ }
4646+```
4747+4848+### Registering a new Toolbox
4949+Using the `registerToolbox` function, provided by `IBlocklyRegisty`, you can register a new toolbox. Once registered, the toolbox will appear automatically in your Blockly editor. You can find more information about switching to another toolbox [here](https://jupyterlab-blockly.readthedocs.io/en/latest/toolbox.html).
5050+5151+```typescript
5252+/**
5353+ * Register a toolbox for the editor.
5454+ *
5555+ * @argument name Name of the toolbox.
5656+ *
5757+ * @argument value Toolbox to register.
5858+ */
5959+ registerToolbox(name: string, value: JSONObject): void {
6060+ this._toolboxes.set(name, value);
6161+ }
6262+```
6363+6464+### Registering a new Generator
6565+Lastly, `IBlocklyRegisty` offers the function `registerGenerator` which lets you register a new Generator. You can read more about switching kernels [here](https://jupyterlab-blockly.readthedocs.io/en/latest/kernels.html).
6666+6767+```typescript
6868+6969+ /**
7070+ * Register new generators.
7171+ *
7272+ * @argument name Name of the generator.
7373+ *
7474+ * @argument generator Generator to register.
7575+ *
7676+ * #### Notes
7777+ * When registering a generator, the name should correspond to the language
7878+ * used by a kernel.
7979+ *
8080+ * If you register a generator for an existing language this will be overwritten.
8181+ */
8282+ registerGenerator(name: string, generator: Blockly.Generator): void {
8383+ this._generators.set(name, generator);
8484+ }
8585+```
8686+8787+8888+## Example - JupyterLab-Niryo-One
8989+The [JupyterLab-Niryo-One](https://github.com/QuantStack/jupyterlab-niryo-one/) extension was built on top of JupyterLab-Blockly and poses as the perfect example. The [Github repository](https://github.com/QuantStack/jupyterlab-niryo-one/) gives access to its entire codebase.
9090+9191+The following code snippet showcases how to register a new toolbox, `BlocklyNiryo.Toolbox`, as `niryo`.
9292+```typescript
9393+// src/index.ts : 10-23
9494+9595+/**
9696+ * Initialization data for the jupyterlab-niryo-one extension.
9797+ */
9898+const plugin: JupyterFrontEndPlugin<void> = {
9999+ id: 'jupyterlab-niryo-one:plugin',
100100+ autoStart: true,
101101+ requires: [IBlocklyRegisty],
102102+ activate: (app: JupyterFrontEnd, blockly: IBlocklyRegisty) => {
103103+ console.log('JupyterLab extension jupyterlab-niryo-one is activated!');
104104+105105+ //Registering the new toolbox containing all Niryo One blocks.
106106+ blockly.registerToolbox('niryo', BlocklyNiryo.Toolbox);
107107+ }
108108+};
109109+```
110110+111111+**NOTE** : `BlocklyNiryo` is defined in `niryo-one-python-generators.ts`.
112112+113113+114114+## Include patches
115115+Currently, for the extension to work, you will need to include the following patch from the JupyterLab-Blockly extension (make sure it is placed in a file named `@jupyterlab+codeeditor+3.4.3.patch`, inside the `patches` folder):
116116+117117+```
118118+// patches/@jupyterlab+codeeditor+3.4.3.patch
119119+120120+diff --git a/node_modules/@jupyterlab/codeeditor/lib/editor.d.ts b/node_modules/@jupyterlab/codeeditor/lib/editor.d.ts
121121+index ffe8d1f..d63b2f8 100644
122122+--- a/node_modules/@jupyterlab/codeeditor/lib/editor.d.ts
123123++++ b/node_modules/@jupyterlab/codeeditor/lib/editor.d.ts
124124+@@ -44,7 +44,7 @@ export declare namespace CodeEditor {
125125+ /**
126126+ * An interface describing editor state coordinates.
127127+ */
128128+- interface ICoordinate extends JSONObject, ClientRect {
129129++ interface ICoordinate extends JSONObject {
130130+ }
131131+ /**
132132+ * A range.
133133+```
134134+135135+You will also need to modify the `MANIFEST.in` file:
136136+```
137137+recursive-include patches *.patch
138138+```
139139+the `package.json` file:
140140+```
141141+"scripts": {
142142+ ...
143143+ "postinstall": "patch-package"
144144+}
145145+````
146146+and, finally, add `patch-package` as a dependency:
147147+```
148148+jlpm add patch-package
149149+```
+3-5
packages/blockly/src/registry.ts
···6262 /**
6363 * Register new blocks.
6464 *
6565- * @argument name Name of the toolbox.
6666- *
6767- * @argument value Toolbox to register.
6565+ * @argument blocks Blocks to register.
6866 */
6967 registerBlocks(blocks: JSONObject[]): void {
7068 Blockly.defineBlocksWithJsonArray(blocks);
···7371 /**
7472 * Register new generators.
7573 *
7676- * @argument name Name of the toolbox.
7474+ * @argument name Name of the generator.
7775 *
7878- * @argument value Toolbox to register.
7676+ * @argument generator Generator to register.
7977 *
8078 * #### Notes
8179 * When registering a generator, the name should correspond to the language