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

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.
Files changed (168) hide show
  1. package/android/build.gradle +2 -0
  2. package/android/generated/java/com/facebook/react/viewmanagers/EnrichedMarkdownManagerDelegate.java +18 -0
  3. package/android/generated/java/com/facebook/react/viewmanagers/EnrichedMarkdownManagerInterface.java +4 -0
  4. package/android/generated/java/com/facebook/react/viewmanagers/EnrichedMarkdownTextManagerDelegate.java +18 -0
  5. package/android/generated/java/com/facebook/react/viewmanagers/EnrichedMarkdownTextManagerInterface.java +4 -0
  6. package/android/generated/jni/react/renderer/components/EnrichedMarkdownTextSpec/EventEmitters.cpp +148 -0
  7. package/android/generated/jni/react/renderer/components/EnrichedMarkdownTextSpec/EventEmitters.h +120 -0
  8. package/android/generated/jni/react/renderer/components/EnrichedMarkdownTextSpec/Props.cpp +30 -0
  9. package/android/generated/jni/react/renderer/components/EnrichedMarkdownTextSpec/Props.h +210 -0
  10. package/android/src/main/java/com/swmansion/enriched/markdown/EnrichedMarkdown.kt +180 -2
  11. package/android/src/main/java/com/swmansion/enriched/markdown/EnrichedMarkdownInternalText.kt +45 -0
  12. package/android/src/main/java/com/swmansion/enriched/markdown/EnrichedMarkdownManager.kt +35 -0
  13. package/android/src/main/java/com/swmansion/enriched/markdown/EnrichedMarkdownText.kt +112 -0
  14. package/android/src/main/java/com/swmansion/enriched/markdown/EnrichedMarkdownTextManager.kt +34 -0
  15. package/android/src/main/java/com/swmansion/enriched/markdown/MeasurementStore.kt +46 -8
  16. package/android/src/main/java/com/swmansion/enriched/markdown/events/CopyRequestEvent.kt +28 -0
  17. package/android/src/main/java/com/swmansion/enriched/markdown/events/LinkLayoutEvent.kt +36 -0
  18. package/android/src/main/java/com/swmansion/enriched/markdown/events/SelectionChangeEvent.kt +43 -0
  19. package/android/src/main/java/com/swmansion/enriched/markdown/input/EnrichedMarkdownTextInputView.kt +25 -3
  20. package/android/src/main/java/com/swmansion/enriched/markdown/input/editing/InputConnectionWrapper.kt +7 -0
  21. package/android/src/main/java/com/swmansion/enriched/markdown/renderer/LinkRenderer.kt +37 -7
  22. package/android/src/main/java/com/swmansion/enriched/markdown/renderer/SpanStyleCache.kt +5 -1
  23. package/android/src/main/java/com/swmansion/enriched/markdown/spans/CodeBackgroundSpan.kt +13 -99
  24. package/android/src/main/java/com/swmansion/enriched/markdown/spans/InlineChipPainter.kt +140 -0
  25. package/android/src/main/java/com/swmansion/enriched/markdown/spans/LinkChipSpans.kt +177 -0
  26. package/android/src/main/java/com/swmansion/enriched/markdown/spans/LinkSpan.kt +1 -6
  27. package/android/src/main/java/com/swmansion/enriched/markdown/styles/InlineMathStyle.kt +16 -1
  28. package/android/src/main/java/com/swmansion/enriched/markdown/styles/LinkStyle.kt +36 -1
  29. package/android/src/main/java/com/swmansion/enriched/markdown/styles/LinkVariantEntry.kt +7 -3
  30. package/android/src/main/java/com/swmansion/enriched/markdown/styles/MathStyle.kt +3 -0
  31. package/android/src/main/java/com/swmansion/enriched/markdown/utils/common/MarkdownViewManagerUtils.kt +51 -0
  32. package/android/src/main/java/com/swmansion/enriched/markdown/utils/common/RenderedSegment.kt +5 -1
  33. package/android/src/main/java/com/swmansion/enriched/markdown/utils/input/PasteTextUtils.kt +34 -2
  34. package/android/src/main/java/com/swmansion/enriched/markdown/utils/text/conversion/HTMLGenerator.kt +2 -1
  35. package/android/src/main/java/com/swmansion/enriched/markdown/utils/text/conversion/MarkdownExtractor.kt +6 -0
  36. package/android/src/main/java/com/swmansion/enriched/markdown/utils/text/view/HeadlessActionMode.kt +88 -0
  37. package/android/src/main/java/com/swmansion/enriched/markdown/utils/text/view/LinkLayoutCollector.kt +100 -0
  38. package/android/src/main/java/com/swmansion/enriched/markdown/utils/text/view/SelectionActionMode.kt +53 -14
  39. package/android/src/main/java/com/swmansion/enriched/markdown/utils/text/view/SelectionChangeReporter.kt +232 -0
  40. package/android/src/main/java/com/swmansion/enriched/markdown/views/BlockMenuOptions.kt +64 -0
  41. package/android/src/main/java/com/swmansion/enriched/markdown/views/CodeBlockContainerView.kt +29 -3
  42. package/android/src/main/java/com/swmansion/enriched/markdown/views/TableContainerView.kt +30 -7
  43. package/android/src/math/java/com/swmansion/enriched/markdown/renderer/MathInlineRenderer.kt +1 -0
  44. package/android/src/math/java/com/swmansion/enriched/markdown/spans/MathInlineSpan.kt +13 -6
  45. package/android/src/math/java/com/swmansion/enriched/markdown/spans/MathMeasureHelper.kt +2 -1
  46. package/android/src/math/java/com/swmansion/enriched/markdown/views/MathContainerView.kt +35 -9
  47. package/cpp/parser/MD4CParser.cpp +24 -2
  48. package/docs/API_REFERENCE.md +183 -4
  49. package/docs/COPY_OPTIONS.md +40 -0
  50. package/docs/INPUT.md +3 -1
  51. package/docs/MENTIONS.md +8 -1
  52. package/docs/STYLES.md +27 -0
  53. package/docs/TEXT.md +2 -0
  54. package/docs/WEB.md +1 -0
  55. package/ios/EnrichedMarkdown.mm +326 -10
  56. package/ios/EnrichedMarkdownText.mm +203 -6
  57. package/ios/attachments/ENRMMathInlineAttachment+macOS.m +2 -1
  58. package/ios/attachments/ENRMMathInlineAttachment.h +3 -0
  59. package/ios/attachments/ENRMMathInlineAttachment.m +2 -1
  60. package/ios/generated/ReactCodegen/EnrichedMarkdownTextSpec/EventEmitters.cpp +148 -0
  61. package/ios/generated/ReactCodegen/EnrichedMarkdownTextSpec/EventEmitters.h +120 -0
  62. package/ios/generated/ReactCodegen/EnrichedMarkdownTextSpec/Props.cpp +30 -0
  63. package/ios/generated/ReactCodegen/EnrichedMarkdownTextSpec/Props.h +210 -0
  64. package/ios/generated/ReactCodegen/EnrichedMarkdownTextSpec/RCTComponentViewHelpers.h +50 -2
  65. package/ios/internals/MeasurementCache.h +8 -2
  66. package/ios/math/ENRMRaTeXBridge.swift +23 -5
  67. package/ios/renderer/ENRMMathInlineRenderer.m +1 -0
  68. package/ios/renderer/LinkRenderer.m +47 -15
  69. package/ios/styles/StyleConfig.h +20 -0
  70. package/ios/styles/StyleConfig.mm +84 -0
  71. package/ios/utils/CodeBackground.m +3 -65
  72. package/ios/utils/ENRMBlockMenu.h +66 -0
  73. package/ios/utils/ENRMChipDrawing.h +24 -0
  74. package/ios/utils/ENRMChipDrawing.m +83 -0
  75. package/ios/utils/ENRMLinkChip.h +70 -0
  76. package/ios/utils/ENRMLinkChip.m +213 -0
  77. package/ios/utils/ENRMLinkLayout.h +50 -0
  78. package/ios/utils/ENRMLinkLayout.m +45 -0
  79. package/ios/utils/ENRMSelectionChange.h +120 -0
  80. package/ios/utils/ENRMSelectionChange.m +282 -0
  81. package/ios/utils/EditMenuUtils+macOS.m +4 -1
  82. package/ios/utils/EditMenuUtils.h +35 -6
  83. package/ios/utils/EditMenuUtils.m +171 -15
  84. package/ios/utils/HTMLGenerator.m +2 -1
  85. package/ios/utils/MarkdownAccessibilityElementBuilder.m +3 -1
  86. package/ios/utils/MarkdownExtractor.m +2 -1
  87. package/ios/utils/RTFExportUtils.m +2 -0
  88. package/ios/utils/RenderedMarkdownSegment.h +2 -1
  89. package/ios/utils/RenderedMarkdownSegment.m +2 -1
  90. package/ios/utils/SegmentRenderer.m +5 -1
  91. package/ios/utils/StylePropsUtils.h +47 -1
  92. package/ios/utils/TextViewLayoutManager.mm +3 -0
  93. package/ios/views/ENRMCodeBlockContainerView.h +16 -2
  94. package/ios/views/ENRMCodeBlockContainerView.m +22 -19
  95. package/ios/views/ENRMMathContainerView.h +14 -0
  96. package/ios/views/ENRMMathContainerView.m +23 -20
  97. package/ios/views/TableContainerView.h +14 -0
  98. package/ios/views/TableContainerView.m +34 -18
  99. package/lib/module/EnrichedMarkdownNativeComponent.ts +138 -1
  100. package/lib/module/EnrichedMarkdownTextNativeComponent.ts +138 -1
  101. package/lib/module/index.js.map +1 -1
  102. package/lib/module/jest/index.js +14 -0
  103. package/lib/module/jest/index.js.map +1 -1
  104. package/lib/module/native/EnrichedMarkdownText.js +169 -11
  105. package/lib/module/native/EnrichedMarkdownText.js.map +1 -1
  106. package/lib/module/normalizeMarkdownStyle.js +17 -4
  107. package/lib/module/normalizeMarkdownStyle.js.map +1 -1
  108. package/lib/module/normalizeMarkdownStyle.web.js +17 -4
  109. package/lib/module/normalizeMarkdownStyle.web.js.map +1 -1
  110. package/lib/module/web/EnrichedMarkdownText.js +6 -3
  111. package/lib/module/web/EnrichedMarkdownText.js.map +1 -1
  112. package/lib/module/web/renderers/InlineRenderers.js +96 -3
  113. package/lib/module/web/renderers/InlineRenderers.js.map +1 -1
  114. package/lib/module/web/styles.js +18 -0
  115. package/lib/module/web/styles.js.map +1 -1
  116. package/lib/module/web/utils.js +13 -0
  117. package/lib/module/web/utils.js.map +1 -1
  118. package/lib/typescript/src/EnrichedMarkdownNativeComponent.d.ts +123 -2
  119. package/lib/typescript/src/EnrichedMarkdownNativeComponent.d.ts.map +1 -1
  120. package/lib/typescript/src/EnrichedMarkdownTextNativeComponent.d.ts +123 -2
  121. package/lib/typescript/src/EnrichedMarkdownTextNativeComponent.d.ts.map +1 -1
  122. package/lib/typescript/src/index.d.ts +2 -2
  123. package/lib/typescript/src/index.d.ts.map +1 -1
  124. package/lib/typescript/src/index.web.d.ts +1 -1
  125. package/lib/typescript/src/index.web.d.ts.map +1 -1
  126. package/lib/typescript/src/jest/index.d.ts +1 -1
  127. package/lib/typescript/src/jest/index.d.ts.map +1 -1
  128. package/lib/typescript/src/native/EnrichedMarkdownText.d.ts +5 -5
  129. package/lib/typescript/src/native/EnrichedMarkdownText.d.ts.map +1 -1
  130. package/lib/typescript/src/normalizeMarkdownStyle.d.ts.map +1 -1
  131. package/lib/typescript/src/normalizeMarkdownStyle.web.d.ts.map +1 -1
  132. package/lib/typescript/src/types/MarkdownStyle.d.ts +50 -5
  133. package/lib/typescript/src/types/MarkdownStyle.d.ts.map +1 -1
  134. package/lib/typescript/src/types/MarkdownStyleInternal.d.ts +12 -0
  135. package/lib/typescript/src/types/MarkdownStyleInternal.d.ts.map +1 -1
  136. package/lib/typescript/src/types/MarkdownTextProps.d.ts +144 -11
  137. package/lib/typescript/src/types/MarkdownTextProps.d.ts.map +1 -1
  138. package/lib/typescript/src/types/MarkdownTextProps.web.d.ts +41 -1
  139. package/lib/typescript/src/types/MarkdownTextProps.web.d.ts.map +1 -1
  140. package/lib/typescript/src/types/events.d.ts +66 -0
  141. package/lib/typescript/src/types/events.d.ts.map +1 -1
  142. package/lib/typescript/src/web/EnrichedMarkdownText.d.ts +1 -1
  143. package/lib/typescript/src/web/EnrichedMarkdownText.d.ts.map +1 -1
  144. package/lib/typescript/src/web/renderers/InlineRenderers.d.ts.map +1 -1
  145. package/lib/typescript/src/web/styles.d.ts.map +1 -1
  146. package/lib/typescript/src/web/types.d.ts +4 -0
  147. package/lib/typescript/src/web/types.d.ts.map +1 -1
  148. package/lib/typescript/src/web/utils.d.ts +4 -0
  149. package/lib/typescript/src/web/utils.d.ts.map +1 -1
  150. package/package.json +1 -1
  151. package/src/EnrichedMarkdownNativeComponent.ts +138 -1
  152. package/src/EnrichedMarkdownTextNativeComponent.ts +138 -1
  153. package/src/index.tsx +6 -0
  154. package/src/index.web.tsx +5 -1
  155. package/src/jest/index.tsx +22 -1
  156. package/src/native/EnrichedMarkdownText.tsx +220 -9
  157. package/src/normalizeMarkdownStyle.ts +15 -0
  158. package/src/normalizeMarkdownStyle.web.ts +13 -0
  159. package/src/types/MarkdownStyle.ts +51 -5
  160. package/src/types/MarkdownStyleInternal.ts +12 -0
  161. package/src/types/MarkdownTextProps.ts +151 -11
  162. package/src/types/MarkdownTextProps.web.ts +43 -1
  163. package/src/types/events.ts +69 -0
  164. package/src/web/EnrichedMarkdownText.tsx +10 -3
  165. package/src/web/renderers/InlineRenderers.tsx +111 -3
  166. package/src/web/styles.ts +25 -0
  167. package/src/web/types.ts +4 -0
  168. package/src/web/utils.ts +8 -0
@@ -19,6 +19,8 @@
19
19
  #import "ENRMMathContainerView.h"
20
20
  #endif
21
21
  #import "ENRMCodeBlockContainerView.h"
22
+ #import "ENRMLinkLayout.h"
23
+ #import "ENRMSelectionChange.h"
22
24
  #import "ENRMSpoilerCapable.h"
23
25
  #import "ENRMSpoilerOverlayView.h"
24
26
  #import "ENRMSpoilerTapUtils.h"
@@ -73,8 +75,10 @@ static char kENRMSegmentFadeAnimatorKey;
73
75
  + (ENRMMd4cFlags *)flagsFromProps:(const EnrichedMarkdownMd4cFlagsStruct &)props;
74
76
  - (void)emitLinkPress:(NSString *)url;
75
77
  - (void)emitLinkLongPress:(NSString *)url;
78
+ - (void)emitLinkLayoutIfNeeded;
76
79
  - (void)emitTaskListItemPress:(NSInteger)index checked:(BOOL)checked text:(NSString *)text;
77
80
  - (void)emitCopyPress:(NSString *)code language:(NSString *)language;
81
+ - (void)emitCopyRequest:(NSString *)source markdown:(NSString *)markdown text:(NSString *)text whole:(BOOL)whole;
78
82
  - (void)emitContextMenuItemPress:(NSString *)itemText
79
83
  selectedText:(NSString *)selectedText
80
84
  selectionStart:(NSUInteger)selectionStart
@@ -103,6 +107,7 @@ static char kENRMSegmentFadeAnimatorKey;
103
107
  BOOL _allowTrailingMargin;
104
108
  BOOL _selectable;
105
109
  BOOL _enableLinkPreview;
110
+ BOOL _linkLongPressSelects;
106
111
  BOOL _enableTaskListItemToggle;
107
112
  BOOL _contextMenuHidden;
108
113
  BOOL _streamingAnimation;
@@ -122,6 +127,21 @@ static char kENRMSegmentFadeAnimatorKey;
122
127
 
123
128
  ENRMSpoilerOverlay _spoilerOverlay;
124
129
 
130
+ BOOL _emitLinkLayout;
131
+ NSArray<NSDictionary *> *_lastLinkLayout;
132
+
133
+ ENRMSelectionChangeReporter *_selectionReporter;
134
+ #if !TARGET_OS_OSX
135
+ ENRMSelectionGestureTracker *_selectionGestures;
136
+ #endif
137
+ // At most one segment holds a selection; a new one clears the previous.
138
+ __weak ENRMPlatformTextView *_selectionOwner;
139
+ // A block long-pressed while contextMenuHidden is on, reported as the selection.
140
+ __weak RCTUIView *_activeBlockView;
141
+ NSString *_activeBlockSource;
142
+ NSString *_activeBlockMarkdown;
143
+ NSString *_activeBlockText;
144
+
125
145
  NSLineBreakStrategy _lineBreakStrategy;
126
146
 
127
147
  ENRMWritingDirectionMode _writingDirectionMode;
@@ -173,7 +193,7 @@ static char kENRMSegmentFadeAnimatorKey;
173
193
  _tableStreamingMode = ENRMTableStreamingModeProgressive;
174
194
  _codeBlockStreamingMode = ENRMCodeBlockStreamingModeProgressive;
175
195
  _contextMenuHidden = NO;
176
- _selectionMenuConfig = (ENRMSelectionMenuConfig){.copyAsMarkdown = YES, .copyImageURL = YES};
196
+ _selectionMenuConfig = ENRMDefaultSelectionMenuConfig();
177
197
  _lineBreakStrategy = NSLineBreakStrategyNone;
178
198
  _writingDirectionMode = ENRMWritingDirectionModeFirstStrong;
179
199
  _resolvedLayoutDirection =
@@ -193,10 +213,132 @@ static char kENRMSegmentFadeAnimatorKey;
193
213
  [strongSelf renderMarkdownContent:strongSelf->_cachedMarkdown];
194
214
  }
195
215
  };
216
+
217
+ _selectionReporter = [[ENRMSelectionChangeReporter alloc]
218
+ initWithSnapshot:^NSDictionary * {
219
+ EnrichedMarkdown *strongSelf = weakSelf;
220
+ return strongSelf ? [strongSelf selectionPayload] : ENRMInactiveSelectionPayload();
221
+ }
222
+ emit:^(NSDictionary *payload) {
223
+ EnrichedMarkdown *strongSelf = weakSelf;
224
+ if (!strongSelf)
225
+ return;
226
+ auto emitter = std::static_pointer_cast<EnrichedMarkdownEventEmitter const>(strongSelf->_eventEmitter);
227
+ if (emitter)
228
+ emitter->onSelectionChange(
229
+ ENRMSelectionChangeEventFromPayload<EnrichedMarkdownEventEmitter::OnSelectionChange>(payload));
230
+ }];
231
+ #if !TARGET_OS_OSX
232
+ // A handle drag starting or ending is reported right away, even when the
233
+ // selection itself is unchanged.
234
+ _selectionGestures = [[ENRMSelectionGestureTracker alloc] initWithChange:^{
235
+ EnrichedMarkdown *strongSelf = weakSelf;
236
+ if (!strongSelf)
237
+ return;
238
+ UITextView *owner = strongSelf->_selectionOwner;
239
+ if (owner)
240
+ [strongSelf->_selectionGestures trackTextView:owner];
241
+ [strongSelf->_selectionReporter forgetLastReported];
242
+ [strongSelf->_selectionReporter flushNow];
243
+ }];
244
+ #endif
196
245
  }
197
246
  return self;
198
247
  }
199
248
 
249
+ #pragma mark - Selection
250
+
251
+ /// The current selection (or active block) as an `onSelectionChange` payload.
252
+ /// Markdown and `whole` follow the same rules as the intercepted Copy.
253
+ - (NSDictionary *)selectionPayload
254
+ {
255
+ RCTUIView *blockView = _activeBlockView;
256
+ if (blockView != nil) {
257
+ if (blockView.superview != self)
258
+ return ENRMInactiveSelectionPayload();
259
+ return ENRMBlockSelectionPayload(_activeBlockSource, _activeBlockMarkdown, _activeBlockText, blockView.frame);
260
+ }
261
+
262
+ #if !TARGET_OS_OSX
263
+ UITextView *owner = _selectionOwner;
264
+ if (owner == nil || ![owner isDescendantOfView:self])
265
+ return ENRMInactiveSelectionPayload();
266
+ NSDictionary *selection = ENRMTextSelectionInTextView(owner, self);
267
+ if (!selection)
268
+ return ENRMInactiveSelectionPayload();
269
+
270
+ NSString *text = selection[ENRMSelectionTextKey];
271
+ // Segments only see their own text, so a selection is the whole component
272
+ // only when it spans the sole segment; the markdown is then the original.
273
+ BOOL whole = [selection[ENRMSelectionFullKey] boolValue] && _segmentViews.count == 1 && _cachedMarkdown.length > 0;
274
+ NSString *markdown =
275
+ whole ? _cachedMarkdown : extractMarkdownFromAttributedString(owner.attributedText, owner.selectedRange);
276
+ NSMutableDictionary *payload = [selection mutableCopy];
277
+ [payload removeObjectForKey:ENRMSelectionFullKey];
278
+ payload[ENRMSelectionActiveKey] = @YES;
279
+ payload[ENRMSelectionSourceKey] = @"selection";
280
+ payload[ENRMSelectionMarkdownKey] = markdown.length > 0 ? markdown : text;
281
+ payload[ENRMSelectionWholeKey] = @(whole);
282
+ payload[ENRMSelectionAdjustingKey] = @(_selectionGestures.adjusting);
283
+ return payload;
284
+ #else
285
+ return ENRMInactiveSelectionPayload();
286
+ #endif
287
+ }
288
+
289
+ - (void)clearActiveBlock
290
+ {
291
+ _activeBlockView = nil;
292
+ _activeBlockSource = nil;
293
+ _activeBlockMarkdown = nil;
294
+ _activeBlockText = nil;
295
+ }
296
+
297
+ - (void)clearSegmentSelection:(ENRMPlatformTextView *)textView
298
+ {
299
+ #if !TARGET_OS_OSX
300
+ textView.selectedRange = NSMakeRange(0, 0);
301
+ [textView resignFirstResponder];
302
+ #endif
303
+ }
304
+
305
+ - (void)clearSelection
306
+ {
307
+ ENRMPlatformTextView *owner = _selectionOwner;
308
+ _selectionOwner = nil;
309
+ [self clearActiveBlock];
310
+ if (owner)
311
+ [self clearSegmentSelection:owner];
312
+ [_selectionReporter flushNow];
313
+ }
314
+
315
+ - (void)handleCommand:(const NSString *)commandName args:(const NSArray *)args
316
+ {
317
+ RCTEnrichedMarkdownHandleCommand(self, commandName, args);
318
+ }
319
+
320
+ - (ENRMBlockCopyRequestBlock)blockMenuSuppressedForSource:(NSString *)source view:(RCTUIView *)view
321
+ {
322
+ __weak EnrichedMarkdown *weakSelf = self;
323
+ __weak RCTUIView *weakView = view;
324
+ return ^(NSString *markdown, NSString *text) {
325
+ EnrichedMarkdown *strongSelf = weakSelf;
326
+ RCTUIView *strongView = weakView;
327
+ if (!strongSelf || !strongView || !strongSelf->_selectionReporter.enabled)
328
+ return;
329
+ ENRMPlatformTextView *owner = strongSelf->_selectionOwner;
330
+ strongSelf->_selectionOwner = nil;
331
+ if (owner)
332
+ [strongSelf clearSegmentSelection:owner];
333
+ strongSelf->_activeBlockView = strongView;
334
+ strongSelf->_activeBlockSource = source;
335
+ strongSelf->_activeBlockMarkdown = markdown;
336
+ strongSelf->_activeBlockText = text;
337
+ [strongSelf->_selectionReporter forgetLastReported];
338
+ [strongSelf->_selectionReporter flushNow];
339
+ };
340
+ }
341
+
200
342
  - (void)configureSegmentViewRegistry
201
343
  {
202
344
  __weak EnrichedMarkdown *weakSelf = self;
@@ -263,7 +405,10 @@ static char kENRMSegmentFadeAnimatorKey;
263
405
  return view;
264
406
  }
265
407
  updateView:^(RCTUIView *view, ENRMRenderedSegment *segment) {
266
- [(ENRMMathContainerView *)view applyLatex:((ENRMMathSegment *)segment.mathSegment).latex];
408
+ ENRMMathSegment *mathSegment = (ENRMMathSegment *)segment.mathSegment;
409
+ ENRMMathContainerView *mathView = (ENRMMathContainerView *)view;
410
+ [mathView applyLatex:mathSegment.latex];
411
+ mathView.highlighted = mathSegment.highlighted;
267
412
  }]];
268
413
  #endif
269
414
 
@@ -505,9 +650,30 @@ static char kENRMSegmentFadeAnimatorKey;
505
650
  }
506
651
  }
507
652
 
508
- // Table and math views cache the copy labels at creation time, so re-push them
509
- // on prop updates (e.g. a language change without a remount) to avoid stale
510
- // labels. Only the copy/copy-as-markdown labels apply to these block menus.
653
+ - (ENRMBlockMenuOptions)blockMenuOptions
654
+ {
655
+ return (ENRMBlockMenuOptions){
656
+ .copyHidden = !_selectionMenuConfig.copyEnabled,
657
+ .copyWritesMarkdown = _selectionMenuConfig.copyFormatMarkdown,
658
+ .copyAsMarkdownHidden = !_selectionMenuConfig.copyAsMarkdown,
659
+ .copyButtonHidden = !_selectionMenuConfig.codeBlockCopyButton,
660
+ .interceptCopy = _selectionMenuConfig.interceptCopy,
661
+ };
662
+ }
663
+
664
+ - (ENRMBlockCopyRequestBlock)blockCopyRequestForSource:(NSString *)source
665
+ {
666
+ __weak EnrichedMarkdown *weakSelf = self;
667
+ return ^(NSString *markdown, NSString *text) {
668
+ EnrichedMarkdown *strongSelf = weakSelf;
669
+ if (strongSelf)
670
+ [strongSelf emitCopyRequest:source markdown:markdown text:text whole:YES];
671
+ };
672
+ }
673
+
674
+ // Table, math and code block views cache the copy labels and menu options at
675
+ // creation time, so re-push them on prop updates (e.g. a language change
676
+ // without a remount) to avoid stale values.
511
677
  - (void)pushSelectionMenuLabelsToSegments
512
678
  {
513
679
  for (RCTUIView *segment in _segmentViews) {
@@ -516,6 +682,7 @@ static char kENRMSegmentFadeAnimatorKey;
516
682
  tableView.copyLabel = _selectionMenuLabels.copyLabel;
517
683
  tableView.copyAsMarkdownLabel = _selectionMenuLabels.copyAsMarkdownLabel;
518
684
  tableView.contextMenuHidden = _contextMenuHidden;
685
+ tableView.menuOptions = [self blockMenuOptions];
519
686
  }
520
687
  #if ENRICHED_MARKDOWN_MATH
521
688
  else if ([segment isKindOfClass:[ENRMMathContainerView class]]) {
@@ -523,6 +690,7 @@ static char kENRMSegmentFadeAnimatorKey;
523
690
  mathView.copyLabel = _selectionMenuLabels.copyLabel;
524
691
  mathView.copyAsMarkdownLabel = _selectionMenuLabels.copyAsMarkdownLabel;
525
692
  mathView.contextMenuHidden = _contextMenuHidden;
693
+ mathView.menuOptions = [self blockMenuOptions];
526
694
  }
527
695
  #endif
528
696
  else if ([segment isKindOfClass:[ENRMCodeBlockContainerView class]]) {
@@ -530,6 +698,7 @@ static char kENRMSegmentFadeAnimatorKey;
530
698
  codeBlockView.copyLabel = _selectionMenuLabels.copyLabel;
531
699
  codeBlockView.copyAsMarkdownLabel = _selectionMenuLabels.copyAsMarkdownLabel;
532
700
  codeBlockView.contextMenuHidden = _contextMenuHidden;
701
+ codeBlockView.menuOptions = [self blockMenuOptions];
533
702
  }
534
703
  }
535
704
  }
@@ -767,6 +936,7 @@ static char kENRMSegmentFadeAnimatorKey;
767
936
 
768
937
  #if !TARGET_OS_OSX
769
938
  view.textView.delegate = self;
939
+ [_selectionGestures trackTextView:view.textView];
770
940
  #else
771
941
  __weak EnrichedMarkdown *weakSelf = self;
772
942
  [view setContextMenuProvider:^NSMenu *_Nullable(NSMenu *baseMenu, NSTextView *textView) {
@@ -807,6 +977,9 @@ static char kENRMSegmentFadeAnimatorKey;
807
977
  tableView.copyLabel = _selectionMenuLabels.copyLabel;
808
978
  tableView.copyAsMarkdownLabel = _selectionMenuLabels.copyAsMarkdownLabel;
809
979
  tableView.contextMenuHidden = _contextMenuHidden;
980
+ tableView.menuOptions = [self blockMenuOptions];
981
+ tableView.onCopyRequest = [self blockCopyRequestForSource:@"table"];
982
+ tableView.onMenuSuppressed = [self blockMenuSuppressedForSource:@"table" view:tableView];
810
983
 
811
984
  __weak EnrichedMarkdown *weakSelf = self;
812
985
 
@@ -847,7 +1020,11 @@ static char kENRMSegmentFadeAnimatorKey;
847
1020
  mathView.copyLabel = _selectionMenuLabels.copyLabel;
848
1021
  mathView.copyAsMarkdownLabel = _selectionMenuLabels.copyAsMarkdownLabel;
849
1022
  mathView.contextMenuHidden = _contextMenuHidden;
1023
+ mathView.menuOptions = [self blockMenuOptions];
1024
+ mathView.onCopyRequest = [self blockCopyRequestForSource:@"math"];
1025
+ mathView.onMenuSuppressed = [self blockMenuSuppressedForSource:@"math" view:mathView];
850
1026
  [mathView applyLatex:mathSegment.latex];
1027
+ mathView.highlighted = mathSegment.highlighted;
851
1028
  return mathView;
852
1029
  }
853
1030
  #endif
@@ -858,6 +1035,9 @@ static char kENRMSegmentFadeAnimatorKey;
858
1035
  codeBlockView.copyLabel = _selectionMenuLabels.copyLabel;
859
1036
  codeBlockView.copyAsMarkdownLabel = _selectionMenuLabels.copyAsMarkdownLabel;
860
1037
  codeBlockView.contextMenuHidden = _contextMenuHidden;
1038
+ codeBlockView.menuOptions = [self blockMenuOptions];
1039
+ codeBlockView.onCopyRequest = [self blockCopyRequestForSource:@"code"];
1040
+ codeBlockView.onMenuSuppressed = [self blockMenuSuppressedForSource:@"code" view:codeBlockView];
861
1041
 
862
1042
  __weak EnrichedMarkdown *weakSelf = self;
863
1043
  codeBlockView.onCopyPress = ^(NSString *code, NSString *language) {
@@ -910,6 +1090,9 @@ static char kENRMSegmentFadeAnimatorKey;
910
1090
  {
911
1091
  [super layoutSubviews];
912
1092
  [self computeSegmentLayoutForWidth:self.bounds.size.width applyFrames:YES];
1093
+ [self emitLinkLayoutIfNeeded];
1094
+ // Segments may have moved or been replaced; re-read the selection (deduplicated).
1095
+ [_selectionReporter schedule];
913
1096
  }
914
1097
 
915
1098
  - (void)updateProps:(Props::Shared const &)props oldProps:(Props::Shared const &)oldProps
@@ -945,6 +1128,14 @@ static char kENRMSegmentFadeAnimatorKey;
945
1128
 
946
1129
  _selectable = newViewProps.selectable;
947
1130
 
1131
+ if (newViewProps.emitLinkLayout != _emitLinkLayout) {
1132
+ _emitLinkLayout = newViewProps.emitLinkLayout;
1133
+ _lastLinkLayout = nil;
1134
+ [self setNeedsLayout];
1135
+ }
1136
+
1137
+ _selectionReporter.enabled = newViewProps.emitSelectionChange;
1138
+
948
1139
  for (RCTUIView *segment in _segmentViews) {
949
1140
  if ([segment isKindOfClass:[EnrichedMarkdownInternalText class]]) {
950
1141
  EnrichedMarkdownInternalText *textSegment = (EnrichedMarkdownInternalText *)segment;
@@ -986,6 +1177,7 @@ static char kENRMSegmentFadeAnimatorKey;
986
1177
  }
987
1178
 
988
1179
  _enableLinkPreview = newViewProps.enableLinkPreview;
1180
+ _linkLongPressSelects = newViewProps.linkLongPressSelects;
989
1181
  _enableTaskListItemToggle = newViewProps.enableTaskListItemToggle;
990
1182
  _contextMenuHidden = newViewProps.contextMenuHidden;
991
1183
 
@@ -1026,9 +1218,7 @@ static char kENRMSegmentFadeAnimatorKey;
1026
1218
  }
1027
1219
 
1028
1220
  _selectionMenuLabels = ENRMParseSelectionMenuLabels(newViewProps.selectionMenuConfig);
1029
- _selectionMenuConfig =
1030
- ENRMBuildSelectionMenuConfig(_selectionMenuLabels, newViewProps.selectionMenuConfig.copyAsMarkdown,
1031
- newViewProps.selectionMenuConfig.copyImageUrl);
1221
+ _selectionMenuConfig = ENRMBuildSelectionMenuConfig(_selectionMenuLabels, newViewProps.selectionMenuConfig);
1032
1222
  [self pushSelectionMenuLabelsToSegments];
1033
1223
 
1034
1224
  if (ENRMAccessibilityLabelsChanged(oldViewProps.accessibilityLabels, newViewProps.accessibilityLabels)) {
@@ -1142,6 +1332,14 @@ static char kENRMSegmentFadeAnimatorKey;
1142
1332
  _cachedMarkdown = nil;
1143
1333
  _renderedMarkdown = nil;
1144
1334
  _config = nil;
1335
+ _emitLinkLayout = NO;
1336
+ _lastLinkLayout = nil;
1337
+ _selectionReporter.enabled = NO;
1338
+ #if !TARGET_OS_OSX
1339
+ [_selectionGestures reset];
1340
+ #endif
1341
+ _selectionOwner = nil;
1342
+ [self clearActiveBlock];
1145
1343
  _md4cFlags = [EnrichedMarkdown flagsFromProps:resetProps->md4cFlags];
1146
1344
  _maxFontSizeMultiplier = 0;
1147
1345
  _allowTrailingMargin = NO;
@@ -1252,6 +1450,40 @@ Class<RCTComponentViewProtocol> EnrichedMarkdownCls(void)
1252
1450
  emitter->onLinkPress({.url = std::string(url.UTF8String)});
1253
1451
  }
1254
1452
 
1453
+ /// Reports link rects of every text segment (offset by the segment origin) to
1454
+ /// JS for `renderLinkDecoration`. Tables are skipped: their cells scroll
1455
+ /// horizontally, so rects would go stale. Runs after each segment layout pass;
1456
+ /// deduplicated so an unchanged layout sends nothing.
1457
+ - (void)emitLinkLayoutIfNeeded
1458
+ {
1459
+ if (!_emitLinkLayout || self.bounds.size.width <= 0)
1460
+ return;
1461
+
1462
+ auto emitter = std::static_pointer_cast<EnrichedMarkdownEventEmitter const>(_eventEmitter);
1463
+ if (!emitter)
1464
+ return;
1465
+
1466
+ NSMutableArray<NSDictionary *> *fragments = [NSMutableArray array];
1467
+ NSInteger linkCount = 0;
1468
+ for (RCTUIView *segment in _segmentViews) {
1469
+ if ([segment isKindOfClass:[TableContainerView class]]) {
1470
+ linkCount += ((TableContainerView *)segment).linkCount;
1471
+ continue;
1472
+ }
1473
+ if (![segment isKindOfClass:[EnrichedMarkdownInternalText class]])
1474
+ continue;
1475
+ NSInteger segmentLinks = 0;
1476
+ [fragments addObjectsFromArray:ENRMCollectLinkLayoutFragments(((EnrichedMarkdownInternalText *)segment).textView,
1477
+ segment.frame.origin, linkCount, &segmentLinks)];
1478
+ linkCount += segmentLinks;
1479
+ }
1480
+
1481
+ if (_lastLinkLayout != nil && [fragments isEqualToArray:_lastLinkLayout])
1482
+ return;
1483
+ _lastLinkLayout = [fragments copy];
1484
+ emitter->onLinkLayout(ENRMLinkLayoutEventFromFragments<EnrichedMarkdownEventEmitter::OnLinkLayout>(fragments));
1485
+ }
1486
+
1255
1487
  - (void)emitLinkLongPress:(NSString *)url
1256
1488
  {
1257
1489
  auto emitter = std::static_pointer_cast<EnrichedMarkdownEventEmitter const>(_eventEmitter);
@@ -1274,6 +1506,18 @@ Class<RCTComponentViewProtocol> EnrichedMarkdownCls(void)
1274
1506
  {.code = std::string(code.UTF8String ?: ""), .language = std::string(language.UTF8String ?: "")});
1275
1507
  }
1276
1508
 
1509
+ - (void)emitCopyRequest:(NSString *)source markdown:(NSString *)markdown text:(NSString *)text whole:(BOOL)whole
1510
+ {
1511
+ auto emitter = std::static_pointer_cast<EnrichedMarkdownEventEmitter const>(_eventEmitter);
1512
+ if (emitter)
1513
+ emitter->onCopyRequest({
1514
+ .source = std::string(source.UTF8String),
1515
+ .markdown = std::string(markdown.UTF8String ?: ""),
1516
+ .text = std::string(text.UTF8String ?: ""),
1517
+ .whole = (bool)whole,
1518
+ });
1519
+ }
1520
+
1277
1521
  - (void)emitContextMenuItemPress:(NSString *)itemText
1278
1522
  selectedText:(NSString *)selectedText
1279
1523
  selectionStart:(NSUInteger)selectionStart
@@ -1317,14 +1561,35 @@ Class<RCTComponentViewProtocol> EnrichedMarkdownCls(void)
1317
1561
  ENRMHandleTapOnTextView(textView, recognizer, ^(NSString *url) { [self emitLinkPress:url]; });
1318
1562
  }
1319
1563
 
1564
+ #if !TARGET_OS_OSX
1565
+ - (void)textViewDidChangeSelection:(UITextView *)textView
1566
+ {
1567
+ [_selectionGestures trackTextView:textView];
1568
+ ENRMDismissSelectionEditMenuIfMoved(textView);
1569
+ if (textView.selectedRange.length > 0) {
1570
+ // Take ownership first so the previous owner's own "cleared" callback is ignored.
1571
+ ENRMPlatformTextView *previous = _selectionOwner;
1572
+ _selectionOwner = textView;
1573
+ [self clearActiveBlock];
1574
+ if (previous && previous != textView)
1575
+ previous.selectedRange = NSMakeRange(0, 0);
1576
+ [_selectionReporter schedule];
1577
+ } else if (textView == _selectionOwner) {
1578
+ [_selectionReporter selectionChanged:NO];
1579
+ }
1580
+ }
1581
+ #endif
1582
+
1320
1583
  // TODO: Remove API_AVAILABLE(ios(16.0)) guard when the minimum iOS deployment target in RN is bumped to 16.
1321
1584
  #if !TARGET_OS_OSX
1322
1585
  - (UIMenu *)textView:(UITextView *)textView
1323
1586
  editMenuForTextInRange:(NSRange)range
1324
1587
  suggestedActions:(NSArray<UIMenuElement *> *)suggestedActions API_AVAILABLE(ios(16.0))
1325
1588
  {
1589
+ // An empty menu presents nothing; nil would make UIKit show its default menu
1590
+ // (the selection and handles stay either way).
1326
1591
  if (_contextMenuHidden) {
1327
- return nil;
1592
+ return [UIMenu menuWithChildren:@[]];
1328
1593
  }
1329
1594
 
1330
1595
  __weak EnrichedMarkdown *weakSelf = self;
@@ -1341,8 +1606,47 @@ Class<RCTComponentViewProtocol> EnrichedMarkdownCls(void)
1341
1606
  ENRMBuildContextMenuActions(_contextMenuItemTexts, _contextMenuItemIcons, textView, range, handler);
1342
1607
 
1343
1608
  NSString *segmentMarkdown = extractMarkdownFromAttributedString(textView.attributedText, range);
1609
+
1610
+ // Segments only see their own text, so a selection is the whole component
1611
+ // only when it spans the sole segment; the markdown is then the original.
1612
+ ENRMCopyRequestHandler onCopyRequest = ^(NSString *markdown, NSString *text, BOOL fullSelection) {
1613
+ EnrichedMarkdown *strongSelf = weakSelf;
1614
+ if (!strongSelf)
1615
+ return;
1616
+ BOOL whole = fullSelection && strongSelf->_segmentViews.count == 1 && strongSelf->_cachedMarkdown.length > 0;
1617
+ [strongSelf emitCopyRequest:@"selection"
1618
+ markdown:whole ? strongSelf->_cachedMarkdown : markdown
1619
+ text:text
1620
+ whole:whole];
1621
+ };
1622
+
1344
1623
  return buildEditMenuForSelection(textView.attributedText, range, segmentMarkdown, _config, suggestedActions,
1345
- customActions, _selectionMenuConfig);
1624
+ customActions, _selectionMenuConfig, onCopyRequest);
1625
+ }
1626
+
1627
+ // Attachments other than images (inline math) are text, not items: without
1628
+ // this a long press on one in an unfocused text view starts UIKit's text item
1629
+ // interaction, which presents nothing and leaves the view unable to select.
1630
+ - (BOOL)textView:(UITextView *)textView
1631
+ shouldInteractWithTextAttachment:(NSTextAttachment *)textAttachment
1632
+ inRange:(NSRange)characterRange
1633
+ interaction:(UITextItemInteraction)interaction
1634
+ {
1635
+ if ([textAttachment isKindOfClass:[ENRMImageAttachment class]]) {
1636
+ return YES;
1637
+ }
1638
+ // A long press selects the attachment like a word, as it does once the view is focused.
1639
+ if (interaction == UITextItemInteractionPresentActions) {
1640
+ __weak UITextView *weakTextView = textView;
1641
+ dispatch_async(dispatch_get_main_queue(), ^{
1642
+ UITextView *strongTextView = weakTextView;
1643
+ if (!strongTextView || NSMaxRange(characterRange) > strongTextView.attributedText.length) {
1644
+ return;
1645
+ }
1646
+ ENRMSelectRangeAndPresentEditMenu(strongTextView, characterRange);
1647
+ });
1648
+ }
1649
+ return NO;
1346
1650
  }
1347
1651
 
1348
1652
  - (BOOL)textView:(UITextView *)textView
@@ -1356,6 +1660,18 @@ Class<RCTComponentViewProtocol> EnrichedMarkdownCls(void)
1356
1660
 
1357
1661
  NSString *urlString = linkURLAtRange(textView, characterRange);
1358
1662
 
1663
+ if (urlString && _linkLongPressSelects) {
1664
+ __weak UITextView *weakTextView = textView;
1665
+ dispatch_async(dispatch_get_main_queue(), ^{
1666
+ UITextView *strongTextView = weakTextView;
1667
+ if (!strongTextView || NSMaxRange(characterRange) > strongTextView.attributedText.length) {
1668
+ return;
1669
+ }
1670
+ ENRMSelectRangeAndPresentEditMenu(strongTextView, characterRange);
1671
+ });
1672
+ return NO;
1673
+ }
1674
+
1359
1675
  if (!urlString || _enableLinkPreview) {
1360
1676
  return YES;
1361
1677
  }