@clinth/repl-pad
v0.1.2
Published
Javascript REPL
Readme
repl-pad
repl-pad takes some Javascript and presents it in an editable REPL-like environment. It is meant for easily making example snippets runnable and editable.
Demos:
Results appear as an inline overlay directly to the right of each line of code:

Repl-pad gives the illusion that each line of code is executed independently, with the result shown to the side. In reality each block executes along with every block that precedes it.
Statements that generate an error are skipped over optimistically, so the REPL experience can be more 'sketchy':

URL-based imports can be used as well:

Caveats
for await ... of and async functions are handled by wrapping the whole snippet in an immediately-invoked async function expression. This allows await to work, but code runs in a single async context rather than as true top-level await.
Because the same code is re-evaluated repeatedly, initiating timers can be problematic. Use the complete mode to avoid this.
Usage
npm install --save @clinth/repl-padRepl-pad is a vanilla web component. It can be used programmatically, but it was designed to be hosted in a stand-alone simple HTML page. Code source comes in base64-encoded via a URI anchor:
pad.html#ugly-base-64-stringA small helper function allows you to generate this URL automatically from a HTML element's innerText. This is meant to convert a static code example into something runnable.
In your HTML:
<pre id="eg1">
const x = 1;
</pre>In your JS:
import {fromInnerText} from '@clinth/repl-pad';
const el = document.getElementById(`eg1`);
// Yields: pad.html#...
const uri = fromInnerText(el, `pad.html`);
// Create an edit link and add after example
const link = document.createElement(`a`);
link.href = uri;
link.innerText = `Edit`;
el.parentNode.insertBefore(link, el.nextSibling);One can add edit links for all elements that match a CSS selector:
<pre>
const x = 1;
</pre>
<pre>
const x = Math.random();
</pre>import {fromQuery} from '@clinth/repl-pad/link.js';
const r = fromQuery(`pre`, `pad.html`);
for (const {el, uri} of r) {
const link = document.createElement(`a`);
link.href = uri;
link.innerText = `Edit`;
el.parentNode.insertBefore(link, el.nextSibling);
}See this in action in docs/index.html.
Programmatic usage
It's also possible to send JS code programmatically:
import {ReplPadElement} from '@clinth/repl-pad';
const r = new ReplPadElement(`
const x = 1;
x;
`);
document.body.appendChild(r);Or use the custom element directly in HTML (after importing the module):
<repl-pad></repl-pad>Result display
Results appear as an overlay to the right of each line. They are colour-coded:
- Default (grey) — normal value
- Red — error
- Italic grey — informational (e.g. pending timer
⏱)
For loops
For for and for...of loops, the index variable's values are shown comma-separated on the opening for line. Expressions inside the loop body also accumulate their values per line:
for (let i = 0; i < 5; i++) {
i * 2;
}The for line shows: 0, 1, 2, 3, 4
The body expression line shows: 0, 2, 4, 6, 8
Timers (setTimeout / setInterval)
setTimeout callbacks show the elapsed time with a ⏱ prefix when they fire. Expressions inside the callback are captured and displayed on their own lines.
setInterval callbacks accumulate values comma-separated, like a loop.
setTimeout(() => {
Math.random();
}, 2000);The setTimeout line shows: ⏱ 2s
The inner expression line shows the return value when it fires.
Refresh button
Hover over any result label to reveal a ↻ button. Clicking it re-executes just that block against the same context from the last full run, without re-running the entire script.
Re-evaluation
Repl-pad evaluates each block along with its preceding blocks. The consequence is that some statements are executed several times.
For example, with three console.log lines, the first line runs three times (once per block that follows). Repl-pad does not re-evaluate console.* statements by default to avoid duplicate output. For this reason, avoid side-effects inside console.*:
// Bad: data is changed within the console statement
console.log(i++);
// Good: side-effect is separate
i++;
console.log(i);Options
Options can be specified as URL parameters:
pad.html?reevalUndef=false&maxResultLength=20#base-64Or as attributes on the web component:
<repl-pad reevalUndef="false" maxResultLength="20"></repl-pad>Or as JS properties before the component runs:
const pad = new ReplPadElement();
pad.maxResultLength = 20;
pad.maxLoopLength = 10;reevalUndef (true/false, default true)
If true, statements that return undefined are re-evaluated when building the cumulative context for later blocks. If false, they are skipped, which prevents some duplicate side-effects.
reevalConsole (true/false, default false)
If true, console.* statements are re-evaluated as part of the cumulative context.
maxResultLength (integer, default 10)
Maximum characters shown per result label before truncating with .... Hover over a truncated result to see the full value. Error messages are never truncated.
maxLoopLength (integer, default 5)
Maximum number of iterations to capture and display for for loops and setInterval firings.
mode (string, default "")
Set to "complete" to run the entire script in a Web Worker (experimental). This avoids timer accumulation across re-runs.
Styling
See docs/pad.html for an example.
repl-pad {
display: flex;
flex: 1;
}CSS custom properties:
| Property | Purpose |
|---|---|
| --mono-font | Monospace font family for the editor and result labels |
| --ui-font | Sans-serif font for any UI chrome |
| --font-size | Base font size |
| --padding | Editor padding |
| --line-height | Line height (must match for overlay alignment) |
| --output | Result label colour |
| --console-error | Error label colour |
| --console-warn | Warning colour |
| --ui-bg | UI background |
| --ui-fg | UI foreground |
| --ui-pinstripe | Subtle background accent |
| --ui-highlight-bg | Text selection background |
| --ui-highlight-fg | Text selection foreground |
| --console-bg | Console panel background |
| --console-fg | Console panel foreground |
| --comment-font | Font family for // comments (falls back to --mono-font) |
| --comment-style | font-style for // comments, e.g. italic or normal (falls back to italic) |
| --comment-color | Colour for // comments (falls back to --ui-fg) |
Only // line comments are styled; /* */ block comments are not detected.
Developing
npm run devContinually rebuilds and serves the demo pages with HMR.
npm run buildBuilds the library to ./dist.
