diff --git a/main.ts b/main.ts index 657cab5..b83d249 100644 --- a/main.ts +++ b/main.ts @@ -6,6 +6,7 @@ import {MovieData, MovieSearch, MovieSearchItem, TEST_SEARCH} from "./src/Movieg import { MoviegrabberSelectionModal } from 'src/MoviegrabberSelectionModal'; import { MovieGalleryView, VIEW_TYPE_MOVIE_GALLERY } from 'src/MovieGalleryView'; import { ConfirmOverwriteModal } from 'src/ConfirmOverwriteModal'; +import { ConfirmCreateNoteModal } from 'src/ConfirmCreateNoteModal'; const OVERWRITE_DELIMITER = /%%==MOVIEGRABBER_KEEP==%%[\s\S]*/ const IMDBID_REGEX = /^ev\d{1,7}\/\d{4}(-\d)?$|^(ch|co|ev|nm|tt)\d{1,7}$/ @@ -81,14 +82,17 @@ export default class Moviegrabber extends Plugin { return; } + // check if search string is valid IMDB-id + let isImdbId = IMDBID_REGEX.test(title); + // build request URL var url = new URL("http://www.omdbapi.com"); url.searchParams.append('apikey', this.settings.OMDb_API_Key); - url.searchParams.append('s', title); + url.searchParams.append(isImdbId ? 'i' : 's', title); url.searchParams.append('type', type); - console.log(`requesting: ${url}`); + // console.log(`requesting: ${url}`); // fetch data var response; @@ -114,6 +118,13 @@ export default class Moviegrabber extends Plugin { return; } + if (isImdbId) { + let movie = data as MovieData; + new ConfirmCreateNoteModal(this.app, movie, () =>{ + this.tryCreateNote(movie, type); + }).open(); + return; + } new MoviegrabberSelectionModal(this.app, data as MovieSearch, (result) => { this.tryCreateNote(result, type); @@ -202,7 +213,7 @@ export default class Moviegrabber extends Plugin { return embed; } - async tryCreateNote(item : MovieSearchItem, type : 'movie' | 'series') { + async tryCreateNote(item : MovieSearchItem | MovieData, type : 'movie' | 'series') { // create path and check for directory before posting the request var dir = type == 'movie' ? this.settings.MovieDirectory : this.settings.SeriesDirectory; @@ -230,9 +241,9 @@ export default class Moviegrabber extends Plugin { this.createNote(item, type, path); } - async createNote(item : MovieSearchItem, type : 'movie' | 'series', path : string, tFile : TFile | null=null) { + async createNote(item : MovieSearchItem | MovieData, type : 'movie' | 'series', path : string, tFile : TFile | null=null) { - var itemData = await this.getOmdbData(item); + var itemData = ('Response' in item ) ? item : await this.getOmdbData(item); if (itemData == null){ var n = new Notice(`something went wrong in fetching ${item.Title} data`) @@ -285,7 +296,7 @@ export default class Moviegrabber extends Plugin { // find delimiter string if it exists and keep whatever is below. let toKeep = OVERWRITE_DELIMITER.exec(oldContent); - this.app.vault.modify(tFile, content + '\n' + toKeep); + this.app.vault.modify(tFile, content + '\n' + (toKeep != null ? toKeep : '')); } if (this.settings.SwitchToCreatedNote) { diff --git a/manifest.json b/manifest.json index 5397e57..666cb82 100644 --- a/manifest.json +++ b/manifest.json @@ -1,7 +1,7 @@ { "id": "moviegrabber", "name": "Moviegrabber", - "version": "1.1.5", + "version": "1.1.6", "minAppVersion": "0.15.0", "description": "Grab movie data from public APIs and transform it into notes that can be used with dataview and properties", "author": "Superschnizel", diff --git a/src/ConfirmCreateNoteModal.ts b/src/ConfirmCreateNoteModal.ts new file mode 100644 index 0000000..d56bd9a --- /dev/null +++ b/src/ConfirmCreateNoteModal.ts @@ -0,0 +1,54 @@ +import { Modal, App, Setting } from "obsidian"; +import { MovieData, MovieSearchItem } from "./MoviegrabberSearchObject"; + +export class ConfirmCreateNoteModal extends Modal { + result: string; + onSubmit: () => void; + data : MovieData; + + constructor(app: App, data: MovieData, onSubmit: () => void) { + super(app); + this.onSubmit = onSubmit; + this.data = data; + } + + onOpen() { + const { contentEl } = this; + + contentEl.createEl("h1", { text: `Create Note for ${this.data.Title} (${this.data.Year})?` }); + + var d = contentEl.createEl("div", {cls: "confirm_preview_item"}) + d.createEl("img", { + attr: {src : this.data.Poster}, + cls : "confirm_preview_img"} + ); + var dd = d.createEl("div", {cls : "confirm_preview_Text"}); + dd.createEl("div", { text: this.data.Title , cls : "preview_Title"}); + dd.createEl("small", { text: `(${this.data.Year})`, cls : "confirm_preview_Year" }); + + + + new Setting(contentEl) + .addButton((btn) => + btn + .setButtonText("Create") + .setCta() + .setClass("moviegrabber-confirm-button") + .onClick(() => { + this.close(); + this.onSubmit(); + })) + .addButton((btn) => + btn + .setButtonText("Cancel") + .setCta() + .onClick(() => { + this.close(); + })); + } + + onClose() { + let { contentEl } = this; + contentEl.empty(); + } + } \ No newline at end of file diff --git a/styles.css b/styles.css index e268f77..b64b9a1 100644 --- a/styles.css +++ b/styles.css @@ -50,6 +50,29 @@ If your plugin does not need CSS, delete this file. background-color: var(--background-modifier-error); } +.confirm_preview_item{ + min-height: fit-content; + display: flex; + align-items: center; + justify-content: center; + flex-direction: column; +} + +.confirm_preview_img { + max-height: 224px; +} + +.confirm_preview_Text{ + margin: var(--size-4-1); + display: flex; + align-items: center; +} + +.confirm_preview_Year{ + font-size: small; + margin-left: var(--size-4-1); +} + /* -------------------------------------------------- */ /* ------------ Moviegrabber Gallery View ----------- */ /* -------------------------------------------------- */