playbyplay is a browser library that shows users of language playgrounds a history of their experiments.
playbyplay weighs ~2.5k minified & gzipped (JS ~1k + CSS ~0.5k + localhistory ~1k) and has no other dependencies.
A very simple demo is available in the demo
directory.
-
Download
localhistory.min.js
from thelocalhistory
directory. -
Download
playbyplay.min.js
andplaybyplay.min.css
from thedist
directory. -
Add them to your web page:
<link rel="stylesheet" href="playbyplay.min.css"> <!-- localhistory.min.js must be before playbyplay.min.js --> <script src="localhistory.min.js"></script> <script src="playbyplay.min.js"></script>
Appends a run to history.
-
run
is the run to append to history.run
should have the following structure:{ input: 'The code inputted by the user', output: 'The playground output', status: 'success' or 'warning' or 'error' (optional) }
-
[options]
is an optional set of key/value pairs passed to localhistory.append.appendIfEqualToLast
is set tofalse
by default. -
[callback]
is an optional callback function, taking one argument:err
isnull
if the run was appended successfully, or anError
object if the run was not appended successfully.
var run = {
input: 'console.log(1 + 1);',
output: '2',
status: 'success'
};
playbyplay.append(run);
Shows a view of all runs previously appended to history. The user can either restore a run, or dismiss the view.
[options]
is an optional set of key/value pairs:parent
is the DOM element into which the view will be appended. Defaults todocument.body
.onShow
is a callback function, taking one argument:err
isnull
if the runs were shown successfully, or anError
object if the runs were not shown successfully.
callback
is a callback function, taking one argument:run
is the run to restore, orundefined
if the user dismissed the view.
playbyplay.show(function(run) {
if (run) {
// Replace the contents of the playground input textarea with `run.input`.
document.getElementById('input-textarea').value = run.input;
}
});
Clears history, removing all previously appended runs.
[callback]
is an optional callback function, taking one argument:err
isnull
if the history was cleared successfully, or anError
object if the history was not cleared successfully.
playbyplay.clear();
true
if the browser supports playbyplay, and false
otherwise.
See localhistory.supported for the list of required features.
Install dependencies:
$ npm install
Lint and test:
$ npm test
Refresh demo and run tests after each change:
$ npm run watch
ECMAScript 2015 and above are encouraged, but keep an eye on dist_dev/playbyplay.js
to make sure the transpiled ES5 code does not become bloated by runtimes and polyfills.