From f6f2db9fe7786d566701aaccf56a956091e5269a Mon Sep 17 00:00:00 2001 From: JenniferW <1627055433@qq.com> Date: Fri, 15 Aug 2025 17:36:33 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/views/order/intention/Step2.vue | 63 +++++++++++++++-------------- 1 file changed, 33 insertions(+), 30 deletions(-) diff --git a/src/views/order/intention/Step2.vue b/src/views/order/intention/Step2.vue index 9046793..839e4a7 100644 --- a/src/views/order/intention/Step2.vue +++ b/src/views/order/intention/Step2.vue @@ -410,19 +410,26 @@ const findCursorConditionIndex = () => { const cursorPos = inputEl.selectionStart; const inputValue = currentInput.value; - const parts = inputValue.split(/[;;]/); - let currentLength = 0; - for (let i = 0; i < parts.length; i++) { - // 加上分号的长度 - const partLength = parts[i].length + 1; - if (cursorPos <= currentLength + partLength) { - return i; - } - currentLength += partLength; + // 修复:处理中英文分号的正则表达式 + const semicolons = []; + const semicolonRegex = /[;;]/g; + let match; + + // 收集所有分号的位置 + while ((match = semicolonRegex.exec(inputValue)) !== null) { + 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)); }; -// 选择建议项 - 修复问题1和问题3 +// 选择建议项 +// 修改selectSuggestion函数,在选择值后自动添加分号 const selectSuggestion = (item) => { + // 使用光标位置确定当前正在编辑的条件索引 + const targetIndex = findCursorConditionIndex(); + if (targetIndex === -1) return; + 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] || ""; if (item.type === "field") { - // 字段建议:直接替换为「字段:」格式 + // 字段建议:直接替换为「字段:」格式(不自动加分号,等待输入值) parts[targetIndex] = `${item.label}:`; } else { - // 值建议处理 + // 值建议处理:选择值后自动添加分号 const colonIndex = targetPart.indexOf(":"); const operator = getOperator(targetPart.split(":").pop() || ""); @@ -799,15 +803,15 @@ const selectSuggestion = (item) => { // 普通值替换 parts[targetIndex] = `${fieldPart}${item.label}`; } - - // 问题1:值选择后自动添加分号 - parts[targetIndex] += ";"; } else { // 无字段时直接处理值 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); }, 0); - // 聚焦输入框并将光标定位到末尾 + // 聚焦输入框并将光标定位到当前条件末尾(分号后) searchInput.value.focus(); const inputEl = searchInput.value.$el.querySelector("input"); if (inputEl) { - // 计算光标应该在的位置 + // 计算光标应该在的位置(分号后面) let cursorPos = 0; for (let i = 0; i < targetIndex; i++) { cursorPos += parts[i].length + 1; // +1 是分号的长度 } - cursorPos += parts[targetIndex].length; + cursorPos += parts[targetIndex].length; // 光标定位到分号后 inputEl.setSelectionRange(cursorPos, cursorPos); } }; - // 导航建议项(上下箭头) const navigateSuggestions = (direction) => { if (!showSuggestions.value || suggestions.value.length === 0) return;