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 snippets selection history #238

Merged
merged 2 commits into from
Aug 15, 2022
Merged
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
25 changes: 25 additions & 0 deletions src/main/menu/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,27 @@ const editMenu: MenuItemConstructorOptions[] = [
}
]

const historyMenu: MenuItemConstructorOptions[] = [
{
label: i18n.t('menu:history.back'),
accelerator: 'CommandOrControl+[',
click: () => {
BrowserWindow.getFocusedWindow()?.webContents.send(
'main-menu:history-back'
)
}
},
{
label: i18n.t('menu:history.forward'),
accelerator: 'CommandOrControl+]',
click: () => {
BrowserWindow.getFocusedWindow()?.webContents.send(
'main-menu:history-forward'
)
}
}
]

const menuItems: MenuItemConstructorOptions[] = [
{
label: i18n.t('menu:app.label'),
Expand All @@ -471,6 +492,10 @@ const menuItems: MenuItemConstructorOptions[] = [
label: i18n.t('menu:markdown.label'),
submenu: markdownMenu
},
{
label: i18n.t('menu:history.label'),
submenu: historyMenu
},
{
label: i18n.t('menu:help.label'),
submenu: helpMenu
Expand Down
5 changes: 5 additions & 0 deletions src/main/services/i18n/locales/en/menu.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 +75,10 @@
"label": "Markdown",
"presentationMode": "Presentation Mode",
"preview": "Preview"
},
"history": {
"label": "History",
"back": "Back",
"forward": "Forward"
}
}
5 changes: 5 additions & 0 deletions src/main/services/i18n/locales/ru/menu.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,5 +74,10 @@
"label": "Markdown",
"presentationMode": "Режим презентации",
"preview": "Просмотр Markdown"
},
"history": {
"label": "История",
"back": "Назад",
"forward": "Вперед"
}
}
10 changes: 9 additions & 1 deletion src/renderer/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ ipc.on('main:focus', () => {
ipc.on('main:app-protocol', (event, payload: string) => {
if (/^masscode:\/\/snippets/.test(payload)) {
const snippetId = payload.split('/').pop()
if (snippetId) goToSnippet(snippetId)
if (snippetId) goToSnippet(snippetId, true)
}
})

Expand Down Expand Up @@ -245,6 +245,14 @@ ipc.on('main-menu:font-size-reset', async () => {
emitter.emit('editor:refresh', true)
})

ipc.on('main-menu:history-back', async () => {
appStore.historyBack()
})

ipc.on('main-menu:history-forward', async () => {
appStore.historyForward()
})

ipc.on('api:snippet-create', (event, body: Snippet) => {
onCreateSnippet(body)
})
Expand Down
2 changes: 1 addition & 1 deletion src/renderer/components/markdown/TheMarkdown.vue
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ const onLink = async (e: Event) => {

if (el.classList.contains('snippet-link')) {
const { snippetId } = el.dataset
if (snippetId) goToSnippet(snippetId)
if (snippetId) goToSnippet(snippetId, true)
}
}

Expand Down
17 changes: 15 additions & 2 deletions src/renderer/components/sidebar/SidebarList.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<template>
<div
ref="listRef"
class="sidebar-list"
:class="{
'is-scrollable': isScrollable || modelValue === 'tags',
Expand Down Expand Up @@ -70,6 +71,7 @@ const props = withDefaults(defineProps<Props>(), {
})

const bodyRef = ref<HTMLElement>()
const listRef = ref<HTMLElement>()

const activeTab = computed({
get: () => props.modelValue,
Expand All @@ -83,10 +85,21 @@ const onClickTab = (tab: Tab) => {
}

emitter.on('scroll-to:folder', id => {
if (props.isSystem) return

nextTick(() => {
const el = document.querySelector<HTMLElement>(`[data-id='${id}']`)
if (el) {
setScrollPosition(bodyRef.value!, el.getBoundingClientRect().top - 210)

if (!el) return

const bounding = el.getBoundingClientRect()
const listHeight = listRef.value!.offsetHeight

const OFFSET = 210 // высота списка системных папок + заголовки
const SHIFT = bounding.top - OFFSET

if (SHIFT > listHeight || bounding.top < 0) {
setScrollPosition(bodyRef.value!, SHIFT)
}
})
})
Expand Down
1 change: 1 addition & 0 deletions src/renderer/components/sidebar/TheSidebar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ const onClickFolder = async (id: string) => {
folderStore.selectId(id)
await snippetStore.setSnippetsByFolderIds(true)
snippetStore.searchQuery = ''
appStore.addToHistory(snippetStore.snippets[0]?.id)
emitter.emit('folder:click', id)
}

Expand Down
3 changes: 3 additions & 0 deletions src/renderer/components/snippets/SnippetListItem.vue
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import type { SystemFolderAlias } from '@shared/types/renderer/sidebar'
import { useTagStore } from '@/store/tags'
import { isToday, format } from 'date-fns'
import { emitter } from '@/composable'
import { useAppStore } from '@/store/app'

interface Props {
id: string
Expand All @@ -59,6 +60,7 @@ const props = defineProps<Props>()
const snippetStore = useSnippetStore()
const folderStore = useFolderStore()
const tagStore = useTagStore()
const appStore = useAppStore()

const itemRef = ref()
const isFocused = ref(false)
Expand Down Expand Up @@ -105,6 +107,7 @@ const onClickSnippet = (e: MouseEvent) => {
snippetStore.selectedMultiple = []
snippetStore.getSnippetsById(props.id)
tagStore.getTags()
appStore.addToHistory(props.id)
}
}

Expand Down
6 changes: 5 additions & 1 deletion src/renderer/composable/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { useSnippetStore } from '@/store/snippets'
import { ipc, track } from '@/electron'
import type { NotificationRequest } from '@shared/types/main'
import type { Snippet, SnippetsSort } from '@shared/types/main/db'
import { useAppStore } from '@/store/app'

export const useApi = createFetch({
baseUrl: `http://localhost:${API_PORT}`
Expand Down Expand Up @@ -86,11 +87,12 @@ export const onCopySnippet = () => {
track('snippets/copy')
}

export const goToSnippet = async (snippetId: string) => {
export const goToSnippet = async (snippetId: string, history?: boolean) => {
if (!snippetId) return

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

const snippet = snippetStore.findSnippetById(snippetId)

Expand All @@ -105,6 +107,8 @@ export const goToSnippet = async (snippetId: string) => {
await snippetStore.getSnippetsById(snippetId)
await snippetStore.setSnippetsByFolderIds()

if (history) appStore.history.push(snippetId)

emitter.emit('folder:click', snippet.folderId)
emitter.emit('scroll-to:snippet', snippetId)
emitter.emit('scroll-to:folder', snippet.folderId)
Expand Down
26 changes: 26 additions & 0 deletions src/renderer/store/app.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { goToSnippet } from '@/composable'
import { platform, store } from '@/electron'
import type { MarkdownSettings } from '@shared/types/main/store'
import type {
Expand Down Expand Up @@ -39,6 +40,8 @@ const MARKDOWN_DEFAULTS: MarkdownSettings = {
codeRenderer: 'highlight.js'
}

const HISTORY_LIMIT = 50

export const useAppStore = defineStore('app', {
state: (): State => ({
isInit: false,
Expand All @@ -64,6 +67,8 @@ export const useAppStore = defineStore('app', {
markdown: MARKDOWN_DEFAULTS,
selectedPreferencesMenu: 'storage',
language: store.preferences.get('language'),
history: [],
historyIndex: 0,
version,
platform: platform()
}),
Expand Down Expand Up @@ -91,6 +96,27 @@ export const useAppStore = defineStore('app', {
setLang (lang: string) {
this.language = lang
store.preferences.set('language', lang)
},
addToHistory (snippetId: string) {
if (!snippetId) return
if (this.history[this.history.length - 1] === snippetId) return

if (this.history.length === HISTORY_LIMIT) this.history.shift()

this.history.push(snippetId)
this.historyIndex = this.history.length - 1
},
historyBack () {
if (this.historyIndex === 0) return

this.historyIndex = this.historyIndex - 1
goToSnippet(this.history[this.historyIndex])
},
historyForward () {
if (this.historyIndex === this.history.length - 1) return

this.historyIndex = this.historyIndex + 1
goToSnippet(this.history[this.historyIndex])
}
}
})
3 changes: 3 additions & 0 deletions src/renderer/views/Main.vue
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ const init = async () => {

if (storedSnippetId) {
snippetStore.setSnippetById(storedSnippetId)
appStore.addToHistory(storedSnippetId)
} else {
appStore.addToHistory(snippetStore.snippets[0]?.id)
}

if (storedFolderId) {
Expand Down
2 changes: 2 additions & 0 deletions src/shared/types/main/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ type MainMenuAction =
| 'font-size-increase'
| 'font-size-decrease'
| 'font-size-reset'
| 'history-back'
| 'history-forward'

type MainAction =
| 'restart'
Expand Down
2 changes: 2 additions & 0 deletions src/shared/types/renderer/store/app.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ export interface State {
markdown: MarkdownSettings
codePreview: CodePreviewSettings
language: string
history: string[]
historyIndex: number
isInit: boolean
isSponsored: boolean
}