@eightyfourthousand/lib-editing 2026.3.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 (304) hide show
  1. package/.babelrc +12 -0
  2. package/.eslintrc.json +18 -0
  3. package/README.md +7 -0
  4. package/jest.config.ts +10 -0
  5. package/package.json +35 -0
  6. package/postcss.config.mjs +9 -0
  7. package/project.json +29 -0
  8. package/src/fixtures/basic/json.ts +396 -0
  9. package/src/fixtures/toh251/json.ts +4913 -0
  10. package/src/fixtures/toh251/passages.ts +2814 -0
  11. package/src/fixtures/types.ts +8 -0
  12. package/src/index.ts +4 -0
  13. package/src/lib/block.ts +226 -0
  14. package/src/lib/components/editor/BlockEditor.tsx +38 -0
  15. package/src/lib/components/editor/EditorBackMatterPage.tsx +95 -0
  16. package/src/lib/components/editor/EditorBodyPage.tsx +87 -0
  17. package/src/lib/components/editor/EditorHeader.tsx +22 -0
  18. package/src/lib/components/editor/EditorLayout.tsx +62 -0
  19. package/src/lib/components/editor/EditorLeftPanelPage.tsx +27 -0
  20. package/src/lib/components/editor/EditorProvider.tsx +399 -0
  21. package/src/lib/components/editor/PaginationProvider.tsx +472 -0
  22. package/src/lib/components/editor/TitlesBuilder.tsx +40 -0
  23. package/src/lib/components/editor/TranslationBuilder.tsx +171 -0
  24. package/src/lib/components/editor/TranslationEditor.tsx +32 -0
  25. package/src/lib/components/editor/extensions/Abbreviation/Abbreviation.ts +133 -0
  26. package/src/lib/components/editor/extensions/Audio/Audio.ts +69 -0
  27. package/src/lib/components/editor/extensions/Bold.ts +43 -0
  28. package/src/lib/components/editor/extensions/Document.ts +8 -0
  29. package/src/lib/components/editor/extensions/DragHandle/DragHandle.ts +429 -0
  30. package/src/lib/components/editor/extensions/EndNoteLink/EndNoteLink.tsx +39 -0
  31. package/src/lib/components/editor/extensions/EndNoteLink/EndNoteLinkHoverContent.tsx +139 -0
  32. package/src/lib/components/editor/extensions/EndNoteLink/EndNoteLinkMark.ts +236 -0
  33. package/src/lib/components/editor/extensions/EndNoteLink/endnote-utils.ts +412 -0
  34. package/src/lib/components/editor/extensions/GlobalConfig.ts +52 -0
  35. package/src/lib/components/editor/extensions/GlossaryInstance/GlossaryInput.tsx +54 -0
  36. package/src/lib/components/editor/extensions/GlossaryInstance/GlossaryInstance.tsx +129 -0
  37. package/src/lib/components/editor/extensions/GlossaryInstance/GlossaryInstanceNode.ts +148 -0
  38. package/src/lib/components/editor/extensions/Heading/Heading.ts +71 -0
  39. package/src/lib/components/editor/extensions/HoverInputField.tsx +54 -0
  40. package/src/lib/components/editor/extensions/Image.ts +18 -0
  41. package/src/lib/components/editor/extensions/Indent.ts +103 -0
  42. package/src/lib/components/editor/extensions/InternalLink/InternalLink.ts +173 -0
  43. package/src/lib/components/editor/extensions/InternalLink/InternalLinkHoverContent.tsx +137 -0
  44. package/src/lib/components/editor/extensions/InternalLink/InternalLinkInput.tsx +71 -0
  45. package/src/lib/components/editor/extensions/InternalLink/index.ts +1 -0
  46. package/src/lib/components/editor/extensions/Italic.ts +50 -0
  47. package/src/lib/components/editor/extensions/LeadingSpace.ts +106 -0
  48. package/src/lib/components/editor/extensions/Line/LineNode.ts +41 -0
  49. package/src/lib/components/editor/extensions/LineGroup/LineGroupNode.ts +124 -0
  50. package/src/lib/components/editor/extensions/Link/Link.ts +65 -0
  51. package/src/lib/components/editor/extensions/Link/LinkHoverContent.tsx +124 -0
  52. package/src/lib/components/editor/extensions/Link/index.ts +1 -0
  53. package/src/lib/components/editor/extensions/List.ts +74 -0
  54. package/src/lib/components/editor/extensions/Mantra/Mantra.ts +88 -0
  55. package/src/lib/components/editor/extensions/Mention/Mention.ts +184 -0
  56. package/src/lib/components/editor/extensions/Mention/MentionHoverContent.tsx +158 -0
  57. package/src/lib/components/editor/extensions/NodeWrapper.tsx +57 -0
  58. package/src/lib/components/editor/extensions/Paragraph/Paragraph.ts +25 -0
  59. package/src/lib/components/editor/extensions/ParagraphIndent.ts +87 -0
  60. package/src/lib/components/editor/extensions/Passage/EditLabel.tsx +57 -0
  61. package/src/lib/components/editor/extensions/Passage/EditorOptions.tsx +29 -0
  62. package/src/lib/components/editor/extensions/Passage/Passage.tsx +238 -0
  63. package/src/lib/components/editor/extensions/Passage/PassageNode.ts +223 -0
  64. package/src/lib/components/editor/extensions/Passage/ReaderOptions.tsx +55 -0
  65. package/src/lib/components/editor/extensions/Passage/ShowAnnotations.tsx +92 -0
  66. package/src/lib/components/editor/extensions/Passage/index.ts +1 -0
  67. package/src/lib/components/editor/extensions/Passage/label.spec.ts +118 -0
  68. package/src/lib/components/editor/extensions/Passage/label.ts +39 -0
  69. package/src/lib/components/editor/extensions/Placeholder.ts +9 -0
  70. package/src/lib/components/editor/extensions/SlashCommand/SlashCommand.ts +65 -0
  71. package/src/lib/components/editor/extensions/SlashCommand/SuggestionList.tsx +109 -0
  72. package/src/lib/components/editor/extensions/SlashCommand/Suggestions.ts +185 -0
  73. package/src/lib/components/editor/extensions/SmallCaps.ts +110 -0
  74. package/src/lib/components/editor/extensions/StarterKit.ts +36 -0
  75. package/src/lib/components/editor/extensions/Subscript.ts +43 -0
  76. package/src/lib/components/editor/extensions/Superscript.ts +43 -0
  77. package/src/lib/components/editor/extensions/Table.ts +32 -0
  78. package/src/lib/components/editor/extensions/TextAlign.ts +5 -0
  79. package/src/lib/components/editor/extensions/TitleMetadata.ts +40 -0
  80. package/src/lib/components/editor/extensions/TitleNode.ts +133 -0
  81. package/src/lib/components/editor/extensions/TitlesNode.ts +102 -0
  82. package/src/lib/components/editor/extensions/Trailer.ts +57 -0
  83. package/src/lib/components/editor/extensions/TranslationDocument.ts +7 -0
  84. package/src/lib/components/editor/extensions/TranslationMetadata.ts +58 -0
  85. package/src/lib/components/editor/extensions/Underline.ts +43 -0
  86. package/src/lib/components/editor/hooks/index.ts +4 -0
  87. package/src/lib/components/editor/hooks/useBlockEditor.ts +53 -0
  88. package/src/lib/components/editor/hooks/useDefaultExtensions.ts +39 -0
  89. package/src/lib/components/editor/hooks/useDirtyStore.ts +33 -0
  90. package/src/lib/components/editor/hooks/useTranslationExtensions.ts +148 -0
  91. package/src/lib/components/editor/index.ts +10 -0
  92. package/src/lib/components/editor/menus/EmptyBubbleMenu.tsx +42 -0
  93. package/src/lib/components/editor/menus/MainBubbleMenu.tsx +51 -0
  94. package/src/lib/components/editor/menus/TranslationBubbleMenu.tsx +57 -0
  95. package/src/lib/components/editor/menus/index.ts +3 -0
  96. package/src/lib/components/editor/menus/selectors/EndNoteSelector.tsx +388 -0
  97. package/src/lib/components/editor/menus/selectors/GlossarySelector.tsx +63 -0
  98. package/src/lib/components/editor/menus/selectors/LinkSelector.tsx +68 -0
  99. package/src/lib/components/editor/menus/selectors/MantraSelector.tsx +119 -0
  100. package/src/lib/components/editor/menus/selectors/NodeSelector.tsx +144 -0
  101. package/src/lib/components/editor/menus/selectors/ParagraphButtons.tsx +68 -0
  102. package/src/lib/components/editor/menus/selectors/SelectorInputField.tsx +68 -0
  103. package/src/lib/components/editor/menus/selectors/TextAlignSelector.tsx +89 -0
  104. package/src/lib/components/editor/menus/selectors/TextButtons.tsx +89 -0
  105. package/src/lib/components/editor/menus/selectors/TranslationNodeSelector.tsx +143 -0
  106. package/src/lib/components/editor/menus/selectors/TranslationTextButtons.tsx +125 -0
  107. package/src/lib/components/editor/menus/selectors/index.ts +5 -0
  108. package/src/lib/components/editor/save-filter.spec.ts +94 -0
  109. package/src/lib/components/editor/save-filter.ts +27 -0
  110. package/src/lib/components/editor/util.ts +304 -0
  111. package/src/lib/components/index.ts +3 -0
  112. package/src/lib/components/reader/ReaderBackMatterPage.tsx +62 -0
  113. package/src/lib/components/reader/ReaderBackMatterPanel.tsx +53 -0
  114. package/src/lib/components/reader/ReaderBodyPage.tsx +46 -0
  115. package/src/lib/components/reader/ReaderBodyPanel.tsx +68 -0
  116. package/src/lib/components/reader/ReaderLayout.tsx +39 -0
  117. package/src/lib/components/reader/ReaderLeftPanel.tsx +8 -0
  118. package/src/lib/components/reader/ReaderLeftPanelPage.tsx +31 -0
  119. package/src/lib/components/reader/TranslationReader.tsx +28 -0
  120. package/src/lib/components/reader/index.ts +2 -0
  121. package/src/lib/components/reader/ssr.ts +3 -0
  122. package/src/lib/components/shared/AiSummarizerPage.tsx +12 -0
  123. package/src/lib/components/shared/BackMatterPanel.tsx +143 -0
  124. package/src/lib/components/shared/BodyPanel.tsx +214 -0
  125. package/src/lib/components/shared/HoverCardProvider.tsx +407 -0
  126. package/src/lib/components/shared/Imprint.tsx +24 -0
  127. package/src/lib/components/shared/LabeledElement.tsx +133 -0
  128. package/src/lib/components/shared/LeftPanel.tsx +65 -0
  129. package/src/lib/components/shared/NavigationContext.ts +64 -0
  130. package/src/lib/components/shared/NavigationProvider.tsx +368 -0
  131. package/src/lib/components/shared/OpenGraphImage.tsx +75 -0
  132. package/src/lib/components/shared/PassageSkeleton.tsx +10 -0
  133. package/src/lib/components/shared/RestrictionWarning.tsx +177 -0
  134. package/src/lib/components/shared/SourceReader.tsx +83 -0
  135. package/src/lib/components/shared/SuggestRevisionForm.tsx +99 -0
  136. package/src/lib/components/shared/TableOfContents.tsx +280 -0
  137. package/src/lib/components/shared/ThreeColumnRenderer.tsx +54 -0
  138. package/src/lib/components/shared/TranslationHeader.tsx +86 -0
  139. package/src/lib/components/shared/TranslationHoverCard.tsx +84 -0
  140. package/src/lib/components/shared/TranslationSkeleton.tsx +16 -0
  141. package/src/lib/components/shared/TranslationTable.tsx +155 -0
  142. package/src/lib/components/shared/bibliography/BibliographyBody.tsx +28 -0
  143. package/src/lib/components/shared/bibliography/BibliographyList.tsx +63 -0
  144. package/src/lib/components/shared/bibliography/index.ts +1 -0
  145. package/src/lib/components/shared/generate-metadata.ts +44 -0
  146. package/src/lib/components/shared/glossary/GlossaryInstanceBody.tsx +144 -0
  147. package/src/lib/components/shared/glossary/GlossaryPaginationProvider.tsx +317 -0
  148. package/src/lib/components/shared/glossary/GlossarySkeleton.tsx +19 -0
  149. package/src/lib/components/shared/glossary/GlossaryTermList.tsx +58 -0
  150. package/src/lib/components/shared/glossary/index.ts +3 -0
  151. package/src/lib/components/shared/hooks/useGlossaryInstanceListener.tsx +42 -0
  152. package/src/lib/components/shared/hooks/useScrollInTab.tsx +43 -0
  153. package/src/lib/components/shared/hooks/useScrollPositionRestore.ts +274 -0
  154. package/src/lib/components/shared/hooks/useTohToggle.tsx +52 -0
  155. package/src/lib/components/shared/index.ts +11 -0
  156. package/src/lib/components/shared/ssr.ts +2 -0
  157. package/src/lib/components/shared/titles/FramedCard.tsx +132 -0
  158. package/src/lib/components/shared/titles/LongTitle.tsx +20 -0
  159. package/src/lib/components/shared/titles/LongTitles.tsx +28 -0
  160. package/src/lib/components/shared/titles/Title.tsx +54 -0
  161. package/src/lib/components/shared/titles/TitleDetails.tsx +47 -0
  162. package/src/lib/components/shared/titles/TitleForm.tsx +37 -0
  163. package/src/lib/components/shared/titles/Titles.tsx +114 -0
  164. package/src/lib/components/shared/titles/TitlesCard.tsx +113 -0
  165. package/src/lib/components/shared/titles/index.ts +8 -0
  166. package/src/lib/components/shared/types.ts +79 -0
  167. package/src/lib/components/ssr.ts +2 -0
  168. package/src/lib/exporters/abbreviation.spec.ts +31 -0
  169. package/src/lib/exporters/abbreviation.ts +22 -0
  170. package/src/lib/exporters/annotation.ts +193 -0
  171. package/src/lib/exporters/audio.spec.ts +77 -0
  172. package/src/lib/exporters/audio.ts +27 -0
  173. package/src/lib/exporters/blockquote.spec.ts +48 -0
  174. package/src/lib/exporters/blockquote.ts +24 -0
  175. package/src/lib/exporters/code.spec.ts +93 -0
  176. package/src/lib/exporters/code.ts +26 -0
  177. package/src/lib/exporters/end-note-link.spec.ts +104 -0
  178. package/src/lib/exporters/end-note-link.ts +35 -0
  179. package/src/lib/exporters/export.ts +12 -0
  180. package/src/lib/exporters/glossary-instance.spec.ts +85 -0
  181. package/src/lib/exporters/glossary-instance.ts +31 -0
  182. package/src/lib/exporters/has-abbreviation.spec.ts +31 -0
  183. package/src/lib/exporters/has-abbreviation.ts +21 -0
  184. package/src/lib/exporters/heading.spec.ts +80 -0
  185. package/src/lib/exporters/heading.ts +28 -0
  186. package/src/lib/exporters/image.spec.ts +48 -0
  187. package/src/lib/exporters/image.ts +25 -0
  188. package/src/lib/exporters/indent.spec.ts +58 -0
  189. package/src/lib/exporters/indent.ts +18 -0
  190. package/src/lib/exporters/index.ts +1 -0
  191. package/src/lib/exporters/internal-link.spec.ts +90 -0
  192. package/src/lib/exporters/internal-link.ts +35 -0
  193. package/src/lib/exporters/italic.spec.ts +84 -0
  194. package/src/lib/exporters/italic.ts +55 -0
  195. package/src/lib/exporters/leading-space.spec.ts +28 -0
  196. package/src/lib/exporters/leading-space.ts +16 -0
  197. package/src/lib/exporters/line-group.spec.ts +48 -0
  198. package/src/lib/exporters/line-group.ts +24 -0
  199. package/src/lib/exporters/line.spec.ts +48 -0
  200. package/src/lib/exporters/line.ts +24 -0
  201. package/src/lib/exporters/link.spec.ts +123 -0
  202. package/src/lib/exporters/link.ts +67 -0
  203. package/src/lib/exporters/list-item.spec.ts +48 -0
  204. package/src/lib/exporters/list-item.ts +24 -0
  205. package/src/lib/exporters/list.spec.ts +82 -0
  206. package/src/lib/exporters/list.ts +31 -0
  207. package/src/lib/exporters/mantra.spec.ts +51 -0
  208. package/src/lib/exporters/mantra.ts +25 -0
  209. package/src/lib/exporters/mention.ts +41 -0
  210. package/src/lib/exporters/paragraph.spec.ts +173 -0
  211. package/src/lib/exporters/paragraph.ts +32 -0
  212. package/src/lib/exporters/quote.spec.ts +56 -0
  213. package/src/lib/exporters/quote.ts +25 -0
  214. package/src/lib/exporters/span.spec.ts +118 -0
  215. package/src/lib/exporters/span.ts +44 -0
  216. package/src/lib/exporters/table-body-data.spec.ts +48 -0
  217. package/src/lib/exporters/table-body-data.ts +24 -0
  218. package/src/lib/exporters/table-body-header.spec.ts +48 -0
  219. package/src/lib/exporters/table-body-header.ts +24 -0
  220. package/src/lib/exporters/table-body-row.spec.ts +48 -0
  221. package/src/lib/exporters/table-body-row.ts +24 -0
  222. package/src/lib/exporters/table.spec.ts +48 -0
  223. package/src/lib/exporters/table.ts +24 -0
  224. package/src/lib/exporters/trailer.spec.ts +48 -0
  225. package/src/lib/exporters/trailer.ts +24 -0
  226. package/src/lib/exporters/util.ts +62 -0
  227. package/src/lib/passage.ts +182 -0
  228. package/src/lib/titles.ts +80 -0
  229. package/src/lib/transformers/abbreviation.spec.ts +87 -0
  230. package/src/lib/transformers/abbreviation.ts +30 -0
  231. package/src/lib/transformers/annotate.ts +146 -0
  232. package/src/lib/transformers/audio.spec.ts +55 -0
  233. package/src/lib/transformers/audio.ts +29 -0
  234. package/src/lib/transformers/blockquote.spec.ts +48 -0
  235. package/src/lib/transformers/blockquote.ts +41 -0
  236. package/src/lib/transformers/code.spec.ts +52 -0
  237. package/src/lib/transformers/code.ts +22 -0
  238. package/src/lib/transformers/deprecated.ts +7 -0
  239. package/src/lib/transformers/end-note-link.spec.ts +56 -0
  240. package/src/lib/transformers/end-note-link.ts +76 -0
  241. package/src/lib/transformers/glossary-instance.spec.ts +55 -0
  242. package/src/lib/transformers/glossary-instance.ts +40 -0
  243. package/src/lib/transformers/has-abbreviation.spec.ts +50 -0
  244. package/src/lib/transformers/has-abbreviation.ts +30 -0
  245. package/src/lib/transformers/heading.spec.ts +62 -0
  246. package/src/lib/transformers/heading.ts +30 -0
  247. package/src/lib/transformers/image.spec.ts +51 -0
  248. package/src/lib/transformers/image.ts +28 -0
  249. package/src/lib/transformers/indent.spec.ts +53 -0
  250. package/src/lib/transformers/indent.ts +17 -0
  251. package/src/lib/transformers/index.ts +33 -0
  252. package/src/lib/transformers/inline-title.spec.ts +59 -0
  253. package/src/lib/transformers/inline-title.ts +34 -0
  254. package/src/lib/transformers/internal-link.spec.ts +67 -0
  255. package/src/lib/transformers/internal-link.ts +65 -0
  256. package/src/lib/transformers/italic.ts +22 -0
  257. package/src/lib/transformers/leading-space.spec.ts +55 -0
  258. package/src/lib/transformers/leading-space.ts +17 -0
  259. package/src/lib/transformers/line-group.spec.ts +48 -0
  260. package/src/lib/transformers/line-group.ts +37 -0
  261. package/src/lib/transformers/line.spec.ts +54 -0
  262. package/src/lib/transformers/line.ts +27 -0
  263. package/src/lib/transformers/link.spec.ts +61 -0
  264. package/src/lib/transformers/link.ts +27 -0
  265. package/src/lib/transformers/list-item.spec.ts +48 -0
  266. package/src/lib/transformers/list-item.ts +37 -0
  267. package/src/lib/transformers/list.spec.ts +58 -0
  268. package/src/lib/transformers/list.ts +42 -0
  269. package/src/lib/transformers/mantra.spec.ts +58 -0
  270. package/src/lib/transformers/mantra.ts +28 -0
  271. package/src/lib/transformers/mention.ts +70 -0
  272. package/src/lib/transformers/paragraph.spec.ts +46 -0
  273. package/src/lib/transformers/paragraph.ts +26 -0
  274. package/src/lib/transformers/quote.spec.ts +42 -0
  275. package/src/lib/transformers/quote.ts +3 -0
  276. package/src/lib/transformers/quoted.ts +3 -0
  277. package/src/lib/transformers/recurse.ts +76 -0
  278. package/src/lib/transformers/reference.ts +3 -0
  279. package/src/lib/transformers/span.spec.ts +68 -0
  280. package/src/lib/transformers/span.ts +78 -0
  281. package/src/lib/transformers/split-at.ts +58 -0
  282. package/src/lib/transformers/split-block.ts +110 -0
  283. package/src/lib/transformers/split-content.ts +67 -0
  284. package/src/lib/transformers/split-insert.ts +76 -0
  285. package/src/lib/transformers/split-marks.ts +42 -0
  286. package/src/lib/transformers/split-node.ts +138 -0
  287. package/src/lib/transformers/table-body-data.spec.ts +44 -0
  288. package/src/lib/transformers/table-body-data.ts +29 -0
  289. package/src/lib/transformers/table-body-header.spec.ts +44 -0
  290. package/src/lib/transformers/table-body-header.ts +29 -0
  291. package/src/lib/transformers/table-body-row.spec.ts +44 -0
  292. package/src/lib/transformers/table-body-row.ts +29 -0
  293. package/src/lib/transformers/table.spec.ts +47 -0
  294. package/src/lib/transformers/table.ts +29 -0
  295. package/src/lib/transformers/trailer.spec.ts +43 -0
  296. package/src/lib/transformers/trailer.ts +26 -0
  297. package/src/lib/transformers/transformer.ts +25 -0
  298. package/src/lib/transformers/unknown.ts +8 -0
  299. package/src/lib/transformers/util.ts +20 -0
  300. package/src/lib/types.ts +10 -0
  301. package/src/ssr.ts +1 -0
  302. package/tsconfig.json +20 -0
  303. package/tsconfig.lib.json +29 -0
  304. package/tsconfig.spec.json +22 -0
@@ -0,0 +1,182 @@
1
+ import { Editor } from '@tiptap/react';
2
+ import { Node } from '@tiptap/pm/model';
3
+ import { v4 as uuidv4 } from 'uuid';
4
+ import {
5
+ annotationExportsFromNode,
6
+ markAnnotationFromNode,
7
+ parameterAnnotationFromNode,
8
+ } from './exporters';
9
+ import { findNodePosition, nodeNotFound } from './exporters/util';
10
+ import { ExporterContext } from './exporters/export';
11
+ import { Passage } from '@eightyfourthousand/data-access';
12
+
13
+ /**
14
+ * Ensures every node in the editor document has a unique, non-null UUID.
15
+ *
16
+ * UUID assignment normally happens asynchronously in NodeView lifecycle hooks
17
+ * (validateAttrs / createNodeViewDom). When save() is called shortly after a
18
+ * structural edit (e.g. pressing Enter to create a new paragraph, or splitting
19
+ * a passage), some nodes may still carry uuid: null or a UUID duplicated from
20
+ * the node they were split from. passagesFromNodes() then reads those nodes
21
+ * synchronously, and annotationExportsFromNode silently skips any node whose
22
+ * uuid is falsy — producing missing paragraph annotations.
23
+ *
24
+ * This function walks the document synchronously and dispatches a single
25
+ * transaction that stamps a fresh UUID onto every node or mark that either:
26
+ * 1. has uuid: null / undefined / empty, OR
27
+ * 2. duplicates the UUID of another node/mark already seen in this walk.
28
+ *
29
+ * Block nodes are updated via tr.setNodeMarkup. Inline marks (Link,
30
+ * InternalLink, GlossaryInstance) carry their own uuid attribute and are
31
+ * updated via tr.removeMark + tr.addMark, since setNodeMarkup does not apply
32
+ * to marks. This prevents duplicate annotation UUIDs when a passage is split
33
+ * and the afterContent inherits copies of the original marks.
34
+ *
35
+ * It must be called (and awaited via a Promise.resolve()) before
36
+ * passagesFromNodes() so that the transaction is committed to the editor state.
37
+ */
38
+ // Parameter annotation UUID attributes that live on block node attrs (not marks)
39
+ // and must be deduplicated alongside the main `uuid` attribute.
40
+ const PARAMETER_UUID_ATTRS = ['leadingSpaceUuid', 'indentUuid'] as const;
41
+
42
+ export const ensureUuids = (editor: Editor): void => {
43
+ const { state, view } = editor;
44
+ const { doc, tr } = state;
45
+ const seen = new Set<string>();
46
+ let changed = false;
47
+
48
+ doc.descendants((node, pos) => {
49
+ // text and doc nodes never carry a uuid
50
+ if (node.type.name === 'text' || node.type.name === 'doc') {
51
+ return true;
52
+ }
53
+
54
+ let newAttrs: Record<string, unknown> | null = null;
55
+
56
+ // Check main uuid
57
+ const existing: string | null | undefined = node.attrs.uuid;
58
+ if (!existing || seen.has(existing)) {
59
+ newAttrs = { ...node.attrs, uuid: uuidv4() };
60
+ seen.add(newAttrs.uuid as string);
61
+ } else {
62
+ seen.add(existing);
63
+ }
64
+
65
+ // Check parameter annotation UUIDs
66
+ for (const attrKey of PARAMETER_UUID_ATTRS) {
67
+ const existingParam = node.attrs[attrKey] as string | null | undefined;
68
+ if (existingParam === undefined) continue; // node type doesn't use this attr
69
+ if (!existingParam || seen.has(existingParam)) {
70
+ newAttrs = { ...(newAttrs ?? node.attrs), [attrKey]: uuidv4() };
71
+ seen.add(newAttrs[attrKey] as string);
72
+ } else {
73
+ seen.add(existingParam);
74
+ }
75
+ }
76
+
77
+ if (newAttrs) {
78
+ tr.setNodeMarkup(pos, undefined, newAttrs);
79
+ changed = true;
80
+ }
81
+
82
+ return true;
83
+ });
84
+
85
+ // Second pass: walk text nodes and fix marks that carry a uuid attribute
86
+ // (Link, InternalLink, GlossaryInstance). doc.descendants does not expose
87
+ // marks directly, so we must walk text nodes to find them.
88
+ doc.descendants((node, pos) => {
89
+ if (node.type.name !== 'text' || node.marks.length === 0) return true;
90
+
91
+ for (const mark of node.marks) {
92
+ const existing: string | null | undefined = mark.attrs.uuid;
93
+ if (existing === undefined) continue; // mark type has no uuid attribute
94
+
95
+ const isDuplicate = existing ? seen.has(existing) : false;
96
+ if (!existing || isDuplicate) {
97
+ const newUuid = uuidv4();
98
+ const newMark = mark.type.create({ ...mark.attrs, uuid: newUuid });
99
+ // pos is the start of the text node; the mark spans the full text node
100
+ tr.removeMark(pos, pos + node.nodeSize, mark.type);
101
+ tr.addMark(pos, pos + node.nodeSize, newMark);
102
+ seen.add(newUuid);
103
+ changed = true;
104
+ } else {
105
+ seen.add(existing);
106
+ }
107
+ }
108
+
109
+ return true;
110
+ });
111
+
112
+ if (changed) {
113
+ view.dispatch(tr);
114
+ }
115
+ };
116
+
117
+ export const passageFromNode = (node: Node, workUuid: string): Passage => {
118
+ const uuid = node.attrs.uuid;
119
+ const type = node.attrs.type;
120
+ const toh = node.attrs.toh;
121
+
122
+ const ctx: ExporterContext = {
123
+ passageUuid: uuid,
124
+ node,
125
+ parent: node,
126
+ root: node,
127
+ start: 0,
128
+ };
129
+ const annotations = [
130
+ ...parameterAnnotationFromNode(ctx),
131
+ ...markAnnotationFromNode(ctx),
132
+ ];
133
+ node.content.forEach((child) => {
134
+ const start = findNodePosition(node, child.attrs.uuid, child.type.name);
135
+ if (start === undefined) {
136
+ return nodeNotFound(child);
137
+ }
138
+ annotations.push(
139
+ ...annotationExportsFromNode({
140
+ passageUuid: uuid,
141
+ node: child,
142
+ parent: node,
143
+ root: node,
144
+ start,
145
+ }),
146
+ );
147
+ });
148
+
149
+ const passage: Passage = {
150
+ uuid,
151
+ type,
152
+ workUuid,
153
+ sort: node.attrs.sort,
154
+ label: node.attrs.label,
155
+ content: node.textContent,
156
+ toh,
157
+ annotations,
158
+ };
159
+ return passage;
160
+ };
161
+
162
+ export const passagesFromNodes = ({
163
+ uuids,
164
+ workUuid,
165
+ editor,
166
+ }: {
167
+ uuids: string[];
168
+ workUuid: string;
169
+ editor: Editor;
170
+ }): Passage[] => {
171
+ const passages: Passage[] = [];
172
+ uuids.forEach((uuid) => {
173
+ const node = editor.$node('passage', { uuid });
174
+ if (!node) {
175
+ console.warn(`No passage node found for uuid: ${uuid}`);
176
+ return;
177
+ }
178
+
179
+ passages.push(passageFromNode(node.node, workUuid));
180
+ });
181
+ return passages;
182
+ };
@@ -0,0 +1,80 @@
1
+ import {
2
+ type ExtendedTranslationLanguage,
3
+ TITLE_TYPES,
4
+ type Titles,
5
+ type TitleType,
6
+ } from '@eightyfourthousand/data-access';
7
+ import type { BlockEditorContent } from './components';
8
+
9
+ const TYPE_FOR_LANGUAGE: Record<ExtendedTranslationLanguage | 'toh', string> = {
10
+ toh: 'tohTitle',
11
+ bo: 'boTitle',
12
+ en: 'enTitle',
13
+ ja: 'jaTitle',
14
+ zh: 'zhTitle',
15
+ 'Bo-Ltn': 'boLtnTitle',
16
+ 'Mt-Ltn': 'mtLtnTitle',
17
+ 'Pi-Ltn': 'piLtnTitle',
18
+ 'Sa-Ltn': 'saLtnTitle',
19
+ 'Zh-Ltn': 'zhLtnTitle',
20
+ };
21
+
22
+ const TITLES_TYPE_TO_NODE_TYPE: Record<TitleType, string> = {
23
+ toh: 'tohs',
24
+ mainTitle: 'mainTitles',
25
+ mainTitleOutsideCatalogueSection: 'alternateMainTitles',
26
+ longTitle: 'longTitles',
27
+ otherTitle: 'otherTitles',
28
+ shortcode: 'shortCodes',
29
+ };
30
+
31
+ export const titlesToDocument = (titles: Titles): BlockEditorContent => {
32
+ const content: BlockEditorContent = {
33
+ type: 'titles',
34
+ content: [],
35
+ };
36
+
37
+ const titlesByType: Partial<Record<TitleType, Titles>> = {};
38
+ titles.forEach((title) => {
39
+ if (!titlesByType[title.type]) {
40
+ titlesByType[title.type] = [];
41
+ }
42
+ titlesByType[title.type]?.push(title);
43
+ });
44
+
45
+ // NOTE: the imported list is already in preferred order
46
+ TITLE_TYPES.forEach((type) => {
47
+ const titlesOfType = titlesByType[type];
48
+ if (titlesOfType?.length) {
49
+ const orderedTitles = [
50
+ ...titlesOfType.filter((title) => title.language === 'bo'),
51
+ ...titlesOfType.filter((title) => title.language === 'en'),
52
+ ...titlesOfType.filter((title) => title.language === 'Bo-Ltn'),
53
+ ...titlesOfType.filter((title) => title.language === 'Sa-Ltn'),
54
+ ...titlesOfType.filter((title) => title.language === 'Pi-Ltn'),
55
+ ...titlesOfType.filter((title) => title.language === 'zh'),
56
+ ...titlesOfType.filter((title) => title.language === 'ja'),
57
+ ];
58
+
59
+ content.content?.push({
60
+ type: TITLES_TYPE_TO_NODE_TYPE[type],
61
+ content: orderedTitles.map((title) => ({
62
+ type: TYPE_FOR_LANGUAGE[title.language],
63
+ attrs: {
64
+ language: title.language,
65
+ uuid: title.uuid,
66
+ type: title.type,
67
+ },
68
+ content: [
69
+ {
70
+ type: 'text',
71
+ text: title.title,
72
+ },
73
+ ],
74
+ })),
75
+ });
76
+ }
77
+ });
78
+
79
+ return content;
80
+ };
@@ -0,0 +1,87 @@
1
+ import {
2
+ Annotation,
3
+ annotationsFromDTO,
4
+ PassageDTO,
5
+ passageFromDTO,
6
+ } from '@eightyfourthousand/data-access';
7
+ import { abbreviation } from './abbreviation';
8
+ import { blockFromPassage } from '../block';
9
+ import { recurseForType } from './recurse';
10
+
11
+ const dto: PassageDTO = {
12
+ sort: 13140,
13
+ type: 'abbreviations',
14
+ uuid: 'e350ec6c-a40c-46bf-8182-acba5a50e4fe',
15
+ label: '',
16
+ xmlId: 'UT22084-001-001-2226',
17
+ parent: 'UT22084-001-001-notes-1',
18
+ content: 'C Choné',
19
+ work_uuid: '092e9a3c-12a6-4e1c-a05b-16835c0e62a8',
20
+ annotations: [
21
+ {
22
+ end: 2,
23
+ type: 'abbreviation',
24
+ uuid: '49c41c6d-0e04-412e-81ad-22f3af377470',
25
+ start: 0,
26
+ content: [
27
+ {
28
+ uuid: 'e350ec6c-a40c-46bf-8182-acba5a50e4fe',
29
+ },
30
+ ],
31
+ passage_uuid: 'e350ec6c-a40c-46bf-8182-acba5a50e4fe',
32
+ },
33
+ {
34
+ end: 7,
35
+ type: 'has-abbreviation',
36
+ uuid: '1e5003e8-e015-4b94-b88c-80b39c263098',
37
+ start: 2,
38
+ content: [
39
+ {
40
+ uuid: 'e350ec6c-a40c-46bf-8182-acba5a50e4fe',
41
+ },
42
+ ],
43
+ passage_uuid: 'e350ec6c-a40c-46bf-8182-acba5a50e4fe',
44
+ },
45
+ ],
46
+ };
47
+
48
+ describe('abbreviation transformer', () => {
49
+ const passage = passageFromDTO(
50
+ dto,
51
+ annotationsFromDTO(dto.annotations || []),
52
+ );
53
+ const block = blockFromPassage(passage);
54
+
55
+ if (!block?.content) {
56
+ throw new Error('Block conversion failed');
57
+ }
58
+
59
+ it('should transform abbreviation annotation correctly', () => {
60
+ const annotation = passage.annotations.find(
61
+ (a: Annotation) => a.type === 'abbreviation',
62
+ ) as Annotation;
63
+ abbreviation({
64
+ root: block,
65
+ parent: block,
66
+ block,
67
+ annotation,
68
+ });
69
+
70
+ const val = recurseForType({
71
+ until: 'abbreviation',
72
+ block,
73
+ });
74
+
75
+ expect(val).toBeDefined();
76
+ expect(val?.type).toBe('abbreviation');
77
+ expect(val?.attrs).toBeDefined();
78
+ expect(val?.attrs?.abbreviation).toBe(
79
+ 'e350ec6c-a40c-46bf-8182-acba5a50e4fe',
80
+ );
81
+ expect(val?.attrs?.uuid).toBe('49c41c6d-0e04-412e-81ad-22f3af377470');
82
+ expect(val?.content).toBeDefined();
83
+ expect(val?.content?.length).toBe(1);
84
+ expect(val?.content?.[0].type).toBe('text');
85
+ expect(val?.content?.[0].text).toBe('C ');
86
+ });
87
+ });
@@ -0,0 +1,30 @@
1
+ import { AbbreviationAnnotation } from '@eightyfourthousand/data-access';
2
+ import { recurse } from './recurse';
3
+ import { Transformer } from './transformer';
4
+ import { splitContent } from './split-content';
5
+
6
+ export const abbreviation: Transformer = (ctx) => {
7
+ const { annotation } = ctx;
8
+ const { abbreviation, uuid } = annotation as AbbreviationAnnotation;
9
+
10
+ recurse({
11
+ until: ['text'],
12
+ ...ctx,
13
+ transform: (ctx) => {
14
+ splitContent({
15
+ ...ctx,
16
+ transform: ({ block }) => {
17
+ block.type = 'abbreviation';
18
+ block.attrs = {
19
+ ...block.attrs,
20
+ abbreviation,
21
+ uuid,
22
+ };
23
+ block.content = [
24
+ { type: 'text', text: block.text, marks: block.marks },
25
+ ];
26
+ },
27
+ });
28
+ },
29
+ });
30
+ };
@@ -0,0 +1,146 @@
1
+ import {
2
+ AnnotationType,
3
+ Annotations,
4
+ ExtendedTranslationLanguage,
5
+ } from '@eightyfourthousand/data-access';
6
+ import type { TranslationEditorContentType, Transformer } from './transformer';
7
+ import {
8
+ audio,
9
+ abbreviation,
10
+ blockquote,
11
+ code,
12
+ deprecated,
13
+ endNoteLink,
14
+ glossaryInstance,
15
+ hasAbbreviation,
16
+ heading,
17
+ image,
18
+ indent,
19
+ inlineTitle,
20
+ internalLink,
21
+ leadingSpace,
22
+ lineGroup,
23
+ line,
24
+ link,
25
+ list,
26
+ listItem,
27
+ mantra,
28
+ mention,
29
+ paragraph,
30
+ quote,
31
+ quoted,
32
+ reference,
33
+ span,
34
+ table,
35
+ tableBodyData,
36
+ tableBodyHeader,
37
+ tableBodyRow,
38
+ trailer,
39
+ unknown,
40
+ } from '.';
41
+ import type { TranslationEditorContentItem } from '../components/editor';
42
+
43
+ const TRANSFORMERS: Partial<Record<AnnotationType, Transformer>> = {
44
+ abbreviation,
45
+ audio,
46
+ blockquote,
47
+ code,
48
+ deprecated,
49
+ endNoteLink,
50
+ glossaryInstance,
51
+ hasAbbreviation,
52
+ heading,
53
+ indent,
54
+ image,
55
+ inlineTitle,
56
+ internalLink,
57
+ leadingSpace,
58
+ line,
59
+ lineGroup,
60
+ link,
61
+ list,
62
+ listItem,
63
+ mantra,
64
+ mention,
65
+ paragraph,
66
+ quote,
67
+ quoted,
68
+ reference,
69
+ span,
70
+ table,
71
+ tableBodyData,
72
+ tableBodyHeader,
73
+ tableBodyRow,
74
+ trailer,
75
+ unknown,
76
+ } as const;
77
+
78
+ export const ITALIC_LANGUAGES: ExtendedTranslationLanguage[] = [
79
+ 'en',
80
+ 'Bo-Ltn',
81
+ 'Mt-Ltn',
82
+ 'Pi-Ltn',
83
+ 'Sa-Ltn',
84
+ 'Zh-Ltn',
85
+ ] as const;
86
+
87
+ export const isAttributeAnnotation = (type: TranslationEditorContentType) => {
88
+ return ['indent', 'leadingSpace'].includes(type);
89
+ };
90
+
91
+ export const isBlockAnnotation = (type: TranslationEditorContentType) => {
92
+ return [
93
+ 'blockquote',
94
+ 'endnotes',
95
+ 'heading',
96
+ 'line',
97
+ 'lineGroup',
98
+ 'list',
99
+ 'listItem',
100
+ 'paragraph',
101
+ 'table',
102
+ 'tableBodyData',
103
+ 'tableBodyHeader',
104
+ 'tableBodyRow',
105
+ 'trailer',
106
+ ].includes(type);
107
+ };
108
+
109
+ export const isInlineAnnotation = (type: TranslationEditorContentType) => {
110
+ return [
111
+ 'abbreviation',
112
+ 'audio',
113
+ 'code',
114
+ 'endNoteLink',
115
+ 'glossaryInstance',
116
+ 'hasAbbreviation',
117
+ 'image',
118
+ 'italic',
119
+ 'inlineTitle',
120
+ 'internalLink',
121
+ 'link',
122
+ 'mantra',
123
+ 'mention',
124
+ 'quote',
125
+ 'quoted',
126
+ 'reference',
127
+ 'span',
128
+ 'text',
129
+ ].includes(type);
130
+ };
131
+
132
+ export const annotateBlock = (
133
+ block: TranslationEditorContentItem,
134
+ annotations: Annotations,
135
+ ) => {
136
+ for (const annotation of annotations) {
137
+ const transformer = TRANSFORMERS[annotation.type] || TRANSFORMERS.unknown;
138
+ transformer?.({ root: block, block, annotation });
139
+ if (!annotation.validated) {
140
+ if (!block.attrs) {
141
+ block.attrs = {};
142
+ }
143
+ block.attrs.invalid = true;
144
+ }
145
+ }
146
+ };
@@ -0,0 +1,55 @@
1
+ import { annotationsFromDTO, PassageDTO, passageFromDTO } from '@eightyfourthousand/data-access';
2
+ import { blockFromPassage } from '../block';
3
+ import { recurseForType } from './recurse';
4
+
5
+ const dto: PassageDTO = {
6
+ sort: 1,
7
+ type: 'root',
8
+ uuid: 'passage-uuid-1234',
9
+ label: '',
10
+ xmlId: 'test-passage',
11
+ parent: 'test-parent',
12
+ content: 'Listen to the audio recording.',
13
+ work_uuid: 'work-uuid-5678',
14
+ annotations: [
15
+ {
16
+ end: 9,
17
+ type: 'audio',
18
+ uuid: 'audio-uuid-1',
19
+ start: 9,
20
+ content: [
21
+ {
22
+ src: 'https://example.com/audio.mp3',
23
+ },
24
+ {
25
+ 'media-type': 'audio/mpeg',
26
+ },
27
+ ],
28
+ passage_uuid: 'passage-uuid-1234',
29
+ },
30
+ ],
31
+ };
32
+
33
+ describe('audio transformer', () => {
34
+ const passage = passageFromDTO(
35
+ dto,
36
+ annotationsFromDTO(dto.annotations || [], dto.content.length),
37
+ );
38
+ const block = blockFromPassage(passage);
39
+
40
+ if (!block?.content) {
41
+ throw new Error('Block conversion failed');
42
+ }
43
+
44
+ it('should transform audio annotation correctly', () => {
45
+ const audioNode = recurseForType({
46
+ until: 'audio',
47
+ block,
48
+ });
49
+ expect(audioNode).toBeDefined();
50
+ expect(audioNode?.type).toBe('audio');
51
+ expect(audioNode?.attrs?.src).toBe('https://example.com/audio.mp3');
52
+ expect(audioNode?.attrs?.mediaType).toBe('audio/mpeg');
53
+ expect(audioNode?.attrs?.uuid).toBe('audio-uuid-1');
54
+ });
55
+ });
@@ -0,0 +1,29 @@
1
+ import { AudioAnnotation } from '@eightyfourthousand/data-access';
2
+ import { Transformer } from './transformer';
3
+ import { recurse } from './recurse';
4
+ import { splitAndInsert } from './split-insert';
5
+
6
+ export const audio: Transformer = (ctx) => {
7
+ const { annotation } = ctx;
8
+ const { src, mediaType, uuid, start, end } = annotation as AudioAnnotation;
9
+ recurse({
10
+ ...ctx,
11
+ until: ['paragraph'],
12
+ transform: (ctx) => {
13
+ splitAndInsert({
14
+ ...ctx,
15
+ transform: ({ block }) => {
16
+ block.type = 'audio';
17
+ block.attrs = {
18
+ ...block.attrs,
19
+ src,
20
+ mediaType,
21
+ uuid,
22
+ start,
23
+ end,
24
+ };
25
+ },
26
+ });
27
+ },
28
+ });
29
+ };
@@ -0,0 +1,48 @@
1
+ import { annotationsFromDTO, PassageDTO, passageFromDTO } from '@eightyfourthousand/data-access';
2
+ import { blockFromPassage } from '../block';
3
+ import { recurseForType } from './recurse';
4
+
5
+ const dto: PassageDTO = {
6
+ sort: 1,
7
+ type: 'root',
8
+ uuid: 'passage-uuid-1234',
9
+ label: '',
10
+ xmlId: 'test-passage',
11
+ parent: 'test-parent',
12
+ content: 'This is a quoted text from scripture.',
13
+ work_uuid: 'work-uuid-5678',
14
+ annotations: [
15
+ {
16
+ end: 37,
17
+ type: 'blockquote',
18
+ uuid: 'blockquote-uuid-1',
19
+ start: 0,
20
+ content: [],
21
+ passage_uuid: 'passage-uuid-1234',
22
+ },
23
+ ],
24
+ };
25
+
26
+ describe('blockquote transformer', () => {
27
+ const passage = passageFromDTO(
28
+ dto,
29
+ annotationsFromDTO(dto.annotations || [], dto.content.length),
30
+ );
31
+ const block = blockFromPassage(passage);
32
+
33
+ if (!block?.content) {
34
+ throw new Error('Block conversion failed');
35
+ }
36
+
37
+ it('should transform blockquote annotation correctly', () => {
38
+ const blockquoteNode = recurseForType({
39
+ until: 'blockquote',
40
+ block,
41
+ });
42
+ expect(blockquoteNode).toBeDefined();
43
+ expect(blockquoteNode?.type).toBe('blockquote');
44
+ expect(blockquoteNode?.attrs?.uuid).toBe('blockquote-uuid-1');
45
+ expect(blockquoteNode?.content).toBeDefined();
46
+ expect(blockquoteNode?.content?.[0]?.type).toBe('paragraph');
47
+ });
48
+ });
@@ -0,0 +1,41 @@
1
+ import { recurse } from './recurse';
2
+ import { splitBlock } from './split-block';
3
+ import { Transformer } from './transformer';
4
+
5
+ export const blockquote: Transformer = (ctx) => {
6
+ const { annotation } = ctx;
7
+ const { start, end, uuid } = annotation || {};
8
+
9
+ recurse({
10
+ ...ctx,
11
+ until: ['paragraph', 'lineGroup'],
12
+ transform: (ctx) => {
13
+ const origType = ctx.block?.type || 'paragraph';
14
+ splitBlock({
15
+ ...ctx,
16
+ transform: (ctx) => {
17
+ const { block } = ctx;
18
+ block.type = 'blockquote';
19
+ block.attrs = {
20
+ ...block.attrs,
21
+ start,
22
+ end,
23
+ uuid,
24
+ };
25
+ block.content = [
26
+ {
27
+ type: origType,
28
+ attrs: {
29
+ ...block.attrs,
30
+ start,
31
+ end,
32
+ uuid,
33
+ },
34
+ content: block.content || [],
35
+ },
36
+ ];
37
+ },
38
+ });
39
+ },
40
+ });
41
+ };