Skip to content

Commit 2f42a31

Browse files
committed
fix: 修复设置搜索文案与子项定位 (Closes #385)
1 parent ee8375e commit 2f42a31

12 files changed

Lines changed: 275 additions & 31 deletions

File tree

‎src/components/settings/SettingsContent.vue‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,23 @@ const onCategorySelect = (id: string) => {
3636
nextTick(() => scrollRef.value?.scrollTo({ top: 0 }));
3737
};
3838
39-
const onSearchSelect = (categoryId: string, itemKey: string) => {
39+
/**
40+
* 定位设置项,子项收起时定位最近的父项
41+
* @param categoryId - 设置分类
42+
* @param itemKey - 设置项键名
43+
* @param parentKeys - 从外到内的父项键名
44+
*/
45+
const onSearchSelect = (categoryId: string, itemKey: string, parentKeys: string[] = []) => {
4046
highlightKey.value = itemKey;
4147
if (activeId.value !== categoryId) {
4248
activeId.value = categoryId;
4349
}
4450
nextTick(() => {
4551
setTimeout(() => {
46-
const el = document.getElementById(`setting-${itemKey}`);
52+
const el = [itemKey, ...parentKeys.toReversed()]
53+
.map((key) => document.getElementById(`setting-${key}`))
54+
.find((element) => element !== null);
55+
if (el) highlightKey.value = el.id.slice("setting-".length);
4756
el?.scrollIntoView({ block: "center", behavior: "smooth" });
4857
setTimeout(() => {
4958
highlightKey.value = undefined;

‎src/components/settings/SettingsItem.vue‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { dialog } from "@/composables/useDialog";
55
66
const props = defineProps<{
77
item: SettingItem;
8-
highlighted?: boolean;
8+
highlightKey?: string;
99
}>();
1010
1111
const { t } = useI18n();
@@ -56,12 +56,12 @@ const descriptionText = computed(() =>
5656
v-if="item.type === 'custom' && item.fullWidth && item.component"
5757
v-bind="item.componentProps"
5858
class="transition-all duration-300"
59-
:class="highlighted ? 'animate-highlight-pulse' : ''"
59+
:class="highlightKey === item.key ? 'animate-highlight-pulse' : ''"
6060
/>
6161
<div
6262
v-else
6363
class="flex items-center justify-between gap-4 rounded-xl bg-surface-panel border border-solid border-outline-variant/15 px-4 py-3.5 transition-all duration-300"
64-
:class="highlighted ? 'animate-highlight-pulse' : ''"
64+
:class="highlightKey === item.key ? 'animate-highlight-pulse' : ''"
6565
>
6666
<div class="min-w-0 flex-1">
6767
<div class="flex items-center gap-2 text-base">
@@ -160,7 +160,12 @@ const descriptionText = computed(() =>
160160
class="mt-2.5 flex flex-col gap-2.5 transition-opacity duration-200"
161161
:class="isChildrenActive ? '' : 'opacity-50 pointer-events-none'"
162162
>
163-
<SettingsItem v-for="child in item.children" :key="child.key" :item="child" />
163+
<SettingsItem
164+
v-for="child in item.children"
165+
:key="child.key"
166+
:item="child"
167+
:highlight-key="highlightKey"
168+
/>
164169
</div>
165170
</div>
166171
</template>
Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
import { mount, type VueWrapper } from "@vue/test-utils";
2+
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
3+
import { createI18n } from "vue-i18n";
4+
import { nextTick } from "vue";
5+
import type { SettingCategory } from "@/types/settings-schema";
6+
import zhCN from "@/i18n/locales/zh-CN.json";
7+
import enUS from "@/i18n/locales/en-US.json";
8+
import SInput from "@/components/ui/SInput.vue";
9+
import SettingsSearch from "./SettingsSearch.vue";
10+
11+
const fixtures = vi.hoisted(() => ({ schema: [] as SettingCategory[] }));
12+
vi.mock("@/settings/schema", () => ({ settingsSchema: fixtures.schema }));
13+
14+
let wrapper: VueWrapper;
15+
16+
/**
17+
* 挂载指定语言的设置搜索
18+
* @param locale - 界面语言
19+
*/
20+
const mountSearch = (locale: "zh-CN" | "en-US" = "zh-CN") => {
21+
const i18n = createI18n({
22+
legacy: false,
23+
locale,
24+
messages: { "zh-CN": zhCN, "en-US": enUS },
25+
});
26+
wrapper = mount(SettingsSearch, {
27+
global: {
28+
plugins: [i18n],
29+
components: { SInput },
30+
stubs: { IconLucideSearch: true, IconLucideX: true },
31+
},
32+
});
33+
return i18n;
34+
};
35+
36+
/**
37+
* 输入搜索词
38+
* @param query - 搜索词
39+
*/
40+
const search = async (query: string) => {
41+
await wrapper.get("input").trigger("focus");
42+
await wrapper.get("input").setValue(query);
43+
};
44+
45+
beforeEach(() => {
46+
fixtures.schema.splice(0, fixtures.schema.length, {
47+
id: "other",
48+
icon: {},
49+
sections: [
50+
{
51+
id: "platformLogin",
52+
items: [
53+
{
54+
key: "qmAccount",
55+
type: "custom",
56+
descriptionKey: "settings.platformLogin.desc",
57+
keywords: ["settings.platformLogin.title"],
58+
},
59+
{
60+
key: "kgAccount",
61+
type: "custom",
62+
descriptionKey: "settings.platformLogin.desc",
63+
keywords: ["settings.platformLogin.title"],
64+
},
65+
{ key: "downloadDir", type: "custom" },
66+
{
67+
key: "pluginImport",
68+
type: "custom",
69+
descriptionKey: "settings.plugins.hint",
70+
},
71+
],
72+
},
73+
],
74+
});
75+
});
76+
77+
afterEach(() => wrapper.unmount());
78+
79+
describe("设置搜索文案", () => {
80+
it.each(["zh-CN", "en-US"] as const)("%s 显示账号标题和组件共用的描述", async (locale) => {
81+
mountSearch(locale);
82+
await search(locale === "zh-CN" ? "登录" : "account");
83+
const messages = locale === "zh-CN" ? zhCN : enUS;
84+
expect(wrapper.text()).toContain(messages.settings.qmAccount.label);
85+
expect(wrapper.text()).toContain(messages.settings.kgAccount.label);
86+
expect(wrapper.text()).toContain(messages.settings.platformLogin.desc);
87+
expect(wrapper.text()).not.toContain("settings.");
88+
});
89+
90+
it.each(["zh-CN", "en-US"] as const)("%s 没有描述的下载目录仍可搜索", async (locale) => {
91+
mountSearch(locale);
92+
const messages = locale === "zh-CN" ? zhCN : enUS;
93+
await search(messages.settings.downloadDir.label);
94+
expect(wrapper.findAll(".font-medium").map((item) => item.text())).toEqual([
95+
messages.settings.downloadDir.label,
96+
]);
97+
expect(wrapper.find(".truncate").exists()).toBe(false);
98+
});
99+
100+
it("缺失的标题、描述和关键词不会显示或参与匹配", async () => {
101+
fixtures.schema[0].sections![0].items.push(
102+
{ key: "missingTitle", type: "custom", keywords: ["settings.platformLogin.title"] },
103+
{ key: "pluginList", type: "custom", keywords: ["settings.missingKeyword"] },
104+
);
105+
mountSearch();
106+
await search("登录");
107+
expect(wrapper.findAll(".font-medium")).toHaveLength(2);
108+
await search("settings");
109+
expect(wrapper.findAll(".font-medium")).toHaveLength(0);
110+
expect(wrapper.text()).toContain(zhCN.common.noData);
111+
});
112+
113+
it("切换语言后更新已有搜索结果", async () => {
114+
const i18n = mountSearch();
115+
await search("QQ");
116+
expect(wrapper.text()).toContain(zhCN.settings.qmAccount.label);
117+
i18n.global.locale.value = "en-US";
118+
await nextTick();
119+
expect(wrapper.text()).toContain(enUS.settings.qmAccount.label);
120+
expect(wrapper.text()).not.toContain(zhCN.settings.qmAccount.label);
121+
});
122+
123+
it("递归搜索子项并携带父项供收起时定位", async () => {
124+
fixtures.schema[0].sections![0].items.push({
125+
key: "appearanceStyle",
126+
type: "select",
127+
searchable: false,
128+
hideChildren: true,
129+
children: [{ key: "backgroundBlur", type: "slider" }],
130+
});
131+
mountSearch();
132+
await search(zhCN.settings.backgroundBlur.label);
133+
await wrapper.get(".top-full .cursor-pointer").trigger("mousedown");
134+
expect(wrapper.emitted("select")).toEqual([["other", "backgroundBlur", ["appearanceStyle"]]]);
135+
expect((wrapper.get("input").element as HTMLInputElement).value).toBe("");
136+
});
137+
138+
it("隐藏的分区和设置项连同子项不参与搜索", async () => {
139+
fixtures.schema[0].sections![0].items[0].visible = () => false;
140+
fixtures.schema[0].sections!.push({
141+
id: "appearanceStyle",
142+
visible: () => false,
143+
items: [{ key: "qmAccount", type: "custom" }],
144+
});
145+
fixtures.schema[0].sections![0].items.push({
146+
key: "appearanceStyle",
147+
type: "select",
148+
visible: () => false,
149+
children: [{ key: "qmAccount", type: "custom" }],
150+
});
151+
mountSearch();
152+
await search("QQ");
153+
expect(wrapper.findAll(".font-medium")).toHaveLength(0);
154+
});
155+
156+
it("只有空格时不遮挡设置菜单", async () => {
157+
mountSearch();
158+
await search(" ");
159+
expect(wrapper.emitted("active-change")).toBeUndefined();
160+
expect(wrapper.find(".top-full").exists()).toBe(false);
161+
});
162+
});

‎src/components/settings/SettingsSearch.vue‎

Lines changed: 44 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,24 @@
11
<script setup lang="ts">
22
import { settingsSchema } from "@/settings/schema";
3+
import type { SettingItem } from "@/types/settings-schema";
34
45
const emit = defineEmits<{
5-
select: [categoryId: string, itemKey: string];
6+
select: [categoryId: string, itemKey: string, parentKeys: string[]];
67
"active-change": [active: boolean];
78
}>();
89
9-
const { t } = useI18n();
10+
const { t, te } = useI18n();
1011
const query = ref("");
1112
const isFocused = ref(false);
1213
13-
const isSearchActive = computed(() => isFocused.value && query.value.length > 0);
14+
const isSearchActive = computed(() => isFocused.value && query.value.trim().length > 0);
1415
1516
watch(isSearchActive, (v) => emit("active-change", v));
1617
1718
interface SearchResult {
1819
categoryId: string;
1920
itemKey: string;
21+
parentKeys: string[];
2022
label: string;
2123
description: string;
2224
categoryLabel: string;
@@ -27,31 +29,54 @@ const results = computed<SearchResult[]>(() => {
2729
if (!q) return [];
2830
const out: SearchResult[] = [];
2931
for (const cat of settingsSchema) {
30-
for (const sec of cat.sections ?? []) {
31-
if (sec.visible && !sec.visible()) continue;
32-
for (const item of sec.items) {
32+
/**
33+
* 检索设置项及子项
34+
* @param items - 当前层设置项
35+
* @param parentKeys - 从外到内的父项键名
36+
*/
37+
const searchItems = (items: SettingItem[], parentKeys: string[]): void => {
38+
for (const item of items) {
3339
if (item.visible && !item.visible()) continue;
34-
if (item.searchable === false) continue;
35-
const label = t(`settings.${item.key}.label`);
36-
const desc = item.hideDescription ? "" : t(`settings.${item.key}.description`);
37-
const kw = item.keywords?.map((k) => t(k)).join(" ") ?? "";
38-
if (`${label} ${desc} ${kw}`.toLowerCase().includes(q)) {
39-
out.push({
40-
categoryId: cat.id,
41-
itemKey: item.key,
42-
label,
43-
description: desc,
44-
categoryLabel: t(`settings.group.${cat.id}`),
45-
});
40+
if (item.searchable !== false) {
41+
const labelKey = `settings.${item.key}.label`;
42+
const descriptionKey = item.descriptionKey ?? `settings.${item.key}.description`;
43+
const label = te(labelKey) ? t(labelKey) : "";
44+
const desc = !item.hideDescription && te(descriptionKey) ? t(descriptionKey) : "";
45+
const kw =
46+
item.keywords
47+
?.filter((key) => te(key))
48+
.map((key) => t(key))
49+
.join(" ") ?? "";
50+
if (label && `${label} ${desc} ${kw}`.toLowerCase().includes(q)) {
51+
out.push({
52+
categoryId: cat.id,
53+
itemKey: item.key,
54+
parentKeys,
55+
label,
56+
description: desc,
57+
categoryLabel: t(`settings.group.${cat.id}`),
58+
});
59+
}
60+
}
61+
if (item.children) {
62+
searchItems(item.children, [...parentKeys, item.key]);
4663
}
4764
}
65+
};
66+
for (const sec of cat.sections ?? []) {
67+
if (sec.visible && !sec.visible()) continue;
68+
searchItems(sec.items, []);
4869
}
4970
}
5071
return out;
5172
});
5273
74+
/**
75+
* 定位搜索结果并清空输入
76+
* @param r - 选中的设置项
77+
*/
5378
const handleSelect = (r: SearchResult) => {
54-
emit("select", r.categoryId, r.itemKey);
79+
emit("select", r.categoryId, r.itemKey, r.parentKeys);
5580
query.value = "";
5681
};
5782

‎src/components/settings/SettingsSection.vue‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ const itemStyle = (i: number) => {
4949
v-for="(item, i) in visibleItems"
5050
:key="item.key"
5151
:item="item"
52-
:highlighted="item.key === highlightKey"
52+
:highlight-key="highlightKey"
5353
class="animate-slide-in-item"
5454
:style="itemStyle(i + 1)"
5555
/>

‎src/i18n/locales/en-US.json‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1724,6 +1724,9 @@
17241724
"label": "Sync Loves",
17251725
"description": "Mirror your likes as Last.fm Love / Unlove"
17261726
},
1727+
"lastfmAccount": {
1728+
"label": "Last.fm Account"
1729+
},
17271730
"lastfm": {
17281731
"connect": "Connect account",
17291732
"connecting": "Finish authorization in your browser…",
@@ -1740,6 +1743,12 @@
17401743
"disconnected": "Disconnected from Last.fm"
17411744
}
17421745
},
1746+
"qmAccount": {
1747+
"label": "QQ Music Account"
1748+
},
1749+
"kgAccount": {
1750+
"label": "Kugou Music Account"
1751+
},
17431752
"platformLogin": {
17441753
"title": "Log in to {name} Account",
17451754
"desc": "Log in to retrieve playback streams directly from the platform",
@@ -1841,6 +1850,9 @@
18411850
"label": "Wake this app via the Orpheus protocol",
18421851
"description": "This protocol is normally used by the web player to launch the official client; enabling it may prevent the official client from being launched"
18431852
},
1853+
"streamingServerList": {
1854+
"label": "Media Source Management"
1855+
},
18441856
"streamingEnabled": {
18451857
"label": "Enable streaming",
18461858
"description": "When disabled, the media sources entry is hidden from the sidebar"
@@ -1977,6 +1989,12 @@
19771989
"confirmDesc": "All playlists, sign-in state and caches will be wiped and settings reset. This cannot be undone.",
19781990
"done": "All data has been cleared"
19791991
},
1992+
"pluginImport": {
1993+
"label": "Import Plugins"
1994+
},
1995+
"pluginList": {
1996+
"label": "Installed Plugins"
1997+
},
19801998
"plugins": {
19811999
"securityWarningTitle": "Mind the security risks before installing",
19822000
"securityWarning": "Plugins are third-party code. The app sandboxes them as much as possible, but the risk cannot be fully eliminated — a malicious plugin may access your data or perform dangerous actions. Only install plugins from sources you trust.",

0 commit comments

Comments
 (0)