1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-03-06 13:59:47 +01:00

154 lines
2.6 KiB
JavaScript
Raw Normal View History

2016-11-03 10:39:38 +01:00
/* eslint-disable no-console */
2017-05-04 17:14:09 -07:00
import { Editor, Raw } from '../../..'
2016-11-03 10:39:38 +01:00
import React from 'react'
import faker from 'faker'
const HEADINGS = 100
const PARAGRAPHS = 8 // Paragraphs per heading
/**
* Define a schema.
*
* @type {Object}
*/
const schema = {
nodes: {
'heading': props => <h1 {...props.attributes}>{props.children}</h1>,
'paragraph': props => <p {...props.attributes} style={{ marginBottom: 20 }}>{props.children}</p>,
},
marks: {
bold: {
fontWeight: 'bold'
},
code: {
fontFamily: 'monospace',
backgroundColor: '#eee',
padding: '3px',
borderRadius: '4px'
},
italic: {
fontStyle: 'italic'
},
underlined: {
textDecoration: 'underline'
}
2016-11-03 10:39:38 +01:00
}
}
const nodes = []
for (let h = 0; h < HEADINGS; h++) {
nodes.push({
kind: 'block',
type: 'heading',
nodes: [{ kind: 'text', text: faker.lorem.sentence() }]
2016-11-03 10:39:38 +01:00
})
for (let p = 0; p < PARAGRAPHS; p++) {
nodes.push({
kind: 'block',
type: 'paragraph',
nodes: [{ kind: 'text', text: faker.lorem.paragraph() }]
2016-11-03 10:39:38 +01:00
})
}
}
/**
* The large text example.
*
* @type {Component}
*/
class LargeDocument extends React.Component {
/**
* Deserialize the initial editor state.
*
* @type {Object}
*/
constructor() {
super()
2016-11-23 09:05:15 -08:00
console.time('deserializeLargeDocument')
this.state = { state: Raw.deserialize({ nodes }, { normalize: false, terse: true }) }
2016-11-23 09:05:15 -08:00
console.timeEnd('deserializeLargeDocument')
2016-11-03 10:39:38 +01:00
}
/**
* On change.
*
* @param {State} state
*/
onChange = (state) => {
this.setState({ state })
}
/**
* On key down, if it's a formatting command toggle a mark.
*
* @param {Event} e
* @param {Object} data
* @param {State} state
* @return {State}
*/
onKeyDown = (e, data, state) => {
if (!data.isMod) return
let mark
switch (data.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()
.toggleMark(mark)
.apply()
e.preventDefault()
return state
}
2016-11-03 10:39:38 +01:00
/**
* Render the editor.
*
* @return {Component} component
*/
render = () => {
return (
<Editor
placeholder={'Enter some plain text...'}
schema={schema}
2017-02-13 16:30:48 -08:00
spellCheck={false}
2016-11-03 10:39:38 +01:00
state={this.state.state}
onChange={this.onChange}
onKeyDown={this.onKeyDown}
2016-11-03 10:39:38 +01:00
/>
)
}
}
/**
* Export.
*/
export default LargeDocument