'use client'; import { useState } from 'react'; import type { TElement } from '@udecode/plate'; import { cn } from '@udecode/cn'; import { toUnitLess } from '@udecode/plate-font'; import { FontSizePlugin } from '@udecode/plate-font/react'; import { HEADING_KEYS } from '@udecode/plate-heading'; import { useEditorPlugin, useEditorSelector } from '@udecode/plate/react'; import { Minus, Plus } from 'lucide-react'; import { Popover, PopoverContent, PopoverTrigger } from './popover'; import { ToolbarButton } from './toolbar'; const DEFAULT_FONT_SIZE = '16'; const FONT_SIZE_MAP = { [HEADING_KEYS.h1]: '36', [HEADING_KEYS.h2]: '24', [HEADING_KEYS.h3]: '20', } as const; const FONT_SIZES = [ '8', '9', '10', '12', '14', '16', '18', '24', '30', '36', '48', '60', '72', '96', ] as const; export function FontSizeToolbarButton() { const [inputValue, setInputValue] = useState(DEFAULT_FONT_SIZE); const [isFocused, setIsFocused] = useState(false); const { api, editor } = useEditorPlugin(FontSizePlugin); const cursorFontSize = useEditorSelector((editor) => { const fontSize = editor.api.marks()?.[FontSizePlugin.key]; if (fontSize) { return toUnitLess(fontSize as string); } const [block] = editor.api.block() || []; if (!block?.type) return DEFAULT_FONT_SIZE; return block.type in FONT_SIZE_MAP ? FONT_SIZE_MAP[block.type as keyof typeof FONT_SIZE_MAP] : DEFAULT_FONT_SIZE; }, []); const handleInputChange = () => { const newSize = toUnitLess(inputValue); if (Number.parseInt(newSize) < 1 || Number.parseInt(newSize) > 100) { editor.tf.focus(); return; } if (newSize !== toUnitLess(cursorFontSize)) { api.fontSize.setMark(`${newSize}px`); } editor.tf.focus(); }; const handleFontSizeChange = (delta: number) => { const newSize = Number(displayValue) + delta; api.fontSize.setMark(`${newSize}px`); editor.tf.focus(); }; const displayValue = isFocused ? inputValue : cursorFontSize; return (
handleFontSizeChange(-1)}> { setIsFocused(false); handleInputChange(); }} onChange={(e) => setInputValue(e.target.value)} onFocus={() => { setIsFocused(true); setInputValue(toUnitLess(cursorFontSize)); }} onKeyDown={(e) => { if (e.key === 'Enter') { e.preventDefault(); handleInputChange(); } }} data-plate-focus="true" type="text" /> e.preventDefault()} > {FONT_SIZES.map((size) => ( ))} handleFontSizeChange(1)}>
); }