Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions internal-plugins/setting/src/env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,7 @@ declare global {
add: (type: 'file' | 'folder') => Promise<{ success: boolean; error?: string }>
addByPath: (filePath: string) => Promise<{ success: boolean; error?: string }>
delete: (id: string) => Promise<{ success: boolean; error?: string }>
deleteWhenNotExist: () => Promise<{ success: boolean; error?: string }>
open: (path: string) => Promise<{ success: boolean; error?: string }>
updateAlias: (id: string, alias: string) => Promise<{ success: boolean; error?: string }>
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,31 @@ async function handleDelete(shortcut: LocalShortcut): Promise<void> {
}
}

//删除失效项目
async function handleInvalidItemDelete(): Promise<void> {
if (isDeleting.value) return
const confirmed = await confirm({
message: '确定要清除所有失效的本地启动项吗?',
type: 'warning'
})
if (!confirmed) return
isDeleting.value = true
try {
const result = await window.ztools.internal.localShortcuts.deleteWhenNotExist()
if (result.success) {
success('删除成功')
} else {
error(result.error || '删除失败')
}
} catch (err) {
console.error('删除失败:', err)
error('删除失败')
} finally {
await loadShortcuts()
isDeleting.value = false
}
}
Comment thread
Flinglin marked this conversation as resolved.

// 获取类型标签
function getTypeLabel(type: string): string {
switch (type) {
Expand Down Expand Up @@ -322,6 +347,9 @@ onMounted(() => {
<button class="btn btn-primary" :disabled="isAdding" @click="handleAdd('folder')">
{{ isAdding ? '添加中...' : '添加文件夹' }}
</button>
<button class="btn btn-primary" :disabled="isDeleting" @click="handleInvalidItemDelete()">
{{ isDeleting ? '删除中...' : '删除失效项' }}
</button>
</div>
</div>

Expand Down
2 changes: 2 additions & 0 deletions resources/preload.js
Original file line number Diff line number Diff line change
Expand Up @@ -818,6 +818,8 @@ window.ztools = {
addByPath: async (filePath) =>
await electron.ipcRenderer.invoke('local-shortcuts:add-by-path', filePath),
delete: async (id) => await electron.ipcRenderer.invoke('local-shortcuts:delete', id),
deleteWhenNotExist: async () =>
await electron.ipcRenderer.invoke('local-shortcuts:delete-when-not-exist'),
open: async (path) => await electron.ipcRenderer.invoke('local-shortcuts:open', path),
updateAlias: async (id, alias) =>
await electron.ipcRenderer.invoke('local-shortcuts:update-alias', id, alias)
Expand Down
42 changes: 42 additions & 0 deletions src/main/api/renderer/localShortcuts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ export class LocalShortcutsAPI {
this.addShortcutByPath(filePath)
)
ipcMain.handle('local-shortcuts:delete', (_event, id: string) => this.deleteShortcut(id))
ipcMain.handle('local-shortcuts:delete-when-not-exist', (_event) =>
this.deleteNotExistShortcut()
)
ipcMain.handle('local-shortcuts:open', (_event, shortcutPath: string) =>
this.openShortcut(shortcutPath)
)
Expand Down Expand Up @@ -390,6 +393,45 @@ export class LocalShortcutsAPI {
return { success: false, error: error instanceof Error ? error.message : '未知错误' }
}
}

private async deleteNotExistShortcut(): Promise<{ success: boolean; error?: string }> {
try {
const shortcuts = this.getAllShortcuts()
const checks = await Promise.all(
shortcuts.map(async (cur) => {
try {
await fs.access(cur.path)
return { shortcut: cur, exists: true }
} catch {
return { shortcut: cur, exists: false }
}
})
)

const existShortcuts: LocalShortcut[] = []
const notExistShortcuts: string[] = []

for (const check of checks) {
if (check.exists) {
existShortcuts.push(check.shortcut)
} else {
notExistShortcuts.push(check.shortcut.id)
}
}

databaseAPI.dbPut(LOCAL_SHORTCUTS_KEY, existShortcuts)

console.log('[LocalShortcut] 删除失效本地启动项成功:', notExistShortcuts.toString())

// 通知渲染进程刷新本地启动项
this.mainWindow?.webContents.send('local-shortcuts-changed')

return { success: true }
} catch (error) {
console.error('[LocalShortcut] 删除失效本地启动项失败:', error)
return { success: false, error: error instanceof Error ? error.message : '未知错误' }
}
}
Comment thread
Flinglin marked this conversation as resolved.
}

export default new LocalShortcutsAPI()
1 change: 1 addition & 0 deletions src/preload/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,7 @@ const api = {
getAll: () => ipcRenderer.invoke('local-shortcuts:get-all'),
add: (type: 'file' | 'folder') => ipcRenderer.invoke('local-shortcuts:add', type),
delete: (id: string) => ipcRenderer.invoke('local-shortcuts:delete', id),
deleteWhenNotExist: () => ipcRenderer.invoke('local-shortcuts:delete-when-not-exist'),
open: (path: string) => ipcRenderer.invoke('local-shortcuts:open', path),
updateAlias: (id: string, alias: string) =>
ipcRenderer.invoke('local-shortcuts:update-alias', id, alias)
Expand Down
1 change: 1 addition & 0 deletions src/renderer/src/env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,7 @@ declare global {
>
add: (type: 'file' | 'folder') => Promise<{ success: boolean; error?: string }>
delete: (id: string) => Promise<{ success: boolean; error?: string }>
deleteWhenNotExist: () => Promise<{ success: boolean; error?: string }>
open: (path: string) => Promise<{ success: boolean; error?: string }>
updateAlias: (id: string, alias: string) => Promise<{ success: boolean; error?: string }>
}
Expand Down