import type { ElementFormatType } from "lexical"; import type { ReactNode } from "react"; import { createContext, useCallback, useContext, useMemo, useState } from "react"; export const blockTypes = [ "h1", "h2", "h3", "paragraph", "number", "bullet", "check", "quote", ] as const; export type BlockType = (typeof blockTypes)[number]; export const elementFormats = ["center", "end", "justify", "left", "right", "start"]; const initialToolbarState = { canUndo: false, canRedo: false, isBold: false, isItalic: false, isUnderline: false, isLink: false, blockType: "paragraph" as BlockType, elementFormat: "left" as ElementFormatType, }; type ToolbarState = typeof initialToolbarState; type ToolbarStateKey = keyof ToolbarState; type ToolbarStateValue = ToolbarState[Key]; type ContextShape = { toolbarState: ToolbarState; updateToolbarState(key: Key, value: ToolbarStateValue): void; }; const Context = createContext(undefined); export const ToolbarContext = ({ children }: { children: ReactNode }) => { const [toolbarState, setToolbarState] = useState(initialToolbarState); const updateToolbarState = useCallback( (key: Key, value: ToolbarStateValue) => { setToolbarState((prev) => ({ ...prev, [key]: value, })); }, [], ); const contextValue = useMemo(() => { return { toolbarState, updateToolbarState, }; }, [toolbarState, updateToolbarState]); return {children}; }; export const useToolbarState = () => { const context = useContext(Context); if (context === undefined) { throw new Error("useToolbarState must be used within a ToolbarProvider"); } return context; };