1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-08-01 04:50:27 +02:00

add always ending in paragraph to image example

This commit is contained in:
Ian Storm Taylor
2016-07-22 21:28:28 -07:00
parent 63867e0e2f
commit e80799252f

View File

@@ -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}
/>
</div>
@@ -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.
*