Gathering detailed insights and metrics for draftjs-conductor
Gathering detailed insights and metrics for draftjs-conductor
Gathering detailed insights and metrics for draftjs-conductor
Gathering detailed insights and metrics for draftjs-conductor
📝✨ Little Draft.js helpers to make rich text editors “just work”
npm install draftjs-conductor
Typescript
Module System
Node Version
NPM Version
TypeScript (78.76%)
JavaScript (8.82%)
HTML (5.96%)
Shell (3.49%)
CSS (2.96%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
MIT License
40 Stars
752 Commits
6 Forks
3 Watchers
13 Branches
6 Contributors
Updated on Mar 08, 2025
Latest Version
3.0.0
Package Id
draftjs-conductor@3.0.0
Unpacked Size
40.57 kB
Size
9.68 kB
File Count
6
NPM Version
8.12.0
Node Version
18.3.0
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
1
30
📝✨ Little Draft.js helpers to make rich text editors just work. Built for Draftail.
Check out the online demo!
By default, Draft.js only provides support for 5 list levels for bulleted and numbered lists. While this is often more than enough, some editors need to go further.
Instead of manually writing and maintaining the list nesting styles, use those little helpers:
1import { getListNestingStyles, blockDepthStyleFn } from "draftjs-conductor"; 2 3<style> 4 {getListNestingStyles(6)} 5</style> 6<Editor blockStyleFn={blockDepthStyleFn} />
getListNestingStyles
will generate the necessary CSS for your editor’s lists. blockDepthStyleFn
will then apply classes to blocks based on their depth, so the styles take effect. Voilà!
If your editor’s maximum list nesting depth never changes, pre-render the styles as a fragment for better performance:
1const listNestingStyles = <style>{getListNestingStyles(6)}</style>;
You can also leverage React.memo
to speed up re-renders even if max
was to change:
1const NestingStyles = React.memo(ListNestingStyles); 2 3<style> 4 <NestingStyles max={max} /> 5</style>;
Relevant Draft.js issues:
The default Draft.js copy-paste handlers lose a lot of the formatting when copy-pasting between Draft.js editors. While this might be ok for some use cases, sites with multiple editors on the same page need them to reliably support copy-paste.
Relevant Draft.js issues:
<p>
tags on paste – facebook/draft-js#523 (comment)All of those problems can be fixed with this library, which overrides the copy
and cut
events to transfer more of the editor’s content, and introduces a function to use with the Draft.js handlePastedText
to retrieve the pasted content.
This will paste all copied content, even if the target editor might not support it. To ensure only supported content is retained, use filters like draftjs-filters.
Note: IE11 isn’t supported, as it doesn't support storing HTML in the clipboard, and we also use the Element.closest
API.
Here’s how to use the copy/cut override, and the paste handler:
1import { 2 onDraftEditorCopy, 3 onDraftEditorCut, 4 handleDraftEditorPastedText, 5} from "draftjs-conductor"; 6 7class MyEditor extends Component { 8 constructor(props: Props) { 9 super(props); 10 11 this.state = { 12 editorState: EditorState.createEmpty(), 13 }; 14 15 this.onChange = this.onChange.bind(this); 16 this.handlePastedText = this.handlePastedText.bind(this); 17 } 18 19 onChange(nextState: EditorState) { 20 this.setState({ editorState: nextState }); 21 } 22 23 handlePastedText( 24 text: string, 25 html: string | null, 26 editorState: EditorState, 27 ) { 28 let newState = handleDraftEditorPastedText(html, editorState); 29 30 if (newState) { 31 this.onChange(newState); 32 return true; 33 } 34 35 return false; 36 } 37 38 render() { 39 const { editorState } = this.state; 40 41 return ( 42 <Editor 43 editorState={editorState} 44 onChange={this.onChange} 45 onCopy={onDraftEditorCopy} 46 onCut={onDraftEditorCut} 47 handlePastedText={this.handlePastedText} 48 /> 49 ); 50 } 51}
The copy/cut event handlers will ensure the clipboard contains a full representation of the Draft.js content state on copy/cut, while handleDraftEditorPastedText
retrieves Draft.js content state from the clipboard. Voilà! This also changes the HTML clipboard content to be more semantic, with less styles copied to other word processors/editors.
You can also use getDraftEditorPastedContent
method and set new EditorState by yourself. It is useful when you need to do some transformation with content (for example filtering unsupported styles), before past it in the state.
The above code relies on the onCopy
and onCut
event handlers, only available from Draft.js v0.11.0 onwards. For Draft.js v0.10.5, use registerCopySource
instead, providing a ref
to the editor:
1import { 2 registerCopySource, 3 handleDraftEditorPastedText, 4} from "draftjs-conductor"; 5 6class MyEditor extends Component { 7 componentDidMount() { 8 this.copySource = registerCopySource(this.editorRef); 9 } 10 11 componentWillUnmount() { 12 if (this.copySource) { 13 this.copySource.unregister(); 14 } 15 } 16 17 render() { 18 const { editorState } = this.state; 19 20 return ( 21 <Editor 22 ref={(ref) => { 23 this.editorRef = ref; 24 }} 25 editorState={editorState} 26 onChange={this.onChange} 27 handlePastedText={this.handlePastedText} 28 /> 29 ); 30 } 31}
The setup is slightly different with draft-js-plugins
(and React hooks) – we need to use the provided getEditorRef
method:
1// reference to the editor 2const editor = useRef<Editor>(null); 3 4// register code for copying 5useEffect(() => { 6 let unregisterCopySource: undefined | unregisterObject = undefined; 7 if (editor.current !== null) { 8 unregisterCopySource = registerCopySource( 9 editor.current.getEditorRef() as any, 10 ); 11 } 12 return () => { 13 unregisterCopySource?.unregister(); 14 }; 15});
See #115 for further details.
Draft.js has its own data conversion helpers, convertFromRaw
and convertToRaw
, which work really well, but aren’t providing that good of an API when initialising or persisting the content of an editor.
We provide two helper methods to simplify the initialisation and serialisation of content. createEditorStateFromRaw
combines EditorState.createWithContent
, EditorState.createEmpty
and convertFromRaw
as a single method:
1import { createEditorStateFromRaw } from "draftjs-conductor"; 2 3// Initialise with `null` if there’s no preexisting state. 4const editorState = createEditorStateFromRaw(null); 5// Initialise with the raw content state otherwise 6const editorState = createEditorStateFromRaw({ entityMap: {}, blocks: [] }); 7// Optionally use a decorator, like with Draft.js APIs. 8const editorState = createEditorStateFromRaw(null, decorator);
To save content, serialiseEditorStateToRaw
combines convertToRaw
with checks for empty content – so empty content is saved as null
, rather than a single text block with empty text as would be the case otherwise.
1import { serialiseEditorStateToRaw } from "draftjs-conductor"; 2 3// Content will be `null` if there’s no textual content, or RawDraftContentState otherwise. 4const content = serialiseEditorStateToRaw(editorState);
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.
View the full list of contributors. MIT licensed. Website content available as CC0. Image credit: FirefoxEmoji.
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
security policy file detected
Details
Reason
license file detected
Details
Reason
packaging workflow detected
Details
Reason
dependency not pinned by hash detected -- score normalized to 3
Details
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
Found 0/30 approved changesets -- score normalized to 0
Reason
no SAST tool detected
Details
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
Reason
Project has not signed or included provenance with any releases.
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
52 existing vulnerabilities detected
Details
Score
Last Scanned on 2025-07-07
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More