1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-08-29 18:09:49 +02:00

Separate out EditorValue component for examples (#2785)

* Separate out EditorValue component for examples

* Made it prettier
This commit is contained in:
Sunny Hirai
2019-05-15 14:50:11 -07:00
committed by GitHub
parent d21cb09a12
commit e4fae23454
2 changed files with 49 additions and 47 deletions

View File

@@ -19,6 +19,52 @@ export const Button = React.forwardRef(
) )
) )
export const EditorValue = React.forwardRef(
({ className, value, ...props }, ref) => {
const textLines = value.document.nodes
.map(node => node.text)
.toArray()
.join('\n')
return (
<div
ref={ref}
{...props}
className={cx(
className,
css`
margin: 30px -20px 0;
`
)}
>
<div
className={css`
font-size: 14px;
padding: 5px 20px;
color: #404040;
border-top: 2px solid #eeeeee;
background: #f8f8f8;
`}
>
Slate's value as text
</div>
<div
className={css`
color: #404040;
font: 12px monospace;
white-space: pre-wrap;
padding: 10px 20px;
div {
margin: 0 0 0.5em;
}
`}
>
{textLines}
</div>
</div>
)
}
)
export const Icon = React.forwardRef(({ className, ...props }, ref) => ( export const Icon = React.forwardRef(({ className, ...props }, ref) => (
<span <span
{...props} {...props}

View File

@@ -8,7 +8,7 @@ import splitJoin from './split-join.js'
import insert from './insert.js' import insert from './insert.js'
import special from './special.js' import special from './special.js'
import { isKeyHotkey } from 'is-hotkey' import { isKeyHotkey } from 'is-hotkey'
import { Button, Icon, Toolbar } from '../components' import { Button, EditorValue, Icon, Toolbar } from '../components'
import { ANDROID_API_VERSION } from 'slate-dev-environment' import { ANDROID_API_VERSION } from 'slate-dev-environment'
/** /**
@@ -73,45 +73,6 @@ const Version = props => (
/> />
) )
const EditorText = props => (
<div
{...props}
className={css`
color: #808080;
background: #f0f0f0;
font: 12px monospace;
white-space: pre-wrap;
margin: 1em -1em;
padding: 0.5em;
div {
margin: 0 0 0.5em;
}
`}
/>
)
const EditorTextCaption = props => (
<div
{...props}
className={css`
color: white;
background: #808080;
padding: 0.5em;
`}
/>
)
/**
* Extract lines of text from `Value`
*
* @return {String[]}
*/
function getTextLines(value) {
return value.document.nodes.map(node => node.text).toArray()
}
/** /**
* Subpages which are each a smoke test. * Subpages which are each a smoke test.
* *
@@ -210,7 +171,7 @@ class RichTextExample extends React.Component {
render() { render() {
const { text } = this.state const { text } = this.state
if (text == null) return <Redirect to="/composition/split-join" /> if (text == null) return <Redirect to="/composition/split-join" />
const textLines = getTextLines(this.state.value) // const textLines = getTextLines(this.state.value)
return ( return (
<div> <div>
<Instruction> <Instruction>
@@ -257,12 +218,7 @@ class RichTextExample extends React.Component {
renderBlock={this.renderBlock} renderBlock={this.renderBlock}
renderMark={this.renderMark} renderMark={this.renderMark}
/> />
<EditorText> <EditorValue value={this.state.value} />
<EditorTextCaption>Text in Slate's `Value`</EditorTextCaption>
{textLines.map((line, index) => (
<div key={index}>{line.length > 0 ? line : ' '}</div>
))}
</EditorText>
</div> </div>
) )
} }