diff --git a/apps/web/apps/web-antd/src/views/agent/chat/catalog-cite-rich-text.vue b/apps/web/apps/web-antd/src/views/agent/chat/catalog-cite-rich-text.vue index 85043ba7..0a23df54 100644 --- a/apps/web/apps/web-antd/src/views/agent/chat/catalog-cite-rich-text.vue +++ b/apps/web/apps/web-antd/src/views/agent/chat/catalog-cite-rich-text.vue @@ -47,7 +47,8 @@ const hasCite = computed(() => segments.value.some((s) => s.type === 'cite')); gap: 4px; margin: 0 2px; padding: 0 6px; - vertical-align: baseline; + overflow: hidden; + vertical-align: middle; font-size: 12px; line-height: 20px; color: hsl(var(--primary)); @@ -62,6 +63,7 @@ const hasCite = computed(() => segments.value.some((s) => s.type === 'cite')); } .cite-msg-chip__title { + min-width: 0; overflow: hidden; text-overflow: ellipsis; } diff --git a/apps/web/apps/web-antd/src/views/agent/chat/catalog-cite-sender.vue b/apps/web/apps/web-antd/src/views/agent/chat/catalog-cite-sender.vue index 20aded27..d76f8678 100644 --- a/apps/web/apps/web-antd/src/views/agent/chat/catalog-cite-sender.vue +++ b/apps/web/apps/web-antd/src/views/agent/chat/catalog-cite-sender.vue @@ -196,20 +196,56 @@ function createChipEl(cite: CatalogCite): HTMLSpanElement { chip.className = 'cite-chip'; chip.contentEditable = 'false'; chip.dataset.citeKey = cite.key; + // 关键样式:动态节点吃不到 scoped 子选择器,避免长标题溢出叠字 + Object.assign(chip.style, { + display: 'inline-flex', + maxWidth: '12rem', + overflow: 'hidden', + alignItems: 'center', + gap: '4px', + margin: '0 2px', + padding: '0 6px', + verticalAlign: 'middle', + fontSize: '12px', + lineHeight: '20px', + whiteSpace: 'nowrap', + borderRadius: '6px', + userSelect: 'none', + }); const kindEl = document.createElement('span'); kindEl.className = 'cite-chip__kind'; kindEl.textContent = catalogCiteLabel(cite.kind); + Object.assign(kindEl.style, { + flexShrink: '0', + opacity: '0.7', + }); const titleEl = document.createElement('span'); titleEl.className = 'cite-chip__title'; - titleEl.textContent = cite.title; + const raw = cite.title.trim() || String(cite.id); + titleEl.textContent = raw.length > 24 ? `${raw.slice(0, 23)}…` : raw; + titleEl.title = cite.title; + Object.assign(titleEl.style, { + minWidth: '0', + overflow: 'hidden', + textOverflow: 'ellipsis', + }); const removeBtn = document.createElement('button'); removeBtn.type = 'button'; removeBtn.className = 'cite-chip__remove'; removeBtn.setAttribute('aria-label', '移除引用'); removeBtn.textContent = '×'; + Object.assign(removeBtn.style, { + flexShrink: '0', + padding: '0', + cursor: 'pointer', + border: 'none', + opacity: '0.6', + background: 'transparent', + color: 'inherit', + }); removeBtn.addEventListener('mousedown', (event) => { event.preventDefault(); event.stopPropagation(); @@ -224,6 +260,21 @@ function createChipEl(cite: CatalogCite): HTMLSpanElement { return chip; } +/** 芯片后插入零宽字符,保证光标落在标签外侧。 */ +function ensureCaretAnchorAfter(chip: HTMLElement) { + const next = chip.nextSibling; + if (next?.nodeType === Node.TEXT_NODE) { + const text = next.textContent ?? ''; + if (!text.startsWith('\u200B')) { + next.textContent = `\u200B${text}`; + } + return next as Text; + } + const anchor = document.createTextNode('\u200B'); + chip.after(anchor); + return anchor; +} + /** 按草稿 token 重建编辑器 DOM(程序化变更时调用)。 */ function renderEditor(text: string, caret?: number) { const editor = editorRef.value; @@ -231,10 +282,16 @@ function renderEditor(text: string, caret?: number) { editor.innerHTML = ''; for (const seg of parseDraftSegments(text)) { if (seg.type === 'text') { - if (seg.value) editor.appendChild(document.createTextNode(seg.value)); + // 序列化会丢掉芯片后的零宽锚点,重建时不再写入 + const value = seg.value.replace(/\u200B/g, ''); + if (value) editor.appendChild(document.createTextNode(value)); } else { const cite = cites.value.find((c) => c.key === seg.key); - if (cite) editor.appendChild(createChipEl(cite)); + if (cite) { + const chip = createChipEl(cite); + editor.appendChild(chip); + ensureCaretAnchorAfter(chip); + } } } if (caret != null) { @@ -249,7 +306,8 @@ function serializeEditor(): string { let out = ''; const walk = (node: Node) => { if (node.nodeType === Node.TEXT_NODE) { - out += node.textContent ?? ''; + // 芯片后的零宽锚点不进草稿 + out += (node.textContent ?? '').replace(/\u200B/g, ''); return; } if (!(node instanceof HTMLElement)) return; @@ -281,6 +339,25 @@ function tokenLengthForChip(key: string): number { return cite ? formatInlineCite(cite).length : 0; } +/** 草稿偏移只计可见字符,忽略芯片后的零宽锚点。 */ +function draftLenInText(text: string) { + return text.replace(/\u200B/g, '').length; +} + +function draftOffsetBeforeDom(text: string, domOffset: number) { + return text.slice(0, Math.max(0, domOffset)).replace(/\u200B/g, '').length; +} + +function domOffsetForDraft(text: string, draftOffset: number) { + let seen = 0; + for (let i = 0; i < text.length; i += 1) { + if (text[i] === '\u200B') continue; + if (seen === draftOffset) return i; + seen += 1; + } + return text.length; +} + /** 将浏览器选区映射为草稿 token 偏移。 */ function getDraftOffsetFromSelection(): number { const editor = editorRef.value; @@ -295,12 +372,12 @@ function getDraftOffsetFromSelection(): number { const walk = (node: Node) => { if (done) return; if (node === range.startContainer && node.nodeType === Node.TEXT_NODE) { - offset += range.startOffset; + offset += draftOffsetBeforeDom(node.textContent ?? '', range.startOffset); done = true; return; } if (node.nodeType === Node.TEXT_NODE) { - offset += node.textContent?.length ?? 0; + offset += draftLenInText(node.textContent ?? ''); return; } if (!(node instanceof HTMLElement)) return; @@ -346,12 +423,23 @@ function setCaretAtDraftOffset(target: number) { }; let found = false; + const placeAfterChip = (chip: HTMLElement) => { + const anchor = ensureCaretAnchorAfter(chip); + // 放到零宽锚点之后,避免后续输入插进锚点前 + state.caret = { node: anchor, offset: 1 }; + found = true; + }; + const walk = (node: Node) => { if (found) return; if (node.nodeType === Node.TEXT_NODE) { - const len = node.textContent?.length ?? 0; + const text = node.textContent ?? ''; + const len = draftLenInText(text); if (remaining <= len) { - state.caret = { node: node as Text, offset: remaining }; + state.caret = { + node: node as Text, + offset: domOffsetForDraft(text, remaining), + }; found = true; return; } @@ -362,15 +450,7 @@ function setCaretAtDraftOffset(target: number) { if (node.dataset.citeKey) { const len = tokenLengthForChip(node.dataset.citeKey); if (remaining <= len) { - const after = node.nextSibling; - if (after?.nodeType === Node.TEXT_NODE) { - state.caret = { node: after as Text, offset: 0 }; - } else { - const spacer = document.createTextNode(''); - node.after(spacer); - state.caret = { node: spacer, offset: 0 }; - } - found = true; + placeAfterChip(node); return; } remaining -= len; @@ -941,45 +1021,13 @@ onBeforeUnmount(() => { content: attr(data-placeholder); } -.cite-editor :deep(.cite-chip), -.cite-editor .cite-chip { - display: inline-flex; - max-width: 12rem; - align-items: center; - gap: 4px; - margin: 0 2px; - padding: 0 6px; - vertical-align: baseline; - font-size: 12px; - line-height: 20px; +/* 颜色走主题;布局已在 createChipEl 内联,防止 scoped 失效叠字 */ +.cite-editor :deep(.cite-chip) { color: hsl(var(--primary)); - white-space: nowrap; background: hsl(var(--primary) / 0.1); - border-radius: 6px; - user-select: none; } -.cite-editor .cite-chip__kind { - flex-shrink: 0; - opacity: 0.7; -} - -.cite-editor .cite-chip__title { - overflow: hidden; - text-overflow: ellipsis; -} - -.cite-editor .cite-chip__remove { - flex-shrink: 0; - padding: 0; - cursor: pointer; - border: none; - opacity: 0.6; - background: transparent; - color: inherit; -} - -.cite-editor .cite-chip__remove:hover { +.cite-editor :deep(.cite-chip__remove:hover) { opacity: 1; }