# react-rte **Repository Path**: kongkongss/react-rte ## Basic Information - **Project Name**: react-rte - **Description**: react-rteaaaaa - **Primary Language**: Unknown - **License**: ISC - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2023-08-01 - **Last Updated**: 2023-08-01 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # Deprecated This repository is long ourdated and even [Draft.js](https://github.com/facebookarchive/draft-js) (on which this editor is built) is long outdated and has been superceded by [Lexical](https://github.com/facebook/lexical). I don't recommend you use this in your project. There are many great alternatives such as [TipTap](https://tiptap.dev/). # React Rich Text Editor This is a UI component built completely in React that is meant to be a full-featured textarea replacement similar to [CKEditor][ckeditor], [TinyMCE][tinymce] and other [rich text "WYSIWYG" editors][rte]. It's based on the excellent, open source [Draft.js][draft-js] from Facebook which is performant and production-tested. ## Demo Try the editor here: [react-rte.org/demo][react-rte-demo] [![Screenshot 1](https://ucassets.blob.core.windows.net/uploads/rte.png)][react-rte-demo] ## Getting Started $ npm install --save react-rte `RichTextEditor` is the main editor component. It is comprised of the Draft.js ``, some UI components (e.g. toolbar) and some helpful abstractions around getting and setting content with HTML/Markdown. `RichTextEditor` is designed to be used like a `textarea` except that instead of `value` being a string, it is an object with `toString` on it. Creating a `value` from a string is also easy using `createValueFromString(markup, 'html')`. ### Browser Compatibility The scripts are transpiled by Babel to ES6. Additionally, at least one of this package's dependencies does not support IE. So, for IE and back-plat support you will need to include some polyfill in your HTML (#74, #196, #203): `` ### Required Webpack configuration If you are not using Webpack, you can skip this section. Webpack is required for isomorphic/server-side rendering support in a Node.js environment. `'react-rte'` contains a bundle that is already built (with CSS) using webpack and is not intended to be consumed again by webpack. So, if you are using webpack you must import RichTextEditor from `react-rte/lib/RichTextEditor` in order to get the un-bundled script which webpack can bundle with your app. If you are using webpack you must add a css loader or else your webpack build will fail. For example: ```js { test: /\.css$/, loaders: [ 'style-loader', 'css-loader?modules' ] }, ``` ### Example Usage: This example uses newer JavaScript and JSX. For an example in old JavaScript, [see below](#example-with-es5-and-no-jsx). ```javascript import React, {Component, PropTypes} from 'react'; import RichTextEditor from 'react-rte'; class MyStatefulEditor extends Component { static propTypes = { onChange: PropTypes.func }; state = { value: RichTextEditor.createEmptyValue() } onChange = (value) => { this.setState({value}); if (this.props.onChange) { // Send the changes up to the parent component as an HTML string. // This is here to demonstrate using `.toString()` but in a real app it // would be better to avoid generating a string on each change. this.props.onChange( value.toString('html') ); } }; render () { return ( ); } } ``` ### Toolbar Customization ```javascript render() { // The toolbarConfig object allows you to specify custom buttons, reorder buttons and to add custom css classes. // Supported inline styles: https://github.com/facebook/draft-js/blob/master/docs/Advanced-Topics-Inline-Styles.md // Supported block types: https://github.com/facebook/draft-js/blob/master/docs/Advanced-Topics-Custom-Block-Render.md#draft-default-block-render-map const toolbarConfig = { // Optionally specify the groups to display (displayed in the order listed). display: ['INLINE_STYLE_BUTTONS', 'BLOCK_TYPE_BUTTONS', 'LINK_BUTTONS', 'BLOCK_TYPE_DROPDOWN', 'HISTORY_BUTTONS'], INLINE_STYLE_BUTTONS: [ {label: 'Bold', style: 'BOLD', className: 'custom-css-class'}, {label: 'Italic', style: 'ITALIC'}, {label: 'Underline', style: 'UNDERLINE'} ], BLOCK_TYPE_DROPDOWN: [ {label: 'Normal', style: 'unstyled'}, {label: 'Heading Large', style: 'header-one'}, {label: 'Heading Medium', style: 'header-two'}, {label: 'Heading Small', style: 'header-three'} ], BLOCK_TYPE_BUTTONS: [ {label: 'UL', style: 'unordered-list-item'}, {label: 'OL', style: 'ordered-list-item'} ] }; return ( ); } ``` ## Motivation In short, this is a modern approach to rich text editing built on a battle-hardened open-source framework and, importantly, we do not store document state in the DOM, eliminating entire classes of common "WYSIWYG" problems. This editor is built on [Draft.js][draft-js] from Facebook. Draft.js is more of a low-level framework (`contentEditable` abstraction), however this component is intended to be a fully polished UI component that you can reach for when you need to replace a `