@jupyterlab/javascript-extension 4.0.0-alpha.2 → 4.0.0-alpha.21

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,19 +1,15 @@
1
1
  # javascript-extension
2
2
 
3
- A JupyterLab extension for running Javascript from a notebook in JupyterLab
3
+ A JupyterLab extension for running Javascript from a notebook in JupyterLab.
4
4
 
5
- ## Prerequisites
6
-
7
- - JupyterLab ^0.27.0
5
+ This extension is in the official JupyterLab distribution.
8
6
 
9
7
  ## Usage
10
8
 
11
- To render VDOM output in IPython:
12
-
13
9
  ```python
14
10
  from IPython.display import Javascript
15
11
 
16
- Javascript('console.log("hello world");')
12
+ Javascript('alert("hello world");')
17
13
  ```
18
14
 
19
15
  ## Development
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jupyterlab/javascript-extension",
3
- "version": "4.0.0-alpha.2",
3
+ "version": "4.0.0-alpha.21",
4
4
  "description": "JupyterLab - Javascript Renderer",
5
5
  "homepage": "https://github.com/jupyterlab/jupyterlab",
6
6
  "bugs": {
@@ -23,7 +23,8 @@
23
23
  },
24
24
  "files": [
25
25
  "lib/*.{d.ts,js,js.map}",
26
- "style/*"
26
+ "style/*",
27
+ "src/**/*.{ts,tsx}"
27
28
  ],
28
29
  "scripts": {
29
30
  "build": "tsc -b",
@@ -32,13 +33,13 @@
32
33
  "watch": "tsc -b --watch"
33
34
  },
34
35
  "dependencies": {
35
- "@jupyterlab/rendermime": "^4.0.0-alpha.2",
36
- "@jupyterlab/rendermime-interfaces": "^4.0.0-alpha.2"
36
+ "@jupyterlab/rendermime": "^4.0.0-alpha.21",
37
+ "@jupyterlab/rendermime-interfaces": "^3.8.0-alpha.21"
37
38
  },
38
39
  "devDependencies": {
39
40
  "rimraf": "~3.0.0",
40
- "typedoc": "~0.21.2",
41
- "typescript": "~4.5.2"
41
+ "typedoc": "~0.23.25",
42
+ "typescript": "~5.0.1-rc"
42
43
  },
43
44
  "publishConfig": {
44
45
  "access": "public"
package/src/index.ts ADDED
@@ -0,0 +1,76 @@
1
+ // Copyright (c) Jupyter Development Team.
2
+ // Distributed under the terms of the Modified BSD License.
3
+ /**
4
+ * @packageDocumentation
5
+ * @module javascript-extension
6
+ */
7
+
8
+ import { RenderedJavaScript } from '@jupyterlab/rendermime';
9
+ import { IRenderMime } from '@jupyterlab/rendermime-interfaces';
10
+
11
+ export const TEXT_JAVASCRIPT_MIMETYPE = 'text/javascript';
12
+ export const APPLICATION_JAVASCRIPT_MIMETYPE = 'application/javascript';
13
+
14
+ function evalInContext(
15
+ code: string,
16
+ element: Element,
17
+ document: Document,
18
+ window: Window
19
+ ) {
20
+ // eslint-disable-next-line
21
+ return eval(code);
22
+ }
23
+
24
+ export class ExperimentalRenderedJavascript extends RenderedJavaScript {
25
+ render(model: IRenderMime.IMimeModel): Promise<void> {
26
+ const trans = this.translator.load('jupyterlab');
27
+ const renderJavascript = () => {
28
+ try {
29
+ const data = model.data[this.mimeType] as string | undefined;
30
+ if (data) {
31
+ evalInContext(data, this.node, document, window);
32
+ }
33
+ return Promise.resolve();
34
+ } catch (error) {
35
+ return Promise.reject(error);
36
+ }
37
+ };
38
+ if (!model.trusted) {
39
+ // If output is not trusted or if arbitrary Javascript execution is not enabled, render an informative error message
40
+ const pre = document.createElement('pre');
41
+ pre.textContent = trans.__(
42
+ 'Are you sure that you want to run arbitrary Javascript within your JupyterLab session?'
43
+ );
44
+ const button = document.createElement('button');
45
+ button.textContent = trans.__('Run');
46
+
47
+ this.node.appendChild(pre);
48
+ this.node.appendChild(button);
49
+
50
+ button.onclick = event => {
51
+ this.node.textContent = '';
52
+ void renderJavascript();
53
+ };
54
+ return Promise.resolve();
55
+ }
56
+ return renderJavascript();
57
+ }
58
+ }
59
+
60
+ /**
61
+ * A mime renderer factory for text/javascript data.
62
+ */
63
+ export const rendererFactory: IRenderMime.IRendererFactory = {
64
+ safe: false,
65
+ mimeTypes: [TEXT_JAVASCRIPT_MIMETYPE, APPLICATION_JAVASCRIPT_MIMETYPE],
66
+ createRenderer: options => new ExperimentalRenderedJavascript(options)
67
+ };
68
+
69
+ const extension: IRenderMime.IExtension = {
70
+ id: '@jupyterlab/javascript-extension:factory',
71
+ rendererFactory,
72
+ rank: 0,
73
+ dataType: 'string'
74
+ };
75
+
76
+ export default extension;