Browse Source

fix: autocomplete replace wrong value

Ahmad Kholid 3 years ago
parent
commit
143feff69f

+ 1 - 1
src/components/newtab/workflow/WorkflowEditBlock.vue

@@ -112,7 +112,7 @@ export default {
       ...autocompleteData.value.common,
       ...autocompleteData.value[props.data.itemId || props.data.blockId],
     }));
-    console.log(autocompleteList.value);
+
     provide('autocompleteData', autocompleteList);
 
     const dataKeywords = {

+ 1 - 3
src/components/newtab/workflow/edit/EditAutocomplete.vue

@@ -31,7 +31,7 @@ function onSearch(value) {
   const path = (value ?? '').replace('@', '.');
   const pathArr = path.split('.');
 
-  if (pathArr.length < 1) {
+  if (pathArr.length <= 1) {
     state.path = '';
     state.pathLen = 0;
 
@@ -56,8 +56,6 @@ const autocompleteList = computed(() => {
 
   const list = typeof data === 'string' ? [] : Object.keys(data || {});
 
-  console.log(cache);
-
   cache.set(state.path, list);
 
   return list;

+ 9 - 4
src/components/ui/UiAutocomplete.vue

@@ -192,22 +192,27 @@ function selectItem(itemIndex) {
   selectedItem = getItem(selectedItem);
 
   let caretPosition;
-  let charLastIndex = 0;
   const isTriggerChar = state.charIndex >= 0 && state.searchText;
 
   if (isTriggerChar) {
     const val = input.value;
     const index = state.charIndex;
     const charLength = props.triggerChar[0].length;
+    const lastSearchIndex = state.searchText.length + index + charLength;
+
+    let charLastIndex = 0;
 
     if (props.replaceAfter) {
       const lastChars = Array.isArray(props.replaceAfter)
         ? props.replaceAfter
         : [props.replaceAfter];
       lastChars.forEach((char) => {
-        const lastIndex = val.lastIndexOf(char);
+        const searchText = val.slice(0, lastSearchIndex);
+        const lastIndex = searchText.lastIndexOf(char);
 
-        if (lastIndex > charLastIndex) charLastIndex = lastIndex - 1;
+        if (lastIndex > charLastIndex && lastIndex > index) {
+          charLastIndex = lastIndex - 1;
+        }
       });
     }
 
@@ -215,7 +220,7 @@ function selectItem(itemIndex) {
     selectedItem =
       val.slice(0, index + charLength + charLastIndex) +
       selectedItem +
-      val.slice(state.searchText.length + index + charLength, val.length);
+      val.slice(lastSearchIndex, val.length);
   }
 
   updateValue(selectedItem);