refactor suggester code
This commit is contained in:
parent
e1a08e0680
commit
4c7ae08f91
2 changed files with 8 additions and 21 deletions
|
|
@ -1,20 +1,13 @@
|
|||
import type { TAbstractFile } from 'obsidian';
|
||||
import { AbstractInputSuggest, TFile } from 'obsidian';
|
||||
|
||||
export class FileSuggest extends AbstractInputSuggest<TFile> {
|
||||
protected getSuggestions(query: string): TFile[] | Promise<TFile[]> {
|
||||
const abstractFiles = this.app.vault.getAllLoadedFiles();
|
||||
const files: TFile[] = [];
|
||||
const lowerCaseInputStr = query.toLowerCase();
|
||||
|
||||
abstractFiles.forEach((file: TAbstractFile) => {
|
||||
// Match against full path instead of just name
|
||||
if (file instanceof TFile && file.path.toLowerCase().contains(lowerCaseInputStr)) {
|
||||
files.push(file);
|
||||
}
|
||||
});
|
||||
|
||||
return files;
|
||||
// we do two filters because otherwise TS type inference does convert the array to TFile[]
|
||||
return this.app.vault.getAllLoadedFiles()
|
||||
.filter(file => file instanceof TFile)
|
||||
.filter(file => file.path.toLowerCase().contains(lowerCaseInputStr));
|
||||
}
|
||||
|
||||
renderSuggestion(value: TFile, el: HTMLElement): void {
|
||||
|
|
|
|||
|
|
@ -1,19 +1,13 @@
|
|||
import type { TAbstractFile } from 'obsidian';
|
||||
import { AbstractInputSuggest, TFolder } from 'obsidian';
|
||||
|
||||
export class FolderSuggest extends AbstractInputSuggest<TFolder> {
|
||||
protected getSuggestions(query: string): TFolder[] | Promise<TFolder[]> {
|
||||
const abstractFiles = this.app.vault.getAllLoadedFiles();
|
||||
const folders: TFolder[] = [];
|
||||
const lowerCaseInputStr = query.toLowerCase();
|
||||
|
||||
abstractFiles.forEach((folder: TAbstractFile) => {
|
||||
if (folder instanceof TFolder && folder.path.toLowerCase().contains(lowerCaseInputStr)) {
|
||||
folders.push(folder);
|
||||
}
|
||||
});
|
||||
|
||||
return folders;
|
||||
// we do two filters because otherwise TS type inference does convert the array to TFolder[]
|
||||
return this.app.vault.getAllLoadedFiles()
|
||||
.filter(file => file instanceof TFolder)
|
||||
.filter(file => file.path.toLowerCase().contains(lowerCaseInputStr));
|
||||
}
|
||||
|
||||
renderSuggestion(value: TFolder, el: HTMLElement): void {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue