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>,
|
2017-04-02 14:57:36 -07:00
|
|
|
},
|
|
|
|
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',
|
2017-04-02 14:57:36 -07:00
|
|
|
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',
|
2017-04-02 14:57:36 -07:00
|
|
|
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')
|
2017-04-02 14:57:36 -07:00
|
|
|
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 })
|
|
|
|
}
|
|
|
|
|
2017-04-02 14:57:36 -07:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
2017-08-02 18:36:33 +02:00
|
|
|
render() {
|
2016-11-03 10:39:38 +01:00
|
|
|
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}
|
2017-04-02 14:57:36 -07:00
|
|
|
onKeyDown={this.onKeyDown}
|
2016-11-03 10:39:38 +01:00
|
|
|
/>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Export.
|
|
|
|
*/
|
|
|
|
|
|
|
|
export default LargeDocument
|