Skip to content

Instantly share code, notes, and snippets.

@abhimalamkar
Forked from scf4/index.js
Created September 12, 2018 12:41
Show Gist options
  • Save abhimalamkar/bbddeb98d36881d85cebd525fd97db84 to your computer and use it in GitHub Desktop.
Save abhimalamkar/bbddeb98d36881d85cebd525fd97db84 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