From 2d249b2535a44f83896fadf0403a0d75b70ff5a1 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Tue, 24 Oct 2017 23:26:38 +0000 Subject: [PATCH] Permalinks: Fix placement of structure tags based on last cursor position. Props felipeelia, afercia. See #29872. Fixes #42042. git-svn-id: https://develop.svn.wordpress.org/trunk@42012 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-admin/js/common.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/wp-admin/js/common.js b/src/wp-admin/js/common.js index e57af5d682..d8748f53af 100644 --- a/src/wp-admin/js/common.js +++ b/src/wp-admin/js/common.js @@ -247,7 +247,8 @@ $availableStructureTags.on( 'click', function() { selectionStart = $permalinkStructure[ 0 ].selectionStart, selectionEnd = $permalinkStructure[ 0 ].selectionEnd, textToAppend = $( this ).text().trim(), - textToAnnounce = $( this ).attr( 'data-added' ); + textToAnnounce = $( this ).attr( 'data-added' ), + newSelectionStart; // Remove structure tag if already part of the structure. if ( -1 !== permalinkStructureValue.indexOf( textToAppend ) ) { @@ -288,6 +289,13 @@ $availableStructureTags.on( 'click', function() { // Disable button. changeStructureTagButtonState( $( this ) ); + + // If input had focus give it back with cursor right after appended text. + if ( permalinkStructureFocused && $permalinkStructure[0].setSelectionRange ) { + newSelectionStart = ( permalinkStructureValue.substr( 0, selectionStart ) + textToAppend ).length; + $permalinkStructure[0].setSelectionRange( newSelectionStart, newSelectionStart ); + $permalinkStructure.focus(); + } } ); $document.ready( function() {