yourjs-box 1.6.0 → 1.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +96 -19
- package/dist/yourjs-box.full.js +243 -29
- package/dist/yourjs-box.js +58 -11
- package/dist/yourjs-box.min.js +8 -7
- package/package.json +2 -1
package/README.md
CHANGED
|
@@ -2,11 +2,13 @@
|
|
|
2
2
|
|
|
3
3
|
Embed an interactive JavaScript console on any web page with one script tag.
|
|
4
4
|
Step-by-step code blocks, DevTools-style output, and code that runs in a Web
|
|
5
|
-
Worker or the page itself.
|
|
5
|
+
Worker or the page itself. TypeScript works too.
|
|
6
6
|
|
|
7
7
|
**[Live demo](https://westc.github.io/yourjs-box/)** ·
|
|
8
8
|
[Examples](https://westc.github.io/yourjs-box/examples/)
|
|
9
9
|
|
|
10
|
+

|
|
11
|
+
|
|
10
12
|
## Usage
|
|
11
13
|
|
|
12
14
|
Add the script wherever you want the console to appear and put the code that
|
|
@@ -61,10 +63,10 @@ If the editor already has code in it you are asked before it is replaced.
|
|
|
61
63
|
history.
|
|
62
64
|
- The **⋯** menu has:
|
|
63
65
|
- **Text size**, which is remembered for every console on the same site.
|
|
64
|
-
- **Open**, which loads a JavaScript file as the code that the console starts
|
|
66
|
+
- **Open**, which loads a JavaScript (or TypeScript) file as the code that the console starts
|
|
65
67
|
with (its hidden blocks are hidden and Reset goes back to it).
|
|
66
68
|
- **Save**, which saves the code that already ran and the code in the editor
|
|
67
|
-
as a JavaScript file that can be opened later.
|
|
69
|
+
as a JavaScript (or TypeScript) file that can be opened later.
|
|
68
70
|
- **Pop out into a window**, which moves the console into a separate window
|
|
69
71
|
while the code keeps running in the page (so in window mode the code can
|
|
70
72
|
still change the page). Close the window or click **Bring it back** to
|
|
@@ -99,12 +101,14 @@ objects and arrays) for a menu with:
|
|
|
99
101
|
| Attribute | Description |
|
|
100
102
|
| --- | --- |
|
|
101
103
|
| `data-block-type` | `"classic"` (default) runs each block like a regular `<script>` so top-level declarations are shared between blocks. `"module"` runs each block like a `<script type="module">`. See below. |
|
|
104
|
+
| `data-language` | `"javascript"` (default) or `"typescript"`. See "TypeScript" below. |
|
|
102
105
|
| `data-show-results` | `"false"` stops the value of the last expression in each block from being shown. |
|
|
103
106
|
| `data-imports-url` | Where packages imported by name are loaded from. See "Importing Packages" below. |
|
|
104
|
-
| `data-libraries-url` | Where to load Vue, Ace, Prism and
|
|
107
|
+
| `data-libraries-url` | Where to load Vue, Ace, Prism, Acorn and Babel from. See "Self-Hosting the Libraries" below. |
|
|
105
108
|
| `data-runner` | `"worker"` (default) runs the code in a Web Worker. `"window"` runs it directly in the page. See below. |
|
|
106
109
|
| `data-divider-orient` | `"vertical"` puts the editor beside the output and `"horizontal"` puts it below. If not specified the editor is beside the output unless the console is narrower than 600px. |
|
|
107
110
|
| `data-hide-prefix` | Any block whose header starts with this prefix is hidden. See below. |
|
|
111
|
+
| `data-loading` | `"lazy"` (default) waits to load the console until it is about to be scrolled into view (or, if it is hidden, shown). `"eager"` loads it right away. See "Loading" below. |
|
|
108
112
|
| `data-theme` | `"light"` or `"dark"`. Defaults to following the system's color scheme like the browser's dev tools. |
|
|
109
113
|
|
|
110
114
|
### Results
|
|
@@ -126,6 +130,36 @@ one block can be used in the next, but `await` can only be used inside of
|
|
|
126
130
|
- Top-level declarations stay inside of their block, just like in a module.
|
|
127
131
|
Use `globalThis` to share values between blocks.
|
|
128
132
|
|
|
133
|
+
### TypeScript
|
|
134
|
+
|
|
135
|
+
Add `data-language="typescript"` to write the code in TypeScript:
|
|
136
|
+
|
|
137
|
+
```html
|
|
138
|
+
<script src="https://cdn.jsdelivr.net/npm/yourjs-box@1/dist/yourjs-box.min.js" data-language="typescript">
|
|
139
|
+
// Describe a person \\
|
|
140
|
+
interface Person { name: string; born: number }
|
|
141
|
+
const people: Person[] = [{name: 'Ada', born: 1815}, {name: 'Grace', born: 1906}];
|
|
142
|
+
|
|
143
|
+
// Find the oldest \\
|
|
144
|
+
function oldest<T extends Person>(list: T[]): T {
|
|
145
|
+
return list.reduce((a, b) => a.born <= b.born ? a : b);
|
|
146
|
+
}
|
|
147
|
+
oldest(people)
|
|
148
|
+
</script>
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
Before each block runs, [Babel](https://babeljs.io/) removes the types, which
|
|
152
|
+
is how most TypeScript tools run code without a build step. The types aren't
|
|
153
|
+
checked, so code with type errors still runs, just like it would in
|
|
154
|
+
JavaScript. Enums, namespaces and parameter properties all work, and errors
|
|
155
|
+
point to the lines and columns in the TypeScript code.
|
|
156
|
+
|
|
157
|
+
TypeScript works with both block types. Only type imports are removed (eg.
|
|
158
|
+
`import type {Options} from 'x'` or the `type Options` part of
|
|
159
|
+
`import {type Options, format} from 'x'`), so an import is never dropped just
|
|
160
|
+
because nothing uses it yet. Babel is only downloaded by consoles that use
|
|
161
|
+
TypeScript.
|
|
162
|
+
|
|
129
163
|
### Importing Packages
|
|
130
164
|
|
|
131
165
|
npm packages can be imported by name. They are loaded from
|
|
@@ -158,7 +192,8 @@ To load packages from somewhere else set `data-imports-url` to a URL where
|
|
|
158
192
|
|
|
159
193
|
The console's interface uses [Vue](https://vuejs.org/),
|
|
160
194
|
[Ace](https://ace.c9.io/), [Prism](https://prismjs.com/) and
|
|
161
|
-
[Acorn](https://github.com/acornjs/acorn)
|
|
195
|
+
[Acorn](https://github.com/acornjs/acorn) (plus
|
|
196
|
+
[Babel](https://babeljs.io/) for TypeScript), which are loaded from unpkg by
|
|
162
197
|
default. Exact versions are always used so that a new release of one of them
|
|
163
198
|
can't change how the console works.
|
|
164
199
|
|
|
@@ -179,6 +214,9 @@ next to their main files):
|
|
|
179
214
|
|
|
180
215
|
```bash
|
|
181
216
|
npm install vue@3.5.43 ace-builds@1.44.0 prismjs@1.30.0 prism-themes@1.9.0 acorn@8.18.0
|
|
217
|
+
|
|
218
|
+
# Only needed for TypeScript consoles
|
|
219
|
+
npm install @babel/standalone@7.29.9
|
|
182
220
|
```
|
|
183
221
|
|
|
184
222
|
The About window lists the versions that each version of JS Box uses.
|
|
@@ -190,14 +228,27 @@ The About window lists the versions that each version of JS Box uses.
|
|
|
190
228
|
`timeLog()`, `timeEnd()`, `trace()`, `group()`, `groupCollapsed()`,
|
|
191
229
|
`groupEnd()` and `clear()` are all shown in the console.
|
|
192
230
|
|
|
231
|
+
### Loading
|
|
232
|
+
|
|
233
|
+
Like `<img loading="lazy">`, a console isn't loaded until it is about to be
|
|
234
|
+
scrolled into view, so a page with many consoles (eg. a long tutorial) only
|
|
235
|
+
loads the ones that are read. A console that is hidden (eg. in a closed tab)
|
|
236
|
+
loads once it is shown. Its space in the page is kept while it waits, so
|
|
237
|
+
nothing moves around when it loads.
|
|
238
|
+
|
|
239
|
+
Until a console loads none of its code runs, including hidden blocks, and in
|
|
240
|
+
window mode the page's `console` functions aren't captured yet. Add
|
|
241
|
+
`data-loading="eager"` to load a console right away (eg. if its hidden blocks
|
|
242
|
+
set up something that the page needs).
|
|
243
|
+
|
|
193
244
|
### Hidden Code
|
|
194
245
|
|
|
195
246
|
If `data-hide-prefix="HIDE"` is specified then a block with a header like
|
|
196
247
|
`// HIDE: Setup \\` will not be shown in the editor. Instead it runs
|
|
197
|
-
automatically as soon as all of the code that came
|
|
198
|
-
up in the output as a collapsed block labelled
|
|
199
|
-
prefix (and optional colon), or "Hidden code" if
|
|
200
|
-
label shows or hides the code.
|
|
248
|
+
automatically (once the console loads) as soon as all of the code that came
|
|
249
|
+
before it has run. It shows up in the output as a collapsed block labelled
|
|
250
|
+
with whatever came after the prefix (and optional colon), or "Hidden code" if
|
|
251
|
+
nothing did. Clicking the label shows or hides the code.
|
|
201
252
|
|
|
202
253
|
### JavaScript API
|
|
203
254
|
|
|
@@ -232,7 +283,7 @@ functions). The options are:
|
|
|
232
283
|
| `placement` | Where the console goes: `"fill"` (default) replaces the target's contents, `"append"` and `"prepend"` add it inside of the target, `"replace"` replaces the target itself and `"before"` and `"after"` add it next to the target. |
|
|
233
284
|
| `height` | The CSS height of the console (a number is treated as pixels). Defaults to `"100%"` so the console fills its container. It is never less than 150px. |
|
|
234
285
|
| `code` | The code that the console starts with. |
|
|
235
|
-
| `runner`, `blockType`, `showResults`, `hidePrefix`, `dividerOrient`, `theme`, `importsUrl`, `librariesUrl` | The same as the `data-*` attributes above. |
|
|
286
|
+
| `runner`, `blockType`, `language`, `showResults`, `hidePrefix`, `dividerOrient`, `theme`, `importsUrl`, `librariesUrl`, `loading` | The same as the `data-*` attributes above. |
|
|
236
287
|
|
|
237
288
|
`YourJSBox.version` is the version of JS Box that was loaded.
|
|
238
289
|
|
|
@@ -274,6 +325,8 @@ Install the development dependencies by running `npm install`.
|
|
|
274
325
|
Pages doesn't always deploy when they are pushed together), publishes to npm
|
|
275
326
|
and, once npm lists the new version, purges jsDelivr's cache. Add
|
|
276
327
|
`--dry-run` to see what it would do or `--skip-tests` to skip the tests.
|
|
328
|
+
- `npm run record-demo` records `demo.gif` (shown at the top of this README)
|
|
329
|
+
from the built files. It needs [ffmpeg](https://ffmpeg.org/).
|
|
277
330
|
- `npm run purge-cdn` purges jsDelivr's cache so that URLs like
|
|
278
331
|
`yourjs-box@1` point to the latest version right away.
|
|
279
332
|
- In VS Code, **Terminal → Run Task…** has tasks for all of these
|
|
@@ -281,18 +334,42 @@ Install the development dependencies by running `npm install`.
|
|
|
281
334
|
- `npm start` builds and then rebuilds whenever one of the files in `src/`
|
|
282
335
|
changes (without serving anything).
|
|
283
336
|
|
|
284
|
-
The kitchen sink example (`examples/kitchen-sink.html`) has
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
337
|
+
The kitchen sink example (`examples/kitchen-sink.html`) has a sidebar for
|
|
338
|
+
choosing an example and every option, including which build is loaded
|
|
339
|
+
(`dist/yourjs-box.full.js`, `dist/yourjs-box.js` or `dist/yourjs-box.min.js`).
|
|
340
|
+
The options are kept in the URL so the page can be reloaded or shared as is.
|
|
288
341
|
|
|
289
342
|
## Roadmap
|
|
290
343
|
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
-
|
|
294
|
-
|
|
295
|
-
-
|
|
344
|
+
Ideas for future versions, roughly from easiest to hardest:
|
|
345
|
+
|
|
346
|
+
- **Format specifiers:** support `%s`, `%d`, `%i`, `%f`, `%o`, `%O` and `%c`
|
|
347
|
+
(CSS styles) in `console.log()` and the other logging functions.
|
|
348
|
+
- **Repeated messages:** show identical messages logged in a row once with a
|
|
349
|
+
count, like the browser's console.
|
|
350
|
+
- **Run all:** run every remaining block with <kbd>Ctrl</kbd> /
|
|
351
|
+
<kbd>⌘</kbd> + <kbd>Shift</kbd> + <kbd>Enter</kbd> or from the
|
|
352
|
+
**⋯** menu.
|
|
353
|
+
- **`data-autorun`:** run every visible block when the page loads.
|
|
354
|
+
- **Clear shortcut:** clear the console with <kbd>Ctrl</kbd> + <kbd>L</kbd>.
|
|
355
|
+
- **Autocomplete:** turn on Ace's keyword and snippet completion in the editor.
|
|
356
|
+
- **`data-remember`:** save the editor's code and history in the browser so
|
|
357
|
+
that they are still there after a reload.
|
|
358
|
+
- **Copy output:** copy everything in the console as text from the **⋯**
|
|
359
|
+
menu.
|
|
360
|
+
- **Clickable error locations:** clicking `snippet-2.js:3:7` in an error
|
|
361
|
+
highlights that line in the code that ran.
|
|
362
|
+
- **More JavaScript API:** `box.run()`, `box.runAll()`, `box.reset()`,
|
|
363
|
+
`box.setCode()` and events (eg. `box.on('log', ...)`) so that a page can
|
|
364
|
+
control the console.
|
|
365
|
+
- **Output filter:** search the output and show only errors, warnings or logs.
|
|
366
|
+
- **Exercises:** hidden blocks that check the code above them and show which
|
|
367
|
+
checks passed (eg. "`sum(2, 3)` should be `5`").
|
|
368
|
+
- **`@timeout`:** an annotation in a block's header that runs the block
|
|
369
|
+
automatically once nothing has been logged for the given number of seconds.
|
|
370
|
+
- **Long output:** keep the console fast when thousands of messages are logged.
|
|
371
|
+
- **Accessibility:** announce new output to screen readers and make every value
|
|
372
|
+
and menu reachable with the keyboard.
|
|
296
373
|
|
|
297
374
|
## License
|
|
298
375
|
|