@difizen/libro-shared-model 0.0.2-alpha.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/api.ts ADDED
@@ -0,0 +1,743 @@
1
+ /**
2
+ * This file defines the shared shared-models types.
3
+ *
4
+ * - Notebook Type.
5
+ * - Notebook Metadata Types.
6
+ * - Cell Types.
7
+ * - Cell Metadata Types.
8
+ *
9
+ * It also defines the shared changes to be used in the events.
10
+ */
11
+
12
+ import type {
13
+ INotebookMetadata,
14
+ PartialJSONValue,
15
+ IRawCell,
16
+ ICodeCell,
17
+ IMarkdownCell,
18
+ IBaseCell,
19
+ IBaseCellMetadata,
20
+ CellType,
21
+ ExecutionCount,
22
+ IOutput,
23
+ IAttachments,
24
+ IUnrecognizedCell,
25
+ } from '@difizen/libro-common';
26
+ import type { Disposable, Event } from '@difizen/mana-common';
27
+
28
+ /**
29
+ * Changes on Sequence-like data are expressed as Quill-inspired deltas.
30
+ *
31
+ * @source https://quilljs.com/docs/delta/
32
+ */
33
+ export type Delta<T> = { insert?: T; delete?: number; retain?: number }[];
34
+
35
+ /**
36
+ * ISharedBase defines common operations that can be performed on any shared object.
37
+ */
38
+ export interface ISharedBase extends Disposable {
39
+ /**
40
+ * Undo an operation.
41
+ */
42
+ undo: () => void;
43
+
44
+ /**
45
+ * Redo an operation.
46
+ */
47
+ redo: () => void;
48
+
49
+ /**
50
+ * Whether the object can redo changes.
51
+ */
52
+ canUndo: () => boolean;
53
+
54
+ /**
55
+ * Whether the object can undo changes.
56
+ */
57
+ canRedo: () => boolean;
58
+
59
+ /**
60
+ * Clear the change stack.
61
+ */
62
+ clearUndoHistory: () => void;
63
+
64
+ /**
65
+ * Perform a transaction. While the function f is called, all changes to the shared
66
+ * document are bundled into a single event.
67
+ */
68
+ transact: (f: () => void) => void;
69
+ }
70
+
71
+ /**
72
+ * Implement an API for Context information on the shared information.
73
+ * This is used by, for example, docregistry to share the file-path of the edited content.
74
+ */
75
+ export interface ISharedDocument extends ISharedBase {
76
+ /**
77
+ * The changed signal.
78
+ */
79
+ readonly changed: Event<DocumentChange>;
80
+ }
81
+
82
+ /**
83
+ * The ISharedText interface defines models that can be bound to a text editor like CodeMirror.
84
+ */
85
+ export interface ISharedText extends ISharedBase {
86
+ /**
87
+ * The changed signal.
88
+ */
89
+ readonly changed: Event<SourceChange>;
90
+
91
+ /**
92
+ * Text
93
+ */
94
+ source: string;
95
+
96
+ /**
97
+ * Get text.
98
+ *
99
+ * @returns Text.
100
+ */
101
+ getSource: () => string;
102
+
103
+ /**
104
+ * Set text.
105
+ *
106
+ * @param value New text.
107
+ */
108
+ setSource: (value: string) => void;
109
+
110
+ /**
111
+ * Replace content from `start' to `end` with `value`.
112
+ *
113
+ * @param start: The start index of the range to replace (inclusive).
114
+ * @param end: The end index of the range to replace (exclusive).
115
+ * @param value: New source (optional).
116
+ */
117
+ updateSource: (start: number, end: number, value?: string) => void;
118
+ }
119
+
120
+ /**
121
+ * Text/Markdown/Code files are represented as ISharedFile
122
+ */
123
+ export interface ISharedFile extends ISharedDocument, ISharedText {
124
+ /**
125
+ * The changed signal.
126
+ */
127
+ readonly changed: Event<FileChange>;
128
+ }
129
+
130
+ /**
131
+ * Implements an API for INotebookContent
132
+ */
133
+ export interface ISharedNotebook extends ISharedDocument {
134
+ /**
135
+ * The changed signal.
136
+ */
137
+ readonly undoChanged: Event<boolean>;
138
+ /**
139
+ * The changed signal.
140
+ */
141
+ readonly redoChanged: Event<boolean>;
142
+ /**
143
+ * The changed signal.
144
+ */
145
+ readonly changed: Event<NotebookChange>;
146
+
147
+ /**
148
+ * Signal triggered when a metadata changes.
149
+ */
150
+ readonly metadataChanged: Event<IMapChange>;
151
+
152
+ /**
153
+ * The list of shared cells in the notebook.
154
+ */
155
+ readonly cells: ISharedCell[];
156
+
157
+ /**
158
+ * Signal triggered when the cells list changes.
159
+ */
160
+ readonly cellsChanged: Event<IListChange>;
161
+
162
+ /**
163
+ * Wether the undo/redo logic should be
164
+ * considered on the full document across all cells.
165
+ */
166
+ readonly disableDocumentWideUndoRedo?: boolean;
167
+
168
+ /**
169
+ * Notebook metadata.
170
+ */
171
+ metadata: INotebookMetadata;
172
+
173
+ /**
174
+ * The minor version number of the
175
+ */
176
+ readonly nbformat_minor: number;
177
+
178
+ /**
179
+ * The major version number of the
180
+ */
181
+ readonly nbformat: number;
182
+
183
+ /**
184
+ * Delete a metadata notebook.
185
+ *
186
+ * @param key The key to delete
187
+ */
188
+ deleteMetadata: (key: string) => void;
189
+
190
+ /**
191
+ * Returns some metadata associated with the notebook.
192
+ *
193
+ * If no `key` is provided, it will return all metadata.
194
+ * Else it will return the value for that key.
195
+ *
196
+ * @param key Key to get from the metadata
197
+ * @returns Notebook's metadata.
198
+ */
199
+ getMetadata: (key?: string) => INotebookMetadata;
200
+
201
+ /**
202
+ * Sets some metadata associated with the notebook.
203
+ *
204
+ * If only one argument is provided, it will override all notebook metadata.
205
+ * Otherwise a single key will be set to a new value.
206
+ *
207
+ * @param metadata All Notebook's metadata or the key to set.
208
+ * @param value New metadata value
209
+ */
210
+ setMetadata: (metadata: INotebookMetadata | string, value?: PartialJSONValue) => void;
211
+
212
+ /**
213
+ * Updates the metadata associated with the notebook.
214
+ *
215
+ * @param value: Metadata's attribute to update.
216
+ */
217
+ updateMetadata: (value: Partial<INotebookMetadata>) => void;
218
+
219
+ /**
220
+ * Add a shared cell at the notebook bottom.
221
+ *
222
+ * @param cell Cell to add.
223
+ *
224
+ * @returns The added cell.
225
+ */
226
+ addCell: (cell: SharedCell.Cell) => ISharedCell;
227
+
228
+ /**
229
+ * Get a shared cell by index.
230
+ *
231
+ * @param index: Cell's position.
232
+ *
233
+ * @returns The requested shared cell.
234
+ */
235
+ getCell: (index: number) => ISharedCell;
236
+
237
+ /**
238
+ * Insert a shared cell into a specific position.
239
+ *
240
+ * @param index Cell's position.
241
+ * @param cell Cell to insert.
242
+ *
243
+ * @returns The inserted cell.
244
+ */
245
+ insertCell: (index: number, cell: SharedCell.Cell) => ISharedCell;
246
+
247
+ /**
248
+ * Insert a list of shared cells into a specific position.
249
+ *
250
+ * @param index Position to insert the cells.
251
+ * @param cells Array of shared cells to insert.
252
+ *
253
+ * @returns The inserted cells.
254
+ */
255
+ insertCells: (index: number, cells: SharedCell.Cell[]) => ISharedCell[];
256
+
257
+ /**
258
+ * Move a cell.
259
+ *
260
+ * @param fromIndex: Index of the cell to move.
261
+ *
262
+ * @param toIndex: New position of the cell.
263
+ */
264
+ moveCell: (fromIndex: number, toIndex: number) => void;
265
+
266
+ /**
267
+ * Remove a cell.
268
+ *
269
+ * @param index: Index of the cell to remove.
270
+ */
271
+ deleteCell: (index: number) => void;
272
+
273
+ /**
274
+ * Remove a range of cells.
275
+ *
276
+ * @param from: The start index of the range to remove (inclusive).
277
+ *
278
+ * @param to: The end index of the range to remove (exclusive).
279
+ */
280
+ deleteCellRange: (from: number, to: number) => void;
281
+ }
282
+
283
+ /**
284
+ * Definition of the map changes for yjs.
285
+ */
286
+ export type MapChange = Map<
287
+ string,
288
+ { action: 'add' | 'update' | 'delete'; oldValue: any; newValue: any }
289
+ >;
290
+
291
+ /**
292
+ * 类型转换器:将libro的cell type(string) 转换为 'code' | 'markdown' | 'raw' 三种之一
293
+ */
294
+ export type CellTypeAdaptor = (cell_type: CellType) => 'code' | 'markdown' | 'raw';
295
+
296
+ /**
297
+ * The namespace for `ISharedNotebook` class statics.
298
+ */
299
+ export namespace ISharedNotebook {
300
+ /**
301
+ * The options used to initialize a a ISharedNotebook
302
+ */
303
+ export interface IOptions {
304
+ /**
305
+ * Wether the the undo/redo logic should be
306
+ * considered on the full document across all cells.
307
+ */
308
+ disableDocumentWideUndoRedo?: boolean;
309
+
310
+ cellTypeAdaptor?: CellTypeAdaptor;
311
+ }
312
+ }
313
+
314
+ /** Cell Types. */
315
+ export type ISharedCell =
316
+ | ISharedCodeCell
317
+ | ISharedRawCell
318
+ | ISharedMarkdownCell
319
+ | ISharedUnrecognizedCell;
320
+
321
+ /**
322
+ * Shared cell namespace
323
+ */
324
+ export namespace SharedCell {
325
+ /**
326
+ * Cell data
327
+ */
328
+ export type Cell = (IRawCell | ICodeCell | IMarkdownCell | IBaseCell) & {
329
+ cell_type: string;
330
+ };
331
+
332
+ /**
333
+ * Shared cell constructor options.
334
+ */
335
+ export interface IOptions {
336
+ /**
337
+ * Optional notebook to which this cell belongs.
338
+ *
339
+ * If not provided the cell will be standalone.
340
+ */
341
+ notebook?: ISharedNotebook | undefined;
342
+ }
343
+ }
344
+
345
+ /**
346
+ * Implements an API for IBaseCell.
347
+ */
348
+ export interface ISharedBaseCell<Metadata extends IBaseCellMetadata = IBaseCellMetadata>
349
+ extends ISharedText {
350
+ /**
351
+ * The type of the cell.
352
+ */
353
+ readonly cell_type: CellType;
354
+
355
+ /**
356
+ * The changed signal.
357
+ */
358
+ readonly changed: Event<CellChange<Metadata>>;
359
+
360
+ /**
361
+ * Cell id.
362
+ */
363
+ readonly id: string;
364
+
365
+ /**
366
+ * Whether the cell is standalone or not.
367
+ *
368
+ * If the cell is standalone. It cannot be
369
+ * inserted into a YNotebook because the Yjs model is already
370
+ * attached to an anonymous Y.Doc instance.
371
+ */
372
+ readonly isStandalone: boolean;
373
+
374
+ /**
375
+ * Cell metadata.
376
+ */
377
+ metadata: Partial<Metadata>;
378
+
379
+ /**
380
+ * Signal triggered when the cell metadata changes.
381
+ */
382
+ readonly metadataChanged: Event<IMapChange>;
383
+
384
+ /**
385
+ * The notebook that this cell belongs to.
386
+ */
387
+ readonly notebook: ISharedNotebook | null;
388
+
389
+ /**
390
+ * Get Cell id.
391
+ *
392
+ * @returns Cell id.
393
+ */
394
+ getId: () => string;
395
+
396
+ /**
397
+ * Delete a metadata cell.
398
+ *
399
+ * @param key The key to delete
400
+ */
401
+ deleteMetadata: (key: string) => void;
402
+
403
+ /**
404
+ * Returns some metadata associated with the cell.
405
+ *
406
+ * If a `key` is provided, returns the metadata value.
407
+ * Otherwise returns all metadata
408
+ *
409
+ * @returns Cell's metadata.
410
+ */
411
+ getMetadata: (key?: string) => Partial<Metadata>;
412
+
413
+ /**
414
+ * Sets some cell metadata.
415
+ *
416
+ * If only one argument is provided, it will override all cell metadata.
417
+ * Otherwise a single key will be set to a new value.
418
+ *
419
+ * @param metadata Cell's metadata or key.
420
+ * @param value Metadata value
421
+ */
422
+ setMetadata: (metadata: Partial<Metadata> | string, value?: PartialJSONValue) => void;
423
+
424
+ /**
425
+ * Serialize the model to JSON.
426
+ */
427
+ toJSON: () => IBaseCell;
428
+ }
429
+
430
+ /**
431
+ * Implements an API for ICodeCell.
432
+ */
433
+ export interface ISharedCodeCell extends ISharedBaseCell<IBaseCellMetadata> {
434
+ /**
435
+ * The type of the cell.
436
+ * note: modified in for libro, code cell type maybe python\sql\javascript etc.
437
+ */
438
+ cell_type: string;
439
+ // cell_type: 'code';
440
+
441
+ /**
442
+ * The code cell's prompt number. Will be null if the cell has not been run.
443
+ */
444
+ execution_count: ExecutionCount;
445
+
446
+ /**
447
+ * Cell outputs
448
+ */
449
+ outputs: IOutput[];
450
+
451
+ /**
452
+ * Execution, display, or stream outputs.
453
+ */
454
+ getOutputs: () => IOutput[];
455
+
456
+ /**
457
+ * Add/Update output.
458
+ */
459
+ setOutputs: (outputs: IOutput[]) => void;
460
+
461
+ /**
462
+ * Replace content from `start' to `end` with `outputs`.
463
+ *
464
+ * @param start: The start index of the range to replace (inclusive).
465
+ *
466
+ * @param end: The end index of the range to replace (exclusive).
467
+ *
468
+ * @param outputs: New outputs (optional).
469
+ */
470
+ updateOutputs: (start: number, end: number, outputs: IOutput[]) => void;
471
+
472
+ /**
473
+ * Serialize the model to JSON.
474
+ */
475
+ toJSON: () => IBaseCell;
476
+ }
477
+
478
+ /**
479
+ * Cell with attachment interface.
480
+ */
481
+ export interface ISharedAttachmentsCell extends ISharedBaseCell<IBaseCellMetadata> {
482
+ /**
483
+ * Cell attachments
484
+ */
485
+ attachments?: IAttachments | undefined;
486
+
487
+ /**
488
+ * Gets the cell attachments.
489
+ *
490
+ * @returns The cell attachments.
491
+ */
492
+ getAttachments: () => IAttachments | undefined;
493
+
494
+ /**
495
+ * Sets the cell attachments
496
+ *
497
+ * @param attachments: The cell attachments.
498
+ */
499
+ setAttachments: (attachments: IAttachments | undefined) => void;
500
+ }
501
+
502
+ /**
503
+ * Implements an API for IMarkdownCell.
504
+ */
505
+ export interface ISharedMarkdownCell extends ISharedAttachmentsCell {
506
+ /**
507
+ * String identifying the type of cell.
508
+ */
509
+ cell_type: 'markdown';
510
+
511
+ /**
512
+ * Serialize the model to JSON.
513
+ */
514
+ toJSON: () => IMarkdownCell;
515
+ }
516
+
517
+ /**
518
+ * Implements an API for IRawCell.
519
+ */
520
+ export interface ISharedRawCell extends ISharedAttachmentsCell {
521
+ /**
522
+ * String identifying the type of cell.
523
+ */
524
+ cell_type: 'raw';
525
+
526
+ /**
527
+ * Serialize the model to JSON.
528
+ */
529
+ toJSON: () => IRawCell;
530
+ }
531
+
532
+ /**
533
+ * Implements an API for IUnrecognizedCell.
534
+ */
535
+ export interface ISharedUnrecognizedCell extends ISharedBaseCell<IBaseCellMetadata> {
536
+ /**
537
+ * The type of the cell.
538
+ *
539
+ * The notebook format specified the type will not be 'markdown' | 'raw' | 'code'
540
+ */
541
+ cell_type: string;
542
+
543
+ /**
544
+ * Serialize the model to JSON.
545
+ */
546
+ toJSON: () => IUnrecognizedCell;
547
+ }
548
+
549
+ export type StateChange<T> = {
550
+ /**
551
+ * Key changed
552
+ */
553
+ name: string;
554
+ /**
555
+ * Old value
556
+ */
557
+ oldValue?: T;
558
+ /**
559
+ * New value
560
+ */
561
+ newValue?: T;
562
+ };
563
+
564
+ /**
565
+ * Generic document change
566
+ */
567
+ export type DocumentChange = {
568
+ /**
569
+ * The context a map => should be part of the document state map
570
+ */
571
+ // FIXME to remove at some point
572
+ contextChange?: MapChange;
573
+ /**
574
+ * Change occurring in the document state.
575
+ */
576
+ stateChange?: StateChange<any>[] | undefined;
577
+ };
578
+
579
+ /**
580
+ * The change types which occur on a list.
581
+ */
582
+ export type ListChangeType =
583
+ /**
584
+ * Item(s) were added to the list.
585
+ */
586
+ | 'add'
587
+
588
+ /**
589
+ * Item(s) were removed from the list.
590
+ */
591
+ | 'remove';
592
+
593
+ /**
594
+ * The changed object which is emitted by a list.
595
+ */
596
+ export interface IListChange<T = any> {
597
+ /**
598
+ * The type of change undergone by the vector.
599
+ */
600
+ type: ListChangeType;
601
+
602
+ /**
603
+ * The new index associated with the change.
604
+ */
605
+ newIndex: number;
606
+
607
+ /**
608
+ * The new values associated with the change.
609
+ *
610
+ * #### Notes
611
+ * The values will be contiguous starting at the `newIndex`.
612
+ */
613
+ newValues: T[];
614
+
615
+ /**
616
+ * The old index associated with the change.
617
+ */
618
+ oldIndex: number;
619
+
620
+ /**
621
+ * The old values associated with the change.
622
+ *
623
+ * #### Notes
624
+ * The values will be contiguous starting at the `oldIndex`.
625
+ */
626
+ oldValues: T[];
627
+ }
628
+
629
+ /**
630
+ * The change types which occur on an observable map.
631
+ */
632
+ export type MapChangeType =
633
+ /**
634
+ * An entry was added.
635
+ */
636
+ | 'add'
637
+
638
+ /**
639
+ * An entry was removed.
640
+ */
641
+ | 'remove'
642
+
643
+ /**
644
+ * An entry was changed.
645
+ */
646
+ | 'change';
647
+
648
+ /**
649
+ * The changed args object which is emitted by an observable map.
650
+ */
651
+ export interface IMapChange<T = any> {
652
+ /**
653
+ * The type of change undergone by the map.
654
+ */
655
+ type: MapChangeType;
656
+
657
+ /**
658
+ * The key of the change.
659
+ */
660
+ key: string;
661
+
662
+ /**
663
+ * The old value of the change.
664
+ */
665
+ oldValue?: T;
666
+
667
+ /**
668
+ * The new value of the change.
669
+ */
670
+ newValue?: T;
671
+ }
672
+
673
+ /**
674
+ * Text source change
675
+ */
676
+ export type SourceChange = {
677
+ /**
678
+ * Text source change
679
+ */
680
+ sourceChange?: Delta<string>;
681
+ };
682
+
683
+ /**
684
+ * Definition of the shared Notebook changes.
685
+ */
686
+ export type NotebookChange = DocumentChange & {
687
+ /**
688
+ * Cell changes
689
+ */
690
+ cellsChange?: Delta<ISharedCell[]>;
691
+ /**
692
+ * Notebook metadata changes
693
+ */
694
+ metadataChange?: {
695
+ oldValue: INotebookMetadata;
696
+ newValue?: INotebookMetadata;
697
+ };
698
+ /**
699
+ * nbformat version change
700
+ */
701
+ nbformatChanged?: {
702
+ key: string;
703
+ oldValue?: number;
704
+ newValue?: number;
705
+ };
706
+ };
707
+
708
+ /**
709
+ * File change
710
+ */
711
+ export type FileChange = DocumentChange & SourceChange;
712
+
713
+ /**
714
+ * Definition of the shared Cell changes.
715
+ */
716
+ export type CellChange<MetadataType extends IBaseCellMetadata = IBaseCellMetadata> =
717
+ SourceChange & {
718
+ /**
719
+ * Cell attachment change
720
+ */
721
+ attachmentsChange?: {
722
+ oldValue?: IAttachments;
723
+ newValue?: IAttachments;
724
+ };
725
+ /**
726
+ * Cell output changes
727
+ */
728
+ outputsChange?: Delta<IOutput[]>;
729
+ /**
730
+ * Cell execution count change
731
+ */
732
+ executionCountChange?: {
733
+ oldValue?: number;
734
+ newValue?: number;
735
+ };
736
+ /**
737
+ * Cell metadata change
738
+ */
739
+ metadataChange?: {
740
+ oldValue?: Partial<MetadataType>;
741
+ newValue?: Partial<MetadataType>;
742
+ };
743
+ };