Add keyMap for toggling TODO in edit mode

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