Follow-up Ie3ed681b: Unstub getCaretPosition() as well

It was pointed out in the review of Ie3ed681b that the naive fallback
for replaceSelection() was not working because CodeEditor only had a
stub getCaretPosition() implementation. So, let's implement that as well
in case people want to use it. It seems to work reasonably well locally
with the native replaceSelection() disabled.

Bug: T342127
Change-Id: I8d3683808394faefa56c7cdff31dc18978c4dd92
This commit is contained in:
Máté Szabó 2023-07-21 14:45:41 +02:00
parent 2f7ce4dd50
commit a6030db858

View file

@ -781,9 +781,24 @@
/** /**
* Gets the position (in resolution of bytes not nessecarily characters) in a textarea * Gets the position (in resolution of bytes not nessecarily characters) in a textarea
* DO NOT CALL THIS DIRECTLY, use $.textSelection( 'functionname', options ) instead * DO NOT CALL THIS DIRECTLY, use $.textSelection( 'functionname', options ) instead
*
* @param {Object} options
* @param {Object} [options.startAndEnd=false] Return range of the selection rather than just start
* @return {number|number[]} If options.startAndEnd is true, returns an array holding the start and
* end of the selection, else returns only the start of the selection as a single number.
*/ */
getCaretPosition: function () { getCaretPosition: function ( options ) {
mw.log( 'codeEditor stub function getCaretPosition called' ); var selection = context.codeEditor.getSelection(),
range = selection.getRange(),
doc = context.codeEditor.getSession().getDocument(),
startOffset = doc.positionToIndex( range.start );
if ( options.startAndEnd ) {
var endOffset = doc.positionToIndex( range.end );
return [ startOffset, endOffset ];
}
return startOffset;
}, },
/** /**