|
| 1 | +import { Token } from '@lumino/coreutils'; |
| 2 | +import { getCitations, CitationRenderer } from 'citation-js-utils'; |
| 3 | +import { Contents, ContentsManager } from '@jupyterlab/services'; |
| 4 | +import { ISignal, Signal } from '@lumino/signaling'; |
| 5 | + |
| 6 | +export interface IBibliographyManager { |
| 7 | + getBibliography(): CitationRenderer | null; |
| 8 | + |
| 9 | + changed: ISignal<this, CitationRenderer | null>; |
| 10 | +} |
| 11 | + |
| 12 | +export const IBibliographyManager = new Token<IBibliographyManager>( |
| 13 | + 'jupyterlab-myst:IBibliographyManager' |
| 14 | +); |
| 15 | + |
| 16 | +export class BibliographyManager implements IBibliographyManager { |
| 17 | + private _renderer: CitationRenderer | null; |
| 18 | + private _changed = new Signal<this, CitationRenderer | null>(this); |
| 19 | + |
| 20 | + get changed(): ISignal<this, CitationRenderer | null> { |
| 21 | + return this._changed; |
| 22 | + } |
| 23 | + |
| 24 | + getBibliography(): CitationRenderer | null { |
| 25 | + return this._renderer; |
| 26 | + } |
| 27 | + |
| 28 | + constructor(contents: Contents.IManager, bibFile: string) { |
| 29 | + this._renderer = null; |
| 30 | + |
| 31 | + contents |
| 32 | + .get(bibFile) |
| 33 | + .then(async model => { |
| 34 | + this._renderer = await getCitations(model.content); |
| 35 | + this._changed.emit(this._renderer); |
| 36 | + }) |
| 37 | + .catch(); |
| 38 | + |
| 39 | + // Handle changes |
| 40 | + contents.fileChanged.connect(async (_, change) => { |
| 41 | + // On create |
| 42 | + if (change.type === 'new') { |
| 43 | + const path = (change.newValue as Contents.IModel).path; |
| 44 | + // Add model to record registry |
| 45 | + if (path === bibFile) { |
| 46 | + const model = await contents.get(path); |
| 47 | + this._renderer = await getCitations(model.content); |
| 48 | + this._changed.emit(this._renderer); |
| 49 | + } |
| 50 | + } |
| 51 | + // On rename |
| 52 | + else if (change.type === 'rename') { |
| 53 | + // Remove by path |
| 54 | + const oldPath = (change.oldValue as Contents.IModel).path; |
| 55 | + // Add under new path! |
| 56 | + const newPath = (change.newValue as Contents.IModel).path; |
| 57 | + // Add model to record registry |
| 58 | + if (newPath === bibFile) { |
| 59 | + const model = await contents.get(newPath); |
| 60 | + this._renderer = await getCitations(model.content); |
| 61 | + this._changed.emit(this._renderer); |
| 62 | + } else if (oldPath === bibFile) { |
| 63 | + this._renderer = null; |
| 64 | + this._changed.emit(this._renderer); |
| 65 | + } |
| 66 | + } |
| 67 | + // On delete |
| 68 | + else if (change.type === 'delete') { |
| 69 | + const path = (change.oldValue as Contents.IModel).path; |
| 70 | + // Add model to record registry |
| 71 | + if (path === bibFile) { |
| 72 | + this._renderer = null; |
| 73 | + this._changed.emit(this._renderer); |
| 74 | + } |
| 75 | + } |
| 76 | + // On save |
| 77 | + else { |
| 78 | + const path = (change.newValue as Contents.IModel).path; |
| 79 | + // Add model to record registry |
| 80 | + // Add model to record registry |
| 81 | + if (path === bibFile) { |
| 82 | + const model = await contents.get(path); |
| 83 | + this._renderer = await getCitations(model.content); |
| 84 | + this._changed.emit(this._renderer); |
| 85 | + } |
| 86 | + } |
| 87 | + }); |
| 88 | + } |
| 89 | +} |
0 commit comments