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

draft-js-utils

Package Overview
Dependencies
Maintainers
2
Versions
17
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

draft-js-utils

Collection of utilities for DraftJS

  • 1.3.2
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
198K
decreased by-7.49%
Maintainers
2
Weekly downloads
 
Created

What is draft-js-utils?

The draft-js-utils package provides utility functions for working with Draft.js, a rich text editor framework for React. These utilities help in handling various operations like converting content state to HTML, extracting entities, and managing inline styles.

What are draft-js-utils's main functionalities?

convertToHTML

This feature allows you to convert Draft.js content state to HTML. The code sample demonstrates how to convert an HTML string to Draft.js content state and then back to HTML.

const { stateToHTML } = require('draft-js-export-html');
const { EditorState, ContentState, convertFromHTML } = require('draft-js');

const html = '<p>Hello, world!</p>';
const contentBlocks = convertFromHTML(html);
const contentState = ContentState.createFromBlockArray(contentBlocks);
const editorState = EditorState.createWithContent(contentState);

const htmlOutput = stateToHTML(editorState.getCurrentContent());
console.log(htmlOutput);

extractEntities

This feature helps in extracting entities from the content state. The code sample shows how to extract entity ranges from a block of text in Draft.js.

const { getEntityRanges } = require('draft-js-utils');
const { EditorState, ContentState, convertFromRaw } = require('draft-js');

const rawContent = {
  blocks: [{
    text: 'Hello, world!',
    type: 'unstyled',
    entityRanges: [{ offset: 0, length: 5, key: '1' }]
  }],
  entityMap: {
    '1': { type: 'LINK', mutability: 'MUTABLE', data: { url: 'http://example.com' } }
  }
};

const contentState = convertFromRaw(rawContent);
const editorState = EditorState.createWithContent(contentState);

const entityRanges = getEntityRanges(editorState.getCurrentContent().getFirstBlock(), (start, end) => true);
console.log(entityRanges);

manageInlineStyles

This feature allows you to manage inline styles in Draft.js. The code sample demonstrates how to get the selected inline styles from the editor state.

const { getSelectedInlineStyle } = require('draft-js-utils');
const { EditorState, ContentState, convertFromRaw } = require('draft-js');

const rawContent = {
  blocks: [{
    text: 'Hello, world!',
    type: 'unstyled',
    inlineStyleRanges: [{ offset: 0, length: 5, style: 'BOLD' }]
  }],
  entityMap: {}
};

const contentState = convertFromRaw(rawContent);
const editorState = EditorState.createWithContent(contentState);

const inlineStyles = getSelectedInlineStyle(editorState);
console.log(inlineStyles);

Other packages similar to draft-js-utils

Keywords

FAQs

Package last updated on 27 Feb 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