251212_01_ENH : kri-editor 1차 완성, 프로젝트 정보 저장 api연동 완료

This commit is contained in:
2025-12-12 17:27:12 +09:00
parent 1080880fba
commit d9775c8a1d
46 changed files with 2124 additions and 1668 deletions

View File

@@ -1,196 +1,19 @@
"use client";
import * as React from "react";
import { EditorContent, useEditor } from "@tiptap/react";
import StarterKit from "@tiptap/starter-kit";
import Underline from "@tiptap/extension-underline";
import TiptapImage from "@tiptap/extension-image";
import Placeholder from "@tiptap/extension-placeholder";
import Link from "@tiptap/extension-link";
import TextAlign from "@tiptap/extension-text-align";
import type { KriEditorProps, KriEditorMode } from "./types";
import { KriEditorHtmlOnly } from "./KriEditorHtmlOnly";
import { KriEditorWysiwyg } from "./KriEditorWysiwyg";
import { Table } from "@tiptap/extension-table";
import TableRow from "@tiptap/extension-table-row";
import TableHeader from "@tiptap/extension-table-header";
import TableCell from "@tiptap/extension-table-cell";
function resolveMode(mode?: KriEditorMode, editorMode?: KriEditorMode): KriEditorMode {
return mode ?? editorMode ?? "wysiwyg";
}
import { toast } from "sonner";
import { cn } from "@/lib/utils";
export function KriEditor(props: KriEditorProps) {
const mode = resolveMode(props.mode, props.editorMode);
import { compressImage } from "./utils/imageCompression";
import { Toolbar } from "./toolbar/Toolbar";
if (mode === "html") {
return <KriEditorHtmlOnly {...props} mode={mode} editorMode={mode} />;
}
export type UploadResult = { url: string };
export type KriEditorProps = {
value: string;
onChange: (html: string) => void;
placeholder?: string;
className?: string;
onImageUpload?: (file: File) => Promise<UploadResult>;
maxImageBytes?: number;
imageMaxWidth?: number;
imageMaxHeight?: number;
imageQuality?: number;
imageMimeType?: "image/webp" | "image/jpeg";
allowBase64?: boolean;
};
export const KriEditor: React.FC<KriEditorProps> = ({
value,
onChange,
placeholder = "내용을 입력하세요…",
className,
onImageUpload,
maxImageBytes = 5 * 1024 * 1024,
imageMaxWidth = 1600,
imageMaxHeight = 1600,
imageQuality = 0.82,
imageMimeType = "image/webp",
allowBase64 = false,
}) => {
const fileRef = React.useRef<HTMLInputElement | null>(null);
const editor = useEditor(
{
immediatelyRender: false,
extensions: [
StarterKit.configure({
blockquote: false,
codeBlock: false,
code: false,
}),
Underline,
Link.configure({
openOnClick: false,
autolink: true,
linkOnPaste: true,
HTMLAttributes: {
class: "text-blue-600 underline underline-offset-2",
},
}),
TextAlign.configure({ types: ["heading", "paragraph"] }),
Table.configure({
resizable: true,
HTMLAttributes: {
class: "w-full table-fixed border-collapse border border-border my-2",
},
}),
TableRow,
TableHeader.configure({
HTMLAttributes: {
class: "border border-border bg-muted px-2 py-1 font-semibold align-top",
},
}),
TableCell.configure({
HTMLAttributes: {
class: "border border-border px-2 py-1 align-top min-w-[80px]",
},
}),
TiptapImage.configure({ allowBase64 }),
Placeholder.configure({ placeholder }),
],
content: value || "<p></p>",
onUpdate: ({ editor }) => onChange(editor.getHTML()),
editorProps: {
attributes: {
class:
"prose prose-sm max-w-none focus:outline-none min-h-[220px] px-0 py-2 " +
"prose-headings:mt-6 prose-headings:mb-3 " +
"prose-p:my-2 prose-ul:my-2 prose-ol:my-2",
},
},
},
[placeholder, allowBase64]
);
React.useEffect(() => {
if (!editor) return;
const current = editor.getHTML();
if (value != null && value != current) {
editor.commands.setContent(value, {
emitUpdate: false,
parseOptions: { preserveWhitespace: "full" },
});
}
}, [value, editor]);
const canImageUpload = Boolean(onImageUpload);
const handlePickImage = () => {
if (!canImageUpload) return;
fileRef.current?.click();
};
const handleFile = async (e: React.ChangeEvent<HTMLInputElement>) => {
try {
const file = e.target.files?.[0];
e.target.value = "";
if (!file || !editor) return;
if (!canImageUpload || !onImageUpload) return;
if (!file.type.startsWith("image/")) {
toast.error("이미지 파일만 업로드할 수 있습니다.");
return;
}
const compressed = await compressImage(file, {
maxBytes: maxImageBytes,
maxWidth: imageMaxWidth,
maxHeight: imageMaxHeight,
mimeType: imageMimeType,
quality: imageQuality,
});
if (compressed.size > maxImageBytes) {
toast.error("이미지 용량이 제한을 초과합니다.", {
description: `최대 ${(maxImageBytes / (1024 * 1024)).toFixed(0)}MB`,
});
return;
}
const { url } = await onImageUpload(compressed);
if (!url || typeof url !== "string") {
toast.error("이미지 업로드 응답이 올바르지 않습니다.");
return;
}
editor.chain().focus().setImage({ src: url }).run();
} catch (err) {
toast.error("이미지 업로드에 실패했습니다.");
console.error(err);
}
};
if (!editor) return <div className={cn("text-sm text-muted-foreground", className)}>에디터 로딩 중…</div>;
return (
<div className={cn("w-full", className)}>
<div className="border-b py-2">
<Toolbar editor={editor} canImageUpload={canImageUpload} onPickImage={handlePickImage} />
<input
ref={fileRef}
type="file"
accept="image/*"
className="hidden"
onChange={handleFile}
disabled={!canImageUpload}
/>
</div>
<div className="py-4">
<EditorContent editor={editor} />
</div>
</div>
);
};
return <KriEditorWysiwyg {...props} mode={mode} editorMode={mode} />;
}