我使用摩纳哥编辑器,我想定义,当用户点击Enter key,它将只适用于特定的条件。
这是我的代码:
renderEditor(el: HTMLElement) {
this.editor = monaco.editor.create(el, {
...
...
});
this.editor.onKeyDown(this.keyDown);
}
keyDown(event: IKeyboardEvent): void {
if (event.code == 'ArrowDown') {
const shouldInsertNewLine = checkIfEnterShouldApplyOnEditorOrNot();
if(! shouldInsertNewLine){
// ?? code here to cancel the 'ArrowDown' hit ??
// (other action happens instead)
}
}
}现在,如果checkIfShouldMoveCursorOnEditorOrNot返回false,那么arrowDown键不应该应用于编辑器(光标不应该移动到下一行)。
我怎样才能做到这一点?
我试过使用event.preventDefault(),但它不起作用。
发布于 2022-09-05 18:06:32
可以使用editor.addCommand
https://github.com/Microsoft/monaco-editor/issues/940在这里找到了答案,
renderEditor(el: HTMLElement) {
this.editor = monaco.editor.create(el, {..});
this.editor.onKeyDown(this.keyDown);
this.shouldInsertNewLine = this.editor.createContextKey('shouldInsertNewLine', true);
this.editor.addCommand(monaco.KeyCode.Enter, this.shouldInsertNewLine.reset, '!shouldInsertNewLine');
}
keyDown(event: IKeyboardEvent): void {
if (event.code == 'ArrowDown') {
const shouldInsertNewLine = checkIfEnterShouldApplyOnEditorOrNot();
this.shouldInsertNewLine?.set(shouldInsertNewLine);
}
}https://stackoverflow.com/questions/73602087
复制相似问题