Skip to content

Commit 5d52cf6

Browse files
committed
feat: 单个Section框引用查看面板增加刷新功能
1 parent 2c430bc commit 5d52cf6

File tree

2 files changed

+73
-46
lines changed

2 files changed

+73
-46
lines changed

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

Lines changed: 42 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,47 @@ export class ReferenceManager {
123123
return res;
124124
}
125125

126+
/**
127+
* 更新当前项目中的一个Section的引用信息
128+
* @param recentFiles
129+
* @param sectionName
130+
*/
131+
public async updateOneSectionReferenceInfo(recentFiles: RecentFileManager.RecentFile[], sectionName: string) {
132+
const fileNameList = this.project.references.sections[sectionName];
133+
const fileNameListNew = [];
134+
for (const fileName of fileNameList) {
135+
const file = recentFiles.find(
136+
(file) =>
137+
PathString.getFileNameFromPath(file.uri.path) === fileName ||
138+
PathString.getFileNameFromPath(file.uri.fsPath) === fileName,
139+
);
140+
if (file) {
141+
// 即使文件存在,也要打开看一看引用块是否在那个文件中。
142+
const thatProject = new Project(file.uri);
143+
loadAllServicesBeforeInit(thatProject);
144+
await thatProject.init();
145+
if (
146+
this.checkReferenceBlockInProject(
147+
thatProject,
148+
PathString.getFileNameFromPath(this.project.uri.path),
149+
sectionName,
150+
)
151+
) {
152+
fileNameListNew.push(fileName);
153+
} else {
154+
toast.warning(`文件 ${fileName} 中不再引用 ${sectionName},已从引用列表中移除`);
155+
}
156+
thatProject.dispose();
157+
}
158+
}
159+
if (fileNameListNew.length === 0) {
160+
// 直接把这个章节从引用列表中删除
161+
delete this.project.references.sections[sectionName];
162+
} else {
163+
this.project.references.sections[sectionName] = fileNameListNew;
164+
}
165+
}
166+
126167
/**
127168
* 更新当前项目的引用信息
128169
* (清理无效的引用)
@@ -132,31 +173,7 @@ export class ReferenceManager {
132173

133174
// 遍历当前项目的每一个被引用的Section框
134175
for (const sectionName in this.project.references.sections) {
135-
const fileNameList = this.project.references.sections[sectionName];
136-
const fileNameListNew = [];
137-
for (const fileName of fileNameList) {
138-
const file = recentFiles.find(
139-
(file) =>
140-
PathString.getFileNameFromPath(file.uri.path) === fileName ||
141-
PathString.getFileNameFromPath(file.uri.fsPath) === fileName,
142-
);
143-
if (file) {
144-
// 即使文件存在,也要打开看一看引用块是否在那个文件中。
145-
const thatProject = new Project(file.uri);
146-
loadAllServicesBeforeInit(thatProject);
147-
await thatProject.init();
148-
if (this.checkReferenceBlockInProject(thatProject, fileName, sectionName)) {
149-
fileNameListNew.push(fileName);
150-
}
151-
thatProject.dispose();
152-
}
153-
}
154-
if (fileNameListNew.length === 0) {
155-
// 直接把这个章节从引用列表中删除
156-
delete this.project.references.sections[sectionName];
157-
} else {
158-
this.project.references.sections[sectionName] = fileNameListNew;
159-
}
176+
await this.updateOneSectionReferenceInfo(recentFiles, sectionName);
160177
}
161178

162179
// 遍历每一个直接引用自己整个文件的文件
@@ -306,14 +323,4 @@ export class ReferenceManager {
306323
this.project.renderer.transformWorld2View(section.rectangle.leftTop),
307324
);
308325
}
309-
310-
private findSectionBySectionName(sectionName: string) {
311-
const section = this.project.stage
312-
.filter((object) => object instanceof Section)
313-
.find((section) => section.text === sectionName);
314-
if (section) {
315-
return section;
316-
}
317-
return null;
318-
}
319326
}

app/src/sub/ReferencesWindow.tsx

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { useAtom } from "jotai";
88
import { useState, useEffect } from "react";
99
import { PathString } from "@/utils/pathString";
1010
import { URI } from "vscode-uri";
11+
import { RecentFileManager } from "@/core/service/dataFileService/RecentFileManager";
1112

1213
export default function ReferencesWindow(props: { currentProjectFileName: string }) {
1314
const currentProjectFileName = props.currentProjectFileName;
@@ -105,25 +106,44 @@ export function SectionReferencePanel(props: { currentProjectFileName: string; s
105106
const [project] = useAtom(activeProjectAtom);
106107
if (!project) return <></>;
107108
const [references, setReferences] = useState(project.references);
108-
function refresh() {
109+
const [isUpdating, setIsUpdating] = useState(false);
110+
111+
async function refresh() {
112+
setIsUpdating(true);
113+
await project?.referenceManager.updateOneSectionReferenceInfo(
114+
await RecentFileManager.getRecentFiles(),
115+
sectionName,
116+
);
109117
setReferences({ ...project!.references });
118+
setIsUpdating(false);
110119
}
111120

112121
useEffect(() => {
113-
refresh();
122+
setReferences({ ...project!.references });
114123
}, []);
115124

116125
return (
117126
<div className="flex flex-col gap-2 p-2">
118-
{references.sections[sectionName].map((fileName) => (
119-
<div
120-
onClick={() => project.referenceManager.jumpToReferenceLocation(fileName, sectionName)}
121-
key={fileName}
122-
className="border-muted text-select-option-text w-full cursor-pointer rounded p-1 text-sm hover:ring"
123-
>
124-
{fileName}
125-
</div>
126-
))}
127+
{isUpdating ? (
128+
<span>正在刷新中...</span>
129+
) : (
130+
<>
131+
{references.sections[sectionName] &&
132+
references.sections[sectionName].map((fileName) => (
133+
<div
134+
onClick={() => project.referenceManager.jumpToReferenceLocation(fileName, sectionName)}
135+
key={fileName}
136+
className="border-muted text-select-option-text w-full cursor-pointer rounded p-1 text-sm hover:ring"
137+
>
138+
{fileName}
139+
</div>
140+
))}
141+
<Button onClick={refresh} variant="outline">
142+
<RefreshCcw />
143+
刷新
144+
</Button>
145+
</>
146+
)}
127147
</div>
128148
);
129149
}

0 commit comments

Comments
 (0)