react-native-pell-rich-editor

repository·master·Indexed 21 days ago

https://github.com/wxik/react-native-rich-editor

A WebView-based rich text editor for React Native (Android and iOS), version 1.10.0. It provides a RichEditor component for content editing and a RichToolbar component for formatting control. Features include support for custom fonts via Base64, custom toolbar actions, and utility functions like createHTML and getContentCSS for processing editor output.

Tokens
4.1K
Snippets
14
Records
18
Agent score
74%

What's inside react-native-pell-rich-editor

  1. Use Custom Fonts in RichEditor

    master

    To use custom fonts, use the initialCSSText property within the editorStyle prop. This involves injecting a @font-face rule via Base64.

    1. Convert your font to Base64 (e.g., via transfonter.org).
    2. Create a stylesheet string containing the @font-face rule.
    3. Pass it to RichEditor using initialCSSText for the font definition and contentCSSText to apply it to the content.
    const FontFamilyStylesheet = `
    @font-face {
        font-family: 'Your Font Family';
        src: url('data:font/ttf;charset=utf-8;base64,...............');
        font-weight: normal;
    }
    `;
    
    const initialCSSText = {
      initialCSSText: `${FontFamilyStylesheet}`,
      contentCSSText: `font-family: 'Your Font Family'`
    };
    
    <RichEditor editorStyle={initialCSSText}/>
  2. Handle the Scroll Problem with RichEditor

    master

    When using useContainer={true}, the editor might have scrolling issues if placed inside a ScrollView. To fix this, you must provide an onCursorPosition callback to handle scroll bar positioning manually.

    Example logic for the callback:

    this.scrollRef.current.scrollTo({y: scrollY - 30, animated: true});
  3. Add Custom Actions to RichToolbar

    master

    You can extend the toolbar by adding custom action names to the actions array. You must then provide an icon via iconMap and a handler function with the same name as the action.

    <RichToolbar
        editor={that.richText}
        actions=[
            actions.setBold,
            actions.setItalic,
            'customAction',
        ]}
        iconMap={{
            customAction: customIcon,
        }}
        customAction={this.handleCustomAction}
    />
  4. Use the RichEditor component

    master

    The RichEditor component is the core editor. It can be customized via several props including initialContentHTML, placeholder, and editorStyle. You can interact with the editor instance using a ref to execute commands or manipulate the DOM.

    <RichEditor
      ref={(r) => this.richtext = r}
      initialContentHTML={'Hello <b>World</b>'}
      editorInitializedCallback={() => this.onEditorInitialized()}
    />
  5. Use the RichToolbar component

    master

    The RichToolbar provides a UI for controlling the RichEditor. It requires a getEditor prop, which must be a function that returns the ref of the RichEditor. This ensures the ref is available when the toolbar renders.

    Common actions include actions.setBold, actions.insertImage, actions.undo, etc.

    const richText = React.useRef();
    
    // In your render:
    <RichToolbar editor={richText} />
  6. RichEditor Props Reference

    master

    A list of available props for the RichEditor component:

    html: Accepts custom HTML.
    placeholder: Wrap the editor content placeholder.
    initialContentHTML: HTML rendered in the content section on load.
    initialFocus: Boolean to request focus on load (default: false).
    disabled: Boolean to disable editor (default: false).
    enterKeyHint: String value to set return key type.
    editorInitializedCallback: Function called when editor is initialized.
    editorStyle: Object for styling (backgroundColor, color, caretColor, placeholderColor, contentCSSText, cssText, initialCSSText).
    onChange: Callback after editor data modification.
    onHeightChange: Callback after height change.
    onMessage: Callback for postMessage internal type processing.
    command: Execute JS in the editor (e.g., this.richText.current?.commandDOM('$.execCommand(...)')).
    commandDOM: Manipulate the DOM in the editor (e.g., this.richText.current?.commandDOM("$('#title').style.color='red'")).
    useContainer: Boolean determining if a View container wraps the WebView (default: true).
    styleWithCSS: Boolean; if true, style attribute of tags are modified (default: false).
    initialHeight: Used if useContainer is false.
    pasteAsPlainText: Boolean; if true, clipboard paste is plain text (default: false).
    useCharacter: Boolean; enables/disables Chinese character support on Android (default: true).
    defaultHttps: Boolean; prepends https:// to links (default: true).
    onPaste: Callback for clipboard paste.
    onKeyUp: Callback for Keyup event.
    onKeyDown: Callback for Keydown event.
    onInput: Callback for input value.
    onLink: Callback for link click.
    onFocus: Callback for editor focus.
    onBlur: Callback for editor blur.
    onCursorPosition: Callback for cursor position changes.
  7. RichEditor Ref Methods

    master

    The following methods are available on the RichEditor ref instance:

    setContentHTML(html: string)
    insertImage(url: string, style?: string)
    insertLink(title: string, url: string)
    insertText(text: string)
    insertHTML(html: string)
    insertVideo(url: string, style?: string)
    setContentFocusHandler(handler: Function): Registers a function called when cursor position or styling changes.
    blurContentEditor()
    focusContentEditor()
    registerToolbar(listener: Function)
  8. Use default actions in RichToolbar

    master

    You can use the defaultActions array to quickly populate a toolbar with common editing commands. These include keyboard toggling, bold, italic, underline, bullet lists, indentation, and more.

    To use them, import defaultActions from the package and pass them to the actions prop of RichToolbar.

    import RichToolbar from 'react-native-rich-editor';
    import { defaultActions } from 'react-native-rich-editor';
    
    // Inside your component
    <RichToolbar
      getEditor={() => this.editorRef}
      actions={defaultActions}
    />
  9. Configure the RichToolbar component

    master

    The RichToolbar component provides a UI for executing rich text editing actions. It requires a connection to a RichEditor instance via the getEditor prop to function correctly.

    Key Props

    PropTypeDefaultDescription
    getEditor() => RefundefinedA function that returns a ref to the RichEditor instance. This is critical for the toolbar to send commands to the editor.
    actionsArraydefaultActionsAn array of action constants to display in the toolbar.
    disabledbooleanfalseIf true, all toolbar buttons are disabled.
    iconSizenumber20The size of the icons in pixels.
    iconGapnumber16The spacing/width used for each button item.
    iconTintstring'#71787F'The color applied to the icons.
    selectedIconTintstringundefinedThe color applied to icons when the action is currently active/selected.
    disabledIconTintstringundefinedThe color applied to icons when the toolbar is disabled.
    horizontalbooleantrueWhether the toolbar items are laid out horizontally in a FlatList.
    iconMapObjectundefinedAn object mapping action constants to image sources or custom icon functions.
    renderActionFunctionundefinedA custom renderer for individual action buttons. Receives (action, selected) as arguments.
    onPressAddImageFunctionundefinedCallback triggered when actions.insertImage is pressed.
    onInsertLinkFunctionundefinedCallback triggered when actions.insertLink is pressed.
    insertVideoFunctionundefinedCallback triggered when actions.insertVideo is pressed.
    styleStylePropundefinedContainer style for the toolbar.
    flatContainerStyleStylePropundefinedStyle for the internal FlatList.
    itemStyleStylePropundefinedStyle for individual action items.
    selectedButtonStyleStylePropundefinedStyle applied to a button when its action is selected.
    unselectedButtonStyleStylePropundefinedStyle applied to a button when its action is not selected.
    disabledButtonStyleStylePropundefinedStyle applied to the toolbar when disabled is true.
    disabledButtonStyleStylePropundefinedStyle applied to a button when it is disabled.
  10. Customize RichToolbar icons

    master

    If you want to use your own icons instead of the default ones, provide an iconMap prop to RichToolbar. The iconMap should be an object where keys are action constants (from the actions object) and values are either an image source or a function that returns a component.

    If you provide a function, it receives an object containing: { selected, disabled, tintColor, iconSize, iconGap }.

    const customIconMap = {
      [actions.setBold]: ({ selected, tintColor, iconSize }) => (
        <MyCustomBoldIcon selected={selected} color={tintColor} size={iconSize} />
      ),
      [actions.setItalic]: require('./my-italic-icon.png'),
    };
    
    <RichToolbar
      getEditor={() => this.editorRef}
      iconMap={customIconMap}
    />