React에 네이버 SmartEditor2 적용하기

2024. 3. 16. 01:47·Frontend/React

네이버 스마트 에디터가 생각보다 좋아서 리액트에 적용하고 싶은데...

네이버 스마트 에디터는 npm으로 설치가 불가한 것 같다.

 

그래서 생각해 낸 방법이 public 폴더에 에디터 html 을 만들고 그 html 파일을 컴포넌트 iframe에 넣어서 가져오는 방식이다!!

해봤더니 잘돼서 공유한다.

 

https://github.com/naver/smarteditor2/releases

 

Releases · naver/smarteditor2

Javascript WYSIWYG HTML editor. Contribute to naver/smarteditor2 development by creating an account on GitHub.

github.com

smarteditor2를 받아준다. 

 

나는 표시한 파일을 받아줬다.

 

tgz 파일을 풀면 package > dist 폴더안에 스마트에디터에 필요한 소스들이 들어있다.

 

dist파일 안에 있는 파일들을 리액트 /public 폴더 안에 smarteditor 폴더를 만들어서 넣어줬다.

그리고 write.html 파일을 만들어 줬다.

 

write.html 파일

<!DOCTYPE html>
<html lang="ko">

<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>SmartEditor</title>
</head>

<body>
	<div>
		<textarea name="editorTxt" id="editorTxt"></textarea>
	</div>

	<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
	<script type="text/javascript" src="./js/service/HuskyEZCreator.js" charset="utf-8"></script>
	<script>
		let oEditors = [];
		var checkedValues = [];
		smartEditor = function () {
			nhn.husky.EZCreator.createInIFrame({
				oAppRef: oEditors,
				elPlaceHolder: "editorTxt",
				sSkinURI: "./SmartEditor2Skin.html",
				fCreator: "createSEditor2"
			})
		}

		function submitPost() {
			oEditors.getById["editorTxt"].exec("UPDATE_CONTENTS_FIELD", [])
			let content = document.getElementById("editorTxt").value

			if (content == '' || content == '<p><br></p>') {
				alert("내용을 입력해주세요.")
				oEditors.getById["editorTxt"].exec("FOCUS");
				return
			} else {
				console.log(content)
				return content;
			}
		}

		window.onload = function () {
			smartEditor();
		}
	</script>
</body>

</html>

 

SmartEditor.jsx

import { useState } from "react";
import { Button, Modal, Box } from "@mui/material";

const style = {
  position: "absolute",
  top: "50%",
  left: "50%",
  transform: "translate(-50%, -50%)",
  bgcolor: "background.paper",
  border: "2px solid #000",
  boxShadow: 24,
  p: 4,
  maxHeight: "80vh",
  overflowY: "scroll",
};

export default function SmartEditor() {
  const [sendData, setSendData] = useState();

  const [open2, setOpen2] = useState(false);
  const handleOpen2 = () => {
    const mailBody = document
      .getElementById("iframeContainer_iframe")
      .contentWindow.submitPost();

    setSendData(mailBody);

    setOpen2(true);
  };
  const handleClose2 = () => setOpen2(false);

  return (
    <div id="container" className="iframeContainer">
      <iframe
        id="iframeContainer_iframe"
        allowFullScreen={true}
        width={"100%"}
        height={"700"}
        scrolling={"no"}
        name="myframe"
        frameBorder={"0"}
        src="./smarteditor/write.html"
      ></iframe>
      <Button variant="outlined" onClick={handleOpen2}>
        미리보기
      </Button>
      <Modal
        open={open2}
        onClose={handleClose2}
        aria-labelledby="modal-modal-title"
        aria-describedby="modal-modal-description"
      >
        <Box sx={style}>
          <div dangerouslySetInnerHTML={{ __html: sendData }}></div>
        </Box>
      </Modal>
    </div>
  );
}

 

아주 잘된다!!

미리보기 기능도 잘된다

 

반응형
저작자표시 (새창열림)

'Frontend > React' 카테고리의 다른 글

tailwind custom color가 적용이 안될때  (1) 2024.05.13
[eslint] react/prop-types, missing in props validation  (3) 2024.05.13
React에 CKEditor5 적용하기 (Online Builder)  (0) 2024.03.15
react에서 alias가 상대경로로 지정 될 경우  (1) 2024.01.15
리액트 context 사용하는 방법  (0) 2024.01.06
'Frontend/React' 카테고리의 다른 글
  • tailwind custom color가 적용이 안될때
  • [eslint] react/prop-types, missing in props validation
  • React에 CKEditor5 적용하기 (Online Builder)
  • react에서 alias가 상대경로로 지정 될 경우
전예방
전예방
  • 전예방
    예방이의 개발일기
    전예방
  • 전체
    오늘
    어제
    • All (125)
      • Info & Tip (2)
      • 유용한 사이트들 (5)
      • Mark Up & StyleSheet (23)
        • HTML (6)
        • CSS & SCSS (10)
        • 반응형 (6)
      • Frontend (66)
        • 전체 (10)
        • JavaScript & Jquery (18)
        • TypeScript (0)
        • React (26)
        • Next.js (3)
        • 성능최적화 (2)
        • 웹접근성 (2)
      • Backend (13)
        • Python (1)
        • JAVA (2)
        • node.js (0)
        • PHP (7)
        • 패키지매니저 (3)
      • Markdown (0)
      • SCM (1)
        • Git&Github (1)
        • SVN (0)
      • IDE (4)
        • VSCode (2)
        • IntelliJ (1)
        • Ecplise (1)
      • 취미생활 (3)
      • 정보처리기사 (2)
      • 코딩자율학습단 (5)
        • 12기 (파이썬) (5)
  • 블로그 메뉴

    • 홈
    • 태그
  • 링크

    • velog
    • github
  • 공지사항

  • 인기 글

  • 태그

    frontend
    회원가입
    리액트
    나도코딩
    php
    react
    파이썬
    React Swiper
    관리자 페이지
    코딩자율학습단
    Admin
    Chart.js
    swiper.js
    TypeScript
    yarn berry
    Python
    CSS
    코딩자율학습
    반응형
    NPM
  • 최근 댓글

  • 최근 글

  • 반응형
  • hELLO· Designed By정상우.v4.10.3
전예방
React에 네이버 SmartEditor2 적용하기

개인정보

  • 티스토리 홈
  • 포럼
  • 로그인
상단으로

티스토리툴바

단축키

내 블로그

내 블로그 - 관리자 홈 전환
Q
Q
새 글 쓰기
W
W

블로그 게시글

글 수정 (권한 있는 경우)
E
E
댓글 영역으로 이동
C
C

모든 영역

이 페이지의 URL 복사
S
S
맨 위로 이동
T
T
티스토리 홈 이동
H
H
단축키 안내
Shift + /
⇧ + /

* 단축키는 한글/영문 대소문자로 이용 가능하며, 티스토리 기본 도메인에서만 동작합니다.