Skip to content

Commit 069e296

Browse files
committed
fix(search): set selected folder & snippet after focus on editor after search
1 parent 986f286 commit 069e296

File tree

3 files changed

+18
-7
lines changed

3 files changed

+18
-7
lines changed

src/renderer/components/editor/TheEditor.vue

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ import parserMarkdown from 'prettier/parser-markdown'
4646
import parserPostcss from 'prettier/parser-postcss'
4747
import parserYaml from 'prettier/parser-yaml'
4848
import { emitter } from '@/composable'
49+
import { useFolderStore } from '@/store/folders'
4950
5051
interface Props {
5152
lang: Language
@@ -69,6 +70,7 @@ const emit = defineEmits<Emits>()
6970
7071
const appStore = useAppStore()
7172
const snippetStore = useSnippetStore()
73+
const folderStore = useFolderStore()
7274
7375
const editorRef = ref()
7476
const cursorPosition = reactive({
@@ -136,9 +138,12 @@ const init = async () => {
136138
editor.selection.on('changeCursor', () => {
137139
getCursorPosition()
138140
})
139-
editor.on('focus', () => {
141+
editor.on('focus', async () => {
140142
if (snippetStore.searchQuery?.length) {
141143
snippetStore.searchQuery = undefined
144+
folderStore.selectId(snippetStore.selected!.folderId)
145+
await snippetStore.setSnippetsByFolderIds()
146+
emitter.emit('scroll-to:snippet', snippetStore.selectedId!)
142147
}
143148
})
144149

src/renderer/components/snippets/SnippetList.vue

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ const listRef = ref()
4545
const bodyRef = ref<HTMLElement>()
4646
const gutterRef = ref()
4747
48+
const scrollToSnippet = (id: string) => {
49+
const el = document.querySelector<HTMLElement>(`[data-id='${id}']`)
50+
if (el?.offsetTop) setScrollPosition(bodyRef.value!, el.offsetTop)
51+
}
52+
4853
onMounted(() => {
4954
interact(listRef.value).resizable({
5055
allowFrom: gutterRef.value,
@@ -62,20 +67,20 @@ onMounted(() => {
6267
6368
watch(
6469
() => appStore.isInit,
65-
() => {
66-
const el = document.querySelector<HTMLElement>(
67-
`[data-id='${snippetStore.selectedId!}']`
68-
)
69-
if (el?.offsetTop) setScrollPosition(bodyRef.value!, el.offsetTop)
70-
}
70+
() => scrollToSnippet(snippetStore.selectedId!)
7171
)
7272
7373
emitter.on('folder:click', () => {
7474
setScrollPosition(bodyRef.value!, 0)
7575
})
7676
77+
emitter.on('scroll-to:snippet', (id: string) => {
78+
scrollToSnippet(id)
79+
})
80+
7781
onUnmounted(() => {
7882
emitter.off('folder:click')
83+
emitter.off('scroll-to:snippet')
7984
})
8085
</script>
8186

src/shared/types/renderer/composable/index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@ export type EmitterEvents = {
44
'folder:click': any
55
'folder:rename': string
66
'scroll-to:folder': string
7+
'scroll-to:snippet': string
78
'search:focus': boolean
89
}

0 commit comments

Comments
 (0)