Skip to content

Commit b1e3478

Browse files
committed
Add YDocument source getter/setter
1 parent c1c146d commit b1e3478

File tree

3 files changed

+74
-3
lines changed

3 files changed

+74
-3
lines changed

javascript/src/api.ts

Lines changed: 23 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,27 @@ 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+
/**
127+
* Get the document source.
128+
*
129+
* @returns Source.
130+
*/
131+
getSource(): string | JSONValue;
132+
133+
/**
134+
* Set the document source.
135+
*
136+
* @param value New source.
137+
*/
138+
setSource(value: string | JSONValue): void;
139+
}
140+
120141
/**
121142
* The ISharedText interface defines models that can be bound to a text editor like CodeMirror.
122143
*/
@@ -158,7 +179,7 @@ export interface ISharedText extends ISharedBase {
158179
/**
159180
* Text/Markdown/Code files are represented as ISharedFile
160181
*/
161-
export interface ISharedFile extends ISharedDocument, ISharedText {
182+
export interface ISharedFile extends ISharedDocumentNoSource, ISharedText {
162183
/**
163184
* The changed signal.
164185
*/

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)