refactor(editor): 待插入引用改名 PendingReference,去掉泛型默认值

宿主导出的 DraftReference 是「草稿里已有的气泡」(带 offset/length),
插件这个类型是「准备插入的引用」(只有 label/text)。两者同名不同义,
按名字 grep 会拿到完全不同的形状。改名为 PendingReference 区分。

selection-popup 的 T = string 默认值生产环境已无人使用(唯一调用方
显式写了类型),只为迁就测试里不写类型的旧写法。去掉默认值后类型仍能
从实参推断,测试无需改动。
This commit is contained in:
unsc9 committed 2026-10-24 21:53:24 +08:00
1 parent 9598278094
commit 4c2d34fce4
4 files changed
+17 -13

No files matched your search

+2 -2
View File
@@ -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<DraftReference>({
const selectionPopup = useSelectionPopup<PendingReference>({
onCommit: (insert) => {
insertReferenceOrText(ctx, scope.sessionId, insert)
},
+8 -4
View File
@@ -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<SidebarSessionInput['state']['getSnap
* whether the edit applied.
*
* The chip's `clipboardText` is the compact `label`, not the full payload —
* see {@link DraftReference.clipboardText}.
* see {@link PendingReference.clipboardText}.
*
* Returns false when the host refused the insert (stale revision, a frozen
* submission phase, or no chip channel at all) so the caller can fall back.
@@ -154,7 +158,7 @@ function appendOffset(snapshot: ReturnType<SidebarSessionInput['state']['getSnap
export function insertDraftReference(
ctx: Context,
sessionId: string,
reference: DraftReference,
reference: PendingReference,
): boolean {
let before: number | undefined
try {
@@ -298,7 +302,7 @@ export function appendToDraft(ctx: Context, sessionId: string, text: string): bo
export function insertReferenceOrText(
ctx: Context,
sessionId: string,
reference: DraftReference,
reference: PendingReference,
): boolean {
return insertDraftReference(ctx, sessionId, reference) || appendToDraft(ctx, sessionId, reference.text)
}
+3 -3
View File
@@ -18,10 +18,10 @@
* source and only reports lines on an unambiguous hit (see
* {@link linesOfSelection}).
*
* The two halves travel separately (see {@link DraftReference}).
* The two halves travel separately (see {@link PendingReference}).
*/
import { relativeTo } from './paths.ts'
import type { DraftReference } from './conversation-draft.ts'
import type { PendingReference } from './conversation-draft.ts'
/** Max inserted selection length (UTF-16 code units, i.e. JS `.length`). */
export const SELECTION_LIMIT = 500
@@ -53,7 +53,7 @@ export function buildSelectionInsert(
cwd: string | undefined,
lines: SelectionLines | undefined,
selected: string,
): DraftReference {
): PendingReference {
const header = headerOf(path, cwd, lines)
if (selected.length > SELECTION_LIMIT) return { label: header, text: header }
return { label: header, text: `\`\`\`${header}\n${selected}\n\`\`\`` }
+4 -4
View File
@@ -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<T = string> {
export interface SelectionPopup<T> {
insert: T
left: number
top: number
}
export interface SelectionPopupOptions<T = string> {
export interface SelectionPopupOptions<T> {
/** Commit the payload into the composer draft (button click). */
onCommit(insert: T): void
/**
@@ -50,7 +50,7 @@ export interface SelectionPopupOptions<T = string> {
getSurface(): HTMLElement | null
}
export interface SelectionPopupControls<T = string> {
export interface SelectionPopupControls<T> {
/** The current popup (null = hidden). */
popup: SelectionPopup<T> | null
/** Attach to the portaled button element. */
@@ -63,7 +63,7 @@ export interface SelectionPopupControls<T = string> {
commit(): void
}
export function useSelectionPopup<T = string>(options: SelectionPopupOptions<T>): SelectionPopupControls<T> {
export function useSelectionPopup<T>(options: SelectionPopupOptions<T>): SelectionPopupControls<T> {
// 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)