1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-03-08 14:59:45 +01:00

172 lines
3.1 KiB
JavaScript
Raw Normal View History

2016-06-28 18:26:56 -07:00
2016-07-06 14:21:11 -07:00
import { Editor, Mark, Raw } from '../..'
2016-06-28 18:26:56 -07:00
import React from 'react'
import ReactDOM from 'react-dom'
2016-07-07 08:35:13 -07:00
import initialState from './state.json'
2016-06-28 18:26:56 -07:00
import { Map } from 'immutable'
/**
* Node renderers.
*
* @type {Object}
*/
const NODES = {
paragraph: props => <p>{props.children}</p>,
image: (props) => {
const { node, state } = props
const { data } = node
const isActive = state.isFocused && state.blocks.includes(node)
const src = data.get('src')
return <img src={src} data-active={isActive} />
}
}
2016-06-28 18:26:56 -07:00
/**
* The images example.
*
* @type {Component}
2016-06-28 18:26:56 -07:00
*/
class Images extends React.Component {
state = {
2016-07-07 08:35:13 -07:00
state: Raw.deserialize(initialState)
2016-06-28 18:26:56 -07:00
};
/**
* Render the app.
*
* @return {Element} element
*/
2016-07-07 08:35:13 -07:00
render = () => {
2016-06-28 18:26:56 -07:00
return (
<div>
{this.renderToolbar()}
{this.renderEditor()}
</div>
)
}
/**
* Render the toolbar.
*
* @return {Element} element
*/
2016-07-07 08:35:13 -07:00
renderToolbar = () => {
2016-06-28 18:26:56 -07:00
return (
<div className="menu toolbar-menu">
2016-07-07 08:35:13 -07:00
<span className="button" onMouseDown={this.onClickImage}>
2016-06-28 18:26:56 -07:00
<span className="material-icons">image</span>
</span>
</div>
)
}
/**
* Render the editor.
*
* @return {Element} element
*/
2016-07-07 08:35:13 -07:00
renderEditor = () => {
2016-06-28 18:26:56 -07:00
return (
<div className="editor">
<Editor
state={this.state.state}
2016-07-07 08:35:13 -07:00
renderNode={this.renderNode}
onChange={this.onChange}
2016-06-28 18:26:56 -07:00
/>
</div>
)
}
2016-07-07 08:35:13 -07:00
/**
* Render a `node`.
*
* @param {Node} node
* @return {Element}
*/
renderNode = (node) => {
return NODES[node.type]
}
/**
* On change.
*
* @param {State} state
*/
onChange = (state) => {
console.groupCollapsed('Change!')
console.log('Document:', state.document.toJS())
console.log('Selection:', state.selection.toJS())
console.log('Content:', Raw.serialize(state))
console.groupEnd()
this.setState({ state })
}
/**
* On clicking the image button, prompt for an image and insert it.
*
* @param {Event} e
*/
onClickImage = (e) => {
e.preventDefault()
const src = window.prompt('Enter the URL of the image:')
if (!src) return
this.insertImage(src)
}
/**
* Insert an image with `src` at the current selection.
*
* @param {String} src
*/
insertImage = (src) => {
let { state } = this.state
if (state.isExpanded) {
state = state
.transform()
.delete()
.apply()
}
const { anchorBlock, selection } = state
let transform = state.transform()
if (anchorBlock.text != '') {
if (selection.isAtEndOf(anchorBlock)) {
transform = transform.splitBlock()
} else if (selection.isAtStartOf(anchorBlock)) {
transform = transform.splitBlock().moveToStartOfPreviousBlock()
} else {
transform = transform.splitBlock().splitBlock().moveToStartOfPreviousBlock()
}
}
state = transform
.setBlock({
type: 'image',
isVoid: true,
data: { src }
})
.apply()
this.setState({ state })
}
2016-06-28 18:26:56 -07:00
}
/**
* Export.
*/
export default Images