|
| 1 | +--- |
| 2 | +title: VanillaJS Usage |
| 3 | +--- |
| 4 | + |
| 5 | +# VanillaJS Usage |
| 6 | + |
| 7 | +<a href="https://microbit-foundation.github.io/makecode-embed/" class="typedoc-ignore">This documentation is best viewed on the documentation site rather than GitHub or NPM package site.</a> |
| 8 | + |
| 9 | +## Blocks rendering |
| 10 | + |
| 11 | +Use {@link vanilla.createMakeCodeRenderBlocks | createMakeCodeRenderBlocks} to create a MakeCode block renderer. Initialise the renderer before calling `renderBlocks` with a {@link vanilla.RenderBlocksRequest | RenderBlocksRequest}, which includes a MakeCode project ([see examples](../src/stories/fixtures.ts)). The function will return a {@link vanilla.RenderBlocksResponse | RenderBlocksResponse}. |
| 12 | + |
| 13 | +```js |
| 14 | +import { createMakeCodeRenderBlocks } from "@microbit/makecode-embed/vanilla"; |
| 15 | + |
| 16 | +const renderer = createMakeCodeRenderBlocks({}); |
| 17 | +renderer.initialize(); |
| 18 | +const result = await renderer.renderBlocks({ code: makeCodeProject }); |
| 19 | + |
| 20 | +document.querySelector<HTMLDivElement>("#app")!.innerHTML = ` |
| 21 | + <div> |
| 22 | + ${result.svg} |
| 23 | + </div> |
| 24 | +`; |
| 25 | +``` |
| 26 | + |
| 27 | +For more examples, take a look at the [MakeCode blocks rendering demo source code](../src/stories/vanilla/makecode-render-blocks.stories.tsx). |
| 28 | + |
| 29 | +## Embed MakeCode editor |
| 30 | + |
| 31 | +Use {@link vanilla.MakeCodeFrameDriver | MakeCodeFrameDriver} class to create a driverRef for an iframe element. The iframe element src URL can be generated using {@link vanilla.createMakeCodeURL | createMakeCodeURL}. |
| 32 | + |
| 33 | +```js |
| 34 | +import { |
| 35 | + Project, |
| 36 | + MakeCodeFrameDriver, |
| 37 | + createMakeCodeURL, |
| 38 | +} from "@microbit/makecode-embed/vanilla"; |
| 39 | + |
| 40 | +// Set up an iframe element. |
| 41 | +const iframe = document.createElement("iframe"); |
| 42 | +iframe.allow = "usb; autoplay; camera; microphone;"; |
| 43 | +iframe.src = createMakeCodeURL( |
| 44 | + "https://makecode.microbit.org", |
| 45 | + undefined, // Version. |
| 46 | + undefined, // Language. |
| 47 | + 1, // Controller. |
| 48 | + undefined // Query params. |
| 49 | +); |
| 50 | +iframe.width = "100%"; |
| 51 | +iframe.height = "100%"; |
| 52 | + |
| 53 | +document.querySelector<HTMLDivElement>("#app")!.appendChild(iframe); |
| 54 | + |
| 55 | +// Create and initialise an instance of MakeCodeFrameDriver. |
| 56 | +const driverRef = new MakeCodeFrameDriver( |
| 57 | + { |
| 58 | + controllerId: "YOUR APP NAME HERE", |
| 59 | + initialProjects: async () => [makeCodeProject], |
| 60 | + onEditorContentLoaded: (e) => console.log("MakeCode is now ready"), |
| 61 | + onWorkspaceSave: (e) => { |
| 62 | + console.log(e.project!.header!.id, e.project); |
| 63 | + }, |
| 64 | + }, |
| 65 | + () => iframe |
| 66 | +); |
| 67 | +driverRef.initialize(); |
| 68 | +``` |
| 69 | + |
| 70 | +For more examples, take a look at the [MakeCode frame demo source code](../src/stories/vanilla/makecode-frame-driver.stories.tsx). |
0 commit comments