jenkins-bot has submitted this change and it was merged. (
https://gerrit.wikimedia.org/r/375617 )
Change subject: Fix cursor position after extension loads
..
Fix cursor position after extension loads
Bug: T174547
Change-Id: I8ce2c61884410c4ca47bd8cbc11d0902df7f1d4a
---
M resources/ext.CodeMirror.js
1 file changed, 25 insertions(+), 8 deletions(-)
Approvals:
jenkins-bot: Verified
Kaldari: Looks good to me, approved
diff --git a/resources/ext.CodeMirror.js b/resources/ext.CodeMirror.js
index e58a5f3..732816a 100644
--- a/resources/ext.CodeMirror.js
+++ b/resources/ext.CodeMirror.js
@@ -324,23 +324,34 @@
* Enables or disables CodeMirror
*/
function switchCodeMirror() {
- var scrollTop,
+ var selectionObj,
+ selectionStart,
+ selectionEnd,
+ scrollTop,
+ hasFocus,
$textbox1 = $( '#wpTextbox1' );
if ( codeMirror ) {
scrollTop = codeMirror.getScrollInfo().top;
+ selectionObj = codeMirror.doc.listSelections()[ 0 ];
+ selectionStart = codeMirror.doc.indexFromPos(
selectionObj.head );
+ selectionEnd = codeMirror.doc.indexFromPos(
selectionObj.anchor );
+ hasFocus = codeMirror.hasFocus();
setCodeEditorPreference( false );
codeMirror.save();
codeMirror.toTextArea();
codeMirror = null;
$.fn.textSelection = origTextSelection;
+ if ( hasFocus ) {
+ $textbox1.focus();
+ }
+ $textbox1.prop( 'selectionStart', selectionStart );
+ $textbox1.prop( 'selectionEnd', selectionEnd );
$textbox1.scrollTop( scrollTop );
} else {
- scrollTop = $textbox1.scrollTop();
// eslint-disable-next-line no-use-before-define
enableCodeMirror();
setCodeEditorPreference( true );
- codeMirror.scrollTo( 0, scrollTop );
}
updateToolbarButton();
}
@@ -351,13 +362,16 @@
function enableCodeMirror() {
var config = mw.config.get( 'extCodeMirrorConfig' );
- if ( codeMirror ) {
- return;
- }
-
mw.loader.using( config.pluginModules, function () {
var $codeMirror,
- $textbox1 = $( '#wpTextbox1' );
+ $textbox1 = $( '#wpTextbox1' ),
+ selectionStart = $textbox1.prop(
'selectionStart' ),
+ selectionEnd = $textbox1.prop( 'selectionEnd' ),
+ scrollTop = $textbox1.scrollTop();
+
+ if ( codeMirror ) { // Already loaded
+ return;
+ }
codeMirror = CodeMirror.fromTextArea( $textbox1[ 0 ], {
mwConfig: config,
@@ -372,6 +386,9 @@
} );
$codeMirror = $( codeMirror.getWrapperElement() );
+ codeMirror.doc.setSelection(
codeMirror.doc.posFromIndex( selectionEnd ), codeMirror.doc.posFromIndex(
selectionStart ) );
+ codeMirror.scrollTo( null, scrollTop );
+
// HACK: font size varies by browser
(chrome/FF/IE)
$codeMirror.css( {
'font-size': $textbox1.css( 'font-size' ),
--
To view, visit https://gerrit.wikimedia.org/r/375617
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: I8ce2c61884410c4ca47bd8cbc11d0902df7f1d4a
Gerrit-PatchSet: 6
Gerrit-Project: mediawiki/extensions/CodeMirror
Gerrit-Branch: master
Gerrit-Owner: Pastakhov
Gerrit-Reviewer: Kaldari
Gerrit-Reviewer: jenkins-bot <>
___
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits