
Security News
Meet Socket at Black Hat and DEF CON 2025 in Las Vegas
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
draft-js-utils
Advanced tools
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.
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);
The draft-js-export-html package provides utilities to convert Draft.js content state to HTML. It is similar to the convertToHTML feature of draft-js-utils but focuses solely on HTML conversion.
The draft-js-import-html package allows you to import HTML content into Draft.js content state. It complements the draft-js-export-html package and is useful for converting HTML to Draft.js content state.
This is a set of utilities originally built for React-RTE, a rich text editor built on the awesome DraftJS.
This project uses Flow for static type checking. The type annotations are optional, but preferred. Each file can opt-in to using Flow by adding @flow
to the docblock or put this as the first line: // @flow
.
Flow typechecks will be run as part of the normal tests npm run test
but can also be run separately using npm run typecheck
.
FAQs
Collection of utilities for DraftJS
The npm package draft-js-utils receives a total of 168,825 weekly downloads. As such, draft-js-utils popularity was classified as popular.
We found that draft-js-utils demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 4 open source maintainers collaborating on the project.
Did you know?
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.
Security News
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
Security News
CAI is a new open source AI framework that automates penetration testing tasks like scanning and exploitation up to 3,600× faster than humans.
Security News
Deno 2.4 brings back bundling, improves dependency updates and telemetry, and makes the runtime more practical for real-world JavaScript projects.