Skip to main content
The Playground interface represents the LiveCodes playground instance returned by createPlayground(). It exposes methods to interact with the playground programmatically.

Methods

load()

Loads the playground if not already loaded.
load
() => Promise<void>
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 programmatically
await playground.load();

run()

Runs the result page after any required compilation for code.
run
() => Promise<void>
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

format()

Formats the code in the editors.
format
(allEditors?: boolean) => Promise<void>
import { createPlayground } from "livecodes";

const playground = await createPlayground("#container");

// Format all editors
await playground.format();

// Format only active editor
await playground.format(false);

getShareUrl()

Gets a share URL for the current project.
getShareUrl
(shortUrl?: boolean) => Promise<string>
import { createPlayground } from "livecodes";

const playground = await createPlayground("#container");
const longUrl = await playground.getShareUrl();
const shortUrl = await playground.getShareUrl(true);

getConfig()

Gets a configuration object representing the playground state.
getConfig
(contentOnly?: boolean) => Promise<Config>
import { createPlayground } from "livecodes";

const playground = await createPlayground("#container");
const config = await playground.getConfig();
const contentOnlyConfig = await playground.getConfig(true);

setConfig()

Loads a new project using the passed configuration object.
setConfig
(config: Partial<Config>) => Promise<Config>
import { createPlayground } from "livecodes";

const playground = await createPlayground("#container");
const config = {
  markup: {
    language: "html",
    content: "Hello World!",
  },
};
const newConfig = await playground.setConfig(config);
// new project loaded

getCode()

Gets the playground code for each editor (markup, style, script) and result page HTML.
getCode
() => Promise<Code>
import { createPlayground } from "livecodes";

const playground = await createPlayground("#container");
const code = await playground.getCode();

// Source code, language and compiled code for the script editor
const { content, language, compiled } = code.script;

// Result page HTML
const result = code.result;

show()

Shows the selected panel.
show
(panel: EditorId | 'editor' | 'result' | 'toggle-result' | Tool['name'], options?: ShowOptions) => Promise<void>
import { createPlayground } from "livecodes";

const playground = await createPlayground("#container");

// Show the style editor
await playground.show("style");

// Toggle result pane
await playground.show("toggle-result");

// Show result in full screen
await playground.show("result", { full: true });

// Show script editor at line 10
await playground.show("script", { line: 10 });

// Show result at 50% zoom
await playground.show("result", { zoom: 0.5 });

// Show console in full screen
await playground.show("console", { full: true });

runTests()

Runs project tests (if present) and gets test results.
runTests
() => Promise<{ results: TestResult[] }>
import { createPlayground } from "livecodes";

const playground = await createPlayground("#container");
const { results } = await playground.runTests();

results.forEach((result) => {
  console.log(result.status); // "pass", "fail", or "skip"
  console.log(result.errors); // array of error strings
});

watch()

Allows watching for various playground events.
watch
WatchFn
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.

Watch Events

load
(event: 'load', fn: () => void) => { remove: () => void }
Called when the playground first loads.
ready
(event: 'ready', fn: (data: { config: Config }) => void) => { remove: () => void }
Called when a new project is loaded (including when imported) and the playground is ready to run.
code
(event: 'code', fn: (data: { code: Code; config: Config }) => void) => { remove: () => void }
Called when the playground content changes. This includes changes in:
  • Code (in editors)
  • Editor languages
  • CSS processors
  • External resources
  • Project info
  • Custom settings
  • Project title
  • Test code
console
(event: 'console', fn: (data: { method: string; args: any[] }) => void) => { remove: () => void }
Called on every console output in the result page.
tests
(event: 'tests', fn: (data: { results: TestResult[]; error?: string }) => void) => { remove: () => void }
Called when tests run.
destroy
(event: 'destroy', fn: () => void) => { remove: () => void }
Called when the playground is destroyed.
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 done
codeWatcher.remove();
consoleWatcher.remove();
testsWatcher.remove();

onChange()

Runs a callback function when code changes.
Deprecated: Use the watch method instead.
onChange
(fn: (data: { code: Code; config: Config }) => void) => { remove: () => void }
deprecated
Registers a callback that runs when code changes.

exec()

Executes custom commands.
exec
(command: APICommands, ...args: any[]) => Promise<{ output: any } | { error: string }>
import { createPlayground } from "livecodes";

const playground = await createPlayground("#container");

// Show LiveCodes version
const versionResult = await playground.exec("showVersion");

// Set broadcast token
const tokenResult = await playground.exec("setBroadcastToken", "my-token");

destroy()

Destroys the playground instance and removes event listeners.
destroy
() => Promise<void>
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

Type Definitions

interface Playground {
  load: () => Promise<void>;
  run: () => Promise<void>;
  format: (allEditors?: boolean) => Promise<void>;
  getShareUrl: (shortUrl?: boolean) => Promise<string>;
  getConfig: (contentOnly?: boolean) => Promise<Config>;
  setConfig: (config: Partial<Config>) => Promise<Config>;
  getCode: () => Promise<Code>;
  show: (
    panel: EditorId | 'editor' | 'result' | 'toggle-result' | Tool['name'],
    options?: { full?: boolean; line?: number; column?: number; zoom?: Config['zoom'] }
  ) => Promise<void>;
  runTests: () => Promise<{ results: TestResult[] }>;
  onChange: (fn: (data: { code: Code; config: Config }) => void) => { remove: () => void };
  watch: WatchFn;
  exec: (command: APICommands, ...args: any[]) => Promise<{ output: any } | { error: string }>;
  destroy: () => Promise<void>;
}

Build docs developers (and LLMs) love