2016-06-18 23:54:08 -07:00
|
|
|
|
2016-07-11 18:36:45 -07:00
|
|
|
import { Editor, Mark, Placeholder, Raw, Utils } from '../..'
|
2016-06-18 23:54:08 -07:00
|
|
|
import React from 'react'
|
2016-07-07 08:35:13 -07:00
|
|
|
import initialState from './state.json'
|
2016-07-07 19:37:34 -07:00
|
|
|
import keycode from 'keycode'
|
2016-06-18 23:54:08 -07:00
|
|
|
|
2016-07-06 14:42:59 -07:00
|
|
|
/**
|
|
|
|
* Node renderers.
|
|
|
|
*
|
|
|
|
* @type {Object}
|
|
|
|
*/
|
|
|
|
|
|
|
|
const NODES = {
|
2016-07-11 18:36:45 -07:00
|
|
|
'block-quote': props => <blockquote {...props.attributes}>{props.children}</blockquote>,
|
|
|
|
'bulleted-list': props => <ul {...props.attributes}>{props.chidlren}</ul>,
|
|
|
|
'heading-one': props => <h1 {...props.attributes}>{props.children}</h1>,
|
|
|
|
'heading-two': props => <h2 {...props.attributes}>{props.children}</h2>,
|
|
|
|
'list-item': props => <li {...props.attributes}>{props.chidlren}</li>,
|
|
|
|
'numbered-list': props => <ol {...props.attributes}>{props.children}</ol>
|
2016-07-06 14:42:59 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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-18 23:54:08 -07:00
|
|
|
/**
|
2016-06-24 10:22:48 -07:00
|
|
|
* The rich text example.
|
|
|
|
*
|
2016-07-06 14:42:59 -07:00
|
|
|
* @type {Component}
|
2016-06-18 23:54:08 -07:00
|
|
|
*/
|
|
|
|
|
2016-06-24 10:22:48 -07:00
|
|
|
class RichText extends React.Component {
|
2016-06-18 23:54:08 -07:00
|
|
|
|
|
|
|
state = {
|
2016-07-07 08:35:13 -07:00
|
|
|
state: Raw.deserialize(initialState)
|
2016-06-18 23:54:08 -07:00
|
|
|
};
|
|
|
|
|
2016-07-07 08:35:13 -07:00
|
|
|
render = () => {
|
2016-06-18 23:54:08 -07:00
|
|
|
return (
|
2016-06-19 00:41:36 -07:00
|
|
|
<div>
|
|
|
|
{this.renderToolbar()}
|
|
|
|
{this.renderEditor()}
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2016-07-07 08:35:13 -07:00
|
|
|
renderToolbar = () => {
|
2016-06-19 00:41:36 -07:00
|
|
|
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-07-07 08:35:13 -07:00
|
|
|
renderMarkButton = (type, icon) => {
|
2016-06-20 17:38:56 -07:00
|
|
|
const isActive = this.hasMark(type)
|
2016-07-07 08:35:13 -07:00
|
|
|
const onMouseDown = e => this.onClickMark(e, type)
|
|
|
|
|
2016-06-20 17:38:56 -07:00
|
|
|
return (
|
2016-07-07 08:35:13 -07:00
|
|
|
<span className="button" onMouseDown={onMouseDown} data-active={isActive}>
|
2016-06-20 17:38:56 -07:00
|
|
|
<span className="material-icons">{icon}</span>
|
|
|
|
</span>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2016-07-07 08:35:13 -07:00
|
|
|
renderBlockButton = (type, icon) => {
|
2016-06-20 17:38:56 -07:00
|
|
|
const isActive = this.hasBlock(type)
|
2016-07-07 08:35:13 -07:00
|
|
|
const onMouseDown = e => this.onClickBlock(e, type)
|
|
|
|
|
2016-06-20 17:38:56 -07:00
|
|
|
return (
|
2016-07-07 08:35:13 -07:00
|
|
|
<span className="button" onMouseDown={onMouseDown} data-active={isActive}>
|
2016-06-20 17:38:56 -07:00
|
|
|
<span className="material-icons">{icon}</span>
|
|
|
|
</span>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2016-07-07 08:35:13 -07:00
|
|
|
renderEditor = () => {
|
2016-06-19 00:41:36 -07:00
|
|
|
return (
|
|
|
|
<div className="editor">
|
|
|
|
<Editor
|
2016-07-11 18:36:45 -07:00
|
|
|
placeholder={'Enter some rich text...'}
|
2016-06-19 00:41:36 -07:00
|
|
|
state={this.state.state}
|
2016-07-07 08:35:13 -07:00
|
|
|
renderNode={this.renderNode}
|
|
|
|
renderMark={this.renderMark}
|
|
|
|
onChange={this.onChange}
|
2016-07-07 19:37:34 -07:00
|
|
|
onKeyDown={this.onKeyDown}
|
2016-06-19 00:41:36 -07:00
|
|
|
/>
|
|
|
|
</div>
|
2016-06-18 23:54:08 -07:00
|
|
|
)
|
|
|
|
}
|
2016-07-07 08:35:13 -07:00
|
|
|
|
|
|
|
renderNode = (node) => {
|
|
|
|
return NODES[node.type]
|
|
|
|
}
|
|
|
|
|
|
|
|
renderMark = (mark) => {
|
|
|
|
return MARKS[mark.type]
|
|
|
|
}
|
|
|
|
|
2016-07-07 19:37:34 -07:00
|
|
|
hasMark = (type) => {
|
|
|
|
const { state } = this.state
|
|
|
|
return state.marks.some(mark => mark.type == type)
|
|
|
|
}
|
|
|
|
|
|
|
|
hasBlock = (type) => {
|
|
|
|
const { state } = this.state
|
|
|
|
return state.blocks.some(node => node.type == type)
|
|
|
|
}
|
|
|
|
|
2016-07-07 08:35:13 -07:00
|
|
|
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 })
|
|
|
|
}
|
|
|
|
|
2016-07-07 19:37:34 -07:00
|
|
|
onKeyDown = (e, state) => {
|
|
|
|
if (!Utils.Key.isCommand(e)) return
|
|
|
|
const key = keycode(e.which)
|
|
|
|
let mark
|
|
|
|
|
|
|
|
switch (key) {
|
|
|
|
case 'b':
|
|
|
|
mark = 'bold'
|
|
|
|
break
|
|
|
|
case 'i':
|
|
|
|
mark = 'italic'
|
|
|
|
break
|
|
|
|
case 'u':
|
|
|
|
mark = 'underlined'
|
|
|
|
break
|
|
|
|
case '`':
|
|
|
|
mark = 'code'
|
|
|
|
break
|
|
|
|
default:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
state = state
|
|
|
|
.transform()
|
|
|
|
[this.hasMark(mark) ? 'unmark' : 'mark'](mark)
|
|
|
|
.apply()
|
|
|
|
|
|
|
|
e.preventDefault()
|
|
|
|
return state
|
|
|
|
}
|
|
|
|
|
|
|
|
onClickMark = (e, type) => {
|
|
|
|
e.preventDefault()
|
|
|
|
const isActive = this.hasMark(type)
|
|
|
|
let { state } = this.state
|
|
|
|
|
|
|
|
state = state
|
|
|
|
.transform()
|
|
|
|
[isActive ? 'unmark' : 'mark'](type)
|
|
|
|
.apply()
|
|
|
|
|
|
|
|
this.setState({ state })
|
|
|
|
}
|
|
|
|
|
|
|
|
onClickBlock = (e, type) => {
|
|
|
|
e.preventDefault()
|
|
|
|
const isActive = this.hasBlock(type)
|
|
|
|
let { state } = this.state
|
|
|
|
|
|
|
|
state = state
|
|
|
|
.transform()
|
|
|
|
.setBlock(isActive ? 'paragraph' : type)
|
|
|
|
.apply()
|
|
|
|
|
|
|
|
this.setState({ state })
|
|
|
|
}
|
|
|
|
|
2016-06-18 23:54:08 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-06-24 10:22:48 -07:00
|
|
|
* Export.
|
2016-06-18 23:54:08 -07:00
|
|
|
*/
|
|
|
|
|
2016-06-24 10:22:48 -07:00
|
|
|
export default RichText
|