|
| 1 | +import { TextSelection } from "prosemirror-state"; |
1 | 2 | import { afterEach, beforeEach, describe, expect, it } from "vitest";
|
2 | 3 | import { BlockNoteEditor } from "../../../editor/BlockNoteEditor";
|
3 | 4 |
|
@@ -101,3 +102,142 @@ describe("Test HTML conversion", () => {
|
101 | 102 | });
|
102 | 103 | }
|
103 | 104 | });
|
| 105 | + |
| 106 | +// Fragments created from ProseMirror selections don't always conform to the |
| 107 | +// schema. This is because ProseMirror preserves the full ancestry of selected |
| 108 | +// nodes, but not the siblings of ancestor nodes. These tests are to verify that |
| 109 | +// Fragments like this are exported to HTML properly, as they can't be created |
| 110 | +// from Block objects like all the other test cases (Block object conversions |
| 111 | +// always conform to the schema). |
| 112 | +describe("Test ProseMirror fragment edge case conversion", () => { |
| 113 | + let editor: BlockNoteEditor; |
| 114 | + const div = document.createElement("div"); |
| 115 | + beforeEach(() => { |
| 116 | + editor = BlockNoteEditor.create(); |
| 117 | + editor.mount(div); |
| 118 | + }); |
| 119 | + |
| 120 | + afterEach(() => { |
| 121 | + editor.mount(undefined); |
| 122 | + editor._tiptapEditor.destroy(); |
| 123 | + editor = undefined as any; |
| 124 | + |
| 125 | + delete (window as Window & { __TEST_OPTIONS?: any }).__TEST_OPTIONS; |
| 126 | + }); |
| 127 | + |
| 128 | + // When the selection starts in a nested block, the Fragment from it omits the |
| 129 | + // `blockContent` node of the parent `blockContainer` if it's not also |
| 130 | + // included in the selection. In the schema, `blockContainer` nodes should |
| 131 | + // contain a single `blockContent` node, so this edge case needs to be tested. |
| 132 | + describe("No block content", () => { |
| 133 | + const blocks: PartialBlock[] = [ |
| 134 | + { |
| 135 | + type: "paragraph", |
| 136 | + content: "Paragraph 1", |
| 137 | + children: [ |
| 138 | + { |
| 139 | + type: "paragraph", |
| 140 | + content: "Nested Paragraph 1", |
| 141 | + }, |
| 142 | + { |
| 143 | + type: "paragraph", |
| 144 | + content: "Nested Paragraph 2", |
| 145 | + }, |
| 146 | + { |
| 147 | + type: "paragraph", |
| 148 | + content: "Nested Paragraph 3", |
| 149 | + }, |
| 150 | + ], |
| 151 | + }, |
| 152 | + { |
| 153 | + type: "paragraph", |
| 154 | + content: "Paragraph 2", |
| 155 | + children: [ |
| 156 | + { |
| 157 | + type: "paragraph", |
| 158 | + content: "Nested Paragraph 1", |
| 159 | + }, |
| 160 | + { |
| 161 | + type: "paragraph", |
| 162 | + content: "Nested Paragraph 2", |
| 163 | + }, |
| 164 | + { |
| 165 | + type: "paragraph", |
| 166 | + content: "Nested Paragraph 3", |
| 167 | + }, |
| 168 | + ], |
| 169 | + }, |
| 170 | + ]; |
| 171 | + |
| 172 | + beforeEach(() => { |
| 173 | + editor.replaceBlocks(editor.document, blocks); |
| 174 | + }); |
| 175 | + |
| 176 | + it("Selection within a block's children", () => { |
| 177 | + // Selection starts and ends within the first block's children. |
| 178 | + editor.prosemirrorView.dispatch( |
| 179 | + editor._tiptapEditor.state.tr.setSelection( |
| 180 | + TextSelection.create(editor._tiptapEditor.state.doc, 18, 80) |
| 181 | + ) |
| 182 | + ); |
| 183 | + |
| 184 | + const copiedFragment = |
| 185 | + editor._tiptapEditor.state.selection.content().content; |
| 186 | + |
| 187 | + const exporter = createExternalHTMLExporter( |
| 188 | + editor._tiptapEditor.schema, |
| 189 | + editor |
| 190 | + ); |
| 191 | + const externalHTML = exporter.exportProseMirrorFragment(copiedFragment); |
| 192 | + expect(externalHTML).toMatchFileSnapshot( |
| 193 | + "./__snapshots_fragment_edge_cases__/" + |
| 194 | + "selectionWithinBlockChildren.html" |
| 195 | + ); |
| 196 | + }); |
| 197 | + |
| 198 | + it("Selection leaves a block's children", () => { |
| 199 | + // Selection starts and ends within the first block's children and ends |
| 200 | + // outside, at a shallower nesting level in the second block. |
| 201 | + editor.prosemirrorView.dispatch( |
| 202 | + editor._tiptapEditor.state.tr.setSelection( |
| 203 | + TextSelection.create(editor._tiptapEditor.state.doc, 18, 97) |
| 204 | + ) |
| 205 | + ); |
| 206 | + |
| 207 | + const copiedFragment = |
| 208 | + editor._tiptapEditor.state.selection.content().content; |
| 209 | + |
| 210 | + const exporter = createExternalHTMLExporter( |
| 211 | + editor._tiptapEditor.schema, |
| 212 | + editor |
| 213 | + ); |
| 214 | + const externalHTML = exporter.exportProseMirrorFragment(copiedFragment); |
| 215 | + expect(externalHTML).toMatchFileSnapshot( |
| 216 | + "./__snapshots_fragment_edge_cases__/" + |
| 217 | + "selectionLeavesBlockChildren.html" |
| 218 | + ); |
| 219 | + }); |
| 220 | + |
| 221 | + it("Selection spans multiple blocks' children", () => { |
| 222 | + // Selection starts and ends within the first block's children and ends |
| 223 | + // within the second block's children. |
| 224 | + editor.prosemirrorView.dispatch( |
| 225 | + editor._tiptapEditor.state.tr.setSelection( |
| 226 | + TextSelection.create(editor._tiptapEditor.state.doc, 18, 163) |
| 227 | + ) |
| 228 | + ); |
| 229 | + |
| 230 | + const copiedFragment = |
| 231 | + editor._tiptapEditor.state.selection.content().content; |
| 232 | + const exporter = createExternalHTMLExporter( |
| 233 | + editor._tiptapEditor.schema, |
| 234 | + editor |
| 235 | + ); |
| 236 | + const externalHTML = exporter.exportProseMirrorFragment(copiedFragment); |
| 237 | + expect(externalHTML).toMatchFileSnapshot( |
| 238 | + "./__snapshots_fragment_edge_cases__/" + |
| 239 | + "selectionSpansBlocksChildren.html" |
| 240 | + ); |
| 241 | + }); |
| 242 | + }); |
| 243 | +}); |
0 commit comments