From bc897f3002c1c642c12b7f70b33cb43bc9dddbe5 Mon Sep 17 00:00:00 2001 From: Siddharth VP Date: Sat, 12 Oct 2024 02:17:51 +0530 Subject: [PATCH] CodeMirror support for WikiEditor keyboard shortcuts Forward keydown events on the CodeMirror DOM element to the #wpTextbox1 element, on which WikiEditor's handlers are registered. CM6 support to come later. Bug: T62928 Depends-On: I18db5b6c53000457524573a9ae06939e8df0dc4f Change-Id: I0e67f2db6b29636fe25583e926a9fb12a69e3851 --- resources/legacy/ext.CodeMirror.WikiEditor.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/resources/legacy/ext.CodeMirror.WikiEditor.js b/resources/legacy/ext.CodeMirror.WikiEditor.js index 998cb77f..fb27db53 100644 --- a/resources/legacy/ext.CodeMirror.WikiEditor.js +++ b/resources/legacy/ext.CodeMirror.WikiEditor.js @@ -196,6 +196,16 @@ function init() { codeMirror.on( 'blur', () => { $textbox1[ 0 ].dispatchEvent( new Event( 'blur' ) ); } ); + codeMirror.on( 'keydown', ( _, e ) => { + if ( e.ctrlKey || e.metaKey ) { + // Possibly a WikiEditor keyboard shortcut + if ( !$textbox1[ 0 ].dispatchEvent( new KeyboardEvent( 'keydown', e ) ) ) { + // If it was actually a WikiEditor keyboard shortcut, the default would be prevented + // for the dispatched event and hence dispatchEvent() would return false + e.preventDefault(); + } + } + } ); mw.hook( 'editRecovery.loadEnd' ).add( ( data ) => { codeMirror.on( 'change', data.fieldChangeHandler ); } );