import { Icon } from "@iconify/react" import { IconButton } from "@ivtui/base" import type { Editor } from "@tiptap/react" import { useEffect, useState } from "react" interface FontSizeControlProps { editor: Editor min?: number max?: number } export function FontSizeControl({ editor, min = 8, max = 72, }: React.PropsWithChildren) { const [fontSize, setFontSize] = useState(14) // Get current font size from editor useEffect(() => { const updateFontSize = () => { const { from, to } = editor.state.selection const marks = editor.state.doc.rangeHasMark(from, to, editor.schema.marks.textStyle) if (marks) { const textStyleMark = editor.getAttributes("textStyle") if (textStyleMark.fontSize) { const size = Number.parseInt(textStyleMark.fontSize.replace("px", ""), 10) if (!Number.isNaN(size)) { setFontSize(size) } } } } editor.on("selectionUpdate", updateFontSize) updateFontSize() return () => { editor.off("selectionUpdate", updateFontSize) } }, [editor]) const handleDecrease = (event: React.MouseEvent) => { event.preventDefault() event.stopPropagation() const newSize = Math.max(min, fontSize - 1) setFontSize(newSize) editor.chain().focus().setFontSize(`${newSize}px`).run() } const handleIncrease = (event: React.MouseEvent) => { event.preventDefault() event.stopPropagation() const newSize = Math.min(max, fontSize + 1) setFontSize(newSize) editor.chain().focus().setFontSize(`${newSize}px`).run() } return (
{fontSize} = max}>
) }