@jupyterlab/vega5-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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jupyterlab/vega5-extension",
3
- "version": "4.0.0-alpha.19",
3
+ "version": "4.0.0-alpha.20",
4
4
  "description": "JupyterLab - Vega 5 and Vega-Lite 5 Mime Renderer Extension",
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",
@@ -37,21 +38,21 @@
37
38
  "watch": "tsc -b --watch"
38
39
  },
39
40
  "dependencies": {
40
- "@jupyterlab/rendermime-interfaces": "^3.8.0-alpha.19",
41
- "@lumino/coreutils": "^2.0.0-beta.0",
42
- "@lumino/widgets": "^2.0.0-beta.1",
41
+ "@jupyterlab/rendermime-interfaces": "^3.8.0-alpha.20",
42
+ "@lumino/coreutils": "^2.0.0-rc.0",
43
+ "@lumino/widgets": "^2.0.0-rc.0",
43
44
  "vega": "^5.20.0",
44
45
  "vega-embed": "^6.2.1",
45
- "vega-lite": "^5.1.0"
46
+ "vega-lite": "^5.6.1-next.1"
46
47
  },
47
48
  "devDependencies": {
48
- "@jupyterlab/testutils": "^4.0.0-alpha.19",
49
+ "@jupyterlab/testutils": "^4.0.0-alpha.20",
49
50
  "@types/jest": "^29.2.0",
50
51
  "@types/webpack-env": "^1.14.1",
51
52
  "jest": "^29.2.0",
52
53
  "rimraf": "~3.0.0",
53
- "typedoc": "~0.22.10",
54
- "typescript": "~4.7.3"
54
+ "typedoc": "~0.23.25",
55
+ "typescript": "~5.0.0-beta"
55
56
  },
56
57
  "publishConfig": {
57
58
  "access": "public"
package/src/index.ts ADDED
@@ -0,0 +1,254 @@
1
+ /* -----------------------------------------------------------------------------
2
+ | Copyright (c) Jupyter Development Team.
3
+ | Distributed under the terms of the Modified BSD License.
4
+ |----------------------------------------------------------------------------*/
5
+ /**
6
+ * @packageDocumentation
7
+ * @module vega5-extension
8
+ */
9
+
10
+ import { IRenderMime } from '@jupyterlab/rendermime-interfaces';
11
+ import { JSONObject } from '@lumino/coreutils';
12
+ import { Widget } from '@lumino/widgets';
13
+ import * as VegaModuleType from 'vega-embed';
14
+
15
+ /**
16
+ * The CSS class to add to the Vega and Vega-Lite widget.
17
+ */
18
+ const VEGA_COMMON_CLASS = 'jp-RenderedVegaCommon5';
19
+
20
+ /**
21
+ * The CSS class to add to the Vega.
22
+ */
23
+ const VEGA_CLASS = 'jp-RenderedVega5';
24
+
25
+ /**
26
+ * The CSS class to add to the Vega-Lite.
27
+ */
28
+ const VEGALITE_CLASS = 'jp-RenderedVegaLite';
29
+
30
+ /**
31
+ * The MIME type for Vega.
32
+ *
33
+ * #### Notes
34
+ * The version of this follows the major version of Vega.
35
+ */
36
+ export const VEGA_MIME_TYPE = 'application/vnd.vega.v5+json';
37
+
38
+ /**
39
+ * The MIME type for Vega-Lite.
40
+ *
41
+ * #### Notes
42
+ * The version of this follows the major version of Vega-Lite.
43
+ */
44
+ export const VEGALITE3_MIME_TYPE = 'application/vnd.vegalite.v3+json';
45
+
46
+ /**
47
+ * The MIME type for Vega-Lite.
48
+ *
49
+ * #### Notes
50
+ * The version of this follows the major version of Vega-Lite.
51
+ */
52
+ export const VEGALITE4_MIME_TYPE = 'application/vnd.vegalite.v4+json';
53
+
54
+ /**
55
+ * The MIME type for Vega-Lite.
56
+ *
57
+ * #### Notes
58
+ * The version of this follows the major version of Vega-Lite.
59
+ */
60
+ export const VEGALITE5_MIME_TYPE = 'application/vnd.vegalite.v5+json';
61
+
62
+ /**
63
+ * A widget for rendering Vega or Vega-Lite data, for usage with rendermime.
64
+ */
65
+ export class RenderedVega extends Widget implements IRenderMime.IRenderer {
66
+ private _result: VegaModuleType.Result;
67
+
68
+ /**
69
+ * Create a new widget for rendering Vega/Vega-Lite.
70
+ */
71
+ constructor(options: IRenderMime.IRendererOptions) {
72
+ super();
73
+ this._mimeType = options.mimeType;
74
+ this._resolver = options.resolver;
75
+ this.addClass(VEGA_COMMON_CLASS);
76
+ this.addClass(
77
+ this._mimeType === VEGA_MIME_TYPE ? VEGA_CLASS : VEGALITE_CLASS
78
+ );
79
+ }
80
+
81
+ /**
82
+ * Render Vega/Vega-Lite into this widget's node.
83
+ */
84
+ async renderModel(model: IRenderMime.IMimeModel): Promise<void> {
85
+ const spec = model.data[this._mimeType] as JSONObject | undefined;
86
+ if (spec === undefined) {
87
+ return;
88
+ }
89
+ const metadata = model.metadata[this._mimeType] as
90
+ | {
91
+ embed_options?: VegaModuleType.EmbedOptions;
92
+ }
93
+ | undefined;
94
+ const embedOptions =
95
+ metadata && metadata.embed_options ? metadata.embed_options : {};
96
+
97
+ // If the JupyterLab theme is dark, render this using a dark Vega theme.
98
+ let bodyThemeDark = document.body.dataset.jpThemeLight === 'false';
99
+ if (bodyThemeDark) {
100
+ embedOptions.theme = 'dark';
101
+ }
102
+
103
+ const mode: VegaModuleType.Mode =
104
+ this._mimeType === VEGA_MIME_TYPE ? 'vega' : 'vega-lite';
105
+
106
+ const vega =
107
+ Private.vega != null ? Private.vega : await Private.ensureVega();
108
+
109
+ const el = document.createElement('div');
110
+
111
+ // clear the output before attaching a chart
112
+ this.node.textContent = '';
113
+ this.node.appendChild(el);
114
+
115
+ if (this._result) {
116
+ this._result.finalize();
117
+ }
118
+
119
+ const loader = vega.vega.loader({
120
+ http: { credentials: 'same-origin' }
121
+ });
122
+ const sanitize = async (uri: string, options: any) => {
123
+ // Use the resolver for any URIs it wants to handle
124
+ const resolver = this._resolver;
125
+ if (resolver?.isLocal && resolver.isLocal(uri)) {
126
+ const absPath = await resolver.resolveUrl(uri);
127
+ uri = await resolver.getDownloadUrl(absPath);
128
+ }
129
+ return loader.sanitize(uri, options);
130
+ };
131
+
132
+ this._result = await vega.default(el, spec, {
133
+ actions: true,
134
+ defaultStyle: true,
135
+ ...embedOptions,
136
+ mode,
137
+ loader: { ...loader, sanitize }
138
+ });
139
+
140
+ if (model.data['image/png']) {
141
+ return;
142
+ }
143
+
144
+ // Add png representation of vega chart to output
145
+ const imageURL = await this._result.view.toImageURL(
146
+ 'png',
147
+ embedOptions.scaleFactor
148
+ );
149
+ model.setData({
150
+ data: { ...model.data, 'image/png': imageURL.split(',')[1] }
151
+ });
152
+ }
153
+
154
+ dispose(): void {
155
+ if (this._result) {
156
+ this._result.finalize();
157
+ }
158
+ super.dispose();
159
+ }
160
+
161
+ private _mimeType: string;
162
+ private _resolver: IRenderMime.IResolver | null;
163
+ }
164
+
165
+ /**
166
+ * A mime renderer factory for vega data.
167
+ */
168
+ export const rendererFactory: IRenderMime.IRendererFactory = {
169
+ safe: true,
170
+ mimeTypes: [
171
+ VEGA_MIME_TYPE,
172
+ VEGALITE3_MIME_TYPE,
173
+ VEGALITE4_MIME_TYPE,
174
+ VEGALITE5_MIME_TYPE
175
+ ],
176
+ createRenderer: options => new RenderedVega(options)
177
+ };
178
+
179
+ const extension: IRenderMime.IExtension = {
180
+ id: '@jupyterlab/vega5-extension:factory',
181
+ rendererFactory,
182
+ rank: 57,
183
+ dataType: 'json',
184
+ documentWidgetFactoryOptions: [
185
+ {
186
+ name: 'Vega5',
187
+ primaryFileType: 'vega5',
188
+ fileTypes: ['vega5', 'json'],
189
+ defaultFor: ['vega5']
190
+ },
191
+ {
192
+ name: 'Vega-Lite5',
193
+ primaryFileType: 'vega-lite5',
194
+ fileTypes: ['vega-lite3', 'vega-lite4', 'vega-lite5', 'json'],
195
+ defaultFor: ['vega-lite3', 'vega-lite4', 'vega-lite5']
196
+ }
197
+ ],
198
+ fileTypes: [
199
+ {
200
+ mimeTypes: [VEGA_MIME_TYPE],
201
+ name: 'vega5',
202
+ extensions: ['.vg', '.vg.json', '.vega'],
203
+ icon: 'ui-components:vega'
204
+ },
205
+ {
206
+ mimeTypes: [VEGALITE5_MIME_TYPE],
207
+ name: 'vega-lite5',
208
+ extensions: ['.vl', '.vl.json', '.vegalite'],
209
+ icon: 'ui-components:vega'
210
+ },
211
+ {
212
+ mimeTypes: [VEGALITE4_MIME_TYPE],
213
+ name: 'vega-lite4',
214
+ extensions: [],
215
+ icon: 'ui-components:vega'
216
+ },
217
+ {
218
+ mimeTypes: [VEGALITE3_MIME_TYPE],
219
+ name: 'vega-lite3',
220
+ extensions: [],
221
+ icon: 'ui-components:vega'
222
+ }
223
+ ]
224
+ };
225
+
226
+ export default extension;
227
+
228
+ /**
229
+ * A namespace for private module data.
230
+ */
231
+ namespace Private {
232
+ /**
233
+ * A cached reference to the vega library.
234
+ */
235
+ export let vega: typeof VegaModuleType;
236
+
237
+ /**
238
+ * A Promise for the initial load of vega.
239
+ */
240
+ export let vegaReady: Promise<typeof VegaModuleType>;
241
+
242
+ /**
243
+ * Lazy-load and cache the vega-embed library
244
+ */
245
+ export function ensureVega(): Promise<typeof VegaModuleType> {
246
+ if (vegaReady) {
247
+ return vegaReady;
248
+ }
249
+
250
+ vegaReady = import('vega-embed');
251
+
252
+ return vegaReady;
253
+ }
254
+ }
package/src/json.d.ts ADDED
@@ -0,0 +1,8 @@
1
+ /*
2
+ * Copyright (c) Jupyter Development Team.
3
+ * Distributed under the terms of the Modified BSD License.
4
+ */
5
+
6
+ declare module '*.json' {
7
+ export const version: string;
8
+ }