mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-09-02 19:52:32 +02:00
fix: insert new node in advance to avoid ime double input with editor marks (#4451)
* fix: insert new node in advance to avoid ime double input * chore: add changeset * refactor: minor refator as review
This commit is contained in:
5
.changeset/old-bees-design.md
Normal file
5
.changeset/old-bees-design.md
Normal file
@@ -0,0 +1,5 @@
|
||||
---
|
||||
'slate-react': patch
|
||||
---
|
||||
|
||||
fix IME double input with editor mark
|
@@ -132,6 +132,7 @@ export const Editable = (props: EditableProps) => {
|
||||
const state = useMemo(
|
||||
() => ({
|
||||
isComposing: false,
|
||||
hasInsertPrefixInCompositon: false,
|
||||
isDraggingInternally: false,
|
||||
isUpdatingSelection: false,
|
||||
latestElement: null as DOMElement | null,
|
||||
@@ -724,6 +725,23 @@ export const Editable = (props: EditableProps) => {
|
||||
) {
|
||||
Editor.insertText(editor, event.data)
|
||||
}
|
||||
|
||||
if (editor.selection && Range.isCollapsed(editor.selection)) {
|
||||
const leafPath = editor.selection.anchor.path
|
||||
const currentTextNode = Node.leaf(editor, leafPath)
|
||||
if (state.hasInsertPrefixInCompositon) {
|
||||
state.hasInsertPrefixInCompositon = false
|
||||
Editor.withoutNormalizing(editor, () => {
|
||||
// remove Unicode BOM prefix added in `onCompositionStart`
|
||||
const text = currentTextNode.text.replace(/^\uFEFF/, '')
|
||||
Transforms.delete(editor, {
|
||||
distance: currentTextNode.text.length,
|
||||
reverse: true,
|
||||
})
|
||||
Transforms.insertText(editor, text)
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
[attributes.onCompositionEnd]
|
||||
@@ -746,9 +764,42 @@ export const Editable = (props: EditableProps) => {
|
||||
hasEditableTarget(editor, event.target) &&
|
||||
!isEventHandled(event, attributes.onCompositionStart)
|
||||
) {
|
||||
const { selection } = editor
|
||||
if (selection && Range.isExpanded(selection)) {
|
||||
Editor.deleteFragment(editor)
|
||||
const { selection, marks } = editor
|
||||
if (selection) {
|
||||
if (Range.isExpanded(selection)) {
|
||||
Editor.deleteFragment(editor)
|
||||
return
|
||||
}
|
||||
const inline = Editor.above(editor, {
|
||||
match: n => Editor.isInline(editor, n),
|
||||
mode: 'highest',
|
||||
})
|
||||
if (inline) {
|
||||
const [, inlinePath] = inline
|
||||
if (Editor.isEnd(editor, selection.anchor, inlinePath)) {
|
||||
const point = Editor.after(editor, inlinePath)!
|
||||
Transforms.setSelection(editor, {
|
||||
anchor: point,
|
||||
focus: point,
|
||||
})
|
||||
}
|
||||
}
|
||||
// insert new node in advance to ensure composition text will insert
|
||||
// along with final input text
|
||||
// add Unicode BOM prefix to avoid normalize removing this node
|
||||
if (marks) {
|
||||
state.hasInsertPrefixInCompositon = true
|
||||
Transforms.insertNodes(
|
||||
editor,
|
||||
{
|
||||
text: '\uFEFF',
|
||||
...marks,
|
||||
},
|
||||
{
|
||||
select: true,
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
|
Reference in New Issue
Block a user