Update entry command
This commit is contained in:
parent
a2b6847325
commit
16299feced
8 changed files with 62 additions and 3 deletions
42
src/main.ts
42
src/main.ts
|
|
@ -37,6 +37,12 @@ export default class MediaDbPlugin extends Plugin {
|
|||
callback: () => this.createMediaDbNote(this.openMediaDbIdSearchModal.bind(this)),
|
||||
});
|
||||
|
||||
this.addCommand({
|
||||
id: 'update-media-db-note',
|
||||
name: 'Update the open note, if it is a Media DB entry.',
|
||||
callback: () => this.updateActiveNote(),
|
||||
});
|
||||
|
||||
// register the settings tab
|
||||
this.addSettingTab(new MediaDbSettingTab(this.app, this));
|
||||
|
||||
|
|
@ -53,10 +59,18 @@ export default class MediaDbPlugin extends Plugin {
|
|||
async createMediaDbNote(modal: () => Promise<MediaTypeModel>): Promise<void> {
|
||||
try {
|
||||
let data: MediaTypeModel = await modal();
|
||||
console.log('MDB | Creating new note...');
|
||||
|
||||
data = await this.apiManager.queryDetailedInfo(data);
|
||||
|
||||
await this.createMediaDbNoteFromModel(data);
|
||||
} catch (e) {
|
||||
console.warn(e);
|
||||
new Notice(e.toString());
|
||||
}
|
||||
}
|
||||
|
||||
async createMediaDbNoteFromModel(data: MediaTypeModel): Promise<void> {
|
||||
try {
|
||||
console.log('MDB | Creating new note...');
|
||||
// console.log(data);
|
||||
|
||||
let fileContent = `---\n${data.toMetaData()}---\n`;
|
||||
|
|
@ -86,6 +100,8 @@ export default class MediaDbPlugin extends Plugin {
|
|||
|
||||
const fileName = replaceIllegalFileNameCharactersInString(data.getFileName());
|
||||
const filePath = `${this.settings.folder.replace(/\/$/, '')}/${fileName}.md`;
|
||||
|
||||
await this.app.vault.delete(this.app.vault.getAbstractFileByPath(filePath));
|
||||
const targetFile = await this.app.vault.create(filePath, fileContent);
|
||||
|
||||
// open file
|
||||
|
|
@ -95,6 +111,7 @@ export default class MediaDbPlugin extends Plugin {
|
|||
return;
|
||||
}
|
||||
await activeLeaf.openFile(targetFile, {state: {mode: 'source'}});
|
||||
|
||||
} catch (e) {
|
||||
console.warn(e);
|
||||
new Notice(e.toString());
|
||||
|
|
@ -122,6 +139,27 @@ export default class MediaDbPlugin extends Plugin {
|
|||
}));
|
||||
}
|
||||
|
||||
async updateActiveNote() {
|
||||
const activeLeaf: TFile = this.app.workspace.getActiveFile();
|
||||
if (!activeLeaf.name) return;
|
||||
|
||||
let metadata = this.app.metadataCache.getFileCache(activeLeaf).frontmatter;
|
||||
|
||||
if (!metadata.type || !metadata.dataSource || !metadata.id) {
|
||||
throw new Error('MDB | active note is not a Media DB entry or is missing metadata');
|
||||
}
|
||||
|
||||
const newMetadata = await this.apiManager.queryDetailedInfo({dataSource: metadata.dataSource, id: metadata.id} as MediaTypeModel);
|
||||
|
||||
if (!newMetadata) {
|
||||
return;
|
||||
}
|
||||
|
||||
console.log('MDB | deleting old entry');
|
||||
await this.app.vault.delete(activeLeaf);
|
||||
await this.createMediaDbNoteFromModel(newMetadata);
|
||||
}
|
||||
|
||||
async loadSettings() {
|
||||
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@ import {mediaDbTag} from '../utils/Utils';
|
|||
|
||||
export class GameModel extends MediaTypeModel {
|
||||
type: string;
|
||||
subType: string;
|
||||
title: string;
|
||||
englishTitle: string;
|
||||
year: string;
|
||||
|
|
@ -27,6 +28,8 @@ export class GameModel extends MediaTypeModel {
|
|||
super();
|
||||
|
||||
Object.assign(this, obj);
|
||||
|
||||
this.type = 'game';
|
||||
}
|
||||
|
||||
toMetaData(): string {
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
export abstract class MediaTypeModel {
|
||||
type: string;
|
||||
subType: string;
|
||||
title: string;
|
||||
englishTitle: string;
|
||||
year: string;
|
||||
|
|
@ -12,4 +13,5 @@ export abstract class MediaTypeModel {
|
|||
abstract getFileName(): string;
|
||||
|
||||
abstract getTags(): string[];
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@ import {mediaDbTag} from '../utils/Utils';
|
|||
|
||||
export class MovieModel extends MediaTypeModel {
|
||||
type: string;
|
||||
subType: string;
|
||||
title: string;
|
||||
englishTitle: string;
|
||||
year: string;
|
||||
|
|
@ -30,6 +31,8 @@ export class MovieModel extends MediaTypeModel {
|
|||
super();
|
||||
|
||||
Object.assign(this, obj);
|
||||
|
||||
this.type = 'movie';
|
||||
}
|
||||
|
||||
toMetaData(): string {
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@ import {mediaDbTag} from '../utils/Utils';
|
|||
|
||||
export class MusicReleaseModel extends MediaTypeModel {
|
||||
type: string;
|
||||
subType: string;
|
||||
title: string;
|
||||
englishTitle: string;
|
||||
year: string;
|
||||
|
|
@ -14,7 +15,6 @@ export class MusicReleaseModel extends MediaTypeModel {
|
|||
|
||||
genres: string[];
|
||||
artists: string[];
|
||||
subType: string;
|
||||
rating: number;
|
||||
|
||||
personalRating: number;
|
||||
|
|
@ -23,6 +23,8 @@ export class MusicReleaseModel extends MediaTypeModel {
|
|||
super();
|
||||
|
||||
Object.assign(this, obj);
|
||||
|
||||
this.type = 'musicRelease';
|
||||
}
|
||||
|
||||
toMetaData(): string {
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@ import {mediaDbTag} from '../utils/Utils';
|
|||
|
||||
export class SeriesModel extends MediaTypeModel {
|
||||
type: string;
|
||||
subType: string;
|
||||
title: string;
|
||||
englishTitle: string;
|
||||
year: string;
|
||||
|
|
@ -33,6 +34,8 @@ export class SeriesModel extends MediaTypeModel {
|
|||
super();
|
||||
|
||||
Object.assign(this, obj);
|
||||
|
||||
this.type = 'series';
|
||||
}
|
||||
|
||||
toMetaData(): string {
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@ import {mediaDbTag} from '../utils/Utils';
|
|||
|
||||
export class WikiModel extends MediaTypeModel {
|
||||
type: string;
|
||||
subType: string;
|
||||
title: string;
|
||||
englishTitle: string;
|
||||
year: string;
|
||||
|
|
@ -21,6 +22,8 @@ export class WikiModel extends MediaTypeModel {
|
|||
super();
|
||||
|
||||
Object.assign(this, obj);
|
||||
|
||||
this.type = 'wiki';
|
||||
}
|
||||
|
||||
toMetaData(): string {
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
import {MediaTypeModel} from '../models/MediaTypeModel';
|
||||
import {TFile} from 'obsidian';
|
||||
|
||||
|
||||
export const pluginName: string = 'obsidian-media-db-plugin';
|
||||
|
|
@ -85,3 +86,7 @@ function traverseMetaData(path: Array<string>, mediaTypeModel: MediaTypeModel):
|
|||
|
||||
return o;
|
||||
}
|
||||
|
||||
export function updateNote(file: TFile) {
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue