@expensify/react-native-live-markdown 0.1.168 → 0.1.169
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/ios/RCTMarkdownUtils.mm +84 -81
- package/package.json +1 -1
package/ios/RCTMarkdownUtils.mm
CHANGED
|
@@ -64,90 +64,11 @@ using namespace facebook;
|
|
|
64
64
|
for (size_t i = 0, n = ranges.size(rt); i < n; ++i) {
|
|
65
65
|
const auto &item = ranges.getValueAtIndex(rt, i).asObject(rt);
|
|
66
66
|
const auto &type = item.getProperty(rt, "type").asString(rt).utf8(rt);
|
|
67
|
-
const auto &
|
|
67
|
+
const auto &start = static_cast<int>(item.getProperty(rt, "start").asNumber());
|
|
68
68
|
const auto &length = static_cast<int>(item.getProperty(rt, "length").asNumber());
|
|
69
69
|
const auto &depth = item.hasProperty(rt, "depth") ? static_cast<int>(item.getProperty(rt, "depth").asNumber()) : 1;
|
|
70
70
|
|
|
71
|
-
|
|
72
|
-
continue;
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
NSRange range = NSMakeRange(location, length);
|
|
76
|
-
|
|
77
|
-
if (type == "bold" || type == "italic" || type == "code" || type == "pre" || type == "h1" || type == "emoji") {
|
|
78
|
-
UIFont *font = [attributedString attribute:NSFontAttributeName atIndex:location effectiveRange:NULL];
|
|
79
|
-
if (type == "bold") {
|
|
80
|
-
font = [RCTFont updateFont:font withWeight:@"bold"];
|
|
81
|
-
} else if (type == "italic") {
|
|
82
|
-
font = [RCTFont updateFont:font withStyle:@"italic"];
|
|
83
|
-
} else if (type == "code") {
|
|
84
|
-
font = [RCTFont updateFont:font withFamily:_markdownStyle.codeFontFamily
|
|
85
|
-
size:[NSNumber numberWithFloat:_markdownStyle.codeFontSize]
|
|
86
|
-
weight:nil
|
|
87
|
-
style:nil
|
|
88
|
-
variant:nil
|
|
89
|
-
scaleMultiplier:0];
|
|
90
|
-
} else if (type == "pre") {
|
|
91
|
-
font = [RCTFont updateFont:font withFamily:_markdownStyle.preFontFamily
|
|
92
|
-
size:[NSNumber numberWithFloat:_markdownStyle.preFontSize]
|
|
93
|
-
weight:nil
|
|
94
|
-
style:nil
|
|
95
|
-
variant:nil
|
|
96
|
-
scaleMultiplier:0];
|
|
97
|
-
} else if (type == "h1") {
|
|
98
|
-
font = [RCTFont updateFont:font withFamily:nil
|
|
99
|
-
size:[NSNumber numberWithFloat:_markdownStyle.h1FontSize]
|
|
100
|
-
weight:@"bold"
|
|
101
|
-
style:nil
|
|
102
|
-
variant:nil
|
|
103
|
-
scaleMultiplier:0];
|
|
104
|
-
} else if (type == "emoji") {
|
|
105
|
-
font = [RCTFont updateFont:font withFamily:nil
|
|
106
|
-
size:[NSNumber numberWithFloat:_markdownStyle.emojiFontSize]
|
|
107
|
-
weight:nil
|
|
108
|
-
style:nil
|
|
109
|
-
variant:nil
|
|
110
|
-
scaleMultiplier:0];
|
|
111
|
-
}
|
|
112
|
-
[attributedString addAttribute:NSFontAttributeName value:font range:range];
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
if (type == "syntax") {
|
|
116
|
-
[attributedString addAttribute:NSForegroundColorAttributeName value:_markdownStyle.syntaxColor range:range];
|
|
117
|
-
} else if (type == "strikethrough") {
|
|
118
|
-
[attributedString addAttribute:NSStrikethroughStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:range];
|
|
119
|
-
} else if (type == "code") {
|
|
120
|
-
[attributedString addAttribute:NSForegroundColorAttributeName value:_markdownStyle.codeColor range:range];
|
|
121
|
-
[attributedString addAttribute:NSBackgroundColorAttributeName value:_markdownStyle.codeBackgroundColor range:range];
|
|
122
|
-
} else if (type == "mention-here") {
|
|
123
|
-
[attributedString addAttribute:NSForegroundColorAttributeName value:_markdownStyle.mentionHereColor range:range];
|
|
124
|
-
[attributedString addAttribute:NSBackgroundColorAttributeName value:_markdownStyle.mentionHereBackgroundColor range:range];
|
|
125
|
-
} else if (type == "mention-user") {
|
|
126
|
-
// TODO: change mention color when it mentions current user
|
|
127
|
-
[attributedString addAttribute:NSForegroundColorAttributeName value:_markdownStyle.mentionUserColor range:range];
|
|
128
|
-
[attributedString addAttribute:NSBackgroundColorAttributeName value:_markdownStyle.mentionUserBackgroundColor range:range];
|
|
129
|
-
} else if (type == "mention-report") {
|
|
130
|
-
[attributedString addAttribute:NSForegroundColorAttributeName value:_markdownStyle.mentionReportColor range:range];
|
|
131
|
-
[attributedString addAttribute:NSBackgroundColorAttributeName value:_markdownStyle.mentionReportBackgroundColor range:range];
|
|
132
|
-
} else if (type == "link") {
|
|
133
|
-
[attributedString addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:range];
|
|
134
|
-
[attributedString addAttribute:NSForegroundColorAttributeName value:_markdownStyle.linkColor range:range];
|
|
135
|
-
} else if (type == "blockquote") {
|
|
136
|
-
CGFloat indent = (_markdownStyle.blockquoteMarginLeft + _markdownStyle.blockquoteBorderWidth + _markdownStyle.blockquotePaddingLeft) * depth;
|
|
137
|
-
NSMutableParagraphStyle *paragraphStyle = [NSMutableParagraphStyle new];
|
|
138
|
-
paragraphStyle.firstLineHeadIndent = indent;
|
|
139
|
-
paragraphStyle.headIndent = indent;
|
|
140
|
-
[attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:range];
|
|
141
|
-
[_blockquoteRangesAndLevels addObject:@{
|
|
142
|
-
@"range": [NSValue valueWithRange:range],
|
|
143
|
-
@"depth": @(depth)
|
|
144
|
-
}];
|
|
145
|
-
} else if (type == "pre") {
|
|
146
|
-
[attributedString addAttribute:NSForegroundColorAttributeName value:_markdownStyle.preColor range:range];
|
|
147
|
-
NSRange rangeForBackground = [inputString characterAtIndex:range.location] == '\n' ? NSMakeRange(range.location + 1, range.length - 1) : range;
|
|
148
|
-
[attributedString addAttribute:NSBackgroundColorAttributeName value:_markdownStyle.preBackgroundColor range:rangeForBackground];
|
|
149
|
-
// TODO: pass background color and ranges to layout manager
|
|
150
|
-
}
|
|
71
|
+
[self applyRangeToAttributedString:attributedString type:type start:start length:length depth:depth];
|
|
151
72
|
}
|
|
152
73
|
|
|
153
74
|
RCTApplyBaselineOffset(attributedString);
|
|
@@ -160,7 +81,89 @@ using namespace facebook;
|
|
|
160
81
|
_prevMarkdownStyle = _markdownStyle;
|
|
161
82
|
|
|
162
83
|
return attributedString;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
- (void)applyRangeToAttributedString:(NSMutableAttributedString *)attributedString type:(const std::string)type start:(const int)start length:(const int)length depth:(const int)depth {
|
|
88
|
+
if (length == 0 || start + length > attributedString.length) {
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
NSRange range = NSMakeRange(start, length);
|
|
93
|
+
|
|
94
|
+
if (type == "bold" || type == "italic" || type == "code" || type == "pre" || type == "h1" || type == "emoji") {
|
|
95
|
+
UIFont *font = [attributedString attribute:NSFontAttributeName atIndex:start effectiveRange:NULL];
|
|
96
|
+
if (type == "bold") {
|
|
97
|
+
font = [RCTFont updateFont:font withWeight:@"bold"];
|
|
98
|
+
} else if (type == "italic") {
|
|
99
|
+
font = [RCTFont updateFont:font withStyle:@"italic"];
|
|
100
|
+
} else if (type == "code") {
|
|
101
|
+
font = [RCTFont updateFont:font withFamily:_markdownStyle.codeFontFamily
|
|
102
|
+
size:[NSNumber numberWithFloat:_markdownStyle.codeFontSize]
|
|
103
|
+
weight:nil
|
|
104
|
+
style:nil
|
|
105
|
+
variant:nil
|
|
106
|
+
scaleMultiplier:0];
|
|
107
|
+
} else if (type == "pre") {
|
|
108
|
+
font = [RCTFont updateFont:font withFamily:_markdownStyle.preFontFamily
|
|
109
|
+
size:[NSNumber numberWithFloat:_markdownStyle.preFontSize]
|
|
110
|
+
weight:nil
|
|
111
|
+
style:nil
|
|
112
|
+
variant:nil
|
|
113
|
+
scaleMultiplier:0];
|
|
114
|
+
} else if (type == "h1") {
|
|
115
|
+
font = [RCTFont updateFont:font withFamily:nil
|
|
116
|
+
size:[NSNumber numberWithFloat:_markdownStyle.h1FontSize]
|
|
117
|
+
weight:@"bold"
|
|
118
|
+
style:nil
|
|
119
|
+
variant:nil
|
|
120
|
+
scaleMultiplier:0];
|
|
121
|
+
} else if (type == "emoji") {
|
|
122
|
+
font = [RCTFont updateFont:font withFamily:nil
|
|
123
|
+
size:[NSNumber numberWithFloat:_markdownStyle.emojiFontSize]
|
|
124
|
+
weight:nil
|
|
125
|
+
style:nil
|
|
126
|
+
variant:nil
|
|
127
|
+
scaleMultiplier:0];
|
|
128
|
+
}
|
|
129
|
+
[attributedString addAttribute:NSFontAttributeName value:font range:range];
|
|
130
|
+
}
|
|
163
131
|
|
|
132
|
+
if (type == "syntax") {
|
|
133
|
+
[attributedString addAttribute:NSForegroundColorAttributeName value:_markdownStyle.syntaxColor range:range];
|
|
134
|
+
} else if (type == "strikethrough") {
|
|
135
|
+
[attributedString addAttribute:NSStrikethroughStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:range];
|
|
136
|
+
} else if (type == "code") {
|
|
137
|
+
[attributedString addAttribute:NSForegroundColorAttributeName value:_markdownStyle.codeColor range:range];
|
|
138
|
+
[attributedString addAttribute:NSBackgroundColorAttributeName value:_markdownStyle.codeBackgroundColor range:range];
|
|
139
|
+
} else if (type == "mention-here") {
|
|
140
|
+
[attributedString addAttribute:NSForegroundColorAttributeName value:_markdownStyle.mentionHereColor range:range];
|
|
141
|
+
[attributedString addAttribute:NSBackgroundColorAttributeName value:_markdownStyle.mentionHereBackgroundColor range:range];
|
|
142
|
+
} else if (type == "mention-user") {
|
|
143
|
+
// TODO: change mention color when it mentions current user
|
|
144
|
+
[attributedString addAttribute:NSForegroundColorAttributeName value:_markdownStyle.mentionUserColor range:range];
|
|
145
|
+
[attributedString addAttribute:NSBackgroundColorAttributeName value:_markdownStyle.mentionUserBackgroundColor range:range];
|
|
146
|
+
} else if (type == "mention-report") {
|
|
147
|
+
[attributedString addAttribute:NSForegroundColorAttributeName value:_markdownStyle.mentionReportColor range:range];
|
|
148
|
+
[attributedString addAttribute:NSBackgroundColorAttributeName value:_markdownStyle.mentionReportBackgroundColor range:range];
|
|
149
|
+
} else if (type == "link") {
|
|
150
|
+
[attributedString addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:range];
|
|
151
|
+
[attributedString addAttribute:NSForegroundColorAttributeName value:_markdownStyle.linkColor range:range];
|
|
152
|
+
} else if (type == "blockquote") {
|
|
153
|
+
CGFloat indent = (_markdownStyle.blockquoteMarginLeft + _markdownStyle.blockquoteBorderWidth + _markdownStyle.blockquotePaddingLeft) * depth;
|
|
154
|
+
NSMutableParagraphStyle *paragraphStyle = [NSMutableParagraphStyle new];
|
|
155
|
+
paragraphStyle.firstLineHeadIndent = indent;
|
|
156
|
+
paragraphStyle.headIndent = indent;
|
|
157
|
+
[attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:range];
|
|
158
|
+
[_blockquoteRangesAndLevels addObject:@{
|
|
159
|
+
@"range": [NSValue valueWithRange:range],
|
|
160
|
+
@"depth": @(depth)
|
|
161
|
+
}];
|
|
162
|
+
} else if (type == "pre") {
|
|
163
|
+
[attributedString addAttribute:NSForegroundColorAttributeName value:_markdownStyle.preColor range:range];
|
|
164
|
+
NSRange rangeForBackground = [[attributedString string] characterAtIndex:range.location] == '\n' ? NSMakeRange(range.location + 1, range.length - 1) : range;
|
|
165
|
+
[attributedString addAttribute:NSBackgroundColorAttributeName value:_markdownStyle.preBackgroundColor range:rangeForBackground];
|
|
166
|
+
// TODO: pass background color and ranges to layout manager
|
|
164
167
|
}
|
|
165
168
|
}
|
|
166
169
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@expensify/react-native-live-markdown",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.169",
|
|
4
4
|
"description": "Drop-in replacement for React Native's TextInput component with Markdown formatting.",
|
|
5
5
|
"main": "lib/commonjs/index",
|
|
6
6
|
"module": "lib/module/index",
|