Skip to content

Commit 2c430bc

Browse files
committed
enhance: 优化点击时造成引用块序号位置查询的时间复杂度
1 parent 2101719 commit 2c430bc

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

app/src/core/stage/stageManager/concreteMethods/StageReferenceManager.tsx

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,13 +94,14 @@ export class ReferenceManager {
9494

9595
/**
9696
* 处理引用按钮点击事件
97-
* 这个函数需要性能优化,鼠标每次点击都会调用这个函数
97+
* O(N) 需要查找每一个引用的Section
9898
* @param clickLocation 点击位置
9999
*/
100100
public onClickReferenceNumber(clickLocation: Vector) {
101-
//
101+
if (Object.keys(this.project.references.sections).length === 0) return;
102+
const sectionNameMap = this.buildSectionName2SectionMap(Object.keys(this.project.references.sections));
102103
for (const sectionName in this.project.references.sections) {
103-
const section = this.findSectionBySectionName(sectionName);
104+
const section = sectionNameMap[sectionName];
104105
if (section) {
105106
if (section.isMouseInReferenceButton(clickLocation)) {
106107
// 打开这个详细信息的引用弹窗
@@ -111,6 +112,17 @@ export class ReferenceManager {
111112
}
112113
}
113114

115+
private buildSectionName2SectionMap(sectionNames: string[]): Record<string, Section> {
116+
const res: Record<string, Section> = {};
117+
const sectionNameSet = new Set(sectionNames);
118+
for (const section of this.project.stage.filter((obj) => obj instanceof Section)) {
119+
if (sectionNameSet.has(section.text)) {
120+
res[section.text] = section;
121+
}
122+
}
123+
return res;
124+
}
125+
114126
/**
115127
* 更新当前项目的引用信息
116128
* (清理无效的引用)

0 commit comments

Comments
 (0)