mirror of
https://github.com/priyanshujain/margin.git
synced 2026-10-02 19:17:03 +00:00
feat(model): add document model and book store
This commit is contained in:
1 parent
03a05b7e8f
commit
c76e5b0a5f
2 files changed
+129
No files matched your search
@@ -0,0 +1,48 @@
|
||||
import { create } from "zustand";
|
||||
import type { JSONContent } from "@tiptap/core";
|
||||
import { type Book, createChapter, starterBook } from "../model/book";
|
||||
|
||||
interface BookState {
|
||||
book: Book;
|
||||
activeChapterId: string;
|
||||
dirty: boolean;
|
||||
setActiveChapter: (id: string) => void;
|
||||
setChapterContent: (id: string, content: JSONContent) => void;
|
||||
setChapterTitle: (id: string, title: string) => void;
|
||||
addChapter: () => void;
|
||||
}
|
||||
|
||||
export const useBook = create<BookState>((set) => {
|
||||
const book = starterBook();
|
||||
return {
|
||||
book,
|
||||
activeChapterId: book.chapters[0].id,
|
||||
dirty: false,
|
||||
setActiveChapter: (id) => set({ activeChapterId: id }),
|
||||
setChapterContent: (id, content) =>
|
||||
set((s) => ({
|
||||
dirty: true,
|
||||
book: {
|
||||
...s.book,
|
||||
chapters: s.book.chapters.map((c) => (c.id === id ? { ...c, content } : c)),
|
||||
},
|
||||
})),
|
||||
setChapterTitle: (id, title) =>
|
||||
set((s) => ({
|
||||
dirty: true,
|
||||
book: {
|
||||
...s.book,
|
||||
chapters: s.book.chapters.map((c) => (c.id === id ? { ...c, title } : c)),
|
||||
},
|
||||
})),
|
||||
addChapter: () =>
|
||||
set((s) => {
|
||||
const chapter = createChapter();
|
||||
return {
|
||||
activeChapterId: chapter.id,
|
||||
dirty: true,
|
||||
book: { ...s.book, chapters: [...s.book.chapters, chapter] },
|
||||
};
|
||||
}),
|
||||
};
|
||||
});
|
||||
Reference in new issue
Block a user