Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

3 лет назад
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. # [Draft.js](http://draftjs.org/) [![Build Status](https://img.shields.io/travis/facebook/draft-js/master.svg?style=flat)](https://travis-ci.org/facebook/draft-js) [![npm version](https://img.shields.io/npm/v/draft-js.svg?style=flat)](https://yarn.pm/draft-js)
  2. ![Live Demo](https://media.giphy.com/media/XHUjaxELpc11SiRSqN/giphy.gif)
  3. Draft.js is a JavaScript rich text editor framework, built for React and
  4. backed by an immutable model.
  5. - **Extensible and Customizable:** We provide the building blocks to enable
  6. the creation of a broad variety of rich text composition experiences, from
  7. basic text styles to embedded media.
  8. - **Declarative Rich Text:** Draft.js fits seamlessly into
  9. [React](http://facebook.github.io/react/) applications,
  10. abstracting away the details of rendering, selection, and input behavior with a
  11. familiar declarative API.
  12. - **Immutable Editor State:** The Draft.js model is built
  13. with [immutable-js](https://facebook.github.io/immutable-js/), offering
  14. an API with functional state updates and aggressively leveraging data persistence
  15. for scalable memory usage.
  16. [Learn how to use Draft.js in your own project.](https://draftjs.org/docs/getting-started/)
  17. ## API Notice
  18. Before getting started, please be aware that we recently changed the API of
  19. Entity storage in Draft. The latest version, `v0.10.0`, supports both the old
  20. and new API. Following that up will be `v0.11.0` which will remove the old API.
  21. If you are interested in helping out, or tracking the progress, please follow
  22. [issue 839](https://github.com/facebook/draft-js/issues/839).
  23. ## Getting Started
  24. Currently Draft.js is distributed via npm. It depends on React and React DOM which must also be installed.
  25. ```
  26. npm install --save draft-js react react-dom
  27. or
  28. yarn add draft-js react react-dom
  29. ```
  30. ### Using Draft.js
  31. ```javascript
  32. import React from 'react';
  33. import ReactDOM from 'react-dom';
  34. import {Editor, EditorState} from 'draft-js';
  35. class MyEditor extends React.Component {
  36. constructor(props) {
  37. super(props);
  38. this.state = {editorState: EditorState.createEmpty()};
  39. this.onChange = (editorState) => this.setState({editorState});
  40. this.setEditor = (editor) => {
  41. this.editor = editor;
  42. };
  43. this.focusEditor = () => {
  44. if (this.editor) {
  45. this.editor.focus();
  46. }
  47. };
  48. }
  49. componentDidMount() {
  50. this.focusEditor();
  51. }
  52. render() {
  53. return (
  54. <div style={styles.editor} onClick={this.focusEditor}>
  55. <Editor
  56. ref={this.setEditor}
  57. editorState={this.state.editorState}
  58. onChange={this.onChange}
  59. />
  60. </div>
  61. );
  62. }
  63. }
  64. const styles = {
  65. editor: {
  66. border: '1px solid gray',
  67. minHeight: '6em'
  68. }
  69. };
  70. ReactDOM.render(
  71. <MyEditor />,
  72. document.getElementById('container')
  73. );
  74. ```
  75. Since the release of React 16.8, you can use [Hooks](https://reactjs.org/docs/hooks-intro.html) as a way to work with `EditorState` without using a class.
  76. ```js
  77. import React from 'react';
  78. import ReactDOM from 'react-dom';
  79. import {Editor, EditorState} from 'draft-js';
  80. function MyEditor() {
  81. const [editorState, setEditorState] = React.useState(
  82. EditorState.createEmpty()
  83. );
  84. const editor = React.useRef(null);
  85. function focusEditor() {
  86. editor.current.focus();
  87. }
  88. React.useEffect(() => {
  89. focusEditor()
  90. }, []);
  91. return (
  92. <div onClick={focusEditor}>
  93. <Editor
  94. ref={editor}
  95. editorState={editorState}
  96. onChange={editorState => setEditorState(editorState)}
  97. />
  98. </div>
  99. );
  100. }
  101. ```
  102. 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.
  103. Because Draft.js supports unicode, you must have the following meta tag in the `<head>` `</head>` block of your HTML file:
  104. ```html
  105. <meta charset="utf-8" />
  106. ```
  107. Further examples of how Draft.js can be used are provided below.
  108. ### Examples
  109. Visit http://draftjs.org/ to try out a basic rich editor example.
  110. The repository includes a variety of different editor examples to demonstrate
  111. some of the features offered by the framework.
  112. To run the examples, first build Draft.js locally:
  113. ```
  114. git clone https://github.com/facebook/draft-js.git
  115. cd draft-js
  116. npm install
  117. npm run build
  118. ```
  119. then open the example HTML files in your browser.
  120. Draft.js is used in production on Facebook, including status and
  121. comment inputs, [Notes](https://www.facebook.com/notes/), and
  122. [messenger.com](https://www.messenger.com).
  123. ## Browser Support
  124. | ![IE / Edge](https://raw.githubusercontent.com/alrra/browser-logos/master/src/edge/edge_32x32.png) <br /> IE / Edge | ![Firefox](https://raw.githubusercontent.com/alrra/browser-logos/master/src/firefox/firefox_32x32.png) <br /> Firefox | ![Chrome](https://raw.githubusercontent.com/alrra/browser-logos/master/src/chrome/chrome_32x32.png) <br /> Chrome | ![Safari](https://raw.githubusercontent.com/alrra/browser-logos/master/src/safari/safari_32x32.png) <br /> Safari | ![iOS Safari](https://raw.githubusercontent.com/alrra/browser-logos/master/src/safari-ios/safari-ios_32x32.png) <br />iOS Safari | ![Chrome for Android](https://raw.githubusercontent.com/alrra/browser-logos/master/src/chrome/chrome_32x32.png) <br/> Chrome for Android |
  125. | --------- | --------- | --------- | --------- | --------- | --------- |
  126. | IE11, Edge [1, 2]| last 2 versions| last 2 versions| last 2 versions| not fully supported [3] | not fully supported [3]
  127. [1] May need a shim or a polyfill for some syntax used in Draft.js ([docs](https://draftjs.org/docs/advanced-topics-issues-and-pitfalls/#polyfills)).
  128. [2] IME inputs have known issues in these browsers, especially Korean ([docs](https://draftjs.org/docs/advanced-topics-issues-and-pitfalls/#ime-and-internet-explorer)).
  129. [3] There are known issues with mobile browsers, especially on Android ([docs](https://draftjs.org/docs/advanced-topics-issues-and-pitfalls/#mobile-not-yet-supported)).
  130. ## Resources and Ecosystem
  131. Check out this curated list of articles and open-sourced projects/utilities: [Awesome Draft-JS](https://github.com/nikgraf/awesome-draft-js).
  132. ## Discussion and Support
  133. Join our [Slack team](https://draftjs.herokuapp.com)!
  134. ## Contribute
  135. We actively welcome pull requests. Learn how to
  136. [contribute](https://github.com/facebook/draft-js/blob/master/CONTRIBUTING.md).
  137. ## License
  138. Draft.js is [MIT licensed](https://github.com/facebook/draft-js/blob/master/LICENSE).
  139. Examples provided in this repository and in the documentation are separately
  140. licensed.