Skip to content

Commit 4ab8e81

Browse files
authored
fix: draw marker for only the first line of a list point (#108)
1 parent f75728c commit 4ab8e81

1 file changed

Lines changed: 33 additions & 27 deletions

File tree

ios/utils/LayoutManagerExtension.mm

Lines changed: 33 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#import "EditorManager.h"
44
#import "ReactNativeRichTextEditorView.h"
55
#import "StyleHeaders.h"
6+
#import "ParagraphsUtils.h"
67

78
@implementation NSLayoutManager (LayoutManagerExtension)
89

@@ -77,42 +78,47 @@ - (void)my_drawBackgroundForGlyphRange:(NSRange)glyphRange atPoint:(CGPoint)orig
7778
[allLists addObjectsFromArray:[olStyle findAllOccurences:editorRange]];
7879

7980
for(StylePair *pair in allLists) {
80-
NSRange paragraphRange = [typedEditor->textView.textStorage.string paragraphRangeForRange:[pair.rangeValue rangeValue]];
81-
NSRange paragraphGlyphRange = [self glyphRangeForCharacterRange:paragraphRange actualCharacterRange:nullptr];
82-
8381
NSParagraphStyle *pStyle = (NSParagraphStyle *)pair.styleValue;
8482
NSDictionary *markerAttributes = @{
8583
NSFontAttributeName: [typedEditor->config primaryFont],
8684
NSForegroundColorAttributeName: [typedEditor->config primaryColor]
8785
};
8886

89-
[self enumerateLineFragmentsForGlyphRange:paragraphGlyphRange
90-
usingBlock:^(CGRect rect, CGRect usedRect, NSTextContainer *container, NSRange lineGlyphRange, BOOL *stop) {
91-
NSString *marker = [self markerForList:pStyle.textLists.firstObject charIndex:[self characterIndexForGlyphAtIndex:lineGlyphRange.location] editor:typedEditor];
92-
93-
if(pStyle.textLists.firstObject.markerFormat == NSTextListMarkerDecimal) {
94-
CGFloat gapWidth = [typedEditor->config orderedListGapWidth];
95-
CGFloat markerWidth = [marker sizeWithAttributes:markerAttributes].width;
96-
CGFloat rightEdge = usedRect.origin.x - gapWidth;
97-
CGFloat numberX = rightEdge - markerWidth;
98-
99-
[marker drawAtPoint:CGPointMake(numberX, usedRect.origin.y + origin.y) withAttributes:markerAttributes];
100-
} else {
101-
CGFloat gapWidth = [typedEditor->config unorderedListGapWidth];
102-
CGFloat bulletSize = [typedEditor->config unorderedListBulletSize];
103-
CGFloat bulletX = usedRect.origin.x - gapWidth - bulletSize;
104-
CGFloat centerY = CGRectGetMidY(usedRect);
87+
NSArray *paragraphs = [ParagraphsUtils getSeparateParagraphsRangesIn:typedEditor->textView range:[pair.rangeValue rangeValue]];
88+
89+
for(NSValue *paragraph in paragraphs) {
90+
NSRange paragraphGlyphRange = [self glyphRangeForCharacterRange:[paragraph rangeValue] actualCharacterRange:nullptr];
91+
92+
[self enumerateLineFragmentsForGlyphRange:paragraphGlyphRange
93+
usingBlock:^(CGRect rect, CGRect usedRect, NSTextContainer *container, NSRange lineGlyphRange, BOOL *stop) {
94+
NSString *marker = [self markerForList:pStyle.textLists.firstObject charIndex:[self characterIndexForGlyphAtIndex:lineGlyphRange.location] editor:typedEditor];
10595

106-
CGContextRef context = UIGraphicsGetCurrentContext();
107-
CGContextSaveGState(context); {
108-
[[typedEditor->config unorderedListBulletColor] setFill];
109-
CGContextAddArc(context, bulletX, centerY, bulletSize/2, 0, 2 * M_PI, YES);
110-
CGContextFillPath(context);
96+
if(pStyle.textLists.firstObject.markerFormat == NSTextListMarkerDecimal) {
97+
CGFloat gapWidth = [typedEditor->config orderedListGapWidth];
98+
CGFloat markerWidth = [marker sizeWithAttributes:markerAttributes].width;
99+
CGFloat rightEdge = usedRect.origin.x - gapWidth;
100+
CGFloat numberX = rightEdge - markerWidth;
101+
102+
[marker drawAtPoint:CGPointMake(numberX, usedRect.origin.y + origin.y) withAttributes:markerAttributes];
103+
} else {
104+
CGFloat gapWidth = [typedEditor->config unorderedListGapWidth];
105+
CGFloat bulletSize = [typedEditor->config unorderedListBulletSize];
106+
CGFloat bulletX = usedRect.origin.x - gapWidth - bulletSize;
107+
CGFloat centerY = CGRectGetMidY(usedRect);
108+
109+
CGContextRef context = UIGraphicsGetCurrentContext();
110+
CGContextSaveGState(context); {
111+
[[typedEditor->config unorderedListBulletColor] setFill];
112+
CGContextAddArc(context, bulletX, centerY, bulletSize/2, 0, 2 * M_PI, YES);
113+
CGContextFillPath(context);
114+
}
115+
CGContextRestoreGState(context);
111116
}
112-
CGContextRestoreGState(context);
117+
// only first line of a list gets its marker drawn
118+
*stop = YES;
113119
}
114-
}
115-
];
120+
];
121+
}
116122
}
117123
}
118124

0 commit comments

Comments
 (0)