From 61be5f888122cf1800d914af43a1b4180a8f95ef Mon Sep 17 00:00:00 2001 From: Dundercover Date: Thu, 15 Nov 2018 17:21:42 +0100 Subject: [PATCH] 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 --- examples/check-lists/index.js | 33 +++++---------- examples/code-highlighting/index.js | 33 +++++---------- examples/embeds/index.js | 33 +++++---------- examples/emojis/index.js | 33 +++++---------- examples/forced-layout/index.js | 33 +++++---------- examples/huge-document/index.js | 29 ++++--------- examples/images/index.js | 33 +++++---------- examples/input-tester/index.js | 16 +++++--- examples/markdown-preview/index.js | 35 +++++----------- examples/markdown-shortcuts/index.js | 33 +++++---------- examples/paste-html/index.js | 33 +++++---------- examples/plain-text/index.js | 35 +++++----------- examples/plugins/index.js | 37 ++++++----------- examples/read-only/index.js | 41 +++++-------------- examples/rtl/index.js | 33 +++++---------- examples/search-highlighting/index.js | 33 +++++---------- examples/tables/index.js | 33 +++++---------- packages/slate-react/src/components/editor.js | 37 +++++++++++++---- 18 files changed, 203 insertions(+), 390 deletions(-) diff --git a/examples/check-lists/index.js b/examples/check-lists/index.js index 4b79b42ca..182882b4f 100644 --- a/examples/check-lists/index.js +++ b/examples/check-lists/index.js @@ -2,9 +2,17 @@ import { Editor } from 'slate-react' import { Value } from 'slate' import React from 'react' -import initialValue from './value.json' +import initialValueAsJson from './value.json' import styled from 'react-emotion' +/** + * Deserialize the initial editor value. + * + * @type {Object} + */ + +const initialValue = Value.fromJSON(initialValueAsJson) + /** * Create a few styling components. * @@ -88,16 +96,6 @@ class CheckListItem extends React.Component { */ class CheckLists extends React.Component { - /** - * Deserialize the initial editor value. - * - * @type {Object} - */ - - state = { - value: Value.fromJSON(initialValue), - } - /** * Render. * @@ -109,8 +107,7 @@ class CheckLists extends React.Component { @@ -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... * diff --git a/examples/code-highlighting/index.js b/examples/code-highlighting/index.js index 9589e43f3..2f6d16da7 100644 --- a/examples/code-highlighting/index.js +++ b/examples/code-highlighting/index.js @@ -3,7 +3,15 @@ import { Value } from 'slate' import Prism from 'prismjs' 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. @@ -67,16 +75,6 @@ function getContent(token) { */ class CodeHighlighting extends React.Component { - /** - * Deserialize the raw initial value. - * - * @type {Object} - */ - - state = { - value: Value.fromJSON(initialValue), - } - /** * Render. * @@ -87,8 +85,7 @@ class CodeHighlighting extends React.Component { return ( { - this.setState({ value }) - } - /** * On key down inside code blocks, insert soft new lines. * diff --git a/examples/embeds/index.js b/examples/embeds/index.js index e4c05d52c..030dd0f1b 100644 --- a/examples/embeds/index.js +++ b/examples/embeds/index.js @@ -3,7 +3,15 @@ import { Value } from 'slate' import React from 'react' 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. @@ -12,16 +20,6 @@ import initialValue from './value.json' */ class Embeds extends React.Component { - /** - * Deserialize the raw initial value. - * - * @type {Object} - */ - - state = { - value: Value.fromJSON(initialValue), - } - /** * The editor's schema. * @@ -46,9 +44,8 @@ class Embeds extends React.Component { return ( ) @@ -70,16 +67,6 @@ class Embeds extends React.Component { return next() } } - - /** - * On change. - * - * @param {Editor} editor - */ - - onChange = ({ value }) => { - this.setState({ value }) - } } /** diff --git a/examples/emojis/index.js b/examples/emojis/index.js index ccf2c9544..876dc6201 100644 --- a/examples/emojis/index.js +++ b/examples/emojis/index.js @@ -2,10 +2,18 @@ import { Editor } from 'slate-react' import { Value } from 'slate' import React from 'react' -import initialValue from './value.json' +import initialValueAsJson from './value.json' import styled from 'react-emotion' import { Button, Icon, Toolbar } from '../components' +/** + * Deserialize the initial editor value. + * + * @type {Object} + */ + +const initialValue = Value.fromJSON(initialValueAsJson) + /** * A styled emoji inline component. * @@ -57,16 +65,6 @@ const noop = e => e.preventDefault() */ class Emojis extends React.Component { - /** - * Deserialize the raw initial value. - * - * @type {Object} - */ - - state = { - value: Value.fromJSON(initialValue), - } - /** * The editor's schema. * @@ -110,9 +108,8 @@ class Emojis extends React.Component { @@ -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 * diff --git a/examples/forced-layout/index.js b/examples/forced-layout/index.js index 8241405f5..5ebab8cad 100644 --- a/examples/forced-layout/index.js +++ b/examples/forced-layout/index.js @@ -2,7 +2,15 @@ import { Editor } from 'slate-react' import { Block, Value } from 'slate' 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. @@ -38,16 +46,6 @@ const schema = { */ class ForcedLayout extends React.Component { - /** - * Deserialize the initial editor value. - * - * @type {Object} - */ - - state = { - value: Value.fromJSON(initialValue), - } - /** * Render the editor. * @@ -58,9 +56,8 @@ class ForcedLayout extends React.Component { return ( ) @@ -87,16 +84,6 @@ class ForcedLayout extends React.Component { return next() } } - - /** - * On change. - * - * @param {Editor} editor - */ - - onChange = ({ value }) => { - this.setState({ value }) - } } /** diff --git a/examples/huge-document/index.js b/examples/huge-document/index.js index 95c349de0..67d840c3f 100644 --- a/examples/huge-document/index.js +++ b/examples/huge-document/index.js @@ -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. * @@ -42,14 +50,6 @@ for (let h = 0; h < HEADINGS; h++) { */ class HugeDocument extends React.Component { - /** - * Deserialize the initial editor value. - * - * @type {Object} - */ - - state = { value: Value.fromJSON(json, { normalize: false }) } - /** * Render the editor. * @@ -61,8 +61,7 @@ class HugeDocument extends React.Component { @@ -114,16 +113,6 @@ class HugeDocument extends React.Component { return next() } } - - /** - * On change. - * - * @param {Editor} editor - */ - - onChange = ({ value }) => { - this.setState({ value }) - } } /** diff --git a/examples/images/index.js b/examples/images/index.js index 333f47396..ec2e9c3b9 100644 --- a/examples/images/index.js +++ b/examples/images/index.js @@ -2,12 +2,20 @@ import { Editor, getEventRange, getEventTransfer } from 'slate-react' import { Block, Value } from 'slate' import React from 'react' -import initialValue from './value.json' +import initialValueAsJson from './value.json' import imageExtensions from 'image-extensions' import isUrl from 'is-url' import styled from 'react-emotion' import { Button, Icon, Toolbar } from '../components' +/** + * Deserialize the initial editor value. + * + * @type {Object} + */ + +const initialValue = Value.fromJSON(initialValueAsJson) + /** * A styled image block component. * @@ -83,16 +91,6 @@ const schema = { */ class Images extends React.Component { - /** - * Deserialize the raw initial value. - * - * @type {Object} - */ - - state = { - value: Value.fromJSON(initialValue), - } - /** * Store a reference to the `editor`. * @@ -120,9 +118,8 @@ class Images extends React.Component { { - this.setState({ value }) - } - /** * On clicking the image button, prompt for an image and insert it. * diff --git a/examples/input-tester/index.js b/examples/input-tester/index.js index b7db8aa63..e8bc81689 100644 --- a/examples/input-tester/index.js +++ b/examples/input-tester/index.js @@ -3,10 +3,18 @@ import { Value } from 'slate' import React from 'react' import styled from 'react-emotion' -import initialValue from './value.json' +import initialValueAsJson from './value.json' import { Icon } from '../components' import { createArrayValue } from 'react-values' +/** + * Deserialize the initial editor value. + * + * @type {Object} + */ + +const initialValue = Value.fromJSON(initialValueAsJson) + const EventsValue = createArrayValue() const Wrapper = styled('div')` @@ -193,10 +201,6 @@ const Event = ({ event, targetRange, selection }) => { } class InputTester extends React.Component { - state = { - value: Value.fromJSON(initialValue), - } - componentDidMount() { const editor = this.el.querySelector('[contenteditable="true"]') editor.addEventListener('keydown', this.onEvent) @@ -221,7 +225,7 @@ class InputTester extends React.Component { spellCheck placeholder="Enter some text..." ref={this.ref} - value={this.state.value} + defaultValue={initialValue} onChange={this.onChange} renderNode={this.renderNode} renderMark={this.renderMark} diff --git a/examples/markdown-preview/index.js b/examples/markdown-preview/index.js index 295bcd0a9..296995c66 100644 --- a/examples/markdown-preview/index.js +++ b/examples/markdown-preview/index.js @@ -11,6 +11,16 @@ import React from 'react' // 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 +/** + * 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. * @@ -18,18 +28,6 @@ import React from 'react' */ 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. @@ -41,8 +39,7 @@ class MarkdownPreview extends React.Component { return ( @@ -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. * diff --git a/examples/markdown-shortcuts/index.js b/examples/markdown-shortcuts/index.js index d435a8374..4e9158483 100644 --- a/examples/markdown-shortcuts/index.js +++ b/examples/markdown-shortcuts/index.js @@ -2,7 +2,15 @@ import { Editor } from 'slate-react' import { Value } from 'slate' 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. @@ -11,16 +19,6 @@ import initialValue from './value.json' */ 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`. * @@ -64,8 +62,7 @@ class MarkdownShortcuts extends React.Component { return ( @@ -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. * diff --git a/examples/paste-html/index.js b/examples/paste-html/index.js index cd89c76d9..0d909cc52 100644 --- a/examples/paste-html/index.js +++ b/examples/paste-html/index.js @@ -3,9 +3,17 @@ import { Editor, getEventTransfer } from 'slate-react' import { Value } from 'slate' import React from 'react' -import initialValue from './value.json' +import initialValueAsJson from './value.json' import styled from 'react-emotion' +/** + * Deserialize the initial editor value. + * + * @type {Object} + */ + +const initialValue = Value.fromJSON(initialValueAsJson) + /** * Tags to blocks. * @@ -152,16 +160,6 @@ const serializer = new Html({ rules: RULES }) */ class PasteHtml extends React.Component { - /** - * Deserialize the raw initial value. - * - * @type {Object} - */ - - state = { - value: Value.fromJSON(initialValue), - } - /** * The editor's schema. * @@ -186,10 +184,9 @@ class PasteHtml extends React.Component { return ( @@ -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. * diff --git a/examples/plain-text/index.js b/examples/plain-text/index.js index 650c670ed..6de284f38 100644 --- a/examples/plain-text/index.js +++ b/examples/plain-text/index.js @@ -3,6 +3,16 @@ import { Editor } from 'slate-react' import React from 'react' +/** + * Deserialize the initial editor value. + * + * @type {Object} + */ + +const initialValue = Plain.deserialize( + 'This is editable plain text, just like a