123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516 |
- /* -----------------------------------------------------------------------------
- | Copyright (c) Jupyter Development Team.
- | Distributed under the terms of the Modified BSD License.
- |----------------------------------------------------------------------------*/
- /**
- * This file defines the shared shared-models types.
- *
- * - Notebook Type.
- * - Notebook Metadata Types.
- * - Cell Types.
- * - Cell Metadata Types.
- *
- * It also defines the shared changes to be used in the events.
- */
- import * as nbformat from '@jupyterlab/nbformat';
- import { PartialJSONObject } from '@lumino/coreutils';
- import { IDisposable } from '@lumino/disposable';
- import { ISignal } from '@lumino/signaling';
- /**
- * ISharedBase defines common operations that can be performed on any shared object.
- */
- export interface ISharedBase extends IDisposable {
- /**
- * Undo an operation.
- */
- undo(): void;
- /**
- * Redo an operation.
- */
- redo(): void;
- /**
- * Whether the object can redo changes.
- */
- canUndo(): boolean;
- /**
- * Whether the object can undo changes.
- */
- canRedo(): boolean;
- /**
- * Clear the change stack.
- */
- clearUndoHistory(): void;
- /**
- * Perform a transaction. While the function f is called, all changes to the shared
- * document are bundled into a single event.
- */
- transact(f: () => void): void;
- }
- /**
- * Implement an API for Context information on the shared information.
- * This is used by, for example, docregistry to share the file-path of the edited content.
- */
- export interface ISharedDocument extends ISharedBase {
- /**
- * The changed signal.
- */
- readonly changed: ISignal<this, DocumentChange>;
- }
- /**
- * The ISharedText interface defines models that can be bound to a text editor like CodeMirror.
- */
- export interface ISharedText extends ISharedBase {
- /**
- * The changed signal.
- */
- readonly changed: ISignal<this, TextChange>;
- /**
- * Gets cell's source.
- *
- * @returns Cell's source.
- */
- getSource(): string;
- /**
- * Sets cell's source.
- *
- * @param value: New source.
- */
- setSource(value: string): void;
- /**
- * Replace content from `start' to `end` with `value`.
- *
- * @param start: The start index of the range to replace (inclusive).
- *
- * @param end: The end index of the range to replace (exclusive).
- *
- * @param value: New source (optional).
- */
- updateSource(start: number, end: number, value?: string): void;
- }
- /**
- * Text/Markdown/Code files are represented as ISharedFile
- */
- export interface ISharedFile extends ISharedDocument, ISharedText {
- /**
- * The changed signal.
- */
- readonly changed: ISignal<this, FileChange>;
- }
- /**
- * Implements an API for nbformat.INotebookContent
- */
- export interface ISharedNotebook extends ISharedDocument {
- /**
- * The changed signal.
- */
- readonly changed: ISignal<this, NotebookChange>;
- /**
- * The minor version number of the nbformat.
- */
- readonly nbformat_minor: number;
- /**
- * The major version number of the nbformat.
- */
- readonly nbformat: number;
- /**
- * The list of shared cells in the notebook.
- */
- readonly cells: ISharedCell[];
- /**
- * Returns the metadata associated with the notebook.
- *
- * @returns Notebook's metadata.
- */
- getMetadata(): nbformat.INotebookMetadata;
- /**
- * Sets the metadata associated with the notebook.
- *
- * @param metadata: Notebook's metadata.
- */
- setMetadata(metadata: nbformat.INotebookMetadata): void;
- /**
- * Updates the metadata associated with the notebook.
- *
- * @param value: Metadata's attribute to update.
- */
- updateMetadata(value: Partial<nbformat.INotebookMetadata>): void;
- /**
- * Get a shared cell by index.
- *
- * @param index: Cell's position.
- *
- * @returns The requested shared cell.
- */
- getCell(index: number): ISharedCell;
- /**
- * Insert a shared cell into a specific position.
- *
- * @param index: Cell's position.
- *
- * @param cell: Cell to insert.
- */
- insertCell(index: number, cell: ISharedCell): void;
- /**
- * Insert a list of shared cells into a specific position.
- *
- * @param index: Position to insert the cells.
- *
- * @param cells: Array of shared cells to insert.
- */
- insertCells(index: number, cells: Array<ISharedCell>): void;
- /**
- * Move a cell.
- *
- * @param fromIndex: Index of the cell to move.
- *
- * @param toIndex: New position of the cell.
- */
- moveCell(fromIndex: number, toIndex: number): void;
- /**
- * Remove a cell.
- *
- * @param index: Index of the cell to remove.
- */
- deleteCell(index: number): void;
- /**
- * Remove a range of cells.
- *
- * @param from: The start index of the range to remove (inclusive).
- *
- * @param to: The end index of the range to remove (exclusive).
- */
- deleteCellRange(from: number, to: number): void;
- }
- /**
- * Definition of the map changes for yjs.
- */
- export type MapChange = Map<
- string,
- { action: 'add' | 'update' | 'delete'; oldValue: any; newValue: any }
- >;
- /**
- * The namespace for `ISharedNotebook` class statics.
- */
- export namespace ISharedNotebook {
- /**
- * The options used to initialize a a ISharedNotebook
- */
- export interface IOptions {
- /**
- * Wether the the undo/redo logic should be
- * considered on the full document across all cells.
- */
- disableDocumentWideUndoRedo: boolean;
- }
- }
- /**
- * The Shared kernelspec metadata.
- */
- export interface ISharedKernelspecMetadata
- extends nbformat.IKernelspecMetadata,
- IDisposable {
- [key: string]: any;
- name: string;
- display_name: string;
- }
- /**
- * The Shared language info metatdata.
- */
- export interface ISharedLanguageInfoMetadata
- extends nbformat.ILanguageInfoMetadata,
- IDisposable {
- [key: string]: any;
- name: string;
- codemirror_mode?: string | PartialJSONObject;
- file_extension?: string;
- mimetype?: string;
- pygments_lexer?: string;
- }
- // Cell Types.
- export type ISharedCell =
- | ISharedCodeCell
- | ISharedRawCell
- | ISharedMarkdownCell
- | ISharedUnrecognizedCell;
- /**
- * Cell-level metadata.
- */
- export interface ISharedBaseCellMetadata extends nbformat.IBaseCellMetadata {
- [key: string]: any;
- }
- /**
- * Implements an API for nbformat.IBaseCell.
- */
- export interface ISharedBaseCell<Metadata extends ISharedBaseCellMetadata>
- extends ISharedText {
- /**
- * Whether the cell is standalone or not.
- *
- * If the cell is standalone. It cannot be
- * inserted into a YNotebook because the Yjs model is already
- * attached to an anonymous Y.Doc instance.
- */
- readonly isStandalone: boolean;
- /**
- * The type of the cell.
- */
- readonly cell_type: nbformat.CellType;
- /**
- * The changed signal.
- */
- readonly changed: ISignal<this, CellChange<Metadata>>;
- /**
- * Create a new YCodeCell that can be inserted into a YNotebook.
- *
- * @todo clone should only be available in the specific implementations i.e. ISharedCodeCell
- */
- clone(): ISharedBaseCell<Metadata>;
- /**
- * Get Cell id.
- *
- * @returns Cell id.
- */
- getId(): string;
- /**
- * Returns the metadata associated with the notebook.
- *
- * @returns Notebook's metadata.
- */
- getMetadata(): Partial<Metadata>;
- /**
- * Sets the metadata associated with the notebook.
- *
- * @param metadata: Notebook's metadata.
- */
- setMetadata(metadata: Partial<Metadata>): void;
- /**
- * Serialize the model to JSON.
- */
- toJSON(): nbformat.IBaseCell;
- }
- /**
- * Implements an API for nbformat.ICodeCell.
- */
- export interface ISharedCodeCell
- extends ISharedBaseCell<ISharedBaseCellMetadata> {
- /**
- * The type of the cell.
- */
- cell_type: 'code';
- /**
- * The code cell's prompt number. Will be null if the cell has not been run.
- */
- execution_count: nbformat.ExecutionCount;
- /**
- * Execution, display, or stream outputs.
- */
- getOutputs(): Array<nbformat.IOutput>;
- /**
- * Add/Update output.
- */
- setOutputs(outputs: Array<nbformat.IOutput>): void;
- /**
- * Replace content from `start' to `end` with `outputs`.
- *
- * @param start: The start index of the range to replace (inclusive).
- *
- * @param end: The end index of the range to replace (exclusive).
- *
- * @param outputs: New outputs (optional).
- */
- updateOutputs(
- start: number,
- end: number,
- outputs: Array<nbformat.IOutput>
- ): void;
- /**
- * Serialize the model to JSON.
- */
- toJSON(): nbformat.IBaseCell;
- }
- /**
- * Implements an API for nbformat.IMarkdownCell.
- */
- export interface ISharedMarkdownCell
- extends ISharedBaseCell<ISharedBaseCellMetadata> {
- /**
- * String identifying the type of cell.
- */
- cell_type: 'markdown';
- /**
- * Gets the cell attachments.
- *
- * @returns The cell attachments.
- */
- getAttachments(): nbformat.IAttachments | undefined;
- /**
- * Sets the cell attachments
- *
- * @param attachments: The cell attachments.
- */
- setAttachments(attachments: nbformat.IAttachments | undefined): void;
- /**
- * Serialize the model to JSON.
- */
- toJSON(): nbformat.IMarkdownCell;
- }
- /**
- * Implements an API for nbformat.IRawCell.
- */
- export interface ISharedRawCell
- extends ISharedBaseCell<ISharedBaseCellMetadata>,
- IDisposable {
- /**
- * String identifying the type of cell.
- */
- cell_type: 'raw';
- /**
- * Gets the cell attachments.
- *
- * @returns The cell attachments.
- */
- getAttachments(): nbformat.IAttachments | undefined;
- /**
- * Sets the cell attachments
- *
- * @param attachments: The cell attachments.
- */
- setAttachments(attachments: nbformat.IAttachments | undefined): void;
- /**
- * Serialize the model to JSON.
- */
- toJSON(): nbformat.IRawCell;
- }
- /**
- * Changes on Sequence-like data are expressed as Quill-inspired deltas.
- *
- * @source https://quilljs.com/docs/delta/
- */
- export type Delta<T> = Array<{ insert?: T; delete?: number; retain?: number }>;
- /**
- * Implements an API for nbformat.IUnrecognizedCell.
- *
- * @todo Is this needed?
- */
- export interface ISharedUnrecognizedCell
- extends ISharedBaseCell<ISharedBaseCellMetadata>,
- IDisposable {
- /**
- * The type of the cell.
- */
- cell_type: 'raw';
- /**
- * Serialize the model to JSON.
- */
- toJSON(): nbformat.ICodeCell;
- }
- export type TextChange = {
- sourceChange?: Delta<string>;
- };
- /**
- * Definition of the shared Notebook changes.
- */
- export type NotebookChange = {
- cellsChange?: Delta<ISharedCell[]>;
- metadataChange?: {
- oldValue: nbformat.INotebookMetadata;
- newValue: nbformat.INotebookMetadata | undefined;
- };
- contextChange?: MapChange;
- stateChange?: Array<{
- name: string;
- oldValue: any;
- newValue: any;
- }>;
- };
- export type FileChange = {
- sourceChange?: Delta<string>;
- contextChange?: MapChange;
- stateChange?: Array<{
- name: string;
- oldValue: any;
- newValue: any;
- }>;
- };
- /**
- * Definition of the shared Cell changes.
- */
- export type CellChange<MetadataType> = {
- sourceChange?: Delta<string>;
- outputsChange?: Delta<nbformat.IOutput[]>;
- executionCountChange?: {
- oldValue: number | undefined;
- newValue: number | undefined;
- };
- metadataChange?: {
- oldValue: Partial<MetadataType> | undefined;
- newValue: Partial<MetadataType> | undefined;
- };
- };
- export type DocumentChange = {
- contextChange?: MapChange;
- stateChange?: Array<{
- name: string;
- oldValue: any;
- newValue: any;
- }>;
- };
|