🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.
TypeScript agentagent-collaborationagent-harnessloop-engineeringmcppersonal-assistantproactive-agentskillssuper-agenttask-management
🐛 fix(task): keep the title focused when a pasted image uploads (#21821)
Rdmclin263ccdfa1f060
diff --git a/src/features/AgentTasks/CreateTaskModal/CreateTaskContent.tsx b/src/features/AgentTasks/CreateTaskModal/CreateTaskContent.tsxindex 198b657e789..53a738af705 100644--- a/src/features/AgentTasks/CreateTaskModal/CreateTaskContent.tsx+++ b/src/features/AgentTasks/CreateTaskModal/CreateTaskContent.tsx@@ -23,6 +23,7 @@ import {getAttachmentFileIdsFromEditor,pickAndInsertAttachments,} from '@/features/EditorCanvas/editorAttachments';+import { releaseEditorSelection } from '@/features/EditorCanvas/releaseEditorSelection';import { usePermission } from '@/hooks/usePermission';import { useGlobalStore } from '@/store/global';import { useTaskStore } from '@/store/task';@@ -239,6 +240,10 @@ const CreateTaskContent = memo<CreateTaskContentProps>(const handleTitleKeyDown = useTitleEnterToInstruction(editor);+ // An image pasted into the instruction finishes uploading later; without+ // this, that update would move the caret back from the title mid-typing.+ const handleTitleFocus = useCallback(() => releaseEditorSelection(editor), [editor]);+return (<Flexbox flex={1} style={{ minHeight: 0 }} onKeyDown={handleKeyDown}><Flexbox@@ -265,6 +270,7 @@ const CreateTaskContent = memo<CreateTaskContentProps>(padding: '4px 0',}}onChange={(e) => setTitle(e.target.value)}+ onFocus={handleTitleFocus}onKeyDown={handleTitleKeyDown}/><Flexbox horizontal gap={4} style={{ flexShrink: 0 }}>diff --git a/src/features/EditorCanvas/releaseEditorSelection.test.ts b/src/features/EditorCanvas/releaseEditorSelection.test.tsnew file mode 100644index 00000000000..8fece68a7da--- /dev/null+++ b/src/features/EditorCanvas/releaseEditorSelection.test.ts@@ -0,0 +1,84 @@+/**+ * @vitest-environment happy-dom+ */+import {+ $createParagraphNode,+ $createTextNode,+ $getRoot,+ createEditor,+ type LexicalEditor,+ type TextNode,+} from 'lexical';+import { afterEach, describe, expect, it } from 'vitest';++import { releaseEditorSelectionOnLexical } from './releaseEditorSelection';++const commit = (editor: LexicalEditor, fn: () => void) =>+ new Promise<void>((resolve) => {+ editor.update(fn, { onUpdate: resolve });+ });++/**+ * A mounted editor whose caret sits in its body, and a title input outside it+ * that the user has moved to, as in the create-task modal after pasting an image.+ */+const setup = async () => {+ const root = document.createElement('div');+ root.contentEditable = 'true';+ const title = document.createElement('input');+ document.body.append(title, root);++ const editor = createEditor({+ onError: (e) => {+ throw e;+ },+ });+ editor.setRootElement(root);++ let text: TextNode | undefined;+ await commit(editor, () => {+ text = $createTextNode('body');+ $getRoot().append($createParagraphNode().append(text));+ text.select(4, 4);+ });++ title.focus();+ window.getSelection()?.removeAllRanges();++ // Stands in for the image node's `setUploaded`: a later async update that+ // changes content without the user touching the editor.+ const finishUpload = () => commit(editor, () => text!.setTextContent('body!'));++ return { editor, finishUpload, root, title };+};++// Browsers move focus along with a DOM selection placed in a contenteditable;+// happy-dom does not, so the selection is what these tests can observe.+const selectionInside = (root: HTMLElement) => {+ const anchor = window.getSelection()?.anchorNode;+ return !!anchor && root.contains(anchor);+};++afterEach(() => {+ document.body.innerHTML = '';+});++describe('releaseEditorSelectionOnLexical', () => {+ it('keeps focus on the title when an upload finishes after the user left the editor', async () => {+ const { editor, finishUpload, root, title } = await setup();++ releaseEditorSelectionOnLexical(editor);+ await finishUpload();++ expect(document.activeElement).toBe(title);+ expect(selectionInside(root)).toBe(false);+ });++ it('without releasing, the same update pulls the selection back into the editor', async () => {+ const { finishUpload, root } = await setup();++ await finishUpload();++ expect(selectionInside(root)).toBe(true);+ });+});diff --git a/src/features/EditorCanvas/releaseEditorSelection.ts b/src/features/EditorCanvas/releaseEditorSelection.tsnew file mode 100644index 00000000000..4570e593e95--- /dev/null+++ b/src/features/EditorCanvas/releaseEditorSelection.ts@@ -0,0 +1,25 @@+import type { IEditor } from '@lobehub/editor';+import { $setSelection, type LexicalEditor, SKIP_DOM_SELECTION_TAG } from 'lexical';++/** Exported for tests, which drive a bare Lexical editor without the kernel. */+export const releaseEditorSelectionOnLexical = (lexicalEditor: LexicalEditor): void => {+ // The tag keeps this update from touching the DOM selection, which already+ // belongs to the field the user moved to.+ lexicalEditor.update(() => $setSelection(null), { tag: SKIP_DOM_SELECTION_TAG });+};++/**+ * Drop the editor's own selection when focus moves to a field outside it.+ *+ * Lexical keeps its last caret after a blur and restores it into the DOM on the+ * next update that changes content. An async update such as an image upload+ * finishing would then pull focus back into the editor while the user is typing+ * elsewhere. With no selection there is nothing to restore; clicking back into+ * the editor creates a new one.+ */+export const releaseEditorSelection = (editor: IEditor | undefined): void => {+ const lexicalEditor = editor?.getLexicalEditor?.();+ if (!lexicalEditor) return;++ releaseEditorSelectionOnLexical(lexicalEditor);+};