Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 28 additions & 18 deletions packages/editor/src/core/components/menus/block-menu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,20 @@ export const BlockMenu = (props: Props) => {
const dismiss = useDismiss(context);
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
const handleClickDragHandle = useCallback(
(event: MouseEvent) => {
Expand Down Expand Up @@ -70,27 +84,27 @@ export const BlockMenu = (props: Props) => {
editor.chain().setNodeSelection(nodePos).run();
}
// Show the menu
setIsOpen(true);
openBlockMenu();
return;
}

// If clicking outside and not on a menu item, hide the menu
if (menuRef.current && !menuRef.current.contains(target)) {
setIsOpen(false);
closeBlockMenu();
}
},
[refs]
[editor, refs, openBlockMenu, closeBlockMenu]
);

useEffect(() => {
const handleKeyDown = (event: KeyboardEvent) => {
if (event.key === "Escape") {
setIsOpen(false);
closeBlockMenu();
}
};

const handleScroll = () => {
setIsOpen(false);
closeBlockMenu();
};
document.addEventListener("click", handleClickDragHandle);
document.addEventListener("contextmenu", handleClickDragHandle);
Expand All @@ -103,7 +117,7 @@ export const BlockMenu = (props: Props) => {
document.removeEventListener("keydown", handleKeyDown);
document.removeEventListener("scroll", handleScroll, true);
};
}, [handleClickDragHandle]);
}, [editor.commands, handleClickDragHandle, closeBlockMenu]);

// Animation effect
useEffect(() => {
Expand Down Expand Up @@ -133,14 +147,9 @@ export const BlockMenu = (props: Props) => {
icon: Trash2,
key: "delete",
label: "Delete",
onClick: (e) => {
e.preventDefault();
e.stopPropagation();

onClick: (_e) => {
// Execute the delete action
editor.chain().deleteSelection().focus().run();

setIsOpen(false);
},
},
{
Expand All @@ -150,10 +159,7 @@ export const BlockMenu = (props: Props) => {
isDisabled:
editor.state.selection.content().content.firstChild?.type.name === CORE_EXTENSIONS.IMAGE ||
editor.isActive(CORE_EXTENSIONS.CUSTOM_IMAGE),
onClick: (e) => {
e.preventDefault();
e.stopPropagation();

onClick: (_e) => {
try {
const { state } = editor;
const { selection } = state;
Expand Down Expand Up @@ -187,7 +193,6 @@ export const BlockMenu = (props: Props) => {
console.error(error.message);
}
}
setIsOpen(false);
},
},
];
Expand Down Expand Up @@ -225,7 +230,12 @@ export const BlockMenu = (props: Props) => {
key={item.key}
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"
onClick={item.onClick}
onClick={(e) => {
item.onClick(e);
e.preventDefault();
e.stopPropagation();
closeBlockMenu();
}}
disabled={item.isDisabled}
>
<item.icon className="h-3 w-3" />
Expand Down
1 change: 1 addition & 0 deletions packages/editor/src/core/extensions/utility.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type TActiveDropbarExtensions =
| CORE_EXTENSIONS.EMOJI
| CORE_EXTENSIONS.SLASH_COMMANDS
| CORE_EXTENSIONS.TABLE
| CORE_EXTENSIONS.SIDE_MENU
| TAdditionalActiveDropbarExtensions;

declare module "@tiptap/core" {
Expand Down
Loading