260907_01_ENH: 에디터 이미지맵 보존 및 SMS 미리보기 문구 지원
This commit is contained in:
@@ -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>
|
||||
)}
|
||||
|
||||
|
||||
93
src/components/kri-editor/extensions/image-map.ts
Normal file
93
src/components/kri-editor/extensions/image-map.ts
Normal file
@@ -0,0 +1,93 @@
|
||||
import { Node, mergeAttributes } from '@tiptap/core'
|
||||
import TiptapImage from '@tiptap/extension-image'
|
||||
import type { DOMOutputSpec } from '@tiptap/pm/model'
|
||||
|
||||
export type ImageMapAreaAttributes = {
|
||||
shape?: string
|
||||
coords?: string
|
||||
href?: string
|
||||
alt?: string
|
||||
title?: string
|
||||
target?: string
|
||||
}
|
||||
|
||||
const readAttribute = (element: Element, name: string): string | undefined =>
|
||||
element.getAttribute(name) ?? undefined
|
||||
|
||||
export const ImageWithMap = TiptapImage.extend({
|
||||
addAttributes() {
|
||||
return {
|
||||
...this.parent?.(),
|
||||
usemap: {
|
||||
default: null,
|
||||
parseHTML: (element) => element.getAttribute('usemap'),
|
||||
renderHTML: (attributes) => attributes.usemap ? { usemap: attributes.usemap } : {},
|
||||
},
|
||||
align: {
|
||||
default: null,
|
||||
parseHTML: (element) => element.getAttribute('align'),
|
||||
renderHTML: (attributes) => attributes.align ? { align: attributes.align } : {},
|
||||
},
|
||||
style: {
|
||||
default: null,
|
||||
parseHTML: (element) => element.getAttribute('style'),
|
||||
renderHTML: (attributes) => attributes.style ? { style: attributes.style } : {},
|
||||
},
|
||||
loading: {
|
||||
default: null,
|
||||
parseHTML: (element) => element.getAttribute('loading'),
|
||||
renderHTML: (attributes) => attributes.loading ? { loading: attributes.loading } : {},
|
||||
},
|
||||
}
|
||||
},
|
||||
})
|
||||
|
||||
export const ImageMap = Node.create({
|
||||
name: 'imageMap',
|
||||
group: 'block',
|
||||
atom: true,
|
||||
|
||||
addAttributes() {
|
||||
return {
|
||||
name: {
|
||||
default: null,
|
||||
parseHTML: (element) => element.getAttribute('name'),
|
||||
renderHTML: (attributes) => attributes.name ? { name: attributes.name } : {},
|
||||
},
|
||||
id: {
|
||||
default: null,
|
||||
parseHTML: (element) => element.getAttribute('id'),
|
||||
renderHTML: (attributes) => attributes.id ? { id: attributes.id } : {},
|
||||
},
|
||||
areas: {
|
||||
default: [],
|
||||
parseHTML: (element) => Array.from(element.children)
|
||||
.filter((child) => child.tagName.toLowerCase() === 'area')
|
||||
.map((area): ImageMapAreaAttributes => ({
|
||||
shape: readAttribute(area, 'shape'),
|
||||
coords: readAttribute(area, 'coords'),
|
||||
href: readAttribute(area, 'href'),
|
||||
alt: readAttribute(area, 'alt'),
|
||||
title: readAttribute(area, 'title'),
|
||||
target: readAttribute(area, 'target'),
|
||||
})),
|
||||
renderHTML: () => ({}),
|
||||
},
|
||||
}
|
||||
},
|
||||
|
||||
parseHTML() {
|
||||
return [{ tag: 'map' }]
|
||||
},
|
||||
|
||||
renderHTML({ node, HTMLAttributes }) {
|
||||
const mapAttributes = { ...HTMLAttributes }
|
||||
delete mapAttributes.areas
|
||||
const areas = (node.attrs.areas as ImageMapAreaAttributes[]).map((area): DOMOutputSpec => [
|
||||
'area',
|
||||
Object.fromEntries(Object.entries(area).filter(([, value]) => Boolean(value))),
|
||||
])
|
||||
|
||||
return ['map', mergeAttributes(mapAttributes), ...areas] as DOMOutputSpec
|
||||
},
|
||||
})
|
||||
@@ -14,6 +14,7 @@ export type {
|
||||
KriEditorToolbarContext,
|
||||
KriEditorToolbarFeature,
|
||||
KriEditorUploadResult,
|
||||
KriEditorWysiwygCompatibility,
|
||||
ToolbarState,
|
||||
} from './types'
|
||||
export type { KriEditorToolbarButtonProps } from './toolbar/ToolbarButton'
|
||||
|
||||
@@ -38,6 +38,8 @@ interface ToolbarProps {
|
||||
onPickImage: () => void
|
||||
showHtmlToggle: boolean
|
||||
isHtmlView: boolean
|
||||
htmlToWysiwygDisabled?: boolean
|
||||
htmlToWysiwygDisabledReason?: string
|
||||
onToggleHtmlView: () => void
|
||||
onFormatHtml: () => void
|
||||
fontFamilyOptions: KriEditorFontFamilyOption[]
|
||||
@@ -77,6 +79,8 @@ export function Toolbar({
|
||||
onPickImage,
|
||||
showHtmlToggle,
|
||||
isHtmlView,
|
||||
htmlToWysiwygDisabled = false,
|
||||
htmlToWysiwygDisabledReason,
|
||||
onToggleHtmlView,
|
||||
onFormatHtml,
|
||||
fontFamilyOptions,
|
||||
@@ -298,7 +302,14 @@ export function Toolbar({
|
||||
{showHtmlToggle && enabled('html') ? (
|
||||
<>
|
||||
<Separator orientation="vertical" className="mx-1 h-6" />
|
||||
<ToolbarButton active={isHtmlView} title={isHtmlView ? 'WYSIWYG로 전환' : 'HTML 편집'} onClick={onToggleHtmlView}><Code2 className="size-4" /></ToolbarButton>
|
||||
<ToolbarButton
|
||||
active={isHtmlView}
|
||||
disabled={isHtmlView && htmlToWysiwygDisabled}
|
||||
title={isHtmlView && htmlToWysiwygDisabled ? (htmlToWysiwygDisabledReason ?? 'WYSIWYG로 전환할 수 없습니다.') : (isHtmlView ? 'WYSIWYG로 전환' : 'HTML 편집')}
|
||||
onClick={onToggleHtmlView}
|
||||
>
|
||||
<Code2 className="size-4" />
|
||||
</ToolbarButton>
|
||||
{isHtmlView ? (
|
||||
<ToolbarButton title="HTML 정렬" onClick={onFormatHtml}><IndentIncrease className="size-4" /></ToolbarButton>
|
||||
) : null}
|
||||
|
||||
@@ -40,6 +40,11 @@ export type KriEditorAllowedHtml = {
|
||||
attributes?: string[]
|
||||
}
|
||||
|
||||
export type KriEditorWysiwygCompatibility = {
|
||||
compatible: boolean
|
||||
reason?: string
|
||||
}
|
||||
|
||||
export type KriEditorFontFamilyOption = {
|
||||
label: string
|
||||
value: string
|
||||
@@ -80,6 +85,9 @@ export type KriEditorProps = {
|
||||
renderToolbarEnd?: (context: KriEditorToolbarContext) => ReactNode
|
||||
editorExtensions?: Extensions
|
||||
allowedCustomHtml?: KriEditorAllowedHtml
|
||||
enableImageMap?: boolean
|
||||
htmlSanitizer?: (html: string) => string
|
||||
getWysiwygCompatibility?: (html: string) => KriEditorWysiwygCompatibility
|
||||
transformPreviewHtml?: (html: string) => string
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user