interfaces.ts 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. // Copyright (c) Jupyter Development Team.
  2. // Distributed under the terms of the Modified BSD License.
  3. import {
  4. Contents, IKernel, Session, Kernel
  5. } from 'jupyter-js-services';
  6. import {
  7. IDisposable
  8. } from 'phosphor/lib/core/disposable';
  9. import {
  10. ISignal
  11. } from 'phosphor/lib/core/signaling';
  12. import {
  13. Widget
  14. } from 'phosphor/lib/ui/widget';
  15. import {
  16. IChangedArgs
  17. } from '../common/interfaces';
  18. /**
  19. * The interface for a document model.
  20. */
  21. export
  22. interface IDocumentModel extends IDisposable {
  23. /**
  24. * A signal emitted when the document content changes.
  25. */
  26. contentChanged: ISignal<IDocumentModel, void>;
  27. /**
  28. * A signal emitted when the model state changes.
  29. */
  30. stateChanged: ISignal<IDocumentModel, IChangedArgs<any>>;
  31. /**
  32. * The dirty state of the model.
  33. *
  34. * #### Notes
  35. * This should be cleared when the document is loaded from
  36. * or saved to disk.
  37. */
  38. dirty: boolean;
  39. /**
  40. * The read-only state of the model.
  41. */
  42. readOnly: boolean;
  43. /**
  44. * The default kernel name of the document.
  45. *
  46. * #### Notes
  47. * This is a read-only property.
  48. */
  49. defaultKernelName: string;
  50. /**
  51. * The default kernel language of the document.
  52. *
  53. * #### Notes
  54. * This is a read-only property.
  55. */
  56. defaultKernelLanguage: string;
  57. /**
  58. * Serialize the model to a string.
  59. */
  60. toString(): string;
  61. /**
  62. * Deserialize the model from a string.
  63. *
  64. * #### Notes
  65. * Should emit a [contentChanged] signal.
  66. */
  67. fromString(value: string): void;
  68. /**
  69. * Serialize the model to JSON.
  70. */
  71. toJSON(): any;
  72. /**
  73. * Deserialize the model from JSON.
  74. *
  75. * #### Notes
  76. * Should emit a [contentChanged] signal.
  77. */
  78. fromJSON(value: any): void;
  79. }
  80. /**
  81. * The document context object.
  82. */
  83. export
  84. interface IDocumentContext<T extends IDocumentModel> extends IDisposable {
  85. /**
  86. * A signal emitted when the kernel changes.
  87. */
  88. kernelChanged: ISignal<IDocumentContext<T>, IKernel>;
  89. /**
  90. * A signal emitted when the path changes.
  91. */
  92. pathChanged: ISignal<IDocumentContext<T>, string>;
  93. /**
  94. * A signal emitted when the contentsModel changes.
  95. */
  96. contentsModelChanged: ISignal<IDocumentContext<T>, Contents.IModel>;
  97. /**
  98. * A signal emitted when the context is fully populated for the first time.
  99. */
  100. populated: ISignal<IDocumentContext<T>, void>;
  101. /**
  102. * A signal emitted when the context is disposed.
  103. */
  104. disposed: ISignal<IDocumentContext<T>, void>;
  105. /**
  106. * Get the model associated with the document.
  107. *
  108. * #### Notes
  109. * This is a read-only property
  110. */
  111. model: T;
  112. /**
  113. * The current kernel associated with the document.
  114. *
  115. * #### Notes
  116. * This is a read-only propery.
  117. */
  118. kernel: IKernel;
  119. /**
  120. * The current path associated with the document.
  121. *
  122. * #### Notes
  123. * This is a read-only property.
  124. */
  125. path: string;
  126. /**
  127. * The current contents model associated with the document
  128. *
  129. * #### Notes
  130. * This is a read-only property. The model will have an
  131. * empty `contents` field. It will be `null` until the
  132. * first save or load to disk.
  133. */
  134. contentsModel: Contents.IModel;
  135. /**
  136. * Get the kernel spec information.
  137. *
  138. * #### Notes
  139. * This is a read-only property.
  140. */
  141. kernelspecs: Kernel.ISpecModels;
  142. /**
  143. * Test whether the context is fully populated.
  144. *
  145. * #### Notes
  146. * This is a read-only property.
  147. */
  148. isPopulated: boolean;
  149. /**
  150. * Change the current kernel associated with the document.
  151. *
  152. * #### Notes
  153. * If no options are given, the session is shut down.
  154. */
  155. changeKernel(options?: Kernel.IModel): Promise<IKernel>;
  156. /**
  157. * Save the document contents to disk.
  158. */
  159. save(): Promise<void>;
  160. /**
  161. * Save the document to a different path chosen by the user.
  162. */
  163. saveAs(): Promise<void>;
  164. /**
  165. * Revert the document contents to disk contents.
  166. */
  167. revert(): Promise<void>;
  168. /**
  169. * Create a checkpoint for the file.
  170. *
  171. * @returns A promise which resolves with the new checkpoint model when the
  172. * checkpoint is created.
  173. */
  174. createCheckpoint(): Promise<Contents.ICheckpointModel>;
  175. /**
  176. * Delete a checkpoint for the file.
  177. *
  178. * @param checkpointID - The id of the checkpoint to delete.
  179. *
  180. * @returns A promise which resolves when the checkpoint is deleted.
  181. */
  182. deleteCheckpoint(checkpointID: string): Promise<void>;
  183. /**
  184. * Restore the file to a known checkpoint state.
  185. *
  186. * @param checkpointID - The optional id of the checkpoint to restore,
  187. * defaults to the most recent checkpoint.
  188. *
  189. * @returns A promise which resolves when the checkpoint is restored.
  190. */
  191. restoreCheckpoint(checkpointID?: string): Promise<void>;
  192. /**
  193. * List available checkpoints for the file.
  194. *
  195. * @returns A promise which resolves with a list of checkpoint models for
  196. * the file.
  197. */
  198. listCheckpoints(): Promise<Contents.ICheckpointModel[]>;
  199. /**
  200. * Get the list of running sessions.
  201. */
  202. listSessions(): Promise<Session.IModel[]>;
  203. /**
  204. * Resolve a url to a correct server path.
  205. */
  206. resolveUrl(url: string): string;
  207. /**
  208. * Add a sibling widget to the document manager.
  209. *
  210. * @param widget - The widget to add to the document manager.
  211. *
  212. * @returns A disposable used to remove the sibling if desired.
  213. *
  214. * #### Notes
  215. * It is assumed that the widget has the same model and context
  216. * as the original widget.
  217. */
  218. addSibling(widget: Widget): IDisposable;
  219. }
  220. /**
  221. * The options used to register a widget factory.
  222. */
  223. export
  224. interface IWidgetFactoryOptions {
  225. /**
  226. * The file extensions the widget can view.
  227. *
  228. * #### Notes
  229. * Use "*" to denote all files. Specific file extensions must be preceded
  230. * with '.', like '.png', '.txt', etc.
  231. */
  232. fileExtensions: string[];
  233. /**
  234. * The name of the widget to display in dialogs.
  235. */
  236. displayName: string;
  237. /**
  238. * The registered name of the model type used to create the widgets.
  239. */
  240. modelName: string;
  241. /**
  242. * The file extensions for which the factory should be the default.
  243. *
  244. * #### Notes
  245. * Use "*" to denote all files. Specific file extensions must be preceded
  246. * with '.', like '.png', '.txt', etc. Entries in this attribute must also
  247. * be included in the fileExtensions attribute.
  248. * The default is an empty array.
  249. *
  250. * **See also:** [[fileExtensions]].
  251. */
  252. defaultFor?: string[];
  253. /**
  254. * Whether the widgets prefer having a kernel started.
  255. *
  256. * The default is `false`.
  257. */
  258. preferKernel?: boolean;
  259. /**
  260. * Whether the widgets can start a kernel when opened.
  261. *
  262. * The default is `false`.
  263. */
  264. canStartKernel?: boolean;
  265. }
  266. /**
  267. * The interface for a widget factory.
  268. */
  269. export
  270. interface IWidgetFactory<T extends Widget, U extends IDocumentModel> extends IDisposable {
  271. /**
  272. * A signal emitted when a widget is created.
  273. */
  274. widgetCreated: ISignal<IWidgetFactory<T, U>, T>;
  275. /**
  276. * Create a new widget.
  277. *
  278. * #### Notes
  279. * It should emit the [widgetCreated] signal with the new widget.
  280. */
  281. createNew(context: IDocumentContext<U>, kernel?: Kernel.IModel): T;
  282. }
  283. /**
  284. * An interface for a widget extension.
  285. */
  286. export
  287. interface IWidgetExtension<T extends Widget, U extends IDocumentModel> {
  288. /**
  289. * Create a new extension for a given widget.
  290. */
  291. createNew(widget: T, context: IDocumentContext<U>): IDisposable;
  292. }
  293. /**
  294. * The interface for a model factory.
  295. */
  296. export
  297. interface IModelFactory<T extends IDocumentModel> extends IDisposable {
  298. /**
  299. * The name of the model.
  300. *
  301. * #### Notes
  302. * This is a read-only property.
  303. */
  304. name: string;
  305. /**
  306. * The type of the file (defaults to `"file"`).
  307. *
  308. * #### Notes
  309. * This is a read-only property.
  310. */
  311. fileType: Contents.FileType;
  312. /**
  313. * The format of the file (default to `"text"`).
  314. *
  315. * This is a read-only property.
  316. */
  317. fileFormat: Contents.FileFormat;
  318. /**
  319. * Create a new model for a given path.
  320. *
  321. * @param languagePreference - An optional kernel language preference.
  322. *
  323. * @returns A new document model.
  324. */
  325. createNew(languagePreference?: string): T;
  326. /**
  327. * Get the preferred kernel language given an extension.
  328. */
  329. preferredLanguage(ext: string): string;
  330. }
  331. /**
  332. * A kernel preference for a given file path and widget.
  333. */
  334. export
  335. interface IKernelPreference {
  336. /**
  337. * The preferred kernel language.
  338. */
  339. language: string;
  340. /**
  341. * Whether to prefer having a kernel started when opening.
  342. */
  343. preferKernel: boolean;
  344. /**
  345. * Whether a kernel when can be started when opening.
  346. */
  347. canStartKernel: boolean;
  348. }
  349. /**
  350. * An interface for a file type.
  351. */
  352. export
  353. interface IFileType {
  354. /**
  355. * The name of the file type.
  356. */
  357. name: string;
  358. /**
  359. * The extension of the file type (e.g. `".txt"`).
  360. */
  361. extension: string;
  362. /**
  363. * The optional mimetype of the file type.
  364. */
  365. mimetype?: string;
  366. /**
  367. * The optional icon class to use for the file type.
  368. */
  369. icon?: string;
  370. /**
  371. * The type of the new file (defaults to `"file"`).
  372. */
  373. fileType?: Contents.FileType;
  374. /**
  375. * The format of the new file (default to `"text"`).
  376. */
  377. fileFormat?: Contents.FileFormat;
  378. }
  379. /**
  380. * An interface for a "Create New" item.
  381. */
  382. export
  383. interface IFileCreator {
  384. /**
  385. * The name of the file creator.
  386. */
  387. name: string;
  388. /**
  389. * The filetype name associated with the creator.
  390. */
  391. fileType: string;
  392. /**
  393. * The optional widget name.
  394. */
  395. widgetName?: string;
  396. /**
  397. * The optional kernel name.
  398. */
  399. kernelName?: string;
  400. }