260907_01_ENH: 에디터 이미지맵 보존 및 SMS 미리보기 문구 지원
This commit is contained in:
@@ -24,3 +24,9 @@ When implementing or changing frontend UI, use this order:
|
||||
- Preserve the installed component's public props, accessibility, keyboard behavior, responsive layout, and loading, empty, error, and disabled states unless the task explicitly changes them.
|
||||
- If the registry lacks a generally reusable capability, propose it in KriUiRegistry; do not modify that repository unless it is explicitly in scope.
|
||||
- Run the consumer repository's focused type-check, lint, tests, and build after installation or upgrade.
|
||||
|
||||
### KriEditor HTML Compatibility
|
||||
|
||||
- `enableImageMap` opt-in preserves `map`, nested `area`, and `img.usemap` HTML in WYSIWYG mode; consumers remain responsible for sanitizing URLs and attributes according to their server policy.
|
||||
- Use `htmlSanitizer` when a consumer must apply a stricter HTML policy than the editor's default DOMPurify profile.
|
||||
- Use `getWysiwygCompatibility` to keep lossy HTML in HTML mode. The callback must return `compatible: false` when WYSIWYG parsing would remove or materially rewrite content.
|
||||
|
||||
@@ -236,8 +236,12 @@ import { KriScatterChart } from '@/components/chart'
|
||||
value={html}
|
||||
onChange={setHtml}
|
||||
onImageUpload={async (file) => upload(file)}
|
||||
enableImageMap
|
||||
htmlSanitizer={sanitizeForServerPolicy}
|
||||
getWysiwygCompatibility={checkWysiwygCompatibility}
|
||||
smsMaxBytes={90}
|
||||
lmsMaxBytes={2000}
|
||||
smsPreviewSuffix={verificationNotice}
|
||||
toolbar={{ image: true, table: false, html: true }}
|
||||
/>
|
||||
```
|
||||
@@ -246,3 +250,7 @@ import { KriScatterChart } from '@/components/chart'
|
||||
- 이미지 업로드 함수는 URL 문자열 또는 `{ url }`을 반환할 수 있습니다.
|
||||
- `toolbar={false}`로 toolbar 전체를 숨길 수 있습니다.
|
||||
- SMS/LMS 기본 제한은 각각 90/2,000바이트입니다.
|
||||
- `enableImageMap`은 `map`, `area`, `img.usemap` 보존을 선택적으로 활성화합니다.
|
||||
- `htmlSanitizer`로 서비스의 서버 정책에 맞는 정제 함수를 주입할 수 있습니다.
|
||||
- `getWysiwygCompatibility`가 `compatible: false`를 반환하면 HTML 원문을 유지하고 WYSIWYG 전환을 비활성화합니다.
|
||||
- `smsPreviewSuffix`는 저장 본문에는 포함하지 않고 SMS 미리보기와 바이트 계산에만 반영합니다.
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "kri-ui-registry",
|
||||
"version": "0.11.7",
|
||||
"version": "0.12.0",
|
||||
"private": true,
|
||||
"description": "KRI internal shadcn UI registry",
|
||||
"scripts": {
|
||||
|
||||
@@ -26,6 +26,7 @@ export function KriEditorSms({
|
||||
isSmsReadOnly = false,
|
||||
smsMaxBytes = 90,
|
||||
lmsMaxBytes = 2000,
|
||||
smsPreviewSuffix = '',
|
||||
}: SmsEditorProps) {
|
||||
const [text, setText] = React.useState(value || '')
|
||||
const textareaRef = React.useRef<HTMLTextAreaElement | null>(null)
|
||||
@@ -33,7 +34,8 @@ export function KriEditorSms({
|
||||
const lastTypeRef = React.useRef<'SMS' | 'LMS' | null>(null)
|
||||
const { scheduleChange, flushChange } = useDebouncedChange(onChange, debounceMs)
|
||||
const textareaInsert = useTextareaInsert(textareaRef)
|
||||
const byteSize = React.useMemo(() => calculateBytes(text), [text])
|
||||
const previewText = smsPreviewSuffix ? `${text}\n${smsPreviewSuffix}` : text
|
||||
const byteSize = React.useMemo(() => calculateBytes(previewText), [previewText])
|
||||
const messageType: 'SMS' | 'LMS' = byteSize > smsMaxBytes || Boolean(smsTitle) ? 'LMS' : 'SMS'
|
||||
const maxBytes = messageType === 'SMS' ? smsMaxBytes : lmsMaxBytes
|
||||
|
||||
@@ -97,7 +99,8 @@ export function KriEditorSms({
|
||||
value={text}
|
||||
onChange={(event) => {
|
||||
const next = event.target.value
|
||||
if (calculateBytes(next) <= lmsMaxBytes) commitText(next)
|
||||
const nextPreview = smsPreviewSuffix ? `${next}\n${smsPreviewSuffix}` : next
|
||||
if (calculateBytes(nextPreview) <= lmsMaxBytes) commitText(next)
|
||||
}}
|
||||
onBlur={() => flushChange(latestTextRef.current)}
|
||||
placeholder={placeholder}
|
||||
@@ -105,6 +108,11 @@ export function KriEditorSms({
|
||||
style={{ minHeight: smsTitle ? '320px' : '360px' }}
|
||||
{...textareaInsert.bindSelectionHandlers}
|
||||
/>
|
||||
{smsPreviewSuffix ? (
|
||||
<div className="whitespace-pre-wrap px-4 pb-4 text-sm leading-relaxed text-gray-700">
|
||||
{smsPreviewSuffix}
|
||||
</div>
|
||||
) : null}
|
||||
</div>
|
||||
{byteSize > maxBytes ? (
|
||||
<p className="mt-2 px-2 text-center text-xs font-medium text-red-600">
|
||||
|
||||
@@ -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
registry/components/kri-editor/extensions/image-map.ts
Normal file
93
registry/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
|
||||
@@ -74,11 +79,15 @@ export type KriEditorProps = {
|
||||
isSmsReadOnly?: boolean
|
||||
smsMaxBytes?: number
|
||||
lmsMaxBytes?: number
|
||||
smsPreviewSuffix?: string
|
||||
toolbar?: KriEditorToolbarConfig | false
|
||||
fontFamilyOptions?: KriEditorFontFamilyOption[]
|
||||
renderToolbarEnd?: (context: KriEditorToolbarContext) => ReactNode
|
||||
editorExtensions?: Extensions
|
||||
allowedCustomHtml?: KriEditorAllowedHtml
|
||||
enableImageMap?: boolean
|
||||
htmlSanitizer?: (html: string) => string
|
||||
getWysiwygCompatibility?: (html: string) => KriEditorWysiwygCompatibility
|
||||
transformPreviewHtml?: (html: string) => string
|
||||
}
|
||||
|
||||
|
||||
@@ -379,6 +379,11 @@
|
||||
"type": "registry:component",
|
||||
"target": "@components/kri-editor/extensions/font-size.ts"
|
||||
},
|
||||
{
|
||||
"path": "kri-editor/extensions/image-map.ts",
|
||||
"type": "registry:component",
|
||||
"target": "@components/kri-editor/extensions/image-map.ts"
|
||||
},
|
||||
{
|
||||
"path": "kri-editor/hooks/useDebouncedChange.ts",
|
||||
"type": "registry:component",
|
||||
|
||||
Reference in New Issue
Block a user