Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 13 additions & 22 deletions packages/lexical-code/src/CodeHighlighterPrism.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
$getCaretRange,
$getCaretRangeInDirection,
$getNodeByKey,
$getRoot,
$getSelection,
$getSiblingCaret,
$getTextPointCaret,
Expand Down Expand Up @@ -843,23 +844,18 @@ export function registerCodeHighlighting(
KEY_ARROW_UP_COMMAND,
(event) => {
const selection = $getSelection();
if (!$isRangeSelection(selection)) {
if (!$isRangeSelection(selection) || !$isSelectionInCode(selection)) {
return false;
}
const firstNode = $getRoot().getFirstDescendant();
const {anchor} = selection;
const anchorNode = anchor.getNode();
if (!$isSelectionInCode(selection)) {
return false;
}
// If at the start of a code block, prevent selection from moving out
if (
selection.isCollapsed() &&
anchor.offset === 0 &&
anchorNode.getPreviousSibling() === null &&
$isCodeNode(anchorNode.getParentOrThrow())
firstNode &&
anchorNode &&
firstNode.getKey() === anchorNode.getKey()
) {
event.preventDefault();
return true;
return false;
}
return $handleShiftLines(KEY_ARROW_UP_COMMAND, event);
},
Expand All @@ -869,23 +865,18 @@ export function registerCodeHighlighting(
KEY_ARROW_DOWN_COMMAND,
(event) => {
const selection = $getSelection();
if (!$isRangeSelection(selection)) {
if (!$isRangeSelection(selection) || !$isSelectionInCode(selection)) {
return false;
}
const lastNode = $getRoot().getLastDescendant();
const {anchor} = selection;
const anchorNode = anchor.getNode();
if (!$isSelectionInCode(selection)) {
return false;
}
// If at the end of a code block, prevent selection from moving out
if (
selection.isCollapsed() &&
anchor.offset === anchorNode.getTextContentSize() &&
anchorNode.getNextSibling() === null &&
$isCodeNode(anchorNode.getParentOrThrow())
lastNode &&
anchorNode &&
lastNode.getKey() === anchorNode.getKey()
) {
event.preventDefault();
return true;
return false;
}
return $handleShiftLines(KEY_ARROW_DOWN_COMMAND, event);
},
Expand Down