Skip to content

xCodeMuse/react-native-cn-richtext-editor

Repository files navigation

react-native-cn-richtext-editor

Richtext editor for react native

Installation

Install using npm:

npm i react-native-cn-richtext-editor 

Install using yarn:

yarn add react-native-cn-richtext-editor 

Usage

Here is a simple overview of our components usage.

importReact,{Component}from'react';import{View,StyleSheet,Keyboard,TouchableWithoutFeedback,Text,KeyboardAvoidingView}from'react-native';importCNRichTextEditor,{CNToolbar,getInitialObject,getDefaultStyles}from"react-native-cn-richtext-editor";constdefaultStyles=getDefaultStyles();classAppextendsComponent{constructor(props){super(props);this.state={selectedTag : 'body',selectedStyles : [],value: [getInitialObject()]};this.editor=null;}onStyleKeyPress=(toolType)=>{this.editor.applyToolbar(toolType);}onSelectedTagChanged=(tag)=>{this.setState({selectedTag: tag})}onSelectedStyleChanged=(styles)=>{this.setState({selectedStyles: styles,})}onValueChanged=(value)=>{this.setState({value: value});}render(){return(<KeyboardAvoidingViewbehavior="padding"enabledkeyboardVerticalOffset={0}style={{flex: 1,paddingTop: 20,backgroundColor:'#eee',flexDirection: 'column',justifyContent: 'flex-end',}}><TouchableWithoutFeedbackonPress={Keyboard.dismiss}><Viewstyle={styles.main}><CNRichTextEditorref={input=>this.editor=input}onSelectedTagChanged={this.onSelectedTagChanged}onSelectedStyleChanged={this.onSelectedStyleChanged}value={this.state.value}style={{backgroundColor : '#fff'}}styleList={defaultStyles}onValueChanged={this.onValueChanged}/></View></TouchableWithoutFeedback><Viewstyle={{minHeight: 35}}><CNToolbarsize={28}bold={<Textstyle={[styles.toolbarButton,styles.boldButton]}>B</Text>}italic={<Textstyle={[styles.toolbarButton,styles.italicButton]}>I</Text>}underline={<Textstyle={[styles.toolbarButton,styles.underlineButton]}>U</Text>}lineThrough={<Textstyle={[styles.toolbarButton,styles.lineThroughButton]}>S</Text>}body={<Textstyle={styles.toolbarButton}>T</Text>}title={<Textstyle={styles.toolbarButton}>h1</Text>}heading={<Textstyle={styles.toolbarButton}>h3</Text>}ul={<Textstyle={styles.toolbarButton}>ul</Text>}ol={<Textstyle={styles.toolbarButton}>ol</Text>}selectedTag={this.state.selectedTag}selectedStyles={this.state.selectedStyles}onStyleKeyPress={this.onStyleKeyPress}/></View></KeyboardAvoidingView>);}}varstyles=StyleSheet.create({main: {flex: 1,marginTop: 10,paddingLeft: 30,paddingRight: 30,paddingBottom: 1,alignItems: 'stretch',},toolbarButton: {fontSize: 20,width: 28,height: 28,textAlign: 'center'},italicButton: {fontStyle: 'italic'},boldButton: {fontWeight: 'bold'},underlineButton: {textDecorationLine: 'underline'},lineThroughButton: {textDecorationLine: 'line-through'},});exportdefaultApp;

More Advanced TextEditor

You need to put more effort :) to use more advanced features of CNRichTextEditor such as:

  • Image Uploading
  • Highlighting Text
  • Change Text Color

Actually we did not implement 'Toolbar buttons and menus' and 'Image Uploading Process' because it totally depends on using expo or pure react-native and also what other packages you prefer to use.

To see an example of how to implement more advanced feature of this editor please check this Link.

Also be noticed that this example is writen with expo and required 'react-native-popup-menu' package.

API

CNRichTextEditor

Props

NameDescriptionRequired
onSelectedTagChangedthis event triggers when selected tag of editor is changed.No
onSelectedStyleChangedthis event triggers when selected style of editor is changed.No
onValueChangedthis event triggers when value of editor is changed.No
onRemoveImagethis event triggers when an image is removed. Callback param in the form {url, id }.No
valuean array object which keeps value of the editorYes
styleListan object consist of styles name and values (use getDefaultStyles function)Yes
ImageComponenta React component (class or functional) which will be used to render images. Will be passed style and source props.No
styleStyles applied to the outermost component.No
contentContainerStyleStyles applied to the scrollview content.No
onFocusCallback that is called when one of text inputs are focused.No
onBlurCallback that is called when one of text inputs are blurred.No

Instance methods

NameParamsDescription
applyToolbartoolTypeApply the given transformation to selected text.
insertImageuri, id?, height?, width?Insert the provided image where cursor is positionned.
focusFocus to the last TextInput

CNToolbar

Props

NameRequiredDescription
selectedTagYesselected tag of the editor
selectedStylesYesselected style of the editor
onStyleKeyPressYesthis event triggers when user press one of toolbar keys
sizeNofont size of toolbar buttons
boldNoa component which renders as bold button
italicNoa component which renders as italic button
underlineNoa component which renders as underline button
lineThroughNoa component which renders as lineThrough button
bodyNoa component which renders as body button
titleNoa component which renders as title button
ulNoa component which renders as ul button
olNoa component which renders as ol button
imageNoa component which renders as image button
highlightNoa component which renders as highlight button
foreColorNoa component which renders as foreColor button
styleNostyle applied to container
colorNodefault color passed to icon
backgroundColorNodefault background color passed to icon
selectedColorNocolor applied when icon is selected
selectedBackgroundColorNobackground color applied when icon is selected
iconContainerStyleNoa style prop assigned to icon container

CNRichTextView

Props

NameRequiredDescription
textYeshtml string (created by convertToHtmlString function
styleNostyle applied to container (req.{flex:1})
styleListNoan object consist of styles name and values (use getDefaultStyles function)

Functions

NameParamReturnsDescription
getInitialObject-javascript objectcreate a initial value for the editor.
convertToHtmlStringarraystringthis function converts value of editor to html string (use it to keep value as html in db)
convertToObjectstringarrayconverts html back to array for RichTextEditor value (use this function only for html string created by convertToHtmlString function)
getDefaultStyles-javascript objectcreates required styles for the editor.

Expo Demo App

Checkout the expo-demo App on Expo which uses react-native-cn-richtext-editor components. If you are looking to test and run expo-demo App locally, click here to view the implementation & run it locally.

License

MIT

About

Richtext editor for react native

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript100.0%