Add props to FileSuggest to deduplicate code used in BookSuggest

This commit is contained in:
Evan Fiordeliso 2025-06-30 10:42:06 -04:00
parent 217f66acf9
commit 20ffc78e8e
5 changed files with 37 additions and 55 deletions

View File

@ -1,48 +0,0 @@
<script lang="ts">
import type { App, TFile } from "obsidian";
import TextInputSuggest, { type Item } from "./TextInputSuggest.svelte";
type Props = {
app: App;
id: string;
asString?: boolean;
value?: TFile | string;
bookFolder?: string;
onSelected?: (fileOrPath: TFile | string) => void;
};
let {
app,
id,
asString,
value = $bindable(),
bookFolder,
onSelected,
}: Props = $props();
let items: Item<TFile | string>[] = $state([]);
function handleChange(query: string) {
items = app.vault
.getMarkdownFiles()
.filter(
(f) =>
(bookFolder === undefined ||
f.path.startsWith(bookFolder)) &&
f.basename.toLowerCase().includes(query.toLowerCase()),
)
.map((f) => ({
text: f.basename,
value: asString ? f.basename : f,
}));
}
</script>
<TextInputSuggest
{app}
{id}
{items}
bind:value
onChange={handleChange}
{onSelected}
/>

View File

@ -1,11 +1,14 @@
<script lang="ts"> <script lang="ts">
import type { App, TFile } from "obsidian"; import type { App, TFile } from "obsidian";
import TextInputSuggest, { type Item } from "./TextInputSuggest.svelte"; import TextInputSuggest, { type Item } from "./TextInputSuggest.svelte";
import type { StringKeys } from "@utils/types";
type Props = { type Props = {
app: App; app: App;
id: string; id: string;
asString?: boolean; asString?: boolean;
property?: StringKeys<TFile>;
inFolder?: string;
value?: TFile | string; value?: TFile | string;
onSelected?: (fileOrPath: TFile | string) => void; onSelected?: (fileOrPath: TFile | string) => void;
}; };
@ -14,6 +17,8 @@
app, app,
id, id,
asString, asString,
property = "path",
inFolder,
value = $bindable(), value = $bindable(),
onSelected, onSelected,
}: Props = $props(); }: Props = $props();
@ -23,8 +28,15 @@
function handleChange(query: string) { function handleChange(query: string) {
items = app.vault items = app.vault
.getMarkdownFiles() .getMarkdownFiles()
.filter((f) => f.path.toLowerCase().includes(query.toLowerCase())) .filter(
.map((f) => ({ text: f.path, value: asString ? f.path : f })); (f) =>
(inFolder === undefined || f.path.startsWith(inFolder)) &&
f[property].toLowerCase().includes(query.toLowerCase()),
)
.map((f) => ({
text: f[property],
value: asString ? f[property] : f,
}));
} }
</script> </script>

View File

@ -1,11 +1,14 @@
<script lang="ts"> <script lang="ts">
import { TFolder, type App } from "obsidian"; import { TFolder, type App } from "obsidian";
import TextInputSuggest, { type Item } from "./TextInputSuggest.svelte"; import TextInputSuggest, { type Item } from "./TextInputSuggest.svelte";
import type { StringKeys } from "@utils/types";
type Props = { type Props = {
app: App; app: App;
id: string; id: string;
asString?: boolean; asString?: boolean;
property?: StringKeys<TFolder>;
inFolder?: string;
value?: TFolder | string; value?: TFolder | string;
onSelected?: (folderOrPath: TFolder | string) => void; onSelected?: (folderOrPath: TFolder | string) => void;
}; };
@ -14,6 +17,8 @@
app, app,
id, id,
asString, asString,
property = "path",
inFolder,
value = $bindable(), value = $bindable(),
onSelected, onSelected,
}: Props = $props(); }: Props = $props();
@ -23,8 +28,15 @@
function handleChange(query: string) { function handleChange(query: string) {
items = app.vault items = app.vault
.getAllFolders() .getAllFolders()
.filter((f) => f.path.toLowerCase().includes(query.toLowerCase())) .filter(
.map((f) => ({ text: f.path, value: asString ? f.path : f })); (f) =>
(inFolder === undefined || f.path.startsWith(inFolder)) &&
f[property].toLowerCase().includes(query.toLowerCase()),
)
.map((f) => ({
text: f[property],
value: asString ? f[property] : f,
}));
} }
</script> </script>

View File

@ -1,7 +1,8 @@
<script lang="ts"> <script lang="ts">
import type BookTrackerPlugin from "@src/main"; import type BookTrackerPlugin from "@src/main";
import type { ReadingLogEntry } from "@src/types"; import type { ReadingLogEntry } from "@src/types";
import BookSuggest from "@ui/components/suggesters/BookSuggest.svelte"; import FileSuggest from "@ui/components/suggesters/FileSuggest.svelte";
interface Props { interface Props {
plugin: BookTrackerPlugin; plugin: BookTrackerPlugin;
entry?: ReadingLogEntry; entry?: ReadingLogEntry;
@ -61,10 +62,12 @@
<form {onsubmit}> <form {onsubmit}>
<div class="fields"> <div class="fields">
<label for="book">Book</label> <label for="book">Book</label>
<BookSuggest <FileSuggest
id="book" id="book"
app={plugin.app} app={plugin.app}
bookFolder={plugin.settings.bookFolder} asString
property="basename"
inFolder={plugin.settings.bookFolder}
bind:value={book} bind:value={book}
/> />
<label for="pagesRead">Pages Read</label> <label for="pagesRead">Pages Read</label>

3
src/utils/types.ts Normal file
View File

@ -0,0 +1,3 @@
export type StringKeys<T> = {
[K in keyof T]: T[K] extends string ? K : never;
}[keyof T];