Skip to content

Commit

Permalink
feat(main): add context menu ipc, typing
Browse files Browse the repository at this point in the history
  • Loading branch information
antonreshetov committed Mar 31, 2022
1 parent cc806ba commit d5c11ee
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/main/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ import { store } from './store'
import { createApiServer } from './services/api/server'
import { createDb } from './services/db'
import { debounce } from 'lodash'
import { subscribeToChannels } from './services/ipc'

const isDev = process.env.NODE_ENV === 'development'

createDb()
createApiServer()
subscribeToChannels()

function createWindow () {
const bounds = store.app.get('bounds')
Expand Down
40 changes: 40 additions & 0 deletions src/main/services/ipc/context-menu.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { createPopupMenu } from '../../components/menu'
import { dialog, ipcMain } from 'electron'

export const subscribeToContextMenu = () => {
ipcMain.handle('context-menu:snippet-fragment', async (event, payload) => {
const { name } = payload

return new Promise(resolve => {
createPopupMenu([
{
label: `Rename "${name}"`,
click: () =>
resolve({
action: 'rename',
payload: payload
})
},
{ type: 'separator' },
{
label: `Delete "${name}"`,
click: () => {
const buttonId = dialog.showMessageBoxSync({
message: `Are you sure you want to permanently delete "${name}"?`,
detail: 'You cannot undo this action.',
buttons: ['Delete', 'Cancel'],
defaultId: 0,
cancelId: 1
})
if (buttonId === 0) {
resolve({
action: 'delete',
payload: payload
})
}
}
}
])
})
})
}
5 changes: 5 additions & 0 deletions src/main/services/ipc/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { subscribeToContextMenu } from './context-menu'

export const subscribeToChannels = () => {
subscribeToContextMenu()
}
20 changes: 18 additions & 2 deletions src/main/types/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,24 @@
import type { AppStore, PreferencesStore } from '../store/module/types'
import type { DB, Folder, Tag, Snippet } from './db'

interface EventCallback {
(event?: IpcRendererEvent, ...args: any[]): void
type ChannelSubject = 'snippet' | 'snippet-fragment' | 'folder'
type ContextMenuAction = 'rename' | 'delete' | 'duplicate'
type CombineWithChannelSubject<
T extends ChannelSubject,
U extends string
> = `${U}:${T}`
export type ContextMenuChannel = CombineWithChannelSubject<
ChannelSubject,
'context-menu'
>
export type Channel = ContextMenuChannel
export interface ContextMenuPayload {
name?: string
}

export interface ContextMenuResponse {
action: ContextMenuAction
data: ContextMenuPayload
}

interface StoreGet<T> {
Expand Down

0 comments on commit d5c11ee

Please sign in to comment.