@jupyterlab/javascript-extension 4.0.0-alpha.19 → 4.0.0-alpha.20

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.
Files changed (2) hide show
  1. package/package.json +7 -6
  2. package/src/index.ts +76 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jupyterlab/javascript-extension",
3
- "version": "4.0.0-alpha.19",
3
+ "version": "4.0.0-alpha.20",
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.19",
36
- "@jupyterlab/rendermime-interfaces": "^3.8.0-alpha.19"
36
+ "@jupyterlab/rendermime": "^4.0.0-alpha.20",
37
+ "@jupyterlab/rendermime-interfaces": "^3.8.0-alpha.20"
37
38
  },
38
39
  "devDependencies": {
39
40
  "rimraf": "~3.0.0",
40
- "typedoc": "~0.22.10",
41
- "typescript": "~4.7.3"
41
+ "typedoc": "~0.23.25",
42
+ "typescript": "~5.0.0-beta"
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;