Skip to content

Commit 7ca5594

Browse files
Refactor useFolderSearch and useKeyboardNavigation to unify search results type as BookmarkFolder
1 parent 5e739b9 commit 7ca5594

File tree

3 files changed

+22
-36
lines changed

3 files changed

+22
-36
lines changed

src/composables/useFolderSearch.ts

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,13 @@
11
import { ref } from 'vue';
22
import type { BookmarkFolder } from './useFolderTree';
33

4-
export interface SearchResultItem {
5-
folder: BookmarkFolder;
6-
type: 'match' | 'parent';
7-
}
8-
94
export function useFolderSearch(
105
allFolders: ReturnType<
116
typeof import('./useFolderTree').useFolderTree
127
>['allFolders'],
138
) {
149
const searchQuery = ref('');
15-
const searchResults = ref<SearchResultItem[]>([]);
10+
const searchResults = ref<BookmarkFolder[]>([]);
1611

1712
const searchFolders = (): void => {
1813
if (!searchQuery.value.trim()) {
@@ -21,18 +16,15 @@ export function useFolderSearch(
2116
}
2217

2318
const query = searchQuery.value.toLowerCase();
24-
const results: SearchResultItem[] = [];
19+
const results: BookmarkFolder[] = [];
2520
const addedIds = new Set<string>();
2621

2722
for (const folder of allFolders.value) {
2823
const titleMatch = folder.title.toLowerCase().includes(query);
2924

3025
if (titleMatch) {
3126
if (!addedIds.has(folder.id)) {
32-
results.push({
33-
folder,
34-
type: 'match',
35-
});
27+
results.push(folder);
3628
addedIds.add(folder.id);
3729
}
3830
}

src/composables/useKeyboardNavigation.ts

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { ref } from 'vue';
2-
import type { SearchResultItem } from './useFolderSearch';
2+
import type { BookmarkFolder } from './useFolderTree';
33

44
export function useKeyboardNavigation() {
55
const highlightedIndex = ref(-1);
@@ -31,9 +31,9 @@ export function useKeyboardNavigation() {
3131

3232
const handleNavigation = (
3333
event: KeyboardEvent,
34-
searchResults: SearchResultItem[],
34+
searchResults: BookmarkFolder[],
3535
callbacks: {
36-
onEnter: (item: SearchResultItem) => void;
36+
onEnter: (item: BookmarkFolder) => void;
3737
onEscape: () => void;
3838
onEmitEnter: () => void;
3939
},
@@ -54,14 +54,9 @@ export function useKeyboardNavigation() {
5454
} else if (event.key === ' ' && event.shiftKey) {
5555
event.preventDefault();
5656
const currentItem = searchResults[highlightedIndex.value];
57-
if (
58-
currentItem?.folder.children &&
59-
currentItem.folder.children.length > 0
60-
) {
57+
if (currentItem?.children && currentItem.children.length > 0) {
6158
showChildrenFor.value =
62-
showChildrenFor.value === currentItem.folder.id
63-
? null
64-
: currentItem.folder.id;
59+
showChildrenFor.value === currentItem.id ? null : currentItem.id;
6560
setTimeout(() => scrollToHighlighted(), 100);
6661
}
6762
} else if (event.key === 'Enter') {

src/entrypoints/popup/components/FolderSelector.vue

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@
2525
<div v-if="searchResults.length > 0">
2626
<div
2727
v-for="(item, index) in searchResults"
28-
:key="`${item.type}-${item.folder.id}`"
29-
:class="['dropdown-item', { highlighted: index === highlightedIndex }, item.type]"
30-
@mousedown="selectFolder(item.folder)"
28+
:key="item.id"
29+
:class="['dropdown-item', { highlighted: index === highlightedIndex }]"
30+
@mousedown="selectFolder(item)"
3131
@mouseenter="highlightedIndex = index"
3232
@keydown="handleItemKeydown($event, item)"
3333
tabindex="-1"
@@ -37,29 +37,29 @@
3737
<div class="folder-name-section">
3838
<span class="folder-icon">📁</span>
3939
<span class="folder-name">
40-
<template v-for="part in highlightText(item.folder.title, searchQuery)" :key="part.text">
40+
<template v-for="part in highlightText(item.title, searchQuery)" :key="part.text">
4141
<span v-if="part.highlighted" class="highlight">{{ part.text }}</span>
4242
<span v-else>{{ part.text }}</span>
4343
</template>
4444
</span>
4545
</div>
46-
<div v-if="item.folder.children && item.folder.children.length > 0" class="folder-actions">
46+
<div v-if="item.children && item.children.length > 0" class="folder-actions">
4747
<span class="children-count">
48-
({{ item.folder.children.length }} {{ item.folder.children.length === 1 ? i18n.t('child') : i18n.t('children') }})
48+
({{ item.children.length }} {{ item.children.length === 1 ? i18n.t('child') : i18n.t('children') }})
4949
</span>
5050
<span class="expand-hint">
5151
5252
</span>
5353
</div>
5454
</div>
55-
<div v-if="item.folder.path" class="folder-breadcrumb">
56-
{{ item.folder.path }}
55+
<div v-if="item.path" class="folder-breadcrumb">
56+
{{ item.path }}
5757
</div>
58-
<div v-if="showChildrenFor === item.folder.id && item.folder.children && item.folder.children.length > 0" class="children-list">
58+
<div v-if="showChildrenFor === item.id && item.children && item.children.length > 0" class="children-list">
5959
<div class="children-header">{{ i18n.t('contains') }}:</div>
6060
<div class="children-items">
6161
<span
62-
v-for="child in item.folder.children"
62+
v-for="child in item.children"
6363
:key="child.id"
6464
class="child-folder"
6565
@click.stop="selectChildFolder(child)"
@@ -86,7 +86,6 @@
8686
<script lang="ts" setup>
8787
import { onMounted, ref, watch } from 'vue';
8888
import { i18n } from '#i18n';
89-
import type { SearchResultItem } from '../../../composables/useFolderSearch';
9089
import { useFolderSearch } from '../../../composables/useFolderSearch';
9190
import type { BookmarkFolder } from '../../../composables/useFolderTree';
9291
import { useFolderTree } from '../../../composables/useFolderTree';
@@ -188,7 +187,7 @@ const handleKeydown = (event: KeyboardEvent) => {
188187
189188
if (showDropdown.value && searchResults.value.length > 0) {
190189
handleNavigation(event, searchResults.value, {
191-
onEnter: (item) => selectFolder(item.folder),
190+
onEnter: (item) => selectFolder(item),
192191
onEscape: () => {
193192
showDropdown.value = false;
194193
searchQuery.value = '';
@@ -212,12 +211,12 @@ const handleKeydown = (event: KeyboardEvent) => {
212211
}
213212
};
214213
215-
const handleItemKeydown = (event: KeyboardEvent, item: SearchResultItem) => {
214+
const handleItemKeydown = (event: KeyboardEvent, item: BookmarkFolder) => {
216215
if (event.key === ' ' && event.shiftKey) {
217216
event.preventDefault();
218-
if (item.folder.children && item.folder.children.length > 0) {
217+
if (item.children && item.children.length > 0) {
219218
showChildrenFor.value =
220-
showChildrenFor.value === item.folder.id ? null : item.folder.id;
219+
showChildrenFor.value === item.id ? null : item.id;
221220
}
222221
}
223222
};

0 commit comments

Comments
 (0)