npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-draft-editor

v1.0.0

Published

React + Redux compatible implementation of DraftJS

Downloads

8

Readme

#React Draft Editor

An implementation of Draft.js which is simple to adapt and extend.

npm install react-draft-editor --save

##What's in the box?

A component for editing text, several components for styling that text, and components for previewing the text being edited as well as rendering it as a React component in your application.

###Prerequisites

Create a parent component.

Import the EditorState class from draft-js.

Import the <DraftEditor /> component from react-draft-editor.

Create an editorState instance.

Create a handler method for onChange events.

import React from 'react'

import {
  EditorState
} from 'draft-js'

import {
  DraftEditor
} from 'react-draft-editor'

export class ParentEditor extends React.Component {
  constructor (props) {
    super(props)
    this.state = {
      editorState: EditorState.createEmpty()
    }
  }

  handleChange(editorState) {
    this.setState({ editorState })
  }

  render() {
    return (
      <DraftEditor
        onChange={this.handleChange.bind(this)}
        editorState={this.state.editorState}
      />
    )
  }
}

With that, we can begin editing text, and we can change the style of that text with keyboard controls. But we can also create visual elements for styling the text by importing some more components.

###Styling Text

For styling text as bold, import the <Bold /> component from react-draft-editor.

import {
  Bold
} from 'react-draft-editor'

Then modify the render function of <ParentEditor />:

  render() {
    const handleChange = this.handleChange.bind(this)
    const { editorState } = this.state
    return (
      <div className='container'>
        <Bold
          onChange={handleChange}
          editorState={editorState}>
            Bold
        </Bold>
        <DraftEditor
          onChange={handleChange}
          editorState={editorState}
        />
      </div>
    )
  }

The <Bold /> component will render as a button in HTML. If you don't want a button you can always extend the component and override the render function. Let's change button to span:

class MyBold extends Bold {
  render() {
  	return (
      <span
        className={this.getClassNames()}
        onMouseDown={this.handleMouseDown.bind(this)}
        onMouseUp={this.handleMouseUp.bind(this)}>
          Bold
      </span>
  	)
  }
}

Then use <MyBold /> in place of <Bold />.

For styling text as italic, import the <Italic /> component. For styling text as underline, import the <Underline /> component.

For everything, import everything:

import {
  Bold,
  Italic,
  Code,
  Underline,
  BlockQuote,
  CodeBlock,
  HeaderOne,
  HeaderTwo,
  HeaderThree,
  HeaderFour,
  HeaderFive,
  HeaderSix,
  OrderedList,
  UnorderedList,
  DraftEditor
} from 'react-draft-editor'

Or, pick and choose. Like a buffet.

###Saving Text

The parent component we have created doesn't do anything with the text you can edit and style. It doesn't import text from anywhere and it doesn't export it, either. That's intentional. The react-draft-editor package doesn't mind where the the text comes from or where it goes to: it only minds about onChange and editorState.

That said, you probably mind a great deal.

Draft.js converts back and forth from JSON using two methods:

  • convertFromRaw
  • convertToRaw

You can import these along with the EditorState class:

import {
  EditorState,
  convertFromRaw,
  convertToRaw
} from 'draft-js'

Modify the parent component to consume two props:

  • onSaveContent
  • content

Here, onSaveContent is handler method, and content is JSON.

export class ParentEditor extends React.Component {
  constructor (props) {
    super(props)
    const { onSaveContent, content } = this.props
    this.state = {
      onSaveContent,
      editorState: EditorState.createWithContent(convertFromRaw(content))
    }
  }

  handleSaveClick() {
  	const { onSaveContent, editorState } = this.state
  	onSaveContent(convertToRaw(editorState.getCurrentContent()))
  }

  render() {
  	return (
  	  <button onClick={this.handleSaveClick.bind(this)}>
  	    Save
  	  </button>
  	)
  }
}

What happens with the JSON? What does onSaveContent do with it?

That's up to you.

###Examples

There are some example implementations included in the package. On the file system, cd to the react-draft-editor package and the directory named pub. In there, you'll find client, server and public. Look in client for the React app, server for the Express app, and public for the HTML and CSS.

To launch the application in your browswer, install all of the dependencies and the devDependencies described in package.json, then at the command line type npm start. Once the start process has completed, printed in your terminal you should see:

[React Draft Editor] 8080

Browse http://localhost:8080.

###One Last Thing

The react-draft-editor package contains React components, but it does not have any opinion on the styling that you apply to those components, or to the Draft.js components it implements, but it does define some class names which change according to user behaviour.

The <Bold /> component will render as a <button /> in HTML, with the class attribute populated as bold:

<button class="bold">B</bold>

When text in the editor is selected and the button is clicked, that class attribute becomes:

<button class="bold is-selected">B</bold>

And that's it. Hover states, focus states, and exactly what styling is applied by the bold and is-selected declarations? Again, that's up to you.

###One Very Last Thing

You may want to include two declarations in your CSS:

  .public-DraftStyleDefault-block { margin: 1rem 0; }
  li .public-DraftStyleDefault-block { margin: 0; }

Users will be tempted to hit their carriage return key with abandon if they don't see some separation between blocks of text in the editor. You won't want that.

And ... you may recall, I mentioned "components for previewing the text" and "rendering it as a React component". Implementations are included in the examples.