1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-07-31 04:20:26 +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 })
}}

View File

@@ -23,4 +23,3 @@ export { default as Text } from './models/text'
*/
export { default as Raw } from './serializers/raw'
export { default as Plaintext } from './serializers/plaintext'

View File

@@ -1,55 +0,0 @@
import Character from '../models/character'
import Element from '../models/element'
import Text from '../models/text'
import Document from '../models/document'
import State from '../models/state'
/**
* Serialize a `state` into a plaintext `string`.
*
* @param {State} state
* @return {String} string
*/
function serialize(state) {
return state.document.nodes
.map(node => node.text)
.join('\n')
}
/**
* Deserialize a plaintext `string` into a `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
}
/**
* Export.
*/
export default {
serialize,
deserialize
}