From 8945ee0e26c788693fb5e671c4ab6bd572bcbe81 Mon Sep 17 00:00:00 2001 From: IvanIhnatsiuk Date: Tue, 4 Nov 2025 23:55:24 +0100 Subject: [PATCH 1/5] feat: add imperative method to get html value --- .../enriched/EnrichedTextInputModule.kt | 33 ++++++++++++ .../enriched/EnrichedTextInputView.kt | 4 ++ .../enriched/EnrichedTextInputViewPackage.kt | 41 +++++++++++---- example/src/App.tsx | 5 ++ ios/EnrichedTextInputModule.h | 22 ++++++++ ios/EnrichedTextInputModule.mm | 51 +++++++++++++++++++ ios/EnrichedTextInputView.h | 1 + ios/EnrichedTextInputView.mm | 16 +++++- package.json | 4 +- src/EnrichedTextInput.tsx | 29 +++++++---- src/NativeEnrichedTextInputModule.ts | 11 ++++ 11 files changed, 191 insertions(+), 26 deletions(-) create mode 100644 android/src/main/java/com/swmansion/enriched/EnrichedTextInputModule.kt create mode 100644 ios/EnrichedTextInputModule.h create mode 100644 ios/EnrichedTextInputModule.mm create mode 100644 src/NativeEnrichedTextInputModule.ts diff --git a/android/src/main/java/com/swmansion/enriched/EnrichedTextInputModule.kt b/android/src/main/java/com/swmansion/enriched/EnrichedTextInputModule.kt new file mode 100644 index 00000000..d8e890e8 --- /dev/null +++ b/android/src/main/java/com/swmansion/enriched/EnrichedTextInputModule.kt @@ -0,0 +1,33 @@ +package com.swmansion.enriched + +import com.facebook.react.bridge.ReactApplicationContext +import com.facebook.react.bridge.UiThreadUtil +import com.facebook.react.module.annotations.ReactModule +import com.facebook.react.uimanager.UIManagerHelper +import com.swmansion.enriched.utils.EnrichedParser + +@ReactModule(name = EnrichedTextInputModule.NAME) +class EnrichedTextInputModule(val reactContext: ReactApplicationContext) : + NativeEnrichedTextInputModuleSpec(reactContext) { + override fun getName(): String = NAME + + override fun getHTMLValue(inputTag: Double): String? { + UiThreadUtil.assertOnUiThread() + val reactNode = inputTag.toInt() + val enrichedInput = getComponent(reactNode) + return enrichedInput?.getHtmlValue() ?: "" + } + + private fun getComponent(reactTag: Int): EnrichedTextInputView? { + return try { + val uiManager = UIManagerHelper.getUIManagerForReactTag(reactContext, reactTag) + uiManager?.resolveView(reactTag) as? EnrichedTextInputView + } catch (_: Throwable) { + null + } + } + + companion object { + const val NAME = "EnrichedTextInputModule" + } +} diff --git a/android/src/main/java/com/swmansion/enriched/EnrichedTextInputView.kt b/android/src/main/java/com/swmansion/enriched/EnrichedTextInputView.kt index bae67e50..d9df1347 100644 --- a/android/src/main/java/com/swmansion/enriched/EnrichedTextInputView.kt +++ b/android/src/main/java/com/swmansion/enriched/EnrichedTextInputView.kt @@ -543,6 +543,10 @@ class EnrichedTextInputView : AppCompatEditText { didAttachToWindow = true } + fun getHtmlValue(): String? { + return EnrichedParser.toHtml(text) + } + companion object { const val CLIPBOARD_TAG = "react-native-enriched-clipboard" } diff --git a/android/src/main/java/com/swmansion/enriched/EnrichedTextInputViewPackage.kt b/android/src/main/java/com/swmansion/enriched/EnrichedTextInputViewPackage.kt index 3b93c129..e0965dad 100644 --- a/android/src/main/java/com/swmansion/enriched/EnrichedTextInputViewPackage.kt +++ b/android/src/main/java/com/swmansion/enriched/EnrichedTextInputViewPackage.kt @@ -1,19 +1,38 @@ package com.swmansion.enriched -import com.facebook.react.ReactPackage +import com.facebook.react.BaseReactPackage +import com.facebook.react.uimanager.ViewManager import com.facebook.react.bridge.NativeModule import com.facebook.react.bridge.ReactApplicationContext -import com.facebook.react.uimanager.ViewManager -import java.util.ArrayList +import com.facebook.react.module.model.ReactModuleInfo +import com.facebook.react.module.model.ReactModuleInfoProvider +import java.util.HashMap -class EnrichedTextInputViewPackage : ReactPackage { +class EnrichedTextInputViewPackage : BaseReactPackage() { override fun createViewManagers(reactContext: ReactApplicationContext): List> { - val viewManagers: MutableList> = ArrayList() - viewManagers.add(EnrichedTextInputViewManager()) - return viewManagers + return listOf(EnrichedTextInputViewManager()) } - override fun createNativeModules(reactContext: ReactApplicationContext): List { - return emptyList() - } -} + override fun getModule(name: String, reactContext: ReactApplicationContext): NativeModule? { + return if (name == EnrichedTextInputModule.NAME) { + EnrichedTextInputModule(reactContext) + } else { + null + } + } + + override fun getReactModuleInfoProvider(): ReactModuleInfoProvider { + return ReactModuleInfoProvider { + val moduleMap: MutableMap = HashMap() + moduleMap[EnrichedTextInputModule.NAME] = ReactModuleInfo( + EnrichedTextInputModule.NAME, + EnrichedTextInputModule.NAME, + false, // canOverrideExistingModule + false, // needsEagerInit + false, // isCxxModule + true // isTurboModule + ) + moduleMap + } + } +} \ No newline at end of file diff --git a/example/src/App.tsx b/example/src/App.tsx index b5dac0c9..d305d7a1 100644 --- a/example/src/App.tsx +++ b/example/src/App.tsx @@ -312,6 +312,11 @@ export default function App() { onPress={openValueModal} style={styles.valueButton} /> +