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

335 lines
6.9 KiB
JavaScript
Raw Normal View History

2016-07-13 15:12:47 -07:00
import { Editor, Mark, Raw, Utils } from '../..'
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-07-15 16:08:13 -07:00
/**
* Define the default node type.
*/
const DEFAULT_NODE = 'paragraph'
/**
2016-07-13 16:19:49 -07:00
* Define a set of node renderers.
*
* @type {Object}
*/
const NODES = {
'block-quote': (props) => <blockquote {...props.attributes}>{props.children}</blockquote>,
2016-07-16 01:02:03 +02:00
'bulleted-list': props => <ul {...props.attributes}>{props.children}</ul>,
2016-07-11 18:36:45 -07:00
'heading-one': props => <h1 {...props.attributes}>{props.children}</h1>,
'heading-two': props => <h2 {...props.attributes}>{props.children}</h2>,
2016-07-16 01:02:03 +02:00
'list-item': props => <li {...props.attributes}>{props.children}</li>,
2016-07-11 18:36:45 -07:00
'numbered-list': props => <ol {...props.attributes}>{props.children}</ol>
}
/**
2016-07-13 15:12:47 -07:00
* Define a set of 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 {
2016-07-13 15:12:47 -07:00
/**
* Deserialize the initial editor state.
*
* @type {Object}
*/
state = {
2016-07-07 08:35:13 -07:00
state: Raw.deserialize(initialState)
};
2016-07-13 15:12:47 -07:00
/**
* Check if the current selection has a mark with `type` in it.
*
* @param {String} type
* @return {Boolean}
*/
2016-07-07 08:35:13 -07:00
2016-07-07 19:37:34 -07:00
hasMark = (type) => {
const { state } = this.state
return state.marks.some(mark => mark.type == type)
}
2016-07-13 15:12:47 -07:00
/**
* Check if the any of the currently selected blocks are of `type`.
*
* @param {String} type
* @return {Boolean}
*/
2016-07-07 19:37:34 -07:00
hasBlock = (type) => {
const { state } = this.state
return state.blocks.some(node => node.type == type)
}
2016-07-13 15:12:47 -07:00
/**
* On change, save the new state.
*
* @param {State} state
*/
2016-07-07 08:35:13 -07:00
onChange = (state) => {
this.setState({ state })
console.log(state.document.toJS())
2016-07-07 08:35:13 -07:00
}
2016-07-13 15:12:47 -07:00
/**
* On key down, if it's a formatting command toggle a mark.
*
* @param {Event} e
* @param {State} state
* @return {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) ? 'removeMark' : 'addMark'](mark)
2016-07-07 19:37:34 -07:00
.apply()
e.preventDefault()
return state
}
2016-07-13 15:12:47 -07:00
/**
* When a mark button is clicked, toggle the current mark.
*
* @param {Event} e
* @param {String} type
*/
2016-07-07 19:37:34 -07:00
onClickMark = (e, type) => {
e.preventDefault()
const isActive = this.hasMark(type)
let { state } = this.state
state = state
.transform()
[isActive ? 'removeMark' : 'addMark'](type)
2016-07-07 19:37:34 -07:00
.apply()
this.setState({ state })
}
2016-07-13 15:12:47 -07:00
/**
* When a block button is clicked, toggle the block type.
*
* @param {Event} e
* @param {String} type
*/
2016-07-07 19:37:34 -07:00
onClickBlock = (e, type) => {
e.preventDefault()
let { state } = this.state
let transform = state.transform()
const { document } = state
2016-07-07 19:37:34 -07:00
// Handle everything but list buttons.
if (type != 'bulleted-list' && type != 'numbered-list') {
const isActive = this.hasBlock(type)
transform = transform.setBlock(isActive ? DEFAULT_NODE : type)
}
2016-07-07 19:37:34 -07:00
2016-07-15 16:08:13 -07:00
// Handle the extra wrapping required for list buttons.
else {
const isList = this.hasBlock('list-item')
const isType = state.blocks.some((block) => {
return !!document.getClosest(block, parent => parent.type == type)
})
if (isList && isType) {
2016-07-15 16:08:13 -07:00
transform = transform
.setBlock(DEFAULT_NODE)
.unwrapBlock('bulleted-list')
.unwrapBlock('numbered-list')
} else if (isList) {
transform = transform
.unwrapBlock(type == 'bulleted-list' ? 'numbered-list' : 'bulleted-list')
.wrapBlock(type)
2016-07-15 16:08:13 -07:00
} else {
transform = transform
.setBlock('list-item')
.wrapBlock(type)
}
}
state = transform.apply()
2016-07-07 19:37:34 -07:00
this.setState({ state })
}
2016-07-13 15:12:47 -07:00
/**
* Render.
*
* @return {Element}
*/
render = () => {
return (
<div>
{this.renderToolbar()}
{this.renderEditor()}
</div>
)
}
/**
* Render the toolbar.
*
* @return {Element}
*/
renderToolbar = () => {
return (
<div className="menu toolbar-menu">
{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')}
</div>
)
}
/**
* Render a mark-toggling toolbar button.
*
* @param {String} type
* @param {String} icon
* @return {Element}
*/
renderMarkButton = (type, icon) => {
const isActive = this.hasMark(type)
const onMouseDown = e => this.onClickMark(e, type)
return (
<span className="button" onMouseDown={onMouseDown} data-active={isActive}>
<span className="material-icons">{icon}</span>
</span>
)
}
/**
* Render a block-toggling toolbar button.
*
* @param {String} type
* @param {String} icon
* @return {Element}
*/
renderBlockButton = (type, icon) => {
const isActive = this.hasBlock(type)
const onMouseDown = e => this.onClickBlock(e, type)
return (
<span className="button" onMouseDown={onMouseDown} data-active={isActive}>
<span className="material-icons">{icon}</span>
</span>
)
}
/**
* Render the Slate editor.
*
* @return {Element}
*/
renderEditor = () => {
return (
<div className="editor">
<Editor
placeholder={'Enter some rich text...'}
state={this.state.state}
renderNode={this.renderNode}
renderMark={this.renderMark}
onChange={this.onChange}
onKeyDown={this.onKeyDown}
/>
</div>
)
}
/**
* Return a node renderer for a Slate `node`.
*
* @param {Node} node
* @return {Component or Void}
*/
renderNode = (node) => {
return NODES[node.type]
}
/**
* Return a mark renderer for a Slate `mark`.
*
* @param {Mark} mark
* @return {Object or Void}
*/
renderMark = (mark) => {
return MARKS[mark.type]
}
}
/**
2016-06-24 10:22:48 -07:00
* Export.
*/
2016-06-24 10:22:48 -07:00
export default RichText