api.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  1. /* -----------------------------------------------------------------------------
  2. | Copyright (c) Jupyter Development Team.
  3. | Distributed under the terms of the Modified BSD License.
  4. |----------------------------------------------------------------------------*/
  5. /**
  6. * This file defines the shared shared-models types.
  7. *
  8. * - Notebook Type.
  9. * - Notebook Metadata Types.
  10. * - Cell Types.
  11. * - Cell Metadata Types.
  12. *
  13. * It also defines the shared changes to be used in the events.
  14. */
  15. import * as nbformat from '@jupyterlab/nbformat';
  16. import { PartialJSONObject } from '@lumino/coreutils';
  17. import { IDisposable } from '@lumino/disposable';
  18. import { ISignal } from '@lumino/signaling';
  19. /**
  20. * ISharedBase defines common operations that can be performed on any shared object.
  21. */
  22. export interface ISharedBase extends IDisposable {
  23. /**
  24. * Undo an operation.
  25. */
  26. undo(): void;
  27. /**
  28. * Redo an operation.
  29. */
  30. redo(): void;
  31. /**
  32. * Whether the object can redo changes.
  33. */
  34. canUndo(): boolean;
  35. /**
  36. * Whether the object can undo changes.
  37. */
  38. canRedo(): boolean;
  39. /**
  40. * Clear the change stack.
  41. */
  42. clearUndoHistory(): void;
  43. /**
  44. * Perform a transaction. While the function f is called, all changes to the shared
  45. * document are bundled into a single event.
  46. */
  47. transact(f: () => void): void;
  48. }
  49. /**
  50. * Implement an API for Context information on the shared information.
  51. * This is used by, for example, docregistry to share the file-path of the edited content.
  52. */
  53. export interface ISharedDocument extends ISharedBase {
  54. /**
  55. * The changed signal.
  56. */
  57. readonly changed: ISignal<this, DocumentChange>;
  58. }
  59. /**
  60. * The ISharedText interface defines models that can be bound to a text editor like CodeMirror.
  61. */
  62. export interface ISharedText extends ISharedBase {
  63. /**
  64. * The changed signal.
  65. */
  66. readonly changed: ISignal<this, TextChange>;
  67. /**
  68. * Gets cell's source.
  69. *
  70. * @returns Cell's source.
  71. */
  72. getSource(): string;
  73. /**
  74. * Sets cell's source.
  75. *
  76. * @param value: New source.
  77. */
  78. setSource(value: string): void;
  79. /**
  80. * Replace content from `start' to `end` with `value`.
  81. *
  82. * @param start: The start index of the range to replace (inclusive).
  83. *
  84. * @param end: The end index of the range to replace (exclusive).
  85. *
  86. * @param value: New source (optional).
  87. */
  88. updateSource(start: number, end: number, value?: string): void;
  89. }
  90. /**
  91. * Text/Markdown/Code files are represented as ISharedFile
  92. */
  93. export interface ISharedFile extends ISharedDocument, ISharedText {
  94. /**
  95. * The changed signal.
  96. */
  97. readonly changed: ISignal<this, FileChange>;
  98. }
  99. /**
  100. * Implements an API for nbformat.INotebookContent
  101. */
  102. export interface ISharedNotebook extends ISharedDocument {
  103. /**
  104. * The changed signal.
  105. */
  106. readonly changed: ISignal<this, NotebookChange>;
  107. /**
  108. * The minor version number of the nbformat.
  109. */
  110. readonly nbformat_minor: number;
  111. /**
  112. * The major version number of the nbformat.
  113. */
  114. readonly nbformat: number;
  115. /**
  116. * The list of shared cells in the notebook.
  117. */
  118. readonly cells: ISharedCell[];
  119. /**
  120. * Returns the metadata associated with the notebook.
  121. *
  122. * @returns Notebook's metadata.
  123. */
  124. getMetadata(): nbformat.INotebookMetadata;
  125. /**
  126. * Sets the metadata associated with the notebook.
  127. *
  128. * @param metadata: Notebook's metadata.
  129. */
  130. setMetadata(metadata: nbformat.INotebookMetadata): void;
  131. /**
  132. * Updates the metadata associated with the notebook.
  133. *
  134. * @param value: Metadata's attribute to update.
  135. */
  136. updateMetadata(value: Partial<nbformat.INotebookMetadata>): void;
  137. /**
  138. * Get a shared cell by index.
  139. *
  140. * @param index: Cell's position.
  141. *
  142. * @returns The requested shared cell.
  143. */
  144. getCell(index: number): ISharedCell;
  145. /**
  146. * Insert a shared cell into a specific position.
  147. *
  148. * @param index: Cell's position.
  149. *
  150. * @param cell: Cell to insert.
  151. */
  152. insertCell(index: number, cell: ISharedCell): void;
  153. /**
  154. * Insert a list of shared cells into a specific position.
  155. *
  156. * @param index: Position to insert the cells.
  157. *
  158. * @param cells: Array of shared cells to insert.
  159. */
  160. insertCells(index: number, cells: Array<ISharedCell>): void;
  161. /**
  162. * Move a cell.
  163. *
  164. * @param fromIndex: Index of the cell to move.
  165. *
  166. * @param toIndex: New position of the cell.
  167. */
  168. moveCell(fromIndex: number, toIndex: number): void;
  169. /**
  170. * Remove a cell.
  171. *
  172. * @param index: Index of the cell to remove.
  173. */
  174. deleteCell(index: number): void;
  175. /**
  176. * Remove a range of cells.
  177. *
  178. * @param from: The start index of the range to remove (inclusive).
  179. *
  180. * @param to: The end index of the range to remove (exclusive).
  181. */
  182. deleteCellRange(from: number, to: number): void;
  183. }
  184. /**
  185. * Definition of the map changes for yjs.
  186. */
  187. export type MapChange = Map<
  188. string,
  189. { action: 'add' | 'update' | 'delete'; oldValue: any; newValue: any }
  190. >;
  191. /**
  192. * The namespace for `ISharedNotebook` class statics.
  193. */
  194. export namespace ISharedNotebook {
  195. /**
  196. * The options used to initialize a a ISharedNotebook
  197. */
  198. export interface IOptions {
  199. /**
  200. * Wether the the undo/redo logic should be
  201. * considered on the full document across all cells.
  202. */
  203. disableDocumentWideUndoRedo: boolean;
  204. }
  205. }
  206. /**
  207. * The Shared kernelspec metadata.
  208. */
  209. export interface ISharedKernelspecMetadata
  210. extends nbformat.IKernelspecMetadata,
  211. IDisposable {
  212. [key: string]: any;
  213. name: string;
  214. display_name: string;
  215. }
  216. /**
  217. * The Shared language info metatdata.
  218. */
  219. export interface ISharedLanguageInfoMetadata
  220. extends nbformat.ILanguageInfoMetadata,
  221. IDisposable {
  222. [key: string]: any;
  223. name: string;
  224. codemirror_mode?: string | PartialJSONObject;
  225. file_extension?: string;
  226. mimetype?: string;
  227. pygments_lexer?: string;
  228. }
  229. // Cell Types.
  230. export type ISharedCell =
  231. | ISharedCodeCell
  232. | ISharedRawCell
  233. | ISharedMarkdownCell
  234. | ISharedUnrecognizedCell;
  235. /**
  236. * Cell-level metadata.
  237. */
  238. export interface ISharedBaseCellMetadata extends nbformat.IBaseCellMetadata {
  239. [key: string]: any;
  240. }
  241. /**
  242. * Implements an API for nbformat.IBaseCell.
  243. */
  244. export interface ISharedBaseCell<Metadata extends ISharedBaseCellMetadata>
  245. extends ISharedText {
  246. /**
  247. * Whether the cell is standalone or not.
  248. *
  249. * If the cell is standalone. It cannot be
  250. * inserted into a YNotebook because the Yjs model is already
  251. * attached to an anonymous Y.Doc instance.
  252. */
  253. readonly isStandalone: boolean;
  254. /**
  255. * The type of the cell.
  256. */
  257. readonly cell_type: nbformat.CellType;
  258. /**
  259. * The changed signal.
  260. */
  261. readonly changed: ISignal<this, CellChange<Metadata>>;
  262. /**
  263. * Create a new YCodeCell that can be inserted into a YNotebook.
  264. *
  265. * @todo clone should only be available in the specific implementations i.e. ISharedCodeCell
  266. */
  267. clone(): ISharedBaseCell<Metadata>;
  268. /**
  269. * Get Cell id.
  270. *
  271. * @returns Cell id.
  272. */
  273. getId(): string;
  274. /**
  275. * Returns the metadata associated with the notebook.
  276. *
  277. * @returns Notebook's metadata.
  278. */
  279. getMetadata(): Partial<Metadata>;
  280. /**
  281. * Sets the metadata associated with the notebook.
  282. *
  283. * @param metadata: Notebook's metadata.
  284. */
  285. setMetadata(metadata: Partial<Metadata>): void;
  286. /**
  287. * Serialize the model to JSON.
  288. */
  289. toJSON(): nbformat.IBaseCell;
  290. }
  291. /**
  292. * Implements an API for nbformat.ICodeCell.
  293. */
  294. export interface ISharedCodeCell
  295. extends ISharedBaseCell<ISharedBaseCellMetadata> {
  296. /**
  297. * The type of the cell.
  298. */
  299. cell_type: 'code';
  300. /**
  301. * The code cell's prompt number. Will be null if the cell has not been run.
  302. */
  303. execution_count: nbformat.ExecutionCount;
  304. /**
  305. * Execution, display, or stream outputs.
  306. */
  307. getOutputs(): Array<nbformat.IOutput>;
  308. /**
  309. * Add/Update output.
  310. */
  311. setOutputs(outputs: Array<nbformat.IOutput>): void;
  312. /**
  313. * Replace content from `start' to `end` with `outputs`.
  314. *
  315. * @param start: The start index of the range to replace (inclusive).
  316. *
  317. * @param end: The end index of the range to replace (exclusive).
  318. *
  319. * @param outputs: New outputs (optional).
  320. */
  321. updateOutputs(
  322. start: number,
  323. end: number,
  324. outputs: Array<nbformat.IOutput>
  325. ): void;
  326. /**
  327. * Serialize the model to JSON.
  328. */
  329. toJSON(): nbformat.IBaseCell;
  330. }
  331. /**
  332. * Implements an API for nbformat.IMarkdownCell.
  333. */
  334. export interface ISharedMarkdownCell
  335. extends ISharedBaseCell<ISharedBaseCellMetadata> {
  336. /**
  337. * String identifying the type of cell.
  338. */
  339. cell_type: 'markdown';
  340. /**
  341. * Gets the cell attachments.
  342. *
  343. * @returns The cell attachments.
  344. */
  345. getAttachments(): nbformat.IAttachments | undefined;
  346. /**
  347. * Sets the cell attachments
  348. *
  349. * @param attachments: The cell attachments.
  350. */
  351. setAttachments(attachments: nbformat.IAttachments | undefined): void;
  352. /**
  353. * Serialize the model to JSON.
  354. */
  355. toJSON(): nbformat.IMarkdownCell;
  356. }
  357. /**
  358. * Implements an API for nbformat.IRawCell.
  359. */
  360. export interface ISharedRawCell
  361. extends ISharedBaseCell<ISharedBaseCellMetadata>,
  362. IDisposable {
  363. /**
  364. * String identifying the type of cell.
  365. */
  366. cell_type: 'raw';
  367. /**
  368. * Gets the cell attachments.
  369. *
  370. * @returns The cell attachments.
  371. */
  372. getAttachments(): nbformat.IAttachments | undefined;
  373. /**
  374. * Sets the cell attachments
  375. *
  376. * @param attachments: The cell attachments.
  377. */
  378. setAttachments(attachments: nbformat.IAttachments | undefined): void;
  379. /**
  380. * Serialize the model to JSON.
  381. */
  382. toJSON(): nbformat.IRawCell;
  383. }
  384. /**
  385. * Changes on Sequence-like data are expressed as Quill-inspired deltas.
  386. *
  387. * @source https://quilljs.com/docs/delta/
  388. */
  389. export type Delta<T> = Array<{ insert?: T; delete?: number; retain?: number }>;
  390. /**
  391. * Implements an API for nbformat.IUnrecognizedCell.
  392. *
  393. * @todo Is this needed?
  394. */
  395. export interface ISharedUnrecognizedCell
  396. extends ISharedBaseCell<ISharedBaseCellMetadata>,
  397. IDisposable {
  398. /**
  399. * The type of the cell.
  400. */
  401. cell_type: 'raw';
  402. /**
  403. * Serialize the model to JSON.
  404. */
  405. toJSON(): nbformat.ICodeCell;
  406. }
  407. export type TextChange = {
  408. sourceChange?: Delta<string>;
  409. };
  410. /**
  411. * Definition of the shared Notebook changes.
  412. */
  413. export type NotebookChange = {
  414. cellsChange?: Delta<ISharedCell[]>;
  415. metadataChange?: {
  416. oldValue: nbformat.INotebookMetadata;
  417. newValue: nbformat.INotebookMetadata | undefined;
  418. };
  419. contextChange?: MapChange;
  420. stateChange?: Array<{
  421. name: string;
  422. oldValue: any;
  423. newValue: any;
  424. }>;
  425. };
  426. export type FileChange = {
  427. sourceChange?: Delta<string>;
  428. contextChange?: MapChange;
  429. stateChange?: Array<{
  430. name: string;
  431. oldValue: any;
  432. newValue: any;
  433. }>;
  434. };
  435. /**
  436. * Definition of the shared Cell changes.
  437. */
  438. export type CellChange<MetadataType> = {
  439. sourceChange?: Delta<string>;
  440. outputsChange?: Delta<nbformat.IOutput[]>;
  441. executionCountChange?: {
  442. oldValue: number | undefined;
  443. newValue: number | undefined;
  444. };
  445. metadataChange?: {
  446. oldValue: Partial<MetadataType> | undefined;
  447. newValue: Partial<MetadataType> | undefined;
  448. };
  449. };
  450. export type DocumentChange = {
  451. contextChange?: MapChange;
  452. stateChange?: Array<{
  453. name: string;
  454. oldValue: any;
  455. newValue: any;
  456. }>;
  457. };