Skip to content

Commit 3ed3008

Browse files
fix: use NSTextAttachment
1 parent db664b6 commit 3ed3008

2 files changed

Lines changed: 9 additions & 58 deletions

File tree

ios/styles/ImageStyle.mm

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ - (void)addImage:(NSString *)uri {
107107
data.uri = uri;
108108

109109
NSMutableDictionary *attributes = [@{
110-
NSAttachmentAttributeName: [self prepareSpacerAttachement],
110+
NSAttachmentAttributeName: [self prepareImageAttachement:uri],
111111
ImageAttributeName: data,
112112
} mutableCopy];
113113

@@ -129,21 +129,17 @@ - (void)addImage:(NSString *)uri {
129129
[_input->textView.textStorage endEditing];
130130
}
131131

132-
-(NSTextAttachment *)prepareSpacerAttachement
132+
-(NSTextAttachment *)prepareImageAttachement:(NSString *)uri
133133
{
134-
NSTextAttachment *spacerAttachment = [[NSTextAttachment alloc] init];
135-
spacerAttachment.bounds = CGRectMake(0, 0, [_input->config imageWidth], [_input->config imageHeight]);
136-
spacerAttachment.image = [self prepareTransparentImage];
134+
NSURL *url = [NSURL URLWithString:uri];
135+
NSData *imgData = [NSData dataWithContentsOfURL:url];
136+
UIImage *image = [UIImage imageWithData:imgData];
137137

138-
return spacerAttachment;
139-
}
138+
NSTextAttachment *attachment = [[NSTextAttachment alloc] init];
139+
attachment.image = image;
140+
attachment.bounds = CGRectMake(0, 0, [_input->config imageWidth], [_input->config imageHeight]);
140141

141-
// Helper to create a 1x1 transparent image
142-
- (UIImage *)prepareTransparentImage {
143-
UIGraphicsBeginImageContextWithOptions(CGSizeMake(1, 1), NO, 0.0);
144-
UIImage *blank = UIGraphicsGetImageFromCurrentImageContext();
145-
UIGraphicsEndImageContext();
146-
return blank;
142+
return attachment;
147143
}
148144

149145
@end

ios/utils/LayoutManagerExtension.mm

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
#import "EnrichedTextInputView.h"
44
#import "StyleHeaders.h"
55
#import "ParagraphsUtils.h"
6-
#import "ImageData.h"
76

87
@implementation NSLayoutManager (LayoutManagerExtension)
98

@@ -58,8 +57,6 @@ - (void)my_drawBackgroundForGlyphRange:(NSRange)glyphRange atPoint:(CGPoint)orig
5857

5958
NSRange inputRange = NSMakeRange(0, typedInput->textView.textStorage.length);
6059

61-
[self drawImages:typedInput origin:origin inputRange:inputRange];
62-
6360
// it isn't the most performant but we have to check for all the blockquotes each time and redraw them
6461
NSArray *allBlockquotes = [bqStyle findAllOccurences:inputRange];
6562

@@ -135,48 +132,6 @@ - (void)my_drawBackgroundForGlyphRange:(NSRange)glyphRange atPoint:(CGPoint)orig
135132
}
136133
}
137134

138-
-(void)drawImages:(EnrichedTextInputView *)typedInput origin:(CGPoint)origin inputRange:(NSRange)inputRange
139-
{
140-
ImageStyle *imgStyle = typedInput->stylesDict[@([ImageStyle getStyleType])];
141-
142-
if (imgStyle == nullptr) { return; }
143-
NSArray *allImages = [imgStyle findAllOccurences:inputRange];
144-
145-
for (StylePair *pair in allImages) {
146-
ImageData *imageData = (ImageData *)pair.styleValue;
147-
NSRange imageCharRange = [pair.rangeValue rangeValue];
148-
149-
// Convert Character Range to Glyph Range
150-
NSRange imageGlyphRange = [self glyphRangeForCharacterRange:imageCharRange actualCharacterRange:NULL];
151-
152-
// Get the Text Container (needed for rect calculation)
153-
NSTextContainer *textContainer = [self textContainerForGlyphAtIndex:imageGlyphRange.location effectiveRange:NULL];
154-
155-
// boundingRectForGlyphRange gives us the calculated space for the \uFFFC character
156-
CGRect imageRect = [self boundingRectForGlyphRange:imageGlyphRange inTextContainer:textContainer];
157-
158-
imageRect.origin.x += origin.x;
159-
imageRect.origin.y += origin.y;
160-
imageRect.size.width = [typedInput->config imageWidth];
161-
imageRect.size.height = [typedInput->config imageHeight];
162-
163-
UIImage *imageToDraw = nil;
164-
165-
if (imageData.uri) {
166-
NSURL *url = [NSURL URLWithString:imageData.uri];
167-
imageToDraw = [UIImage imageWithContentsOfFile:url.path];
168-
}
169-
170-
if (imageToDraw) {
171-
[imageToDraw drawInRect:imageRect];
172-
} else {
173-
// Draw a placeholder box if image failed to load
174-
[[UIColor lightGrayColor] setFill];
175-
UIRectFill(imageRect);
176-
}
177-
}
178-
}
179-
180135
- (NSString *)markerForList:(NSTextList *)list charIndex:(NSUInteger)index input:(EnrichedTextInputView *)input {
181136
if(list.markerFormat == NSTextListMarkerDecimal) {
182137
NSString *fullText = input->textView.textStorage.string;

0 commit comments

Comments
 (0)