Interface: Playground
An object that represents the LiveCodes playground instance.
The object exposes multiple methods that can be used to interact with the playground.
See docs for details.
Hierarchy
↳
Playground
Properties
destroy
• destroy: () => Promise
<void
>
Destroys the playground instance, and removes event listeners.
Further call to any SDK methods throws an error.
Example
import { createPlayground } from "livecodes";
createPlayground("#container").then(async (playground) => {
await playground.destroy();
// playground destroyed
// any further SDK call throws an error
});
Type declaration
▸ (): Promise
<void
>
Returns
Promise
<void
>
Inherited from
Defined in
exec
• exec: (command
: APICommands
, ...args
: any
[]) => Promise
<{ output
: any
} | { error
: string
}>
Executes custom commands, including: "setBroadcastToken"
and "showVersion"
.
See docs for details.
Type declaration
▸ (command
, ...args
): Promise
<{ output
: any
} | { error
: string
}>
Parameters
Name | Type |
---|---|
command | APICommands |
...args | any [] |
Returns
Promise
<{ output
: any
} | { error
: string
}>
Inherited from
Defined in
format
• format: (allEditors?
: boolean
) => Promise
<void
>
Formats the code.
By default, the code in all editors (markup, style and script) is formatted.
To format only the active editor, the value false
should be passed as an argument.
Example
import { createPlayground } from "livecodes";
createPlayground("#container").then(async (playground) => {
await playground.format();
// code in editors is formatted
});
Type declaration
▸ (allEditors?
): Promise
<void
>
Parameters
Name | Type |
---|---|
allEditors? | boolean |
Returns
Promise
<void
>
Inherited from
Defined in
getCode
• getCode: () => Promise
<Code
>
Gets the playground code (including source code, source language and compiled code) for each editor (markup, style, script), in addition to result page HTML.
See Code for the structure of the returned object.
Example
import { createPlayground } from "livecodes";
createPlayground("#container").then(async (playground) => {
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;
});
Type declaration
▸ (): Promise
<Code
>
Returns
Promise
<Code
>
Inherited from
Defined in
getConfig
• getConfig: (contentOnly?
: boolean
) => Promise
<Config
>
Gets a 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.
Example
import { createPlayground } from "livecodes";
createPlayground("#container").then(async (playground) => {
const config = await playground.getConfig();
});
Type declaration
▸ (contentOnly?
): Promise
<Config
>
Parameters
Name | Type |
---|---|
contentOnly? | boolean |
Returns
Promise
<Config
>
Inherited from
Defined in
getShareUrl
• getShareUrl: (shortUrl?
: boolean
) => Promise
<string
>
Gets a share url for the current project.
By default, the url has a long query string representing the compressed encoded config object.
If the argument shortUrl
was set to true
, a short url is generated.
Example
import { createPlayground } from "livecodes";
createPlayground("#container").then(async (playground) => {
const longUrl = await playground.getShareUrl();
const shortUrl = await playground.getShareUrl(true);
});
Type declaration
▸ (shortUrl?
): Promise
<string
>
Parameters
Name | Type |
---|---|
shortUrl? | boolean |
Returns
Promise
<string
>
Inherited from
Defined in
load
• load: () => Promise
<void
>
Loads the playground, if not already loaded.
When the embed option loading is set to "click"
, the playground is not loaded automatically.
Instead, a screen is shown with "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.
Type declaration
▸ (): Promise
<void
>
Returns
Promise
<void
>
Defined in
onChange
• onChange: (fn
: (data
: { code
: Code
; config
: Config
}) => void
) => { remove
: () => void
}
Runs a callback function when code changes.
Deprecated
Use watch
method instead.
Type declaration
▸ (fn
): Object
Parameters
Name | Type |
---|---|
fn | (data : { code : Code ; config : Config }) => void |
Returns
Object
Name | Type |
---|---|
remove | () => void |
Inherited from
Defined in
run
• run: () => Promise
<void
>
Runs the result page (after any required compilation for code).
Example
import { createPlayground } from "livecodes";
createPlayground("#container").then(async (playground) => {
await playground.run();
// new result page is displayed
});
Type declaration
▸ (): Promise
<void
>
Returns
Promise
<void
>
Inherited from
Defined in
runTests
• runTests: () => Promise
<{ results
: TestResult
[] }>
Runs project tests (if present) and gets test results.
Example
import { createPlayground } from "livecodes";
createPlayground("#container").then(async (playground) => {
const { results } = await playground.runTests();
});
Type declaration
▸ (): Promise
<{ results
: TestResult
[] }>
Returns
Promise
<{ results
: TestResult
[] }>
Inherited from
Defined in
setConfig
• setConfig: (config
: Partial
<Config
>) => Promise
<Config
>
Loads a new project using the passed configuration object.
Example
import { createPlayground } from "livecodes";
createPlayground("#container").then(async (playground) => {
const config = {
markup: {
language: "html",
content: "Hello World!",
},
};
const newConfig = await playground.setConfig(config);
// new project loaded
});
Type declaration
▸ (config
): Promise
<Config
>
Parameters
Name | Type |
---|---|
config | Partial <Config > |
Returns
Promise
<Config
>
Inherited from
Defined in
show
• show: (panel
: "result"
| EditorId
| "console"
| "compiled"
| "tests"
, options?
: { column?
: number
; full?
: boolean
; line?
: number
; zoom?
: 1
| 0.5
| 0.25
}) => Promise
<void
>
Shows the selected panel.
See docs for details.
Example
await playground.show("style");
await playground.show("result", { full: true });
await playground.show("script");
await playground.show("result", { zoom: 0.5 });
await playground.show("console", { full: true });
Type declaration
▸ (panel
, options?
): Promise
<void
>
Parameters
Name | Type |
---|---|
panel | "result" | EditorId | "console" | "compiled" | "tests" |
options? | Object |
options.column? | number |
options.full? | boolean |
options.line? | number |
options.zoom? | 1 | 0.5 | 0.25 |
Returns
Promise
<void
>
Inherited from
Defined in
watch
• watch: WatchLoad
& WatchReady
& WatchCode
& WatchConsole
& WatchTests
& WatchDestroy
Allows to watch for various playground events. It 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 will be called with an object that supplies relevant data to the callback function (e.g. code, console output, test results).
The watch method returns an object with a single method (remove
), which when called will remove the callback from watching further events.
See docs for details.
Example
import { createPlayground } from "livecodes";
createPlayground("#container").then((playground) => {
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); // "pass", "fail" or "skip"
console.log(testResult.errors); // array of errors as strings
});
});
// then later
codeWatcher.remove();
consoleWatcher.remove();
testsWatcher.remove();
// events are no longer watched
});