File tree Expand file tree Collapse file tree 3 files changed +90
-0
lines changed Expand file tree Collapse file tree 3 files changed +90
-0
lines changed Original file line number Diff line number Diff line change @@ -295,6 +295,13 @@ export interface ISharedNotebook extends ISharedDocument {
295295 */
296296 deleteCellRange ( from : number , to : number ) : void ;
297297
298+ /**
299+ * Override the notebook with a JSON-serialized document.
300+ *
301+ * @param value The notebook
302+ */
303+ fromJSON ( value : nbformat . INotebookContent ) : void ;
304+
298305 /**
299306 * Serialize the model to JSON.
300307 */
Original file line number Diff line number Diff line change @@ -1524,6 +1524,34 @@ export class YNotebook
15241524 this . ymeta . set ( 'metadata' , { ...this . getMetadata ( ) , ...value } ) ;
15251525 }
15261526
1527+ /**
1528+ * Override the notebook with a JSON-serialized document.
1529+ *
1530+ * @param value The notebook
1531+ */
1532+ fromJSON ( value : nbformat . INotebookContent ) : void {
1533+ this . transact ( ( ) => {
1534+ this . nbformat = value . nbformat ;
1535+ this . nbformat_minor = value . nbformat_minor ;
1536+
1537+ const metadata = value . metadata ;
1538+ if ( metadata [ 'orig_nbformat' ] !== undefined ) {
1539+ delete metadata [ 'orig_nbformat' ] ;
1540+ }
1541+ this . metadata = metadata ;
1542+
1543+ const useId = value . nbformat === 4 && value . nbformat_minor >= 5 ;
1544+ const ycells = value . cells . map ( cell => {
1545+ if ( ! useId ) {
1546+ delete cell . id ;
1547+ }
1548+ return cell ;
1549+ } ) ;
1550+ this . insertCells ( this . cells . length , ycells ) ;
1551+ this . deleteCellRange ( 0 , this . cells . length ) ;
1552+ } ) ;
1553+ }
1554+
15271555 /**
15281556 * Serialize the model to JSON.
15291557 */
Original file line number Diff line number Diff line change @@ -321,6 +321,61 @@ describe('@jupyter-notebook/ydoc', () => {
321321 ] ) ;
322322 } ) ;
323323 } ) ;
324+
325+ describe ( '#fromJSON' , ( ) => {
326+ it ( 'should load a serialize notebook' , ( ) => {
327+ const notebook = new YNotebook ( ) ;
328+ notebook . fromJSON ( {
329+ cells : [ ] ,
330+ metadata : {
331+ dummy : 42
332+ } ,
333+ nbformat : 4 ,
334+ nbformat_minor : 5
335+ } ) ;
336+
337+ expect ( notebook . cells ) . toHaveLength ( 0 ) ;
338+ expect ( notebook . nbformat ) . toEqual ( 4 ) ;
339+ expect ( notebook . nbformat_minor ) . toEqual ( 5 ) ;
340+ } ) ;
341+
342+ it ( 'should remove orig_nbformat' , ( ) => {
343+ const notebook = new YNotebook ( ) ;
344+ notebook . fromJSON ( {
345+ cells : [ ] ,
346+ metadata : {
347+ dummy : 42 ,
348+ orig_nbformat : 3
349+ } ,
350+ nbformat : 4 ,
351+ nbformat_minor : 5
352+ } ) ;
353+
354+ expect ( notebook . getMetadata ( 'orig_nbformat' ) ) . toEqual ( undefined ) ;
355+ } ) ;
356+
357+ it ( 'should remove cell id for version <4.5' , ( ) => {
358+ const notebook = new YNotebook ( ) ;
359+ notebook . fromJSON ( {
360+ cells : [
361+ {
362+ cell_type : 'code' ,
363+ id : 'first-cell' ,
364+ source : '' ,
365+ metadata : { }
366+ }
367+ ] ,
368+ metadata : {
369+ dummy : 42
370+ } ,
371+ nbformat : 4 ,
372+ nbformat_minor : 4
373+ } ) ;
374+
375+ expect ( notebook . cells ) . toHaveLength ( 1 ) ;
376+ expect ( notebook . cells [ 0 ] . id ) . not . toEqual ( 'first-cell' ) ;
377+ } ) ;
378+ } ) ;
324379 } ) ;
325380
326381 describe ( 'YCell standalone' , ( ) => {
You can’t perform that action at this time.
0 commit comments