๋ด ๋ด๋น์ ์๋์์ง๋ง, ์ฌ์ฉ๋ฒ์ด ๊ถ๊ธํด์ ์ฐพ์๋ณด์๋ค.
๊ฐ์ฌํ๊ฒ๋ ์ ์ ๋ฆฌ๋ ๋ธ๋ก๊ทธ๊ฐ ์์์ผ๋ฉฐ ํ๋ค๊ฐ ๊ถ๊ธํ ๋ถ๋ถ๋ค์ ๋ ์ฐพ์๋ดค๋ค.
๐ TOAST UI ์ฌ์ฉ๋ฒ ์ ๋ฆฌ๋ ๋ธ๋ก๊ทธ
npm install @toast-ui/react-editor
ToastUI.js
import { Editor } from "@toast-ui/react-editor";
import "@toast-ui/editor/dist/toastui-editor.css";
import "@toast-ui/editor/dist/toastui-editor-viewer.css";
import { useRef, useState, useEffect } from "react";
const ToastUI = ({ initialValue = "" }) => {
const [initialValueState, setInitialValueState] = useState(initialValue); // initialValue ๊ฐ์ ์ํ๊ฐ์ผ๋ก ๊ด๋ฆฌ
// Editor DOM ์ ํ์ฉ
const editorRef = useRef(null);
useEffect(() => {
if (editorRef.current) {
editorRef.current.getInstance().focus();
if (initialValue === "") {
editorRef.current.getInstance().reset();
} else {
editorRef.current.getInstance().setMarkdown(initialValue); // cursor๋ฅผ ์ด๊ธฐ๊ฐ์ ๋ค์ ์์น์ํด
setInitialValueState(initialValue);
}
}
}, [initialValue]);
// ๋ฑ๋ก ๋ฒํผ ํธ๋ค๋ฌ
const handleRegisterBtn = () => {
// ์
๋ ฅ์ฐฝ์ ์
๋ ฅํ ๋ด์ฉ์ MarkDown ํํ๋ก ์ทจ๋
console.log(editorRef.current?.getInstance().getMarkdown());
// ์
๋ ฅ์ฐฝ์ ์
๋ ฅํ ๋ด์ฉ์ HTML ํ๊ทธ ํํ๋ก ์ทจ๋
console.log(editorRef.current?.getInstance().getHTML());
};
return (
<>
<h2>๐ Editor Toast</h2>
<Editor
placeholder="๋ด์ฉ์ ์
๋ ฅํด์ฃผ์ธ์."
previewStyle="vertical" // ๋ฏธ๋ฆฌ๋ณด๊ธฐ ์คํ์ผ ์ง์
height="300px" // ์๋ํฐ ์ฐฝ ๋์ด
initialEditType="wysiwyg" // ์ด๊ธฐ ์
๋ ฅ๋ชจ๋ ์ค์ (๋ํดํธ markdown)
initialValue={initialValueState}
toolbarItems={[
// ํด๋ฐ ์ต์
์ค์
["heading", "bold", "italic", "strike"],
["hr", "quote"],
["ul", "ol", "task", "indent", "outdent"],
["table", "image", "link"],
["code", "codeblock"],
]}
ref={editorRef}
useCommandShortcut={true} // ํค๋ณด๋ ์
๋ ฅ ์ปจํธ๋กค ๋ฐฉ์ง
/>
<button onClick={handleRegisterBtn}>๋ฑ๋ก</button>
</>
);
};
export default ToastUI;
App.js
import "./App.css";
import ToastUI from "./components/ToastUI";
function App() {
return (
<>
<h1>ํ ์คํธUI ์น์๋ํฐ ์ฌ์ฉ ์์ </h1>
<ToastUI />
</>
);
}
export default App;