From 7bfb1ca9874ee34062cf5e9d2884db27e96ed820 Mon Sep 17 00:00:00 2001 From: kric_jspark Date: Tue, 8 Sep 2026 07:47:32 +0900 Subject: [PATCH] =?UTF-8?q?260908=5F01=5FENH:=20=EC=9D=B4=EB=A9=94?= =?UTF-8?q?=EC=9D=BC=20=ED=85=9C=ED=94=8C=EB=A6=BF=EC=97=90=EC=84=9C=20,=20=20=ED=83=9C=EA=B7=B8=20=EC=82=AC=EC=9A=A9=20?= =?UTF-8?q?=EC=8B=9C=20=EB=B0=98=ED=88=AC=EB=AA=85=20=EC=98=81=EC=97=AD?= =?UTF-8?q?=EC=9C=BC=EB=A1=9C=20=EA=B0=80=EC=8B=9C=ED=99=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../kri-editor/KriEditorWysiwyg.tsx | 17 ++- .../kri-editor/hooks/useImageMapOverlay.ts | 46 +++++++ .../kri-editor/utils/imageMapOverlay.ts | 112 ++++++++++++++++++ .../message-template/MessageTemplate.tsx | 7 ++ .../message-template/emailHtmlPolicy.ts | 7 ++ 5 files changed, 188 insertions(+), 1 deletion(-) create mode 100644 src/components/kri-editor/hooks/useImageMapOverlay.ts create mode 100644 src/components/kri-editor/utils/imageMapOverlay.ts diff --git a/src/components/kri-editor/KriEditorWysiwyg.tsx b/src/components/kri-editor/KriEditorWysiwyg.tsx index 97b5225..80034ce 100644 --- a/src/components/kri-editor/KriEditorWysiwyg.tsx +++ b/src/components/kri-editor/KriEditorWysiwyg.tsx @@ -35,6 +35,7 @@ import { applyLinkPreviewClass, KRI_EDITOR_LINK_CLASS } from "./utils/linkPrevie import { Toolbar } from "./toolbar/Toolbar"; import { useDebouncedChange } from "./hooks/useDebouncedChange"; import { useTextareaInsert } from "./hooks/useTextareaInsert"; +import { useImageMapOverlay } from "./hooks/useImageMapOverlay"; type ViewMode = "wysiwyg" | "html"; @@ -114,6 +115,7 @@ export function KriEditorWysiwyg(props: KriEditorProps) { const { scheduleChange, flushChange, optimisticHtmlRef } = useDebouncedChange(onChange, debounceMs); const fileRef = useRef(null); + const previewContainerRef = useRef(null); const htmlTextareaRef = useRef(null); const htmlTi = useTextareaInsert(htmlTextareaRef); const { insertText: insertTextareaText } = htmlTi; @@ -653,6 +655,8 @@ export function KriEditorWysiwyg(props: KriEditorProps) { return transformPreviewHtml ? transformPreviewHtml(withLinkStyle) : withLinkStyle; }, [allowedCustomHtml, htmlSanitizer, previewSource, sanitizeHtmlOpt, transformPreviewHtml]); + const imageMapOverlayBoxes = useImageMapOverlay(previewContainerRef, previewHtml); + const wysiwygCompatibility = useMemo( () => getCompatibility(view === "html" ? previewSource : (editor?.getHTML() ?? previewSource)), [editor, getCompatibility, previewSource, view], @@ -736,8 +740,19 @@ export function KriEditorWysiwyg(props: KriEditorProps) { {showPreview && (
미리보기
-
+
+ {imageMapOverlayBoxes.length > 0 && ( +
)} diff --git a/src/components/kri-editor/hooks/useImageMapOverlay.ts b/src/components/kri-editor/hooks/useImageMapOverlay.ts new file mode 100644 index 0000000..f86f425 --- /dev/null +++ b/src/components/kri-editor/hooks/useImageMapOverlay.ts @@ -0,0 +1,46 @@ +import { useLayoutEffect, useRef, useState } from "react" +import { computeImageMapOverlayBoxes, type ImageMapOverlayBox } from "../utils/imageMapOverlay" + +// previewHtml이 바뀔 때마다(이미지 로드/리사이즈 포함) / 오버레이 위치를 다시 계산한다. +export function useImageMapOverlay( + containerRef: React.RefObject, + previewHtml: string, +): ImageMapOverlayBox[] { + const [boxes, setBoxes] = useState([]) + const frameRef = useRef(null) + + useLayoutEffect(() => { + const container = containerRef.current + if (!container) { + setBoxes([]) + return + } + + const recompute = () => setBoxes(computeImageMapOverlayBoxes(container)) + + const scheduleRecompute = () => { + if (frameRef.current !== null) cancelAnimationFrame(frameRef.current) + frameRef.current = requestAnimationFrame(recompute) + } + + scheduleRecompute() + + const images = Array.from(container.querySelectorAll("img[usemap]")) + images.forEach((img) => { + if (!img.complete) img.addEventListener("load", scheduleRecompute) + }) + + const resizeObserver = new ResizeObserver(scheduleRecompute) + resizeObserver.observe(container) + window.addEventListener("resize", scheduleRecompute) + + return () => { + if (frameRef.current !== null) cancelAnimationFrame(frameRef.current) + images.forEach((img) => img.removeEventListener("load", scheduleRecompute)) + resizeObserver.disconnect() + window.removeEventListener("resize", scheduleRecompute) + } + }, [containerRef, previewHtml]) + + return boxes +} diff --git a/src/components/kri-editor/utils/imageMapOverlay.ts b/src/components/kri-editor/utils/imageMapOverlay.ts new file mode 100644 index 0000000..f6c215f --- /dev/null +++ b/src/components/kri-editor/utils/imageMapOverlay.ts @@ -0,0 +1,112 @@ +export type ImageMapOverlayBox = { + key: string + style: { + left: number + top: number + width: number + height: number + borderRadius?: string + clipPath?: string + } +} + +const parseCoords = (raw: string | null): number[] => + (raw ?? "") + .split(",") + .map((value) => Number(value.trim())) + .filter((value) => Number.isFinite(value)) + +// coords는 의 원본(naturalWidth/Height) 기준이라, 미리보기에서 이미지가 +// 리사이즈되어 렌더링된 크기에 맞춰 스케일 변환한 뒤 컨테이너 기준 좌표로 옮겨야 한다. +export function computeImageMapOverlayBoxes(container: HTMLElement): ImageMapOverlayBox[] { + const boxes: ImageMapOverlayBox[] = [] + const containerRect = container.getBoundingClientRect() + const images = container.querySelectorAll("img[usemap]") + + images.forEach((img, imageIndex) => { + if (!img.complete || img.naturalWidth === 0 || img.naturalHeight === 0) return + + const mapName = img.getAttribute("usemap")?.replace(/^#/, "") + if (!mapName) return + + const map = container.querySelector(`map[name="${CSS.escape(mapName)}"]`) + if (!map) return + + const imgRect = img.getBoundingClientRect() + if (imgRect.width === 0 || imgRect.height === 0) return + + const scaleX = imgRect.width / img.naturalWidth + const scaleY = imgRect.height / img.naturalHeight + const offsetX = imgRect.left - containerRect.left + const offsetY = imgRect.top - containerRect.top + + map.querySelectorAll("area").forEach((area, areaIndex) => { + const shape = (area.getAttribute("shape") || "rect").toLowerCase() + const coords = parseCoords(area.getAttribute("coords")) + const key = `${imageIndex}-${areaIndex}` + + if (shape === "rect" && coords.length >= 4) { + const [x1, y1, x2, y2] = coords + boxes.push({ + key, + style: { + left: offsetX + Math.min(x1, x2) * scaleX, + top: offsetY + Math.min(y1, y2) * scaleY, + width: Math.abs(x2 - x1) * scaleX, + height: Math.abs(y2 - y1) * scaleY, + }, + }) + return + } + + if (shape === "circle" && coords.length >= 3) { + const [cx, cy, r] = coords + boxes.push({ + key, + style: { + left: offsetX + (cx - r) * scaleX, + top: offsetY + (cy - r) * scaleY, + width: r * 2 * scaleX, + height: r * 2 * scaleY, + borderRadius: "9999px", + }, + }) + return + } + + if (shape === "poly" && coords.length >= 6 && coords.length % 2 === 0) { + const xs = coords.filter((_, i) => i % 2 === 0) + const ys = coords.filter((_, i) => i % 2 === 1) + const minX = Math.min(...xs) + const maxX = Math.max(...xs) + const minY = Math.min(...ys) + const maxY = Math.max(...ys) + const width = maxX - minX + const height = maxY - minY + if (width <= 0 || height <= 0) return + + const points: string[] = [] + for (let i = 0; i < coords.length; i += 2) { + const px = ((coords[i] - minX) / width) * 100 + const py = ((coords[i + 1] - minY) / height) * 100 + points.push(`${px}% ${py}%`) + } + + boxes.push({ + key, + style: { + left: offsetX + minX * scaleX, + top: offsetY + minY * scaleY, + width: width * scaleX, + height: height * scaleY, + clipPath: `polygon(${points.join(", ")})`, + }, + }) + } + + // shape="default"(이미지 전체)는 별도 오버레이 없이 그대로 둔다. + }) + }) + + return boxes +} diff --git a/src/components/message-template/MessageTemplate.tsx b/src/components/message-template/MessageTemplate.tsx index ce25226..bec85f7 100644 --- a/src/components/message-template/MessageTemplate.tsx +++ b/src/components/message-template/MessageTemplate.tsx @@ -60,6 +60,7 @@ import { import { getEmailHtmlPolicyIssues, getEmailWysiwygCompatibility, + hasImageMapMarkup, sanitizeEmailHtml, } from "@/components/message-template/emailHtmlPolicy"; @@ -609,6 +610,12 @@ const MessageTemplate = ({ pageTitle, defaultTemplate }: MessageTemplateProps) = /> )} + {!isSms && hasImageMapMarkup(selectedTemplate.tmplatBody) && ( +

+ ※ <map>, <area> 가 사용된 경우 해당 영역이 반투명하게 보여집니다. +

+ )} + {!isSms && (
diff --git a/src/components/message-template/emailHtmlPolicy.ts b/src/components/message-template/emailHtmlPolicy.ts index 8bd4970..71c2386 100644 --- a/src/components/message-template/emailHtmlPolicy.ts +++ b/src/components/message-template/emailHtmlPolicy.ts @@ -190,6 +190,13 @@ export function getEmailHtmlPolicyIssues(html: string): string[] { return [...new Set(issues)]; } +export function hasImageMapMarkup(html: string): boolean { + if (!html) return false; + const template = parseHtml(html); + if (!template) return /<\s*(map|area)\b/i.test(html); + return template.content.querySelector("map, area") !== null; +} + export function sanitizeEmailHtml(html: string): string { if (!html) return ""; const purifier = resolvePurifier();