Skip to content

Commit 2f0c15c

Browse files
committed
Update to the modals
1 parent da16b2a commit 2f0c15c

File tree

5 files changed

+174
-104
lines changed

5 files changed

+174
-104
lines changed

src/main.ts

Lines changed: 89 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -95,38 +95,72 @@ export default class MediaDbPlugin extends Plugin {
9595

9696
async createEntryWithAdvancedSearchModal() {
9797
let results: MediaTypeModel[] = [];
98+
99+
const {advancedSearchOptions, advancedSearchModal} = await this.openMediaDbAdvancedSearchModal();
100+
if (!advancedSearchOptions) {
101+
advancedSearchModal.close();
102+
return;
103+
}
104+
105+
let apiSearchResults: MediaTypeModel[] = undefined;
98106
try {
99-
const {query, apis} = await this.openMediaDbAdvancedSearchModal();
107+
apiSearchResults = await this.apiManager.query(advancedSearchOptions.query, advancedSearchOptions.apis);
108+
} catch (e) {
109+
console.warn(e);
110+
new Notice(e.toString());
111+
advancedSearchModal.close();
112+
return;
113+
}
100114

101-
new Notice('MediaDB Searching...');
115+
advancedSearchModal.close();
102116

103-
const apiSearchResults = await this.apiManager.query(query, apis);
104-
const selectResults = await this.openMediaDbSelectModal(apiSearchResults, false);
105-
results = await this.queryDetails(selectResults);
117+
const {selectRes, selectModal} = await this.openMediaDbSelectModal(apiSearchResults, false);
118+
if (!selectRes) {
119+
selectModal.close();
120+
return;
121+
}
122+
123+
try {
124+
results = await this.queryDetails(selectRes);
106125
} catch (e) {
107126
console.warn(e);
108127
new Notice(e.toString());
128+
selectModal.close();
129+
return;
109130
}
110131

132+
selectModal.close();
133+
111134
debugLog(results);
112-
await this.createMediaDbNotes(results);
135+
if (results) {
136+
await this.createMediaDbNotes(results);
137+
}
113138
}
114139

115140
async createEntryWithIdSearchModal() {
116141
let result: MediaTypeModel = undefined;
117-
try {
118-
const {query, api} = await this.openMediaDbIdSearchModal();
119142

120-
new Notice('MediaDB Searching...');
143+
const {idSearchOptions, idSearchModal} = await this.openMediaDbIdSearchModal();
144+
if (!idSearchOptions) {
145+
idSearchModal.close();
146+
return;
147+
}
121148

122-
result = await this.apiManager.queryDetailedInfoById(query, api);
149+
try {
150+
result = await this.apiManager.queryDetailedInfoById(idSearchOptions.query, idSearchOptions.api);
123151
} catch (e) {
124152
console.warn(e);
125153
new Notice(e.toString());
154+
idSearchModal.close();
155+
return;
126156
}
127157

158+
idSearchModal.close();
159+
128160
debugLog(result);
129-
await this.createMediaDbNoteFromModel(result);
161+
if (result) {
162+
await this.createMediaDbNoteFromModel(result);
163+
}
130164
}
131165

132166
async createMediaDbNotes(models: MediaTypeModel[], attachFile?: TFile): Promise<void> {
@@ -340,23 +374,23 @@ export default class MediaDbPlugin extends Plugin {
340374
}
341375

342376
let selectedResults: MediaTypeModel[] = [];
377+
const modal = new MediaDbSearchResultModal(this, results, true);
343378
try {
344379
selectedResults = await new Promise((resolve, reject) => {
345-
const searchResultModal = new MediaDbSearchResultModal(this.app, this, results, true, (res, err) => {
380+
modal.title = `Results for \'${title}\'`;
381+
modal.setSubmitCallback(res => resolve(res));
382+
modal.setSkipCallback(() => reject(new UserCancelError('user skipped')));
383+
modal.setCloseCallback(err => {
346384
if (err) {
347-
return reject(err);
385+
reject(err);
348386
}
349-
resolve(res);
350-
}, () => {
351387
reject(new UserCancelError('user canceled'));
352-
}, () => {
353-
reject(new UserSkipError('user skipped'));
354388
});
355389

356-
searchResultModal.title = `Results for \'${title}\'`;
357-
searchResultModal.open();
390+
modal.open();
358391
});
359392
} catch (e) {
393+
modal.close();
360394
if (e instanceof UserCancelError) {
361395
erroredFiles.push({filePath: file.path, error: e.message});
362396
canceled = true;
@@ -377,6 +411,8 @@ export default class MediaDbPlugin extends Plugin {
377411

378412
const detailedResults = await this.queryDetails(selectedResults);
379413
await this.createMediaDbNotes(detailedResults, appendContent ? file : null);
414+
415+
modal.close();
380416
}
381417
}
382418

@@ -396,39 +432,53 @@ export default class MediaDbPlugin extends Plugin {
396432
const targetFile = await this.app.vault.create(filePath, fileContent);
397433
}
398434

399-
async openMediaDbAdvancedSearchModal(): Promise<{ query: string, apis: string[] }> {
400-
return await new Promise((resolve, reject) => {
401-
new MediaDbAdvancedSearchModal(this.app, this, (res, err) => {
435+
async openMediaDbAdvancedSearchModal(): Promise<{ advancedSearchOptions: { query: string, apis: string[] }, advancedSearchModal: MediaDbAdvancedSearchModal }> {
436+
const modal = new MediaDbAdvancedSearchModal(this);
437+
const res: { query: string, apis: string[] } = await new Promise((resolve, reject) => {
438+
modal.setSubmitCallback(res => resolve(res));
439+
modal.setCloseCallback(err => {
402440
if (err) {
403-
return reject(err);
441+
reject(err);
404442
}
405-
resolve(res);
406-
}).open();
443+
resolve(undefined);
444+
});
445+
446+
modal.open();
407447
});
448+
return {advancedSearchOptions: res, advancedSearchModal: modal};
408449
}
409450

410-
async openMediaDbIdSearchModal(): Promise<{ query: string, api: string }> {
411-
return await new Promise((resolve, reject) => {
412-
new MediaDbIdSearchModal(this.app, this, (res, err) => {
451+
async openMediaDbIdSearchModal(): Promise<{ idSearchOptions: { query: string, api: string }, idSearchModal: MediaDbIdSearchModal }> {
452+
const modal = new MediaDbIdSearchModal(this);
453+
const res: { query: string, api: string } = await new Promise((resolve, reject) => {
454+
modal.setSubmitCallback(res => resolve(res));
455+
modal.setCloseCallback(err => {
413456
if (err) {
414-
return reject(err);
457+
reject(err);
415458
}
416-
resolve(res);
417-
}).open();
459+
resolve(undefined);
460+
});
461+
462+
modal.open();
418463
});
464+
return {idSearchOptions: res, idSearchModal: modal};
419465
}
420466

421-
async openMediaDbSelectModal(resultsToDisplay: MediaTypeModel[], skipButton: boolean = false): Promise<MediaTypeModel[]> {
422-
return await new Promise((resolve, reject) => {
423-
new MediaDbSearchResultModal(this.app, this, resultsToDisplay, skipButton, (res, err) => {
467+
async openMediaDbSelectModal(resultsToDisplay: MediaTypeModel[], skipButton: boolean = false): Promise<{ selectRes: MediaTypeModel[], selectModal: MediaDbSearchResultModal }> {
468+
const modal = new MediaDbSearchResultModal(this, resultsToDisplay, skipButton);
469+
const res: MediaTypeModel[] = await new Promise((resolve, reject) => {
470+
modal.setSubmitCallback(res => resolve(res));
471+
modal.setSkipCallback(() => resolve([]));
472+
modal.setCloseCallback(err => {
424473
if (err) {
425-
return reject(err);
474+
reject(err);
426475
}
427-
resolve(res);
428-
}, () => {
429-
resolve([]);
430-
}).open();
476+
resolve(undefined);
477+
});
478+
479+
modal.open();
431480
});
481+
return {selectRes: res, selectModal: modal};
432482
}
433483

434484
async loadSettings() {

src/modals/MediaDbAdvancedSearchModal.ts

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {App, ButtonComponent, Component, Modal, Notice, Setting, TextComponent, ToggleComponent} from 'obsidian';
1+
import {ButtonComponent, Component, Modal, Notice, Setting, TextComponent, ToggleComponent} from 'obsidian';
22
import {MediaTypeModel} from '../models/MediaTypeModel';
33
import {debugLog} from '../utils/Utils';
44
import MediaDbPlugin from '../main';
@@ -9,19 +9,27 @@ export class MediaDbAdvancedSearchModal extends Modal {
99
plugin: MediaDbPlugin;
1010
searchBtn: ButtonComponent;
1111
selectedApis: { name: string, selected: boolean }[];
12-
onSubmit: (res: { query: string, apis: string[] }, err?: Error) => void;
12+
submitCallback?: (res: { query: string, apis: string[] }) => void;
13+
closeCallback?: (err?: Error) => void;
1314

14-
constructor(app: App, plugin: MediaDbPlugin, onSubmit?: (res: { query: string, apis: string[] }, err?: Error) => void) {
15-
super(app);
15+
constructor(plugin: MediaDbPlugin) {
16+
super(plugin.app);
1617
this.plugin = plugin;
17-
this.onSubmit = onSubmit;
1818
this.selectedApis = [];
1919
for (const api of this.plugin.apiManager.apis) {
2020
this.selectedApis.push({name: api.apiName, selected: false});
2121
}
2222
}
2323

24-
submitCallback(event: KeyboardEvent) {
24+
setSubmitCallback(submitCallback: (res: { query: string, apis: string[] }) => void): void {
25+
this.submitCallback = submitCallback;
26+
}
27+
28+
setCloseCallback(closeCallback: (err?: Error) => void): void {
29+
this.closeCallback = closeCallback;
30+
}
31+
32+
keyPressCallback(event: KeyboardEvent) {
2533
if (event.key === 'Enter') {
2634
this.search();
2735
}
@@ -44,17 +52,11 @@ export class MediaDbAdvancedSearchModal extends Modal {
4452
}
4553

4654
if (!this.isBusy) {
47-
try {
48-
this.isBusy = true;
49-
this.searchBtn.setDisabled(false);
50-
this.searchBtn.setButtonText('Searching...');
51-
52-
this.onSubmit({query: this.query, apis: apis});
53-
} catch (e) {
54-
this.onSubmit(null, e);
55-
} finally {
56-
this.close();
57-
}
55+
this.isBusy = true;
56+
this.searchBtn.setDisabled(false);
57+
this.searchBtn.setButtonText('Searching...');
58+
59+
this.submitCallback({query: this.query, apis: apis});
5860
}
5961
}
6062

@@ -68,7 +70,7 @@ export class MediaDbAdvancedSearchModal extends Modal {
6870
searchComponent.inputEl.style.width = '100%';
6971
searchComponent.setPlaceholder(placeholder);
7072
searchComponent.onChange(value => (this.query = value));
71-
searchComponent.inputEl.addEventListener('keydown', this.submitCallback.bind(this));
73+
searchComponent.inputEl.addEventListener('keydown', this.keyPressCallback.bind(this));
7274

7375
contentEl.appendChild(searchComponent.inputEl);
7476
searchComponent.inputEl.focus();
@@ -110,9 +112,9 @@ export class MediaDbAdvancedSearchModal extends Modal {
110112
}
111113

112114
onClose() {
115+
this.closeCallback();
113116
const {contentEl} = this;
114117
contentEl.empty();
115118
}
116119

117-
118120
}

src/modals/MediaDbIdSearchModal.ts

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {App, ButtonComponent, DropdownComponent, Modal, Notice, Setting, TextComponent} from 'obsidian';
1+
import {ButtonComponent, DropdownComponent, Modal, Notice, Setting, TextComponent} from 'obsidian';
22
import {MediaTypeModel} from '../models/MediaTypeModel';
33
import {debugLog} from '../utils/Utils';
44
import MediaDbPlugin from '../main';
@@ -9,16 +9,24 @@ export class MediaDbIdSearchModal extends Modal {
99
plugin: MediaDbPlugin;
1010
searchBtn: ButtonComponent;
1111
selectedApi: string;
12-
onSubmit: (res: { query: string, api: string }, err?: Error) => void;
12+
submitCallback?: (res: { query: string, api: string }, err?: Error) => void;
13+
closeCallback?: (err?: Error) => void;
1314

14-
constructor(app: App, plugin: MediaDbPlugin, onSubmit?: (res: { query: string, api: string }, err?: Error) => void) {
15-
super(app);
15+
constructor(plugin: MediaDbPlugin) {
16+
super(plugin.app);
1617
this.plugin = plugin;
17-
this.onSubmit = onSubmit;
1818
this.selectedApi = plugin.apiManager.apis[0].apiName;
1919
}
2020

21-
submitCallback(event: KeyboardEvent) {
21+
setSubmitCallback(submitCallback: (res: { query: string, api: string }, err?: Error) => void): void {
22+
this.submitCallback = submitCallback;
23+
}
24+
25+
setCloseCallback(closeCallback: (err?: Error) => void): void {
26+
this.closeCallback = closeCallback;
27+
}
28+
29+
keyPressCallback(event: KeyboardEvent) {
2230
if (event.key === 'Enter') {
2331
this.search();
2432
}
@@ -39,17 +47,11 @@ export class MediaDbIdSearchModal extends Modal {
3947
}
4048

4149
if (!this.isBusy) {
42-
try {
43-
this.isBusy = true;
44-
this.searchBtn.setDisabled(false);
45-
this.searchBtn.setButtonText('Searching...');
46-
47-
this.onSubmit({query: this.query, api: this.selectedApi});
48-
} catch (e) {
49-
this.onSubmit(null, e);
50-
} finally {
51-
this.close();
52-
}
50+
this.isBusy = true;
51+
this.searchBtn.setDisabled(false);
52+
this.searchBtn.setButtonText('Searching...');
53+
54+
this.submitCallback({query: this.query, api: this.selectedApi});
5355
}
5456
}
5557

@@ -63,7 +65,7 @@ export class MediaDbIdSearchModal extends Modal {
6365
searchComponent.inputEl.style.width = '100%';
6466
searchComponent.setPlaceholder(placeholder);
6567
searchComponent.onChange(value => (this.query = value));
66-
searchComponent.inputEl.addEventListener('keydown', this.submitCallback.bind(this));
68+
searchComponent.inputEl.addEventListener('keydown', this.keyPressCallback.bind(this));
6769

6870
contentEl.appendChild(searchComponent.inputEl);
6971
searchComponent.inputEl.focus();
@@ -98,9 +100,9 @@ export class MediaDbIdSearchModal extends Modal {
98100
}
99101

100102
onClose() {
103+
this.closeCallback();
101104
const {contentEl} = this;
102105
contentEl.empty();
103106
}
104107

105-
106108
}

0 commit comments

Comments
 (0)