Action

3 State Check Box Sorting

Last update over 2 years ago

This action is a modified version of “Move Checked to End” by zisoft
But made to also work with “- { }”, “- {-}”, “- {x}” type checkboxes

this version will…
move checked items “- [x]” & “- {x}” to the end of a list
move ongoing items “- {-}” to the beginning of a list,
but after regular text
while maintaining list sections, if you have multiple lists in 1 document
move indented items with the parent task (indented items will not be sorted)
* do NOT indent your parent items
intended to be used with @aglietortoie’s “simple list”, "simple list+”, or "markdown” type syntaxes

<< instructions >>
- if you would like to change the order, modify line #91
- a break for a check list is any line with
- blank line
- only spaces and tabs
- starts with letters and no spaces before the letters
* if you need to indent your list title, add a blank line above it

original action heavily referenced : https://directory.getdrafts.com/a/1eO
“Move Checked to End” by zisoft
original documentation // See online documentation for examples
https://docs.getdrafts.com/docs/actions/scripting

A better “before and after” comparison with indentation shown in action discription (sorry cannot seem to make it show correctly here)

[Before]
Title
- { } test 1
- {-} test 2
- [x] test 3
- {x} test 4
- {-} test 5

[After]
Title
- {-} test 2
- {-} test 5
- { } test 1
- [x] test 3
- {x} test 4

Steps

  • script

    // move checked items "- [x]" & "- {x}" to the end of a list
    // move ongoing items "- {-}" to the beginning of a list,
    // but after regular text
    // while maintaining list sections, if you have multiple lists in 1 document
    // move indented items with the parent task (indented items will not be sorted)
    //     * do NOT indent your parent items
    // intended to be used with @aglietortoise's "simple list”, "simple list+”, or "markdown" type syntaxes
    
    // << instructions >>
    // if you would like to change the order, modify line #92
    // a break for a check list is any line with
    //     - blank line
    //     - only spaces and tabs
    //     - starts with letters and no spaces before the letters
    //     * if you need to indent your list title, add a blank line above it
    
    // original action referenced : https://directory.getdrafts.com/a/1eO
    // "Move Checked to End" by zisoft
    //     original documentation // See online documentation for examples
    //     https://docs.getdrafts.com/docs/actions/scripting
    
    
    // grab draft content
    let content = editor.getText();
    
    // separate each line of content into arrays
    let lines = content.split("\n");
    lines.push(""); // make sure the content ends with an empty line
    
    // creating  "boxes", for possible outcomes
    // "- { }" or "- [ ]" : Unstarted
    // "- {-}" : Started
    // "- {x}" or "- [x]" : Completed
    // Others : unchecked
    let unchecked = [];
    let itemUnstarted = [];
    let itemStarted = [];
    let itemCompleted = [];
    let new_content = [];
    
    // Setting a variable for last line used, so indented lines can follow the parent line "box"
    var lastLine = ""
    
    // to find indented items
    const re = /^(\s*)(.*)/;
    
    
    for (let line of lines) {
        //search all lines, if it starts with spaces, and has letters 
        const match = re.exec(line);
        // if line startsWith "- {" or "- [" or (line not emply & starts with spaces and has letters))
        if (line.startsWith("- {") || line.startsWith("- [") || (line.length > 0 && match[1] && match[2])) {    //line.length > 0 && line.match(re)
            // found a list item
            if (line.startsWith("- { }") || (line.startsWith("- [ ]"))) {
                itemUnstarted.push(line);
                lastLine = line;
            }
            else if (line.startsWith("- {-}")) {
                itemStarted.push(line);
                lastLine = line;
            }
            else if (line.startsWith("- {x}") || (line.startsWith("- [x]"))) {
                itemCompleted.push(line);
                lastLine = line;
            }
            else if (line.length > 0 && match[1] && match[2]) {
                // if line is indented, look at previous un-indented line and follow which box it went to
                if (lastLine.startsWith("- { }") || (lastLine.startsWith("- [ ]"))) {
                    itemUnstarted.push(line);
                }
                else if (lastLine.startsWith("- {-}")) {
                    itemStarted.push(line);
                }
                else if (lastLine.startsWith("- {x}") || (lastLine.startsWith("- [x]"))) {
                    itemCompleted.push(line);
                }
                else {
                    unchecked.push(line);
                }
            }
            else {
                // starts with "- {" or "- [" but not a check box
                unchecked.push(line);
                lastLine = line;
            }
        }
        else {
            // not with in a list
            if (unchecked.length > 0 || itemUnstarted.length > 0 || itemStarted.length > 0 || itemCompleted.length > 0) {
                // output lists
                // change order inside "concat", to change checklist priority
                new_content = new_content.concat(unchecked, itemStarted, itemUnstarted, itemCompleted);
                
                // reset "boxes" for next list
                unchecked = [];
                itemUnstarted = [];
                itemStarted = [];
                itemCompleted = [];
            }
            
            new_content.push(line);
            lastLine = line;
        }
    }
    
    
    new_content.pop();    // remove the added empty line
    
    editor.setText(new_content.join("\n"));
    editor.activate();
    

Options

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