260115_01_ENH : SMS 에디터 수정 및 이미지 리로드 관련 버그 수정
This commit is contained in:
@@ -1,13 +1,15 @@
|
||||
"use client";
|
||||
|
||||
import * as React from "react";
|
||||
import { cn } from "@/lib/utils";
|
||||
import { toast } from "sonner";
|
||||
|
||||
import type { KriEditorProps } from "./types";
|
||||
import { cn } from "./utils/cn";
|
||||
import { compressImage } from "./utils/imageCompression";
|
||||
import { useDebouncedChange } from "./hooks/useDebouncedChange";
|
||||
import { sanitizeHtmlForPreview } from "./utils/sanitizeHtml";
|
||||
import {useCallback} from "react";
|
||||
import {toast} from "sonner";
|
||||
import {compressImage} from "@/components/kri-editor/utils/imageCompression";
|
||||
import { useTextareaInsert } from "./hooks/useTextareaInsert";
|
||||
|
||||
type HtmlOnlyProps = Omit<KriEditorProps, "mode"> & {
|
||||
mode: "html";
|
||||
@@ -38,19 +40,11 @@ export function KriEditorHtmlOnly(props: HtmlOnlyProps) {
|
||||
const textareaRef = React.useRef<HTMLTextAreaElement | null>(null);
|
||||
const fileRef = React.useRef<HTMLInputElement | null>(null);
|
||||
const latestHtmlRef = React.useRef(html);
|
||||
latestHtmlRef.current = html;
|
||||
|
||||
const canImageUpload = Boolean(onImageUpload);
|
||||
|
||||
const { scheduleChange, flushChange } = useDebouncedChange(onChange, debounceMs);
|
||||
|
||||
// 외부 value 변경 시 동기화
|
||||
React.useEffect(() => {
|
||||
if ((value || "") !== latestHtmlRef.current) {
|
||||
setHtml(value || "");
|
||||
latestHtmlRef.current = value || "";
|
||||
}
|
||||
}, [value]);
|
||||
const ti = useTextareaInsert(textareaRef);
|
||||
|
||||
const handleTextChange = (e: React.ChangeEvent<HTMLTextAreaElement>) => {
|
||||
const next = e.target.value;
|
||||
@@ -156,6 +150,46 @@ export function KriEditorHtmlOnly(props: HtmlOnlyProps) {
|
||||
[html],
|
||||
);
|
||||
|
||||
// 외부 value 변경 시 동기화
|
||||
React.useEffect(() => {
|
||||
if ((value || "") !== latestHtmlRef.current) {
|
||||
setHtml(value || "");
|
||||
latestHtmlRef.current = value || "";
|
||||
}
|
||||
}, [value]);
|
||||
|
||||
React.useEffect(() => {
|
||||
if (textareaRef.current && props.onEditorReady) {
|
||||
props.onEditorReady(textareaRef.current);
|
||||
}
|
||||
},[props.onEditorReady])
|
||||
|
||||
React.useEffect(() => {
|
||||
if (!props.onCommandReady) return;
|
||||
|
||||
props.onCommandReady({
|
||||
focus: () => textareaRef.current?.focus(),
|
||||
insertText: (placeholder: string) => {
|
||||
const current = latestHtmlRef.current ?? "";
|
||||
|
||||
ti.insertText(
|
||||
placeholder,
|
||||
current,
|
||||
(next) => {
|
||||
setHtml(next);
|
||||
latestHtmlRef.current = next;
|
||||
},
|
||||
(next) => {
|
||||
// html 모드도 debounce 쓰면 flush 권장
|
||||
flushChange(next);
|
||||
},
|
||||
);
|
||||
},
|
||||
});
|
||||
|
||||
return () => props.onCommandReady?.(null);
|
||||
}, [props.onCommandReady, ti, flushChange]);
|
||||
|
||||
return (
|
||||
<div className={cn("rounded-md border bg-white", className)}>
|
||||
{label && (
|
||||
@@ -186,6 +220,7 @@ export function KriEditorHtmlOnly(props: HtmlOnlyProps) {
|
||||
onBlur={handleBlur}
|
||||
placeholder={placeholder}
|
||||
rows={rows}
|
||||
{...ti.bindSelectionHandlers}
|
||||
/>
|
||||
|
||||
<input
|
||||
|
||||
Reference in New Issue
Block a user