Skip to content
A React framework for building text editors.
Branch: master
Clone or download
danielkza and facebook-github-bot draft-js: clean up useless divs from HTML when pasting content
Summary:
When pasting rich (HTML) content into a Draft editor, the generated blocks
are flattened such that they can be represented in the internal state model.
Until a change to the tree-based model is fully implemented, that may cause
formatting to be lost if the content is wrapped in multiple levels of divs,
as the parsing does not traverse them deeply.

Improve the amount of formatting that can be preserved in the current model
by stripping out divs considered 'useless' (with no text or styling), such
that more important elements can be 'brought to the surface' and parsed
in a more pleasant manner.

Reviewed By: claudiopro

Differential Revision: D15923965

fbshipit-source-id: 72824ddba69b5d08374f505187ed4a6ea7c4d573
Latest commit 0f5427a Jun 24, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.github Add an example jsfiddle to the issue template (#650) Sep 14, 2016
docs Fix broken id anchor (#2095) Jun 14, 2019
examples Deprecate legacy convertFromHTMLToContentBlocks Jun 19, 2019
meta Adds support for nullish coalescing operator (#2076) Jun 12, 2019
scripts Upgrades @babel/core, babel-preset-fbjs, and gulp-util (#2028) Mar 7, 2019
src draft-js: clean up useless divs from HTML when pasting content Jun 24, 2019
website Upgrades docusaurus (#2039) Mar 20, 2019
.babelrc Adds support for nullish coalescing operator (#2076) Jun 12, 2019
.eslintignore Fixed license, Flow and lint issues in draft-js Apr 27, 2018
.eslintrc.js Relicense under MIT and remove patents grant (#1967) Jan 4, 2019
.gitignore Cleans up and refreshes generated website resources (#1998) Feb 8, 2019
.travis.yml Adds email notifications for TravisCI builds (#1990) Feb 4, 2019
CHANGELOG.md fix typo in changelog Jan 22, 2018
CODE_OF_CONDUCT.md Update link to code of conduct (#1855) Sep 6, 2018
CONTRIBUTING.md Add issue triage guidelines to CONTRIBUTING.md (#1896) Oct 16, 2018
LICENSE Relicense under MIT and remove patents grant (#1967) Jan 4, 2019
LICENSE-examples Open source Draft.js 🎉 Feb 22, 2016
README.md Add import statements for hooks-example in README (#2075) May 20, 2019
gulpfile.js Adds support for nullish coalescing operator (#2076) Jun 12, 2019
package.json Adds support for nullish coalescing operator (#2076) Jun 12, 2019
prettier.config.js Relicense under MIT and remove patents grant (#1967) Jan 4, 2019
yarn.lock Adds support for nullish coalescing operator (#2076) Jun 12, 2019

README.md

Draft.js Build Status npm version

Live Demo

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

  • Extensible and Customizable: We provide the building blocks to enable the creation of a broad variety of rich text composition experiences, from simple text styles to embedded media.
  • Declarative Rich Text: Draft.js fits seamlessly into React applications, abstracting away the details of rendering, selection, and input behavior with a familiar declarative API.
  • Immutable Editor State: The Draft.js model is built with immutable-js, offering an API with functional state updates and aggressively leveraging data persistence for scalable memory usage.

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

API Notice

Before getting started, please be aware that we recently changed the API of Entity storage in Draft. The latest version, v0.10.0, supports both the old and new API. Following that up will be v0.11.0 which will remove the old API. If you are interested in helping out, or tracking the progress, please follow issue 839.

Getting Started

Currently Draft.js is distributed via npm. It depends on React and React DOM which must also be installed.

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

or

yarn add draft-js react react-dom

Using Draft.js

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

class MyEditor extends React.Component {
  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 ReactDOM from 'react-dom';
import {Editor, EditorState} from 'draft-js';

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

  const editor = React.useRef(null);

  function focusEditor() {
    editor.current.focus();
  }

  React.useEffect(() => {
    focusEditor()
  }, []);

  return (
    <div onClick={focusEditor}>
      <Editor
        ref={editor}
        editorState={editorState}
        onChange={editorState => setEditorState(editorState)}
      />
    </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 below.

Examples

Visit http://draftjs.org/ to try out a simple rich editor example.

The repository includes a variety of different editor examples to demonstrate some of the features offered by the framework.

To run the examples, first build Draft.js locally:

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

then open the example HTML files in your browser.

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

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 actively 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.

You can’t perform that action at this time.