Skip to content

Commit 555782b

Browse files
committed
fix(view): update sidebar list based on filtered tasks in two-column views
1 parent d2845e6 commit 555782b

File tree

6 files changed

+197
-117
lines changed

6 files changed

+197
-117
lines changed

src/components/features/fluent/managers/FluentComponentManager.ts

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -644,22 +644,28 @@ export class FluentComponentManager extends Component {
644644

645645
// Set tasks on the component
646646
if (typeof targetComponent.setTasks === "function") {
647-
// Special handling for components that need only all tasks (single parameter)
648-
// Review and tags views need all tasks to build their indices
649-
if (viewId === "review" || viewId === "tags") {
647+
// Special handling for components that need filtered + all tasks
648+
// Tags view: uses filtered tasks for tag index (left sidebar), all tasks for tree view lookup
649+
if (viewId === "tags") {
650+
console.log(
651+
`[FluentComponent] Calling setTasks for ${viewId} with FILTERED tasks (${filteredTasks.length}) and ALL tasks (${tasks.length})`,
652+
);
653+
targetComponent.setTasks(filteredTasks, tasks);
654+
} else if (viewId === "review") {
655+
// Review view still needs all tasks
650656
console.log(
651657
`[FluentComponent] Calling setTasks for ${viewId} with ALL tasks:`,
652658
tasks.length,
653659
);
654660
targetComponent.setTasks(tasks);
655661
} else if (viewId === "projects" && !project) {
656-
// Projects overview mode: pass ALL tasks to build project index
657-
// and FILTERED tasks to apply filter to project task lists
658-
// This ensures: left sidebar shows all projects, right panel shows filtered tasks
662+
// Projects overview mode: standardized semantics
663+
// First param: filtered tasks (for building sidebar project index)
664+
// Second param: all tasks (for tree view parent-child lookup)
659665
console.log(
660-
`[FluentComponent] Calling setTasks for projects with ALL tasks (${tasks.length}) and FILTERED tasks (${filteredTasks.length})`,
666+
`[FluentComponent] Calling setTasks for projects with FILTERED tasks (${filteredTasks.length}) and ALL tasks (${tasks.length})`,
661667
);
662-
targetComponent.setTasks(tasks, filteredTasks);
668+
targetComponent.setTasks(filteredTasks, tasks);
663669
} else {
664670
// Use filtered tasks
665671
let filteredTasksLocal = [...filteredTasks];
@@ -890,7 +896,10 @@ export class FluentComponentManager extends Component {
890896
if (target?.setTasks) {
891897
if (viewId === "projects" || this.isContentBasedView(viewId)) {
892898
target.setTasks(filteredTasks, tasks, true);
893-
} else if (viewId === "review" || viewId === "tags") {
899+
} else if (viewId === "tags") {
900+
// Tags view: filtered tasks for tag index, all tasks for tree view lookup
901+
target.setTasks(filteredTasks, tasks);
902+
} else if (viewId === "review") {
894903
target.setTasks(tasks);
895904
} else {
896905
target.setTasks(filteredTasks);

src/components/features/task/view/ProjectViewComponent.ts

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,14 @@ export class ProjectViewComponent extends TwoColumnViewBase<string> {
4343

4444
/**
4545
* 重写基类中的索引构建方法,为项目创建索引
46+
* 使用 sourceTasks(筛选后的任务)构建索引,确保左侧栏只显示相关项目
4647
*/
4748
protected buildItemsIndex(): void {
4849
// 清除现有索引
4950
this.allProjectsMap.clear();
5051

51-
// 为每个任务的项目建立索引,使用 getEffectiveProject 统一获取项目名
52-
this.allTasks.forEach((task) => {
52+
// 使用 sourceTasks(筛选后的任务)为每个任务的项目建立索引
53+
this.sourceTasks.forEach((task) => {
5354
const projectName = getEffectiveProject(task);
5455
if (projectName) {
5556
if (!this.allProjectsMap.has(projectName)) {
@@ -59,9 +60,12 @@ export class ProjectViewComponent extends TwoColumnViewBase<string> {
5960
}
6061
});
6162

62-
// 构建项目树结构
63+
// 构建项目树结构(同样使用 sourceTasks)
6364
const separator = this.plugin.settings.projectPathSeparator || "/";
64-
this.projectTree = buildProjectTreeFromTasks(this.allTasks, separator);
65+
this.projectTree = buildProjectTreeFromTasks(
66+
this.sourceTasks,
67+
separator,
68+
);
6569

6670
// 更新项目计数
6771
if (this.countEl) {
@@ -244,11 +248,12 @@ export class ProjectViewComponent extends TwoColumnViewBase<string> {
244248
}
245249

246250
// 根据视图模式使用不同的筛选逻辑
251+
// 使用 sourceTasks 进行筛选,保持外部过滤状态
247252
if (this.viewMode === "tree" && this.projectTree) {
248253
// 树状模式:使用包含式筛选(选父含子)
249254
const separator = this.plugin.settings.projectPathSeparator || "/";
250255
this.filteredTasks = filterTasksByProjectPaths(
251-
this.allTasks,
256+
this.sourceTasks,
252257
this.selectedItems.items,
253258
separator,
254259
);
@@ -265,8 +270,8 @@ export class ProjectViewComponent extends TwoColumnViewBase<string> {
265270
}
266271
});
267272

268-
// 将任务ID转换为实际任务对象
269-
this.filteredTasks = this.allTasks.filter((task) =>
273+
// 将任务ID转换为实际任务对象(从 sourceTasks 中筛选)
274+
this.filteredTasks = this.sourceTasks.filter((task) =>
270275
resultTaskIds.has(task.id),
271276
);
272277
}
@@ -358,6 +363,9 @@ export class ProjectViewComponent extends TwoColumnViewBase<string> {
358363
* 更新任务
359364
*/
360365
public updateTask(updatedTask: Task): void {
366+
// 更新 allTasksMap
367+
this.allTasksMap.set(updatedTask.id, updatedTask);
368+
361369
let needsFullRefresh = false;
362370
const taskIndex = this.allTasks.findIndex(
363371
(t) => t.id === updatedTask.id,
@@ -376,6 +384,14 @@ export class ProjectViewComponent extends TwoColumnViewBase<string> {
376384
needsFullRefresh = true;
377385
}
378386

387+
// 同时更新 sourceTasks(如果任务存在于其中)
388+
const sourceIndex = this.sourceTasks.findIndex(
389+
(t) => t.id === updatedTask.id,
390+
);
391+
if (sourceIndex !== -1) {
392+
this.sourceTasks[sourceIndex] = updatedTask;
393+
}
394+
379395
// 如果项目更改或任务是新的,重建索引并完全刷新UI
380396
if (needsFullRefresh) {
381397
this.buildItemsIndex();

src/components/features/task/view/TagViewComponent.ts

Lines changed: 35 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export class TagViewComponent extends TwoColumnViewBase<string> {
2323
constructor(
2424
parentEl: HTMLElement,
2525
app: App,
26-
plugin: TaskProgressBarPlugin
26+
plugin: TaskProgressBarPlugin,
2727
) {
2828
// 配置基类需要的参数
2929
const config: TwoColumnViewConfig = {
@@ -45,31 +45,32 @@ export class TagViewComponent extends TwoColumnViewBase<string> {
4545
* @returns Normalized tag with # prefix
4646
*/
4747
private normalizeTag(tag: string): string {
48-
if (typeof tag !== 'string') {
48+
if (typeof tag !== "string") {
4949
return tag;
5050
}
51-
51+
5252
// Trim whitespace
5353
const trimmed = tag.trim();
54-
54+
5555
// If empty or already starts with #, return as is
56-
if (!trimmed || trimmed.startsWith('#')) {
56+
if (!trimmed || trimmed.startsWith("#")) {
5757
return trimmed;
5858
}
59-
59+
6060
// Add # prefix
6161
return `#${trimmed}`;
6262
}
6363

6464
/**
6565
* 重写基类中的索引构建方法,为标签创建索引
66+
* 使用 sourceTasks(筛选后的任务)构建索引,确保左侧栏只显示相关标签
6667
*/
6768
protected buildItemsIndex(): void {
6869
// 清除已有索引
6970
this.allTagsMap.clear();
7071

71-
// 为每个任务的标签建立索引
72-
this.allTasks.forEach((task) => {
72+
// 使用 sourceTasks(筛选后的任务)为每个任务的标签建立索引
73+
this.sourceTasks.forEach((task) => {
7374
if (task.metadata.tags && task.metadata.tags.length > 0) {
7475
task.metadata.tags.forEach((tag) => {
7576
// 跳过非字符串类型的标签
@@ -139,7 +140,7 @@ export class TagViewComponent extends TwoColumnViewBase<string> {
139140
private renderTagHierarchy(
140141
node: Record<string, any>,
141142
parentEl: HTMLElement,
142-
level: number
143+
level: number,
143144
) {
144145
// 按字母排序键,但排除元数据属性
145146
const keys = Object.keys(node)
@@ -212,7 +213,7 @@ export class TagViewComponent extends TwoColumnViewBase<string> {
212213
this.renderTagHierarchy(
213214
childNode,
214215
childrenContainer,
215-
level + 1
216+
level + 1,
216217
);
217218
}
218219
});
@@ -260,9 +261,9 @@ export class TagViewComponent extends TwoColumnViewBase<string> {
260261
set.forEach((id) => resultTaskIds.add(id));
261262
});
262263

263-
// 将任务ID转换为实际任务对象
264-
this.filteredTasks = this.allTasks.filter((task) =>
265-
resultTaskIds.has(task.id)
264+
// 将任务ID转换为实际任务对象(从 sourceTasks 中筛选,保持外部过滤状态)
265+
this.filteredTasks = this.sourceTasks.filter((task) =>
266+
resultTaskIds.has(task.id),
266267
);
267268

268269
// 按优先级和截止日期排序
@@ -313,7 +314,7 @@ export class TagViewComponent extends TwoColumnViewBase<string> {
313314
(taskTag) =>
314315
// 跳过非字符串类型的标签
315316
typeof taskTag === "string" &&
316-
(taskTag === tag || taskTag.startsWith(tag + "/"))
317+
(taskTag === tag || taskTag.startsWith(tag + "/")),
317318
);
318319
});
319320

@@ -346,7 +347,7 @@ export class TagViewComponent extends TwoColumnViewBase<string> {
346347
let title = t(this.config.rightColumnDefaultTitle);
347348
if (this.selectedItems.items.length > 1) {
348349
title = `${this.selectedItems.items.length} ${t(
349-
this.config.multiSelectText
350+
this.config.multiSelectText,
350351
)}`;
351352
}
352353
const countText = `${this.filteredTasks.length} ${t("tasks")}`;
@@ -364,7 +365,7 @@ export class TagViewComponent extends TwoColumnViewBase<string> {
364365
const toggleEl = headerEl.createDiv({ cls: "section-toggle" });
365366
setIcon(
366367
toggleEl,
367-
section.isExpanded ? "chevron-down" : "chevron-right"
368+
section.isExpanded ? "chevron-down" : "chevron-right",
368369
);
369370
const titleEl = headerEl.createDiv({ cls: "section-title" });
370371
titleEl.setText(`#${section.tag.replace("#", "")}`);
@@ -382,7 +383,7 @@ export class TagViewComponent extends TwoColumnViewBase<string> {
382383
taskListEl,
383384
this.plugin,
384385
this.app,
385-
this.config.rendererContext
386+
this.config.rendererContext,
386387
);
387388
section.renderer.onTaskSelected = this.onTaskSelected;
388389
section.renderer.onTaskCompleted = this.onTaskCompleted;
@@ -393,15 +394,15 @@ export class TagViewComponent extends TwoColumnViewBase<string> {
393394
section.tasks,
394395
this.isTreeView,
395396
this.allTasksMap,
396-
t("No tasks found for this tag.")
397+
t("No tasks found for this tag."),
397398
);
398399

399400
// 注册切换事件
400401
this.registerDomEvent(headerEl, "click", () => {
401402
section.isExpanded = !section.isExpanded;
402403
setIcon(
403404
toggleEl,
404-
section.isExpanded ? "chevron-down" : "chevron-right"
405+
section.isExpanded ? "chevron-down" : "chevron-right",
405406
);
406407
section.isExpanded ? taskListEl.show() : taskListEl.hide();
407408
});
@@ -446,9 +447,12 @@ export class TagViewComponent extends TwoColumnViewBase<string> {
446447
* 更新任务
447448
*/
448449
public updateTask(updatedTask: Task): void {
450+
// 更新 allTasksMap
451+
this.allTasksMap.set(updatedTask.id, updatedTask);
452+
449453
let needsFullRefresh = false;
450454
const taskIndex = this.allTasks.findIndex(
451-
(t) => t.id === updatedTask.id
455+
(t) => t.id === updatedTask.id,
452456
);
453457

454458
if (taskIndex !== -1) {
@@ -469,6 +473,14 @@ export class TagViewComponent extends TwoColumnViewBase<string> {
469473
needsFullRefresh = true; // 新任务,需要完全刷新
470474
}
471475

476+
// 同时更新 sourceTasks(如果任务存在于其中)
477+
const sourceIndex = this.sourceTasks.findIndex(
478+
(t) => t.id === updatedTask.id,
479+
);
480+
if (sourceIndex !== -1) {
481+
this.sourceTasks[sourceIndex] = updatedTask;
482+
}
483+
472484
// 如果标签变化或任务是新的,重建索引并完全刷新UI
473485
if (needsFullRefresh) {
474486
this.buildItemsIndex();
@@ -477,7 +489,7 @@ export class TagViewComponent extends TwoColumnViewBase<string> {
477489
} else {
478490
// 否则,仅更新过滤列表中的任务
479491
const filteredIndex = this.filteredTasks.findIndex(
480-
(t) => t.id === updatedTask.id
492+
(t) => t.id === updatedTask.id,
481493
);
482494
if (filteredIndex !== -1) {
483495
this.filteredTasks[filteredIndex] = updatedTask;
@@ -495,13 +507,13 @@ export class TagViewComponent extends TwoColumnViewBase<string> {
495507
// 跳过非字符串类型的标签
496508
typeof taskTag === "string" &&
497509
(taskTag === section.tag ||
498-
taskTag.startsWith(section.tag + "/"))
510+
taskTag.startsWith(section.tag + "/")),
499511
)
500512
) {
501513
// 检查任务是否实际存在于此分区的列表中
502514
if (
503515
section.tasks.some(
504-
(t) => t.id === updatedTask.id
516+
(t) => t.id === updatedTask.id,
505517
)
506518
) {
507519
section.renderer?.updateTask(updatedTask);

0 commit comments

Comments
 (0)