260728_05_ENH: 에디터 동적 툴바 사용가능하도록 수정
This commit is contained in:
@@ -20,7 +20,7 @@ import TableCell from "@tiptap/extension-table-cell";
|
||||
import { toast } from "sonner";
|
||||
import { cn } from "@/lib/utils";
|
||||
|
||||
import type { KriEditorProps, ToolbarState } from "./types";
|
||||
import type { KriEditorCommand, KriEditorProps, ToolbarState } from "./types";
|
||||
import { resolveUploadUrl } from "./types";
|
||||
import { FontSize } from "./extensions/font-size";
|
||||
import { Div } from "./extensions/div";
|
||||
@@ -101,6 +101,7 @@ export function KriEditorWysiwyg(props: KriEditorProps) {
|
||||
const fileRef = useRef<HTMLInputElement | null>(null);
|
||||
const htmlTextareaRef = useRef<HTMLTextAreaElement | null>(null);
|
||||
const htmlTi = useTextareaInsert(htmlTextareaRef);
|
||||
const { insertText: insertTextareaText } = htmlTi;
|
||||
|
||||
const [view, setView] = useState<ViewMode>("wysiwyg");
|
||||
const [previewSource, setPreviewSource] = useState(value ?? "");
|
||||
@@ -407,6 +408,7 @@ export function KriEditorWysiwyg(props: KriEditorProps) {
|
||||
if(onImageUploadError){
|
||||
onImageUploadError(err);
|
||||
}else{
|
||||
toast.error("이미지 업로드에 실패했습니다.");
|
||||
console.error(err);
|
||||
}
|
||||
}
|
||||
@@ -509,6 +511,38 @@ export function KriEditorWysiwyg(props: KriEditorProps) {
|
||||
};
|
||||
}, [editor, flushChange]);
|
||||
|
||||
const command = useMemo<KriEditorCommand>(
|
||||
() => ({
|
||||
focus: () => {
|
||||
if (viewRef.current === "html") htmlTextareaRef.current?.focus();
|
||||
else editor?.chain().focus().run();
|
||||
},
|
||||
insertText: (text: string) => {
|
||||
if (viewRef.current === "html") {
|
||||
const current = htmlDraftRef.current ?? "";
|
||||
insertTextareaText(
|
||||
text,
|
||||
current,
|
||||
(next) => {
|
||||
htmlDraftRef.current = next;
|
||||
if (htmlTextareaRef.current) htmlTextareaRef.current.value = next;
|
||||
},
|
||||
(next) => {
|
||||
flushChange(next);
|
||||
}
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!editor) return;
|
||||
restoreSelection();
|
||||
editor.chain().focus().insertContent(text).run();
|
||||
flushChange(editor.getHTML());
|
||||
},
|
||||
}),
|
||||
[editor, restoreSelection, flushChange, insertTextareaText]
|
||||
);
|
||||
|
||||
useEffect(() => {
|
||||
if (!props.onCommandReady) return;
|
||||
|
||||
@@ -518,43 +552,10 @@ export function KriEditorWysiwyg(props: KriEditorProps) {
|
||||
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());
|
||||
},
|
||||
});
|
||||
props.onCommandReady(command);
|
||||
|
||||
return () => props.onCommandReady?.(null);
|
||||
}, [editor, props.onCommandReady, restoreSelection, flushChange, htmlTi]);
|
||||
}, [command, editor, props.onCommandReady]);
|
||||
|
||||
const previewHtml = useMemo(() => {
|
||||
return sanitizeHtmlOpt ? sanitizeHtml(previewSource) : previewSource;
|
||||
@@ -597,6 +598,7 @@ export function KriEditorWysiwyg(props: KriEditorProps) {
|
||||
onAddRow={addRow}
|
||||
onAddColumn={addColumn}
|
||||
onDeleteTable={deleteTable}
|
||||
customContent={props.renderToolbarEnd?.({ editor, command, view })}
|
||||
/>
|
||||
</div>
|
||||
) : null}
|
||||
|
||||
Reference in New Issue
Block a user