Skip to content

Commit b90a666

Browse files
committed
feat: add deletion by ref
1 parent 982c0ca commit b90a666

7 files changed

Lines changed: 49 additions & 0 deletions

File tree

android/src/main/java/com/swmansion/enriched/textinput/EnrichedTextInputView.kt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1005,6 +1005,19 @@ class EnrichedTextInputView :
10051005
selection?.validateStyles()
10061006
}
10071007

1008+
fun deleteAtSelection() {
1009+
runAsATransaction {
1010+
val start = selectionStart
1011+
val end = selectionEnd
1012+
1013+
if (start != end) {
1014+
text?.delete(start, end)
1015+
} else if (start > 0) {
1016+
text?.delete(start - 1, start)
1017+
}
1018+
}
1019+
}
1020+
10081021
fun requestHTML(requestId: Int) {
10091022
val html =
10101023
try {

android/src/main/java/com/swmansion/enriched/textinput/EnrichedTextInputViewManager.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,10 @@ class EnrichedTextInputViewManager :
479479
view?.setTextAlignment(alignment)
480480
}
481481

482+
override fun deleteAtSelection(view: EnrichedTextInputView?) {
483+
view?.deleteAtSelection()
484+
}
485+
482486
override fun measure(
483487
context: Context,
484488
localData: ReadableMap?,

ios/EnrichedTextInputView.mm

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1296,6 +1296,8 @@ - (void)handleCommand:(const NSString *)commandName args:(const NSArray *)args {
12961296
if (!_placeholderLabel.isHidden) {
12971297
[self refreshPlaceholderLabelStyles];
12981298
}
1299+
} else if ([commandName isEqualToString:@"deleteAtSelection"]) {
1300+
[self deleteAtSelection];
12991301
}
13001302
}
13011303

@@ -1335,6 +1337,18 @@ - (void)setValue:(NSString *)value {
13351337
[self anyTextMayHaveBeenModified];
13361338
}
13371339

1340+
- (void)deleteAtSelection {
1341+
UITextRange *selectedRange = self.textView.selectedTextRange;
1342+
1343+
if (selectedRange.empty) {
1344+
[self.textView deleteBackward];
1345+
} else {
1346+
[self.textView replaceRange:selectedRange withText:@""];
1347+
}
1348+
1349+
[self anyTextMayHaveBeenModified];
1350+
}
1351+
13381352
- (void)setCustomSelection:(NSInteger)visibleStart end:(NSInteger)visibleEnd {
13391353
NSString *text = textView.textStorage.string;
13401354

src/native/EnrichedTextInput.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,9 @@ export const EnrichedTextInput = ({
277277
) => {
278278
Commands.setTextAlignment(nullthrows(nativeRef.current), alignment);
279279
},
280+
deleteAtSelection: () => {
281+
Commands.deleteAtSelection(nullthrows(nativeRef.current));
282+
},
280283
}));
281284

282285
const handleMentionEvent = (e: NativeSyntheticEvent<OnMentionEvent>) => {

src/spec/EnrichedTextInputNativeComponent.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,7 @@ interface NativeCommands {
482482
viewRef: React.ElementRef<ComponentType>,
483483
alignment: string
484484
) => void;
485+
deleteAtSelection: (viewRef: React.ElementRef<ComponentType>) => void;
485486
}
486487

487488
export const Commands: NativeCommands = codegenNativeCommands<NativeCommands>({
@@ -516,6 +517,7 @@ export const Commands: NativeCommands = codegenNativeCommands<NativeCommands>({
516517
'addMention',
517518
'requestHTML',
518519
'setTextAlignment',
520+
'deleteAtSelection',
519521
],
520522
});
521523

src/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,8 @@ export interface EnrichedTextInputInstance extends NativeMethods {
564564
setTextAlignment: (
565565
alignment: 'left' | 'center' | 'right' | 'justify' | 'auto'
566566
) => void;
567+
568+
deleteAtSelection: () => void;
567569
}
568570

569571
export interface ContextMenuItem {

src/web/EnrichedTextInput.tsx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,17 @@ export const EnrichedTextInput = ({
376376
runFocused(editor, (c) => c.setTextAlign(alignment));
377377
}
378378
},
379+
deleteAtSelection: () => {
380+
runFocused(editor, (c) => {
381+
const { from, to } = editor.state.selection;
382+
383+
if (from !== to) {
384+
return c.deleteSelection();
385+
} else {
386+
return c.deleteRange({ from: from - 1, to });
387+
}
388+
});
389+
},
379390
}),
380391
[editor, mentionIndicatorsRef, useHtmlNormalizerRef]
381392
);

0 commit comments

Comments
 (0)