Further refactoring of the bulk / folder import logic

This commit is contained in:
Karn 2025-09-03 20:37:04 +01:00
parent 4798f1144b
commit 29269aa990
3 changed files with 34 additions and 20 deletions

View file

@ -26,6 +26,7 @@ import type { SearchModalOptions } from './utils/ModalHelper';
import { ModalHelper, ModalResultCode } from './utils/ModalHelper';
import type { CreateNoteOptions } from './utils/Utils';
import { dateTimeToString, markdownTable, replaceIllegalFileNameCharactersInString, unCamelCase, hasTemplaterPlugin, useTemplaterPluginInFile } from './utils/Utils';
import { BulkImportLookupMethod } from 'src/utils/BulkImportLookupMethod';
export type Metadata = Record<string, unknown>;
@ -646,7 +647,7 @@ export default class MediaDbPlugin extends Plugin {
if (!lookupValue || typeof lookupValue !== 'string') {
erroredFiles.push({ filePath: file.path, error: `metadata field '${fieldName}' not found, empty, or not a string` });
continue;
} else if (lookupMethod == 'id') {
} else if (lookupMethod === BulkImportLookupMethod.ID) {
try {
const model = await this.apiManager.queryDetailedInfoById(lookupValue, selectedAPI);
if (model) {
@ -658,7 +659,7 @@ export default class MediaDbPlugin extends Plugin {
erroredFiles.push({ filePath: file.path, error: `${e}` });
continue;
}
} else if (lookupMethod == 'title') {
} else if (lookupMethod === BulkImportLookupMethod.TITLE) {
let results: MediaTypeModel[] = [];
try {
results = await this.apiManager.query(lookupValue, [selectedAPI]);
@ -705,6 +706,9 @@ export default class MediaDbPlugin extends Plugin {
await this.createMediaDbNotes(detailedResults, appendContent ? file : undefined);
selectModal.close();
} else {
erroredFiles.push({ filePath: file.path, error: `invalid lookup type` });
continue;
}
}
}

View file

@ -2,6 +2,7 @@ import type { App, ButtonComponent } from 'obsidian';
import { DropdownComponent, Modal, Setting, TextComponent, ToggleComponent } from 'obsidian';
import type MediaDbPlugin from '../main';
import type { APIModel } from 'src/api/APIModel';
import { BulkImportLookupMethod } from 'src/utils/BulkImportLookupMethod';
export class MediaDbFolderImportModal extends Modal {
plugin: MediaDbPlugin;
@ -17,7 +18,7 @@ export class MediaDbFolderImportModal extends Modal {
this.plugin = plugin;
this.onSubmit = onSubmit;
this.selectedApi = plugin.apiManager.apis[0].apiName;
this.lookupMethod = '';
this.lookupMethod = BulkImportLookupMethod.TITLE;
this.fieldName = '';
this.appendContent = false;
}
@ -32,14 +33,16 @@ export class MediaDbFolderImportModal extends Modal {
contentEl.createEl('h2', { text: 'Import folder as Media DB entries' });
const onAPIChange = (value: string) => {
this.selectedApi = value;
};
const apiOptions = this.plugin.apiManager.apis.map((api: APIModel) => {
return { value: api.apiName, display: api.apiName };
});
this.createDropdownEl(contentEl, 'API to search', onAPIChange, apiOptions);
this.createDropdownEl(
contentEl,
'API to search',
(value: string) => {
this.selectedApi = value;
},
this.plugin.apiManager.apis.map((api: APIModel) => {
return { value: api.apiName, display: api.apiName };
}),
);
contentEl.createDiv({ cls: 'media-db-plugin-spacer' });
contentEl.createEl('h3', { text: 'Append note content to Media DB entry?' });
@ -64,14 +67,17 @@ export class MediaDbFolderImportModal extends Modal {
text: 'Choose whether to search the API by title (can return multiple results) or lookup directly using an ID (returns at most one result), and specify the name of the frontmatter property which contains the title or ID of the media.',
});
const onlookupMethodChange = (value: string) => {
this.lookupMethod = value;
};
const lookupMethodOptions = [
{ value: 'title', display: 'Title' },
{ value: 'id', display: 'ID' },
];
this.createDropdownEl(contentEl, 'Lookup media by', onlookupMethodChange, lookupMethodOptions);
this.createDropdownEl(
contentEl,
'Lookup media by',
(value: string) => {
this.lookupMethod = value;
},
[
{ value: BulkImportLookupMethod.TITLE, display: 'Title' },
{ value: BulkImportLookupMethod.ID, display: 'ID' },
],
);
contentEl.createDiv({ cls: 'media-db-plugin-spacer' });
@ -108,7 +114,7 @@ export class MediaDbFolderImportModal extends Modal {
});
}
createDropdownEl(parentEl: HTMLElement, label: string, onChange: { (value: string): void }, options: { value: string; display: string }[]): void {
createDropdownEl(parentEl: HTMLElement, label: string, onChange: (value: string) => void, options: { value: string; display: string }[]): void {
const wrapperEl = parentEl.createEl('div', { cls: 'media-db-plugin-list-wrapper' });
const labelWrapperEl = wrapperEl.createEl('div', { cls: 'media-db-plugin-list-text-wrapper' });
labelWrapperEl.createEl('span', { text: label, cls: 'media-db-plugin-list-text' });

View file

@ -0,0 +1,4 @@
export enum BulkImportLookupMethod {
ID = 'id',
TITLE = 'title',
}