1.0.43 • Published 3 years ago

react-native-cn-richtext-editor v1.0.43

Weekly downloads
382
License
MIT
Repository
github
Last release
3 years ago

react-native-cn-richtext-editor

Deprecated. Use react-native-cn-quill instead.

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.

import React, { Component } from 'react';
import { View, StyleSheet, Keyboard
, TouchableWithoutFeedback, Text
, KeyboardAvoidingView } from 'react-native';

import  CNRichTextEditor , { CNToolbar, getInitialObject , getDefaultStyles } from "react-native-cn-richtext-editor";

const defaultStyles = getDefaultStyles();

class App extends Component {
 
    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 (
            <KeyboardAvoidingView 
            behavior="padding" 
            enabled
            keyboardVerticalOffset={0}
            style={{
                flex: 1,
                paddingTop: 20,
                backgroundColor:'#eee',
                flexDirection: 'column', 
                justifyContent: 'flex-end', 
            }}
            >
                <TouchableWithoutFeedback onPress={Keyboard.dismiss} >             
                    <View style={styles.main}>
                        <CNRichTextEditor                   
                            ref={input => this.editor = input}
                            onSelectedTagChanged={this.onSelectedTagChanged}
                            onSelectedStyleChanged={this.onSelectedStyleChanged}
                            value={this.state.value}
                            style={{ backgroundColor : '#fff'}}
                            styleList={defaultStyles}
                            onValueChanged={this.onValueChanged}
                        />                        
                    </View>
                </TouchableWithoutFeedback>

                <View style={{
                    minHeight: 35
                }}>

                    <CNToolbar
                                style={{
                                    height: 35,
                                }}
                                iconSetContainerStyle={{
                                    flexGrow: 1,
                                    justifyContent: 'space-evenly',
                                    alignItems: 'center',
                                }}
                                size={30}
                                iconSet={[
                                    {
                                        type: 'tool',
                                        iconArray: [{
                                            toolTypeText: 'image',
                                            iconComponent:
                                                <Text style={styles.toolbarButton}>
                                                image
                                                </Text>
                                        }]
                                    },
                                    {
                                        type: 'tool',
                                        iconArray: [{
                                            toolTypeText: 'bold',
                                            buttonTypes: 'style',
                                            iconComponent:
                                                <Text style={styles.toolbarButton}>
                                                bold
                                                </Text>
                                        }]
                                    },
                                    {
                                        type: 'seperator'
                                    },
                                    {
                                        type: 'tool',
                                        iconArray: [
                                            {
                                                toolTypeText: 'body',
                                                buttonTypes: 'tag',
                                                iconComponent:
                                                    <Text style={styles.toolbarButton}>
                                                    body
                                                    </Text>
                                            },
                                        ]
                                    },
                                    {
                                        type: 'tool',
                                        iconArray: [
                                            {
                                                toolTypeText: 'ul',
                                                buttonTypes: 'tag',
                                                iconComponent:
                                                    <Text style={styles.toolbarButton}>
                                                    ul
                                                    </Text>
                                            }
                                        ]
                                    },
                                    {
                                        type: 'tool',
                                        iconArray: [
                                            {
                                                toolTypeText: 'ol',
                                                buttonTypes: 'tag',
                                                iconComponent:
                                                    <Text style={styles.toolbarButton}>
                                                    ol
                                                    </Text>
                                            }
                                        ]
                                    },
                                ]}
                                selectedTag={this.state.selectedTag}
                                selectedStyles={this.state.selectedStyles}
                                onStyleKeyPress={this.onStyleKeyPress}
                            />
                </View>
        </KeyboardAvoidingView>
        );
    }

}

var styles = 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'
    },
});


export default App;

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
textInputStyleTextInput styleNo
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
placeholderThe string that will be rendered before text input has been entered.No
textInputPropsAn object containing additional props to be passed to the TextInput componentNo

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 (as of 1.0.41, this prop is deprecated)
italicNoa component which renders as italic button (as of 1.0.41, this prop is deprecated)
underlineNoa component which renders as underline button (as of 1.0.41, this prop is deprecated)
lineThroughNoa component which renders as lineThrough button (as of 1.0.41, this prop is deprecated)
bodyNoa component which renders as body button (as of 1.0.41, this prop is deprecated)
titleNoa component which renders as title button (as of 1.0.41, this prop is deprecated)
ulNoa component which renders as ul button (as of 1.0.41, this prop is deprecated)
olNoa component which renders as ol button (as of 1.0.41, this prop is deprecated)
imageNoa component which renders as image button (as of 1.0.41, this prop is deprecated)
highlightNoa component which renders as highlight button (as of 1.0.41, this prop is deprecated)
foreColorNoa component which renders as foreColor button (as of 1.0.41, this prop is deprecated)
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
iconSetYesarray of icons to display
iconSetContainerStyleNoa style props assigned to icon set 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

1.0.43

3 years ago

2.0.0-rc3

4 years ago

2.0.0-rc2

4 years ago

2.0.0-rc1

4 years ago

1.0.42

5 years ago

1.0.41

5 years ago

1.0.40

5 years ago

1.0.39

5 years ago

1.0.38

5 years ago

1.0.37

5 years ago

1.0.36

5 years ago

1.0.35

5 years ago

1.0.34

5 years ago

1.0.33

5 years ago

1.0.32

5 years ago

1.0.31

5 years ago

1.0.30

5 years ago

1.0.29

5 years ago

1.0.28

5 years ago

1.0.27

5 years ago

1.0.26

5 years ago

1.0.25

5 years ago

1.0.24

5 years ago

1.0.23

5 years ago

1.0.22

5 years ago

1.0.21

5 years ago

1.0.20

5 years ago

1.0.19

5 years ago

1.0.18

5 years ago

1.0.17

5 years ago

1.0.16

5 years ago

1.0.15

5 years ago

1.0.14

5 years ago

1.0.13

5 years ago

1.0.12

5 years ago

1.0.11

5 years ago

1.0.10

5 years ago

1.0.9

5 years ago

1.0.8

5 years ago

1.0.7

5 years ago

1.0.6

5 years ago

1.0.5

5 years ago

1.0.4

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago