Przeglądaj źródła

Backport PR #11064: Share notebook's metadata (#11169)

Co-authored-by: Carlos Herrero <carlosherrerocontact@gmail.com>
MeeseeksMachine 3 lat temu
rodzic
commit
45377719fc

+ 4 - 0
packages/celltags/src/tool.ts

@@ -209,6 +209,10 @@ export class TagTool extends NotebookTools.Tool {
         this.refreshTags();
         this.loadActiveTags();
       });
+      this.tracker.currentWidget.content.activeCellChanged.connect(() => {
+        this.refreshTags();
+        this.loadActiveTags();
+      });
     }
     this.tracker.currentChanged.connect(() => {
       this.refreshTags();

+ 31 - 2
packages/notebook/src/model.ts

@@ -19,6 +19,7 @@ import {
   IModelDB,
   IObservableJSON,
   IObservableList,
+  IObservableMap,
   IObservableUndoableList,
   ModelDB
 } from '@jupyterlab/observables';
@@ -28,10 +29,12 @@ import {
   nullTranslator,
   TranslationBundle
 } from '@jupyterlab/translation';
-import { UUID } from '@lumino/coreutils';
+import { JSONObject, ReadonlyPartialJSONValue, UUID } from '@lumino/coreutils';
 import { ISignal, Signal } from '@lumino/signaling';
 import { CellList } from './celllist';
 
+const UNSHARED_KEYS = ['kernelspec', 'language_info'];
+
 /**
  * The definition of a model object for a notebook widget.
  */
@@ -105,7 +108,7 @@ export class NotebookModel implements INotebookModel {
       metadata.set('language_info', { name });
     }
     this._ensureMetadata();
-    metadata.changed.connect(this.triggerContentChange, this);
+    metadata.changed.connect(this._onMetadataChanged, this);
     this._deletedCells = [];
 
     this.sharedModel.changed.connect(this._onStateChanged, this);
@@ -428,6 +431,27 @@ close the notebook without saving it.`,
         this.triggerStateChange(value);
       });
     }
+
+    if (changes.metadataChange) {
+      const metadata = changes.metadataChange.newValue as JSONObject;
+      this._modelDBMutex(() => {
+        Object.entries(metadata).forEach(([key, value]) => {
+          this.metadata.set(key, value);
+        });
+      });
+    }
+  }
+
+  private _onMetadataChanged(
+    metadata: IObservableJSON,
+    change: IObservableMap.IChangedArgs<ReadonlyPartialJSONValue | undefined>
+  ): void {
+    if (!UNSHARED_KEYS.includes(change.key)) {
+      this._modelDBMutex(() => {
+        this.sharedModel.updateMetadata(metadata.toJSON());
+      });
+    }
+    this.triggerContentChange();
   }
 
   /**
@@ -475,6 +499,11 @@ close the notebook without saving it.`,
    */
   readonly sharedModel = models.YNotebook.create() as models.ISharedNotebook;
 
+  /**
+   * A mutex to update the shared model.
+   */
+  protected readonly _modelDBMutex = models.createMutex();
+
   /**
    * The underlying `IModelDB` instance in which model
    * data is stored.

+ 17 - 0
packages/shared-models/src/ymodels.ts

@@ -209,6 +209,7 @@ export class YNotebook
       return this._ycellMapping.get(ycell) as YCellType;
     });
 
+    this.ymeta.observe(this._onMetadataChanged);
     this.ystate.observe(this._onStateChanged);
   }
 
@@ -237,6 +238,7 @@ export class YNotebook
    */
   dispose(): void {
     this.ycells.unobserve(this._onYCellsChanged);
+    this.ymeta.unobserve(this._onMetadataChanged);
     this.ystate.unobserve(this._onStateChanged);
   }
 
@@ -344,6 +346,7 @@ export class YNotebook
    * @param value: Metadata's attribute to update.
    */
   updateMetadata(value: Partial<nbformat.INotebookMetadata>): void {
+    // TODO: Maybe modify only attributes instead of replacing the whole metadata?
     this.ymeta.set('metadata', Object.assign({}, this.getMetadata(), value));
   }
 
@@ -401,6 +404,20 @@ export class YNotebook
     });
   };
 
+  /**
+   * Handle a change to the ystate.
+   */
+  private _onMetadataChanged = (event: Y.YMapEvent<any>) => {
+    if (event.keysChanged.has('metadata')) {
+      const change = event.changes.keys.get('metadata');
+      const metadataChange = {
+        oldValue: change?.oldValue ? change!.oldValue : undefined,
+        newValue: this.getMetadata()
+      };
+      this._changed.emit({ metadataChange });
+    }
+  };
+
   /**
    * Handle a change to the ystate.
    */