This repository has been archived by the owner on Nov 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(text-editor): improve command handling
- Loading branch information
1 parent
0000aa6
commit c3c0534
Showing
3 changed files
with
92 additions
and
45 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
35 changes: 35 additions & 0 deletions
35
apps/customer-service/src/components/text-editor/plugins/my-custom-command-handler-plugin.ts
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,35 @@ | ||
import { useContext, useEffect } from 'react'; | ||
import { useLexicalComposerContext } from '@lexical/react/LexicalComposerContext'; | ||
import { mergeRegister } from '@lexical/utils'; | ||
import { COMMAND_PRIORITY_LOW, KEY_ENTER_COMMAND } from 'lexical'; | ||
|
||
import { FormElementContext } from '~/components/messages/message-form'; | ||
|
||
// Lexical React plugins are React components, which makes them | ||
// highly composable. Furthermore, you can lazy load plugins if | ||
// desired, so you don't pay the cost for plugins until you | ||
// actually use them. | ||
export default function MyCustomCommandHandlerPlugin() { | ||
const [editor] = useLexicalComposerContext(); | ||
const form = useContext(FormElementContext); | ||
|
||
useEffect(() => { | ||
return mergeRegister( | ||
editor.registerCommand( | ||
KEY_ENTER_COMMAND, | ||
(event: KeyboardEvent) => { | ||
// skipping if shift is pressed (defaulting to line-break) | ||
if (event !== null && !event.ctrlKey) { | ||
event.preventDefault(); | ||
form?.current?.requestSubmit(); | ||
return true; | ||
} | ||
return false; | ||
}, | ||
COMMAND_PRIORITY_LOW | ||
) | ||
); | ||
}, [editor]); | ||
|
||
return null; | ||
} |
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