From 8b49d040f267c6de346e9e225a0d6d0e22029007 Mon Sep 17 00:00:00 2001 From: David Brochart Date: Tue, 17 Sep 2024 08:24:31 +0200 Subject: [PATCH] Add YDocument source getter/setter --- javascript/src/api.ts | 24 ++++++++++++++++++++++-- javascript/src/ydocument.ts | 32 ++++++++++++++++++++++++++++++++ javascript/src/ynotebook.ts | 20 +++++++++++++++++++- 3 files changed, 73 insertions(+), 3 deletions(-) diff --git a/javascript/src/api.ts b/javascript/src/api.ts index d929a46..44c395b 100644 --- a/javascript/src/api.ts +++ b/javascript/src/api.ts @@ -85,7 +85,7 @@ export interface ISharedBase extends IObservableDisposable { * 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 { +interface ISharedDocumentNoSource extends ISharedBase { /** * Document version */ @@ -117,6 +117,26 @@ export interface ISharedDocument extends ISharedBase { readonly changed: ISignal; } +/** + * 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 ISharedDocumentNoSource { + /** + * Get the document source. + * + * @returns Source. + */ + getSource(): string | JSONValue; + + /** + * Set the document source. + * + * @param value New source. + */ + setSource(value: string | JSONValue): void; +} + /** * The ISharedText interface defines models that can be bound to a text editor like CodeMirror. */ @@ -158,7 +178,7 @@ export interface ISharedText extends ISharedBase { /** * Text/Markdown/Code files are represented as ISharedFile */ -export interface ISharedFile extends ISharedDocument, ISharedText { +export interface ISharedFile extends ISharedDocumentNoSource, ISharedText { /** * The changed signal. */ diff --git a/javascript/src/ydocument.ts b/javascript/src/ydocument.ts index 96a3fae..dc0d02b 100644 --- a/javascript/src/ydocument.ts +++ b/javascript/src/ydocument.ts @@ -145,6 +145,38 @@ export abstract class YDocument } } + /** + * Get the document source + * + * @returns The source + */ + get source(): JSONValue | string { + return this.getSource(); + } + + /** + * Set the document source + * + * @param value The source to set + */ + set source(value: JSONValue | string) { + this.setSource(value); + } + + /** + * Get the document source + * + * @returns The source + */ + abstract getSource(): JSONValue | string; + + /** + * Set the document source + * + * @param value The source to set + */ + abstract setSource(value: JSONValue | string): void; + /** * Undo an operation. */ diff --git a/javascript/src/ynotebook.ts b/javascript/src/ynotebook.ts index 088b712..2e54b1f 100644 --- a/javascript/src/ynotebook.ts +++ b/javascript/src/ynotebook.ts @@ -4,7 +4,7 @@ |----------------------------------------------------------------------------*/ import type * as nbformat from '@jupyterlab/nbformat'; -import { JSONExt, PartialJSONValue } from '@lumino/coreutils'; +import { JSONExt, JSONValue, PartialJSONValue } from '@lumino/coreutils'; import { ISignal, Signal } from '@lumino/signaling'; import * as Y from 'yjs'; import type { @@ -404,6 +404,24 @@ export class YNotebook }); } + /** + * Get the notebook source + * + * @returns The notebook + */ + getSource(): JSONValue { + return this.toJSON() as JSONValue; + } + + /** + * Set the notebook source + * + * @param value The notebook + */ + setSource(value: JSONValue): void { + this.fromJSON(value as nbformat.INotebookContent); + } + /** * Override the notebook with a JSON-serialized document. *