react-native-enriched-markdown-scaffold 1.0.2-scaffold.11 → 1.0.2-scaffold.13

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.
@@ -663,12 +663,12 @@ class EnrichedMarkdown
663
663
  }
664
664
 
665
665
  is RenderedSegment.Math -> {
666
- mathContainerClass
667
- ?.getMethod("applyLatex", String::class.java)
668
- ?.invoke(view, segment.latex)
669
666
  mathContainerClass
670
667
  ?.getMethod("setHighlighted", Boolean::class.javaPrimitiveType)
671
668
  ?.invoke(view, segment.highlighted)
669
+ mathContainerClass
670
+ ?.getMethod("applyLatex", String::class.java)
671
+ ?.invoke(view, segment.latex)
672
672
  }
673
673
 
674
674
  is RenderedSegment.CodeBlock -> {
@@ -809,8 +809,8 @@ class EnrichedMarkdown
809
809
  runCatching {
810
810
  resolvedClass.getMethod("setOnMenuSuppressed", Function2::class.java).invoke(view, onMenuSuppressed)
811
811
  }.onFailure { Log.e(TAG, "Failed to set onMenuSuppressed on math view", it) }
812
- resolvedClass.getMethod("applyLatex", String::class.java).invoke(view, segment.latex)
813
812
  resolvedClass.getMethod("setHighlighted", Boolean::class.javaPrimitiveType).invoke(view, segment.highlighted)
813
+ resolvedClass.getMethod("applyLatex", String::class.java).invoke(view, segment.latex)
814
814
  view
815
815
  } catch (e: Exception) {
816
816
  Log.e(TAG, "Failed to create math view", e)
@@ -13,7 +13,16 @@ class HighlightRenderer : NodeRenderer {
13
13
  onLinkLongPress: ((String) -> Unit)?,
14
14
  factory: RendererFactory,
15
15
  ) {
16
- factory.renderWithSpan(builder, { factory.renderChildren(node, builder, onLinkPress, onLinkLongPress) }) { start, end, blockStyle ->
16
+ val renderChildren = {
17
+ val wasInHighlight = factory.inHighlight
18
+ factory.inHighlight = true
19
+ try {
20
+ factory.renderChildren(node, builder, onLinkPress, onLinkLongPress)
21
+ } finally {
22
+ factory.inHighlight = wasInHighlight
23
+ }
24
+ }
25
+ factory.renderWithSpan(builder, renderChildren) { start, end, blockStyle ->
17
26
  builder.setSpan(
18
27
  HighlightSpan(factory.styleCache, blockStyle),
19
28
  start,
@@ -32,6 +32,13 @@ class RendererFactory(
32
32
 
33
33
  val styleCache = SpanStyleCache(config.style)
34
34
 
35
+ /**
36
+ * Rendering inside a `==…==` span: a formula there takes the highlight colour, which a
37
+ * span over the text cannot give it. Read by the maths renderer, which lives in the
38
+ * optional math source set and cannot be reached from [HighlightRenderer].
39
+ */
40
+ var inHighlight: Boolean = false
41
+
35
42
  /**
36
43
  * Spans registered here are applied after the full document tree is rendered, so they
37
44
  * always end up after block-level spans in the SpannableStringBuilder's internal array.
@@ -63,6 +70,7 @@ class RendererFactory(
63
70
  fun resetForNewRender() {
64
71
  blockStyleContext.resetForNewRender()
65
72
  deferredSpans.clear()
73
+ inHighlight = false
66
74
  }
67
75
 
68
76
  fun createImageSpan(
@@ -24,6 +24,9 @@ class MathInlineRenderer(
24
24
  if (latex.isEmpty()) return
25
25
 
26
26
  val blockStyle = factory.blockStyleContext.requireBlockStyle()
27
+ val mathColor = config.style.inlineMathStyle.color
28
+ // Inside `==…==` the formula takes the highlight colour, by the rule text follows.
29
+ val textColor = if (factory.inHighlight) factory.styleCache.getHighlightColorFor(mathColor) else mathColor
27
30
 
28
31
  val start = builder.length
29
32
  builder.append("\uFFFC")
@@ -34,7 +37,7 @@ class MathInlineRenderer(
34
37
  context = context,
35
38
  latex = latex,
36
39
  fontSize = blockStyle.fontSize,
37
- textColor = config.style.inlineMathStyle.color,
40
+ textColor = textColor,
38
41
  paddingVertical = config.style.inlineMathStyle.paddingVertical,
39
42
  fontWeight = config.style.inlineMathStyle.fontWeight,
40
43
  glyphOverflow = config.style.inlineMathStyle.glyphOverflow,
@@ -63,11 +63,20 @@ class MathContainerView(
63
63
 
64
64
  private val highlightBackgroundColor = styleConfig.highlightStyle.backgroundColor
65
65
 
66
- // Set reflectively: washes the formula's own frame for a `==$$…$$==` block.
66
+ // The highlight colour as text resolves it: the paragraph colour left in place means
67
+ // "inherit", which for a formula is its own colour.
68
+ private val highlightColor =
69
+ styleConfig.highlightStyle.color.let { if (it == styleConfig.paragraphStyle.color) mathStyle.color else it }
70
+
71
+ // Set reflectively, before applyLatex: a `==$$…$$==` block washes the formula's own
72
+ // frame and parses the formula in the highlight colour. Changed afterwards, the cached
73
+ // latex is parsed again.
67
74
  var highlighted: Boolean = false
68
75
  set(value) {
76
+ val changed = field != value
69
77
  field = value
70
78
  mathView.setBackgroundColor(if (value) highlightBackgroundColor else Color.TRANSPARENT)
79
+ if (changed && cachedLatex.isNotEmpty()) applyLatex(cachedLatex)
71
80
  }
72
81
 
73
82
  init {
@@ -116,7 +125,8 @@ class MathContainerView(
116
125
  fun applyLatex(latex: String) {
117
126
  cachedLatex = latex
118
127
  try {
119
- val displayList = RaTeXEngine.parseBlocking(latex, displayMode = true, color = mathStyle.color)
128
+ val color = if (highlighted) highlightColor else mathStyle.color
129
+ val displayList = RaTeXEngine.parseBlocking(latex, displayMode = true, color = color)
120
130
  mathView.renderer =
121
131
  RaTeXRenderer(displayList, mathStyle.fontSize, ratexTypefaceLoader(mathStyle.fontWeight))
122
132
  } catch (e: Exception) {
@@ -407,8 +407,8 @@ static char kENRMSegmentFadeAnimatorKey;
407
407
  updateView:^(RCTUIView *view, ENRMRenderedSegment *segment) {
408
408
  ENRMMathSegment *mathSegment = (ENRMMathSegment *)segment.mathSegment;
409
409
  ENRMMathContainerView *mathView = (ENRMMathContainerView *)view;
410
- [mathView applyLatex:mathSegment.latex];
411
410
  mathView.highlighted = mathSegment.highlighted;
411
+ [mathView applyLatex:mathSegment.latex];
412
412
  }]];
413
413
  #endif
414
414
 
@@ -1023,8 +1023,8 @@ static char kENRMSegmentFadeAnimatorKey;
1023
1023
  mathView.menuOptions = [self blockMenuOptions];
1024
1024
  mathView.onCopyRequest = [self blockCopyRequestForSource:@"math"];
1025
1025
  mathView.onMenuSuppressed = [self blockMenuSuppressedForSource:@"math" view:mathView];
1026
- [mathView applyLatex:mathSegment.latex];
1027
1026
  mathView.highlighted = mathSegment.highlighted;
1027
+ [mathView applyLatex:mathSegment.latex];
1028
1028
  return mathView;
1029
1029
  }
1030
1030
  #endif
@@ -863,6 +863,7 @@ static const NSTimeInterval kENRMAtomicSnapPollInterval = 0.1;
863
863
  // Positions are relative to the parsed fragment, so shift them onto the spot
864
864
  // it landed. Pasted markdown carries formulas like any other source.
865
865
  [_mathChipController applyAtoms:parsed.mathAtoms offset:insertLocation];
866
+ [self emitMarkdownAfterAppliedAtoms:parsed.mathAtoms];
866
867
  return;
867
868
  }
868
869
 
@@ -887,6 +888,17 @@ static const NSTimeInterval kENRMAtomicSnapPollInterval = 0.1;
887
888
  formattingRanges:formattingRanges
888
889
  blockRanges:blockRanges];
889
890
  [_mathChipController applyAtoms:parsed.mathAtoms offset:insertLocation + lead];
891
+ [self emitMarkdownAfterAppliedAtoms:parsed.mathAtoms];
892
+ }
893
+
894
+ /// replaceTextInRange: emits markdown before the chips get their LaTeX, so that
895
+ /// markdown has a bare U+FFFC where each formula goes. Emit again once the chips
896
+ /// are applied. Android applies chips before its emit, so it does not need this.
897
+ - (void)emitMarkdownAfterAppliedAtoms:(NSArray<ENRMMathAtom *> *)atoms
898
+ {
899
+ if (atoms.count > 0 && _inputEventEmitter.emitMarkdown) {
900
+ [_inputEventEmitter emitOnChangeMarkdown];
901
+ }
890
902
  }
891
903
 
892
904
  #pragma mark - Formatting
@@ -1517,6 +1529,7 @@ static const NSTimeInterval kENRMAtomicSnapPollInterval = 0.1;
1517
1529
 
1518
1530
  // replaceTextInRange: already emitted text/selection/formatting for the edit, but
1519
1531
  // the caret moved after it and the chip attribute changed the measured height.
1532
+ [self emitMarkdownAfterAppliedAtoms:@[ atom ]];
1520
1533
  [_inputEventEmitter emitOnChangeSelection];
1521
1534
  [self requestHeightUpdate];
1522
1535
  [self scheduleRelayoutIfNeeded];
@@ -27,7 +27,14 @@
27
27
  ENRMMathInlineAttachment *attachment = [[ENRMMathInlineAttachment alloc] init];
28
28
  attachment.latex = latex;
29
29
  attachment.fontSize = currentFont ? currentFont.pointSize : [_config paragraphFontSize];
30
- attachment.mathTextColor = [_config inlineMathColor];
30
+ RCTUIColor *mathColor = [_config inlineMathColor];
31
+ if (context.inHighlight) {
32
+ mathColor = [RenderContext calculateHighlightColor:[_config highlightColor]
33
+ paragraphColor:[_config paragraphColor]
34
+ blockColor:mathColor]
35
+ ?: mathColor;
36
+ }
37
+ attachment.mathTextColor = mathColor;
31
38
  attachment.containerPaddingVertical = [_config inlineMathPaddingVertical];
32
39
  attachment.bold = ENRMMathFontWeightIsBold([_config inlineMathFontWeight]);
33
40
  attachment.glyphOverflow = [_config inlineMathGlyphOverflow];
@@ -10,11 +10,17 @@ NSString *const HighlightAttributeName = @"EnrichedMarkdownHighlight";
10
10
 
11
11
  #pragma mark - Rendering
12
12
 
13
+ - (id)takeContextSnapshot:(RenderContext *)context
14
+ {
15
+ return [context snapshotScope];
16
+ }
17
+
13
18
  - (void)renderNodeContent:(MarkdownASTNode *)node
14
19
  into:(NSMutableAttributedString *)output
15
20
  context:(RenderContext *)context
16
21
  {
17
22
  NSUInteger start = output.length;
23
+ context.inHighlight = YES;
18
24
  [_rendererFactory renderChildrenOfNode:node into:output context:context];
19
25
 
20
26
  NSRange range = NSMakeRange(start, output.length - start);
@@ -45,6 +45,9 @@ typedef NS_ENUM(NSInteger, ListType) { ListTypeUnordered, ListTypeOrdered };
45
45
  @property (nonatomic, assign) BOOL allowFontScaling;
46
46
  @property (nonatomic, assign) CGFloat maxFontSizeMultiplier;
47
47
  @property (nonatomic, assign) NSInteger taskItemCount;
48
+ /// Rendering inside a `==…==` span: a formula there takes the highlight colour,
49
+ /// which the attributed string's foreground cannot give an attachment.
50
+ @property (nonatomic, assign) BOOL inHighlight;
48
51
 
49
52
  - (instancetype)init;
50
53
  - (void)reset;
@@ -25,6 +25,7 @@ static NSArray<NSString *> *ENRMScopedKeyPaths(void)
25
25
  @"listType",
26
26
  @"listItemNumber",
27
27
  @"accumulatedIndent",
28
+ @"inHighlight",
28
29
  ];
29
30
  });
30
31
  return keyPaths;
@@ -301,6 +302,7 @@ static NSArray<NSString *> *ENRMScopedKeyPaths(void)
301
302
  _listItemNumber = 0;
302
303
  _accumulatedIndent = 0;
303
304
  _taskItemCount = 0;
305
+ _inHighlight = NO;
304
306
 
305
307
  // Revert shared style object to baseline defaults
306
308
  _currentBlockStyle.fontSize = 0;
@@ -25,7 +25,9 @@ NS_ASSUME_NONNULL_BEGIN
25
25
 
26
26
  @property (nonatomic, strong) StyleConfig *config;
27
27
 
28
- /// Washes the formula's own frame in the highlight background (a `==$$…$$==` block).
28
+ /// A `==$$…$$==` block: the formula's own frame takes the highlight background
29
+ /// and the formula the highlight colour. Set it before `applyLatex:`; a change
30
+ /// afterwards parses the cached latex again.
29
31
  @property (nonatomic, assign) BOOL highlighted;
30
32
  @property (nonatomic, copy, readonly) NSString *cachedLatex;
31
33
  @property (nonatomic, strong, nullable) ENRMAccessibilityLabels *accessibilityLabels;
@@ -2,6 +2,7 @@
2
2
  #import "ENRMAccessibilityLabels.h"
3
3
  #import "ENRMFeatureFlags.h"
4
4
  #import "ENRMMathFontWeight.h"
5
+ #import "RenderContext.h"
5
6
  #include <TargetConditionals.h>
6
7
 
7
8
  #if ENRICHED_MARKDOWN_MATH
@@ -96,8 +97,24 @@
96
97
 
97
98
  - (void)setHighlighted:(BOOL)highlighted
98
99
  {
100
+ BOOL changed = _highlighted != highlighted;
99
101
  _highlighted = highlighted;
100
102
  _mathView.backgroundColor = highlighted ? self.config.highlightBackgroundColor : [RCTUIColor clearColor];
103
+ if (changed && _cachedLatex.length > 0) {
104
+ [self applyLatex:_cachedLatex];
105
+ }
106
+ }
107
+
108
+ - (RCTUIColor *)formulaColor
109
+ {
110
+ StyleConfig *config = self.config;
111
+ if (!_highlighted) {
112
+ return config.mathColor;
113
+ }
114
+ return [RenderContext calculateHighlightColor:config.highlightColor
115
+ paragraphColor:config.paragraphColor
116
+ blockColor:config.mathColor]
117
+ ?: config.mathColor;
101
118
  }
102
119
 
103
120
  - (void)applyLatex:(NSString *)latex
@@ -105,15 +122,16 @@
105
122
  _cachedLatex = [latex copy];
106
123
 
107
124
  StyleConfig *config = self.config;
125
+ RCTUIColor *color = [self formulaColor];
108
126
 
109
127
  ENRMRaTeXRenderResult *result = [ENRMRaTeXBridge parse:latex
110
128
  displayMode:YES
111
129
  fontSize:config.mathFontSize
112
- color:config.mathColor
130
+ color:color
113
131
  bold:ENRMMathFontWeightIsBold(config.mathFontWeight)
114
132
  glyphOverflow:config.mathGlyphOverflow];
115
133
  _mathView.renderResult = result;
116
- _mathView.fallbackSource = result ? nil : ENRMMathFallbackString(latex, @"$$", config.mathFontSize, config.mathColor);
134
+ _mathView.fallbackSource = result ? nil : ENRMMathFallbackString(latex, @"$$", config.mathFontSize, color);
117
135
 
118
136
  CGFloat padding = config.mathPadding;
119
137
  _mathView.frame = CGRectMake(padding, padding, ceil(result.width), ceil(result.totalHeight));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-enriched-markdown-scaffold",
3
- "version": "1.0.2-scaffold.11",
3
+ "version": "1.0.2-scaffold.13",
4
4
  "description": "Markdown-based Rich Text solution for React Native",
5
5
  "main": "./lib/module/index",
6
6
  "module": "./lib/module/index",