Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@scf4
Created July 5, 2017 19:30
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save scf4/bb49f9167d1c24357d5ee0a8bbb777de to your computer and use it in GitHub Desktop.
Save scf4/bb49f9167d1c24357d5ee0a8bbb777de to your computer and use it in GitHub Desktop.
[Draft.js plugins] How to force block style on first line for a heading/title like Medium
// Import and add to your Editor's plugins
import { RichUtils } from 'draft-js';
const HEADING = 'header-one';
export default () => ({
onChange: (editorState) => {
const currentContent = editorState.getCurrentContent();
const firstBlockKey = currentContent.getBlockMap().first().getKey();
const currentBlockKey = editorState.getSelection().getAnchorKey();
const isFirstBlock = (currentBlockKey === firstBlockKey);
const currentBlockType = RichUtils.getCurrentBlockType(editorState);
const isHeading = currentBlockType === HEADING;
if (isFirstBlock !== isHeading) {
return RichUtils.toggleBlockType(editorState, HEADING);
}
return editorState;
},
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment