Skip to content

Commit

Permalink
Backport PR #11064: Share notebook's metadata (#11169)
Browse files Browse the repository at this point in the history
Co-authored-by: Carlos Herrero <carlosherrerocontact@gmail.com>
  • Loading branch information
meeseeksmachine and hbcarlos committed Sep 28, 2021
1 parent 10bc307 commit 4537771
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 2 deletions.
4 changes: 4 additions & 0 deletions packages/celltags/src/tool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
33 changes: 31 additions & 2 deletions packages/notebook/src/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
IModelDB,
IObservableJSON,
IObservableList,
IObservableMap,
IObservableUndoableList,
ModelDB
} from '@jupyterlab/observables';
Expand All @@ -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.
*/
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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();
}

/**
Expand Down Expand Up @@ -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.
Expand Down
17 changes: 17 additions & 0 deletions packages/shared-models/src/ymodels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ export class YNotebook
return this._ycellMapping.get(ycell) as YCellType;
});

this.ymeta.observe(this._onMetadataChanged);
this.ystate.observe(this._onStateChanged);
}

Expand Down Expand Up @@ -237,6 +238,7 @@ export class YNotebook
*/
dispose(): void {
this.ycells.unobserve(this._onYCellsChanged);
this.ymeta.unobserve(this._onMetadataChanged);
this.ystate.unobserve(this._onStateChanged);
}

Expand Down Expand Up @@ -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));
}

Expand Down Expand Up @@ -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.
*/
Expand Down

0 comments on commit 4537771

Please sign in to comment.