Skip to content

Commit

Permalink
feat: create from selected template project
Browse files Browse the repository at this point in the history
  • Loading branch information
ksalzke committed Oct 3, 2021
1 parent 122eabd commit 0ad62a7
Showing 1 changed file with 17 additions and 18 deletions.
35 changes: 17 additions & 18 deletions Resources/createFromTemplate.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down

0 comments on commit 0ad62a7

Please sign in to comment.