Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import android.graphics.Color
import android.graphics.Rect
import android.graphics.text.LineBreaker
import android.os.Build
import android.text.Editable
import android.text.InputType
import android.text.Spannable
import android.util.AttributeSet
Expand Down Expand Up @@ -332,6 +333,35 @@ class EnrichedTextInputView : AppCompatEditText {
setSelection(actualStart, actualEnd)
}

fun insertValue(

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately I tested it once again and on Android we need to fix merging spans first before merging this PR:

Screen.Recording.2026-04-02.at.09.14.41.1.mov

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you able to provide any guidance on when merging spans might be fixed, and this insertValue method might be rolled out as a result?

This is the single most important feature for our use at the moment.

value: String,
start: Int,
end: Int,
) {
val currentText = text as Editable

if (currentText.isEmpty()) {
setValue(value)

return
}

runAsATransaction {
val newText = parseText(value)

currentText.replace(
if (currentText.length < start) currentText.length else start,
if (currentText.length < end) currentText.length else end,
newText,
)

observeAsyncImages()

// Scroll to the last char of inserted text
setSelection(start + newText.length)
}
}

// Helper: Walks through the string skipping ZWSPs to find the Nth visible character
private fun getActualIndex(visibleIndex: Int): Int {
val currentText = text as Spannable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,15 @@ class EnrichedTextInputViewManager :
view?.setCustomSelection(start, end)
}

override fun insertValue(
view: EnrichedTextInputView?,
text: String,
start: Int,
end: Int,
) {
view?.insertValue(text, start, end)
}

override fun toggleBold(view: EnrichedTextInputView?) {
view?.verifyAndToggleStyle(EnrichedSpans.BOLD)
}
Expand Down
41 changes: 41 additions & 0 deletions ios/EnrichedTextInputView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -1156,6 +1156,12 @@ - (void)handleCommand:(const NSString *)commandName args:(const NSArray *)args {
} else if ([commandName isEqualToString:@"requestHTML"]) {
NSInteger requestId = [((NSNumber *)args[0]) integerValue];
[self requestHTML:requestId];
} else if ([commandName isEqualToString:@"insertValue"]) {
NSString *text = (NSString *)args[0];
NSInteger start = [((NSNumber *)args[1]) integerValue];
NSInteger end = [((NSNumber *)args[2]) integerValue];

[self insertValue:text start:start end:end];
}
}

Expand Down Expand Up @@ -1202,6 +1208,41 @@ - (void)setCustomSelection:(NSInteger)visibleStart end:(NSInteger)visibleEnd {
textView.selectedRange = NSMakeRange(actualStart, actualEnd - actualStart);
}

- (void)insertValue:(NSString *)value
Comment thread
kacperzolkiewski marked this conversation as resolved.
start:(NSInteger)visibleStart
end:(NSInteger)visibleEnd {
if (textView.text.length == 0) {
[self setValue:value];

return;
}

if (textView.text.length < visibleStart) {
visibleStart = textView.text.length;
}

if (textView.text.length < visibleEnd) {
visibleEnd = textView.text.length;
}

NSRange range = NSMakeRange(visibleStart, visibleEnd - visibleStart);

NSString *initiallyProcessedHtml = [parser initiallyProcessHtml:value];
if (initiallyProcessedHtml == nullptr) {
// just plain text
[textView.textStorage replaceCharactersInRange:range withString:value];

recentlyChangedRange = range;
textView.selectedRange = NSRange(range.location + value.length, 0);
} else {
// we've got some seemingly proper html
[parser replaceFromHtml:initiallyProcessedHtml range:range];
}

// set recentlyChangedRange and check for changes
[self anyTextMayHaveBeenModified];
}

// Helper: Walks through the string skipping ZWSPs to find the Nth visible
// character
- (NSUInteger)getActualIndex:(NSInteger)visibleIndex text:(NSString *)text {
Expand Down
4 changes: 4 additions & 0 deletions src/EnrichedTextInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export interface EnrichedTextInputInstance extends NativeMethods {
setValue: (value: string) => void;
setSelection: (start: number, end: number) => void;
getHTML: () => Promise<string>;
insertValue: (text: string, start: number, end: number) => void;

// Text formatting commands
toggleBold: () => void;
Expand Down Expand Up @@ -310,6 +311,9 @@ export const EnrichedTextInput = ({
setSelection: (start: number, end: number) => {
Commands.setSelection(nullthrows(nativeRef.current), start, end);
},
insertValue: (text, start, end) => {
Commands.insertValue(nullthrows(nativeRef.current), text, start, end);
},
}));

const handleMentionEvent = (e: NativeSyntheticEvent<OnMentionEvent>) => {
Expand Down
7 changes: 7 additions & 0 deletions src/spec/EnrichedTextInputNativeComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,12 @@ interface NativeCommands {
start: Int32,
end: Int32
) => void;
insertValue: (
viewRef: React.ElementRef<ComponentType>,
text: string,
start: Int32,
end: Int32
) => void;

// Text formatting commands
toggleBold: (viewRef: React.ElementRef<ComponentType>) => void;
Expand Down Expand Up @@ -357,6 +363,7 @@ export const Commands: NativeCommands = codegenNativeCommands<NativeCommands>({
'blur',
'setValue',
'setSelection',
'insertValue',

// Text formatting commands
'toggleBold',
Expand Down
Loading