From 6b2e237a1bcf29e0a87c4f69e6d36a883fadc32e Mon Sep 17 00:00:00 2001 From: xuchang Date: Fri, 21 Feb 2025 16:48:16 +0800 Subject: [PATCH] =?UTF-8?q?Revert=20"fix:=20=E5=8E=BB=E9=99=A4TextInputPlu?= =?UTF-8?q?gin.ets=E4=B8=AD=E5=A4=9A=E4=BD=99=E7=9A=84delete=E7=9B=91?= =?UTF-8?q?=E5=90=AC=EF=BC=9B=E8=BF=99=E9=83=A8=E5=88=86=E5=88=A0=E9=99=A4?= =?UTF-8?q?=E9=80=BB=E8=BE=91=EF=BC=8C=E4=BC=9A=E5=9C=A8FlutterView.ets?= =?UTF-8?q?=E7=9A=84onKeyPreIme=E5=81=9A=E5=88=A0=E9=99=A4=E5=AD=97?= =?UTF-8?q?=E7=AC=A6=E7=9A=84=E4=BC=A0=E9=80=92=EF=BC=8C=E5=9C=A8TextInput?= =?UTF-8?q?Channel.ets=E7=9A=84'TextInput.setEditingState'=E5=9B=9E?= =?UTF-8?q?=E8=B0=83=E4=B8=AD=E5=81=9A=E5=AD=97=E7=AC=A6=E4=B8=B2=E6=9B=B4?= =?UTF-8?q?=E6=96=B0=E9=80=BB=E8=BE=91=E3=80=82"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This reverts commit a6c7e25407ffc0d26fb4bc21ff34af26e5ded5d6. Signed-off-by: xuchang --- .../editing/ListenableEditingState.test.ets | 7 +++ .../plugin/editing/ListenableEditingState.ets | 52 +++++++++++++++++++ .../ets/plugin/editing/TextInputPlugin.ets | 26 ++++++++++ 3 files changed, 85 insertions(+) diff --git a/shell/platform/ohos/flutter_embedding/application/src/ohosTest/ets/test/plugin/editing/ListenableEditingState.test.ets b/shell/platform/ohos/flutter_embedding/application/src/ohosTest/ets/test/plugin/editing/ListenableEditingState.test.ets index fa1d6a1641..1a0c51c5b0 100644 --- a/shell/platform/ohos/flutter_embedding/application/src/ohosTest/ets/test/plugin/editing/ListenableEditingState.test.ets +++ b/shell/platform/ohos/flutter_embedding/application/src/ohosTest/ets/test/plugin/editing/ListenableEditingState.test.ets @@ -18,6 +18,13 @@ export default function ListenableEditingStateTest() { expect(editingState.getSelectionStart()).assertEqual(5); expect(editingState.getSelectionEnd()).assertEqual(5); }) + + it('should handle delete event correctly (right)', 0, () => { + editingState.handleDeleteEvent(true, 1); + expect(editingState.getStringCache()).assertEqual('hello'); + expect(editingState.getSelectionStart()).assertEqual(5); + expect(editingState.getSelectionEnd()).assertEqual(5); + }) it('should handle newline event correctly', 0, () => { editingState.handleNewlineEvent(); expect(editingState.getStringCache()).assertEqual('hello\n'); diff --git a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/plugin/editing/ListenableEditingState.ets b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/plugin/editing/ListenableEditingState.ets index 690fdff3a4..1d4e82adce 100644 --- a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/plugin/editing/ListenableEditingState.ets +++ b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/plugin/editing/ListenableEditingState.ets @@ -247,6 +247,58 @@ export class ListenableEditingState { } } + handleDeleteEvent(leftOrRight: boolean, length: number): void { + if (length === 0) { + return; + } + + let start = + this.mSelectionStartCache < this.mSelectionEndCache ? this.mSelectionStartCache : this.mSelectionEndCache; + let end = this.mSelectionStartCache > this.mSelectionEndCache ? this.mSelectionStartCache : this.mSelectionEndCache; + + if (leftOrRight == false) { + //delete left + if (start == 0 && end == 0) { + return; + } + + let unicodeStart = start; + if (start == end) { + for (let i = 0; i < length; i++) { + unicodeStart = FlutterTextUtils.getOffsetBefore(this.mStringCache, unicodeStart); + if (unicodeStart === 0) { + break; + } + } + } + this.replace(unicodeStart, end, "", 0, 0); + this.mSelectionStartCache = unicodeStart; + let tempStr: string = this.mStringCache.slice(0, unicodeStart) + this.mStringCache.slice(end); + this.mStringCache = tempStr; + this.mSelectionEndCache = this.mSelectionStartCache; + } else if (leftOrRight == true) { + //delete right + if (start == this.mStringCache.length) { + return; + } + let unicodeEnd = end; + if (start == end) { + for (let i = 0; i < length; i++) { + unicodeEnd = FlutterTextUtils.getOffsetAfter(this.mStringCache, unicodeEnd); + if (unicodeEnd === this.mStringCache.length) { + break; + } + } + } + this.replace(start, unicodeEnd, "", 0, 0); + this.mSelectionEndCache = start; + let tempStr: string = this.mStringCache.slice(0, start) + (unicodeEnd >= this.mStringCache.length ? "" : this.mStringCache.slice(unicodeEnd)); + this.mStringCache = tempStr; + this.mSelectionStartCache = this.mSelectionEndCache; + } + this.notifyListenersIfNeeded(true, true, false); + } + handleNewlineEvent(): void { // 获取光标所在位置; // 当光标移动前位置小于移动后的位置时,获取光标移动前位置;反之获取移动后位置 diff --git a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/plugin/editing/TextInputPlugin.ets b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/plugin/editing/TextInputPlugin.ets index 4509d270ba..8692e4ee2a 100644 --- a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/plugin/editing/TextInputPlugin.ets +++ b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/plugin/editing/TextInputPlugin.ets @@ -253,6 +253,22 @@ class TextInputMethodHandlerImpl implements TextInputMethodHandler { return; } + try { + this.inputMethodController.on('deleteLeft', this.deleteLeftCallback) + } catch (err) { + Log.e(TextInputMethodHandlerImpl.TAG, "Failed to subscribe deleteLeft:" + JSON.stringify(err)); + this.cancelListenKeyBoardEvent(); + return; + } + + try { + this.inputMethodController.on('deleteRight', this.deleteRightCallback) + } catch (err) { + Log.e(TextInputMethodHandlerImpl.TAG, "Failed to subscribe deleteRight:" + JSON.stringify(err)); + this.cancelListenKeyBoardEvent(); + return; + } + try { this.inputMethodController.on('sendFunctionKey', this.sendFunctionKeyCallback) } catch (err) { @@ -285,6 +301,14 @@ class TextInputMethodHandlerImpl implements TextInputMethodHandler { this.mEditable.handleInsertTextEvent(text); } + private deleteLeftCallback = (length: number) => { + this.mEditable.handleDeleteEvent(false, length); + } + + private deleteRightCallback = (length: number) => { + this.mEditable.handleDeleteEvent(true, length); + } + private sendFunctionKeyCallback = (functionKey: inputMethod.FunctionKey) => { /// 临时规避缺少newline对应枚举类型问题 if (functionKey.enterKeyType == NEWLINE_KEY_TYPE) { @@ -303,6 +327,8 @@ class TextInputMethodHandlerImpl implements TextInputMethodHandler { cancelListenKeyBoardEvent(): void { this.inputMethodController?.off('insertText', this.insertTextCallback); + this.inputMethodController?.off('deleteLeft', this.deleteLeftCallback); + this.inputMethodController?.off('deleteRight', this.deleteRightCallback); this.inputMethodController?.off('sendFunctionKey', this.sendFunctionKeyCallback); this.inputMethodController?.off('sendKeyboardStatus', this.sendKeyboardStatusCallback); this.inputMethodController?.off('selectByRange', this.selectByRangeCallback); -- Gitee