| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538 |
- /* eslint-disable max-lines */
- /**
- * LexicalChatInput — 基于 Lexical 的聊天输入框
- *
- * 基于 Lexical 的聊天输入框,支持 slash command 和 mention tag。
- * - Enter 发送,Shift+Enter 换行
- * - 支持 IME 输入
- * - 自动高度(默认显示 3 行,超出后滚动)
- * - disabled 状态
- * - 通过 SlashCommandPlugin / MentionPlugin 支持 `/` / `@` 的触发面板
- *
- * 独立输入展示壳,不承载会话编排逻辑,仅做三处适配:
- * 1. useChatViewActiveTaskProvider 来自 @/v4/activeTaskProvider.js(配置面读取);
- * 2. ChatComposerPasteEvent 收口为本文件导出的结构类型;
- * 3. mention 面板用 enableMentionPanel 控制;slash command 始终读取 CLI workspace catalog。
- */
- import { $getPromptMarkdown } from "@/mentions/promptSerialization.js";
- import { PromptClipboardPlugin } from "@/mentions/PromptClipboardPlugin.js";
- import {
- resolveComposerKeyAction,
- shouldBareEnterFallThroughToNewline,
- } from "@/shortcuts/composerShortcuts.js";
- import { useEffectiveShortcutBindings } from "@/shortcuts/useShortcutBindings.js";
- import { useCallback, useEffect, useMemo, useRef } from "react";
- import { LexicalComposer } from "@lexical/react/LexicalComposer";
- import { PlainTextPlugin } from "@lexical/react/LexicalPlainTextPlugin";
- import { ContentEditable } from "@lexical/react/LexicalContentEditable";
- import { HistoryPlugin } from "@lexical/react/LexicalHistoryPlugin";
- import { useLexicalComposerContext } from "@lexical/react/LexicalComposerContext";
- import {
- $createParagraphNode,
- $createTextNode,
- $getSelection,
- $getNodeByKey,
- $setSelection,
- $getRoot,
- $isParagraphNode,
- $isRangeSelection,
- $isTextNode,
- KEY_ARROW_DOWN_COMMAND,
- KEY_ARROW_UP_COMMAND,
- KEY_BACKSPACE_COMMAND,
- COMMAND_PRIORITY_HIGH,
- KEY_ENTER_COMMAND,
- type EditorState,
- type LexicalEditor,
- } from "lexical";
- import { SlashCommandPlugin } from "./SlashCommandPlugin.js";
- import type { AppSlashCommand } from "./slashCommandHelpers.js";
- import { MentionPlugin } from "./mentions/MentionPlugin.js";
- import { useChatViewActiveTaskProvider } from "@/v4/activeTaskProvider.js";
- import {
- $createPromptMentionNode,
- $isPromptMentionNode,
- PromptMentionNode,
- } from "./mentions/nodes/PromptMentionNode.js";
- import { logger } from "./logger.js";
- import { recordInputLag } from "./lib/uiPerfArmsTelemetry.js";
- import { navigatePromptHistory } from "./lib/promptHistory.js";
- import { resolveMobileInputTextSizeClassName } from "@/lib/mobileTextInput.js";
- import type { MentionItemData } from "@/mentions/mentionTypes.js";
- import type { ComposerMentionPrefill } from "@/store/zcodeSessionStoreTypes.js";
- /** 旧 useChatComposer 已删;粘贴事件收口为最小结构类型(ClipboardEvent 结构兼容)。 */
- export interface ChatComposerPasteEvent {
- clipboardData: DataTransfer | null;
- preventDefault: () => void;
- stopPropagation?: () => void;
- }
- export interface LexicalChatInputHandle {
- clear: () => void;
- focus: () => void;
- getEditorState: () => EditorState;
- getMarkdown: () => string;
- getText: () => string;
- appendText: (text: string) => void;
- appendFileMention: (
- label: string,
- value: string,
- markdown: string,
- data?: MentionItemData,
- trailingText?: string,
- ) => void;
- prependMentionIfMissing: (mention: ComposerMentionPrefill) => boolean;
- setMention: (mention: ComposerMentionPrefill, trailingText?: string) => void;
- insertMention: (mention: ComposerMentionPrefill, selectionState?: EditorState) => void;
- setText: (text: string) => void;
- setTextWithPluginMentions: (text: string) => void;
- setEditorStateJson: (editorStateJson: string) => void;
- setSkillMention: (skillName: string, markdown?: string, trailingText?: string) => void;
- setSlashCommandMention: (commandName: string, markdown?: string, trailingText?: string) => void;
- }
- interface LexicalEnterSubmitOptions {
- allowSubmitWhenEmpty?: boolean;
- ctrlKey?: boolean;
- enterSubmits: boolean;
- isComposing?: boolean;
- metaKey?: boolean;
- shiftKey?: boolean;
- text: string;
- }
- interface LexicalModifiedEnterSubmitOptions {
- allowSubmitWhenEmpty?: boolean;
- ctrlKey?: boolean;
- isComposing?: boolean;
- metaKey?: boolean;
- modifiedEnterSubmits: boolean;
- shiftKey?: boolean;
- text: string;
- }
- export type LexicalSubmitResult = boolean | void;
- interface LeadingChineseSlashAliasInputOptions {
- data: string | null;
- inputType: string;
- isAtEditorStart: boolean;
- isComposing?: boolean;
- }
- const CHINESE_SLASH_ALIAS = "、";
- const STANDARD_SLASH_TRIGGER = "/";
- import { HISTORY_NAVIGATION_UPDATE_TAG, PROGRAMMATIC_UPDATE_TAG } from "./lib/editorUpdateTags.js";
- export function shouldSubmitLexicalEnter({
- allowSubmitWhenEmpty = false,
- ctrlKey = false,
- enterSubmits,
- isComposing = false,
- metaKey = false,
- shiftKey = false,
- text,
- }: LexicalEnterSubmitOptions): boolean {
- if (!enterSubmits) {
- return false;
- }
- if (shiftKey || ctrlKey || metaKey || isComposing) {
- return false;
- }
- return Boolean(text.trim() || allowSubmitWhenEmpty);
- }
- export function shouldSubmitLexicalModifiedEnter({
- allowSubmitWhenEmpty = false,
- ctrlKey = false,
- isComposing = false,
- metaKey = false,
- modifiedEnterSubmits,
- shiftKey = false,
- text,
- }: LexicalModifiedEnterSubmitOptions): boolean {
- return (
- modifiedEnterSubmits &&
- (ctrlKey || metaKey) &&
- !shiftKey &&
- !isComposing &&
- Boolean(text.trim() || allowSubmitWhenEmpty)
- );
- }
- export function shouldResetLexicalEditorAfterSubmit(result: LexicalSubmitResult): boolean {
- return result !== false;
- }
- export function shouldNormalizeLeadingChineseSlashAliasInput({
- data,
- inputType,
- isAtEditorStart,
- isComposing = false,
- }: LeadingChineseSlashAliasInputOptions): boolean {
- return (
- data === CHINESE_SLASH_ALIAS && inputType === "insertText" && isAtEditorStart && !isComposing
- );
- }
- /** 提取编辑器内容;mention 节点会在这里输出 markdown */
- function getEditorMarkdown(editorState: EditorState): string {
- let text = "";
- editorState.read(() => {
- text = $getPromptMarkdown();
- });
- return text;
- }
- function replaceEditorText(editor: LexicalEditor, text: string) {
- editor.update(
- () => {
- const root = $getRoot();
- root.clear();
- // Lexical getTextContent() 用 \n\n 分隔段落,对称处理防止换行翻倍
- for (const line of text.split("\n\n")) {
- const paragraph = $createParagraphNode();
- if (line) {
- paragraph.append($createTextNode(line));
- }
- root.append(paragraph);
- }
- root.getLastChild()?.selectEnd();
- },
- { tag: PROGRAMMATIC_UPDATE_TAG },
- );
- }
- const INLINE_PLUGIN_MENTION_PATTERN =
- /\[@((?:\\.|[^\]])+)\]\(plugin:\/\/([a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+)\)/g;
- function replaceEditorTextWithPluginMentions(editor: LexicalEditor, text: string) {
- editor.update(
- () => {
- const root = $getRoot();
- root.clear();
- for (const line of text.split("\n\n")) {
- const paragraph = $createParagraphNode();
- let cursor = 0;
- for (const match of line.matchAll(INLINE_PLUGIN_MENTION_PATTERN)) {
- const start = match.index ?? 0;
- if (start > cursor) paragraph.append($createTextNode(line.slice(cursor, start)));
- const markdown = match[0];
- const label = match[1]?.replaceAll("\\]", "]").replaceAll("\\[", "[") ?? "";
- const pluginId = match[2] ?? "";
- paragraph.append(
- $createPromptMentionNode({
- id: `plugin:${pluginId}`,
- category: "plugins",
- label,
- value: pluginId,
- markdown,
- data: { pluginId },
- }),
- );
- cursor = start + markdown.length;
- }
- if (cursor < line.length || paragraph.getChildrenSize() === 0) {
- paragraph.append($createTextNode(line.slice(cursor)));
- }
- root.append(paragraph);
- }
- root.getLastChild()?.selectEnd();
- },
- { tag: PROGRAMMATIC_UPDATE_TAG },
- );
- }
- export function replaceEditorWithMention(
- editor: LexicalEditor,
- mention: ComposerMentionPrefill,
- trailingText = " ",
- ) {
- editor.update(
- () => {
- const root = $getRoot();
- root.clear();
- const paragraph = $createParagraphNode();
- // 预填后保留空格供用户直接继续输入;选区正确性由 TextNode 文本/DOM 契约保证。
- const trailing = $createTextNode(trailingText || " ");
- paragraph.append($createPromptMentionNode(mention), trailing);
- root.append(paragraph);
- trailing.selectEnd();
- },
- { tag: PROGRAMMATIC_UPDATE_TAG },
- );
- }
- /**
- * 在草稿开头插入结构化 mention,同时保留已有 Lexical 节点和段落。
- *
- * 不能通过 getMarkdown → setMention 重建:setMention 是替换型预填,会把旧 mention
- * 序列化后作为普通 TextNode 放回编辑器,导致已有 Plugin / 文件 / Skill chip 降级。
- */
- export function prependEditorMentionIfMissing(
- editor: LexicalEditor,
- mention: ComposerMentionPrefill,
- ): boolean {
- let inserted = false;
- editor.update(
- () => {
- const root = $getRoot();
- const alreadyPresent = root
- .getAllTextNodes()
- .some(
- (node) =>
- $isPromptMentionNode(node) &&
- node.getMention().category === mention.category &&
- node.getMention().value === mention.value,
- );
- if (alreadyPresent) return;
- const mentionNode = $createPromptMentionNode(mention);
- const separator = $createTextNode(" ");
- const firstBlock = root.getFirstChild();
- if ($isParagraphNode(firstBlock)) {
- const firstInline = firstBlock.getFirstChild();
- if (firstInline) {
- firstInline.insertBefore(mentionNode);
- mentionNode.insertAfter(separator);
- } else {
- firstBlock.append(mentionNode, separator);
- }
- } else {
- const paragraph = $createParagraphNode().append(mentionNode, separator);
- if (firstBlock) firstBlock.insertBefore(paragraph);
- else root.append(paragraph);
- }
- separator.selectEnd();
- inserted = true;
- },
- { discrete: true, tag: PROGRAMMATIC_UPDATE_TAG },
- );
- return inserted;
- }
- function replaceEditorStateJson(editor: LexicalEditor, editorStateJson: string) {
- const editorState = editor.parseEditorState(editorStateJson);
- editor.setEditorState(editorState, { tag: PROGRAMMATIC_UPDATE_TAG });
- }
- function resetEditor(editor: LexicalEditor) {
- replaceEditorText(editor, "");
- }
- function replaceEditorWithSkillMention(
- editor: LexicalEditor,
- skillName: string,
- markdown = `$${skillName}`,
- trailingText = " ",
- ) {
- editor.update(
- () => {
- const root = $getRoot();
- root.clear();
- const paragraph = $createParagraphNode();
- paragraph.append(
- $createPromptMentionNode({
- id: `prefill-skill:${skillName}`,
- category: "skills",
- // 预填入口之前虽然会插入 skill node,但节点里保存的 markdown 仍固定成 `$slug`,
- // 导致像 New Skill 这种入口即使理论上有完整转译内容,进入输入框后也会被悄悄降级。
- // 这里显式透传原始 markdown,并直接显示 frontmatter name,避免 UI 再加工 skill 标题。
- label: skillName,
- value: skillName,
- markdown,
- }),
- $createTextNode(trailingText),
- );
- root.append(paragraph);
- root.getLastChild()?.selectEnd();
- },
- { tag: PROGRAMMATIC_UPDATE_TAG },
- );
- }
- function replaceEditorWithSlashCommandMention(
- editor: LexicalEditor,
- commandName: string,
- markdown = `/${commandName}`,
- trailingText = " ",
- ) {
- editor.update(
- () => {
- const root = $getRoot();
- root.clear();
- const paragraph = $createParagraphNode();
- paragraph.append(
- $createPromptMentionNode({
- id: `prefill-slash:${commandName}`,
- category: "commands",
- label: commandName,
- value: commandName,
- markdown,
- }),
- $createTextNode(trailingText),
- );
- root.append(paragraph);
- root.getLastChild()?.selectEnd();
- },
- { tag: PROGRAMMATIC_UPDATE_TAG },
- );
- }
- function appendEditorFileMention(
- editor: LexicalEditor,
- label: string,
- value: string,
- markdown: string,
- data?: MentionItemData,
- trailingText = " ",
- ) {
- editor.update(
- () => {
- const root = $getRoot();
- const lastChild = root.getLastChild();
- const paragraph = $isParagraphNode(lastChild) ? lastChild : $createParagraphNode();
- if (!$isParagraphNode(lastChild)) {
- root.append(paragraph);
- }
- const currentText = paragraph.getTextContent();
- if (currentText.length > 0 && !/\s$/.test(currentText)) {
- paragraph.append($createTextNode(" "));
- }
- paragraph.append(
- $createPromptMentionNode({
- id: `dropped-file:${value}`,
- category: "files",
- label,
- value,
- markdown,
- data,
- }),
- $createTextNode(trailingText),
- );
- paragraph.selectEnd();
- },
- { tag: PROGRAMMATIC_UPDATE_TAG },
- );
- }
- function appendEditorPlainText(editor: LexicalEditor, text: string) {
- editor.update(
- () => {
- const root = $getRoot();
- const lastChild = root.getLastChild();
- const paragraph = $isParagraphNode(lastChild) ? lastChild : $createParagraphNode();
- if (!$isParagraphNode(lastChild)) {
- root.append(paragraph);
- }
- const currentText = paragraph.getTextContent();
- if (currentText.length > 0 && !/\s$/.test(currentText)) {
- paragraph.append($createTextNode(" "));
- }
- paragraph.append($createTextNode(text));
- paragraph.selectEnd();
- },
- { tag: PROGRAMMATIC_UPDATE_TAG },
- );
- }
- function getPromptMentionIdAfterDomSelection(rootElement: HTMLElement): string | null {
- const selection = window.getSelection();
- if (!selection?.isCollapsed || !selection.anchorNode) {
- return null;
- }
- const anchorNode = selection.anchorNode;
- if (!rootElement.contains(anchorNode)) {
- return null;
- }
- if (anchorNode.nodeType === Node.ELEMENT_NODE) {
- const child = anchorNode.childNodes.item(selection.anchorOffset);
- return getPromptMentionIdFromBoundaryNode(child);
- }
- if (anchorNode.nodeType !== Node.TEXT_NODE) {
- return null;
- }
- if (selection.anchorOffset !== (anchorNode.textContent ?? "").length) {
- return null;
- }
- return getPromptMentionIdFromBoundaryNode(anchorNode.nextSibling);
- }
- function getPromptMentionIdFromBoundaryNode(node: Node | null): string | null {
- if (!(node instanceof HTMLElement) || !node.matches("[data-mention-id]")) {
- return null;
- }
- return node.dataset.mentionId ?? null;
- }
- function selectAfterPromptMentionById(mentionId: string): boolean {
- const mentionNode = $getRoot()
- .getAllTextNodes()
- .find(
- (node): node is PromptMentionNode =>
- $isPromptMentionNode(node) && node.getMention().id === mentionId,
- );
- if (!mentionNode) {
- return false;
- }
- const nextSibling = mentionNode.getNextSibling();
- if ($isTextNode(nextSibling)) {
- const text = nextSibling.getTextContent();
- const offset = /^\s/.test(text) ? Math.min(1, text.length) : 0;
- nextSibling.select(offset, offset);
- return true;
- }
- mentionNode.selectNext(0, 0);
- return true;
- }
- /**
- * 键盘行为插件:Enter 发送,Shift+Enter 换行
- *
- * 用 COMMAND_PRIORITY_HIGH 截获 Enter 按键,
- * 阻止 Lexical 默认的段落插入行为。
- */
- function KeyboardPlugin({
- onSubmit,
- onModifiedSubmit,
- disabled,
- submitDisabled,
- allowSubmitWhenEmpty,
- enterSubmits,
- }: {
- onSubmit: (text: string) => LexicalSubmitResult;
- onModifiedSubmit?: (text: string) => LexicalSubmitResult;
- disabled?: boolean;
- submitDisabled?: boolean;
- allowSubmitWhenEmpty?: boolean;
- enterSubmits: boolean;
- }) {
- const [editor] = useLexicalComposerContext();
- // 作用域改绑层:读生效表的 composer 命令切片;ref 透传避免按键监听重挂。
- const effectiveShortcutBindings = useEffectiveShortcutBindings();
- const composerEffectiveRef = useRef({
- composerSend: effectiveShortcutBindings.composerSend ?? [],
- composerInsertNewline: effectiveShortcutBindings.composerInsertNewline ?? [],
- });
- composerEffectiveRef.current = {
- composerSend: effectiveShortcutBindings.composerSend ?? [],
- composerInsertNewline: effectiveShortcutBindings.composerInsertNewline ?? [],
- };
- useEffect(() => {
- const unregisterEnter = editor.registerCommand(
- KEY_ENTER_COMMAND,
- (event: KeyboardEvent | null) => {
- if (!event) return false;
- if (disabled) {
- event.preventDefault();
- return true;
- }
- // IME 正在组合中(如中文输入法),不拦截
- if (event.isComposing) {
- return false;
- }
- const text = getEditorMarkdown(editor.getEditorState());
- // 作用域改绑层:用户键位表优先于内置默认。
- // 命中换行 → 放行 Lexical 插段落;命中发送 → 走与主链等价的门禁分支;
- // 未命中 → 落到下方主链(含反转投递 / 修饰组合换行 / 视口门禁)。
- const composerEffective = composerEffectiveRef.current;
- const scopedAction = resolveComposerKeyAction(event, composerEffective);
- if (scopedAction === "newline") {
- return false;
- }
- if (scopedAction === "send") {
- const modifiedScopedEnter = event.shiftKey || event.ctrlKey || event.metaKey;
- // 反转投递开启时带修饰组合让位主链(Ctrl+Enter = 反向 delivery,交付语义比键位更具体)
- if (!(modifiedScopedEnter && onModifiedSubmit)) {
- // 与主链裸 Enter 等价的门禁:无修饰组合受 submitDisabled / 手机视口 enterSubmits;
- // 带修饰组合不受视口门禁(与主链 onModifiedSubmit 路径一致)。
- if (submitDisabled || (!modifiedScopedEnter && !enterSubmits)) {
- return false;
- }
- event.preventDefault();
- if (
- shouldSubmitLexicalEnter({
- allowSubmitWhenEmpty,
- enterSubmits: modifiedScopedEnter ? true : enterSubmits,
- text,
- })
- ) {
- const submitResult = onSubmit(text);
- if (shouldResetLexicalEditorAfterSubmit(submitResult)) {
- resetEditor(editor);
- }
- }
- return true;
- }
- }
- // 主链前置检查:composerSend 已改绑走(生效绑定不含裸 Enter)时,
- // 裸 Enter 不再代表发送,放行 Lexical 换行("Ctrl+Enter 党"改绑后的预期行为)。
- if (
- !event.shiftKey &&
- !event.ctrlKey &&
- !event.metaKey &&
- shouldBareEnterFallThroughToNewline(composerEffective)
- ) {
- return false;
- }
- if (
- !submitDisabled &&
- onModifiedSubmit &&
- shouldSubmitLexicalModifiedEnter({
- allowSubmitWhenEmpty,
- ctrlKey: event.ctrlKey,
- metaKey: event.metaKey,
- modifiedEnterSubmits: true,
- shiftKey: event.shiftKey,
- text,
- })
- ) {
- event.preventDefault();
- const submitResult = onModifiedSubmit(text);
- if (shouldResetLexicalEditorAfterSubmit(submitResult)) {
- resetEditor(editor);
- }
- return true;
- }
- // Shift+Enter,以及未启用反转投递时的 Ctrl/Meta+Enter 继续换行。
- if (event.shiftKey || event.ctrlKey || event.metaKey) {
- return false;
- }
- // 当前请求进行中时,输入框仍允许继续编辑草稿,但此时不能再次提交。
- // 之前把状态直接映射成 disabled,导致输入和联想面板一起失效;如果只去掉 disabled,
- // Enter 又会误触发 submit 并清空草稿。这里在 submitDisabled 时把 Enter 退回给 Lexical 处理换行。
- if (submitDisabled || !enterSubmits) {
- return false;
- }
- event.preventDefault();
- if (
- shouldSubmitLexicalEnter({
- allowSubmitWhenEmpty,
- enterSubmits,
- text,
- })
- ) {
- const submitResult = onSubmit(text);
- // 业务层可能拒绝本次提交并要求草稿留在输入框。
- // Enter 键盘层不能无条件 reset,否则即使业务层未发送,用户输入也会被吃掉。
- if (shouldResetLexicalEditorAfterSubmit(submitResult)) {
- resetEditor(editor);
- }
- }
- return true;
- },
- COMMAND_PRIORITY_HIGH,
- );
- const unregisterBackspace = editor.registerCommand(
- KEY_BACKSPACE_COMMAND,
- (event: KeyboardEvent | null) => {
- const selection = $getSelection();
- if (!$isRangeSelection(selection) || !selection.isCollapsed()) {
- return false;
- }
- const anchor = selection.anchor;
- if (anchor.type !== "text") {
- return false;
- }
- const node = anchor.getNode();
- if (!$isTextNode(node)) {
- return false;
- }
- const text = node.getTextContent();
- if (anchor.offset !== 1 || text !== " ") {
- return false;
- }
- const previousSibling = node.getPreviousSibling();
- if (!$isPromptMentionNode(previousSibling)) {
- return false;
- }
- // mention 插入时会自动补一个空格用于继续输入,
- // 之前 Backspace 会先删这个空格,再删 token,用户体感是“要按两次才删掉标签”。
- // 这里在“光标正好位于补位空格后”时,直接一次性删除空格 + mention token。
- event?.preventDefault();
- previousSibling.remove();
- node.remove();
- node.getParent()?.selectEnd();
- return true;
- },
- COMMAND_PRIORITY_HIGH,
- );
- const handleRootKeyDownCapture = (event: KeyboardEvent) => {
- if (
- disabled ||
- event.key !== "ArrowRight" ||
- !event.altKey ||
- event.shiftKey ||
- event.ctrlKey ||
- event.metaKey ||
- event.isComposing
- ) {
- return;
- }
- const rootElement = editor.getRootElement();
- if (!rootElement) {
- return;
- }
- const mentionId = getPromptMentionIdAfterDomSelection(rootElement);
- if (!mentionId) {
- return;
- }
- // macOS Option+ArrowRight 会按 DOM 文本做词级跳转,之前会把光标落进
- // mention token 内部的文件名文本;下一次输入时 Lexical 会把 token 当作被替换内容删除。
- // 在 token 左边界显式把 selection 移到 token 后侧,保持 mention 的原子编辑语义。
- event.preventDefault();
- event.stopImmediatePropagation();
- editor.update(
- () => {
- selectAfterPromptMentionById(mentionId);
- },
- { tag: PROGRAMMATIC_UPDATE_TAG },
- );
- };
- // 作用域改绑层·非 Enter 分发(统一开放策略):KEY_ENTER_COMMAND 只对 Enter
- // 派发,用户把发送/换行绑成非 Enter 键(如 F9)时由 root keydown capture 分发,
- // 否则改绑是死绑定,且裸 Enter 回退换行会让键盘发送能力整体丢失。
- // 非 Enter 物理键不存在手机软键盘误发问题,send 不受 enterSubmits 视口门禁;
- // 与反转投递(仅响应 Ctrl/Meta+Enter)无交集,无需让位。
- const handleNonEnterScopedKeydown = (event: KeyboardEvent) => {
- if (disabled || event.repeat || event.isComposing || event.key === "Enter") {
- return;
- }
- const scopedAction = resolveComposerKeyAction(event, composerEffectiveRef.current);
- if (!scopedAction) {
- return;
- }
- if (scopedAction === "newline") {
- event.preventDefault();
- editor.update(() => {
- const selection = $getSelection();
- if ($isRangeSelection(selection)) {
- selection.insertLineBreak();
- }
- });
- return;
- }
- if (submitDisabled) {
- return;
- }
- event.preventDefault();
- const text = getEditorMarkdown(editor.getEditorState());
- if (shouldSubmitLexicalEnter({ allowSubmitWhenEmpty, enterSubmits: true, text })) {
- const submitResult = onSubmit(text);
- if (shouldResetLexicalEditorAfterSubmit(submitResult)) {
- resetEditor(editor);
- }
- }
- };
- const unregisterRootListener = editor.registerRootListener(
- (rootElement, previousRootElement) => {
- previousRootElement?.removeEventListener("keydown", handleRootKeyDownCapture, {
- capture: true,
- });
- rootElement?.addEventListener("keydown", handleRootKeyDownCapture, {
- capture: true,
- });
- previousRootElement?.removeEventListener("keydown", handleNonEnterScopedKeydown, {
- capture: true,
- });
- rootElement?.addEventListener("keydown", handleNonEnterScopedKeydown, {
- capture: true,
- });
- },
- );
- return () => {
- unregisterEnter();
- unregisterBackspace();
- unregisterRootListener();
- editor.getRootElement()?.removeEventListener("keydown", handleRootKeyDownCapture, {
- capture: true,
- });
- editor.getRootElement()?.removeEventListener("keydown", handleNonEnterScopedKeydown, {
- capture: true,
- });
- };
- }, [
- allowSubmitWhenEmpty,
- disabled,
- editor,
- enterSubmits,
- onModifiedSubmit,
- onSubmit,
- submitDisabled,
- ]);
- return null;
- }
- /**
- * 文本变化插件
- *
- * 直接用 Lexical 自带的 OnChangePlugin 时,第一次从空编辑器输入字符会被内部的
- * `prevEditorState.isEmpty()` 直接跳过,父组件的 input 状态拿不到首字符,发送按钮和输入内容会错位。
- * 这里改成自己监听 update,只在序列化后的 markdown 真正变化时同步,首字符输入也能稳定回传。
- */
- function TextContentPlugin({
- onChange,
- taskId,
- }: {
- onChange?: (text: string) => void;
- taskId?: string | null;
- }) {
- const [editor] = useLexicalComposerContext();
- // IME 组合态标记:不直接依赖 editor.isComposing(),因为它在 update listener 同步执行时
- // 是否已反映组合态存在时序不确定性,读不到 true 会把中文/日文长文本组合的高耗时误报成打字卡顿。
- // 改由 compositionstart/compositionend 事件自行维护,稳健可控。
- const composingRef = useRef(false);
- useEffect(() => {
- const handleCompositionStart = () => {
- composingRef.current = true;
- };
- const handleCompositionEnd = () => {
- // compositionend 触发时组合刚结束,但「组合提交」这一拍的 update listener 通常在同一轮
- // 任务里同步执行,若立即置 false,这次高耗时会被误判为打字卡顿。用 queueMicrotask 把置 false
- // 延后到当前同步任务之后,确保组合提交那一拍仍按组合态短路,再恢复正常计入。
- queueMicrotask(() => {
- composingRef.current = false;
- });
- };
- // root 会重挂,用 registerRootListener 在新旧 root 上正确解绑/绑定。
- return editor.registerRootListener((rootElement, previousRootElement) => {
- previousRootElement?.removeEventListener("compositionstart", handleCompositionStart);
- previousRootElement?.removeEventListener("compositionend", handleCompositionEnd);
- rootElement?.addEventListener("compositionstart", handleCompositionStart);
- rootElement?.addEventListener("compositionend", handleCompositionEnd);
- });
- }, [editor]);
- useEffect(() => {
- if (!onChange) {
- return;
- }
- return editor.registerUpdateListener(
- ({ dirtyElements, dirtyLeaves, editorState, prevEditorState, tags }) => {
- if (dirtyElements.size === 0 && dirtyLeaves.size === 0) {
- return;
- }
- // 输入卡顿计时:包住「全量序列化 + onChange 同步重渲染」这段处理热点。
- const startedAt = performance.now();
- const nextText = getEditorMarkdown(editorState);
- const previousText = getEditorMarkdown(prevEditorState);
- if (nextText === previousText) {
- return;
- }
- onChange(nextText);
- const lagMs = performance.now() - startedAt;
- // 程序化改写与 IME 组合态不算打字卡顿(判定在 recordInputLag 内统一短路)。
- recordInputLag({
- lagMs,
- textLength: nextText.length,
- isProgrammatic: tags.has(PROGRAMMATIC_UPDATE_TAG),
- isComposing: composingRef.current,
- taskId: taskId ?? undefined,
- });
- },
- );
- }, [editor, onChange, taskId]);
- return null;
- }
- /** 编辑器可编辑状态控制插件 */
- function EditablePlugin({ editable }: { editable: boolean }) {
- const [editor] = useLexicalComposerContext();
- useEffect(() => {
- editor.setEditable(editable);
- }, [editor, editable]);
- return null;
- }
- function E2ELexicalInputBridgePlugin({ inputTestId }: { inputTestId?: string }) {
- const [editor] = useLexicalComposerContext();
- useEffect(() => {
- if (!inputTestId || typeof document === "undefined") {
- return;
- }
- const bridge = {
- focus: () => editor.focus(),
- getEditorState: () => editor.getEditorState(),
- getText: () => getEditorMarkdown(editor.getEditorState()),
- setText: (text: string) => replaceEditorText(editor, text),
- setTextWithPluginMentions: (text: string) => replaceEditorTextWithPluginMentions(editor, text),
- setEditorStateJson: (editorStateJson: string) =>
- replaceEditorStateJson(editor, editorStateJson),
- };
- let attachedInput: HTMLElement | null = null;
- const detachBridge = (input: HTMLElement | null) => {
- if (
- input &&
- (input as { __zcodeLexicalInputE2E?: typeof bridge }).__zcodeLexicalInputE2E === bridge
- ) {
- delete (input as { __zcodeLexicalInputE2E?: typeof bridge }).__zcodeLexicalInputE2E;
- }
- input?.removeAttribute("data-e2e-lexical-bridge");
- };
- const attachBridge = (input: HTMLElement | null) => {
- if (attachedInput && attachedInput !== input) {
- detachBridge(attachedInput);
- }
- attachedInput = input;
- if (!input) {
- return;
- }
- // E2E 需要驱动真实 Lexical state;只改 DOM contenteditable 会绕过 editor update,
- // 容易把文本误打到主输入框,导致测试结论和产品行为脱节。
- Object.defineProperty(input, "__zcodeLexicalInputE2E", {
- configurable: true,
- value: bridge,
- });
- input.setAttribute("data-e2e-lexical-bridge", "ready");
- };
- const resolveInput = (rootElement: HTMLElement | null) => {
- if (rootElement?.getAttribute("data-testid") === inputTestId) {
- return rootElement;
- }
- return document.querySelector<HTMLElement>(`[data-testid="${inputTestId}"]`);
- };
- let retryTimer: number | null = null;
- const tryAttachBridge = () => {
- const nextInput = resolveInput(editor.getRootElement());
- attachBridge(nextInput);
- if (nextInput && retryTimer !== null) {
- window.clearInterval(retryTimer);
- retryTimer = null;
- }
- };
- const unregisterRoot = editor.registerRootListener((rootElement, previousRootElement) => {
- if (previousRootElement !== rootElement) {
- detachBridge(previousRootElement);
- }
- // E2E bridge 之前只在 effect 里查询一次 DOM。
- // 编辑器 root 如果比插件晚挂载或被 Lexical 重挂载,bridge 会永久缺失。
- attachBridge(resolveInput(rootElement));
- });
- tryAttachBridge();
- // edit 场景里 ChatPromptEditor 的 initialValue 回填、Lexical root 注册、
- // React DOM 提交顺序可能跨多个帧。短轮询只负责补挂测试 bridge,不参与产品行为。
- retryTimer = window.setInterval(tryAttachBridge, 100);
- return () => {
- unregisterRoot();
- if (retryTimer !== null) {
- window.clearInterval(retryTimer);
- }
- detachBridge(attachedInput);
- };
- }, [editor, inputTestId]);
- return null;
- }
- function PromptHistoryPlugin({
- entries,
- disabled,
- }: {
- entries: readonly string[];
- disabled?: boolean;
- }) {
- const [editor] = useLexicalComposerContext();
- const historyIndexRef = useRef<number | null>(null);
- const applyingHistoryRef = useRef(false);
- useEffect(() => {
- if (historyIndexRef.current !== null && entries[historyIndexRef.current] === undefined) {
- historyIndexRef.current = null;
- }
- }, [entries]);
- useEffect(() => {
- return editor.registerUpdateListener(({ dirtyElements, dirtyLeaves, editorState }) => {
- if (dirtyElements.size === 0 && dirtyLeaves.size === 0) {
- return;
- }
- if (applyingHistoryRef.current) {
- applyingHistoryRef.current = false;
- return;
- }
- const currentIndex = historyIndexRef.current;
- if (currentIndex === null) {
- return;
- }
- if (entries[currentIndex] === undefined) {
- historyIndexRef.current = null;
- return;
- }
- if (getEditorMarkdown(editorState) !== entries[currentIndex]) {
- historyIndexRef.current = null;
- }
- });
- }, [editor, entries]);
- const applyHistoryEntry = useCallback(
- (nextIndex: number | null, nextValue: string) => {
- historyIndexRef.current = nextIndex;
- applyingHistoryRef.current = true;
- // 使用专用 HISTORY_NAVIGATION_UPDATE_TAG 而非通用 PROGRAMMATIC_UPDATE_TAG,
- // 使 SlashCommandPlugin 能区分"历史回填"与"用户正在输入 slash 查询",
- // 防止回填含 / 的历史条目时面板重新打开并以 COMMAND_PRIORITY_CRITICAL 吞掉后续方向键。
- editor.update(
- () => {
- const root = $getRoot();
- root.clear();
- for (const line of nextValue.split("\n\n")) {
- const paragraph = $createParagraphNode();
- if (line) {
- paragraph.append($createTextNode(line));
- }
- root.append(paragraph);
- }
- root.getLastChild()?.selectEnd();
- },
- { tag: HISTORY_NAVIGATION_UPDATE_TAG },
- );
- },
- [editor],
- );
- const handleHistoryNavigation = useCallback(
- (direction: "up" | "down") => (event: KeyboardEvent | null) => {
- if (!event) return false;
- if (
- disabled ||
- event.shiftKey ||
- event.ctrlKey ||
- event.metaKey ||
- event.altKey ||
- event.isComposing
- ) {
- return false;
- }
- const text = getEditorMarkdown(editor.getEditorState());
- const currentIndex = historyIndexRef.current;
- // 历史导航只在“空输入”或“已经进入历史浏览态”时接管上下键,
- // 避免抢走多行输入原本的光标移动行为。
- if (currentIndex === null && text.length > 0) {
- return false;
- }
- const result = navigatePromptHistory(entries, currentIndex, direction);
- if (!result.shouldHandle) {
- return false;
- }
- event.preventDefault();
- applyHistoryEntry(result.nextIndex, result.nextValue);
- return true;
- },
- [applyHistoryEntry, disabled, editor, entries],
- );
- useEffect(() => {
- const unregisterUp = editor.registerCommand(
- KEY_ARROW_UP_COMMAND,
- handleHistoryNavigation("up"),
- COMMAND_PRIORITY_HIGH,
- );
- const unregisterDown = editor.registerCommand(
- KEY_ARROW_DOWN_COMMAND,
- handleHistoryNavigation("down"),
- COMMAND_PRIORITY_HIGH,
- );
- return () => {
- unregisterUp();
- unregisterDown();
- };
- }, [editor, handleHistoryNavigation]);
- return null;
- }
- function isCollapsedSelectionAtEditorStart(): boolean {
- const selection = $getSelection();
- if (!$isRangeSelection(selection) || !selection.isCollapsed()) {
- return false;
- }
- const [startPoint] = selection.getStartEndPoints() ?? [];
- if (!startPoint || startPoint.offset !== 0) {
- return false;
- }
- const root = $getRoot();
- const startNode = startPoint.getNode();
- if (startPoint.type === "text") {
- const firstTextNode = root.getAllTextNodes()[0] ?? null;
- return firstTextNode?.is(startNode) ?? root.getTextContentSize() === 0;
- }
- if (startNode.is(root)) {
- return true;
- }
- const firstDescendant = root.getFirstDescendant();
- return firstDescendant?.is(startNode) ?? root.getTextContentSize() === 0;
- }
- function LeadingChineseSlashAliasPlugin({ disabled }: { disabled?: boolean }) {
- const [editor] = useLexicalComposerContext();
- useEffect(() => {
- if (disabled) {
- return;
- }
- const handleBeforeInput = (event: InputEvent) => {
- let shouldNormalize = false;
- editor.getEditorState().read(() => {
- shouldNormalize = shouldNormalizeLeadingChineseSlashAliasInput({
- data: event.data,
- inputType: event.inputType,
- isAtEditorStart: isCollapsedSelectionAtEditorStart(),
- isComposing: event.isComposing,
- });
- });
- if (!shouldNormalize) {
- return;
- }
- // 中文输入法下用户在输入框第一位想打 `/` 唤起命令时,
- // 可能会实际输入顿号 `、`。这里只拦截真实手输且位于全文开头的顿号,
- // 立即归一成标准 `/`,避免把顿号扩散成 slash command 的另一套匹配语法。
- event.preventDefault();
- editor.update(
- () => {
- if (!isCollapsedSelectionAtEditorStart()) {
- return;
- }
- const selection = $getSelection();
- if (!$isRangeSelection(selection)) {
- return;
- }
- selection.insertText(STANDARD_SLASH_TRIGGER);
- },
- { tag: PROGRAMMATIC_UPDATE_TAG },
- );
- };
- return editor.registerRootListener((rootElement, previousRootElement) => {
- previousRootElement?.removeEventListener("beforeinput", handleBeforeInput as EventListener);
- rootElement?.addEventListener("beforeinput", handleBeforeInput as EventListener);
- });
- }, [disabled, editor]);
- return null;
- }
- function PasteCapturePlugin({
- disabled,
- onPaste,
- }: {
- disabled?: boolean;
- onPaste?: (event: ChatComposerPasteEvent) => void;
- }) {
- const [editor] = useLexicalComposerContext();
- useEffect(() => {
- if (!onPaste) {
- return;
- }
- const handlePaste = (event: ClipboardEvent) => {
- if (disabled) {
- return;
- }
- const wasDefaultPrevented = event.defaultPrevented;
- onPaste(event);
- if (event.defaultPrevented && !wasDefaultPrevented) {
- // 长文本已经被转成附件后,必须阻断 Lexical 后续 paste command,
- // 否则会出现“附件有了,正文也被插入”的双份内容。
- event.stopImmediatePropagation();
- }
- };
- return editor.registerRootListener((rootElement, previousRootElement) => {
- previousRootElement?.removeEventListener("paste", handlePaste, {
- capture: true,
- });
- rootElement?.addEventListener("paste", handlePaste, { capture: true });
- });
- }, [disabled, editor, onPaste]);
- return null;
- }
- /** 暴露 focus / clear / getText 给外部 */
- export function insertEditorMention(
- editor: LexicalEditor,
- mention: ComposerMentionPrefill,
- selectionState?: EditorState,
- ) {
- const selection = selectionState?.read(() => $getSelection()?.clone() ?? null);
- editor.update(
- () => {
- // 浮层获取焦点后 Lexical 选区会丢失,恢复打开菜单前的光标,避免覆盖整份草稿。
- // 草稿可能在菜单打开期间被替换;旧节点不存在时不能恢复选区,否则 Lexical 会抛错。
- if ($isRangeSelection(selection)) {
- if ($getNodeByKey(selection.anchor.key) && $getNodeByKey(selection.focus.key)) {
- $setSelection(selection);
- } else {
- $getRoot().selectEnd();
- }
- }
- let target = $getSelection();
- if (!$isRangeSelection(target)) target = $getRoot().selectEnd();
- const trailing = $createTextNode(" ");
- target.insertNodes([$createPromptMentionNode(mention), trailing]);
- trailing.selectEnd();
- },
- { tag: PROGRAMMATIC_UPDATE_TAG },
- );
- }
- function EditorApiPlugin({
- editorApiRef,
- }: {
- editorApiRef?: React.MutableRefObject<LexicalChatInputHandle | null>;
- }) {
- const [editor] = useLexicalComposerContext();
- useEffect(() => {
- if (!editorApiRef) {
- return;
- }
- editorApiRef.current = {
- clear: () => resetEditor(editor),
- focus: () => editor.focus(),
- getEditorState: () => editor.getEditorState(),
- getMarkdown: () => getEditorMarkdown(editor.getEditorState()),
- getText: () => getEditorMarkdown(editor.getEditorState()),
- appendText: (text: string) => appendEditorPlainText(editor, text),
- appendFileMention: (
- label: string,
- value: string,
- markdown: string,
- data?: MentionItemData,
- trailingText = " ",
- ) => appendEditorFileMention(editor, label, value, markdown, data, trailingText),
- insertMention: (mention, selectionState) =>
- insertEditorMention(editor, mention, selectionState),
- prependMentionIfMissing: (mention) => prependEditorMentionIfMissing(editor, mention),
- setMention: (mention, trailingText) =>
- replaceEditorWithMention(editor, mention, trailingText),
- setText: (text: string) => replaceEditorText(editor, text),
- setTextWithPluginMentions: (text: string) => replaceEditorTextWithPluginMentions(editor, text),
- setEditorStateJson: (editorStateJson: string) =>
- replaceEditorStateJson(editor, editorStateJson),
- setSkillMention: (skillName: string, markdown = `$${skillName}`, trailingText = " ") =>
- replaceEditorWithSkillMention(editor, skillName, markdown, trailingText),
- setSlashCommandMention: (
- commandName: string,
- markdown = `/${commandName}`,
- trailingText = " ",
- ) => replaceEditorWithSlashCommandMention(editor, commandName, markdown, trailingText),
- };
- return () => {
- editorApiRef.current = null;
- };
- }, [editor, editorApiRef]);
- return null;
- }
- interface LexicalChatInputProps {
- placeholder?: string;
- disabled?: boolean;
- submitDisabled?: boolean;
- allowSubmitWhenEmpty?: boolean;
- enterSubmits?: boolean;
- onSubmit: (text: string) => LexicalSubmitResult;
- onModifiedSubmit?: (text: string) => LexicalSubmitResult;
- onChange?: (text: string) => void;
- onFocus?: () => void;
- triggerPanelContainer?: HTMLElement | null;
- workspacePath: string;
- workspaceIdentity?: string;
- taskId: string | null;
- /** 仅影响 Skill 引用目录;草稿可使用 prewarm Session runtime。 */
- skillCatalogSessionId?: string | null;
- inputTestId?: string;
- editorApiRef?: React.MutableRefObject<LexicalChatInputHandle | null>;
- promptHistory?: readonly string[];
- avoidIosInputFocusZoom?: boolean;
- compactPlaceholder?: boolean;
- onWhiteboardMentionSelected?: (boardId: string) => void | Promise<void>;
- onPaste?: (event: ChatComposerPasteEvent) => void;
- excludedSlashCommandNames?: readonly string[];
- /** App 层本地斜杠命令(如 `/side`),选中即执行 UI 行为,不发送。 */
- appSlashCommands?: readonly AppSlashCommand[];
- /** mention(@/#)面板开关。v4 数据面未就绪时显式关闭,入口保留。 */
- enableMentionPanel?: boolean;
- }
- const EDITOR_THEME = {
- paragraph: "m-0",
- };
- export function LexicalChatInput({
- placeholder,
- disabled = false,
- submitDisabled = false,
- allowSubmitWhenEmpty = false,
- enterSubmits = true,
- onSubmit,
- onModifiedSubmit,
- onChange,
- onFocus,
- triggerPanelContainer,
- workspacePath,
- workspaceIdentity,
- taskId,
- skillCatalogSessionId,
- inputTestId,
- editorApiRef,
- promptHistory = [],
- avoidIosInputFocusZoom = false,
- compactPlaceholder = false,
- onWhiteboardMentionSelected,
- onPaste,
- excludedSlashCommandNames,
- appSlashCommands,
- enableMentionPanel = true,
- }: LexicalChatInputProps) {
- const inputMountedAtRef = useRef(Date.now());
- const lastReadyLogKeyRef = useRef<string | null>(null);
- const activeTaskProvider = useChatViewActiveTaskProvider(
- taskId,
- workspacePath,
- workspaceIdentity,
- );
- useEffect(() => {
- const readyLogKey = [
- workspaceIdentity ?? workspacePath,
- taskId ?? "draft",
- activeTaskProvider,
- triggerPanelContainer ? "trigger-ready" : "trigger-missing",
- ].join("|");
- if (lastReadyLogKeyRef.current === readyLogKey) {
- return;
- }
- lastReadyLogKeyRef.current = readyLogKey;
- let frameId: number | null = null;
- const logReady = () => {
- logger.info("[LexicalChatInput] 输入编辑器首帧完成", {
- durationMs: Date.now() - inputMountedAtRef.current,
- activeTaskProvider,
- disabled,
- hasPromptHistory: promptHistory.length > 0,
- submitDisabled,
- taskId,
- triggerPanelReady: Boolean(triggerPanelContainer),
- workspaceIdentity: workspaceIdentity ?? null,
- workspacePath,
- });
- };
- // Lexical 编辑器初始化、slash/mention plugin 和外部 portal 分属不同组件。
- // 这里等浏览器下一帧再打点,才能和 composer shell 首帧、toolbar portal ready 对齐。
- if (typeof requestAnimationFrame === "function") {
- frameId = requestAnimationFrame(logReady);
- } else {
- logReady();
- }
- return () => {
- if (frameId !== null) {
- cancelAnimationFrame(frameId);
- }
- };
- }, [
- activeTaskProvider,
- disabled,
- promptHistory.length,
- submitDisabled,
- taskId,
- triggerPanelContainer,
- workspaceIdentity,
- workspacePath,
- ]);
- const handleSubmit = useCallback(
- (text: string) => {
- const submitResult = onSubmit(text);
- requestAnimationFrame(() => {
- editorApiRef?.current?.focus();
- });
- return submitResult;
- },
- [editorApiRef, onSubmit],
- );
- // Lexical 的 ContentEditable props 是一个互斥联合类型,
- // aria-placeholder 一旦出现就要求 placeholder 同时存在。
- // 之前直接写三元 JSX,TypeScript 在合并两条分支时没有正确保留这组联动约束,导致误报缺少 placeholder。
- const contentEditableProps: React.ComponentProps<typeof ContentEditable> = placeholder
- ? {
- "aria-placeholder": placeholder,
- placeholder: (
- <div
- className={`pointer-events-none absolute left-0 top-0 ${compactPlaceholder ? "line-clamp-2" : ""} ${resolveMobileInputTextSizeClassName(
- {
- avoidIosInputFocusZoom,
- },
- )} text-foreground-subtlest`}
- >
- {placeholder}
- </div>
- ),
- }
- : {
- placeholder: null,
- };
- const contentEditable = (
- <ContentEditable
- // Bugfix: mention node 使用固定行高的 inline-flex chip,普通正文如果继承浏览器 normal line-height,
- // 在 token 后继续输入文字时会按不同 line box 计算基线;这里显式收口正文行高,并允许手机远控使用 16px 防止 iOS 聚焦放大。
- className={`min-h-10 max-h-40 overflow-y-auto ${resolveMobileInputTextSizeClassName({
- avoidIosInputFocusZoom,
- })} text-foreground outline-none`}
- data-testid={inputTestId}
- onFocus={onFocus}
- {...contentEditableProps}
- />
- );
- const initialConfig = useMemo(
- () => ({
- namespace: "ChatInput",
- theme: EDITOR_THEME,
- nodes: [PromptMentionNode],
- // LexicalComposer initialConfig 每次 render 新建会让输入区子树被记录为 props 变化;
- // 配置内容本身是静态的,固定引用能避免无关状态更新触发 composer 子树冒泡。
- onError: (error: Error) => {
- logger.error("[LexicalChatInput] editor error:", error);
- },
- }),
- [],
- );
- return (
- <div className="relative flex-1">
- <LexicalComposer initialConfig={initialConfig}>
- <div className="relative">
- <PlainTextPlugin contentEditable={contentEditable} ErrorBoundary={LexicalErrorBoundary} />
- <HistoryPlugin />
- <PromptClipboardPlugin />
- <TextContentPlugin onChange={onChange} taskId={taskId} />
- <KeyboardPlugin
- onSubmit={handleSubmit}
- onModifiedSubmit={onModifiedSubmit}
- disabled={disabled}
- submitDisabled={submitDisabled}
- allowSubmitWhenEmpty={allowSubmitWhenEmpty}
- enterSubmits={enterSubmits}
- />
- <PromptHistoryPlugin entries={promptHistory} disabled={disabled} />
- <EditablePlugin editable={!disabled} />
- <E2ELexicalInputBridgePlugin inputTestId={inputTestId} />
- <EditorApiPlugin editorApiRef={editorApiRef} />
- <LeadingChineseSlashAliasPlugin disabled={disabled} />
- <PasteCapturePlugin disabled={disabled} onPaste={onPaste} />
- </div>
- <SlashCommandPlugin
- workspacePath={workspacePath}
- workspaceIdentity={workspaceIdentity}
- sessionId={skillCatalogSessionId ?? taskId}
- provider={activeTaskProvider}
- container={triggerPanelContainer}
- disabled={disabled}
- excludedCommandNames={excludedSlashCommandNames}
- appCommands={appSlashCommands}
- />
- {enableMentionPanel ? (
- <MentionPlugin
- workspacePath={workspacePath}
- workspaceIdentity={workspaceIdentity}
- sessionId={skillCatalogSessionId ?? taskId}
- provider={activeTaskProvider}
- container={triggerPanelContainer}
- disabled={disabled}
- onWhiteboardMentionSelected={onWhiteboardMentionSelected}
- />
- ) : null}
- </LexicalComposer>
- </div>
- );
- }
- /** 简单的错误边界 */
- function LexicalErrorBoundary({ children }: { children: React.ReactNode }) {
- return <>{children}</>;
- }
|