Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add delete keyboard shortcut #358

Closed
wants to merge 2 commits into from
Closed
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
3 changes: 2 additions & 1 deletion src/main/services/ipc/context-menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@ export const subscribeToContextMenu = () => {
type,
data: undefined
})
}
},
accelerator: 'CommandOrControl+Backspace'
}
]

Expand Down
16 changes: 16 additions & 0 deletions src/renderer/components/snippets/SnippetList.vue
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,15 @@
<script setup lang="ts">
import { emitter, setScrollPosition } from '@/composable'
import { useAppStore } from '@/store/app'
import { useFolderStore } from '@/store/folders'
import { useSnippetStore } from '@/store/snippets'
import { onMounted, onUnmounted, ref, watch } from 'vue'
import interact from 'interactjs'
import { store } from '@/electron'

const snippetStore = useSnippetStore()
const appStore = useAppStore()
const folderStore = useFolderStore()

const listRef = ref()
const bodyRef = ref<HTMLElement>()
Expand All @@ -50,6 +52,17 @@ const scrollToSnippet = (id: string) => {
if (el?.offsetTop) setScrollPosition(bodyRef.value!, el.offsetTop)
}

const keyboardListener = async (e: KeyboardEvent) => {
// handle ctrlcmd+backspace
if (e.key === 'Backspace' && (e.ctrlKey || e.metaKey)) {
const selectedId =
snippetStore.selectedId || snippetStore.selectedMultiple?.[0]?.id
if (!selectedId) return
const type = folderStore.selectedAlias ?? 'folder'
await snippetStore.deleteSnippetsByIdAndType(selectedId, type)
}
}

onMounted(() => {
interact(listRef.value).resizable({
allowFrom: gutterRef.value,
Expand All @@ -63,6 +76,8 @@ onMounted(() => {
store.app.set('snippetListWidth', width)
}
})

window.addEventListener('keydown', keyboardListener)
})

watch(
Expand All @@ -81,6 +96,7 @@ emitter.on('scroll-to:snippet', (id: string) => {
onUnmounted(() => {
emitter.off('folder:click')
emitter.off('scroll-to:snippet')
window.removeEventListener('keydown', keyboardListener)
})
</script>

Expand Down
40 changes: 1 addition & 39 deletions src/renderer/components/snippets/SnippetListItem.vue
Original file line number Diff line number Diff line change
Expand Up @@ -129,46 +129,8 @@ const onClickContextMenu = async () => {
selectedCount: snippetStore.selectedMultiple.length
})

const moveToTrash = async (alias?: SystemFolderAlias) => {
if (snippetStore.selectedIds.length) {
for (const id of snippetStore.selectedIds) {
await snippetStore.patchSnippetsById(id, {
isDeleted: true
})
}
} else {
await snippetStore.patchSnippetsById(props.id, {
isDeleted: true
})
}
if (!alias) {
await snippetStore.getSnippetsByFolderIds(folderStore.selectedIds!)
snippetStore.selected = snippetStore.snippets[0]
} else {
await snippetStore.getSnippets()
snippetStore.setSnippetsByAlias(alias)
}
}

if (action === 'delete') {
if (type === 'folder') {
await moveToTrash()
track('snippets/move-to-trash')
}

if (type === 'favorites' || type === 'all' || type === 'inbox') {
await moveToTrash(type)
}

if (type === 'trash') {
if (snippetStore.selectedIds.length) {
await snippetStore.deleteSnippetsByIds(snippetStore.selectedIds)
} else {
await snippetStore.deleteSnippetsById(props.id)
}
await snippetStore.getSnippets()
snippetStore.setSnippetsByAlias(type)
}
await snippetStore.deleteSnippetsByIdAndType(props.id, type as string)
}

if (action === 'duplicate') {
Expand Down
43 changes: 42 additions & 1 deletion src/renderer/store/snippets.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Language } from '../../shared/types/renderer/editor'
import type { SystemFolderAlias } from '@shared/types/renderer/sidebar'
import { sortSnippetsBy, useApi } from '@/composable'
import { i18n, store } from '@/electron'
import { i18n, store, track } from '@/electron'
import type {
Snippet,
SnippetContent,
Expand Down Expand Up @@ -300,6 +300,47 @@ export const useSnippetStore = defineStore('snippets', {
store.app.set('sort', sort)

sortSnippetsBy(this.snippets, this.sort)
},
async moveToTrash (id: string, alias?: SystemFolderAlias) {
const folderStore = useFolderStore()

if (this.selectedIds.length) {
for (const _id of this.selectedIds) {
await this.patchSnippetsById(_id, {
isDeleted: true
})
}
} else {
await this.patchSnippetsById(id, {
isDeleted: true
})
}
if (!alias) {
await this.getSnippetsByFolderIds(folderStore.selectedIds!)
this.selected = this.snippets[0]
} else {
await this.getSnippets()
this.setSnippetsByAlias(alias)
}
},
async deleteSnippetsByIdAndType (
id: string,
type?: SystemFolderAlias | 'folder' | string
) {
if (type === 'folder') {
await this.moveToTrash(id, type)
track('snippets/move-to-trash')
} else if (type === 'favorites' || type === 'all' || type === 'inbox') {
await this.moveToTrash(id, type)
} else if (type === 'trash') {
if (this.selectedIds.length) {
await this.deleteSnippetsByIds(this.selectedIds)
} else {
await this.deleteSnippetsById(id)
}
await this.getSnippets()
this.setSnippetsByAlias(type)
}
}
}
})