Commit 8a7299db authored by Phil Hughes's avatar Phil Hughes

Correctly replaces string when selecting in dropdown

parent 7d608fdd
...@@ -76,13 +76,25 @@ ...@@ -76,13 +76,25 @@
static getSearchInput(filteredSearchInput) { static getSearchInput(filteredSearchInput) {
const selectionStart = filteredSearchInput.selectionStart; const selectionStart = filteredSearchInput.selectionStart;
const inputValue = filteredSearchInput.value; const inputValue = filteredSearchInput.value;
const rightPos = inputValue.slice(selectionStart).search(/\s/); const { right } = gl.DropdownUtils.getInputSelectionPosition(filteredSearchInput);
if (rightPos < 0) { if (right < 0) {
return inputValue; return inputValue;
} }
return inputValue.slice(0, rightPos + selectionStart + 1).trim(); return inputValue.slice(0, right + selectionStart + 1).trim();
}
static getInputSelectionPosition(input) {
const inputValue = input.value;
const selectionStart = input.selectionStart;
const left = inputValue.slice(0, selectionStart + 1).search(/\S+$/);
const right = inputValue.slice(selectionStart).search(/\s/);
return {
left,
right,
};
} }
} }
......
...@@ -62,30 +62,24 @@ ...@@ -62,30 +62,24 @@
static addWordToInput(tokenName, tokenValue = '') { static addWordToInput(tokenName, tokenValue = '') {
const input = document.querySelector('.filtered-search'); const input = document.querySelector('.filtered-search');
const inputValue = input.value;
const word = `${tokenName}:${tokenValue}`; const word = `${tokenName}:${tokenValue}`;
const inputValue = gl.DropdownUtils.getSearchInput(input).trim(); // Get the string to replace
const { lastToken, searchToken } = gl.FilteredSearchTokenizer.processTokens(inputValue); const selectionStart = input.selectionStart;
const lastSearchToken = searchToken.split(' ').last(); const { left } = gl.DropdownUtils.getInputSelectionPosition(input);
const lastInputCharacter = input.value[input.value.length - 1]; let { right } = gl.DropdownUtils.getInputSelectionPosition(input);
const lastInputTrimmedCharacter = input.value.trim()[input.value.trim().length - 1];
// Remove the typed tokenName if (right < 0) {
if (word.indexOf(lastSearchToken) === 0 && searchToken !== '') { right = inputValue.length;
// Remove spaces after the colon
if (lastInputCharacter === ' ' && lastInputTrimmedCharacter === ':') {
input.value = input.value.trim();
}
input.value = input.value.slice(0, -1 * lastSearchToken.length);
} else if (lastInputCharacter !== ' ' || (lastToken && lastToken.value[lastToken.value.length - 1] === ' ')) {
// Remove the existing tokenValue
const lastTokenString = `${lastToken.key}:${lastToken.symbol}${lastToken.value}`;
input.value = input.value.slice(0, -1 * lastTokenString.length);
} }
if (left !== -1) {
input.value = `${inputValue.substr(0, left)}${word}${inputValue.substr(right + selectionStart)}`;
} else {
input.value += word; input.value += word;
} }
}
updateCurrentDropdownOffset() { updateCurrentDropdownOffset() {
this.updateDropdownOffset(this.currentDropdown); this.updateDropdownOffset(this.currentDropdown);
...@@ -98,7 +92,8 @@ ...@@ -98,7 +92,8 @@
const filterIconPadding = 27; const filterIconPadding = 27;
const offset = gl.text const offset = gl.text
.getTextWidth(gl.DropdownUtils.getSearchInput(this.filteredSearchInput).trim(), this.font) + filterIconPadding; .getTextWidth(gl.DropdownUtils.getSearchInput(this.filteredSearchInput).trim(), this.font)
+ filterIconPadding;
this.mapping[key].reference.setOffset(offset); this.mapping[key].reference.setOffset(offset);
} }
......
...@@ -193,7 +193,8 @@ ...@@ -193,7 +193,8 @@
} }
showOnClick() { showOnClick() {
const currentDropdownRef = this.dropdownManager.mapping[this.dropdownManager.currentDropdown].reference; const dropdown = this.dropdownManager.mapping[this.dropdownManager.currentDropdown];
const currentDropdownRef = dropdown.reference;
this.setDropdownWrapper(); this.setDropdownWrapper();
currentDropdownRef.dispatchInputEvent(); currentDropdownRef.dispatchInputEvent();
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment