yourjs-box 1.9.0 → 1.11.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 +78 -27
- package/dist/yourjs-box.d.ts +143 -0
- package/dist/yourjs-box.full.js +161 -12
- package/dist/yourjs-box.js +89 -9
- package/dist/yourjs-box.min.js +5 -5
- package/package.json +3 -2
package/README.md
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
|
-
#
|
|
1
|
+
# YourJS Box
|
|
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
5
|
Worker or the page itself. TypeScript works too.
|
|
6
6
|
|
|
7
7
|
**[Live demo](https://westc.github.io/yourjs-box/)** ·
|
|
8
|
-
[Examples](https://westc.github.io/yourjs-box
|
|
8
|
+
[Examples](https://westc.github.io/yourjs-box/#examples) ·
|
|
9
|
+
[Changelog](CHANGELOG.md)
|
|
9
10
|
|
|
10
11
|

|
|
11
12
|
|
|
@@ -100,20 +101,28 @@ objects and arrays) for a menu with:
|
|
|
100
101
|
- **Refresh** (for objects, arrays, etc.), which shows the value as it is now
|
|
101
102
|
if the code has changed it. Anything that was expanded stays expanded.
|
|
102
103
|
|
|
103
|
-
###
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
|
111
|
-
|
|
|
112
|
-
| `data-runner` | `"worker"`
|
|
113
|
-
| `data-
|
|
114
|
-
| `data-
|
|
115
|
-
| `data-
|
|
116
|
-
| `data-
|
|
104
|
+
### Options
|
|
105
|
+
|
|
106
|
+
Each option can be set with a `data-*` attribute on the script tag or, with
|
|
107
|
+
the [JavaScript API](#javascript-api), as an option of `YourJSBox.create()` or
|
|
108
|
+
`YourJSBox.from()`. Attribute values are always strings, while the API also
|
|
109
|
+
takes booleans for the true/false options and an array for `rulers`.
|
|
110
|
+
|
|
111
|
+
| Attribute | API option | Default | Description |
|
|
112
|
+
| --- | --- | --- | --- |
|
|
113
|
+
| `data-runner` | `runner` | `"worker"` | `"worker"` runs the code in a Web Worker. `"window"` runs it directly in the page. See "Where Code Runs" below. |
|
|
114
|
+
| `data-block-type` | `blockType` | `"classic"` | `"classic"` 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 "Module Blocks" below. |
|
|
115
|
+
| `data-language` | `language` | `"javascript"` | `"javascript"` or `"typescript"`. See "TypeScript" below. |
|
|
116
|
+
| `data-show-results` | `showResults` | `"true"` | `"false"` stops the value of the last expression in each block from being shown. See "Results" below. |
|
|
117
|
+
| `data-hide-prefix` | `hidePrefix` | None | Any block whose header starts with this prefix is hidden but still runs. See "Hidden Code" below. |
|
|
118
|
+
| `data-hide-empty-output` | `hideEmptyOutput` | `"true"` | `"true"` only shows the editor until there is something in the output (eg. once code is run). The output then stays (even after Clear) until the console is reset. `"false"` always shows the output. |
|
|
119
|
+
| `data-divider-orient` | `dividerOrient` | Automatic | `"vertical"` puts the editor beside the output and `"horizontal"` puts it below. If not given, the editor is beside the output unless the console is narrower than 600px. |
|
|
120
|
+
| `data-theme` | `theme` | The system's | `"light"` or `"dark"`. If not given, it follows the system's color scheme like the browser's dev tools. |
|
|
121
|
+
| `data-word-wrap` | `wordWrap` | `"false"` | `"true"` wraps long lines in the editor instead of scrolling sideways. |
|
|
122
|
+
| `data-rulers` | `rulers` | `"80"` | The columns where lines are shown in the editor, eg. `"80, 120"` (or `[80, 120]` with the API). `""` shows none. |
|
|
123
|
+
| `data-loading` | `loading` | `"lazy"` | `"lazy"` 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. |
|
|
124
|
+
| `data-imports-url` | `importsUrl` | esm.sh | Where packages imported by name are loaded from. `""` turns this off. See "Importing Packages" below. |
|
|
125
|
+
| `data-libraries-url` | `librariesUrl` | unpkg | Where to load Vue, Ace, Prism, Acorn and Babel from. See "Self-Hosting the Libraries" below. |
|
|
117
126
|
|
|
118
127
|
### Results
|
|
119
128
|
|
|
@@ -295,7 +304,7 @@ box.destroy();
|
|
|
295
304
|
`YourJSBox.create(options)` returns `{element, destroy}` where `element` is the
|
|
296
305
|
console's IFRAME and `destroy()` removes it, stops its code and closes its
|
|
297
306
|
pop-out window (in window mode it also restores the page's `console`
|
|
298
|
-
functions).
|
|
307
|
+
functions). Its own options are:
|
|
299
308
|
|
|
300
309
|
| Option | Description |
|
|
301
310
|
| --- | --- |
|
|
@@ -303,9 +312,42 @@ functions). The options are:
|
|
|
303
312
|
| `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. |
|
|
304
313
|
| `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. |
|
|
305
314
|
| `code` | The code that the console starts with. |
|
|
306
|
-
| `runner`, `blockType`, `language`, `showResults`, `hidePrefix`, `dividerOrient`, `theme`, `importsUrl`, `librariesUrl`, `loading` | The same as the `data-*` attributes above. |
|
|
307
315
|
|
|
308
|
-
|
|
316
|
+
It also takes every option in [Options](#options) (eg. `theme` or `wordWrap`).
|
|
317
|
+
|
|
318
|
+
`YourJSBox.from(elements, options)` turns existing elements (eg. the code blocks
|
|
319
|
+
in a page made from Markdown) into consoles that start with their code:
|
|
320
|
+
|
|
321
|
+
```html
|
|
322
|
+
<script src="https://cdn.jsdelivr.net/npm/yourjs-box@1/dist/yourjs-box.min.js"></script>
|
|
323
|
+
<script>YourJSBox.from('pre > code.language-js');</script>
|
|
324
|
+
```
|
|
325
|
+
|
|
326
|
+
- `elements` is a CSS selector, an element or a list of elements (eg. an array
|
|
327
|
+
or a `NodeList`).
|
|
328
|
+
- Each element's text is the console's code, so code that was already
|
|
329
|
+
highlighted (eg. by Prism or highlight.js) works too. A `<code>` that is the
|
|
330
|
+
only thing in a `<pre>` is treated as the `<pre>`.
|
|
331
|
+
- `options` are the same as for `create()` (except `target` and `code`) and
|
|
332
|
+
apply to every console. `placement` defaults to `"replace"` and `height`
|
|
333
|
+
defaults to a height that fits the code (from 250px to 600px).
|
|
334
|
+
- The `data-*` attributes of each element (or its `<pre>`) override the
|
|
335
|
+
options for that console (eg. `data-theme="dark"` or `data-height="400"`)
|
|
336
|
+
and a `language-ts` or `language-typescript` class turns on TypeScript.
|
|
337
|
+
- It returns an array with `{element, destroy}` for each console, in the same
|
|
338
|
+
order as the elements. Destroying a console that replaced its element puts
|
|
339
|
+
the element back.
|
|
340
|
+
|
|
341
|
+
`YourJSBox.version` is the version of YourJS Box that was loaded.
|
|
342
|
+
|
|
343
|
+
The package includes TypeScript types for `YourJSBox` (`dist/yourjs-box.d.ts`),
|
|
344
|
+
which also give you autocomplete in VS Code. Install the package (eg.
|
|
345
|
+
`npm install --save-dev yourjs-box`, even if the script itself comes from a
|
|
346
|
+
CDN) and add this line to the top of a TypeScript or JavaScript file:
|
|
347
|
+
|
|
348
|
+
```js
|
|
349
|
+
/// <reference types="yourjs-box" />
|
|
350
|
+
```
|
|
309
351
|
|
|
310
352
|
### Where Code Runs
|
|
311
353
|
|
|
@@ -330,21 +372,25 @@ between blocks.
|
|
|
330
372
|
Install the development dependencies by running `npm install`.
|
|
331
373
|
|
|
332
374
|
- `npm run dev` builds, rebuilds whenever one of the files in `src/` changes
|
|
333
|
-
and serves the examples at http://localhost:3000/
|
|
334
|
-
your default browser) with the browser reloading automatically after
|
|
335
|
-
rebuild. Use `BROWSER=none npm run dev` to keep it from opening a browser.
|
|
375
|
+
and serves the landing page and examples at http://localhost:3000/ (opening
|
|
376
|
+
it in your default browser) with the browser reloading automatically after
|
|
377
|
+
each rebuild. Use `BROWSER=none npm run dev` to keep it from opening a browser.
|
|
336
378
|
- `npm run build` builds the files in `dist/` once.
|
|
337
379
|
- `npm test` builds and then runs the browser tests in `test/run.js` using your
|
|
338
380
|
installed copy of Google Chrome (set `CHROME_PATH` to use another Chromium
|
|
339
381
|
based browser). An internet connection is needed because the console loads
|
|
340
382
|
its libraries from CDNs. Pass part of a test's name to run only matching
|
|
341
383
|
tests (eg. `node test/run.js module`).
|
|
384
|
+
- As you make changes, add notes for them under **Unreleased** in
|
|
385
|
+
[CHANGELOG.md](CHANGELOG.md).
|
|
342
386
|
- `npm run release -- <patch|minor|major>` releases a new version: it checks
|
|
343
|
-
that you're on an up to date, clean `main
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
387
|
+
that you're on an up to date, clean `main` and that CHANGELOG.md has notes
|
|
388
|
+
under **Unreleased**, runs the tests, runs `npm version` (which also moves
|
|
389
|
+
those notes into a dated section for the new version), pushes `main` and then
|
|
390
|
+
the tag (separately, because GitHub Pages doesn't always deploy when they are
|
|
391
|
+
pushed together), publishes to npm and, once npm lists the new version,
|
|
392
|
+
purges jsDelivr's cache. Add `--dry-run` to see what it would do or
|
|
393
|
+
`--skip-tests` to skip the tests.
|
|
348
394
|
- `npm run record-demo` records `demo.gif` (shown at the top of this README)
|
|
349
395
|
from the built files. It needs [ffmpeg](https://ffmpeg.org/).
|
|
350
396
|
- `npm run og-image` makes `og-image.png` (the picture shown when the landing
|
|
@@ -365,6 +411,11 @@ The options are kept in the URL so the page can be reloaded or shared as is.
|
|
|
365
411
|
|
|
366
412
|
Ideas for future versions, roughly from easiest to hardest:
|
|
367
413
|
|
|
414
|
+
- **Smaller Acorn:** Acorn's package only has an unminified build (about 60 KB
|
|
415
|
+
compressed). A minified copy is about 35 KB (jsDelivr makes one
|
|
416
|
+
automatically, but unpkg and self-hosted copies don't), so Acorn could be
|
|
417
|
+
loaded from jsDelivr by default or a minified copy could be published with
|
|
418
|
+
this package.
|
|
368
419
|
- **`data-autorun`:** run every visible block when the page loads.
|
|
369
420
|
- **Autocomplete:** turn on Ace's keyword and snippet completion in the editor.
|
|
370
421
|
- **`data-remember`:** save the editor's code and history in the browser so
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Types for the `YourJSBox` global that loading YourJS Box provides. See
|
|
3
|
+
* https://github.com/westc/yourjs-box#javascript-api for more details.
|
|
4
|
+
*
|
|
5
|
+
* Use them in TypeScript (or in JavaScript checked by VS Code) with:
|
|
6
|
+
* /// <reference types="yourjs-box" />
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
declare namespace YourJSBox {
|
|
10
|
+
/**
|
|
11
|
+
* The options that can also be set with the `data-*` attributes of a script
|
|
12
|
+
* tag (eg. `wordWrap` is the same as `data-word-wrap`).
|
|
13
|
+
*/
|
|
14
|
+
interface ConsoleOptions {
|
|
15
|
+
/**
|
|
16
|
+
* `"worker"` (default) runs the code in a Web Worker. `"window"` runs it
|
|
17
|
+
* directly in the page.
|
|
18
|
+
*/
|
|
19
|
+
runner?: 'worker' | 'window';
|
|
20
|
+
/**
|
|
21
|
+
* `"classic"` (default) runs each block like a regular `<script>` so
|
|
22
|
+
* top-level declarations are shared between blocks. `"module"` runs each
|
|
23
|
+
* block like a `<script type="module">`.
|
|
24
|
+
*/
|
|
25
|
+
blockType?: 'classic' | 'module';
|
|
26
|
+
/** `"javascript"` (default) or `"typescript"`. */
|
|
27
|
+
language?: 'javascript' | 'typescript';
|
|
28
|
+
/**
|
|
29
|
+
* Whether the value of the last expression in each block is shown.
|
|
30
|
+
* Defaults to `true`.
|
|
31
|
+
*/
|
|
32
|
+
showResults?: boolean | 'true' | 'false';
|
|
33
|
+
/** Any block whose header starts with this prefix is hidden but still runs. */
|
|
34
|
+
hidePrefix?: string;
|
|
35
|
+
/**
|
|
36
|
+
* Whether only the editor is shown until there is something in the
|
|
37
|
+
* output. Defaults to `true`.
|
|
38
|
+
*/
|
|
39
|
+
hideEmptyOutput?: boolean | 'true' | 'false';
|
|
40
|
+
/**
|
|
41
|
+
* `"vertical"` puts the editor beside the output and `"horizontal"` puts
|
|
42
|
+
* it below. If not given, the editor is beside the output unless the
|
|
43
|
+
* console is narrower than 600px.
|
|
44
|
+
*/
|
|
45
|
+
dividerOrient?: 'vertical' | 'horizontal';
|
|
46
|
+
/** If not given, the theme follows the system's color scheme. */
|
|
47
|
+
theme?: 'light' | 'dark';
|
|
48
|
+
/** Whether long lines wrap in the editor. Defaults to `false`. */
|
|
49
|
+
wordWrap?: boolean | 'true' | 'false';
|
|
50
|
+
/**
|
|
51
|
+
* The columns where lines are shown in the editor (eg. `[80, 120]` or
|
|
52
|
+
* `"80, 120"`). Defaults to `80` and `""` (or `[]`) shows none.
|
|
53
|
+
*/
|
|
54
|
+
rulers?: number | number[] | string;
|
|
55
|
+
/**
|
|
56
|
+
* `"lazy"` (default) waits to load the console until it is about to be
|
|
57
|
+
* scrolled into view. `"eager"` loads it right away.
|
|
58
|
+
*/
|
|
59
|
+
loading?: 'lazy' | 'eager';
|
|
60
|
+
/**
|
|
61
|
+
* Where packages imported by name are loaded from, where `{specifier}` is
|
|
62
|
+
* replaced by the package (eg. `"https://esm.sh/{specifier}"`, which is
|
|
63
|
+
* the default). `""` turns this off.
|
|
64
|
+
*/
|
|
65
|
+
importsUrl?: string;
|
|
66
|
+
/**
|
|
67
|
+
* Where to load Vue, Ace, Prism, Acorn and Babel from, where `{name}` and
|
|
68
|
+
* `{version}` are replaced by each library's name and version (eg.
|
|
69
|
+
* `"/node_modules/{name}/"`). Defaults to unpkg.
|
|
70
|
+
*/
|
|
71
|
+
librariesUrl?: string;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** Where a console goes relative to its target. */
|
|
75
|
+
type Placement = 'fill' | 'append' | 'prepend' | 'replace' | 'before' | 'after';
|
|
76
|
+
|
|
77
|
+
interface CreateOptions extends ConsoleOptions {
|
|
78
|
+
/** The element (or a CSS selector for it) that the console is placed relative to. */
|
|
79
|
+
target: string | Element;
|
|
80
|
+
/**
|
|
81
|
+
* `"fill"` (default) replaces the target's contents, `"append"` and
|
|
82
|
+
* `"prepend"` add the console inside of the target, `"replace"` replaces
|
|
83
|
+
* the target itself and `"before"` and `"after"` add it next to the target.
|
|
84
|
+
*/
|
|
85
|
+
placement?: Placement;
|
|
86
|
+
/**
|
|
87
|
+
* The CSS height of the console (a number is treated as pixels).
|
|
88
|
+
* Defaults to `"100%"`. It is never less than 150px.
|
|
89
|
+
*/
|
|
90
|
+
height?: string | number;
|
|
91
|
+
/** The code that the console starts with. */
|
|
92
|
+
code?: string;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
interface FromOptions extends ConsoleOptions {
|
|
96
|
+
/** Defaults to `"replace"`. */
|
|
97
|
+
placement?: Placement;
|
|
98
|
+
/**
|
|
99
|
+
* The CSS height of each console (a number is treated as pixels).
|
|
100
|
+
* Defaults to a height that fits the code (from 250px to 600px).
|
|
101
|
+
*/
|
|
102
|
+
height?: string | number;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/** A console that was created. */
|
|
106
|
+
interface Box {
|
|
107
|
+
/** The console's IFRAME. */
|
|
108
|
+
readonly element: HTMLIFrameElement;
|
|
109
|
+
/**
|
|
110
|
+
* Removes the console, stops its code and closes its pop-out window (in
|
|
111
|
+
* window mode it also restores the page's `console` functions). For a
|
|
112
|
+
* console made by `from()` that replaced its element, the element is put
|
|
113
|
+
* back.
|
|
114
|
+
*/
|
|
115
|
+
destroy(): void;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
declare const YourJSBox: {
|
|
120
|
+
/** The version of YourJS Box that was loaded (eg. `"1.10.0"`). */
|
|
121
|
+
readonly version: string;
|
|
122
|
+
/**
|
|
123
|
+
* Creates a console.
|
|
124
|
+
* @throws {TypeError} If the target or the placement isn't valid.
|
|
125
|
+
*/
|
|
126
|
+
create(options: YourJSBox.CreateOptions): YourJSBox.Box;
|
|
127
|
+
/**
|
|
128
|
+
* Turns existing elements (eg. the code blocks in a page made from Markdown)
|
|
129
|
+
* into consoles that start with their code. The `data-*` attributes of each
|
|
130
|
+
* element override `options` and a `language-ts` class turns on TypeScript.
|
|
131
|
+
* @param elements
|
|
132
|
+
* A CSS selector, an element or a list of elements (eg. a `NodeList`).
|
|
133
|
+
* @returns The consoles in the same order as the elements.
|
|
134
|
+
*/
|
|
135
|
+
from(
|
|
136
|
+
elements: string | Element | ArrayLike<Element> | Iterable<Element>,
|
|
137
|
+
options?: YourJSBox.FromOptions
|
|
138
|
+
): YourJSBox.Box[];
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
interface Window {
|
|
142
|
+
YourJSBox: typeof YourJSBox;
|
|
143
|
+
}
|