diff --git a/src-tauri/src/pgn.rs b/src-tauri/src/pgn.rs index aa158da3..05478cd9 100644 --- a/src-tauri/src/pgn.rs +++ b/src-tauri/src/pgn.rs @@ -34,15 +34,22 @@ impl PgnParser { fn offset_by_index(&mut self, n: usize, state: &AppState, file: &String) -> io::Result<()> { let offset_index = n / GAME_OFFSET_FREQ; let n_left = n % GAME_OFFSET_FREQ; + let pgn_offsets = state.pgn_offsets.get(file).unwrap(); - let offset = match offset_index { - 0 => self.start, - _ => state.pgn_offsets.get(file).unwrap()[offset_index - 1], - }; + if offset_index == 0 || offset_index < pgn_offsets.len() { + let offset = match offset_index { + 0 => self.start, + _ => pgn_offsets[offset_index - 1], + }; - self.reader.seek(SeekFrom::Start(offset))?; + self.reader.seek(SeekFrom::Start(offset))?; + + self.skip_games(n_left)?; + } else { + self.reader.seek(SeekFrom::Start(self.start))?; + self.skip_games(n)?; + } - self.skip_games(n_left)?; Ok(()) } diff --git a/src/components/boards/BoardAnalysis.tsx b/src/components/boards/BoardAnalysis.tsx index 86ffe673..eb4cf34d 100644 --- a/src/components/boards/BoardAnalysis.tsx +++ b/src/components/boards/BoardAnalysis.tsx @@ -7,7 +7,7 @@ import { enableAllAtom, } from "@/state/atoms"; import { keyMapAtom } from "@/state/keybinds"; -import { getVariationLine } from "@/utils/chess"; +import { defaultPGN, getVariationLine } from "@/utils/chess"; import { saveToFile } from "@/utils/tabs"; import { Paper, Portal, Stack, Tabs } from "@mantine/core"; import { useHotkeys, useToggle } from "@mantine/hooks"; @@ -67,7 +67,7 @@ function BoardAnalysis() { tab: currentTab, store, }); - }, [setCurrentTab, currentTab]); + }, [setCurrentTab, currentTab, documentDir, store]); useEffect(() => { if (currentTab?.file && autoSave && dirty) { saveFile(); @@ -82,7 +82,7 @@ function BoardAnalysis() { return { ...prev }; }); reset(); - writeTextFile(currentTab?.file?.path!, "\n\n", { + writeTextFile(currentTab?.file?.path!, `\n\n${defaultPGN()}\n\n`, { append: true, }); }, [setCurrentTab, reset, currentTab?.file?.path]); diff --git a/src/utils/chess.ts b/src/utils/chess.ts index 9d397b6c..54938217 100644 --- a/src/utils/chess.ts +++ b/src/utils/chess.ts @@ -225,6 +225,10 @@ function headersToPGN(game: GameHeaders): string { return headers; } +export function defaultPGN() { + return `[Event "?"]\n[Site "?"]\n[Date "????.??.??"]\n[Round "?"]\n[White "?"]\n[Black "?"]\n[Result "*"]\n\n*`; +} + export function getPGN( tree: TreeNode, {