@react-native-ohos/react-native-custom-keyboard 1.0.4-rc.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (43) hide show
  1. package/LICENSE +8 -0
  2. package/README.OpenSource +11 -0
  3. package/README.md +13 -0
  4. package/harmony/custom_keyboard/BuildProfile.ets +23 -0
  5. package/harmony/custom_keyboard/Index.ets +7 -0
  6. package/harmony/custom_keyboard/build-profile.json5 +28 -0
  7. package/harmony/custom_keyboard/gitignore +6 -0
  8. package/harmony/custom_keyboard/hvigorfile.ts +12 -0
  9. package/harmony/custom_keyboard/obfuscation-rule.txt +18 -0
  10. package/harmony/custom_keyboard/oh-package.json5 +11 -0
  11. package/harmony/custom_keyboard/src/main/cpp/CMakeLists.txt +9 -0
  12. package/harmony/custom_keyboard/src/main/cpp/CustomKeyboardPackage.cpp +25 -0
  13. package/harmony/custom_keyboard/src/main/cpp/CustomKeyboardPackage.h +18 -0
  14. package/harmony/custom_keyboard/src/main/cpp/CustomKeyboardTurboModuleSpec.cpp +366 -0
  15. package/harmony/custom_keyboard/src/main/cpp/CustomKeyboardTurboModuleSpec.h +46 -0
  16. package/harmony/custom_keyboard/src/main/cpp/generated/custom_keyboard/RNOH/generated/BaseReactNativeOhTplReactNativeCustomKeyboardPackage.h +75 -0
  17. package/harmony/custom_keyboard/src/main/cpp/generated/custom_keyboard/RNOH/generated/turbo_modules/CustomKeyboardNativeModule.cpp +28 -0
  18. package/harmony/custom_keyboard/src/main/cpp/generated/custom_keyboard/RNOH/generated/turbo_modules/CustomKeyboardNativeModule.h +21 -0
  19. package/harmony/custom_keyboard/src/main/cpp/generated/custom_keyboard/react/renderer/components/custom_keyboard/ComponentDescriptors.h +24 -0
  20. package/harmony/custom_keyboard/src/main/cpp/generated/custom_keyboard/react/renderer/components/custom_keyboard/EventEmitters.cpp +16 -0
  21. package/harmony/custom_keyboard/src/main/cpp/generated/custom_keyboard/react/renderer/components/custom_keyboard/EventEmitters.h +17 -0
  22. package/harmony/custom_keyboard/src/main/cpp/generated/custom_keyboard/react/renderer/components/custom_keyboard/Props.cpp +19 -0
  23. package/harmony/custom_keyboard/src/main/cpp/generated/custom_keyboard/react/renderer/components/custom_keyboard/Props.h +18 -0
  24. package/harmony/custom_keyboard/src/main/cpp/generated/custom_keyboard/react/renderer/components/custom_keyboard/ShadowNodes.cpp +17 -0
  25. package/harmony/custom_keyboard/src/main/cpp/generated/custom_keyboard/react/renderer/components/custom_keyboard/ShadowNodes.h +23 -0
  26. package/harmony/custom_keyboard/src/main/cpp/generated/custom_keyboard/react/renderer/components/custom_keyboard/States.cpp +16 -0
  27. package/harmony/custom_keyboard/src/main/cpp/generated/custom_keyboard/react/renderer/components/custom_keyboard/States.h +19 -0
  28. package/harmony/custom_keyboard/src/main/ets/Logger.ts +64 -0
  29. package/harmony/custom_keyboard/src/main/ets/RNCustomKeyboardPackage.ts +47 -0
  30. package/harmony/custom_keyboard/src/main/ets/RNCustomKeyboardTurboModule.ts +64 -0
  31. package/harmony/custom_keyboard/src/main/ets/generated/components/ts.ts +5 -0
  32. package/harmony/custom_keyboard/src/main/ets/generated/index.ets +8 -0
  33. package/harmony/custom_keyboard/src/main/ets/generated/ts.ts +9 -0
  34. package/harmony/custom_keyboard/src/main/ets/generated/turboModules/CustomKeyboardNativeModule.ts +32 -0
  35. package/harmony/custom_keyboard/src/main/ets/generated/turboModules/ts.ts +8 -0
  36. package/harmony/custom_keyboard/src/main/module.json5 +11 -0
  37. package/harmony/custom_keyboard/ts.ts +8 -0
  38. package/harmony/custom_keyboard.har +0 -0
  39. package/img/rntpc_react-native-custom-keyboard.png +0 -0
  40. package/index.d.ts +17 -0
  41. package/index.js +66 -0
  42. package/package.json +44 -0
  43. package/src/specs/v2/NativeCustomKeyboard.ts +21 -0
package/LICENSE ADDED
@@ -0,0 +1,8 @@
1
+ The MIT License (MIT)
2
+ Copyright © 2025 <copyright holders>
3
+
4
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
5
+
6
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
7
+
8
+ THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,11 @@
1
+ [
2
+ {
3
+ "Name": "react-native-custom-keyboard",
4
+ "License": "MIT License",
5
+ "License File": "LICENSE",
6
+ "Version Number": "1.0.3",
7
+ "Owner" : "xiafeng@huawei.com",
8
+ "Upstream URL": "https://github.com/reactnativecn/react-native-custom-keyboard",
9
+ "Description": ""
10
+ }
11
+ ]
package/README.md ADDED
@@ -0,0 +1,13 @@
1
+ # react-native-custom-keyboard
2
+
3
+ 本项目基于 [react-native-custom-keyboard](https://github.com/reactnativecn/react-native-custom-keyboard)
4
+
5
+ ## 文档地址 / Documentation URL
6
+
7
+ - [中文](https://gitee.com/react-native-oh-library/usage-docs/blob/master/zh-cn/react-native-custom-keyboard.md)
8
+
9
+ - [English](https://gitee.com/react-native-oh-library/usage-docs/blob/master/en/react-native-custom-keyboard.md)
10
+
11
+ ## 请悉知 / Acknowledgements
12
+
13
+ 本项目基于 [The MIT License (MIT)](https://www.mit-license.org) ,请自由地享受和参与开源。
@@ -0,0 +1,23 @@
1
+ /*
2
+ * Copyright (c) 2025 Huawei Device Co., Ltd. All rights reserved
3
+ * Use of this source code is governed by a MIT license that can be
4
+ * found in the LICENSE file.
5
+ */
6
+
7
+ /**
8
+ * Use these variables when you tailor your ArkTS code. They must be of the const type.
9
+ */
10
+ export const HAR_VERSION = '1.0.4-rc.1';
11
+ export const BUILD_MODE_NAME = 'debug';
12
+ export const DEBUG = true;
13
+ export const TARGET_NAME = 'default';
14
+
15
+ /**
16
+ * BuildProfile Class is used only for compatibility purposes.
17
+ */
18
+ export default class BuildProfile {
19
+ static readonly HAR_VERSION = HAR_VERSION;
20
+ static readonly BUILD_MODE_NAME = BUILD_MODE_NAME;
21
+ static readonly DEBUG = DEBUG;
22
+ static readonly TARGET_NAME = TARGET_NAME;
23
+ }
@@ -0,0 +1,7 @@
1
+ /*
2
+ * Copyright (c) 2025 Huawei Device Co., Ltd. All rights reserved
3
+ * Use of this source code is governed by a MIT license that can be
4
+ * found in the LICENSE file.
5
+ */
6
+
7
+ export * from "./ts";
@@ -0,0 +1,28 @@
1
+ {
2
+ "apiType": "stageMode",
3
+ "buildOption": {
4
+ },
5
+ "buildOptionSet": [
6
+ {
7
+ "name": "release",
8
+ "arkOptions": {
9
+ "obfuscation": {
10
+ "ruleOptions": {
11
+ "enable": true,
12
+ "files": [
13
+ "./obfuscation-rules.txt"
14
+ ]
15
+ }
16
+ }
17
+ },
18
+ },
19
+ ],
20
+ "targets": [
21
+ {
22
+ "name": "default"
23
+ },
24
+ {
25
+ "name": "ohosTest"
26
+ }
27
+ ]
28
+ }
@@ -0,0 +1,6 @@
1
+ /node_modules
2
+ /oh_modules
3
+ /.preview
4
+ /build
5
+ /.cxx
6
+ /.test
@@ -0,0 +1,12 @@
1
+ /*
2
+ * Copyright (c) 2025 Huawei Device Co., Ltd. All rights reserved
3
+ * Use of this source code is governed by a MIT license that can be
4
+ * found in the LICENSE file.
5
+ */
6
+
7
+ import { harTasks } from '@ohos/hvigor-ohos-plugin';
8
+
9
+ export default {
10
+ system: harTasks, /* Built-in plugin of Hvigor. It cannot be modified. */
11
+ plugins:[] /* Custom plugin to extend the functionality of Hvigor. */
12
+ }
@@ -0,0 +1,18 @@
1
+ # Define project specific obfuscation rules here.
2
+ # You can include the obfuscation configuration files in the current module's build-profile.json5.
3
+ #
4
+ # For more details, see
5
+ # https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/source-obfuscation-V5
6
+
7
+ # Obfuscation options:
8
+ # -disable-obfuscation: disable all obfuscations
9
+ # -enable-property-obfuscation: obfuscate the property names
10
+ # -enable-toplevel-obfuscation: obfuscate the names in the global scope
11
+ # -compact: remove unnecessary blank spaces and all line feeds
12
+ # -remove-log: remove all console.* statements
13
+ # -print-namecache: print the name cache that contains the mapping from the old names to new names
14
+ # -apply-namecache: reuse the given cache file
15
+
16
+ # Keep options:
17
+ # -keep-property-name: specifies property names that you want to keep
18
+ # -keep-global-name: specifies names that you want to keep in the global scope
@@ -0,0 +1,11 @@
1
+ {
2
+ "name": "@react-native-ohos/react-native-custom-keyboard",
3
+ "version": "1.0.4-rc.1",
4
+ "description": "",
5
+ "main": "Index.ets",
6
+ "author": "",
7
+ "license": "Apache-2.0",
8
+ "dependencies": {
9
+ "@rnoh/react-native-openharmony": "file:../react_native_openharmony"
10
+ }
11
+ }
@@ -0,0 +1,9 @@
1
+ cmake_minimum_required(VERSION 3.13)
2
+ set(CMAKE_VERBOSE_MAKEFILE on)
3
+
4
+ set(rnoh_custom_keyboard_package_generated_dir "${CMAKE_CURRENT_SOURCE_DIR}/generated")
5
+ file(GLOB_RECURSE rnoh_custom_keyboard_package_generated_SRC "${rnoh_custom_keyboard_package_generated_dir}/*.cpp")
6
+ file(GLOB rnoh_custom_keyboard_package_SRC CONFIGURE_DEPENDS *.cpp)
7
+ add_library(rnoh_custom_keyboard_package SHARED ${rnoh_custom_keyboard_package_SRC} ${rnoh_custom_keyboard_package_generated_SRC})
8
+ target_include_directories(rnoh_custom_keyboard_package PUBLIC ${CMAKE_CURRENT_SOURCE_DIR} ${rnoh_custom_keyboard_package_generated_dir})
9
+ target_link_libraries(rnoh_custom_keyboard_package PUBLIC rnoh)
@@ -0,0 +1,25 @@
1
+ /*
2
+ * Copyright (c) 2025 Huawei Device Co., Ltd. All rights reserved
3
+ * Use of this source code is governed by a MIT license that can be
4
+ * found in the LICENSE file.
5
+ */
6
+
7
+ #include "CustomKeyboardPackage.h"
8
+ #include "CustomKeyboardTurboModuleSpec.h"
9
+
10
+ using namespace facebook;
11
+ namespace rnoh {
12
+ class CustomKeyboardTurboModuleFactoryDelegate : public TurboModuleFactoryDelegate {
13
+ public:
14
+ SharedTurboModule createTurboModule(Context ctx, const std::string &name) const override {
15
+ if (name == "CustomKeyboardNativeModule") {
16
+ return std::make_shared<NativeCustomKeyboardTurboModuleSpecJSI>(ctx, name);
17
+ }
18
+ return nullptr;
19
+ };
20
+ };
21
+
22
+ std::unique_ptr<TurboModuleFactoryDelegate> CustomKeyboardPackage::createTurboModuleFactoryDelegate() {
23
+ return std::make_unique<CustomKeyboardTurboModuleFactoryDelegate>();
24
+ }
25
+ } // namespace rnoh
@@ -0,0 +1,18 @@
1
+ /*
2
+ * Copyright (c) 2025 Huawei Device Co., Ltd. All rights reserved
3
+ * Use of this source code is governed by a MIT license that can be
4
+ * found in the LICENSE file.
5
+ */
6
+
7
+ #pragma once
8
+ #include "RNOH/Package.h"
9
+
10
+ namespace rnoh {
11
+ class CustomKeyboardPackage : public Package {
12
+
13
+ public:
14
+ CustomKeyboardPackage(Package::Context ctx) : Package(ctx) {}
15
+
16
+ std::unique_ptr<TurboModuleFactoryDelegate> createTurboModuleFactoryDelegate() override;
17
+ };
18
+ } // namespace rnoh
@@ -0,0 +1,366 @@
1
+ /*
2
+ * Copyright (c) 2025 Huawei Device Co., Ltd. All rights reserved
3
+ * Use of this source code is governed by a MIT license that can be
4
+ * found in the LICENSE file.
5
+ */
6
+
7
+ // NOTE: This entire file should be codegen'ed.
8
+ #include "CustomKeyboardTurboModuleSpec.h"
9
+
10
+ using namespace rnoh;
11
+ using namespace facebook;
12
+
13
+ static constexpr double HEIGHT = 216;
14
+ static constexpr int AVOIDENCE = 0;
15
+ static constexpr int START_SURFACE_ID=45;
16
+ static constexpr int CLICK_TARGET_ID=NODE_ON_CLICK+101;
17
+ NativeCustomKeyboardTurboModuleSpecJSI::~NativeCustomKeyboardTurboModuleSpecJSI() {
18
+ for (const UserData *userData : m_userDatas) {
19
+ delete userData;
20
+ userData = nullptr;
21
+ }
22
+ }
23
+ TextInputNode *NativeCustomKeyboardTurboModuleSpecJSI::getEditById(int tag) {
24
+ auto weakInstance = m_ctx.instance;
25
+ auto instance = weakInstance.lock();
26
+ if (!instance) {
27
+ return nullptr;
28
+ }
29
+ auto instanceCapi = std::dynamic_pointer_cast<RNInstanceCAPI>(instance);
30
+ if (!instanceCapi) {
31
+ return nullptr;
32
+ }
33
+ auto componentInstance = instanceCapi->findComponentInstanceByTag(tag);
34
+ if (!componentInstance) {
35
+ return nullptr;
36
+ }
37
+ auto input = std::dynamic_pointer_cast<TextInputComponentInstance>(componentInstance);
38
+ if (!input) {
39
+ return nullptr;
40
+ }
41
+ ArkUINode &node = input->getLocalRootArkUINode();
42
+ TextInputNode *textInputNode = dynamic_cast<TextInputNode *>(&node);
43
+ return textInputNode;
44
+ }
45
+
46
+ static std::shared_ptr<RNInstanceCAPI> getInstanceCapi(std::weak_ptr<RNInstance> weakInstance) {
47
+ auto instance = weakInstance.lock();
48
+ if (!instance) {
49
+ return nullptr;
50
+ }
51
+ auto instanceCapi = std::dynamic_pointer_cast<RNInstanceCAPI>(instance);
52
+ if (!instanceCapi) {
53
+ return nullptr;
54
+ }
55
+ return instanceCapi;
56
+ }
57
+
58
+ static void getDevicePhycicalPixels(std::shared_ptr<RNInstanceCAPI> instanceCapi, double &width, double &height,
59
+ double &scale) {
60
+ auto turboModule = instanceCapi->getTurboModule("DeviceInfo");
61
+ auto deviceInfoTurboModule = std::dynamic_pointer_cast<DeviceInfoTurboModule>(turboModule);
62
+ auto displayMetrics = deviceInfoTurboModule->callSync("getConstants", {});
63
+ if (displayMetrics.count("Dimensions") != 0) {
64
+ auto Dimensions = displayMetrics.at("Dimensions");
65
+ auto screenPhysicalPixels = Dimensions.at("screenPhysicalPixels");
66
+ width = screenPhysicalPixels.at("width").asDouble();
67
+ height = screenPhysicalPixels.at("height").asDouble();
68
+ scale = screenPhysicalPixels.at("scale").asDouble();
69
+ }
70
+ }
71
+
72
+ static void setTextInputUserData(TextInputNode *textInput, UserData *userData) {
73
+ ArkUI_NodeHandle view = textInput->getArkUINodeHandle();
74
+ NativeNodeApi::getInstance()->setUserData(view, userData);
75
+ }
76
+
77
+ static UserData *getTextInputUserData(TextInputNode *textInput) {
78
+ ArkUI_NodeHandle view = textInput->getArkUINodeHandle();
79
+ auto data=(NativeNodeApi::getInstance()->getUserData(view));
80
+ UserData *userData = reinterpret_cast<UserData*>(data);
81
+ return userData;
82
+ }
83
+
84
+ static void getTextSelection(TextInputNode* textInput, int32_t &start, int32_t &end) {
85
+ const ArkUI_AttributeItem* item =
86
+ NativeNodeApi::getInstance()->getAttribute(textInput->getArkUINodeHandle(), NODE_TEXT_INPUT_TEXT_SELECTION);
87
+ start = item->value[0].i32;
88
+ end = item->value[1].i32;
89
+ }
90
+
91
+ static void setCustomKeyboard(TextInputNode *textInput, ArkUI_Node* CustomKeyboard, int32_t const &avoidence){
92
+ ArkUI_NumberValue value = {.i32 = avoidence};
93
+ ArkUI_AttributeItem item{.object=CustomKeyboard,.value=&value,.size=1};
94
+ NativeNodeApi::getInstance()->setAttribute(
95
+ textInput->getArkUINodeHandle(), NODE_TEXT_INPUT_CUSTOM_KEYBOARD, &item);
96
+ }
97
+
98
+ void NativeCustomKeyboardTurboModuleSpecJSI::install(jsi::Runtime &rt, int32_t tag, std::string type) {
99
+ this->m_ctx.taskExecutor->runTask(TaskThread::MAIN, [this, tag, type]() {
100
+ auto edit = getEditById(tag);
101
+ if (!edit) {
102
+ return;
103
+ }
104
+ auto instanceCapi = getInstanceCapi(this->m_ctx.instance);
105
+ if (!instanceCapi) {
106
+ return;
107
+ }
108
+ static int surfaceID = START_SURFACE_ID;
109
+ while(instanceCapi->findComponentInstanceByTag(surfaceID)){surfaceID++;}
110
+ instanceCapi->createSurface(surfaceID, "CustomKeyboard");
111
+ folly::dynamic initialProps = folly::dynamic::object("tag", tag)("type", type);
112
+ double width, height, scale;
113
+ getDevicePhycicalPixels(instanceCapi, width, height, scale);
114
+ instanceCapi->startSurface(surfaceID, 0.0f, 0.0f, width / scale, HEIGHT, 0.0f, 0.0f, scale, false,
115
+ std::move(initialProps));
116
+ auto comp = instanceCapi->findComponentInstanceByTag(surfaceID);
117
+ if (!comp) {
118
+ return;
119
+ }
120
+ ArkUINode &node = comp->getLocalRootArkUINode();
121
+ ArkUI_NodeHandle uiNode = node.getArkUINodeHandle();
122
+ //空值 说明edit没有绑定点击事件
123
+ if(!getTextInputUserData(edit)) {
124
+ NativeNodeApi::getInstance()->registerNodeEvent(
125
+ edit->getArkUINodeHandle(), NODE_ON_CLICK, CLICK_TARGET_ID, edit->getArkUINodeHandle());
126
+ auto callback = [](ArkUI_NodeEvent *event) {
127
+ if (OH_ArkUI_NodeEvent_GetTargetId(event) == CLICK_TARGET_ID) {
128
+ //get textInputNode and userData
129
+ ArkUI_NodeHandle textNode=OH_ArkUI_NodeEvent_GetNodeHandle(event);
130
+ void* data=(NativeNodeApi::getInstance()->getUserData(textNode));
131
+ UserData *userData = reinterpret_cast<UserData*>(data);
132
+ if (userData && userData->data && userData->enabled == false) {
133
+ userData->enabled = true;
134
+ //set customkeyboard
135
+ ArkUI_NumberValue value = {.i32 = AVOIDENCE};
136
+ ArkUI_AttributeItem item{.object=userData->data,.value=&value,.size=1};
137
+ NativeNodeApi::getInstance()->setAttribute(
138
+ textNode, NODE_TEXT_INPUT_CUSTOM_KEYBOARD, &item);
139
+ }
140
+ }
141
+ };
142
+ NativeNodeApi::getInstance()->addNodeEventReceiver(edit->getArkUINodeHandle(),callback);
143
+ }
144
+
145
+ setCustomKeyboard(edit, uiNode, AVOIDENCE);
146
+ UserData *userData = new UserData({.data = uiNode, .enabled = true});
147
+ this->m_userDatas.insert(userData);
148
+ setTextInputUserData(edit, userData);
149
+ });
150
+ }
151
+
152
+ void NativeCustomKeyboardTurboModuleSpecJSI::uninstall(jsi::Runtime &rt, int32_t tag) {
153
+ this->m_ctx.taskExecutor->runTask(TaskThread::MAIN, [this, tag]() {
154
+ auto instanceCapi = getInstanceCapi(this->m_ctx.instance);
155
+ if (!instanceCapi) {
156
+ return;
157
+ }
158
+ auto edit = getEditById(tag);
159
+ edit->setFocusStatus(false);
160
+ auto error_code =
161
+ NativeNodeApi::getInstance()->resetAttribute(edit->getArkUINodeHandle(), NODE_TEXT_INPUT_CUSTOM_KEYBOARD);
162
+ if(error_code){
163
+ DLOG(ERROR)<<"uninstall custom keyboard error"<<std::endl;
164
+ return;
165
+ }
166
+ edit->setFocusStatus(true);
167
+ UserData *userData = getTextInputUserData(edit);
168
+ userData->data=nullptr;
169
+ });
170
+ }
171
+
172
+ void NativeCustomKeyboardTurboModuleSpecJSI::insertText(jsi::Runtime &rt, int32_t tag, std::string text) {
173
+ this->m_ctx.taskExecutor->runTask(TaskThread::MAIN, [this, tag, text]() {
174
+ TextInputNode *textInputNode = this->getEditById(tag);
175
+ int start = 0;
176
+ int end = 0;
177
+ getTextSelection(textInputNode, start, end);
178
+ std::string content = textInputNode->getTextContent();
179
+ content.replace(start, end - start, text);
180
+ textInputNode->setTextContent(content);
181
+ textInputNode->setTextSelection(start+text.size(), start+text.size());
182
+ });
183
+ }
184
+ void NativeCustomKeyboardTurboModuleSpecJSI::backSpace(jsi::Runtime &rt, int32_t tag) {
185
+ this->m_ctx.taskExecutor->runTask(TaskThread::MAIN, [this, tag]() {
186
+ TextInputNode *textInputNode = this->getEditById(tag);
187
+ int start = 0;
188
+ int end = 0;
189
+ getTextSelection(textInputNode, start, end);
190
+ std::string content = textInputNode->getTextContent();
191
+ if (start != end) {
192
+ content.replace(start, end - start, "");
193
+ textInputNode->setTextContent(content);
194
+ textInputNode->setTextSelection(start, start);
195
+ } else if (start != 0) {
196
+ content.erase(content.cbegin() + start - 1);
197
+ textInputNode->setTextContent(content);
198
+ textInputNode->setTextSelection(start - 1, start - 1);
199
+ }
200
+ });
201
+ }
202
+ void NativeCustomKeyboardTurboModuleSpecJSI::doDelete(jsi::Runtime &rt, int32_t tag) {
203
+ this->m_ctx.taskExecutor->runTask(TaskThread::MAIN, [this, tag]() {
204
+ TextInputNode *textInputNode = this->getEditById(tag);
205
+ int start = 0;
206
+ int end = 0;
207
+ getTextSelection(textInputNode, start, end);
208
+ std::string content = textInputNode->getTextContent();
209
+ if (start != end) {
210
+ content.replace(start, end - start, "");
211
+ textInputNode->setTextContent(content);
212
+ textInputNode->setTextSelection(start, start);
213
+ } else if (end != content.size()) {
214
+ content.erase(content.cbegin() + end);
215
+ textInputNode->setTextContent(content);
216
+ textInputNode->setTextSelection(end, end);
217
+ }
218
+ });
219
+ }
220
+ void NativeCustomKeyboardTurboModuleSpecJSI::moveLeft(jsi::Runtime &rt, int32_t tag) {
221
+ this->m_ctx.taskExecutor->runTask(TaskThread::MAIN, [this, tag]() {
222
+ TextInputNode *textInputNode = this->getEditById(tag);
223
+ int start = 0;
224
+ int end = 0;
225
+ getTextSelection(textInputNode, start, end);
226
+ if (start != end) {
227
+ textInputNode->setTextSelection(start, start);
228
+ } else if (start != 0) {
229
+ textInputNode->setTextSelection(start - 1, start - 1);
230
+ }
231
+ });
232
+ }
233
+ void NativeCustomKeyboardTurboModuleSpecJSI::moveRight(jsi::Runtime &rt, int32_t tag) {
234
+ this->m_ctx.taskExecutor->runTask(TaskThread::MAIN, [this, tag]() {
235
+ TextInputNode *textInputNode = this->getEditById(tag);
236
+ int start = 0;
237
+ int end = 0;
238
+ getTextSelection(textInputNode, start, end);
239
+ std::string content = textInputNode->getTextContent();
240
+ if (start != end) {
241
+ textInputNode->setTextSelection(end, end);
242
+ } else if (end != content.size()) {
243
+ textInputNode->setTextSelection(end + 1, end + 1);
244
+ }
245
+ });
246
+ }
247
+
248
+ void NativeCustomKeyboardTurboModuleSpecJSI::switchSystemKeyboard(jsi::Runtime &rt, int32_t tag) {
249
+ this->m_ctx.taskExecutor->runTask(TaskThread::MAIN, [this, tag]() {
250
+ auto instanceCapi = getInstanceCapi(this->m_ctx.instance);
251
+ if (!instanceCapi) {
252
+ return;
253
+ }
254
+ auto edit = getEditById(tag);
255
+ edit->setFocusStatus(false);
256
+ auto error_code =
257
+ NativeNodeApi::getInstance()->resetAttribute(edit->getArkUINodeHandle(), NODE_TEXT_INPUT_CUSTOM_KEYBOARD);
258
+ if(error_code){
259
+ DLOG(ERROR)<<"switch system keyboard error"<<std::endl;
260
+ return;
261
+ }
262
+ edit->setFocusStatus(true);
263
+ UserData *userData = getTextInputUserData(edit);
264
+ userData->enabled = false;
265
+ });
266
+ }
267
+
268
+ // 各个方法的宿主函数定义
269
+ static jsi::Value __hostFunction_NativeCustomKeyboardTurboModuleSpecJSI_install(jsi::Runtime &rt,
270
+ react::TurboModule &turboModule,
271
+ const jsi::Value *args, size_t count) {
272
+ static_cast<NativeCustomKeyboardTurboModuleSpecJSI *>(&turboModule)
273
+ ->install(rt, args[0].asNumber(), args[1].asString(rt).utf8(rt));
274
+ return jsi::Value::undefined();
275
+ }
276
+
277
+ static jsi::Value __hostFunction_NativeCustomKeyboardTurboModuleSpecJSI_uninstall(jsi::Runtime &rt,
278
+ react::TurboModule &turboModule,
279
+ const jsi::Value *args,
280
+ size_t count) {
281
+ static_cast<NativeCustomKeyboardTurboModuleSpecJSI *>(&turboModule)->uninstall(rt, args[0].asNumber());
282
+ return jsi::Value::undefined();
283
+ }
284
+
285
+ static jsi::Value __hostFunction_NativeCustomKeyboardTurboModuleSpecJSI_insertText(jsi::Runtime &rt,
286
+ react::TurboModule &turboModule,
287
+ const jsi::Value *args,
288
+ size_t count) {
289
+ static_cast<NativeCustomKeyboardTurboModuleSpecJSI *>(&turboModule)
290
+ ->insertText(rt, args[0].asNumber(), args[1].asString(rt).utf8(rt));
291
+ return jsi::Value::undefined();
292
+ }
293
+
294
+ static jsi::Value __hostFunction_NativeCustomKeyboardTurboModuleSpecJSI_backSpace(jsi::Runtime &rt,
295
+ react::TurboModule &turboModule,
296
+ const jsi::Value *args,
297
+ size_t count) {
298
+ static_cast<NativeCustomKeyboardTurboModuleSpecJSI *>(&turboModule)->backSpace(rt, args[0].asNumber());
299
+ return jsi::Value::undefined();
300
+ }
301
+
302
+ static jsi::Value __hostFunction_NativeCustomKeyboardTurboModuleSpecJSI_doDelete(jsi::Runtime &rt,
303
+ react::TurboModule &turboModule,
304
+ const jsi::Value *args, size_t count) {
305
+ static_cast<NativeCustomKeyboardTurboModuleSpecJSI *>(&turboModule)->doDelete(rt, args[0].asNumber());
306
+ return jsi::Value::undefined();
307
+ }
308
+
309
+ static jsi::Value __hostFunction_NativeCustomKeyboardTurboModuleSpecJSI_moveLeft(jsi::Runtime &rt,
310
+ react::TurboModule &turboModule,
311
+ const jsi::Value *args, size_t count) {
312
+ static_cast<NativeCustomKeyboardTurboModuleSpecJSI *>(&turboModule)->moveLeft(rt, args[0].asNumber());
313
+ return jsi::Value::undefined();
314
+ }
315
+
316
+ static jsi::Value __hostFunction_NativeCustomKeyboardTurboModuleSpecJSI_moveRight(jsi::Runtime &rt,
317
+ react::TurboModule &turboModule,
318
+ const jsi::Value *args,
319
+ size_t count) {
320
+ static_cast<NativeCustomKeyboardTurboModuleSpecJSI *>(&turboModule)->moveRight(rt, args[0].asNumber());
321
+ return jsi::Value::undefined();
322
+ }
323
+
324
+ static jsi::Value __hostFunction_NativeCustomKeyboardTurboModuleSpecJSI_switchSystemKeyboard(
325
+ jsi::Runtime &rt, react::TurboModule &turboModule, const jsi::Value *args, size_t count) {
326
+ static_cast<NativeCustomKeyboardTurboModuleSpecJSI *>(&turboModule)->switchSystemKeyboard(rt, args[0].asNumber());
327
+ return jsi::Value::undefined();
328
+ }
329
+
330
+ static jsi::Value __hostFunction_NativeCustomKeyboardTurboModuleSpecJSI_displayRNOHError(
331
+ jsi::Runtime &rt, react::TurboModule &turboModule, const jsi::Value *args, size_t count) {
332
+ return jsi::Value(static_cast<ArkTSTurboModule &>(turboModule).call(rt, "displayRNOHError", args, count));
333
+ }
334
+
335
+ static jsi::Value __hostFunction_NativeCustomKeyboardTurboModuleSpecJSI_throwExceptionCpp(
336
+ jsi::Runtime &rt, react::TurboModule &turboModule, const jsi::Value *args, size_t count) {
337
+ throw std::runtime_error("Exception thrown from C++");
338
+ }
339
+
340
+ static jsi::Value __hostFunction_NativeCustomKeyboardTurboModuleSpecJSI_throwExceptionArk(
341
+ jsi::Runtime &rt, react::TurboModule &turboModule, const jsi::Value *args, size_t count) {
342
+ return jsi::Value(static_cast<ArkTSTurboModule &>(turboModule).call(rt, "throwExceptionArk", args, count));
343
+ }
344
+
345
+ NativeCustomKeyboardTurboModuleSpecJSI::NativeCustomKeyboardTurboModuleSpecJSI(const ArkTSTurboModule::Context ctx,
346
+ const std::string name)
347
+ : ArkTSTurboModule(ctx, name) {
348
+ methodMap_ = {};
349
+
350
+ methodMap_["install"] = MethodMetadata{2, __hostFunction_NativeCustomKeyboardTurboModuleSpecJSI_install};
351
+ methodMap_["uninstall"] = MethodMetadata{1, __hostFunction_NativeCustomKeyboardTurboModuleSpecJSI_uninstall};
352
+ methodMap_["insertText"] = MethodMetadata{2, __hostFunction_NativeCustomKeyboardTurboModuleSpecJSI_insertText};
353
+ methodMap_["backSpace"] = MethodMetadata{1, __hostFunction_NativeCustomKeyboardTurboModuleSpecJSI_backSpace};
354
+ methodMap_["doDelete"] = MethodMetadata{1, __hostFunction_NativeCustomKeyboardTurboModuleSpecJSI_doDelete};
355
+ methodMap_["moveLeft"] = MethodMetadata{1, __hostFunction_NativeCustomKeyboardTurboModuleSpecJSI_moveLeft};
356
+ methodMap_["moveRight"] = MethodMetadata{1, __hostFunction_NativeCustomKeyboardTurboModuleSpecJSI_moveRight};
357
+ methodMap_["switchSystemKeyboard"] =
358
+ MethodMetadata{1, __hostFunction_NativeCustomKeyboardTurboModuleSpecJSI_switchSystemKeyboard};
359
+
360
+ methodMap_["displayRNOHError"] =
361
+ MethodMetadata{1, __hostFunction_NativeCustomKeyboardTurboModuleSpecJSI_displayRNOHError};
362
+ methodMap_["throwExceptionCpp"] =
363
+ MethodMetadata{0, __hostFunction_NativeCustomKeyboardTurboModuleSpecJSI_throwExceptionCpp};
364
+ methodMap_["throwExceptionArk"] =
365
+ MethodMetadata{0, __hostFunction_NativeCustomKeyboardTurboModuleSpecJSI_throwExceptionArk};
366
+ }
@@ -0,0 +1,46 @@
1
+ /*
2
+ * Copyright (c) 2025 Huawei Device Co., Ltd. All rights reserved
3
+ * Use of this source code is governed by a MIT license that can be
4
+ * found in the LICENSE file.
5
+ */
6
+
7
+
8
+ // NOTE: This entire file should be codegen'ed.
9
+ #pragma once
10
+
11
+ #include <ReactCommon/TurboModule.h>
12
+ #include "RNOH/ArkTSTurboModule.h"
13
+ #include "RNOH/arkui/TextInputNode.h"
14
+ #include "RNOHCorePackage/ComponentInstances/TextInputComponentInstance.h"
15
+ #include "RNOH/RNInstanceCAPI.h"
16
+ #include "RNOHCorePackage/TurboModules/DeviceInfoTurboModule.h"
17
+ using namespace facebook;
18
+
19
+ namespace rnoh {
20
+
21
+ typedef struct{
22
+ ArkUI_NodeHandle data;
23
+ bool enabled;
24
+ }UserData;
25
+
26
+ class JSI_EXPORT NativeCustomKeyboardTurboModuleSpecJSI : public ArkTSTurboModule {
27
+ public:
28
+ NativeCustomKeyboardTurboModuleSpecJSI(const ArkTSTurboModule::Context ctx, const std::string name);
29
+ ~NativeCustomKeyboardTurboModuleSpecJSI();
30
+ void install(jsi::Runtime &rt, int32_t tag, std::string type);
31
+ void uninstall(jsi::Runtime &rt, int32_t tag);
32
+ void insertText(jsi::Runtime &rt, int32_t tag, std::string text);
33
+ void backSpace(jsi::Runtime &rt, int32_t tag);
34
+ void doDelete(jsi::Runtime &rt, int32_t tag);
35
+ void moveLeft(jsi::Runtime &rt, int32_t tag);
36
+ void moveRight(jsi::Runtime &rt, int32_t tag);
37
+ void switchSystemKeyboard(jsi::Runtime &rt, int32_t tag);
38
+
39
+ private:
40
+ TextInputNode *getEditById(int id);
41
+
42
+ private:
43
+ std::unordered_set<UserData *> m_userDatas;
44
+ };
45
+
46
+ } // namespace rnoh