@khanacademy/perseus 0.0.1 → 0.1.0

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 (503) hide show
  1. package/.babelrc.js +1 -1
  2. package/CHANGELOG.md +7 -0
  3. package/dist/es/index.js +15 -1
  4. package/dist/es/index.js.map +1 -1
  5. package/dist/index.css +11934 -0
  6. package/dist/index.js +15 -1
  7. package/dist/index.js.map +1 -1
  8. package/dist/strings.js +148 -1
  9. package/package.json +40 -4
  10. package/src/README.md +7 -0
  11. package/src/__stories__/item-renderer.stories.jsx +27 -0
  12. package/src/__stories__/server-item-renderer.stories.jsx +27 -0
  13. package/src/__testdata__/item-renderer_testdata.js +161 -0
  14. package/src/__testdata__/renderer_testdata.js +123 -0
  15. package/src/__tests__/__snapshots__/item-renderer_test.jsx.snap +233 -0
  16. package/src/__tests__/__snapshots__/perseus-markdown_test.jsx.snap +516 -0
  17. package/src/__tests__/__snapshots__/pure-markdown_test.js.snap +113 -0
  18. package/src/__tests__/__snapshots__/renderer_test.jsx.snap +576 -0
  19. package/src/__tests__/__snapshots__/server-item-renderer_test.jsx.snap +306 -0
  20. package/src/__tests__/a11y_test.jsx +304 -0
  21. package/src/__tests__/item-renderer_test.jsx +892 -0
  22. package/src/__tests__/mock-asset-loading-widget.jsx +57 -0
  23. package/src/__tests__/mock-widget.jsx +35 -0
  24. package/src/__tests__/perseus-markdown_test.jsx +747 -0
  25. package/src/__tests__/pure-markdown_test.js +552 -0
  26. package/src/__tests__/renderability_test.jsx +481 -0
  27. package/src/__tests__/renderer-api_test.jsx +333 -0
  28. package/src/__tests__/renderer_test.jsx +1792 -0
  29. package/src/__tests__/server-item-renderer_test.jsx +516 -0
  30. package/src/__tests__/test-items/image-item.js +25 -0
  31. package/src/__tests__/test-items/input-number-1-item.js +27 -0
  32. package/src/__tests__/test-items/input-number-2-item.js +39 -0
  33. package/src/__tests__/test-items/table-item.js +30 -0
  34. package/src/__tests__/tex-wrangler_test.js +83 -0
  35. package/src/__tests__/util_test.js +41 -0
  36. package/src/a11y.js +46 -0
  37. package/src/all-widgets.js +9 -0
  38. package/src/article-renderer.jsx +215 -0
  39. package/src/asset-context.js +30 -0
  40. package/src/basic-widgets.js +13 -0
  41. package/src/components/__stories__/button-group.stories.jsx +61 -0
  42. package/src/components/__stories__/fixed-to-responsive.stories.jsx +132 -0
  43. package/src/components/__stories__/graph.stories.jsx +27 -0
  44. package/src/components/__stories__/graphie.stories.jsx +28 -0
  45. package/src/components/__stories__/hud.stories.jsx +36 -0
  46. package/src/components/__stories__/icon.stories.jsx +56 -0
  47. package/src/components/__stories__/image-loader.stories.jsx +60 -0
  48. package/src/components/__stories__/info-tip.stories.jsx +33 -0
  49. package/src/components/__stories__/inline-icon.stories.jsx +39 -0
  50. package/src/components/__stories__/input-with-examples.stories.jsx +51 -0
  51. package/src/components/__stories__/lint.stories.jsx +40 -0
  52. package/src/components/__stories__/math-input.stories.jsx +29 -0
  53. package/src/components/__stories__/math-output.stories.jsx +24 -0
  54. package/src/components/__stories__/multi-button-group.stories.jsx +69 -0
  55. package/src/components/__stories__/number-input.stories.jsx +42 -0
  56. package/src/components/__stories__/possible-answers.stories.jsx +22 -0
  57. package/src/components/__stories__/prop-check-box.stories.jsx +62 -0
  58. package/src/components/__stories__/range-input.stories.jsx +28 -0
  59. package/src/components/__stories__/simple-keypad-input.stories.jsx +28 -0
  60. package/src/components/__stories__/sortable.stories.jsx +73 -0
  61. package/src/components/__stories__/stub-tag-editor.stories.jsx +44 -0
  62. package/src/components/__stories__/svg-image.stories.jsx +60 -0
  63. package/src/components/__stories__/tex.stories.jsx +18 -0
  64. package/src/components/__stories__/text-buttons.stories.jsx +51 -0
  65. package/src/components/__stories__/text-input.stories.jsx +34 -0
  66. package/src/components/__stories__/text-list-editor.stories.jsx +33 -0
  67. package/src/components/__stories__/zoomable-tex.stories.jsx +39 -0
  68. package/src/components/__stories__/zoomable.stories.jsx +40 -0
  69. package/src/components/__testdata__/sorter_testdata.js +20 -0
  70. package/src/components/__tests__/__snapshots__/sortable_test.jsx.snap +79 -0
  71. package/src/components/__tests__/__snapshots__/sorter_test.jsx.snap +297 -0
  72. package/src/components/__tests__/number-input_test.jsx +112 -0
  73. package/src/components/__tests__/sortable_test.jsx +234 -0
  74. package/src/components/__tests__/sorter_test.jsx +111 -0
  75. package/src/components/__tests__/zoomable_test.jsx +388 -0
  76. package/src/components/button-group.jsx +131 -0
  77. package/src/components/constants.js +15 -0
  78. package/src/components/fixed-to-responsive.jsx +163 -0
  79. package/src/components/graph.jsx +414 -0
  80. package/src/components/graphie-classes.jsx +148 -0
  81. package/src/components/graphie-movables.jsx +342 -0
  82. package/src/components/graphie.jsx +353 -0
  83. package/src/components/highlighting/highlightable-content.jsx +245 -0
  84. package/src/components/highlighting/highlights.js +208 -0
  85. package/src/components/highlighting/ranges.js +299 -0
  86. package/src/components/highlighting/types.js +73 -0
  87. package/src/components/highlighting/ui/highlight-renderer.jsx +179 -0
  88. package/src/components/highlighting/ui/highlight-set-renderer.jsx +190 -0
  89. package/src/components/highlighting/ui/highlight-tooltip.jsx +153 -0
  90. package/src/components/highlighting/ui/highlighting-ui.jsx +163 -0
  91. package/src/components/highlighting/ui/selection-tracker.jsx +195 -0
  92. package/src/components/highlighting/ui/types.js +21 -0
  93. package/src/components/highlighting/ui/util.js +166 -0
  94. package/src/components/highlighting/word-indexer.jsx +155 -0
  95. package/src/components/hud.jsx +179 -0
  96. package/src/components/icon.jsx +149 -0
  97. package/src/components/image-loader.jsx +175 -0
  98. package/src/components/info-tip/info-tip.jsx +149 -0
  99. package/src/components/info-tip.jsx +38 -0
  100. package/src/components/inline-icon.jsx +81 -0
  101. package/src/components/input-with-examples.jsx +231 -0
  102. package/src/components/lint.jsx +456 -0
  103. package/src/components/math-input.jsx +282 -0
  104. package/src/components/math-output.jsx +128 -0
  105. package/src/components/multi-button-group.jsx +137 -0
  106. package/src/components/new-tooltip/README.md +110 -0
  107. package/src/components/new-tooltip/__stories__/flexible-position.stories.jsx +437 -0
  108. package/src/components/new-tooltip/__stories__/new-tooltip.stories.jsx +665 -0
  109. package/src/components/new-tooltip/__stories__/tooltip-arrow.stories.jsx +70 -0
  110. package/src/components/new-tooltip/__stories__/tooltip-bubble.stories.jsx +98 -0
  111. package/src/components/new-tooltip/__tests__/style-constants_test.js +8 -0
  112. package/src/components/new-tooltip/__tests__/util_test.js +47 -0
  113. package/src/components/new-tooltip/flexible-position.jsx +182 -0
  114. package/src/components/new-tooltip/new-tooltip.jsx +399 -0
  115. package/src/components/new-tooltip/style-constants.js +13 -0
  116. package/src/components/new-tooltip/tooltip-arrow.jsx +184 -0
  117. package/src/components/new-tooltip/tooltip-bubble.jsx +223 -0
  118. package/src/components/new-tooltip/tooltip-portal.jsx +101 -0
  119. package/src/components/new-tooltip/tooltip-positioner.jsx +529 -0
  120. package/src/components/new-tooltip/types.js +183 -0
  121. package/src/components/new-tooltip/util.js +27 -0
  122. package/src/components/number-input.jsx +253 -0
  123. package/src/components/possible-answers.jsx +36 -0
  124. package/src/components/prop-check-box.jsx +78 -0
  125. package/src/components/range-input.jsx +61 -0
  126. package/src/components/simple-keypad-input.jsx +91 -0
  127. package/src/components/sortable.jsx +872 -0
  128. package/src/components/stub-tag-editor.jsx +53 -0
  129. package/src/components/svg-image.jsx +762 -0
  130. package/src/components/tex-buttons.jsx +322 -0
  131. package/src/components/tex.jsx +70 -0
  132. package/src/components/text-input.jsx +82 -0
  133. package/src/components/text-list-editor.jsx +157 -0
  134. package/src/components/tooltip.jsx +395 -0
  135. package/src/components/visibility-observer/__stories__/visibility-observer.stories.jsx +140 -0
  136. package/src/components/visibility-observer/get-scroll-parent.js +45 -0
  137. package/src/components/visibility-observer/visibility-observer.js +109 -0
  138. package/src/components/visibility-observer/visibility-observer.jsx +123 -0
  139. package/src/components/zoomable-tex.jsx +70 -0
  140. package/src/components/zoomable.jsx +304 -0
  141. package/src/components.js +17 -0
  142. package/src/dependencies.js +22 -0
  143. package/src/error-boundary.jsx +48 -0
  144. package/src/extra-widgets.js +75 -0
  145. package/src/gorgon/README +40 -0
  146. package/src/gorgon/__tests__/matcher_test.js +497 -0
  147. package/src/gorgon/__tests__/rule_test.js +101 -0
  148. package/src/gorgon/__tests__/rules_test.js +487 -0
  149. package/src/gorgon/__tests__/selector-parser_test.js +52 -0
  150. package/src/gorgon/__tests__/tree-transformer_test.js +446 -0
  151. package/src/gorgon/gorgon.js +290 -0
  152. package/src/gorgon/proptypes.js +29 -0
  153. package/src/gorgon/rule.js +413 -0
  154. package/src/gorgon/rules/absolute-url.js +24 -0
  155. package/src/gorgon/rules/all-rules.js +72 -0
  156. package/src/gorgon/rules/blockquoted-math.js +10 -0
  157. package/src/gorgon/rules/blockquoted-widget.js +10 -0
  158. package/src/gorgon/rules/double-spacing-after-terminal.js +12 -0
  159. package/src/gorgon/rules/extra-content-spacing.js +12 -0
  160. package/src/gorgon/rules/heading-level-1.js +14 -0
  161. package/src/gorgon/rules/heading-level-skip.js +20 -0
  162. package/src/gorgon/rules/heading-sentence-case.js +11 -0
  163. package/src/gorgon/rules/heading-title-case.js +63 -0
  164. package/src/gorgon/rules/image-alt-text.js +21 -0
  165. package/src/gorgon/rules/image-in-table.js +10 -0
  166. package/src/gorgon/rules/image-spaces-around-urls.js +35 -0
  167. package/src/gorgon/rules/image-widget.js +50 -0
  168. package/src/gorgon/rules/link-click-here.js +11 -0
  169. package/src/gorgon/rules/lint-utils.js +48 -0
  170. package/src/gorgon/rules/long-paragraph.js +14 -0
  171. package/src/gorgon/rules/math-adjacent.js +10 -0
  172. package/src/gorgon/rules/math-align-extra-break.js +11 -0
  173. package/src/gorgon/rules/math-align-linebreaks.js +43 -0
  174. package/src/gorgon/rules/math-empty.js +10 -0
  175. package/src/gorgon/rules/math-font-size.js +12 -0
  176. package/src/gorgon/rules/math-frac.js +10 -0
  177. package/src/gorgon/rules/math-nested.js +11 -0
  178. package/src/gorgon/rules/math-starts-with-space.js +12 -0
  179. package/src/gorgon/rules/math-text-empty.js +10 -0
  180. package/src/gorgon/rules/math-without-dollars.js +14 -0
  181. package/src/gorgon/rules/nested-lists.js +11 -0
  182. package/src/gorgon/rules/profanity.js +10 -0
  183. package/src/gorgon/rules/table-missing-cells.js +20 -0
  184. package/src/gorgon/rules/unbalanced-code-delimiters.js +14 -0
  185. package/src/gorgon/rules/unescaped-dollar.js +10 -0
  186. package/src/gorgon/rules/widget-in-table.js +10 -0
  187. package/src/gorgon/selector.js +506 -0
  188. package/src/gorgon/tree-transformer.js +588 -0
  189. package/src/hint-renderer.jsx +159 -0
  190. package/src/hints-renderer.jsx +359 -0
  191. package/src/icon-paths.js +120 -0
  192. package/src/index.js +149 -2
  193. package/src/init.js +49 -0
  194. package/src/interaction-tracker.js +60 -0
  195. package/src/interactive2/__tests__/movable-mock.js +48 -0
  196. package/src/interactive2/__tests__/movable-point_test.js +146 -0
  197. package/src/interactive2/interactive-util.js +152 -0
  198. package/src/interactive2/movable-helper-methods.js +160 -0
  199. package/src/interactive2/movable-line-options.js +392 -0
  200. package/src/interactive2/movable-line.js +322 -0
  201. package/src/interactive2/movable-point-options.js +190 -0
  202. package/src/interactive2/movable-point.jsx +604 -0
  203. package/src/interactive2/movable-polygon-options.js +351 -0
  204. package/src/interactive2/movable-polygon.js +367 -0
  205. package/src/interactive2/movable.js +273 -0
  206. package/src/interactive2/objective_.js +79 -0
  207. package/src/interactive2/types.js +3 -0
  208. package/src/interactive2/wrapped-defaults.js +86 -0
  209. package/src/interactive2/wrapped-ellipse.js +87 -0
  210. package/src/interactive2/wrapped-line.js +109 -0
  211. package/src/interactive2/wrapped-path.js +47 -0
  212. package/src/interactive2.js +91 -0
  213. package/src/item-renderer.jsx +518 -0
  214. package/src/item-version.js +12 -0
  215. package/src/jipt-paragraphs.js +44 -0
  216. package/src/jquery.mobile.vmouse.js +545 -0
  217. package/src/keypad-context.js +33 -0
  218. package/src/loading-context.js +20 -0
  219. package/src/logging/log.js +107 -0
  220. package/src/logging/log_test.js +41 -0
  221. package/src/mixins/changeable.jsx +115 -0
  222. package/src/mixins/editor-jsonify.jsx +13 -0
  223. package/src/mixins/provide-keypad.jsx +131 -0
  224. package/src/mixins/widget-jsonify-deprecated.jsx +23 -0
  225. package/src/mixins/widget-prop-blacklist.js +36 -0
  226. package/src/multi-items/__stories__/multi-renderer.stories.js +88 -0
  227. package/src/multi-items/__testdata__/multi-renderer_testdata.js +174 -0
  228. package/src/multi-items/__tests__/__snapshots__/multi-renderer_test.js.snap +994 -0
  229. package/src/multi-items/__tests__/items_test.js +245 -0
  230. package/src/multi-items/__tests__/multi-renderer_test.js +903 -0
  231. package/src/multi-items/__tests__/prop-type-builders_test.js +134 -0
  232. package/src/multi-items/__tests__/shapes_test.js +44 -0
  233. package/src/multi-items/__tests__/trees_test.js +316 -0
  234. package/src/multi-items/item-types.js +55 -0
  235. package/src/multi-items/items.js +176 -0
  236. package/src/multi-items/multi-renderer.jsx +542 -0
  237. package/src/multi-items/prop-type-builders.js +105 -0
  238. package/src/multi-items/shape-types.js +47 -0
  239. package/src/multi-items/shapes.js +53 -0
  240. package/src/multi-items/tree-types.js +42 -0
  241. package/src/multi-items/trees.js +345 -0
  242. package/src/multi-items.js +76 -0
  243. package/src/not-gorgon.js +129 -0
  244. package/src/perseus-api.jsx +270 -0
  245. package/src/perseus-env.js +26 -0
  246. package/src/perseus-error.js +19 -0
  247. package/src/perseus-markdown.jsx +434 -0
  248. package/src/perseus-types.js +1597 -0
  249. package/src/pure-markdown.js +346 -0
  250. package/src/question-paragraph.jsx +30 -0
  251. package/src/renderability.jsx +108 -0
  252. package/src/renderer.jsx +1931 -0
  253. package/src/server-item-renderer.jsx +467 -0
  254. package/src/sigfigs.jsx +492 -0
  255. package/src/styles/articles.less +748 -0
  256. package/src/styles/constants.js +119 -0
  257. package/src/styles/global-constants.js +350 -0
  258. package/src/styles/global-styles.js +307 -0
  259. package/src/styles/khan-exercise.css +1304 -0
  260. package/src/styles/media-queries.js +48 -0
  261. package/src/styles/perseus-renderer.less +1008 -0
  262. package/src/styles/shared.js +132 -0
  263. package/src/styles/util.less +47 -0
  264. package/src/styles/variables.boostrap.less +301 -0
  265. package/src/styles/variables.less +68 -0
  266. package/src/styles/variables.shared.less +356 -0
  267. package/src/styles/widgets/categorizer.less +39 -0
  268. package/src/styles/widgets/definition.less +86 -0
  269. package/src/styles/widgets/dropdown.less +3 -0
  270. package/src/styles/widgets/expression.less +147 -0
  271. package/src/styles/widgets/graded-group.less +36 -0
  272. package/src/styles/widgets/grapher.less +11 -0
  273. package/src/styles/widgets/group.less +19 -0
  274. package/src/styles/widgets/image.less +29 -0
  275. package/src/styles/widgets/interactive-graph.less +61 -0
  276. package/src/styles/widgets/label-image.less +41 -0
  277. package/src/styles/widgets/matcher.less +8 -0
  278. package/src/styles/widgets/matrix.less +120 -0
  279. package/src/styles/widgets/measurer.less +20 -0
  280. package/src/styles/widgets/orderer.less +97 -0
  281. package/src/styles/widgets/passage.less +151 -0
  282. package/src/styles/widgets/plotter.less +33 -0
  283. package/src/styles/widgets/radio.less +52 -0
  284. package/src/styles/widgets/reaction-diagram.less +35 -0
  285. package/src/styles/widgets/sequence.less +5 -0
  286. package/src/styles/widgets/sortable.less +127 -0
  287. package/src/styles/widgets/table.less +56 -0
  288. package/src/styles/widgets/transformer.less +28 -0
  289. package/src/styles/widgets/unit.less +14 -0
  290. package/src/tex-wrangler.js +176 -0
  291. package/src/traversal.jsx +141 -0
  292. package/src/types.js +538 -0
  293. package/src/util/a11y.js +24 -0
  294. package/src/util/answer-types.js +862 -0
  295. package/src/util/answer-types_test.js +115 -0
  296. package/src/util/colors.js +94 -0
  297. package/src/util/fix-passage-refs.jsx +122 -0
  298. package/src/util/graph-utils.js +16 -0
  299. package/src/util/graphie.js +1585 -0
  300. package/src/util/interactive.js +4071 -0
  301. package/src/util/katex-preprocess.js +16 -0
  302. package/src/util/math.js +296 -0
  303. package/src/util/mobile-native-utils.js +14 -0
  304. package/src/util/mobile-native-utils_test.js +24 -0
  305. package/src/util/register-all-widgets-for-testing.js +12 -0
  306. package/src/util/scroll-utils.js +162 -0
  307. package/src/util/sizing-utils.js +46 -0
  308. package/src/util/tex.js +203 -0
  309. package/src/util/url-utils.js +36 -0
  310. package/src/util.js +990 -0
  311. package/src/version.js +11 -0
  312. package/src/widget-container.jsx +174 -0
  313. package/src/widgets/__stories__/categorizer.stories.jsx +15 -0
  314. package/src/widgets/__stories__/cs-program.stories.jsx +15 -0
  315. package/src/widgets/__stories__/definition.stories.jsx +38 -0
  316. package/src/widgets/__stories__/dropdown.stories.jsx +15 -0
  317. package/src/widgets/__stories__/explanation.stories.jsx +19 -0
  318. package/src/widgets/__stories__/expression.stories.jsx +26 -0
  319. package/src/widgets/__stories__/graded-group-set.stories.jsx +32 -0
  320. package/src/widgets/__stories__/graded-group.stories.jsx +32 -0
  321. package/src/widgets/__stories__/grapher.stories.jsx +47 -0
  322. package/src/widgets/__stories__/group.stories.jsx +15 -0
  323. package/src/widgets/__stories__/iframe.stories.jsx +15 -0
  324. package/src/widgets/__stories__/image.stories.jsx +62 -0
  325. package/src/widgets/__stories__/input-number.stories.jsx +90 -0
  326. package/src/widgets/__stories__/interaction.stories.jsx +15 -0
  327. package/src/widgets/__stories__/interactive-graph.stories.jsx +61 -0
  328. package/src/widgets/__stories__/matcher.stories.jsx +15 -0
  329. package/src/widgets/__stories__/matrix.stories.jsx +15 -0
  330. package/src/widgets/__stories__/number-line.stories.jsx +15 -0
  331. package/src/widgets/__stories__/numeric-input.stories.jsx +19 -0
  332. package/src/widgets/__stories__/orderer.stories.jsx +15 -0
  333. package/src/widgets/__stories__/passage-ref.stories.jsx +19 -0
  334. package/src/widgets/__stories__/passage.stories.jsx +19 -0
  335. package/src/widgets/__stories__/radio.stories.jsx +82 -0
  336. package/src/widgets/__stories__/sorter.stories.jsx +15 -0
  337. package/src/widgets/__stories__/transformer.stories.jsx +15 -0
  338. package/src/widgets/__stories__/video.stories.jsx +15 -0
  339. package/src/widgets/__testdata__/categorizer_testdata.js +50 -0
  340. package/src/widgets/__testdata__/cs-program_testdata.js +28 -0
  341. package/src/widgets/__testdata__/dropdown_testdata.js +34 -0
  342. package/src/widgets/__testdata__/explanation_testdata.js +56 -0
  343. package/src/widgets/__testdata__/expression_testdata.js +95 -0
  344. package/src/widgets/__testdata__/graded-group-set_testdata.js +358 -0
  345. package/src/widgets/__testdata__/graded-group_testdata.js +79 -0
  346. package/src/widgets/__testdata__/grapher_testdata.js +376 -0
  347. package/src/widgets/__testdata__/group_testdata.js +156 -0
  348. package/src/widgets/__testdata__/iframe_testdata.js +36 -0
  349. package/src/widgets/__testdata__/image_testdata.js +38 -0
  350. package/src/widgets/__testdata__/input-number_testdata.js +73 -0
  351. package/src/widgets/__testdata__/interaction_testdata.js +264 -0
  352. package/src/widgets/__testdata__/interactive-graph_testdata.js +646 -0
  353. package/src/widgets/__testdata__/matcher_testdata.js +34 -0
  354. package/src/widgets/__testdata__/matrix_testdata.js +29 -0
  355. package/src/widgets/__testdata__/number-line_testdata.js +31 -0
  356. package/src/widgets/__testdata__/numeric-input_testdata.js +266 -0
  357. package/src/widgets/__testdata__/orderer_testdata.js +58 -0
  358. package/src/widgets/__testdata__/passage-ref_testdata.js +41 -0
  359. package/src/widgets/__testdata__/passage_testdata.js +71 -0
  360. package/src/widgets/__testdata__/radio_testdata.js +210 -0
  361. package/src/widgets/__testdata__/sorter_testdata.js +20 -0
  362. package/src/widgets/__testdata__/transformer_testdata.js +109 -0
  363. package/src/widgets/__testdata__/video_testdata.js +22 -0
  364. package/src/widgets/__tests__/__snapshots__/categorizer_test.jsx.snap +928 -0
  365. package/src/widgets/__tests__/__snapshots__/cs-program_test.jsx.snap +75 -0
  366. package/src/widgets/__tests__/__snapshots__/definition_test.js.snap +99 -0
  367. package/src/widgets/__tests__/__snapshots__/dropdown_test.jsx.snap +143 -0
  368. package/src/widgets/__tests__/__snapshots__/explanation_test.js.snap +473 -0
  369. package/src/widgets/__tests__/__snapshots__/expression_test.jsx.snap +38 -0
  370. package/src/widgets/__tests__/__snapshots__/graded-group-set-jipt_test.jsx.snap +754 -0
  371. package/src/widgets/__tests__/__snapshots__/graded-group-set_test.jsx.snap +310 -0
  372. package/src/widgets/__tests__/__snapshots__/graded-group_test.jsx.snap +863 -0
  373. package/src/widgets/__tests__/__snapshots__/grapher_test.jsx.snap +2880 -0
  374. package/src/widgets/__tests__/__snapshots__/group_test.jsx.snap +1154 -0
  375. package/src/widgets/__tests__/__snapshots__/iframe_test.jsx.snap +69 -0
  376. package/src/widgets/__tests__/__snapshots__/image_test.jsx.snap +388 -0
  377. package/src/widgets/__tests__/__snapshots__/input-number_test.jsx.snap +978 -0
  378. package/src/widgets/__tests__/__snapshots__/interaction_test.jsx.snap +311 -0
  379. package/src/widgets/__tests__/__snapshots__/interactive-graph_test.jsx.snap +2915 -0
  380. package/src/widgets/__tests__/__snapshots__/matcher_test.jsx.snap +1039 -0
  381. package/src/widgets/__tests__/__snapshots__/matrix_test.jsx.snap +450 -0
  382. package/src/widgets/__tests__/__snapshots__/number-line_test.jsx.snap +888 -0
  383. package/src/widgets/__tests__/__snapshots__/numeric-input_test.jsx.snap +930 -0
  384. package/src/widgets/__tests__/__snapshots__/orderer_test.jsx.snap +275 -0
  385. package/src/widgets/__tests__/__snapshots__/passage-ref_test.jsx.snap +446 -0
  386. package/src/widgets/__tests__/__snapshots__/passage_test.jsx.snap +1988 -0
  387. package/src/widgets/__tests__/__snapshots__/radio_test.jsx.snap +7107 -0
  388. package/src/widgets/__tests__/__snapshots__/video_test.jsx.snap +163 -0
  389. package/src/widgets/__tests__/categorizer_test.jsx +205 -0
  390. package/src/widgets/__tests__/cs-program_test.jsx +45 -0
  391. package/src/widgets/__tests__/definition_test.js +152 -0
  392. package/src/widgets/__tests__/dropdown_test.jsx +106 -0
  393. package/src/widgets/__tests__/explanation_test.js +199 -0
  394. package/src/widgets/__tests__/expression_test.jsx +439 -0
  395. package/src/widgets/__tests__/graded-group-set-jipt_test.jsx +33 -0
  396. package/src/widgets/__tests__/graded-group-set_test.jsx +270 -0
  397. package/src/widgets/__tests__/graded-group_test.jsx +233 -0
  398. package/src/widgets/__tests__/grapher.cypress.jsx +486 -0
  399. package/src/widgets/__tests__/grapher_test.jsx +35 -0
  400. package/src/widgets/__tests__/group_test.jsx +512 -0
  401. package/src/widgets/__tests__/iframe_test.jsx +47 -0
  402. package/src/widgets/__tests__/image_test.jsx +65 -0
  403. package/src/widgets/__tests__/input-number_test.jsx +360 -0
  404. package/src/widgets/__tests__/interaction_test.jsx +33 -0
  405. package/src/widgets/__tests__/interactive-graph.cypress.jsx +697 -0
  406. package/src/widgets/__tests__/interactive-graph_test.jsx +105 -0
  407. package/src/widgets/__tests__/matcher_test.jsx +157 -0
  408. package/src/widgets/__tests__/matrix_test.jsx +84 -0
  409. package/src/widgets/__tests__/number-line_test.jsx +81 -0
  410. package/src/widgets/__tests__/numeric-input_test.jsx +574 -0
  411. package/src/widgets/__tests__/orderer_test.jsx +75 -0
  412. package/src/widgets/__tests__/passage-ref_test.jsx +166 -0
  413. package/src/widgets/__tests__/passage_test.jsx +133 -0
  414. package/src/widgets/__tests__/radio/base-radio_test.jsx +82 -0
  415. package/src/widgets/__tests__/radio_test.jsx +651 -0
  416. package/src/widgets/__tests__/renderQuestion.jsx +65 -0
  417. package/src/widgets/__tests__/unit_test.jsx +144 -0
  418. package/src/widgets/__tests__/video_test.jsx +55 -0
  419. package/src/widgets/categorizer.jsx +338 -0
  420. package/src/widgets/cs-program.jsx +228 -0
  421. package/src/widgets/definition.jsx +141 -0
  422. package/src/widgets/dropdown.jsx +143 -0
  423. package/src/widgets/explanation.jsx +315 -0
  424. package/src/widgets/expression.jsx +654 -0
  425. package/src/widgets/graded-group-answer-bar.jsx +152 -0
  426. package/src/widgets/graded-group-set.jsx +285 -0
  427. package/src/widgets/graded-group.jsx +448 -0
  428. package/src/widgets/grapher/util.jsx +819 -0
  429. package/src/widgets/grapher.jsx +658 -0
  430. package/src/widgets/group.jsx +213 -0
  431. package/src/widgets/iframe.jsx +199 -0
  432. package/src/widgets/image.jsx +309 -0
  433. package/src/widgets/input-number.jsx +410 -0
  434. package/src/widgets/interaction/arrow-picker.jsx +39 -0
  435. package/src/widgets/interaction/color-picker.jsx +69 -0
  436. package/src/widgets/interaction/dash-picker.jsx +46 -0
  437. package/src/widgets/interaction/element-container.jsx +97 -0
  438. package/src/widgets/interaction.jsx +742 -0
  439. package/src/widgets/interactive-graph.jsx +2821 -0
  440. package/src/widgets/label-image/__stories__/answer-choices.stories.jsx +93 -0
  441. package/src/widgets/label-image/__stories__/marker.stories.jsx +172 -0
  442. package/src/widgets/label-image/answer-choices.jsx +311 -0
  443. package/src/widgets/label-image/marker.jsx +365 -0
  444. package/src/widgets/label-image/types.js +33 -0
  445. package/src/widgets/label-image.jsx +841 -0
  446. package/src/widgets/label-image_test.js +840 -0
  447. package/src/widgets/lights-puzzle.jsx +356 -0
  448. package/src/widgets/matcher.jsx +271 -0
  449. package/src/widgets/matrix.jsx +615 -0
  450. package/src/widgets/measurer.jsx +215 -0
  451. package/src/widgets/molecule/__tests__/layout_test.jsx +159 -0
  452. package/src/widgets/molecule/__tests__/parser_test.jsx +142 -0
  453. package/src/widgets/molecule/molecule-drawing.jsx +175 -0
  454. package/src/widgets/molecule/molecule-layout.jsx +367 -0
  455. package/src/widgets/molecule/smiles-parser.jsx +300 -0
  456. package/src/widgets/molecule.jsx +172 -0
  457. package/src/widgets/number-line.jsx +858 -0
  458. package/src/widgets/numeric-input.jsx +622 -0
  459. package/src/widgets/orderer.jsx +753 -0
  460. package/src/widgets/passage/__tests__/passage-markdown_test.jsx +382 -0
  461. package/src/widgets/passage/get-line-height-for-node.js +10 -0
  462. package/src/widgets/passage/passage-markdown.jsx +416 -0
  463. package/src/widgets/passage-ref-target.jsx +70 -0
  464. package/src/widgets/passage-ref.jsx +198 -0
  465. package/src/widgets/passage.jsx +601 -0
  466. package/src/widgets/plotter.jsx +1198 -0
  467. package/src/widgets/radio/__stories__/base-radio.stories.jsx +40 -0
  468. package/src/widgets/radio/__stories__/choice-icon.stories.jsx +157 -0
  469. package/src/widgets/radio/__stories__/choice-none-above.stories.jsx +26 -0
  470. package/src/widgets/radio/__stories__/cross-out-button.stories.jsx +64 -0
  471. package/src/widgets/radio/__stories__/cross-out-menu.stories.jsx +59 -0
  472. package/src/widgets/radio/__stories__/cross-out-tooltip.stories.jsx +59 -0
  473. package/src/widgets/radio/__stories__/focus-ring.stories.jsx +38 -0
  474. package/src/widgets/radio/__stories__/option-status.stories.jsx +118 -0
  475. package/src/widgets/radio/__stories__/toggleable-radio-button.stories.jsx +28 -0
  476. package/src/widgets/radio/base-radio.jsx +642 -0
  477. package/src/widgets/radio/choice-icon.jsx +468 -0
  478. package/src/widgets/radio/choice-none-above.jsx +54 -0
  479. package/src/widgets/radio/choice.jsx +692 -0
  480. package/src/widgets/radio/choice.jsx.fixture.js +64 -0
  481. package/src/widgets/radio/cross-out-button.jsx +136 -0
  482. package/src/widgets/radio/cross-out-menu-wrapper.jsx +492 -0
  483. package/src/widgets/radio/cross-out-tooltip.jsx +129 -0
  484. package/src/widgets/radio/focus-ring.jsx +51 -0
  485. package/src/widgets/radio/option-status.jsx +89 -0
  486. package/src/widgets/radio/toggleable-radio-button.jsx +133 -0
  487. package/src/widgets/radio/util.js +32 -0
  488. package/src/widgets/radio/util_test.js +19 -0
  489. package/src/widgets/radio/widget.jsx +470 -0
  490. package/src/widgets/radio.jsx +156 -0
  491. package/src/widgets/reaction-diagram.jsx +136 -0
  492. package/src/widgets/sequence.jsx +140 -0
  493. package/src/widgets/simulator.jsx +808 -0
  494. package/src/widgets/sorter.jsx +146 -0
  495. package/src/widgets/table.jsx +395 -0
  496. package/src/widgets/transformer.jsx +2870 -0
  497. package/src/widgets/unit.jsx +371 -0
  498. package/src/widgets/video-transcript-link.jsx +84 -0
  499. package/src/widgets/video-transcript-link_test.js +211 -0
  500. package/src/widgets/video.jsx +131 -0
  501. package/src/widgets.js +431 -0
  502. package/src/zoom.js +539 -0
  503. package/src/__tests__/index.test.js +0 -14
@@ -0,0 +1,195 @@
1
+ /* eslint-disable react/no-unsafe */
2
+ // @flow
3
+ /**
4
+ * Tracks the user's current selection, and exposes it to the subtree using the
5
+ * function-as-children pattern.
6
+ */
7
+ import * as React from "react";
8
+
9
+ import type {DOMHighlight, DOMRange} from "./types.js";
10
+
11
+ export type TrackedSelection = {|
12
+ // The focus of the current selection - that is, the boundary point of the
13
+ // selection that the user is dragging around.
14
+ //
15
+ // We don't simply track the full `Selection` object, which contains all of
16
+ // this focus and range information and more, because the browser reuses
17
+ // the global `Selection` object and mutates it, which breaks our
18
+ // `shouldComponentUpdate` checks.
19
+ focusNode: Node,
20
+ focusOffset: number,
21
+
22
+ // If the current selection maps to a valid new highlight, we cache the
23
+ // highlight object here.
24
+ proposedHighlight: DOMHighlight,
25
+ |};
26
+
27
+ type SelectionTrackerProps = {
28
+ // A function that builds a DOMHighlight from the given DOMRange, if
29
+ // possible. If it would not currently be valid to add a highlight over the
30
+ // given DOMRange, returns null.
31
+ buildHighlight: (domRange: DOMRange) => ?DOMHighlight,
32
+ // The function-as-children pattern: passes the tracked selection, and
33
+ // whether the user is currently using their mouse to select, down the
34
+ // tree.
35
+ children?: (
36
+ trackedSelection: ?TrackedSelection,
37
+ userIsMouseSelecting: boolean,
38
+ ) => React.Element<any>,
39
+ // If false, will not track selections.
40
+ enabled: boolean,
41
+ ...
42
+ };
43
+
44
+ type SelectionTrackerState = {
45
+ // The current state of the mouse button. We distinguish between down,
46
+ // down and the selection has changed since going down, and up.
47
+ mouseState: "down" | "down-and-selecting" | "up",
48
+ // The current TrackedSelection, if any.
49
+ trackedSelection: ?TrackedSelection,
50
+ ...
51
+ };
52
+
53
+ class SelectionTracker extends React.PureComponent<
54
+ SelectionTrackerProps,
55
+ SelectionTrackerState,
56
+ > {
57
+ state: SelectionTrackerState = {
58
+ mouseState: "up",
59
+ trackedSelection: null,
60
+ };
61
+
62
+ componentDidMount() {
63
+ this._updateListeners(false, this.props.enabled);
64
+ }
65
+
66
+ UNSAFE_componentWillReceiveProps(nextProps: SelectionTrackerProps) {
67
+ if (this.props.buildHighlight !== nextProps.buildHighlight) {
68
+ // The highlight-building function changed, so the
69
+ // proposedHighlight we built with it might be different, or no
70
+ // longer be valid. Update accordingly.
71
+ this._updateTrackedSelection(nextProps.buildHighlight);
72
+ }
73
+
74
+ this._updateListeners(this.props.enabled, nextProps.enabled);
75
+ }
76
+
77
+ componentWillUnmount() {
78
+ this._updateListeners(this.props.enabled, false);
79
+ }
80
+
81
+ _updateListeners(wasListening: boolean, willListen: boolean) {
82
+ if (!wasListening && willListen) {
83
+ window.addEventListener("mousedown", this._handleMouseDown);
84
+ window.addEventListener("mouseup", this._handleMouseUp);
85
+ document.addEventListener(
86
+ "selectionchange",
87
+ this._handleSelectionChange,
88
+ );
89
+ } else if (wasListening && !willListen) {
90
+ window.removeEventListener("mousedown", this._handleMouseDown);
91
+ window.removeEventListener("mouseup", this._handleMouseUp);
92
+ document.removeEventListener(
93
+ "selectionchange",
94
+ this._handleSelectionChange,
95
+ );
96
+
97
+ // Additionally, reset the state, to guard against errors where we
98
+ // re-enter listening mode and have stale values stored.
99
+ this.setState({
100
+ mouseState: "up",
101
+ trackedSelection: null,
102
+ });
103
+ }
104
+ }
105
+
106
+ /**
107
+ * Get the current selection focus and range, if present and non-collapsed.
108
+ *
109
+ * Otherwise, if there is no current selection or it's collapsed, return
110
+ * null.
111
+ */
112
+ _computeFocusAndRange(): ?{|
113
+ focusNode: Node,
114
+ focusOffset: number,
115
+ range: DOMRange,
116
+ |} {
117
+ const selection = document.getSelection();
118
+ if (!selection || selection.rangeCount === 0) {
119
+ return null;
120
+ }
121
+
122
+ const range = selection.getRangeAt(0);
123
+ if (range.collapsed) {
124
+ return null;
125
+ }
126
+
127
+ // NOTE(mdr): The focus node is guaranteed to exist, because
128
+ // there's a range, but the Flow type annotations for
129
+ // Selection don't know that. Cast it ourselves.
130
+ const focusNode: Node = (selection.focusNode: any);
131
+ const focusOffset = selection.focusOffset;
132
+ return {focusNode, focusOffset, range};
133
+ }
134
+
135
+ /**
136
+ * Compute the current TrackedSelection from the document state.
137
+ */
138
+ _computeTrackedSelection(
139
+ buildHighlight: (domRange: DOMRange) => ?DOMHighlight,
140
+ ): ?TrackedSelection {
141
+ const focusAndRange = this._computeFocusAndRange();
142
+ if (!focusAndRange) {
143
+ return null;
144
+ }
145
+
146
+ const {focusNode, focusOffset, range} = focusAndRange;
147
+ const proposedHighlight = buildHighlight(range);
148
+ if (!proposedHighlight) {
149
+ return null;
150
+ }
151
+
152
+ return {focusNode, focusOffset, proposedHighlight};
153
+ }
154
+
155
+ /**
156
+ * Update the TrackedSelection to reflect the document state.
157
+ */
158
+ _updateTrackedSelection(
159
+ buildHighlight: (domRange: DOMRange) => ?DOMHighlight,
160
+ ) {
161
+ const trackedSelection = this._computeTrackedSelection(buildHighlight);
162
+ this.setState({trackedSelection});
163
+ }
164
+
165
+ _handleSelectionChange: () => void = () => {
166
+ this._updateTrackedSelection(this.props.buildHighlight);
167
+
168
+ if (this.state.mouseState === "down") {
169
+ this.setState({
170
+ mouseState: "down-and-selecting",
171
+ });
172
+ }
173
+ };
174
+
175
+ _handleMouseDown: () => void = () => {
176
+ this.setState({mouseState: "down"});
177
+ };
178
+
179
+ _handleMouseUp: () => void = () => {
180
+ this.setState({mouseState: "up"});
181
+ };
182
+
183
+ render(): null | React.Element<"div"> {
184
+ const {mouseState, trackedSelection} = this.state;
185
+ const userIsMouseSelecting = mouseState === "down-and-selecting";
186
+
187
+ return this.props.children ? (
188
+ <div>
189
+ {this.props.children(trackedSelection, userIsMouseSelecting)}
190
+ </div>
191
+ ) : null;
192
+ }
193
+ }
194
+
195
+ export default SelectionTracker;
@@ -0,0 +1,21 @@
1
+ // @flow
2
+ // Import the DOM-related types from the parent directory, and re-export them
3
+ // to the UI code.
4
+ import type {DOMHighlight, DOMHighlightSet, DOMRange} from "../types.js";
5
+
6
+ export type {DOMHighlight, DOMHighlightSet, DOMRange};
7
+
8
+ export type Position = {|
9
+ left: number,
10
+ top: number,
11
+ |};
12
+
13
+ export type Rect = {|
14
+ ...Position,
15
+ width: number,
16
+ height: number,
17
+ |};
18
+
19
+ // TODO(mdr): Is this way of passing around `z-index: 0` actually worth the
20
+ // trouble? We used to have more, but now we don't need them...
21
+ export type ZIndexes = {belowContent: number, ...};
@@ -0,0 +1,166 @@
1
+ // @flow
2
+ /**
3
+ * Utility functions for highlighting UI.
4
+ */
5
+ import {rangesOverlap, intersectRanges} from "../ranges.js";
6
+
7
+ import type {DOMRange, Position, Rect} from "./types.js";
8
+
9
+ /**
10
+ * Given two positions relative to the same origin, return `child`'s position
11
+ * relative to `parent`'s position.
12
+ */
13
+ export function getRelativePosition(
14
+ child: Position,
15
+ parent: Position,
16
+ ): Position {
17
+ return {
18
+ left: child.left - parent.left,
19
+ top: child.top - parent.top,
20
+ };
21
+ }
22
+
23
+ /**
24
+ * `getRelativePosition`, but for Rects.
25
+ *
26
+ * Given a rectangle and a position relative to the same origin, return a new
27
+ * rectangle of the same size, whose position is relative to the given
28
+ * position.
29
+ */
30
+ export function getRelativeRect(
31
+ {left, top, width, height}: Rect,
32
+ parent: Position,
33
+ ): Rect {
34
+ return {
35
+ ...getRelativePosition({left, top}, parent),
36
+ width,
37
+ height,
38
+ };
39
+ }
40
+
41
+ /**
42
+ * Given a DOMRange, return a list of client-relative rects that cover the text
43
+ * inside the DOMRange.
44
+ *
45
+ * This behaves differently than the browser's built-in `getClientRects` method
46
+ * for DOMRanges, because this method only gets rects for the *text* nodes,
47
+ * whereas the browser's method will also return rects for any fully-contained
48
+ * *element* nodes.
49
+ *
50
+ * For example, if an entire paragraph is selected, then `getClientRects` will
51
+ * return a rectangle that covers the entire paragraph block, whereas this
52
+ * method will only return rectangles for the text inside the paragraph.
53
+ */
54
+ export function getClientRectsForTextInRange(
55
+ range: DOMRange,
56
+ ): $ReadOnlyArray<Rect> {
57
+ const mutableRects = [];
58
+ addClientRectsForTextInNodeAndRange(
59
+ range.commonAncestorContainer,
60
+ range,
61
+ mutableRects,
62
+ );
63
+ return mutableRects;
64
+ }
65
+
66
+ /**
67
+ * A helper method for getClientRectsForTextInRange.
68
+ *
69
+ * Given a node and a range, find text that is contained within both the node
70
+ * and the range by recursing down the node's subtree, and push the text's
71
+ * client rects onto the given `mutableRects` array.
72
+ */
73
+ function addClientRectsForTextInNodeAndRange(
74
+ node: Node,
75
+ range: DOMRange,
76
+ // eslint-disable-next-line flowtype/no-mutable-array
77
+ mutableRects: Array<Rect>,
78
+ ): void {
79
+ const nodeContentsRange = new Range();
80
+ nodeContentsRange.selectNodeContents(node);
81
+
82
+ if (node.nodeType === Node.TEXT_NODE) {
83
+ // Get the text that appears both in this text node and in the range
84
+ // we're searching.
85
+ const intersectedRange = intersectRanges(range, nodeContentsRange);
86
+ if (intersectedRange) {
87
+ addClientRectsForText(node, intersectedRange, mutableRects);
88
+ }
89
+ } else if (node.nodeType === Node.ELEMENT_NODE) {
90
+ // Don't bother deep-traversing a node that doesn't even overlap the
91
+ // range we're searching.
92
+ if (!rangesOverlap(range, nodeContentsRange)) {
93
+ return;
94
+ }
95
+
96
+ // NOTE(mdr): Safari returns a non-iterable `childNodes` object, so
97
+ // convert it to an array.
98
+ for (const child of Array.from(node.childNodes)) {
99
+ // TODO(mdr): If this parent has overflow: hidden, bound the child
100
+ // rects accordingly. This would allow us to remove the hack
101
+ // we added to .perseus-sr-only.
102
+ addClientRectsForTextInNodeAndRange(child, range, mutableRects);
103
+ }
104
+ }
105
+ }
106
+
107
+ /**
108
+ * Given a DOMRange of text fully contained by the given `textNode`, compute
109
+ * rectangles that visually cover the range, and push them onto the given
110
+ * `mutableRects` array.
111
+ *
112
+ * This function adjusts the rectangles to the line height of the text, to
113
+ * match the visual behavior of native text selection, and remove annoying gaps
114
+ * between lines that disrupt hover behavior.
115
+ */
116
+ function addClientRectsForText(
117
+ textNode: Node,
118
+ textRange: DOMRange,
119
+ // eslint-disable-next-line flowtype/no-mutable-array
120
+ mutableRects: Array<Rect>,
121
+ ): void {
122
+ const parentElement = textNode.parentElement;
123
+ const computedStyle = window.getComputedStyle(parentElement);
124
+
125
+ // NOTE(mdr): I'm not sure how computed line height works in all contexts
126
+ // in all browsers. It's valid to specify line height as a ratio,
127
+ // relative to font size. Latest Chrome seems to transform this into a
128
+ // px value, but maybe some browsers don't, so I'm being defensive here
129
+ // and only using the computed line height if it's explicitly a px
130
+ // value. We could also handle other browsers by computing font size
131
+ // and doing the math ourselves, but let's punt that until we need it.
132
+ let lineHeight = null;
133
+ if (
134
+ typeof computedStyle.lineHeight === "string" &&
135
+ computedStyle.lineHeight.endsWith("px")
136
+ ) {
137
+ const parsedLineHeight = parseFloat(computedStyle.lineHeight);
138
+ if (!isNaN(parsedLineHeight)) {
139
+ lineHeight = parsedLineHeight;
140
+ }
141
+ }
142
+
143
+ // NOTE(mdr): Safari returns a non-iterable `getClientRects` object, so
144
+ // convert it to an array.
145
+ const boundingRects = Array.from(textRange.getClientRects());
146
+
147
+ for (const boundingRect of boundingRects) {
148
+ const rect: Rect = {
149
+ left: boundingRect.left,
150
+ top: boundingRect.top,
151
+ height: boundingRect.height,
152
+ width: boundingRect.width,
153
+ };
154
+
155
+ // Adjust the rectangle according to the text's line height. It should
156
+ // keep the same bottom coordinate, so the top coordinate will move
157
+ // accordingly.
158
+ if (lineHeight !== null) {
159
+ const heightIncrease = lineHeight - rect.height;
160
+ rect.top -= heightIncrease;
161
+ rect.height = lineHeight;
162
+ }
163
+
164
+ mutableRects.push(rect);
165
+ }
166
+ }
@@ -0,0 +1,155 @@
1
+ // @flow
2
+ /**
3
+ * This component, whenever its content updates, traverses the DOM to find
4
+ * semantic words and send the corresponding DOMRanges to the caller via a
5
+ * callback.
6
+ *
7
+ * A "word" here is a sequence of non-whitespace text characters, uninterrupted
8
+ * by the start/end of a non-inline element. That is, a word can span across
9
+ * text nodes and inline tags like <b>, but cannot span across block
10
+ * paragraphs.
11
+ *
12
+ * The words yielded by WordIndexer are sorted in document order, which makes
13
+ * them amenable to binary search - see `findBoundaryWordIndex` in ranges.js.
14
+ */
15
+ import * as React from "react";
16
+
17
+ import {Errors} from "../../logging/log.js";
18
+ import {PerseusError} from "../../perseus-error.js";
19
+
20
+ import type {DOMRange} from "./types.js";
21
+
22
+ type WordIndexerProps = {|
23
+ // The content to display and traverse in search of words.
24
+ children?: React.Element<any>,
25
+ // After each mount and update, this callback is called with the list of
26
+ // word ranges in `children`'s DOM, sorted in document order.
27
+ onWordsUpdate: (wordRanges: $ReadOnlyArray<DOMRange>) => mixed,
28
+ |};
29
+
30
+ class WordIndexer extends React.PureComponent<WordIndexerProps> {
31
+ _container: ?HTMLElement;
32
+ /* eslint-enable react/sort-comp */
33
+
34
+ componentDidMount() {
35
+ this._sendWordsUpdate();
36
+ }
37
+
38
+ componentDidUpdate() {
39
+ this._sendWordsUpdate();
40
+ }
41
+
42
+ _sendWordsUpdate() {
43
+ this.props.onWordsUpdate(this._getWordRanges());
44
+ }
45
+
46
+ _getWordRanges(): $ReadOnlyArray<DOMRange> {
47
+ const container = this._container;
48
+
49
+ if (!container) {
50
+ throw new PerseusError(
51
+ "invariant violation: `this._container` is not defined, but " +
52
+ "this method is only called after mount/update",
53
+ Errors.NotAllowed,
54
+ );
55
+ }
56
+
57
+ // Create a TreeWalker that visits text nodes and element nodes.
58
+ // We select the types of nodes we're interested in by bitwise-ORing
59
+ // the desired filters together.
60
+ const treeWalker = container.ownerDocument.createTreeWalker(
61
+ container,
62
+ NodeFilter.SHOW_TEXT | NodeFilter.SHOW_ELEMENT,
63
+ () => NodeFilter.FILTER_ACCEPT,
64
+ false,
65
+ );
66
+
67
+ // Traverse the container's descendant text nodes in DOM order,
68
+ // indexing the words we find.
69
+ const index = [];
70
+ let trailingWordRange = null;
71
+ while (treeWalker.nextNode()) {
72
+ const node = treeWalker.currentNode;
73
+
74
+ if (node.nodeType === Node.ELEMENT_NODE) {
75
+ const element: Element = (node: any);
76
+ if (getComputedStyle(element).display !== "inline") {
77
+ // Block-level elements interrupt text words, so set the
78
+ // current trailing word to `null`.
79
+ //
80
+ // This includes exiting one paragraph to visit another,
81
+ // but also includes text interrupted by inline-block
82
+ // elements.
83
+ //
84
+ // TODO(mdr): Is this behavior *generalizably* correct?
85
+ // TODO(mdr): I don't *fully* understand the performance
86
+ // implications of `getComputedStyle`, but I think it's
87
+ // gonna be nbd at the current call sites. If we try to
88
+ // generalize to frequently-updating content, we might
89
+ // want to watch this as a potential performance
90
+ // bottleneck.
91
+ trailingWordRange = null;
92
+ }
93
+ } else if (node.nodeType === Node.TEXT_NODE) {
94
+ const text = node.nodeValue;
95
+
96
+ // A "word" is here simply defined as a maximal-length
97
+ // substring of non-space characters.
98
+ //
99
+ // Loop over all the words in the string, by repeatedly calling
100
+ // `wordPattern.exec(text)`.
101
+ const wordPattern = /\S+/g;
102
+ let wordMatch;
103
+ let newTrailingWordRange = null;
104
+ while ((wordMatch = wordPattern.exec(text)) !== null) {
105
+ // TODO(mdr): We found a new Flow error when upgrading:
106
+ // "index (Cannot get `wordMatch.index` because property `index` is missing in null [1].)"
107
+ // $FlowFixMe[incompatible-use](0.57.3->0.75.0)
108
+ const startOffset = wordMatch.index;
109
+ // TODO(mdr): We found a new Flow error when upgrading:
110
+ // "index (Cannot get `wordMatch.index` because property `index` is missing in null [1].)", "0 (Cannot get `wordMatch[0]` because an indexer property is missing in null [1].)"
111
+ // $FlowFixMe[incompatible-use](0.57.3->0.75.0)
112
+ const endOffset = wordMatch.index + wordMatch[0].length;
113
+
114
+ let range;
115
+ if (startOffset === 0 && trailingWordRange) {
116
+ // This word is at the start of this text node, and
117
+ // there's a trailing word from the previous text node,
118
+ // and there was no non-inline element between them.
119
+ // This word and the trailing word are probably
120
+ // intended to be two parts of the same word.
121
+ //
122
+ // So, instead of creating a new word range, we add
123
+ // this word to the previous range by extending it.
124
+ range = trailingWordRange;
125
+ range.setEnd(node, endOffset);
126
+ } else {
127
+ // Otherwise, start a new word range here, and add it
128
+ // to the list.
129
+ range = node.ownerDocument.createRange();
130
+ range.setStart(node, startOffset);
131
+ range.setEnd(node, endOffset);
132
+ index.push(range);
133
+ }
134
+
135
+ if (endOffset === text.length) {
136
+ newTrailingWordRange = range;
137
+ }
138
+ }
139
+ trailingWordRange = newTrailingWordRange;
140
+ }
141
+ }
142
+
143
+ return index;
144
+ }
145
+
146
+ render(): React.Element<"div"> {
147
+ return (
148
+ <div ref={(container) => (this._container = container)}>
149
+ {this.props.children}
150
+ </div>
151
+ );
152
+ }
153
+ }
154
+
155
+ export default WordIndexer;