1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-08-16 20:24:01 +02:00

Restore dom (#2782)

* Working version of restore dom

* Fix linting errors
This commit is contained in:
Sunny Hirai
2019-05-16 10:59:51 -07:00
committed by GitHub
parent e4fae23454
commit d24f3b16ec
7 changed files with 221 additions and 3 deletions

View File

@@ -53,6 +53,7 @@ class Content extends React.Component {
static propTypes = {
autoCorrect: Types.bool.isRequired,
className: Types.string,
contentKey: Types.number,
editor: Types.object.isRequired,
id: Types.string,
readOnly: Types.bool.isRequired,
@@ -486,6 +487,7 @@ class Content extends React.Component {
return (
<Container
key={this.props.contentKey}
{...handlers}
{...data}
ref={this.ref}

View File

@@ -79,7 +79,7 @@ class Editor extends React.Component {
* @type {Object}
*/
state = { value: this.props.defaultValue }
state = { value: this.props.defaultValue, contentKey: 0 }
/**
* Temporary values.
@@ -151,6 +151,7 @@ class Editor extends React.Component {
const { options, readOnly, value: valueFromProps } = this.props
const { value: valueFromState } = this.state
const value = valueFromProps || valueFromState
const { contentKey } = this.state
this.controller.setReadOnly(readOnly)
this.controller.setValue(value, options)
@@ -170,6 +171,7 @@ class Editor extends React.Component {
ref={this.tmp.contentRef}
autoCorrect={autoCorrect}
className={className}
contentKey={contentKey}
editor={this}
id={id}
onEvent={(handler, event) => this.run(handler, event)}

View File

@@ -4,6 +4,7 @@ import EditorPropsPlugin from './editor-props'
import RenderingPlugin from './rendering'
import QueriesPlugin from './queries'
import DOMPlugin from '../dom'
import RestoreDOMPlugin from './restore-dom'
/**
* A plugin that adds the React-specific rendering logic to the editor.
@@ -20,7 +21,7 @@ function ReactPlugin(options = {}) {
const domPlugin = DOMPlugin({
plugins: [editorPropsPlugin, ...plugins],
})
const restoreDomPlugin = RestoreDOMPlugin()
const placeholderPlugin = PlaceholderPlugin({
placeholder,
when: (editor, node) =>
@@ -30,7 +31,13 @@ function ReactPlugin(options = {}) {
Array.from(node.texts()).length === 1,
})
return [domPlugin, placeholderPlugin, renderingPlugin, queriesPlugin]
return [
domPlugin,
restoreDomPlugin,
placeholderPlugin,
renderingPlugin,
queriesPlugin,
]
}
/**

View File

@@ -0,0 +1,21 @@
function RestoreDOMPlugin() {
/**
* Makes sure that on the next Content `render` the DOM is restored.
* This gets us around issues where the DOM is in a different state than
* React's virtual DOM and would crash.
*
* @param {Editor} editor
*/
function restoreDOM(editor) {
editor.setState({ contentKey: editor.state.contentKey + 1 })
}
return {
commands: {
restoreDOM,
},
}
}
export default RestoreDOMPlugin