260728_05_ENH: 에디터 동적 툴바 사용가능하도록 수정 #16
@@ -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 <KriEditorHtmlOnly {...props} mode={mode} />;
|
||||
}
|
||||
|
||||
if(mode === "sms") {
|
||||
return <KriEditorSms {...props} mode={mode} />;
|
||||
return <KriEditorSms {...props} mode={mode} />
|
||||
}
|
||||
|
||||
return <KriEditorWysiwyg {...props} mode={mode} />;
|
||||
|
||||
@@ -117,7 +117,7 @@ export function KriEditorHtmlOnly({
|
||||
})
|
||||
} catch (error) {
|
||||
if (onImageUploadError) onImageUploadError(error)
|
||||
else console.error(error)
|
||||
else toast.error('이미지 업로드에 실패했습니다.')
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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<HTMLTextAreaElement | null>(null)
|
||||
@@ -79,6 +79,7 @@ export function KriEditorSms({
|
||||
|
||||
return (
|
||||
<div className={cn('mx-auto w-full max-w-[320px]', className)}>
|
||||
{label ? <div className="mb-2 text-sm font-semibold">{label}</div> : null}
|
||||
<div className="relative h-[600px] w-full overflow-hidden rounded-[40px] border-8 border-gray-800 bg-white shadow-2xl">
|
||||
<div className="absolute top-0 left-1/2 z-10 h-6 w-32 -translate-x-1/2 rounded-b-2xl bg-gray-800" />
|
||||
<div className="flex h-full flex-col bg-gray-50 px-4 pt-8 pb-4">
|
||||
@@ -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}
|
||||
/>
|
||||
</div>
|
||||
{label ? <div className="my-2 text-sm font-semibold text-red-600">{label}</div> : null}
|
||||
{byteSize > maxBytes ? (
|
||||
<p className="mt-2 px-2 text-center text-xs font-medium text-red-600">
|
||||
최대 용량을 {byteSize - maxBytes}byte 초과했습니다.
|
||||
|
||||
@@ -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<HTMLInputElement | null>(null);
|
||||
const htmlTextareaRef = useRef<HTMLTextAreaElement | null>(null);
|
||||
const htmlTi = useTextareaInsert(htmlTextareaRef);
|
||||
const { insertText: insertTextareaText } = htmlTi;
|
||||
|
||||
const [view, setView] = useState<ViewMode>("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<KriEditorCommand>(
|
||||
() => ({
|
||||
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 })}
|
||||
/>
|
||||
</div>
|
||||
) : null}
|
||||
|
||||
@@ -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'
|
||||
|
||||
@@ -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({
|
||||
<ToolbarButton active={isHtmlView} title={isHtmlView ? 'WYSIWYG로 전환' : 'HTML 편집'} onClick={onToggleHtmlView}><Code2 className="size-4" /></ToolbarButton>
|
||||
</>
|
||||
) : null}
|
||||
{customContent ? (
|
||||
<>
|
||||
<Separator orientation="vertical" className="mx-1 h-6" />
|
||||
{customContent}
|
||||
</>
|
||||
) : null}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
};
|
||||
|
||||
@@ -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<KriEditorUploadResult>
|
||||
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 = {
|
||||
|
||||
Reference in New Issue
Block a user