import React, { useEffect, useState, useCallback } from "react";
import { Check, ChevronDown } from "lucide-react";
// editor
import { EditorMenuItemNames, EditorRefApi } from "@plane/document-editor";
// ui
import { CustomMenu, Tooltip } from "@plane/ui";
// constants
import { TOOLBAR_ITEMS, TYPOGRAPHY_ITEMS, ToolbarMenuItem } from "@/constants/editor";
// helpers
import { cn } from "@/helpers/common.helper";
type Props = {
editorRef: EditorRefApi;
};
type ToolbarButtonProps = {
item: ToolbarMenuItem;
isActive: boolean;
executeCommand: (commandName: EditorMenuItemNames) => void;
};
const ToolbarButton: React.FC = React.memo((props) => {
const { item, isActive, executeCommand } = props;
return (
{item.name}
{item.shortcut && {item.shortcut.join(" + ")}}
}
>
);
});
ToolbarButton.displayName = "ToolbarButton";
const toolbarItems = TOOLBAR_ITEMS.document;
export const PageToolbar: React.FC = ({ editorRef }) => {
const [activeStates, setActiveStates] = useState>({});
const updateActiveStates = useCallback(() => {
const newActiveStates: Record = {};
Object.values(toolbarItems)
.flat()
.forEach((item) => {
newActiveStates[item.key] = editorRef.isMenuItemActive(item.key);
});
setActiveStates(newActiveStates);
}, [editorRef]);
useEffect(() => {
const unsubscribe = editorRef.onStateChange(updateActiveStates);
updateActiveStates();
return () => unsubscribe();
}, [editorRef, updateActiveStates]);
const activeTypography = TYPOGRAPHY_ITEMS.find((item) => editorRef.isMenuItemActive(item.key));
return (
{activeTypography?.name || "Text"}
}
className="pr-2"
placement="bottom-start"
closeOnSelect
maxHeight="lg"
>
{TYPOGRAPHY_ITEMS.map((item) => (
editorRef.executeMenuItemCommand(item.key)}
>
{item.name}
{activeTypography?.key === item.key && }
))}
{Object.keys(toolbarItems).map((key) => (
{toolbarItems[key].map((item) => (
))}
))}
);
};