|
| 1 | +import type { MathNotebookStore } from '../../store/types' |
| 2 | +import { ipcMain } from 'electron' |
| 3 | +import { |
| 4 | + ensureSpaceDirectory, |
| 5 | + getSpaceStatePath, |
| 6 | +} from '../../storage/providers/markdown/runtime/spaces' |
| 7 | +import { |
| 8 | + readSpaceState, |
| 9 | + writeSpaceState, |
| 10 | +} from '../../storage/providers/markdown/runtime/spaceState' |
| 11 | +import { store } from '../../store' |
| 12 | + |
| 13 | +function getVaultPath(): string | null { |
| 14 | + return store.preferences.get('storage.vaultPath') as string | null |
| 15 | +} |
| 16 | + |
| 17 | +function isMarkdownEngine(): boolean { |
| 18 | + return store.preferences.get('storage.engine') === 'markdown' |
| 19 | +} |
| 20 | + |
| 21 | +export function registerSpacesHandlers() { |
| 22 | + ipcMain.handle('spaces:math:read', () => { |
| 23 | + if (!isMarkdownEngine()) { |
| 24 | + return { |
| 25 | + sheets: store.mathNotebook.get('sheets') ?? [], |
| 26 | + activeSheetId: store.mathNotebook.get('activeSheetId') ?? null, |
| 27 | + } satisfies MathNotebookStore |
| 28 | + } |
| 29 | + |
| 30 | + const vaultPath = getVaultPath() |
| 31 | + if (!vaultPath) { |
| 32 | + return { |
| 33 | + sheets: [], |
| 34 | + activeSheetId: null, |
| 35 | + } satisfies MathNotebookStore |
| 36 | + } |
| 37 | + |
| 38 | + ensureSpaceDirectory(vaultPath, 'math') |
| 39 | + const statePath = getSpaceStatePath(vaultPath, 'math') |
| 40 | + const state = readSpaceState<MathNotebookStore>(statePath) |
| 41 | + |
| 42 | + if (state) { |
| 43 | + return { |
| 44 | + sheets: Array.isArray(state.sheets) ? state.sheets : [], |
| 45 | + activeSheetId: state.activeSheetId ?? null, |
| 46 | + } satisfies MathNotebookStore |
| 47 | + } |
| 48 | + |
| 49 | + // Migration: read from electron-store, write to vault |
| 50 | + const legacy: MathNotebookStore = { |
| 51 | + sheets: store.mathNotebook.get('sheets') ?? [], |
| 52 | + activeSheetId: store.mathNotebook.get('activeSheetId') ?? null, |
| 53 | + } |
| 54 | + |
| 55 | + if (legacy.sheets.length > 0) { |
| 56 | + writeSpaceState(statePath, legacy) |
| 57 | + } |
| 58 | + |
| 59 | + return legacy |
| 60 | + }) |
| 61 | + |
| 62 | + ipcMain.handle('spaces:math:write', (_, data: MathNotebookStore) => { |
| 63 | + if (!isMarkdownEngine()) { |
| 64 | + store.mathNotebook.set('sheets', data.sheets) |
| 65 | + store.mathNotebook.set('activeSheetId', data.activeSheetId) |
| 66 | + return |
| 67 | + } |
| 68 | + |
| 69 | + const vaultPath = getVaultPath() |
| 70 | + if (!vaultPath) { |
| 71 | + return |
| 72 | + } |
| 73 | + |
| 74 | + ensureSpaceDirectory(vaultPath, 'math') |
| 75 | + const statePath = getSpaceStatePath(vaultPath, 'math') |
| 76 | + writeSpaceState(statePath, data) |
| 77 | + }) |
| 78 | +} |
0 commit comments