index.ts 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. // Copyright (c) Jupyter Development Team.
  2. // Distributed under the terms of the Modified BSD License.
  3. import {
  4. CommandLinker
  5. } from '@jupyterlab/apputils';
  6. import {
  7. Base64ModelFactory, DocumentRegistry, TextModelFactory
  8. } from '@jupyterlab/docregistry';
  9. import {
  10. IRenderMime, RenderMime
  11. } from '@jupyterlab/rendermime';
  12. import {
  13. Application, IPlugin
  14. } from '@phosphor/application';
  15. import {
  16. createRendermimePlugins
  17. } from './mimerenderers';
  18. import {
  19. ApplicationShell
  20. } from './shell';
  21. export { ApplicationShell } from './shell';
  22. export { ILayoutRestorer, LayoutRestorer } from './layoutrestorer';
  23. import '../style/index.css';
  24. /**
  25. * The type for all JupyterLab plugins.
  26. */
  27. export
  28. type JupyterLabPlugin<T> = IPlugin<JupyterLab, T>;
  29. /**
  30. * JupyterLab is the main application class. It is instantiated once and shared.
  31. */
  32. export
  33. class JupyterLab extends Application<ApplicationShell> {
  34. /**
  35. * Construct a new JupyterLab object.
  36. */
  37. constructor(options: JupyterLab.IOptions = {}) {
  38. super({ shell: new ApplicationShell() });
  39. this._info = {
  40. name: options.name || 'JupyterLab',
  41. namespace: options.namespace || 'jupyterlab',
  42. version: options.version || 'unknown',
  43. devMode: options.devMode || false,
  44. settingsDir: options.settingsDir || '',
  45. assetsDir: options.assetsDir || ''
  46. };
  47. if (options.devMode) {
  48. this.shell.addClass('jp-mod-devMode');
  49. }
  50. let linker = new CommandLinker({ commands: this.commands });
  51. this.commandLinker = linker;
  52. let linkHandler = {
  53. handleLink: (node: HTMLElement, path: string) => {
  54. linker.connectNode(node, 'file-operations:open', { path });
  55. }
  56. };
  57. this.rendermime = new RenderMime({ linkHandler });
  58. RenderMime.addDefaultFactories(this.rendermime);
  59. let registry = this.docRegistry = new DocumentRegistry();
  60. registry.addModelFactory(new TextModelFactory());
  61. registry.addModelFactory(new Base64ModelFactory());
  62. registry.addFileType({
  63. name: 'Text',
  64. extension: '.txt',
  65. contentType: 'file',
  66. fileFormat: 'text'
  67. });
  68. registry.addCreator({ name: 'Text File', fileType: 'Text', });
  69. if (options.mimeExtensions) {
  70. let plugins = createRendermimePlugins(options.mimeExtensions);
  71. plugins.forEach(plugin => { this.registerPlugin(plugin); });
  72. }
  73. }
  74. /**
  75. * The document registry instance used by the application.
  76. */
  77. readonly docRegistry: DocumentRegistry;
  78. /**
  79. * The rendermime instance used by the application.
  80. */
  81. readonly rendermime: RenderMime;
  82. /**
  83. * The command linker used by the application.
  84. */
  85. readonly commandLinker: CommandLinker;
  86. /**
  87. * The information about the application.
  88. */
  89. get info(): JupyterLab.IInfo {
  90. return this._info;
  91. }
  92. /**
  93. * Promise that resolves when state is first restored, returning layout description.
  94. *
  95. * #### Notes
  96. * This is just a reference to `shell.restored`.
  97. */
  98. get restored(): Promise<ApplicationShell.ILayout> {
  99. return this.shell.restored;
  100. }
  101. /**
  102. * Register plugins from a plugin module.
  103. *
  104. * @param mod - The plugin module to register.
  105. */
  106. registerPluginModule(mod: JupyterLab.IPluginModule): void {
  107. let data = mod.default;
  108. // Handle commonjs exports.
  109. if (!mod.hasOwnProperty('__esModule')) {
  110. data = mod as any;
  111. }
  112. if (!Array.isArray(data)) {
  113. data = [data];
  114. }
  115. data.forEach(item => { this.registerPlugin(item); });
  116. }
  117. /**
  118. * Register the plugins from multiple plugin modules.
  119. *
  120. * @param mods - The plugin modules to register.
  121. */
  122. registerPluginModules(mods: JupyterLab.IPluginModule[]): void {
  123. mods.forEach(mod => { this.registerPluginModule(mod); });
  124. }
  125. private _info: JupyterLab.IInfo;
  126. }
  127. /**
  128. * The namespace for `JupyterLab` class statics.
  129. */
  130. export
  131. namespace JupyterLab {
  132. /**
  133. * The options used to initialize a JupyterLab object.
  134. */
  135. export
  136. interface IOptions {
  137. /**
  138. * The name of the JupyterLab application.
  139. */
  140. name?: string;
  141. /**
  142. * The namespace/prefix plugins may use to denote their origin.
  143. *
  144. * #### Notes
  145. * This field may be used by persistent storage mechanisms such as state
  146. * databases, cookies, session storage, etc.
  147. *
  148. * If unspecified, the default value is `'jupyterlab'`.
  149. */
  150. namespace?: string;
  151. /**
  152. * The version of the JupyterLab application.
  153. */
  154. version?: string;
  155. /**
  156. * Whether the application is in dev mode.
  157. */
  158. devMode?: boolean;
  159. /**
  160. * The settings directory of the app on the server.
  161. */
  162. settingsDir?: string;
  163. /**
  164. * The assets directory of the app on the server.
  165. */
  166. assetsDir?: string;
  167. /**
  168. * The mime renderer extensions.
  169. */
  170. mimeExtensions?: IRenderMime.IExtensionModule[];
  171. }
  172. /**
  173. * The information about a JupyterLab application.
  174. */
  175. export
  176. interface IInfo {
  177. /**
  178. * The name of the JupyterLab application.
  179. */
  180. readonly name: string;
  181. /**
  182. * The namespace/prefix plugins may use to denote their origin.
  183. */
  184. readonly namespace: string;
  185. /**
  186. * The version of the JupyterLab application.
  187. */
  188. readonly version: string;
  189. /**
  190. * Whether the application is in dev mode.
  191. */
  192. readonly devMode: boolean;
  193. /**
  194. * The settings directory of the app on the server.
  195. */
  196. readonly settingsDir: string;
  197. /**
  198. * The assets directory of the app on the server.
  199. */
  200. readonly assetsDir: string;
  201. }
  202. /**
  203. * The interface for a module that exports a plugin or plugins as
  204. * the default value.
  205. */
  206. export
  207. interface IPluginModule {
  208. /**
  209. * The default export.
  210. */
  211. default: JupyterLabPlugin<any> | JupyterLabPlugin<any>[];
  212. }
  213. }