|
| 1 | +import { describe, it, expect, vi } from 'vitest'; |
| 2 | +import { Schema } from 'prosemirror-model'; |
| 3 | +import { EditorState, TextSelection } from 'prosemirror-state'; |
| 4 | +import { |
| 5 | + selectBlockSdtAfterTextBlockEnd, |
| 6 | + selectBlockSdtBeforeTextBlockStart, |
| 7 | +} from './selectBlockSdtAtTextBlockBoundary.js'; |
| 8 | +import { findFirstContentCursorPosInNode, findLastContentCursorPosInNode } from './helpers/textPositions.js'; |
| 9 | + |
| 10 | +const makeSchema = () => |
| 11 | + new Schema({ |
| 12 | + nodes: { |
| 13 | + doc: { content: 'block+' }, |
| 14 | + paragraph: { group: 'block', content: 'inline*' }, |
| 15 | + run: { inline: true, group: 'inline', content: 'inline*' }, |
| 16 | + structuredContentBlock: { |
| 17 | + group: 'block', |
| 18 | + content: 'block*', |
| 19 | + isolating: true, |
| 20 | + attrs: { |
| 21 | + lockMode: { default: 'unlocked' }, |
| 22 | + }, |
| 23 | + }, |
| 24 | + mathBlock: { group: 'block', atom: true }, |
| 25 | + image: { inline: true, group: 'inline', atom: true }, |
| 26 | + text: { group: 'inline' }, |
| 27 | + }, |
| 28 | + marks: {}, |
| 29 | + }); |
| 30 | + |
| 31 | +const run = (schema, text) => schema.nodes.run.create(null, schema.text(text)); |
| 32 | +const paragraph = (schema, text) => schema.nodes.paragraph.create(null, run(schema, text)); |
| 33 | + |
| 34 | +const findTextPos = (doc, text, offset = 0) => { |
| 35 | + let found = null; |
| 36 | + doc.descendants((node, pos) => { |
| 37 | + if (!node.isText || found != null) return found == null; |
| 38 | + const index = node.text.indexOf(text); |
| 39 | + if (index === -1) return true; |
| 40 | + found = pos + index + offset; |
| 41 | + return false; |
| 42 | + }); |
| 43 | + expect(found).not.toBeNull(); |
| 44 | + return found; |
| 45 | +}; |
| 46 | + |
| 47 | +const findBlockSdt = (doc) => { |
| 48 | + let result = null; |
| 49 | + doc.descendants((node, pos) => { |
| 50 | + if (node.type.name !== 'structuredContentBlock') return true; |
| 51 | + result = { node, pos, end: pos + node.nodeSize }; |
| 52 | + return false; |
| 53 | + }); |
| 54 | + expect(result).not.toBeNull(); |
| 55 | + return result; |
| 56 | +}; |
| 57 | + |
| 58 | +const findBlockSdtByText = (doc, text) => { |
| 59 | + let result = null; |
| 60 | + doc.descendants((node, pos) => { |
| 61 | + if (node.type.name !== 'structuredContentBlock' || node.textContent !== text) return true; |
| 62 | + result = { node, pos, end: pos + node.nodeSize }; |
| 63 | + return false; |
| 64 | + }); |
| 65 | + expect(result).not.toBeNull(); |
| 66 | + return result; |
| 67 | +}; |
| 68 | + |
| 69 | +const makeDoc = (schema, lockMode = 'contentLocked') => { |
| 70 | + const imageRun = schema.nodes.run.create(null, schema.nodes.image.create()); |
| 71 | + const sdt = schema.nodes.structuredContentBlock.create({ lockMode }, [ |
| 72 | + paragraph(schema, 'Inner text'), |
| 73 | + schema.nodes.paragraph.create(null, imageRun), |
| 74 | + ]); |
| 75 | + return schema.node('doc', null, [paragraph(schema, 'Before'), sdt, paragraph(schema, 'After')]); |
| 76 | +}; |
| 77 | + |
| 78 | +const makeNestedDoc = (schema, lockMode = 'contentLocked') => { |
| 79 | + const innerSdt = schema.nodes.structuredContentBlock.create({ lockMode }, [paragraph(schema, 'Nested text')]); |
| 80 | + const outerSdt = schema.nodes.structuredContentBlock.create({ lockMode: 'unlocked' }, [ |
| 81 | + paragraph(schema, 'Outer before'), |
| 82 | + innerSdt, |
| 83 | + paragraph(schema, 'Outer after'), |
| 84 | + ]); |
| 85 | + return schema.node('doc', null, [paragraph(schema, 'Before'), outerSdt, paragraph(schema, 'After')]); |
| 86 | +}; |
| 87 | + |
| 88 | +const makeEmptyBlockSdtDoc = (schema) => { |
| 89 | + const sdt = schema.nodes.structuredContentBlock.create({ lockMode: 'contentLocked' }, [ |
| 90 | + schema.nodes.paragraph.create(), |
| 91 | + ]); |
| 92 | + return schema.node('doc', null, [paragraph(schema, 'Before'), sdt, paragraph(schema, 'After')]); |
| 93 | +}; |
| 94 | + |
| 95 | +const makeAtomBlockSdtDoc = (schema) => { |
| 96 | + const sdt = schema.nodes.structuredContentBlock.create({ lockMode: 'contentLocked' }, [ |
| 97 | + schema.nodes.mathBlock.create(), |
| 98 | + ]); |
| 99 | + return schema.node('doc', null, [paragraph(schema, 'Before'), sdt, paragraph(schema, 'After')]); |
| 100 | +}; |
| 101 | + |
| 102 | +describe('selectBlockSdtBeforeTextBlockStart', () => { |
| 103 | + it.each(['unlocked', 'sdtLocked', 'contentLocked', 'sdtContentLocked'])( |
| 104 | + 'selects the previous %s block SDT content from the following textblock start', |
| 105 | + (lockMode) => { |
| 106 | + const schema = makeSchema(); |
| 107 | + const doc = makeDoc(schema, lockMode); |
| 108 | + const sdt = findBlockSdt(doc); |
| 109 | + const afterStart = findTextPos(doc, 'After'); |
| 110 | + const state = EditorState.create({ schema, doc, selection: TextSelection.create(doc, afterStart) }); |
| 111 | + |
| 112 | + let dispatched; |
| 113 | + const ok = selectBlockSdtBeforeTextBlockStart()({ state, dispatch: (tr) => (dispatched = tr) }); |
| 114 | + |
| 115 | + expect(ok).toBe(true); |
| 116 | + expect(dispatched).toBeDefined(); |
| 117 | + expect(dispatched.selection).toBeInstanceOf(TextSelection); |
| 118 | + expect(dispatched.selection.from).toBe(findFirstContentCursorPosInNode(sdt.node, sdt.pos)); |
| 119 | + expect(dispatched.selection.to).toBe(findLastContentCursorPosInNode(sdt.node, sdt.pos)); |
| 120 | + }, |
| 121 | + ); |
| 122 | + |
| 123 | + it('returns false away from the following textblock start', () => { |
| 124 | + const schema = makeSchema(); |
| 125 | + const doc = makeDoc(schema); |
| 126 | + const state = EditorState.create({ |
| 127 | + schema, |
| 128 | + doc, |
| 129 | + selection: TextSelection.create(doc, findTextPos(doc, 'After', 1)), |
| 130 | + }); |
| 131 | + const dispatch = vi.fn(); |
| 132 | + |
| 133 | + const ok = selectBlockSdtBeforeTextBlockStart()({ state, dispatch }); |
| 134 | + |
| 135 | + expect(ok).toBe(false); |
| 136 | + expect(dispatch).not.toHaveBeenCalled(); |
| 137 | + }); |
| 138 | + |
| 139 | + it.each([ |
| 140 | + ['empty paragraph', makeEmptyBlockSdtDoc], |
| 141 | + ['block atom', makeAtomBlockSdtDoc], |
| 142 | + ])('returns false for previous block SDT with %s content', (_, makeAdjacentDoc) => { |
| 143 | + const schema = makeSchema(); |
| 144 | + const doc = makeAdjacentDoc(schema); |
| 145 | + const afterStart = findTextPos(doc, 'After'); |
| 146 | + const state = EditorState.create({ schema, doc, selection: TextSelection.create(doc, afterStart) }); |
| 147 | + const dispatch = vi.fn(); |
| 148 | + |
| 149 | + const ok = selectBlockSdtBeforeTextBlockStart()({ state, dispatch }); |
| 150 | + |
| 151 | + expect(ok).toBe(false); |
| 152 | + expect(dispatch).not.toHaveBeenCalled(); |
| 153 | + }); |
| 154 | + |
| 155 | + it('selects nested previous block SDT content from the following nested textblock start', () => { |
| 156 | + const schema = makeSchema(); |
| 157 | + const doc = makeNestedDoc(schema); |
| 158 | + const nestedSdt = findBlockSdtByText(doc, 'Nested text'); |
| 159 | + const outerAfterStart = findTextPos(doc, 'Outer after'); |
| 160 | + const state = EditorState.create({ schema, doc, selection: TextSelection.create(doc, outerAfterStart) }); |
| 161 | + |
| 162 | + let dispatched; |
| 163 | + const ok = selectBlockSdtBeforeTextBlockStart()({ state, dispatch: (tr) => (dispatched = tr) }); |
| 164 | + |
| 165 | + expect(ok).toBe(true); |
| 166 | + expect(dispatched.selection.from).toBe(findFirstContentCursorPosInNode(nestedSdt.node, nestedSdt.pos)); |
| 167 | + expect(dispatched.selection.to).toBe(findLastContentCursorPosInNode(nestedSdt.node, nestedSdt.pos)); |
| 168 | + }); |
| 169 | +}); |
| 170 | + |
| 171 | +describe('selectBlockSdtAfterTextBlockEnd', () => { |
| 172 | + it.each(['unlocked', 'sdtLocked', 'contentLocked', 'sdtContentLocked'])( |
| 173 | + 'selects the next %s block SDT content from the preceding textblock end', |
| 174 | + (lockMode) => { |
| 175 | + const schema = makeSchema(); |
| 176 | + const doc = makeDoc(schema, lockMode); |
| 177 | + const sdt = findBlockSdt(doc); |
| 178 | + const beforeEnd = findTextPos(doc, 'Before', 'Before'.length); |
| 179 | + const state = EditorState.create({ schema, doc, selection: TextSelection.create(doc, beforeEnd) }); |
| 180 | + |
| 181 | + let dispatched; |
| 182 | + const ok = selectBlockSdtAfterTextBlockEnd()({ state, dispatch: (tr) => (dispatched = tr) }); |
| 183 | + |
| 184 | + expect(ok).toBe(true); |
| 185 | + expect(dispatched).toBeDefined(); |
| 186 | + expect(dispatched.selection).toBeInstanceOf(TextSelection); |
| 187 | + expect(dispatched.selection.from).toBe(findFirstContentCursorPosInNode(sdt.node, sdt.pos)); |
| 188 | + expect(dispatched.selection.to).toBe(findLastContentCursorPosInNode(sdt.node, sdt.pos)); |
| 189 | + }, |
| 190 | + ); |
| 191 | + |
| 192 | + it.each([ |
| 193 | + ['empty paragraph', makeEmptyBlockSdtDoc], |
| 194 | + ['block atom', makeAtomBlockSdtDoc], |
| 195 | + ])('returns false for next block SDT with %s content', (_, makeAdjacentDoc) => { |
| 196 | + const schema = makeSchema(); |
| 197 | + const doc = makeAdjacentDoc(schema); |
| 198 | + const beforeEnd = findTextPos(doc, 'Before', 'Before'.length); |
| 199 | + const state = EditorState.create({ schema, doc, selection: TextSelection.create(doc, beforeEnd) }); |
| 200 | + const dispatch = vi.fn(); |
| 201 | + |
| 202 | + const ok = selectBlockSdtAfterTextBlockEnd()({ state, dispatch }); |
| 203 | + |
| 204 | + expect(ok).toBe(false); |
| 205 | + expect(dispatch).not.toHaveBeenCalled(); |
| 206 | + }); |
| 207 | + |
| 208 | + it('selects nested next block SDT content from the preceding nested textblock end', () => { |
| 209 | + const schema = makeSchema(); |
| 210 | + const doc = makeNestedDoc(schema); |
| 211 | + const nestedSdt = findBlockSdtByText(doc, 'Nested text'); |
| 212 | + const outerBeforeEnd = findTextPos(doc, 'Outer before', 'Outer before'.length); |
| 213 | + const state = EditorState.create({ schema, doc, selection: TextSelection.create(doc, outerBeforeEnd) }); |
| 214 | + |
| 215 | + let dispatched; |
| 216 | + const ok = selectBlockSdtAfterTextBlockEnd()({ state, dispatch: (tr) => (dispatched = tr) }); |
| 217 | + |
| 218 | + expect(ok).toBe(true); |
| 219 | + expect(dispatched.selection.from).toBe(findFirstContentCursorPosInNode(nestedSdt.node, nestedSdt.pos)); |
| 220 | + expect(dispatched.selection.to).toBe(findLastContentCursorPosInNode(nestedSdt.node, nestedSdt.pos)); |
| 221 | + }); |
| 222 | +}); |
0 commit comments