Skip to content

Commit f4d5b20

Browse files
fix(super-editor): skip empty block SDT content selection
1 parent b9ebc98 commit f4d5b20

2 files changed

Lines changed: 48 additions & 0 deletions

File tree

packages/super-editor/src/editors/v1/core/commands/selectBlockSdtAtTextBlockBoundary.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ function selectAdjacentBlockSdtContent(direction) {
6262
const contentStart = findFirstContentCursorPosInNode(node, nodePos);
6363
const contentEnd = findLastContentCursorPosInNode(node, nodePos);
6464
if (contentStart == null || contentEnd == null) return false;
65+
if (contentStart >= contentEnd) return false;
6566

6667
if (dispatch) {
6768
dispatch(state.tr.setSelection(TextSelection.create(state.doc, contentStart, contentEnd)).scrollIntoView());

packages/super-editor/src/editors/v1/core/commands/selectBlockSdtAtTextBlockBoundary.test.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ const makeSchema = () =>
2121
lockMode: { default: 'unlocked' },
2222
},
2323
},
24+
mathBlock: { group: 'block', atom: true },
2425
image: { inline: true, group: 'inline', atom: true },
2526
text: { group: 'inline' },
2627
},
@@ -84,6 +85,20 @@ const makeNestedDoc = (schema, lockMode = 'contentLocked') => {
8485
return schema.node('doc', null, [paragraph(schema, 'Before'), outerSdt, paragraph(schema, 'After')]);
8586
};
8687

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+
87102
describe('selectBlockSdtBeforeTextBlockStart', () => {
88103
it.each(['unlocked', 'sdtLocked', 'contentLocked', 'sdtContentLocked'])(
89104
'selects the previous %s block SDT content from the following textblock start',
@@ -121,6 +136,22 @@ describe('selectBlockSdtBeforeTextBlockStart', () => {
121136
expect(dispatch).not.toHaveBeenCalled();
122137
});
123138

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+
124155
it('selects nested previous block SDT content from the following nested textblock start', () => {
125156
const schema = makeSchema();
126157
const doc = makeNestedDoc(schema);
@@ -158,6 +189,22 @@ describe('selectBlockSdtAfterTextBlockEnd', () => {
158189
},
159190
);
160191

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+
161208
it('selects nested next block SDT content from the preceding nested textblock end', () => {
162209
const schema = makeSchema();
163210
const doc = makeNestedDoc(schema);

0 commit comments

Comments
 (0)