260907_01_ENH: 에디터 이미지맵 보존 및 SMS 미리보기 문구 지원

This commit is contained in:
2026-09-07 14:28:31 +09:00
parent b36893610f
commit b6ed23fa88
9 changed files with 416 additions and 17 deletions

View File

@@ -25,6 +25,7 @@ import type { KriEditorCommand, KriEditorProps, ToolbarState } from "./types";
import { resolveUploadUrl } from "./types";
import { FontSize } from "./extensions/font-size";
import { Div } from "./extensions/div";
import { ImageMap, ImageWithMap } from "./extensions/image-map";
import { compressImage } from "./utils/imageCompression";
import { sanitizeHtml } from "./utils/sanitizeHtml";
import { preserveEmptyParagraphs } from "./utils/emptyParagraph";
@@ -97,6 +98,9 @@ export function KriEditorWysiwyg(props: KriEditorProps) {
fontFamilyOptions = DEFAULT_FONT_FAMILY_OPTIONS,
editorExtensions = [],
allowedCustomHtml,
enableImageMap = false,
htmlSanitizer,
getWysiwygCompatibility,
transformPreviewHtml,
} = props;
@@ -114,9 +118,14 @@ export function KriEditorWysiwyg(props: KriEditorProps) {
const htmlTi = useTextareaInsert(htmlTextareaRef);
const { insertText: insertTextareaText } = htmlTi;
const [view, setView] = useState<ViewMode>("wysiwyg");
const getCompatibility = useCallback(
(html: string) => getWysiwygCompatibility?.(html) ?? { compatible: true },
[getWysiwygCompatibility],
);
const initialView: ViewMode = showHtmlToggle && !getCompatibility(value ?? "").compatible ? "html" : "wysiwyg";
const [view, setView] = useState<ViewMode>(initialView);
const [previewSource, setPreviewSource] = useState(value ?? "");
const viewRef = useRef<ViewMode>("wysiwyg");
const viewRef = useRef<ViewMode>(initialView);
useEffect(() => {
viewRef.current = view;
@@ -179,7 +188,7 @@ export function KriEditorWysiwyg(props: KriEditorProps) {
TableHeader,
TableCell,
TiptapImage.configure({
(enableImageMap ? ImageWithMap : TiptapImage).configure({
inline: true,
allowBase64,
HTMLAttributes: {
@@ -191,6 +200,7 @@ export function KriEditorWysiwyg(props: KriEditorProps) {
alwaysPreserveAspectRatio: true,
},
}),
...(enableImageMap ? [ImageMap] : []),
Placeholder.configure({ placeholder }),
...editorExtensions,
],
@@ -475,12 +485,19 @@ export function KriEditorWysiwyg(props: KriEditorProps) {
}
const raw = htmlDraftRef.current;
const safe = sanitizeHtmlOpt ? sanitizeHtml(raw, allowedCustomHtml) : raw;
const compatibility = getCompatibility(raw);
if (!compatibility.compatible) {
toast.message(compatibility.reason ?? "현재 HTML은 WYSIWYG로 전환할 수 없습니다.");
return;
}
const safe = htmlSanitizer
? htmlSanitizer(raw)
: sanitizeHtmlOpt ? sanitizeHtml(raw, allowedCustomHtml) : raw;
editor.commands.setContent(safe, { emitUpdate: false, parseOptions: { preserveWhitespace: "full" } });
flushChange(preserveEmptyParagraphs(editor.getHTML()));
setView("wysiwyg");
}, [allowedCustomHtml, editor, flushChange, sanitizeHtmlOpt]);
}, [allowedCustomHtml, editor, flushChange, getCompatibility, htmlSanitizer, sanitizeHtmlOpt]);
const formatHtmlView = useCallback(async () => {
try {
@@ -526,9 +543,27 @@ export function KriEditorWysiwyg(props: KriEditorProps) {
useEffect(() => {
setPreviewSource(value ?? "");
if (!editor) return;
if (viewRef.current === "html") return;
if (value == null) return;
const compatibility = getCompatibility(value);
if (showHtmlToggle && !compatibility.compatible) {
htmlDraftRef.current = value;
if (htmlTextareaRef.current) htmlTextareaRef.current.value = value;
if (viewRef.current !== "html") {
viewRef.current = "html";
setView("html");
}
return;
}
if (viewRef.current === "html") {
if (getWysiwygCompatibility && value !== optimisticHtmlRef.current && value !== htmlDraftRef.current) {
htmlDraftRef.current = value;
if (htmlTextareaRef.current) htmlTextareaRef.current.value = value;
}
return;
}
// 빈 문자열("")은 ProseMirror 스키마상 유효한 문서가 아니라 setContent가 조용히 실패해
// 이전 템플릿 본문이 그대로 남는 문제가 있었음 -> 빈 값은 빈 문단으로 정규화해서 비교/설정.
const normalizedValue = value || "<p></p>";
@@ -547,7 +582,7 @@ export function KriEditorWysiwyg(props: KriEditorProps) {
editor.createNodeViews();
}
}
}, [value, editor, view]);
}, [value, editor, view, getCompatibility, getWysiwygCompatibility, showHtmlToggle]);
useEffect(() => {
if (!editor) return;
@@ -611,10 +646,17 @@ export function KriEditorWysiwyg(props: KriEditorProps) {
const previewHtml = useMemo(() => {
const preserved = preserveEmptyParagraphs(previewSource);
const sanitized = sanitizeHtmlOpt ? sanitizeHtml(preserved, allowedCustomHtml) : preserved;
const sanitized = htmlSanitizer
? htmlSanitizer(preserved)
: sanitizeHtmlOpt ? sanitizeHtml(preserved, allowedCustomHtml) : preserved;
const withLinkStyle = applyLinkPreviewClass(sanitized);
return transformPreviewHtml ? transformPreviewHtml(withLinkStyle) : withLinkStyle;
}, [allowedCustomHtml, previewSource, sanitizeHtmlOpt, transformPreviewHtml]);
}, [allowedCustomHtml, htmlSanitizer, previewSource, sanitizeHtmlOpt, transformPreviewHtml]);
const wysiwygCompatibility = useMemo(
() => getCompatibility(view === "html" ? previewSource : (editor?.getHTML() ?? previewSource)),
[editor, getCompatibility, previewSource, view],
);
if (!editor) {
return (
@@ -640,6 +682,8 @@ export function KriEditorWysiwyg(props: KriEditorProps) {
onPickImage={handlePickImage}
showHtmlToggle={showHtmlToggle}
isHtmlView={view === "html"}
htmlToWysiwygDisabled={!wysiwygCompatibility.compatible}
htmlToWysiwygDisabledReason={wysiwygCompatibility.reason}
onToggleHtmlView={onToggleHtmlView}
onFormatHtml={formatHtmlView}
fontFamilyOptions={fontFamilyOptions}
@@ -681,6 +725,11 @@ export function KriEditorWysiwyg(props: KriEditorProps) {
placeholder={placeholder}
{...htmlTi.bindSelectionHandlers}
/>
{!wysiwygCompatibility.compatible ? (
<p className="mt-2 text-xs text-amber-700" role="status">
{wysiwygCompatibility.reason ?? "현재 HTML은 WYSIWYG에서 손실 없이 편집할 수 없습니다."}
</p>
) : null}
</div>
)}