260803_01_ENH: 에디터에 QR 코드 삽입 추가

This commit is contained in:
2026-08-03 15:15:04 +09:00
parent 605d2dba49
commit a28981099f
18 changed files with 665 additions and 38 deletions

View File

@@ -26,6 +26,7 @@ import { FontSize } from "./extensions/font-size";
import { Div } from "./extensions/div";
import { compressImage } from "./utils/imageCompression";
import { sanitizeHtml } from "./utils/sanitizeHtml";
import { applyLinkPreviewClass, KRI_EDITOR_LINK_CLASS } from "./utils/linkPreview";
import { Toolbar } from "./toolbar/Toolbar";
import { useDebouncedChange } from "./hooks/useDebouncedChange";
import { useTextareaInsert } from "./hooks/useTextareaInsert";
@@ -87,6 +88,9 @@ export function KriEditorWysiwyg(props: KriEditorProps) {
allowBase64 = false,
sanitizeHtml: sanitizeHtmlOpt = true,
toolbar = {},
editorExtensions = [],
allowedCustomHtml,
transformPreviewHtml,
} = props;
const showHtmlToggle = mode === "both";
@@ -154,7 +158,7 @@ export function KriEditorWysiwyg(props: KriEditorProps) {
openOnClick: false,
linkOnPaste: true,
HTMLAttributes: {
class: "cursor-pointer text-blue-600 underline underline-offset-2",
class: KRI_EDITOR_LINK_CLASS,
...LINK_HTML_ATTRS,
},
}),
@@ -179,6 +183,7 @@ export function KriEditorWysiwyg(props: KriEditorProps) {
},
}),
Placeholder.configure({ placeholder }),
...editorExtensions,
],
content: value || "<p></p>",
@@ -436,12 +441,12 @@ export function KriEditorWysiwyg(props: KriEditorProps) {
}
const raw = htmlDraftRef.current;
const safe = sanitizeHtmlOpt ? sanitizeHtml(raw) : raw;
const safe = sanitizeHtmlOpt ? sanitizeHtml(raw, allowedCustomHtml) : raw;
editor.commands.setContent(safe, { emitUpdate: false, parseOptions: { preserveWhitespace: "full" } });
flushChange(editor.getHTML());
setView("wysiwyg");
}, [editor, flushChange, sanitizeHtmlOpt]);
}, [allowedCustomHtml, editor, flushChange, sanitizeHtmlOpt]);
useEffect(() => {
if (!editor) return;
@@ -558,8 +563,10 @@ export function KriEditorWysiwyg(props: KriEditorProps) {
}, [command, editor, props.onCommandReady]);
const previewHtml = useMemo(() => {
return sanitizeHtmlOpt ? sanitizeHtml(previewSource) : previewSource;
}, [previewSource, sanitizeHtmlOpt]);
const sanitized = sanitizeHtmlOpt ? sanitizeHtml(previewSource, allowedCustomHtml) : previewSource;
const withLinkStyle = applyLinkPreviewClass(sanitized);
return transformPreviewHtml ? transformPreviewHtml(withLinkStyle) : withLinkStyle;
}, [allowedCustomHtml, previewSource, sanitizeHtmlOpt, transformPreviewHtml]);
if (!editor) {
return (
@@ -630,7 +637,7 @@ export function KriEditorWysiwyg(props: KriEditorProps) {
<div className={cn("border-t p-3", previewClassName)}>
<div className="mb-2 text-sm font-semibold">미리보기</div>
<div className="rounded-md border bg-muted/30 p-3">
<div dangerouslySetInnerHTML={{ __html: previewHtml }} />
<div className="kri-editor-preview" dangerouslySetInnerHTML={{ __html: previewHtml }} />
</div>
</div>
)}

View File

@@ -4,6 +4,7 @@ export { KriEditorSms } from './KriEditorSms'
export { KriEditorWysiwyg } from './KriEditorWysiwyg'
export { ToolbarButton as KriEditorToolbarButton } from './toolbar/ToolbarButton'
export type {
KriEditorAllowedHtml,
KriEditorCommand,
KriEditorMode,
KriEditorProps,

View File

@@ -1,3 +1,4 @@
import type { Extensions } from '@tiptap/core'
import type { Editor } from '@tiptap/react'
import type { ReactNode } from 'react'
@@ -33,6 +34,11 @@ export type KriEditorToolbarContext = {
view: 'wysiwyg' | 'html'
}
export type KriEditorAllowedHtml = {
tags?: string[]
attributes?: string[]
}
export type KriEditorProps = {
onEditorReady?: (editor: Editor | HTMLTextAreaElement | null) => void
onCommandReady?: (command: KriEditorCommand | null) => void
@@ -64,6 +70,10 @@ export type KriEditorProps = {
lmsMaxBytes?: number
toolbar?: KriEditorToolbarConfig | false
renderToolbarEnd?: (context: KriEditorToolbarContext) => ReactNode
editorExtensions?: Extensions
allowedCustomHtml?: KriEditorAllowedHtml
/** 미리보기 패널에 표시할 HTML을 sanitize 이후 추가로 가공하고 싶을 때 사용 (예: 커스텀 태그를 대체 이미지로 치환) */
transformPreviewHtml?: (html: string) => string
}
export type ToolbarState = {

View File

@@ -0,0 +1,29 @@
export const KRI_EDITOR_LINK_CLASS = "cursor-pointer text-blue-600 underline underline-offset-2";
const CLASS_ATTR_PATTERN = /class\s*=\s*"([^"]*)"|class\s*=\s*'([^']*)'/i;
/**
* 미리보기(dangerouslySetInnerHTML)에 쓰이는 원본 HTML은 예전 템플릿이거나
* HTML 모드에서 직접 작성된 경우 에디터의 링크 스타일 클래스가 없을 수 있다.
* <a> 태그에 KRI_EDITOR_LINK_CLASS를 보정해 항상 동일하게 보이도록 한다.
*/
export function applyLinkPreviewClass(html: string): string {
if (!html) return html;
return html.replace(/<a\b([^>]*)>/gi, (match, attrs: string) => {
const classMatch = attrs.match(CLASS_ATTR_PATTERN);
if (!classMatch) {
return `<a${attrs} class="${KRI_EDITOR_LINK_CLASS}">`;
}
const existingClasses = (classMatch[1] ?? classMatch[2] ?? "").split(/\s+/).filter(Boolean);
const missingClasses = KRI_EDITOR_LINK_CLASS.split(" ").filter((c) => !existingClasses.includes(c));
if (missingClasses.length === 0) return match;
const mergedClass = [...existingClasses, ...missingClasses].join(" ");
const newAttrs = attrs.replace(CLASS_ATTR_PATTERN, `class="${mergedClass}"`);
return `<a${newAttrs}>`;
});
}

View File

@@ -2,27 +2,41 @@
import DOMPurify from "dompurify";
const HTML_PROFILE = {
USE_PROFILES: {
html: true,
},
} as const;
export type SanitizeHtmlOptions = {
tags?: string[];
attributes?: string[];
};
export function sanitizeHtml(
input: string,
options?: SanitizeHtmlOptions,
): string {
if (!input) {
return "";
}
// DOMPurify는 window/document가 있어야 동작한다. Next.js는 "use client" 컴포넌트도
// 최초 HTML을 만들 때 서버에서 한 번 실행하므로, SSR 단계에서는 sanitize를 건너뛴다.
// (이 값은 하이드레이션 후 클라이언트에서 다시 렌더링되며 정상적으로 sanitize된다.)
if (typeof window === "undefined") {
return input;
}
return DOMPurify.sanitize(
input,
HTML_PROFILE,
{
USE_PROFILES: {
html: true,
},
ADD_TAGS: options?.tags ?? [],
ADD_ATTR: options?.attributes ?? [],
},
);
}
export function sanitizeHtmlForPreview(
input: string,
options?: SanitizeHtmlOptions,
): string {
return sanitizeHtml(input);
return sanitizeHtml(input, options);
}