mirror of
https://github.com/heroblackink/ultimate-todoist-sync-for-obsidian.git
synced 2026-07-22 07:40:27 +00:00
refactor: replace fileMetadata.todoistTasks with taskFileMapping
- Remove deprecated fileMetadata.todoistTasks and todoistCount usage - Use taskFileMapping-derived methods (getTasksInFile, getTaskCountInFile) - Delete obsolete frontMatter update logic in obsidianToTodoist.ts - Remove redundant saveSettings() calls - Simplify fileOperation.ts metadata checks
This commit is contained in:
parent
876c7ddeca
commit
df2e668d70
3 changed files with 13 additions and 111 deletions
46
main.ts
46
main.ts
|
|
@ -183,53 +183,15 @@ export default class UltimateTodoistSyncForObsidian extends Plugin {
|
|||
|
||||
|
||||
|
||||
/* 使用其他文件管理器移动,obsidian触发了删除事件,删除了所有的任务
|
||||
//监听删除事件,当文件被删除后,读取frontMatter中的tasklist,批量删除
|
||||
this.registerEvent(this.app.metadataCache.on('deleted', async(file,prevCache) => {
|
||||
try{
|
||||
if(!this.settings.apiInitialized){
|
||||
return
|
||||
}
|
||||
//console.log('a new file has modified')
|
||||
console.log(`file deleted`)
|
||||
//读取frontMatter
|
||||
const frontMatter = await this.cacheOperation.getFileMetadata(file.path)
|
||||
if(frontMatter === null || frontMatter.todoistTasks === undefined){
|
||||
console.log('There is no task in the deleted files.')
|
||||
return
|
||||
}
|
||||
//判断todoistTasks是否为null
|
||||
console.log(frontMatter.todoistTasks)
|
||||
if(!( this.checkModuleClass())){
|
||||
return
|
||||
}
|
||||
if (!await this.checkAndHandleSyncLock('obsidianToTodoist')) return;
|
||||
await this.todoistSync.deleteTasksByIds(frontMatter.todoistTasks)
|
||||
this.syncLock = false
|
||||
this.saveSettings()
|
||||
}catch(error){
|
||||
console.error(`An error occurred while deleting task in the file: ${error}`);
|
||||
this.syncLock = false
|
||||
}
|
||||
|
||||
|
||||
|
||||
}));
|
||||
*/
|
||||
|
||||
|
||||
//监听 rename 事件,更新 task data 中的 path
|
||||
//监听 rename 事件,更新 task data 中的 path
|
||||
this.registerEvent(this.app.vault.on('rename', async (file,oldpath) => {
|
||||
if(!this.settings.apiInitialized){
|
||||
return
|
||||
}
|
||||
console.log(`${oldpath} is renamed`)
|
||||
//读取frontMatter
|
||||
//const frontMatter = await this.fileOperation.getFrontMatter(file)
|
||||
const frontMatter = await this.cacheOperation.getFileMetadata(oldpath)
|
||||
console.log(frontMatter)
|
||||
if(frontMatter === null || frontMatter.todoistTasks === undefined){
|
||||
//console.log('删除的文件中没有task')
|
||||
const taskCount = this.cacheOperation.getTaskCountInFile(oldpath)
|
||||
if(taskCount === 0){
|
||||
console.log('The renamed file has no tasks.')
|
||||
return
|
||||
}
|
||||
if(!(this.checkModuleClass())){
|
||||
|
|
|
|||
|
|
@ -185,12 +185,6 @@ export class FileOperation {
|
|||
await this.app.vault.modify(file, newContent)
|
||||
this.plugin.logOperation?.log('FILE_TODOIST_TAG_ADDED', `Added todoist tag to file: ${filepath}`, filepath);
|
||||
|
||||
//update filemetadate
|
||||
const metadata = await this.plugin.cacheOperation.getFileMetadata(filepath)
|
||||
if(!metadata){
|
||||
await this.plugin.cacheOperation.newEmptyFileMetadata(filepath)
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -281,12 +275,6 @@ export class FileOperation {
|
|||
await this.plugin.backupOperation?.backupFile(filepath);
|
||||
await this.app.vault.modify(file, newContent)
|
||||
|
||||
//update filemetadate
|
||||
const metadata = await this.plugin.cacheOperation.getFileMetadata(filepath)
|
||||
if(!metadata){
|
||||
await this.plugin.cacheOperation.newEmptyFileMetadata(filepath)
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -27,17 +27,15 @@ export class ObsidianToTodoistSync {
|
|||
currentFileValue = view?.data;
|
||||
}
|
||||
|
||||
const frontMatter = await this.plugin.cacheOperation.getFileMetadata(filepath);
|
||||
if (!frontMatter || !frontMatter.todoistTasks) {
|
||||
console.log('frontmatter has no task');
|
||||
const taskIds = this.plugin.cacheOperation.getTasksInFile(filepath);
|
||||
if (taskIds.length === 0) {
|
||||
console.log('No tasks in this file');
|
||||
return;
|
||||
}
|
||||
|
||||
const currentFileValueWithOutFrontMatter = currentFileValue.replace(/^---[\s\S]*?---\n/, '');
|
||||
const frontMatter_todoistTasks = frontMatter.todoistTasks;
|
||||
const frontMatter_todoistCount = frontMatter.todoistCount;
|
||||
|
||||
const deleteTasksPromises = frontMatter_todoistTasks
|
||||
const deleteTasksPromises = taskIds
|
||||
.filter((taskId: string) => !currentFileValueWithOutFrontMatter.includes(taskId))
|
||||
.map(async (taskId: string) => {
|
||||
try {
|
||||
|
|
@ -55,21 +53,13 @@ export class ObsidianToTodoistSync {
|
|||
});
|
||||
|
||||
const deletedTaskIds = await Promise.all(deleteTasksPromises);
|
||||
const deletedTaskAmount = deletedTaskIds.length;
|
||||
if (!deletedTaskIds.length) {
|
||||
if (deletedTaskIds.length === 0) {
|
||||
return;
|
||||
}
|
||||
for (const taskId of deletedTaskIds) {
|
||||
this.plugin.cacheOperation.deleteTaskFileMapping(taskId);
|
||||
}
|
||||
this.plugin.saveSettings();
|
||||
|
||||
const newFrontMatter_todoistTasks = frontMatter_todoistTasks.filter(
|
||||
(taskId: string) => !deletedTaskIds.includes(taskId)
|
||||
);
|
||||
|
||||
const newFileMetadata = { todoistTasks: newFrontMatter_todoistTasks, todoistCount: (frontMatter_todoistCount - deletedTaskAmount) };
|
||||
await this.plugin.cacheOperation.updateFileMetadata(filepath, newFileMetadata);
|
||||
}
|
||||
|
||||
async lineContentNewTaskCheck(editor: Editor, view: MarkdownView): Promise<void> {
|
||||
|
|
@ -101,7 +91,6 @@ export class ObsidianToTodoistSync {
|
|||
this.plugin.logOperation?.log('OBSIDIAN_TASK_COMPLETED', `Completed task in Obsidian: ${newTask.content}`, filepath, todoist_id);
|
||||
this.plugin.logOperation?.log('TODOIST_TASK_COMPLETED', `Completed task in Todoist: ${newTask.content}`, filepath, todoist_id);
|
||||
}
|
||||
this.plugin.saveSettings();
|
||||
|
||||
const text_with_out_link = `${linetxt} %%[todoist_id:: ${todoist_id}]%%`;
|
||||
const link = this.plugin.settings.useAppURI ? `[link](todoist://task?id=${newTask.id})` : `[link](${newTask.url})`;
|
||||
|
|
@ -111,18 +100,7 @@ export class ObsidianToTodoistSync {
|
|||
view.app.workspace.activeEditor?.editor?.replaceRange(text, from, to);
|
||||
|
||||
try {
|
||||
const frontMatter = await this.plugin.cacheOperation.getFileMetadata(filepath);
|
||||
|
||||
if (!frontMatter) {
|
||||
// empty
|
||||
}
|
||||
|
||||
const newFrontMatter = { ...frontMatter };
|
||||
newFrontMatter.todoistCount = (newFrontMatter.todoistCount ?? 0) + 1;
|
||||
newFrontMatter.todoistTasks = [...(newFrontMatter.todoistTasks || []), todoist_id];
|
||||
|
||||
await this.plugin.cacheOperation.updateFileMetadata(filepath, newFrontMatter);
|
||||
|
||||
this.plugin.saveSettings();
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
}
|
||||
|
|
@ -158,16 +136,6 @@ export class ObsidianToTodoistSync {
|
|||
|
||||
const content = currentFileValue;
|
||||
|
||||
let newFrontMatter;
|
||||
const frontMatter = await this.plugin.cacheOperation.getFileMetadata(filepath);
|
||||
|
||||
if (!frontMatter) {
|
||||
console.log('frontmatter is empty');
|
||||
newFrontMatter = {};
|
||||
} else {
|
||||
newFrontMatter = { ...frontMatter };
|
||||
}
|
||||
|
||||
let hasNewTask = false;
|
||||
const lines = content.split('\n');
|
||||
|
||||
|
|
@ -200,9 +168,6 @@ export class ObsidianToTodoistSync {
|
|||
const text = this.plugin.taskParser.addTodoistLink(text_with_out_link, link);
|
||||
lines[i] = text;
|
||||
|
||||
newFrontMatter.todoistCount = (newFrontMatter.todoistCount ?? 0) + 1;
|
||||
newFrontMatter.todoistTasks = [...(newFrontMatter.todoistTasks || []), todoist_id];
|
||||
|
||||
hasNewTask = true;
|
||||
|
||||
} catch (error) {
|
||||
|
|
@ -216,9 +181,6 @@ export class ObsidianToTodoistSync {
|
|||
const newContent = lines.join('\n');
|
||||
await this.plugin.backupOperation?.backupFile(filepath);
|
||||
await this.app.vault.modify(file, newContent);
|
||||
|
||||
await this.plugin.cacheOperation.updateFileMetadata(filepath, newFrontMatter);
|
||||
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
}
|
||||
|
|
@ -226,14 +188,6 @@ export class ObsidianToTodoistSync {
|
|||
}
|
||||
|
||||
async lineModifiedTaskCheck(filepath: string, lineText: string, lineNumber: number, fileContent: string): Promise<void> {
|
||||
if (this.plugin.settings.enableFullVaultSync) {
|
||||
const metadata = await this.plugin.cacheOperation.getFileMetadata(filepath);
|
||||
if (!metadata) {
|
||||
await this.plugin.cacheOperation.newEmptyFileMetadata(filepath);
|
||||
}
|
||||
this.plugin.saveSettings();
|
||||
}
|
||||
|
||||
if (this.plugin.taskParser.hasTodoistId(lineText) && this.plugin.taskParser.hasTodoistTag(lineText)) {
|
||||
const lineTask = await this.plugin.taskParser.convertTextToTodoistTaskObject(lineText, filepath, lineNumber, fileContent);
|
||||
const lineTask_todoist_id = (lineTask.todoist_id).toString();
|
||||
|
|
@ -471,15 +425,13 @@ export class ObsidianToTodoistSync {
|
|||
}
|
||||
|
||||
async updateTaskDescription(filepath: string): Promise<void> {
|
||||
const metadata = await this.plugin.cacheOperation.getFileMetadata(filepath);
|
||||
const taskIds = this.plugin.cacheOperation.getTasksInFile(filepath);
|
||||
|
||||
if (!metadata || !metadata.todoistTasks) {
|
||||
if (taskIds.length === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
const todoistTasks = metadata.todoistTasks;
|
||||
|
||||
for (const taskId of todoistTasks) {
|
||||
for (const taskId of taskIds) {
|
||||
try {
|
||||
const taskMapping = this.plugin.cacheOperation.getTaskFileMapping(taskId);
|
||||
if (taskMapping) {
|
||||
|
|
|
|||
Loading…
Reference in a new issue