Further refactoring of the bulk / folder import logic
This commit is contained in:
parent
4798f1144b
commit
29269aa990
3 changed files with 34 additions and 20 deletions
|
|
@ -26,6 +26,7 @@ import type { SearchModalOptions } from './utils/ModalHelper';
|
||||||
import { ModalHelper, ModalResultCode } from './utils/ModalHelper';
|
import { ModalHelper, ModalResultCode } from './utils/ModalHelper';
|
||||||
import type { CreateNoteOptions } from './utils/Utils';
|
import type { CreateNoteOptions } from './utils/Utils';
|
||||||
import { dateTimeToString, markdownTable, replaceIllegalFileNameCharactersInString, unCamelCase, hasTemplaterPlugin, useTemplaterPluginInFile } 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>;
|
export type Metadata = Record<string, unknown>;
|
||||||
|
|
||||||
|
|
@ -646,7 +647,7 @@ export default class MediaDbPlugin extends Plugin {
|
||||||
if (!lookupValue || typeof lookupValue !== 'string') {
|
if (!lookupValue || typeof lookupValue !== 'string') {
|
||||||
erroredFiles.push({ filePath: file.path, error: `metadata field '${fieldName}' not found, empty, or not a string` });
|
erroredFiles.push({ filePath: file.path, error: `metadata field '${fieldName}' not found, empty, or not a string` });
|
||||||
continue;
|
continue;
|
||||||
} else if (lookupMethod == 'id') {
|
} else if (lookupMethod === BulkImportLookupMethod.ID) {
|
||||||
try {
|
try {
|
||||||
const model = await this.apiManager.queryDetailedInfoById(lookupValue, selectedAPI);
|
const model = await this.apiManager.queryDetailedInfoById(lookupValue, selectedAPI);
|
||||||
if (model) {
|
if (model) {
|
||||||
|
|
@ -658,7 +659,7 @@ export default class MediaDbPlugin extends Plugin {
|
||||||
erroredFiles.push({ filePath: file.path, error: `${e}` });
|
erroredFiles.push({ filePath: file.path, error: `${e}` });
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
} else if (lookupMethod == 'title') {
|
} else if (lookupMethod === BulkImportLookupMethod.TITLE) {
|
||||||
let results: MediaTypeModel[] = [];
|
let results: MediaTypeModel[] = [];
|
||||||
try {
|
try {
|
||||||
results = await this.apiManager.query(lookupValue, [selectedAPI]);
|
results = await this.apiManager.query(lookupValue, [selectedAPI]);
|
||||||
|
|
@ -705,6 +706,9 @@ export default class MediaDbPlugin extends Plugin {
|
||||||
await this.createMediaDbNotes(detailedResults, appendContent ? file : undefined);
|
await this.createMediaDbNotes(detailedResults, appendContent ? file : undefined);
|
||||||
|
|
||||||
selectModal.close();
|
selectModal.close();
|
||||||
|
} else {
|
||||||
|
erroredFiles.push({ filePath: file.path, error: `invalid lookup type` });
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@ import type { App, ButtonComponent } from 'obsidian';
|
||||||
import { DropdownComponent, Modal, Setting, TextComponent, ToggleComponent } from 'obsidian';
|
import { DropdownComponent, Modal, Setting, TextComponent, ToggleComponent } from 'obsidian';
|
||||||
import type MediaDbPlugin from '../main';
|
import type MediaDbPlugin from '../main';
|
||||||
import type { APIModel } from 'src/api/APIModel';
|
import type { APIModel } from 'src/api/APIModel';
|
||||||
|
import { BulkImportLookupMethod } from 'src/utils/BulkImportLookupMethod';
|
||||||
|
|
||||||
export class MediaDbFolderImportModal extends Modal {
|
export class MediaDbFolderImportModal extends Modal {
|
||||||
plugin: MediaDbPlugin;
|
plugin: MediaDbPlugin;
|
||||||
|
|
@ -17,7 +18,7 @@ export class MediaDbFolderImportModal extends Modal {
|
||||||
this.plugin = plugin;
|
this.plugin = plugin;
|
||||||
this.onSubmit = onSubmit;
|
this.onSubmit = onSubmit;
|
||||||
this.selectedApi = plugin.apiManager.apis[0].apiName;
|
this.selectedApi = plugin.apiManager.apis[0].apiName;
|
||||||
this.lookupMethod = '';
|
this.lookupMethod = BulkImportLookupMethod.TITLE;
|
||||||
this.fieldName = '';
|
this.fieldName = '';
|
||||||
this.appendContent = false;
|
this.appendContent = false;
|
||||||
}
|
}
|
||||||
|
|
@ -32,14 +33,16 @@ export class MediaDbFolderImportModal extends Modal {
|
||||||
|
|
||||||
contentEl.createEl('h2', { text: 'Import folder as Media DB entries' });
|
contentEl.createEl('h2', { text: 'Import folder as Media DB entries' });
|
||||||
|
|
||||||
const onAPIChange = (value: string) => {
|
this.createDropdownEl(
|
||||||
|
contentEl,
|
||||||
|
'API to search',
|
||||||
|
(value: string) => {
|
||||||
this.selectedApi = value;
|
this.selectedApi = value;
|
||||||
};
|
},
|
||||||
const apiOptions = this.plugin.apiManager.apis.map((api: APIModel) => {
|
this.plugin.apiManager.apis.map((api: APIModel) => {
|
||||||
return { value: api.apiName, display: api.apiName };
|
return { value: api.apiName, display: api.apiName };
|
||||||
});
|
}),
|
||||||
|
);
|
||||||
this.createDropdownEl(contentEl, 'API to search', onAPIChange, apiOptions);
|
|
||||||
|
|
||||||
contentEl.createDiv({ cls: 'media-db-plugin-spacer' });
|
contentEl.createDiv({ cls: 'media-db-plugin-spacer' });
|
||||||
contentEl.createEl('h3', { text: 'Append note content to Media DB entry?' });
|
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.',
|
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.createDropdownEl(
|
||||||
|
contentEl,
|
||||||
|
'Lookup media by',
|
||||||
|
(value: string) => {
|
||||||
this.lookupMethod = value;
|
this.lookupMethod = value;
|
||||||
};
|
},
|
||||||
const lookupMethodOptions = [
|
[
|
||||||
{ value: 'title', display: 'Title' },
|
{ value: BulkImportLookupMethod.TITLE, display: 'Title' },
|
||||||
{ value: 'id', display: 'ID' },
|
{ value: BulkImportLookupMethod.ID, display: 'ID' },
|
||||||
];
|
],
|
||||||
this.createDropdownEl(contentEl, 'Lookup media by', onlookupMethodChange, lookupMethodOptions);
|
);
|
||||||
|
|
||||||
contentEl.createDiv({ cls: 'media-db-plugin-spacer' });
|
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 wrapperEl = parentEl.createEl('div', { cls: 'media-db-plugin-list-wrapper' });
|
||||||
const labelWrapperEl = wrapperEl.createEl('div', { cls: 'media-db-plugin-list-text-wrapper' });
|
const labelWrapperEl = wrapperEl.createEl('div', { cls: 'media-db-plugin-list-text-wrapper' });
|
||||||
labelWrapperEl.createEl('span', { text: label, cls: 'media-db-plugin-list-text' });
|
labelWrapperEl.createEl('span', { text: label, cls: 'media-db-plugin-list-text' });
|
||||||
|
|
|
||||||
4
src/utils/BulkImportLookupMethod.ts
Normal file
4
src/utils/BulkImportLookupMethod.ts
Normal file
|
|
@ -0,0 +1,4 @@
|
||||||
|
export enum BulkImportLookupMethod {
|
||||||
|
ID = 'id',
|
||||||
|
TITLE = 'title',
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue