260810_06_ENH: 에디터 html에 프리티어 적용
This commit is contained in:
@@ -9,6 +9,7 @@ import { compressImage } from './utils/imageCompression'
|
||||
import { useDebouncedChange } from './hooks/useDebouncedChange'
|
||||
import { sanitizeHtmlForPreview } from './utils/sanitizeHtml'
|
||||
import { preserveEmptyParagraphs } from './utils/emptyParagraph'
|
||||
import { formatHtmlSource } from './utils/formatHtml'
|
||||
import { useTextareaInsert } from './hooks/useTextareaInsert'
|
||||
|
||||
type HtmlOnlyProps = Omit<KriEditorProps, 'mode'> & { mode: 'html' }
|
||||
@@ -47,12 +48,31 @@ export function KriEditorHtmlOnly({
|
||||
else scheduleChange(next)
|
||||
}, [flushChange, scheduleChange])
|
||||
|
||||
// 사용자가 타이핑한 값이 아니라 외부에서 새로 세팅된 데이터일 때만(최초 마운트 포함)
|
||||
// prettier로 포맷팅해서 반영한다 — 편집 중(commitHtml 경로)에는 절대 재포맷하지 않는다.
|
||||
const applyExternalValue = React.useCallback((next: string) => {
|
||||
setHtml(next)
|
||||
latestHtmlRef.current = next
|
||||
formatHtmlSource(next)
|
||||
.then((formatted) => {
|
||||
if (latestHtmlRef.current !== next) return // 그 사이 값이 또 바뀌었으면 건너뜀
|
||||
setHtml(formatted)
|
||||
latestHtmlRef.current = formatted
|
||||
})
|
||||
.catch(() => {})
|
||||
}, [])
|
||||
|
||||
React.useEffect(() => {
|
||||
applyExternalValue(value || '')
|
||||
// 최초 마운트 시 1회만 실행 — 이후 value 변경은 아래 effect가 처리한다.
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [])
|
||||
|
||||
React.useEffect(() => {
|
||||
if ((value || '') !== latestHtmlRef.current) {
|
||||
setHtml(value || '')
|
||||
latestHtmlRef.current = value || ''
|
||||
applyExternalValue(value || '')
|
||||
}
|
||||
}, [value])
|
||||
}, [value, applyExternalValue])
|
||||
|
||||
React.useEffect(() => {
|
||||
onEditorReady?.(textareaRef.current)
|
||||
@@ -122,6 +142,16 @@ export function KriEditorHtmlOnly({
|
||||
}
|
||||
}
|
||||
|
||||
const handleFormatHtml = React.useCallback(async () => {
|
||||
try {
|
||||
const formatted = await formatHtmlSource(latestHtmlRef.current)
|
||||
commitHtml(formatted)
|
||||
} catch (error) {
|
||||
toast.error('HTML 정렬에 실패했습니다.')
|
||||
console.error(error)
|
||||
}
|
||||
}, [commitHtml])
|
||||
|
||||
const previewHtml = React.useMemo(
|
||||
() => sanitizeHtmlForPreview(preserveEmptyParagraphs(html)),
|
||||
[html],
|
||||
@@ -133,11 +163,16 @@ export function KriEditorHtmlOnly({
|
||||
<div className="flex flex-col gap-2 px-3 py-2">
|
||||
<div className="text-muted-foreground flex items-center justify-between text-xs">
|
||||
<span>HTML 모드</span>
|
||||
{onImageUpload ? (
|
||||
<button type="button" onClick={() => fileRef.current?.click()} className="rounded border px-2 py-1 hover:bg-muted">
|
||||
이미지 업로드
|
||||
<div className="flex items-center gap-2">
|
||||
<button type="button" onClick={handleFormatHtml} className="rounded border px-2 py-1 hover:bg-muted">
|
||||
정렬
|
||||
</button>
|
||||
) : null}
|
||||
{onImageUpload ? (
|
||||
<button type="button" onClick={() => fileRef.current?.click()} className="rounded border px-2 py-1 hover:bg-muted">
|
||||
이미지 업로드
|
||||
</button>
|
||||
) : null}
|
||||
</div>
|
||||
</div>
|
||||
<textarea
|
||||
ref={textareaRef}
|
||||
|
||||
@@ -29,6 +29,7 @@ import { compressImage } from "./utils/imageCompression";
|
||||
import { sanitizeHtml } from "./utils/sanitizeHtml";
|
||||
import { preserveEmptyParagraphs } from "./utils/emptyParagraph";
|
||||
import { DEFAULT_FONT_FAMILY_OPTIONS } from "./utils/fontFamilyOptions";
|
||||
import { formatHtmlSource } from "./utils/formatHtml";
|
||||
import { applyLinkPreviewClass, KRI_EDITOR_LINK_CLASS } from "./utils/linkPreview";
|
||||
import { Toolbar } from "./toolbar/Toolbar";
|
||||
import { useDebouncedChange } from "./hooks/useDebouncedChange";
|
||||
@@ -456,8 +457,20 @@ export function KriEditorWysiwyg(props: KriEditorProps) {
|
||||
if (!editor) return;
|
||||
|
||||
if (viewRef.current === "wysiwyg") {
|
||||
htmlDraftRef.current = editor.getHTML();
|
||||
const raw = editor.getHTML();
|
||||
htmlDraftRef.current = raw;
|
||||
setView("html");
|
||||
|
||||
// WYSIWYG에서 막 넘어온, 사용자가 직접 타이핑하지 않은 HTML이므로
|
||||
// 화면 표시용으로만 정렬한다(onChange는 호출하지 않음 — 저장값은 그대로 유지).
|
||||
formatHtmlSource(raw)
|
||||
.then((formatted) => {
|
||||
if (viewRef.current !== "html" || htmlDraftRef.current !== raw) return;
|
||||
htmlDraftRef.current = formatted;
|
||||
if (htmlTextareaRef.current) htmlTextareaRef.current.value = formatted;
|
||||
setPreviewSource(formatted);
|
||||
})
|
||||
.catch(() => {});
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -469,6 +482,19 @@ export function KriEditorWysiwyg(props: KriEditorProps) {
|
||||
setView("wysiwyg");
|
||||
}, [allowedCustomHtml, editor, flushChange, sanitizeHtmlOpt]);
|
||||
|
||||
const formatHtmlView = useCallback(async () => {
|
||||
try {
|
||||
const formatted = await formatHtmlSource(htmlDraftRef.current);
|
||||
htmlDraftRef.current = formatted;
|
||||
if (htmlTextareaRef.current) htmlTextareaRef.current.value = formatted;
|
||||
setPreviewSource(formatted);
|
||||
scheduleChange(preserveEmptyParagraphs(formatted));
|
||||
} catch (err) {
|
||||
toast.error("HTML 정렬에 실패했습니다.");
|
||||
console.error(err);
|
||||
}
|
||||
}, [scheduleChange]);
|
||||
|
||||
useEffect(() => {
|
||||
if (!editor) return;
|
||||
|
||||
@@ -615,6 +641,7 @@ export function KriEditorWysiwyg(props: KriEditorProps) {
|
||||
showHtmlToggle={showHtmlToggle}
|
||||
isHtmlView={view === "html"}
|
||||
onToggleHtmlView={onToggleHtmlView}
|
||||
onFormatHtml={formatHtmlView}
|
||||
fontFamilyOptions={fontFamilyOptions}
|
||||
onSetFontFamily={setFontFamily}
|
||||
onSetFontSize={setFontSize}
|
||||
|
||||
@@ -10,6 +10,7 @@ import {
|
||||
Code2,
|
||||
Highlighter,
|
||||
Image as ImageIcon,
|
||||
IndentIncrease,
|
||||
Italic,
|
||||
Link2,
|
||||
List,
|
||||
@@ -38,6 +39,7 @@ interface ToolbarProps {
|
||||
showHtmlToggle: boolean
|
||||
isHtmlView: boolean
|
||||
onToggleHtmlView: () => void
|
||||
onFormatHtml: () => void
|
||||
fontFamilyOptions: KriEditorFontFamilyOption[]
|
||||
onSetFontFamily: (value: string) => void
|
||||
onSetFontSize: (value: string) => void
|
||||
@@ -76,6 +78,7 @@ export function Toolbar({
|
||||
showHtmlToggle,
|
||||
isHtmlView,
|
||||
onToggleHtmlView,
|
||||
onFormatHtml,
|
||||
fontFamilyOptions,
|
||||
onSetFontFamily,
|
||||
onSetFontSize,
|
||||
@@ -296,6 +299,9 @@ export function Toolbar({
|
||||
<>
|
||||
<Separator orientation="vertical" className="mx-1 h-6" />
|
||||
<ToolbarButton active={isHtmlView} title={isHtmlView ? 'WYSIWYG로 전환' : 'HTML 편집'} onClick={onToggleHtmlView}><Code2 className="size-4" /></ToolbarButton>
|
||||
{isHtmlView ? (
|
||||
<ToolbarButton title="HTML 정렬" onClick={onFormatHtml}><IndentIncrease className="size-4" /></ToolbarButton>
|
||||
) : null}
|
||||
</>
|
||||
) : null}
|
||||
{customContent ? (
|
||||
|
||||
21
registry/components/kri-editor/utils/formatHtml.ts
Normal file
21
registry/components/kri-editor/utils/formatHtml.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
/**
|
||||
* HTML 소스 보기를 사람이 읽기 좋은 형태로 들여쓴다.
|
||||
* prettier/standalone + html 플러그인을 동적 import로 불러와, 실제로
|
||||
* HTML 모드를 사용할 때만 해당 청크가 로드되도록 한다(초기 번들 크기 영향 없음).
|
||||
*/
|
||||
export async function formatHtmlSource(html: string): Promise<string> {
|
||||
if (!html.trim()) return html;
|
||||
|
||||
const [{ format }, htmlPlugin] = await Promise.all([
|
||||
import("prettier/standalone"),
|
||||
import("prettier/plugins/html"),
|
||||
]);
|
||||
|
||||
return format(html, {
|
||||
parser: "html",
|
||||
plugins: [htmlPlugin],
|
||||
printWidth: 100,
|
||||
// 인라인 요소(span/a/mark 등) 사이 공백은 실제 렌더링에 영향을 주므로 보존한다.
|
||||
htmlWhitespaceSensitivity: "css",
|
||||
});
|
||||
}
|
||||
@@ -345,6 +345,7 @@
|
||||
"@tiptap/starter-kit",
|
||||
"dompurify",
|
||||
"lucide-react",
|
||||
"prettier",
|
||||
"sonner"
|
||||
],
|
||||
"files": [
|
||||
@@ -423,6 +424,11 @@
|
||||
"type": "registry:component",
|
||||
"target": "@components/kri-editor/utils/fontFamilyOptions.ts"
|
||||
},
|
||||
{
|
||||
"path": "kri-editor/utils/formatHtml.ts",
|
||||
"type": "registry:component",
|
||||
"target": "@components/kri-editor/utils/formatHtml.ts"
|
||||
},
|
||||
{
|
||||
"path": "kri-editor/utils/imageCompression.ts",
|
||||
"type": "registry:component",
|
||||
|
||||
Reference in New Issue
Block a user