From 14c752548d2899b7411fcc8480ce3e2784df1253 Mon Sep 17 00:00:00 2001 From: Martin Raifer Date: Fri, 26 May 2023 17:59:04 +0200 Subject: [PATCH] keep cursor at edit position while typing in the middle --- CHANGELOG.md | 2 ++ modules/util/get_set_value.js | 12 ++++++++++-- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 52125745d..c83345c2d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -52,6 +52,7 @@ _Breaking developer changes, which may affect downstream projects or sites that * Properly handle case sensitive tag values in taginfo suggestions in raw tag editor ([#9640]) * Fix dysfunctional autocomplete of wikidata fields for some languages with country-codes ([#9638]) * Prevent certain tag values from corrupting css classes when they contain whitespaces ([#9637], thanks [@k-yle]) +* Don't move the cursor to the end of (some) input fields while editing in the middle ([#9233]) #### :earth_asia: Localization * Send `Accept-Language` header on Nominatim API calls ([#9501], thanks [@k-yle]) * Add Address and Phone format for India ([#9482], thanks [@biswajit-k]) @@ -69,6 +70,7 @@ _Breaking developer changes, which may affect downstream projects or sites that * Build icons from configured presets source and also process field value `icons` in `npm run build:data` [#7427]: https://github.com/openstreetmap/iD/issues/7427 +[#9233]: https://github.com/openstreetmap/iD/issues/9233 [#9482]: https://github.com/openstreetmap/iD/pull/9482 [#9483]: https://github.com/openstreetmap/iD/pull/9483 [#9492]: https://github.com/openstreetmap/iD/pull/9492 diff --git a/modules/util/get_set_value.js b/modules/util/get_set_value.js index 486a3902e..305ae5aad 100644 --- a/modules/util/get_set_value.js +++ b/modules/util/get_set_value.js @@ -2,7 +2,7 @@ // which can result in layout/repaint thrashing in some situations. /** @returns {string} */ export function utilGetSetValue(selection, value) { - function d3_selection_value(value) { + function setValue(value) { function valueNull() { delete this.value; } @@ -27,9 +27,17 @@ export function utilGetSetValue(selection, value) { ? valueFunction : valueConstant); } + function stickyCursor(func) { + return function() { + const cursor = { start: this.selectionStart, end: this.selectionEnd }; + func.apply(this, arguments); + this.setSelectionRange(cursor.start, cursor.end); + } + } + if (arguments.length === 1) { return selection.property('value'); } - return selection.each(d3_selection_value(value)); + return selection.each(stickyCursor(setValue(value))); }