'use client' import { useEffect, useState, type ReactNode } from 'react' import type { Editor } from '@tiptap/react' import { AlignCenter, AlignLeft, AlignRight, Bold, Code2, Highlighter, Image as ImageIcon, IndentIncrease, Italic, Link2, List, ListOrdered, Palette, Strikethrough, Table2, Underline, } from 'lucide-react' import { Button } from '@/components/ui/button' import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuSeparator, DropdownMenuTrigger } from '@/components/ui/dropdown-menu' import { Input } from '@/components/ui/input' import { Popover, PopoverContent, PopoverTrigger } from '@/components/ui/popover' import { Separator } from '@/components/ui/separator' import { cn } from '@/lib/utils' import type { KriEditorFontFamilyOption, KriEditorToolbarConfig, KriEditorToolbarFeature, ToolbarState } from '../types' import { ToolbarButton } from './ToolbarButton' interface ToolbarProps { editor: Editor state: ToolbarState config?: KriEditorToolbarConfig className?: string canImageUpload: boolean onPickImage: () => void showHtmlToggle: boolean isHtmlView: boolean onToggleHtmlView: () => void onFormatHtml: () => void fontFamilyOptions: KriEditorFontFamilyOption[] onSetFontFamily: (value: string) => void onSetFontSize: (value: string) => void onSetTextColor: (value: string) => void onUnsetTextColor: () => void onSetHighlight: (value: string) => void onUnsetHighlight: () => void linkUrl: string onSetLink: (url: string) => void onUnsetLink: () => void onInsertTable: () => void onAddRow: () => void onAddColumn: () => void onDeleteTable: () => void customContent?: ReactNode } const PRESET_COLORS = [ '#111827', '#374151', '#6B7280', '#9CA3AF', '#D1D5DB', '#EF4444', '#F97316', '#F59E0B', '#84CC16', '#22C55E', '#06B6D4', '#3B82F6', '#6366F1', '#A855F7', '#EC4899', ] const HIGHLIGHT_COLORS = ['#FEF3C7', '#FDE68A', '#A7F3D0', '#BAE6FD', '#DDD6FE', '#FBCFE8', '#FFE4E6', '#E5E7EB', '#F5F5F4', '#FFFFFF'] function Swatch({ color }: { color: string | null }) { return } export function Toolbar({ editor, state, config, className, canImageUpload, onPickImage, showHtmlToggle, isHtmlView, onToggleHtmlView, onFormatHtml, fontFamilyOptions, onSetFontFamily, onSetFontSize, onSetTextColor, onUnsetTextColor, onSetHighlight, onUnsetHighlight, linkUrl, onSetLink, onUnsetLink, onInsertTable, onAddRow, onAddColumn, onDeleteTable, customContent, }: ToolbarProps) { const enabled = (feature: KriEditorToolbarFeature) => feature !== 'image' && feature !== 'table' && config?.[feature] !== false const showFontControls = enabled('fontFamily') || enabled('fontSize') const showTextStyle = enabled('bold') || enabled('italic') || enabled('underline') || enabled('strike') const showColors = enabled('textColor') || enabled('highlight') const showLists = enabled('bulletList') || enabled('orderedList') const [linkOpen, setLinkOpen] = useState(false) const [linkDraft, setLinkDraft] = useState(linkUrl) useEffect(() => { if (linkOpen) setLinkDraft(linkUrl) }, [linkOpen, linkUrl]) const applyLink = () => { const next = linkDraft.trim() if (!next) return onSetLink(next) setLinkOpen(false) } return (
{enabled('fontFamily') ? ( ) : null} {enabled('fontSize') ? ( ) : null} {showFontControls && showTextStyle ? : null} {enabled('bold') ? ( editor.chain().focus().toggleBold().run()} title="굵게"> ) : null} {enabled('italic') ? ( editor.chain().focus().toggleItalic().run()} title="기울임"> ) : null} {enabled('underline') ? ( editor.chain().focus().toggleUnderline().run()} title="밑줄"> ) : null} {enabled('strike') ? ( editor.chain().focus().toggleStrike().run()} title="취소선"> ) : null} {showTextStyle && showColors ? : null} {enabled('textColor') ? (
텍스트 색상
{PRESET_COLORS.map((color) => (
event.preventDefault()} onChange={(event) => onSetTextColor(event.target.value)} aria-label="사용자 지정 텍스트 색상" />
) : null} {enabled('highlight') ? (
하이라이트
{HIGHLIGHT_COLORS.map((color) => (
event.preventDefault()} onChange={(event) => onSetHighlight(event.target.value)} aria-label="사용자 지정 하이라이트" />
) : null} {showColors && enabled('align') ? : null} {enabled('align') ? ( <> editor.chain().focus().setTextAlign('left').run()} title="왼쪽 정렬"> editor.chain().focus().setTextAlign('center').run()} title="가운데 정렬"> editor.chain().focus().setTextAlign('right').run()} title="오른쪽 정렬"> ) : null} {enabled('align') && showLists ? : null} {enabled('bulletList') ? editor.chain().focus().toggleBulletList().run()} title="불릿"> : null} {enabled('orderedList') ? editor.chain().focus().toggleOrderedList().run()} title="번호"> : null} {showLists && (enabled('link') || enabled('image') || enabled('table')) ? : null} {enabled('link') ? (
{ event.preventDefault() applyLink() }} >
setLinkDraft(event.target.value)} placeholder="https://example.com" autoFocus />
{state.isLink ? ( ) : null}
) : null} {enabled('image') ? : null} {enabled('table') ? ( { event.preventDefault(); onInsertTable() }}>표 삽입 { event.preventDefault(); onAddRow() }} disabled={!state.inTable}>행 추가 { event.preventDefault(); onAddColumn() }} disabled={!state.inTable}>열 추가 { event.preventDefault(); onDeleteTable() }} disabled={!state.inTable} className="text-destructive">표 삭제 ) : null} {showHtmlToggle && enabled('html') ? ( <> {isHtmlView ? ( ) : null} ) : null} {customContent ? ( <> {customContent} ) : null}
) }