Skip to content

Commit 2c5c0f6

Browse files
authored
fix(YfmNote): improved note removal on Backspace (#778)
1 parent 79d7d3c commit 2c5c0f6

File tree

2 files changed

+27
-11
lines changed

2 files changed

+27
-11
lines changed

src/extensions/yfm/YfmNote/commands.test.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,19 @@ const schema = new Schema({
1919
},
2020
});
2121

22-
const {doc, paragraph: p, yfm_note: note, yfm_note_title: noteTitle} = builders(schema);
22+
const {
23+
doc,
24+
paragraph: p,
25+
yfm_note: note,
26+
yfm_note_title: noteTitle,
27+
yfm_note_content: noteContent,
28+
} = builders(schema);
2329

2430
describe('YfmNote commands', () => {
2531
it('removeNote: should replace note with its content', () => {
26-
const pmDoc = doc(note(noteTitle('note title'), p('note content in paragraph')));
32+
const pmDoc = doc(
33+
note(noteTitle('note title'), noteContent(p('note content in paragraph'))),
34+
);
2735
const view = new EditorView(null, {
2836
state: EditorState.create({
2937
schema,
@@ -38,12 +46,14 @@ describe('YfmNote commands', () => {
3846
});
3947

4048
it("backToNoteTitle: should move cursor to the end of note's title", () => {
41-
const pmDoc = doc(note(noteTitle('note title'), p('note content in paragraph')));
49+
const pmDoc = doc(
50+
note(noteTitle('note title'), noteContent(p('note content in paragraph'))),
51+
);
4252
const view = new EditorView(null, {
4353
state: EditorState.create({
4454
schema,
4555
doc: pmDoc,
46-
selection: TextSelection.create(pmDoc, 14),
56+
selection: TextSelection.create(pmDoc, 15),
4757
}),
4858
});
4959
const res = backToNoteTitle(view.state, view.dispatch, view);

src/extensions/yfm/YfmNote/commands.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,12 @@ export const removeNote: Command = (state, dispatch, view) => {
8282
if (dispatch) {
8383
const noteTitleNode = $cursor.parent;
8484
const noteNode = $cursor.node(-1);
85-
const content = noteNode.content.replaceChild(
86-
0,
87-
pType(schema).create(null, noteTitleNode.content),
85+
86+
const noteContentNode = noteNode.lastChild!;
87+
const content = Fragment.from(pType(schema).create(null, noteTitleNode.content)).append(
88+
noteContentNode.content,
8889
);
90+
8991
const from = $cursor.before(-1);
9092
const to = from + noteNode.nodeSize;
9193
const tr = state.tr.replaceWith(from, to, content);
@@ -107,18 +109,22 @@ export const backToNoteTitle: Command = (state, dispatch, view) => {
107109
if (!$cursor) return false;
108110
if (
109111
!isSameNodeType($cursor.parent, pType(schema)) ||
110-
!isSameNodeType($cursor.node(-1), noteType(schema))
112+
!isSameNodeType($cursor.node(-1), noteContentType(schema)) ||
113+
!isSameNodeType($cursor.node(-2), noteType(schema))
111114
) {
112115
return false;
113116
}
114-
const noteNode = $cursor.node(-1);
115-
if ($cursor.parent !== noteNode.maybeChild(1)) return false;
117+
118+
const noteNode = $cursor.node(-2);
119+
const noteContentNode = $cursor.node(-1);
120+
if ($cursor.parent !== noteContentNode.firstChild) return false;
121+
116122
if (view?.endOfTextblock('backward', state)) {
117123
if (dispatch) {
118124
const noteTitleNode = noteNode.firstChild!;
119125
dispatch(
120126
state.tr.setSelection(
121-
TextSelection.create(state.doc, $cursor.before(-1) + noteTitleNode.nodeSize),
127+
TextSelection.create(state.doc, $cursor.before(-2) + noteTitleNode.nodeSize),
122128
),
123129
);
124130
}

0 commit comments

Comments
 (0)