forked from mProjectsCode/obsidian-media-db-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
62a4f2e
commit 92f3e6b
Showing
13 changed files
with
183 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,30 @@ | ||
import {MediaTypeModel} from '../models/MediaTypeModel'; | ||
|
||
export abstract class APIModel { | ||
apiName: string; | ||
apiUrl: string; | ||
apiDescription: string; | ||
types: string[]; | ||
apiName: string; | ||
apiUrl: string; | ||
apiDescription: string; | ||
types: string[]; | ||
|
||
/** | ||
* This function should query the api and return a list of matches. The matches should be caped at 20. | ||
* | ||
* @param title the title to query for | ||
*/ | ||
abstract searchByTitle(title: string): Promise<MediaTypeModel[]>; | ||
/** | ||
* This function should query the api and return a list of matches. The matches should be caped at 20. | ||
* | ||
* @param title the title to query for | ||
*/ | ||
abstract searchByTitle(title: string): Promise<MediaTypeModel[]>; | ||
|
||
abstract getById(item: MediaTypeModel): Promise<MediaTypeModel>; | ||
abstract getById(item: MediaTypeModel): Promise<MediaTypeModel>; | ||
|
||
hasType(type: string): boolean { | ||
return this.types.contains(type); | ||
} | ||
hasType(type: string): boolean { | ||
return this.types.contains(type); | ||
} | ||
|
||
hasTypeOverlap(types: string[]): boolean { | ||
for (const type of types) { | ||
if (this.hasType(type)) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
hasTypeOverlap(types: string[]): boolean { | ||
for (const type of types) { | ||
if (this.hasType(type)) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
import {App, ButtonComponent, DropdownComponent, Modal, Notice, Setting, TextComponent} from 'obsidian'; | ||
import {MediaTypeModel} from '../models/MediaTypeModel'; | ||
import {APIManager} from '../api/APIManager'; | ||
|
||
export class MediaDbIdSearchModal extends Modal { | ||
query: string; | ||
isBusy: boolean; | ||
apiManager: APIManager; | ||
searchBtn: ButtonComponent; | ||
selectedApi: string; | ||
onSubmit: (err: Error, result?: MediaTypeModel) => void; | ||
|
||
constructor(app: App, apiManager: APIManager, onSubmit?: (err: Error, result?: MediaTypeModel) => void) { | ||
super(app); | ||
this.apiManager = apiManager; | ||
this.onSubmit = onSubmit; | ||
this.selectedApi = ''; | ||
} | ||
|
||
submitCallback(event: KeyboardEvent) { | ||
if (event.key === 'Enter') { | ||
this.search(); | ||
} | ||
} | ||
|
||
async search(): Promise<MediaTypeModel> { | ||
|
||
console.log(this.selectedApi); | ||
|
||
if (!this.query) { | ||
new Notice('MDB: no Id entered'); | ||
return; | ||
} | ||
|
||
if (!this.selectedApi) { | ||
new Notice('MDB: No API selected'); | ||
return; | ||
} | ||
|
||
if (!this.isBusy) { | ||
try { | ||
this.isBusy = true; | ||
this.searchBtn.setDisabled(false); | ||
this.searchBtn.setButtonText('Searching...'); | ||
|
||
console.log('MDB | query started with id ' + this.query); | ||
|
||
const api = this.apiManager.getApiByName(this.selectedApi); | ||
if (!api) { | ||
this.onSubmit(new Error('the selected api does not exist')); | ||
} | ||
const res = await api.getById({id: this.query} as MediaTypeModel); // TODO: fix jank | ||
|
||
// console.log(res) | ||
|
||
this.onSubmit(null, res); | ||
} catch (e) { | ||
this.onSubmit(e); | ||
} finally { | ||
this.close(); | ||
} | ||
} | ||
} | ||
|
||
onOpen() { | ||
const {contentEl} = this; | ||
|
||
contentEl.createEl('h2', {text: 'Search media db by id'}); | ||
|
||
const placeholder = 'Search by id'; | ||
const searchComponent = new TextComponent(contentEl); | ||
searchComponent.inputEl.style.width = '100%'; | ||
searchComponent.setPlaceholder(placeholder); | ||
searchComponent.onChange(value => (this.query = value)); | ||
searchComponent.inputEl.addEventListener('keydown', this.submitCallback.bind(this)); | ||
|
||
contentEl.appendChild(searchComponent.inputEl); | ||
searchComponent.inputEl.focus(); | ||
|
||
const apiSelectorWrapper = contentEl.createEl('div', {cls: 'media-db-plugin-list-wrapper'}); | ||
const apiSelectorTExtWrapper = apiSelectorWrapper.createEl('div', {cls: 'media-db-plugin-list-text-wrapper'}); | ||
apiSelectorTExtWrapper.createEl('span', {text: 'API to search', cls: 'media-db-plugin-list-text'}); | ||
|
||
const apiSelectorComponent = new DropdownComponent(apiSelectorWrapper); | ||
apiSelectorComponent.onChange((value: string) => { | ||
this.selectedApi = value; | ||
}); | ||
for (const api of this.apiManager.apis) { | ||
apiSelectorComponent.addOption(api.apiName, api.apiName); | ||
} | ||
apiSelectorWrapper.appendChild(apiSelectorComponent.selectEl); | ||
|
||
new Setting(contentEl) | ||
.addButton(btn => btn.setButtonText('Cancel').onClick(() => this.close())) | ||
.addButton(btn => { | ||
return (this.searchBtn = btn | ||
.setButtonText('Ok') | ||
.setCta() | ||
.onClick(() => { | ||
this.search(); | ||
})); | ||
}); | ||
} | ||
|
||
onClose() { | ||
const {contentEl} = this; | ||
contentEl.empty(); | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters