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 @@ -1005,6 +1005,19 @@ class EnrichedTextInputView :
selection?.validateStyles()
}

fun deleteAtSelection() {
runAsATransaction {
val start = selectionStart
val end = selectionEnd

if (start != end) {
text?.delete(start, end)
} else if (start > 0) {
text?.delete(start - 1, start)
}
}
}
Comment thread
kacperzolkiewski marked this conversation as resolved.

fun requestHTML(requestId: Int) {
val html =
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,10 @@ class EnrichedTextInputViewManager :
view?.setTextAlignment(alignment)
}

override fun deleteAtSelection(view: EnrichedTextInputView?) {
view?.deleteAtSelection()
}

override fun measure(
context: Context,
localData: ReadableMap?,
Expand Down
14 changes: 14 additions & 0 deletions ios/EnrichedTextInputView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -1296,6 +1296,8 @@ - (void)handleCommand:(const NSString *)commandName args:(const NSArray *)args {
if (!_placeholderLabel.isHidden) {
[self refreshPlaceholderLabelStyles];
}
} else if ([commandName isEqualToString:@"deleteAtSelection"]) {
[self deleteAtSelection];
}
}

Expand Down Expand Up @@ -1335,6 +1337,18 @@ - (void)setValue:(NSString *)value {
[self anyTextMayHaveBeenModified];
}

- (void)deleteAtSelection {
UITextRange *selectedRange = self.textView.selectedTextRange;

if (selectedRange.empty) {
[self.textView deleteBackward];
} else {
[self.textView replaceRange:selectedRange withText:@""];
}

[self anyTextMayHaveBeenModified];
}
Comment on lines +1340 to +1359

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Is it okay if I use selectedTextRange instead of selectedRange?

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.

You mean self.textView.selectedTextRange?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes. It's just that everywhere around the code a textView.selectedRange is being used.

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.

O ok, yes it would be better to use textView.selectedRange the same like in other places.


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

Expand Down
3 changes: 3 additions & 0 deletions src/native/EnrichedTextInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,9 @@ export const EnrichedTextInput = ({
) => {
Commands.setTextAlignment(nullthrows(nativeRef.current), alignment);
},
deleteAtSelection: () => {
Commands.deleteAtSelection(nullthrows(nativeRef.current));
},
}));

const handleMentionEvent = (e: NativeSyntheticEvent<OnMentionEvent>) => {
Expand Down
2 changes: 2 additions & 0 deletions src/spec/EnrichedTextInputNativeComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,7 @@ interface NativeCommands {
viewRef: React.ElementRef<ComponentType>,
alignment: string
) => void;
deleteAtSelection: (viewRef: React.ElementRef<ComponentType>) => void;
}

export const Commands: NativeCommands = codegenNativeCommands<NativeCommands>({
Expand Down Expand Up @@ -516,6 +517,7 @@ export const Commands: NativeCommands = codegenNativeCommands<NativeCommands>({
'addMention',
'requestHTML',
'setTextAlignment',
'deleteAtSelection',
],
});

Expand Down
2 changes: 2 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,8 @@ export interface EnrichedTextInputInstance extends NativeMethods {
setTextAlignment: (
alignment: 'left' | 'center' | 'right' | 'justify' | 'auto'
) => void;

deleteAtSelection: () => void;
}

export interface ContextMenuItem {
Expand Down
11 changes: 11 additions & 0 deletions src/web/EnrichedTextInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,17 @@ export const EnrichedTextInput = ({
runFocused(editor, (c) => c.setTextAlign(alignment));
}
},
deleteAtSelection: () => {
runFocused(editor, (c) => {
const { from, to } = editor.state.selection;

if (from !== to) {
return c.deleteSelection();
} else {
return c.deleteRange({ from: from - 1, to });
}
Comment thread
kacperzolkiewski marked this conversation as resolved.
Outdated
});
},
}),
[editor, mentionIndicatorsRef, useHtmlNormalizerRef]
);
Expand Down
Loading