Skip to content

Commit

Permalink
fix(backspace): empty tags before the caret cannot be removed
Browse files Browse the repository at this point in the history
  • Loading branch information
luolonghao committed Sep 10, 2024
1 parent 88777fc commit 141a384
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
6 changes: 6 additions & 0 deletions src/plugins/backspace-key.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,12 @@ export default (editor: Editor) => {
editor.history.save();
return;
}
if ((prevNode.isMark || prevNode.name === 'a') && prevNode.isEmpty) {
event.preventDefault();
prevNode.remove();
editor.history.save();
return;
}
if (prevNode.isText && prevNode.text().length === 1 && prevNode.parent().isBlock) {
event.preventDefault();
const block = prevNode.closestBlock();
Expand Down
36 changes: 34 additions & 2 deletions tests/plugins/backspace-key.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ describe('plugins / backspace-key', () => {
);
});

it('should remove emtpy mark', () => {
it('should remove empty mark (1)', () => {
const content = `
<h1>foo</h1>
<p><code><focus /><br /></code></p>
Expand All @@ -324,7 +324,23 @@ describe('plugins / backspace-key', () => {
);
});

it('should remove emtpy link', () => {
it('should remove empty mark (2)', () => {
const content = `
<p><strong></strong><focus /><br /></p>
`;
const output = `
<p><focus /><br /></p>
`;
testPlugin(
content,
output,
editor => {
editor.keystroke.keydown('backspace');
},
);
});

it('should remove empty link (1)', () => {
const content = `
<h1>foo</h1>
<p><a href="bar"><focus /><br /></a></p>
Expand All @@ -341,6 +357,22 @@ describe('plugins / backspace-key', () => {
);
});

it('should remove empty link (2)', () => {
const content = `
<p><a href="#"></a><focus /><br /></p>
`;
const output = `
<p><focus /><br /></p>
`;
testPlugin(
content,
output,
editor => {
editor.keystroke.keydown('backspace');
},
);
});

it('should merge heading into paragraph', () => {
const content = `
<p>foo</p>
Expand Down

0 comments on commit 141a384

Please sign in to comment.