-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Implement custom AutoFocusPlugin * Update comment
- Loading branch information
Showing
2 changed files
with
31 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
src/components/chat-view/chat-input/plugins/auto-focus/AutoFocusPlugin.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { useLexicalComposerContext } from '@lexical/react/LexicalComposerContext' | ||
import { useEffect } from 'react' | ||
|
||
export type AutoFocusPluginProps = { | ||
defaultSelection?: 'rootStart' | 'rootEnd' | ||
} | ||
|
||
export default function AutoFocusPlugin({ | ||
defaultSelection, | ||
}: AutoFocusPluginProps) { | ||
const [editor] = useLexicalComposerContext() | ||
|
||
useEffect(() => { | ||
editor.focus( | ||
() => { | ||
const rootElement = editor.getRootElement() | ||
if (rootElement) { | ||
// requestAnimationFrame is required here for unknown reasons, possibly related to the Obsidian plugin environment. | ||
requestAnimationFrame(() => { | ||
rootElement.focus() | ||
}) | ||
} | ||
}, | ||
{ defaultSelection }, | ||
) | ||
}, [defaultSelection, editor]) | ||
|
||
return null | ||
} |