|
| 1 | +import { describe, it, expect, vi } from 'vitest'; |
| 2 | +import { Schema } from 'prosemirror-model'; |
| 3 | +import { EditorState, TextSelection } from 'prosemirror-state'; |
| 4 | +import { deleteBlockSdtAtTextBlockStart } from './deleteBlockSdtAtTextBlockStart.js'; |
| 5 | + |
| 6 | +const makeSchema = () => |
| 7 | + new Schema({ |
| 8 | + nodes: { |
| 9 | + doc: { content: 'block+' }, |
| 10 | + paragraph: { group: 'block', content: 'inline*' }, |
| 11 | + structuredContentBlock: { |
| 12 | + group: 'block', |
| 13 | + content: 'block*', |
| 14 | + isolating: true, |
| 15 | + attrs: { |
| 16 | + lockMode: { default: 'unlocked' }, |
| 17 | + }, |
| 18 | + }, |
| 19 | + text: { group: 'inline' }, |
| 20 | + }, |
| 21 | + marks: {}, |
| 22 | + }); |
| 23 | + |
| 24 | +const makeDoc = (schema, lockMode = 'unlocked', sdtContent = [schema.node('paragraph', null, schema.text('Inner'))]) => |
| 25 | + schema.node('doc', null, [ |
| 26 | + schema.node('paragraph', null, schema.text('Before')), |
| 27 | + schema.node('structuredContentBlock', { lockMode }, sdtContent), |
| 28 | + schema.node('paragraph', null, schema.text('After')), |
| 29 | + ]); |
| 30 | + |
| 31 | +const findBlockSdt = (doc) => { |
| 32 | + let result = null; |
| 33 | + doc.descendants((node, pos) => { |
| 34 | + if (node.type.name === 'structuredContentBlock') { |
| 35 | + result = { node, pos, end: pos + node.nodeSize }; |
| 36 | + return false; |
| 37 | + } |
| 38 | + return true; |
| 39 | + }); |
| 40 | + return result; |
| 41 | +}; |
| 42 | + |
| 43 | +const paragraphStartInSdt = (doc, index = 0) => { |
| 44 | + const sdt = findBlockSdt(doc); |
| 45 | + expect(sdt).not.toBeNull(); |
| 46 | + |
| 47 | + let seen = 0; |
| 48 | + let start = null; |
| 49 | + sdt.node.descendants((node, offset) => { |
| 50 | + if (node.type.name !== 'paragraph') return true; |
| 51 | + if (seen === index) { |
| 52 | + start = sdt.pos + 1 + offset + 1; |
| 53 | + return false; |
| 54 | + } |
| 55 | + seen += 1; |
| 56 | + return true; |
| 57 | + }); |
| 58 | + |
| 59 | + expect(start).not.toBeNull(); |
| 60 | + return start; |
| 61 | +}; |
| 62 | + |
| 63 | +describe('deleteBlockSdtAtTextBlockStart', () => { |
| 64 | + it('deletes an unlocked block SDT when the caret is at the start of its first paragraph', () => { |
| 65 | + const schema = makeSchema(); |
| 66 | + const doc = makeDoc(schema); |
| 67 | + const state = EditorState.create({ schema, doc, selection: TextSelection.create(doc, paragraphStartInSdt(doc)) }); |
| 68 | + |
| 69 | + let dispatched; |
| 70 | + const ok = deleteBlockSdtAtTextBlockStart()({ |
| 71 | + state, |
| 72 | + dispatch: (tr) => { |
| 73 | + dispatched = tr; |
| 74 | + }, |
| 75 | + }); |
| 76 | + |
| 77 | + expect(ok).toBe(true); |
| 78 | + expect(dispatched).toBeDefined(); |
| 79 | + expect(findBlockSdt(dispatched.doc)).toBeNull(); |
| 80 | + expect(dispatched.doc.textContent).toBe('BeforeAfter'); |
| 81 | + }); |
| 82 | + |
| 83 | + it('returns false for sdtLocked so Delete can fall through for in-SDT content edits', () => { |
| 84 | + const schema = makeSchema(); |
| 85 | + const doc = makeDoc(schema, 'sdtLocked'); |
| 86 | + const state = EditorState.create({ schema, doc, selection: TextSelection.create(doc, paragraphStartInSdt(doc)) }); |
| 87 | + const dispatch = vi.fn(); |
| 88 | + |
| 89 | + const ok = deleteBlockSdtAtTextBlockStart()({ state, dispatch }); |
| 90 | + |
| 91 | + expect(ok).toBe(false); |
| 92 | + expect(dispatch).not.toHaveBeenCalled(); |
| 93 | + }); |
| 94 | + |
| 95 | + it('consumes sdtContentLocked block SDT wrapper delete without dispatching', () => { |
| 96 | + const schema = makeSchema(); |
| 97 | + const doc = makeDoc(schema, 'sdtContentLocked'); |
| 98 | + const state = EditorState.create({ schema, doc, selection: TextSelection.create(doc, paragraphStartInSdt(doc)) }); |
| 99 | + const dispatch = vi.fn(); |
| 100 | + |
| 101 | + const ok = deleteBlockSdtAtTextBlockStart()({ state, dispatch }); |
| 102 | + |
| 103 | + expect(ok).toBe(true); |
| 104 | + expect(dispatch).not.toHaveBeenCalled(); |
| 105 | + }); |
| 106 | + |
| 107 | + it('returns false when the caret is not at the paragraph start', () => { |
| 108 | + const schema = makeSchema(); |
| 109 | + const doc = makeDoc(schema); |
| 110 | + const state = EditorState.create({ |
| 111 | + schema, |
| 112 | + doc, |
| 113 | + selection: TextSelection.create(doc, paragraphStartInSdt(doc) + 1), |
| 114 | + }); |
| 115 | + const dispatch = vi.fn(); |
| 116 | + |
| 117 | + const ok = deleteBlockSdtAtTextBlockStart()({ state, dispatch }); |
| 118 | + |
| 119 | + expect(ok).toBe(false); |
| 120 | + expect(dispatch).not.toHaveBeenCalled(); |
| 121 | + }); |
| 122 | + |
| 123 | + it('returns false from later paragraphs inside the same block SDT', () => { |
| 124 | + const schema = makeSchema(); |
| 125 | + const doc = makeDoc(schema, 'unlocked', [ |
| 126 | + schema.node('paragraph', null, schema.text('First')), |
| 127 | + schema.node('paragraph', null, schema.text('Second')), |
| 128 | + ]); |
| 129 | + const state = EditorState.create({ |
| 130 | + schema, |
| 131 | + doc, |
| 132 | + selection: TextSelection.create(doc, paragraphStartInSdt(doc, 1)), |
| 133 | + }); |
| 134 | + const dispatch = vi.fn(); |
| 135 | + |
| 136 | + const ok = deleteBlockSdtAtTextBlockStart()({ state, dispatch }); |
| 137 | + |
| 138 | + expect(ok).toBe(false); |
| 139 | + expect(dispatch).not.toHaveBeenCalled(); |
| 140 | + }); |
| 141 | + |
| 142 | + it('returns false outside a block SDT', () => { |
| 143 | + const schema = makeSchema(); |
| 144 | + const doc = makeDoc(schema); |
| 145 | + const state = EditorState.create({ schema, doc, selection: TextSelection.create(doc, 1) }); |
| 146 | + const dispatch = vi.fn(); |
| 147 | + |
| 148 | + const ok = deleteBlockSdtAtTextBlockStart()({ state, dispatch }); |
| 149 | + |
| 150 | + expect(ok).toBe(false); |
| 151 | + expect(dispatch).not.toHaveBeenCalled(); |
| 152 | + }); |
| 153 | +}); |
0 commit comments