Complete API reference for the LiveCodes Playground class
The Playground interface represents the LiveCodes playground instance returned by createPlayground(). It exposes methods to interact with the playground programmatically.
When the embed option loading is set to "click", the playground is not loaded automatically. Instead, a screen is shown with a “Click to load” button. Calling the SDK method load() allows loading the playground.If the playground was not loaded, calling any other method will load the playground first before executing.
import { createPlayground } from "livecodes";const playground = await createPlayground("#container", { loading: "click",});// Load the playground programmaticallyawait playground.load();
Executes the current code and displays the result page.
import { createPlayground } from "livecodes";const playground = await createPlayground("#container");await playground.run();// new result page is displayed
By default, the code in all editors (markup, style and script) is formatted. To format only the active editor, pass false.
import { createPlayground } from "livecodes";const playground = await createPlayground("#container");// Format all editorsawait playground.format();// Format only active editorawait playground.format(false);
The configuration object representing the playground state. This can be used to restore state if passed as an EmbedOptions property when creating playgrounds, or can be manipulated and loaded in run-time using setConfig method.
import { createPlayground } from "livecodes";const playground = await createPlayground("#container");// Show the style editorawait playground.show("style");// Toggle result paneawait playground.show("toggle-result");// Show result in full screenawait playground.show("result", { full: true });// Show script editor at line 10await playground.show("script", { line: 10 });// Show result at 50% zoomawait playground.show("result", { zoom: 0.5 });// Show console in full screenawait playground.show("console", { full: true });
Takes 2 arguments: event name and a callback function that will be called on every event.Event name can be one of: "load" | "ready" | "code" | "console" | "tests" | "destroy"In some events, the callback function receives an object with relevant data.Returns an object with a remove() method to stop watching the event.
import { createPlayground } from "livecodes";const playground = await createPlayground("#container");const codeWatcher = playground.watch("code", ({ code, config }) => { // This will run on every code change console.log("code:", code); console.log("config:", config);});const consoleWatcher = playground.watch("console", ({ method, args }) => { // This will run on every console output console[method](...args);});const testsWatcher = playground.watch("tests", ({ results }) => { // This will run when tests run results.forEach((testResult) => { console.log("status:", testResult.status); console.log(testResult.errors); });});// Remove watchers when donecodeWatcher.remove();consoleWatcher.remove();testsWatcher.remove();
Further calls to any SDK methods will throw an error after destruction.
import { createPlayground } from "livecodes";const playground = await createPlayground("#container");await playground.destroy();// Playground destroyed// Any further SDK call throws an error