* Toggle the archive status of a task
(task: TaskInfo)
| 1116 | * Toggle the archive status of a task |
| 1117 | */ |
| 1118 | async toggleArchive(task: TaskInfo): Promise<TaskInfo> { |
| 1119 | const file = this.plugin.app.vault.getAbstractFileByPath(task.path); |
| 1120 | if (!(file instanceof TFile)) { |
| 1121 | throw new Error(`Cannot find task file: ${task.path}`); |
| 1122 | } |
| 1123 | |
| 1124 | const archiveTag = this.plugin.fieldMapper.getMapping().archiveTag; |
| 1125 | const archivePlan = buildTaskArchiveState(task, archiveTag, getCurrentTimestamp()); |
| 1126 | const { updatedTask, isCurrentlyArchived, dateModified } = archivePlan; |
| 1127 | |
| 1128 | // Step 2: Persist to file |
| 1129 | await this.plugin.app.fileManager.processFrontMatter(file, (frontmatter) => { |
| 1130 | const dateModifiedField = this.plugin.fieldMapper.toUserField("dateModified"); |
| 1131 | applyTaskArchiveFrontmatterChange({ |
| 1132 | frontmatter, |
| 1133 | archiveTag, |
| 1134 | isCurrentlyArchived, |
| 1135 | dateModified, |
| 1136 | dateModifiedField, |
| 1137 | }); |
| 1138 | }); |
| 1139 | |
| 1140 | // Step 2.5: Move file based on archive operation and settings |
| 1141 | let movedFile = file; |
| 1142 | const movePlan = buildTaskArchiveMovePlan({ |
| 1143 | isCurrentlyArchived, |
| 1144 | moveArchivedTasks: this.plugin.settings.moveArchivedTasks, |
| 1145 | archiveFolderTemplate: this.plugin.settings.archiveFolder, |
| 1146 | tasksFolderTemplate: this.plugin.settings.tasksFolder, |
| 1147 | fileName: file.name, |
| 1148 | taskData: { |
| 1149 | title: updatedTask.title || "", |
| 1150 | priority: updatedTask.priority, |
| 1151 | status: updatedTask.status, |
| 1152 | contexts: updatedTask.contexts, |
| 1153 | projects: updatedTask.projects, |
| 1154 | }, |
| 1155 | processFolderTemplate: (folderTemplate, taskData) => |
| 1156 | this.processFolderTemplate(folderTemplate, taskData), |
| 1157 | }); |
| 1158 | if (movePlan) { |
| 1159 | try { |
| 1160 | await ensureFolderExists(this.plugin.app.vault, movePlan.destinationFolder); |
| 1161 | |
| 1162 | const existingFile = this.plugin.app.vault.getAbstractFileByPath(movePlan.newPath); |
| 1163 | if (existingFile) { |
| 1164 | throw new Error( |
| 1165 | `A file named "${file.name}" already exists in the ${movePlan.destinationKind} folder "${movePlan.destinationFolder}". Cannot move task to avoid overwriting existing file.` |
| 1166 | ); |
| 1167 | } |
| 1168 | |
| 1169 | await this.plugin.app.fileManager.renameFile(file, movePlan.newPath); |
| 1170 | |
| 1171 | const resolvedMovedFile = this.plugin.app.vault.getAbstractFileByPath( |
| 1172 | movePlan.newPath |
| 1173 | ); |
| 1174 | if (!(resolvedMovedFile instanceof TFile)) { |
| 1175 | throw new Error(`Failed to resolve moved task file: ${movePlan.newPath}`); |