draft-js README

The project's README from github.com/facebookarchive/draft-js. As one markdown file: README.md.

draftjs-logo

Draft.js

Build Status npm version

Live Demo


Status

THIS PROJECT IS CURRENTLY IN MAINTENANCE MODE. It will not receive any feature updates, only critical security bug patches. On 31st December 2022 the repo will be fully archived.

For users looking for an open source alternative, Meta have been working on migrating to a new framework, called Lexical. It's still experimental, and we're working on adding migration guides, but, we believe, it provides a more performant and accessible alternative.


Draft.js is a JavaScript rich text editor framework, built for React and backed by an immutable model.

Learn how to use Draft.js in your own project.

Draft.js is used in production on Facebook, including status and comment inputs, Notes, and messenger.com.

API Notice

Before getting started, please be aware that we recently changed the API of Entity storage in Draft.

Previously, the old API was set to be removed in v0.11.0. Since, the plans have changed— v0.11.0 still supports the old API and v0.12.0 will remove it. Refer to the docs for more information and information on how to migrate.

Getting Started

npm install --save draft-js react react-dom

or

yarn add draft-js react react-dom

Draft.js depends on React and React DOM which must also be installed.

Using Draft.js

import React from 'react';
import ReactDOM from 'react-dom';
import {Editor, EditorState} from 'draft-js';

function MyEditor() {

  
  constructor(props) {
    super(props);
    this.state = {editorState: EditorState.createEmpty()};
    this.onChange = (editorState) => this.setState({editorState});
    this.setEditor = (editor) => {
      this.editor = editor;
    };
    this.focusEditor = () => {
      if (this.editor) {
        this.editor.focus();
      }
    };
  }

  componentDidMount() {
    this.focusEditor();
  }

  render() {
    return (
      <div style={styles.editor} onClick={this.focusEditor}>
        <Editor
          ref={this.setEditor}
          editorState={this.state.editorState}
          onChange={this.onChange}
        />
      </div>
    );
  }
}

const styles = {
  editor: {
    border: '1px solid gray',
    minHeight: '6em'
  }
};

ReactDOM.render(
  <MyEditor />,
  document.getElementById('container')
);

Since the release of React 16.8, you can use Hooks as a way to work with EditorState without using a class.

import React from "react";
import { Editor, EditorState } from "draft-js";
import "draft-js/dist/Draft.css";

export default function MyEditor() {
  const [editorState, setEditorState] = React.useState(() =>
    EditorState.createEmpty()
  );

  const editor = React.useRef(null);
  function focusEditor() {
    editor.current.focus();
  }

  return (
    <div
      style={{ border: "1px solid black", minHeight: "6em", cursor: "text" }}
      onClick={focusEditor}
    >
      <Editor
        ref={editor}
        editorState={editorState}
        onChange={setEditorState}
        placeholder="Write something!"
      />
    </div>
  );
}

Note that the editor itself is only as tall as its contents. In order to give users a visual cue, we recommend setting a border and a minimum height via the .DraftEditor-root CSS selector, or using a wrapper div like in the above example.

Because Draft.js supports unicode, you must have the following meta tag in the <head> </head> block of your HTML file:

<meta charset="utf-8" />

Further examples of how Draft.js can be used are provided in the /examples directory of this repo.

Building Draft.js

Draft.js is built with Yarn v1. Using other package managers mgiht work, but is not officially supported.

To clone and build, run:

git clone https://github.com/facebook/draft-js.git
cd draft-js
yarn install
yarn run build

Examples

To run the examples in the /examples directory, first build Draft.js locally as described above. Then, open the example HTML files in your browser.

Browser Support

IE / Edge
IE / Edge
Firefox
Firefox
Chrome
Chrome
Safari
Safari
iOS Safari
iOS Safari
Chrome for Android
Chrome for Android
IE11, Edge [1, 2] last 2 versions last 2 versions last 2 versions not fully supported [3] not fully supported [3]

[1] May need a shim or a polyfill for some syntax used in Draft.js (docs).

[2] IME inputs have known issues in these browsers, especially Korean (docs).

[3] There are known issues with mobile browsers, especially on Android (docs).

Resources and Ecosystem

Check out this curated list of articles and open-sourced projects/utilities: Awesome Draft-JS.

Discussion and Support

Join our Slack team!

Contribute

We welcome pull requests. Learn how to contribute.

License

Draft.js is MIT licensed.

Examples provided in this repository and in the documentation are separately licensed.

draft-js (facebookarchive/draft-js)

Searching Database…

22.6K ·GitHub
archived
这是什么?它是一个专为 React 打造的 JavaScript 富文本编辑器框架,底层采用不可变数据模型——用来在网页应用里实现富文本编辑功能。
所以它不是一个能直接放进我应用里的现成编辑器?没错——它是一个框架,不是做好的成品组件。你需要用它的各种构件自己搭建编辑体验,从基础的文字样式到嵌入媒体都可以。
怎么把它加到我的 React 项目里?和 React 一起安装:npm install draft-js react react-dom —— Draft.js 同时需要 React 和 React DOM。从 React 16.8 开始,你可以用 Hooks 来管理 EditorState,不必再写 class 组件。
它还在维护吗?新项目该选它吗?不——它已进入维护模式,只修复严重的安全漏洞,仓库也已在 2022 年 12 月 31 日完全归档。对于新项目,Meta 推荐使用它的后继框架 Lexical。
支持哪些浏览器?支持 IE11 和 Edge(可能需要 polyfill,而且 IME 输入有已知问题,尤其是韩文),以及 Firefox、Chrome 和 Safari 的最近 2 个版本。移动端没有完整支持,Android 上问题尤其多。
Draft.js 在生产环境里有哪些实际应用?Facebook 自己在用:状态和评论输入框、Notes 以及 messenger.com 都是基于它构建的。
为什么编辑器状态要设计成不可变的?它的数据模型基于 immutable-js 构建,状态更新是函数式的,数据持久化也很激进——即使文档很长、撤销历史很多,内存占用也能保持可控。
Lexical 是什么?就是 Meta 建议替代 Draft.js 的那个框架吗?它是 Meta 更新的网页文本编辑器框架。Draft.js 归档时 Lexical 还处于实验阶段,但 Meta 认为它在性能和无障碍方面更好,并且正在编写迁移指南。
编辑器显示出来被压扁了,该怎么设置样式?它的高度只和内容一样高,所以要给 .DraftEditor-root 选择器加上边框和最小高度,或者用一个 div 把它包起来。另外你的 HTML 里还需要一个 unicode 的 meta 标签,因为 Draft.js 支持 unicode。
Size
36 MB
Stars
22.6K
Age
10y
Commits · 90 Days