mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-02-01 13:18:29 +01:00
f69d2c4a12
* update examples and walkthroughs * deprecate data keyboard properties * update examples * add is-hotkey to resources doc * udpate docs * update docs * fix split-block test
125 lines
2.1 KiB
JavaScript
125 lines
2.1 KiB
JavaScript
/* eslint-disable no-console */
|
|
|
|
import { Editor } from 'slate-react'
|
|
import { State } from 'slate'
|
|
|
|
import React from 'react'
|
|
import faker from 'faker'
|
|
|
|
/**
|
|
* Create a huge JSON document.
|
|
*
|
|
* @type {Object}
|
|
*/
|
|
|
|
const HEADINGS = 100
|
|
const PARAGRAPHS = 8 // Paragraphs per heading
|
|
const nodes = []
|
|
const json = {
|
|
document: { nodes }
|
|
}
|
|
|
|
for (let h = 0; h < HEADINGS; h++) {
|
|
nodes.push({
|
|
kind: 'block',
|
|
type: 'heading',
|
|
nodes: [{ kind: 'text', ranges: [{ text: faker.lorem.sentence() }] }]
|
|
})
|
|
|
|
for (let p = 0; p < PARAGRAPHS; p++) {
|
|
nodes.push({
|
|
kind: 'block',
|
|
type: 'paragraph',
|
|
nodes: [{ kind: 'text', ranges: [{ text: faker.lorem.paragraph() }] }]
|
|
})
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 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'
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* The huge document example.
|
|
*
|
|
* @type {Component}
|
|
*/
|
|
|
|
class HugeDocument extends React.Component {
|
|
|
|
/**
|
|
* Deserialize the initial editor state.
|
|
*
|
|
* @type {Object}
|
|
*/
|
|
|
|
constructor() {
|
|
super()
|
|
console.time('deserializeHugeDocument')
|
|
this.state = { state: State.fromJSON(json, { normalize: false }) }
|
|
console.timeEnd('deserializeHugeDocument')
|
|
}
|
|
|
|
/**
|
|
* On change.
|
|
*
|
|
* @param {Change} change
|
|
*/
|
|
|
|
onChange = ({ state }) => {
|
|
this.setState({ state })
|
|
}
|
|
|
|
/**
|
|
* Render the editor.
|
|
*
|
|
* @return {Component} component
|
|
*/
|
|
|
|
render() {
|
|
return (
|
|
<Editor
|
|
placeholder={'Enter some text...'}
|
|
schema={schema}
|
|
spellCheck={false}
|
|
state={this.state.state}
|
|
onChange={this.onChange}
|
|
onKeyDown={this.onKeyDown}
|
|
/>
|
|
)
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
* Export.
|
|
*/
|
|
|
|
export default HugeDocument
|