1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-08-11 17:53:59 +02:00

feat: add useComposing hook (#5695)

This commit is contained in:
yf-yang
2024-08-21 07:03:03 +08:00
committed by GitHub
parent e5fed793e7
commit 6cb38e37a4
4 changed files with 854 additions and 813 deletions

View File

@@ -0,0 +1,5 @@
---
'slate-react': minor
---
feat: Add useComposing hook"

View File

@@ -8,6 +8,10 @@
React hooks for Slate editors React hooks for Slate editors
#### `useComposing(): boolean`
Get the current `composing` state of the editor. It deals with `compositionstart`, `compositionupdate`, `compositionend` events.
#### `useFocused(): boolean` #### `useFocused(): boolean`
Get the current `focused` state of the editor. Get the current `focused` state of the editor.

View File

@@ -73,6 +73,7 @@ import {
} from '../utils/weak-maps' } from '../utils/weak-maps'
import { RestoreDOM } from './restore-dom/restore-dom' import { RestoreDOM } from './restore-dom/restore-dom'
import { AndroidInputManager } from '../hooks/android-input-manager/android-input-manager' import { AndroidInputManager } from '../hooks/android-input-manager/android-input-manager'
import { ComposingContext } from '../hooks/use-composing'
type DeferredOperation = () => void type DeferredOperation = () => void
@@ -949,6 +950,7 @@ export const Editable = forwardRef(
return ( return (
<ReadOnlyContext.Provider value={readOnly}> <ReadOnlyContext.Provider value={readOnly}>
<ComposingContext.Provider value={isComposing}>
<DecorateContext.Provider value={decorate}> <DecorateContext.Provider value={decorate}>
<RestoreDOM node={ref} receivedUserInput={receivedUserInput}> <RestoreDOM node={ref} receivedUserInput={receivedUserInput}>
<Component <Component
@@ -1092,7 +1094,10 @@ export const Editable = forwardRef(
isDOMNode(relatedTarget) && isDOMNode(relatedTarget) &&
ReactEditor.hasDOMNode(editor, relatedTarget) ReactEditor.hasDOMNode(editor, relatedTarget)
) { ) {
const node = ReactEditor.toSlateNode(editor, relatedTarget) const node = ReactEditor.toSlateNode(
editor,
relatedTarget
)
if (Element.isElement(node) && !editor.isVoid(node)) { if (Element.isElement(node) && !editor.isVoid(node)) {
return return
@@ -1191,7 +1196,9 @@ export const Editable = forwardRef(
}) })
} }
androidInputManagerRef.current?.handleCompositionEnd(event) androidInputManagerRef.current?.handleCompositionEnd(
event
)
if ( if (
isEventHandled(event, attributes.onCompositionEnd) || isEventHandled(event, attributes.onCompositionEnd) ||
@@ -1310,7 +1317,10 @@ export const Editable = forwardRef(
if (Range.isExpanded(selection)) { if (Range.isExpanded(selection)) {
Editor.deleteFragment(editor) Editor.deleteFragment(editor)
} else { } else {
const node = Node.parent(editor, selection.anchor.path) const node = Node.parent(
editor,
selection.anchor.path
)
if (Editor.isVoid(editor, node)) { if (Editor.isVoid(editor, node)) {
Transforms.delete(editor) Transforms.delete(editor)
} }
@@ -1585,7 +1595,10 @@ export const Editable = forwardRef(
Transforms.collapse(editor, { edge: 'focus' }) Transforms.collapse(editor, { edge: 'focus' })
} }
Transforms.move(editor, { unit: 'word', reverse: !isRTL }) Transforms.move(editor, {
unit: 'word',
reverse: !isRTL,
})
return return
} }
@@ -1596,7 +1609,10 @@ export const Editable = forwardRef(
Transforms.collapse(editor, { edge: 'focus' }) Transforms.collapse(editor, { edge: 'focus' })
} }
Transforms.move(editor, { unit: 'word', reverse: isRTL }) Transforms.move(editor, {
unit: 'word',
reverse: isRTL,
})
return return
} }
@@ -1781,6 +1797,7 @@ export const Editable = forwardRef(
</Component> </Component>
</RestoreDOM> </RestoreDOM>
</DecorateContext.Provider> </DecorateContext.Provider>
</ComposingContext.Provider>
</ReadOnlyContext.Provider> </ReadOnlyContext.Provider>
) )
} }

View File

@@ -0,0 +1,15 @@
import { createContext, useContext } from 'react'
/**
* A React context for sharing the `composing` state of the editor.
*/
export const ComposingContext = createContext(false)
/**
* Get the current `composing` state of the editor.
*/
export const useComposing = (): boolean => {
return useContext(ComposingContext)
}