|
1 |
| -import {Notice, parseYaml, Plugin, stringifyYaml, TFile, TFolder} from 'obsidian'; |
| 1 | +import {MarkdownView, Notice, parseYaml, Plugin, stringifyYaml, TFile, TFolder} from 'obsidian'; |
2 | 2 | import {getDefaultSettings, MediaDbPluginSettings, MediaDbSettingTab} from './settings/Settings';
|
3 | 3 | import {APIManager} from './api/APIManager';
|
4 | 4 | import {MediaTypeModel} from './models/MediaTypeModel';
|
@@ -88,6 +88,78 @@ export default class MediaDbPlugin extends Plugin {
|
88 | 88 | return true;
|
89 | 89 | },
|
90 | 90 | });
|
| 91 | + // register link insert command |
| 92 | + this.addCommand({ |
| 93 | + id: 'add-media-db-link', |
| 94 | + name: 'Add a link.', |
| 95 | + checkCallback: (checking: boolean) => { |
| 96 | + if (!this.app.workspace.getActiveFile()) { |
| 97 | + return false; |
| 98 | + } |
| 99 | + if (!checking) { |
| 100 | + this.createLinkWithSearchModal(); |
| 101 | + } |
| 102 | + return true; |
| 103 | + }, |
| 104 | + }); |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * first very simple approach |
| 109 | + * - replace the detail query |
| 110 | + * - maybe custom link syntax |
| 111 | + */ |
| 112 | + async createLinkWithSearchModal() { |
| 113 | + let results: MediaTypeModel[] = []; |
| 114 | + |
| 115 | + const {advancedSearchOptions, advancedSearchModal} = await this.openMediaDbAdvancedSearchModal(); |
| 116 | + if (!advancedSearchOptions) { |
| 117 | + advancedSearchModal.close(); |
| 118 | + return; |
| 119 | + } |
| 120 | + |
| 121 | + let apiSearchResults: MediaTypeModel[] = undefined; |
| 122 | + try { |
| 123 | + apiSearchResults = await this.apiManager.query(advancedSearchOptions.query, advancedSearchOptions.apis); |
| 124 | + } catch (e) { |
| 125 | + console.warn(e); |
| 126 | + new Notice(e.toString()); |
| 127 | + advancedSearchModal.close(); |
| 128 | + return; |
| 129 | + } |
| 130 | + |
| 131 | + advancedSearchModal.close(); |
| 132 | + |
| 133 | + const {selectRes, selectModal} = await this.openMediaDbSelectModal(apiSearchResults, false, false); |
| 134 | + if (!selectRes) { |
| 135 | + selectModal.close(); |
| 136 | + return; |
| 137 | + } |
| 138 | + |
| 139 | + // TODO: let's try to not query details for this |
| 140 | + try { |
| 141 | + results = await this.queryDetails(selectRes); |
| 142 | + } catch (e) { |
| 143 | + console.warn(e); |
| 144 | + new Notice(e.toString()); |
| 145 | + selectModal.close(); |
| 146 | + return; |
| 147 | + } |
| 148 | + |
| 149 | + selectModal.close(); |
| 150 | + |
| 151 | + if (!results || results.length < 1) { |
| 152 | + return; |
| 153 | + } |
| 154 | + |
| 155 | + const link = `[${results[0].title}](${results[0].url})` |
| 156 | + |
| 157 | + const view = this.app.workspace.getActiveViewOfType(MarkdownView); |
| 158 | + |
| 159 | + // Make sure the user is editing a Markdown file. |
| 160 | + if (view) { |
| 161 | + view.editor.replaceRange(link, view.editor.getCursor()); |
| 162 | + } |
91 | 163 | }
|
92 | 164 |
|
93 | 165 | async createEntryWithSearchModal() {
|
@@ -473,8 +545,8 @@ export default class MediaDbPlugin extends Plugin {
|
473 | 545 | return {idSearchOptions: res, idSearchModal: modal};
|
474 | 546 | }
|
475 | 547 |
|
476 |
| - async openMediaDbSelectModal(resultsToDisplay: MediaTypeModel[], skipButton: boolean = false): Promise<{ selectRes: MediaTypeModel[], selectModal: MediaDbSearchResultModal }> { |
477 |
| - const modal = new MediaDbSearchResultModal(this, resultsToDisplay, skipButton); |
| 548 | + async openMediaDbSelectModal(resultsToDisplay: MediaTypeModel[], skipButton: boolean = false, allowMultiSelect: boolean = true): Promise<{ selectRes: MediaTypeModel[], selectModal: MediaDbSearchResultModal }> { |
| 549 | + const modal = new MediaDbSearchResultModal(this, resultsToDisplay, skipButton, allowMultiSelect); |
478 | 550 | const res: MediaTypeModel[] = await new Promise((resolve, reject) => {
|
479 | 551 | modal.setSubmitCallback(res => resolve(res));
|
480 | 552 | modal.setSkipCallback(() => resolve([]));
|
|
0 commit comments