yourjs-box 1.5.0 → 1.7.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/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2023-present Christopher West
3
+ Copyright (c) 2023-present Chris West
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
package/README.md CHANGED
@@ -2,7 +2,7 @@
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/)
@@ -40,6 +40,7 @@ If the editor already has code in it you are asked before it is replaced.
40
40
 
41
41
  ### Toolbar
42
42
 
43
+ - The **⋯** button opens a menu (see below).
43
44
  - **Clear** removes everything from the console. Code can also call
44
45
  `console.clear()`.
45
46
  - The **layout** button switches between showing the editor beside or below
@@ -50,12 +51,20 @@ If the editor already has code in it you are asked before it is replaced.
50
51
  - **Run** runs the next block of code.
51
52
  - Clicking the **logo** opens the About window, which shows the version, this
52
53
  console's settings and keyboard shortcuts.
53
- - The **⋯** button opens a menu with:
54
+ - **History** (or <kbd>Alt</kbd>/<kbd>&#8997;</kbd>+<kbd>H</kbd>) lists the code
55
+ that was run so it can be put back into the editor. Like the browser's
56
+ console, pressing <kbd>&uarr;</kbd> when the cursor is at the very start of
57
+ the editor (with nothing selected) shows the previous code that was run and
58
+ <kbd>&darr;</kbd> at the very end goes forward again and eventually back to
59
+ the code that hasn't been run yet. After running code from the history, the
60
+ code that hasn't been run yet comes back. Hidden blocks aren't in the
61
+ history.
62
+ - The **&#8943;** menu has:
54
63
  - **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
64
+ - **Open**, which loads a JavaScript (or TypeScript) file as the code that the console starts
56
65
  with (its hidden blocks are hidden and Reset goes back to it).
57
66
  - **Save**, which saves the code that already ran and the code in the editor
58
- as a JavaScript file that can be opened later.
67
+ as a JavaScript (or TypeScript) file that can be opened later.
59
68
  - **Pop out into a window**, which moves the console into a separate window
60
69
  while the code keeps running in the page (so in window mode the code can
61
70
  still change the page). Close the window or click **Bring it back** to
@@ -69,14 +78,31 @@ If the editor already has code in it you are asked before it is replaced.
69
78
  (eg. an infinite loop) by starting a new worker.
70
79
  - **About JS Box**
71
80
 
81
+ ### Right-Clicking Values
82
+
83
+ Right-click any value in the output (including values inside of expanded
84
+ objects and arrays) for a menu with:
85
+
86
+ - **Copy as JSON** and **Save as JSON&hellip;**. Maps and Sets become arrays
87
+ and BigInts become strings. Circular references (an object inside of itself)
88
+ are left out and a message says how many were left out.
89
+ - **Store as global variable**, which stores the actual value (not a copy) as
90
+ `temp1`, `temp2`, etc. so that later code can use it (in window mode it is
91
+ stored on the page's `window`).
92
+ - **Copy property path** (for properties and array items), which copies the
93
+ path from the logged value like `people[0].name`.
94
+ - **Refresh** (for objects, arrays, etc.), which shows the value as it is now
95
+ if the code has changed it. Anything that was expanded stays expanded.
96
+
72
97
  ### Attributes
73
98
 
74
99
  | Attribute | Description |
75
100
  | --- | --- |
76
101
  | `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. |
102
+ | `data-language` | `"javascript"` (default) or `"typescript"`. See "TypeScript" below. |
77
103
  | `data-show-results` | `"false"` stops the value of the last expression in each block from being shown. |
78
104
  | `data-imports-url` | Where packages imported by name are loaded from. See "Importing Packages" below. |
79
- | `data-libraries-url` | Where to load Vue, Ace, Prism and Acorn from. See "Self-Hosting the Libraries" below. |
105
+ | `data-libraries-url` | Where to load Vue, Ace, Prism, Acorn and Babel from. See "Self-Hosting the Libraries" below. |
80
106
  | `data-runner` | `"worker"` (default) runs the code in a Web Worker. `"window"` runs it directly in the page. See below. |
81
107
  | `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. |
82
108
  | `data-hide-prefix` | Any block whose header starts with this prefix is hidden. See below. |
@@ -101,6 +127,36 @@ one block can be used in the next, but `await` can only be used inside of
101
127
  - Top-level declarations stay inside of their block, just like in a module.
102
128
  Use `globalThis` to share values between blocks.
103
129
 
130
+ ### TypeScript
131
+
132
+ Add `data-language="typescript"` to write the code in TypeScript:
133
+
134
+ ```html
135
+ <script src="https://cdn.jsdelivr.net/npm/yourjs-box@1/dist/yourjs-box.min.js" data-language="typescript">
136
+ // Describe a person \\
137
+ interface Person { name: string; born: number }
138
+ const people: Person[] = [{name: 'Ada', born: 1815}, {name: 'Grace', born: 1906}];
139
+
140
+ // Find the oldest \\
141
+ function oldest<T extends Person>(list: T[]): T {
142
+ return list.reduce((a, b) => a.born <= b.born ? a : b);
143
+ }
144
+ oldest(people)
145
+ </script>
146
+ ```
147
+
148
+ Before each block runs, [Babel](https://babeljs.io/) removes the types, which
149
+ is how most TypeScript tools run code without a build step. The types aren't
150
+ checked, so code with type errors still runs, just like it would in
151
+ JavaScript. Enums, namespaces and parameter properties all work, and errors
152
+ point to the lines and columns in the TypeScript code.
153
+
154
+ TypeScript works with both block types. Only type imports are removed (eg.
155
+ `import type {Options} from 'x'` or the `type Options` part of
156
+ `import {type Options, format} from 'x'`), so an import is never dropped just
157
+ because nothing uses it yet. Babel is only downloaded by consoles that use
158
+ TypeScript.
159
+
104
160
  ### Importing Packages
105
161
 
106
162
  npm packages can be imported by name. They are loaded from
@@ -133,7 +189,8 @@ To load packages from somewhere else set `data-imports-url` to a URL where
133
189
 
134
190
  The console's interface uses [Vue](https://vuejs.org/),
135
191
  [Ace](https://ace.c9.io/), [Prism](https://prismjs.com/) and
136
- [Acorn](https://github.com/acornjs/acorn), which are loaded from unpkg by
192
+ [Acorn](https://github.com/acornjs/acorn) (plus
193
+ [Babel](https://babeljs.io/) for TypeScript), which are loaded from unpkg by
137
194
  default. Exact versions are always used so that a new release of one of them
138
195
  can't change how the console works.
139
196
 
@@ -154,6 +211,9 @@ next to their main files):
154
211
 
155
212
  ```bash
156
213
  npm install vue@3.5.43 ace-builds@1.44.0 prismjs@1.30.0 prism-themes@1.9.0 acorn@8.18.0
214
+
215
+ # Only needed for TypeScript consoles
216
+ npm install @babel/standalone@7.29.9
157
217
  ```
158
218
 
159
219
  The About window lists the versions that each version of JS Box uses.
@@ -207,7 +267,7 @@ functions). The options are:
207
267
  | `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
268
  | `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
269
  | `code` | The code that the console starts with. |
210
- | `runner`, `blockType`, `showResults`, `hidePrefix`, `dividerOrient`, `theme`, `importsUrl`, `librariesUrl` | The same as the `data-*` attributes above. |
270
+ | `runner`, `blockType`, `language`, `showResults`, `hidePrefix`, `dividerOrient`, `theme`, `importsUrl`, `librariesUrl` | The same as the `data-*` attributes above. |
211
271
 
212
272
  `YourJSBox.version` is the version of JS Box that was loaded.
213
273
 
@@ -234,8 +294,9 @@ between blocks.
234
294
  Install the development dependencies by running `npm install`.
235
295
 
236
296
  - `npm run dev` builds, rebuilds whenever one of the files in `src/` changes
237
- and serves the examples at http://localhost:3000/examples/ with the browser
238
- reloading automatically after each rebuild.
297
+ and serves the examples at http://localhost:3000/examples/ (opening them in
298
+ your default browser) with the browser reloading automatically after each
299
+ rebuild. Use `BROWSER=none npm run dev` to keep it from opening a browser.
239
300
  - `npm run build` builds the files in `dist/` once.
240
301
  - `npm test` builds and then runs the browser tests in `test/run.js` using your
241
302
  installed copy of Google Chrome (set `CHROME_PATH` to use another Chromium
@@ -260,18 +321,10 @@ which cover every feature. Add `?build=standard` or `?build=min` to its URL to
260
321
  test `dist/yourjs-box.js` or `dist/yourjs-box.min.js` instead of
261
322
  `dist/yourjs-box.full.js`.
262
323
 
263
- ## Roadmap
264
-
265
- - Open button - Load a JS file from the filesystem.
266
- - Save button - Save the current inputs as a JS file that can be opened later.
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.
268
- - Allow for TypeScript
269
- - Allow for CoffeeScript
270
-
271
324
  ## License
272
325
 
273
326
  Released under the [MIT License](LICENSE). You're free to use, modify and
274
327
  distribute it, including in commercial projects, as long as the copyright and
275
328
  license notice is kept.
276
329
 
277
- Copyright (c) 2023-present Christopher West
330
+ Copyright (c) 2023-present Chris West