Skip to content

Commit 8acad55

Browse files
Merge pull request #32 from divyanshu-patil/feat/new_props
Feat: disableImagePasting Prop
2 parents a88dbf0 + d040e85 commit 8acad55

File tree

8 files changed

+78
-17
lines changed

8 files changed

+78
-17
lines changed

android/src/main/java/com/typerich/TypeRichTextInputView.kt

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ class TypeRichTextInputView : AppCompatEditText {
6464
private var lineHeightPx: Int? = null
6565
private var isSettingTextFromJS = false
6666
private var isInitialized = false
67+
private var disableImagePasting = false
6768

6869
constructor(context: Context) : super(context) {
6970
prepareComponent()
@@ -140,23 +141,34 @@ class TypeRichTextInputView : AppCompatEditText {
140141
override fun onCreateInputConnection(outAttrs: EditorInfo): InputConnection? {
141142
val ic = super.onCreateInputConnection(outAttrs) ?: return null
142143

143-
EditorInfoCompat.setContentMimeTypes(
144-
outAttrs,
145-
arrayOf("image/png", "image/jpg", "image/jpeg", "image/gif", "image/webp")
146-
)
144+
if (!disableImagePasting) {
145+
EditorInfoCompat.setContentMimeTypes(
146+
outAttrs,
147+
arrayOf("image/png", "image/jpg", "image/jpeg", "image/gif", "image/webp")
148+
)
149+
150+
return InputConnectionCompat.createWrapper(ic, outAttrs, onCommitContent)
151+
}
147152

148-
return InputConnectionCompat.createWrapper(ic, outAttrs, onCommitContent)
153+
return ic
149154
}
150155

151156
private val onCommitContent = InputConnectionCompat.OnCommitContentListener { info, flags, _ ->
152157
try {
153-
// request permission if needed
154-
if ((flags and InputConnectionCompat.INPUT_CONTENT_GRANT_READ_URI_PERMISSION) != 0) {
158+
val hasPermission =
159+
(flags and InputConnectionCompat.INPUT_CONTENT_GRANT_READ_URI_PERMISSION) != 0
160+
161+
if (hasPermission) {
155162
try {
156163
info.requestPermission()
157-
} catch (ex: Exception) {
158-
// permission failed
164+
} catch (_: Exception) {}
165+
}
166+
167+
if (disableImagePasting) {
168+
if (hasPermission) {
169+
try { info.releasePermission() } catch (_: Exception) {}
159170
}
171+
return@OnCommitContentListener false
160172
}
161173

162174
val uri = info.contentUri
@@ -189,7 +201,7 @@ class TypeRichTextInputView : AppCompatEditText {
189201
}
190202
}
191203

192-
// paste handler
204+
// context menu paste handler
193205
override fun onTextContextMenuItem(id: Int): Boolean {
194206
if (id == android.R.id.paste || id == android.R.id.pasteAsPlainText) {
195207
val clipboard = context.getSystemService(Context.CLIPBOARD_SERVICE) as? ClipboardManager
@@ -198,6 +210,12 @@ class TypeRichTextInputView : AppCompatEditText {
198210
val clip = clipboard.primaryClip ?: return super.onTextContextMenuItem(id)
199211
val item = clip.getItemAt(0)
200212

213+
if (disableImagePasting) {
214+
if (item.uri != null || item.intent?.data != null) {
215+
return true
216+
}
217+
}
218+
201219
// uri
202220
item.uri?.let { uri ->
203221
val source = EnumPasteSource.CLIPBOARD.value
@@ -577,6 +595,10 @@ class TypeRichTextInputView : AppCompatEditText {
577595
}
578596
}
579597

598+
fun setDisableImagePasting(disabled: Boolean){
599+
this.disableImagePasting = disabled
600+
}
601+
580602
override fun isLayoutRequested(): Boolean {
581603
return false
582604
}

android/src/main/java/com/typerich/TypeRichTextInputViewManager.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,11 @@ class TypeRichTextInputViewManager :
145145
view?.setLineHeightReact(lineHeight)
146146
}
147147

148+
@ReactProp(name = "disableImagePasting")
149+
override fun setDisableImagePasting(view: TypeRichTextInputView?, value: Boolean) {
150+
view?.setDisableImagePasting(value)
151+
}
152+
148153
override fun onAfterUpdateTransaction(view: TypeRichTextInputView) {
149154
super.onAfterUpdateTransaction(view)
150155
view.afterUpdateTransaction()

example/src/App.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ export default function App() {
180180
fontWeight={'200'}
181181
fontSize={24}
182182
color="indigo"
183+
disableImagePasting={false}
183184
/>
184185
</View>
185186
<TextInput

ios/TypeRichTextInputView.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ NS_ASSUME_NONNULL_BEGIN
2626
- (void)invalidateTextLayoutFromCommand;
2727
- (void)updatePlaceholderVisibilityFromCommand;
2828
- (void)dispatchSelectionChangeIfNeeded;
29+
- (BOOL)isDisableImagePasting;
2930

3031
@end
3132
NS_ASSUME_NONNULL_END

ios/TypeRichTextInputView.mm

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ @implementation TypeRichTextInputView {
5252
/// Commands to call from js side
5353
TypeRichTextInputCommands *_commandHandler;
5454
// BOOL _isHandlingUserInput;
55+
56+
/// Disabling Image Pasing
57+
BOOL _disableImagePasting;
5558
}
5659

5760
#pragma mark - Fabric registration
@@ -124,6 +127,8 @@ - (instancetype)initWithFrame:(CGRect)frame {
124127
_textView.font = defaultFont;
125128
_placeholderLabel.font = defaultFont;
126129

130+
_disableImagePasting = NO;
131+
127132
// Add textView as subview (not contentView)
128133
[self addSubview:_textView];
129134

@@ -301,6 +306,11 @@ - (void)updateProps:(Props::Shared const &)props
301306
);
302307
}
303308

309+
// disableImagePasting
310+
if (!oldPropsPtr || newProps.disableImagePasting != oldPropsPtr->disableImagePasting) {
311+
_disableImagePasting = newProps.disableImagePasting;
312+
}
313+
304314
#pragma mark - Style Props
305315

306316
// Text color
@@ -815,4 +825,7 @@ - (void)dispatchSelectionChangeIfNeeded {
815825
// return _isHandlingUserInput;
816826
//}
817827

828+
- (BOOL)isDisableImagePasting{
829+
return _disableImagePasting;
830+
}
818831
@end

ios/inputTextView/TypeRichUITextView.mm

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,17 @@ @implementation TypeRichUITextView
1313
- (void)paste:(id)sender {
1414
UIPasteboard *pb = UIPasteboard.generalPasteboard;
1515

16-
if (pb.hasImages) {
16+
if ([self.owner isDisableImagePasting] &&
17+
pb.hasImages &&
18+
!pb.hasStrings) {
19+
return;
20+
}
21+
22+
if (
23+
![self.owner isDisableImagePasting]
24+
&& pb.hasImages
25+
) {
26+
1727
UIImage *image = pb.image;
1828
if (!image) {
1929
[super paste:sender];
@@ -53,7 +63,11 @@ - (void)paste:(id)sender {
5363

5464
- (BOOL)canPasteItemProviders:(NSArray<NSItemProvider *> *)itemProviders {
5565
for (NSItemProvider *provider in itemProviders) {
56-
if ([provider hasItemConformingToTypeIdentifier:@"public.text"] ||
66+
if ([provider hasItemConformingToTypeIdentifier:@"public.text"]) {
67+
return YES;
68+
}
69+
70+
if (![self.owner isDisableImagePasting] &&
5771
[provider hasItemConformingToTypeIdentifier:@"public.image"]) {
5872
return YES;
5973
}
@@ -65,6 +79,13 @@ - (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
6579
if (action == @selector(paste:)) {
6680
UIPasteboard *pb = UIPasteboard.generalPasteboard;
6781

82+
if (
83+
[self.owner isDisableImagePasting] &&
84+
pb.hasImages &&
85+
!pb.hasStrings) {
86+
return NO;
87+
}
88+
6889
// Allow paste if there is text OR image
6990
if (pb.hasStrings || pb.hasImages) {
7091
return YES;

src/TypeRichTextInput.tsx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export function normalizeEvent<T>(event: MaybeNativeEvent<T>): T {
2828
return event as T;
2929
}
3030

31-
// Public facing props (same as NativeProps but events normalized)
31+
// normalised events
3232
export interface TypeRichTextInputProps
3333
extends Omit<
3434
TypeRichTextInputNativeProps,
@@ -38,7 +38,6 @@ export interface TypeRichTextInputProps
3838
| 'onInputBlur'
3939
| 'onPasteImage'
4040
> {
41-
// JS-friendly callbacks
4241
onFocus?: () => void;
4342
onBlur?: () => void;
4443
onChangeText?: (value: string) => void;
@@ -67,8 +66,7 @@ export interface TypeRichTextInputRef {
6766
* - Fabric-based rendering
6867
* - custom ShadowNode on Android
6968
*
70-
* iOS support is currently unavailable and renders a `View` comp as fallback
71-
* we are planning to add support for ios too soon
69+
* iOS support is currently in Beta Stage and
7270
*/
7371
const TypeRichTextInput = forwardRef(
7472
(props: TypeRichTextInputProps, ref: Ref<TypeRichTextInputRef>) => {

src/TypeRichTextInputNativeComponent.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ export interface TypeRichTextInputNativeProps extends ViewProps {
5050
keyboardAppearance?: WithDefault<'default' | 'light' | 'dark', 'default'>; // ios only
5151

5252
// Todo
53-
// disableImagePasting?: boolean
53+
disableImagePasting?: boolean;
5454

5555
// event callbacks
5656
onInputFocus?: DirectEventHandler<null>;

0 commit comments

Comments
 (0)