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

289 lines
6.2 KiB
JavaScript
Raw Normal View History

2016-06-20 12:57:31 -07:00
import Editor, { Mark, Raw } from '../..'
import React from 'react'
import ReactDOM from 'react-dom'
/**
* State.
*/
const state = {
nodes: [
{
type: 'paragraph',
nodes: [
{
type: 'text',
ranges: [
{
2016-06-19 00:41:36 -07:00
text: 'This is editable '
},
{
2016-06-19 00:41:36 -07:00
text: 'rich',
marks: [
{
2016-06-19 00:41:36 -07:00
type: 'bold'
}
]
},
{
2016-06-19 00:41:36 -07:00
text: ' text, '
},
{
2016-06-19 00:41:36 -07:00
text: 'much',
marks: [
{
2016-06-19 00:41:36 -07:00
type: 'italic'
}
]
},
{
2016-06-19 00:41:36 -07:00
text: ' better than a '
},
{
text: '<textarea>',
marks: [
{
type: 'code'
}
]
},
{
text: '!'
}
]
}
]
},
{
type: 'paragraph',
nodes: [
{
type: 'text',
ranges: [
{
text: 'Since it\'s rich text, you can do things like turn a selection of text ',
},
{
text: 'bold',
marks: [
{
type: 'bold'
}
]
},{
text: ', or add a semanticlly rendered block quote in the middle of the page, like this:'
}
]
}
]
},
{
type: 'block-quote',
nodes: [
{
type: 'text',
ranges: [
{
text: 'A wise quote.'
}
]
}
]
},
{
type: 'paragraph',
nodes: [
{
type: 'text',
ranges: [
{
text: 'Try it out for yourself!'
}
]
}
]
}
]
}
/**
* App.
*/
class App 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-20 17:38:56 -07:00
const { currentMarks } = state
return currentMarks.some(mark => mark.type == type)
}
hasBlock(type) {
const { state } = this.state
const { currentWrappingNodes } = state
return currentWrappingNodes.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-20 17:38:56 -07:00
.setType(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() {
2016-06-20 17:38:56 -07:00
const isBold = this.hasMark('bold')
const isCode = this.hasMark('code')
const isItalic = this.hasMark('italic')
const isUnderlined = this.hasMark('underlined')
2016-06-19 00:41:36 -07:00
return (
<div className="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'
}
}
}
}
}
/**
* Attach.
*/
const app = <App />
const root = document.body.querySelector('main')
ReactDOM.render(app, root)