[React] ๐ŸžTOAST UI ์‚ฌ์šฉ๋ฒ•

TATAยท2023๋…„ 4์›” 23์ผ
1

React

๋ชฉ๋ก ๋ณด๊ธฐ
25/32
post-thumbnail

๋‚ด ๋‹ด๋‹น์€ ์•„๋‹ˆ์—ˆ์ง€๋งŒ, ์‚ฌ์šฉ๋ฒ•์ด ๊ถ๊ธˆํ•ด์„œ ์ฐพ์•„๋ณด์•˜๋‹ค.
๊ฐ์‚ฌํ•˜๊ฒŒ๋„ ์ž˜ ์ •๋ฆฌ๋œ ๋ธ”๋กœ๊ทธ๊ฐ€ ์žˆ์—ˆ์œผ๋ฉฐ ํ•˜๋‹ค๊ฐ€ ๊ถ๊ธˆํ•œ ๋ถ€๋ถ„๋“ค์€ ๋” ์ฐพ์•„๋ดค๋‹ค.
๐Ÿ‘‰ 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;
profile
๐Ÿพ

0๊ฐœ์˜ ๋Œ“๊ธ€