UNPKG

20.2 kBTypeScriptView Raw
1/**
2 * @packageDocumentation
3 * @module rendermime-interfaces
4 */
5import type { ReadonlyPartialJSONObject } from '@lumino/coreutils';
6import type { Widget } from '@lumino/widgets';
7/**
8 * A namespace for rendermime associated interfaces.
9 */
10export declare namespace IRenderMime {
11 /**
12 * A model for mime data.
13 */
14 interface IMimeModel {
15 /**
16 * Whether the data in the model is trusted.
17 */
18 readonly trusted: boolean;
19 /**
20 * The data associated with the model.
21 */
22 readonly data: ReadonlyPartialJSONObject;
23 /**
24 * The metadata associated with the model.
25 *
26 * Among others, it can include an attribute named `fragment`
27 * that stores a URI fragment identifier for the MIME resource.
28 */
29 readonly metadata: ReadonlyPartialJSONObject;
30 /**
31 * Set the data associated with the model.
32 *
33 * #### Notes
34 * Calling this function may trigger an asynchronous operation
35 * that could cause the renderer to be rendered with a new model
36 * containing the new data.
37 */
38 setData(options: IMimeModel.ISetDataOptions): void;
39 }
40 /**
41 * The namespace for IMimeModel associated interfaces.
42 */
43 namespace IMimeModel {
44 /**
45 * The options used to update a mime model.
46 */
47 interface ISetDataOptions {
48 /**
49 * The new data object.
50 */
51 data?: ReadonlyPartialJSONObject;
52 /**
53 * The new metadata object.
54 */
55 metadata?: ReadonlyPartialJSONObject;
56 }
57 }
58 /**
59 * A toolbar item.
60 */
61 interface IToolbarItem {
62 /**
63 * Unique item name
64 */
65 name: string;
66 /**
67 * Toolbar widget
68 */
69 widget: Widget;
70 }
71 /**
72 * The options used to initialize a document widget factory.
73 *
74 * This interface is intended to be used by mime renderer extensions
75 * to define a document opener that uses its renderer factory.
76 */
77 interface IDocumentWidgetFactoryOptions {
78 /**
79 * The name of the widget to display in dialogs.
80 */
81 readonly name: string;
82 /**
83 * The label of the widget to display in dialogs.
84 * If not given, name is used instead.
85 */
86 readonly label?: string;
87 /**
88 * The name of the document model type.
89 */
90 readonly modelName?: string;
91 /**
92 * The primary file type of the widget.
93 */
94 readonly primaryFileType: string;
95 /**
96 * The file types the widget can view.
97 */
98 readonly fileTypes: ReadonlyArray<string>;
99 /**
100 * The file types for which the factory should be the default.
101 */
102 readonly defaultFor?: ReadonlyArray<string>;
103 /**
104 * The file types for which the factory should be the default for rendering,
105 * if that is different than the default factory (which may be for editing)
106 * If undefined, then it will fall back on the default file type.
107 */
108 readonly defaultRendered?: ReadonlyArray<string>;
109 /**
110 * The application language translator.
111 */
112 readonly translator?: ITranslator;
113 /**
114 * A function returning a list of toolbar items to add to the toolbar.
115 */
116 readonly toolbarFactory?: (widget?: Widget) => IToolbarItem[];
117 }
118 namespace LabIcon {
119 /**
120 * The simplest possible interface for defining a generic icon.
121 */
122 interface IIcon {
123 /**
124 * The name of the icon. By convention, the icon name will be namespaced
125 * as so:
126 *
127 * "pkg-name:icon-name"
128 */
129 readonly name: string;
130 /**
131 * A string containing the raw contents of an svg file.
132 */
133 svgstr: string;
134 }
135 /**
136 * Interface for generic renderer.
137 */
138 interface IRenderer {
139 readonly render: (container: HTMLElement, options?: any) => void;
140 readonly unrender?: (container: HTMLElement, options?: any) => void;
141 }
142 /**
143 * A type that can be resolved to a LabIcon instance.
144 */
145 type IResolvable = string | (IIcon & Partial<IRenderer>);
146 }
147 /**
148 * A file type to associate with the renderer.
149 */
150 interface IFileType {
151 /**
152 * The name of the file type.
153 */
154 readonly name: string;
155 /**
156 * The mime types associated the file type.
157 */
158 readonly mimeTypes: ReadonlyArray<string>;
159 /**
160 * The extensions of the file type (e.g. `".txt"`). Can be a compound
161 * extension (e.g. `".table.json`).
162 */
163 readonly extensions: ReadonlyArray<string>;
164 /**
165 * An optional display name for the file type.
166 */
167 readonly displayName?: string;
168 /**
169 * An optional pattern for a file name (e.g. `^Dockerfile$`).
170 */
171 readonly pattern?: string;
172 /**
173 * The icon for the file type. Can either be a string containing the name
174 * of an existing icon, or an object with \{name, svgstr\} fields, where
175 * svgstr is a string containing the raw contents of an svg file.
176 */
177 readonly icon?: LabIcon.IResolvable;
178 /**
179 * The icon class name for the file type.
180 */
181 readonly iconClass?: string;
182 /**
183 * The icon label for the file type.
184 */
185 readonly iconLabel?: string;
186 /**
187 * The file format for the file type ('text', 'base64', or 'json').
188 */
189 readonly fileFormat?: string | null;
190 }
191 /**
192 * An interface for using a RenderMime.IRenderer for output and read-only documents.
193 */
194 interface IExtension {
195 /**
196 * The ID of the extension.
197 *
198 * #### Notes
199 * The convention for extension IDs in JupyterLab is the full NPM package
200 * name followed by a colon and a unique string token, e.g.
201 * `'@jupyterlab/apputils-extension:settings'` or `'foo-extension:bar'`.
202 */
203 readonly id: string;
204 /**
205 * Extension description.
206 *
207 * #### Notes
208 * This can be used to provide user documentation on the feature
209 * brought by the extension.
210 */
211 readonly description?: string;
212 /**
213 * A renderer factory to be registered to render the MIME type.
214 */
215 readonly rendererFactory: IRendererFactory;
216 /**
217 * The rank passed to `RenderMime.addFactory`. If not given,
218 * defaults to the `defaultRank` of the factory.
219 */
220 readonly rank?: number;
221 /**
222 * The timeout after user activity to re-render the data.
223 */
224 readonly renderTimeout?: number;
225 /**
226 * Preferred data type from the model. Defaults to `string`.
227 */
228 readonly dataType?: 'string' | 'json';
229 /**
230 * The options used to open a document with the renderer factory.
231 */
232 readonly documentWidgetFactoryOptions?: IDocumentWidgetFactoryOptions | ReadonlyArray<IDocumentWidgetFactoryOptions>;
233 /**
234 * The optional file type associated with the extension.
235 */
236 readonly fileTypes?: ReadonlyArray<IFileType>;
237 }
238 /**
239 * The interface for a module that exports an extension or extensions as
240 * the default value.
241 */
242 interface IExtensionModule {
243 /**
244 * The default export.
245 */
246 readonly default: IExtension | ReadonlyArray<IExtension>;
247 }
248 /**
249 * A widget which displays the contents of a mime model.
250 */
251 interface IRenderer extends Widget {
252 /**
253 * Render a mime model.
254 *
255 * @param model - The mime model to render.
256 *
257 * @returns A promise which resolves when rendering is complete.
258 *
259 * #### Notes
260 * This method may be called multiple times during the lifetime
261 * of the widget to update it if and when new data is available.
262 */
263 renderModel(model: IMimeModel): Promise<void>;
264 }
265 /**
266 * The interface for a renderer factory.
267 */
268 interface IRendererFactory {
269 /**
270 * Whether the factory is a "safe" factory.
271 *
272 * #### Notes
273 * A "safe" factory produces renderer widgets which can render
274 * untrusted model data in a usable way. *All* renderers must
275 * handle untrusted data safely, but some may simply failover
276 * with a "Run cell to view output" message. A "safe" renderer
277 * is an indication that its sanitized output will be useful.
278 */
279 readonly safe: boolean;
280 /**
281 * The mime types handled by this factory.
282 */
283 readonly mimeTypes: ReadonlyArray<string>;
284 /**
285 * The default rank of the factory. If not given, defaults to 100.
286 */
287 readonly defaultRank?: number;
288 /**
289 * Create a renderer which displays the mime data.
290 *
291 * @param options - The options used to render the data.
292 */
293 createRenderer(options: IRendererOptions): IRenderer;
294 }
295 /**
296 * The options used to create a renderer.
297 */
298 interface IRendererOptions {
299 /**
300 * The preferred mimeType to render.
301 */
302 mimeType: string;
303 /**
304 * The html sanitizer.
305 */
306 sanitizer: ISanitizer;
307 /**
308 * An optional url resolver.
309 */
310 resolver: IResolver | null;
311 /**
312 * An optional link handler.
313 */
314 linkHandler: ILinkHandler | null;
315 /**
316 * The LaTeX typesetter.
317 */
318 latexTypesetter: ILatexTypesetter | null;
319 /**
320 * The Markdown parser.
321 */
322 markdownParser?: IMarkdownParser | null;
323 /**
324 * The application language translator.
325 */
326 translator?: ITranslator;
327 }
328 /**
329 * The options used to sanitize.
330 */
331 interface ISanitizerOptions {
332 /**
333 * The allowed tags.
334 */
335 allowedTags?: string[];
336 /**
337 * The allowed attributes for a given tag.
338 */
339 allowedAttributes?: {
340 [key: string]: string[];
341 };
342 /**
343 * The allowed style values for a given tag.
344 */
345 allowedStyles?: {
346 [key: string]: {
347 [key: string]: RegExp[];
348 };
349 };
350 }
351 /**
352 * An object that handles html sanitization.
353 */
354 interface ISanitizer {
355 /**
356 * @returns Whether to replace URLs by HTML anchors.
357 */
358 getAutolink?(): boolean;
359 /**
360 * Sanitize an HTML string.
361 *
362 * @param dirty - The dirty text.
363 * @param options - The optional sanitization options.
364 *
365 * @returns The sanitized string.
366 */
367 sanitize(dirty: string, options?: ISanitizerOptions): string;
368 }
369 /**
370 * An object that handles links on a node.
371 */
372 interface ILinkHandler {
373 /**
374 * Add the link handler to the node.
375 *
376 * @param node the anchor node for which to handle the link.
377 *
378 * @param path the path to open when the link is clicked.
379 *
380 * @param id an optional element id to scroll to when the path is opened.
381 */
382 handleLink(node: HTMLElement, path: string, id?: string): void;
383 /**
384 * Add the path handler to the node.
385 *
386 * @param node the anchor node for which to handle the link.
387 *
388 * @param path the path to open when the link is clicked.
389 *
390 * @param scope the scope to which the path is bound.
391 *
392 * @param id an optional element id to scroll to when the path is opened.
393 */
394 handlePath?(node: HTMLElement, path: string, scope: 'kernel' | 'server', id?: string): void;
395 }
396 interface IResolvedLocation {
397 /**
398 * Location scope.
399 */
400 scope: 'kernel' | 'server';
401 /**
402 * Resolved path.
403 */
404 path: string;
405 }
406 /**
407 * An object that resolves relative URLs.
408 */
409 interface IResolver {
410 /**
411 * Resolve a relative url to an absolute url path.
412 */
413 resolveUrl(url: string): Promise<string>;
414 /**
415 * Get the download url for a given absolute url path.
416 *
417 * #### Notes
418 * This URL may include a query parameter.
419 */
420 getDownloadUrl(url: string): Promise<string>;
421 /**
422 * Whether the URL should be handled by the resolver
423 * or not.
424 *
425 * @param allowRoot - Whether the paths starting at Unix-style filesystem root (`/`) are permitted.
426 *
427 * #### Notes
428 * This is similar to the `isLocal` check in `URLExt`,
429 * but can also perform additional checks on whether the
430 * resolver should handle a given URL.
431 */
432 isLocal?: (url: string, allowRoot?: boolean) => boolean;
433 /**
434 * Resolve a path from Jupyter kernel to a path:
435 * - relative to `root_dir` (preferrably) this is in jupyter-server scope,
436 * - path understood and known by kernel (if such a path exists).
437 * Returns `null` if there is no file matching provided path in neither
438 * kernel nor jupyter-server contents manager.
439 */
440 resolvePath?: (path: string) => Promise<IResolvedLocation | null>;
441 }
442 /**
443 * The interface for a LaTeX typesetter.
444 */
445 interface ILatexTypesetter {
446 /**
447 * Typeset a DOM element.
448 *
449 * @param element - the DOM element to typeset. The typesetting may
450 * happen synchronously or asynchronously.
451 */
452 typeset(element: HTMLElement): void;
453 }
454 /**
455 * The interface for a Markdown parser.
456 */
457 interface IMarkdownParser {
458 /**
459 * Render a markdown source into unsanitized HTML.
460 *
461 * @param source - The string to render.
462 * @returns - A promise of the string containing HTML which may require sanitization.
463 */
464 render(source: string): Promise<string>;
465 }
466 /**
467 * Bundle of gettext-based translation functions for a specific domain.
468 */
469 type TranslationBundle = {
470 /**
471 * Alias for `gettext` (translate strings without number inflection)
472 * @param msgid message (text to translate)
473 * @param args
474 *
475 * @returns A translated string if found, or the original string.
476 */
477 __(msgid: string, ...args: any[]): string;
478 /**
479 * Alias for `ngettext` (translate accounting for plural forms)
480 * @param msgid message for singular
481 * @param msgid_plural message for plural
482 * @param n determines which plural form to use
483 * @param args
484 *
485 * @returns A translated string if found, or the original string.
486 */
487 _n(msgid: string, msgid_plural: string, n: number, ...args: any[]): string;
488 /**
489 * Alias for `pgettext` (translate in given context)
490 * @param msgctxt context
491 * @param msgid message (text to translate)
492 * @param args
493 *
494 * @returns A translated string if found, or the original string.
495 */
496 _p(msgctxt: string, msgid: string, ...args: any[]): string;
497 /**
498 * Alias for `npgettext` (translate accounting for plural forms in given context)
499 * @param msgctxt context
500 * @param msgid message for singular
501 * @param msgid_plural message for plural
502 * @param n number used to determine which plural form to use
503 * @param args
504 *
505 * @returns A translated string if found, or the original string.
506 */
507 _np(msgctxt: string, msgid: string, msgid_plural: string, n: number, ...args: any[]): string;
508 /**
509 * Look up the message id in the catalog and return the corresponding message string.
510 * Otherwise, the message id is returned.
511 *
512 * @param msgid message (text to translate)
513 * @param args
514 *
515 * @returns A translated string if found, or the original string.
516 */
517 gettext(msgid: string, ...args: any[]): string;
518 /**
519 * Do a plural-forms lookup of a message id. msgid is used as the message id for
520 * purposes of lookup in the catalog, while n is used to determine which plural form
521 * to use. Otherwise, when n is 1 msgid is returned, and msgid_plural is returned in
522 * all other cases.
523 *
524 * @param msgid message for singular
525 * @param msgid_plural message for plural
526 * @param n determines which plural form to use
527 * @param args
528 *
529 * @returns A translated string if found, or the original string.
530 */
531 ngettext(msgid: string, msgid_plural: string, n: number, ...args: any[]): string;
532 /**
533 * Look up the context and message id in the catalog and return the corresponding
534 * message string. Otherwise, the message id is returned.
535 *
536 * @param msgctxt context
537 * @param msgid message (text to translate)
538 * @param args
539 *
540 * @returns A translated string if found, or the original string.
541 */
542 pgettext(msgctxt: string, msgid: string, ...args: any[]): string;
543 /**
544 * Do a plural-forms lookup of a message id. msgid is used as the message id for
545 * purposes of lookup in the catalog, while n is used to determine which plural
546 * form to use. Otherwise, when n is 1 msgid is returned, and msgid_plural is
547 * returned in all other cases.
548 *
549 * @param msgctxt context
550 * @param msgid message for singular
551 * @param msgid_plural message for plural
552 * @param n number used to determine which plural form to use
553 * @param args
554 *
555 * @returns A translated string if found, or the original string.
556 */
557 npgettext(msgctxt: string, msgid: string, msgid_plural: string, n: number, ...args: any[]): string;
558 /**
559 * Do a plural-forms lookup of a message id. msgid is used as the message id for
560 * purposes of lookup in the catalog, while n is used to determine which plural
561 * form to use. Otherwise, when n is 1 msgid is returned, and msgid_plural is
562 * returned in all other cases.
563 *
564 * @param domain - The translations domain.
565 * @param msgctxt - The message context.
566 * @param msgid - The singular string to translate.
567 * @param msgid_plural - The plural string to translate.
568 * @param n - The number for pluralization.
569 * @param args - Any additional values to use with interpolation
570 *
571 * @returns A translated string if found, or the original string.
572 */
573 dcnpgettext(domain: string, msgctxt: string, msgid: string, msgid_plural: string, n: number, ...args: any[]): string;
574 };
575 /**
576 * Translation provider interface
577 */
578 interface ITranslator {
579 /**
580 * The code of the language in use.
581 */
582 readonly languageCode: string;
583 /**
584 * Load translation bundles for a given domain.
585 *
586 * @param domain The translation domain to use for translations.
587 *
588 * @returns The translation bundle if found, or the English bundle.
589 */
590 load(domain: string): TranslationBundle;
591 }
592}