mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-07-31 20:40:19 +02:00
Add defaultValue
prop to Editor
component (#2418)
* Add `defaultValue` prop to `Editor` component * Use `defaultValue` prop in the `Read Only` example * Use `defaultValue` prop in the `Check Lists` example * Use `defaultValue` prop in the `Code Highlighting` example * Use `defaultValue` prop in the `Embeds` example * Use `defaultValue` prop in the `Emojis` example * Use `defaultValue` prop in the `Forced Layout` example * Use `defaultValue` prop in the `Huge Document` example * Use `defaultValue` prop in the `Images` example * Use `defaultValue` prop in the `Input Tester` example * Use `defaultValue` prop in the `Markdown Preview` example * Use `defaultValue` prop in the `Markdown Shortcuts` example * Use `defaultValue` prop in the `Paste HTML` example * Use `defaultValue` prop in the `Plain Text` example * Use `defaultValue` prop in the `Plugins` example * Use `defaultValue` prop in the `RTL` example * Use `defaultValue` prop in the `Search Highlighting` example * Use `defaultValue` prop in the `Tables` example
This commit is contained in:
committed by
Ian Storm Taylor
parent
5849dcb810
commit
61be5f8881
@@ -2,9 +2,17 @@ import { Editor } from 'slate-react'
|
|||||||
import { Value } from 'slate'
|
import { Value } from 'slate'
|
||||||
|
|
||||||
import React from 'react'
|
import React from 'react'
|
||||||
import initialValue from './value.json'
|
import initialValueAsJson from './value.json'
|
||||||
import styled from 'react-emotion'
|
import styled from 'react-emotion'
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Deserialize the initial editor value.
|
||||||
|
*
|
||||||
|
* @type {Object}
|
||||||
|
*/
|
||||||
|
|
||||||
|
const initialValue = Value.fromJSON(initialValueAsJson)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a few styling components.
|
* Create a few styling components.
|
||||||
*
|
*
|
||||||
@@ -88,16 +96,6 @@ class CheckListItem extends React.Component {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
class CheckLists extends React.Component {
|
class CheckLists extends React.Component {
|
||||||
/**
|
|
||||||
* Deserialize the initial editor value.
|
|
||||||
*
|
|
||||||
* @type {Object}
|
|
||||||
*/
|
|
||||||
|
|
||||||
state = {
|
|
||||||
value: Value.fromJSON(initialValue),
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Render.
|
* Render.
|
||||||
*
|
*
|
||||||
@@ -109,8 +107,7 @@ class CheckLists extends React.Component {
|
|||||||
<Editor
|
<Editor
|
||||||
spellCheck
|
spellCheck
|
||||||
placeholder="Get to work..."
|
placeholder="Get to work..."
|
||||||
value={this.state.value}
|
defaultValue={initialValue}
|
||||||
onChange={this.onChange}
|
|
||||||
onKeyDown={this.onKeyDown}
|
onKeyDown={this.onKeyDown}
|
||||||
renderNode={this.renderNode}
|
renderNode={this.renderNode}
|
||||||
/>
|
/>
|
||||||
@@ -133,16 +130,6 @@ class CheckLists extends React.Component {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* On change, save the new value.
|
|
||||||
*
|
|
||||||
* @param {Editor} editor
|
|
||||||
*/
|
|
||||||
|
|
||||||
onChange = ({ value }) => {
|
|
||||||
this.setState({ value })
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* On key down...
|
* On key down...
|
||||||
*
|
*
|
||||||
|
@@ -3,7 +3,15 @@ import { Value } from 'slate'
|
|||||||
|
|
||||||
import Prism from 'prismjs'
|
import Prism from 'prismjs'
|
||||||
import React from 'react'
|
import React from 'react'
|
||||||
import initialValue from './value.json'
|
import initialValueAsJson from './value.json'
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Deserialize the initial editor value.
|
||||||
|
*
|
||||||
|
* @type {Object}
|
||||||
|
*/
|
||||||
|
|
||||||
|
const initialValue = Value.fromJSON(initialValueAsJson)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Define our code components.
|
* Define our code components.
|
||||||
@@ -67,16 +75,6 @@ function getContent(token) {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
class CodeHighlighting extends React.Component {
|
class CodeHighlighting extends React.Component {
|
||||||
/**
|
|
||||||
* Deserialize the raw initial value.
|
|
||||||
*
|
|
||||||
* @type {Object}
|
|
||||||
*/
|
|
||||||
|
|
||||||
state = {
|
|
||||||
value: Value.fromJSON(initialValue),
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Render.
|
* Render.
|
||||||
*
|
*
|
||||||
@@ -87,8 +85,7 @@ class CodeHighlighting extends React.Component {
|
|||||||
return (
|
return (
|
||||||
<Editor
|
<Editor
|
||||||
placeholder="Write some code..."
|
placeholder="Write some code..."
|
||||||
value={this.state.value}
|
defaultValue={initialValue}
|
||||||
onChange={this.onChange}
|
|
||||||
onKeyDown={this.onKeyDown}
|
onKeyDown={this.onKeyDown}
|
||||||
renderNode={this.renderNode}
|
renderNode={this.renderNode}
|
||||||
renderMark={this.renderMark}
|
renderMark={this.renderMark}
|
||||||
@@ -155,16 +152,6 @@ class CodeHighlighting extends React.Component {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* On change, save the new value.
|
|
||||||
*
|
|
||||||
* @param {Editor} editor
|
|
||||||
*/
|
|
||||||
|
|
||||||
onChange = ({ value }) => {
|
|
||||||
this.setState({ value })
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* On key down inside code blocks, insert soft new lines.
|
* On key down inside code blocks, insert soft new lines.
|
||||||
*
|
*
|
||||||
|
@@ -3,7 +3,15 @@ import { Value } from 'slate'
|
|||||||
|
|
||||||
import React from 'react'
|
import React from 'react'
|
||||||
import Video from './video'
|
import Video from './video'
|
||||||
import initialValue from './value.json'
|
import initialValueAsJson from './value.json'
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Deserialize the initial editor value.
|
||||||
|
*
|
||||||
|
* @type {Object}
|
||||||
|
*/
|
||||||
|
|
||||||
|
const initialValue = Value.fromJSON(initialValueAsJson)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The images example.
|
* The images example.
|
||||||
@@ -12,16 +20,6 @@ import initialValue from './value.json'
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
class Embeds extends React.Component {
|
class Embeds extends React.Component {
|
||||||
/**
|
|
||||||
* Deserialize the raw initial value.
|
|
||||||
*
|
|
||||||
* @type {Object}
|
|
||||||
*/
|
|
||||||
|
|
||||||
state = {
|
|
||||||
value: Value.fromJSON(initialValue),
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The editor's schema.
|
* The editor's schema.
|
||||||
*
|
*
|
||||||
@@ -46,9 +44,8 @@ class Embeds extends React.Component {
|
|||||||
return (
|
return (
|
||||||
<Editor
|
<Editor
|
||||||
placeholder="Enter some text..."
|
placeholder="Enter some text..."
|
||||||
value={this.state.value}
|
defaultValue={initialValue}
|
||||||
schema={this.schema}
|
schema={this.schema}
|
||||||
onChange={this.onChange}
|
|
||||||
renderNode={this.renderNode}
|
renderNode={this.renderNode}
|
||||||
/>
|
/>
|
||||||
)
|
)
|
||||||
@@ -70,16 +67,6 @@ class Embeds extends React.Component {
|
|||||||
return next()
|
return next()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* On change.
|
|
||||||
*
|
|
||||||
* @param {Editor} editor
|
|
||||||
*/
|
|
||||||
|
|
||||||
onChange = ({ value }) => {
|
|
||||||
this.setState({ value })
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@@ -2,10 +2,18 @@ import { Editor } from 'slate-react'
|
|||||||
import { Value } from 'slate'
|
import { Value } from 'slate'
|
||||||
|
|
||||||
import React from 'react'
|
import React from 'react'
|
||||||
import initialValue from './value.json'
|
import initialValueAsJson from './value.json'
|
||||||
import styled from 'react-emotion'
|
import styled from 'react-emotion'
|
||||||
import { Button, Icon, Toolbar } from '../components'
|
import { Button, Icon, Toolbar } from '../components'
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Deserialize the initial editor value.
|
||||||
|
*
|
||||||
|
* @type {Object}
|
||||||
|
*/
|
||||||
|
|
||||||
|
const initialValue = Value.fromJSON(initialValueAsJson)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A styled emoji inline component.
|
* A styled emoji inline component.
|
||||||
*
|
*
|
||||||
@@ -57,16 +65,6 @@ const noop = e => e.preventDefault()
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
class Emojis extends React.Component {
|
class Emojis extends React.Component {
|
||||||
/**
|
|
||||||
* Deserialize the raw initial value.
|
|
||||||
*
|
|
||||||
* @type {Object}
|
|
||||||
*/
|
|
||||||
|
|
||||||
state = {
|
|
||||||
value: Value.fromJSON(initialValue),
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The editor's schema.
|
* The editor's schema.
|
||||||
*
|
*
|
||||||
@@ -110,9 +108,8 @@ class Emojis extends React.Component {
|
|||||||
<Editor
|
<Editor
|
||||||
placeholder="Write some 😍👋🎉..."
|
placeholder="Write some 😍👋🎉..."
|
||||||
ref={this.ref}
|
ref={this.ref}
|
||||||
value={this.state.value}
|
defaultValue={initialValue}
|
||||||
schema={this.schema}
|
schema={this.schema}
|
||||||
onChange={this.onChange}
|
|
||||||
renderNode={this.renderNode}
|
renderNode={this.renderNode}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
@@ -156,16 +153,6 @@ class Emojis extends React.Component {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* On change.
|
|
||||||
*
|
|
||||||
* @param {Editor} editor
|
|
||||||
*/
|
|
||||||
|
|
||||||
onChange = ({ value }) => {
|
|
||||||
this.setState({ value })
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* When clicking a emoji, insert it
|
* When clicking a emoji, insert it
|
||||||
*
|
*
|
||||||
|
@@ -2,7 +2,15 @@ import { Editor } from 'slate-react'
|
|||||||
import { Block, Value } from 'slate'
|
import { Block, Value } from 'slate'
|
||||||
|
|
||||||
import React from 'react'
|
import React from 'react'
|
||||||
import initialValue from './value.json'
|
import initialValueAsJson from './value.json'
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Deserialize the initial editor value.
|
||||||
|
*
|
||||||
|
* @type {Object}
|
||||||
|
*/
|
||||||
|
|
||||||
|
const initialValue = Value.fromJSON(initialValueAsJson)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A simple schema to enforce the nodes in the Slate document.
|
* A simple schema to enforce the nodes in the Slate document.
|
||||||
@@ -38,16 +46,6 @@ const schema = {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
class ForcedLayout extends React.Component {
|
class ForcedLayout extends React.Component {
|
||||||
/**
|
|
||||||
* Deserialize the initial editor value.
|
|
||||||
*
|
|
||||||
* @type {Object}
|
|
||||||
*/
|
|
||||||
|
|
||||||
state = {
|
|
||||||
value: Value.fromJSON(initialValue),
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Render the editor.
|
* Render the editor.
|
||||||
*
|
*
|
||||||
@@ -58,9 +56,8 @@ class ForcedLayout extends React.Component {
|
|||||||
return (
|
return (
|
||||||
<Editor
|
<Editor
|
||||||
placeholder="Enter a title..."
|
placeholder="Enter a title..."
|
||||||
value={this.state.value}
|
defaultValue={initialValue}
|
||||||
schema={schema}
|
schema={schema}
|
||||||
onChange={this.onChange}
|
|
||||||
renderNode={this.renderNode}
|
renderNode={this.renderNode}
|
||||||
/>
|
/>
|
||||||
)
|
)
|
||||||
@@ -87,16 +84,6 @@ class ForcedLayout extends React.Component {
|
|||||||
return next()
|
return next()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* On change.
|
|
||||||
*
|
|
||||||
* @param {Editor} editor
|
|
||||||
*/
|
|
||||||
|
|
||||||
onChange = ({ value }) => {
|
|
||||||
this.setState({ value })
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@@ -35,6 +35,14 @@ for (let h = 0; h < HEADINGS; h++) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Deserialize the initial editor value.
|
||||||
|
*
|
||||||
|
* @type {Object}
|
||||||
|
*/
|
||||||
|
|
||||||
|
const initialValue = Value.fromJSON(json, { normalize: false })
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The huge document example.
|
* The huge document example.
|
||||||
*
|
*
|
||||||
@@ -42,14 +50,6 @@ for (let h = 0; h < HEADINGS; h++) {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
class HugeDocument extends React.Component {
|
class HugeDocument extends React.Component {
|
||||||
/**
|
|
||||||
* Deserialize the initial editor value.
|
|
||||||
*
|
|
||||||
* @type {Object}
|
|
||||||
*/
|
|
||||||
|
|
||||||
state = { value: Value.fromJSON(json, { normalize: false }) }
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Render the editor.
|
* Render the editor.
|
||||||
*
|
*
|
||||||
@@ -61,8 +61,7 @@ class HugeDocument extends React.Component {
|
|||||||
<Editor
|
<Editor
|
||||||
placeholder="Enter some text..."
|
placeholder="Enter some text..."
|
||||||
spellCheck={false}
|
spellCheck={false}
|
||||||
value={this.state.value}
|
defaultValue={initialValue}
|
||||||
onChange={this.onChange}
|
|
||||||
renderNode={this.renderNode}
|
renderNode={this.renderNode}
|
||||||
renderMark={this.renderMark}
|
renderMark={this.renderMark}
|
||||||
/>
|
/>
|
||||||
@@ -114,16 +113,6 @@ class HugeDocument extends React.Component {
|
|||||||
return next()
|
return next()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* On change.
|
|
||||||
*
|
|
||||||
* @param {Editor} editor
|
|
||||||
*/
|
|
||||||
|
|
||||||
onChange = ({ value }) => {
|
|
||||||
this.setState({ value })
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@@ -2,12 +2,20 @@ import { Editor, getEventRange, getEventTransfer } from 'slate-react'
|
|||||||
import { Block, Value } from 'slate'
|
import { Block, Value } from 'slate'
|
||||||
|
|
||||||
import React from 'react'
|
import React from 'react'
|
||||||
import initialValue from './value.json'
|
import initialValueAsJson from './value.json'
|
||||||
import imageExtensions from 'image-extensions'
|
import imageExtensions from 'image-extensions'
|
||||||
import isUrl from 'is-url'
|
import isUrl from 'is-url'
|
||||||
import styled from 'react-emotion'
|
import styled from 'react-emotion'
|
||||||
import { Button, Icon, Toolbar } from '../components'
|
import { Button, Icon, Toolbar } from '../components'
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Deserialize the initial editor value.
|
||||||
|
*
|
||||||
|
* @type {Object}
|
||||||
|
*/
|
||||||
|
|
||||||
|
const initialValue = Value.fromJSON(initialValueAsJson)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A styled image block component.
|
* A styled image block component.
|
||||||
*
|
*
|
||||||
@@ -83,16 +91,6 @@ const schema = {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
class Images extends React.Component {
|
class Images extends React.Component {
|
||||||
/**
|
|
||||||
* Deserialize the raw initial value.
|
|
||||||
*
|
|
||||||
* @type {Object}
|
|
||||||
*/
|
|
||||||
|
|
||||||
state = {
|
|
||||||
value: Value.fromJSON(initialValue),
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Store a reference to the `editor`.
|
* Store a reference to the `editor`.
|
||||||
*
|
*
|
||||||
@@ -120,9 +118,8 @@ class Images extends React.Component {
|
|||||||
<Editor
|
<Editor
|
||||||
placeholder="Enter some text..."
|
placeholder="Enter some text..."
|
||||||
ref={this.ref}
|
ref={this.ref}
|
||||||
value={this.state.value}
|
defaultValue={initialValue}
|
||||||
schema={schema}
|
schema={schema}
|
||||||
onChange={this.onChange}
|
|
||||||
onDrop={this.onDropOrPaste}
|
onDrop={this.onDropOrPaste}
|
||||||
onPaste={this.onDropOrPaste}
|
onPaste={this.onDropOrPaste}
|
||||||
renderNode={this.renderNode}
|
renderNode={this.renderNode}
|
||||||
@@ -153,16 +150,6 @@ class Images extends React.Component {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* On change.
|
|
||||||
*
|
|
||||||
* @param {Editor} editor
|
|
||||||
*/
|
|
||||||
|
|
||||||
onChange = ({ value }) => {
|
|
||||||
this.setState({ value })
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* On clicking the image button, prompt for an image and insert it.
|
* On clicking the image button, prompt for an image and insert it.
|
||||||
*
|
*
|
||||||
|
@@ -3,10 +3,18 @@ import { Value } from 'slate'
|
|||||||
|
|
||||||
import React from 'react'
|
import React from 'react'
|
||||||
import styled from 'react-emotion'
|
import styled from 'react-emotion'
|
||||||
import initialValue from './value.json'
|
import initialValueAsJson from './value.json'
|
||||||
import { Icon } from '../components'
|
import { Icon } from '../components'
|
||||||
import { createArrayValue } from 'react-values'
|
import { createArrayValue } from 'react-values'
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Deserialize the initial editor value.
|
||||||
|
*
|
||||||
|
* @type {Object}
|
||||||
|
*/
|
||||||
|
|
||||||
|
const initialValue = Value.fromJSON(initialValueAsJson)
|
||||||
|
|
||||||
const EventsValue = createArrayValue()
|
const EventsValue = createArrayValue()
|
||||||
|
|
||||||
const Wrapper = styled('div')`
|
const Wrapper = styled('div')`
|
||||||
@@ -193,10 +201,6 @@ const Event = ({ event, targetRange, selection }) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class InputTester extends React.Component {
|
class InputTester extends React.Component {
|
||||||
state = {
|
|
||||||
value: Value.fromJSON(initialValue),
|
|
||||||
}
|
|
||||||
|
|
||||||
componentDidMount() {
|
componentDidMount() {
|
||||||
const editor = this.el.querySelector('[contenteditable="true"]')
|
const editor = this.el.querySelector('[contenteditable="true"]')
|
||||||
editor.addEventListener('keydown', this.onEvent)
|
editor.addEventListener('keydown', this.onEvent)
|
||||||
@@ -221,7 +225,7 @@ class InputTester extends React.Component {
|
|||||||
spellCheck
|
spellCheck
|
||||||
placeholder="Enter some text..."
|
placeholder="Enter some text..."
|
||||||
ref={this.ref}
|
ref={this.ref}
|
||||||
value={this.state.value}
|
defaultValue={initialValue}
|
||||||
onChange={this.onChange}
|
onChange={this.onChange}
|
||||||
renderNode={this.renderNode}
|
renderNode={this.renderNode}
|
||||||
renderMark={this.renderMark}
|
renderMark={this.renderMark}
|
||||||
|
@@ -11,6 +11,16 @@ import React from 'react'
|
|||||||
// eslint-disable-next-line
|
// eslint-disable-next-line
|
||||||
;Prism.languages.markdown=Prism.languages.extend("markup",{}),Prism.languages.insertBefore("markdown","prolog",{blockquote:{pattern:/^>(?:[\t ]*>)*/m,alias:"punctuation"},code:[{pattern:/^(?: {4}|\t).+/m,alias:"keyword"},{pattern:/``.+?``|`[^`\n]+`/,alias:"keyword"}],title:[{pattern:/\w+.*(?:\r?\n|\r)(?:==+|--+)/,alias:"important",inside:{punctuation:/==+$|--+$/}},{pattern:/(^\s*)#+.+/m,lookbehind:!0,alias:"important",inside:{punctuation:/^#+|#+$/}}],hr:{pattern:/(^\s*)([*-])([\t ]*\2){2,}(?=\s*$)/m,lookbehind:!0,alias:"punctuation"},list:{pattern:/(^\s*)(?:[*+-]|\d+\.)(?=[\t ].)/m,lookbehind:!0,alias:"punctuation"},"url-reference":{pattern:/!?\[[^\]]+\]:[\t ]+(?:\S+|<(?:\\.|[^>\\])+>)(?:[\t ]+(?:"(?:\\.|[^"\\])*"|'(?:\\.|[^'\\])*'|\((?:\\.|[^)\\])*\)))?/,inside:{variable:{pattern:/^(!?\[)[^\]]+/,lookbehind:!0},string:/(?:"(?:\\.|[^"\\])*"|'(?:\\.|[^'\\])*'|\((?:\\.|[^)\\])*\))$/,punctuation:/^[\[\]!:]|[<>]/},alias:"url"},bold:{pattern:/(^|[^\\])(\*\*|__)(?:(?:\r?\n|\r)(?!\r?\n|\r)|.)+?\2/,lookbehind:!0,inside:{punctuation:/^\*\*|^__|\*\*$|__$/}},italic:{pattern:/(^|[^\\])([*_])(?:(?:\r?\n|\r)(?!\r?\n|\r)|.)+?\2/,lookbehind:!0,inside:{punctuation:/^[*_]|[*_]$/}},url:{pattern:/!?\[[^\]]+\](?:\([^\s)]+(?:[\t ]+"(?:\\.|[^"\\])*")?\)| ?\[[^\]\n]*\])/,inside:{variable:{pattern:/(!?\[)[^\]]+(?=\]$)/,lookbehind:!0},string:{pattern:/"(?:\\.|[^"\\])*"(?=\)$)/}}}}),Prism.languages.markdown.bold.inside.url=Prism.util.clone(Prism.languages.markdown.url),Prism.languages.markdown.italic.inside.url=Prism.util.clone(Prism.languages.markdown.url),Prism.languages.markdown.bold.inside.italic=Prism.util.clone(Prism.languages.markdown.italic),Prism.languages.markdown.italic.inside.bold=Prism.util.clone(Prism.languages.markdown.bold); // prettier-ignore
|
;Prism.languages.markdown=Prism.languages.extend("markup",{}),Prism.languages.insertBefore("markdown","prolog",{blockquote:{pattern:/^>(?:[\t ]*>)*/m,alias:"punctuation"},code:[{pattern:/^(?: {4}|\t).+/m,alias:"keyword"},{pattern:/``.+?``|`[^`\n]+`/,alias:"keyword"}],title:[{pattern:/\w+.*(?:\r?\n|\r)(?:==+|--+)/,alias:"important",inside:{punctuation:/==+$|--+$/}},{pattern:/(^\s*)#+.+/m,lookbehind:!0,alias:"important",inside:{punctuation:/^#+|#+$/}}],hr:{pattern:/(^\s*)([*-])([\t ]*\2){2,}(?=\s*$)/m,lookbehind:!0,alias:"punctuation"},list:{pattern:/(^\s*)(?:[*+-]|\d+\.)(?=[\t ].)/m,lookbehind:!0,alias:"punctuation"},"url-reference":{pattern:/!?\[[^\]]+\]:[\t ]+(?:\S+|<(?:\\.|[^>\\])+>)(?:[\t ]+(?:"(?:\\.|[^"\\])*"|'(?:\\.|[^'\\])*'|\((?:\\.|[^)\\])*\)))?/,inside:{variable:{pattern:/^(!?\[)[^\]]+/,lookbehind:!0},string:/(?:"(?:\\.|[^"\\])*"|'(?:\\.|[^'\\])*'|\((?:\\.|[^)\\])*\))$/,punctuation:/^[\[\]!:]|[<>]/},alias:"url"},bold:{pattern:/(^|[^\\])(\*\*|__)(?:(?:\r?\n|\r)(?!\r?\n|\r)|.)+?\2/,lookbehind:!0,inside:{punctuation:/^\*\*|^__|\*\*$|__$/}},italic:{pattern:/(^|[^\\])([*_])(?:(?:\r?\n|\r)(?!\r?\n|\r)|.)+?\2/,lookbehind:!0,inside:{punctuation:/^[*_]|[*_]$/}},url:{pattern:/!?\[[^\]]+\](?:\([^\s)]+(?:[\t ]+"(?:\\.|[^"\\])*")?\)| ?\[[^\]\n]*\])/,inside:{variable:{pattern:/(!?\[)[^\]]+(?=\]$)/,lookbehind:!0},string:{pattern:/"(?:\\.|[^"\\])*"(?=\)$)/}}}}),Prism.languages.markdown.bold.inside.url=Prism.util.clone(Prism.languages.markdown.url),Prism.languages.markdown.italic.inside.url=Prism.util.clone(Prism.languages.markdown.url),Prism.languages.markdown.bold.inside.italic=Prism.util.clone(Prism.languages.markdown.italic),Prism.languages.markdown.italic.inside.bold=Prism.util.clone(Prism.languages.markdown.bold); // prettier-ignore
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Deserialize the initial editor value.
|
||||||
|
*
|
||||||
|
* @type {Object}
|
||||||
|
*/
|
||||||
|
|
||||||
|
const initialValue = Plain.deserialize(
|
||||||
|
'Slate is flexible enough to add **decorations** that can format text based on its content. For example, this editor has **Markdown** preview decorations on it, to make it _dead_ simple to make an editor with built-in Markdown previewing.\n## Try it out!\nTry it out for yourself!'
|
||||||
|
)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The markdown preview example.
|
* The markdown preview example.
|
||||||
*
|
*
|
||||||
@@ -18,18 +28,6 @@ import React from 'react'
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
class MarkdownPreview extends React.Component {
|
class MarkdownPreview extends React.Component {
|
||||||
/**
|
|
||||||
* Deserialize the initial editor value.
|
|
||||||
*
|
|
||||||
* @type {Object}
|
|
||||||
*/
|
|
||||||
|
|
||||||
state = {
|
|
||||||
value: Plain.deserialize(
|
|
||||||
'Slate is flexible enough to add **decorations** that can format text based on its content. For example, this editor has **Markdown** preview decorations on it, to make it _dead_ simple to make an editor with built-in Markdown previewing.\n## Try it out!\nTry it out for yourself!'
|
|
||||||
),
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* Render the example.
|
* Render the example.
|
||||||
@@ -41,8 +39,7 @@ class MarkdownPreview extends React.Component {
|
|||||||
return (
|
return (
|
||||||
<Editor
|
<Editor
|
||||||
placeholder="Write some markdown..."
|
placeholder="Write some markdown..."
|
||||||
value={this.state.value}
|
defaultValue={initialValue}
|
||||||
onChange={this.onChange}
|
|
||||||
renderMark={this.renderMark}
|
renderMark={this.renderMark}
|
||||||
decorateNode={this.decorateNode}
|
decorateNode={this.decorateNode}
|
||||||
/>
|
/>
|
||||||
@@ -134,16 +131,6 @@ class MarkdownPreview extends React.Component {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* On change.
|
|
||||||
*
|
|
||||||
* @param {Editor} editor
|
|
||||||
*/
|
|
||||||
|
|
||||||
onChange = ({ value }) => {
|
|
||||||
this.setState({ value })
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Define a decorator for markdown styles.
|
* Define a decorator for markdown styles.
|
||||||
*
|
*
|
||||||
|
@@ -2,7 +2,15 @@ import { Editor } from 'slate-react'
|
|||||||
import { Value } from 'slate'
|
import { Value } from 'slate'
|
||||||
|
|
||||||
import React from 'react'
|
import React from 'react'
|
||||||
import initialValue from './value.json'
|
import initialValueAsJson from './value.json'
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Deserialize the initial editor value.
|
||||||
|
*
|
||||||
|
* @type {Object}
|
||||||
|
*/
|
||||||
|
|
||||||
|
const initialValue = Value.fromJSON(initialValueAsJson)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The auto-markdown example.
|
* The auto-markdown example.
|
||||||
@@ -11,16 +19,6 @@ import initialValue from './value.json'
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
class MarkdownShortcuts extends React.Component {
|
class MarkdownShortcuts extends React.Component {
|
||||||
/**
|
|
||||||
* Deserialize the raw initial value.
|
|
||||||
*
|
|
||||||
* @type {Object}
|
|
||||||
*/
|
|
||||||
|
|
||||||
state = {
|
|
||||||
value: Value.fromJSON(initialValue),
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the block type for a series of auto-markdown shortcut `chars`.
|
* Get the block type for a series of auto-markdown shortcut `chars`.
|
||||||
*
|
*
|
||||||
@@ -64,8 +62,7 @@ class MarkdownShortcuts extends React.Component {
|
|||||||
return (
|
return (
|
||||||
<Editor
|
<Editor
|
||||||
placeholder="Write some markdown..."
|
placeholder="Write some markdown..."
|
||||||
value={this.state.value}
|
defaultValue={initialValue}
|
||||||
onChange={this.onChange}
|
|
||||||
onKeyDown={this.onKeyDown}
|
onKeyDown={this.onKeyDown}
|
||||||
renderNode={this.renderNode}
|
renderNode={this.renderNode}
|
||||||
/>
|
/>
|
||||||
@@ -108,16 +105,6 @@ class MarkdownShortcuts extends React.Component {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* On change.
|
|
||||||
*
|
|
||||||
* @param {Editor} editor
|
|
||||||
*/
|
|
||||||
|
|
||||||
onChange = ({ value }) => {
|
|
||||||
this.setState({ value })
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* On key down, check for our specific key shortcuts.
|
* On key down, check for our specific key shortcuts.
|
||||||
*
|
*
|
||||||
|
@@ -3,9 +3,17 @@ import { Editor, getEventTransfer } from 'slate-react'
|
|||||||
import { Value } from 'slate'
|
import { Value } from 'slate'
|
||||||
|
|
||||||
import React from 'react'
|
import React from 'react'
|
||||||
import initialValue from './value.json'
|
import initialValueAsJson from './value.json'
|
||||||
import styled from 'react-emotion'
|
import styled from 'react-emotion'
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Deserialize the initial editor value.
|
||||||
|
*
|
||||||
|
* @type {Object}
|
||||||
|
*/
|
||||||
|
|
||||||
|
const initialValue = Value.fromJSON(initialValueAsJson)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tags to blocks.
|
* Tags to blocks.
|
||||||
*
|
*
|
||||||
@@ -152,16 +160,6 @@ const serializer = new Html({ rules: RULES })
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
class PasteHtml extends React.Component {
|
class PasteHtml extends React.Component {
|
||||||
/**
|
|
||||||
* Deserialize the raw initial value.
|
|
||||||
*
|
|
||||||
* @type {Object}
|
|
||||||
*/
|
|
||||||
|
|
||||||
state = {
|
|
||||||
value: Value.fromJSON(initialValue),
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The editor's schema.
|
* The editor's schema.
|
||||||
*
|
*
|
||||||
@@ -186,10 +184,9 @@ class PasteHtml extends React.Component {
|
|||||||
return (
|
return (
|
||||||
<Editor
|
<Editor
|
||||||
placeholder="Paste in some HTML..."
|
placeholder="Paste in some HTML..."
|
||||||
value={this.state.value}
|
defaultValue={initialValue}
|
||||||
schema={this.schema}
|
schema={this.schema}
|
||||||
onPaste={this.onPaste}
|
onPaste={this.onPaste}
|
||||||
onChange={this.onChange}
|
|
||||||
renderNode={this.renderNode}
|
renderNode={this.renderNode}
|
||||||
renderMark={this.renderMark}
|
renderMark={this.renderMark}
|
||||||
/>
|
/>
|
||||||
@@ -277,16 +274,6 @@ class PasteHtml extends React.Component {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* On change, save the new value.
|
|
||||||
*
|
|
||||||
* @param {Editor} editor
|
|
||||||
*/
|
|
||||||
|
|
||||||
onChange = ({ value }) => {
|
|
||||||
this.setState({ value })
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* On paste, deserialize the HTML and then insert the fragment.
|
* On paste, deserialize the HTML and then insert the fragment.
|
||||||
*
|
*
|
||||||
|
@@ -3,6 +3,16 @@ import { Editor } from 'slate-react'
|
|||||||
|
|
||||||
import React from 'react'
|
import React from 'react'
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Deserialize the initial editor value.
|
||||||
|
*
|
||||||
|
* @type {Object}
|
||||||
|
*/
|
||||||
|
|
||||||
|
const initialValue = Plain.deserialize(
|
||||||
|
'This is editable plain text, just like a <textarea>!'
|
||||||
|
)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The plain text example.
|
* The plain text example.
|
||||||
*
|
*
|
||||||
@@ -10,18 +20,6 @@ import React from 'react'
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
class PlainText extends React.Component {
|
class PlainText extends React.Component {
|
||||||
/**
|
|
||||||
* Deserialize the initial editor value.
|
|
||||||
*
|
|
||||||
* @type {Object}
|
|
||||||
*/
|
|
||||||
|
|
||||||
state = {
|
|
||||||
value: Plain.deserialize(
|
|
||||||
'This is editable plain text, just like a <textarea>!'
|
|
||||||
),
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Render the editor.
|
* Render the editor.
|
||||||
*
|
*
|
||||||
@@ -32,21 +30,10 @@ class PlainText extends React.Component {
|
|||||||
return (
|
return (
|
||||||
<Editor
|
<Editor
|
||||||
placeholder="Enter some plain text..."
|
placeholder="Enter some plain text..."
|
||||||
value={this.state.value}
|
defaultValue={initialValue}
|
||||||
onChange={this.onChange}
|
|
||||||
/>
|
/>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* On change.
|
|
||||||
*
|
|
||||||
* @param {Editor} editor
|
|
||||||
*/
|
|
||||||
|
|
||||||
onChange = ({ value }) => {
|
|
||||||
this.setState({ value })
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@@ -6,6 +6,17 @@ import CollapseOnEscape from './collapse-on-escape'
|
|||||||
import SoftBreak from './soft-break'
|
import SoftBreak from './soft-break'
|
||||||
import WordCount from './word-count'
|
import WordCount from './word-count'
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Deserialize the initial editor value.
|
||||||
|
*
|
||||||
|
* @type {Object}
|
||||||
|
*/
|
||||||
|
|
||||||
|
const initialValue = Plain.deserialize(`This example shows how you can extend Slate with plugins! It uses four fairly simple plugins, but you can use any plugins you want, or write your own!
|
||||||
|
The first is a simple plugin to collapse the selection whenever the escape key is pressed. Try selecting some text and pressing escape.
|
||||||
|
The second is another simple plugin that inserts a "soft" break when enter is pressed instead of creating a new block. Try pressing enter!
|
||||||
|
The third is an example of using the plugin.render property to create a higher-order-component.`)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Plugins.
|
* Plugins.
|
||||||
*/
|
*/
|
||||||
@@ -19,19 +30,6 @@ const plugins = [CollapseOnEscape(), SoftBreak(), WordCount()]
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
class Plugins extends React.Component {
|
class Plugins extends React.Component {
|
||||||
/**
|
|
||||||
* Deserialize the initial editor value.
|
|
||||||
*
|
|
||||||
* @type {Object}
|
|
||||||
*/
|
|
||||||
|
|
||||||
state = {
|
|
||||||
value: Plain.deserialize(`This example shows how you can extend Slate with plugins! It uses four fairly simple plugins, but you can use any plugins you want, or write your own!
|
|
||||||
The first is a simple plugin to collapse the selection whenever the escape key is pressed. Try selecting some text and pressing escape.
|
|
||||||
The second is another simple plugin that inserts a "soft" break when enter is pressed instead of creating a new block. Try pressing enter!
|
|
||||||
The third is an example of using the plugin.render property to create a higher-order-component.`),
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Render the editor.
|
* Render the editor.
|
||||||
*
|
*
|
||||||
@@ -43,21 +41,10 @@ The third is an example of using the plugin.render property to create a higher-o
|
|||||||
<Editor
|
<Editor
|
||||||
placeholder="Enter some text..."
|
placeholder="Enter some text..."
|
||||||
plugins={plugins}
|
plugins={plugins}
|
||||||
value={this.state.value}
|
defaultValue={initialValue}
|
||||||
onChange={this.onChange}
|
|
||||||
/>
|
/>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* On change.
|
|
||||||
*
|
|
||||||
* @param {Editor} editor
|
|
||||||
*/
|
|
||||||
|
|
||||||
onChange = ({ value }) => {
|
|
||||||
this.setState({ value })
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@@ -3,6 +3,16 @@ import { Editor } from 'slate-react'
|
|||||||
|
|
||||||
import React from 'react'
|
import React from 'react'
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Deserialize the read-only value.
|
||||||
|
*
|
||||||
|
* @type {Object}
|
||||||
|
*/
|
||||||
|
|
||||||
|
const value = Plain.deserialize(
|
||||||
|
'This is read-only text. You should not be able to edit it, which is useful for scenarios where you want to render via Slate, without giving the user editing permissions.'
|
||||||
|
)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The read-only example.
|
* The read-only example.
|
||||||
*
|
*
|
||||||
@@ -10,18 +20,6 @@ import React from 'react'
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
class ReadOnly extends React.Component {
|
class ReadOnly extends React.Component {
|
||||||
/**
|
|
||||||
* Deserialize the initial editor value.
|
|
||||||
*
|
|
||||||
* @type {Object}
|
|
||||||
*/
|
|
||||||
|
|
||||||
state = {
|
|
||||||
value: Plain.deserialize(
|
|
||||||
'This is read-only text. You should not be able to edit it, which is useful for scenarios where you want to render via Slate, without giving the user editing permissions.'
|
|
||||||
),
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Render the editor.
|
* Render the editor.
|
||||||
*
|
*
|
||||||
@@ -29,24 +27,7 @@ class ReadOnly extends React.Component {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
return (
|
return <Editor defaultValue={value} readOnly />
|
||||||
<Editor
|
|
||||||
readOnly
|
|
||||||
placeholder="Enter some text..."
|
|
||||||
value={this.state.value}
|
|
||||||
onChange={this.onChange}
|
|
||||||
/>
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* On change.
|
|
||||||
*
|
|
||||||
* @param {Editor} editor
|
|
||||||
*/
|
|
||||||
|
|
||||||
onChange = ({ value }) => {
|
|
||||||
this.setState({ value })
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -2,7 +2,15 @@ import { Editor } from 'slate-react'
|
|||||||
import { Value } from 'slate'
|
import { Value } from 'slate'
|
||||||
|
|
||||||
import React from 'react'
|
import React from 'react'
|
||||||
import initialValue from './value.json'
|
import initialValueAsJson from './value.json'
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Deserialize the initial editor value.
|
||||||
|
*
|
||||||
|
* @type {Object}
|
||||||
|
*/
|
||||||
|
|
||||||
|
const initialValue = Value.fromJSON(initialValueAsJson)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A right-to-left text example.
|
* A right-to-left text example.
|
||||||
@@ -11,16 +19,6 @@ import initialValue from './value.json'
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
class RTL extends React.Component {
|
class RTL extends React.Component {
|
||||||
/**
|
|
||||||
* Deserialize the initial editor value.
|
|
||||||
*
|
|
||||||
* @type {Object}
|
|
||||||
*/
|
|
||||||
|
|
||||||
state = {
|
|
||||||
value: Value.fromJSON(initialValue),
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Render the editor.
|
* Render the editor.
|
||||||
*
|
*
|
||||||
@@ -31,8 +29,7 @@ class RTL extends React.Component {
|
|||||||
return (
|
return (
|
||||||
<Editor
|
<Editor
|
||||||
placeholder="Enter some plain text..."
|
placeholder="Enter some plain text..."
|
||||||
value={this.state.value}
|
defaultValue={initialValue}
|
||||||
onChange={this.onChange}
|
|
||||||
onKeyDown={this.onKeyDown}
|
onKeyDown={this.onKeyDown}
|
||||||
renderNode={this.renderNode}
|
renderNode={this.renderNode}
|
||||||
/>
|
/>
|
||||||
@@ -57,16 +54,6 @@ class RTL extends React.Component {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* On change.
|
|
||||||
*
|
|
||||||
* @param {Editor} editor
|
|
||||||
*/
|
|
||||||
|
|
||||||
onChange = ({ value }) => {
|
|
||||||
this.setState({ value })
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* On key down, if it's <shift-enter> add a soft break.
|
* On key down, if it's <shift-enter> add a soft break.
|
||||||
*
|
*
|
||||||
|
@@ -2,10 +2,18 @@ import { Editor } from 'slate-react'
|
|||||||
import { Value } from 'slate'
|
import { Value } from 'slate'
|
||||||
|
|
||||||
import React from 'react'
|
import React from 'react'
|
||||||
import initialValue from './value.json'
|
import initialValueAsJson from './value.json'
|
||||||
import styled from 'react-emotion'
|
import styled from 'react-emotion'
|
||||||
import { Icon, Toolbar } from '../components'
|
import { Icon, Toolbar } from '../components'
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Deserialize the initial editor value.
|
||||||
|
*
|
||||||
|
* @type {Object}
|
||||||
|
*/
|
||||||
|
|
||||||
|
const initialValue = Value.fromJSON(initialValueAsJson)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Some styled components for the search box.
|
* Some styled components for the search box.
|
||||||
*
|
*
|
||||||
@@ -35,16 +43,6 @@ const SearchInput = styled('input')`
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
class SearchHighlighting extends React.Component {
|
class SearchHighlighting extends React.Component {
|
||||||
/**
|
|
||||||
* Deserialize the initial editor value.
|
|
||||||
*
|
|
||||||
* @type {Object}
|
|
||||||
*/
|
|
||||||
|
|
||||||
state = {
|
|
||||||
value: Value.fromJSON(initialValue),
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The editor's schema.
|
* The editor's schema.
|
||||||
*
|
*
|
||||||
@@ -91,9 +89,8 @@ class SearchHighlighting extends React.Component {
|
|||||||
<Editor
|
<Editor
|
||||||
placeholder="Enter some rich text..."
|
placeholder="Enter some rich text..."
|
||||||
ref={this.ref}
|
ref={this.ref}
|
||||||
value={this.state.value}
|
defaultValue={initialValue}
|
||||||
schema={this.schema}
|
schema={this.schema}
|
||||||
onChange={this.onChange}
|
|
||||||
renderMark={this.renderMark}
|
renderMark={this.renderMark}
|
||||||
spellCheck
|
spellCheck
|
||||||
/>
|
/>
|
||||||
@@ -123,16 +120,6 @@ class SearchHighlighting extends React.Component {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* On change, save the new `value`.
|
|
||||||
*
|
|
||||||
* @param {Editor} editor
|
|
||||||
*/
|
|
||||||
|
|
||||||
onChange = ({ value }) => {
|
|
||||||
this.setState({ value })
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* On input change, update the decorations.
|
* On input change, update the decorations.
|
||||||
*
|
*
|
||||||
|
@@ -3,7 +3,15 @@ import { Editor, getEventTransfer } from 'slate-react'
|
|||||||
import { Value } from 'slate'
|
import { Value } from 'slate'
|
||||||
|
|
||||||
import React from 'react'
|
import React from 'react'
|
||||||
import initialValue from './value.json'
|
import initialValueAsJson from './value.json'
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Deserialize the initial editor value.
|
||||||
|
*
|
||||||
|
* @type {Object}
|
||||||
|
*/
|
||||||
|
|
||||||
|
const initialValue = Value.fromJSON(initialValueAsJson)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The tables example.
|
* The tables example.
|
||||||
@@ -12,16 +20,6 @@ import initialValue from './value.json'
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
class Tables extends React.Component {
|
class Tables extends React.Component {
|
||||||
/**
|
|
||||||
* Deserialize the raw initial value.
|
|
||||||
*
|
|
||||||
* @type {Object}
|
|
||||||
*/
|
|
||||||
|
|
||||||
state = {
|
|
||||||
value: Value.fromJSON(initialValue),
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Render the example.
|
* Render the example.
|
||||||
*
|
*
|
||||||
@@ -32,8 +30,7 @@ class Tables extends React.Component {
|
|||||||
return (
|
return (
|
||||||
<Editor
|
<Editor
|
||||||
placeholder="Enter some text..."
|
placeholder="Enter some text..."
|
||||||
value={this.state.value}
|
defaultValue={initialValue}
|
||||||
onChange={this.onChange}
|
|
||||||
onKeyDown={this.onKeyDown}
|
onKeyDown={this.onKeyDown}
|
||||||
onDrop={this.onDropOrPaste}
|
onDrop={this.onDropOrPaste}
|
||||||
onPaste={this.onDropOrPaste}
|
onPaste={this.onDropOrPaste}
|
||||||
@@ -101,16 +98,6 @@ class Tables extends React.Component {
|
|||||||
event.preventDefault()
|
event.preventDefault()
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* On change.
|
|
||||||
*
|
|
||||||
* @param {Editor} editor
|
|
||||||
*/
|
|
||||||
|
|
||||||
onChange = ({ value }) => {
|
|
||||||
this.setState({ value })
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* On delete, do nothing if at the end of a table cell.
|
* On delete, do nothing if at the end of a table cell.
|
||||||
*
|
*
|
||||||
|
@@ -35,6 +35,7 @@ class Editor extends React.Component {
|
|||||||
autoCorrect: Types.bool,
|
autoCorrect: Types.bool,
|
||||||
autoFocus: Types.bool,
|
autoFocus: Types.bool,
|
||||||
className: Types.string,
|
className: Types.string,
|
||||||
|
defaultValue: SlateTypes.value,
|
||||||
id: Types.string,
|
id: Types.string,
|
||||||
onChange: Types.func,
|
onChange: Types.func,
|
||||||
options: Types.object,
|
options: Types.object,
|
||||||
@@ -46,7 +47,7 @@ class Editor extends React.Component {
|
|||||||
spellCheck: Types.bool,
|
spellCheck: Types.bool,
|
||||||
style: Types.object,
|
style: Types.object,
|
||||||
tabIndex: Types.number,
|
tabIndex: Types.number,
|
||||||
value: SlateTypes.value.isRequired,
|
value: SlateTypes.value,
|
||||||
...EVENT_HANDLERS.reduce((obj, handler) => {
|
...EVENT_HANDLERS.reduce((obj, handler) => {
|
||||||
obj[handler] = Types.func
|
obj[handler] = Types.func
|
||||||
return obj
|
return obj
|
||||||
@@ -77,7 +78,7 @@ class Editor extends React.Component {
|
|||||||
* @type {Object}
|
* @type {Object}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
state = {}
|
state = { value: this.props.defaultValue }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Temporary values.
|
* Temporary values.
|
||||||
@@ -105,7 +106,7 @@ class Editor extends React.Component {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (this.tmp.change) {
|
if (this.tmp.change) {
|
||||||
this.props.onChange(this.tmp.change)
|
this.handleChange(this.tmp.change)
|
||||||
this.tmp.change = null
|
this.tmp.change = null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -118,7 +119,7 @@ class Editor extends React.Component {
|
|||||||
this.tmp.updates++
|
this.tmp.updates++
|
||||||
|
|
||||||
if (this.tmp.change) {
|
if (this.tmp.change) {
|
||||||
this.props.onChange(this.tmp.change)
|
this.handleChange(this.tmp.change)
|
||||||
this.tmp.change = null
|
this.tmp.change = null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -146,12 +147,17 @@ class Editor extends React.Component {
|
|||||||
this.resolveController(plugins, schema, commands, queries, placeholder)
|
this.resolveController(plugins, schema, commands, queries, placeholder)
|
||||||
|
|
||||||
// Set the current props on the controller.
|
// Set the current props on the controller.
|
||||||
const { options, readOnly, value } = props
|
const { options, readOnly, value: valueFromProps } = props
|
||||||
|
const { value: valueFromState } = this.state
|
||||||
|
const value = valueFromProps || valueFromState
|
||||||
this.controller.setReadOnly(readOnly)
|
this.controller.setReadOnly(readOnly)
|
||||||
this.controller.setValue(value, options)
|
this.controller.setValue(value, options)
|
||||||
|
|
||||||
// Render the editor's children with the controller.
|
// Render the editor's children with the controller.
|
||||||
const children = this.controller.run('renderEditor', props)
|
const children = this.controller.run('renderEditor', {
|
||||||
|
...props,
|
||||||
|
value,
|
||||||
|
})
|
||||||
return children
|
return children
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -178,11 +184,14 @@ class Editor extends React.Component {
|
|||||||
)
|
)
|
||||||
|
|
||||||
this.tmp.resolves++
|
this.tmp.resolves++
|
||||||
const react = ReactPlugin(this.props)
|
const react = ReactPlugin({
|
||||||
|
...this.props,
|
||||||
|
value: this.props.value || this.state.value,
|
||||||
|
})
|
||||||
|
|
||||||
const onChange = change => {
|
const onChange = change => {
|
||||||
if (this.tmp.mounted) {
|
if (this.tmp.mounted) {
|
||||||
this.props.onChange(change)
|
this.handleChange(change)
|
||||||
} else {
|
} else {
|
||||||
this.tmp.change = change
|
this.tmp.change = change
|
||||||
}
|
}
|
||||||
@@ -197,6 +206,18 @@ class Editor extends React.Component {
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
handleChange(change) {
|
||||||
|
const { onChange } = this.props
|
||||||
|
const { value } = this.state
|
||||||
|
|
||||||
|
if (value) {
|
||||||
|
// Syncing value inside this component since parent does not want control of it (defaultValue was used)
|
||||||
|
this.setState({ value: change.value })
|
||||||
|
}
|
||||||
|
|
||||||
|
onChange(change)
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Mimic the API of the `Editor` controller, so that this component instance
|
* Mimic the API of the `Editor` controller, so that this component instance
|
||||||
* can be passed in its place to plugins.
|
* can be passed in its place to plugins.
|
||||||
|
Reference in New Issue
Block a user