1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-08-19 05:31:56 +02:00

move plaintext serializer out of core

This commit is contained in:
Ian Storm Taylor
2016-06-21 10:49:11 -07:00
parent 7fa78870c3
commit d742d109a5
3 changed files with 49 additions and 62 deletions

View File

@@ -1,23 +1,66 @@
import Editor from '../..'
import Editor, { Character, Document, Element, State, Text } from '../..'
import React from 'react'
import ReactDOM from 'react-dom'
import { Plaintext } from '../..'
/**
* State.
* The initial editor state.
*
* @type {String}
*/
const state = 'This is editable plain text, just like a <textarea>!'
/**
* App.
* A helper to deserialize a string into an editor state.
*
* @param {String} string
* @return {State} state
*/
function deserialize(string) {
const characters = string
.split('')
.reduce((list, char) => {
return list.push(Character.create({ text: char }))
}, Character.createList())
const text = Text.create({ characters })
const texts = Element.createMap([text])
const node = Element.create({
type: 'paragraph',
nodes: texts,
})
const nodes = Element.createMap([node])
const document = Document.create({ nodes })
const state = State.create({ document })
return state
}
/**
* A helper to serialize an editor state into a string.
*
* @param {State} state
* @return {String} string
*/
function serialize(state) {
return state.document.nodes
.map(node => node.text)
.join('\n')
}
/**
* The example's app.
*
* @type {Component} App
*/
class App extends React.Component {
state = {
state: Plaintext.deserialize(state)
state: deserialize(state)
};
render() {
@@ -28,7 +71,7 @@ class App extends React.Component {
console.groupCollapsed('Change!')
console.log('Document:', state.document.toJS())
console.log('Selection:', state.selection.toJS())
console.log('Content:', Plaintext.serialize(state))
console.log('Content:', serialize(state))
console.groupEnd()
this.setState({ state })
}}