260728_05_ENH: 에디터 동적 툴바 사용가능하도록 수정 #16
@@ -5,19 +5,19 @@ import { KriEditorHtmlOnly } from "./KriEditorHtmlOnly";
|
|||||||
import { KriEditorWysiwyg } from "./KriEditorWysiwyg";
|
import { KriEditorWysiwyg } from "./KriEditorWysiwyg";
|
||||||
import {KriEditorSms} from "./KriEditorSms";
|
import {KriEditorSms} from "./KriEditorSms";
|
||||||
|
|
||||||
function resolveMode(mode?: KriEditorMode): KriEditorMode {
|
function resolveMode(mode?: KriEditorMode, editorMode?: KriEditorMode): KriEditorMode {
|
||||||
return mode ?? "wysiwyg";
|
return mode ?? editorMode ?? "wysiwyg";
|
||||||
}
|
}
|
||||||
|
|
||||||
export function KriEditor(props: KriEditorProps) {
|
export function KriEditor(props: KriEditorProps) {
|
||||||
const mode = resolveMode(props.mode);
|
const mode = resolveMode(props.mode, props.editorMode);
|
||||||
|
|
||||||
if (mode === "html") {
|
if (mode === "html") {
|
||||||
return <KriEditorHtmlOnly {...props} mode={mode} />;
|
return <KriEditorHtmlOnly {...props} mode={mode} />;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(mode === "sms") {
|
if(mode === "sms") {
|
||||||
return <KriEditorSms {...props} mode={mode} />;
|
return <KriEditorSms {...props} mode={mode} />
|
||||||
}
|
}
|
||||||
|
|
||||||
return <KriEditorWysiwyg {...props} mode={mode} />;
|
return <KriEditorWysiwyg {...props} mode={mode} />;
|
||||||
|
|||||||
@@ -117,7 +117,7 @@ export function KriEditorHtmlOnly({
|
|||||||
})
|
})
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
if (onImageUploadError) onImageUploadError(error)
|
if (onImageUploadError) onImageUploadError(error)
|
||||||
else console.error(error)
|
else toast.error('이미지 업로드에 실패했습니다.')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -23,9 +23,9 @@ export function KriEditorSms({
|
|||||||
changeSmsType,
|
changeSmsType,
|
||||||
onEditorReady,
|
onEditorReady,
|
||||||
onCommandReady,
|
onCommandReady,
|
||||||
|
isSmsReadOnly = false,
|
||||||
smsMaxBytes = 90,
|
smsMaxBytes = 90,
|
||||||
lmsMaxBytes = 2000,
|
lmsMaxBytes = 2000,
|
||||||
isSmsReadOnly = false,
|
|
||||||
}: SmsEditorProps) {
|
}: SmsEditorProps) {
|
||||||
const [text, setText] = React.useState(value || '')
|
const [text, setText] = React.useState(value || '')
|
||||||
const textareaRef = React.useRef<HTMLTextAreaElement | null>(null)
|
const textareaRef = React.useRef<HTMLTextAreaElement | null>(null)
|
||||||
@@ -79,6 +79,7 @@ export function KriEditorSms({
|
|||||||
|
|
||||||
return (
|
return (
|
||||||
<div className={cn('mx-auto w-full max-w-[320px]', className)}>
|
<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="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="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">
|
<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}
|
ref={textareaRef}
|
||||||
className="w-full flex-1 resize-none bg-transparent p-4 text-sm leading-relaxed text-gray-700 outline-none"
|
className="w-full flex-1 resize-none bg-transparent p-4 text-sm leading-relaxed text-gray-700 outline-none"
|
||||||
value={text}
|
value={text}
|
||||||
readOnly={isSmsReadOnly}
|
|
||||||
onChange={(event) => {
|
onChange={(event) => {
|
||||||
const next = event.target.value
|
const next = event.target.value
|
||||||
if (calculateBytes(next) <= lmsMaxBytes) commitText(next)
|
if (calculateBytes(next) <= lmsMaxBytes) commitText(next)
|
||||||
}}
|
}}
|
||||||
onBlur={() => flushChange(latestTextRef.current)}
|
onBlur={() => flushChange(latestTextRef.current)}
|
||||||
placeholder={placeholder}
|
placeholder={placeholder}
|
||||||
|
readOnly={isSmsReadOnly}
|
||||||
style={{ minHeight: smsTitle ? '320px' : '360px' }}
|
style={{ minHeight: smsTitle ? '320px' : '360px' }}
|
||||||
{...textareaInsert.bindSelectionHandlers}
|
{...textareaInsert.bindSelectionHandlers}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
{label ? <div className="my-2 text-sm font-semibold text-red-600">{label}</div> : null}
|
|
||||||
{byteSize > maxBytes ? (
|
{byteSize > maxBytes ? (
|
||||||
<p className="mt-2 px-2 text-center text-xs font-medium text-red-600">
|
<p className="mt-2 px-2 text-center text-xs font-medium text-red-600">
|
||||||
최대 용량을 {byteSize - maxBytes}byte 초과했습니다.
|
최대 용량을 {byteSize - maxBytes}byte 초과했습니다.
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ import TableCell from "@tiptap/extension-table-cell";
|
|||||||
import { toast } from "sonner";
|
import { toast } from "sonner";
|
||||||
import { cn } from "@/lib/utils";
|
import { cn } from "@/lib/utils";
|
||||||
|
|
||||||
import type { KriEditorProps, ToolbarState } from "./types";
|
import type { KriEditorCommand, KriEditorProps, ToolbarState } from "./types";
|
||||||
import { resolveUploadUrl } from "./types";
|
import { resolveUploadUrl } from "./types";
|
||||||
import { FontSize } from "./extensions/font-size";
|
import { FontSize } from "./extensions/font-size";
|
||||||
import { Div } from "./extensions/div";
|
import { Div } from "./extensions/div";
|
||||||
@@ -101,6 +101,7 @@ export function KriEditorWysiwyg(props: KriEditorProps) {
|
|||||||
const fileRef = useRef<HTMLInputElement | null>(null);
|
const fileRef = useRef<HTMLInputElement | null>(null);
|
||||||
const htmlTextareaRef = useRef<HTMLTextAreaElement | null>(null);
|
const htmlTextareaRef = useRef<HTMLTextAreaElement | null>(null);
|
||||||
const htmlTi = useTextareaInsert(htmlTextareaRef);
|
const htmlTi = useTextareaInsert(htmlTextareaRef);
|
||||||
|
const { insertText: insertTextareaText } = htmlTi;
|
||||||
|
|
||||||
const [view, setView] = useState<ViewMode>("wysiwyg");
|
const [view, setView] = useState<ViewMode>("wysiwyg");
|
||||||
const [previewSource, setPreviewSource] = useState(value ?? "");
|
const [previewSource, setPreviewSource] = useState(value ?? "");
|
||||||
@@ -407,6 +408,7 @@ export function KriEditorWysiwyg(props: KriEditorProps) {
|
|||||||
if(onImageUploadError){
|
if(onImageUploadError){
|
||||||
onImageUploadError(err);
|
onImageUploadError(err);
|
||||||
}else{
|
}else{
|
||||||
|
toast.error("이미지 업로드에 실패했습니다.");
|
||||||
console.error(err);
|
console.error(err);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -509,6 +511,38 @@ export function KriEditorWysiwyg(props: KriEditorProps) {
|
|||||||
};
|
};
|
||||||
}, [editor, flushChange]);
|
}, [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(() => {
|
useEffect(() => {
|
||||||
if (!props.onCommandReady) return;
|
if (!props.onCommandReady) return;
|
||||||
|
|
||||||
@@ -518,43 +552,10 @@ export function KriEditorWysiwyg(props: KriEditorProps) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
props.onCommandReady({
|
props.onCommandReady(command);
|
||||||
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());
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
return () => props.onCommandReady?.(null);
|
return () => props.onCommandReady?.(null);
|
||||||
}, [editor, props.onCommandReady, restoreSelection, flushChange, htmlTi]);
|
}, [command, editor, props.onCommandReady]);
|
||||||
|
|
||||||
const previewHtml = useMemo(() => {
|
const previewHtml = useMemo(() => {
|
||||||
return sanitizeHtmlOpt ? sanitizeHtml(previewSource) : previewSource;
|
return sanitizeHtmlOpt ? sanitizeHtml(previewSource) : previewSource;
|
||||||
@@ -597,6 +598,7 @@ export function KriEditorWysiwyg(props: KriEditorProps) {
|
|||||||
onAddRow={addRow}
|
onAddRow={addRow}
|
||||||
onAddColumn={addColumn}
|
onAddColumn={addColumn}
|
||||||
onDeleteTable={deleteTable}
|
onDeleteTable={deleteTable}
|
||||||
|
customContent={props.renderToolbarEnd?.({ editor, command, view })}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
) : null}
|
) : null}
|
||||||
|
|||||||
@@ -2,12 +2,15 @@ export { KriEditor } from './KriEditor'
|
|||||||
export { KriEditorHtmlOnly } from './KriEditorHtmlOnly'
|
export { KriEditorHtmlOnly } from './KriEditorHtmlOnly'
|
||||||
export { KriEditorSms } from './KriEditorSms'
|
export { KriEditorSms } from './KriEditorSms'
|
||||||
export { KriEditorWysiwyg } from './KriEditorWysiwyg'
|
export { KriEditorWysiwyg } from './KriEditorWysiwyg'
|
||||||
|
export { ToolbarButton as KriEditorToolbarButton } from './toolbar/ToolbarButton'
|
||||||
export type {
|
export type {
|
||||||
KriEditorCommand,
|
KriEditorCommand,
|
||||||
KriEditorMode,
|
KriEditorMode,
|
||||||
KriEditorProps,
|
KriEditorProps,
|
||||||
KriEditorToolbarConfig,
|
KriEditorToolbarConfig,
|
||||||
|
KriEditorToolbarContext,
|
||||||
KriEditorToolbarFeature,
|
KriEditorToolbarFeature,
|
||||||
KriEditorUploadResult,
|
KriEditorUploadResult,
|
||||||
ToolbarState,
|
ToolbarState,
|
||||||
} from './types'
|
} from './types'
|
||||||
|
export type { KriEditorToolbarButtonProps } from './toolbar/ToolbarButton'
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
'use client'
|
'use client'
|
||||||
|
|
||||||
import { useEffect, useState } from 'react'
|
import { useEffect, useState, type ReactNode } from 'react'
|
||||||
import type { Editor } from '@tiptap/react'
|
import type { Editor } from '@tiptap/react'
|
||||||
import {
|
import {
|
||||||
AlignCenter,
|
AlignCenter,
|
||||||
@@ -50,6 +50,7 @@ interface ToolbarProps {
|
|||||||
onAddRow: () => void
|
onAddRow: () => void
|
||||||
onAddColumn: () => void
|
onAddColumn: () => void
|
||||||
onDeleteTable: () => void
|
onDeleteTable: () => void
|
||||||
|
customContent?: ReactNode
|
||||||
}
|
}
|
||||||
|
|
||||||
const PRESET_COLORS = [
|
const PRESET_COLORS = [
|
||||||
@@ -85,6 +86,7 @@ export function Toolbar({
|
|||||||
onAddRow,
|
onAddRow,
|
||||||
onAddColumn,
|
onAddColumn,
|
||||||
onDeleteTable,
|
onDeleteTable,
|
||||||
|
customContent,
|
||||||
}: ToolbarProps) {
|
}: ToolbarProps) {
|
||||||
const enabled = (feature: KriEditorToolbarFeature) => config?.[feature] !== false
|
const enabled = (feature: KriEditorToolbarFeature) => config?.[feature] !== false
|
||||||
const showTextStyle = enabled('bold') || enabled('italic') || enabled('underline') || enabled('strike')
|
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>
|
<ToolbarButton active={isHtmlView} title={isHtmlView ? 'WYSIWYG로 전환' : 'HTML 편집'} onClick={onToggleHtmlView}><Code2 className="size-4" /></ToolbarButton>
|
||||||
</>
|
</>
|
||||||
) : null}
|
) : null}
|
||||||
|
{customContent ? (
|
||||||
|
<>
|
||||||
|
<Separator orientation="vertical" className="mx-1 h-6" />
|
||||||
|
{customContent}
|
||||||
|
</>
|
||||||
|
) : null}
|
||||||
</div>
|
</div>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import type { ReactNode, MouseEvent } from "react";
|
|||||||
import { Button } from "@/components/ui/button";
|
import { Button } from "@/components/ui/button";
|
||||||
import { cn } from "@/lib/utils";
|
import { cn } from "@/lib/utils";
|
||||||
|
|
||||||
type Props = {
|
export type KriEditorToolbarButtonProps = {
|
||||||
active?: boolean;
|
active?: boolean;
|
||||||
disabled?: boolean;
|
disabled?: boolean;
|
||||||
title?: string;
|
title?: string;
|
||||||
@@ -13,7 +13,7 @@ type Props = {
|
|||||||
className?: string;
|
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) => {
|
const onMouseDown = (e: MouseEvent) => {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import type { Editor } from '@tiptap/react'
|
import type { Editor } from '@tiptap/react'
|
||||||
|
import type { ReactNode } from 'react'
|
||||||
|
|
||||||
export type KriEditorMode = 'wysiwyg' | 'both' | 'html' | 'sms'
|
export type KriEditorMode = 'wysiwyg' | 'both' | 'html' | 'sms'
|
||||||
export type KriEditorUploadResult = string | { url: string }
|
export type KriEditorUploadResult = string | { url: string }
|
||||||
@@ -26,8 +27,13 @@ export type KriEditorCommand = {
|
|||||||
insertText: (text: string) => void
|
insertText: (text: string) => void
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export type KriEditorToolbarContext = {
|
||||||
|
editor: Editor
|
||||||
|
command: KriEditorCommand
|
||||||
|
view: 'wysiwyg' | 'html'
|
||||||
|
}
|
||||||
|
|
||||||
export type KriEditorProps = {
|
export type KriEditorProps = {
|
||||||
isSmsReadOnly?: boolean
|
|
||||||
onEditorReady?: (editor: Editor | HTMLTextAreaElement | null) => void
|
onEditorReady?: (editor: Editor | HTMLTextAreaElement | null) => void
|
||||||
onCommandReady?: (command: KriEditorCommand | null) => void
|
onCommandReady?: (command: KriEditorCommand | null) => void
|
||||||
label?: string
|
label?: string
|
||||||
@@ -38,6 +44,8 @@ export type KriEditorProps = {
|
|||||||
showPreview?: boolean
|
showPreview?: boolean
|
||||||
previewClassName?: string
|
previewClassName?: string
|
||||||
mode?: KriEditorMode
|
mode?: KriEditorMode
|
||||||
|
/** @deprecated mode를 사용하세요. */
|
||||||
|
editorMode?: KriEditorMode
|
||||||
onImageUpload?: (file: File) => Promise<KriEditorUploadResult>
|
onImageUpload?: (file: File) => Promise<KriEditorUploadResult>
|
||||||
maxImageBytes?: number
|
maxImageBytes?: number
|
||||||
imageMaxWidth?: number
|
imageMaxWidth?: number
|
||||||
@@ -51,9 +59,11 @@ export type KriEditorProps = {
|
|||||||
debounceMs?: number
|
debounceMs?: number
|
||||||
smsTitle?: string
|
smsTitle?: string
|
||||||
changeSmsType?: (type: 'SMS' | 'LMS') => void
|
changeSmsType?: (type: 'SMS' | 'LMS') => void
|
||||||
|
isSmsReadOnly?: boolean
|
||||||
smsMaxBytes?: number
|
smsMaxBytes?: number
|
||||||
lmsMaxBytes?: number
|
lmsMaxBytes?: number
|
||||||
toolbar?: KriEditorToolbarConfig | false
|
toolbar?: KriEditorToolbarConfig | false
|
||||||
|
renderToolbarEnd?: (context: KriEditorToolbarContext) => ReactNode
|
||||||
}
|
}
|
||||||
|
|
||||||
export type ToolbarState = {
|
export type ToolbarState = {
|
||||||
|
|||||||
Reference in New Issue
Block a user