1.1.2 • Published 6 years ago

draft-js-regex-highlighter-plugin v1.1.2

Weekly downloads
4
License
MIT
Repository
github
Last release
6 years ago

draft-js-regex-highlighter-plugin

A DraftJS plugin for highlighting valid/invalid strings based on regex and/or supplied strings.

This plugin has only been tested with react-draft-wysiwyg but should work using any DraftJS-based editor.

View Demo

Usage

npm i --save draft-js-regex-highlighter-plugin

then import from your editor component

import { createHighlighterPlugin } from "draft-js-regex-highlighter-plugin"

Example

This example uses a regex to match all {{ handlebar }} variables.

import React, { Component } from "react"
import { Editor } from "react-draft-wysiwyg"
import { createHighlighterPlugin } from "draft-js-regex-highlighter-plugin"
import { EditorState } from "draft-js"

const highlighterConfig = {
      regex: new RegExp(/{{\s*([^}]+)\s*}}/, 'g'),
      theme: {
        valid: {
            background: "lightblue"
        }
        invalid: {
            background: "red"
        }
      }
    }

const customDecorators = [
    createHighlighterPlugin(highlighterConfig)
]

export default class DemoEditor extends Component {

    state = {
        editorState: EditorState.createEmpty()
    }

    onChange = (editorState) => {
        this.setState({ editorState })
    }

    render() {
        return (
            <Editor
                editorState={this.state.editorState}
                onEditorStateChange={this.onChange}
                customDecorators={customDecorators}
            />
        )
    }
}

Options

ParamTypeRequiredDescription
regexRegExp*The primary pattern used to match content
secondaryRegexRegExpOptional secondary pattern to further drill down on validity of matches
variablesobjectAn object of strings to further drill down on validity of matches
componentNodeOptional React component to render instead of the default Highlighter component
themeobjectTheme object for styling the valid and invalid matches

License

MIT

1.1.2

6 years ago

1.1.1

6 years ago

1.1.0

6 years ago

1.0.4

6 years ago

1.0.3

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago