251212_01_ENH : kri-editor 1차 완성, 프로젝트 정보 저장 api연동 완료
This commit is contained in:
31
src/components/kri-editor/extensions/div.ts
Normal file
31
src/components/kri-editor/extensions/div.ts
Normal file
@@ -0,0 +1,31 @@
|
||||
import { Node, mergeAttributes } from "@tiptap/core";
|
||||
|
||||
export const Div = Node.create({
|
||||
name: "divBlock",
|
||||
group: "block",
|
||||
content: "block+",
|
||||
defining: true,
|
||||
|
||||
addAttributes() {
|
||||
return {
|
||||
class: {
|
||||
default: null as string | null,
|
||||
parseHTML: (el) => (el as HTMLElement).getAttribute("class"),
|
||||
renderHTML: (attrs) => (attrs.class ? { class: attrs.class } : {}),
|
||||
},
|
||||
style: {
|
||||
default: null as string | null,
|
||||
parseHTML: (el) => (el as HTMLElement).getAttribute("style"),
|
||||
renderHTML: (attrs) => (attrs.style ? { style: attrs.style } : {}),
|
||||
},
|
||||
};
|
||||
},
|
||||
|
||||
parseHTML() {
|
||||
return [{ tag: "div" }];
|
||||
},
|
||||
|
||||
renderHTML({ HTMLAttributes }) {
|
||||
return ["div", mergeAttributes(HTMLAttributes), 0];
|
||||
},
|
||||
});
|
||||
@@ -1,5 +1,13 @@
|
||||
import { Extension } from "@tiptap/core";
|
||||
|
||||
declare module "@tiptap/core" {
|
||||
interface Commands<ReturnType> {
|
||||
fontSize: {
|
||||
setFontSize: (fontSize: string | null) => ReturnType;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
export const FontSize = Extension.create({
|
||||
name: "fontSize",
|
||||
addGlobalAttributes() {
|
||||
@@ -19,4 +27,17 @@ export const FontSize = Extension.create({
|
||||
},
|
||||
];
|
||||
},
|
||||
|
||||
addCommands() {
|
||||
return {
|
||||
setFontSize:
|
||||
(fontSize) =>
|
||||
({ chain }) => {
|
||||
if (!fontSize) {
|
||||
return chain().setMark("textStyle", { fontSize: null }).removeEmptyTextStyle().run();
|
||||
}
|
||||
return chain().setMark("textStyle", { fontSize }).run();
|
||||
},
|
||||
};
|
||||
},
|
||||
});
|
||||
|
||||
@@ -1,57 +0,0 @@
|
||||
import { Node, mergeAttributes } from "@tiptap/core";
|
||||
import { ReactNodeViewRenderer } from "@tiptap/react";
|
||||
import type { ImageUploadNodeOptions } from "./types";
|
||||
import { ImageUploadNodeView } from "./image-upload-nodeview";
|
||||
|
||||
declare module "@tiptap/core" {
|
||||
interface Commands<ReturnType> {
|
||||
imageUpload: {
|
||||
setImageUploadNode: () => ReturnType;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
export const ImageUploadNode = Node.create({
|
||||
name: "imageUpload",
|
||||
group: "block",
|
||||
atom: true,
|
||||
draggable: true,
|
||||
selectable: true,
|
||||
|
||||
addOptions() {
|
||||
return {
|
||||
accept: "image/*",
|
||||
maxSize: 0,
|
||||
upload: undefined,
|
||||
onError: undefined,
|
||||
} satisfies Partial<ImageUploadNodeOptions>;
|
||||
},
|
||||
|
||||
addAttributes() {
|
||||
return {
|
||||
accept: { default: this.options.accept },
|
||||
maxSize: { default: this.options.maxSize },
|
||||
};
|
||||
},
|
||||
|
||||
parseHTML() {
|
||||
return [{ tag: 'div[data-type="image-upload"]' }];
|
||||
},
|
||||
|
||||
renderHTML({ HTMLAttributes }) {
|
||||
return ["div", mergeAttributes({ "data-type": "image-upload" }, HTMLAttributes)];
|
||||
},
|
||||
|
||||
addNodeView() {
|
||||
return ReactNodeViewRenderer(ImageUploadNodeView);
|
||||
},
|
||||
|
||||
addCommands() {
|
||||
return {
|
||||
setImageUploadNode:
|
||||
() =>
|
||||
({ commands }) =>
|
||||
commands.insertContent({ type: this.name }),
|
||||
};
|
||||
},
|
||||
});
|
||||
@@ -1,130 +0,0 @@
|
||||
"use client";
|
||||
|
||||
import * as React from "react";
|
||||
import { NodeViewWrapper } from "@tiptap/react";
|
||||
import { toast } from "sonner";
|
||||
import type { ImageUploadNodeViewProps } from "./types";
|
||||
import { cn } from "@/lib/utils";
|
||||
|
||||
export function ImageUploadNodeView(props: any) {
|
||||
const { editor, extension, getPos, node } = props as ImageUploadNodeViewProps & {
|
||||
node: any;
|
||||
};
|
||||
|
||||
const opts = extension.options;
|
||||
const inputRef = React.useRef<HTMLInputElement | null>(null);
|
||||
|
||||
const [status, setStatus] = React.useState<"idle" | "uploading" | "error" | "success">("idle");
|
||||
const [progress, setProgress] = React.useState<number>(0);
|
||||
|
||||
const doUpload = React.useCallback(
|
||||
async (file: File) => {
|
||||
if (!file.type.startsWith("image/")) {
|
||||
opts.onError?.("이미지 파일만 업로드할 수 있습니다.");
|
||||
toast.error("이미지 파일만 업로드할 수 있습니다.");
|
||||
return;
|
||||
}
|
||||
if (opts.maxSize > 0 && file.size > opts.maxSize) {
|
||||
const mb = (opts.maxSize / (1024 * 1024)).toFixed(0);
|
||||
opts.onError?.(`이미지 용량이 제한을 초과합니다. (최대 ${mb}MB)`);
|
||||
toast.error("이미지 용량이 제한을 초과합니다.", { description: `최대 ${mb}MB` });
|
||||
return;
|
||||
}
|
||||
|
||||
setStatus("uploading");
|
||||
setProgress(10);
|
||||
|
||||
try {
|
||||
const url = await opts.upload(file);
|
||||
if (!url) throw new Error("upload returned empty url");
|
||||
|
||||
setProgress(100);
|
||||
setStatus("success");
|
||||
|
||||
const pos = getPos();
|
||||
const fileName = file.name.replace(/\.[^/.]+$/, "") || "image";
|
||||
|
||||
editor
|
||||
.chain()
|
||||
.focus()
|
||||
.deleteRange({ from: pos, to: pos + node.nodeSize })
|
||||
.insertContentAt(pos, [
|
||||
{ type: "image", attrs: { src: url, alt: fileName, title: fileName } },
|
||||
])
|
||||
.insertContentAt(pos + 1, { type: "paragraph" })
|
||||
.run();
|
||||
} catch (e) {
|
||||
setStatus("error");
|
||||
setProgress(0);
|
||||
opts.onError?.("이미지 업로드에 실패했습니다.");
|
||||
toast.error("이미지 업로드에 실패했습니다.");
|
||||
// eslint-disable-next-line no-console
|
||||
console.error(e);
|
||||
}
|
||||
},
|
||||
[editor, getPos, node?.nodeSize, opts]
|
||||
);
|
||||
|
||||
const onPick = React.useCallback(() => {
|
||||
if (status === "uploading") return;
|
||||
inputRef.current?.click();
|
||||
}, [status]);
|
||||
|
||||
const onChange = React.useCallback(
|
||||
(e: React.ChangeEvent<HTMLInputElement>) => {
|
||||
const f = e.target.files?.[0];
|
||||
e.target.value = "";
|
||||
if (!f) return;
|
||||
void doUpload(f);
|
||||
},
|
||||
[doUpload]
|
||||
);
|
||||
|
||||
const onDrop = React.useCallback(
|
||||
(e: React.DragEvent) => {
|
||||
e.preventDefault();
|
||||
if (status === "uploading") return;
|
||||
const f = e.dataTransfer.files?.[0];
|
||||
if (!f) return;
|
||||
void doUpload(f);
|
||||
},
|
||||
[doUpload, status]
|
||||
);
|
||||
|
||||
const onDragOver = React.useCallback((e: React.DragEvent) => {
|
||||
e.preventDefault();
|
||||
}, []);
|
||||
|
||||
return (
|
||||
<NodeViewWrapper className="not-prose">
|
||||
<div
|
||||
className={cn("my-3 rounded-md border border-dashed p-4", status === "uploading" && "opacity-80")}
|
||||
onClick={onPick}
|
||||
onDrop={onDrop}
|
||||
onDragOver={onDragOver}
|
||||
role="button"
|
||||
tabIndex={0}
|
||||
>
|
||||
<input
|
||||
ref={inputRef}
|
||||
type="file"
|
||||
accept={opts.accept}
|
||||
className="hidden"
|
||||
onChange={onChange}
|
||||
disabled={status === "uploading"}
|
||||
/>
|
||||
|
||||
{status === "uploading" ? (
|
||||
<div className="text-sm">
|
||||
업로드 중… {progress}%
|
||||
<div className="mt-2 h-2 w-full rounded bg-muted">
|
||||
<div className="h-2 rounded bg-primary" style={{ width: `${progress}%` }} />
|
||||
</div>
|
||||
</div>
|
||||
) : (
|
||||
<div className="text-sm text-muted-foreground">이미지를 드래그하거나 클릭해 업로드할 수 있습니다.</div>
|
||||
)}
|
||||
</div>
|
||||
</NodeViewWrapper>
|
||||
);
|
||||
}
|
||||
@@ -1,15 +0,0 @@
|
||||
import type { Editor } from "@tiptap/react";
|
||||
|
||||
export type ImageUploadNodeOptions = {
|
||||
accept: string;
|
||||
maxSize: number;
|
||||
upload: (file: File) => Promise<string>; // url 반환
|
||||
onError?: (message: string) => void;
|
||||
};
|
||||
|
||||
export type ImageUploadNodeViewProps = {
|
||||
editor: Editor;
|
||||
extension: { options: ImageUploadNodeOptions };
|
||||
getPos: () => number;
|
||||
node: any;
|
||||
};
|
||||
Reference in New Issue
Block a user