260803_02_ENH: kri editor에서 preview에서도 link에 스타일 적용
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "kri-ui-registry",
|
"name": "kri-ui-registry",
|
||||||
"version": "0.9.7",
|
"version": "0.9.8",
|
||||||
"private": true,
|
"private": true,
|
||||||
"description": "KRI internal shadcn UI registry",
|
"description": "KRI internal shadcn UI registry",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
|||||||
@@ -26,6 +26,7 @@ import { FontSize } from "./extensions/font-size";
|
|||||||
import { Div } from "./extensions/div";
|
import { Div } from "./extensions/div";
|
||||||
import { compressImage } from "./utils/imageCompression";
|
import { compressImage } from "./utils/imageCompression";
|
||||||
import { sanitizeHtml } from "./utils/sanitizeHtml";
|
import { sanitizeHtml } from "./utils/sanitizeHtml";
|
||||||
|
import { applyLinkPreviewClass, KRI_EDITOR_LINK_CLASS } from "./utils/linkPreview";
|
||||||
import { Toolbar } from "./toolbar/Toolbar";
|
import { Toolbar } from "./toolbar/Toolbar";
|
||||||
import { useDebouncedChange } from "./hooks/useDebouncedChange";
|
import { useDebouncedChange } from "./hooks/useDebouncedChange";
|
||||||
import { useTextareaInsert } from "./hooks/useTextareaInsert";
|
import { useTextareaInsert } from "./hooks/useTextareaInsert";
|
||||||
@@ -157,7 +158,7 @@ export function KriEditorWysiwyg(props: KriEditorProps) {
|
|||||||
openOnClick: false,
|
openOnClick: false,
|
||||||
linkOnPaste: true,
|
linkOnPaste: true,
|
||||||
HTMLAttributes: {
|
HTMLAttributes: {
|
||||||
class: "cursor-pointer text-blue-600 underline underline-offset-2",
|
class: KRI_EDITOR_LINK_CLASS,
|
||||||
...LINK_HTML_ATTRS,
|
...LINK_HTML_ATTRS,
|
||||||
},
|
},
|
||||||
}),
|
}),
|
||||||
@@ -563,7 +564,8 @@ export function KriEditorWysiwyg(props: KriEditorProps) {
|
|||||||
|
|
||||||
const previewHtml = useMemo(() => {
|
const previewHtml = useMemo(() => {
|
||||||
const sanitized = sanitizeHtmlOpt ? sanitizeHtml(previewSource, allowedCustomHtml) : previewSource;
|
const sanitized = sanitizeHtmlOpt ? sanitizeHtml(previewSource, allowedCustomHtml) : previewSource;
|
||||||
return transformPreviewHtml ? transformPreviewHtml(sanitized) : sanitized;
|
const withLinkStyle = applyLinkPreviewClass(sanitized);
|
||||||
|
return transformPreviewHtml ? transformPreviewHtml(withLinkStyle) : withLinkStyle;
|
||||||
}, [allowedCustomHtml, previewSource, sanitizeHtmlOpt, transformPreviewHtml]);
|
}, [allowedCustomHtml, previewSource, sanitizeHtmlOpt, transformPreviewHtml]);
|
||||||
|
|
||||||
if (!editor) {
|
if (!editor) {
|
||||||
|
|||||||
29
registry/components/kri-editor/utils/linkPreview.ts
Normal file
29
registry/components/kri-editor/utils/linkPreview.ts
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
export const KRI_EDITOR_LINK_CLASS = "cursor-pointer text-blue-600 underline underline-offset-2";
|
||||||
|
|
||||||
|
const CLASS_ATTR_PATTERN = /class\s*=\s*"([^"]*)"|class\s*=\s*'([^']*)'/i;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 미리보기(dangerouslySetInnerHTML)에 쓰이는 원본 HTML은 예전 템플릿이거나
|
||||||
|
* HTML 모드에서 직접 작성된 경우 에디터의 링크 스타일 클래스가 없을 수 있다.
|
||||||
|
* <a> 태그에 KRI_EDITOR_LINK_CLASS를 보정해 항상 동일하게 보이도록 한다.
|
||||||
|
*/
|
||||||
|
export function applyLinkPreviewClass(html: string): string {
|
||||||
|
if (!html) return html;
|
||||||
|
|
||||||
|
return html.replace(/<a\b([^>]*)>/gi, (match, attrs: string) => {
|
||||||
|
const classMatch = attrs.match(CLASS_ATTR_PATTERN);
|
||||||
|
|
||||||
|
if (!classMatch) {
|
||||||
|
return `<a${attrs} class="${KRI_EDITOR_LINK_CLASS}">`;
|
||||||
|
}
|
||||||
|
|
||||||
|
const existingClasses = (classMatch[1] ?? classMatch[2] ?? "").split(/\s+/).filter(Boolean);
|
||||||
|
const missingClasses = KRI_EDITOR_LINK_CLASS.split(" ").filter((c) => !existingClasses.includes(c));
|
||||||
|
|
||||||
|
if (missingClasses.length === 0) return match;
|
||||||
|
|
||||||
|
const mergedClass = [...existingClasses, ...missingClasses].join(" ");
|
||||||
|
const newAttrs = attrs.replace(CLASS_ATTR_PATTERN, `class="${mergedClass}"`);
|
||||||
|
return `<a${newAttrs}>`;
|
||||||
|
});
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user