Draft.js Fiddle
A fiddle to repro issues in Draft.js
HTML
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.3.0/react.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.3.0/react-dom.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/immutable/3.8.1/immutable.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/draft-js/0.10.1/Draft.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/draft-js/0.10.1/Draft.css">
<b>Bold Text</b>, Regular Text
<div id="react-root"></div>
CSS
body {
font-family: Helvetica, sans-serif;
}
.container-root {
border: 1px solid black;
padding: 5px;
margin: 5px;
}
Babel + JSX
const {Editor, EditorState, SelectionState, Modifier:DraftModifier} = Draft;
class Container extends React.Component {
constructor(props) {
super(props);
this.state = {
editorState: EditorState.createEmpty()
};
}
handleChange = (editorState) => {
this.setState({editorState});
logEditorText('setState@handleChange', editorState);
}
onPaste = (event) => {
const newEditorState = removeEditorStyles(this.state.editorState);
this.setState({
editorState: newEditorState
});
logEditorText('setState@onPaste', newEditorState);
}
componentDidUpdate() {
console.group("render cycle");
}
render() {
logEditorText('render', this.state.editorState);
console.groupEnd();
return (
<div className="container-root" onPaste={this.onPaste}>
<Editor
placeholder="Type away :)"
editorState={this.state.editorState}
onChange={this.handleChange}
/>
</div>
);
}
}
function getBlockSelection(block, start, end) {
const blockKey = block.getKey();
return new SelectionState({
anchorKey: blockKey,
anchorOffset: start,
focusKey: blockKey,
focusOffset: end,
});
}
function removeEditorStyles(editorState) {
let newEditorState = editorState;
let newContent = editorState.getCurrentContent();
const blocks = newContent.getBlocksAsArray();
for (let block of blocks) {
block.findStyleRanges(() => true, function(start, end) {
newContent = DraftModifier.removeInlineStyle(
newContent,
getBlockSelection(block, start, end),
block.getInlineStyleAt(start),
);
});
newEditorState = EditorState.push(newEditorState, newContent, 'change-inline-style');
}
return newEditorState;
}
function logEditorText(label, editorState) {
const editorText = editorState.getCurrentContent().getPlainText();
console.log(label, `"${editorText}"`);
}
ReactDOM.render(<Container />,...