Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): 修复老浏览器keypress事件无法触发 导致中文符号不正确的问题 #6007

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/core/src/text-area/event-handlers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
} from './composition'
import handleOnKeydown from './keydown'
import handleKeypress from './keypress'
import handleInput from './input'
import handleOnCopy from './copy'
import handleOnCut from './cut'
import handleOnPaste from './paste'
Expand All @@ -30,6 +31,7 @@ const eventConf = {
compositionupdate: handleCompositionUpdate,
keydown: handleOnKeydown,
keypress: handleKeypress,
input: handleInput,
copy: handleOnCopy,
cut: handleOnCut,
paste: handleOnPaste,
Expand Down
32 changes: 32 additions & 0 deletions packages/core/src/text-area/event-handlers/input.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* @description 监听 keypress 事件
* @author wangfupeng
*/

import { Editor } from 'slate'
import { IDomEditor } from '../../editor/interface'
import TextArea from '../TextArea'
import { HAS_BEFORE_INPUT_SUPPORT } from '../../utils/ua'
import { hasEditableTarget } from '../helpers'

//

function handleInput(event: Event, textarea: TextArea, editor: IDomEditor) {
// 这里是兼容不完全支持 beforeInput 的浏览器。对于支持 beforeInput 的浏览器,会用 beforeinput 事件处理
if (HAS_BEFORE_INPUT_SUPPORT) return

if (textarea.isComposing) return

const { readOnly } = editor.getConfig()
if (readOnly) return
if (!hasEditableTarget(editor, event.target)) return

event.preventDefault()

const text = (event as any).data as string

// 这里只兼容 beforeInput 的 insertText 类型,其他的(如删除、换行)使用 keydown 来兼容
Editor.insertText(editor, text)
}

export default handleInput