feat(frontend): handle no brain selection (#2932)

# Description

Please include a summary of the changes and the related issue. Please
also include relevant motivation and context.

## Checklist before requesting a review

Please delete options that are not relevant.

- [ ] My code follows the style guidelines of this project
- [ ] I have performed a self-review of my code
- [ ] I have commented hard-to-understand areas
- [ ] I have ideally added tests that prove my fix is effective or that
my feature works
- [ ] New and existing unit tests pass locally with my changes
- [ ] Any dependent changes have been merged

## Screenshots (if appropriate):
This commit is contained in:
Antoine Dewez 2024-07-31 16:05:11 +02:00 committed by GitHub
parent 685558560c
commit b6bb55d133
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 20 additions and 3 deletions

View File

@ -63,7 +63,24 @@ export const Editor = ({
return (
<EditorContent
className="w-full caret-accent"
onKeyDown={(event) => void submitOnEnter(event)}
onKeyDown={(event) => {
if (event.key === "Enter" && !event.shiftKey && !currentBrain) {
event.preventDefault();
const lastChar = editor?.state.doc.textBetween(
editor.state.selection.$from.pos - 1,
editor.state.selection.$from.pos,
undefined,
"\ufffc"
);
if (lastChar === " ") {
editor?.chain().insertContent("@").focus().run();
} else {
editor?.chain().insertContent(" @").focus().run();
}
} else {
submitOnEnter(event);
}
}}
editor={editor}
/>
);

View File

@ -16,7 +16,7 @@ export const useCreateEditorState = (placeholder?: string) => {
const { BrainMention, items } = useBrainMention();
const { remainingCredits } = useUserSettingsContext();
const PreventEnter = Extension.create({
const PreventNewline = Extension.create({
addKeyboardShortcuts: () => {
return {
Enter: () => true,
@ -35,7 +35,7 @@ export const useCreateEditorState = (placeholder?: string) => {
preserveWhitespace: "full",
},
extensions: [
PreventEnter,
PreventNewline,
Placeholder.configure({
showOnlyWhenEditable: true,
placeholder: placeholder ?? t("actions_bar_placeholder"),