Skip to content

Commit

Permalink
wip: bib manager
Browse files Browse the repository at this point in the history
  • Loading branch information
agoose77 authored and rowanc1 committed Jun 23, 2023
1 parent 277f4a0 commit ba11bf5
Show file tree
Hide file tree
Showing 4 changed files with 381 additions and 5 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
"@myst-theme/diagrams": "^0.3.2",
"@myst-theme/frontmatter": "^0.3.2",
"@myst-theme/providers": "^0.3.2",
"citation-js-utils": "^1.0.0",
"katex": "^0.15.2",
"myst-ext-card": "^1.0.0",
"myst-ext-exercise": "^1.0.0",
Expand Down
89 changes: 89 additions & 0 deletions src/bibliography.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import { Token } from '@lumino/coreutils';
import { getCitations, CitationRenderer } from 'citation-js-utils';
import { Contents, ContentsManager } from '@jupyterlab/services';
import { ISignal, Signal } from '@lumino/signaling';

export interface IBibliographyManager {
getBibliography(): CitationRenderer | null;

changed: ISignal<this, CitationRenderer | null>;
}

export const IBibliographyManager = new Token<IBibliographyManager>(
'jupyterlab-myst:IBibliographyManager'
);

export class BibliographyManager implements IBibliographyManager {
private _renderer: CitationRenderer | null;
private _changed = new Signal<this, CitationRenderer | null>(this);

get changed(): ISignal<this, CitationRenderer | null> {
return this._changed;
}

getBibliography(): CitationRenderer | null {
return this._renderer;
}

constructor(contents: Contents.IManager, bibFile: string) {
this._renderer = null;

contents
.get(bibFile)
.then(async model => {
this._renderer = await getCitations(model.content);
this._changed.emit(this._renderer);
})
.catch();

// Handle changes
contents.fileChanged.connect(async (_, change) => {
// On create
if (change.type === 'new') {
const path = (change.newValue as Contents.IModel).path;
// Add model to record registry
if (path === bibFile) {
const model = await contents.get(path);
this._renderer = await getCitations(model.content);
this._changed.emit(this._renderer);
}
}
// On rename
else if (change.type === 'rename') {
// Remove by path
const oldPath = (change.oldValue as Contents.IModel).path;
// Add under new path!
const newPath = (change.newValue as Contents.IModel).path;
// Add model to record registry
if (newPath === bibFile) {
const model = await contents.get(newPath);
this._renderer = await getCitations(model.content);
this._changed.emit(this._renderer);
} else if (oldPath === bibFile) {
this._renderer = null;
this._changed.emit(this._renderer);
}
}
// On delete
else if (change.type === 'delete') {
const path = (change.oldValue as Contents.IModel).path;
// Add model to record registry
if (path === bibFile) {
this._renderer = null;
this._changed.emit(this._renderer);
}
}
// On save
else {
const path = (change.newValue as Contents.IModel).path;
// Add model to record registry
// Add model to record registry
if (path === bibFile) {
const model = await contents.get(path);
this._renderer = await getCitations(model.content);
this._changed.emit(this._renderer);
}
}
});
}
}
23 changes: 22 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import {
} from '@jupyterlab/notebook';
import { Cell } from '@jupyterlab/cells';
import { MySTContentFactory } from './MySTContentFactory';
import { IBibliographyManager, BibliographyManager } from './bibliography';

import { IRenderMimeRegistry } from '@jupyterlab/rendermime';

import { notebookCellExecuted } from './actions';
Expand Down Expand Up @@ -73,4 +75,23 @@ const mimeRendererPlugin: JupyterFrontEndPlugin<void> = {
}
};

export default [plugin, executorPlugin, mimeRendererPlugin];
const bibPlugin: JupyterFrontEndPlugin<IBibliographyManager> = {
id: 'jupyterlab-myst:bibliography',
requires: [],
provides: IBibliographyManager,
autoStart: true,
activate: (app: JupyterFrontEnd) => {
console.log('Using jupyterlab-myst:bibliography');

const manager = new BibliographyManager(
app.serviceManager.contents,
'bibliography.bib'
);
manager.changed.connect((manager, renderer) => {
console.log(renderer, 'CHANGE');
});
return manager;
}
};

export default [plugin, executorPlugin, mimeRendererPlugin, bibPlugin];
Loading

0 comments on commit ba11bf5

Please sign in to comment.