Skip to content
Prev Previous commit
Next Next commit
feat(snippets): add copy link to context menu
  • Loading branch information
antonreshetov committed Aug 13, 2022
commit c6dc63c577f0bc1eaed3d6a30e4b4502e90d4b82
3 changes: 2 additions & 1 deletion src/main/services/i18n/locales/en/common.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,6 @@
"show": "Show",
"collapse-all": "Collapse All",
"expand-all": "Expand All",
"restore": "Restore"
"restore": "Restore",
"copy-snippet-link": "Copy Snippet Link"
}
3 changes: 2 additions & 1 deletion src/main/services/i18n/locales/ru/common.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,6 @@
"show": "Show",
"collapse-all": "Закрыть все",
"expand-all": "Открыть все",
"restore": "Восстановить"
"restore": "Восстановить",
"copy-snippet-link": "Скопировать ссылку"
}
11 changes: 11 additions & 0 deletions src/main/services/ipc/context-menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,17 @@ export const subscribeToContextMenu = () => {
}
},
{ type: 'separator' },
{
label: i18n.t('copy-snippet-link'),
click: () => {
resolve({
action: 'copy-snippet-link',
type,
data: true
})
}
},
{ type: 'separator' },
{
label: i18n.t('duplicate'),
click: () => {
Expand Down
10 changes: 9 additions & 1 deletion src/renderer/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ import {
onCopySnippet,
emitter,
onCreateSnippet,
onAddDescription
onAddDescription,
goToSnippet
} from '@/composable'
import { useRoute } from 'vue-router'
import type { Snippet } from '@shared/types/main/db'
Expand Down Expand Up @@ -164,6 +165,13 @@ ipc.on('main:focus', () => {
showSupportToast()
})

ipc.on('main:app-protocol', (event, payload: string) => {
if (/^masscode:\/\/snippets/.test(payload)) {
const snippetId = payload.split('/').pop()
if (snippetId) goToSnippet(snippetId)
}
})

ipc.on('main-menu:preferences', () => {
router.push('/preferences')
})
Expand Down
8 changes: 7 additions & 1 deletion src/renderer/components/snippets/SnippetListItem.vue
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import type {
ContextMenuRequest,
ContextMenuResponse
} from '@shared/types/main'
import { onClickOutside } from '@vueuse/core'
import { onClickOutside, useClipboard } from '@vueuse/core'
import { computed, ref } from 'vue'
import type { SystemFolderAlias } from '@shared/types/renderer/sidebar'
import { useTagStore } from '@/store/tags'
Expand Down Expand Up @@ -225,6 +225,12 @@ const onClickContextMenu = async () => {
track('snippets/restore-from-trash')
}

if (action === 'copy-snippet-link') {
const { copy } = useClipboard({ source: `masscode://snippets/${props.id}` })
copy()
track('snippets/copy-link')
}

isHighlighted.value = false
isFocused.value = false
snippetStore.isContextState = false
Expand Down
20 changes: 20 additions & 0 deletions src/renderer/composable/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,26 @@ export const onCopySnippet = () => {
track('snippets/copy')
}

export const goToSnippet = async (snippetId: string) => {
if (!snippetId) return

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

const snippet = snippetStore.findSnippetById(snippetId)

if (!snippet) return

folderStore.selectId(snippet.folderId)

snippetStore.fragment = 0

await snippetStore.getSnippetsById(snippetId)
await snippetStore.setSnippetsByFolderIds()

emitter.emit('folder:click', snippet.folderId)
}

export const setScrollPosition = (el: HTMLElement, offset: number) => {
const ps = el.querySelector('.ps')
if (ps) ps.scrollTop = offset
Expand Down
7 changes: 5 additions & 2 deletions src/renderer/store/snippets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export const useSnippetStore = defineStore('snippets', {
const tagStore = useTagStore()
const tags: Tag[] = []

if (this.selected?.tagsIds.length) {
if (this.selected?.tagsIds?.length) {
this.selected.tagsIds.forEach(i => {
const tag = tagStore.tags.find(t => t.id === i)
if (tag) tags.push(tag)
Expand Down Expand Up @@ -270,9 +270,12 @@ export const useSnippetStore = defineStore('snippets', {
this.selected = snippets[0]
},
setSnippetById (id: string) {
const snippet = this.all.find(i => i.id === id)
const snippet = this.findSnippetById(id)
if (snippet) this.selected = snippet
},
findSnippetById (id: string) {
return this.all.find(i => i.id === id)
},
async emptyTrash () {
const ids = this.all.filter(i => i.isDeleted).map(i => i.id)
await this.deleteSnippetsByIds(ids)
Expand Down
1 change: 1 addition & 0 deletions src/shared/types/main/analytics.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ type SnippetEvents =
| 'add-to-favorites'
| 'code-preview'
| 'copy'
| 'copy-link'
| 'create-screenshot'
| 'delete-fragment'
| 'delete-from-favorites'
Expand Down
1 change: 1 addition & 0 deletions src/shared/types/main/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type ContextMenuAction =
| 'collapse-all'
| 'expand-all'
| 'restore-from-trash'
| 'copy-snippet-link'
| 'none'

export type ContextMenuType =
Expand Down