mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-08-18 21:21:21 +02:00
Differentiate between useSlate and useEditor hooks (#3941)
* Rename useEditor function to useSlateStatic * Expose useEditor hook with deprecated flag * Clarify useEditor deprecation in file and in docs
This commit is contained in:
@@ -34,10 +34,6 @@ A wrapper around the provider to handle `onChange` events, because the editor is
|
|||||||
|
|
||||||
React hooks for Slate editors
|
React hooks for Slate editors
|
||||||
|
|
||||||
###### `useEditor`
|
|
||||||
|
|
||||||
Get the current editor object from the React context.
|
|
||||||
|
|
||||||
###### `useFocused`
|
###### `useFocused`
|
||||||
|
|
||||||
Get the current `focused` state of the editor.
|
Get the current `focused` state of the editor.
|
||||||
@@ -52,7 +48,11 @@ Get the current `selected` state of an element.
|
|||||||
|
|
||||||
###### `useSlate`
|
###### `useSlate`
|
||||||
|
|
||||||
Get the current editor object from the React context.
|
Get the current editor object from the React context. Re-renders the context whenever changes occur in the editor.
|
||||||
|
|
||||||
|
###### `useSlateStatic`
|
||||||
|
|
||||||
|
Get the current editor object from the React context. A version of useSlate that does not re-render the context. Previously called `useEditor`.
|
||||||
|
|
||||||
## ReactEditor
|
## ReactEditor
|
||||||
|
|
||||||
@@ -137,4 +137,3 @@ Adds React and DOM specific behaviors to the editor.
|
|||||||
## Utils
|
## Utils
|
||||||
|
|
||||||
Private convenience modules
|
Private convenience modules
|
||||||
|
|
||||||
|
@@ -4,7 +4,7 @@ import { Editor, Range, Element, NodeEntry, Ancestor, Descendant } from 'slate'
|
|||||||
import ElementComponent from './element'
|
import ElementComponent from './element'
|
||||||
import TextComponent from './text'
|
import TextComponent from './text'
|
||||||
import { ReactEditor } from '..'
|
import { ReactEditor } from '..'
|
||||||
import { useEditor } from '../hooks/use-editor'
|
import { useSlateStatic } from '../hooks/use-slate-static'
|
||||||
import { NODE_TO_INDEX, NODE_TO_PARENT } from '../utils/weak-maps'
|
import { NODE_TO_INDEX, NODE_TO_PARENT } from '../utils/weak-maps'
|
||||||
import { RenderElementProps, RenderLeafProps } from './editable'
|
import { RenderElementProps, RenderLeafProps } from './editable'
|
||||||
|
|
||||||
@@ -28,7 +28,7 @@ const Children = (props: {
|
|||||||
renderLeaf,
|
renderLeaf,
|
||||||
selection,
|
selection,
|
||||||
} = props
|
} = props
|
||||||
const editor = useEditor()
|
const editor = useSlateStatic()
|
||||||
const path = ReactEditor.findPath(editor, node)
|
const path = ReactEditor.findPath(editor, node)
|
||||||
const children = []
|
const children = []
|
||||||
const isLeafBlock =
|
const isLeafBlock =
|
||||||
|
@@ -4,7 +4,7 @@ import { Editor, Node, Range, NodeEntry, Element as SlateElement } from 'slate'
|
|||||||
|
|
||||||
import Text from './text'
|
import Text from './text'
|
||||||
import Children from './children'
|
import Children from './children'
|
||||||
import { ReactEditor, useEditor, useReadOnly } from '..'
|
import { ReactEditor, useSlateStatic, useReadOnly } from '..'
|
||||||
import { SelectedContext } from '../hooks/use-selected'
|
import { SelectedContext } from '../hooks/use-selected'
|
||||||
import { useIsomorphicLayoutEffect } from '../hooks/use-isomorphic-layout-effect'
|
import { useIsomorphicLayoutEffect } from '../hooks/use-isomorphic-layout-effect'
|
||||||
import {
|
import {
|
||||||
@@ -37,7 +37,7 @@ const Element = (props: {
|
|||||||
selection,
|
selection,
|
||||||
} = props
|
} = props
|
||||||
const ref = useRef<HTMLElement>(null)
|
const ref = useRef<HTMLElement>(null)
|
||||||
const editor = useEditor()
|
const editor = useSlateStatic()
|
||||||
const readOnly = useReadOnly()
|
const readOnly = useReadOnly()
|
||||||
const isInline = editor.isInline(element)
|
const isInline = editor.isInline(element)
|
||||||
const key = ReactEditor.findKey(editor, element)
|
const key = ReactEditor.findKey(editor, element)
|
||||||
@@ -150,7 +150,7 @@ const MemoizedElement = React.memo(Element, (prev, next) => {
|
|||||||
|
|
||||||
export const DefaultElement = (props: RenderElementProps) => {
|
export const DefaultElement = (props: RenderElementProps) => {
|
||||||
const { attributes, children, element } = props
|
const { attributes, children, element } = props
|
||||||
const editor = useEditor()
|
const editor = useSlateStatic()
|
||||||
const Tag = editor.isInline(element) ? 'span' : 'div'
|
const Tag = editor.isInline(element) ? 'span' : 'div'
|
||||||
return (
|
return (
|
||||||
<Tag {...attributes} style={{ position: 'relative' }}>
|
<Tag {...attributes} style={{ position: 'relative' }}>
|
||||||
|
@@ -3,7 +3,7 @@ import { Node } from 'slate'
|
|||||||
|
|
||||||
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-editor'
|
import { EditorContext } from '../hooks/use-slate-static'
|
||||||
import { SlateContext } from '../hooks/use-slate'
|
import { SlateContext } from '../hooks/use-slate'
|
||||||
import { EDITOR_TO_ON_CHANGE } from '../utils/weak-maps'
|
import { EDITOR_TO_ON_CHANGE } from '../utils/weak-maps'
|
||||||
|
|
||||||
|
@@ -1,7 +1,7 @@
|
|||||||
import React from 'react'
|
import React from 'react'
|
||||||
import { Editor, Text, Path, Element, Node } from 'slate'
|
import { Editor, Text, Path, Element, Node } from 'slate'
|
||||||
|
|
||||||
import { ReactEditor, useEditor } from '..'
|
import { ReactEditor, useSlateStatic } from '..'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Leaf content strings.
|
* Leaf content strings.
|
||||||
@@ -14,7 +14,7 @@ const String = (props: {
|
|||||||
text: Text
|
text: Text
|
||||||
}) => {
|
}) => {
|
||||||
const { isLast, leaf, parent, text } = props
|
const { isLast, leaf, parent, text } = props
|
||||||
const editor = useEditor()
|
const editor = useSlateStatic()
|
||||||
const path = ReactEditor.findPath(editor, text)
|
const path = ReactEditor.findPath(editor, text)
|
||||||
const parentPath = Path.parent(path)
|
const parentPath = Path.parent(path)
|
||||||
|
|
||||||
|
@@ -2,7 +2,7 @@ import React, { useRef } from 'react'
|
|||||||
import { Range, Element, Text as SlateText } from 'slate'
|
import { Range, Element, Text as SlateText } from 'slate'
|
||||||
|
|
||||||
import Leaf from './leaf'
|
import Leaf from './leaf'
|
||||||
import { ReactEditor, useEditor } from '..'
|
import { ReactEditor, useSlateStatic } from '..'
|
||||||
import { RenderLeafProps } from './editable'
|
import { RenderLeafProps } from './editable'
|
||||||
import { useIsomorphicLayoutEffect } from '../hooks/use-isomorphic-layout-effect'
|
import { useIsomorphicLayoutEffect } from '../hooks/use-isomorphic-layout-effect'
|
||||||
import {
|
import {
|
||||||
@@ -23,7 +23,7 @@ const Text = (props: {
|
|||||||
text: SlateText
|
text: SlateText
|
||||||
}) => {
|
}) => {
|
||||||
const { decorations, isLast, parent, renderLeaf, text } = props
|
const { decorations, isLast, parent, renderLeaf, text } = props
|
||||||
const editor = useEditor()
|
const editor = useSlateStatic()
|
||||||
const ref = useRef<HTMLSpanElement>(null)
|
const ref = useRef<HTMLSpanElement>(null)
|
||||||
const leaves = SlateText.decorations(text, decorations)
|
const leaves = SlateText.decorations(text, decorations)
|
||||||
const key = ReactEditor.findKey(editor, text)
|
const key = ReactEditor.findKey(editor, text)
|
||||||
|
@@ -4,6 +4,7 @@ import { ReactEditor } from '../plugin/react-editor'
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* A React context for sharing the editor object.
|
* A React context for sharing the editor object.
|
||||||
|
* @deprecated Use useSlateStatic instead.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export const EditorContext = createContext<ReactEditor | null>(null)
|
export const EditorContext = createContext<ReactEditor | null>(null)
|
||||||
|
25
packages/slate-react/src/hooks/use-slate-static.tsx
Normal file
25
packages/slate-react/src/hooks/use-slate-static.tsx
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
import { createContext, useContext } from 'react'
|
||||||
|
|
||||||
|
import { ReactEditor } from '../plugin/react-editor'
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A React context for sharing the editor object.
|
||||||
|
*/
|
||||||
|
|
||||||
|
export const EditorContext = createContext<ReactEditor | null>(null)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the current editor object from the React context.
|
||||||
|
*/
|
||||||
|
|
||||||
|
export const useSlateStatic = () => {
|
||||||
|
const editor = useContext(EditorContext)
|
||||||
|
|
||||||
|
if (!editor) {
|
||||||
|
throw new Error(
|
||||||
|
`The \`useSlateStatic\` hook must be used inside the <Slate> component's context.`
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
return editor
|
||||||
|
}
|
@@ -9,7 +9,7 @@ export { DefaultLeaf } from './components/leaf'
|
|||||||
export { Slate } from './components/slate'
|
export { Slate } from './components/slate'
|
||||||
|
|
||||||
// Hooks
|
// Hooks
|
||||||
export { useEditor } from './hooks/use-editor'
|
export { useSlateStatic } from './hooks/use-slate-static'
|
||||||
export { useFocused } from './hooks/use-focused'
|
export { useFocused } from './hooks/use-focused'
|
||||||
export { useReadOnly } from './hooks/use-read-only'
|
export { useReadOnly } from './hooks/use-read-only'
|
||||||
export { useSelected } from './hooks/use-selected'
|
export { useSelected } from './hooks/use-selected'
|
||||||
|
@@ -3,7 +3,7 @@ import {
|
|||||||
Slate,
|
Slate,
|
||||||
Editable,
|
Editable,
|
||||||
withReact,
|
withReact,
|
||||||
useEditor,
|
useSlateStatic,
|
||||||
useReadOnly,
|
useReadOnly,
|
||||||
ReactEditor,
|
ReactEditor,
|
||||||
} from 'slate-react'
|
} from 'slate-react'
|
||||||
@@ -75,7 +75,7 @@ const Element = props => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const CheckListItemElement = ({ attributes, children, element }) => {
|
const CheckListItemElement = ({ attributes, children, element }) => {
|
||||||
const editor = useEditor()
|
const editor = useSlateStatic()
|
||||||
const readOnly = useReadOnly()
|
const readOnly = useReadOnly()
|
||||||
const { checked } = element
|
const { checked } = element
|
||||||
return (
|
return (
|
||||||
|
@@ -1,6 +1,6 @@
|
|||||||
import React, { useState, useMemo } from 'react'
|
import React, { useState, useMemo } from 'react'
|
||||||
import { Transforms, createEditor, Node } from 'slate'
|
import { Transforms, createEditor, Node } from 'slate'
|
||||||
import { Slate, Editable, useEditor, withReact } from 'slate-react'
|
import { Slate, Editable, useSlateStatic, withReact } from 'slate-react'
|
||||||
import { withHistory } from 'slate-history'
|
import { withHistory } from 'slate-history'
|
||||||
import { css } from 'emotion'
|
import { css } from 'emotion'
|
||||||
|
|
||||||
@@ -114,7 +114,7 @@ const EditableVoidElement = ({ attributes, children, element }) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const InsertEditableVoidButton = () => {
|
const InsertEditableVoidButton = () => {
|
||||||
const editor = useEditor()
|
const editor = useSlateStatic()
|
||||||
return (
|
return (
|
||||||
<Button
|
<Button
|
||||||
onMouseDown={event => {
|
onMouseDown={event => {
|
||||||
|
@@ -4,7 +4,7 @@ import {
|
|||||||
Slate,
|
Slate,
|
||||||
Editable,
|
Editable,
|
||||||
withReact,
|
withReact,
|
||||||
useEditor,
|
useSlateStatic,
|
||||||
ReactEditor,
|
ReactEditor,
|
||||||
useFocused,
|
useFocused,
|
||||||
useSelected,
|
useSelected,
|
||||||
@@ -40,7 +40,7 @@ const Element = props => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const VideoElement = ({ attributes, children, element }) => {
|
const VideoElement = ({ attributes, children, element }) => {
|
||||||
const editor = useEditor()
|
const editor = useSlateStatic()
|
||||||
const { url } = element
|
const { url } = element
|
||||||
return (
|
return (
|
||||||
<div {...attributes}>
|
<div {...attributes}>
|
||||||
|
@@ -5,7 +5,7 @@ import { Node, Transforms, createEditor } from 'slate'
|
|||||||
import {
|
import {
|
||||||
Slate,
|
Slate,
|
||||||
Editable,
|
Editable,
|
||||||
useEditor,
|
useSlateStatic,
|
||||||
useSelected,
|
useSelected,
|
||||||
useFocused,
|
useFocused,
|
||||||
withReact,
|
withReact,
|
||||||
@@ -109,7 +109,7 @@ const ImageElement = ({ attributes, children, element }) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const InsertImageButton = () => {
|
const InsertImageButton = () => {
|
||||||
const editor = useEditor()
|
const editor = useSlateStatic()
|
||||||
return (
|
return (
|
||||||
<Button
|
<Button
|
||||||
onMouseDown={event => {
|
onMouseDown={event => {
|
||||||
|
Reference in New Issue
Block a user