diff --git a/src/components/kri-editor/KriEditor.tsx b/src/components/kri-editor/KriEditor.tsx index c5c5dcc..df47653 100644 --- a/src/components/kri-editor/KriEditor.tsx +++ b/src/components/kri-editor/KriEditor.tsx @@ -5,19 +5,19 @@ import { KriEditorHtmlOnly } from "./KriEditorHtmlOnly"; import { KriEditorWysiwyg } from "./KriEditorWysiwyg"; import {KriEditorSms} from "./KriEditorSms"; -function resolveMode(mode?: KriEditorMode): KriEditorMode { - return mode ?? "wysiwyg"; +function resolveMode(mode?: KriEditorMode, editorMode?: KriEditorMode): KriEditorMode { + return mode ?? editorMode ?? "wysiwyg"; } export function KriEditor(props: KriEditorProps) { - const mode = resolveMode(props.mode); + const mode = resolveMode(props.mode, props.editorMode); if (mode === "html") { return ; } if(mode === "sms") { - return ; + return } return ; diff --git a/src/components/kri-editor/KriEditorHtmlOnly.tsx b/src/components/kri-editor/KriEditorHtmlOnly.tsx index f4b3c48..8475fda 100644 --- a/src/components/kri-editor/KriEditorHtmlOnly.tsx +++ b/src/components/kri-editor/KriEditorHtmlOnly.tsx @@ -117,7 +117,7 @@ export function KriEditorHtmlOnly({ }) } catch (error) { if (onImageUploadError) onImageUploadError(error) - else console.error(error) + else toast.error('이미지 업로드에 실패했습니다.') } } diff --git a/src/components/kri-editor/KriEditorSms.tsx b/src/components/kri-editor/KriEditorSms.tsx index d246e1d..91a2365 100644 --- a/src/components/kri-editor/KriEditorSms.tsx +++ b/src/components/kri-editor/KriEditorSms.tsx @@ -23,9 +23,9 @@ export function KriEditorSms({ changeSmsType, onEditorReady, onCommandReady, + isSmsReadOnly = false, smsMaxBytes = 90, lmsMaxBytes = 2000, - isSmsReadOnly = false, }: SmsEditorProps) { const [text, setText] = React.useState(value || '') const textareaRef = React.useRef(null) @@ -79,6 +79,7 @@ export function KriEditorSms({ return (
+ {label ?
{label}
: null}
@@ -94,18 +95,17 @@ export function KriEditorSms({ ref={textareaRef} className="w-full flex-1 resize-none bg-transparent p-4 text-sm leading-relaxed text-gray-700 outline-none" value={text} - readOnly={isSmsReadOnly} onChange={(event) => { const next = event.target.value if (calculateBytes(next) <= lmsMaxBytes) commitText(next) }} onBlur={() => flushChange(latestTextRef.current)} placeholder={placeholder} + readOnly={isSmsReadOnly} style={{ minHeight: smsTitle ? '320px' : '360px' }} {...textareaInsert.bindSelectionHandlers} />
- {label ?
{label}
: null} {byteSize > maxBytes ? (

최대 용량을 {byteSize - maxBytes}byte 초과했습니다. diff --git a/src/components/kri-editor/KriEditorWysiwyg.tsx b/src/components/kri-editor/KriEditorWysiwyg.tsx index 40c3d90..180323d 100644 --- a/src/components/kri-editor/KriEditorWysiwyg.tsx +++ b/src/components/kri-editor/KriEditorWysiwyg.tsx @@ -20,7 +20,7 @@ import TableCell from "@tiptap/extension-table-cell"; import { toast } from "sonner"; import { cn } from "@/lib/utils"; -import type { KriEditorProps, ToolbarState } from "./types"; +import type { KriEditorCommand, KriEditorProps, ToolbarState } from "./types"; import { resolveUploadUrl } from "./types"; import { FontSize } from "./extensions/font-size"; import { Div } from "./extensions/div"; @@ -101,6 +101,7 @@ export function KriEditorWysiwyg(props: KriEditorProps) { const fileRef = useRef(null); const htmlTextareaRef = useRef(null); const htmlTi = useTextareaInsert(htmlTextareaRef); + const { insertText: insertTextareaText } = htmlTi; const [view, setView] = useState("wysiwyg"); const [previewSource, setPreviewSource] = useState(value ?? ""); @@ -407,6 +408,7 @@ export function KriEditorWysiwyg(props: KriEditorProps) { if(onImageUploadError){ onImageUploadError(err); }else{ + toast.error("이미지 업로드에 실패했습니다."); console.error(err); } } @@ -509,6 +511,38 @@ export function KriEditorWysiwyg(props: KriEditorProps) { }; }, [editor, flushChange]); + const command = useMemo( + () => ({ + focus: () => { + if (viewRef.current === "html") htmlTextareaRef.current?.focus(); + else editor?.chain().focus().run(); + }, + insertText: (text: string) => { + if (viewRef.current === "html") { + const current = htmlDraftRef.current ?? ""; + insertTextareaText( + text, + current, + (next) => { + htmlDraftRef.current = next; + if (htmlTextareaRef.current) htmlTextareaRef.current.value = next; + }, + (next) => { + flushChange(next); + } + ); + return; + } + + if (!editor) return; + restoreSelection(); + editor.chain().focus().insertContent(text).run(); + flushChange(editor.getHTML()); + }, + }), + [editor, restoreSelection, flushChange, insertTextareaText] + ); + useEffect(() => { if (!props.onCommandReady) return; @@ -518,43 +552,10 @@ export function KriEditorWysiwyg(props: KriEditorProps) { return; } - props.onCommandReady({ - focus: () => { - if (viewRef.current === "html") htmlTextareaRef.current?.focus(); - else editor.chain().focus().run(); - }, - - insertText: (placeholder: string) => { - // 1) HTML textarea 모드 - if (viewRef.current === "html") { - const current = htmlDraftRef.current ?? ""; - - htmlTi.insertText( - placeholder, - current, - (next) => { - htmlDraftRef.current = next; - // uncontrolled textarea이므로 DOM value를 직접 갱신 - if (htmlTextareaRef.current) htmlTextareaRef.current.value = next; - }, - (next) => { - // 삽입은 즉시 커밋(저장 버튼 클릭 직후 반영 누락 방지) - flushChange(next); - } - ); - - return; - } - - // 2) WYSIWYG(tiptap) 모드 - restoreSelection(); - editor.chain().focus().insertContent(placeholder).run(); - flushChange(editor.getHTML()); - }, - }); + props.onCommandReady(command); return () => props.onCommandReady?.(null); - }, [editor, props.onCommandReady, restoreSelection, flushChange, htmlTi]); + }, [command, editor, props.onCommandReady]); const previewHtml = useMemo(() => { return sanitizeHtmlOpt ? sanitizeHtml(previewSource) : previewSource; @@ -597,6 +598,7 @@ export function KriEditorWysiwyg(props: KriEditorProps) { onAddRow={addRow} onAddColumn={addColumn} onDeleteTable={deleteTable} + customContent={props.renderToolbarEnd?.({ editor, command, view })} />

) : null} diff --git a/src/components/kri-editor/index.ts b/src/components/kri-editor/index.ts index afd4073..86e0a7d 100644 --- a/src/components/kri-editor/index.ts +++ b/src/components/kri-editor/index.ts @@ -2,12 +2,15 @@ export { KriEditor } from './KriEditor' export { KriEditorHtmlOnly } from './KriEditorHtmlOnly' export { KriEditorSms } from './KriEditorSms' export { KriEditorWysiwyg } from './KriEditorWysiwyg' +export { ToolbarButton as KriEditorToolbarButton } from './toolbar/ToolbarButton' export type { KriEditorCommand, KriEditorMode, KriEditorProps, KriEditorToolbarConfig, + KriEditorToolbarContext, KriEditorToolbarFeature, KriEditorUploadResult, ToolbarState, } from './types' +export type { KriEditorToolbarButtonProps } from './toolbar/ToolbarButton' diff --git a/src/components/kri-editor/toolbar/Toolbar.tsx b/src/components/kri-editor/toolbar/Toolbar.tsx index eb97c25..0b87723 100644 --- a/src/components/kri-editor/toolbar/Toolbar.tsx +++ b/src/components/kri-editor/toolbar/Toolbar.tsx @@ -1,6 +1,6 @@ 'use client' -import { useEffect, useState } from 'react' +import { useEffect, useState, type ReactNode } from 'react' import type { Editor } from '@tiptap/react' import { AlignCenter, @@ -50,6 +50,7 @@ interface ToolbarProps { onAddRow: () => void onAddColumn: () => void onDeleteTable: () => void + customContent?: ReactNode } const PRESET_COLORS = [ @@ -85,6 +86,7 @@ export function Toolbar({ onAddRow, onAddColumn, onDeleteTable, + customContent, }: ToolbarProps) { const enabled = (feature: KriEditorToolbarFeature) => config?.[feature] !== false const showTextStyle = enabled('bold') || enabled('italic') || enabled('underline') || enabled('strike') @@ -278,6 +280,12 @@ export function Toolbar({ ) : null} + {customContent ? ( + <> + + {customContent} + + ) : null}
) } diff --git a/src/components/kri-editor/toolbar/ToolbarButton.tsx b/src/components/kri-editor/toolbar/ToolbarButton.tsx index 4b28e37..833e728 100644 --- a/src/components/kri-editor/toolbar/ToolbarButton.tsx +++ b/src/components/kri-editor/toolbar/ToolbarButton.tsx @@ -4,7 +4,7 @@ import type { ReactNode, MouseEvent } from "react"; import { Button } from "@/components/ui/button"; import { cn } from "@/lib/utils"; -type Props = { +export type KriEditorToolbarButtonProps = { active?: boolean; disabled?: boolean; title?: string; @@ -13,7 +13,7 @@ type Props = { className?: string; }; -export function ToolbarButton({ active, disabled, title, onClick, children, className }: Props) { +export function ToolbarButton({ active, disabled, title, onClick, children, className }: KriEditorToolbarButtonProps) { const onMouseDown = (e: MouseEvent) => { e.preventDefault(); }; diff --git a/src/components/kri-editor/types.ts b/src/components/kri-editor/types.ts index 4709337..ed055a4 100644 --- a/src/components/kri-editor/types.ts +++ b/src/components/kri-editor/types.ts @@ -1,4 +1,5 @@ import type { Editor } from '@tiptap/react' +import type { ReactNode } from 'react' export type KriEditorMode = 'wysiwyg' | 'both' | 'html' | 'sms' export type KriEditorUploadResult = string | { url: string } @@ -26,8 +27,13 @@ export type KriEditorCommand = { insertText: (text: string) => void } +export type KriEditorToolbarContext = { + editor: Editor + command: KriEditorCommand + view: 'wysiwyg' | 'html' +} + export type KriEditorProps = { - isSmsReadOnly?: boolean onEditorReady?: (editor: Editor | HTMLTextAreaElement | null) => void onCommandReady?: (command: KriEditorCommand | null) => void label?: string @@ -38,6 +44,8 @@ export type KriEditorProps = { showPreview?: boolean previewClassName?: string mode?: KriEditorMode + /** @deprecated mode를 사용하세요. */ + editorMode?: KriEditorMode onImageUpload?: (file: File) => Promise maxImageBytes?: number imageMaxWidth?: number @@ -51,9 +59,11 @@ export type KriEditorProps = { debounceMs?: number smsTitle?: string changeSmsType?: (type: 'SMS' | 'LMS') => void + isSmsReadOnly?: boolean smsMaxBytes?: number lmsMaxBytes?: number toolbar?: KriEditorToolbarConfig | false + renderToolbarEnd?: (context: KriEditorToolbarContext) => ReactNode } export type ToolbarState = {