修改优化

This commit is contained in:
JenniferW 2025-08-15 17:36:33 +08:00
parent be2c60ead0
commit f6f2db9fe7
1 changed files with 33 additions and 30 deletions

View File

@ -410,19 +410,26 @@ const findCursorConditionIndex = () => {
const cursorPos = inputEl.selectionStart; const cursorPos = inputEl.selectionStart;
const inputValue = currentInput.value; const inputValue = currentInput.value;
const parts = inputValue.split(/[;]/);
let currentLength = 0; //
for (let i = 0; i < parts.length; i++) { const semicolons = [];
// const semicolonRegex = /[;]/g;
const partLength = parts[i].length + 1; let match;
if (cursorPos <= currentLength + partLength) {
return i; //
} while ((match = semicolonRegex.exec(inputValue)) !== null) {
currentLength += partLength; semicolons.push(match.index);
} }
return parts.length - 1; //
for (let i = 0; i < semicolons.length; i++) {
if (cursorPos <= semicolons[i]) {
return i;
}
}
//
return semicolons.length;
}; };
// //
@ -765,24 +772,21 @@ const getOperator = (conditionValue) => {
return operators.find((op) => conditionValue.startsWith(op)); return operators.find((op) => conditionValue.startsWith(op));
}; };
// - 13 //
// selectSuggestion
const selectSuggestion = (item) => { const selectSuggestion = (item) => {
// 使
const targetIndex = findCursorConditionIndex();
if (targetIndex === -1) return;
const parts = currentInput.value.split(/[;]/); const parts = currentInput.value.split(/[;]/);
//
let targetIndex = parts.length - 1;
// 使
if (parts.length > 0 && parts[targetIndex].trim() === "") {
targetIndex = Math.max(0, parts.length - 2);
}
let targetPart = parts[targetIndex] || ""; let targetPart = parts[targetIndex] || "";
if (item.type === "field") { if (item.type === "field") {
// : // :
parts[targetIndex] = `${item.label}:`; parts[targetIndex] = `${item.label}:`;
} else { } else {
// //
const colonIndex = targetPart.indexOf(":"); const colonIndex = targetPart.indexOf(":");
const operator = getOperator(targetPart.split(":").pop() || ""); const operator = getOperator(targetPart.split(":").pop() || "");
@ -799,15 +803,15 @@ const selectSuggestion = (item) => {
// //
parts[targetIndex] = `${fieldPart}${item.label}`; parts[targetIndex] = `${fieldPart}${item.label}`;
} }
// 1
parts[targetIndex] += ";";
} else { } else {
// //
parts[targetIndex] = item.label; parts[targetIndex] = item.label;
}
// 1 //
parts[targetIndex] += ";"; const lastChar = parts[targetIndex].slice(-1);
if (![",", ";", ""].includes(lastChar)) {
parts[targetIndex] += ";"; // 使
} }
} }
@ -819,21 +823,20 @@ const selectSuggestion = (item) => {
handleInput(currentInput.value); handleInput(currentInput.value);
}, 0); }, 0);
// //
searchInput.value.focus(); searchInput.value.focus();
const inputEl = searchInput.value.$el.querySelector("input"); const inputEl = searchInput.value.$el.querySelector("input");
if (inputEl) { if (inputEl) {
// //
let cursorPos = 0; let cursorPos = 0;
for (let i = 0; i < targetIndex; i++) { for (let i = 0; i < targetIndex; i++) {
cursorPos += parts[i].length + 1; // +1 cursorPos += parts[i].length + 1; // +1
} }
cursorPos += parts[targetIndex].length; cursorPos += parts[targetIndex].length; //
inputEl.setSelectionRange(cursorPos, cursorPos); inputEl.setSelectionRange(cursorPos, cursorPos);
} }
}; };
// //
const navigateSuggestions = (direction) => { const navigateSuggestions = (direction) => {
if (!showSuggestions.value || suggestions.value.length === 0) return; if (!showSuggestions.value || suggestions.value.length === 0) return;