Default “Tasks” action in “Editing” action group not working as expected

The Tasks action in the Editing action group by @agiletortoise doesn’t function as expected.

https://actions.getdrafts.com/g/1Ba

I can insert “- [ ]” just fine, but when I tap the action again, only the task part “[ ]” is removed. The hyphen remains. And then, if you tap the task action again, you get two hyphens. For example…

Task Line <- baseline start
- [ ] Task Line <- first tap
- Task Line <- second tap
- - [ ] Task Line <- third tap
- - [ ] - [ ] Task Line <- fourth tap
- - [ ] - [ ] - [ ] Task Line <- fifth tap

I’ve tried fixing it myself to no avail. Can someone help?

Here is the current script…

// Toggle tasks marks on selected lines
var offState = “- [ ]”;
var onState = “- [x]”;

// grab state
var [lnStart, lnLen] = editor.getSelectedLineRange();
var lnText = editor.getTextInRange(lnStart, lnLen);
var [selStart, selLen] = editor.getSelectedRange();

// just add mark if empty line
if (lnText.length == 0 || lnText == “\n”) {
editor.setSelectedText(${offState} );
editor.setSelectedRange(selStart + offState.length + 1, 0);
}
else {
// create line array and tracking vars
var lines = lnText.split(’\n’);
var startOffset = 0;
var lengthAdjust = 0;
var flTrailing = false;
if (lines[lines.length - 1] == “”) {
lines.pop();
flTrailing = true;
}
var newLines = [];
const re = /^(\s*[-*] )?([ ]|[x])?(.)/;
const containsRe = /^(\s?[-*] )?([ ]|[x])(.
)/;

// determine if we are removing or adding marks
var flRemoving = true;
for (var line of lines) {
if (line.length > 0 && !line.match(containsRe)) {
flRemoving = false;
}
}

if (!flRemoving) {
// add marks
var isFirst = true;
for (var line of lines) {
const match = re.exec(line);
if (match[2] || line.length == 0) {
newLines.push(line);
}
else {
var prefix = match[1];
var suffix = match[3];
if (!prefix) { prefix = “”; }
if (!suffix) { suffix = “”; }
newLines.push(${prefix}${offState} ${suffix});
if (isFirst) {
startOffset = offState.length + 1;
}
else {
lengthAdjust += (offState.length + 1);
}
}
isFirst = false;
}
} else {
// remove marks
var isFirst = true;
for (var line of lines) {
if (line.trim() == “”) {
newLines.push(line);
continue;
}
const match = re.exec(line);
var prefix = match[1];
var suffix = match[3];
var state = match[2];
if (!prefix) { prefix = “”; }
if (!suffix) { suffix = “”; }
if (suffix.startsWith(" ")) {
suffix = suffix.substr(1);
if (isFirst) { startOffset -= 1; }
else { lengthAdjust -= 1; }
}
newLines.push(${prefix}${suffix});
if (isFirst) {
startOffset -= state.length;
}
else {
lengthAdjust -= state.length;
}
isFirst = false;
}
}

// update text and selection
if (flTrailing) {
newLines.push("");
}
var newText = newLines.join("\n");
editor.setTextInRange(lnStart, lnLen, newText);
editor.setSelectedRange(selStart + startOffset, selLen + lengthAdjust);
}

Surprised this hasn’t been noticed before. The adding the additional line mark is definitely a bug. I have updated the action as posted in the directory if you would like to re-install.

As far as the correct behavior when removing task marks, that’s a difficult one because the action has to reference to know what the previous state of the document was to know if it should also be removing list marks - which may have already been there. Using “undo” would be the more reliable way to toggle off if you decide the result of the action was not what you wanted after toggling tasks on.

1 Like