For posterity, an easy way to implement this is to add a handleKeyDown
to the inputRules
function of prosemirror-inputrules
. Original source code here.
export function inputRules({rules}) {
let plugin = new Plugin({
state: {
init() { return null },
apply(tr, prev) {
let stored = tr.getMeta(this)
if (stored) return stored
return tr.selectionSet || tr.docChanged ? null : prev
}
},
props: {
handleTextInput(view, from, to, text) {
return run(view, from, to, text, rules, plugin)
},
handleDOMEvents: {
compositionend: (view) => {
setTimeout(() => {
let {$cursor} = view.state.selection
if ($cursor) run(view, $cursor.pos, $cursor.pos, "", rules, plugin)
})
}
},
handleKeyDown(view, event) {
if (event.key !== "Enter") return false;
let {$cursor} = view.state.selection
if ($cursor) return run(view, $cursor.pos, $cursor.pos, "\n", rules, plugin)
return false;
}
},
isInputRules: true
})
return plugin
}
Within a specific inputrule, you would then want to add the \n
character at the end. For example for codeblocks,
textblockTypeInputRule(/^```([^\s]*)[\s\n]$/, nodeType, match => ({...match[1] && {lang: match[1]}}))
Edit: gif of working example