index.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. /*-----------------------------------------------------------------------------
  2. | Copyright (c) Jupyter Development Team.
  3. | Distributed under the terms of the Modified BSD License.
  4. |----------------------------------------------------------------------------*/
  5. import { ReadonlyPartialJSONObject } from '@lumino/coreutils';
  6. import { Widget } from '@lumino/widgets';
  7. /**
  8. * A namespace for rendermime associated interfaces.
  9. */
  10. export namespace IRenderMime {
  11. /**
  12. * A model for mime data.
  13. */
  14. export 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. export namespace IMimeModel {
  44. /**
  45. * The options used to update a mime model.
  46. */
  47. export 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. export interface IToolbarItem {
  62. name: string;
  63. widget: Widget;
  64. }
  65. /**
  66. * The options used to initialize a document widget factory.
  67. *
  68. * This interface is intended to be used by mime renderer extensions
  69. * to define a document opener that uses its renderer factory.
  70. */
  71. export interface IDocumentWidgetFactoryOptions {
  72. /**
  73. * The name of the widget to display in dialogs.
  74. */
  75. readonly name: string;
  76. /**
  77. * The name of the document model type.
  78. */
  79. readonly modelName?: string;
  80. /**
  81. * The primary file type of the widget.
  82. */
  83. readonly primaryFileType: string;
  84. /**
  85. * The file types the widget can view.
  86. */
  87. readonly fileTypes: ReadonlyArray<string>;
  88. /**
  89. * The file types for which the factory should be the default.
  90. */
  91. readonly defaultFor?: ReadonlyArray<string>;
  92. /**
  93. * The file types for which the factory should be the default for rendering,
  94. * if that is different than the default factory (which may be for editing)
  95. * If undefined, then it will fall back on the default file type.
  96. */
  97. readonly defaultRendered?: ReadonlyArray<string>;
  98. /**
  99. * A function returning a list of toolbar items to add to the toolbar.
  100. */
  101. readonly toolbarFactory?: (widget?: IRenderer) => IToolbarItem[];
  102. }
  103. export namespace LabIcon {
  104. /**
  105. * The simplest possible interface for defining a generic icon.
  106. */
  107. export interface IIcon {
  108. /**
  109. * The name of the icon. By convention, the icon name will be namespaced
  110. * as so:
  111. *
  112. * "pkg-name:icon-name"
  113. */
  114. readonly name: string;
  115. /**
  116. * A string containing the raw contents of an svg file.
  117. */
  118. svgstr: string;
  119. }
  120. /**
  121. * Interface for generic renderer.
  122. */
  123. export interface IRenderer {
  124. readonly render: (container: HTMLElement, options?: any) => void;
  125. // TODO: make unrenderer optional once @lumino/virtualdom > 1.4.1 is used
  126. readonly unrender: (container: HTMLElement) => void;
  127. }
  128. /**
  129. * A type that can be resolved to a LabIcon instance.
  130. */
  131. export type IResolvable = string | (IIcon & Partial<IRenderer>);
  132. }
  133. /**
  134. * A file type to associate with the renderer.
  135. */
  136. export interface IFileType {
  137. /**
  138. * The name of the file type.
  139. */
  140. readonly name: string;
  141. /**
  142. * The mime types associated the file type.
  143. */
  144. readonly mimeTypes: ReadonlyArray<string>;
  145. /**
  146. * The extensions of the file type (e.g. `".txt"`). Can be a compound
  147. * extension (e.g. `".table.json`).
  148. */
  149. readonly extensions: ReadonlyArray<string>;
  150. /**
  151. * An optional display name for the file type.
  152. */
  153. readonly displayName?: string;
  154. /**
  155. * An optional pattern for a file name (e.g. `^Dockerfile$`).
  156. */
  157. readonly pattern?: string;
  158. /**
  159. * The icon for the file type. Can either be a string containing the name
  160. * of an existing icon, or an object with {name, svgstr} fields, where
  161. * svgstr is a string containing the raw contents of an svg file.
  162. */
  163. readonly icon?: LabIcon.IResolvable;
  164. /**
  165. * The icon class name for the file type.
  166. */
  167. readonly iconClass?: string;
  168. /**
  169. * The icon label for the file type.
  170. */
  171. readonly iconLabel?: string;
  172. /**
  173. * The file format for the file type ('text', 'base64', or 'json').
  174. */
  175. readonly fileFormat?: string;
  176. }
  177. /**
  178. * An interface for using a RenderMime.IRenderer for output and read-only documents.
  179. */
  180. export interface IExtension {
  181. /**
  182. * The ID of the extension.
  183. *
  184. * #### Notes
  185. * The convention for extension IDs in JupyterLab is the full NPM package
  186. * name followed by a colon and a unique string token, e.g.
  187. * `'@jupyterlab/apputils-extension:settings'` or `'foo-extension:bar'`.
  188. */
  189. readonly id: string;
  190. /**
  191. * A renderer factory to be registered to render the MIME type.
  192. */
  193. readonly rendererFactory: IRendererFactory;
  194. /**
  195. * The rank passed to `RenderMime.addFactory`. If not given,
  196. * defaults to the `defaultRank` of the factory.
  197. */
  198. readonly rank?: number;
  199. /**
  200. * The timeout after user activity to re-render the data.
  201. */
  202. readonly renderTimeout?: number;
  203. /**
  204. * Preferred data type from the model. Defaults to `string`.
  205. */
  206. readonly dataType?: 'string' | 'json';
  207. /**
  208. * The options used to open a document with the renderer factory.
  209. */
  210. readonly documentWidgetFactoryOptions?:
  211. | IDocumentWidgetFactoryOptions
  212. | ReadonlyArray<IDocumentWidgetFactoryOptions>;
  213. /**
  214. * The optional file type associated with the extension.
  215. */
  216. readonly fileTypes?: ReadonlyArray<IFileType>;
  217. }
  218. /**
  219. * The interface for a module that exports an extension or extensions as
  220. * the default value.
  221. */
  222. export interface IExtensionModule {
  223. /**
  224. * The default export.
  225. */
  226. readonly default: IExtension | ReadonlyArray<IExtension>;
  227. }
  228. /**
  229. * A widget which displays the contents of a mime model.
  230. */
  231. export interface IRenderer extends Widget {
  232. /**
  233. * Render a mime model.
  234. *
  235. * @param model - The mime model to render.
  236. *
  237. * @returns A promise which resolves when rendering is complete.
  238. *
  239. * #### Notes
  240. * This method may be called multiple times during the lifetime
  241. * of the widget to update it if and when new data is available.
  242. */
  243. renderModel(model: IMimeModel): Promise<void>;
  244. }
  245. /**
  246. * The interface for a renderer factory.
  247. */
  248. export interface IRendererFactory {
  249. /**
  250. * Whether the factory is a "safe" factory.
  251. *
  252. * #### Notes
  253. * A "safe" factory produces renderer widgets which can render
  254. * untrusted model data in a usable way. *All* renderers must
  255. * handle untrusted data safely, but some may simply failover
  256. * with a "Run cell to view output" message. A "safe" renderer
  257. * is an indication that its sanitized output will be useful.
  258. */
  259. readonly safe: boolean;
  260. /**
  261. * The mime types handled by this factory.
  262. */
  263. readonly mimeTypes: ReadonlyArray<string>;
  264. /**
  265. * The default rank of the factory. If not given, defaults to 100.
  266. */
  267. readonly defaultRank?: number;
  268. /**
  269. * Create a renderer which displays the mime data.
  270. *
  271. * @param options - The options used to render the data.
  272. */
  273. createRenderer(options: IRendererOptions): IRenderer;
  274. }
  275. /**
  276. * The options used to create a renderer.
  277. */
  278. export interface IRendererOptions {
  279. /**
  280. * The preferred mimeType to render.
  281. */
  282. mimeType: string;
  283. /**
  284. * The html sanitizer.
  285. */
  286. sanitizer: ISanitizer;
  287. /**
  288. * An optional url resolver.
  289. */
  290. resolver: IResolver | null;
  291. /**
  292. * An optional link handler.
  293. */
  294. linkHandler: ILinkHandler | null;
  295. /**
  296. * The LaTeX typesetter.
  297. */
  298. latexTypesetter: ILatexTypesetter | null;
  299. }
  300. /**
  301. * An object that handles html sanitization.
  302. */
  303. export interface ISanitizer {
  304. /**
  305. * Sanitize an HTML string.
  306. */
  307. sanitize(dirty: string): string;
  308. }
  309. /**
  310. * An object that handles links on a node.
  311. */
  312. export interface ILinkHandler {
  313. /**
  314. * Add the link handler to the node.
  315. *
  316. * @param node: the anchor node for which to handle the link.
  317. *
  318. * @param path: the path to open when the link is clicked.
  319. *
  320. * @param id: an optional element id to scroll to when the path is opened.
  321. */
  322. handleLink(node: HTMLElement, path: string, id?: string): void;
  323. }
  324. /**
  325. * An object that resolves relative URLs.
  326. */
  327. export interface IResolver {
  328. /**
  329. * Resolve a relative url to an absolute url path.
  330. */
  331. resolveUrl(url: string): Promise<string>;
  332. /**
  333. * Get the download url for a given absolute url path.
  334. *
  335. * #### Notes
  336. * This URL may include a query parameter.
  337. */
  338. getDownloadUrl(url: string): Promise<string>;
  339. /**
  340. * Whether the URL should be handled by the resolver
  341. * or not.
  342. *
  343. * #### Notes
  344. * This is similar to the `isLocal` check in `URLExt`,
  345. * but can also perform additional checks on whether the
  346. * resolver should handle a given URL.
  347. */
  348. isLocal?: (url: string) => boolean;
  349. }
  350. /**
  351. * The interface for a LaTeX typesetter.
  352. */
  353. export interface ILatexTypesetter {
  354. /**
  355. * Typeset a DOM element.
  356. *
  357. * @param element - the DOM element to typeset. The typesetting may
  358. * happen synchronously or asynchronously.
  359. *
  360. * #### Notes
  361. * The application-wide rendermime object has a settable
  362. * `latexTypesetter` property which is used wherever LaTeX
  363. * typesetting is required. Extensions wishing to provide their
  364. * own typesetter may replace that on the global `lab.rendermime`.
  365. */
  366. typeset(element: HTMLElement): void;
  367. }
  368. }