Skip to content

Commit 1e1b59b

Browse files
committed
fix(track-changes): tighten setNodeMarkup heuristic and fix toggleLinkedStyle cursor handling
- Add step.insert === 1 check to exclude lift() false positives - Add map.appendMap() for consistency with other step handlers - Fix nodeAt() null-return by always falling back to findParentNodeClosestToPos - Update stale test to reflect new cursor selection behavior
1 parent d18953e commit 1e1b59b

3 files changed

Lines changed: 8 additions & 5 deletions

File tree

packages/super-editor/src/extensions/linked-styles/linked-styles.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ export const LinkedStyles = Extension.create({
6868
const { tr } = params;
6969
let node = tr.doc.nodeAt(tr.selection.$from.pos);
7070

71-
if (node && node.type.name !== 'paragraph') {
71+
if (!node || node.type.name !== 'paragraph') {
7272
node = findParentNodeClosestToPos(tr.selection.$from, (n) => {
7373
return n.type.name === 'paragraph';
7474
})?.node;

packages/super-editor/src/extensions/linked-styles/linked-styles.test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,13 +135,13 @@ describe('LinkedStyles Extension', () => {
135135
});
136136

137137
describe('toggleLinkedStyle', () => {
138-
it('should return false for an empty selection', () => {
138+
it('should apply style with a cursor (empty) selection', () => {
139139
setParagraphCursor(editor.view, 0); // Cursor selection at first paragraph
140140
const result = editor.commands.toggleLinkedStyle(headingStyle, 'paragraph');
141141

142-
expect(result).toBe(false);
142+
expect(result).toBe(true);
143143
const firstParagraph = findParagraphInfo(editor.state.doc, 0);
144-
expect(getParagraphProps(firstParagraph.node).styleId).toBeUndefined();
144+
expect(getParagraphProps(firstParagraph.node).styleId).toBe('Heading1');
145145
});
146146

147147
it('should apply style when no style is currently set', () => {

packages/super-editor/src/extensions/track-changes/trackChangesHelpers/replaceAroundStep.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,13 @@ export const replaceAroundStep = ({
9797
// Detect setNodeMarkup steps: they preserve the node type and content,
9898
// only changing attributes (e.g. paragraph styleId for heading changes).
9999
// These are safe to apply — they don't alter document structure.
100-
const isNodeMarkupChange = step.structure && step.gapFrom === step.from + 1 && step.gapTo === step.to - 1;
100+
// We also check step.insert === 1 to exclude lift() operations (insert === 0).
101+
const isNodeMarkupChange =
102+
step.structure && step.insert === 1 && step.gapFrom === step.from + 1 && step.gapTo === step.to - 1;
101103

102104
if (isNodeMarkupChange) {
103105
newTr.step(step);
106+
map.appendMap(step.getMap());
104107
return;
105108
}
106109

0 commit comments

Comments
 (0)