yourjs-box 1.3.1 → 1.5.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
@@ -24,7 +24,8 @@ should be loaded into the console inside of the script tag:
24
24
  </div>
25
25
  ```
26
26
 
27
- The console fills its container. The script is also available from unpkg at
27
+ The console fills its container (and is never less than 150px tall). The
28
+ script is also available from unpkg at
28
29
  `https://unpkg.com/yourjs-box@1/dist/yourjs-box.min.js`.
29
30
 
30
31
  ### Code Blocks
@@ -51,6 +52,10 @@ If the editor already has code in it you are asked before it is replaced.
51
52
  console's settings and keyboard shortcuts.
52
53
  - The **&#8943;** button opens a menu with:
53
54
  - **Text size**, which is remembered for every console on the same site.
55
+ - **Open**, which loads a JavaScript file as the code that the console starts
56
+ with (its hidden blocks are hidden and Reset goes back to it).
57
+ - **Save**, which saves the code that already ran and the code in the editor
58
+ as a JavaScript file that can be opened later.
54
59
  - **Pop out into a window**, which moves the console into a separate window
55
60
  while the code keeps running in the page (so in window mode the code can
56
61
  still change the page). Close the window or click **Bring it back** to
@@ -70,6 +75,7 @@ If the editor already has code in it you are asked before it is replaced.
70
75
  | --- | --- |
71
76
  | `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. |
72
77
  | `data-show-results` | `"false"` stops the value of the last expression in each block from being shown. |
78
+ | `data-imports-url` | Where packages imported by name are loaded from. See "Importing Packages" below. |
73
79
  | `data-libraries-url` | Where to load Vue, Ace, Prism and Acorn from. See "Self-Hosting the Libraries" below. |
74
80
  | `data-runner` | `"worker"` (default) runs the code in a Web Worker. `"window"` runs it directly in the page. See below. |
75
81
  | `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. |
@@ -91,10 +97,38 @@ one block can be used in the next, but `await` can only be used inside of
91
97
  `<script type="module">` instead:
92
98
 
93
99
  - Top-level `await` works.
94
- - `import` works (eg. `import {camelCase} from 'https://cdn.jsdelivr.net/npm/lodash-es/+esm';`).
100
+ - `import` works (eg. `import {camelCase} from 'lodash-es';`).
95
101
  - Top-level declarations stay inside of their block, just like in a module.
96
102
  Use `globalThis` to share values between blocks.
97
103
 
104
+ ### Importing Packages
105
+
106
+ npm packages can be imported by name. They are loaded from
107
+ [esm.sh](https://esm.sh/), which turns npm packages into modules that work in
108
+ the browser:
109
+
110
+ ```js
111
+ // In module blocks (data-block-type="module")
112
+ import _ from 'lodash';
113
+ import {format} from 'date-fns@4';
114
+
115
+ // In any block (including classic blocks), inside of async code
116
+ const {default: dayjs} = await import('dayjs');
117
+ ```
118
+
119
+ Relative paths (eg. `./utils.js`), URLs (eg. `https://example.com/x.js`) and
120
+ `node:` imports are left alone. Packages that need Node.js (eg. ones that use
121
+ `fs`) can't run in a browser.
122
+
123
+ To load packages from somewhere else set `data-imports-url` to a URL where
124
+ `{specifier}` is replaced with what was imported (eg. `lodash@4/fp`):
125
+
126
+ | Where | `data-imports-url` |
127
+ | --- | --- |
128
+ | esm.sh (the default) | `https://esm.sh/{specifier}` |
129
+ | jsDelivr | `https://cdn.jsdelivr.net/npm/{specifier}/+esm` |
130
+ | Turned off (only URLs and paths can be imported) | `""` |
131
+
98
132
  ### Self-Hosting the Libraries
99
133
 
100
134
  The console's interface uses [Vue](https://vuejs.org/),
@@ -140,6 +174,43 @@ up in the output as a collapsed block labelled with whatever came after the
140
174
  prefix (and optional colon), or "Hidden code" if nothing did. Clicking the
141
175
  label shows or hides the code.
142
176
 
177
+ ### JavaScript API
178
+
179
+ Loading the script also provides `YourJSBox` for creating consoles from
180
+ JavaScript (eg. in React, Vue or any page that adds content dynamically). A
181
+ script tag in the `<head>` only provides the API, while one in the `<body>` is
182
+ also replaced by a console (with the code inside of it) as usual.
183
+
184
+ ```html
185
+ <script src="https://cdn.jsdelivr.net/npm/yourjs-box@1/dist/yourjs-box.min.js"></script>
186
+ ```
187
+
188
+ ```js
189
+ const box = YourJSBox.create({
190
+ target: '#lesson',
191
+ code: "// Say hello \\\\\nconsole.log('Hello!');",
192
+ theme: 'dark',
193
+ });
194
+
195
+ // Later (eg. when a component is removed):
196
+ box.destroy();
197
+ ```
198
+
199
+ `YourJSBox.create(options)` returns `{element, destroy}` where `element` is the
200
+ console's IFRAME and `destroy()` removes it, stops its code and closes its
201
+ pop-out window (in window mode it also restores the page's `console`
202
+ functions). The options are:
203
+
204
+ | Option | Description |
205
+ | --- | --- |
206
+ | `target` | Required. The element (or a CSS selector for it) that the console is placed relative to. |
207
+ | `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. |
208
+ | `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. |
209
+ | `code` | The code that the console starts with. |
210
+ | `runner`, `blockType`, `showResults`, `hidePrefix`, `dividerOrient`, `theme`, `importsUrl`, `librariesUrl` | The same as the `data-*` attributes above. |
211
+
212
+ `YourJSBox.version` is the version of JS Box that was loaded.
213
+
143
214
  ### Where Code Runs
144
215
 
145
216
  The console's interface is always in an IFRAME so that its styles and the
@@ -171,6 +242,16 @@ Install the development dependencies by running `npm install`.
171
242
  based browser). An internet connection is needed because the console loads
172
243
  its libraries from CDNs. Pass part of a test's name to run only matching
173
244
  tests (eg. `node test/run.js module`).
245
+ - `npm run release -- <patch|minor|major>` releases a new version: it checks
246
+ that you're on an up to date, clean `main`, runs the tests, runs
247
+ `npm version`, pushes `main` and then the tag (separately, because GitHub
248
+ Pages doesn't always deploy when they are pushed together), publishes to npm
249
+ and, once npm lists the new version, purges jsDelivr's cache. Add
250
+ `--dry-run` to see what it would do or `--skip-tests` to skip the tests.
251
+ - `npm run purge-cdn` purges jsDelivr's cache so that URLs like
252
+ `yourjs-box@1` point to the latest version right away.
253
+ - In VS Code, **Terminal &rarr; Run Task&hellip;** has tasks for all of these
254
+ (eg. Build, Dev server, Test, Release&hellip; and Redeploy GitHub Pages).
174
255
  - `npm start` builds and then rebuilds whenever one of the files in `src/`
175
256
  changes (without serving anything).
176
257
 
@@ -186,7 +267,6 @@ test `dist/yourjs-box.js` or `dist/yourjs-box.min.js` instead of
186
267
  - Add `@timeout` annotation to the special comments that will allow you to input the amount of seconds to wait since the last call to a console logging function before automatically running the next comment segmented block.
187
268
  - Allow for TypeScript
188
269
  - Allow for CoffeeScript
189
- - Allow code to run in main window.
190
270
 
191
271
  ## License
192
272