diff --git a/examples/images/index.js b/examples/images/index.js index f654c4035..5dbcacc61 100644 --- a/examples/images/index.js +++ b/examples/images/index.js @@ -87,6 +87,7 @@ class Images extends React.Component { state={this.state.state} renderNode={this.renderNode} onChange={this.onChange} + onDocumentChange={this.onDocumentChange} onPaste={this.onPaste} /> @@ -114,6 +115,34 @@ class Images extends React.Component { this.setState({ state }) } + /** + * On document change, if the last block is an image, add another paragraph. + * + * @param {Document} document + * @param {State} state + */ + + onDocumentChange = (document, state) => { + const blocks = document.getBlocks() + const last = blocks.last() + if (last.type != 'image') return + + const normalized = state + .transform() + .collapseToEndOf(last) + .splitBlock() + .setBlock({ + type: 'paragraph', + isVoid: false, + data: {} + }) + .apply({ + snapshot: false + }) + + this.onChange(normalized) + } + /** * On clicking the image button, prompt for an image and insert it. *