260115_01_ENH : SMS 에디터 수정 및 이미지 리로드 관련 버그 수정
This commit is contained in:
@@ -18,15 +18,16 @@ import TableHeader from "@tiptap/extension-table-header";
|
||||
import TableCell from "@tiptap/extension-table-cell";
|
||||
|
||||
import { toast } from "sonner";
|
||||
import { cn } from "@/lib/utils";
|
||||
|
||||
import type { KriEditorProps, KriEditorMode, ToolbarState } from "./types";
|
||||
import { FontSize } from "./extensions/font-size";
|
||||
import { Div } from "./extensions/div";
|
||||
import { cn } from "./utils/cn";
|
||||
import { compressImage } from "./utils/imageCompression";
|
||||
import { sanitizeHtml } from "./utils/sanitizeHtml";
|
||||
import { Toolbar } from "./toolbar/Toolbar";
|
||||
import { useDebouncedChange } from "./hooks/useDebouncedChange";
|
||||
import { useTextareaInsert } from "./hooks/useTextareaInsert";
|
||||
|
||||
type ViewMode = "wysiwyg" | "html";
|
||||
|
||||
@@ -65,6 +66,7 @@ function getToolbarState(editor: Editor): ToolbarState {
|
||||
|
||||
export function KriEditorWysiwyg(props: KriEditorProps) {
|
||||
const {
|
||||
|
||||
label,
|
||||
value,
|
||||
onChange,
|
||||
@@ -91,7 +93,6 @@ export function KriEditorWysiwyg(props: KriEditorProps) {
|
||||
} = props;
|
||||
|
||||
const mode = resolveMode(modeProp, editorMode);
|
||||
const isLmsMode = mode === "lms";
|
||||
const showHtmlToggle = mode === "both";
|
||||
|
||||
const LINK_HTML_ATTRS = {
|
||||
@@ -102,9 +103,13 @@ export function KriEditorWysiwyg(props: KriEditorProps) {
|
||||
const { scheduleChange, flushChange, optimisticHtmlRef } = useDebouncedChange(onChange, debounceMs);
|
||||
|
||||
const fileRef = useRef<HTMLInputElement | null>(null);
|
||||
const htmlTextareaRef = useRef<HTMLTextAreaElement | null>(null);
|
||||
const htmlTi = useTextareaInsert(htmlTextareaRef);
|
||||
|
||||
const [view, setView] = useState<ViewMode>("wysiwyg");
|
||||
const [wysiwygKey, setWysiwygKey] = useState<number>(0);
|
||||
const viewRef = useRef<ViewMode>("wysiwyg");
|
||||
|
||||
useEffect(() => {
|
||||
viewRef.current = view;
|
||||
}, [view]);
|
||||
@@ -133,26 +138,12 @@ export function KriEditorWysiwyg(props: KriEditorProps) {
|
||||
immediatelyRender: false,
|
||||
|
||||
extensions: [
|
||||
isLmsMode
|
||||
? StarterKit.configure({
|
||||
heading: false,
|
||||
bulletList: false,
|
||||
orderedList: false,
|
||||
listItem: false,
|
||||
blockquote: false,
|
||||
codeBlock: false,
|
||||
code: false,
|
||||
strike: false,
|
||||
bold: false,
|
||||
italic: false,
|
||||
horizontalRule: false,
|
||||
})
|
||||
: StarterKit.configure({
|
||||
heading: false,
|
||||
blockquote: false,
|
||||
codeBlock: false,
|
||||
code: false,
|
||||
}),
|
||||
StarterKit.configure({
|
||||
heading: false,
|
||||
blockquote: false,
|
||||
codeBlock: false,
|
||||
code: false,
|
||||
}),
|
||||
|
||||
Underline,
|
||||
TextStyle,
|
||||
@@ -183,6 +174,7 @@ export function KriEditorWysiwyg(props: KriEditorProps) {
|
||||
allowBase64,
|
||||
HTMLAttributes: {
|
||||
class: "max-w-full h-auto",
|
||||
"data-kri-image": "true",
|
||||
},
|
||||
resize: {
|
||||
enabled: true,
|
||||
@@ -259,52 +251,11 @@ export function KriEditorWysiwyg(props: KriEditorProps) {
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
},[wysiwygKey]);
|
||||
|
||||
useEffect(() => {
|
||||
if (!editor) return;
|
||||
|
||||
const sync = () => {
|
||||
selectionRef.current = { from: editor.state.selection.from, to: editor.state.selection.to };
|
||||
setTb(getToolbarState(editor));
|
||||
};
|
||||
|
||||
sync();
|
||||
editor.on("selectionUpdate", sync);
|
||||
editor.on("transaction", sync);
|
||||
|
||||
return () => {
|
||||
editor.off("selectionUpdate", sync);
|
||||
editor.off("transaction", sync);
|
||||
};
|
||||
}, [editor]);
|
||||
|
||||
useEffect(() => {
|
||||
if (!editor) return;
|
||||
if (viewRef.current === "html") return;
|
||||
|
||||
const current = editor.getHTML();
|
||||
if (value != null && value !== current && value !== optimisticHtmlRef.current) {
|
||||
editor.commands.setContent(value, {
|
||||
emitUpdate: false,
|
||||
parseOptions: { preserveWhitespace: "full" },
|
||||
});
|
||||
}
|
||||
}, [value, editor, optimisticHtmlRef]);
|
||||
|
||||
useEffect(() => {
|
||||
if (!editor) return;
|
||||
|
||||
const onBlur = () => {
|
||||
if (viewRef.current === "html") flushChange(htmlDraftRef.current);
|
||||
else flushChange(editor.getHTML());
|
||||
};
|
||||
|
||||
editor.on("blur", onBlur);
|
||||
return () => {
|
||||
editor.off("blur", onBlur);
|
||||
};
|
||||
}, [editor, flushChange]);
|
||||
console.log(wysiwygKey)
|
||||
}, [wysiwygKey]);
|
||||
|
||||
const restoreSelection = useCallback(() => {
|
||||
if (!editor) return;
|
||||
@@ -504,6 +455,118 @@ const normalizeLinkUrl = (raw: string): string => {
|
||||
setView("wysiwyg");
|
||||
}, [editor, flushChange, sanitizeHtmlOpt]);
|
||||
|
||||
useEffect(() => {
|
||||
if (!editor) return;
|
||||
|
||||
const sync = () => {
|
||||
selectionRef.current = { from: editor.state.selection.from, to: editor.state.selection.to };
|
||||
setTb(getToolbarState(editor));
|
||||
};
|
||||
|
||||
sync();
|
||||
editor.on("selectionUpdate", sync);
|
||||
editor.on("transaction", sync);
|
||||
|
||||
return () => {
|
||||
editor.off("selectionUpdate", sync);
|
||||
editor.off("transaction", sync);
|
||||
};
|
||||
}, [editor]);
|
||||
|
||||
useEffect(() => {
|
||||
if (!props.onEditorReady || !editor) return;
|
||||
|
||||
if (viewRef.current === "html") {
|
||||
props.onEditorReady(htmlTextareaRef.current);
|
||||
} else {
|
||||
props.onEditorReady(editor);
|
||||
}
|
||||
}, [editor, view, props.onEditorReady]);
|
||||
|
||||
useEffect(() => {
|
||||
if (!editor) return;
|
||||
if (viewRef.current === "html") return;
|
||||
if (value == null) return;
|
||||
|
||||
const current = editor.getHTML();
|
||||
|
||||
if (value !== current && value !== optimisticHtmlRef.current) {
|
||||
// 이미지가 포함된 HTML 업데이트면, 아예 editor를 새로 생성
|
||||
editor.commands.setContent(value, {
|
||||
emitUpdate: false,
|
||||
parseOptions: {preserveWhitespace: "full"},
|
||||
});
|
||||
}
|
||||
}, [value, editor, view]);
|
||||
|
||||
useEffect(() => {
|
||||
if (value.includes("<img")) {
|
||||
setWysiwygKey((k) => k + 1);
|
||||
return;
|
||||
}
|
||||
}, [value]);
|
||||
|
||||
useEffect(() => {
|
||||
if (!editor) return;
|
||||
|
||||
const onBlur = () => {
|
||||
if (viewRef.current === "html") flushChange(htmlDraftRef.current);
|
||||
else flushChange(editor.getHTML());
|
||||
};
|
||||
|
||||
editor.on("blur", onBlur);
|
||||
return () => {
|
||||
editor.off("blur", onBlur);
|
||||
};
|
||||
}, [editor, flushChange]);
|
||||
|
||||
useEffect(() => {
|
||||
if (!props.onCommandReady) return;
|
||||
|
||||
// editor가 아직 없으면 null 전달
|
||||
if (!editor) {
|
||||
props.onCommandReady(null);
|
||||
return;
|
||||
}
|
||||
|
||||
props.onCommandReady({
|
||||
focus: () => {
|
||||
if (viewRef.current === "html") htmlTextareaRef.current?.focus();
|
||||
else editor.chain().focus().run();
|
||||
},
|
||||
|
||||
insertText: (placeholder: string) => {
|
||||
// 1) HTML textarea 모드
|
||||
if (viewRef.current === "html") {
|
||||
const current = htmlDraftRef.current ?? "";
|
||||
|
||||
htmlTi.insertText(
|
||||
placeholder,
|
||||
current,
|
||||
(next) => {
|
||||
htmlDraftRef.current = next;
|
||||
// uncontrolled textarea이므로 DOM value를 직접 갱신
|
||||
if (htmlTextareaRef.current) htmlTextareaRef.current.value = next;
|
||||
},
|
||||
(next) => {
|
||||
// 삽입은 즉시 커밋(저장 버튼 클릭 직후 반영 누락 방지)
|
||||
flushChange(next);
|
||||
}
|
||||
);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
// 2) WYSIWYG(tiptap) 모드
|
||||
restoreSelection();
|
||||
editor.chain().focus().insertContent(placeholder).run();
|
||||
flushChange(editor.getHTML());
|
||||
},
|
||||
});
|
||||
|
||||
return () => props.onCommandReady?.(null);
|
||||
}, [editor, props.onCommandReady, restoreSelection, flushChange, htmlTi]);
|
||||
|
||||
const previewHtml = useMemo(() => {
|
||||
const raw = optimisticHtmlRef.current || "";
|
||||
return sanitizeHtmlOpt ? sanitizeHtml(raw) : raw;
|
||||
@@ -532,7 +595,6 @@ const normalizeLinkUrl = (raw: string): string => {
|
||||
showHtmlToggle={showHtmlToggle}
|
||||
isHtmlView={view === "html"}
|
||||
onToggleHtmlView={onToggleHtmlView}
|
||||
isLmsMode={isLmsMode}
|
||||
onSetFontSize={setFontSize}
|
||||
onSetTextColor={setTextColor}
|
||||
onUnsetTextColor={unsetTextColor}
|
||||
@@ -553,6 +615,7 @@ const normalizeLinkUrl = (raw: string): string => {
|
||||
) : (
|
||||
<div className="p-2">
|
||||
<textarea
|
||||
ref={htmlTextareaRef}
|
||||
className="h-[220px] w-full resize-y rounded-md border bg-background p-3 font-mono text-sm outline-none"
|
||||
defaultValue={htmlDraftRef.current}
|
||||
onMouseDown={(e) => e.stopPropagation()}
|
||||
@@ -562,6 +625,7 @@ const normalizeLinkUrl = (raw: string): string => {
|
||||
}}
|
||||
onBlur={() => flushChange(htmlDraftRef.current)}
|
||||
placeholder={placeholder}
|
||||
{...htmlTi.bindSelectionHandlers}
|
||||
/>
|
||||
</div>
|
||||
)}
|
||||
|
||||
Reference in New Issue
Block a user