From 4c2d34fce4dc2715edf37dca1783fd15b13c61dc Mon Sep 17 00:00:00 2001 From: unsc9 <438590997@qq.com> Date: Sat, 24 Oct 2026 21:53:24 +0800 Subject: [PATCH] =?UTF-8?q?refactor(editor):=20=E5=BE=85=E6=8F=92=E5=85=A5?= =?UTF-8?q?=E5=BC=95=E7=94=A8=E6=94=B9=E5=90=8D=20PendingReference?= =?UTF-8?q?=EF=BC=8C=E5=8E=BB=E6=8E=89=E6=B3=9B=E5=9E=8B=E9=BB=98=E8=AE=A4?= =?UTF-8?q?=E5=80=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 宿主导出的 DraftReference 是「草稿里已有的气泡」(带 offset/length), 插件这个类型是「准备插入的引用」(只有 label/text)。两者同名不同义, 按名字 grep 会拿到完全不同的形状。改名为 PendingReference 区分。 selection-popup 的 T = string 默认值生产环境已无人使用(唯一调用方 显式写了类型),只为迁就测试里不写类型的旧写法。去掉默认值后类型仍能 从实参推断,测试无需改动。 --- src/client/TextEditor.tsx | 4 ++-- src/client/conversation-draft.ts | 12 ++++++++---- src/client/selection-payload.ts | 6 +++--- src/client/selection-popup.ts | 8 ++++---- 4 files changed, 17 insertions(+), 13 deletions(-) diff --git a/src/client/TextEditor.tsx b/src/client/TextEditor.tsx index 95e7240d..8aeed642 100644 --- a/src/client/TextEditor.tsx +++ b/src/client/TextEditor.tsx @@ -38,7 +38,7 @@ import { cmSurfaceTheme, CmThemeCompartment } from './cm-themes.ts' import { cmSearchExtensions, CmSearchPhrases } from './cm-search.ts' import { isDarkScheme, subscribeColorScheme } from './theme.ts' import { SandboxStatusBar } from './SandboxStatusBar.tsx' -import { insertReferenceOrText, type DraftReference } from './conversation-draft.ts' +import { insertReferenceOrText, type PendingReference } from './conversation-draft.ts' import { useSelectionPopup } from './selection-popup.ts' import { buildSelectionInsert, linesOfSelection } from './selection-payload.ts' import { analyzeMarkdownHtml } from './markdown-html.ts' @@ -220,7 +220,7 @@ export function TextEditor(props: FileViewerProps) { * listeners (outside mousedown, Escape, hidden tab/window, surface * leaving the viewport) — see selection-popup.ts. */ - const selectionPopup = useSelectionPopup({ + const selectionPopup = useSelectionPopup({ onCommit: (insert) => { insertReferenceOrText(ctx, scope.sessionId, insert) }, diff --git a/src/client/conversation-draft.ts b/src/client/conversation-draft.ts index 3c01a2f9..0bfd50d2 100644 --- a/src/client/conversation-draft.ts +++ b/src/client/conversation-draft.ts @@ -33,8 +33,12 @@ import { baseName, workspaceRelativePath } from './paths.ts' * composer shows, and the `text` the model receives on send. The two are * deliberately different — a selection reads as `docs/a.md:12-15` on screen * while the model still gets the whole fenced block. + * + * Named `Pending*` to keep clear of the host's own `DraftReference`, which is + * the opposite thing: a chip ALREADY in the draft, carrying its offset and + * length. */ -export interface DraftReference { +export interface PendingReference { label: string text: string /** @@ -146,7 +150,7 @@ function appendOffset(snapshot: ReturnType SELECTION_LIMIT) return { label: header, text: header } return { label: header, text: `\`\`\`${header}\n${selected}\n\`\`\`` } diff --git a/src/client/selection-popup.ts b/src/client/selection-popup.ts index f573886c..18f9a97b 100644 --- a/src/client/selection-popup.ts +++ b/src/client/selection-popup.ts @@ -33,13 +33,13 @@ import { useEffect, useRef, useState, type RefObject } from 'react' * The floating "add to conversation" action: payload + viewport anchor. * The payload type belongs to the caller; the hook only carries it through. */ -export interface SelectionPopup { +export interface SelectionPopup { insert: T left: number top: number } -export interface SelectionPopupOptions { +export interface SelectionPopupOptions { /** Commit the payload into the composer draft (button click). */ onCommit(insert: T): void /** @@ -50,7 +50,7 @@ export interface SelectionPopupOptions { getSurface(): HTMLElement | null } -export interface SelectionPopupControls { +export interface SelectionPopupControls { /** The current popup (null = hidden). */ popup: SelectionPopup | null /** Attach to the portaled button element. */ @@ -63,7 +63,7 @@ export interface SelectionPopupControls { commit(): void } -export function useSelectionPopup(options: SelectionPopupOptions): SelectionPopupControls { +export function useSelectionPopup(options: SelectionPopupOptions): SelectionPopupControls { // Latest-callback refs: the dismissal listeners live for the mount's // lifetime, so they must not capture stale closures across renders. const onCommitRef = useRef(options.onCommit)