-
-
Notifications
You must be signed in to change notification settings - Fork 699
fix: save file caption/name on every keystroke instead of on close #2575
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,7 +9,6 @@ import { | |
| ChangeEvent, | ||
| KeyboardEvent, | ||
| useCallback, | ||
| useEffect, | ||
| useState, | ||
| } from "react"; | ||
| import { RiFontFamily } from "react-icons/ri"; | ||
|
|
@@ -59,49 +58,42 @@ export const FileRenameButton = () => { | |
| }, | ||
| }); | ||
|
|
||
| const [currentEditingName, setCurrentEditingName] = useState<string>(); | ||
|
|
||
| useEffect(() => { | ||
| if (block === undefined) { | ||
| return; | ||
| } | ||
|
|
||
| setCurrentEditingName(block.props.name); | ||
| }, [block]); | ||
| const [popoverOpen, setPopoverOpen] = useState(false); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Reset popover state when the selected block changes. Same issue here: Suggested fix import {
ChangeEvent,
KeyboardEvent,
useCallback,
+ useEffect,
useState,
} from "react";
@@
const [popoverOpen, setPopoverOpen] = useState(false);
+
+ useEffect(() => {
+ setPopoverOpen(false);
+ }, [block?.id]);Also applies to: 88-90, 93-96 🤖 Prompt for AI Agents |
||
|
|
||
| const handleChange = useCallback( | ||
| (event: ChangeEvent<HTMLInputElement>) => | ||
| setCurrentEditingName(event.currentTarget.value), | ||
| [], | ||
| ); | ||
|
|
||
| const handleEnter = useCallback( | ||
| (event: KeyboardEvent) => { | ||
| (event: ChangeEvent<HTMLInputElement>) => { | ||
| if ( | ||
| block !== undefined && | ||
| editorHasBlockWithType(editor, block.type, { | ||
| name: "string", | ||
| }) && | ||
| event.key === "Enter" && | ||
| !event.nativeEvent.isComposing | ||
| }) | ||
| ) { | ||
| event.preventDefault(); | ||
| editor.updateBlock(block.id, { | ||
| props: { | ||
| name: currentEditingName, | ||
| name: event.currentTarget.value, | ||
| }, | ||
| }); | ||
| } | ||
| }, | ||
| [block, currentEditingName, editor], | ||
| [block, editor], | ||
| ); | ||
|
|
||
| const handleKeyDown = useCallback((event: KeyboardEvent) => { | ||
| if (event.key === "Enter" && !event.nativeEvent.isComposing) { | ||
| event.preventDefault(); | ||
| setPopoverOpen(false); | ||
| } | ||
| }, []); | ||
|
|
||
| if (block === undefined) { | ||
| return null; | ||
| } | ||
|
|
||
| return ( | ||
| <Components.Generic.Popover.Root> | ||
| <Components.Generic.Popover.Root | ||
| open={popoverOpen} | ||
| onOpenChange={setPopoverOpen} | ||
| > | ||
| <Components.Generic.Popover.Trigger> | ||
| <Components.FormattingToolbar.Button | ||
| className={"bn-button"} | ||
|
|
@@ -114,6 +106,7 @@ export const FileRenameButton = () => { | |
| dict.formatting_toolbar.file_rename.tooltip["file"] | ||
| } | ||
| icon={<RiFontFamily />} | ||
| onClick={() => setPopoverOpen((open) => !open)} | ||
| /> | ||
| </Components.Generic.Popover.Trigger> | ||
| <Components.Generic.Popover.Content | ||
|
|
@@ -124,14 +117,14 @@ export const FileRenameButton = () => { | |
| <Components.Generic.Form.TextInput | ||
| name={"file-name"} | ||
| icon={<RiFontFamily />} | ||
| value={currentEditingName || ""} | ||
| value={block.props.name} | ||
| autoFocus={true} | ||
| placeholder={ | ||
| dict.formatting_toolbar.file_rename.input_placeholder[ | ||
| block.type | ||
| ] || dict.formatting_toolbar.file_rename.input_placeholder["file"] | ||
| } | ||
| onKeyDown={handleEnter} | ||
| onKeyDown={handleKeyDown} | ||
| onChange={handleChange} | ||
| /> | ||
| </Components.Generic.Form.Root> | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reset popover state when the selected block changes.
popoverOpen(Line 61) survives renders whereblockisundefined(Line 88), so this popover can reopen unexpectedly when a file block is selected again. Please resetpopoverOpenwhen block identity changes.Suggested fix
import { ChangeEvent, KeyboardEvent, useCallback, + useEffect, useState, } from "react"; @@ const [popoverOpen, setPopoverOpen] = useState(false); + + useEffect(() => { + setPopoverOpen(false); + }, [block?.id]);Also applies to: 88-90, 93-96
🤖 Prompt for AI Agents