|
/** |
|
* Ryan_quickAddLines - This action creates a new task for each line in the currently open Draft. |
|
* |
|
* @param {String} text the text which lines should be added as tasks to Todoist |
|
* @param {String} project the project to add the task |
|
* @return {Boolean|Number} false when adding fails, task number when adding succeeded |
|
*/ |
|
function Ryan_quickAddLines(text, project = null) { |
|
let todoist = new Todoist() |
|
let lines = text.split("\n"); |
|
let createdTasksCounter = 0; |
|
// repeat for each line |
|
for (line of lines) { |
|
if (line.length !== 0) { |
|
if (!Ryan_quickAdd({ |
|
todoist: todoist, |
|
content: line, |
|
project: project |
|
})) { |
|
// if failed directly return, quickadd will display the error |
|
return false; |
|
} else { |
|
createdTasksCounter++; |
|
} |
|
} |
|
} |
|
return createdTasksCounter; |
|
} |
|
|
|
|
|
/** |
|
* Ryan_quickAdd - This function adds the provided string to Todoist using the quickAdd API. |
|
* This supports the Todoist natural language which will be processed by Todoist automatically. |
|
* |
|
* @param {Todoist_Object|undefined} todoist_obj - if already created, otherwise the function will create its own. |
|
* @param {string} content the task content as string |
|
* @param {String} project the project to add the task |
|
* @return {Boolean} true when added successfully, false when adding task failed |
|
*/ |
|
function Ryan_quickAdd({ |
|
todoist = new Todoist(), |
|
content, |
|
project = null |
|
}) { |
|
if (project) { |
|
content = `${content} #${project}` |
|
} |
|
if (!todoist.quickAdd(content)) { |
|
let error = Draftist_getLastTodoistError(todoist) |
|
let errorMsg = "adding tasks failed, todoist returned:\n" + error |
|
Draftist_failAction("Quick Add", errorMsg) |
|
return false; |
|
} else { |
|
return true; |
|
} |
|
} |
|
|
|
|
|
/** |
|
* Ryan_quickAddLinesFromCurrentDraft - use todoist quick add for each line of the current draft |
|
* @param {String} project the project to add the task |
|
* |
|
* @return {Boolean} true when added successfully, false when adding task failed |
|
*/ |
|
function Ryan_quickAddLinesFromCurrentDraft(project=null) { |
|
if (draft.content.length == 0) { |
|
Draftist_cancelAction("Add Tasks from current Draft", "Draft is blank") |
|
return false; |
|
} else { |
|
let taskNumber = Ryan_quickAddLines(draft.content, project); |
|
if (taskNumber) { |
|
// succeeded |
|
projectSuccess = "" |
|
if (project) { |
|
projectSuccess = ` to project ${project}` |
|
} |
|
Draftist_succeedAction("", false, "successfully added " + taskNumber + " tasks" + projectSuccess) |
|
return true; |
|
} else { |
|
return false; |
|
} |
|
} |
|
} |