Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

draft-js

Package Overview
Dependencies
Maintainers
4
Versions
32
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

draft-js

A React framework for building text editors.

  • 0.11.0-beta2
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
809K
increased by2.19%
Maintainers
4
Weekly downloads
 
Created

What is draft-js?

Draft.js is a JavaScript rich text editor framework, built for React. It provides a set of immutable models and helper functions for creating rich text editors with a high degree of customization and control over the content and behavior.

What are draft-js's main functionalities?

Creating a Basic Editor

This code demonstrates how to create a basic rich text editor using Draft.js. It initializes an empty editor state and renders the editor component.

import React from 'react';
import { Editor, EditorState } from 'draft-js';
import 'draft-js/dist/Draft.css';

class MyEditor extends React.Component {
  constructor(props) {
    super(props);
    this.state = { editorState: EditorState.createEmpty() };
    this.onChange = (editorState) => this.setState({ editorState });
  }

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

export default MyEditor;

Handling Text Formatting

This code demonstrates how to handle text formatting commands in Draft.js. It uses the `RichUtils.handleKeyCommand` method to apply formatting like bold, italic, etc., based on keyboard shortcuts.

import React from 'react';
import { Editor, EditorState, RichUtils } from 'draft-js';
import 'draft-js/dist/Draft.css';

class MyEditor extends React.Component {
  constructor(props) {
    super(props);
    this.state = { editorState: EditorState.createEmpty() };
    this.onChange = (editorState) => this.setState({ editorState });
    this.handleKeyCommand = this.handleKeyCommand.bind(this);
  }

  handleKeyCommand(command, editorState) {
    const newState = RichUtils.handleKeyCommand(editorState, command);
    if (newState) {
      this.onChange(newState);
      return 'handled';
    }
    return 'not-handled';
  }

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

export default MyEditor;

Custom Block Rendering

This code demonstrates how to implement custom block rendering in Draft.js. It defines a custom block renderer function and a custom block component to render specific types of content blocks.

import React from 'react';
import { Editor, EditorState, ContentBlock } from 'draft-js';
import 'draft-js/dist/Draft.css';

function myBlockRenderer(contentBlock) {
  const type = contentBlock.getType();
  if (type === 'atomic') {
    return {
      component: MyCustomBlockComponent,
      editable: false,
    };
  }
}

class MyCustomBlockComponent extends React.Component {
  render() {
    return <div>My Custom Block</div>;
  }
}

class MyEditor extends React.Component {
  constructor(props) {
    super(props);
    this.state = { editorState: EditorState.createEmpty() };
    this.onChange = (editorState) => this.setState({ editorState });
  }

  render() {
    return (
      <Editor
        editorState={this.state.editorState}
        onChange={this.onChange}
        blockRendererFn={myBlockRenderer}
      />
    );
  }
}

export default MyEditor;

Other packages similar to draft-js

Keywords

FAQs

Package last updated on 08 Mar 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc