Skip to content

Commit 8595b91

Browse files
fix: make image width and height necessary
1 parent a06c37a commit 8595b91

7 files changed

Lines changed: 14 additions & 25 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -527,7 +527,7 @@ class EnrichedTextInputView : AppCompatEditText {
527527
parametrizedStyles?.setLinkSpan(start, end, text, url)
528528
}
529529

530-
fun addImage(src: String, width: Float?, height: Float?) {
530+
fun addImage(src: String, width: Float, height: Float) {
531531
val isValid = verifyStyle(EnrichedSpans.IMAGE)
532532
if (!isValid) return
533533

android/src/main/java/com/swmansion/enriched/styles/ParametrizedStyles.kt

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ class ParametrizedStyles(private val view: EnrichedTextInputView) {
170170
}
171171
}
172172

173-
fun setImageSpan(src: String, width: Float?, height: Float?) {
173+
fun setImageSpan(src: String, width: Float, height: Float) {
174174
if (view.selection == null) return
175175

176176
val spannable = view.text as SpannableStringBuilder
@@ -188,10 +188,7 @@ class ParametrizedStyles(private val view: EnrichedTextInputView) {
188188

189189
val uri = Uri.fromFile(File(src))
190190

191-
val imageWidth = width ?: view.htmlStyle.imgWidth
192-
val imageHeight = height ?: view.htmlStyle.imgHeight
193-
194-
val span = EnrichedImageSpan(view.context, uri, imageWidth.toInt(), imageHeight.toInt())
191+
val span = EnrichedImageSpan(view.context, uri, width.toInt(), height.toInt())
195192
val (safeStart, safeEnd) = spannable.getSafeSpanBoundaries(start, end)
196193
spannable.setSpan(span, safeStart, safeEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
197194
}

example/src/App.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ export default function App() {
207207
closeValueModal();
208208
};
209209

210-
const selectImage = async (width?: number, height?: number) => {
210+
const selectImage = async (width: number, height: number) => {
211211
const response = await launchImageLibrary({
212212
mediaType: 'photo',
213213
selectionLimit: 1,

example/src/components/ImageModal.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { Icon } from './Icon';
66
interface ImageModalProps {
77
isOpen: boolean;
88
onClose: () => void;
9-
onSubmit: (width?: number, height?: number) => void;
9+
onSubmit: (width: number, height: number) => void;
1010
}
1111

1212
export const ImageModal: FC<ImageModalProps> = ({
@@ -20,8 +20,8 @@ export const ImageModal: FC<ImageModalProps> = ({
2020
const handleSave = () => {
2121
const parsedWidth = parseFloat(width);
2222
const parsedHeight = parseFloat(height);
23-
const finalWidth = isNaN(parsedWidth) ? undefined : parsedWidth;
24-
const finalHeight = isNaN(parsedHeight) ? undefined : parsedHeight;
23+
const finalWidth = isNaN(parsedWidth) ? 0 : parsedWidth;
24+
const finalHeight = isNaN(parsedHeight) ? 0 : parsedHeight;
2525

2626
onSubmit(finalWidth, finalHeight);
2727
};

ios/EnrichedTextInputView.mm

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -808,11 +808,8 @@ - (void)handleCommand:(const NSString *)commandName args:(const NSArray *)args {
808808
[self toggleParagraphStyle:[CodeBlockStyle getStyleType]];
809809
} else if([commandName isEqualToString:@"addImage"]) {
810810
NSString *uri = (NSString *)args[0];
811-
NSNumber *widthNum = ((NSNumber*)args[1]);
812-
NSNumber *heightNum = ((NSNumber*)args[2]);
813-
814-
CGFloat imgWidth = [widthNum isKindOfClass:[NSNull class]] ? config.imageWidth : [widthNum floatValue];
815-
CGFloat imgHeight = [heightNum isKindOfClass:[NSNull class]] ? config.imageHeight : [heightNum floatValue];
811+
CGFloat imgWidth = [(NSNumber*)args[1] floatValue];
812+
CGFloat imgHeight = [(NSNumber*)args[2] floatValue];
816813

817814
[self addImage:uri width:imgWidth height:imgHeight];
818815
}

src/EnrichedTextInput.tsx

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ export interface EnrichedTextInputInstance extends NativeMethods {
5252
toggleOrderedList: () => void;
5353
toggleUnorderedList: () => void;
5454
setLink: (start: number, end: number, text: string, url: string) => void;
55-
setImage: (src: string, width?: number, height?: number) => void;
55+
setImage: (src: string, width: number, height: number) => void;
5656
startMention: (indicator: string) => void;
5757
setMention: (
5858
indicator: string,
@@ -272,13 +272,8 @@ export const EnrichedTextInput = ({
272272
setLink: (start: number, end: number, text: string, url: string) => {
273273
Commands.addLink(nullthrows(nativeRef.current), start, end, text, url);
274274
},
275-
setImage: (uri: string, width?: number, height?: number) => {
276-
Commands.addImage(
277-
nullthrows(nativeRef.current),
278-
uri,
279-
width ?? null,
280-
height ?? null
281-
);
275+
setImage: (uri: string, width: number, height: number) => {
276+
Commands.addImage(nullthrows(nativeRef.current), uri, width, height);
282277
},
283278
setMention: (
284279
indicator: string,

src/EnrichedTextInputNativeComponent.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,8 +194,8 @@ interface NativeCommands {
194194
addImage: (
195195
viewRef: React.ElementRef<ComponentType>,
196196
uri: string,
197-
width: Float | null,
198-
height: Float | null
197+
width: Float,
198+
height: Float
199199
) => void;
200200
startMention: (
201201
viewRef: React.ElementRef<ComponentType>,

0 commit comments

Comments
 (0)