1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-07-31 12:30:11 +02:00

fix: hide placeholder when composing (#4352)

This commit is contained in:
Sihong
2021-08-06 02:05:42 +08:00
committed by GitHub
parent bde6e80476
commit 4b373dc290
2 changed files with 13 additions and 2 deletions

View File

@@ -0,0 +1,5 @@
---
'slate-react': patch
---
Do not display placeholder when composing

View File

@@ -1,4 +1,4 @@
import React, { useEffect, useRef, useMemo, useCallback } from 'react' import React, { useEffect, useRef, useMemo, useCallback, useState } from 'react'
import { import {
Editor, Editor,
Element, Element,
@@ -112,6 +112,8 @@ export const Editable = (props: EditableProps) => {
...attributes ...attributes
} = props } = props
const editor = useSlate() const editor = useSlate()
// Rerender editor when composition status changed
const [isComposing, setIsComposing] = useState(false)
const ref = useRef<HTMLDivElement>(null) const ref = useRef<HTMLDivElement>(null)
// Update internal state on each render. // Update internal state on each render.
@@ -370,6 +372,7 @@ export const Editable = (props: EditableProps) => {
// then we will abort because we're still composing and the selection // then we will abort because we're still composing and the selection
// won't be updated properly. // won't be updated properly.
// https://www.w3.org/TR/input-events-2/ // https://www.w3.org/TR/input-events-2/
state.isComposing && setIsComposing(false)
state.isComposing = false state.isComposing = false
} }
@@ -481,7 +484,8 @@ export const Editable = (props: EditableProps) => {
placeholder && placeholder &&
editor.children.length === 1 && editor.children.length === 1 &&
Array.from(Node.texts(editor)).length === 1 && Array.from(Node.texts(editor)).length === 1 &&
Node.string(editor) === '' Node.string(editor) === '' &&
!isComposing
) { ) {
const start = Editor.start(editor, []) const start = Editor.start(editor, [])
decorations.push({ decorations.push({
@@ -648,6 +652,7 @@ export const Editable = (props: EditableProps) => {
hasEditableTarget(editor, event.target) && hasEditableTarget(editor, event.target) &&
!isEventHandled(event, attributes.onCompositionEnd) !isEventHandled(event, attributes.onCompositionEnd)
) { ) {
state.isComposing && setIsComposing(false)
state.isComposing = false state.isComposing = false
// COMPAT: In Chrome, `beforeinput` events for compositions // COMPAT: In Chrome, `beforeinput` events for compositions
@@ -667,6 +672,7 @@ export const Editable = (props: EditableProps) => {
hasEditableTarget(editor, event.target) && hasEditableTarget(editor, event.target) &&
!isEventHandled(event, attributes.onCompositionUpdate) !isEventHandled(event, attributes.onCompositionUpdate)
) { ) {
!state.isComposing && setIsComposing(true)
state.isComposing = true state.isComposing = true
} }
}, },