260908_01_ENH: 이메일 템플릿에서 <area>, <map> 태그 사용 시 반투명 영역으로 가시화
This commit is contained in:
@@ -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<HTMLInputElement | null>(null);
|
||||
const previewContainerRef = useRef<HTMLDivElement | null>(null);
|
||||
const htmlTextareaRef = useRef<HTMLTextAreaElement | null>(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 && (
|
||||
<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 ref={previewContainerRef} className="relative rounded-md border bg-muted/30 p-3">
|
||||
<div dangerouslySetInnerHTML={{ __html: previewHtml }} />
|
||||
{imageMapOverlayBoxes.length > 0 && (
|
||||
<div className="pointer-events-none absolute inset-0" aria-hidden="true">
|
||||
{imageMapOverlayBoxes.map((box) => (
|
||||
<div
|
||||
key={box.key}
|
||||
className="absolute border-2 border-sky-500 bg-sky-500/30"
|
||||
style={box.style}
|
||||
/>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
46
src/components/kri-editor/hooks/useImageMapOverlay.ts
Normal file
46
src/components/kri-editor/hooks/useImageMapOverlay.ts
Normal file
@@ -0,0 +1,46 @@
|
||||
import { useLayoutEffect, useRef, useState } from "react"
|
||||
import { computeImageMapOverlayBoxes, type ImageMapOverlayBox } from "../utils/imageMapOverlay"
|
||||
|
||||
// previewHtml이 바뀔 때마다(이미지 로드/리사이즈 포함) <map>/<area> 오버레이 위치를 다시 계산한다.
|
||||
export function useImageMapOverlay(
|
||||
containerRef: React.RefObject<HTMLElement | null>,
|
||||
previewHtml: string,
|
||||
): ImageMapOverlayBox[] {
|
||||
const [boxes, setBoxes] = useState<ImageMapOverlayBox[]>([])
|
||||
const frameRef = useRef<number | null>(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<HTMLImageElement>("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
|
||||
}
|
||||
112
src/components/kri-editor/utils/imageMapOverlay.ts
Normal file
112
src/components/kri-editor/utils/imageMapOverlay.ts
Normal file
@@ -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는 <img>의 원본(naturalWidth/Height) 기준이라, 미리보기에서 이미지가
|
||||
// 리사이즈되어 렌더링된 크기에 맞춰 스케일 변환한 뒤 컨테이너 기준 좌표로 옮겨야 한다.
|
||||
export function computeImageMapOverlayBoxes(container: HTMLElement): ImageMapOverlayBox[] {
|
||||
const boxes: ImageMapOverlayBox[] = []
|
||||
const containerRect = container.getBoundingClientRect()
|
||||
const images = container.querySelectorAll<HTMLImageElement>("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<HTMLMapElement>(`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
|
||||
}
|
||||
@@ -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) && (
|
||||
<p className="text-sm text-muted-foreground">
|
||||
※ <map>, <area> 가 사용된 경우 해당 영역이 반투명하게 보여집니다.
|
||||
</p>
|
||||
)}
|
||||
|
||||
{!isSms && (
|
||||
<div className="space-y-2">
|
||||
<div className="flex items-center justify-between">
|
||||
|
||||
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user