Add keyMap for toggling TODO in edit mode

Execute in console this script

app.workspace.activeLeaf.view.sourceMode.cmEditor.setOption('extraKeys', {
  'Ctrl-Enter': function(cm) {
    const doc = cm.doc
    const lineNum = doc.getCursor().line
    const lineContent = doc.getLine(lineNum)
    const isEmptyTodo = string => /^-\s\[\s\]/.test(string)
    const isCheckedTodo = string => /^-\s\[x\]/.test(string)
    
    const check = () => doc.replaceRange('x', {line: lineNum, ch: 3}, {line: lineNum, ch: 4})
    const uncheck = () => doc.replaceRange(' ', {line: lineNum, ch: 3}, {line: lineNum, ch: 4})

    if (isEmptyTodo(lineContent)) {
        console.log('empty')
        check()
        return true
    }

    if (isCheckedTodo(lineContent)) {
        console.log('checked')
        uncheck()
        return true
    }
    
    console.log('not todo')
    return false
  }
})

@Silver I think something like this could be added into release with no problem as that’s a usual CodeMirror binding, right?

4 Likes

Snippets below will add a global keyMap (previous was only for current pane)

When Vim disabled:

CodeMirror.keyMap.default= {...CodeMirror.keyMap.default, ...{
      'Ctrl-Enter': function(cm) {
        const doc = cm.doc
        const lineNum = doc.getCursor().line
        const lineContent = doc.getLine(lineNum)
        const isEmptyTodo = string => /^-\s\[\s\]/.test(string)
        const isCheckedTodo = string => /^-\s\[x\]/.test(string)

        const check = () => doc.replaceRange('x', {line: lineNum, ch: 3}, {line: lineNum, ch: 4})
        const uncheck = () => doc.replaceRange(' ', {line: lineNum, ch: 3}, {line: lineNum, ch: 4})

        if (isEmptyTodo(lineContent)) {
            console.log('empty')
            check()
            return true
        }

        if (isCheckedTodo(lineContent)) {
            console.log('checked')
            uncheck()
            return true
        }

        console.log('not todo')
        return false
      }
    }
}

When Vim enabled:

CodeMirror.keyMap.vim = {...CodeMirror.keyMap.default, ...{
      'Ctrl-Enter': function(cm) {
        const doc = cm.doc
        const lineNum = doc.getCursor().line
        const lineContent = doc.getLine(lineNum)
        const isEmptyTodo = string => /^-\s\[\s\]/.test(string)
        const isCheckedTodo = string => /^-\s\[x\]/.test(string)

        const check = () => doc.replaceRange('x', {line: lineNum, ch: 3}, {line: lineNum, ch: 4})
        const uncheck = () => doc.replaceRange(' ', {line: lineNum, ch: 3}, {line: lineNum, ch: 4})

        if (isEmptyTodo(lineContent)) {
            console.log('empty')
            check()
            return true
        }

        if (isCheckedTodo(lineContent)) {
            console.log('checked')
            uncheck()
            return true
        }

        console.log('not todo')
        return false
      }
    }
}

You can run both snippets to have functionality in both modes.

1 Like

Thanks a lot for this @mrjackphil