260810_06_ENH: 에디터 html에 프리티어 적용
This commit is contained in:
17
package-lock.json
generated
17
package-lock.json
generated
@@ -31,6 +31,7 @@
|
|||||||
"echarts": "^6.1.0",
|
"echarts": "^6.1.0",
|
||||||
"echarts-for-react": "^3.0.6",
|
"echarts-for-react": "^3.0.6",
|
||||||
"lucide-react": "^1.23.0",
|
"lucide-react": "^1.23.0",
|
||||||
|
"prettier": "^3.9.6",
|
||||||
"rimraf": "^6.0.0",
|
"rimraf": "^6.0.0",
|
||||||
"serve": "^14.2.4",
|
"serve": "^14.2.4",
|
||||||
"shadcn": "^4.8.3",
|
"shadcn": "^4.8.3",
|
||||||
@@ -4581,6 +4582,22 @@
|
|||||||
"url": "https://github.com/sponsors/sindresorhus"
|
"url": "https://github.com/sponsors/sindresorhus"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/prettier": {
|
||||||
|
"version": "3.9.6",
|
||||||
|
"resolved": "https://registry.npmjs.org/prettier/-/prettier-3.9.6.tgz",
|
||||||
|
"integrity": "sha512-OpN0zzVdiaiAhxpuuj5efpIS4sY9j7bY6uR5mnj5yPzGkdkjNKSJeUThPb60Jw29QuAZgA4o+/iB49kFiaBX6g==",
|
||||||
|
"dev": true,
|
||||||
|
"license": "MIT",
|
||||||
|
"bin": {
|
||||||
|
"prettier": "bin/prettier.cjs"
|
||||||
|
},
|
||||||
|
"engines": {
|
||||||
|
"node": ">=14"
|
||||||
|
},
|
||||||
|
"funding": {
|
||||||
|
"url": "https://github.com/prettier/prettier?sponsor=1"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/pretty-ms": {
|
"node_modules/pretty-ms": {
|
||||||
"version": "9.3.0",
|
"version": "9.3.0",
|
||||||
"resolved": "https://registry.npmjs.org/pretty-ms/-/pretty-ms-9.3.0.tgz",
|
"resolved": "https://registry.npmjs.org/pretty-ms/-/pretty-ms-9.3.0.tgz",
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "kri-ui-registry",
|
"name": "kri-ui-registry",
|
||||||
"version": "0.11.5",
|
"version": "0.11.6",
|
||||||
"private": true,
|
"private": true,
|
||||||
"description": "KRI internal shadcn UI registry",
|
"description": "KRI internal shadcn UI registry",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
@@ -33,6 +33,7 @@
|
|||||||
"echarts": "^6.1.0",
|
"echarts": "^6.1.0",
|
||||||
"echarts-for-react": "^3.0.6",
|
"echarts-for-react": "^3.0.6",
|
||||||
"lucide-react": "^1.23.0",
|
"lucide-react": "^1.23.0",
|
||||||
|
"prettier": "^3.9.6",
|
||||||
"rimraf": "^6.0.0",
|
"rimraf": "^6.0.0",
|
||||||
"serve": "^14.2.4",
|
"serve": "^14.2.4",
|
||||||
"shadcn": "^4.8.3",
|
"shadcn": "^4.8.3",
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ import { compressImage } from './utils/imageCompression'
|
|||||||
import { useDebouncedChange } from './hooks/useDebouncedChange'
|
import { useDebouncedChange } from './hooks/useDebouncedChange'
|
||||||
import { sanitizeHtmlForPreview } from './utils/sanitizeHtml'
|
import { sanitizeHtmlForPreview } from './utils/sanitizeHtml'
|
||||||
import { preserveEmptyParagraphs } from './utils/emptyParagraph'
|
import { preserveEmptyParagraphs } from './utils/emptyParagraph'
|
||||||
|
import { formatHtmlSource } from './utils/formatHtml'
|
||||||
import { useTextareaInsert } from './hooks/useTextareaInsert'
|
import { useTextareaInsert } from './hooks/useTextareaInsert'
|
||||||
|
|
||||||
type HtmlOnlyProps = Omit<KriEditorProps, 'mode'> & { mode: 'html' }
|
type HtmlOnlyProps = Omit<KriEditorProps, 'mode'> & { mode: 'html' }
|
||||||
@@ -47,12 +48,31 @@ export function KriEditorHtmlOnly({
|
|||||||
else scheduleChange(next)
|
else scheduleChange(next)
|
||||||
}, [flushChange, scheduleChange])
|
}, [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(() => {
|
React.useEffect(() => {
|
||||||
if ((value || '') !== latestHtmlRef.current) {
|
if ((value || '') !== latestHtmlRef.current) {
|
||||||
setHtml(value || '')
|
applyExternalValue(value || '')
|
||||||
latestHtmlRef.current = value || ''
|
|
||||||
}
|
}
|
||||||
}, [value])
|
}, [value, applyExternalValue])
|
||||||
|
|
||||||
React.useEffect(() => {
|
React.useEffect(() => {
|
||||||
onEditorReady?.(textareaRef.current)
|
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(
|
const previewHtml = React.useMemo(
|
||||||
() => sanitizeHtmlForPreview(preserveEmptyParagraphs(html)),
|
() => sanitizeHtmlForPreview(preserveEmptyParagraphs(html)),
|
||||||
[html],
|
[html],
|
||||||
@@ -133,12 +163,17 @@ export function KriEditorHtmlOnly({
|
|||||||
<div className="flex flex-col gap-2 px-3 py-2">
|
<div className="flex flex-col gap-2 px-3 py-2">
|
||||||
<div className="text-muted-foreground flex items-center justify-between text-xs">
|
<div className="text-muted-foreground flex items-center justify-between text-xs">
|
||||||
<span>HTML 모드</span>
|
<span>HTML 모드</span>
|
||||||
|
<div className="flex items-center gap-2">
|
||||||
|
<button type="button" onClick={handleFormatHtml} className="rounded border px-2 py-1 hover:bg-muted">
|
||||||
|
정렬
|
||||||
|
</button>
|
||||||
{onImageUpload ? (
|
{onImageUpload ? (
|
||||||
<button type="button" onClick={() => fileRef.current?.click()} className="rounded border px-2 py-1 hover:bg-muted">
|
<button type="button" onClick={() => fileRef.current?.click()} className="rounded border px-2 py-1 hover:bg-muted">
|
||||||
이미지 업로드
|
이미지 업로드
|
||||||
</button>
|
</button>
|
||||||
) : null}
|
) : null}
|
||||||
</div>
|
</div>
|
||||||
|
</div>
|
||||||
<textarea
|
<textarea
|
||||||
ref={textareaRef}
|
ref={textareaRef}
|
||||||
className="min-h-[200px] w-full resize-y rounded-md border bg-background p-2 font-mono text-sm outline-none"
|
className="min-h-[200px] w-full resize-y rounded-md border bg-background p-2 font-mono text-sm outline-none"
|
||||||
|
|||||||
@@ -29,6 +29,7 @@ import { compressImage } from "./utils/imageCompression";
|
|||||||
import { sanitizeHtml } from "./utils/sanitizeHtml";
|
import { sanitizeHtml } from "./utils/sanitizeHtml";
|
||||||
import { preserveEmptyParagraphs } from "./utils/emptyParagraph";
|
import { preserveEmptyParagraphs } from "./utils/emptyParagraph";
|
||||||
import { DEFAULT_FONT_FAMILY_OPTIONS } from "./utils/fontFamilyOptions";
|
import { DEFAULT_FONT_FAMILY_OPTIONS } from "./utils/fontFamilyOptions";
|
||||||
|
import { formatHtmlSource } from "./utils/formatHtml";
|
||||||
import { applyLinkPreviewClass, KRI_EDITOR_LINK_CLASS } from "./utils/linkPreview";
|
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";
|
||||||
@@ -456,8 +457,20 @@ export function KriEditorWysiwyg(props: KriEditorProps) {
|
|||||||
if (!editor) return;
|
if (!editor) return;
|
||||||
|
|
||||||
if (viewRef.current === "wysiwyg") {
|
if (viewRef.current === "wysiwyg") {
|
||||||
htmlDraftRef.current = editor.getHTML();
|
const raw = editor.getHTML();
|
||||||
|
htmlDraftRef.current = raw;
|
||||||
setView("html");
|
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;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -469,6 +482,19 @@ export function KriEditorWysiwyg(props: KriEditorProps) {
|
|||||||
setView("wysiwyg");
|
setView("wysiwyg");
|
||||||
}, [allowedCustomHtml, editor, flushChange, sanitizeHtmlOpt]);
|
}, [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(() => {
|
useEffect(() => {
|
||||||
if (!editor) return;
|
if (!editor) return;
|
||||||
|
|
||||||
@@ -615,6 +641,7 @@ export function KriEditorWysiwyg(props: KriEditorProps) {
|
|||||||
showHtmlToggle={showHtmlToggle}
|
showHtmlToggle={showHtmlToggle}
|
||||||
isHtmlView={view === "html"}
|
isHtmlView={view === "html"}
|
||||||
onToggleHtmlView={onToggleHtmlView}
|
onToggleHtmlView={onToggleHtmlView}
|
||||||
|
onFormatHtml={formatHtmlView}
|
||||||
fontFamilyOptions={fontFamilyOptions}
|
fontFamilyOptions={fontFamilyOptions}
|
||||||
onSetFontFamily={setFontFamily}
|
onSetFontFamily={setFontFamily}
|
||||||
onSetFontSize={setFontSize}
|
onSetFontSize={setFontSize}
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ import {
|
|||||||
Code2,
|
Code2,
|
||||||
Highlighter,
|
Highlighter,
|
||||||
Image as ImageIcon,
|
Image as ImageIcon,
|
||||||
|
IndentIncrease,
|
||||||
Italic,
|
Italic,
|
||||||
Link2,
|
Link2,
|
||||||
List,
|
List,
|
||||||
@@ -38,6 +39,7 @@ interface ToolbarProps {
|
|||||||
showHtmlToggle: boolean
|
showHtmlToggle: boolean
|
||||||
isHtmlView: boolean
|
isHtmlView: boolean
|
||||||
onToggleHtmlView: () => void
|
onToggleHtmlView: () => void
|
||||||
|
onFormatHtml: () => void
|
||||||
fontFamilyOptions: KriEditorFontFamilyOption[]
|
fontFamilyOptions: KriEditorFontFamilyOption[]
|
||||||
onSetFontFamily: (value: string) => void
|
onSetFontFamily: (value: string) => void
|
||||||
onSetFontSize: (value: string) => void
|
onSetFontSize: (value: string) => void
|
||||||
@@ -76,6 +78,7 @@ export function Toolbar({
|
|||||||
showHtmlToggle,
|
showHtmlToggle,
|
||||||
isHtmlView,
|
isHtmlView,
|
||||||
onToggleHtmlView,
|
onToggleHtmlView,
|
||||||
|
onFormatHtml,
|
||||||
fontFamilyOptions,
|
fontFamilyOptions,
|
||||||
onSetFontFamily,
|
onSetFontFamily,
|
||||||
onSetFontSize,
|
onSetFontSize,
|
||||||
@@ -296,6 +299,9 @@ export function Toolbar({
|
|||||||
<>
|
<>
|
||||||
<Separator orientation="vertical" className="mx-1 h-6" />
|
<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} title={isHtmlView ? 'WYSIWYG로 전환' : 'HTML 편집'} onClick={onToggleHtmlView}><Code2 className="size-4" /></ToolbarButton>
|
||||||
|
{isHtmlView ? (
|
||||||
|
<ToolbarButton title="HTML 정렬" onClick={onFormatHtml}><IndentIncrease className="size-4" /></ToolbarButton>
|
||||||
|
) : null}
|
||||||
</>
|
</>
|
||||||
) : null}
|
) : null}
|
||||||
{customContent ? (
|
{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",
|
"@tiptap/starter-kit",
|
||||||
"dompurify",
|
"dompurify",
|
||||||
"lucide-react",
|
"lucide-react",
|
||||||
|
"prettier",
|
||||||
"sonner"
|
"sonner"
|
||||||
],
|
],
|
||||||
"files": [
|
"files": [
|
||||||
@@ -423,6 +424,11 @@
|
|||||||
"type": "registry:component",
|
"type": "registry:component",
|
||||||
"target": "@components/kri-editor/utils/fontFamilyOptions.ts"
|
"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",
|
"path": "kri-editor/utils/imageCompression.ts",
|
||||||
"type": "registry:component",
|
"type": "registry:component",
|
||||||
|
|||||||
Reference in New Issue
Block a user