Skip to content

Commit bdda814

Browse files
committed
fix for #67
1 parent fdbba31 commit bdda814

File tree

4 files changed

+14
-20
lines changed

4 files changed

+14
-20
lines changed

src/main.ts

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ export default class MediaDbPlugin extends Plugin {
232232

233233
let fileContent = await this.generateMediaDbNoteContents(mediaTypeModel, options);
234234

235-
await this.createNote(this.mediaTypeManager.getFileName(mediaTypeModel), fileContent, options.openNote);
235+
await this.createNote(this.mediaTypeManager.getFileName(mediaTypeModel), fileContent, options);
236236
} catch (e) {
237237
console.warn(e);
238238
new Notice(e.toString());
@@ -325,18 +325,18 @@ export default class MediaDbPlugin extends Plugin {
325325
*
326326
* @param fileName
327327
* @param fileContent
328-
* @param openFile
328+
* @param options
329329
*/
330-
async createNote(fileName: string, fileContent: string, openFile: boolean = false) {
331-
fileName = replaceIllegalFileNameCharactersInString(fileName);
332-
const filePath = `${this.settings.folder.replace(/\/$/, '')}/${fileName}.md`;
333-
334-
// find and possibly create the folder set in settings
335-
const folder = this.app.vault.getAbstractFileByPath(this.settings.folder);
330+
async createNote(fileName: string, fileContent: string, options: CreateNoteOptions) {
331+
// find and possibly create the folder set in settings or passed in folder
332+
const folder = options.folder ?? this.app.vault.getAbstractFileByPath(this.settings.folder);
336333
if (!folder) {
337-
await this.app.vault.createFolder(this.settings.folder.replace(/\/$/, ''));
334+
await this.app.vault.createFolder(folder.path);
338335
}
339336

337+
fileName = replaceIllegalFileNameCharactersInString(fileName);
338+
const filePath = `${folder.path}/${fileName}.md`;
339+
340340
// find and delete file with the same name
341341
const file = this.app.vault.getAbstractFileByPath(filePath);
342342
if (file) {
@@ -348,7 +348,7 @@ export default class MediaDbPlugin extends Plugin {
348348
console.debug(`MDB | created new file at ${filePath}`);
349349

350350
// open newly crated file
351-
if (openFile) {
351+
if (options.openNote) {
352352
const activeLeaf = this.app.workspace.getUnpinnedLeaf();
353353
if (!activeLeaf) {
354354
console.warn('MDB | no active leaf, not opening newly created note');
@@ -377,10 +377,7 @@ export default class MediaDbPlugin extends Plugin {
377377
throw new Error('MDB | active note is not a Media DB entry or is missing metadata');
378378
}
379379

380-
381-
382380
let oldMediaTypeModel = this.mediaTypeManager.createMediaTypeModelFromMediaType(metadata, metadata.type);
383-
384381
// console.debug(oldMediaTypeModel);
385382

386383
let newMediaTypeModel = await this.apiManager.queryDetailedInfoById(metadata.id, metadata.dataSource);
@@ -389,15 +386,14 @@ export default class MediaDbPlugin extends Plugin {
389386
}
390387

391388
newMediaTypeModel = Object.assign(oldMediaTypeModel, newMediaTypeModel.getWithOutUserData());
392-
393389
// console.debug(newMediaTypeModel);
394390

395391
// deletion not happening anymore why is this log statement still here
396392
console.debug('MDB | deleting old entry');
397393
if (onlyMetadata) {
398-
await this.createMediaDbNoteFromModel(newMediaTypeModel, {attachFile: activeFile, openNote: true});
394+
await this.createMediaDbNoteFromModel(newMediaTypeModel, {attachFile: activeFile, folder: activeFile.parent, openNote: true});
399395
} else {
400-
await this.createMediaDbNoteFromModel(newMediaTypeModel, {attachTemplate: true, openNote: true});
396+
await this.createMediaDbNoteFromModel(newMediaTypeModel, {attachTemplate: true, folder: activeFile.parent, openNote: true});
401397
}
402398

403399
}

src/modals/MediaDbSearchResultModal.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ export class MediaDbSearchResultModal extends SelectModal<MediaTypeModel> {
6262
}
6363

6464
onClose() {
65-
console.log('close');
6665
this.closeCallback();
6766
}
6867
}

src/settings/PropertyMapper.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ export class PropertyMapper {
1616
* @param obj
1717
*/
1818
convertObject(obj: object): object {
19-
console.log('test1');
20-
2119
if (!obj.hasOwnProperty('type')) {
2220
return obj;
2321
}

src/utils/Utils.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {MediaTypeModel} from '../models/MediaTypeModel';
2-
import {TFile} from 'obsidian';
2+
import {TFile, TFolder} from 'obsidian';
33

44

55
export const pluginName: string = 'obsidian-media-db-plugin';
@@ -211,6 +211,7 @@ export interface CreateNoteOptions {
211211
attachTemplate?: boolean,
212212
attachFile?: TFile,
213213
openNote?: boolean,
214+
folder?: TFolder,
214215
}
215216

216217
export function migrateObject<T extends object>(object: T, oldData: any, defaultData: T): void {

0 commit comments

Comments
 (0)