@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.
- package/.babelrc +12 -0
- package/.eslintrc.json +18 -0
- package/README.md +7 -0
- package/jest.config.ts +10 -0
- package/package.json +35 -0
- package/postcss.config.mjs +9 -0
- package/project.json +29 -0
- package/src/fixtures/basic/json.ts +396 -0
- package/src/fixtures/toh251/json.ts +4913 -0
- package/src/fixtures/toh251/passages.ts +2814 -0
- package/src/fixtures/types.ts +8 -0
- package/src/index.ts +4 -0
- package/src/lib/block.ts +226 -0
- package/src/lib/components/editor/BlockEditor.tsx +38 -0
- package/src/lib/components/editor/EditorBackMatterPage.tsx +95 -0
- package/src/lib/components/editor/EditorBodyPage.tsx +87 -0
- package/src/lib/components/editor/EditorHeader.tsx +22 -0
- package/src/lib/components/editor/EditorLayout.tsx +62 -0
- package/src/lib/components/editor/EditorLeftPanelPage.tsx +27 -0
- package/src/lib/components/editor/EditorProvider.tsx +399 -0
- package/src/lib/components/editor/PaginationProvider.tsx +472 -0
- package/src/lib/components/editor/TitlesBuilder.tsx +40 -0
- package/src/lib/components/editor/TranslationBuilder.tsx +171 -0
- package/src/lib/components/editor/TranslationEditor.tsx +32 -0
- package/src/lib/components/editor/extensions/Abbreviation/Abbreviation.ts +133 -0
- package/src/lib/components/editor/extensions/Audio/Audio.ts +69 -0
- package/src/lib/components/editor/extensions/Bold.ts +43 -0
- package/src/lib/components/editor/extensions/Document.ts +8 -0
- package/src/lib/components/editor/extensions/DragHandle/DragHandle.ts +429 -0
- package/src/lib/components/editor/extensions/EndNoteLink/EndNoteLink.tsx +39 -0
- package/src/lib/components/editor/extensions/EndNoteLink/EndNoteLinkHoverContent.tsx +139 -0
- package/src/lib/components/editor/extensions/EndNoteLink/EndNoteLinkMark.ts +236 -0
- package/src/lib/components/editor/extensions/EndNoteLink/endnote-utils.ts +412 -0
- package/src/lib/components/editor/extensions/GlobalConfig.ts +52 -0
- package/src/lib/components/editor/extensions/GlossaryInstance/GlossaryInput.tsx +54 -0
- package/src/lib/components/editor/extensions/GlossaryInstance/GlossaryInstance.tsx +129 -0
- package/src/lib/components/editor/extensions/GlossaryInstance/GlossaryInstanceNode.ts +148 -0
- package/src/lib/components/editor/extensions/Heading/Heading.ts +71 -0
- package/src/lib/components/editor/extensions/HoverInputField.tsx +54 -0
- package/src/lib/components/editor/extensions/Image.ts +18 -0
- package/src/lib/components/editor/extensions/Indent.ts +103 -0
- package/src/lib/components/editor/extensions/InternalLink/InternalLink.ts +173 -0
- package/src/lib/components/editor/extensions/InternalLink/InternalLinkHoverContent.tsx +137 -0
- package/src/lib/components/editor/extensions/InternalLink/InternalLinkInput.tsx +71 -0
- package/src/lib/components/editor/extensions/InternalLink/index.ts +1 -0
- package/src/lib/components/editor/extensions/Italic.ts +50 -0
- package/src/lib/components/editor/extensions/LeadingSpace.ts +106 -0
- package/src/lib/components/editor/extensions/Line/LineNode.ts +41 -0
- package/src/lib/components/editor/extensions/LineGroup/LineGroupNode.ts +124 -0
- package/src/lib/components/editor/extensions/Link/Link.ts +65 -0
- package/src/lib/components/editor/extensions/Link/LinkHoverContent.tsx +124 -0
- package/src/lib/components/editor/extensions/Link/index.ts +1 -0
- package/src/lib/components/editor/extensions/List.ts +74 -0
- package/src/lib/components/editor/extensions/Mantra/Mantra.ts +88 -0
- package/src/lib/components/editor/extensions/Mention/Mention.ts +184 -0
- package/src/lib/components/editor/extensions/Mention/MentionHoverContent.tsx +158 -0
- package/src/lib/components/editor/extensions/NodeWrapper.tsx +57 -0
- package/src/lib/components/editor/extensions/Paragraph/Paragraph.ts +25 -0
- package/src/lib/components/editor/extensions/ParagraphIndent.ts +87 -0
- package/src/lib/components/editor/extensions/Passage/EditLabel.tsx +57 -0
- package/src/lib/components/editor/extensions/Passage/EditorOptions.tsx +29 -0
- package/src/lib/components/editor/extensions/Passage/Passage.tsx +238 -0
- package/src/lib/components/editor/extensions/Passage/PassageNode.ts +223 -0
- package/src/lib/components/editor/extensions/Passage/ReaderOptions.tsx +55 -0
- package/src/lib/components/editor/extensions/Passage/ShowAnnotations.tsx +92 -0
- package/src/lib/components/editor/extensions/Passage/index.ts +1 -0
- package/src/lib/components/editor/extensions/Passage/label.spec.ts +118 -0
- package/src/lib/components/editor/extensions/Passage/label.ts +39 -0
- package/src/lib/components/editor/extensions/Placeholder.ts +9 -0
- package/src/lib/components/editor/extensions/SlashCommand/SlashCommand.ts +65 -0
- package/src/lib/components/editor/extensions/SlashCommand/SuggestionList.tsx +109 -0
- package/src/lib/components/editor/extensions/SlashCommand/Suggestions.ts +185 -0
- package/src/lib/components/editor/extensions/SmallCaps.ts +110 -0
- package/src/lib/components/editor/extensions/StarterKit.ts +36 -0
- package/src/lib/components/editor/extensions/Subscript.ts +43 -0
- package/src/lib/components/editor/extensions/Superscript.ts +43 -0
- package/src/lib/components/editor/extensions/Table.ts +32 -0
- package/src/lib/components/editor/extensions/TextAlign.ts +5 -0
- package/src/lib/components/editor/extensions/TitleMetadata.ts +40 -0
- package/src/lib/components/editor/extensions/TitleNode.ts +133 -0
- package/src/lib/components/editor/extensions/TitlesNode.ts +102 -0
- package/src/lib/components/editor/extensions/Trailer.ts +57 -0
- package/src/lib/components/editor/extensions/TranslationDocument.ts +7 -0
- package/src/lib/components/editor/extensions/TranslationMetadata.ts +58 -0
- package/src/lib/components/editor/extensions/Underline.ts +43 -0
- package/src/lib/components/editor/hooks/index.ts +4 -0
- package/src/lib/components/editor/hooks/useBlockEditor.ts +53 -0
- package/src/lib/components/editor/hooks/useDefaultExtensions.ts +39 -0
- package/src/lib/components/editor/hooks/useDirtyStore.ts +33 -0
- package/src/lib/components/editor/hooks/useTranslationExtensions.ts +148 -0
- package/src/lib/components/editor/index.ts +10 -0
- package/src/lib/components/editor/menus/EmptyBubbleMenu.tsx +42 -0
- package/src/lib/components/editor/menus/MainBubbleMenu.tsx +51 -0
- package/src/lib/components/editor/menus/TranslationBubbleMenu.tsx +57 -0
- package/src/lib/components/editor/menus/index.ts +3 -0
- package/src/lib/components/editor/menus/selectors/EndNoteSelector.tsx +388 -0
- package/src/lib/components/editor/menus/selectors/GlossarySelector.tsx +63 -0
- package/src/lib/components/editor/menus/selectors/LinkSelector.tsx +68 -0
- package/src/lib/components/editor/menus/selectors/MantraSelector.tsx +119 -0
- package/src/lib/components/editor/menus/selectors/NodeSelector.tsx +144 -0
- package/src/lib/components/editor/menus/selectors/ParagraphButtons.tsx +68 -0
- package/src/lib/components/editor/menus/selectors/SelectorInputField.tsx +68 -0
- package/src/lib/components/editor/menus/selectors/TextAlignSelector.tsx +89 -0
- package/src/lib/components/editor/menus/selectors/TextButtons.tsx +89 -0
- package/src/lib/components/editor/menus/selectors/TranslationNodeSelector.tsx +143 -0
- package/src/lib/components/editor/menus/selectors/TranslationTextButtons.tsx +125 -0
- package/src/lib/components/editor/menus/selectors/index.ts +5 -0
- package/src/lib/components/editor/save-filter.spec.ts +94 -0
- package/src/lib/components/editor/save-filter.ts +27 -0
- package/src/lib/components/editor/util.ts +304 -0
- package/src/lib/components/index.ts +3 -0
- package/src/lib/components/reader/ReaderBackMatterPage.tsx +62 -0
- package/src/lib/components/reader/ReaderBackMatterPanel.tsx +53 -0
- package/src/lib/components/reader/ReaderBodyPage.tsx +46 -0
- package/src/lib/components/reader/ReaderBodyPanel.tsx +68 -0
- package/src/lib/components/reader/ReaderLayout.tsx +39 -0
- package/src/lib/components/reader/ReaderLeftPanel.tsx +8 -0
- package/src/lib/components/reader/ReaderLeftPanelPage.tsx +31 -0
- package/src/lib/components/reader/TranslationReader.tsx +28 -0
- package/src/lib/components/reader/index.ts +2 -0
- package/src/lib/components/reader/ssr.ts +3 -0
- package/src/lib/components/shared/AiSummarizerPage.tsx +12 -0
- package/src/lib/components/shared/BackMatterPanel.tsx +143 -0
- package/src/lib/components/shared/BodyPanel.tsx +214 -0
- package/src/lib/components/shared/HoverCardProvider.tsx +407 -0
- package/src/lib/components/shared/Imprint.tsx +24 -0
- package/src/lib/components/shared/LabeledElement.tsx +133 -0
- package/src/lib/components/shared/LeftPanel.tsx +65 -0
- package/src/lib/components/shared/NavigationContext.ts +64 -0
- package/src/lib/components/shared/NavigationProvider.tsx +368 -0
- package/src/lib/components/shared/OpenGraphImage.tsx +75 -0
- package/src/lib/components/shared/PassageSkeleton.tsx +10 -0
- package/src/lib/components/shared/RestrictionWarning.tsx +177 -0
- package/src/lib/components/shared/SourceReader.tsx +83 -0
- package/src/lib/components/shared/SuggestRevisionForm.tsx +99 -0
- package/src/lib/components/shared/TableOfContents.tsx +280 -0
- package/src/lib/components/shared/ThreeColumnRenderer.tsx +54 -0
- package/src/lib/components/shared/TranslationHeader.tsx +86 -0
- package/src/lib/components/shared/TranslationHoverCard.tsx +84 -0
- package/src/lib/components/shared/TranslationSkeleton.tsx +16 -0
- package/src/lib/components/shared/TranslationTable.tsx +155 -0
- package/src/lib/components/shared/bibliography/BibliographyBody.tsx +28 -0
- package/src/lib/components/shared/bibliography/BibliographyList.tsx +63 -0
- package/src/lib/components/shared/bibliography/index.ts +1 -0
- package/src/lib/components/shared/generate-metadata.ts +44 -0
- package/src/lib/components/shared/glossary/GlossaryInstanceBody.tsx +144 -0
- package/src/lib/components/shared/glossary/GlossaryPaginationProvider.tsx +317 -0
- package/src/lib/components/shared/glossary/GlossarySkeleton.tsx +19 -0
- package/src/lib/components/shared/glossary/GlossaryTermList.tsx +58 -0
- package/src/lib/components/shared/glossary/index.ts +3 -0
- package/src/lib/components/shared/hooks/useGlossaryInstanceListener.tsx +42 -0
- package/src/lib/components/shared/hooks/useScrollInTab.tsx +43 -0
- package/src/lib/components/shared/hooks/useScrollPositionRestore.ts +274 -0
- package/src/lib/components/shared/hooks/useTohToggle.tsx +52 -0
- package/src/lib/components/shared/index.ts +11 -0
- package/src/lib/components/shared/ssr.ts +2 -0
- package/src/lib/components/shared/titles/FramedCard.tsx +132 -0
- package/src/lib/components/shared/titles/LongTitle.tsx +20 -0
- package/src/lib/components/shared/titles/LongTitles.tsx +28 -0
- package/src/lib/components/shared/titles/Title.tsx +54 -0
- package/src/lib/components/shared/titles/TitleDetails.tsx +47 -0
- package/src/lib/components/shared/titles/TitleForm.tsx +37 -0
- package/src/lib/components/shared/titles/Titles.tsx +114 -0
- package/src/lib/components/shared/titles/TitlesCard.tsx +113 -0
- package/src/lib/components/shared/titles/index.ts +8 -0
- package/src/lib/components/shared/types.ts +79 -0
- package/src/lib/components/ssr.ts +2 -0
- package/src/lib/exporters/abbreviation.spec.ts +31 -0
- package/src/lib/exporters/abbreviation.ts +22 -0
- package/src/lib/exporters/annotation.ts +193 -0
- package/src/lib/exporters/audio.spec.ts +77 -0
- package/src/lib/exporters/audio.ts +27 -0
- package/src/lib/exporters/blockquote.spec.ts +48 -0
- package/src/lib/exporters/blockquote.ts +24 -0
- package/src/lib/exporters/code.spec.ts +93 -0
- package/src/lib/exporters/code.ts +26 -0
- package/src/lib/exporters/end-note-link.spec.ts +104 -0
- package/src/lib/exporters/end-note-link.ts +35 -0
- package/src/lib/exporters/export.ts +12 -0
- package/src/lib/exporters/glossary-instance.spec.ts +85 -0
- package/src/lib/exporters/glossary-instance.ts +31 -0
- package/src/lib/exporters/has-abbreviation.spec.ts +31 -0
- package/src/lib/exporters/has-abbreviation.ts +21 -0
- package/src/lib/exporters/heading.spec.ts +80 -0
- package/src/lib/exporters/heading.ts +28 -0
- package/src/lib/exporters/image.spec.ts +48 -0
- package/src/lib/exporters/image.ts +25 -0
- package/src/lib/exporters/indent.spec.ts +58 -0
- package/src/lib/exporters/indent.ts +18 -0
- package/src/lib/exporters/index.ts +1 -0
- package/src/lib/exporters/internal-link.spec.ts +90 -0
- package/src/lib/exporters/internal-link.ts +35 -0
- package/src/lib/exporters/italic.spec.ts +84 -0
- package/src/lib/exporters/italic.ts +55 -0
- package/src/lib/exporters/leading-space.spec.ts +28 -0
- package/src/lib/exporters/leading-space.ts +16 -0
- package/src/lib/exporters/line-group.spec.ts +48 -0
- package/src/lib/exporters/line-group.ts +24 -0
- package/src/lib/exporters/line.spec.ts +48 -0
- package/src/lib/exporters/line.ts +24 -0
- package/src/lib/exporters/link.spec.ts +123 -0
- package/src/lib/exporters/link.ts +67 -0
- package/src/lib/exporters/list-item.spec.ts +48 -0
- package/src/lib/exporters/list-item.ts +24 -0
- package/src/lib/exporters/list.spec.ts +82 -0
- package/src/lib/exporters/list.ts +31 -0
- package/src/lib/exporters/mantra.spec.ts +51 -0
- package/src/lib/exporters/mantra.ts +25 -0
- package/src/lib/exporters/mention.ts +41 -0
- package/src/lib/exporters/paragraph.spec.ts +173 -0
- package/src/lib/exporters/paragraph.ts +32 -0
- package/src/lib/exporters/quote.spec.ts +56 -0
- package/src/lib/exporters/quote.ts +25 -0
- package/src/lib/exporters/span.spec.ts +118 -0
- package/src/lib/exporters/span.ts +44 -0
- package/src/lib/exporters/table-body-data.spec.ts +48 -0
- package/src/lib/exporters/table-body-data.ts +24 -0
- package/src/lib/exporters/table-body-header.spec.ts +48 -0
- package/src/lib/exporters/table-body-header.ts +24 -0
- package/src/lib/exporters/table-body-row.spec.ts +48 -0
- package/src/lib/exporters/table-body-row.ts +24 -0
- package/src/lib/exporters/table.spec.ts +48 -0
- package/src/lib/exporters/table.ts +24 -0
- package/src/lib/exporters/trailer.spec.ts +48 -0
- package/src/lib/exporters/trailer.ts +24 -0
- package/src/lib/exporters/util.ts +62 -0
- package/src/lib/passage.ts +182 -0
- package/src/lib/titles.ts +80 -0
- package/src/lib/transformers/abbreviation.spec.ts +87 -0
- package/src/lib/transformers/abbreviation.ts +30 -0
- package/src/lib/transformers/annotate.ts +146 -0
- package/src/lib/transformers/audio.spec.ts +55 -0
- package/src/lib/transformers/audio.ts +29 -0
- package/src/lib/transformers/blockquote.spec.ts +48 -0
- package/src/lib/transformers/blockquote.ts +41 -0
- package/src/lib/transformers/code.spec.ts +52 -0
- package/src/lib/transformers/code.ts +22 -0
- package/src/lib/transformers/deprecated.ts +7 -0
- package/src/lib/transformers/end-note-link.spec.ts +56 -0
- package/src/lib/transformers/end-note-link.ts +76 -0
- package/src/lib/transformers/glossary-instance.spec.ts +55 -0
- package/src/lib/transformers/glossary-instance.ts +40 -0
- package/src/lib/transformers/has-abbreviation.spec.ts +50 -0
- package/src/lib/transformers/has-abbreviation.ts +30 -0
- package/src/lib/transformers/heading.spec.ts +62 -0
- package/src/lib/transformers/heading.ts +30 -0
- package/src/lib/transformers/image.spec.ts +51 -0
- package/src/lib/transformers/image.ts +28 -0
- package/src/lib/transformers/indent.spec.ts +53 -0
- package/src/lib/transformers/indent.ts +17 -0
- package/src/lib/transformers/index.ts +33 -0
- package/src/lib/transformers/inline-title.spec.ts +59 -0
- package/src/lib/transformers/inline-title.ts +34 -0
- package/src/lib/transformers/internal-link.spec.ts +67 -0
- package/src/lib/transformers/internal-link.ts +65 -0
- package/src/lib/transformers/italic.ts +22 -0
- package/src/lib/transformers/leading-space.spec.ts +55 -0
- package/src/lib/transformers/leading-space.ts +17 -0
- package/src/lib/transformers/line-group.spec.ts +48 -0
- package/src/lib/transformers/line-group.ts +37 -0
- package/src/lib/transformers/line.spec.ts +54 -0
- package/src/lib/transformers/line.ts +27 -0
- package/src/lib/transformers/link.spec.ts +61 -0
- package/src/lib/transformers/link.ts +27 -0
- package/src/lib/transformers/list-item.spec.ts +48 -0
- package/src/lib/transformers/list-item.ts +37 -0
- package/src/lib/transformers/list.spec.ts +58 -0
- package/src/lib/transformers/list.ts +42 -0
- package/src/lib/transformers/mantra.spec.ts +58 -0
- package/src/lib/transformers/mantra.ts +28 -0
- package/src/lib/transformers/mention.ts +70 -0
- package/src/lib/transformers/paragraph.spec.ts +46 -0
- package/src/lib/transformers/paragraph.ts +26 -0
- package/src/lib/transformers/quote.spec.ts +42 -0
- package/src/lib/transformers/quote.ts +3 -0
- package/src/lib/transformers/quoted.ts +3 -0
- package/src/lib/transformers/recurse.ts +76 -0
- package/src/lib/transformers/reference.ts +3 -0
- package/src/lib/transformers/span.spec.ts +68 -0
- package/src/lib/transformers/span.ts +78 -0
- package/src/lib/transformers/split-at.ts +58 -0
- package/src/lib/transformers/split-block.ts +110 -0
- package/src/lib/transformers/split-content.ts +67 -0
- package/src/lib/transformers/split-insert.ts +76 -0
- package/src/lib/transformers/split-marks.ts +42 -0
- package/src/lib/transformers/split-node.ts +138 -0
- package/src/lib/transformers/table-body-data.spec.ts +44 -0
- package/src/lib/transformers/table-body-data.ts +29 -0
- package/src/lib/transformers/table-body-header.spec.ts +44 -0
- package/src/lib/transformers/table-body-header.ts +29 -0
- package/src/lib/transformers/table-body-row.spec.ts +44 -0
- package/src/lib/transformers/table-body-row.ts +29 -0
- package/src/lib/transformers/table.spec.ts +47 -0
- package/src/lib/transformers/table.ts +29 -0
- package/src/lib/transformers/trailer.spec.ts +43 -0
- package/src/lib/transformers/trailer.ts +26 -0
- package/src/lib/transformers/transformer.ts +25 -0
- package/src/lib/transformers/unknown.ts +8 -0
- package/src/lib/transformers/util.ts +20 -0
- package/src/lib/types.ts +10 -0
- package/src/ssr.ts +1 -0
- package/tsconfig.json +20 -0
- package/tsconfig.lib.json +29 -0
- package/tsconfig.spec.json +22 -0
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { annotationsFromDTO, PassageDTO, passageFromDTO } from '@eightyfourthousand/data-access';
|
|
2
|
+
import { blockFromPassage } from '../block';
|
|
3
|
+
|
|
4
|
+
const dto: PassageDTO = {
|
|
5
|
+
sort: 1,
|
|
6
|
+
type: 'root',
|
|
7
|
+
uuid: 'passage-uuid-1234',
|
|
8
|
+
label: '',
|
|
9
|
+
xmlId: 'test-passage',
|
|
10
|
+
parent: 'test-parent',
|
|
11
|
+
content: 'First item in list',
|
|
12
|
+
work_uuid: 'work-uuid-5678',
|
|
13
|
+
annotations: [
|
|
14
|
+
{
|
|
15
|
+
end: 18,
|
|
16
|
+
type: 'list',
|
|
17
|
+
uuid: 'list-uuid-1',
|
|
18
|
+
start: 0,
|
|
19
|
+
content: [
|
|
20
|
+
{
|
|
21
|
+
nesting: '0',
|
|
22
|
+
},
|
|
23
|
+
{
|
|
24
|
+
'list-spacing': 'compact',
|
|
25
|
+
},
|
|
26
|
+
{
|
|
27
|
+
'list-item-style': 'bullet',
|
|
28
|
+
},
|
|
29
|
+
],
|
|
30
|
+
passage_uuid: 'passage-uuid-1234',
|
|
31
|
+
},
|
|
32
|
+
],
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
describe('list transformer', () => {
|
|
36
|
+
const passage = passageFromDTO(
|
|
37
|
+
dto,
|
|
38
|
+
annotationsFromDTO(dto.annotations || [], dto.content.length),
|
|
39
|
+
);
|
|
40
|
+
const block = blockFromPassage(passage);
|
|
41
|
+
|
|
42
|
+
if (!block?.content) {
|
|
43
|
+
throw new Error('Block conversion failed');
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
it('should transform list annotation correctly', () => {
|
|
47
|
+
// The list should be in block.content[0] and become a bulletList
|
|
48
|
+
const listNode = block.content?.[0];
|
|
49
|
+
expect(listNode).toBeDefined();
|
|
50
|
+
expect(listNode?.type).toBe('bulletList');
|
|
51
|
+
expect(listNode?.attrs?.uuid).toBe('list-uuid-1');
|
|
52
|
+
expect(listNode?.attrs?.nesting).toBe(0);
|
|
53
|
+
expect(listNode?.attrs?.spacing).toBe('compact');
|
|
54
|
+
expect(listNode?.attrs?.itemStyle).toBe('bullet');
|
|
55
|
+
expect(listNode?.content).toBeDefined();
|
|
56
|
+
expect(listNode?.content?.[0]?.type).toBe('paragraph');
|
|
57
|
+
});
|
|
58
|
+
});
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { ListAnnotation } from '@eightyfourthousand/data-access';
|
|
2
|
+
import { recurse } from './recurse';
|
|
3
|
+
import { splitBlock } from './split-block';
|
|
4
|
+
import { Transformer } from './transformer';
|
|
5
|
+
|
|
6
|
+
export const list: Transformer = (ctx) => {
|
|
7
|
+
const { annotation } = ctx;
|
|
8
|
+
const { start, end, uuid, nesting, spacing, itemStyle } =
|
|
9
|
+
(annotation as ListAnnotation) || {};
|
|
10
|
+
|
|
11
|
+
recurse({
|
|
12
|
+
...ctx,
|
|
13
|
+
until: ['paragraph'],
|
|
14
|
+
transform: (ctx) =>
|
|
15
|
+
splitBlock({
|
|
16
|
+
...ctx,
|
|
17
|
+
transform: ({ block }) => {
|
|
18
|
+
block.type = 'bulletList';
|
|
19
|
+
block.attrs = {
|
|
20
|
+
...block.attrs,
|
|
21
|
+
nesting,
|
|
22
|
+
spacing,
|
|
23
|
+
itemStyle,
|
|
24
|
+
start,
|
|
25
|
+
end,
|
|
26
|
+
uuid,
|
|
27
|
+
};
|
|
28
|
+
block.content = [
|
|
29
|
+
{
|
|
30
|
+
type: 'paragraph',
|
|
31
|
+
content: block.content || [],
|
|
32
|
+
attrs: {
|
|
33
|
+
start,
|
|
34
|
+
end,
|
|
35
|
+
uuid,
|
|
36
|
+
},
|
|
37
|
+
},
|
|
38
|
+
];
|
|
39
|
+
},
|
|
40
|
+
}),
|
|
41
|
+
});
|
|
42
|
+
};
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Annotation,
|
|
3
|
+
annotationsFromDTO,
|
|
4
|
+
PassageDTO,
|
|
5
|
+
passageFromDTO,
|
|
6
|
+
} from '@eightyfourthousand/data-access';
|
|
7
|
+
import { mantra } from './mantra';
|
|
8
|
+
import { blockFromPassage } from '../block';
|
|
9
|
+
import { findTextNodeWithMarks } from './recurse';
|
|
10
|
+
|
|
11
|
+
const dto: PassageDTO = {
|
|
12
|
+
sort: 1,
|
|
13
|
+
type: 'root',
|
|
14
|
+
uuid: 'passage-uuid-1234',
|
|
15
|
+
label: '',
|
|
16
|
+
xmlId: 'test-passage',
|
|
17
|
+
parent: 'test-parent',
|
|
18
|
+
content: 'oṃ maṇi padme hūṃ',
|
|
19
|
+
work_uuid: 'work-uuid-5678',
|
|
20
|
+
annotations: [
|
|
21
|
+
{
|
|
22
|
+
end: 17,
|
|
23
|
+
type: 'mantra',
|
|
24
|
+
uuid: 'mantra-uuid-1',
|
|
25
|
+
start: 0,
|
|
26
|
+
content: [
|
|
27
|
+
{
|
|
28
|
+
lang: 'Sa-Ltn',
|
|
29
|
+
},
|
|
30
|
+
],
|
|
31
|
+
passage_uuid: 'passage-uuid-1234',
|
|
32
|
+
},
|
|
33
|
+
],
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
describe('mantra transformer', () => {
|
|
37
|
+
const passage = passageFromDTO(
|
|
38
|
+
dto,
|
|
39
|
+
annotationsFromDTO(dto.annotations || [], dto.content.length),
|
|
40
|
+
);
|
|
41
|
+
const block = blockFromPassage(passage);
|
|
42
|
+
|
|
43
|
+
if (!block?.content) {
|
|
44
|
+
throw new Error('Block conversion failed');
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
it('should transform mantra annotation correctly', () => {
|
|
48
|
+
const textNode = findTextNodeWithMarks(block);
|
|
49
|
+
expect(textNode).toBeDefined();
|
|
50
|
+
expect(textNode?.type).toBe('text');
|
|
51
|
+
expect(textNode?.marks).toBeDefined();
|
|
52
|
+
const mantraMark = textNode?.marks?.find((m: any) => m.type === 'mantra');
|
|
53
|
+
expect(mantraMark).toBeDefined();
|
|
54
|
+
expect(mantraMark?.attrs?.uuid).toBe('mantra-uuid-1');
|
|
55
|
+
expect(mantraMark?.attrs?.lang).toBe('Sa-Ltn');
|
|
56
|
+
expect(textNode?.text).toBe('oṃ maṇi padme hūṃ');
|
|
57
|
+
});
|
|
58
|
+
});
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { MantraAnnotation } from '@eightyfourthousand/data-access';
|
|
2
|
+
import { Transformer } from './transformer';
|
|
3
|
+
import { splitContent } from './split-content';
|
|
4
|
+
import { recurse } from './recurse';
|
|
5
|
+
|
|
6
|
+
export const mantra: Transformer = (ctx) => {
|
|
7
|
+
const { annotation } = ctx;
|
|
8
|
+
|
|
9
|
+
const { lang, uuid } = annotation as MantraAnnotation;
|
|
10
|
+
if (!lang) {
|
|
11
|
+
return;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
recurse({
|
|
15
|
+
...ctx,
|
|
16
|
+
until: ['text'],
|
|
17
|
+
transform: (ctx) =>
|
|
18
|
+
splitContent({
|
|
19
|
+
...ctx,
|
|
20
|
+
transform: ({ block: item }) => {
|
|
21
|
+
item.marks = [
|
|
22
|
+
...(item.marks || []),
|
|
23
|
+
{ type: 'mantra', attrs: { uuid, lang } },
|
|
24
|
+
];
|
|
25
|
+
},
|
|
26
|
+
}),
|
|
27
|
+
});
|
|
28
|
+
};
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { MentionAnnotation } from '@eightyfourthousand/data-access';
|
|
2
|
+
import { Transformer } from './transformer';
|
|
3
|
+
import { recurse } from './recurse';
|
|
4
|
+
import { splitAndInsert } from './split-insert';
|
|
5
|
+
|
|
6
|
+
export const mention: Transformer = (ctx) => {
|
|
7
|
+
const { annotation } = ctx;
|
|
8
|
+
const {
|
|
9
|
+
entity,
|
|
10
|
+
linkType,
|
|
11
|
+
text,
|
|
12
|
+
displayText,
|
|
13
|
+
uuid,
|
|
14
|
+
start,
|
|
15
|
+
end,
|
|
16
|
+
isSameWork,
|
|
17
|
+
subtype,
|
|
18
|
+
linkToh,
|
|
19
|
+
} = annotation as MentionAnnotation;
|
|
20
|
+
|
|
21
|
+
const item = {
|
|
22
|
+
uuid,
|
|
23
|
+
entity,
|
|
24
|
+
linkType,
|
|
25
|
+
text,
|
|
26
|
+
displayText,
|
|
27
|
+
isSameWork,
|
|
28
|
+
subtype,
|
|
29
|
+
linkToh,
|
|
30
|
+
};
|
|
31
|
+
recurse({
|
|
32
|
+
...ctx,
|
|
33
|
+
until: ['text'],
|
|
34
|
+
transform: (ctx) => {
|
|
35
|
+
splitAndInsert({
|
|
36
|
+
...ctx,
|
|
37
|
+
transform: ({ block, parent }) => {
|
|
38
|
+
if (!parent || !parent.content) {
|
|
39
|
+
console.warn(
|
|
40
|
+
'mention transform: transformer expects to find a parent block with content.',
|
|
41
|
+
);
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// Check if a mention Node already exists at this position for batching
|
|
46
|
+
const mention = parent.content.find(
|
|
47
|
+
(child) =>
|
|
48
|
+
child.type === 'mention' &&
|
|
49
|
+
child.attrs?.start === start &&
|
|
50
|
+
child.attrs?.end === end,
|
|
51
|
+
);
|
|
52
|
+
|
|
53
|
+
if (mention) {
|
|
54
|
+
const items = mention.attrs?.items || [];
|
|
55
|
+
items.push(item);
|
|
56
|
+
mention.attrs = { ...mention.attrs, items };
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
block.type = 'mention';
|
|
61
|
+
block.attrs = {
|
|
62
|
+
start,
|
|
63
|
+
end,
|
|
64
|
+
items: [item],
|
|
65
|
+
};
|
|
66
|
+
},
|
|
67
|
+
});
|
|
68
|
+
},
|
|
69
|
+
});
|
|
70
|
+
};
|
|
@@ -0,0 +1,46 @@
|
|
|
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 the first paragraph.',
|
|
13
|
+
work_uuid: 'work-uuid-5678',
|
|
14
|
+
annotations: [
|
|
15
|
+
{
|
|
16
|
+
end: 28,
|
|
17
|
+
type: 'paragraph',
|
|
18
|
+
uuid: 'paragraph-uuid-1',
|
|
19
|
+
start: 0,
|
|
20
|
+
content: [],
|
|
21
|
+
passage_uuid: 'passage-uuid-1234',
|
|
22
|
+
},
|
|
23
|
+
],
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
describe('paragraph 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 paragraph annotation correctly', () => {
|
|
38
|
+
const paragraphNode = recurseForType({
|
|
39
|
+
until: 'paragraph',
|
|
40
|
+
block,
|
|
41
|
+
});
|
|
42
|
+
expect(paragraphNode).toBeDefined();
|
|
43
|
+
expect(paragraphNode?.type).toBe('paragraph');
|
|
44
|
+
expect(paragraphNode?.attrs?.uuid).toBe('paragraph-uuid-1');
|
|
45
|
+
});
|
|
46
|
+
});
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { splitBlock } from './split-block';
|
|
2
|
+
import { Transformer } from './transformer';
|
|
3
|
+
import { recurse } from './recurse';
|
|
4
|
+
|
|
5
|
+
export const paragraph: Transformer = (ctx) => {
|
|
6
|
+
const { annotation } = ctx;
|
|
7
|
+
const { start, end, uuid } = annotation || {};
|
|
8
|
+
|
|
9
|
+
recurse({
|
|
10
|
+
...ctx,
|
|
11
|
+
until: ['paragraph'],
|
|
12
|
+
transform: (ctx) =>
|
|
13
|
+
splitBlock({
|
|
14
|
+
...ctx,
|
|
15
|
+
transform: ({ block }) => {
|
|
16
|
+
block.type = 'paragraph';
|
|
17
|
+
block.attrs = {
|
|
18
|
+
...block.attrs,
|
|
19
|
+
start,
|
|
20
|
+
end,
|
|
21
|
+
uuid,
|
|
22
|
+
};
|
|
23
|
+
},
|
|
24
|
+
}),
|
|
25
|
+
});
|
|
26
|
+
};
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { annotationsFromDTO, PassageDTO, passageFromDTO } from '@eightyfourthousand/data-access';
|
|
2
|
+
import { blockFromPassage } from '../block';
|
|
3
|
+
|
|
4
|
+
// The quote transformer is a no-op (pass), so we just verify it doesn't break
|
|
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: 'Some quoted text',
|
|
13
|
+
work_uuid: 'work-uuid-5678',
|
|
14
|
+
annotations: [
|
|
15
|
+
{
|
|
16
|
+
end: 16,
|
|
17
|
+
type: 'quote',
|
|
18
|
+
uuid: 'quote-uuid-1',
|
|
19
|
+
start: 0,
|
|
20
|
+
content: [],
|
|
21
|
+
passage_uuid: 'passage-uuid-1234',
|
|
22
|
+
},
|
|
23
|
+
],
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
describe('quote 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 not transform quote annotation (pass-through)', () => {
|
|
38
|
+
// quote is a pass-through transformer, so the block structure should remain unchanged
|
|
39
|
+
expect(block).toBeDefined();
|
|
40
|
+
expect(block.content).toBeDefined();
|
|
41
|
+
});
|
|
42
|
+
});
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { AnnotationType } from '@eightyfourthousand/data-access';
|
|
2
|
+
import { TransformationContextWithCallback, Transformer } from './transformer';
|
|
3
|
+
import { TranslationEditorContentItem } from '../components';
|
|
4
|
+
|
|
5
|
+
export const recurse: Transformer = (ctx) => {
|
|
6
|
+
if (trasformOnMatch(ctx)) {
|
|
7
|
+
return;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
const { block } = ctx;
|
|
11
|
+
for (const child of block.content || []) {
|
|
12
|
+
if (trasformOnMatch({ ...ctx, parent: block, block: child })) {
|
|
13
|
+
return;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
recurse({
|
|
17
|
+
...ctx,
|
|
18
|
+
parent: block,
|
|
19
|
+
block: child,
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
const trasformOnMatch = (ctx: TransformationContextWithCallback) => {
|
|
25
|
+
const { block, annotation, until = [], transform } = ctx;
|
|
26
|
+
const type = block.type || 'unknown';
|
|
27
|
+
if (
|
|
28
|
+
block.attrs?.uuid !== annotation.uuid &&
|
|
29
|
+
block.attrs?.start <= annotation.start &&
|
|
30
|
+
block.attrs?.end >= annotation.end &&
|
|
31
|
+
(!until.length || until.includes(type as AnnotationType))
|
|
32
|
+
) {
|
|
33
|
+
transform?.(ctx);
|
|
34
|
+
return true;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
return false;
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
export const recurseForType = ({
|
|
41
|
+
block,
|
|
42
|
+
until,
|
|
43
|
+
}: {
|
|
44
|
+
block: TranslationEditorContentItem;
|
|
45
|
+
until: AnnotationType;
|
|
46
|
+
}): TranslationEditorContentItem | undefined => {
|
|
47
|
+
if (block.type === until) {
|
|
48
|
+
return block;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
for (const child of block.content || []) {
|
|
52
|
+
const val = recurseForType({ block: child, until });
|
|
53
|
+
if (val) {
|
|
54
|
+
return val;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Recursively searches a node tree to find the first text node that has marks applied.
|
|
61
|
+
* Used in tests to verify that mark transformers (span, code, link, etc.) are working correctly.
|
|
62
|
+
*/
|
|
63
|
+
export const findTextNodeWithMarks = (
|
|
64
|
+
node: TranslationEditorContentItem,
|
|
65
|
+
): TranslationEditorContentItem | null => {
|
|
66
|
+
if (node.type === 'text' && node.marks && node.marks.length > 0) {
|
|
67
|
+
return node;
|
|
68
|
+
}
|
|
69
|
+
if (node.content) {
|
|
70
|
+
for (const child of node.content) {
|
|
71
|
+
const found = findTextNodeWithMarks(child);
|
|
72
|
+
if (found) return found;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
return null;
|
|
76
|
+
};
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Annotation,
|
|
3
|
+
annotationsFromDTO,
|
|
4
|
+
PassageDTO,
|
|
5
|
+
passageFromDTO,
|
|
6
|
+
} from '@eightyfourthousand/data-access';
|
|
7
|
+
import { span } from './span';
|
|
8
|
+
import { blockFromPassage } from '../block';
|
|
9
|
+
import { findTextNodeWithMarks } from './recurse';
|
|
10
|
+
|
|
11
|
+
const dto: PassageDTO = {
|
|
12
|
+
sort: 1,
|
|
13
|
+
type: 'root',
|
|
14
|
+
uuid: 'passage-uuid-1234',
|
|
15
|
+
label: '',
|
|
16
|
+
xmlId: 'test-passage',
|
|
17
|
+
parent: 'test-parent',
|
|
18
|
+
content: 'Some bold text here',
|
|
19
|
+
work_uuid: 'work-uuid-5678',
|
|
20
|
+
annotations: [
|
|
21
|
+
{
|
|
22
|
+
end: 14,
|
|
23
|
+
type: 'span',
|
|
24
|
+
uuid: 'span-uuid-1',
|
|
25
|
+
start: 5,
|
|
26
|
+
content: [
|
|
27
|
+
{
|
|
28
|
+
'text-style': 'text-bold',
|
|
29
|
+
},
|
|
30
|
+
],
|
|
31
|
+
passage_uuid: 'passage-uuid-1234',
|
|
32
|
+
},
|
|
33
|
+
],
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
describe('span transformer', () => {
|
|
37
|
+
const passage = passageFromDTO(
|
|
38
|
+
dto,
|
|
39
|
+
annotationsFromDTO(dto.annotations || [], dto.content.length),
|
|
40
|
+
);
|
|
41
|
+
const block = blockFromPassage(passage);
|
|
42
|
+
|
|
43
|
+
if (!block?.content) {
|
|
44
|
+
throw new Error('Block conversion failed');
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
it('should transform span annotation correctly', () => {
|
|
48
|
+
const annotation = passage.annotations.find(
|
|
49
|
+
(a: Annotation) => a.type === 'span',
|
|
50
|
+
) as Annotation;
|
|
51
|
+
span({
|
|
52
|
+
root: block,
|
|
53
|
+
parent: block,
|
|
54
|
+
block,
|
|
55
|
+
annotation,
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
const textNode = findTextNodeWithMarks(block);
|
|
59
|
+
expect(textNode).toBeDefined();
|
|
60
|
+
expect(textNode?.type).toBe('text');
|
|
61
|
+
expect(textNode?.marks).toBeDefined();
|
|
62
|
+
const boldMark = textNode?.marks?.find((m: any) => m.type === 'bold');
|
|
63
|
+
expect(boldMark).toBeDefined();
|
|
64
|
+
expect(boldMark?.attrs?.uuid).toBe('span-uuid-1');
|
|
65
|
+
expect(boldMark?.attrs?.textStyle).toBe('text-bold');
|
|
66
|
+
expect(textNode?.text).toBe('bold text');
|
|
67
|
+
});
|
|
68
|
+
});
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { ExtendedTranslationLanguage, SpanAnnotation } from '@eightyfourthousand/data-access';
|
|
2
|
+
import type { Transformer } from './transformer';
|
|
3
|
+
import { type SpanMarkType, MARK_TYPES } from '../types';
|
|
4
|
+
import { splitContent } from './split-content';
|
|
5
|
+
import { ITALIC_LANGUAGES } from './annotate';
|
|
6
|
+
import { recurse } from './recurse';
|
|
7
|
+
|
|
8
|
+
const MARK_TYPE_FOR_SPAN_TYPE: {
|
|
9
|
+
[key: string]: (
|
|
10
|
+
lang?: ExtendedTranslationLanguage,
|
|
11
|
+
) => SpanMarkType | undefined;
|
|
12
|
+
} = {
|
|
13
|
+
distinct: () => 'italic',
|
|
14
|
+
emphasis: () => 'italic',
|
|
15
|
+
foreign: (lang) => {
|
|
16
|
+
if (lang && ITALIC_LANGUAGES.includes(lang)) {
|
|
17
|
+
return 'italic';
|
|
18
|
+
}
|
|
19
|
+
return undefined;
|
|
20
|
+
},
|
|
21
|
+
'small-caps': () => 'smallCaps',
|
|
22
|
+
subscript: () => 'subscript',
|
|
23
|
+
superscript: () => 'superscript',
|
|
24
|
+
'text-bold': () => 'bold',
|
|
25
|
+
underline: () => 'underline',
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
export const span: Transformer = (ctx) => {
|
|
29
|
+
const { annotation } = ctx;
|
|
30
|
+
const { textStyle, lang, uuid, start, end } = annotation as SpanAnnotation;
|
|
31
|
+
if (!textStyle) {
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const markType = MARK_TYPES.includes(textStyle)
|
|
36
|
+
? textStyle
|
|
37
|
+
: MARK_TYPE_FOR_SPAN_TYPE[textStyle]?.(lang);
|
|
38
|
+
if (!markType) {
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
recurse({
|
|
43
|
+
...ctx,
|
|
44
|
+
until: ['text', 'glossaryInstance'],
|
|
45
|
+
transform: (ctx) => {
|
|
46
|
+
splitContent({
|
|
47
|
+
...ctx,
|
|
48
|
+
transform: ({ block }) => {
|
|
49
|
+
// If the block already has the mark, skip it becuase the editor
|
|
50
|
+
// doesn't like multiple marks of the same type on a single block.
|
|
51
|
+
if (block.marks?.some((m) => m.type === markType)) {
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
block.attrs = {
|
|
56
|
+
...block.attrs,
|
|
57
|
+
start,
|
|
58
|
+
end,
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
block.marks = [
|
|
62
|
+
...(block.marks || []),
|
|
63
|
+
{
|
|
64
|
+
type: markType,
|
|
65
|
+
attrs: {
|
|
66
|
+
...block.attrs,
|
|
67
|
+
uuid,
|
|
68
|
+
type: 'span',
|
|
69
|
+
lang,
|
|
70
|
+
textStyle,
|
|
71
|
+
},
|
|
72
|
+
},
|
|
73
|
+
];
|
|
74
|
+
},
|
|
75
|
+
});
|
|
76
|
+
},
|
|
77
|
+
});
|
|
78
|
+
};
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { Transformer } from './transformer';
|
|
2
|
+
import { splitNode } from './split-node';
|
|
3
|
+
import type { TranslationEditorContentItem } from '../components/editor';
|
|
4
|
+
|
|
5
|
+
export const sort = (nodes: TranslationEditorContentItem[]) => {
|
|
6
|
+
return nodes.sort((a, b) => (a.attrs?.start ?? 0) - (b.attrs?.start ?? 0));
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
export const insert = (
|
|
10
|
+
node: TranslationEditorContentItem,
|
|
11
|
+
nodes: TranslationEditorContentItem[],
|
|
12
|
+
) => {
|
|
13
|
+
// unshift to prioritize new block when sorting
|
|
14
|
+
nodes.unshift(node);
|
|
15
|
+
return sort(nodes);
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export const splitContentAt: Transformer = (ctx) => {
|
|
19
|
+
const { root, parent, annotation, transform } = ctx;
|
|
20
|
+
const { start, end } = annotation;
|
|
21
|
+
|
|
22
|
+
if (start !== end) {
|
|
23
|
+
console.warn(
|
|
24
|
+
'splitContentAt: transformer expects to find an annotation with zero-length (insertion point).',
|
|
25
|
+
);
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
if (!parent || !parent.content) {
|
|
30
|
+
console.warn(
|
|
31
|
+
'splitContentAt: transformer expects to find a parent block with content.',
|
|
32
|
+
);
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const annotationEnd = end;
|
|
37
|
+
const currentContent = parent.content || [];
|
|
38
|
+
const newContent = [];
|
|
39
|
+
|
|
40
|
+
for (const item of currentContent) {
|
|
41
|
+
const { prefix, middle, suffix } = splitNode(item, start, end);
|
|
42
|
+
|
|
43
|
+
const lastPrefix = prefix.at(-1);
|
|
44
|
+
const firstSuffix = suffix[0];
|
|
45
|
+
|
|
46
|
+
if (annotationEnd === lastPrefix?.attrs?.end) {
|
|
47
|
+
transform?.({ root, parent, block: lastPrefix, annotation });
|
|
48
|
+
} else if (annotationEnd === firstSuffix?.attrs?.end) {
|
|
49
|
+
transform?.({ root, parent, block: firstSuffix, annotation });
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
newContent.push(...prefix);
|
|
53
|
+
newContent.push(...middle);
|
|
54
|
+
newContent.push(...suffix);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
parent.content = newContent;
|
|
58
|
+
};
|