UNPKG

18.8 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 /**
385 * An object that resolves relative URLs.
386 */
387 interface IResolver {
388 /**
389 * Resolve a relative url to an absolute url path.
390 */
391 resolveUrl(url: string): Promise<string>;
392 /**
393 * Get the download url for a given absolute url path.
394 *
395 * #### Notes
396 * This URL may include a query parameter.
397 */
398 getDownloadUrl(url: string): Promise<string>;
399 /**
400 * Whether the URL should be handled by the resolver
401 * or not.
402 *
403 * #### Notes
404 * This is similar to the `isLocal` check in `URLExt`,
405 * but can also perform additional checks on whether the
406 * resolver should handle a given URL.
407 */
408 isLocal?: (url: string) => boolean;
409 }
410 /**
411 * The interface for a LaTeX typesetter.
412 */
413 interface ILatexTypesetter {
414 /**
415 * Typeset a DOM element.
416 *
417 * @param element - the DOM element to typeset. The typesetting may
418 * happen synchronously or asynchronously.
419 */
420 typeset(element: HTMLElement): void;
421 }
422 /**
423 * The interface for a Markdown parser.
424 */
425 interface IMarkdownParser {
426 /**
427 * Render a markdown source.
428 *
429 * @param source - The string to render.
430 * @returns - A promise of the string.
431 */
432 render(source: string): Promise<string>;
433 }
434 /**
435 * Bundle of gettext-based translation functions for a specific domain.
436 */
437 type TranslationBundle = {
438 /**
439 * Alias for `gettext` (translate strings without number inflection)
440 * @param msgid message (text to translate)
441 * @param args
442 *
443 * @returns A translated string if found, or the original string.
444 */
445 __(msgid: string, ...args: any[]): string;
446 /**
447 * Alias for `ngettext` (translate accounting for plural forms)
448 * @param msgid message for singular
449 * @param msgid_plural message for plural
450 * @param n determines which plural form to use
451 * @param args
452 *
453 * @returns A translated string if found, or the original string.
454 */
455 _n(msgid: string, msgid_plural: string, n: number, ...args: any[]): string;
456 /**
457 * Alias for `pgettext` (translate in given context)
458 * @param msgctxt context
459 * @param msgid message (text to translate)
460 * @param args
461 *
462 * @returns A translated string if found, or the original string.
463 */
464 _p(msgctxt: string, msgid: string, ...args: any[]): string;
465 /**
466 * Alias for `npgettext` (translate accounting for plural forms in given context)
467 * @param msgctxt context
468 * @param msgid message for singular
469 * @param msgid_plural message for plural
470 * @param n number used to determine which plural form to use
471 * @param args
472 *
473 * @returns A translated string if found, or the original string.
474 */
475 _np(msgctxt: string, msgid: string, msgid_plural: string, n: number, ...args: any[]): string;
476 /**
477 * Look up the message id in the catalog and return the corresponding message string.
478 * Otherwise, the message id is returned.
479 *
480 * @param msgid message (text to translate)
481 * @param args
482 *
483 * @returns A translated string if found, or the original string.
484 */
485 gettext(msgid: string, ...args: any[]): string;
486 /**
487 * Do a plural-forms lookup of a message id. msgid is used as the message id for
488 * purposes of lookup in the catalog, while n is used to determine which plural form
489 * to use. Otherwise, when n is 1 msgid is returned, and msgid_plural is returned in
490 * all other cases.
491 *
492 * @param msgid message for singular
493 * @param msgid_plural message for plural
494 * @param n determines which plural form to use
495 * @param args
496 *
497 * @returns A translated string if found, or the original string.
498 */
499 ngettext(msgid: string, msgid_plural: string, n: number, ...args: any[]): string;
500 /**
501 * Look up the context and message id in the catalog and return the corresponding
502 * message string. Otherwise, the message id is returned.
503 *
504 * @param msgctxt context
505 * @param msgid message (text to translate)
506 * @param args
507 *
508 * @returns A translated string if found, or the original string.
509 */
510 pgettext(msgctxt: string, msgid: string, ...args: any[]): string;
511 /**
512 * Do a plural-forms lookup of a message id. msgid is used as the message id for
513 * purposes of lookup in the catalog, while n is used to determine which plural
514 * form to use. Otherwise, when n is 1 msgid is returned, and msgid_plural is
515 * returned in all other cases.
516 *
517 * @param msgctxt context
518 * @param msgid message for singular
519 * @param msgid_plural message for plural
520 * @param n number used to determine which plural form to use
521 * @param args
522 *
523 * @returns A translated string if found, or the original string.
524 */
525 npgettext(msgctxt: string, msgid: string, msgid_plural: string, n: number, ...args: any[]): string;
526 /**
527 * Do a plural-forms lookup of a message id. msgid is used as the message id for
528 * purposes of lookup in the catalog, while n is used to determine which plural
529 * form to use. Otherwise, when n is 1 msgid is returned, and msgid_plural is
530 * returned in all other cases.
531 *
532 * @param domain - The translations domain.
533 * @param msgctxt - The message context.
534 * @param msgid - The singular string to translate.
535 * @param msgid_plural - The plural string to translate.
536 * @param n - The number for pluralization.
537 * @param args - Any additional values to use with interpolation
538 *
539 * @returns A translated string if found, or the original string.
540 */
541 dcnpgettext(domain: string, msgctxt: string, msgid: string, msgid_plural: string, n: number, ...args: any[]): string;
542 };
543 /**
544 * Translation provider interface
545 */
546 interface ITranslator {
547 /**
548 * The code of the language in use.
549 */
550 readonly languageCode: string;
551 /**
552 * Load translation bundles for a given domain.
553 *
554 * @param domain The translation domain to use for translations.
555 *
556 * @returns The translation bundle if found, or the English bundle.
557 */
558 load(domain: string): TranslationBundle;
559 }
560}