Skip to content

Commit d764645

Browse files
committed
调整代码写法
1 parent a2db510 commit d764645

3 files changed

Lines changed: 21 additions & 24 deletions

File tree

app/src/config/index.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,7 @@ const openSettingDialog = (app: App, initialTab: TConfigTab = "editor") => {
7474
});
7575
dialog.element.setAttribute("data-key", Constants.DIALOG_SETTING);
7676

77-
const tabWrap = dialog.element.querySelector(".config__tab-wrap") as HTMLElement | null;
78-
if (!tabWrap) {
79-
console.error("openSettingDialog: .config__tab-wrap not found");
80-
dialog.destroy();
81-
return;
82-
}
77+
const tabWrap = dialog.element.querySelector(".config__tab-wrap") as HTMLElement;
8378
bindSettingSaveDelegation(tabWrap);
8479
initConfigSearch(dialog.element, app);
8580
(dialog.element.querySelector(".b3-dialog__container") as HTMLElement).style.maxWidth = "1280px";

app/src/config/search.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type {TConfigTab} from "./types";
22
import {getConfigTabDefs} from "./tabs";
3-
import {collectSettingTabSearchStrings, textMatchesConfigSearch} from "./ui/search";
3+
import {collectSettingTabSearchStrings, textMatchesSearch} from "./ui/search";
44
import {buildEditorSections, editorSettings} from "./editor";
55
import {buildFileSections, fileSettings} from "./file";
66
import {buildAppearanceSections, appearanceSettings} from "./appearance";
@@ -270,6 +270,8 @@ const getTabSearchStrings = (tabId: TConfigTab): string[] => {
270270
export const initConfigSearch = (element: HTMLElement, app: App) => {
271271
const tabSearchStrings = getConfigTabDefs().map((def) => ({
272272
id: def.id,
273+
// TODO build*Sections() 的结果是动态的,需要单独给每一个 sections 保存一个缓存,
274+
// 每次 build*Sections() 之后都更新缓存,搜索时直接使用缓存。
273275
strings: getTabSearchStrings(def.id),
274276
}));
275277
const inputElement = element.querySelector(".b3-form__icon input") as HTMLInputElement;
@@ -294,7 +296,7 @@ export const initConfigSearch = (element: HTMLElement, app: App) => {
294296
}
295297
// TODO 在把所有设置项都改成注册式之后,把 .toLowerCase() 移到对应的收集文案的函数里只处理一次,而不是在这里反复处理
296298
// TODO 预先将含 HTML 的文案转为纯文本(比如 innerText 或 textContent),避免命中 HTML 标签中的文本(例如搜索 "code" 会命中包含 <code> 标签的文案)
297-
if (textMatchesConfigSearch(subItem, keywords)) {
299+
if (textMatchesSearch(subItem, keywords)) {
298300
matchedTabIds.add(id);
299301
break;
300302
}

app/src/config/ui/search.ts

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ const stackExtraSearchStrings = (row: SettingRowStack): string[] => {
3131
};
3232

3333
/** 设置面板内搜索:单条文案是否包含查询串(已 `trim` + `toLowerCase` 的 `queryLower`) */
34-
export const textMatchesConfigSearch = (text: string, queryLower: string): boolean => {
34+
export const textMatchesSearch = (text: string, queryLower: string): boolean => {
3535
if (!queryLower) {
3636
return true;
3737
}
@@ -52,40 +52,40 @@ export const configRowMatchesSearchQuery = (row: SettingRow, queryLower: string)
5252
case "range":
5353
case "notebookSavePath":
5454
return (
55-
textMatchesConfigSearch(row.title, queryLower) ||
56-
textMatchesConfigSearch(row.desc, queryLower)
55+
textMatchesSearch(row.title, queryLower) ||
56+
textMatchesSearch(row.desc, queryLower)
5757
);
5858
case "number":
5959
if (
60-
textMatchesConfigSearch(row.title, queryLower) ||
61-
textMatchesConfigSearch(row.desc, queryLower)
60+
textMatchesSearch(row.title, queryLower) ||
61+
textMatchesSearch(row.desc, queryLower)
6262
) {
6363
return true;
6464
}
65-
if (row.unit && textMatchesConfigSearch(row.unit, queryLower)) {
65+
if (row.unit && textMatchesSearch(row.unit, queryLower)) {
6666
return true;
6767
}
6868
return false;
6969
case "select":
7070
if (
71-
textMatchesConfigSearch(row.title, queryLower) ||
72-
textMatchesConfigSearch(row.desc, queryLower)
71+
textMatchesSearch(row.title, queryLower) ||
72+
textMatchesSearch(row.desc, queryLower)
7373
) {
7474
return true;
7575
}
76-
return row.options.some((o) => textMatchesConfigSearch(o.label ?? String(o.value), queryLower));
76+
return row.options.some((o) => textMatchesSearch(o.label ?? String(o.value), queryLower));
7777
case "button":
7878
return (
79-
textMatchesConfigSearch(row.title, queryLower) ||
80-
textMatchesConfigSearch(row.label, queryLower) ||
81-
(row.desc ? textMatchesConfigSearch(row.desc, queryLower) : false)
79+
textMatchesSearch(row.title, queryLower) ||
80+
textMatchesSearch(row.label, queryLower) ||
81+
(row.desc ? textMatchesSearch(row.desc, queryLower) : false)
8282
);
8383
case "custom":
84-
return row.keywords.some((k) => textMatchesConfigSearch(k, queryLower));
84+
return row.keywords.some((k) => textMatchesSearch(k, queryLower));
8585
case "switchQuery":
86-
return switchQuerySearchStrings(row).some((s) => textMatchesConfigSearch(s, queryLower));
86+
return switchQuerySearchStrings(row).some((s) => textMatchesSearch(s, queryLower));
8787
case "stack":
88-
return stackExtraSearchStrings(row).some((s) => textMatchesConfigSearch(s, queryLower));
88+
return stackExtraSearchStrings(row).some((s) => textMatchesSearch(s, queryLower));
8989
}
9090
};
9191

@@ -135,7 +135,7 @@ export const filterSettingSections = <T extends {title?: string; items: SettingR
135135
}
136136
const out: T[] = [];
137137
for (const section of sections) {
138-
if (textMatchesConfigSearch(section.title ?? "", queryLower)) {
138+
if (textMatchesSearch(section.title ?? "", queryLower)) {
139139
out.push(section);
140140
continue;
141141
}

0 commit comments

Comments
 (0)