mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-08-13 10:44:02 +02:00
Do not needlessly JSON.stringify() the whole editor (#4411)
* Do not needlessly JSON.stringify() the whole editor * Refactor based on feedback * Linting fix for error messages * linting fix Co-authored-by: Dylan Schiemann <dylan@dojotoolkit.org>
This commit is contained in:
@@ -1,7 +1,5 @@
|
|||||||
import React, { useMemo, useState, useCallback, useEffect } from 'react'
|
import React, { useMemo, useState, useCallback, useEffect } from 'react'
|
||||||
import { Editor, Node, Element, Descendant } from 'slate'
|
import { Editor, Node, Element, Descendant } from 'slate'
|
||||||
import invariant from 'tiny-invariant'
|
|
||||||
|
|
||||||
import { ReactEditor } from '../plugin/react-editor'
|
import { ReactEditor } from '../plugin/react-editor'
|
||||||
import { FocusedContext } from '../hooks/use-focused'
|
import { FocusedContext } from '../hooks/use-focused'
|
||||||
import { EditorContext } from '../hooks/use-slate-static'
|
import { EditorContext } from '../hooks/use-slate-static'
|
||||||
@@ -23,17 +21,17 @@ export const Slate = (props: {
|
|||||||
const { editor, children, onChange, value, ...rest } = props
|
const { editor, children, onChange, value, ...rest } = props
|
||||||
const [key, setKey] = useState(0)
|
const [key, setKey] = useState(0)
|
||||||
const context: [ReactEditor] = useMemo(() => {
|
const context: [ReactEditor] = useMemo(() => {
|
||||||
invariant(
|
if (!Node.isNodeList(value)) {
|
||||||
Node.isNodeList(value),
|
throw new Error(
|
||||||
`[Slate] value is invalid! Expected a list of elements but got: ${JSON.stringify(
|
`[Slate] value is invalid! Expected a list of elements` +
|
||||||
value
|
`but got: ${JSON.stringify(value)}`
|
||||||
)}`
|
)
|
||||||
)
|
}
|
||||||
invariant(
|
if (!Editor.isEditor(editor)) {
|
||||||
Editor.isEditor(editor),
|
throw new Error(
|
||||||
`[Slate] editor is invalid! you passed: ${JSON.stringify(editor)}`
|
`[Slate] editor is invalid! you passed:` + `${JSON.stringify(editor)}`
|
||||||
)
|
)
|
||||||
|
}
|
||||||
editor.children = value
|
editor.children = value
|
||||||
Object.assign(editor, rest)
|
Object.assign(editor, rest)
|
||||||
return [editor]
|
return [editor]
|
||||||
|
Reference in New Issue
Block a user