yourjs-box 1.8.0 → 1.10.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 CHANGED
@@ -1,13 +1,14 @@
1
- # yourjs-box
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/examples/)
8
+ [Examples](https://westc.github.io/yourjs-box/#examples) ·
9
+ [Changelog](CHANGELOG.md)
9
10
 
10
- ![JS Box running code one block at a time, expanding an object, showing a table and an error](demo.gif)
11
+ ![YourJS Box running code one block at a time, expanding an object, showing a table and an error](demo.gif)
11
12
 
12
13
  ## Usage
13
14
 
@@ -35,6 +36,9 @@ script is also available from unpkg at
35
36
  A comment line that ends with `\\` is a header. Headers split the code into
36
37
  blocks which are run one at a time each time the run button is clicked (or
37
38
  <kbd>Ctrl</kbd>/<kbd>Cmd</kbd>+<kbd>Enter</kbd> is pressed in the editor).
39
+ <kbd>Ctrl</kbd>/<kbd>Cmd</kbd>+<kbd>Shift</kbd>+<kbd>Enter</kbd> (or **Run all
40
+ blocks** in the **&#8943;** menu) runs all of the remaining blocks, one after
41
+ another and in order with any hidden blocks.
38
42
 
39
43
  Hovering over code that already ran shows a "Copy to editor" button which
40
44
  copies that code into the editor so that it can be run again, as is or modified.
@@ -43,8 +47,8 @@ If the editor already has code in it you are asked before it is replaced.
43
47
  ### Toolbar
44
48
 
45
49
  - The **&#8943;** button opens a menu (see below).
46
- - **Clear** removes everything from the console. Code can also call
47
- `console.clear()`.
50
+ - **Clear** (or <kbd>Ctrl</kbd>+<kbd>L</kbd>) removes everything from the
51
+ console. Code can also call `console.clear()`.
48
52
  - The **layout** button switches between showing the editor beside or below
49
53
  the console.
50
54
  - **Full screen** shows the console using the whole screen. If the browser
@@ -62,6 +66,7 @@ If the editor already has code in it you are asked before it is replaced.
62
66
  code that hasn't been run yet comes back. Hidden blocks aren't in the
63
67
  history.
64
68
  - The **&#8943;** menu has:
69
+ - **Run all blocks**, which runs all of the remaining blocks.
65
70
  - **Text size**, which is remembered for every console on the same site.
66
71
  - **Open**, which loads a JavaScript (or TypeScript) file as the code that the console starts
67
72
  with (its hidden blocks are hidden and Reset goes back to it).
@@ -78,7 +83,7 @@ If the editor already has code in it you are asked before it is replaced.
78
83
  - **Reset**, which clears the console and puts the original code back into
79
84
  the editor. In worker mode this also stops any code that is still running
80
85
  (eg. an infinite loop) by starting a new worker.
81
- - **About JS Box**
86
+ - **About YourJS Box**
82
87
 
83
88
  ### Right-Clicking Values
84
89
 
@@ -107,9 +112,12 @@ objects and arrays) for a menu with:
107
112
  | `data-libraries-url` | Where to load Vue, Ace, Prism, Acorn and Babel from. See "Self-Hosting the Libraries" below. |
108
113
  | `data-runner` | `"worker"` (default) runs the code in a Web Worker. `"window"` runs it directly in the page. See below. |
109
114
  | `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. |
115
+ | `data-hide-empty-output` | `"true"` (default) 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. |
110
116
  | `data-hide-prefix` | Any block whose header starts with this prefix is hidden. See below. |
111
117
  | `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. |
118
+ | `data-rulers` | The columns where lines are shown in the editor, eg. `"80, 120"`. Defaults to `"80"` and `""` shows none. |
112
119
  | `data-theme` | `"light"` or `"dark"`. Defaults to following the system's color scheme like the browser's dev tools. |
120
+ | `data-word-wrap` | `"true"` wraps long lines in the editor instead of scrolling sideways. |
113
121
 
114
122
  ### Results
115
123
 
@@ -219,7 +227,7 @@ npm install vue@3.5.43 ace-builds@1.44.0 prismjs@1.30.0 prism-themes@1.9.0 acorn
219
227
  npm install @babel/standalone@7.29.9
220
228
  ```
221
229
 
222
- The About window lists the versions that each version of JS Box uses.
230
+ The About window lists the versions that each version of YourJS Box uses.
223
231
 
224
232
  ### Console Functions
225
233
 
@@ -228,6 +236,22 @@ The About window lists the versions that each version of JS Box uses.
228
236
  `timeLog()`, `timeEnd()`, `trace()`, `group()`, `groupCollapsed()`,
229
237
  `groupEnd()` and `clear()` are all shown in the console.
230
238
 
239
+ Like the browser's console, the first argument can use format specifiers:
240
+ `%s` (string), `%d` or `%i` (integer), `%f` (number), `%o` and `%O` (a value
241
+ that can be expanded), `%c` (CSS styles for the text after it) and `%%` (a
242
+ percent sign). For example:
243
+
244
+ ```js
245
+ console.log('%cHello%c, %s!', 'color: white; background: #1a73e8; padding: 2px 6px; border-radius: 4px', '', 'world');
246
+ ```
247
+
248
+ Like the browser, `%c` only allows CSS for things like colors, fonts, borders,
249
+ margins and padding, and never anything that loads a URL.
250
+
251
+ A message that is the same as the one logged right before it is shown once with
252
+ a count instead of being repeated. Messages that include objects are always
253
+ shown again since the objects may have changed.
254
+
231
255
  ### Loading
232
256
 
233
257
  Like `<img loading="lazy">`, a console isn't loaded until it is about to be
@@ -283,9 +307,32 @@ functions). The options are:
283
307
  | `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. |
284
308
  | `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. |
285
309
  | `code` | The code that the console starts with. |
286
- | `runner`, `blockType`, `language`, `showResults`, `hidePrefix`, `dividerOrient`, `theme`, `importsUrl`, `librariesUrl`, `loading` | The same as the `data-*` attributes above. |
310
+ | `runner`, `blockType`, `language`, `showResults`, `hidePrefix`, `hideEmptyOutput`, `dividerOrient`, `theme`, `wordWrap`, `rulers`, `importsUrl`, `librariesUrl`, `loading` | The same as the `data-*` attributes above. `wordWrap`, `hideEmptyOutput` and `showResults` can be booleans and `rulers` can be an array (eg. `[80, 120]`). |
311
+
312
+ `YourJSBox.from(elements, options)` turns existing elements (eg. the code blocks
313
+ in a page made from Markdown) into consoles that start with their code:
314
+
315
+ ```html
316
+ <script src="https://cdn.jsdelivr.net/npm/yourjs-box@1/dist/yourjs-box.min.js"></script>
317
+ <script>YourJSBox.from('pre > code.language-js');</script>
318
+ ```
287
319
 
288
- `YourJSBox.version` is the version of JS Box that was loaded.
320
+ - `elements` is a CSS selector, an element or a list of elements (eg. an array
321
+ or a `NodeList`).
322
+ - Each element's text is the console's code, so code that was already
323
+ highlighted (eg. by Prism or highlight.js) works too. A `<code>` that is the
324
+ only thing in a `<pre>` is treated as the `<pre>`.
325
+ - `options` are the same as for `create()` (except `target` and `code`) and
326
+ apply to every console. `placement` defaults to `"replace"` and `height`
327
+ defaults to a height that fits the code (from 250px to 600px).
328
+ - The `data-*` attributes of each element (or its `<pre>`) override the
329
+ options for that console (eg. `data-theme="dark"` or `data-height="400"`)
330
+ and a `language-ts` or `language-typescript` class turns on TypeScript.
331
+ - It returns an array with `{element, destroy}` for each console, in the same
332
+ order as the elements. Destroying a console that replaced its element puts
333
+ the element back.
334
+
335
+ `YourJSBox.version` is the version of YourJS Box that was loaded.
289
336
 
290
337
  ### Where Code Runs
291
338
 
@@ -310,15 +357,17 @@ between blocks.
310
357
  Install the development dependencies by running `npm install`.
311
358
 
312
359
  - `npm run dev` builds, rebuilds whenever one of the files in `src/` changes
313
- and serves the examples at http://localhost:3000/examples/ (opening them in
314
- your default browser) with the browser reloading automatically after each
315
- rebuild. Use `BROWSER=none npm run dev` to keep it from opening a browser.
360
+ and serves the landing page and examples at http://localhost:3000/ (opening
361
+ it in your default browser) with the browser reloading automatically after
362
+ each rebuild. Use `BROWSER=none npm run dev` to keep it from opening a browser.
316
363
  - `npm run build` builds the files in `dist/` once.
317
364
  - `npm test` builds and then runs the browser tests in `test/run.js` using your
318
365
  installed copy of Google Chrome (set `CHROME_PATH` to use another Chromium
319
366
  based browser). An internet connection is needed because the console loads
320
367
  its libraries from CDNs. Pass part of a test's name to run only matching
321
368
  tests (eg. `node test/run.js module`).
369
+ - Before releasing, move the notes under **Unreleased** in
370
+ [CHANGELOG.md](CHANGELOG.md) into a section for the new version.
322
371
  - `npm run release -- <patch|minor|major>` releases a new version: it checks
323
372
  that you're on an up to date, clean `main`, runs the tests, runs
324
373
  `npm version`, pushes `main` and then the tag (separately, because GitHub
@@ -327,6 +376,8 @@ Install the development dependencies by running `npm install`.
327
376
  `--dry-run` to see what it would do or `--skip-tests` to skip the tests.
328
377
  - `npm run record-demo` records `demo.gif` (shown at the top of this README)
329
378
  from the built files. It needs [ffmpeg](https://ffmpeg.org/).
379
+ - `npm run og-image` makes `og-image.png` (the picture shown when the landing
380
+ page is shared) from the built files.
330
381
  - `npm run purge-cdn` purges jsDelivr's cache so that URLs like
331
382
  `yourjs-box@1` point to the latest version right away.
332
383
  - In VS Code, **Terminal &rarr; Run Task&hellip;** has tasks for all of these
@@ -343,15 +394,12 @@ The options are kept in the URL so the page can be reloaded or shared as is.
343
394
 
344
395
  Ideas for future versions, roughly from easiest to hardest:
345
396
 
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>&#8984;</kbd> + <kbd>Shift</kbd> + <kbd>Enter</kbd> or from the
352
- **&#8943;** menu.
397
+ - **Smaller Acorn:** Acorn's package only has an unminified build (about 60 KB
398
+ compressed). A minified copy is about 35 KB (jsDelivr makes one
399
+ automatically, but unpkg and self-hosted copies don't), so Acorn could be
400
+ loaded from jsDelivr by default or a minified copy could be published with
401
+ this package.
353
402
  - **`data-autorun`:** run every visible block when the page loads.
354
- - **Clear shortcut:** clear the console with <kbd>Ctrl</kbd> + <kbd>L</kbd>.
355
403
  - **Autocomplete:** turn on Ace's keyword and snippet completion in the editor.
356
404
  - **`data-remember`:** save the editor's code and history in the browser so
357
405
  that they are still there after a reload.