[WIKI-696] fix: peek view close on click block menu options (#7861)

* fix: block close

* chore : add toggle option in block menu

* fix: separate methods
This commit is contained in:
Vipin Chaudhary 2025-09-30 15:29:35 +05:30 committed by GitHub
parent 843faf85b7
commit 0ad439fa63
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 29 additions and 18 deletions

View file

@ -43,6 +43,20 @@ export const BlockMenu = (props: Props) => {
const dismiss = useDismiss(context); const dismiss = useDismiss(context);
const { getFloatingProps } = useInteractions([dismiss]); const { getFloatingProps } = useInteractions([dismiss]);
const openBlockMenu = useCallback(() => {
if (!isOpen) {
setIsOpen(true);
editor.commands.addActiveDropbarExtension(CORE_EXTENSIONS.SIDE_MENU);
}
}, [editor, isOpen]);
const closeBlockMenu = useCallback(() => {
if (isOpen) {
setIsOpen(false);
editor.commands.removeActiveDropbarExtension(CORE_EXTENSIONS.SIDE_MENU);
}
}, [editor, isOpen]);
// Handle click on drag handle // Handle click on drag handle
const handleClickDragHandle = useCallback( const handleClickDragHandle = useCallback(
(event: MouseEvent) => { (event: MouseEvent) => {
@ -70,27 +84,27 @@ export const BlockMenu = (props: Props) => {
editor.chain().setNodeSelection(nodePos).run(); editor.chain().setNodeSelection(nodePos).run();
} }
// Show the menu // Show the menu
setIsOpen(true); openBlockMenu();
return; return;
} }
// If clicking outside and not on a menu item, hide the menu // If clicking outside and not on a menu item, hide the menu
if (menuRef.current && !menuRef.current.contains(target)) { if (menuRef.current && !menuRef.current.contains(target)) {
setIsOpen(false); closeBlockMenu();
} }
}, },
[refs] [editor, refs, openBlockMenu, closeBlockMenu]
); );
useEffect(() => { useEffect(() => {
const handleKeyDown = (event: KeyboardEvent) => { const handleKeyDown = (event: KeyboardEvent) => {
if (event.key === "Escape") { if (event.key === "Escape") {
setIsOpen(false); closeBlockMenu();
} }
}; };
const handleScroll = () => { const handleScroll = () => {
setIsOpen(false); closeBlockMenu();
}; };
document.addEventListener("click", handleClickDragHandle); document.addEventListener("click", handleClickDragHandle);
document.addEventListener("contextmenu", handleClickDragHandle); document.addEventListener("contextmenu", handleClickDragHandle);
@ -103,7 +117,7 @@ export const BlockMenu = (props: Props) => {
document.removeEventListener("keydown", handleKeyDown); document.removeEventListener("keydown", handleKeyDown);
document.removeEventListener("scroll", handleScroll, true); document.removeEventListener("scroll", handleScroll, true);
}; };
}, [handleClickDragHandle]); }, [editor.commands, handleClickDragHandle, closeBlockMenu]);
// Animation effect // Animation effect
useEffect(() => { useEffect(() => {
@ -133,14 +147,9 @@ export const BlockMenu = (props: Props) => {
icon: Trash2, icon: Trash2,
key: "delete", key: "delete",
label: "Delete", label: "Delete",
onClick: (e) => { onClick: (_e) => {
e.preventDefault();
e.stopPropagation();
// Execute the delete action // Execute the delete action
editor.chain().deleteSelection().focus().run(); editor.chain().deleteSelection().focus().run();
setIsOpen(false);
}, },
}, },
{ {
@ -150,10 +159,7 @@ export const BlockMenu = (props: Props) => {
isDisabled: isDisabled:
editor.state.selection.content().content.firstChild?.type.name === CORE_EXTENSIONS.IMAGE || editor.state.selection.content().content.firstChild?.type.name === CORE_EXTENSIONS.IMAGE ||
editor.isActive(CORE_EXTENSIONS.CUSTOM_IMAGE), editor.isActive(CORE_EXTENSIONS.CUSTOM_IMAGE),
onClick: (e) => { onClick: (_e) => {
e.preventDefault();
e.stopPropagation();
try { try {
const { state } = editor; const { state } = editor;
const { selection } = state; const { selection } = state;
@ -187,7 +193,6 @@ export const BlockMenu = (props: Props) => {
console.error(error.message); console.error(error.message);
} }
} }
setIsOpen(false);
}, },
}, },
]; ];
@ -225,7 +230,12 @@ export const BlockMenu = (props: Props) => {
key={item.key} key={item.key}
type="button" type="button"
className="flex w-full items-center gap-1.5 truncate rounded px-1 py-1.5 text-xs text-custom-text-200 hover:bg-custom-background-90" className="flex w-full items-center gap-1.5 truncate rounded px-1 py-1.5 text-xs text-custom-text-200 hover:bg-custom-background-90"
onClick={item.onClick} onClick={(e) => {
item.onClick(e);
e.preventDefault();
e.stopPropagation();
closeBlockMenu();
}}
disabled={item.isDisabled} disabled={item.isDisabled}
> >
<item.icon className="h-3 w-3" /> <item.icon className="h-3 w-3" />

View file

@ -16,6 +16,7 @@ type TActiveDropbarExtensions =
| CORE_EXTENSIONS.EMOJI | CORE_EXTENSIONS.EMOJI
| CORE_EXTENSIONS.SLASH_COMMANDS | CORE_EXTENSIONS.SLASH_COMMANDS
| CORE_EXTENSIONS.TABLE | CORE_EXTENSIONS.TABLE
| CORE_EXTENSIONS.SIDE_MENU
| TAdditionalActiveDropbarExtensions; | TAdditionalActiveDropbarExtensions;
declare module "@tiptap/core" { declare module "@tiptap/core" {