Skip to content

Commit 3f32311

Browse files
Add YDocument source getter/setter (#273)
1 parent dd34d8d commit 3f32311

File tree

3 files changed

+73
-3
lines changed

3 files changed

+73
-3
lines changed

javascript/src/api.ts

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ export interface ISharedBase extends IObservableDisposable {
8585
* Implement an API for Context information on the shared information.
8686
* This is used by, for example, docregistry to share the file-path of the edited content.
8787
*/
88-
export interface ISharedDocument extends ISharedBase {
88+
interface ISharedDocumentNoSource extends ISharedBase {
8989
/**
9090
* Document version
9191
*/
@@ -117,6 +117,26 @@ export interface ISharedDocument extends ISharedBase {
117117
readonly changed: ISignal<this, DocumentChange>;
118118
}
119119

120+
/**
121+
* Implement an API for Context information on the shared information.
122+
* This is used by, for example, docregistry to share the file-path of the edited content.
123+
*/
124+
export interface ISharedDocument extends ISharedDocumentNoSource {
125+
/**
126+
* Get the document source.
127+
*
128+
* @returns Source.
129+
*/
130+
getSource(): string | JSONValue;
131+
132+
/**
133+
* Set the document source.
134+
*
135+
* @param value New source.
136+
*/
137+
setSource(value: string | JSONValue): void;
138+
}
139+
120140
/**
121141
* The ISharedText interface defines models that can be bound to a text editor like CodeMirror.
122142
*/
@@ -158,7 +178,7 @@ export interface ISharedText extends ISharedBase {
158178
/**
159179
* Text/Markdown/Code files are represented as ISharedFile
160180
*/
161-
export interface ISharedFile extends ISharedDocument, ISharedText {
181+
export interface ISharedFile extends ISharedDocumentNoSource, ISharedText {
162182
/**
163183
* The changed signal.
164184
*/

javascript/src/ydocument.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,38 @@ export abstract class YDocument<T extends DocumentChange>
145145
}
146146
}
147147

148+
/**
149+
* Get the document source
150+
*
151+
* @returns The source
152+
*/
153+
get source(): JSONValue | string {
154+
return this.getSource();
155+
}
156+
157+
/**
158+
* Set the document source
159+
*
160+
* @param value The source to set
161+
*/
162+
set source(value: JSONValue | string) {
163+
this.setSource(value);
164+
}
165+
166+
/**
167+
* Get the document source
168+
*
169+
* @returns The source
170+
*/
171+
abstract getSource(): JSONValue | string;
172+
173+
/**
174+
* Set the document source
175+
*
176+
* @param value The source to set
177+
*/
178+
abstract setSource(value: JSONValue | string): void;
179+
148180
/**
149181
* Undo an operation.
150182
*/

javascript/src/ynotebook.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
|----------------------------------------------------------------------------*/
55

66
import type * as nbformat from '@jupyterlab/nbformat';
7-
import { JSONExt, PartialJSONValue } from '@lumino/coreutils';
7+
import { JSONExt, JSONValue, PartialJSONValue } from '@lumino/coreutils';
88
import { ISignal, Signal } from '@lumino/signaling';
99
import * as Y from 'yjs';
1010
import type {
@@ -404,6 +404,24 @@ export class YNotebook
404404
});
405405
}
406406

407+
/**
408+
* Get the notebook source
409+
*
410+
* @returns The notebook
411+
*/
412+
getSource(): JSONValue {
413+
return this.toJSON() as JSONValue;
414+
}
415+
416+
/**
417+
* Set the notebook source
418+
*
419+
* @param value The notebook
420+
*/
421+
setSource(value: JSONValue): void {
422+
this.fromJSON(value as nbformat.INotebookContent);
423+
}
424+
407425
/**
408426
* Override the notebook with a JSON-serialized document.
409427
*

0 commit comments

Comments
 (0)