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,31 +5,37 @@ import android.content.res.Configuration
5
5
  import android.os.Build
6
6
  import android.os.Handler
7
7
  import android.os.Looper
8
- import android.util.AttributeSet
9
8
  import android.util.Log
10
9
  import android.view.View
11
- import android.widget.FrameLayout
12
10
  import com.facebook.react.bridge.Arguments
13
11
  import com.facebook.react.bridge.ReadableMap
14
12
  import com.facebook.react.uimanager.StateWrapper
15
13
  import com.swmansion.enriched.markdown.accessibility.AccessibilityLabels
14
+ import com.swmansion.enriched.markdown.math.LatexErrorReporter
16
15
  import com.swmansion.enriched.markdown.parser.Md4cFlags
17
16
  import com.swmansion.enriched.markdown.parser.Parser
17
+ import com.swmansion.enriched.markdown.segments.BlockquoteContainerView
18
+ import com.swmansion.enriched.markdown.segments.CodeBlockContainerView
19
+ import com.swmansion.enriched.markdown.segments.ContainerNodeView
20
+ import com.swmansion.enriched.markdown.segments.DynamicBlockProps
21
+ import com.swmansion.enriched.markdown.segments.MarkdownSegmentRenderer
22
+ import com.swmansion.enriched.markdown.segments.RenderedSegment
23
+ import com.swmansion.enriched.markdown.segments.SegmentViewConfig
24
+ import com.swmansion.enriched.markdown.segments.SegmentViewCreators
25
+ import com.swmansion.enriched.markdown.segments.SegmentViewFactory
26
+ import com.swmansion.enriched.markdown.segments.TableContainerView
27
+ import com.swmansion.enriched.markdown.segments.splitASTIntoSegments
18
28
  import com.swmansion.enriched.markdown.spoiler.SpoilerOverlay
19
29
  import com.swmansion.enriched.markdown.styles.StyleConfig
20
30
  import com.swmansion.enriched.markdown.utils.common.BreakStrategyUtils
21
31
  import com.swmansion.enriched.markdown.utils.common.CodeBlockStreamingMode
22
- import com.swmansion.enriched.markdown.utils.common.FeatureFlags
23
- import com.swmansion.enriched.markdown.utils.common.MarkdownSegmentRenderer
24
- import com.swmansion.enriched.markdown.utils.common.RenderedSegment
25
- import com.swmansion.enriched.markdown.utils.common.SegmentReconciler
26
32
  import com.swmansion.enriched.markdown.utils.common.StreamingMarkdownFilter
27
33
  import com.swmansion.enriched.markdown.utils.common.TableStreamingMode
28
34
  import com.swmansion.enriched.markdown.utils.common.emitLinkLayout
29
35
  import com.swmansion.enriched.markdown.utils.common.emitSelectionChange
30
36
  import com.swmansion.enriched.markdown.utils.common.isReducedMotionEnabled
31
- import com.swmansion.enriched.markdown.utils.common.splitASTIntoSegments
32
37
  import com.swmansion.enriched.markdown.utils.text.TailFadeInAnimator
38
+ import com.swmansion.enriched.markdown.utils.text.view.ImagePressHost
33
39
  import com.swmansion.enriched.markdown.utils.text.view.LinkLayoutCollector
34
40
  import com.swmansion.enriched.markdown.utils.text.view.LinkLayoutFragment
35
41
  import com.swmansion.enriched.markdown.utils.text.view.SelectionChangeReporter
@@ -38,584 +44,640 @@ import com.swmansion.enriched.markdown.utils.text.view.SelectionRect
38
44
  import com.swmansion.enriched.markdown.utils.text.view.SelectionSnapshot
39
45
  import com.swmansion.enriched.markdown.utils.text.view.applySelectionColors
40
46
  import com.swmansion.enriched.markdown.utils.text.view.currentTextSelection
47
+ import com.swmansion.enriched.markdown.utils.text.view.emitImagePressEvent
41
48
  import com.swmansion.enriched.markdown.views.BlockMenuOptions
42
- import com.swmansion.enriched.markdown.views.BlockSegmentView
43
- import com.swmansion.enriched.markdown.views.CodeBlockContainerView
44
- import com.swmansion.enriched.markdown.views.TableContainerView
45
49
  import java.util.EnumSet
46
50
  import java.util.concurrent.ExecutorService
47
51
  import java.util.concurrent.Executors
48
- import kotlin.math.ceil
49
- import kotlin.math.max
50
-
51
- class EnrichedMarkdown
52
- @JvmOverloads
53
- constructor(
54
- context: Context,
55
- attrs: AttributeSet? = null,
56
- defStyleAttr: Int = 0,
57
- ) : FrameLayout(context, attrs, defStyleAttr) {
58
- private enum class DirtyFlag {
59
- RECREATE_SEGMENTS,
60
- FORCE_HEIGHT,
61
- }
62
-
63
- private val parser = Parser.shared
64
- private val mainHandler = Handler(Looper.getMainLooper())
65
- private val executor: ExecutorService = Executors.newSingleThreadExecutor()
66
- private val mathContainerClass: Class<*>? by lazy {
67
- try {
68
- Class.forName("com.swmansion.enriched.markdown.views.MathContainerView")
69
- } catch (_: Exception) {
70
- null
71
- }
72
- }
73
52
 
74
- private var currentRenderId = 0L
75
- private val segmentViews = mutableListOf<View>()
76
- private val segmentSignatures = mutableListOf<Long>()
77
- private val dirtyFlags = EnumSet.noneOf(DirtyFlag::class.java)
78
- var streamingAnimation: Boolean = false
79
-
80
- // used to force a Yoga re-measure when a block image resolves its box height
81
- var stateWrapper: StateWrapper? = null
82
- private var forceHeightRecalculationCounter = 0
83
-
84
- var tableStreamingMode: TableStreamingMode = TableStreamingMode.PROGRESSIVE
85
- var codeBlockStreamingMode: CodeBlockStreamingMode = CodeBlockStreamingMode.PROGRESSIVE
86
- private var renderPending: Boolean = false
87
-
88
- var currentMarkdown: String = ""
89
- private set
90
-
91
- var markdownStyle: StyleConfig? = null
92
- private set
93
-
94
- private var markdownStyleMap: ReadableMap? = null
95
- private var lastKnownFontScale: Float = context.resources.configuration.fontScale
96
-
97
- var md4cFlags: Md4cFlags = Md4cFlags.DEFAULT
98
- private set
99
- private var allowFontScaling: Boolean = true
100
- private var maxFontSizeMultiplier: Float = 0f
101
- private var allowTrailingMargin: Boolean = false
102
- private var imageRequestHeaders: Map<String, String> = emptyMap()
103
- private var selectable: Boolean = true
104
- private var contextMenuHidden: Boolean = false
105
- private var selectionColor: Int? = null
106
- private var selectionHandleColor: Int? = null
107
- private var selectionMenuConfig = SelectionMenuConfig()
108
- private var accessibilityLabels = AccessibilityLabels()
109
- private var textBreakStrategy: String = BreakStrategyUtils.DEFAULT_STRATEGY
110
- private var linkLayoutEnabled = false
111
- private var lastLinkLayout: List<LinkLayoutFragment>? = null
112
-
113
- // At most one segment holds a selection; a new one clears the previous.
114
- private var selectionOwner: EnrichedMarkdownInternalText? = null
115
-
116
- // A block long-pressed while contextMenuHidden is on, reported as the selection.
117
- private class ActiveBlock(
118
- val view: View,
119
- val source: String,
120
- val markdown: String,
121
- val text: String,
122
- )
53
+ class EnrichedMarkdown(
54
+ context: Context,
55
+ ) : ContainerNodeView(context),
56
+ ImagePressHost {
57
+ private enum class DirtyFlag {
58
+ RECREATE_SEGMENTS,
59
+ FORCE_HEIGHT,
60
+ }
123
61
 
124
- private var activeBlock: ActiveBlock? = null
125
- private val selectionReporter =
126
- SelectionChangeReporter(this, ::selectionSnapshot) { snapshot -> emitSelectionChange(this, snapshot) }
127
-
128
- private var onLinkPressCallback: ((String) -> Unit)? = null
129
- private var onLinkLongPressCallback: ((String) -> Unit)? = null
130
- private var onTaskListItemPressCallback: ((Int, Boolean, String) -> Unit)? = null
131
- private var onCopyPressCallback: ((String, String) -> Unit)? = null
132
- private var contextMenuItemTexts: List<String> = emptyList()
133
- var onContextMenuItemPressCallback: ((itemText: String, selectedText: String, selectionStart: Int, selectionEnd: Int) -> Unit)? = null
134
- var onCopyRequestCallback: ((source: String, markdown: String, text: String, whole: Boolean) -> Unit)? = null
135
- var spoilerOverlay: SpoilerOverlay = SpoilerOverlay.PARTICLES
136
- set(value) {
137
- if (field == value) return
138
- field = value
139
- segmentViews.filterIsInstance<EnrichedMarkdownInternalText>().forEach {
140
- it.spoilerOverlay = value
141
- }
62
+ private val parser = Parser.shared
63
+ private val mainHandler = Handler(Looper.getMainLooper())
64
+ private val executor: ExecutorService = Executors.newSingleThreadExecutor()
65
+ private val mathContainerClass: Class<*>? by lazy { SegmentViewCreators.mathContainerClass() }
66
+ private val videoContainerClass: Class<*>? by lazy { SegmentViewCreators.videoContainerClass() }
67
+
68
+ private var currentRenderId = 0L
69
+ private val dirtyFlags = EnumSet.noneOf(DirtyFlag::class.java)
70
+ var streamingAnimation: Boolean = false
71
+
72
+ // used to force a Yoga re-measure when a block image resolves its box height
73
+ var stateWrapper: StateWrapper? = null
74
+ private var forceHeightRecalculationCounter = 0
75
+
76
+ var tableStreamingMode: TableStreamingMode = TableStreamingMode.PROGRESSIVE
77
+ var codeBlockStreamingMode: CodeBlockStreamingMode = CodeBlockStreamingMode.PROGRESSIVE
78
+ private var renderPending: Boolean = false
79
+
80
+ var currentMarkdown: String = ""
81
+ private set
82
+
83
+ var markdownStyle: StyleConfig? = null
84
+ private set
85
+
86
+ private var markdownStyleMap: ReadableMap? = null
87
+ private var lastKnownFontScale: Float = context.resources.configuration.fontScale
88
+
89
+ var md4cFlags: Md4cFlags = Md4cFlags.DEFAULT
90
+ private set
91
+ private var isGFM: Boolean = true
92
+ private var allowFontScaling: Boolean = true
93
+ private var maxFontSizeMultiplier: Float = 0f
94
+ private var imageRequestHeaders: Map<String, String> = emptyMap()
95
+ private var selectable: Boolean = true
96
+ private var selectionColor: Int? = null
97
+ private var selectionHandleColor: Int? = null
98
+ private var selectionMenuConfig = SelectionMenuConfig()
99
+ private var accessibilityLabels = AccessibilityLabels()
100
+ private var textBreakStrategy: String = BreakStrategyUtils.DEFAULT_STRATEGY
101
+
102
+ // --- Fork: contextMenuHidden, onSelectionChange, renderLinkDecoration (onLinkLayout). ---
103
+ // P1 of the v1.1.1 merge wires these to the root's own segments only; segments nested in
104
+ // a BlockquoteContainerView are not reached yet (P2 / A2 moves them into
105
+ // DynamicBlockProps/SegmentViewConfig and walks ContainerNodeView recursively).
106
+ private var contextMenuHidden: Boolean = false
107
+ private var linkLayoutEnabled = false
108
+ private var lastLinkLayout: List<LinkLayoutFragment>? = null
109
+
110
+ // At most one segment holds a selection; a new one clears the previous.
111
+ private var selectionOwner: EnrichedMarkdownInternalText? = null
112
+
113
+ // A block long-pressed while contextMenuHidden is on, reported as the selection.
114
+ private class ActiveBlock(
115
+ val view: View,
116
+ val source: String,
117
+ val markdown: String,
118
+ val text: String,
119
+ )
120
+
121
+ private var activeBlock: ActiveBlock? = null
122
+ private val selectionReporter =
123
+ SelectionChangeReporter(this, ::selectionSnapshot) { snapshot -> emitSelectionChange(this, snapshot) }
124
+
125
+ private var onLinkPressCallback: ((String) -> Unit)? = null
126
+ private var onLinkLongPressCallback: ((String) -> Unit)? = null
127
+ private var onTaskListItemPressCallback: ((Int, Boolean, String) -> Unit)? = null
128
+ private var onLatexErrorCallback: LatexErrorReporter? = null
129
+
130
+ private val reportedLatexErrors = HashSet<String>()
131
+
132
+ private val latexErrorReporter =
133
+ LatexErrorReporter { source, message, displayMode ->
134
+ val key = (if (displayMode) "B " else "I ") + source
135
+ if (reportedLatexErrors.add(key)) {
136
+ onLatexErrorCallback?.report(source, message, displayMode)
137
+ }
138
+ }
139
+ private var contextMenuItemTexts: List<String> = emptyList()
140
+ var onContextMenuItemPressCallback: ((itemText: String, selectedText: String, selectionStart: Int, selectionEnd: Int) -> Unit)? = null
141
+ var onCopyRequestCallback: ((source: String, markdown: String, text: String, whole: Boolean) -> Unit)? = null
142
+ var spoilerOverlay: SpoilerOverlay = SpoilerOverlay.PARTICLES
143
+ set(value) {
144
+ if (field == value) return
145
+ field = value
146
+ segmentViews.filterIsInstance<EnrichedMarkdownInternalText>().forEach {
147
+ it.spoilerOverlay = value
142
148
  }
143
- var enableTaskListItemToggle: Boolean = true
144
- set(value) {
145
- if (field == value) return
146
- field = value
147
- segmentViews.filterIsInstance<EnrichedMarkdownInternalText>().forEach {
148
- it.enableTaskListItemToggle = value
149
- }
149
+ }
150
+ var enableTaskListItemToggle: Boolean = true
151
+ set(value) {
152
+ if (field == value) return
153
+ field = value
154
+ segmentViews.filterIsInstance<EnrichedMarkdownInternalText>().forEach {
155
+ it.enableTaskListItemToggle = value
150
156
  }
151
-
152
- fun setMarkdownContent(markdown: String) {
153
- if (currentMarkdown == markdown) return
154
- currentMarkdown = markdown
155
- renderPending = true
156
157
  }
157
158
 
158
- fun setMarkdownStyle(style: ReadableMap?) {
159
- markdownStyleMap = style
160
- val newConfig = style?.let { StyleConfig(it, context, allowFontScaling, maxFontSizeMultiplier) }
161
- newConfig?.imageRequestHeaders = imageRequestHeaders
162
- if (markdownStyle == newConfig) return
163
- markdownStyle = newConfig
164
- dirtyFlags += DirtyFlag.RECREATE_SEGMENTS
165
- dirtyFlags += DirtyFlag.FORCE_HEIGHT
166
- renderPending = true
167
- }
159
+ private val dynamicProps = DynamicBlockProps()
168
160
 
169
- fun setImageRequestHeaders(headers: Map<String, String>) {
170
- if (imageRequestHeaders == headers) return
171
- imageRequestHeaders = headers
172
- markdownStyle?.imageRequestHeaders = headers
173
- dirtyFlags += DirtyFlag.RECREATE_SEGMENTS
174
- renderPending = true
161
+ var enableBlockContextMenu: Boolean
162
+ get() = dynamicProps.enableBlockContextMenu
163
+ set(value) {
164
+ dynamicProps.enableBlockContextMenu = value
175
165
  }
176
-
177
- fun commitProps() {
178
- MeasurementStore.updateStreamingTableMode(id, tableStreamingMode)
179
- MeasurementStore.updateStreamingCodeBlockMode(id, codeBlockStreamingMode)
180
- MeasurementStore.updateFontScalingSettings(id, allowFontScaling, maxFontSizeMultiplier)
181
- if (renderPending) {
182
- renderPending = false
183
- scheduleRenderIfNeeded()
184
- }
166
+ var enableCodeBlockPress: Boolean
167
+ get() = dynamicProps.enableCodeBlockPress
168
+ set(value) {
169
+ dynamicProps.enableCodeBlockPress = value
185
170
  }
186
171
 
187
- override fun onConfigurationChanged(newConfig: Configuration) {
188
- super.onConfigurationChanged(newConfig)
189
- if (!allowFontScaling) return
190
- val newFontScale = newConfig.fontScale
191
- if (newFontScale != lastKnownFontScale) {
192
- lastKnownFontScale = newFontScale
193
- recreateStyleConfig()
194
- dirtyFlags += DirtyFlag.RECREATE_SEGMENTS
195
- dirtyFlags += DirtyFlag.FORCE_HEIGHT
196
- scheduleRenderIfNeeded()
197
- }
198
- }
172
+ init {
173
+ segmentViewFactory = RootFactory()
174
+ }
199
175
 
200
- fun setMd4cFlags(flags: Md4cFlags) {
201
- if (md4cFlags == flags) return
202
- md4cFlags = flags
203
- renderPending = true
204
- }
176
+ fun setMarkdownContent(markdown: String) {
177
+ if (currentMarkdown == markdown) return
178
+ currentMarkdown = markdown
179
+ renderPending = true
180
+ }
205
181
 
206
- fun setAllowFontScaling(allow: Boolean) {
207
- if (allowFontScaling == allow) return
208
- allowFontScaling = allow
209
- MeasurementStore.updateFontScalingSettings(id, allowFontScaling, maxFontSizeMultiplier)
210
- recreateStyleConfig()
211
- dirtyFlags += DirtyFlag.RECREATE_SEGMENTS
212
- dirtyFlags += DirtyFlag.FORCE_HEIGHT
213
- renderPending = true
182
+ fun setMarkdownStyle(style: ReadableMap?) {
183
+ markdownStyleMap = style
184
+ val newConfig = style?.let { StyleConfig(it, context, allowFontScaling, maxFontSizeMultiplier) }
185
+ newConfig?.imageRequestHeaders = imageRequestHeaders
186
+ if (markdownStyle == newConfig) return
187
+ markdownStyle = newConfig
188
+ dirtyFlags += DirtyFlag.RECREATE_SEGMENTS
189
+ dirtyFlags += DirtyFlag.FORCE_HEIGHT
190
+ renderPending = true
191
+ }
192
+
193
+ fun setImageRequestHeaders(headers: Map<String, String>) {
194
+ if (imageRequestHeaders == headers) return
195
+ imageRequestHeaders = headers
196
+ markdownStyle?.imageRequestHeaders = headers
197
+ dirtyFlags += DirtyFlag.RECREATE_SEGMENTS
198
+ renderPending = true
199
+ }
200
+
201
+ fun commitProps() {
202
+ MeasurementStore.updateStreamingTableMode(id, tableStreamingMode)
203
+ MeasurementStore.updateStreamingCodeBlockMode(id, codeBlockStreamingMode)
204
+ MeasurementStore.updateFontScalingSettings(id, allowFontScaling, maxFontSizeMultiplier)
205
+ if (renderPending) {
206
+ renderPending = false
207
+ scheduleRenderIfNeeded()
214
208
  }
209
+ }
215
210
 
216
- fun setMaxFontSizeMultiplier(multiplier: Float) {
217
- if (maxFontSizeMultiplier == multiplier) return
218
- maxFontSizeMultiplier = multiplier
219
- MeasurementStore.updateFontScalingSettings(id, allowFontScaling, maxFontSizeMultiplier)
211
+ override fun onConfigurationChanged(newConfig: Configuration) {
212
+ super.onConfigurationChanged(newConfig)
213
+ if (!allowFontScaling) return
214
+ val newFontScale = newConfig.fontScale
215
+ if (newFontScale != lastKnownFontScale) {
216
+ lastKnownFontScale = newFontScale
220
217
  recreateStyleConfig()
221
218
  dirtyFlags += DirtyFlag.RECREATE_SEGMENTS
222
219
  dirtyFlags += DirtyFlag.FORCE_HEIGHT
223
- renderPending = true
220
+ scheduleRenderIfNeeded()
224
221
  }
222
+ }
225
223
 
226
- fun setAllowTrailingMargin(allow: Boolean) {
227
- if (allowTrailingMargin == allow) return
228
- allowTrailingMargin = allow
229
- dirtyFlags += DirtyFlag.RECREATE_SEGMENTS
230
- dirtyFlags += DirtyFlag.FORCE_HEIGHT
231
- renderPending = true
232
- }
224
+ fun setMd4cFlags(flags: Md4cFlags) {
225
+ if (md4cFlags == flags) return
226
+ md4cFlags = flags
227
+ renderPending = true
228
+ }
233
229
 
234
- fun setIsSelectable(value: Boolean) {
235
- if (selectable == value) return
236
- selectable = value
237
- segmentViews.filterIsInstance<EnrichedMarkdownInternalText>().forEach {
238
- it.setIsSelectable(value)
239
- }
240
- }
230
+ fun setIsGFM(value: Boolean) {
231
+ if (isGFM == value) return
232
+ isGFM = value
233
+ renderPending = true
234
+ }
241
235
 
242
- fun setContextMenuHidden(value: Boolean) {
243
- if (contextMenuHidden == value) return
244
- contextMenuHidden = value
245
- applyContextMenuHiddenToSegments()
246
- }
236
+ fun setAllowFontScaling(allow: Boolean) {
237
+ if (allowFontScaling == allow) return
238
+ allowFontScaling = allow
239
+ MeasurementStore.updateFontScalingSettings(id, allowFontScaling, maxFontSizeMultiplier)
240
+ recreateStyleConfig()
241
+ dirtyFlags += DirtyFlag.RECREATE_SEGMENTS
242
+ dirtyFlags += DirtyFlag.FORCE_HEIGHT
243
+ renderPending = true
244
+ }
247
245
 
248
- /**
249
- * Fans the flag out to every segment that owns a copy menu: text segments
250
- * (floating ActionMode toolbar), tables, code blocks and math blocks (all use
251
- * their own long-press popup, so none is covered by the ActionMode callback).
252
- */
253
- private fun applyContextMenuHiddenToSegments() {
254
- segmentViews.forEach { view ->
255
- when {
256
- view is EnrichedMarkdownInternalText -> view.contextMenuHidden = contextMenuHidden
257
- view is TableContainerView -> view.contextMenuHidden = contextMenuHidden
258
- view is CodeBlockContainerView -> view.contextMenuHidden = contextMenuHidden
259
- isMathContainerView(view) -> setMathContextMenuHidden(view)
260
- }
261
- }
262
- }
246
+ fun setMaxFontSizeMultiplier(multiplier: Float) {
247
+ if (maxFontSizeMultiplier == multiplier) return
248
+ maxFontSizeMultiplier = multiplier
249
+ MeasurementStore.updateFontScalingSettings(id, allowFontScaling, maxFontSizeMultiplier)
250
+ recreateStyleConfig()
251
+ dirtyFlags += DirtyFlag.RECREATE_SEGMENTS
252
+ dirtyFlags += DirtyFlag.FORCE_HEIGHT
253
+ renderPending = true
254
+ }
263
255
 
264
- /**
265
- * MathContainerView lives in the optional `math` source set, so it is only
266
- * reachable reflectively. The Kotlin `var` compiles to a setter taking a
267
- * primitive boolean — `Boolean::class.java` would resolve to
268
- * `java.lang.Boolean` and silently miss.
269
- */
270
- private fun setMathContextMenuHidden(view: View) {
271
- runCatching {
272
- view.javaClass
273
- .getMethod("setContextMenuHidden", Boolean::class.javaPrimitiveType)
274
- .invoke(view, contextMenuHidden)
275
- }.onFailure { Log.e(TAG, "Failed to set contextMenuHidden on math view", it) }
276
- }
256
+ fun setAllowTrailingMargin(allow: Boolean) {
257
+ if (trailingMarginEnabled == allow) return
258
+ trailingMarginEnabled = allow
259
+ dirtyFlags += DirtyFlag.RECREATE_SEGMENTS
260
+ dirtyFlags += DirtyFlag.FORCE_HEIGHT
261
+ renderPending = true
262
+ }
277
263
 
278
- fun setSelectionColor(color: Int?) {
279
- if (selectionColor == color) return
280
- selectionColor = color
281
- applySelectionColorsToSegments()
264
+ fun setIsSelectable(value: Boolean) {
265
+ if (selectable == value) return
266
+ selectable = value
267
+ segmentViews.filterIsInstance<EnrichedMarkdownInternalText>().forEach {
268
+ it.setIsSelectable(value)
282
269
  }
270
+ }
283
271
 
284
- fun setSelectionHandleColor(color: Int?) {
285
- if (selectionHandleColor == color) return
286
- selectionHandleColor = color
287
- applySelectionColorsToSegments()
288
- }
272
+ fun setContextMenuHidden(value: Boolean) {
273
+ if (contextMenuHidden == value) return
274
+ contextMenuHidden = value
275
+ applyContextMenuHiddenToSegments()
276
+ }
289
277
 
290
- fun setTextBreakStrategy(strategy: String) {
291
- if (textBreakStrategy == strategy) return
292
- textBreakStrategy = strategy
293
- MeasurementStore.updateBreakStrategy(id, strategy)
294
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
295
- val resolved = BreakStrategyUtils.resolveBreakStrategy(strategy)
296
- segmentViews.filterIsInstance<EnrichedMarkdownInternalText>().forEach {
297
- it.breakStrategy = resolved
298
- }
278
+ /**
279
+ * Fans the flag out to every segment that owns a copy menu: text segments
280
+ * (floating ActionMode toolbar), tables, code blocks and math blocks (all use
281
+ * their own long-press popup, so none is covered by the ActionMode callback).
282
+ */
283
+ private fun applyContextMenuHiddenToSegments() {
284
+ segmentViews.forEach { view ->
285
+ when {
286
+ view is EnrichedMarkdownInternalText -> view.contextMenuHidden = contextMenuHidden
287
+ view is TableContainerView -> view.contextMenuHidden = contextMenuHidden
288
+ view is CodeBlockContainerView -> view.contextMenuHidden = contextMenuHidden
289
+ isMathContainerView(view) -> setMathContextMenuHidden(view)
299
290
  }
300
- dirtyFlags += DirtyFlag.FORCE_HEIGHT
301
- renderPending = true
302
291
  }
292
+ }
303
293
 
304
- private fun applySelectionColorsToSegments() {
294
+ /**
295
+ * MathContainerView lives in the optional `math` source set, so it is only
296
+ * reachable reflectively. The Kotlin `var` compiles to a setter taking a
297
+ * primitive boolean — `Boolean::class.java` would resolve to
298
+ * `java.lang.Boolean` and silently miss.
299
+ */
300
+ private fun setMathContextMenuHidden(view: View) {
301
+ runCatching {
302
+ view.javaClass
303
+ .getMethod("setContextMenuHidden", Boolean::class.javaPrimitiveType)
304
+ .invoke(view, contextMenuHidden)
305
+ }.onFailure { Log.e(TAG, "Failed to set contextMenuHidden on math view", it) }
306
+ }
307
+
308
+ fun setSelectionColor(color: Int?) {
309
+ if (selectionColor == color) return
310
+ selectionColor = color
311
+ applySelectionColorsToSegments()
312
+ }
313
+
314
+ fun setSelectionHandleColor(color: Int?) {
315
+ if (selectionHandleColor == color) return
316
+ selectionHandleColor = color
317
+ applySelectionColorsToSegments()
318
+ }
319
+
320
+ fun setTextBreakStrategy(strategy: String) {
321
+ if (textBreakStrategy == strategy) return
322
+ textBreakStrategy = strategy
323
+ MeasurementStore.updateBreakStrategy(id, strategy)
324
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
325
+ val resolved = BreakStrategyUtils.resolveBreakStrategy(strategy)
305
326
  segmentViews.filterIsInstance<EnrichedMarkdownInternalText>().forEach {
306
- it.applySelectionColors(selectionColor, selectionHandleColor)
327
+ it.breakStrategy = resolved
307
328
  }
308
329
  }
330
+ dirtyFlags += DirtyFlag.FORCE_HEIGHT
331
+ renderPending = true
332
+ }
309
333
 
310
- fun setOnLinkPressCallback(callback: (String) -> Unit) {
311
- onLinkPressCallback = callback
334
+ private fun applySelectionColorsToSegments() {
335
+ segmentViews.filterIsInstance<EnrichedMarkdownInternalText>().forEach {
336
+ it.applySelectionColors(selectionColor, selectionHandleColor)
312
337
  }
338
+ }
313
339
 
314
- fun setOnLinkLongPressCallback(callback: (String) -> Unit) {
315
- onLinkLongPressCallback = callback
316
- }
340
+ fun setOnLinkPressCallback(callback: (String) -> Unit) {
341
+ onLinkPressCallback = callback
342
+ }
317
343
 
318
- fun setOnTaskListItemPressCallback(callback: ((taskIndex: Int, checked: Boolean, itemText: String) -> Unit)?) {
319
- onTaskListItemPressCallback = callback
320
- }
344
+ fun setOnLinkLongPressCallback(callback: (String) -> Unit) {
345
+ onLinkLongPressCallback = callback
346
+ }
321
347
 
322
- fun setOnCopyPressCallback(callback: ((code: String, language: String) -> Unit)?) {
323
- onCopyPressCallback = callback
324
- }
348
+ fun setOnLatexErrorCallback(callback: LatexErrorReporter) {
349
+ onLatexErrorCallback = callback
350
+ }
325
351
 
326
- fun setContextMenuItems(items: List<String>) {
327
- contextMenuItemTexts = items
328
- segmentViews.filterIsInstance<EnrichedMarkdownInternalText>().forEach {
329
- it.setContextMenuItems(items, ::forwardContextMenuItemPress)
330
- }
352
+ override var imagePressEnabled: Boolean = false
353
+ private set
354
+
355
+ override fun emitOnImagePress(
356
+ url: String,
357
+ altText: String,
358
+ ) {
359
+ emitImagePressEvent(url, altText)
360
+ }
361
+
362
+ fun setEnableImagePress(enabled: Boolean) {
363
+ imagePressEnabled = enabled
364
+ }
365
+
366
+ fun setOnTaskListItemPressCallback(callback: ((taskIndex: Int, checked: Boolean, itemText: String) -> Unit)?) {
367
+ onTaskListItemPressCallback = callback
368
+ }
369
+
370
+ fun setOnCopyPressCallback(callback: ((code: String, language: String) -> Unit)?) {
371
+ dynamicProps.onCopyPress = callback
372
+ }
373
+
374
+ fun setOnCodeBlockPressCallback(callback: ((code: String, language: String) -> Unit)?) {
375
+ dynamicProps.onCodeBlockPress = callback
376
+ }
377
+
378
+ fun setContextMenuItems(items: List<String>) {
379
+ contextMenuItemTexts = items
380
+ segmentViews.filterIsInstance<EnrichedMarkdownInternalText>().forEach {
381
+ it.setContextMenuItems(items, ::forwardContextMenuItemPress)
331
382
  }
383
+ }
332
384
 
333
- fun setSelectionMenuConfig(config: SelectionMenuConfig) {
334
- if (selectionMenuConfig == config) return
335
- selectionMenuConfig = config
336
- segmentViews.filterIsInstance<EnrichedMarkdownInternalText>().forEach {
337
- it.selectionMenuConfig = config
385
+ fun setSelectionMenuConfig(config: SelectionMenuConfig) {
386
+ if (selectionMenuConfig == config) return
387
+ selectionMenuConfig = config
388
+ dynamicProps.copyLabel = config.copyLabel
389
+ dynamicProps.copyAsMarkdownLabel = config.copyAsMarkdownLabel
390
+ segmentViews.filterIsInstance<EnrichedMarkdownInternalText>().forEach {
391
+ it.selectionMenuConfig = config
392
+ }
393
+ // Labels are read live from dynamicProps; the per-item menu options (and the code
394
+ // block header copy button visibility) are still pushed to each block view.
395
+ pushMenuOptionsToBlockSegments()
396
+ }
397
+
398
+ private fun pushMenuOptionsToBlockSegments() {
399
+ segmentViews.forEach { view ->
400
+ when {
401
+ view is TableContainerView -> view.menuOptions = blockMenuOptions("table")
402
+ view is CodeBlockContainerView -> view.menuOptions = blockMenuOptions("code")
403
+ isMathContainerView(view) -> setMathMenuOptions(view)
338
404
  }
339
- // Block views cache the copy labels and menu options, so re-push them on
340
- // update (e.g. a language change without a remount) to avoid stale values.
341
- pushCopyLabelsToBlockSegments()
342
405
  }
406
+ }
343
407
 
344
- private fun pushCopyLabelsToBlockSegments() {
345
- val copyLabel = selectionMenuConfig.copyLabel
346
- val copyAsMarkdownLabel = selectionMenuConfig.copyAsMarkdownLabel
347
- segmentViews.forEach { view ->
348
- when {
349
- view is TableContainerView -> {
350
- view.copyLabel = copyLabel
351
- view.copyAsMarkdownLabel = copyAsMarkdownLabel
352
- view.menuOptions = blockMenuOptions("table")
353
- }
408
+ private fun setMathMenuOptions(view: View) {
409
+ runCatching {
410
+ view.javaClass
411
+ .getMethod("setMenuOptions", BlockMenuOptions::class.java)
412
+ .invoke(view, blockMenuOptions("math"))
413
+ }.onFailure { Log.e(TAG, "Failed to set menuOptions on math view", it) }
414
+ }
354
415
 
355
- view is CodeBlockContainerView -> {
356
- view.copyLabel = copyLabel
357
- view.copyAsMarkdownLabel = copyAsMarkdownLabel
358
- view.menuOptions = blockMenuOptions("code")
359
- }
416
+ /** Wires the fork's menu hooks into a freshly created (reflective) math block view. */
417
+ private fun configureMathView(view: View) {
418
+ if (!isMathContainerView(view)) return
419
+ setMathMenuOptions(view)
420
+ setMathContextMenuHidden(view)
421
+ val onMenuSuppressed: (String, String) -> Unit = { markdown, text ->
422
+ onBlockMenuSuppressed(view, "math", markdown, text)
423
+ }
424
+ runCatching {
425
+ view.javaClass.getMethod("setOnMenuSuppressed", Function2::class.java).invoke(view, onMenuSuppressed)
426
+ }.onFailure { Log.e(TAG, "Failed to set onMenuSuppressed on math view", it) }
427
+ }
360
428
 
361
- isMathContainerView(view) -> {
362
- runCatching {
363
- view.javaClass.getMethod("setCopyLabel", String::class.java).invoke(view, copyLabel)
364
- view.javaClass
365
- .getMethod("setCopyAsMarkdownLabel", String::class.java)
366
- .invoke(view, copyAsMarkdownLabel)
367
- view.javaClass
368
- .getMethod("setMenuOptions", BlockMenuOptions::class.java)
369
- .invoke(view, blockMenuOptions("math"))
370
- }
371
- }
372
- }
373
- }
429
+ fun setAccessibilityLabels(labels: AccessibilityLabels) {
430
+ if (accessibilityLabels == labels) return
431
+ accessibilityLabels = labels
432
+ segmentViews.filterIsInstance<EnrichedMarkdownInternalText>().forEach {
433
+ it.accessibilityLabels = labels
374
434
  }
375
-
376
- fun setAccessibilityLabels(labels: AccessibilityLabels) {
377
- if (accessibilityLabels == labels) return
378
- accessibilityLabels = labels
379
- segmentViews.filterIsInstance<EnrichedMarkdownInternalText>().forEach {
380
- it.accessibilityLabels = labels
381
- }
382
- segmentViews.filterIsInstance<TableContainerView>().forEach {
383
- it.accessibilityLabels = labels
384
- }
385
- mathContainerClass?.let { mathClass ->
386
- segmentViews.filter { mathClass.isInstance(it) }.forEach { view ->
387
- runCatching {
388
- mathClass
389
- .getMethod("setAccessibilityLabels", AccessibilityLabels::class.java)
390
- .invoke(view, labels)
391
- }
435
+ segmentViews.filterIsInstance<TableContainerView>().forEach {
436
+ it.accessibilityLabels = labels
437
+ }
438
+ mathContainerClass?.let { mathClass ->
439
+ segmentViews.filter { mathClass.isInstance(it) }.forEach { view ->
440
+ runCatching {
441
+ mathClass
442
+ .getMethod("setAccessibilityLabels", AccessibilityLabels::class.java)
443
+ .invoke(view, labels)
392
444
  }
393
445
  }
394
446
  }
447
+ }
395
448
 
396
- private fun blockMenuOptions(source: String) =
397
- BlockMenuOptions.from(selectionMenuConfig) { markdown, text ->
398
- onCopyRequestCallback?.invoke(source, markdown, text, true)
399
- }
449
+ private fun forwardContextMenuItemPress(
450
+ itemText: String,
451
+ selectedText: String,
452
+ selectionStart: Int,
453
+ selectionEnd: Int,
454
+ ) {
455
+ onContextMenuItemPressCallback?.invoke(itemText, selectedText, selectionStart, selectionEnd)
456
+ }
400
457
 
401
- // Segments only see their own text, so a selection is the whole component
402
- // only when it spans the sole segment; the markdown is then the original.
403
- private fun forwardSegmentCopyRequest(
404
- markdown: String,
405
- text: String,
406
- fullSelection: Boolean,
407
- ) {
408
- val whole = fullSelection && segmentViews.size == 1 && currentMarkdown.isNotEmpty()
409
- onCopyRequestCallback?.invoke("selection", if (whole) currentMarkdown else markdown, text, whole)
458
+ private fun blockMenuOptions(source: String) =
459
+ BlockMenuOptions.from(selectionMenuConfig) { markdown, text ->
460
+ onCopyRequestCallback?.invoke(source, markdown, text, true)
410
461
  }
411
462
 
412
- private fun forwardContextMenuItemPress(
413
- itemText: String,
414
- selectedText: String,
415
- selectionStart: Int,
416
- selectionEnd: Int,
417
- ) {
418
- onContextMenuItemPressCallback?.invoke(itemText, selectedText, selectionStart, selectionEnd)
419
- }
463
+ // Segments only see their own text, so a selection is the whole component
464
+ // only when it spans the sole segment; the markdown is then the original.
465
+ private fun forwardSegmentCopyRequest(
466
+ markdown: String,
467
+ text: String,
468
+ fullSelection: Boolean,
469
+ ) {
470
+ val whole = fullSelection && segmentViews.size == 1 && currentMarkdown.isNotEmpty()
471
+ onCopyRequestCallback?.invoke("selection", if (whole) currentMarkdown else markdown, text, whole)
472
+ }
420
473
 
421
- fun setEmitSelectionChange(enabled: Boolean) {
422
- selectionReporter.enabled = enabled
423
- }
474
+ fun setEmitSelectionChange(enabled: Boolean) {
475
+ selectionReporter.enabled = enabled
476
+ }
477
+
478
+ /** Clears the selection of every segment (and any active block) and reports `active: false`. */
479
+ fun clearSelection() {
480
+ val owner = selectionOwner
481
+ selectionOwner = null
482
+ activeBlock = null
483
+ owner?.clearSelection()
484
+ selectionReporter.flushNow()
485
+ }
424
486
 
425
- /** Clears the selection of every segment (and any active block) and reports `active: false`. */
426
- fun clearSelection() {
427
- val owner = selectionOwner
428
- selectionOwner = null
487
+ private fun onSegmentSelectionChanged(
488
+ view: EnrichedMarkdownInternalText,
489
+ hasSelection: Boolean,
490
+ ) {
491
+ if (hasSelection) {
492
+ // Take ownership first so the previous owner's own "cleared" callback is ignored.
493
+ val previous = selectionOwner
494
+ selectionOwner = view
429
495
  activeBlock = null
430
- owner?.clearSelection()
431
- selectionReporter.flushNow()
496
+ if (previous !== view) previous?.clearSelection()
497
+ selectionReporter.schedule()
498
+ } else if (selectionOwner === view) {
499
+ selectionReporter.onSelectionChanged(false)
432
500
  }
501
+ }
433
502
 
434
- private fun onSegmentSelectionChanged(
435
- view: EnrichedMarkdownInternalText,
436
- hasSelection: Boolean,
437
- ) {
438
- if (hasSelection) {
439
- // Take ownership first so the previous owner's own "cleared" callback is ignored.
440
- val previous = selectionOwner
441
- selectionOwner = view
442
- activeBlock = null
443
- if (previous !== view) previous?.clearSelection()
444
- selectionReporter.schedule()
445
- } else if (selectionOwner === view) {
446
- selectionReporter.onSelectionChanged(false)
447
- }
448
- }
503
+ private fun onBlockMenuSuppressed(
504
+ view: View,
505
+ source: String,
506
+ markdown: String,
507
+ text: String,
508
+ ) {
509
+ if (!selectionReporter.enabled) return
510
+ val previous = selectionOwner
511
+ selectionOwner = null
512
+ previous?.clearSelection()
513
+ activeBlock = ActiveBlock(view, source, markdown, text)
514
+ selectionReporter.forgetLastReported()
515
+ selectionReporter.flushNow()
516
+ }
449
517
 
450
- private fun onBlockMenuSuppressed(
451
- view: View,
452
- source: String,
453
- markdown: String,
454
- text: String,
455
- ) {
456
- if (!selectionReporter.enabled) return
457
- val previous = selectionOwner
458
- selectionOwner = null
459
- previous?.clearSelection()
460
- activeBlock = ActiveBlock(view, source, markdown, text)
461
- selectionReporter.forgetLastReported()
462
- selectionReporter.flushNow()
463
- }
518
+ private fun selectionSnapshot(): SelectionSnapshot {
519
+ activeBlock?.let { block ->
520
+ if (block.view.parent !== this) return SelectionSnapshot.INACTIVE
521
+ val rect =
522
+ SelectionRect.fromPx(
523
+ block.view.left.toFloat(),
524
+ block.view.top.toFloat(),
525
+ block.view.right.toFloat(),
526
+ block.view.bottom.toFloat(),
527
+ )
528
+ return SelectionSnapshot.block(block.source, block.markdown, block.text, rect)
529
+ }
530
+ val owner = selectionOwner?.takeIf { it.parent === this } ?: return SelectionSnapshot.INACTIVE
531
+ val selection = owner.currentTextSelection(owner.left, owner.top) ?: return SelectionSnapshot.INACTIVE
532
+ // Same rule as forwardSegmentCopyRequest.
533
+ val whole = selection.fullSelection && segmentViews.size == 1 && currentMarkdown.isNotEmpty()
534
+ return SelectionSnapshot(
535
+ active = true,
536
+ source = "selection",
537
+ text = selection.text,
538
+ markdown = if (whole) currentMarkdown else selection.markdown,
539
+ whole = whole,
540
+ rect = selection.rect,
541
+ startRect = selection.startRect,
542
+ endRect = selection.endRect,
543
+ )
544
+ }
464
545
 
465
- private fun selectionSnapshot(): SelectionSnapshot {
466
- activeBlock?.let { block ->
467
- if (block.view.parent !== this) return SelectionSnapshot.INACTIVE
468
- val rect =
469
- SelectionRect.fromPx(
470
- block.view.left.toFloat(),
471
- block.view.top.toFloat(),
472
- block.view.right.toFloat(),
473
- block.view.bottom.toFloat(),
474
- )
475
- return SelectionSnapshot.block(block.source, block.markdown, block.text, rect)
476
- }
477
- val owner = selectionOwner?.takeIf { it.parent === this } ?: return SelectionSnapshot.INACTIVE
478
- val selection = owner.currentTextSelection(owner.left, owner.top) ?: return SelectionSnapshot.INACTIVE
479
- // Same rule as forwardSegmentCopyRequest.
480
- val whole = selection.fullSelection && segmentViews.size == 1 && currentMarkdown.isNotEmpty()
481
- return SelectionSnapshot(
482
- active = true,
483
- source = "selection",
484
- text = selection.text,
485
- markdown = if (whole) currentMarkdown else selection.markdown,
486
- whole = whole,
487
- rect = selection.rect,
488
- startRect = selection.startRect,
489
- endRect = selection.endRect,
490
- )
546
+ private fun recreateStyleConfig() {
547
+ markdownStyleMap?.let {
548
+ markdownStyle =
549
+ StyleConfig(it, context, allowFontScaling, maxFontSizeMultiplier).also { config ->
550
+ config.imageRequestHeaders = imageRequestHeaders
551
+ }
491
552
  }
553
+ }
554
+
555
+ private fun scheduleRenderIfNeeded() {
556
+ if (currentMarkdown.isNotEmpty()) scheduleRender()
557
+ }
558
+
559
+ private fun scheduleRender() {
560
+ val style = markdownStyle ?: return
561
+ val markdown = currentMarkdown.takeIf { it.isNotEmpty() } ?: return
562
+ val isStreaming = streamingAnimation
563
+ val tableMode = tableStreamingMode
564
+ val codeBlockMode = codeBlockStreamingMode
492
565
 
493
- private fun recreateStyleConfig() {
494
- markdownStyleMap?.let {
495
- markdownStyle =
496
- StyleConfig(it, context, allowFontScaling, maxFontSizeMultiplier).also { config ->
497
- config.imageRequestHeaders = imageRequestHeaders
566
+ val renderId = ++currentRenderId
567
+
568
+ executor.execute {
569
+ try {
570
+ val filtered =
571
+ if (isStreaming) {
572
+ StreamingMarkdownFilter.renderableMarkdownForStreaming(markdown, tableMode, codeBlockMode)
573
+ } else {
574
+ null
498
575
  }
499
- }
500
- }
576
+ val renderableMarkdown = filtered?.markdown ?: markdown
577
+ val hasPendingCodeBlock = filtered?.endsInsideOpenCodeFence ?: false
501
578
 
502
- private fun scheduleRenderIfNeeded() {
503
- if (currentMarkdown.isNotEmpty()) scheduleRender()
504
- }
579
+ if (renderableMarkdown.isEmpty()) {
580
+ postToMain(renderId) { applyRenderedSegments(emptyList(), false) }
581
+ return@execute
582
+ }
505
583
 
506
- private fun scheduleRender() {
507
- val style = markdownStyle ?: return
508
- val markdown = currentMarkdown.takeIf { it.isNotEmpty() } ?: return
509
- val isStreaming = streamingAnimation
510
- val tableMode = tableStreamingMode
511
- val codeBlockMode = codeBlockStreamingMode
512
-
513
- val renderId = ++currentRenderId
514
-
515
- executor.execute {
516
- try {
517
- val filtered =
518
- if (isStreaming) {
519
- StreamingMarkdownFilter.renderableMarkdownForStreaming(markdown, tableMode, codeBlockMode)
520
- } else {
521
- null
522
- }
523
- val renderableMarkdown = filtered?.markdown ?: markdown
524
- val hasPendingCodeBlock = filtered?.endsInsideOpenCodeFence ?: false
525
-
526
- if (renderableMarkdown.isEmpty()) {
527
- postToMain(renderId) { applyRenderedSegments(emptyList(), style, false) }
584
+ val ast =
585
+ parser.parseMarkdown(renderableMarkdown, md4cFlags, isGFM) ?: run {
586
+ postToMain(renderId) { applyRenderedSegments(emptyList(), false) }
528
587
  return@execute
529
588
  }
530
589
 
531
- val ast =
532
- parser.parseMarkdown(renderableMarkdown, md4cFlags) ?: run {
533
- postToMain(renderId) { applyRenderedSegments(emptyList(), style, false) }
534
- return@execute
535
- }
536
-
537
- val segments = splitASTIntoSegments(ast)
538
- val renderedSegments =
539
- MarkdownSegmentRenderer.render(
540
- segments,
541
- style,
542
- context,
543
- onLinkPressCallback,
544
- onLinkLongPressCallback,
545
- )
546
-
547
- postToMain(renderId) { applyRenderedSegments(renderedSegments, style, hasPendingCodeBlock) }
548
- } catch (e: Exception) {
549
- Log.e(TAG, "Render failed", e)
550
- postToMain(renderId) { applyRenderedSegments(emptyList(), style, false) }
551
- }
590
+ val segments = splitASTIntoSegments(ast)
591
+ val renderedSegments =
592
+ MarkdownSegmentRenderer.render(
593
+ segments,
594
+ style,
595
+ context,
596
+ onLinkPressCallback,
597
+ onLinkLongPressCallback,
598
+ onLatexError = latexErrorReporter,
599
+ )
600
+
601
+ postToMain(renderId) { applyRenderedSegments(renderedSegments, hasPendingCodeBlock) }
602
+ } catch (e: Exception) {
603
+ Log.e(TAG, "Render failed", e)
604
+ postToMain(renderId) { applyRenderedSegments(emptyList(), false) }
552
605
  }
553
606
  }
607
+ }
554
608
 
555
- // Trailing code block whose closing fence hasn't streamed in yet, if any.
556
- private var pendingCodeBlockSegment: RenderedSegment.CodeBlock? = null
609
+ // Trailing code block whose closing fence hasn't streamed in yet, if any.
610
+ private var pendingCodeBlockSegment: RenderedSegment.CodeBlock? = null
557
611
 
558
- private fun applyRenderedSegments(
559
- renderedSegments: List<RenderedSegment>,
560
- style: StyleConfig,
561
- hasPendingCodeBlock: Boolean,
562
- ) {
563
- pendingCodeBlockSegment =
564
- if (hasPendingCodeBlock) renderedSegments.lastOrNull() as? RenderedSegment.CodeBlock else null
565
-
566
- val reset = DirtyFlag.RECREATE_SEGMENTS in dirtyFlags
567
- val forceHeight = DirtyFlag.FORCE_HEIGHT in dirtyFlags
568
- dirtyFlags.clear()
569
-
570
- val result =
571
- SegmentReconciler.reconcile(
572
- currentViews = segmentViews.toList(),
573
- currentSignatures = segmentSignatures.toList(),
574
- renderedSegments = renderedSegments,
575
- reset = reset,
576
- matchesKind = ::viewMatchesSegmentKind,
577
- createView = { segment ->
578
- val view = createSegmentView(segment, style)
579
- animateNewView(view, segment)
580
- view
581
- },
582
- updateView = { view, segment -> updateSegmentView(view, segment) },
583
- )
612
+ private fun applyRenderedSegments(
613
+ renderedSegments: List<RenderedSegment>,
614
+ hasPendingCodeBlock: Boolean,
615
+ ) {
616
+ pendingCodeBlockSegment =
617
+ if (hasPendingCodeBlock) renderedSegments.lastOrNull() as? RenderedSegment.CodeBlock else null
584
618
 
585
- result.viewsToRemove.forEach { removeView(it) }
586
- result.viewsToAttach.forEach { addView(it) }
619
+ val reset = DirtyFlag.RECREATE_SEGMENTS in dirtyFlags
620
+ val forceHeight = DirtyFlag.FORCE_HEIGHT in dirtyFlags
621
+ dirtyFlags.clear()
587
622
 
588
- segmentViews.clear()
589
- segmentViews.addAll(result.views)
590
- segmentSignatures.clear()
591
- segmentSignatures.addAll(result.signatures)
623
+ val topologyChanged = applySegments(renderedSegments, reset)
592
624
 
593
- if (selectionOwner?.let { owner -> segmentViews.none { it === owner } } == true) selectionOwner = null
594
- if (activeBlock?.let { block -> segmentViews.none { it === block.view } } == true) activeBlock = null
625
+ if (selectionOwner?.let { owner -> segmentViews.none { it === owner } } == true) selectionOwner = null
626
+ if (activeBlock?.let { block -> segmentViews.none { it === block.view } } == true) activeBlock = null
595
627
 
596
- // A just-closed block has unchanged content, so the reconciler reuses it
597
- // without an update; sync pending here to trigger its deferred highlight.
598
- segmentViews.forEachIndexed { index, view ->
599
- if (view is CodeBlockContainerView) {
600
- view.pending = pendingCodeBlockSegment != null && index == segmentViews.size - 1
601
- }
628
+ // A just-closed block has unchanged content, so the reconciler reuses it
629
+ // without an update; sync pending here to trigger its deferred highlight.
630
+ segmentViews.forEachIndexed { index, view ->
631
+ if (view is CodeBlockContainerView) {
632
+ view.pending = pendingCodeBlockSegment != null && index == segmentViews.size - 1
602
633
  }
634
+ }
603
635
 
604
- val topologyChanged = result.viewsToAttach.isNotEmpty() || result.viewsToRemove.isNotEmpty()
605
-
606
- if (width > 0) {
607
- val heightBefore = computeSegmentsTotalHeight()
608
- layoutSegments()
609
- val heightAfter = computeSegmentsTotalHeight()
636
+ if (width > 0) {
637
+ val heightBefore = computeSegmentsTotalHeight()
638
+ layoutSegmentsAndReport()
639
+ val heightAfter = computeSegmentsTotalHeight()
610
640
 
611
- if (forceHeight || topologyChanged || heightBefore != heightAfter) {
612
- MeasurementStore.invalidate(id)
613
- requestLayout()
614
- }
641
+ if (forceHeight || topologyChanged || heightBefore != heightAfter) {
642
+ MeasurementStore.invalidate(id)
643
+ requestLayout()
615
644
  }
616
645
  }
646
+ }
617
647
 
618
- private fun viewMatchesSegmentKind(
648
+ private fun isMathContainerView(view: View): Boolean = mathContainerClass?.isInstance(view) == true
649
+
650
+ private fun isVideoContainerView(view: View): Boolean = videoContainerClass?.isInstance(view) == true
651
+
652
+ private fun segmentViewConfig(): SegmentViewConfig =
653
+ SegmentViewConfig(
654
+ context = context,
655
+ style = markdownStyle!!,
656
+ allowFontScaling = allowFontScaling,
657
+ maxFontSizeMultiplier = maxFontSizeMultiplier,
658
+ accessibilityLabels = accessibilityLabels,
659
+ selectionMenuConfig = selectionMenuConfig,
660
+ textBreakStrategy = textBreakStrategy,
661
+ selectable = selectable,
662
+ selectionColor = selectionColor,
663
+ selectionHandleColor = selectionHandleColor,
664
+ contextMenuItemTexts = contextMenuItemTexts,
665
+ dynamicProps = dynamicProps,
666
+ onLinkPress = onLinkPressCallback,
667
+ onLinkLongPress = onLinkLongPressCallback,
668
+ onTaskListItemPress = onTaskListItemPressCallback,
669
+ onContextMenuItemPress = ::forwardContextMenuItemPress,
670
+ onLatexError = latexErrorReporter,
671
+ )
672
+
673
+ /**
674
+ * Root factory: builds child views through the shared creators, then layers on
675
+ * root-only concerns (spoiler overlay mode, task-list toggle, streaming tail /
676
+ * fade-in animation, and the pending code block sync for the open trailing
677
+ * fence).
678
+ */
679
+ private inner class RootFactory : SegmentViewFactory {
680
+ override fun matchesKind(
619
681
  view: View,
620
682
  segment: RenderedSegment,
621
683
  ): Boolean =
@@ -624,22 +686,55 @@ class EnrichedMarkdown
624
686
  is RenderedSegment.Table -> view is TableContainerView
625
687
  is RenderedSegment.Math -> isMathContainerView(view)
626
688
  is RenderedSegment.CodeBlock -> view is CodeBlockContainerView
689
+ is RenderedSegment.Blockquote -> view is BlockquoteContainerView
690
+ is RenderedSegment.Video -> isVideoContainerView(view)
627
691
  }
628
692
 
629
- private fun isMathContainerView(view: View): Boolean = mathContainerClass?.isInstance(view) == true
693
+ override fun createView(segment: RenderedSegment): View {
694
+ val config = segmentViewConfig()
695
+ return when (segment) {
696
+ is RenderedSegment.Text -> {
697
+ SegmentViewCreators.createTextView(segment, config).apply {
698
+ spoilerOverlay = this@EnrichedMarkdown.spoilerOverlay
699
+ enableTaskListItemToggle = this@EnrichedMarkdown.enableTaskListItemToggle
700
+ contextMenuHidden = this@EnrichedMarkdown.contextMenuHidden
701
+ onCopyRequest = this@EnrichedMarkdown::forwardSegmentCopyRequest
702
+ onSelectionChangedCallback = this@EnrichedMarkdown::onSegmentSelectionChanged
703
+ }
704
+ }
705
+
706
+ is RenderedSegment.Table -> {
707
+ SegmentViewCreators.createTableView(segment, config).apply {
708
+ contextMenuHidden = this@EnrichedMarkdown.contextMenuHidden
709
+ menuOptions = blockMenuOptions("table")
710
+ onMenuSuppressed = { markdown, text -> onBlockMenuSuppressed(this, "table", markdown, text) }
711
+ }
712
+ }
630
713
 
631
- private fun createSegmentView(
632
- segment: RenderedSegment,
633
- style: StyleConfig,
634
- ): View =
635
- when (segment) {
636
- is RenderedSegment.Text -> createTextView(segment)
637
- is RenderedSegment.Table -> createTableView(segment, style)
638
- is RenderedSegment.Math -> createMathView(segment, style)
639
- is RenderedSegment.CodeBlock -> createCodeBlockView(segment, style)
714
+ is RenderedSegment.Math -> {
715
+ SegmentViewCreators.createMathView(segment, config).also(this@EnrichedMarkdown::configureMathView)
716
+ }
717
+
718
+ is RenderedSegment.CodeBlock -> {
719
+ SegmentViewCreators.createCodeBlockView(segment, config).apply {
720
+ pending = segment === pendingCodeBlockSegment
721
+ contextMenuHidden = this@EnrichedMarkdown.contextMenuHidden
722
+ menuOptions = blockMenuOptions("code")
723
+ onMenuSuppressed = { markdown, text -> onBlockMenuSuppressed(this, "code", markdown, text) }
724
+ }
725
+ }
726
+
727
+ is RenderedSegment.Blockquote -> {
728
+ SegmentViewCreators.createBlockquoteView(segment, config)
729
+ }
730
+
731
+ is RenderedSegment.Video -> {
732
+ SegmentViewCreators.createVideoView(segment, config)
733
+ }
640
734
  }
735
+ }
641
736
 
642
- private fun updateSegmentView(
737
+ override fun updateView(
643
738
  view: View,
644
739
  segment: RenderedSegment,
645
740
  ) {
@@ -647,9 +742,7 @@ class EnrichedMarkdown
647
742
  is RenderedSegment.Text -> {
648
743
  val textView = view as EnrichedMarkdownInternalText
649
744
  val tailStart = textView.text?.length ?: 0
650
- textView.lastElementMarginBottom = segment.lastElementMarginBottom
651
- textView.applyStyledText(segment.styledText)
652
- segment.imageSpans.forEach { it.registerTextView(textView) }
745
+ SegmentViewCreators.updateTextView(textView, segment)
653
746
  animateTextViewTail(textView, tailStart)
654
747
  }
655
748
 
@@ -663,9 +756,7 @@ class EnrichedMarkdown
663
756
  }
664
757
 
665
758
  is RenderedSegment.Math -> {
666
- mathContainerClass
667
- ?.getMethod("applyLatex", String::class.java)
668
- ?.invoke(view, segment.latex)
759
+ SegmentViewCreators.updateMathView(view, segment)
669
760
  }
670
761
 
671
762
  is RenderedSegment.CodeBlock -> {
@@ -674,281 +765,140 @@ class EnrichedMarkdown
674
765
  applyCodeBlockNode(segment.node)
675
766
  }
676
767
  }
768
+
769
+ is RenderedSegment.Blockquote -> {
770
+ (view as BlockquoteContainerView).applyBlockquoteNode(segment.node)
771
+ }
772
+
773
+ is RenderedSegment.Video -> {
774
+ SegmentViewCreators.updateVideoView(view, segment)
775
+ }
677
776
  }
678
777
  }
679
778
 
680
- private fun animateNewView(
779
+ override fun animateNewView(
681
780
  view: View,
682
781
  segment: RenderedSegment,
683
782
  ) {
684
783
  if (!streamingAnimation) return
685
784
  when (segment) {
686
785
  is RenderedSegment.Text -> animateTextViewTail(view as EnrichedMarkdownInternalText, 0)
687
- is RenderedSegment.Table, is RenderedSegment.Math, is RenderedSegment.CodeBlock -> animateBlockViewFadeIn(view)
688
- }
689
- }
690
-
691
- private fun animateTextViewTail(
692
- view: EnrichedMarkdownInternalText,
693
- tailStart: Int,
694
- ) {
695
- if (!streamingAnimation) return
696
- val textLength = view.text?.length ?: 0
697
- if (textLength <= tailStart) return
698
- TailFadeInAnimator(view).animate(tailStart, textLength)
699
- }
700
-
701
- private fun animateBlockViewFadeIn(view: View) {
702
- if (!streamingAnimation) return
703
- if (isReducedMotionEnabled(context)) return
704
- view.alpha = 0f
705
- view
706
- .animate()
707
- .alpha(1f)
708
- .setDuration(BLOCK_FADE_DURATION_MS)
709
- .start()
710
- }
711
-
712
- private fun createTextView(segment: RenderedSegment.Text) =
713
- EnrichedMarkdownInternalText(context).apply {
714
- spoilerOverlay = this@EnrichedMarkdown.spoilerOverlay
715
- enableTaskListItemToggle = this@EnrichedMarkdown.enableTaskListItemToggle
716
- selectionMenuConfig = this@EnrichedMarkdown.selectionMenuConfig
717
- contextMenuHidden = this@EnrichedMarkdown.contextMenuHidden
718
- accessibilityLabels = this@EnrichedMarkdown.accessibilityLabels
719
- setIsSelectable(selectable)
720
- markdownStyle?.paragraphStyle?.fontSize?.let { setTextSize(android.util.TypedValue.COMPLEX_UNIT_PX, it) }
721
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
722
- breakStrategy = BreakStrategyUtils.resolveBreakStrategy(textBreakStrategy)
723
- }
724
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && segment.needsJustify) {
725
- justificationMode = android.text.Layout.JUSTIFICATION_MODE_INTER_WORD
726
- }
727
- lastElementMarginBottom = segment.lastElementMarginBottom
728
- applyStyledText(segment.styledText)
729
- segment.imageSpans.forEach { it.registerTextView(this) }
730
-
731
- onTaskListItemPressCallback = { taskIndex, checked, itemText ->
732
- this@EnrichedMarkdown.onTaskListItemPressCallback?.invoke(taskIndex, checked, itemText)
733
- }
734
- onCopyRequest = ::forwardSegmentCopyRequest
735
- onSelectionChangedCallback = ::onSegmentSelectionChanged
736
-
737
- if (contextMenuItemTexts.isNotEmpty()) {
738
- setContextMenuItems(contextMenuItemTexts, ::forwardContextMenuItemPress)
739
- }
740
-
741
- applySelectionColors(selectionColor, selectionHandleColor)
742
- }
743
-
744
- private fun createTableView(
745
- segment: RenderedSegment.Table,
746
- style: StyleConfig,
747
- ) = TableContainerView(context, style).apply {
748
- allowFontScaling = this@EnrichedMarkdown.allowFontScaling
749
- maxFontSizeMultiplier = this@EnrichedMarkdown.maxFontSizeMultiplier
750
- accessibilityLabels = this@EnrichedMarkdown.accessibilityLabels
751
- onLinkPress = onLinkPressCallback
752
- onLinkLongPress = onLinkLongPressCallback
753
- copyLabel = this@EnrichedMarkdown.selectionMenuConfig.copyLabel
754
- copyAsMarkdownLabel = this@EnrichedMarkdown.selectionMenuConfig.copyAsMarkdownLabel
755
- contextMenuHidden = this@EnrichedMarkdown.contextMenuHidden
756
- menuOptions = blockMenuOptions("table")
757
- onMenuSuppressed = { markdown, text -> onBlockMenuSuppressed(this, "table", markdown, text) }
758
- applyTableNode(segment.node)
759
- }
760
786
 
761
- private fun createCodeBlockView(
762
- segment: RenderedSegment.CodeBlock,
763
- style: StyleConfig,
764
- ) = CodeBlockContainerView(context, style).apply {
765
- copyLabel = this@EnrichedMarkdown.selectionMenuConfig.copyLabel
766
- copyAsMarkdownLabel = this@EnrichedMarkdown.selectionMenuConfig.copyAsMarkdownLabel
767
- contextMenuHidden = this@EnrichedMarkdown.contextMenuHidden
768
- menuOptions = blockMenuOptions("code")
769
- onMenuSuppressed = { markdown, text -> onBlockMenuSuppressed(this, "code", markdown, text) }
770
- onCopyPress = { code, language ->
771
- this@EnrichedMarkdown.onCopyPressCallback?.invoke(code, language)
772
- }
773
- pending = segment === pendingCodeBlockSegment
774
- applyCodeBlockNode(segment.node)
775
- }
776
-
777
- private fun createMathView(
778
- segment: RenderedSegment.Math,
779
- style: StyleConfig,
780
- ): View {
781
- val resolvedClass = mathContainerClass
782
- if (!FeatureFlags.IS_MATH_ENABLED || resolvedClass == null) return View(context)
783
- return try {
784
- val view =
785
- resolvedClass
786
- .getConstructor(Context::class.java, StyleConfig::class.java)
787
- .newInstance(context, style) as View
788
- runCatching {
789
- resolvedClass
790
- .getMethod("setAccessibilityLabels", AccessibilityLabels::class.java)
791
- .invoke(view, accessibilityLabels)
792
- }
793
- resolvedClass
794
- .getMethod("setCopyLabel", String::class.java)
795
- .invoke(view, selectionMenuConfig.copyLabel)
796
- resolvedClass
797
- .getMethod("setCopyAsMarkdownLabel", String::class.java)
798
- .invoke(view, selectionMenuConfig.copyAsMarkdownLabel)
799
- resolvedClass
800
- .getMethod("setMenuOptions", BlockMenuOptions::class.java)
801
- .invoke(view, blockMenuOptions("math"))
802
- setMathContextMenuHidden(view)
803
- val onMenuSuppressed: (String, String) -> Unit = { markdown, text ->
804
- onBlockMenuSuppressed(view, "math", markdown, text)
805
- }
806
- runCatching {
807
- resolvedClass.getMethod("setOnMenuSuppressed", Function2::class.java).invoke(view, onMenuSuppressed)
808
- }.onFailure { Log.e(TAG, "Failed to set onMenuSuppressed on math view", it) }
809
- resolvedClass.getMethod("applyLatex", String::class.java).invoke(view, segment.latex)
810
- view
811
- } catch (e: Exception) {
812
- Log.e(TAG, "Failed to create math view", e)
813
- View(context)
787
+ is RenderedSegment.Table,
788
+ is RenderedSegment.Math,
789
+ is RenderedSegment.CodeBlock,
790
+ is RenderedSegment.Blockquote,
791
+ is RenderedSegment.Video,
792
+ -> animateBlockViewFadeIn(view)
814
793
  }
815
794
  }
795
+ }
816
796
 
817
- private fun postToMain(
818
- renderId: Long,
819
- action: () -> Unit,
820
- ) {
821
- mainHandler.post {
822
- if (renderId == currentRenderId) action()
823
- }
824
- }
797
+ private fun animateTextViewTail(
798
+ view: EnrichedMarkdownInternalText,
799
+ tailStart: Int,
800
+ ) {
801
+ if (!streamingAnimation) return
802
+ val textLength = view.text?.length ?: 0
803
+ if (textLength <= tailStart) return
804
+ TailFadeInAnimator(view).animate(tailStart, textLength)
805
+ }
825
806
 
826
- override fun onLayout(
827
- changed: Boolean,
828
- l: Int,
829
- t: Int,
830
- r: Int,
831
- b: Int,
832
- ) {
833
- layoutSegments()
834
- }
807
+ private fun animateBlockViewFadeIn(view: View) {
808
+ if (!streamingAnimation) return
809
+ if (isReducedMotionEnabled(context)) return
810
+ view.alpha = 0f
811
+ view
812
+ .animate()
813
+ .alpha(1f)
814
+ .setDuration(BLOCK_FADE_DURATION_MS)
815
+ .start()
816
+ }
835
817
 
836
- fun onImageLayoutChanged() {
837
- if (width > 0) {
838
- layoutSegments()
839
- requestLayout()
840
- }
841
- MeasurementStore.invalidate(id)
842
- val wrapper = stateWrapper ?: return
843
- val state = Arguments.createMap()
844
- state.putInt("forceHeightRecalculationCounter", ++forceHeightRecalculationCounter)
845
- wrapper.updateState(state)
818
+ private fun postToMain(
819
+ renderId: Long,
820
+ action: () -> Unit,
821
+ ) {
822
+ mainHandler.post {
823
+ if (renderId == currentRenderId) action()
846
824
  }
825
+ }
847
826
 
848
- private fun layoutSegments() {
849
- val containerWidth = width
850
- if (containerWidth <= 0) return
851
-
852
- val needsOverhang =
853
- segmentViews.any { view ->
854
- view is TableContainerView &&
855
- ceil(view.tableStyle.horizontalOverflow.toDouble()).toInt() > 0
856
- }
857
- if (clipChildren == needsOverhang) {
858
- clipChildren = !needsOverhang
859
- }
860
-
861
- var currentY = 0
862
- val lastIndex = segmentViews.lastIndex
863
- val widthSpec = MeasureSpec.makeMeasureSpec(containerWidth, MeasureSpec.EXACTLY)
864
- val heightSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)
865
-
866
- segmentViews.forEachIndexed { index, view ->
867
- val segment = view as? BlockSegmentView
868
- val shouldAddBottomMargin = index != lastIndex || allowTrailingMargin
869
-
870
- currentY += segment?.segmentMarginTop ?: 0
871
-
872
- val overhang =
873
- if (view is TableContainerView) {
874
- max(ceil(view.tableStyle.horizontalOverflow.toDouble()).toInt(), 0)
875
- } else {
876
- 0
877
- }
878
-
879
- if (overhang > 0) {
880
- val extendedWidth = containerWidth + overhang * 2
881
- val extWidthSpec = MeasureSpec.makeMeasureSpec(extendedWidth, MeasureSpec.EXACTLY)
882
- view.measure(extWidthSpec, heightSpec)
883
- view.layout(-overhang, currentY, containerWidth + overhang, currentY + view.measuredHeight)
884
- } else {
885
- view.measure(widthSpec, heightSpec)
886
- view.layout(0, currentY, containerWidth, currentY + view.measuredHeight)
887
- }
888
- currentY += view.measuredHeight
889
-
890
- if (shouldAddBottomMargin) {
891
- currentY += segment?.segmentMarginBottom ?: 0
892
- }
893
- }
827
+ override fun onLayout(
828
+ changed: Boolean,
829
+ l: Int,
830
+ t: Int,
831
+ r: Int,
832
+ b: Int,
833
+ ) {
834
+ layoutSegmentsAndReport()
835
+ }
894
836
 
895
- emitLinkLayoutIfNeeded()
896
- // Segments may have moved or been replaced; re-read the selection (deduplicated).
897
- if (selectionOwner != null || activeBlock != null) selectionReporter.schedule() else selectionReporter.flushNow()
898
- }
837
+ /**
838
+ * ContainerNodeView.layoutSegments is final, so the fork's post-layout hooks that used to
839
+ * sit at its end run here instead: report link rects (renderLinkDecoration) and re-read the
840
+ * selection, since segments may have moved or been replaced (deduplicated).
841
+ */
842
+ private fun layoutSegmentsAndReport() {
843
+ layoutSegments()
844
+ if (width <= 0) return
845
+ emitLinkLayoutIfNeeded()
846
+ if (selectionOwner != null || activeBlock != null) selectionReporter.schedule() else selectionReporter.flushNow()
847
+ }
899
848
 
900
- fun setEmitLinkLayout(enabled: Boolean) {
901
- if (linkLayoutEnabled == enabled) return
902
- linkLayoutEnabled = enabled
903
- lastLinkLayout = null
904
- emitLinkLayoutIfNeeded()
905
- }
849
+ fun setEmitLinkLayout(enabled: Boolean) {
850
+ if (linkLayoutEnabled == enabled) return
851
+ linkLayoutEnabled = enabled
852
+ lastLinkLayout = null
853
+ emitLinkLayoutIfNeeded()
854
+ }
906
855
 
907
- /**
908
- * Reports link rects of every text segment (offset by the segment's position) to JS for
909
- * `renderLinkDecoration`. Tables are skipped: their cells scroll horizontally, so rects would
910
- * go stale. Runs after each segment layout pass; deduplicated so an unchanged layout sends nothing.
911
- */
912
- private fun emitLinkLayoutIfNeeded() {
913
- if (!linkLayoutEnabled || width <= 0) return
914
- val fragments = mutableListOf<LinkLayoutFragment>()
915
- var linkCount = 0
916
- segmentViews.forEach { view ->
917
- if (view is TableContainerView) {
918
- linkCount += view.linkCount
919
- return@forEach
920
- }
921
- if (view !is EnrichedMarkdownInternalText) return@forEach
922
- val (segmentFragments, segmentLinks) = LinkLayoutCollector.collect(view, view.left, view.top, linkCount)
923
- fragments += segmentFragments
924
- linkCount += segmentLinks
925
- }
926
- if (fragments == lastLinkLayout) return
927
- lastLinkLayout = fragments
928
- emitLinkLayout(this, fragments)
929
- }
856
+ /**
857
+ * Reports link rects of every text segment (offset by the segment's position) to JS for
858
+ * `renderLinkDecoration`. Tables are skipped: their cells scroll horizontally, so rects would
859
+ * go stale. Runs after each segment layout pass; deduplicated so an unchanged layout sends nothing.
860
+ *
861
+ * P1 limitation: links inside a BlockquoteContainerView are neither reported nor counted,
862
+ * so linkIndex of later links is shifted. Fixed in P2 (A2) by walking containers recursively.
863
+ */
864
+ private fun emitLinkLayoutIfNeeded() {
865
+ if (!linkLayoutEnabled || width <= 0) return
866
+ val fragments = mutableListOf<LinkLayoutFragment>()
867
+ var linkCount = 0
868
+ segmentViews.forEach { view ->
869
+ if (view is TableContainerView) {
870
+ linkCount += view.linkCount
871
+ return@forEach
872
+ }
873
+ if (view !is EnrichedMarkdownInternalText) return@forEach
874
+ val (segmentFragments, segmentLinks) = LinkLayoutCollector.collect(view, view.left, view.top, linkCount)
875
+ fragments += segmentFragments
876
+ linkCount += segmentLinks
877
+ }
878
+ if (fragments == lastLinkLayout) return
879
+ lastLinkLayout = fragments
880
+ emitLinkLayout(this, fragments)
881
+ }
930
882
 
931
- private fun computeSegmentsTotalHeight(): Int {
932
- var totalHeight = 0
933
- val lastIndex = segmentViews.lastIndex
934
- segmentViews.forEachIndexed { index, view ->
935
- val segment = view as? BlockSegmentView
936
- totalHeight += segment?.segmentMarginTop ?: 0
937
- totalHeight += view.measuredHeight
938
- if (index != lastIndex || allowTrailingMargin) {
939
- totalHeight += segment?.segmentMarginBottom ?: 0
940
- }
941
- }
942
- return totalHeight
883
+ fun onImageLayoutChanged() {
884
+ if (width > 0) {
885
+ layoutSegmentsAndReport()
886
+ requestLayout()
943
887
  }
888
+ MeasurementStore.invalidate(id)
889
+ val wrapper = stateWrapper ?: return
890
+ val state = Arguments.createMap()
891
+ state.putInt("forceHeightRecalculationCounter", ++forceHeightRecalculationCounter)
892
+ wrapper.updateState(state)
893
+ }
944
894
 
945
- fun cleanup() {
946
- selectionReporter.cancel()
947
- executor.shutdownNow()
948
- }
895
+ fun cleanup() {
896
+ selectionReporter.cancel()
897
+ executor.shutdownNow()
898
+ }
949
899
 
950
- companion object {
951
- private const val TAG = "EnrichedMarkdown"
952
- private const val BLOCK_FADE_DURATION_MS = 200L
953
- }
900
+ companion object {
901
+ private const val TAG = "EnrichedMarkdown"
902
+ private const val BLOCK_FADE_DURATION_MS = 200L
954
903
  }
904
+ }