From 6e2f412911b67d3f4e40feb7f5b9ea2e2e623a7c Mon Sep 17 00:00:00 2001 From: HQQ <316660035@qq.com> Date: Tue, 3 Dec 2024 15:37:19 +0800 Subject: [PATCH] =?UTF-8?q?fix(core):=20=E4=BF=AE=E5=A4=8D=E8=80=81?= =?UTF-8?q?=E6=B5=8F=E8=A7=88=E5=99=A8keypress=E4=BA=8B=E4=BB=B6=E6=97=A0?= =?UTF-8?q?=E6=B3=95=E8=A7=A6=E5=8F=91=20=E5=AF=BC=E8=87=B4=E4=B8=AD?= =?UTF-8?q?=E6=96=87=E7=AC=A6=E5=8F=B7=E4=B8=8D=E6=AD=A3=E7=A1=AE=E7=9A=84?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/text-area/event-handlers/index.ts | 2 ++ .../src/text-area/event-handlers/input.ts | 32 +++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 packages/core/src/text-area/event-handlers/input.ts diff --git a/packages/core/src/text-area/event-handlers/index.ts b/packages/core/src/text-area/event-handlers/index.ts index cc49b8666..16541b356 100644 --- a/packages/core/src/text-area/event-handlers/index.ts +++ b/packages/core/src/text-area/event-handlers/index.ts @@ -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' @@ -30,6 +31,7 @@ const eventConf = { compositionupdate: handleCompositionUpdate, keydown: handleOnKeydown, keypress: handleKeypress, + input: handleInput, copy: handleOnCopy, cut: handleOnCut, paste: handleOnPaste, diff --git a/packages/core/src/text-area/event-handlers/input.ts b/packages/core/src/text-area/event-handlers/input.ts new file mode 100644 index 000000000..6e3027485 --- /dev/null +++ b/packages/core/src/text-area/event-handlers/input.ts @@ -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