|
2 | 2 |
|
3 | 3 | @implementation MarkdownLayoutManager
|
4 | 4 |
|
| 5 | +- (BOOL)isRange:(NSRange)smallerRange inRange:(NSRange)largerRange { |
| 6 | + NSUInteger start = smallerRange.location; |
| 7 | + NSUInteger end = start + smallerRange.length; |
| 8 | + NSUInteger location = largerRange.location; |
| 9 | + return location >= start && location < end; |
| 10 | +} |
| 11 | + |
| 12 | +- (CGRect)rectByAddingPadding:(CGFloat)padding toRect:(CGRect)rect { |
| 13 | + rect.origin.x -= padding; |
| 14 | + rect.origin.y -= padding; |
| 15 | + rect.size.width += padding * 2; |
| 16 | + rect.size.height += padding * 2; |
| 17 | + return rect; |
| 18 | +} |
| 19 | + |
5 | 20 | - (void)drawBackgroundForGlyphRange:(NSRange)glyphsToShow atPoint:(CGPoint)origin {
|
6 |
| - [super drawBackgroundForGlyphRange:glyphsToShow atPoint:origin]; |
7 |
| - |
8 |
| - [self enumerateLineFragmentsForGlyphRange:glyphsToShow usingBlock:^(CGRect rect, CGRect usedRect, NSTextContainer * _Nonnull textContainer, NSRange glyphRange, BOOL * _Nonnull stop) { |
9 |
| - __block BOOL isBlockquote = NO; |
10 |
| - __block int currentDepth = 0; |
11 |
| - RCTMarkdownUtils *markdownUtils = [self valueForKey:@"markdownUtils"]; |
12 |
| - [markdownUtils.blockquoteRangesAndLevels enumerateObjectsUsingBlock:^(NSDictionary *item, NSUInteger idx, BOOL * _Nonnull stop) { |
13 |
| - NSRange range = [[item valueForKey:@"range"] rangeValue]; |
14 |
| - currentDepth = [[item valueForKey:@"depth"] unsignedIntegerValue]; |
15 |
| - NSUInteger start = range.location; |
16 |
| - NSUInteger end = start + range.length; |
17 |
| - NSUInteger location = glyphRange.location; |
18 |
| - if (location >= start && location < end) { |
19 |
| - isBlockquote = YES; |
20 |
| - *stop = YES; |
21 |
| - } |
| 21 | + [super drawBackgroundForGlyphRange:glyphsToShow atPoint:origin]; |
| 22 | + |
| 23 | + RCTMarkdownStyle *style = [_markdownUtils markdownStyle]; |
| 24 | + [self drawBlockquotesForRanges:[_markdownUtils blockquoteRangesAndLevels] andGlyphRange:glyphsToShow atPoint:origin withColor:[style blockquoteBorderColor] width:[style blockquoteBorderWidth] margin:[style blockquoteMarginLeft] andPadding:[style blockquotePaddingLeft]]; |
| 25 | + [self drawPreBackgroundForRanges:[_markdownUtils preRanges] atPoint:origin withColor:[style preBackgroundColor] borderColor:[style preBorderColor] borderWidth:[style preBorderWidth] borderRadius:[style preBorderRadius] andPadding:[style prePadding]]; |
| 26 | + [self drawCodeBackgroundForRanges:[_markdownUtils codeRanges] atPoint:origin withColor:[style codeBackgroundColor] borderColor:[style codeBorderColor] borderWidth:[style codeBorderWidth] borderRadius:[style codeBorderRadius] andPadding:[style codePadding]]; |
| 27 | +} |
| 28 | + |
| 29 | +- (void)drawBlockquotesForRanges:(NSArray<NSDictionary*>*)ranges andGlyphRange:(NSRange)glyphsToShow atPoint:(CGPoint)origin withColor:(UIColor*)color width:(CGFloat)width margin:(CGFloat)margin andPadding:(CGFloat)padding { |
| 30 | + [self enumerateLineFragmentsForGlyphRange:glyphsToShow usingBlock:^(CGRect rect, CGRect usedRect, NSTextContainer * _Nonnull textContainer, NSRange glyphRange, BOOL * _Nonnull stop) { |
| 31 | + __block BOOL isBlockquote = NO; |
| 32 | + __block int currentDepth = 0; |
| 33 | + |
| 34 | + [ranges enumerateObjectsUsingBlock:^(NSDictionary *item, NSUInteger idx, BOOL * _Nonnull stop) { |
| 35 | + NSRange range = [[item valueForKey:@"range"] rangeValue]; |
| 36 | + currentDepth = [[item valueForKey:@"depth"] unsignedIntegerValue]; |
| 37 | + if ([self isRange:range inRange:glyphRange]) { |
| 38 | + isBlockquote = YES; |
| 39 | + *stop = YES; |
| 40 | + } |
| 41 | + }]; |
| 42 | + if (isBlockquote) { |
| 43 | + CGFloat paddingLeft = origin.x; |
| 44 | + CGFloat paddingTop = origin.y; |
| 45 | + CGFloat y = paddingTop + rect.origin.y; |
| 46 | + CGFloat height = rect.size.height; |
| 47 | + CGFloat shift = margin + width + padding; |
| 48 | + for (int level = 0; level < currentDepth; level++) { |
| 49 | + CGFloat x = paddingLeft + (level * shift) + margin; |
| 50 | + CGRect lineRect = CGRectMake(x, y, width, height); |
| 51 | + [color setFill]; |
| 52 | + UIRectFill(lineRect); |
| 53 | + } |
| 54 | + } |
| 55 | + }]; |
| 56 | +} |
| 57 | + |
| 58 | +- (void)drawPreBackgroundForRanges:(NSArray<NSValue*>*)ranges atPoint:(CGPoint)origin withColor:(UIColor*)backgroundColor borderColor:(UIColor*)borderColor borderWidth:(CGFloat)borderWidth borderRadius:(CGFloat)borderRadius andPadding:(CGFloat)padding { |
| 59 | + __block CGRect preRect = CGRectNull; |
| 60 | + [ranges enumerateObjectsUsingBlock:^(NSValue *item, NSUInteger idx, BOOL * _Nonnull stop) { |
| 61 | + NSRange range = [item rangeValue]; |
| 62 | + range.location += 1; |
| 63 | + range.length -= 1; |
| 64 | + |
| 65 | + [self enumerateLineFragmentsForGlyphRange:range usingBlock:^(CGRect rect, CGRect usedRect, NSTextContainer * _Nonnull textContainer, NSRange glyphRange, BOOL * _Nonnull stop) { |
| 66 | + if (CGRectIsNull(preRect)) { |
| 67 | + preRect = usedRect; |
| 68 | + CGFloat paddingLeft = origin.x; |
| 69 | + preRect.origin.x += paddingLeft; |
| 70 | + CGFloat paddingTop = origin.y; |
| 71 | + preRect.origin.y += paddingTop; |
| 72 | + } else { |
| 73 | + CGFloat usedWidth = usedRect.size.width; |
| 74 | + if (usedWidth > preRect.size.width) { |
| 75 | + preRect.size.width = usedWidth; |
| 76 | + } |
| 77 | + preRect.size.height += usedRect.size.height; |
| 78 | + } |
| 79 | + }]; |
| 80 | + |
| 81 | + if (!CGRectIsNull(preRect)) { |
| 82 | + preRect = [self rectByAddingPadding:padding toRect:preRect]; |
| 83 | + [self drawBackgroundWithColor:backgroundColor borderColor:borderColor borderWidth:borderWidth andBorderRadius:borderRadius forRect:preRect isLeftOpen:NO isRightOpen:NO]; |
| 84 | + preRect = CGRectNull; |
| 85 | + } |
| 86 | + }]; |
| 87 | +} |
| 88 | + |
| 89 | +- (void)drawCodeBackgroundForRanges:(NSArray<NSValue*>*)ranges atPoint:(CGPoint)origin withColor:(UIColor*)backgroundColor borderColor:(UIColor*)borderColor borderWidth:(CGFloat)borderWidth borderRadius:(CGFloat)borderRadius andPadding:(CGFloat)padding { |
| 90 | + [ranges enumerateObjectsUsingBlock:^(NSValue *item, NSUInteger idx, BOOL * _Nonnull stop) { |
| 91 | + NSRange range = [item rangeValue]; |
| 92 | + [self enumerateLineFragmentsForGlyphRange:range usingBlock:^(CGRect rect, CGRect usedRect, NSTextContainer * _Nonnull textContainer, NSRange glyphRange, BOOL * _Nonnull stop) { |
| 93 | + BOOL isLeftSideOpen = YES; |
| 94 | + BOOL isRightSideOpen = YES; |
| 95 | + |
| 96 | + NSRange adjustedRange = glyphRange; |
| 97 | + if (range.location > adjustedRange.location) { |
| 98 | + adjustedRange.length -= range.location - adjustedRange.location; |
| 99 | + adjustedRange.location = range.location; |
| 100 | + isLeftSideOpen = NO; |
| 101 | + } |
| 102 | + |
| 103 | + NSUInteger rangeEndLocation = range.location + range.length; |
| 104 | + NSUInteger adjustedRangeEndLocation = adjustedRange.location + adjustedRange.length; |
| 105 | + if (rangeEndLocation < adjustedRangeEndLocation) { |
| 106 | + adjustedRange.length -= adjustedRangeEndLocation - rangeEndLocation; |
| 107 | + isRightSideOpen = NO; |
| 108 | + } |
| 109 | + |
| 110 | + CGRect codeRect = [self boundingRectForGlyphRange:adjustedRange inTextContainer:textContainer]; |
| 111 | + CGFloat paddingLeft = origin.x; |
| 112 | + codeRect.origin.x += paddingLeft; |
| 113 | + CGFloat paddingTop = origin.y; |
| 114 | + codeRect.origin.y += paddingTop; |
| 115 | + codeRect = [self rectByAddingPadding:padding toRect:codeRect]; |
| 116 | + [self drawBackgroundWithColor:backgroundColor borderColor:borderColor borderWidth:borderWidth andBorderRadius:borderRadius forRect:codeRect isLeftOpen:isLeftSideOpen isRightOpen:isRightSideOpen]; |
| 117 | + }]; |
22 | 118 | }];
|
23 |
| - if (isBlockquote) { |
24 |
| - CGFloat paddingLeft = origin.x; |
25 |
| - CGFloat paddingTop = origin.y; |
26 |
| - CGFloat y = paddingTop + rect.origin.y; |
27 |
| - CGFloat width = markdownUtils.markdownStyle.blockquoteBorderWidth; |
28 |
| - CGFloat height = rect.size.height; |
29 |
| - CGFloat shift = markdownUtils.markdownStyle.blockquoteMarginLeft + markdownUtils.markdownStyle.blockquoteBorderWidth + markdownUtils.markdownStyle.blockquotePaddingLeft; |
30 |
| - for (int level = 0; level < currentDepth; level++) { |
31 |
| - CGFloat x = paddingLeft + (level * shift) + markdownUtils.markdownStyle.blockquoteMarginLeft; |
32 |
| - CGRect lineRect = CGRectMake(x, y, width, height); |
33 |
| - [markdownUtils.markdownStyle.blockquoteBorderColor setFill]; |
34 |
| - UIRectFill(lineRect); |
35 |
| - } |
| 119 | +} |
| 120 | + |
| 121 | +- (void)drawBackgroundWithColor:(UIColor*)backgroundColor borderColor:(UIColor*)borderColor borderWidth:(CGFloat)borderWidth andBorderRadius:(CGFloat)radius forRect:(CGRect)rect isLeftOpen:(BOOL)isLeftOpen isRightOpen:(BOOL)isRightOpen { |
| 122 | + UIRectCorner corners = 0; |
| 123 | + if (!isLeftOpen) { |
| 124 | + corners |= UIRectCornerTopLeft | UIRectCornerBottomLeft; |
36 | 125 | }
|
37 |
| - }]; |
| 126 | + if (!isRightOpen) { |
| 127 | + corners |= UIRectCornerTopRight | UIRectCornerBottomRight; |
| 128 | + } |
| 129 | + UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:rect byRoundingCorners:corners cornerRadii:CGSizeMake(radius, radius)]; |
| 130 | + |
| 131 | + [backgroundColor setFill]; |
| 132 | + [path fill]; |
| 133 | + [borderColor setStroke]; |
| 134 | + [path setLineWidth:borderWidth]; |
| 135 | + [path stroke]; |
| 136 | + |
| 137 | + if (isLeftOpen) { |
| 138 | + [self openSideForRect:rect withBorderWidth:borderWidth isLeft:YES]; |
| 139 | + } |
| 140 | + if (isRightOpen) { |
| 141 | + [self openSideForRect:rect withBorderWidth:borderWidth isLeft:NO]; |
| 142 | + } |
| 143 | +} |
| 144 | + |
| 145 | +- (void)openSideForRect:(CGRect)rect withBorderWidth:(CGFloat)borderWidth isLeft:(BOOL)isLeft { |
| 146 | + UIBezierPath *path = [[UIBezierPath alloc] init]; |
| 147 | + CGFloat x = isLeft ? CGRectGetMinX(rect) : CGRectGetMaxX(rect); |
| 148 | + [path moveToPoint:CGPointMake(x, CGRectGetMinY(rect) - borderWidth)]; |
| 149 | + [path addLineToPoint:CGPointMake(x, CGRectGetMaxY(rect) + borderWidth)]; |
| 150 | + [[UIColor clearColor] setStroke]; |
| 151 | + [path setLineWidth:borderWidth + 1]; |
| 152 | + [path strokeWithBlendMode:kCGBlendModeClear alpha:1.0]; |
38 | 153 | }
|
39 | 154 |
|
40 | 155 | @end
|
0 commit comments