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

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