|
| 1 | +import { describe, it, expect, beforeEach, afterEach } from 'vitest'; |
| 2 | +import { EditorState, NodeSelection, Selection, TextSelection } from 'prosemirror-state'; |
| 3 | +import { initTestEditor } from '@tests/helpers/helpers.js'; |
| 4 | +import { getViewModeSelectionWithoutStructuredContent } from './getViewModeSelectionWithoutStructuredContent.js'; |
| 5 | + |
| 6 | +function findNode(doc, nodeType) { |
| 7 | + let result = null; |
| 8 | + |
| 9 | + doc.descendants((node, pos) => { |
| 10 | + if (node.type.name === nodeType) { |
| 11 | + result = { node, pos }; |
| 12 | + return false; |
| 13 | + } |
| 14 | + }); |
| 15 | + |
| 16 | + return result; |
| 17 | +} |
| 18 | + |
| 19 | +describe('getViewModeSelectionWithoutStructuredContent', () => { |
| 20 | + let editor; |
| 21 | + let schema; |
| 22 | + |
| 23 | + beforeEach(() => { |
| 24 | + ({ editor } = initTestEditor()); |
| 25 | + ({ schema } = editor); |
| 26 | + }); |
| 27 | + |
| 28 | + afterEach(() => { |
| 29 | + editor?.destroy(); |
| 30 | + editor = null; |
| 31 | + schema = null; |
| 32 | + }); |
| 33 | + |
| 34 | + function createState(doc, selection) { |
| 35 | + return EditorState.create({ |
| 36 | + schema, |
| 37 | + doc, |
| 38 | + selection, |
| 39 | + plugins: editor.state.plugins, |
| 40 | + }); |
| 41 | + } |
| 42 | + |
| 43 | + it('normalizes structured content node selections', () => { |
| 44 | + const blockSdt = schema.nodes.structuredContentBlock.create({ id: 'block-1' }, [ |
| 45 | + schema.nodes.paragraph.create(null, schema.text('Block field')), |
| 46 | + ]); |
| 47 | + const doc = schema.nodes.doc.create(null, [schema.nodes.paragraph.create(null, schema.text('Before')), blockSdt]); |
| 48 | + const sdt = findNode(doc, 'structuredContentBlock'); |
| 49 | + const state = createState(doc, NodeSelection.create(doc, sdt.pos)); |
| 50 | + |
| 51 | + const result = getViewModeSelectionWithoutStructuredContent(state); |
| 52 | + const expected = Selection.near(doc.resolve(sdt.pos), -1); |
| 53 | + |
| 54 | + expect(result?.eq(expected)).toBe(true); |
| 55 | + }); |
| 56 | + |
| 57 | + it('returns null for block structured content node selections when no outside selection exists', () => { |
| 58 | + const blockSdt = schema.nodes.structuredContentBlock.create({ id: 'block-1' }, [ |
| 59 | + schema.nodes.paragraph.create(null, schema.text('Block field')), |
| 60 | + ]); |
| 61 | + const doc = schema.nodes.doc.create(null, [blockSdt]); |
| 62 | + const sdt = findNode(doc, 'structuredContentBlock'); |
| 63 | + const state = createState(doc, NodeSelection.create(doc, sdt.pos)); |
| 64 | + |
| 65 | + expect(getViewModeSelectionWithoutStructuredContent(state)).toBeNull(); |
| 66 | + }); |
| 67 | + |
| 68 | + it('normalizes non-empty text selections fully inside the same inline structured content', () => { |
| 69 | + const inlineSdt = schema.nodes.structuredContent.create({ id: 'inline-1' }, schema.text('Field')); |
| 70 | + const paragraph = schema.nodes.paragraph.create(null, [schema.text('A '), inlineSdt, schema.text(' Z')]); |
| 71 | + const doc = schema.nodes.doc.create(null, [paragraph]); |
| 72 | + const sdt = findNode(doc, 'structuredContent'); |
| 73 | + const selection = TextSelection.create(doc, sdt.pos + 2, sdt.pos + inlineSdt.nodeSize - 1); |
| 74 | + const state = createState(doc, selection); |
| 75 | + |
| 76 | + const result = getViewModeSelectionWithoutStructuredContent(state); |
| 77 | + const expected = Selection.near(doc.resolve(sdt.pos), -1); |
| 78 | + |
| 79 | + expect(result?.eq(expected)).toBe(true); |
| 80 | + }); |
| 81 | + |
| 82 | + it('returns null for collapsed cursor selections inside structured content', () => { |
| 83 | + const inlineSdt = schema.nodes.structuredContent.create({ id: 'inline-1' }, schema.text('Field')); |
| 84 | + const paragraph = schema.nodes.paragraph.create(null, [schema.text('A '), inlineSdt, schema.text(' Z')]); |
| 85 | + const doc = schema.nodes.doc.create(null, [paragraph]); |
| 86 | + const sdt = findNode(doc, 'structuredContent'); |
| 87 | + const state = createState(doc, TextSelection.create(doc, sdt.pos + 2)); |
| 88 | + |
| 89 | + expect(getViewModeSelectionWithoutStructuredContent(state)).toBeNull(); |
| 90 | + }); |
| 91 | + |
| 92 | + it('returns null for selections outside structured content', () => { |
| 93 | + const paragraph = schema.nodes.paragraph.create(null, schema.text('Plain text')); |
| 94 | + const doc = schema.nodes.doc.create(null, [paragraph]); |
| 95 | + const state = createState(doc, TextSelection.create(doc, 1, 5)); |
| 96 | + |
| 97 | + expect(getViewModeSelectionWithoutStructuredContent(state)).toBeNull(); |
| 98 | + }); |
| 99 | + |
| 100 | + it('returns null when a selection crosses structured content boundaries', () => { |
| 101 | + const inlineSdt = schema.nodes.structuredContent.create({ id: 'inline-1' }, schema.text('Field')); |
| 102 | + const paragraph = schema.nodes.paragraph.create(null, [schema.text('A '), inlineSdt, schema.text(' Z')]); |
| 103 | + const doc = schema.nodes.doc.create(null, [paragraph]); |
| 104 | + const sdt = findNode(doc, 'structuredContent'); |
| 105 | + const state = createState(doc, TextSelection.create(doc, sdt.pos - 1, sdt.pos + 3)); |
| 106 | + |
| 107 | + expect(getViewModeSelectionWithoutStructuredContent(state)).toBeNull(); |
| 108 | + }); |
| 109 | +}); |
0 commit comments