From 0ad62a7bd0f1d94ed711cd485bd0ef98f5e427dc Mon Sep 17 00:00:00 2001 From: Kaitlin Salzke Date: Sun, 3 Oct 2021 11:31:41 +1100 Subject: [PATCH] feat: create from selected template project --- Resources/createFromTemplate.js | 35 ++++++++++++++++----------------- 1 file changed, 17 insertions(+), 18 deletions(-) diff --git a/Resources/createFromTemplate.js b/Resources/createFromTemplate.js index 6793607..e85ed4b 100644 --- a/Resources/createFromTemplate.js +++ b/Resources/createFromTemplate.js @@ -1,27 +1,22 @@ /* global PlugIn Form Preferences */ (() => { const action = new PlugIn.Action(async function (selection, sender) { - // action code - // selection options: tasks, projects, folders, tags const templateLibrary = this.templateLibrary - // load preferences const preferences = new Preferences('com.KaitlinSalzke.Templates') - const templateFormPromise = generateTemplateForm().show( - 'Choose Template', - 'Create' - ) - - templateFormPromise.then(async function (form) { - const destination = await templateLibrary.getDestination( - form.values.template - ) - const created = await templateLibrary.createFromTemplate(form.values.template, destination) - if (form.values.goTo) URL.fromString('omnifocus:///task/' + created.id.primaryKey).call(() => {}) - }) + let template = (selection.projects.length === 1 && templateLibrary.getTemplateFolder().flattenedProjects.includes(selection.projects[0])) ? selection.projects[0] : null + if (template === null) { + templateForm = await generateTemplateForm() + await templateForm.show('Choose Template', 'Create') + template = templateForm.values.template + } + + const destination = await templateLibrary.getDestination(template) + const created = await templateLibrary.createFromTemplate(template, destination) + if (templateForm.values.goTo) URL.fromString('omnifocus:///task/' + created.id.primaryKey).call(() => {}) - function generateTemplateForm () { + async function generateTemplateForm () { // select template to use and destination - show form const templateFolder = templateLibrary.getTemplateFolder() const templateProjects = templateFolder.flattenedProjects @@ -48,11 +43,15 @@ }) action.validate = function (selection, sender) { + // get templateFolder + const lib = this.templateLibrary + // load preferences const preferences = new Preferences('com.KaitlinSalzke.Templates') - // show if preferences are set to always enabled or if nothing is selected - return preferences.readBoolean('alwaysEnable') || (selection.tasks.length === 0 && selection.projects.length === 0) + return preferences.readBoolean('alwaysEnable') // show if always enabled preference is set + || (selection.tasks.length === 0 && selection.projects.length === 0) // show if nothing selected + || (selection.projects.length === 1 && lib.getTemplateFolder().flattenedProjects.includes(selection.projects[0])) // show if a template project is selected } return action