mirror of
https://github.com/priyanshujain/margin.git
synced 2026-10-03 03:27:03 +00:00
changes
This commit is contained in:
1 parent
c86d5a7d5c
commit
0e0d334f6a
33 files changed
+401
-101
No files matched your search
+18
-5
@@ -4,6 +4,7 @@ import {
|
||||
type Book,
|
||||
type BookMetadata,
|
||||
type BookSettings,
|
||||
type ChapterKind,
|
||||
type Cover,
|
||||
chapterKind,
|
||||
createChapter,
|
||||
@@ -17,6 +18,10 @@ interface BookState {
|
||||
book: Book | null;
|
||||
activeChapterId: string;
|
||||
dirty: boolean;
|
||||
exporting: string | null;
|
||||
notice: string | null;
|
||||
setExporting: (label: string | null) => void;
|
||||
setNotice: (message: string | null) => void;
|
||||
openBook: (book: Book) => void;
|
||||
closeBook: () => void;
|
||||
setActiveChapter: (id: string) => void;
|
||||
@@ -25,7 +30,7 @@ interface BookState {
|
||||
addChapter: () => void;
|
||||
addPage: (group: "front" | "back", title: string) => void;
|
||||
deleteChapter: (id: string) => void;
|
||||
moveChapter: (from: number, to: number) => void;
|
||||
moveChapter: (from: number, to: number, toKind?: ChapterKind) => void;
|
||||
setMetadata: (patch: Partial<BookMetadata>) => void;
|
||||
setSettings: (patch: Partial<BookSettings>) => void;
|
||||
setCover: (patch: Partial<Cover>) => void;
|
||||
@@ -36,6 +41,10 @@ export const useBook = create<BookState>((set) => ({
|
||||
book: null,
|
||||
activeChapterId: "",
|
||||
dirty: false,
|
||||
exporting: null,
|
||||
notice: null,
|
||||
setExporting: (label) => set({ exporting: label }),
|
||||
setNotice: (message) => set({ notice: message }),
|
||||
openBook: (book) => {
|
||||
const normalized = normalizeBook(book);
|
||||
set({ book: normalized, activeChapterId: normalized.chapters[0]?.id ?? "", dirty: false });
|
||||
@@ -83,13 +92,17 @@ export const useBook = create<BookState>((set) => ({
|
||||
s.activeChapterId === id ? (chapters[index] ?? chapters[index - 1])?.id ?? COVER_ID : s.activeChapterId;
|
||||
return { dirty: true, activeChapterId, book: { ...s.book, chapters } };
|
||||
}),
|
||||
moveChapter: (from, to) =>
|
||||
moveChapter: (from, to, toKind) =>
|
||||
set((s) => {
|
||||
if (!s.book || from === to) return {};
|
||||
if (!s.book) return {};
|
||||
const chapters = [...s.book.chapters];
|
||||
if (from < 0 || from >= chapters.length || to < 0 || to >= chapters.length) return {};
|
||||
if (from < 0 || from >= chapters.length) return {};
|
||||
const target = chapters[from];
|
||||
const nextKind = toKind ?? chapterKind(target);
|
||||
const dest = Math.max(0, Math.min(from < to ? to - 1 : to, chapters.length - 1));
|
||||
if (from === dest && chapterKind(target) === nextKind) return {};
|
||||
const [moved] = chapters.splice(from, 1);
|
||||
chapters.splice(to, 0, moved);
|
||||
chapters.splice(dest, 0, nextKind === "body" ? { ...moved, kind: undefined } : { ...moved, kind: nextKind });
|
||||
return { dirty: true, book: { ...s.book, chapters } };
|
||||
}),
|
||||
setMetadata: (patch) =>
|
||||
|
||||
Reference in new issue
Block a user