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

180 lines
4.1 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'
/**
2016-06-24 10:22:48 -07:00
* The rich text example.
*
* @type {Component} RichText
*/
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 => this.renderNode(node)}
renderMark={mark => this.renderMark(mark)}
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>
)
}
renderNode(node) {
switch (node.type) {
2016-06-20 17:38:56 -07:00
case 'block-quote': {
return (props) => <blockquote>{props.children}</blockquote>
}
case 'bulleted-list': {
return (props) => <ul>{props.chidlren}</ul>
}
case 'heading-one': {
return (props) => <h1>{props.children}</h1>
}
case 'heading-two': {
return (props) => <h2>{props.children}</h2>
}
case 'list-item': {
return (props) => <li>{props.chidlren}</li>
}
case 'numbered-list': {
return (props) => <ol>{props.children}</ol>
}
case 'paragraph': {
2016-06-20 17:38:56 -07:00
return (props) => <p>{props.children}</p>
}
}
}
renderMark(mark) {
switch (mark.type) {
case 'bold': {
return {
fontWeight: 'bold'
}
}
case 'code': {
return {
fontFamily: 'monospace',
backgroundColor: '#eee',
padding: '3px',
borderRadius: '4px'
}
}
2016-06-20 17:38:56 -07:00
case 'italic': {
return {
fontStyle: 'italic'
}
}
case 'underlined': {
return {
textDecoration: 'underline'
}
}
}
}
}
/**
2016-06-24 10:22:48 -07:00
* Export.
*/
2016-06-24 10:22:48 -07:00
export default RichText