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 837253925b36ccecde2d1c0b3c9beda366918781..21d2e3f1039e4e77d8fdf957bcaa6bf8054264a9 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 @@ -171,6 +171,47 @@ export class ListenableEditingState { this.notifyListenersIfNeeded(true, true, false); } + handleInsertEnglishTextEvent(text: string): void { + let start = this.mSelectionStartCache < this.mSelectionEndCache ? this.mSelectionStartCache : this.mSelectionEndCache; + let end = this.mSelectionStartCache > this.mSelectionEndCache ? this.mSelectionStartCache : this.mSelectionEndCache; + const length = text.length; + this.replace(start, end, text, 0, length); + let englishReg = new RegExp("[A-Za-z^']"); + + if (start == 0 || !this.mStringCache.substring(start - 1, start).match(englishReg)) { + let tempStr: string = this.mStringCache.substring(0, start) + text + this.mStringCache.substring(end); + this.mStringCache = tempStr; + this.setSelectionStart(this.mStringCache.length); + this.setSelectionEnd(this.mStringCache.length); + } else { + for (let i = start; i <= start; i--) { + if (this.mStringCache.substring(i - 1, i).match(englishReg)) { + start = i; + } else { + break; + } + } + for (let i = end; i >= start; i++) { + if (this.mStringCache.substring(i, i + 1).match(englishReg)) { + end = i; + } else { + break; + } + } + let tempStr: string = this.mStringCache.substring(0, start - 1) + text + this.mStringCache.substring(end + 1); + let selectionLength: number = start - 1 + text.length; + this.mStringCache = tempStr; + this.setSelectionStart(selectionLength); + this.setSelectionEnd(selectionLength); + } + + if (this.mListeners == null) { + Log.e(TAG, "mListeners is null"); + return; + } + this.notifyListenersIfNeeded(true, true, false); + } + updateTextInputState(state: TextEditState): void { this.beginBatchEdit(); this.setStringCache(state.text); 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 6a6b814e384c54a66d97b1bf244aeeb3cd1f7e43..d859c72a056b280d247676fce09812fb98ffcc91 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 @@ -78,7 +78,6 @@ class TextInputMethodHandlerImpl implements TextInputMethodHandler { mEditable: ListenableEditingState; private mRestartInputPending: boolean = false; private plugin: EditingStateWatcher | Any; - private imcFlag: boolean = false; private inputTypeNone: string = 'NONE' private keyboardStatus: inputMethod.KeyboardStatus = inputMethod.KeyboardStatus.HIDE; private inputAttribute: inputMethod.InputAttribute = { textInputType: 0, enterKeyType: 1 } @@ -133,6 +132,7 @@ class TextInputMethodHandlerImpl implements TextInputMethodHandler { + editingState.selectionEnd + " composingStart:" + editingState.composingStart + " composingEnd" + editingState.composingEnd); this.mEditable.updateTextInputState(editingState); + this.changeSelection(); } clearClient(): void { @@ -144,22 +144,11 @@ class TextInputMethodHandlerImpl implements TextInputMethodHandler { return; } await this.attach(true); - if (!this.imcFlag) { - this.listenKeyBoardEvent(); - } - this.inputMethodController.showTextInput().then(() => { - Log.d(TextInputMethodHandlerImpl.TAG, "Succeeded in showing softKeyboard"); - }).catch((err: Any) => { - Log.e(TextInputMethodHandlerImpl.TAG, "Failed to show softKeyboard:" + JSON.stringify(err)); - }); + this.listenKeyBoardEvent(); } private async hideTextInput(): Promise { - this.inputMethodController.hideTextInput().then(() => { - Log.d(TextInputMethodHandlerImpl.TAG, "Succeeded in hide softKeyboard"); - }).catch((err: Any) => { - Log.e(TextInputMethodHandlerImpl.TAG, "Failed to hide softKeyboard:" + JSON.stringify(err)); - }) + this.inputMethodController.detach(); } async attach(showKeyboard: boolean): Promise { @@ -264,12 +253,16 @@ class TextInputMethodHandlerImpl implements TextInputMethodHandler { return; } Log.d(TextInputMethodHandlerImpl.TAG, "listenKeyBoardEvent success"); - this.imcFlag = true; } private insertTextCallback = (text: string) => { Log.d(TextInputMethodHandlerImpl.TAG, "insertText: " + text); - this.mEditable.handleInsertTextEvent(text); + if (text.length > 1 && text.substring(text.length - 1, text.length == " ") { + this.mEditable.handleInsertEnglishTextEvent(text); + } else { + this.mEditable.handleInsertTextEvent(text); + } + this.changeSelection(); } @@ -287,6 +280,7 @@ class TextInputMethodHandlerImpl implements TextInputMethodHandler { /// 临时规避缺少newline对应枚举类型问题 if (functionKey.enterKeyType == NEWLINE_KEY_TYPE) { this.mEditable.handleNewlineEvent(); + this.changeSelection(); } this.mEditable.handleFunctionKey(functionKey); } @@ -319,8 +313,6 @@ class TextInputMethodHandlerImpl implements TextInputMethodHandler { this.inputMethodController.off('sendFunctionKey', this.sendFunctionKeyCallback); this.inputMethodController.off('sendKeyboardStatus', this.sendKeyboardStatusCallback); this.inputMethodController.off('selectByRange', this.selectByRangeCallback); - this.imcFlag = false; - this.inputMethodController.detach() } public clearTextInputClient(): void {