import { Editor } from 'slate-react' import { Value } from 'slate' import React from 'react' import initialValue from './value.json' import { isKeyHotkey } from 'is-hotkey' import { Button, Icon, Toolbar } from '../components' /** * Define the default node type. * * @type {String} */ const DEFAULT_NODE = 'paragraph' /** * Define hotkey matchers. * * @type {Function} */ const isBoldHotkey = isKeyHotkey('mod+b') const isItalicHotkey = isKeyHotkey('mod+i') const isUnderlinedHotkey = isKeyHotkey('mod+u') const isCodeHotkey = isKeyHotkey('mod+`') /** * The rich text example. * * @type {Component} */ class RichTextExample extends React.Component { /** * Deserialize the initial editor value. * * @type {Object} */ state = { value: Value.fromJSON(initialValue), } /** * Check if the current selection has a mark with `type` in it. * * @param {String} type * @return {Boolean} */ hasMark = type => { const { value } = this.state return value.activeMarks.some(mark => mark.type == type) } /** * Check if the any of the currently selected blocks are of `type`. * * @param {String} type * @return {Boolean} */ hasBlock = type => { const { value } = this.state return value.blocks.some(node => node.type == type) } /** * Render. * * @return {Element} */ render() { return (
{children}case 'bulleted-list': return
{children}
case 'italic':
return {children}
case 'underlined':
return {children}
}
}
/**
* On change, save the new `value`.
*
* @param {Change} change
*/
onChange = ({ value }) => {
this.setState({ value })
}
/**
* On key down, if it's a formatting command toggle a mark.
*
* @param {Event} event
* @param {Change} change
* @return {Change}
*/
onKeyDown = (event, change) => {
let mark
if (isBoldHotkey(event)) {
mark = 'bold'
} else if (isItalicHotkey(event)) {
mark = 'italic'
} else if (isUnderlinedHotkey(event)) {
mark = 'underlined'
} else if (isCodeHotkey(event)) {
mark = 'code'
} else {
return
}
event.preventDefault()
change.toggleMark(mark)
return true
}
/**
* When a mark button is clicked, toggle the current mark.
*
* @param {Event} event
* @param {String} type
*/
onClickMark = (event, type) => {
event.preventDefault()
const { value } = this.state
const change = value.change().toggleMark(type)
this.onChange(change)
}
/**
* When a block button is clicked, toggle the block type.
*
* @param {Event} event
* @param {String} type
*/
onClickBlock = (event, type) => {
event.preventDefault()
const { value } = this.state
const change = value.change()
const { document } = value
// Handle everything but list buttons.
if (type != 'bulleted-list' && type != 'numbered-list') {
const isActive = this.hasBlock(type)
const isList = this.hasBlock('list-item')
if (isList) {
change
.setBlocks(isActive ? DEFAULT_NODE : type)
.unwrapBlock('bulleted-list')
.unwrapBlock('numbered-list')
} else {
change.setBlocks(isActive ? DEFAULT_NODE : type)
}
} else {
// Handle the extra wrapping required for list buttons.
const isList = this.hasBlock('list-item')
const isType = value.blocks.some(block => {
return !!document.getClosest(block.key, parent => parent.type == type)
})
if (isList && isType) {
change
.setBlocks(DEFAULT_NODE)
.unwrapBlock('bulleted-list')
.unwrapBlock('numbered-list')
} else if (isList) {
change
.unwrapBlock(
type == 'bulleted-list' ? 'numbered-list' : 'bulleted-list'
)
.wrapBlock(type)
} else {
change.setBlocks('list-item').wrapBlock(type)
}
}
this.onChange(change)
}
}
/**
* Export.
*/
export default RichTextExample