Skip to content

Commit

Permalink
Fix: full vault sync fix, mark completion fix (#106)
Browse files Browse the repository at this point in the history
  • Loading branch information
thesamim authored Apr 6, 2024
1 parent c4b856a commit a15c45a
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 30 deletions.
9 changes: 6 additions & 3 deletions src/fileOperation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,10 @@ export class FileOperation {
if (!this.plugin.taskParser?.hasTickTickId(line) && !this.plugin.taskParser?.hasTickTickTag(line)) {
//console.log(line)
//console.log('prepare to add TickTick tag')
const newLine = this.plugin.taskParser?.addTickTickTag(line);
//console.log(newLine)
let newLine = this.plugin.taskParser?.addTickTickTag(line);
//strip item id in case it was an item before
newLine = this.plugin.taskParser?.stripLineItemId(newLine)
//console.log(newLine)
lines[i] = newLine
modified = true
}
Expand All @@ -116,6 +118,7 @@ export class FileOperation {
const newContent = lines.join('\n')
//console.log(newContent)
await this.app.vault.modify(file, newContent)
new Notice("New Tasks will be added to TickTick on next Sync.")
// console.error("Modified: ", file?.path, new Date().toISOString());

// //update filemetadate
Expand Down Expand Up @@ -152,7 +155,7 @@ export class FileOperation {
//console.log('prepare to add TickTick link')
const taskID = this.plugin.taskParser?.getTickTickIdFromLineText(line)
const taskObject = await this.plugin.cacheOperation?.loadTaskFromCacheID(taskID)
const newLine = this.plugin.taskParser?.addTickTickLink(line, taskObject.id)
const newLine = this.plugin.taskParser?.addTickTickLink(line, taskObject.id, taskObject.projecId)
// console.log(newLine)
lines[i] = newLine
modified = true
Expand Down
8 changes: 7 additions & 1 deletion src/syncModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,7 @@ export class SyncMan {
//`Task ${lastLineTaskticktickId} was modified`
await this.plugin.saveSettings()
let message = `Task ${lineTask_ticktick_id} is updated.`;
console.log("#####", message);
new Notice(message);

if (contentChanged) {
Expand Down Expand Up @@ -552,7 +553,12 @@ export class SyncMan {


} else { //Not a task, check Items.
modified = await this.handleTaskItem(lineText, filepath, fileContent, lineNumber);
// When Full Vault Sync is enabled, we can tell the difference between items and subtasks
// everything is a subtask
// TODO: in the fullness of time, see if there's a way to differentiate.
if (!this.plugin.settings.enableFullVaultSync) {
modified = await this.handleTaskItem(lineText, filepath, fileContent, lineNumber);
}
}
return modified
}
Expand Down
82 changes: 56 additions & 26 deletions src/taskParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,12 @@ enum Priority {
}

const keywords = {
TickTick_TAG: '#ticktick', DUE_DATE: '⏳|🗓️|📅|📆|🗓',
TickTick_TAG: '#ticktick',
DUE_DATE: '⏳|🗓️|📅|📆|🗓',
TIME: '⌚',
TASK_DUE_DATE: '📅',
TASK_COMPLETE: '✅',
ALL_TASK_EMOJI: '➕|⏳|🛫|📅|✅|❌',
// priorityIcons: "⏬|🔽|🔼|⏫|🔺",
// priority: `\s([${priorityEmojis.toString()}])\s`
priority: `\\s([\u{23EC}\u{1F53D}\u{1F53C}\u{23EB}\u{1F53A}])\\s`
Expand Down Expand Up @@ -112,7 +115,8 @@ const REGEX = {
priorityRegex: /^.*([🔺🔼🔽]).*$/u,
BLANK_LINE: /^\s*$/,
TickTick_EVENT_DATE: /(\d{4})-(\d{2})-(\d{2})/,
ITEM_LINE: /\[(.*?)\]\s*(.*?)\s*%%(.*?)%%/
ITEM_LINE: /\[(.*?)\]\s*(.*?)\s*%%(.*?)%%/,
REMOVE_ITEM_ID: /\s%%[^\[](.*?)[^\]]%%/g
};

export class TaskParser {
Expand All @@ -131,7 +135,7 @@ export class TaskParser {

task.title = this.stripOBSUrl(task.title);

resultLine = `- [${task.status > 0 ? 'X' : ' '}] ${task.title}`;
resultLine = `- [${task.status > 0 ? 'x' : ' '}] ${task.title}`;



Expand Down Expand Up @@ -197,6 +201,11 @@ export class TaskParser {
return (TaskContent);
}

stripLineItemId(lineText: string) {
let line = lineText.replace(REGEX.REMOVE_ITEM_ID, '')
return line;
}

addTagsToLine(resultLine: string, tags: ITask.tags) {
//we're looking for the ticktick tag without the #
const regEx = new RegExp(keywords.TickTick_TAG.substring(1), 'i');
Expand Down Expand Up @@ -252,32 +261,37 @@ export class TaskParser {
}

//find task items
for (let i = (lineNumber + 1); i <= lines.length; i++) {
const line = lines[i];
//console.log(line)
//If it is a blank line, it means there is no parent
if (this.isLineBlank(line)) {
break;
}
//If the number of tabs is greater than or equal to the current line, it's an item
if (this.getTabIndentation(line) > lineTextTabIndentation) {
//console.log(`Indentation is ${this.getTabIndentation(line)}`)
if (this.hasTickTickId(line)) {
//it's another task bail
// When Full Vault Sync is enabled, we can tell the difference between items and subtasks
// everything is a subtask
// TODO: in the fullness of time, see if there's a way to differentiate.
if (!this.plugin.settings.enableFullVaultSync) {
for (let i = (lineNumber + 1); i <= lines.length; i++) {
const line = lines[i];
//console.log(line)
//If it is a blank line, it means there is no parent
if (this.isLineBlank(line)) {
break;
}
//If the number of tabs is greater than or equal to the current line, it's an item
if (this.getTabIndentation(line) > lineTextTabIndentation) {
//console.log(`Indentation is ${this.getTabIndentation(line)}`)
if (this.hasTickTickId(line)) {
//it's another task bail
break;
}
const item = this.getItemFromLine(line);
taskItems.push(item);
} else {
//we're either done with items, or onto the next task or blank line.
//we're done.
break;
}
const item = this.getItemFromLine(line);
taskItems.push(item);
} else {
//we're either done with items, or onto the next task or blank line.
//we're done.
break;
}

}
}

let dueDateStruct = this.getDueDateFromLineText(textWithoutIndentation);
var timeZone = Intl.DateTimeFormat().resolvedOptions().timeZone;
let timeZone = Intl.DateTimeFormat().resolvedOptions().timeZone;


const tags = this.getAllTagsFromLineText(textWithoutIndentation);
Expand All @@ -292,6 +306,7 @@ export class TaskParser {
projectName = await this.plugin.cacheOperation?.getProjectNameByIdFromCache(projectId);
}
} else {
//Check if we need to add this to a specific project by tag.
if (tags) {
for (const tag of tags) {
let labelName = tag.replace(/#/g, '');
Expand Down Expand Up @@ -670,7 +685,14 @@ export class TaskParser {

addTickTickTag(str: string): string {
//TODO: assumption that there is at least one space before. validate.
return (str + `${keywords.TickTick_TAG}`);
if (str.charAt(str.length - 1) === ' ')
{
str = (str + `${keywords.TickTick_TAG}`);
} else {
str = (str + ` ${keywords.TickTick_TAG} `);
}
console.log("####", str);
return str;
}

getObsidianUrlFromFilepath(filepath: string) {
Expand All @@ -683,6 +705,7 @@ export class TaskParser {
}

addTickTickLink(linetext: string, taskId: string, projecId: string): string {

let url = this.createURL(taskId, projecId);
const regex = new RegExp(`${keywords.TickTick_TAG}`, 'gi');
const link = ` [link](${url})`;
Expand Down Expand Up @@ -759,8 +782,15 @@ export class TaskParser {
private addItems(resultLine: string, items: any[]): string {
//TODO count indentations?
items.forEach(item => {
let completion = item.status > 0 ? '- [X]' : '- [ ]';
resultLine = `${resultLine} \n${completion} ${item.title} %%${item.id}%%`;
let completion = item.status > 0 ? '- [x]' : '- [ ]';
// When Full Vault Sync is enabled, we can tell the difference between items and subtasks
// everything is a subtask
// TODO: in the fullness of time, see if there's a way to differentiate.
if (!this.plugin.settings.enableFullVaultSync) {
resultLine = `${resultLine} \n${completion} ${item.title} %%${item.id}%%`;
} else {
resultLine = `${resultLine} \n${completion} ${item.title}`;
}
});
return resultLine;
}
Expand Down

0 comments on commit a15c45a

Please sign in to comment.