Skip to content

Commit 24f33c2

Browse files
committed
feat(main: ipc): add context menu for snippets
1 parent bc306fe commit 24f33c2

File tree

2 files changed

+68
-3
lines changed

2 files changed

+68
-3
lines changed

src/main/services/ipc/context-menu.ts

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import { createPopupMenu } from '../../components/menu'
22
import { dialog, ipcMain } from 'electron'
3-
import type { ContextMenuPayload, ContextMenuResponse } from '../../types'
3+
import type {
4+
ContextMenuAction,
5+
ContextMenuPayload,
6+
ContextMenuResponse
7+
} from '../../types'
48

59
export const subscribeToContextMenu = () => {
610
ipcMain.handle<ContextMenuPayload, ContextMenuResponse>(
@@ -41,4 +45,56 @@ export const subscribeToContextMenu = () => {
4145
})
4246
}
4347
)
48+
49+
ipcMain.handle<ContextMenuPayload, ContextMenuResponse>(
50+
'context-menu:snippet',
51+
async () => {
52+
return new Promise(resolve => {
53+
let action: ContextMenuAction = 'none'
54+
55+
const menu = createPopupMenu([
56+
{
57+
label: 'Add to Favorites',
58+
click: () => {
59+
action = 'favorites'
60+
resolve({
61+
action,
62+
data: {}
63+
})
64+
}
65+
},
66+
{ type: 'separator' },
67+
{
68+
label: 'Duplicate',
69+
click: () => {
70+
action = 'duplicate'
71+
resolve({
72+
action,
73+
data: {}
74+
})
75+
}
76+
},
77+
{
78+
label: 'Delete',
79+
click: () => {
80+
action = 'delete'
81+
resolve({
82+
action,
83+
data: {}
84+
})
85+
}
86+
}
87+
])
88+
89+
menu.on('menu-will-close', async () => {
90+
setImmediate(() => {
91+
resolve({
92+
action,
93+
data: {}
94+
})
95+
})
96+
})
97+
})
98+
}
99+
)
44100
}

src/main/types/index.d.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,25 @@ import type { AppStore, PreferencesStore } from '../store/module/types'
22
import type { DB, Folder, Tag, Snippet } from './db'
33

44
type ChannelSubject = 'snippet' | 'snippet-fragment' | 'folder'
5-
type ContextMenuAction = 'rename' | 'delete' | 'duplicate'
5+
6+
type ContextMenuAction =
7+
| 'rename'
8+
| 'delete'
9+
| 'duplicate'
10+
| 'favorites'
11+
| 'none'
12+
613
type CombineWithChannelSubject<
714
T extends ChannelSubject,
815
U extends string
916
> = `${U}:${T}`
17+
1018
export type ContextMenuChannel = CombineWithChannelSubject<
1119
ChannelSubject,
1220
'context-menu'
1321
>
14-
export type Channel = ContextMenuChannel
22+
23+
export type Channel = 'restart' | ContextMenuChannel
1524
export interface ContextMenuPayload {
1625
name?: string
1726
}

0 commit comments

Comments
 (0)