1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-02-24 09:13:24 +01:00

165 lines
3.7 KiB
JavaScript
Raw Normal View History

2016-07-06 14:21:11 -07:00
import { Editor, Mark, Raw } from '../..'
import React from 'react'
import state from './state.json'
/**
* Node renderers.
*
* @type {Object}
*/
const NODES = {
'block-quote': props => <blockquote>{props.children}</blockquote>,
'bulleted-list': props => <ul>{props.chidlren}</ul>,
'heading-one': props => <h1>{props.children}</h1>,
'heading-two': props => <h2>{props.children}</h2>,
'list-item': props => <li>{props.chidlren}</li>,
'numbered-list': props => <ol>{props.children}</ol>,
'paragraph': props => <p>{props.children}</p>
}
/**
* Mark renderers.
*
* @type {Object}
*/
const MARKS = {
bold: {
fontWeight: 'bold'
},
code: {
fontFamily: 'monospace',
backgroundColor: '#eee',
padding: '3px',
borderRadius: '4px'
},
italic: {
fontStyle: 'italic'
},
underlined: {
textDecoration: 'underline'
}
}
/**
2016-06-24 10:22:48 -07:00
* The rich text example.
*
* @type {Component}
*/
2016-06-24 10:22:48 -07:00
class RichText extends React.Component {
state = {
state: Raw.deserialize(state)
};
2016-06-20 17:38:56 -07:00
hasMark(type) {
2016-06-19 00:41:36 -07:00
const { state } = this.state
2016-06-23 23:59:22 -07:00
const { marks } = state
return marks.some(mark => mark.type == type)
2016-06-20 17:38:56 -07:00
}
hasBlock(type) {
const { state } = this.state
2016-06-23 23:59:22 -07:00
const { blocks } = state
return blocks.some(node => node.type == type)
2016-06-19 00:41:36 -07:00
}
onClickMark(e, type) {
e.preventDefault()
2016-06-20 17:38:56 -07:00
const isActive = this.hasMark(type)
let { state } = this.state
state = state
.transform()
[isActive ? 'unmark' : 'mark'](type)
.apply()
this.setState({ state })
}
2016-06-19 00:41:36 -07:00
2016-06-20 17:38:56 -07:00
onClickBlock(e, type) {
e.preventDefault()
const isActive = this.hasBlock(type)
2016-06-19 00:41:36 -07:00
let { state } = this.state
state = state
.transform()
2016-06-23 23:59:22 -07:00
.setBlock(isActive ? 'paragraph' : type)
2016-06-19 00:41:36 -07:00
.apply()
2016-06-20 12:57:31 -07:00
this.setState({ state })
2016-06-19 00:41:36 -07:00
}
render() {
return (
2016-06-19 00:41:36 -07:00
<div>
{this.renderToolbar()}
{this.renderEditor()}
</div>
)
}
renderToolbar() {
return (
2016-06-28 15:47:29 -07:00
<div className="menu toolbar-menu">
2016-06-20 17:38:56 -07:00
{this.renderMarkButton('bold', 'format_bold')}
{this.renderMarkButton('italic', 'format_italic')}
{this.renderMarkButton('underlined', 'format_underlined')}
{this.renderMarkButton('code', 'code')}
{this.renderBlockButton('heading-one', 'looks_one')}
{this.renderBlockButton('heading-two', 'looks_two')}
{this.renderBlockButton('block-quote', 'format_quote')}
{this.renderBlockButton('numbered-list', 'format_list_numbered')}
{this.renderBlockButton('bulleted-list', 'format_list_bulleted')}
2016-06-19 00:41:36 -07:00
</div>
)
}
2016-06-20 17:38:56 -07:00
renderMarkButton(type, icon) {
const isActive = this.hasMark(type)
return (
2016-06-21 10:43:04 -07:00
<span className="button" onMouseDown={e => this.onClickMark(e, type)} data-active={isActive}>
2016-06-20 17:38:56 -07:00
<span className="material-icons">{icon}</span>
</span>
)
}
renderBlockButton(type, icon) {
const isActive = this.hasBlock(type)
return (
2016-06-21 10:43:04 -07:00
<span className="button" onMouseDown={e => this.onClickBlock(e, type)} data-active={isActive}>
2016-06-20 17:38:56 -07:00
<span className="material-icons">{icon}</span>
</span>
)
}
2016-06-19 00:41:36 -07:00
renderEditor() {
return (
<div className="editor">
<Editor
state={this.state.state}
renderNode={node => NODES[node.type]}
renderMark={mark => MARKS[mark.type]}
2016-06-19 00:41:36 -07:00
onChange={(state) => {
2016-06-20 13:21:24 -07:00
console.groupCollapsed('Change!')
2016-06-19 00:41:36 -07:00
console.log('Document:', state.document.toJS())
2016-06-20 13:21:24 -07:00
console.log('Selection:', state.selection.toJS())
2016-06-19 00:41:36 -07:00
console.log('Content:', Raw.serialize(state))
2016-06-20 13:21:24 -07:00
console.groupEnd()
2016-06-19 00:41:36 -07:00
this.setState({ state })
}}
/>
</div>
)
}
}
/**
2016-06-24 10:22:48 -07:00
* Export.
*/
2016-06-24 10:22:48 -07:00
export default RichText