Action

Add to List (Custom)

Last update over 4 years ago - Unlisted

Opens a prompt to select the list where you want to add the trimmed content of the current draft.
An option to create a new list is also presented. After the addition the next available draft is loaded that was not used for the addition, and is not a list draft (as defined by the presence of a unique tag). Should such a draft not be available, a new draft is created and loaded in.

Steps

  • script

    // add to list
    // tag to assign to list drafts
    const listTag = "ref_list";
    
    
    // grab text
    const currentContent = draft.content.trim();
    
    let res;
    {
    	let ws = Workspace.create();
    	ws.tagFilter = listTag;
    	ws.tagFilterRequireAll = true;
    	res = ws.query("all");
    }
    res.sort((a, b) => (a.title.toUpperCase() > b.title.toUpperCase()) ? 1 : -1);
    let listTitles = [];
    let p = Prompt.create();
    
    for (i in res)
    {
    	p.addButton(res[i].title, res[i].uuid);
    	listTitles.push(res[i].title);
    }
    p.addButton("CREATE NEW LIST");
    
    showSelectPrompt:
    
    if (p.show() == 1)
    {
    	if (p.buttonPressed != "CREATE NEW LIST")
    	{
    		var selecteduuid = p.buttonPressed;
    		var selectedDraft = Draft.find((selecteduuid));
    	}
    	else
    	{
    		var pN = Prompt.create();
    		pN.title = "create new list";
    		pN.message = "set title for new list";
    		pN.addTextField("setTitle", "set title", "", [autocorrect = false, autocapitalization = "words", keyboard = "default", wantsFocus = true]);
    		pN.addButton("set title");
    		if (pN.show())
    		{
    			if (pN.buttonPressed == "set title")
    			{
    				var newListCategory = pN.fieldValues["setTitle"];
    				if (newListCategory != "")
    				{
    					if (listTitles.includes(newListCategory))
    					{
    						alert("list already exists, restart action and select the right list!");
    						context.cancel("user selected already existing list!")
    					}
    					else
    					{
    						var selectedDraft = Draft.create();
    						selectedDraft.content = "# " + newListCategory + "\n\n";
    						selectedDraft.languageGrammar = "Simple List";
    					}
    				}
    				else
    				{
    					alert("you must set a title, aborting!");
    					context.cancel("no title specified");
    				}
    			}
    			else
    			{
    				context.cancel("user cancelled title input");
    			}
    		}
    	}
    
    
    	let category = selectedDraft.title;
    
    	let d;
    	if (selectedDraft.content.startsWith("# " + category))
    	{
    		d = selectedDraft;
    	}
    
    	d.addTag(listTag);
    	d.content = d.content + "- [ ] " + currentContent + "\n"
    	d.update();
    
    	editor.load(d);
    }
    else
    {
    	context.cancel("user cancelled add to list");
    }
    
    loadDrafts = Draft.query("", "inbox", [], [listTag], "modified", true);
    if (loadDrafts.length > 1)
    {
    	if (loadDrafts[0].uuid == draft.uuid)
    	{
    		app.displayInfoMessage("loaded second draft from Drafts inbox");
    		editor.load(loadDrafts[1]);
    	}
    	else
    	{
    		app.displayInfoMessage("loaded first draft from Drafts inbox");
    		editor.load(loadDrafts[0]);
    	}
    }
    else if (loadDrafts.length == 1)
    {
    	if (loadDrafts[0].uuid == draft.uuid)
    	{
    		app.displayWarningMessage("created new draft, no reload of draft used for list addition");
    		editor.load(Draft.create());
    	}
    	else
    	{
    		app.displayInfoMessage("loaded first draft from Drafts inbox");
    		editor.load(loadDrafts[0]);
    	}
    }
    else
    {
    	app.displayWarningMessage("created new draft, inbox was empty");
    	editor.load(Draft.create());
    }

Options

  • After Success Default
    Notification Error
    Log Level Error
Items available in the Drafts Directory are uploaded by community members. Use appropriate caution reviewing downloaded items before use.