wix-incubator / react-native-zss-rich-text-editor

React Native rich text editor based on ZSSRichTextEditor
Other
839 stars 310 forks source link
editor react-native richtexteditor zssrichtexteditor

React Native Rich Text Editor

A fully functional Rich Text Editor for both Android and iOS, based off the ZSSRichTextEditor project.

Installation

npm i --save react-native-zss-rich-text-editor

On Android, add the following to the end of your android/app/build.gradle

project.afterEvaluate {
    apply from: '../../node_modules/react-native-zss-rich-text-editor/htmlCopy.gradle';
    copyEditorHtmlToAppAssets(file('../../node_modules/react-native-zss-rich-text-editor'))
}

Also, follow instructions here to add the native react-native-webview-bridge-updated dependency.

Usage

react-native-zss-rich-text-editor exports two Components and one const dictionary:

RichTextEditor

The editor component. Simply place this component in your view hierarchy to receive a fully functional Rich text Editor.

RichTextEditor takes the following optional props:

RichTextEditor also has methods that can be used on its ref to set styling at the current selection or cursor position:

This method shows a dialog for setting a link title and url, that will be inserted at the current cursor location.

To adjust content, placeholders or css, use these methods

These methods are used when adding content such as images or links that will intefere with the cursor position. prepareInsert saves the current selection, and restoreSelection will replace it after the insertion is done. It is called implicitly by insertImage and insertLink so they should probably never be called directly.

To get the content or title HTML, use these asynchronous methods.

To focus or blur sections, use these methods

To know when the title or content are in focus, use the following methods.

This method registers a function that will get called whenver the cursor position changes or a change is made to the styling of the editor at the cursor's position., The callback will be called with an array of actions that are active at the cusor position, allowing a toolbar to respond to changes.

Example Usage:

<RichTextEditor
  ref={(r) => this.richtext = r}
  initialTitleHTML={'Title!!'}
  initialContentHTML={'Hello <b>World</b> <p>this is a new paragraph</p> <p>this is another new paragraph</p>'}
  editorInitializedCallback={() => this.onEditorInitialized()}
/>

RichTextEditor

RichTextToolbar

This is a Component that provides a toolbar for easily controlling an editor. It is designed to be used together with a RichTextEditor component.

The RichTextToolbar has one required property:

Which must provide a function that returns a ref to a RichTextEditor component.

This is because the ref is not created until after the first render, before which the toolbar is rendered. This means that any ref passed directly will inevitably be passed as undefined.

Other props supported by the RichTextToolbar component are:

Example Usage:

<RichTextToolbar
    getEditor={() => this.richtext}
/>

RichTextEditor

RichTextEditor

actions

This is a set of consts of all supported actions. These will be passed in arrays to all callbacks registered with the editor using the registerToolbar() method.

{
    setTitleHtml: 'SET_TITLE_HTML',
    setContentHtml: 'SET_CONTENT_HTML',
    getTitleHtml: 'GET_TITLE_HTML',
    getTitleText: 'GET_TITLE_TEXT',
    getContentHtml: 'GET_CONTENT_HTML',
    getSelectedText: 'GET_SELECTED_TEXT',
    blurTitleEditor: 'BLUR_TITLE_EDITOR',
    blurContentEditor: 'BLUR_CONTENT_EDITOR',
    focusTitle: 'FOCUS_TITLE',
    focusContent: 'FOCUS_CONTENT',

    setBold: 'bold',
    setItalic: 'italic',
    setUnderline: 'underline',
    heading1: 'h1',
    heading2: 'h2',
    heading3: 'h3',
    heading4: 'h4',
    heading5: 'h5',
    heading6: 'h6',
    setParagraph: 'SET_PARAGRAPH',
    removeFormat: 'REMOVE_FORMAT',
    alignLeft: 'justifyLeft',
    alignCenter: 'justifyCenter',
    alignRight: 'justifyRight',
    alignFull: 'justifyFull',
    insertBulletsList: 'unorderedList',
    insertOrderedList: 'orderedList',
    insertLink: 'INST_LINK',
    updateLink: 'UPDATE_LINK',
    insertImage: 'INST_IMAGE',
    setSubscript: 'subscript',
    setSuperscript: 'superscript',
    setStrikethrough: 'strikeThrough',
    setHR: 'horizontalRule',
    setIndent: 'indent',
    setOutdent: 'outdent',
    setTitlePlaceholder: 'SET_TITLE_PLACEHOLDER',
    setContentPlaceholder: 'SET_CONTENT_PLACEHOLDER',
    setTitleFocusHandler: 'SET_TITLE_FOCUS_HANDLER',
    setContentFocusHandler: 'SET_CONTENT_FOCUS_HANDLER',
    prepareInsert: 'PREPARE_INSERT',
    restoreSelection: 'RESTORE_SELECTION',
    setCustomCSS: 'SET_CUSTOM_CSS',
    setTextColor: 'SET_TEXT_COLOR',
    setBackgroundColor: 'SET_BACKGROUND_COLOR',
}

Attribution

react-native-zss-rich-text-editor is a wrapper around the amazing ZSSRichTextEditor project. It also communicates with the editor using (a tiny fork) of the awesome react-native-webview-bridge project.