Skip to content

React

설치

bash
npm install @newtil/editor react

사용

tsx
import "@newtil/editor";                        // Custom Element 등록 (필수, 래퍼보다 먼저)
import { NewtilEditor } from "@newtil/editor/react";

function App() {
  const [md, setMd] = useState("");
  return (
    <NewtilEditor
      value={md}
      onChange={(detail) => setMd(detail.markdown)}
      toolbar
      placeholder="여기에 입력..."
      style={{ minHeight: 420 }}
    />
  );
}

WARNING

import "@newtil/editor" 는 Custom Element 를 전역 등록한다. React 래퍼보다 먼저 import 해야 한다. Next.js 등 SSR 환경에서는 next/dynamicssr: false 로 불러온다.

Props

정본은 src/react/index.tsxNewtilEditorProps.

Prop타입기본값설명
valuestring마크다운. 바뀌면 value 속성으로 넘긴다(편집기가 낸 값과 같으면 무시)
placeholderstring빈 편집기 안내
readOnlybooleanfalse읽기 전용
toolbarbooleanfalse상단 툴바
floatingToolbarbooleantruefalse 면 플로팅 툴바 숨김
langstring문자열 언어 ko | en. 없으면 조상 lang<html lang>
messagesPartial<Messages>문자열 일부 덮어쓰기, 예: { emptyLineHint: "…" }
onImageUpload(file: File) => Promise<string>이미지 업로드 훅(파일 선택·붙여넣기·끌어놓기)
onDrawingSaveDrawingSaveHandler그림판 저장 훅 — 그림판 연동
onDrawingLoadDrawingLoadHandler그림판 불러오기 훅
onChange(detail: { markdown, html }) => void내용 변경
onFocus() => void포커스
onBlur() => void블러
classNamestring호스트 요소의 class
styleCSSProperties호스트 요소의 인라인 스타일

래퍼는 mode·empty-line-hint prop 이 없다. 필요하면 ref 의 element 로 직접 다룬다(element.mode = "source", element.setAttribute("empty-line-hint", "…")). modechange 이벤트도 element.addEventListener 로 받는다.

Ref

tsx
import { useRef } from "react";
import type { NewtilEditorRef } from "@newtil/editor/react";

function App() {
  const ref = useRef<NewtilEditorRef>(null);
  const save = () => console.log(ref.current?.markdown, ref.current?.html);
  return <NewtilEditor ref={ref} toolbar />;
}
필드타입설명
markdownstring현재 마크다운
htmlstring현재 HTML
elementHTMLElement | null<newtil-editor> DOM — 래퍼에 없는 속성·프로퍼티·이벤트는 여기로

이미지 · 그림판 훅

tsx
<NewtilEditor
  onImageUpload={async (file) => {
    const form = new FormData(); form.append("file", file);
    const res = await fetch("/api/upload", { method: "POST", body: form });
    return (await res.json()).url;
  }}
  onDrawingSave={async ({ png, data, previousSrc }) => uploadDrawing(png, data, previousSrc)}
  onDrawingLoad={async (src) => fetchDrawingJson(src)}
/>

타입

ts
import type {
  NewtilEditorProps, NewtilEditorRef, NewtilEditorChangeDetail, ImageUploadHandler,
  DrawingData, DrawingSaveInput, DrawingSaveHandler, DrawingLoadHandler,
} from "@newtil/editor/react";