react-native-enriched-markdown-scaffold 1.0.2-scaffold.9 → 1.2.0-scaffold.1

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 (346) hide show
  1. package/ReactNativeEnrichedMarkdown.podspec +31 -10
  2. package/android/build.gradle +49 -12
  3. package/android/consumer-rules.pro +3 -0
  4. package/android/generated/java/com/facebook/react/viewmanagers/EnrichedMarkdownManagerDelegate.java +18 -0
  5. package/android/generated/java/com/facebook/react/viewmanagers/EnrichedMarkdownManagerInterface.java +6 -0
  6. package/android/generated/java/com/facebook/react/viewmanagers/EnrichedMarkdownTextInputManagerDelegate.java +3 -0
  7. package/android/generated/java/com/facebook/react/viewmanagers/EnrichedMarkdownTextInputManagerInterface.java +1 -0
  8. package/android/generated/java/com/facebook/react/viewmanagers/EnrichedMarkdownTextManagerDelegate.java +18 -0
  9. package/android/generated/java/com/facebook/react/viewmanagers/EnrichedMarkdownTextManagerInterface.java +6 -0
  10. package/android/generated/jni/react/renderer/components/EnrichedMarkdownTextSpec/EventEmitters.cpp +62 -0
  11. package/android/generated/jni/react/renderer/components/EnrichedMarkdownTextSpec/EventEmitters.h +44 -0
  12. package/android/generated/jni/react/renderer/components/EnrichedMarkdownTextSpec/Props.cpp +67 -2
  13. package/android/generated/jni/react/renderer/components/EnrichedMarkdownTextSpec/Props.h +929 -178
  14. package/android/src/main/baseline-prof.txt +21 -22
  15. package/android/src/main/cpp/jni-adapter.cpp +17 -5
  16. package/android/src/main/java/com/swmansion/enriched/markdown/EnrichedMarkdown.kt +718 -768
  17. package/android/src/main/java/com/swmansion/enriched/markdown/EnrichedMarkdownInternalText.kt +1 -1
  18. package/android/src/main/java/com/swmansion/enriched/markdown/EnrichedMarkdownManager.kt +72 -3
  19. package/android/src/main/java/com/swmansion/enriched/markdown/EnrichedMarkdownText.kt +210 -7
  20. package/android/src/main/java/com/swmansion/enriched/markdown/EnrichedMarkdownTextManager.kt +65 -1
  21. package/android/src/main/java/com/swmansion/enriched/markdown/MeasurementStore.kt +191 -26
  22. package/android/src/main/java/com/swmansion/enriched/markdown/accessibility/AccessibleMarkdownTextView.kt +10 -0
  23. package/android/src/main/java/com/swmansion/enriched/markdown/accessibility/MarkdownAccessibilityHelper.kt +54 -6
  24. package/android/src/main/java/com/swmansion/enriched/markdown/events/CodeBlockPressEvent.kt +24 -0
  25. package/android/src/main/java/com/swmansion/enriched/markdown/events/ImagePressEvent.kt +25 -0
  26. package/android/src/main/java/com/swmansion/enriched/markdown/events/LatexErrorEvent.kt +29 -0
  27. package/android/src/main/java/com/swmansion/enriched/markdown/input/EnrichedMarkdownTextInputManager.kt +21 -0
  28. package/android/src/main/java/com/swmansion/enriched/markdown/input/EnrichedMarkdownTextInputView.kt +101 -9
  29. package/android/src/main/java/com/swmansion/enriched/markdown/input/editing/EditPipeline.kt +48 -0
  30. package/android/src/main/java/com/swmansion/enriched/markdown/input/editing/InputConnectionWrapper.kt +7 -0
  31. package/android/src/main/java/com/swmansion/enriched/markdown/input/editing/MarkdownShortcutMatcher.kt +64 -0
  32. package/android/src/main/java/com/swmansion/enriched/markdown/input/formatting/InputRemend.kt +44 -6
  33. package/android/src/main/java/com/swmansion/enriched/markdown/input/layout/InputEventEmitter.kt +9 -8
  34. package/android/src/main/java/com/swmansion/enriched/markdown/math/LatexErrorReporter.kt +9 -0
  35. package/android/src/main/java/com/swmansion/enriched/markdown/parser/MarkdownASTNode.kt +6 -0
  36. package/android/src/main/java/com/swmansion/enriched/markdown/parser/Parser.kt +5 -1
  37. package/android/src/main/java/com/swmansion/enriched/markdown/renderer/BlankLineRenderer.kt +38 -0
  38. package/android/src/main/java/com/swmansion/enriched/markdown/renderer/BlockStyleContext.kt +2 -2
  39. package/android/src/main/java/com/swmansion/enriched/markdown/renderer/BlockquoteRenderer.kt +3 -2
  40. package/android/src/main/java/com/swmansion/enriched/markdown/renderer/BlockquoteTextRenderer.kt +55 -0
  41. package/android/src/main/java/com/swmansion/enriched/markdown/renderer/CodeBlockRenderer.kt +8 -1
  42. package/android/src/main/java/com/swmansion/enriched/markdown/renderer/HighlightRenderer.kt +10 -1
  43. package/android/src/main/java/com/swmansion/enriched/markdown/renderer/NodeRenderer.kt +31 -3
  44. package/android/src/main/java/com/swmansion/enriched/markdown/renderer/ParagraphRenderer.kt +17 -2
  45. package/android/src/main/java/com/swmansion/enriched/markdown/renderer/Renderer.kt +40 -21
  46. package/android/src/main/java/com/swmansion/enriched/markdown/segments/AdmonitionIcons.kt +62 -0
  47. package/android/src/main/java/com/swmansion/enriched/markdown/{views → segments}/BlockSegmentView.kt +1 -1
  48. package/android/src/main/java/com/swmansion/enriched/markdown/segments/BlockquoteContainerView.kt +401 -0
  49. package/android/src/main/java/com/swmansion/enriched/markdown/{views → segments}/CodeBlockContainerView.kt +55 -16
  50. package/android/src/main/java/com/swmansion/enriched/markdown/segments/ContainerNodeView.kt +164 -0
  51. package/android/src/main/java/com/swmansion/enriched/markdown/{utils/common → segments}/MarkdownSegment.kt +21 -1
  52. package/android/src/main/java/com/swmansion/enriched/markdown/segments/RenderedSegment.kt +137 -0
  53. package/android/src/main/java/com/swmansion/enriched/markdown/segments/SegmentHeightMeasurer.kt +139 -0
  54. package/android/src/main/java/com/swmansion/enriched/markdown/{utils/common → segments}/SegmentReconciler.kt +1 -1
  55. package/android/src/main/java/com/swmansion/enriched/markdown/{utils/common → segments}/SegmentSignature.kt +3 -1
  56. package/android/src/main/java/com/swmansion/enriched/markdown/segments/SegmentViewCreators.kt +262 -0
  57. package/android/src/main/java/com/swmansion/enriched/markdown/segments/SegmentViewFactory.kt +29 -0
  58. package/android/src/main/java/com/swmansion/enriched/markdown/{views → segments}/TableContainerView.kt +100 -36
  59. package/android/src/main/java/com/swmansion/enriched/markdown/spans/CodeBackgroundSpan.kt +18 -17
  60. package/android/src/main/java/com/swmansion/enriched/markdown/spans/CodeBlockSpan.kt +3 -0
  61. package/android/src/main/java/com/swmansion/enriched/markdown/spans/HighlightSpan.kt +60 -6
  62. package/android/src/main/java/com/swmansion/enriched/markdown/spans/ImageSpan.kt +15 -5
  63. package/android/src/main/java/com/swmansion/enriched/markdown/spans/InlineBackgroundGeometry.kt +42 -0
  64. package/android/src/main/java/com/swmansion/enriched/markdown/spans/InlineChipPainter.kt +4 -27
  65. package/android/src/main/java/com/swmansion/enriched/markdown/spans/LineHeightSpan.kt +10 -0
  66. package/android/src/main/java/com/swmansion/enriched/markdown/spans/LinkSpan.kt +1 -1
  67. package/android/src/main/java/com/swmansion/enriched/markdown/spoiler/SpoilerDrawUtils.kt +11 -0
  68. package/android/src/main/java/com/swmansion/enriched/markdown/styles/BlockquoteStyle.kt +27 -0
  69. package/android/src/main/java/com/swmansion/enriched/markdown/styles/LinkVariantEntry.kt +2 -0
  70. package/android/src/main/java/com/swmansion/enriched/markdown/styles/StyleConfig.kt +10 -0
  71. package/android/src/main/java/com/swmansion/enriched/markdown/styles/VideoStyle.kt +31 -0
  72. package/android/src/main/java/com/swmansion/enriched/markdown/utils/common/BorderStyleUtils.kt +86 -0
  73. package/android/src/main/java/com/swmansion/enriched/markdown/utils/common/EllipsizeUtils.kt +27 -0
  74. package/android/src/main/java/com/swmansion/enriched/markdown/utils/common/EnrichedMarkdownAncestorUtils.kt +10 -0
  75. package/android/src/main/java/com/swmansion/enriched/markdown/utils/common/FeatureFlags.kt +1 -0
  76. package/android/src/main/java/com/swmansion/enriched/markdown/utils/common/MarkdownViewManagerUtils.kt +36 -0
  77. package/android/src/main/java/com/swmansion/enriched/markdown/utils/common/ReadableMapExtensions.kt +5 -0
  78. package/android/src/main/java/com/swmansion/enriched/markdown/utils/common/serialization/MarkdownASTSerializer.kt +227 -0
  79. package/android/src/main/java/com/swmansion/enriched/markdown/utils/input/PasteTextUtils.kt +34 -2
  80. package/android/src/main/java/com/swmansion/enriched/markdown/utils/text/conversion/HTMLGenerator.kt +50 -28
  81. package/android/src/main/java/com/swmansion/enriched/markdown/utils/text/conversion/MarkdownExtractor.kt +16 -21
  82. package/android/src/main/java/com/swmansion/enriched/markdown/utils/text/extensions/SpannableExtensions.kt +4 -3
  83. package/android/src/main/java/com/swmansion/enriched/markdown/utils/text/span/BlockSpacing.kt +3 -2
  84. package/android/src/main/java/com/swmansion/enriched/markdown/utils/text/span/SpanFlags.kt +13 -0
  85. package/android/src/main/java/com/swmansion/enriched/markdown/utils/text/view/CodeBlockPressHost.kt +15 -0
  86. package/android/src/main/java/com/swmansion/enriched/markdown/utils/text/view/ImagePressHost.kt +19 -0
  87. package/android/src/main/java/com/swmansion/enriched/markdown/utils/text/view/LinkEvents.kt +11 -0
  88. package/android/src/main/java/com/swmansion/enriched/markdown/utils/text/view/LinkLongPressMovementMethod.kt +87 -2
  89. package/android/src/main/java/com/swmansion/enriched/markdown/utils/text/view/TextViewSetup.kt +79 -0
  90. package/android/src/main/jni/react/renderer/components/EnrichedMarkdownTextSpec/conversions.h +2 -0
  91. package/android/src/math/java/com/swmansion/enriched/markdown/renderer/MathInlineRenderer.kt +9 -3
  92. package/android/src/math/java/com/swmansion/enriched/markdown/{views → segments}/MathContainerView.kt +86 -29
  93. package/android/src/math/java/com/swmansion/enriched/markdown/spans/MathInlineSpan.kt +33 -7
  94. package/android/src/video/java/com/swmansion/enriched/markdown/segments/VideoContainerView.kt +115 -0
  95. package/cpp/md4c/md4c.c +343 -236
  96. package/cpp/md4c/md4c.h +28 -11
  97. package/cpp/parser/MD4CParser.cpp +266 -4
  98. package/cpp/parser/MD4CParser.hpp +6 -2
  99. package/cpp/parser/MarkdownASTNode.hpp +4 -1
  100. package/cpp/wasm/ASTSerializer.cpp +6 -0
  101. package/cpp/wasm/md4c_wasm.cpp +5 -1
  102. package/docs/API_REFERENCE.md +199 -12
  103. package/docs/BREAKING_CHANGES.md +75 -0
  104. package/docs/CODE_HIGHLIGHT.md +4 -0
  105. package/docs/COPY_OPTIONS.md +52 -9
  106. package/docs/ELEMENTS_STRUCTURE.md +48 -1
  107. package/docs/INPUT.md +60 -3
  108. package/docs/LATEX_MATH.md +30 -0
  109. package/docs/MACOS.md +1 -1
  110. package/docs/MENTIONS.md +2 -2
  111. package/docs/STYLES.md +27 -0
  112. package/docs/TEXT.md +8 -0
  113. package/docs/WEB.md +44 -10
  114. package/ios/EnrichedMarkdown.mm +285 -43
  115. package/ios/EnrichedMarkdownText.mm +185 -23
  116. package/ios/attachments/ENRMImageAttachment.h +17 -2
  117. package/ios/attachments/ENRMImageAttachment.m +26 -28
  118. package/ios/attachments/ENRMMathInlineAttachment+macOS.m +10 -2
  119. package/ios/attachments/ENRMMathInlineAttachment.h +3 -1
  120. package/ios/attachments/ENRMMathInlineAttachment.m +17 -1
  121. package/ios/attachments/ENRMMathInlineAttachmentShared.h +3 -0
  122. package/ios/generated/ReactCodegen/EnrichedMarkdownTextSpec/EventEmitters.cpp +62 -0
  123. package/ios/generated/ReactCodegen/EnrichedMarkdownTextSpec/EventEmitters.h +44 -0
  124. package/ios/generated/ReactCodegen/EnrichedMarkdownTextSpec/Props.cpp +67 -2
  125. package/ios/generated/ReactCodegen/EnrichedMarkdownTextSpec/Props.h +929 -178
  126. package/ios/input/ENRMInputFormatter.mm +68 -12
  127. package/ios/input/ENRMInputFormatterStyle.mm +9 -2
  128. package/ios/input/ENRMInputParser.mm +18 -0
  129. package/ios/input/ENRMInputRemend.mm +40 -3
  130. package/ios/input/ENRMMarkdownShortcutMatcher.h +39 -0
  131. package/ios/input/ENRMMarkdownShortcutMatcher.mm +66 -0
  132. package/ios/input/EnrichedMarkdownTextInput.mm +129 -1
  133. package/ios/internals/ENRMViewFreeMeasurement.h +45 -6
  134. package/ios/internals/MeasurementCache.h +20 -4
  135. package/ios/internals/ShadowMeasurementUtils.h +2 -0
  136. package/ios/math/ENRMLatexErrorCoordinator.h +27 -0
  137. package/ios/math/ENRMLatexErrorCoordinator.mm +75 -0
  138. package/ios/math/ENRMLatexErrorReporting.h +13 -0
  139. package/ios/math/ENRMRaTeXBridge.swift +46 -3
  140. package/ios/parser/ENRMMarkdownParser.h +3 -0
  141. package/ios/parser/ENRMMarkdownParser.mm +11 -2
  142. package/ios/parser/MarkdownASTNode.h +8 -1
  143. package/ios/parser/MarkdownASTNode.m +20 -0
  144. package/ios/parser/MarkdownParserBridge.mm +13 -2
  145. package/ios/renderer/AttributedRenderer.h +13 -1
  146. package/ios/renderer/AttributedRenderer.m +24 -13
  147. package/ios/renderer/BaselineShiftTextAttributes.h +4 -0
  148. package/ios/renderer/BaselineShiftTextAttributes.m +4 -0
  149. package/ios/renderer/BlankLineRenderer.h +4 -0
  150. package/ios/renderer/BlankLineRenderer.m +51 -0
  151. package/ios/renderer/BlockquoteRenderer.m +1 -0
  152. package/ios/renderer/CodeBlockRenderer.m +8 -0
  153. package/ios/renderer/CodeRenderer.m +5 -2
  154. package/ios/renderer/ENRMBlockquoteTextRenderer.h +25 -0
  155. package/ios/renderer/ENRMBlockquoteTextRenderer.m +48 -0
  156. package/ios/renderer/ENRMImageRenderer.m +1 -1
  157. package/ios/renderer/ENRMMathInlineRenderer.m +9 -1
  158. package/ios/renderer/HeadingRenderer.m +1 -0
  159. package/ios/renderer/HighlightRenderer.m +6 -0
  160. package/ios/renderer/LinkRenderer.m +1 -1
  161. package/ios/renderer/ListItemRenderer.m +51 -2
  162. package/ios/renderer/ParagraphRenderer.m +10 -3
  163. package/ios/renderer/RenderContext.h +16 -0
  164. package/ios/renderer/RenderContext.m +16 -0
  165. package/ios/renderer/RendererFactory.m +6 -0
  166. package/ios/renderer/SubscriptRenderer.m +1 -0
  167. package/ios/renderer/SuperscriptRenderer.m +1 -0
  168. package/ios/segments/ENRMAdmonitionIcons.h +31 -0
  169. package/ios/segments/ENRMAdmonitionIcons.m +93 -0
  170. package/ios/segments/ENRMBlockquoteContainerView.h +90 -0
  171. package/ios/segments/ENRMBlockquoteContainerView.mm +551 -0
  172. package/ios/{views → segments}/ENRMCodeBlockContainerView.h +9 -4
  173. package/ios/{views → segments}/ENRMCodeBlockContainerView.m +147 -36
  174. package/ios/segments/ENRMContainerNodeView.h +52 -0
  175. package/ios/segments/ENRMContainerNodeView.mm +201 -0
  176. package/ios/segments/ENRMDynamicBlockProps.h +32 -0
  177. package/ios/segments/ENRMDynamicBlockProps.m +15 -0
  178. package/ios/{views → segments}/ENRMMathContainerView.h +13 -5
  179. package/ios/{views → segments}/ENRMMathContainerView.m +60 -9
  180. package/ios/segments/ENRMSVGPath.h +21 -0
  181. package/ios/segments/ENRMSVGPath.m +409 -0
  182. package/ios/segments/ENRMSegmentHeightMeasurer.h +33 -0
  183. package/ios/segments/ENRMSegmentHeightMeasurer.mm +94 -0
  184. package/ios/segments/ENRMVideoContainerView.h +40 -0
  185. package/ios/segments/ENRMVideoContainerView.m +421 -0
  186. package/ios/{utils → segments}/RenderedMarkdownSegment.h +19 -2
  187. package/ios/{utils → segments}/RenderedMarkdownSegment.m +42 -1
  188. package/ios/segments/SegmentRenderer.h +26 -0
  189. package/ios/{utils → segments}/SegmentRenderer.m +46 -9
  190. package/ios/{views → segments}/TableContainerView.h +5 -4
  191. package/ios/{views → segments}/TableContainerView.m +96 -12
  192. package/ios/styles/StyleConfig.h +17 -0
  193. package/ios/styles/StyleConfig.mm +90 -0
  194. package/ios/utils/ENRMCodeBlockContent.mm +13 -0
  195. package/ios/utils/ENRMFeatureFlags.h +6 -0
  196. package/ios/utils/ENRMTextRenderer.h +8 -0
  197. package/ios/utils/ENRMTextRenderer.m +26 -10
  198. package/ios/utils/ENRMUIKit.h +1 -0
  199. package/ios/utils/FontUtils.h +2 -1
  200. package/ios/utils/FontUtils.m +22 -6
  201. package/ios/utils/LinkTapUtils.h +17 -2
  202. package/ios/utils/LinkTapUtils.m +44 -2
  203. package/ios/utils/ListMarkerDrawer.m +25 -1
  204. package/ios/utils/MarkdownASTSerializer.h +7 -0
  205. package/ios/utils/MarkdownASTSerializer.m +243 -0
  206. package/ios/utils/MarkdownAccessibilityElementBuilder.m +9 -67
  207. package/ios/utils/MarkdownExtractor.m +6 -3
  208. package/ios/utils/ParagraphStyleUtils.h +25 -1
  209. package/ios/utils/ParagraphStyleUtils.m +51 -7
  210. package/ios/utils/StylePropsUtils.h +60 -1
  211. package/ios/utils/TextViewLayoutManager.mm +57 -0
  212. package/lib/module/EnrichedMarkdownNativeComponent.ts +110 -4
  213. package/lib/module/EnrichedMarkdownTextInput.js +60 -2
  214. package/lib/module/EnrichedMarkdownTextInput.js.map +1 -1
  215. package/lib/module/EnrichedMarkdownTextInputNativeComponent.ts +12 -0
  216. package/lib/module/EnrichedMarkdownTextNativeComponent.ts +115 -4
  217. package/lib/module/admonitionDefaults.js +34 -0
  218. package/lib/module/admonitionDefaults.js.map +1 -0
  219. package/lib/module/index.js.map +1 -1
  220. package/lib/module/jest/index.js +84 -4
  221. package/lib/module/jest/index.js.map +1 -1
  222. package/lib/module/native/EnrichedMarkdownText.js +58 -5
  223. package/lib/module/native/EnrichedMarkdownText.js.map +1 -1
  224. package/lib/module/normalizeMarkdownShortcuts.js +31 -0
  225. package/lib/module/normalizeMarkdownShortcuts.js.map +1 -0
  226. package/lib/module/normalizeMarkdownStyle.js +15 -1
  227. package/lib/module/normalizeMarkdownStyle.js.map +1 -1
  228. package/lib/module/normalizeMarkdownStyle.web.js +15 -1
  229. package/lib/module/normalizeMarkdownStyle.web.js.map +1 -1
  230. package/lib/module/styleUtils.js +15 -3
  231. package/lib/module/styleUtils.js.map +1 -1
  232. package/lib/module/utils/textInputState.js +4 -3
  233. package/lib/module/utils/textInputState.js.map +1 -1
  234. package/lib/module/utils/usePressability.js +28 -0
  235. package/lib/module/utils/usePressability.js.map +1 -0
  236. package/lib/module/web/EnrichedMarkdownText.js +19 -6
  237. package/lib/module/web/EnrichedMarkdownText.js.map +1 -1
  238. package/lib/module/web/globalStyles.js +7 -1
  239. package/lib/module/web/globalStyles.js.map +1 -1
  240. package/lib/module/web/nativeProps.js +70 -0
  241. package/lib/module/web/nativeProps.js.map +1 -0
  242. package/lib/module/web/parseMarkdown.js +5 -3
  243. package/lib/module/web/parseMarkdown.js.map +1 -1
  244. package/lib/module/web/renderers/BlockRenderers.js +164 -8
  245. package/lib/module/web/renderers/BlockRenderers.js.map +1 -1
  246. package/lib/module/web/renderers/admonitionIcons.js +34 -0
  247. package/lib/module/web/renderers/admonitionIcons.js.map +1 -0
  248. package/lib/module/web/styles.js +30 -8
  249. package/lib/module/web/styles.js.map +1 -1
  250. package/lib/module/web/wasm/md4c.js +0 -0
  251. package/lib/module/web/wasm/md4c.js.map +1 -1
  252. package/lib/typescript/src/EnrichedMarkdownNativeComponent.d.ts +103 -3
  253. package/lib/typescript/src/EnrichedMarkdownNativeComponent.d.ts.map +1 -1
  254. package/lib/typescript/src/EnrichedMarkdownTextInput.d.ts +27 -3
  255. package/lib/typescript/src/EnrichedMarkdownTextInput.d.ts.map +1 -1
  256. package/lib/typescript/src/EnrichedMarkdownTextInputNativeComponent.d.ts +11 -0
  257. package/lib/typescript/src/EnrichedMarkdownTextInputNativeComponent.d.ts.map +1 -1
  258. package/lib/typescript/src/EnrichedMarkdownTextNativeComponent.d.ts +108 -3
  259. package/lib/typescript/src/EnrichedMarkdownTextNativeComponent.d.ts.map +1 -1
  260. package/lib/typescript/src/admonitionDefaults.d.ts +10 -0
  261. package/lib/typescript/src/admonitionDefaults.d.ts.map +1 -0
  262. package/lib/typescript/src/index.d.ts +3 -2
  263. package/lib/typescript/src/index.d.ts.map +1 -1
  264. package/lib/typescript/src/index.web.d.ts +1 -1
  265. package/lib/typescript/src/index.web.d.ts.map +1 -1
  266. package/lib/typescript/src/jest/index.d.ts +2 -2
  267. package/lib/typescript/src/jest/index.d.ts.map +1 -1
  268. package/lib/typescript/src/native/EnrichedMarkdownText.d.ts +3 -3
  269. package/lib/typescript/src/native/EnrichedMarkdownText.d.ts.map +1 -1
  270. package/lib/typescript/src/normalizeMarkdownShortcuts.d.ts +7 -0
  271. package/lib/typescript/src/normalizeMarkdownShortcuts.d.ts.map +1 -0
  272. package/lib/typescript/src/normalizeMarkdownStyle.d.ts.map +1 -1
  273. package/lib/typescript/src/normalizeMarkdownStyle.web.d.ts.map +1 -1
  274. package/lib/typescript/src/styleUtils.d.ts.map +1 -1
  275. package/lib/typescript/src/types/MarkdownStyle.d.ts +59 -1
  276. package/lib/typescript/src/types/MarkdownStyle.d.ts.map +1 -1
  277. package/lib/typescript/src/types/MarkdownStyleInternal.d.ts +21 -0
  278. package/lib/typescript/src/types/MarkdownStyleInternal.d.ts.map +1 -1
  279. package/lib/typescript/src/types/MarkdownTextProps.d.ts +93 -8
  280. package/lib/typescript/src/types/MarkdownTextProps.d.ts.map +1 -1
  281. package/lib/typescript/src/types/MarkdownTextProps.web.d.ts +25 -1
  282. package/lib/typescript/src/types/MarkdownTextProps.web.d.ts.map +1 -1
  283. package/lib/typescript/src/types/events.d.ts +33 -5
  284. package/lib/typescript/src/types/events.d.ts.map +1 -1
  285. package/lib/typescript/src/utils/textInputState.d.ts +5 -3
  286. package/lib/typescript/src/utils/textInputState.d.ts.map +1 -1
  287. package/lib/typescript/src/utils/usePressability.d.ts +24 -0
  288. package/lib/typescript/src/utils/usePressability.d.ts.map +1 -0
  289. package/lib/typescript/src/web/EnrichedMarkdownText.d.ts +1 -1
  290. package/lib/typescript/src/web/EnrichedMarkdownText.d.ts.map +1 -1
  291. package/lib/typescript/src/web/globalStyles.d.ts +1 -0
  292. package/lib/typescript/src/web/globalStyles.d.ts.map +1 -1
  293. package/lib/typescript/src/web/nativeProps.d.ts +34 -0
  294. package/lib/typescript/src/web/nativeProps.d.ts.map +1 -0
  295. package/lib/typescript/src/web/parseMarkdown.d.ts +1 -1
  296. package/lib/typescript/src/web/parseMarkdown.d.ts.map +1 -1
  297. package/lib/typescript/src/web/renderers/BlockRenderers.d.ts.map +1 -1
  298. package/lib/typescript/src/web/renderers/admonitionIcons.d.ts +5 -0
  299. package/lib/typescript/src/web/renderers/admonitionIcons.d.ts.map +1 -0
  300. package/lib/typescript/src/web/styles.d.ts +1 -0
  301. package/lib/typescript/src/web/styles.d.ts.map +1 -1
  302. package/lib/typescript/src/web/types.d.ts +8 -2
  303. package/lib/typescript/src/web/types.d.ts.map +1 -1
  304. package/package.json +1 -1
  305. package/postinstall.mjs +7 -0
  306. package/src/EnrichedMarkdownNativeComponent.ts +110 -4
  307. package/src/EnrichedMarkdownTextInput.tsx +101 -4
  308. package/src/EnrichedMarkdownTextInputNativeComponent.ts +12 -0
  309. package/src/EnrichedMarkdownTextNativeComponent.ts +115 -4
  310. package/src/admonitionDefaults.ts +58 -0
  311. package/src/index.tsx +5 -0
  312. package/src/index.web.tsx +3 -0
  313. package/src/jest/index.tsx +138 -5
  314. package/src/native/EnrichedMarkdownText.tsx +55 -3
  315. package/src/normalizeMarkdownShortcuts.ts +38 -0
  316. package/src/normalizeMarkdownStyle.ts +17 -0
  317. package/src/normalizeMarkdownStyle.web.ts +17 -0
  318. package/src/styleUtils.ts +17 -6
  319. package/src/types/MarkdownStyle.ts +62 -1
  320. package/src/types/MarkdownStyleInternal.ts +24 -0
  321. package/src/types/MarkdownTextProps.ts +95 -7
  322. package/src/types/MarkdownTextProps.web.ts +26 -0
  323. package/src/types/events.ts +36 -5
  324. package/src/utils/textInputState.ts +5 -3
  325. package/src/utils/usePressability.ts +34 -0
  326. package/src/web/EnrichedMarkdownText.tsx +36 -3
  327. package/src/web/globalStyles.ts +10 -0
  328. package/src/web/nativeProps.ts +78 -0
  329. package/src/web/parseMarkdown.ts +10 -2
  330. package/src/web/renderers/BlockRenderers.tsx +175 -6
  331. package/src/web/renderers/admonitionIcons.ts +37 -0
  332. package/src/web/styles.ts +28 -3
  333. package/src/web/types.ts +12 -1
  334. package/src/web/wasm/md4c.js +0 -0
  335. package/vendor-ratex.mjs +86 -17
  336. package/android/src/main/java/com/swmansion/enriched/markdown/renderer/DocumentRenderer.kt +0 -16
  337. package/android/src/main/java/com/swmansion/enriched/markdown/utils/common/RenderedSegment.kt +0 -88
  338. package/ios/utils/SegmentRenderer.h +0 -24
  339. /package/ios/{views → segments}/ENRMTableGridView.h +0 -0
  340. /package/ios/{views → segments}/ENRMTableGridView.m +0 -0
  341. /package/ios/{views → segments}/ENRMTableIOSGridView.h +0 -0
  342. /package/ios/{views → segments}/ENRMTableIOSGridView.m +0 -0
  343. /package/ios/{utils → segments}/SegmentReconciler.h +0 -0
  344. /package/ios/{utils → segments}/SegmentReconciler.m +0 -0
  345. /package/ios/{utils → segments}/SegmentViewRegistry.h +0 -0
  346. /package/ios/{utils → segments}/SegmentViewRegistry.m +0 -0
@@ -5,8 +5,12 @@ import android.os.Looper
5
5
  import android.text.Spannable
6
6
  import android.text.method.ArrowKeyMovementMethod
7
7
  import android.view.MotionEvent
8
+ import android.view.View
8
9
  import android.view.ViewConfiguration
10
+ import android.view.ViewParent
9
11
  import android.widget.TextView
12
+ import com.swmansion.enriched.markdown.spans.CodeBlockSpan
13
+ import com.swmansion.enriched.markdown.spans.ImageSpan
10
14
  import com.swmansion.enriched.markdown.spans.LinkSpan
11
15
  import com.swmansion.enriched.markdown.spans.SpoilerSpan
12
16
  import com.swmansion.enriched.markdown.spoiler.SpoilerCapable
@@ -28,6 +32,8 @@ class LinkLongPressMovementMethod : ArrowKeyMovementMethod() {
28
32
  private var startX = 0f
29
33
  private var startY = 0f
30
34
  private var pressedLink: LinkSpan? = null
35
+ private var pressedImage: ImageSpan? = null
36
+ private var pressedCodeBlock: CodeBlockSpan? = null
31
37
 
32
38
  var isLinkTouchActive: Boolean = false
33
39
  private set
@@ -44,8 +50,26 @@ class LinkLongPressMovementMethod : ArrowKeyMovementMethod() {
44
50
  startY = event.y
45
51
 
46
52
  pressedLink = findLinkSpan(widget, buffer, event)
47
- isLinkTouchActive = pressedLink != null
48
- isTouchWithinTextBounds = charOffsetAt(widget, event) != null
53
+ pressedImage =
54
+ if (pressedLink == null && imagePressHost(widget)?.imagePressEnabled == true) {
55
+ findImageSpan(widget, buffer, event)
56
+ } else {
57
+ null
58
+ }
59
+ pressedCodeBlock =
60
+ if (pressedLink == null &&
61
+ pressedImage == null &&
62
+ // Skip while text is selected so the tap clears the selection
63
+ // instead of firing the press (matches the web guard).
64
+ !widget.hasSelection() &&
65
+ codeBlockPressHost(widget)?.codeBlockPressEnabled == true
66
+ ) {
67
+ findCodeBlockSpan(widget, buffer, event)
68
+ } else {
69
+ null
70
+ }
71
+ isLinkTouchActive = pressedLink != null || pressedImage != null || pressedCodeBlock != null
72
+ isTouchWithinTextBounds = charOffsetAt(widget, event) != null || pressedCodeBlock != null
49
73
  pressedLink?.let { scheduleLongPress(widget, it) }
50
74
  }
51
75
 
@@ -57,14 +81,20 @@ class LinkLongPressMovementMethod : ArrowKeyMovementMethod() {
57
81
  cancelLongPress()
58
82
  isLinkTouchActive = false
59
83
  pressedLink = null
84
+ pressedImage = null
85
+ pressedCodeBlock = null
60
86
  }
61
87
  }
62
88
 
63
89
  MotionEvent.ACTION_UP -> {
64
90
  cancelLongPress()
65
91
  val tappedLink = pressedLink
92
+ val tappedImage = pressedImage
93
+ val tappedCodeBlock = pressedCodeBlock
66
94
  isLinkTouchActive = false
67
95
  pressedLink = null
96
+ pressedImage = null
97
+ pressedCodeBlock = null
68
98
 
69
99
  if (handleSpoilerTap(widget, buffer, event)) {
70
100
  return true
@@ -77,12 +107,24 @@ class LinkLongPressMovementMethod : ArrowKeyMovementMethod() {
77
107
  tappedLink.onClick(widget)
78
108
  return true
79
109
  }
110
+
111
+ if (tappedImage != null && findImageSpan(widget, buffer, event) === tappedImage) {
112
+ imagePressHost(widget)?.emitOnImagePress(tappedImage.imageUrl, tappedImage.altText)
113
+ return true
114
+ }
115
+
116
+ if (tappedCodeBlock != null && findCodeBlockSpan(widget, buffer, event) === tappedCodeBlock) {
117
+ codeBlockPressHost(widget)?.emitOnCodeBlockPress(tappedCodeBlock.code, tappedCodeBlock.language)
118
+ return true
119
+ }
80
120
  }
81
121
 
82
122
  MotionEvent.ACTION_CANCEL -> {
83
123
  cancelLongPress()
84
124
  isLinkTouchActive = false
85
125
  pressedLink = null
126
+ pressedImage = null
127
+ pressedCodeBlock = null
86
128
  }
87
129
  }
88
130
 
@@ -158,6 +200,49 @@ class LinkLongPressMovementMethod : ArrowKeyMovementMethod() {
158
200
  return buffer.getSpans(offset, offset, LinkSpan::class.java).firstOrNull()
159
201
  }
160
202
 
203
+ private fun findImageSpan(
204
+ widget: TextView,
205
+ buffer: Spannable,
206
+ event: MotionEvent,
207
+ ): ImageSpan? {
208
+ val offset = charOffsetAt(widget, event) ?: return null
209
+ return buffer.getSpans(offset, offset, ImageSpan::class.java).firstOrNull()
210
+ }
211
+
212
+ private fun imagePressHost(widget: View): ImagePressHost? {
213
+ if (widget is ImagePressHost) return widget
214
+ var parent: ViewParent? = widget.parent
215
+ while (parent != null && parent !is ImagePressHost) {
216
+ parent = parent.parent
217
+ }
218
+ return parent as? ImagePressHost
219
+ }
220
+
221
+ // A code block is a whole-line box, so this matches a tap anywhere on the line
222
+ // (ignoring x), unlike charOffsetAt which needs an exact glyph.
223
+ private fun findCodeBlockSpan(
224
+ widget: TextView,
225
+ buffer: Spannable,
226
+ event: MotionEvent,
227
+ ): CodeBlockSpan? {
228
+ val layout = widget.layout ?: return null
229
+ val y = event.y - widget.totalPaddingTop + widget.scrollY
230
+ if (y < 0f || y > layout.height) return null
231
+ val line = layout.getLineForVertical(y.toInt())
232
+ val lineStart = layout.getLineStart(line)
233
+ val lineEnd = layout.getLineEnd(line)
234
+ return buffer.getSpans(lineStart, lineEnd, CodeBlockSpan::class.java).firstOrNull()
235
+ }
236
+
237
+ private fun codeBlockPressHost(widget: View): CodeBlockPressHost? {
238
+ if (widget is CodeBlockPressHost) return widget
239
+ var parent: ViewParent? = widget.parent
240
+ while (parent != null && parent !is CodeBlockPressHost) {
241
+ parent = parent.parent
242
+ }
243
+ return parent as? CodeBlockPressHost
244
+ }
245
+
161
246
  private fun handleSpoilerTap(
162
247
  widget: TextView,
163
248
  buffer: Spannable,
@@ -2,13 +2,92 @@ package com.swmansion.enriched.markdown.utils.text.view
2
2
 
3
3
  import android.graphics.Color
4
4
  import android.os.Build
5
+ import android.text.GetChars
6
+ import android.text.Spannable
7
+ import android.text.SpannableString
8
+ import android.text.SpannableStringBuilder
5
9
  import android.view.textclassifier.TextClassifier
6
10
  import androidx.appcompat.widget.AppCompatTextView
7
11
  import androidx.core.view.ViewCompat
8
12
  import com.swmansion.enriched.markdown.accessibility.AccessibleMarkdownTextView
9
13
 
14
+ /**
15
+ * Adopts the renderer's buffer instead of copying it: the default factory's copy is
16
+ * quadratic in span count. Safe because each segment's buffer is owned by one view.
17
+ */
18
+ private object NoCopySpannableFactory : Spannable.Factory() {
19
+ override fun newSpannable(source: CharSequence): Spannable =
20
+ when (source) {
21
+ is SpannableStringBuilder -> InsertionOrderedSpannable(source)
22
+ is Spannable -> source
23
+ else -> SpannableString(source)
24
+ }
25
+ }
26
+
27
+ /**
28
+ * Hides that [buffer] is a `SpannableStringBuilder`. Layout reads a builder's paragraph
29
+ * spans in position order rather than insertion order, which paints nested list margins
30
+ * outer-first and pushes nested checkboxes an indent right.
31
+ *
32
+ * Hiding the type has a price, and it is deliberate: every paragraph-span query from
33
+ * `Layout` now goes through the builder's sorted `getSpans` instead of the unsorted fast
34
+ * path the framework reserves for builders. The `layout` benchmark in the example app's
35
+ * `display-benchmark` covers that cost, so it is already accounted for. Do not
36
+ * "optimise" this wrapper away.
37
+ */
38
+ private class InsertionOrderedSpannable(
39
+ private val buffer: SpannableStringBuilder,
40
+ ) : Spannable,
41
+ GetChars {
42
+ override val length: Int get() = buffer.length
43
+
44
+ override fun get(index: Int): Char = buffer[index]
45
+
46
+ override fun subSequence(
47
+ startIndex: Int,
48
+ endIndex: Int,
49
+ ): CharSequence = buffer.subSequence(startIndex, endIndex)
50
+
51
+ override fun getChars(
52
+ start: Int,
53
+ end: Int,
54
+ dest: CharArray,
55
+ destoff: Int,
56
+ ) = buffer.getChars(start, end, dest, destoff)
57
+
58
+ override fun <T : Any?> getSpans(
59
+ start: Int,
60
+ end: Int,
61
+ type: Class<T>,
62
+ ): Array<T> = buffer.getSpans(start, end, type)
63
+
64
+ override fun getSpanStart(tag: Any): Int = buffer.getSpanStart(tag)
65
+
66
+ override fun getSpanEnd(tag: Any): Int = buffer.getSpanEnd(tag)
67
+
68
+ override fun getSpanFlags(tag: Any): Int = buffer.getSpanFlags(tag)
69
+
70
+ override fun nextSpanTransition(
71
+ start: Int,
72
+ limit: Int,
73
+ type: Class<*>?,
74
+ ): Int = buffer.nextSpanTransition(start, limit, type)
75
+
76
+ override fun setSpan(
77
+ what: Any,
78
+ start: Int,
79
+ end: Int,
80
+ flags: Int,
81
+ ) = buffer.setSpan(what, start, end, flags)
82
+
83
+ override fun removeSpan(what: Any) = buffer.removeSpan(what)
84
+
85
+ override fun toString(): String = buffer.toString()
86
+ }
87
+
10
88
  fun AccessibleMarkdownTextView.setupAsMarkdownTextView() {
11
89
  setBackgroundColor(Color.TRANSPARENT)
90
+ setSpannableFactory(NoCopySpannableFactory)
12
91
  includeFontPadding = false
13
92
  movementMethod = LinkLongPressMovementMethod.createInstance()
14
93
  setTextIsSelectable(true)
@@ -16,6 +16,8 @@ inline folly::dynamic toDynamic(const EnrichedMarkdownTextProps &props) {
16
16
  serializedProps["maxFontSizeMultiplier"] = props.maxFontSizeMultiplier;
17
17
  serializedProps["allowTrailingMargin"] = props.allowTrailingMargin;
18
18
  serializedProps["streamingAnimation"] = props.streamingAnimation;
19
+ serializedProps["numberOfLines"] = props.numberOfLines;
20
+ serializedProps["ellipsizeMode"] = props.ellipsizeMode;
19
21
 
20
22
  folly::dynamic imageRequestHeaders = folly::dynamic::array();
21
23
  for (const auto &header : props.imageRequestHeaders) {
@@ -23,7 +23,12 @@ class MathInlineRenderer(
23
23
  val latex = extractLatex(node)
24
24
  if (latex.isEmpty()) return
25
25
 
26
- val blockStyle = factory.blockStyleContext.requireBlockStyle()
26
+ val fontSize =
27
+ factory.blockStyleContext.currentBlockStyleOrNull()?.fontSize
28
+ ?: config.style.mathStyle.fontSize
29
+ val mathColor = config.style.inlineMathStyle.color
30
+ // Inside `==…==` the formula takes the highlight colour, by the rule text follows.
31
+ val textColor = if (factory.inHighlight) factory.styleCache.getHighlightColorFor(mathColor) else mathColor
27
32
 
28
33
  val start = builder.length
29
34
  builder.append("\uFFFC")
@@ -33,11 +38,12 @@ class MathInlineRenderer(
33
38
  MathInlineSpan(
34
39
  context = context,
35
40
  latex = latex,
36
- fontSize = blockStyle.fontSize,
37
- textColor = config.style.inlineMathStyle.color,
41
+ fontSize = fontSize,
42
+ textColor = textColor,
38
43
  paddingVertical = config.style.inlineMathStyle.paddingVertical,
39
44
  fontWeight = config.style.inlineMathStyle.fontWeight,
40
45
  glyphOverflow = config.style.inlineMathStyle.glyphOverflow,
46
+ onLatexError = config.onLatexError,
41
47
  )
42
48
 
43
49
  builder.setSpan(span, start, end, SPAN_FLAGS_EXCLUSIVE_EXCLUSIVE)
@@ -1,9 +1,11 @@
1
- package com.swmansion.enriched.markdown.views
1
+ package com.swmansion.enriched.markdown.segments
2
2
 
3
3
  import android.content.ClipData
4
4
  import android.content.ClipboardManager
5
5
  import android.content.Context
6
6
  import android.graphics.Canvas
7
+ import android.graphics.Color
8
+ import android.graphics.Paint
7
9
  import android.util.Log
8
10
  import android.view.Gravity
9
11
  import android.view.View
@@ -12,12 +14,16 @@ import android.view.ViewGroup.LayoutParams.WRAP_CONTENT
12
14
  import android.widget.FrameLayout
13
15
  import android.widget.HorizontalScrollView
14
16
  import com.swmansion.enriched.markdown.accessibility.AccessibilityLabels
17
+ import com.swmansion.enriched.markdown.math.LatexErrorReporter
15
18
  import com.swmansion.enriched.markdown.spans.MathMeasureHelper
16
19
  import com.swmansion.enriched.markdown.spans.MathMeasureRequest
17
20
  import com.swmansion.enriched.markdown.spans.MathRenderMode
18
21
  import com.swmansion.enriched.markdown.spans.ratexTypefaceLoader
19
22
  import com.swmansion.enriched.markdown.styles.MathStyle
20
23
  import com.swmansion.enriched.markdown.styles.StyleConfig
24
+ import com.swmansion.enriched.markdown.views.BlockMenuOptions
25
+ import com.swmansion.enriched.markdown.views.ContextMenuPopup
26
+ import com.swmansion.enriched.markdown.views.blockCopyItems
21
27
  import io.ratex.RaTeXEngine
22
28
  import io.ratex.RaTeXFontLoader
23
29
  import io.ratex.RaTeXRenderer
@@ -37,10 +43,11 @@ class MathContainerView(
37
43
  field = value
38
44
  updateAccessibilityLabel()
39
45
  }
46
+ var dynamicProps: DynamicBlockProps = DynamicBlockProps()
47
+ var onLatexError: LatexErrorReporter? = null
40
48
 
41
- // Set reflectively by EnrichedMarkdown (math is an optional module).
42
- var copyLabel: String = ""
43
- var copyAsMarkdownLabel: String = ""
49
+ // Fork: set reflectively by EnrichedMarkdown (math is an optional module). Labels
50
+ // come from dynamicProps. contextMenuHidden wins over enableBlockContextMenu (D1).
44
51
  var contextMenuHidden: Boolean = false
45
52
  var menuOptions: BlockMenuOptions = BlockMenuOptions()
46
53
 
@@ -60,6 +67,24 @@ class MathContainerView(
60
67
 
61
68
  private val mathView = RaTeXCanvasView(context, mathStyle.glyphOverflow)
62
69
 
70
+ private val highlightBackgroundColor = styleConfig.highlightStyle.backgroundColor
71
+
72
+ // The highlight colour as text resolves it: the paragraph colour left in place means
73
+ // "inherit", which for a formula is its own colour.
74
+ private val highlightColor =
75
+ styleConfig.highlightStyle.color.let { if (it == styleConfig.paragraphStyle.color) mathStyle.color else it }
76
+
77
+ // Set reflectively, before applyLatex: a `==$$…$$==` block washes the formula's own
78
+ // frame and parses the formula in the highlight colour. Changed afterwards, the cached
79
+ // latex is parsed again.
80
+ var highlighted: Boolean = false
81
+ set(value) {
82
+ val changed = field != value
83
+ field = value
84
+ mathView.setBackgroundColor(if (value) highlightBackgroundColor else Color.TRANSPARENT)
85
+ if (changed && cachedLatex.isNotEmpty()) applyLatex(cachedLatex)
86
+ }
87
+
63
88
  init {
64
89
  setBackgroundColor(mathStyle.backgroundColor)
65
90
 
@@ -93,25 +118,25 @@ class MathContainerView(
93
118
  importantForAccessibility = View.IMPORTANT_FOR_ACCESSIBILITY_YES
94
119
  updateAccessibilityLabel()
95
120
 
96
- setOnLongClickListener { view ->
97
- showContextMenu(view)
98
- true
99
- }
100
- mathView.setOnLongClickListener { view ->
101
- showContextMenu(view)
102
- true
103
- }
121
+ setOnLongClickListener { view -> showContextMenu(view) }
122
+ mathView.setOnLongClickListener { view -> showContextMenu(view) }
104
123
  }
105
124
 
106
125
  fun applyLatex(latex: String) {
107
126
  cachedLatex = latex
108
127
  try {
109
- 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)
110
130
  mathView.renderer =
111
131
  RaTeXRenderer(displayList, mathStyle.fontSize, ratexTypefaceLoader(mathStyle.fontWeight))
132
+ mathView.fallbackText = null
112
133
  } catch (e: Exception) {
113
134
  Log.e(TAG, "Failed to render LaTeX", e)
114
135
  mathView.renderer = null
136
+ mathView.fallbackText = "\$\$" + latex + "\$\$"
137
+ mathView.fallbackColor = mathStyle.color
138
+ mathView.fallbackFontSize = mathStyle.fontSize
139
+ onLatexError?.report(latex, e.message, true)
115
140
  }
116
141
  mathView.requestLayout()
117
142
  mathView.invalidate()
@@ -122,14 +147,17 @@ class MathContainerView(
122
147
  contentDescription = accessibilityLabels.mathEquation.replace("{latex}", cachedLatex)
123
148
  }
124
149
 
125
- private fun showContextMenu(anchor: View) {
150
+ private fun showContextMenu(anchor: View): Boolean {
126
151
  if (contextMenuHidden) {
127
- onMenuSuppressed?.invoke("$$\n$cachedLatex\n$$", cachedLatex)
128
- return
152
+ val onSuppressed = onMenuSuppressed ?: return false
153
+ onSuppressed("$$\n$cachedLatex\n$$", cachedLatex)
154
+ return true
129
155
  }
156
+ if (!dynamicProps.enableBlockContextMenu) return false
157
+ if (!menuOptions.hasItems) return false
130
158
  val clipboard = context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
131
159
  ContextMenuPopup.show(anchor, this) {
132
- blockCopyItems(menuOptions, copyLabel, copyAsMarkdownLabel, markdown = {
160
+ blockCopyItems(menuOptions, dynamicProps.copyLabel, dynamicProps.copyAsMarkdownLabel, markdown = {
133
161
  "$$\n$cachedLatex\n$$"
134
162
  }, rawText = { cachedLatex }, copyRaw = {
135
163
  clipboard.setPrimaryClip(ClipData.newPlainText("Math", cachedLatex))
@@ -137,6 +165,7 @@ class MathContainerView(
137
165
  clipboard.setPrimaryClip(ClipData.newPlainText("Math", "$$\n$cachedLatex\n$$"))
138
166
  })
139
167
  }
168
+ return true
140
169
  }
141
170
 
142
171
  // glyphOverflow px of headroom above and below the RaTeX box: digit and capital
@@ -147,25 +176,53 @@ class MathContainerView(
147
176
  ) : View(context) {
148
177
  var renderer: RaTeXRenderer? = null
149
178
 
179
+ var fallbackText: String? = null
180
+ var fallbackColor: Int = 0
181
+ var fallbackFontSize: Float = 0f
182
+ private val fallbackPaint = Paint(Paint.ANTI_ALIAS_FLAG)
183
+
150
184
  override fun onMeasure(
151
185
  widthMeasureSpec: Int,
152
186
  heightMeasureSpec: Int,
153
187
  ) {
154
- val currentRenderer =
155
- renderer
156
- ?: return setMeasuredDimension(0, 0)
157
- setMeasuredDimension(
158
- ceil(currentRenderer.widthPx).toInt().coerceAtLeast(1),
159
- ceil(currentRenderer.totalHeightPx).toInt().coerceAtLeast(1) + (glyphOverflow * 2).toInt(),
160
- )
188
+ val currentRenderer = renderer
189
+ if (currentRenderer != null) {
190
+ // glyphOverflow px of headroom above and below the TeX box (fork, HWK-1341).
191
+ setMeasuredDimension(
192
+ ceil(currentRenderer.widthPx).toInt().coerceAtLeast(1),
193
+ ceil(currentRenderer.totalHeightPx).toInt().coerceAtLeast(1) + (glyphOverflow * 2).toInt(),
194
+ )
195
+ return
196
+ }
197
+
198
+ val fallback = fallbackText
199
+ if (fallback != null) {
200
+ fallbackPaint.textSize = fallbackFontSize
201
+ val metrics = fallbackPaint.fontMetrics
202
+ setMeasuredDimension(
203
+ ceil(fallbackPaint.measureText(fallback)).toInt().coerceAtLeast(1),
204
+ ceil(metrics.descent - metrics.ascent).toInt().coerceAtLeast(1) + (glyphOverflow * 2).toInt(),
205
+ )
206
+ return
207
+ }
208
+
209
+ setMeasuredDimension(0, 0)
161
210
  }
162
211
 
163
212
  override fun onDraw(canvas: Canvas) {
164
- val currentRenderer = renderer ?: return
165
- canvas.save()
166
- canvas.translate(0f, glyphOverflow)
167
- currentRenderer.draw(canvas)
168
- canvas.restore()
213
+ val currentRenderer = renderer
214
+ if (currentRenderer != null) {
215
+ canvas.save()
216
+ canvas.translate(0f, glyphOverflow)
217
+ currentRenderer.draw(canvas)
218
+ canvas.restore()
219
+ return
220
+ }
221
+
222
+ val fallback = fallbackText ?: return
223
+ fallbackPaint.textSize = fallbackFontSize
224
+ fallbackPaint.color = fallbackColor
225
+ canvas.drawText(fallback, 0f, glyphOverflow - fallbackPaint.fontMetrics.ascent, fallbackPaint)
169
226
  }
170
227
  }
171
228
 
@@ -5,6 +5,7 @@ import android.graphics.Bitmap
5
5
  import android.graphics.Canvas
6
6
  import android.graphics.Paint
7
7
  import android.text.style.ReplacementSpan
8
+ import com.swmansion.enriched.markdown.math.LatexErrorReporter
8
9
  import io.ratex.RaTeXEngine
9
10
  import io.ratex.RaTeXRenderer
10
11
  import kotlin.math.ceil
@@ -17,6 +18,7 @@ class MathInlineSpan(
17
18
  private val paddingVertical: Float,
18
19
  private val fontWeight: String = "normal",
19
20
  private val glyphOverflow: Float = 0f,
21
+ private val onLatexError: LatexErrorReporter? = null,
20
22
  ) : ReplacementSpan() {
21
23
  private var cachedBitmap: Bitmap? = null
22
24
  private var cachedWidth = 0
@@ -24,6 +26,8 @@ class MathInlineSpan(
24
26
  private var mathDescent = 0f
25
27
  private var renderFailed = false
26
28
 
29
+ private var fallbackText: String? = null
30
+
27
31
  private fun prepareResources() {
28
32
  if (cachedBitmap != null && !cachedBitmap!!.isRecycled) return
29
33
  if (renderFailed) return
@@ -51,12 +55,10 @@ class MathInlineSpan(
51
55
  canvas.translate(0f, overflowPx.toFloat())
52
56
  renderer.draw(canvas)
53
57
  cachedBitmap = bitmap
54
- } catch (_: Exception) {
58
+ } catch (e: Exception) {
55
59
  renderFailed = true
56
- val estimatedHeight = fontSize * 1.2f
57
- cachedWidth = (fontSize * latex.length * 0.6f).toInt().coerceAtLeast(1)
58
- mathAscent = estimatedHeight * 0.7f
59
- mathDescent = estimatedHeight * 0.3f
60
+ fallbackText = "\$" + latex + "\$"
61
+ onLatexError?.report(latex, e.message, false)
60
62
  }
61
63
  }
62
64
 
@@ -69,6 +71,19 @@ class MathInlineSpan(
69
71
  ): Int {
70
72
  prepareResources()
71
73
 
74
+ val fallback = fallbackText
75
+ if (fallback != null) {
76
+ cachedWidth = ceil(paint.measureText(fallback)).toInt().coerceAtLeast(1)
77
+ fm?.apply {
78
+ val paintFm = paint.fontMetricsInt
79
+ ascent = paintFm.ascent
80
+ top = paintFm.top
81
+ descent = paintFm.descent
82
+ bottom = paintFm.bottom
83
+ }
84
+ return cachedWidth
85
+ }
86
+
72
87
  fm?.apply {
73
88
  // Must stay identical to MathInlinePlaceholderSpan.getSize: that span measures
74
89
  // this one off the main thread, and a box that disagrees by even a pixel either
@@ -94,9 +109,20 @@ class MathInlineSpan(
94
109
  paint: Paint,
95
110
  ) {
96
111
  prepareResources()
97
- cachedBitmap?.let {
112
+
113
+ val bitmap = cachedBitmap
114
+ if (bitmap != null) {
115
+ // The bitmap carries glyphOverflow px of headroom above the TeX box (see prepareResources).
98
116
  val bitmapY = y - ceil(mathAscent) - glyphOverflow
99
- canvas.drawBitmap(it, x, bitmapY, paint)
117
+ canvas.drawBitmap(bitmap, x, bitmapY, paint)
118
+ return
119
+ }
120
+
121
+ fallbackText?.let { fallback ->
122
+ val originalColor = paint.color
123
+ paint.color = textColor
124
+ canvas.drawText(fallback, x, y.toFloat(), paint)
125
+ paint.color = originalColor
100
126
  }
101
127
  }
102
128
  }
@@ -0,0 +1,115 @@
1
+ package com.swmansion.enriched.markdown.segments
2
+
3
+ import android.content.ClipData
4
+ import android.content.ClipboardManager
5
+ import android.content.Context
6
+ import android.net.Uri
7
+ import android.widget.FrameLayout
8
+ import androidx.media3.common.MediaItem
9
+ import androidx.media3.exoplayer.ExoPlayer
10
+ import androidx.media3.ui.PlayerView
11
+ import com.swmansion.enriched.markdown.parser.MarkdownASTNode
12
+ import com.swmansion.enriched.markdown.styles.StyleConfig
13
+ import com.swmansion.enriched.markdown.views.ContextMenuPopup
14
+ import kotlin.math.roundToInt
15
+
16
+ class VideoContainerView(
17
+ context: Context,
18
+ private val styleConfig: StyleConfig,
19
+ ) : FrameLayout(context),
20
+ BlockSegmentView {
21
+ private val playerView = PlayerView(context)
22
+ private var player: ExoPlayer? = null
23
+ private var currentUrl: String? = null
24
+ var dynamicProps: DynamicBlockProps = DynamicBlockProps()
25
+
26
+ override val segmentMarginTop: Int get() = styleConfig.videoStyle.marginTop.toInt()
27
+ override val segmentMarginBottom: Int get() = styleConfig.videoStyle.marginBottom.toInt()
28
+
29
+ init {
30
+ val videoStyle = styleConfig.videoStyle
31
+ setBackgroundColor(videoStyle.backgroundColor)
32
+ clipToOutline = true
33
+ outlineProvider = RoundedOutlineProvider(videoStyle.borderRadius)
34
+
35
+ playerView.apply {
36
+ clipToOutline = true
37
+ outlineProvider = RoundedOutlineProvider(videoStyle.borderRadius)
38
+ }
39
+
40
+ addView(playerView, LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT))
41
+
42
+ isLongClickable = true
43
+ setOnLongClickListener { view -> showContextMenu(view) }
44
+
45
+ playerView.isLongClickable = true
46
+ playerView.setOnLongClickListener { showContextMenu(it) }
47
+ }
48
+
49
+ fun applyVideoNode(node: MarkdownASTNode) {
50
+ val url = node.getAttribute("url") ?: return
51
+ if (url == currentUrl) return
52
+
53
+ releasePlayer()
54
+ currentUrl = url
55
+
56
+ val exoPlayer = ExoPlayer.Builder(context).build()
57
+ exoPlayer.playWhenReady = false
58
+ exoPlayer.setMediaItem(MediaItem.fromUri(Uri.parse(url)))
59
+ exoPlayer.prepare()
60
+ player = exoPlayer
61
+ playerView.player = exoPlayer
62
+ }
63
+
64
+ override fun onMeasure(
65
+ widthMeasureSpec: Int,
66
+ heightMeasureSpec: Int,
67
+ ) {
68
+ val width = MeasureSpec.getSize(widthMeasureSpec)
69
+ val height = (width / styleConfig.videoStyle.resolvedAspectRatio).roundToInt()
70
+ super.onMeasure(
71
+ MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY),
72
+ MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY),
73
+ )
74
+ }
75
+
76
+ override fun onDetachedFromWindow() {
77
+ super.onDetachedFromWindow()
78
+ releasePlayer()
79
+ }
80
+
81
+ private fun showContextMenu(anchor: android.view.View): Boolean {
82
+ val url = currentUrl ?: return false
83
+ if (!dynamicProps.enableBlockContextMenu || url.isEmpty()) return false
84
+ ContextMenuPopup.show(anchor, this) {
85
+ item(ContextMenuPopup.Icon.COPY, dynamicProps.copyLabel) { copyToClipboard(url) }
86
+ item(ContextMenuPopup.Icon.DOCUMENT, dynamicProps.copyAsMarkdownLabel) { copyToClipboard(videoMarkdown(url)) }
87
+ }
88
+ return true
89
+ }
90
+
91
+ private fun videoMarkdown(url: String): String = if (url.contains('"')) "<video src='$url' />" else "<video src=\"$url\" />"
92
+
93
+ private fun copyToClipboard(text: String) {
94
+ val clipboard = context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
95
+ clipboard.setPrimaryClip(ClipData.newPlainText("Video", text))
96
+ }
97
+
98
+ private fun releasePlayer() {
99
+ player?.release()
100
+ player = null
101
+ playerView.player = null
102
+ currentUrl = null
103
+ }
104
+ }
105
+
106
+ private class RoundedOutlineProvider(
107
+ private val radius: Float,
108
+ ) : android.view.ViewOutlineProvider() {
109
+ override fun getOutline(
110
+ view: android.view.View,
111
+ outline: android.graphics.Outline,
112
+ ) {
113
+ outline.setRoundRect(0, 0, view.width, view.height, radius)
114
+ }
115
+ }