This commit is contained in:
mProjectsCode 2022-10-18 11:38:22 +02:00
parent fdbba31d84
commit bdda8146aa
4 changed files with 14 additions and 20 deletions

View file

@ -232,7 +232,7 @@ export default class MediaDbPlugin extends Plugin {
let fileContent = await this.generateMediaDbNoteContents(mediaTypeModel, options); let fileContent = await this.generateMediaDbNoteContents(mediaTypeModel, options);
await this.createNote(this.mediaTypeManager.getFileName(mediaTypeModel), fileContent, options.openNote); await this.createNote(this.mediaTypeManager.getFileName(mediaTypeModel), fileContent, options);
} catch (e) { } catch (e) {
console.warn(e); console.warn(e);
new Notice(e.toString()); new Notice(e.toString());
@ -325,18 +325,18 @@ export default class MediaDbPlugin extends Plugin {
* *
* @param fileName * @param fileName
* @param fileContent * @param fileContent
* @param openFile * @param options
*/ */
async createNote(fileName: string, fileContent: string, openFile: boolean = false) { async createNote(fileName: string, fileContent: string, options: CreateNoteOptions) {
fileName = replaceIllegalFileNameCharactersInString(fileName); // find and possibly create the folder set in settings or passed in folder
const filePath = `${this.settings.folder.replace(/\/$/, '')}/${fileName}.md`; const folder = options.folder ?? this.app.vault.getAbstractFileByPath(this.settings.folder);
// find and possibly create the folder set in settings
const folder = this.app.vault.getAbstractFileByPath(this.settings.folder);
if (!folder) { if (!folder) {
await this.app.vault.createFolder(this.settings.folder.replace(/\/$/, '')); await this.app.vault.createFolder(folder.path);
} }
fileName = replaceIllegalFileNameCharactersInString(fileName);
const filePath = `${folder.path}/${fileName}.md`;
// find and delete file with the same name // find and delete file with the same name
const file = this.app.vault.getAbstractFileByPath(filePath); const file = this.app.vault.getAbstractFileByPath(filePath);
if (file) { if (file) {
@ -348,7 +348,7 @@ export default class MediaDbPlugin extends Plugin {
console.debug(`MDB | created new file at ${filePath}`); console.debug(`MDB | created new file at ${filePath}`);
// open newly crated file // open newly crated file
if (openFile) { if (options.openNote) {
const activeLeaf = this.app.workspace.getUnpinnedLeaf(); const activeLeaf = this.app.workspace.getUnpinnedLeaf();
if (!activeLeaf) { if (!activeLeaf) {
console.warn('MDB | no active leaf, not opening newly created note'); console.warn('MDB | no active leaf, not opening newly created note');
@ -377,10 +377,7 @@ export default class MediaDbPlugin extends Plugin {
throw new Error('MDB | active note is not a Media DB entry or is missing metadata'); throw new Error('MDB | active note is not a Media DB entry or is missing metadata');
} }
let oldMediaTypeModel = this.mediaTypeManager.createMediaTypeModelFromMediaType(metadata, metadata.type); let oldMediaTypeModel = this.mediaTypeManager.createMediaTypeModelFromMediaType(metadata, metadata.type);
// console.debug(oldMediaTypeModel); // console.debug(oldMediaTypeModel);
let newMediaTypeModel = await this.apiManager.queryDetailedInfoById(metadata.id, metadata.dataSource); let newMediaTypeModel = await this.apiManager.queryDetailedInfoById(metadata.id, metadata.dataSource);
@ -389,15 +386,14 @@ export default class MediaDbPlugin extends Plugin {
} }
newMediaTypeModel = Object.assign(oldMediaTypeModel, newMediaTypeModel.getWithOutUserData()); newMediaTypeModel = Object.assign(oldMediaTypeModel, newMediaTypeModel.getWithOutUserData());
// console.debug(newMediaTypeModel); // console.debug(newMediaTypeModel);
// deletion not happening anymore why is this log statement still here // deletion not happening anymore why is this log statement still here
console.debug('MDB | deleting old entry'); console.debug('MDB | deleting old entry');
if (onlyMetadata) { if (onlyMetadata) {
await this.createMediaDbNoteFromModel(newMediaTypeModel, {attachFile: activeFile, openNote: true}); await this.createMediaDbNoteFromModel(newMediaTypeModel, {attachFile: activeFile, folder: activeFile.parent, openNote: true});
} else { } else {
await this.createMediaDbNoteFromModel(newMediaTypeModel, {attachTemplate: true, openNote: true}); await this.createMediaDbNoteFromModel(newMediaTypeModel, {attachTemplate: true, folder: activeFile.parent, openNote: true});
} }
} }

View file

@ -62,7 +62,6 @@ export class MediaDbSearchResultModal extends SelectModal<MediaTypeModel> {
} }
onClose() { onClose() {
console.log('close');
this.closeCallback(); this.closeCallback();
} }
} }

View file

@ -16,8 +16,6 @@ export class PropertyMapper {
* @param obj * @param obj
*/ */
convertObject(obj: object): object { convertObject(obj: object): object {
console.log('test1');
if (!obj.hasOwnProperty('type')) { if (!obj.hasOwnProperty('type')) {
return obj; return obj;
} }

View file

@ -1,5 +1,5 @@
import {MediaTypeModel} from '../models/MediaTypeModel'; import {MediaTypeModel} from '../models/MediaTypeModel';
import {TFile} from 'obsidian'; import {TFile, TFolder} from 'obsidian';
export const pluginName: string = 'obsidian-media-db-plugin'; export const pluginName: string = 'obsidian-media-db-plugin';
@ -211,6 +211,7 @@ export interface CreateNoteOptions {
attachTemplate?: boolean, attachTemplate?: boolean,
attachFile?: TFile, attachFile?: TFile,
openNote?: boolean, openNote?: boolean,
folder?: TFolder,
} }
export function migrateObject<T extends object>(object: T, oldData: any, defaultData: T): void { export function migrateObject<T extends object>(object: T, oldData: any, defaultData: T): void {