index.ts 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. // Copyright (c) Jupyter Development Team.
  2. // Distributed under the terms of the Modified BSD License.
  3. import {
  4. JSONValue
  5. } from '@phosphor/coreutils';
  6. import {
  7. Widget
  8. } from '@phosphor/widgets';
  9. /**
  10. * A namespace for rendermime associated interfaces.
  11. */
  12. export
  13. namespace IRenderMime {
  14. /**
  15. * An observable model for mime data.
  16. */
  17. export
  18. interface IMimeModel {
  19. /**
  20. * The data associated with the model.
  21. */
  22. readonly data: IBundle;
  23. /**
  24. * The metadata associated with the model.
  25. */
  26. readonly metadata: IBundle;
  27. }
  28. /**
  29. * A bundle for mime data.
  30. */
  31. export
  32. interface IBundle {
  33. /**
  34. * Get a value for a given key.
  35. *
  36. * @param key - the key.
  37. *
  38. * @returns the value for that key.
  39. */
  40. get(key: string): JSONValue;
  41. /**
  42. * Set a key-value pair in the bundle.
  43. *
  44. * @param key - The key to set.
  45. *
  46. * @param value - The value for the key.
  47. *
  48. * @returns the old value for the key, or undefined
  49. * if that did not exist.
  50. */
  51. set(key: string, value: JSONValue): JSONValue;
  52. /**
  53. * Check whether the bundle has a key.
  54. *
  55. * @param key - the key to check.
  56. *
  57. * @returns `true` if the bundle has the key, `false` otherwise.
  58. */
  59. has(key: string): boolean;
  60. /**
  61. * Get a list of the keys in the bundle.
  62. *
  63. * @returns - a list of keys.
  64. */
  65. keys(): string[];
  66. /**
  67. * Remove a key from the bundle.
  68. *
  69. * @param key - the key to remove.
  70. *
  71. * @returns the value of the given key,
  72. * or undefined if that does not exist.
  73. */
  74. delete(key: string): JSONValue;
  75. }
  76. /**
  77. * The options used to initialize a document widget factory.
  78. */
  79. export
  80. interface IDocumentWidgetFactoryOptions {
  81. /**
  82. * The file extensions the widget can view.
  83. *
  84. * #### Notes
  85. * Use "*" to denote all files. Specific file extensions must be preceded
  86. * with '.', like '.png', '.txt', etc. They may themselves contain a
  87. * period (e.g. .table.json).
  88. */
  89. readonly fileExtensions: string[];
  90. /**
  91. * The name of the widget to display in dialogs.
  92. */
  93. readonly name: string;
  94. /**
  95. * The file extensions for which the factory should be the default.
  96. *
  97. * #### Notes
  98. * Use "*" to denote all files. Specific file extensions must be preceded
  99. * with '.', like '.png', '.txt', etc. Entries in this attribute must also
  100. * be included in the fileExtensions attribute.
  101. * The default is an empty array.
  102. *
  103. * **See also:** [[fileExtensions]].
  104. */
  105. readonly defaultFor?: string[];
  106. /**
  107. * Whether the widget factory is read only.
  108. */
  109. readonly readOnly?: boolean;
  110. /**
  111. * The registered name of the model type used to create the widgets.
  112. */
  113. readonly modelName?: string;
  114. /**
  115. * Whether the widgets prefer having a kernel started.
  116. */
  117. readonly preferKernel?: boolean;
  118. /**
  119. * Whether the widgets can start a kernel when opened.
  120. */
  121. readonly canStartKernel?: boolean;
  122. }
  123. /**
  124. * An interface for using a RenderMime.IRenderer for output and read-only documents.
  125. */
  126. export
  127. interface IExtension {
  128. /**
  129. * The MIME type for the renderer, which is the output MIME type it will handle.
  130. */
  131. mimeType: string;
  132. /**
  133. * A renderer factory to be registered to render the MIME type.
  134. */
  135. rendererFactory: IRendererFactory;
  136. /**
  137. * The index passed to `RenderMime.addRenderer`.
  138. */
  139. rendererIndex?: number;
  140. /**
  141. * The timeout after user activity to re-render the data.
  142. */
  143. renderTimeout?: number;
  144. /**
  145. * Preferred data type from the model. Defaults to `string`.
  146. */
  147. dataType?: 'string' | 'json';
  148. /**
  149. * The icon class name for the widget.
  150. */
  151. iconClass?: string;
  152. /**
  153. * The icon label for the widget.
  154. */
  155. iconLabel?: string;
  156. /**
  157. * The options used for using the renderer for documents.
  158. */
  159. documentWidgetFactoryOptions?: IDocumentWidgetFactoryOptions;
  160. }
  161. /**
  162. * The interface for a module that exports an extension or extensions as
  163. * the default value.
  164. */
  165. export
  166. interface IExtensionModule {
  167. /**
  168. * The default export.
  169. */
  170. default: IExtension | IExtension[];
  171. }
  172. /**
  173. * A widget that provides a ready promise.
  174. */
  175. export
  176. interface IRendererWidget extends Widget {
  177. /**
  178. * Render a mime model.
  179. */
  180. render(model: IMimeModel): Promise<void>;
  181. }
  182. /**
  183. * The interface for a renderer factory.
  184. */
  185. export
  186. interface IRendererFactory {
  187. /**
  188. * The mimeTypes this renderer accepts.
  189. */
  190. readonly mimeTypes: string[];
  191. /**
  192. * Whether the renderer can render given the render options.
  193. *
  194. * @param options - The options that would be used to render the data.
  195. */
  196. canCreateRenderer(options: IRendererOptions): boolean;
  197. /**
  198. * Create a renderer the transformed mime data.
  199. *
  200. * @param options - The options used to render the data.
  201. */
  202. createRenderer(options: IRendererOptions): IRendererWidget;
  203. /**
  204. * Whether the renderer will sanitize the data given the render options.
  205. *
  206. * @param options - The options that would be used to render the data.
  207. */
  208. wouldSanitize(options: IRendererOptions): boolean;
  209. }
  210. /**
  211. * The options used to create a renderer.
  212. */
  213. export
  214. interface IRendererOptions {
  215. /**
  216. * The preferred mimeType to render.
  217. */
  218. mimeType: string;
  219. /**
  220. * Whether the data is trusted.
  221. */
  222. trusted: boolean;
  223. /**
  224. * The html sanitizer.
  225. */
  226. sanitizer: ISanitizer;
  227. /**
  228. * An optional url resolver.
  229. */
  230. resolver?: IResolver;
  231. /**
  232. * An optional link handler.
  233. */
  234. linkHandler?: ILinkHandler;
  235. }
  236. /**
  237. * An object that handles html sanitization.
  238. */
  239. export
  240. interface ISanitizer {
  241. /**
  242. * Sanitize an HTML string.
  243. */
  244. sanitize(dirty: string): string;
  245. }
  246. /**
  247. * An object that handles links on a node.
  248. */
  249. export
  250. interface ILinkHandler {
  251. /**
  252. * Add the link handler to the node.
  253. */
  254. handleLink(node: HTMLElement, url: string): void;
  255. }
  256. /**
  257. * An object that resolves relative URLs.
  258. */
  259. export
  260. interface IResolver {
  261. /**
  262. * Resolve a relative url to a correct server path.
  263. */
  264. resolveUrl(url: string): Promise<string>;
  265. /**
  266. * Get the download url of a given absolute server path.
  267. */
  268. getDownloadUrl(path: string): Promise<string>;
  269. }
  270. }