Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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 @@ -426,6 +426,28 @@ class EnrichedTextInputView :
layoutManager.invalidateLayout()
}

fun insertText(
text: String,
start: Int,
end: Int,
) {
val editable = this.text ?: return
val replaceStart: Int
val replaceEnd: Int

if (start < 0) {
// Use current selection
replaceStart = selectionStart
replaceEnd = selectionEnd
} else {
replaceStart = getActualIndex(start)
replaceEnd = getActualIndex(end)
}

editable.replace(replaceStart, replaceEnd, text)
setSelection(replaceStart + text.length)
}

fun setCustomSelection(
visibleStart: Int,
visibleEnd: Int,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,15 @@ class EnrichedTextInputViewManager :
view?.setValue(text)
}

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

override fun setSelection(
view: EnrichedTextInputView?,
start: Int,
Expand Down
27 changes: 27 additions & 0 deletions ios/EnrichedTextInputView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -1332,6 +1332,11 @@ - (void)handleCommand:(const NSString *)commandName args:(const NSArray *)args {
CGFloat imgHeight = [(NSNumber *)args[2] floatValue];

[self addImage:uri width:imgWidth height:imgHeight];
} else if ([commandName isEqualToString:@"insertText"]) {
NSString *text = (NSString *)args[0];
NSInteger start = [((NSNumber *)args[1]) integerValue];
NSInteger end = [((NSNumber *)args[2]) integerValue];
[self insertText:text start:start end:end];
} else if ([commandName isEqualToString:@"setSelection"]) {
NSInteger start = [((NSNumber *)args[0]) integerValue];
NSInteger end = [((NSNumber *)args[1]) integerValue];
Expand Down Expand Up @@ -1375,6 +1380,28 @@ - (void)setValue:(NSString *)value {
[self anyTextMayHaveBeenModified];
}

- (void)insertText:(NSString *)text start:(NSInteger)start end:(NSInteger)end {
NSString *currentText = textView.textStorage.string;

NSRange replaceRange;
if (start < 0) {
// Use current selection
replaceRange = textView.selectedRange;
} else {
NSUInteger actualStart = [self getActualIndex:start text:currentText];
NSUInteger actualEnd = [self getActualIndex:end text:currentText];
replaceRange = NSMakeRange(actualStart, actualEnd - actualStart);
}

// Replace the range with the new text, preserving surrounding attributes
[textView.textStorage replaceCharactersInRange:replaceRange withString:text];

// Move cursor to end of inserted text
textView.selectedRange = NSMakeRange(replaceRange.location + text.length, 0);

[self anyTextMayHaveBeenModified];
}

- (void)setCustomSelection:(NSInteger)visibleStart end:(NSInteger)visibleEnd {
NSString *text = textView.textStorage.string;

Expand Down
5 changes: 5 additions & 0 deletions src/native/EnrichedTextInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,11 @@ export const EnrichedTextInput = ({
setValue: (value: string) => {
Commands.setValue(nullthrows(nativeRef.current), value);
},
insertText: (text: string, start?: number, end?: number) => {
const s = start ?? -1;
const e = end ?? s;
Commands.insertText(nullthrows(nativeRef.current), text, s, e);
},
getHTML: () => {
return new Promise<string>((resolve, reject) => {
const requestId = nextHtmlRequestId.current++;
Expand Down
7 changes: 7 additions & 0 deletions src/spec/EnrichedTextInputNativeComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,12 @@ interface NativeCommands {
focus: (viewRef: React.ElementRef<ComponentType>) => void;
blur: (viewRef: React.ElementRef<ComponentType>) => void;
setValue: (viewRef: React.ElementRef<ComponentType>, text: string) => void;
insertText: (
viewRef: React.ElementRef<ComponentType>,
text: string,
start: Int32,
end: Int32
) => void;
setSelection: (
viewRef: React.ElementRef<ComponentType>,
start: Int32,
Expand Down Expand Up @@ -477,6 +483,7 @@ export const Commands: NativeCommands = codegenNativeCommands<NativeCommands>({
'focus',
'blur',
'setValue',
'insertText',
'setSelection',

// Text formatting commands
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,7 @@ export interface EnrichedTextInputInstance extends NativeMethods {
focus: () => void;
blur: () => void;
setValue: (value: string) => void;
insertText: (text: string, start?: number, end?: number) => void;
setSelection: (start: number, end: number) => void;
getHTML: () => Promise<string>;

Expand Down
10 changes: 10 additions & 0 deletions src/web/EnrichedTextInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,16 @@ export const EnrichedTextInput = ({
})
.run();
},
insertText: (text: string, start?: number, end?: number) => {
if (start !== undefined && start >= 0) {
const doc = editor.state.doc;
const from = nativePosToTiptapPos(doc, start);
const to = end !== undefined ? nativePosToTiptapPos(doc, end) : from;
editor.chain().focus().insertContentAt({ from, to }, text).run();
} else {
editor.commands.insertContent(text);
}
},
getHTML: () => Promise.resolve(normalizeHtmlFromTiptap(editor.getHTML())),
toggleBold: () => {},
toggleItalic: () => {},
Expand Down