interfaces.ts 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. // Copyright (c) Jupyter Development Team.
  2. // Distributed under the terms of the Modified BSD License.
  3. import {
  4. IContents, IKernel, ISession
  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>, IContents.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. * The unique id of the context.
  107. *
  108. * #### Notes
  109. * This is a read-only property.
  110. */
  111. id: string;
  112. /**
  113. * Get the model associated with the document.
  114. *
  115. * #### Notes
  116. * This is a read-only property
  117. */
  118. model: T;
  119. /**
  120. * The current kernel associated with the document.
  121. *
  122. * #### Notes
  123. * This is a read-only propery.
  124. */
  125. kernel: IKernel;
  126. /**
  127. * The current path associated with the document.
  128. *
  129. * #### Notes
  130. * This is a read-only property.
  131. */
  132. path: string;
  133. /**
  134. * The current contents model associated with the document
  135. *
  136. * #### Notes
  137. * This is a read-only property. The model will have an
  138. * empty `contents` field. It will be `null` until the
  139. * first save or load to disk.
  140. */
  141. contentsModel: IContents.IModel;
  142. /**
  143. * Get the kernel spec information.
  144. *
  145. * #### Notes
  146. * This is a read-only property.
  147. */
  148. kernelspecs: IKernel.ISpecModels;
  149. /**
  150. * Test whether the context is fully populated.
  151. *
  152. * #### Notes
  153. * This is a read-only property.
  154. */
  155. isPopulated: boolean;
  156. /**
  157. * Change the current kernel associated with the document.
  158. *
  159. * #### Notes
  160. * If no options are given, the session is shut down.
  161. */
  162. changeKernel(options?: IKernel.IModel): Promise<IKernel>;
  163. /**
  164. * Save the document contents to disk.
  165. */
  166. save(): Promise<void>;
  167. /**
  168. * Save the document to a different path chosen by the user.
  169. */
  170. saveAs(): Promise<void>;
  171. /**
  172. * Revert the document contents to disk contents.
  173. */
  174. revert(): Promise<void>;
  175. /**
  176. * Create a checkpoint for the file.
  177. *
  178. * @returns A promise which resolves with the new checkpoint model when the
  179. * checkpoint is created.
  180. */
  181. createCheckpoint(): Promise<IContents.ICheckpointModel>;
  182. /**
  183. * Delete a checkpoint for the file.
  184. *
  185. * @param checkpointID - The id of the checkpoint to delete.
  186. *
  187. * @returns A promise which resolves when the checkpoint is deleted.
  188. */
  189. deleteCheckpoint(checkpointID: string): Promise<void>;
  190. /**
  191. * Restore the file to a known checkpoint state.
  192. *
  193. * @param checkpointID - The optional id of the checkpoint to restore,
  194. * defaults to the most recent checkpoint.
  195. *
  196. * @returns A promise which resolves when the checkpoint is restored.
  197. */
  198. restoreCheckpoint(checkpointID?: string): Promise<void>;
  199. /**
  200. * List available checkpoints for the file.
  201. *
  202. * @returns A promise which resolves with a list of checkpoint models for
  203. * the file.
  204. */
  205. listCheckpoints(): Promise<IContents.ICheckpointModel[]>;
  206. /**
  207. * Get the list of running sessions.
  208. */
  209. listSessions(): Promise<ISession.IModel[]>;
  210. /**
  211. * Resolve a url to a correct server path.
  212. */
  213. resolveUrl(url: string): string;
  214. /**
  215. * Add a sibling widget to the document manager.
  216. *
  217. * @param widget - The widget to add to the document manager.
  218. *
  219. * @returns A disposable used to remove the sibling if desired.
  220. *
  221. * #### Notes
  222. * It is assumed that the widget has the same model and context
  223. * as the original widget.
  224. */
  225. addSibling(widget: Widget): IDisposable;
  226. }
  227. /**
  228. * The options used to register a widget factory.
  229. */
  230. export
  231. interface IWidgetFactoryOptions {
  232. /**
  233. * The file extensions the widget can view.
  234. *
  235. * #### Notes
  236. * Use "*" to denote all files. Specific file extensions must be preceded
  237. * with '.', like '.png', '.txt', etc.
  238. */
  239. fileExtensions: string[];
  240. /**
  241. * The name of the widget to display in dialogs.
  242. */
  243. displayName: string;
  244. /**
  245. * The registered name of the model type used to create the widgets.
  246. */
  247. modelName: string;
  248. /**
  249. * The file extensions for which the factory should be the default.
  250. *
  251. * #### Notes
  252. * Use "*" to denote all files. Specific file extensions must be preceded
  253. * with '.', like '.png', '.txt', etc. Entries in this attribute must also
  254. * be included in the fileExtensions attribute.
  255. * The default is an empty array.
  256. *
  257. * **See also:** [[fileExtensions]].
  258. */
  259. defaultFor?: string[];
  260. /**
  261. * Whether the widgets prefer having a kernel started.
  262. *
  263. * The default is `false`.
  264. */
  265. preferKernel?: boolean;
  266. /**
  267. * Whether the widgets can start a kernel when opened.
  268. *
  269. * The default is `false`.
  270. */
  271. canStartKernel?: boolean;
  272. }
  273. /**
  274. * The interface for a widget factory.
  275. */
  276. export
  277. interface IWidgetFactory<T extends Widget, U extends IDocumentModel> extends IDisposable {
  278. /**
  279. * A signal emitted when a widget is created.
  280. */
  281. widgetCreated: ISignal<IWidgetFactory<T, U>, T>;
  282. /**
  283. * Create a new widget.
  284. *
  285. * #### Notes
  286. * It should emit the [widgetCreated] signal with the new widget.
  287. */
  288. createNew(context: IDocumentContext<U>, kernel?: IKernel.IModel): T;
  289. }
  290. /**
  291. * An interface for a widget extension.
  292. */
  293. export
  294. interface IWidgetExtension<T extends Widget, U extends IDocumentModel> {
  295. /**
  296. * Create a new extension for a given widget.
  297. */
  298. createNew(widget: T, context: IDocumentContext<U>): IDisposable;
  299. }
  300. /**
  301. * The interface for a model factory.
  302. */
  303. export
  304. interface IModelFactory extends IDisposable {
  305. /**
  306. * The name of the model.
  307. *
  308. * #### Notes
  309. * This is a read-only property.
  310. */
  311. name: string;
  312. /**
  313. * The type of the file (defaults to `"file"`).
  314. *
  315. * #### Notes
  316. * This is a read-only property.
  317. */
  318. fileType: IContents.FileType;
  319. /**
  320. * The format of the file (default to `"text"`).
  321. *
  322. * This is a read-only property.
  323. */
  324. fileFormat: IContents.FileFormat;
  325. /**
  326. * Create a new model for a given path.
  327. *
  328. * @param languagePreference - An optional kernel language preference.
  329. *
  330. * @returns A new document model.
  331. */
  332. createNew(languagePreference?: string): IDocumentModel;
  333. /**
  334. * Get the preferred kernel language given an extension.
  335. */
  336. preferredLanguage(ext: string): string;
  337. }
  338. /**
  339. * A kernel preference for a given file path and widget.
  340. */
  341. export
  342. interface IKernelPreference {
  343. /**
  344. * The preferred kernel language.
  345. */
  346. language: string;
  347. /**
  348. * Whether to prefer having a kernel started when opening.
  349. */
  350. preferKernel: boolean;
  351. /**
  352. * Whether a kernel when can be started when opening.
  353. */
  354. canStartKernel: boolean;
  355. }
  356. /**
  357. * An interface for a file type.
  358. */
  359. export
  360. interface IFileType {
  361. /**
  362. * The name of the file type.
  363. */
  364. name: string;
  365. /**
  366. * The extension of the file type (e.g. `".txt"`).
  367. */
  368. extension: string;
  369. /**
  370. * The optional mimetype of the file type.
  371. */
  372. mimetype?: string;
  373. /**
  374. * The optional icon class to use for the file type.
  375. */
  376. icon?: string;
  377. /**
  378. * The type of the new file (defaults to `"file"`).
  379. */
  380. fileType?: IContents.FileType;
  381. /**
  382. * The format of the new file (default to `"text"`).
  383. */
  384. fileFormat?: IContents.FileFormat;
  385. }
  386. /**
  387. * An interface for a "Create New" item.
  388. */
  389. export
  390. interface IFileCreator {
  391. /**
  392. * The name of the file creator.
  393. */
  394. name: string;
  395. /**
  396. * The filetype name associated with the creator.
  397. */
  398. fileType: string;
  399. /**
  400. * The optional widget name.
  401. */
  402. widgetName?: string;
  403. /**
  404. * The optional kernel name.
  405. */
  406. kernelName?: string;
  407. }