Datalayer VS Code Extension - v0.0.4
    Preparing search index...

    Class NotebookDocument

    Represents a Jupyter notebook document in VS Code custom editor. Manages document lifecycle, content persistence, and edit tracking with support for both collaborative Datalayer notebooks and local file-based notebooks.

    This class implements different behaviors based on the URI scheme:

    • datalayer:// URIs: Collaborative notebooks with real-time sync (read-only locally)
    • file:// URIs: Local notebooks with full edit tracking and persistence
    const document = await NotebookDocument.create(uri, undefined, delegate);
    document.makeEdit({ type: "content-update", content: newContent });
    await document.save(cancellationToken);

    Hierarchy (View Summary)

    Implements

    • CustomDocument
    Index

    Constructors

    Properties

    _disposables: Disposable[] = []

    Collection of child disposables to clean up

    _documentData: Uint8Array
    _edits: NotebookEdit[] = []
    _onDidChange: EventEmitter<{ label: string; redo(): void; undo(): void }> = ...
    _onDidChangeDocument: EventEmitter<
        { content?: Uint8Array; edits: readonly NotebookEdit[] },
    > = ...
    _onDidDispose: EventEmitter<void> = ...
    _savedEdits: NotebookEdit[] = []
    _uri: Uri
    onDidChange: Event<{ label: string; redo(): void; undo(): void }> = ...
    onDidChangeContent: Event<
        { content?: Uint8Array; edits: readonly NotebookEdit[] },
    > = ...
    onDidDispose: Event<void> = ...

    Accessors

    • get documentData(): Uint8Array

      Current notebook content as binary data.

      Contains the serialized Jupyter notebook JSON structure. Updated automatically when edits are applied via makeEdit().

      Returns Uint8Array

      Binary representation of the notebook content

    • get isDisposed(): boolean

      Gets whether this instance has been disposed.

      Returns boolean

      True if disposed, false otherwise

    • get uri(): Uri

      The notebook's URI.

      The URI scheme determines the document's behavior:

      • datalayer://: Collaborative notebook with real-time synchronization
      • file://: Local notebook with traditional file persistence
      • untitled:: New unsaved notebook

      Returns Uri

      The VS Code URI for this notebook document

    Methods

    • Registers a disposable to be cleaned up when this instance is disposed.

      Type Parameters

      • T extends Disposable

      Parameters

      • value: T

        The disposable to register

      Returns T

      The registered disposable

    • Creates a backup of the notebook document.

      Saves the current document state to a backup location and returns a backup descriptor for VS Code's backup/restore system.

      Parameters

      • destination: Uri

        URI for the backup location

      • cancellation: CancellationToken

        Cancellation token for the backup operation

      Returns Promise<CustomDocumentBackup>

      Promise resolving to backup descriptor with cleanup function

    • Disposes of the notebook document and cleans up resources.

      Fires disposal events and calls the parent disposable cleanup. Should be called when the document is no longer needed to prevent memory leaks.

      Returns void

    • Records an edit operation on the notebook document.

      Behavior differs based on the URI scheme:

      • Collaborative notebooks (datalayer://): Skips edit tracking since changes are managed by the Datalayer platform's real-time synchronization
      • Local notebooks: Tracks edits for undo/redo functionality and dirty state

      For local notebooks, updates the document content and fires change events with undo/redo handlers for VS Code's edit history.

      Parameters

      Returns void

      // Apply content update to local notebook
      const notebookJson = { cells: [...], metadata: {...} };
      const edit: NotebookEdit = {
      type: "content-update",
      content: new TextEncoder().encode(JSON.stringify(notebJson))
      };

      document.makeEdit(edit);
      // Document content is updated and undo/redo is available
      // Edit on collaborative notebook (no-op for tracking)
      collaborativeDoc.makeEdit(edit);
      // Edit is ignored for history but still fires change events
    • Reverts the notebook document to its last saved state.

      Reloads content from disk and restores the edit history to the last saved state. Fires document change events to notify the UI of the content restoration.

      Parameters

      • _cancellation: CancellationToken

        Cancellation token (currently unused)

      Returns Promise<void>

    • Saves the notebook document to its original location.

      Behavior differs based on the URI scheme:

      • Collaborative notebooks (datalayer://): No-op since changes are automatically synchronized to the Datalayer platform
      • Local notebooks: Persists current content to the file system

      Parameters

      • cancellation: CancellationToken

        Cancellation token for the save operation

      Returns Promise<void>

    • Saves the notebook document to a specified location.

      Handles different scenarios based on URI schemes and target locations:

      • Collaborative to same location: No-op since changes are auto-synchronized
      • Collaborative to different location: Exports current content as local file
      • Local notebook: Retrieves fresh content from webview and saves to target

      Parameters

      • targetResource: Uri

        URI where to save the notebook

      • cancellation: CancellationToken

        Cancellation token for the save operation

      Returns Promise<void>

    • Creates a new NotebookDocument instance from a URI.

      Handles both regular notebook files and backup restoration scenarios. For backup restoration, the backupId parameter should contain the backup file URI. Supports both local file URIs and collaborative Datalayer URIs.

      Parameters

      • uri: Uri

        The notebook URI to open

      • backupId: string

        Optional backup ID for document restoration

      • delegate: NotebookDocumentDelegate

        Delegate for webview content retrieval and management

      Returns Promise<NotebookDocument>

      Promise resolving to the created notebook document instance

    • Returns a minimal valid empty Jupyter notebook. Used when opening blank/empty .ipynb files.

      Returns Uint8Array

      Binary representation of an empty notebook

    • Reads notebook content from a URI.

      Handles different URI schemes:

      • untitled: Returns empty content for new notebooks
      • Other schemes: Reads from VS Code file system

      Parameters

      • uri: Uri

        URI to read notebook content from

      Returns Promise<Uint8Array<ArrayBufferLike>>

      Binary representation of the notebook content