Filter Draft.js content to preserve only the formatting you allow. Built for Draftail and Wagtail.
![Screenshot of Microsoft Word with tens of toolbars activated](https://thibaudcolas.github.io/draftjs-filters/word-toolbars-overload.jpg)
The main use case is to select what formatting to keep when copy-pasting rich text into an editor, for example from Word or Google Docs, addressing Draft.js limitations like #166 and #504. Check out the online demo!
If you want to learn more about how this is used in practice, have a look at Rethinking rich text pipelines with Draft.js.
Using the filters
First, grab the package from npm:
npm install --save draftjs-filters
Then, in your editor import filterEditorState
and call it in the Draft.js onChange
handler. This function takes two parameters: the filtering configuration, and the editorState
.
import { filterEditorState } from "draftjs-filters"
function onChange(nextState) {
const { editorState } = this.state
let filteredState = nextState
const shouldFilterPaste =
nextState.getCurrentContent() !== editorState.getCurrentContent() &&
nextState.getLastChangeType() === "insert-fragment"
if (shouldFilterPaste) {
filteredState = filterEditorState(
{
blocks: ["header-two", "header-three", "unordered-list-item"],
styles: ["BOLD"],
entities: [
{
type: "IMAGE",
attributes: ["src"],
whitelist: {
src: "^http",
},
},
{
type: "LINK",
attributes: ["url"],
},
],
maxNesting: 1,
whitespacedCharacters: ["\n", "\t", "📷"],
},
filteredState,
)
}
this.setState({ editorState: filteredState })
}
Here are the available options:
blocks: Array<string>,
styles: Array<string>,
entities: Array<{
type: string,
attributes: Array<string>,
whitelist: {
[attribute: string]: string,
},
}>,
maxNesting: number,
whitespacedCharacters: Array<string>,
Types
If your project uses Flow, type inference should just work. If you don't use Flow, it won't get in your way either.
Advanced usage
filterEditorState
isn't very flexible. If you want more control over the filtering, simply compose your own filter function with the other single-purpose utilities. The Draft.js filters are published as ES6 modules using Rollup – module bundlers like Rollup and Webpack will tree shake (remove) the unused functions so you only bundle the code you use.
preserveAtomicBlocks((content: ContentState))
resetAtomicBlocks((content: ContentState))
removeInvalidAtomicBlocks((whitelist: Array<Object>), (content: ContentState))
removeInvalidDepthBlocks((content: ContentState))
limitBlockDepth((max: number), (content: ContentState))
preserveBlockByText(
(rules: Array<{
test: string,
type: string,
depth: number,
}>),
(content: ContentState),
)
filterBlockTypes((whitelist: Array<string>), (content: ContentState))
filterInlineStyles((whitelist: Array<string>), (content: ContentState))
cloneEntities((content: ContentState))
filterEntityRanges(
(filterFn: (
content: ContentState,
entityKey: string,
block: ContentBlock,
) => boolean),
(content: ContentState),
)
shouldKeepEntityType((whitelist: Array<Object>), (type: string))
shouldRemoveImageEntity((entityType: string), (blockType: string))
shouldKeepEntityByAttribute(
(entityTypes: Array<Object>),
(entityType: string),
(data: Object),
)
filterEntityData((entityTypes: Array<Object>), (content: ContentState))
replaceTextBySpaces((characters: Array<string>), (content: ContentState))
Browser support and polyfills
The Draft.js filters follow the browser support targets of Draft.js. Be sure to have a look at the required Draft.js polyfills.
Word processor support
Have a look at our test data in pasting/
.
Editor - Browser | Chrome Windows | Chrome macOS | Firefox Windows | Firefox macOS | Edge Windows | IE11 Windows | Safari macOS | Safari iOS | Chrome Android |
---|
Word 2016 | | | | | | | | N/A | N/A |
Word 2010 | | N/A | | N/A | | | N/A | N/A | N/A |
Apple Pages | N/A | | N/A | | N/A | N/A | | | N/A |
Google Docs | | | | | | | | | |
Word Online | | | | | | Unsupported | | ? | ? |
Dropbox Paper | | | | | | Unsupported | | ? | ? |
Draft.js | | | | | | | | | |
Use the Draft.js Cut/Copy/Paste testing plan. We target specific external sources, and have ready-made test documents available to test them:
External sources
Here are external sources we want to pay special attention to, and for which we have ready-made test documents with diverse rich content.
IE11
There are known Draft.js issues with pasting in IE11. For now, we advise users to turn on stripPastedStyles
in IE11 only so that Draft.js removes all formatting but preserves whitespace:
const IS_IE11 = !window.ActiveXObject && "ActiveXObject" in window
const editor = <Editor stripPastedStyles={IS_IE11} />
Contributing
See anything you like in here? Anything missing? We welcome all support, whether on bug reports, feature requests, code, design, reviews, tests, documentation, and more. Please have a look at our contribution guidelines.
Credits
View the full list of contributors. MIT licensed. Website content available as CC0.
Microsoft Word toolbars screenshot from PCWorld – Microsoft Word Turns 25 article.