Skip to content

Commit

Permalink
feat(main: ipc): add context menu for snippets
Browse files Browse the repository at this point in the history
  • Loading branch information
antonreshetov committed Mar 31, 2022
1 parent bc306fe commit 24f33c2
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 3 deletions.
58 changes: 57 additions & 1 deletion src/main/services/ipc/context-menu.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { createPopupMenu } from '../../components/menu'
import { dialog, ipcMain } from 'electron'
import type { ContextMenuPayload, ContextMenuResponse } from '../../types'
import type {
ContextMenuAction,
ContextMenuPayload,
ContextMenuResponse
} from '../../types'

export const subscribeToContextMenu = () => {
ipcMain.handle<ContextMenuPayload, ContextMenuResponse>(
Expand Down Expand Up @@ -41,4 +45,56 @@ export const subscribeToContextMenu = () => {
})
}
)

ipcMain.handle<ContextMenuPayload, ContextMenuResponse>(
'context-menu:snippet',
async () => {
return new Promise(resolve => {
let action: ContextMenuAction = 'none'

const menu = createPopupMenu([
{
label: 'Add to Favorites',
click: () => {
action = 'favorites'
resolve({
action,
data: {}
})
}
},
{ type: 'separator' },
{
label: 'Duplicate',
click: () => {
action = 'duplicate'
resolve({
action,
data: {}
})
}
},
{
label: 'Delete',
click: () => {
action = 'delete'
resolve({
action,
data: {}
})
}
}
])

menu.on('menu-will-close', async () => {
setImmediate(() => {
resolve({
action,
data: {}
})
})
})
})
}
)
}
13 changes: 11 additions & 2 deletions src/main/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,25 @@ import type { AppStore, PreferencesStore } from '../store/module/types'
import type { DB, Folder, Tag, Snippet } from './db'

type ChannelSubject = 'snippet' | 'snippet-fragment' | 'folder'
type ContextMenuAction = 'rename' | 'delete' | 'duplicate'

type ContextMenuAction =
| 'rename'
| 'delete'
| 'duplicate'
| 'favorites'
| 'none'

type CombineWithChannelSubject<
T extends ChannelSubject,
U extends string
> = `${U}:${T}`

export type ContextMenuChannel = CombineWithChannelSubject<
ChannelSubject,
'context-menu'
>
export type Channel = ContextMenuChannel

export type Channel = 'restart' | ContextMenuChannel
export interface ContextMenuPayload {
name?: string
}
Expand Down

0 comments on commit 24f33c2

Please sign in to comment.