generated from tpl/obsidian-sample-plugin
Reorganize utils dir
This commit is contained in:
parent
113ddefac7
commit
e4a416ec2f
|
@ -5,7 +5,7 @@ import {
|
||||||
BookTrackerSettingTab,
|
BookTrackerSettingTab,
|
||||||
} from "@ui/settings";
|
} from "@ui/settings";
|
||||||
import { getBookByLegacyId, type SearchResult } from "@data-sources/goodreads";
|
import { getBookByLegacyId, type SearchResult } from "@data-sources/goodreads";
|
||||||
import { Templater } from "@utils/templater";
|
import { Templater } from "@utils/Templater";
|
||||||
import {
|
import {
|
||||||
GoodreadsSearchModal,
|
GoodreadsSearchModal,
|
||||||
GoodreadsSearchSuggestModal,
|
GoodreadsSearchSuggestModal,
|
||||||
|
@ -18,7 +18,8 @@ import {
|
||||||
READ_STATE,
|
READ_STATE,
|
||||||
TO_BE_READ_STATE,
|
TO_BE_READ_STATE,
|
||||||
} from "./const";
|
} from "./const";
|
||||||
import { ReadingLog, Storage } from "@utils/storage";
|
import { Storage } from "@utils/Storage";
|
||||||
|
import { ReadingLog } from "@utils/ReadingLog";
|
||||||
import { registerReadingLogCodeBlockProcessor } from "@ui/code-blocks";
|
import { registerReadingLogCodeBlockProcessor } from "@ui/code-blocks";
|
||||||
|
|
||||||
export default class BookTrackerPlugin extends Plugin {
|
export default class BookTrackerPlugin extends Plugin {
|
||||||
|
@ -32,7 +33,7 @@ export default class BookTrackerPlugin extends Plugin {
|
||||||
|
|
||||||
this.templater = new Templater(this.app);
|
this.templater = new Templater(this.app);
|
||||||
this.storage = new Storage(this.app, this);
|
this.storage = new Storage(this.app, this);
|
||||||
this.readingLog = new ReadingLog(this);
|
this.readingLog = new ReadingLog(this.storage);
|
||||||
|
|
||||||
this.addCommand({
|
this.addCommand({
|
||||||
id: "search-goodreads",
|
id: "search-goodreads",
|
||||||
|
|
|
@ -21,11 +21,3 @@ export interface Book {
|
||||||
isbn: string;
|
isbn: string;
|
||||||
isbn13: string;
|
isbn13: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface ReadingLogEntry {
|
|
||||||
book: string;
|
|
||||||
pagesRead: number;
|
|
||||||
pagesReadTotal: number;
|
|
||||||
pagesRemaining: number;
|
|
||||||
createdAt: Date;
|
|
||||||
}
|
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import type { ReadingLogEntry } from "@src/types";
|
import type { ReadingLogEntry } from "@utils/ReadingLog";
|
||||||
import { Edit, Trash, Plus } from "lucide-svelte";
|
import { Edit, Trash, Plus } from "lucide-svelte";
|
||||||
import { ReadingLogEntryEditModal } from "@ui/modals";
|
import { ReadingLogEntryEditModal } from "@ui/modals";
|
||||||
import type BookTrackerPlugin from "@src/main";
|
import type BookTrackerPlugin from "@src/main";
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
import ReadingLogEntryEditModalView from "./ReadingLogEntryEditModalView.svelte";
|
import ReadingLogEntryEditModalView from "./ReadingLogEntryEditModalView.svelte";
|
||||||
import type { ReadingLogEntry } from "@src/types";
|
import type { ReadingLogEntry } from "@utils/ReadingLog";
|
||||||
import { SvelteModal } from "./SvelteModal";
|
import { SvelteModal } from "./SvelteModal";
|
||||||
import type BookTrackerPlugin from "@src/main";
|
import type BookTrackerPlugin from "@src/main";
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
<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 "@utils/ReadingLog";
|
||||||
import FileSuggest from "@ui/components/suggesters/FileSuggest.svelte";
|
import FileSuggest from "@ui/components/suggesters/FileSuggest.svelte";
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
|
|
|
@ -1,50 +1,24 @@
|
||||||
import BookTrackerPlugin from "@src/main";
|
import type { Storage } from "./Storage";
|
||||||
import type { ReadingLogEntry } from "@src/types";
|
|
||||||
import { App } from "obsidian";
|
|
||||||
|
|
||||||
export class Storage {
|
export interface ReadingLogEntry {
|
||||||
public constructor(
|
book: string;
|
||||||
private readonly app: App,
|
pagesRead: number;
|
||||||
private readonly plugin: BookTrackerPlugin
|
pagesReadTotal: number;
|
||||||
) {}
|
pagesRemaining: number;
|
||||||
|
createdAt: Date;
|
||||||
private getFilePath(filename: string): string {
|
|
||||||
return `${this.plugin.manifest.dir!!}/${filename}`;
|
|
||||||
}
|
|
||||||
|
|
||||||
public async readJSON<T>(filename: string): Promise<T | null> {
|
|
||||||
const filePath = this.getFilePath(filename);
|
|
||||||
const content = await this.app.vault.adapter.read(filePath);
|
|
||||||
if (!content) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
return JSON.parse(content) as T;
|
|
||||||
} catch (error) {
|
|
||||||
console.error(`Error parsing JSON from ${filePath}:`, error);
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public async writeJSON<T>(filename: string, data: T): Promise<void> {
|
|
||||||
const filePath = this.getFilePath(filename);
|
|
||||||
const content = JSON.stringify(data, null, 2);
|
|
||||||
await this.app.vault.adapter.write(filePath, content);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export class ReadingLog {
|
export class ReadingLog {
|
||||||
private entries: ReadingLogEntry[] = [];
|
private entries: ReadingLogEntry[] = [];
|
||||||
|
|
||||||
public constructor(private readonly plugin: BookTrackerPlugin) {
|
public constructor(private readonly storage: Storage) {
|
||||||
this.loadEntries().catch((error) => {
|
this.loadEntries().catch((error) => {
|
||||||
console.error("Failed to load reading log entries:", error);
|
console.error("Failed to load reading log entries:", error);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private async loadEntries() {
|
private async loadEntries() {
|
||||||
const entries = await this.plugin.storage.readJSON<ReadingLogEntry[]>(
|
const entries = await this.storage.readJSON<ReadingLogEntry[]>(
|
||||||
"reading-log.json"
|
"reading-log.json"
|
||||||
);
|
);
|
||||||
if (entries) {
|
if (entries) {
|
||||||
|
@ -63,7 +37,7 @@ export class ReadingLog {
|
||||||
|
|
||||||
private async storeEntries() {
|
private async storeEntries() {
|
||||||
this.sortEntries();
|
this.sortEntries();
|
||||||
await this.plugin.storage.writeJSON("reading-log.json", this.entries);
|
await this.storage.writeJSON("reading-log.json", this.entries);
|
||||||
}
|
}
|
||||||
|
|
||||||
public getEntries(): ReadingLogEntry[] {
|
public getEntries(): ReadingLogEntry[] {
|
|
@ -0,0 +1,34 @@
|
||||||
|
import BookTrackerPlugin from "@src/main";
|
||||||
|
import { App } from "obsidian";
|
||||||
|
|
||||||
|
export class Storage {
|
||||||
|
public constructor(
|
||||||
|
private readonly app: App,
|
||||||
|
private readonly plugin: BookTrackerPlugin
|
||||||
|
) {}
|
||||||
|
|
||||||
|
private getFilePath(filename: string): string {
|
||||||
|
return `${this.plugin.manifest.dir!!}/${filename}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async readJSON<T>(filename: string): Promise<T | null> {
|
||||||
|
const filePath = this.getFilePath(filename);
|
||||||
|
const content = await this.app.vault.adapter.read(filePath);
|
||||||
|
if (!content) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
return JSON.parse(content) as T;
|
||||||
|
} catch (error) {
|
||||||
|
console.error(`Error parsing JSON from ${filePath}:`, error);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public async writeJSON<T>(filename: string, data: T): Promise<void> {
|
||||||
|
const filePath = this.getFilePath(filename);
|
||||||
|
const content = JSON.stringify(data, null, 2);
|
||||||
|
await this.app.vault.adapter.write(filePath, content);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue