Skip to content

Commit 5aa9108

Browse files
committed
Modal helper
1 parent 25b899d commit 5aa9108

File tree

3 files changed

+166
-141
lines changed

3 files changed

+166
-141
lines changed

src/api/apis/BoardGameGeekAPI.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,17 +79,16 @@ export class BoardGameGeekAPI extends APIModel {
7979
const genres = Array.from(boardgame.querySelectorAll('boardgamecategory')).map(n => n!.textContent!);
8080

8181
const model = new BoardGameModel({
82-
type: MediaType.BoardGame,
8382
title,
8483
englishTitle: title,
8584
year: year === '0' ? '' : year,
8685
dataSource: this.apiName,
8786
url: `https://boardgamegeek.com/boardgame/${id}`,
88-
id,
87+
id: id,
8988

90-
genres,
91-
onlineRating,
92-
image,
89+
genres: genres,
90+
onlineRating: onlineRating,
91+
image: image,
9392
released: true,
9493

9594
userData: {

src/main.ts

Lines changed: 27 additions & 136 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,8 @@ import {APIManager} from './api/APIManager';
44
import {MediaTypeModel} from './models/MediaTypeModel';
55
import {dateTimeToString, markdownTable, replaceIllegalFileNameCharactersInString, UserCancelError, UserSkipError} from './utils/Utils';
66
import {OMDbAPI} from './api/apis/OMDbAPI';
7-
import {MediaDbAdvancedSearchModal} from './modals/MediaDbAdvancedSearchModal';
87
import {MediaDbSearchResultModal} from './modals/MediaDbSearchResultModal';
98
import {MALAPI} from './api/apis/MALAPI';
10-
import {MediaDbIdSearchModal} from './modals/MediaDbIdSearchModal';
119
import {WikipediaAPI} from './api/apis/WikipediaAPI';
1210
import {MusicBrainzAPI} from './api/apis/MusicBrainzAPI';
1311
import {MediaTypeManager} from './utils/MediaTypeManager';
@@ -17,12 +15,14 @@ import {PropertyMapper} from './settings/PropertyMapper';
1715
import {YAMLConverter} from './utils/YAMLConverter';
1816
import {MediaDbFolderImportModal} from './modals/MediaDbFolderImportModal';
1917
import {PropertyMapping, PropertyMappingModel} from './settings/PropertyMapping';
18+
import {ModalHelper} from './utils/ModalHelper';
2019

2120
export default class MediaDbPlugin extends Plugin {
2221
settings: MediaDbPluginSettings;
2322
apiManager: APIManager;
2423
mediaTypeManager: MediaTypeManager;
2524
modelPropertyMapper: PropertyMapper;
25+
modalHelper: ModalHelper;
2626

2727
frontMatterRexExpPattern: string = '^(---)\\n[\\s\\S]*?\\n---';
2828

@@ -39,6 +39,7 @@ export default class MediaDbPlugin extends Plugin {
3939

4040
this.mediaTypeManager = new MediaTypeManager();
4141
this.modelPropertyMapper = new PropertyMapper(this);
42+
this.modalHelper = new ModalHelper(this);
4243

4344
await this.loadSettings();
4445
// register the settings tab
@@ -91,7 +92,7 @@ export default class MediaDbPlugin extends Plugin {
9192
// register link insert command
9293
this.addCommand({
9394
id: 'add-media-db-link',
94-
name: 'Add a link.',
95+
name: 'Insert link',
9596
checkCallback: (checking: boolean) => {
9697
if (!this.app.workspace.getActiveFile()) {
9798
return false;
@@ -111,49 +112,24 @@ export default class MediaDbPlugin extends Plugin {
111112
* - maybe custom link syntax
112113
*/
113114
async createLinkWithSearchModal() {
114-
let results: MediaTypeModel[] = [];
115115

116-
const {advancedSearchOptions, advancedSearchModal} = await this.openMediaDbAdvancedSearchModal();
117-
if (!advancedSearchOptions) {
118-
advancedSearchModal.close();
119-
return;
120-
}
121-
122-
let apiSearchResults: MediaTypeModel[] = undefined;
123-
try {
124-
apiSearchResults = await this.apiManager.query(advancedSearchOptions.query, advancedSearchOptions.apis);
125-
} catch (e) {
126-
console.warn(e);
127-
new Notice(e.toString());
128-
advancedSearchModal.close();
129-
return;
130-
}
131-
132-
advancedSearchModal.close();
116+
let apiSearchResults: MediaTypeModel[] = await this.modalHelper.openAdvancedSearchModal(async (advancedSearchOptions) => {
117+
return await this.apiManager.query(advancedSearchOptions.query, advancedSearchOptions.apis);
118+
});
133119

134-
const {selectRes, selectModal} = await this.openMediaDbSelectModal(apiSearchResults, false, false);
135-
if (!selectRes) {
136-
selectModal.close();
120+
if (!apiSearchResults) {
137121
return;
138122
}
139123

140-
// TODO: let's try to not query details for this
141-
try {
142-
results = await this.queryDetails(selectRes);
143-
} catch (e) {
144-
console.warn(e);
145-
new Notice(e.toString());
146-
selectModal.close();
147-
return;
148-
}
149-
150-
selectModal.close();
124+
const selectResults: MediaTypeModel[] = await this.modalHelper.openSelectModal(apiSearchResults, async (selectedMediaTypeModels) => {
125+
return await this.queryDetails(selectedMediaTypeModels);
126+
});
151127

152-
if (!results || results.length < 1) {
128+
if (!selectResults || selectResults.length < 1) {
153129
return;
154130
}
155131

156-
const link = `[${results[0].title}](${results[0].url})`
132+
const link = `[${selectResults[0].title}](${selectResults[0].url})`
157133

158134
const view = this.app.workspace.getActiveViewOfType(MarkdownView);
159135

@@ -171,71 +147,35 @@ export default class MediaDbPlugin extends Plugin {
171147
* TODO: further refactor: extract it into own method, pass the action (api query) as lambda as well as an options object
172148
*/
173149
async createEntryWithAdvancedSearchModal() {
174-
let results: MediaTypeModel[] = [];
150+
const apiSearchResults: MediaTypeModel[] = await this.modalHelper.openAdvancedSearchModal(async (advancedSearchOptions) => {
151+
return await this.apiManager.query(advancedSearchOptions.query, advancedSearchOptions.apis);
152+
});
175153

176-
const {advancedSearchOptions, advancedSearchModal} = await this.openMediaDbAdvancedSearchModal();
177-
if (!advancedSearchOptions) {
178-
advancedSearchModal.close();
154+
if (!apiSearchResults) {
179155
return;
180156
}
181157

182-
let apiSearchResults: MediaTypeModel[] = undefined;
183-
try {
184-
apiSearchResults = await this.apiManager.query(advancedSearchOptions.query, advancedSearchOptions.apis);
185-
} catch (e) {
186-
console.warn(e);
187-
new Notice(e.toString());
188-
advancedSearchModal.close();
189-
return;
190-
}
191-
192-
advancedSearchModal.close();
158+
const selectResults: MediaTypeModel[] = await this.modalHelper.openSelectModal(apiSearchResults, async (selectedMediaTypeModels) => {
159+
return await this.queryDetails(selectedMediaTypeModels);
160+
});
193161

194-
const {selectRes, selectModal} = await this.openMediaDbSelectModal(apiSearchResults, false);
195-
if (!selectRes) {
196-
selectModal.close();
162+
if (!selectResults) {
197163
return;
198164
}
199165

200-
try {
201-
results = await this.queryDetails(selectRes);
202-
} catch (e) {
203-
console.warn(e);
204-
new Notice(e.toString());
205-
selectModal.close();
206-
return;
207-
}
208-
209-
selectModal.close();
210-
211-
if (results) {
212-
await this.createMediaDbNotes(results);
213-
}
166+
await this.createMediaDbNotes(selectResults);
214167
}
215168

216169
async createEntryWithIdSearchModal() {
217-
let result: MediaTypeModel = undefined;
170+
const idSearchResult: MediaTypeModel = await this.modalHelper.openIdSearchModal(async (idSearchOptions) => {
171+
return await this.apiManager.queryDetailedInfoById(idSearchOptions.query, idSearchOptions.api);
172+
})
218173

219-
const {idSearchOptions, idSearchModal} = await this.openMediaDbIdSearchModal();
220-
if (!idSearchOptions) {
221-
idSearchModal.close();
174+
if (!idSearchResult) {
222175
return;
223176
}
224177

225-
try {
226-
result = await this.apiManager.queryDetailedInfoById(idSearchOptions.query, idSearchOptions.api);
227-
} catch (e) {
228-
console.warn(e);
229-
new Notice(e.toString());
230-
idSearchModal.close();
231-
return;
232-
}
233-
234-
idSearchModal.close();
235-
236-
if (result) {
237-
await this.createMediaDbNoteFromModel(result);
238-
}
178+
await this.createMediaDbNoteFromModel(idSearchResult);
239179
}
240180

241181
async createMediaDbNotes(models: MediaTypeModel[], attachFile?: TFile): Promise<void> {
@@ -517,55 +457,6 @@ export default class MediaDbPlugin extends Plugin {
517457
const targetFile = await this.app.vault.create(filePath, fileContent);
518458
}
519459

520-
async openMediaDbAdvancedSearchModal(): Promise<{ advancedSearchOptions: { query: string, apis: string[] }, advancedSearchModal: MediaDbAdvancedSearchModal }> {
521-
const modal = new MediaDbAdvancedSearchModal(this);
522-
const res: { query: string, apis: string[] } = await new Promise((resolve, reject) => {
523-
modal.setSubmitCallback(res => resolve(res));
524-
modal.setCloseCallback(err => {
525-
if (err) {
526-
reject(err);
527-
}
528-
resolve(undefined);
529-
});
530-
531-
modal.open();
532-
});
533-
return {advancedSearchOptions: res, advancedSearchModal: modal};
534-
}
535-
536-
async openMediaDbIdSearchModal(): Promise<{ idSearchOptions: { query: string, api: string }, idSearchModal: MediaDbIdSearchModal }> {
537-
const modal = new MediaDbIdSearchModal(this);
538-
const res: { query: string, api: string } = await new Promise((resolve, reject) => {
539-
modal.setSubmitCallback(res => resolve(res));
540-
modal.setCloseCallback(err => {
541-
if (err) {
542-
reject(err);
543-
}
544-
resolve(undefined);
545-
});
546-
547-
modal.open();
548-
});
549-
return {idSearchOptions: res, idSearchModal: modal};
550-
}
551-
552-
async openMediaDbSelectModal(resultsToDisplay: MediaTypeModel[], skipButton: boolean = false, allowMultiSelect: boolean = true): Promise<{ selectRes: MediaTypeModel[], selectModal: MediaDbSearchResultModal }> {
553-
const modal = new MediaDbSearchResultModal(this, resultsToDisplay, skipButton, allowMultiSelect);
554-
const res: MediaTypeModel[] = await new Promise((resolve, reject) => {
555-
modal.setSubmitCallback(res => resolve(res));
556-
modal.setSkipCallback(() => resolve([]));
557-
modal.setCloseCallback(err => {
558-
if (err) {
559-
reject(err);
560-
}
561-
resolve(undefined);
562-
});
563-
564-
modal.open();
565-
});
566-
return {selectRes: res, selectModal: modal};
567-
}
568-
569460
async loadSettings() {
570461
// console.log(DEFAULT_SETTINGS);
571462
const diskSettings: MediaDbPluginSettings = await this.loadData();

0 commit comments

Comments
 (0)