FIX: allows selectText to take a scroll position as opt (#28554)

Prior to this fix using:

- `replaceText(...)`
- `selectText(..., ..., { scroll: true})`

Wouldn't have the expected behaviour as the scroll from selectText will attempt to save the scroll position and restore it AFTER the replacement happened. This commit allows scroll to be a Boolean or a Number, when a number, it will be used to restore the scrollTop position.

I tried to write tests for this specific behavior but couldn't reproduce the issue in tests.
This commit is contained in:
Joffrey JAFFEUX 2024-08-26 18:32:52 +02:00 committed by GitHub
parent 0e7d5b712a
commit d62c32ba71
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -114,11 +114,13 @@ export default Mixin.create({
if (!this.element) { if (!this.element) {
return; return;
} }
this._textarea.selectionStart = from; this._textarea.selectionStart = from;
this._textarea.selectionEnd = from + length; this._textarea.selectionEnd = from + length;
if (opts.scroll) { if (opts.scroll === true || typeof opts.scroll === "number") {
const oldScrollPos = this._textarea.scrollTop; const oldScrollPos =
typeof opts.scroll === "number"
? opts.scroll
: this._textarea.scrollTop;
if (!this.capabilities.isIOS) { if (!this.capabilities.isIOS) {
this._textarea.focus(); this._textarea.focus();
} }
@ -578,6 +580,7 @@ export default Mixin.create({
let autocompletePrefix = `${indentationLevel}${newPrefix}`; let autocompletePrefix = `${indentationLevel}${newPrefix}`;
let autocompletePostfix = text.substring(offset); let autocompletePostfix = text.substring(offset);
const autocompletePrefixLength = autocompletePrefix.length; const autocompletePrefixLength = autocompletePrefix.length;
let scrollPosition;
/* /*
For numeric items, we have to also replace the rest of the For numeric items, we have to also replace the rest of the
@ -598,6 +601,7 @@ export default Mixin.create({
numericBullet + 1 numericBullet + 1
); );
autocompletePrefix += autocompletePostfix; autocompletePrefix += autocompletePostfix;
scrollPosition = this._textarea.scrollTop;
this.replaceText( this.replaceText(
text.substring(offset, offset + autocompletePrefix.length), text.substring(offset, offset + autocompletePrefix.length),
@ -610,7 +614,9 @@ export default Mixin.create({
this._insertAt(offset, offset, autocompletePrefix); this._insertAt(offset, offset, autocompletePrefix);
} }
this.selectText(offset + autocompletePrefixLength, 0); this.selectText(offset + autocompletePrefixLength, 0, {
scroll: scrollPosition,
});
} else { } else {
// Clear the new autocompleted list item if there is no other text. // Clear the new autocompleted list item if there is no other text.
const offsetWithoutPrefix = offset - `\n${listPrefix}`.length; const offsetWithoutPrefix = offset - `\n${listPrefix}`.length;