Action

Toggle Done

Posted by niftinessafoot, Last update 9 months ago

Uses Drafts’ Tasks API to toggle task completion state, regardless of syntax. Works with Markdown, Simple List, and Taskpaper syntaxes.

Steps

  • script

    const [lineStart, lineLength] = editor.getSelectedLineRange();
    const [cursorPos] = editor.getSelectedRange();
    const index = draft.tasks.findIndex(
      ({ lineRange }) => lineRange[0] === lineStart
    );
    const currentTask = draft.tasks[index];
    
    if (!!currentTask) {
      if (!currentTask.isCompleted) {
        editor.completeTask(currentTask);
      } else {
        editor.resetTask(currentTask);
    
        // Refresh cursor position—updated state may have changed the line length.
        const newLength = draft.tasks[index].lineRange.reduce((a, b) => a + b, 0);
    
        if (cursorPos > newLength) {
          editor.setSelectedRange(newLength, 0);
        }
      }
    
      draft.update();
    }
    

Options

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