修改优化

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) {
// 收集所有分号的位置
while ((match = semicolonRegex.exec(inputValue)) !== null) {
semicolons.push(match.index);
}
// 确定光标所在的条件索引
for (let i = 0; i < semicolons.length; i++) {
if (cursorPos <= semicolons[i]) {
return i; return i;
} }
currentLength += partLength;
} }
return parts.length - 1; // 如果光标在最后一个分号之后,返回最后一个条件索引
return semicolons.length;
}; };
// 处理输入框容器点击 // 处理输入框容器点击
@ -765,24 +772,21 @@ const getOperator = (conditionValue) => {
return operators.find((op) => conditionValue.startsWith(op)); return operators.find((op) => conditionValue.startsWith(op));
}; };
// 选择建议项 - 修复问题1和问题3 // 选择建议项
// 修改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;