Move checked list items to the end of the list

This action moves the checked items of a list to the end of the list, maintaining the structure of the draft. So if you have multiple lists in your document, they are treated each of their own.

Example:
- [ ] item 1
- [x] item 2
- [x] item 3
- [ ] item 4

After running the action the list appears as:
- [ ] item 1
- [ ] item 4
- [x] item 2
- [x] item 3

Move Checked to End

3 Likes

Nice.

My 0,02€ on the code:

 for (let item of unchecked) {
                new_content += item + "\n";
            }

could be written a bit more concisely as

new_content += unchecked.join('\n') + checked.join('\n');
1 Like

If I start with a draft like this:

Example 1:
- [ ] item 1
- [x] item 2
- [x] item 3
- [ ] item 4

Example 2:
- [ ] item A
- [x] item B
- [x] item C
- [ ] item D

Example 3:
- [ ] item 1
- [x] item 2
- [x] item 3
- [ ] item 4

Then run the action, I get a draft that looks like this:

Example 1:
- [ ] item 1
- [ ] item 4
- [x] item 2
- [x] item 3

Example 2:
- [ ] item A
- [ ] item D
- [x] item B
- [x] item C

Example 3:

However, if I add a new line character to the end of the final line so that it ends like this:

Example 3:
- [ ] item 1
- [x] item 2
- [x] item 3
- [ ] item 4

The result is then more what you would expect.

Example 1:
- [ ] item 1
- [ ] item 4
- [x] item 2
- [x] item 3

Example 2:
- [ ] item A
- [ ] item D
- [x] item B
- [x] item C

Example 3:
- [ ] item 1
- [ ] item 4
- [x] item 2
- [x] item 3

I think your current logic would benefit from a tweak to account for the case of the last line of a multi-list draft not ending in a non-checklist entry.

Without rewriting some of your core code to address this edge case, at the start, you could carry out a check on the last line, append a newline if necessary, keep a note if you do, and then remove the newline at the end of the processing if one was added.

For the following code:

if (device.model === "iPhone") {
    app.hideActionList();
}

You may also want to check out if editor.activate() might give you another option for what you want to do?

Hope that helps.

1 Like

@sylumer: Many thanks for the hints, I have refined my code