|
| 1 | +import { describe, it, expect, beforeEach, afterEach } from 'vitest'; |
| 2 | +import { EditorState } from 'prosemirror-state'; |
| 3 | +import type { Node as PMNode, Schema } from 'prosemirror-model'; |
| 4 | +import { initTestEditor } from '@tests/helpers/helpers.js'; |
| 5 | +import { applyEditableSlotAtInlineBoundary } from './ensure-editable-slot-inline-boundary.js'; |
| 6 | + |
| 7 | +function findStructuredContent(doc: PMNode): { node: PMNode; pos: number } | null { |
| 8 | + let found: { node: PMNode; pos: number } | null = null; |
| 9 | + doc.descendants((node, pos) => { |
| 10 | + if (node.type.name === 'structuredContent') { |
| 11 | + found = { node, pos }; |
| 12 | + return false; |
| 13 | + } |
| 14 | + return true; |
| 15 | + }); |
| 16 | + return found; |
| 17 | +} |
| 18 | + |
| 19 | +function zwspCount(text: string): number { |
| 20 | + return (text.match(/\u200B/g) ?? []).length; |
| 21 | +} |
| 22 | + |
| 23 | +describe('applyEditableSlotAtInlineBoundary', () => { |
| 24 | + let schema: Schema; |
| 25 | + let destroy: (() => void) | undefined; |
| 26 | + |
| 27 | + beforeEach(() => { |
| 28 | + const { editor } = initTestEditor(); |
| 29 | + schema = editor.schema; |
| 30 | + destroy = () => editor.destroy(); |
| 31 | + }); |
| 32 | + |
| 33 | + afterEach(() => { |
| 34 | + destroy?.(); |
| 35 | + destroy = undefined; |
| 36 | + }); |
| 37 | + |
| 38 | + it('inserts zero-width space after trailing inline SDT (direction after)', () => { |
| 39 | + const inlineSdt = schema.nodes.structuredContent.create({ id: 'i1' }, schema.text('Field')); |
| 40 | + const doc = schema.nodes.doc.create(null, [schema.nodes.paragraph.create(null, [schema.text('A '), inlineSdt])]); |
| 41 | + const sdt = findStructuredContent(doc); |
| 42 | + expect(sdt).not.toBeNull(); |
| 43 | + const afterSdt = sdt!.pos + sdt!.node.nodeSize; |
| 44 | + |
| 45 | + const state = EditorState.create({ schema, doc }); |
| 46 | + const tr = applyEditableSlotAtInlineBoundary(state.tr, afterSdt, 'after'); |
| 47 | + |
| 48 | + expect(tr.docChanged).toBe(true); |
| 49 | + expect(zwspCount(tr.doc.textContent)).toBe(1); |
| 50 | + expect(tr.selection.from).toBe(afterSdt + 1); |
| 51 | + expect(tr.selection.empty).toBe(true); |
| 52 | + }); |
| 53 | + |
| 54 | + it('does not insert when text follows inline SDT (direction after)', () => { |
| 55 | + const inlineSdt = schema.nodes.structuredContent.create({ id: 'i1' }, schema.text('Field')); |
| 56 | + const doc = schema.nodes.doc.create(null, [ |
| 57 | + schema.nodes.paragraph.create(null, [schema.text('A '), inlineSdt, schema.text(' Z')]), |
| 58 | + ]); |
| 59 | + const sdt = findStructuredContent(doc); |
| 60 | + expect(sdt).not.toBeNull(); |
| 61 | + const afterSdt = sdt!.pos + sdt!.node.nodeSize; |
| 62 | + const beforeText = doc.textContent; |
| 63 | + |
| 64 | + const state = EditorState.create({ schema, doc }); |
| 65 | + const tr = applyEditableSlotAtInlineBoundary(state.tr, afterSdt, 'after'); |
| 66 | + |
| 67 | + expect(tr.docChanged).toBe(false); |
| 68 | + expect(tr.doc.textContent).toBe(beforeText); |
| 69 | + expect(tr.selection.from).toBe(afterSdt); |
| 70 | + }); |
| 71 | + |
| 72 | + it('inserts zero-width space before leading inline SDT (direction before)', () => { |
| 73 | + const inlineSdt = schema.nodes.structuredContent.create({ id: 'i1' }, schema.text('Field')); |
| 74 | + const doc = schema.nodes.doc.create(null, [schema.nodes.paragraph.create(null, [inlineSdt, schema.text(' tail')])]); |
| 75 | + const sdt = findStructuredContent(doc); |
| 76 | + expect(sdt).not.toBeNull(); |
| 77 | + const beforeSdt = sdt!.pos; |
| 78 | + |
| 79 | + const state = EditorState.create({ schema, doc }); |
| 80 | + const tr = applyEditableSlotAtInlineBoundary(state.tr, beforeSdt, 'before'); |
| 81 | + |
| 82 | + expect(tr.docChanged).toBe(true); |
| 83 | + expect(zwspCount(tr.doc.textContent)).toBe(1); |
| 84 | + expect(tr.doc.textContent).toContain('tail'); |
| 85 | + expect(tr.selection.from).toBe(beforeSdt + 1); |
| 86 | + }); |
| 87 | + |
| 88 | + it('does not insert when text precedes inline SDT (direction before)', () => { |
| 89 | + const inlineSdt = schema.nodes.structuredContent.create({ id: 'i1' }, schema.text('Field')); |
| 90 | + const doc = schema.nodes.doc.create(null, [schema.nodes.paragraph.create(null, [schema.text('A '), inlineSdt])]); |
| 91 | + const sdt = findStructuredContent(doc); |
| 92 | + expect(sdt).not.toBeNull(); |
| 93 | + const beforeSdt = sdt!.pos; |
| 94 | + const beforeText = doc.textContent; |
| 95 | + |
| 96 | + const state = EditorState.create({ schema, doc }); |
| 97 | + const tr = applyEditableSlotAtInlineBoundary(state.tr, beforeSdt, 'before'); |
| 98 | + |
| 99 | + expect(tr.docChanged).toBe(false); |
| 100 | + expect(tr.doc.textContent).toBe(beforeText); |
| 101 | + expect(tr.selection.from).toBe(beforeSdt); |
| 102 | + }); |
| 103 | + |
| 104 | + it('inserts when following sibling is an empty run (direction after)', () => { |
| 105 | + const inlineSdt = schema.nodes.structuredContent.create({ id: 'i1' }, schema.text('Field')); |
| 106 | + const emptyRun = schema.nodes.run.create(); |
| 107 | + const doc = schema.nodes.doc.create(null, [schema.nodes.paragraph.create(null, [inlineSdt, emptyRun])]); |
| 108 | + const sdt = findStructuredContent(doc); |
| 109 | + expect(sdt).not.toBeNull(); |
| 110 | + const afterSdt = sdt!.pos + sdt!.node.nodeSize; |
| 111 | + |
| 112 | + const state = EditorState.create({ schema, doc }); |
| 113 | + const tr = applyEditableSlotAtInlineBoundary(state.tr, afterSdt, 'after'); |
| 114 | + |
| 115 | + expect(tr.docChanged).toBe(true); |
| 116 | + expect(zwspCount(tr.doc.textContent)).toBe(1); |
| 117 | + expect(tr.selection.from).toBe(afterSdt + 1); |
| 118 | + }); |
| 119 | + |
| 120 | + it('inserts when preceding sibling is an empty run (direction before)', () => { |
| 121 | + const inlineSdt = schema.nodes.structuredContent.create({ id: 'i1' }, schema.text('Field')); |
| 122 | + const emptyRun = schema.nodes.run.create(); |
| 123 | + const doc = schema.nodes.doc.create(null, [schema.nodes.paragraph.create(null, [emptyRun, inlineSdt])]); |
| 124 | + const sdt = findStructuredContent(doc); |
| 125 | + expect(sdt).not.toBeNull(); |
| 126 | + const beforeSdt = sdt!.pos; |
| 127 | + |
| 128 | + const state = EditorState.create({ schema, doc }); |
| 129 | + const tr = applyEditableSlotAtInlineBoundary(state.tr, beforeSdt, 'before'); |
| 130 | + |
| 131 | + expect(tr.docChanged).toBe(true); |
| 132 | + expect(zwspCount(tr.doc.textContent)).toBe(1); |
| 133 | + expect(tr.selection.from).toBe(beforeSdt + 1); |
| 134 | + }); |
| 135 | + |
| 136 | + it('clamps an oversized position to doc end then may insert zero-width space (direction after)', () => { |
| 137 | + const inlineSdt = schema.nodes.structuredContent.create({ id: 'i1' }, schema.text('Field')); |
| 138 | + const doc = schema.nodes.doc.create(null, [schema.nodes.paragraph.create(null, [inlineSdt])]); |
| 139 | + const sizeBefore = doc.content.size; |
| 140 | + |
| 141 | + const state = EditorState.create({ schema, doc }); |
| 142 | + const tr = applyEditableSlotAtInlineBoundary(state.tr, sizeBefore + 999, 'after'); |
| 143 | + |
| 144 | + // Clamps to `doc.content.size`; gap after last inline has no node → ZWSP + caret (size may grow by schema-specific steps). |
| 145 | + expect(tr.docChanged).toBe(true); |
| 146 | + expect(tr.doc.content.size).toBeGreaterThan(sizeBefore); |
| 147 | + expect(zwspCount(tr.doc.textContent)).toBeGreaterThanOrEqual(1); |
| 148 | + expect(tr.selection.from).toBeGreaterThan(0); |
| 149 | + expect(tr.selection.from).toBeLessThanOrEqual(tr.doc.content.size); |
| 150 | + }); |
| 151 | + |
| 152 | + it('clamps a negative position to 0 then may insert zero-width space (direction before)', () => { |
| 153 | + const inlineSdt = schema.nodes.structuredContent.create({ id: 'i1' }, schema.text('Field')); |
| 154 | + const doc = schema.nodes.doc.create(null, [schema.nodes.paragraph.create(null, [inlineSdt])]); |
| 155 | + |
| 156 | + const state = EditorState.create({ schema, doc }); |
| 157 | + const tr = applyEditableSlotAtInlineBoundary(state.tr, -999, 'before'); |
| 158 | + |
| 159 | + expect(tr.docChanged).toBe(true); |
| 160 | + expect(zwspCount(tr.doc.textContent)).toBe(1); |
| 161 | + expect(tr.selection.from).toBe(1); |
| 162 | + }); |
| 163 | +}); |
0 commit comments