284 lines
12 KiB
TypeScript
284 lines
12 KiB
TypeScript
'use client'
|
|
|
|
import { useEffect, useState } from 'react'
|
|
import type { Editor } from '@tiptap/react'
|
|
import {
|
|
AlignCenter,
|
|
AlignLeft,
|
|
AlignRight,
|
|
Bold,
|
|
Code2,
|
|
Highlighter,
|
|
Image as ImageIcon,
|
|
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 { 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
|
|
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
|
|
}
|
|
|
|
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 <span className="ml-1 inline-block size-2 rounded-full border" style={{ backgroundColor: color ?? 'transparent' }} />
|
|
}
|
|
|
|
export function Toolbar({
|
|
editor,
|
|
state,
|
|
config,
|
|
className,
|
|
canImageUpload,
|
|
onPickImage,
|
|
showHtmlToggle,
|
|
isHtmlView,
|
|
onToggleHtmlView,
|
|
onSetFontSize,
|
|
onSetTextColor,
|
|
onUnsetTextColor,
|
|
onSetHighlight,
|
|
onUnsetHighlight,
|
|
linkUrl,
|
|
onSetLink,
|
|
onUnsetLink,
|
|
onInsertTable,
|
|
onAddRow,
|
|
onAddColumn,
|
|
onDeleteTable,
|
|
}: ToolbarProps) {
|
|
const enabled = (feature: KriEditorToolbarFeature) => config?.[feature] !== false
|
|
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 (
|
|
<div className={cn('flex items-center gap-1 overflow-x-auto whitespace-nowrap px-2 py-1', className)}>
|
|
{enabled('fontSize') ? (
|
|
<select
|
|
className="h-8 w-[72px] rounded-md border bg-background px-2 text-sm"
|
|
value={state.fontSize}
|
|
onChange={(event) => onSetFontSize(event.target.value)}
|
|
title="폰트 크기"
|
|
>
|
|
{['12px', '14px', '16px', '18px', '20px', '24px', '28px', '32px'].map((value) => (
|
|
<option key={value} value={value}>{value.replace('px', '')}</option>
|
|
))}
|
|
</select>
|
|
) : null}
|
|
|
|
{enabled('fontSize') && showTextStyle ? <Separator orientation="vertical" className="mx-1 h-6" /> : null}
|
|
|
|
{enabled('bold') ? (
|
|
<ToolbarButton active={state.isBold} disabled={!editor.can().chain().focus().toggleBold().run()} onClick={() => editor.chain().focus().toggleBold().run()} title="굵게">
|
|
<Bold className="size-4" />
|
|
</ToolbarButton>
|
|
) : null}
|
|
{enabled('italic') ? (
|
|
<ToolbarButton active={state.isItalic} disabled={!editor.can().chain().focus().toggleItalic().run()} onClick={() => editor.chain().focus().toggleItalic().run()} title="기울임">
|
|
<Italic className="size-4" />
|
|
</ToolbarButton>
|
|
) : null}
|
|
{enabled('underline') ? (
|
|
<ToolbarButton active={state.isUnderline} onClick={() => editor.chain().focus().toggleUnderline().run()} title="밑줄">
|
|
<Underline className="size-4" />
|
|
</ToolbarButton>
|
|
) : null}
|
|
{enabled('strike') ? (
|
|
<ToolbarButton active={state.isStrike} onClick={() => editor.chain().focus().toggleStrike().run()} title="취소선">
|
|
<Strikethrough className="size-4" />
|
|
</ToolbarButton>
|
|
) : null}
|
|
|
|
{showTextStyle && showColors ? <Separator orientation="vertical" className="mx-1 h-6" /> : null}
|
|
|
|
{enabled('textColor') ? (
|
|
<Popover>
|
|
<PopoverTrigger asChild>
|
|
<span>
|
|
<ToolbarButton title="텍스트 색상" active={Boolean(state.textColor)}>
|
|
<Palette className="size-4" /><Swatch color={state.textColor} />
|
|
</ToolbarButton>
|
|
</span>
|
|
</PopoverTrigger>
|
|
<PopoverContent className="w-56" align="start">
|
|
<div className="text-muted-foreground mb-2 text-xs">텍스트 색상</div>
|
|
<div className="grid grid-cols-5 gap-2">
|
|
{PRESET_COLORS.map((color) => (
|
|
<button key={color} type="button" className="size-6 rounded border" style={{ backgroundColor: color }} onMouseDown={(event) => event.preventDefault()} onClick={() => onSetTextColor(color)} aria-label={`색상 ${color}`} />
|
|
))}
|
|
</div>
|
|
<div className="mt-3 flex items-center gap-2">
|
|
<input type="color" className="h-8 w-10 cursor-pointer rounded-md border p-1" onMouseDown={(event) => event.preventDefault()} onChange={(event) => onSetTextColor(event.target.value)} aria-label="사용자 지정 텍스트 색상" />
|
|
<button type="button" className="text-muted-foreground text-xs underline" onMouseDown={(event) => event.preventDefault()} onClick={onUnsetTextColor}>색상 제거</button>
|
|
</div>
|
|
</PopoverContent>
|
|
</Popover>
|
|
) : null}
|
|
|
|
{enabled('highlight') ? (
|
|
<Popover>
|
|
<PopoverTrigger asChild>
|
|
<span>
|
|
<ToolbarButton title="하이라이트" active={Boolean(state.highlightColor)}>
|
|
<Highlighter className="size-4" /><Swatch color={state.highlightColor} />
|
|
</ToolbarButton>
|
|
</span>
|
|
</PopoverTrigger>
|
|
<PopoverContent className="w-56" align="start">
|
|
<div className="text-muted-foreground mb-2 text-xs">하이라이트</div>
|
|
<div className="grid grid-cols-5 gap-2">
|
|
{HIGHLIGHT_COLORS.map((color) => (
|
|
<button key={color} type="button" className="size-6 rounded border" style={{ backgroundColor: color }} onMouseDown={(event) => event.preventDefault()} onClick={() => onSetHighlight(color)} aria-label={`하이라이트 ${color}`} />
|
|
))}
|
|
</div>
|
|
<div className="mt-3 flex items-center gap-2">
|
|
<input type="color" className="h-8 w-10 cursor-pointer rounded-md border p-1" onMouseDown={(event) => event.preventDefault()} onChange={(event) => onSetHighlight(event.target.value)} aria-label="사용자 지정 하이라이트" />
|
|
<button type="button" className="text-muted-foreground text-xs underline" onMouseDown={(event) => event.preventDefault()} onClick={onUnsetHighlight}>하이라이트 제거</button>
|
|
</div>
|
|
</PopoverContent>
|
|
</Popover>
|
|
) : null}
|
|
|
|
{showColors && enabled('align') ? <Separator orientation="vertical" className="mx-1 h-6" /> : null}
|
|
|
|
{enabled('align') ? (
|
|
<>
|
|
<ToolbarButton active={state.align === 'left'} onClick={() => editor.chain().focus().setTextAlign('left').run()} title="왼쪽 정렬"><AlignLeft className="size-4" /></ToolbarButton>
|
|
<ToolbarButton active={state.align === 'center'} onClick={() => editor.chain().focus().setTextAlign('center').run()} title="가운데 정렬"><AlignCenter className="size-4" /></ToolbarButton>
|
|
<ToolbarButton active={state.align === 'right'} onClick={() => editor.chain().focus().setTextAlign('right').run()} title="오른쪽 정렬"><AlignRight className="size-4" /></ToolbarButton>
|
|
</>
|
|
) : null}
|
|
|
|
{enabled('align') && showLists ? <Separator orientation="vertical" className="mx-1 h-6" /> : null}
|
|
|
|
{enabled('bulletList') ? <ToolbarButton active={state.isBulletList} onClick={() => editor.chain().focus().toggleBulletList().run()} title="불릿"><List className="size-4" /></ToolbarButton> : null}
|
|
{enabled('orderedList') ? <ToolbarButton active={state.isOrderedList} onClick={() => editor.chain().focus().toggleOrderedList().run()} title="번호"><ListOrdered className="size-4" /></ToolbarButton> : null}
|
|
|
|
{showLists && (enabled('link') || enabled('image') || enabled('table')) ? <Separator orientation="vertical" className="mx-1 h-6" /> : null}
|
|
|
|
{enabled('link') ? (
|
|
<Popover open={linkOpen} onOpenChange={setLinkOpen}>
|
|
<PopoverTrigger asChild>
|
|
<span>
|
|
<ToolbarButton active={state.isLink} title={state.isLink ? '링크 수정' : '링크 삽입'}>
|
|
<Link2 className="size-4" />
|
|
</ToolbarButton>
|
|
</span>
|
|
</PopoverTrigger>
|
|
<PopoverContent className="w-80" align="start">
|
|
<form
|
|
className="space-y-3"
|
|
onSubmit={(event) => {
|
|
event.preventDefault()
|
|
applyLink()
|
|
}}
|
|
>
|
|
<div className="space-y-1.5">
|
|
<label htmlFor="kri-editor-link-url" className="text-sm font-medium">링크 URL</label>
|
|
<Input
|
|
id="kri-editor-link-url"
|
|
value={linkDraft}
|
|
onChange={(event) => setLinkDraft(event.target.value)}
|
|
placeholder="https://example.com"
|
|
autoFocus
|
|
/>
|
|
</div>
|
|
<div className="flex justify-end gap-2">
|
|
{state.isLink ? (
|
|
<Button
|
|
type="button"
|
|
variant="outline"
|
|
size="sm"
|
|
onClick={() => {
|
|
onUnsetLink()
|
|
setLinkOpen(false)
|
|
}}
|
|
>
|
|
링크 해제
|
|
</Button>
|
|
) : null}
|
|
<Button type="submit" size="sm" disabled={!linkDraft.trim()}>적용</Button>
|
|
</div>
|
|
</form>
|
|
</PopoverContent>
|
|
</Popover>
|
|
) : null}
|
|
{enabled('image') ? <ToolbarButton title={canImageUpload ? '이미지 업로드' : 'onImageUpload 미설정'} disabled={!canImageUpload} onClick={onPickImage}><ImageIcon className="size-4" /></ToolbarButton> : null}
|
|
|
|
{enabled('table') ? (
|
|
<DropdownMenu>
|
|
<DropdownMenuTrigger asChild><span><ToolbarButton title="표"><Table2 className="size-4" /></ToolbarButton></span></DropdownMenuTrigger>
|
|
<DropdownMenuContent align="end" className="w-44">
|
|
<DropdownMenuItem onSelect={(event) => { event.preventDefault(); onInsertTable() }}>표 삽입</DropdownMenuItem>
|
|
<DropdownMenuSeparator />
|
|
<DropdownMenuItem onSelect={(event) => { event.preventDefault(); onAddRow() }} disabled={!state.inTable}>행 추가</DropdownMenuItem>
|
|
<DropdownMenuItem onSelect={(event) => { event.preventDefault(); onAddColumn() }} disabled={!state.inTable}>열 추가</DropdownMenuItem>
|
|
<DropdownMenuSeparator />
|
|
<DropdownMenuItem onSelect={(event) => { event.preventDefault(); onDeleteTable() }} disabled={!state.inTable} className="text-destructive">표 삭제</DropdownMenuItem>
|
|
</DropdownMenuContent>
|
|
</DropdownMenu>
|
|
) : null}
|
|
|
|
{showHtmlToggle && enabled('html') ? (
|
|
<>
|
|
<Separator orientation="vertical" className="mx-1 h-6" />
|
|
<ToolbarButton active={isHtmlView} title={isHtmlView ? 'WYSIWYG로 전환' : 'HTML 편집'} onClick={onToggleHtmlView}><Code2 className="size-4" /></ToolbarButton>
|
|
</>
|
|
) : null}
|
|
</div>
|
|
)
|
|
}
|