From a6030db8580928ab2210e98fec62c0921017136e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1t=C3=A9=20Szab=C3=B3?= Date: Fri, 21 Jul 2023 14:45:41 +0200 Subject: [PATCH] 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 --- modules/jquery.codeEditor.js | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/modules/jquery.codeEditor.js b/modules/jquery.codeEditor.js index d4d6da9d..59f84e1f 100644 --- a/modules/jquery.codeEditor.js +++ b/modules/jquery.codeEditor.js @@ -781,9 +781,24 @@ /** * Gets the position (in resolution of bytes not nessecarily characters) in a textarea * 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 () { - mw.log( 'codeEditor stub function getCaretPosition called' ); + getCaretPosition: function ( options ) { + 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; }, /**