bug修复

This commit is contained in:
2026-05-19 13:33:48 +08:00
parent 1e53162f0d
commit 025f226205
24 changed files with 2590 additions and 1214 deletions
+29 -7
View File
@@ -7,6 +7,17 @@ import type {
SaveDialogResult
} from './ipc-types.js'
const bufferedOpenFiles: string[][] = []
let openFilesHandler: ((paths: string[]) => void) | null = null
ipcRenderer.on('myreader:open-files', (_e, paths: string[]) => {
if (openFilesHandler) {
openFilesHandler(paths)
} else {
bufferedOpenFiles.push(paths)
}
})
export interface MyReaderApi {
openFileDialog: () => Promise<OpenFileResult>
openFolderDialog: () => Promise<OpenFolderResult>
@@ -16,7 +27,6 @@ export interface MyReaderApi {
writeFile: (filePath: string, content: string) => Promise<void>
listDirectory: (dirPath: string) => Promise<ListDirResult>
parentDirectory: (dirPath: string) => Promise<string>
/** 相对当前 Markdown 文件解析内链 href → 绝对路径(外链原样返回) */
resolveRelativePath: (baseFilePath: string, href: string) => Promise<string>
openExternal: (url: string) => Promise<void>
defaultDirectory: () => Promise<string>
@@ -26,6 +36,7 @@ export interface MyReaderApi {
onRequestClose: (handler: () => void) => () => void
onMenuAction: (handler: (action: string) => void) => () => void
onOpenFiles: (handler: (paths: string[]) => void) => () => void
getPendingFilesIPC: () => Promise<string[]>
}
const api: MyReaderApi = {
@@ -67,13 +78,24 @@ const api: MyReaderApi = {
}
},
onOpenFiles: (handler: (paths: string[]) => void) => {
const listener = (_e: unknown, paths: string[]): void => {
handler(paths)
}
ipcRenderer.on('myreader:open-files', listener)
openFilesHandler = handler
return () => {
ipcRenderer.removeListener('myreader:open-files', listener)
openFilesHandler = null
}
},
getPendingFilesIPC: async (): Promise<string[]> => {
const fromMain = await ipcRenderer.invoke('myreader:get-pending-files')
const fromBuffer = bufferedOpenFiles.flat()
bufferedOpenFiles.length = 0
const seen = new Set<string>()
const merged: string[] = []
for (const p of [...fromMain, ...fromBuffer]) {
const key = p.replace(/\\/g, '/').toLowerCase()
if (seen.has(key)) continue
seen.add(key)
merged.push(p)
}
return merged
}
}
@@ -81,4 +103,4 @@ try {
contextBridge.exposeInMainWorld('myreader', api)
} catch (e) {
console.error('[MyReader] preload contextBridge failed', e)
}
}