| name | livecodes/sdk-methods |
| description | Use SDK methods to interact with playgrounds: run, getCode, setConfig, getConfig, watch, runTests, format, getShareUrl, show, destroy. Load this skill when programmatically controlling embedded playgrounds, reacting to code changes, or retrieving compiled output.
|
| type | core |
| library | livecodes |
| library_version | 0.13.0 |
| requires | ["sdk-embedding"] |
| sources | ["live-codes/livecodes:docs/docs/sdk/js-ts.mdx","live-codes/livecodes:src/sdk/index.ts"] |
This skill builds on sdk-embedding. Read it first for foundational concepts.
LiveCodes — Use SDK Methods
The Playground object returned by createPlayground exposes methods to programmatically control the playground.
Setup
import { createPlayground } from 'livecodes';
const playground = await createPlayground('#container', {
template: 'react',
});
await playground.run();
const code = await playground.getCode();
await playground.setConfig({
});
Core Patterns
Run the playground
await playground.run();
Get current code and config
const code = await playground.getCode();
console.log(code.markup.content);
console.log(code.markup.language);
console.log(code.markup.compiled);
console.log(code.result);
const config = await playground.getConfig();
console.log(config.title);
console.log(config.markup.language);
const contentConfig = await playground.getConfig(true);
Update playground configuration
await playground.setConfig({
markup: { language: 'markdown', content: '# New Content' },
});
const newConfig = await playground.setConfig({
script: { language: 'typescript', content: 'const x: number = 1;' },
});
Watch for changes
const codeWatcher = playground.watch('code', ({ code, config }) => {
console.log('Code changed:', code.script.content);
});
const runWatcher = playground.watch('run', ({ code, config }) => {
console.log('Playground ran. Result HTML:', code.result);
});
const consoleWatcher = playground.watch('console', ({ method, args }) => {
console[method](...args);
});
const testsWatcher = playground.watch('tests', ({ results, error }) => {
results.forEach((r) => console.log(r.status, r.testPath));
});
codeWatcher.remove();
runWatcher.remove();
consoleWatcher.remove();
testsWatcher.remove();
Run tests programmatically
const { results } = await playground.runTests();
results.forEach((result) => {
console.log(result.status);
console.log(result.errors);
console.log(result.testPath);
});
Show specific panel
await playground.show('editor');
await playground.show('markup');
await playground.show('style');
await playground.show('script');
await playground.show('console');
await playground.show('compiled');
await playground.show('tests');
await playground.show('result');
await playground.show('toggle-result');
await playground.show('result', { full: true });
await playground.show('result', { zoom: 0.5 });
playground.(, { : , : });
Format code
await playground.format();
await playground.format(false);
Get share URL
const longUrl = await playground.getShareUrl();
const shortUrl = await playground.getShareUrl(true);
Execute custom commands
await playground.exec('setBroadcastToken', 'my-token');
await playground.exec('showVersion');
Destroy playground
await playground.destroy();
Common Mistakes
HIGH Not awaiting async SDK methods
Wrong:
const playground = await createPlayground('#container', {});
const code = playground.getCode();
console.log(code);
Correct:
const playground = await createPlayground('#container', {});
const code = await playground.getCode();
console.log(code.markup.content);
All SDK methods return Promises. Use await or .then() to get the result.
Source: docs/docs/sdk/js-ts.mdx — SDK methods section
MEDIUM watch callback receives wrong data structure
Wrong:
playground.watch('code', (data) => {
console.log(data.content);
});
Correct:
playground.watch('code', ({ code, config }) => {
console.log(code.markup.content);
console.log(code.style.content);
console.log(code.script.content);
console.log(code.result);
});
playground.watch('run', ({ code, config }) => {
console.log(code.result);
});
playground.watch('console', ({ method, args }) => {
console[method](...args);
});
playground.watch('tests', ({ results, error }) => {
if (error) console.error(error);
results.forEach((r) => console.(r.));
});
Source: docs/docs/sdk/js-ts.mdx — watch method section
LOW Using deprecated onChange instead of watch
Wrong:
const watcher = playground.onChange(({ code, config }) => {
console.log('changed');
});
Correct:
const watcher = playground.watch('code', ({ code, config }) => {
console.log('changed');
});
watcher.remove();
onChange is deprecated. Use watch('code', callback) instead.
Source: src/sdk/index.ts — onChange marked as deprecated
SDK Method Reference
| Method | Returns | Description |
|---|
load() | Promise<void> | Load playground (for click-to-load mode) |
run() | Promise<void> | Run the result page |
format(allEditors?) | Promise<void> | Format code |
getShareUrl(shortUrl?) | Promise<string> | Get shareable URL |
getConfig(contentOnly?) | Promise<Config> | Get configuration |
setConfig(config) | Promise<Config> | Update configuration |
getCode() | Promise<Code> | Get code from all editors |
show(panel, options?) | Promise<void> | Show specific panel |
runTests() | Promise<{results}> | Run tests |
watch(event, fn) | {remove()} | Subscribe to events |
exec(command, ...args) | Promise<any> | Execute custom command |
destroy() | Promise<void> | Clean up and remove |