Action

Cut Task and Prepend at Header

Posted by @derekvan, Last update over 3 years ago

This action takes a task-formatted text and notes, cuts it, and pastes at the selected header.

e.g.,

  • the task
  • the task and a note > note > more note

put the cursor on the line of the task you want and invoke the action. Will grab the whole line of the task as well as any notes below.

Requires two libraries to be installed:

TAD Core: https://actions.getdrafts.com/g/1aM
MGChecklist: https://mattgemmell.com/mgchecklistprompt-docs/

Steps

  • includeAction

    name
    TAD
  • includeAction

    name
    MGCheckListPrompt Library
  • script

    // Select task and notes, cut, begin navigating markers for pasting
    
    let [loc, len] = editor.getSelectedLineRange(),
      str = editor.getTextInRange(loc, len),
      icon = '- [ ]',
      regex2 = /- /;
    
    
    let split = str.split(/\r?\n/,2);
    
    let todoTitle = split[0].trim(); 
    todoTitle = todoTitle.replace(regex2,"");
    
    const fullContent = draft.content;
    
    const fullTask = fullContent.split('\n- ')
                              .find(task => task.includes(todoTitle));
    let notesArray = fullTask.split('\n');
    notesArray.shift();
    
    function filterItems(arr, query) {
      return arr.filter(function(el) {
        return el.toLowerCase().indexOf(query.toLowerCase()) !== -1
      })
    }
    
    let newArray = filterItems(notesArray,"> ");
    let notes = newArray.join('\n');
    
    var theTask = "- " + todoTitle + "\n" + notes;
    
    editor.setSelectedRange(loc, theTask.trim().length);
    var cut = editor.getSelectedText();
    editor.setSelectedText('');
    draft.setTemplateTag("task",theTask);
  • script

    // Navigate Draft
    
    var task = draft.getTemplateTag("task").trim();
    var d = draft.content;
    var selRange = editor.getSelectedRange();
    
    // Get the headers.
    var markers = editor.navigationMarkers;
    console.log(JSON.stringify(markers));
    var markDict = [];
    markDict.push({"title":"Home"});
    for (let m in markers)
    {
        let level = markers[m].level;
        let hash = "";
                for (let step = 0; step <= level; step++)
                {
                    hash += "#";
                }
        
        markDict.push({"title":hash + " " + markers[m].label});
        
    }
    
    markDict.push({"title":"End"});
    console.log(JSON.stringify(markDict));
    
    // Create an MGCheckListPrompt to choose some drafts.
    var prompt = new MGCheckListPrompt();
    prompt.message = "Move to which header?";
    prompt.allowsTypeToSelect= true;
    prompt.singleSelectionMode = true;
    prompt.selectsImmediately = true;
    prompt.addItems(markDict);
    
    // Show the prompt.
    var selectedItems = prompt.show();
    
    // Report the result.
    if (prompt.didShow) {
    	if (selectedItems != null) {
            for (let s of selectedItems)
            {
                var header = markDict[s].title;
            }
            if (header == "Home"){
                editor.setSelectedRange(0,0);
                let q = task;
                editor.setText(q + "\n" + d);
            }
    
            else {
                if (header == "End"){
                    editor.setSelectedRange(d.length,0);
                    let q = task;
                    editor.setText(d + "\n" + q);
            
                }
                else {
                    var position = d.search(header);
                    editor.setSelectedRange(position+header.length,0);
                    let q = editor.TA_sectionPrependByHeading(header,"\n" + task);
                    editor.setText(q);
                }
            }   
            editor.focus();
        }
    
    	 else {
            app.displayInfoMessage("Prompt was cancelled.");
            context.cancel();
    	}
    } else {
        app.displayErrorMessage("Something went wrong.");
        context.cancel();
    }

Options

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