mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-08-22 15:02:51 +02:00
Capture element selections through useSelected (#4365)
* Capture element selections (ianstormtaylor#4338) * Rebase * Add changeset
This commit is contained in:
5
.changeset/tender-crabs-begin.md
Normal file
5
.changeset/tender-crabs-begin.md
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
'slate-react': patch
|
||||||
|
---
|
||||||
|
|
||||||
|
fix a bug where element selections were not captured by useSelected
|
@@ -5,7 +5,6 @@ import { Editor, Node, Range, NodeEntry, Element as SlateElement } from 'slate'
|
|||||||
import Text from './text'
|
import Text from './text'
|
||||||
import useChildren from '../hooks/use-children'
|
import useChildren from '../hooks/use-children'
|
||||||
import { ReactEditor, useSlateStatic, useReadOnly } from '..'
|
import { ReactEditor, useSlateStatic, useReadOnly } from '..'
|
||||||
import { SelectedContext } from '../hooks/use-selected'
|
|
||||||
import { useIsomorphicLayoutEffect } from '../hooks/use-isomorphic-layout-effect'
|
import { useIsomorphicLayoutEffect } from '../hooks/use-isomorphic-layout-effect'
|
||||||
import {
|
import {
|
||||||
NODE_TO_ELEMENT,
|
NODE_TO_ELEMENT,
|
||||||
@@ -131,11 +130,7 @@ const Element = (props: {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
return (
|
return renderElement({ attributes, children, element })
|
||||||
<SelectedContext.Provider value={!!selection}>
|
|
||||||
{renderElement({ attributes, children, element })}
|
|
||||||
</SelectedContext.Provider>
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const MemoizedElement = React.memo(Element, (prev, next) => {
|
const MemoizedElement = React.memo(Element, (prev, next) => {
|
||||||
|
@@ -12,6 +12,7 @@ import {
|
|||||||
RenderLeafProps,
|
RenderLeafProps,
|
||||||
RenderPlaceholderProps,
|
RenderPlaceholderProps,
|
||||||
} from '../components/editable'
|
} from '../components/editable'
|
||||||
|
import { SelectedContext } from './use-selected'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Children.
|
* Children.
|
||||||
@@ -60,15 +61,17 @@ const useChildren = (props: {
|
|||||||
|
|
||||||
if (Element.isElement(n)) {
|
if (Element.isElement(n)) {
|
||||||
children.push(
|
children.push(
|
||||||
<ElementComponent
|
<SelectedContext.Provider value={!!sel}>
|
||||||
decorations={ds}
|
<ElementComponent
|
||||||
element={n}
|
decorations={ds}
|
||||||
key={key.id}
|
element={n}
|
||||||
renderElement={renderElement}
|
key={key.id}
|
||||||
renderPlaceholder={renderPlaceholder}
|
renderElement={renderElement}
|
||||||
renderLeaf={renderLeaf}
|
renderPlaceholder={renderPlaceholder}
|
||||||
selection={sel}
|
renderLeaf={renderLeaf}
|
||||||
/>
|
selection={sel}
|
||||||
|
/>
|
||||||
|
</SelectedContext.Provider>
|
||||||
)
|
)
|
||||||
} else {
|
} else {
|
||||||
children.push(
|
children.push(
|
||||||
|
Reference in New Issue
Block a user