mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-03-06 05:49:47 +01:00
* split rendering out of schema * remove default components * first stab at new schema * make default normalizations smarter * revert to forcing defaults to be verbose? * refactor reason constants * split nodes into blocks/inlines * get tests passing * restructure schema tests * add parent test * cleanup * remove defaults from schema * refactor schema rule.nodes validation, update example * embed schema in state objects * fixes * update examples, and fixes * update walkthroughs * update docs * remove old schemas doc page * add more tests * update benchmarks
129 lines
2.3 KiB
JavaScript
129 lines
2.3 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', leaves: [{ text: faker.lorem.sentence() }] }]
|
|
})
|
|
|
|
for (let p = 0; p < PARAGRAPHS; p++) {
|
|
nodes.push({
|
|
kind: 'block',
|
|
type: 'paragraph',
|
|
nodes: [{ kind: 'text', leaves: [{ text: faker.lorem.paragraph() }] }]
|
|
})
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 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 (
|
|
<div className="editor">
|
|
<Editor
|
|
placeholder="Enter some text..."
|
|
spellCheck={false}
|
|
state={this.state.state}
|
|
onChange={this.onChange}
|
|
onKeyDown={this.onKeyDown}
|
|
renderNode={this.renderNode}
|
|
renderMark={this.renderMark}
|
|
/>
|
|
</div>
|
|
)
|
|
}
|
|
|
|
/**
|
|
* Render a Slate node.
|
|
*
|
|
* @param {Object} props
|
|
* @return {Element}
|
|
*/
|
|
|
|
renderNode = (props) => {
|
|
const { attributes, children, node } = props
|
|
switch (node.type) {
|
|
case 'heading': return <h1 {...attributes}>{children}</h1>
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Render a Slate mark.
|
|
*
|
|
* @param {Object} props
|
|
* @return {Element}
|
|
*/
|
|
|
|
renderMark = (props) => {
|
|
const { children, mark } = props
|
|
switch (mark.type) {
|
|
case 'bold': return <strong>{children}</strong>
|
|
case 'code': return <code>{children}</code>
|
|
case 'italic': return <em>{children}</em>
|
|
case 'underlined': return <u>{children}</u>
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
* Export.
|
|
*/
|
|
|
|
export default HugeDocument
|