UNPKG

9.87 kBJavaScriptView Raw
1import { nullTranslator } from '@jupyterlab/translation';
2import { Widget } from '@lumino/widgets';
3import * as renderers from './renderers';
4/**
5 * A common base class for mime renderers.
6 */
7export class RenderedCommon extends Widget {
8 /**
9 * Construct a new rendered common widget.
10 *
11 * @param options - The options for initializing the widget.
12 */
13 constructor(options) {
14 super();
15 this.mimeType = options.mimeType;
16 this.sanitizer = options.sanitizer;
17 this.resolver = options.resolver;
18 this.linkHandler = options.linkHandler;
19 this.translator = options.translator || nullTranslator;
20 this.latexTypesetter = options.latexTypesetter;
21 this.node.dataset['mimeType'] = this.mimeType;
22 }
23 /**
24 * Render a mime model.
25 *
26 * @param model - The mime model to render.
27 *
28 * @returns A promise which resolves when rendering is complete.
29 *
30 * #### Notes
31 * If the DOM node for this widget already has content, it is emptied
32 * before rendering. Subclasses that do not want this behavior
33 * (if, for instance, they are using DOM diffing), should override
34 * this method and not call `super.renderModel()`.
35 */
36 async renderModel(model) {
37 // TODO compare model against old model for early bail?
38 // Empty any existing content in the node from previous renders
39 while (this.node.firstChild) {
40 this.node.removeChild(this.node.firstChild);
41 }
42 // Toggle the trusted class on the widget.
43 this.toggleClass('jp-mod-trusted', model.trusted);
44 // Render the actual content.
45 await this.render(model);
46 // Handle the fragment identifier if given.
47 const { fragment } = model.metadata;
48 if (fragment) {
49 this.setFragment(fragment);
50 }
51 }
52 /**
53 * Set the URI fragment identifier.
54 *
55 * @param fragment - The URI fragment identifier.
56 */
57 setFragment(fragment) {
58 /* no-op */
59 }
60}
61/**
62 * A common base class for HTML mime renderers.
63 */
64export class RenderedHTMLCommon extends RenderedCommon {
65 /**
66 * Construct a new rendered HTML common widget.
67 *
68 * @param options - The options for initializing the widget.
69 */
70 constructor(options) {
71 super(options);
72 this.addClass('jp-RenderedHTMLCommon');
73 }
74 setFragment(fragment) {
75 let el;
76 try {
77 el = this.node.querySelector(fragment.startsWith('#')
78 ? `#${CSS.escape(fragment.slice(1))}`
79 : fragment);
80 }
81 catch (error) {
82 console.warn('Unable to set URI fragment identifier.', error);
83 }
84 if (el) {
85 el.scrollIntoView();
86 }
87 }
88}
89/**
90 * A mime renderer for displaying HTML and math.
91 */
92export class RenderedHTML extends RenderedHTMLCommon {
93 /**
94 * Construct a new rendered HTML widget.
95 *
96 * @param options - The options for initializing the widget.
97 */
98 constructor(options) {
99 super(options);
100 this.addClass('jp-RenderedHTML');
101 }
102 /**
103 * Render a mime model.
104 *
105 * @param model - The mime model to render.
106 *
107 * @returns A promise which resolves when rendering is complete.
108 */
109 render(model) {
110 return renderers.renderHTML({
111 host: this.node,
112 source: String(model.data[this.mimeType]),
113 trusted: model.trusted,
114 resolver: this.resolver,
115 sanitizer: this.sanitizer,
116 linkHandler: this.linkHandler,
117 shouldTypeset: this.isAttached,
118 latexTypesetter: this.latexTypesetter,
119 translator: this.translator
120 });
121 }
122 /**
123 * A message handler invoked on an `'after-attach'` message.
124 */
125 onAfterAttach(msg) {
126 if (this.latexTypesetter) {
127 this.latexTypesetter.typeset(this.node);
128 }
129 }
130}
131/**
132 * A mime renderer for displaying LaTeX output.
133 */
134export class RenderedLatex extends RenderedCommon {
135 /**
136 * Construct a new rendered LaTeX widget.
137 *
138 * @param options - The options for initializing the widget.
139 */
140 constructor(options) {
141 super(options);
142 this.addClass('jp-RenderedLatex');
143 }
144 /**
145 * Render a mime model.
146 *
147 * @param model - The mime model to render.
148 *
149 * @returns A promise which resolves when rendering is complete.
150 */
151 render(model) {
152 return renderers.renderLatex({
153 host: this.node,
154 source: String(model.data[this.mimeType]),
155 shouldTypeset: this.isAttached,
156 latexTypesetter: this.latexTypesetter
157 });
158 }
159 /**
160 * A message handler invoked on an `'after-attach'` message.
161 */
162 onAfterAttach(msg) {
163 if (this.latexTypesetter) {
164 this.latexTypesetter.typeset(this.node);
165 }
166 }
167}
168/**
169 * A mime renderer for displaying images.
170 */
171export class RenderedImage extends RenderedCommon {
172 /**
173 * Construct a new rendered image widget.
174 *
175 * @param options - The options for initializing the widget.
176 */
177 constructor(options) {
178 super(options);
179 this.addClass('jp-RenderedImage');
180 }
181 /**
182 * Render a mime model.
183 *
184 * @param model - The mime model to render.
185 *
186 * @returns A promise which resolves when rendering is complete.
187 */
188 render(model) {
189 const metadata = model.metadata[this.mimeType];
190 return renderers.renderImage({
191 host: this.node,
192 mimeType: this.mimeType,
193 source: String(model.data[this.mimeType]),
194 width: metadata && metadata.width,
195 height: metadata && metadata.height,
196 needsBackground: model.metadata['needs_background'],
197 unconfined: metadata && metadata.unconfined
198 });
199 }
200}
201/**
202 * A mime renderer for displaying Markdown with embedded latex.
203 */
204export class RenderedMarkdown extends RenderedHTMLCommon {
205 /**
206 * Construct a new rendered markdown widget.
207 *
208 * @param options - The options for initializing the widget.
209 */
210 constructor(options) {
211 super(options);
212 this.addClass('jp-RenderedMarkdown');
213 }
214 /**
215 * Render a mime model.
216 *
217 * @param model - The mime model to render.
218 *
219 * @returns A promise which resolves when rendering is complete.
220 */
221 render(model) {
222 return renderers.renderMarkdown({
223 host: this.node,
224 source: String(model.data[this.mimeType]),
225 trusted: model.trusted,
226 resolver: this.resolver,
227 sanitizer: this.sanitizer,
228 linkHandler: this.linkHandler,
229 shouldTypeset: this.isAttached,
230 latexTypesetter: this.latexTypesetter,
231 translator: this.translator
232 });
233 }
234 /**
235 * A message handler invoked on an `'after-attach'` message.
236 */
237 onAfterAttach(msg) {
238 if (this.latexTypesetter) {
239 this.latexTypesetter.typeset(this.node);
240 }
241 }
242}
243/**
244 * A widget for displaying SVG content.
245 */
246export class RenderedSVG extends RenderedCommon {
247 /**
248 * Construct a new rendered SVG widget.
249 *
250 * @param options - The options for initializing the widget.
251 */
252 constructor(options) {
253 super(options);
254 this.addClass('jp-RenderedSVG');
255 }
256 /**
257 * Render a mime model.
258 *
259 * @param model - The mime model to render.
260 *
261 * @returns A promise which resolves when rendering is complete.
262 */
263 render(model) {
264 const metadata = model.metadata[this.mimeType];
265 return renderers.renderSVG({
266 host: this.node,
267 source: String(model.data[this.mimeType]),
268 trusted: model.trusted,
269 unconfined: metadata && metadata.unconfined,
270 translator: this.translator
271 });
272 }
273 /**
274 * A message handler invoked on an `'after-attach'` message.
275 */
276 onAfterAttach(msg) {
277 if (this.latexTypesetter) {
278 this.latexTypesetter.typeset(this.node);
279 }
280 }
281}
282/**
283 * A widget for displaying plain text and console text.
284 */
285export class RenderedText extends RenderedCommon {
286 /**
287 * Construct a new rendered text widget.
288 *
289 * @param options - The options for initializing the widget.
290 */
291 constructor(options) {
292 super(options);
293 this.addClass('jp-RenderedText');
294 }
295 /**
296 * Render a mime model.
297 *
298 * @param model - The mime model to render.
299 *
300 * @returns A promise which resolves when rendering is complete.
301 */
302 render(model) {
303 return renderers.renderText({
304 host: this.node,
305 sanitizer: this.sanitizer,
306 source: String(model.data[this.mimeType]),
307 translator: this.translator
308 });
309 }
310}
311/**
312 * A widget for displaying JavaScript output.
313 */
314export class RenderedJavaScript extends RenderedCommon {
315 /**
316 * Construct a new rendered text widget.
317 *
318 * @param options - The options for initializing the widget.
319 */
320 constructor(options) {
321 super(options);
322 this.addClass('jp-RenderedJavaScript');
323 }
324 /**
325 * Render a mime model.
326 *
327 * @param model - The mime model to render.
328 *
329 * @returns A promise which resolves when rendering is complete.
330 */
331 render(model) {
332 const trans = this.translator.load('jupyterlab');
333 return renderers.renderText({
334 host: this.node,
335 sanitizer: this.sanitizer,
336 source: trans.__('JavaScript output is disabled in JupyterLab'),
337 translator: this.translator
338 });
339 }
340}
341//# sourceMappingURL=widgets.js.map
\No newline at end of file