mirror of
https://github.com/heroblackink/ultimate-todoist-sync-for-obsidian.git
synced 2026-07-22 07:40:27 +00:00
Full vault sync is supported.
This commit is contained in:
parent
9fb98bbdd9
commit
bda683784e
4 changed files with 80 additions and 12 deletions
|
|
@ -113,6 +113,45 @@ export class FileOperation {
|
|||
}
|
||||
}
|
||||
|
||||
//add #todoist at the end of task line, if full vault sync enabled
|
||||
async addTodoistTagToFile(filepath: string) {
|
||||
// 获取文件对象并更新内容
|
||||
const file = this.app.vault.getAbstractFileByPath(filepath)
|
||||
const content = await this.app.vault.read(file)
|
||||
|
||||
const lines = content.split('\n')
|
||||
let modified = false
|
||||
|
||||
for (let i = 0; i < lines.length; i++) {
|
||||
const line = lines[i]
|
||||
if(!this.taskParser.isMarkdownTask(line)){
|
||||
//console.log(line)
|
||||
//console.log("It is not a markdown task.")
|
||||
continue;
|
||||
}
|
||||
//if content is empty
|
||||
if(this.taskParser.getTaskContentFromLineText(line) == ""){
|
||||
//console.log("Line content is empty")
|
||||
continue;
|
||||
}
|
||||
if (!this.taskParser.hasTodoistId(line) && !this.taskParser.hasTodoistTag(line)) {
|
||||
//console.log(line)
|
||||
//console.log('prepare to add todoist tag')
|
||||
const newLine = this.taskParser.addTodoistTag(line);
|
||||
//console.log(newLine)
|
||||
lines[i] = newLine
|
||||
modified = true
|
||||
}
|
||||
}
|
||||
|
||||
if (modified) {
|
||||
//console.log('file is modified')
|
||||
const newContent = lines.join('\n')
|
||||
//console.log(newContent)
|
||||
await this.app.vault.modify(file, newContent)
|
||||
}
|
||||
}
|
||||
|
||||
// sync updated task content to file
|
||||
async syncUpdatedTaskContentToTheFile(evt:Object) {
|
||||
const taskId = evt.object_id
|
||||
|
|
@ -128,15 +167,15 @@ export class FileOperation {
|
|||
let modified = false
|
||||
|
||||
for (let i = 0; i < lines.length; i++) {
|
||||
const line = lines[i]
|
||||
if (line.includes(taskId) && this.taskParser.hasTodoistTag(line)) {
|
||||
const oldTaskContent = this.taskParser.getTaskContentFromLineText(line)
|
||||
const newTaskContent = evt.extra_data.content
|
||||
const line = lines[i]
|
||||
if (line.includes(taskId) && this.taskParser.hasTodoistTag(line)) {
|
||||
const oldTaskContent = this.taskParser.getTaskContentFromLineText(line)
|
||||
const newTaskContent = evt.extra_data.content
|
||||
|
||||
lines[i] = line.replace(oldTaskContent, newTaskContent)
|
||||
modified = true
|
||||
break
|
||||
}
|
||||
lines[i] = line.replace(oldTaskContent, newTaskContent)
|
||||
modified = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if (modified) {
|
||||
|
|
|
|||
|
|
@ -18,6 +18,7 @@ export interface UltimateTodoistSyncSettings {
|
|||
automaticSynchronizationInterval:Number;
|
||||
todoistTasksData:any;
|
||||
fileMetadata:any;
|
||||
enableFullVaultSync: boolean;
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -28,6 +29,7 @@ export const DEFAULT_SETTINGS: UltimateTodoistSyncSettings = {
|
|||
automaticSynchronizationInterval: 300, //default aync interval 300s
|
||||
todoistTasksData:{},
|
||||
fileMetadata:{},
|
||||
enableFullVaultSync:false,
|
||||
//mySetting: 'default',
|
||||
//todoistTasksFilePath: 'todoistTasks.json'
|
||||
|
||||
|
|
@ -112,7 +114,7 @@ export class UltimateTodoistSyncSettingTab extends PluginSettingTab {
|
|||
//
|
||||
})
|
||||
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
/*
|
||||
|
|
@ -163,7 +165,19 @@ export class UltimateTodoistSyncSettingTab extends PluginSettingTab {
|
|||
|
||||
|
||||
|
||||
|
||||
new Setting(containerEl)
|
||||
.setName('Full vault sync')
|
||||
.setDesc('By default, only tasks marked with #todoist are synchronized. If this option is turned on, all tasks in the vault will be synchronized.')
|
||||
.addToggle(component =>
|
||||
component
|
||||
.setValue(this.plugin.settings.enableFullVaultSync)
|
||||
.onChange((value)=>{
|
||||
this.plugin.settings.enableFullVaultSync = value
|
||||
this.plugin.saveSettings()
|
||||
new Notice("Full vault sync is enabled.")
|
||||
})
|
||||
|
||||
)
|
||||
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -136,7 +136,8 @@ export class TodoistSync {
|
|||
const line = cursor.line
|
||||
const linetxt = editor.getLine(line)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
//添加task
|
||||
|
|
@ -237,6 +238,11 @@ export class TodoistSync {
|
|||
currentFileValue = view?.data
|
||||
}
|
||||
|
||||
if(this.settings.enableFullVaultSync){
|
||||
//console.log('full vault sync enabled')
|
||||
//console.log(filepath)
|
||||
await this.fileOperation.addTodoistTagToFile(filepath)
|
||||
}
|
||||
|
||||
const content = currentFileValue
|
||||
|
||||
|
|
|
|||
|
|
@ -518,5 +518,14 @@ export class TaskParser {
|
|||
console.error(`Error extracting date from string '${localDateString}': ${error}`);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
isMarkdownTask(str: string): boolean {
|
||||
const taskRegex = /^\s*-\s+\[([x ])\]/;
|
||||
return taskRegex.test(str);
|
||||
}
|
||||
|
||||
addTodoistTag(str: string): string {
|
||||
return(str +` ${keywords.TODOIST_TAG}`);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue