import { Editor, Mark, Raw } from '../..' import React from 'react' import state from './state.json' /** * The rich text example. * * @type {Component} RichText */ class RichText extends React.Component { state = { state: Raw.deserialize(state) }; hasMark(type) { const { state } = this.state const { marks } = state return marks.some(mark => mark.type == type) } hasBlock(type) { const { state } = this.state const { blocks } = state return blocks.some(node => node.type == type) } 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 }) } render() { return (
{this.renderToolbar()} {this.renderEditor()}
) } renderToolbar() { return (
{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')}
) } renderMarkButton(type, icon) { const isActive = this.hasMark(type) return ( this.onClickMark(e, type)} data-active={isActive}> {icon} ) } renderBlockButton(type, icon) { const isActive = this.hasBlock(type) return ( this.onClickBlock(e, type)} data-active={isActive}> {icon} ) } renderEditor() { return (
this.renderNode(node)} renderMark={mark => this.renderMark(mark)} 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 }) }} />
) } renderNode(node) { switch (node.type) { case 'block-quote': { return (props) =>
{props.children}
} case 'bulleted-list': { return (props) => } case 'heading-one': { return (props) =>

{props.children}

} case 'heading-two': { return (props) =>

{props.children}

} case 'list-item': { return (props) =>
  • {props.chidlren}
  • } case 'numbered-list': { return (props) =>
      {props.children}
    } case 'paragraph': { return (props) =>

    {props.children}

    } } } renderMark(mark) { switch (mark.type) { case 'bold': { return { fontWeight: 'bold' } } case 'code': { return { fontFamily: 'monospace', backgroundColor: '#eee', padding: '3px', borderRadius: '4px' } } case 'italic': { return { fontStyle: 'italic' } } case 'underlined': { return { textDecoration: 'underline' } } } } } /** * Export. */ export default RichText