Skip to content

Commit

Permalink
fix(codeBlock): backspace key deletes code block when all code is sel…
Browse files Browse the repository at this point in the history
…ected
  • Loading branch information
luolonghao committed Jul 15, 2024
1 parent 2af8052 commit d4c24bf
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
5 changes: 4 additions & 1 deletion src/plugins/backspace-key.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,10 @@ export default (editor: Editor) => {
const boxNode = range.commonAncestor.closest('lake-box');
const box = getBox(boxNode);
const boxValue = box.value;
if (box.name === 'codeBlock' && (boxValue.code === undefined || boxValue.code === '')) {
if (
range.isCollapsed && box.name === 'codeBlock' &&
(boxValue.code === undefined || boxValue.code === '')
) {
event.preventDefault();
editor.selection.removeBox(box);
editor.history.save();
Expand Down
23 changes: 22 additions & 1 deletion tests/plugins/backspace-key.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -856,7 +856,7 @@ describe('plugins / backspace-key', () => {
);
});

it('should remove empty code block', () => {
it('should remove empty code block when selected range is collapsed', () => {
const content = `
<p>foo</p>
<lake-box type="block" name="codeBlock" focus="center"></lake-box>
Expand All @@ -877,4 +877,25 @@ describe('plugins / backspace-key', () => {
);
});

it('should not remove empty code block when selected range is expanded', () => {
const content = `
<p>foo</p>
<lake-box type="block" name="codeBlock" focus="center"></lake-box>
`;
const output = `
<p>foo</p>
<lake-box type="block" name="codeBlock" focus="center"></lake-box>
`;
testPlugin(
content,
output,
editor => {
const boxNode = editor.container.find('lake-box');
boxNode.find('.cm-line').text('foo');
editor.selection.range.selectNodeContents(boxNode.find('.cm-line'));
editor.keystroke.keydown('backspace');
},
);
});

});

0 comments on commit d4c24bf

Please sign in to comment.