Skip to content

Commit

Permalink
adding suggesters for settings
Browse files Browse the repository at this point in the history
  • Loading branch information
Superschnizel committed Nov 26, 2023
1 parent 06e4123 commit e457677
Show file tree
Hide file tree
Showing 6 changed files with 358 additions and 28 deletions.
68 changes: 40 additions & 28 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { MoviegrabberSelectionModal } from 'src/MoviegrabberSelectionModal';
import { MovieGalleryView, VIEW_TYPE_MOVIE_GALLERY } from 'src/MovieGalleryView';
import { ConfirmOverwriteModal } from 'src/ConfirmOverwriteModal';
import { ConfirmCreateNoteModal } from 'src/ConfirmCreateNoteModal';
import { FolderSuggest } from 'src/interface/FolderSuggester';
import { FileSuggest } from 'src/interface/FileSuggester';

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}$/
Expand Down Expand Up @@ -440,24 +442,29 @@ class MoviegrabberSettingTab extends PluginSettingTab {
new Setting(containerEl)
.setName('Movie folder')
.setDesc('Folder in which to save the generated notes for series')
.addText(text => text
.setPlaceholder('Movies')
.setValue(this.plugin.settings.MovieDirectory)
.onChange(async (value) => {
this.plugin.settings.MovieDirectory = value;
await this.plugin.saveSettings();
}));
.addSearch((cb) => {
new FolderSuggest(cb.inputEl, this.plugin.app);
cb.setPlaceholder("Example: folder1/folder2")
.setValue(this.plugin.settings.MovieDirectory)
.onChange(async (newFolder) => {
this.plugin.settings.MovieDirectory = newFolder;
await this.plugin.saveSettings();
});
});


new Setting(containerEl)
.setName('Series folder')
.setDesc('Folder in which to save the generated notes for series')
.addText(text => text
.setPlaceholder('Series')
.setValue(this.plugin.settings.SeriesDirectory)
.onChange(async (value) => {
this.plugin.settings.SeriesDirectory = value;
await this.plugin.saveSettings();
}));
.addSearch((cb) => {
new FolderSuggest(cb.inputEl, this.plugin.app);
cb.setPlaceholder("Example: folder1/folder2")
.setValue(this.plugin.settings.SeriesDirectory)
.onChange(async (newFolder) => {
this.plugin.settings.SeriesDirectory = newFolder;
await this.plugin.saveSettings();
});
});

new Setting(containerEl)
.setName('OMDb API key')
Expand Down Expand Up @@ -495,24 +502,29 @@ class MoviegrabberSettingTab extends PluginSettingTab {
new Setting(containerEl)
.setName('Movie template file path')
.setDesc('Path to the template file that is used to create notes for movies')
.addText(text => text
.setPlaceholder('')
.setValue(this.plugin.settings.MovieTemplatePath)
.onChange(async (value) => {
this.plugin.settings.MovieTemplatePath = value;
await this.plugin.saveSettings();
}));
.addSearch((cb) => {
new FileSuggest(cb.inputEl, this.plugin.app);
cb.setPlaceholder("Example: folder1/folder2")
.setValue(this.plugin.settings.MovieTemplatePath)
.onChange(async (newFile) => {
this.plugin.settings.MovieTemplatePath = newFile;
await this.plugin.saveSettings();
});
});

new Setting(containerEl)
.setName('Series template file path')
.setDesc('Path to the template file that is used to create notes for series')
.addText(text => text
.setPlaceholder('')
.setValue(this.plugin.settings.SeriesTemplatePath)
.onChange(async (value) => {
this.plugin.settings.SeriesTemplatePath = value;
await this.plugin.saveSettings();
}));
.addSearch((cb) => {
new FileSuggest(cb.inputEl, this.plugin.app);
cb.setPlaceholder("Example: folder1/folder2")
.setValue(this.plugin.settings.SeriesTemplatePath)
.onChange(async (newFile) => {
this.plugin.settings.SeriesTemplatePath = newFile;
await this.plugin.saveSettings();
});
});

new Setting(containerEl)
.setName('Create example template file')
.setDesc('Creates an example template file to expand and use.\nThe file is called `/Moviegrabber-example-template`')
Expand Down
12 changes: 12 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,8 @@
"obsidian": "latest",
"tslib": "2.4.0",
"typescript": "4.7.4"
},
"dependencies": {
"@popperjs/core": "^2.11.8"
}
}
65 changes: 65 additions & 0 deletions src/interface/FileSuggester.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Credits go to Liam's Periodic Notes Plugin: https://github.com/liamcain/obsidian-periodic-notes
// here adapted from Templater plugin https://github.com/SilentVoid13/Templater

import { TAbstractFile, TFile, TFolder } from "obsidian";

import { TextInputSuggest } from "./suggest";

export class FileSuggest extends TextInputSuggest<TFile> {
getSuggestions(inputStr: string): TFile[] {
const abstractFiles = this.app.vault.getAllLoadedFiles();
const files: TFile[] = [];
const lowerCaseInputStr = inputStr.toLowerCase();

abstractFiles.forEach((file: TAbstractFile) => {
if (
file instanceof TFile &&
file.extension === "md" &&
file.path.toLowerCase().contains(lowerCaseInputStr)
) {
files.push(file);
}
});

return files;
}

renderSuggestion(file: TFile, el: HTMLElement): void {
el.setText(file.path);
}

selectSuggestion(file: TFile): void {
this.inputEl.value = file.path;
this.inputEl.trigger("input");
this.close();
}
}

export class FolderSuggest extends TextInputSuggest<TFolder> {
getSuggestions(inputStr: string): TFolder[] {
const abstractFiles = this.app.vault.getAllLoadedFiles();
const folders: TFolder[] = [];
const lowerCaseInputStr = inputStr.toLowerCase();

abstractFiles.forEach((folder: TAbstractFile) => {
if (
folder instanceof TFolder &&
folder.path.toLowerCase().contains(lowerCaseInputStr)
) {
folders.push(folder);
}
});

return folders;
}

renderSuggestion(file: TFolder, el: HTMLElement): void {
el.setText(file.path);
}

selectSuggestion(file: TFolder): void {
this.inputEl.value = file.path;
this.inputEl.trigger("input");
this.close();
}
}
34 changes: 34 additions & 0 deletions src/interface/FolderSuggester.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Credits go to Liam's Periodic Notes Plugin: https://github.com/liamcain/obsidian-periodic-notes
// here adapted from Templater plugin https://github.com/SilentVoid13/Templater

import { TAbstractFile, TFolder } from "obsidian";
import { TextInputSuggest } from "./suggest";

export class FolderSuggest extends TextInputSuggest<TFolder> {
getSuggestions(inputStr: string): TFolder[] {
const abstractFiles = app.vault.getAllLoadedFiles();
const folders: TFolder[] = [];
const lowerCaseInputStr = inputStr.toLowerCase();

abstractFiles.forEach((folder: TAbstractFile) => {
if (
folder instanceof TFolder &&
folder.path.toLowerCase().contains(lowerCaseInputStr)
) {
folders.push(folder);
}
});

return folders;
}

renderSuggestion(file: TFolder, el: HTMLElement): void {
el.setText(file.path);
}

selectSuggestion(file: TFolder): void {
this.inputEl.value = file.path;
this.inputEl.trigger("input");
this.close();
}
}
Loading

0 comments on commit e457677

Please sign in to comment.