Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #6051 Deprecate keycode to code #6054

Merged
merged 5 commits into from
May 9, 2024
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ const formatStep = (step: Step) => {
};

export function isSelectAll(event: KeyboardEvent): boolean {
return event.keyCode === 65 && (IS_APPLE ? event.metaKey : event.ctrlKey);
return event.code === 'KeyA' && (IS_APPLE ? event.metaKey : event.ctrlKey);
}

// stolen from LexicalSelection-test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,9 @@ export default function TypingPerfPlugin(): JSX.Element | null {
};

const keyDownHandler = function keyDownHandler(event: KeyboardEvent) {
const keyCode = event.keyCode;
const keyCode = event.code;

if (keyCode === 8 || keyCode === 13) {
if (keyCode === 'Backspace' || keyCode === 'Enter') {
measureEventStart();
}
};
Expand Down
16 changes: 10 additions & 6 deletions packages/lexical-selection/src/__tests__/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -781,8 +781,8 @@ export async function applySelectionInputs(
new KeyboardEvent('keydown', {
bubbles: true,
cancelable: true,
code: 'KeyZ',
ctrlKey: true,
key: 'z',
keyCode: 90,
}),
);
Expand All @@ -794,8 +794,8 @@ export async function applySelectionInputs(
new KeyboardEvent('keydown', {
bubbles: true,
cancelable: true,
code: 'KeyZ',
ctrlKey: true,
key: 'z',
keyCode: 90,
shiftKey: true,
}),
Expand Down Expand Up @@ -877,15 +877,15 @@ export async function applySelectionInputs(
}
}

export function setAnchorPoint(
export function $setAnchorPoint(
point: Pick<PointType, 'type' | 'offset' | 'key'>,
) {
const selection = $getSelection();

if (!$isRangeSelection(selection)) {
const dummyTextNode = $createTextNode();
dummyTextNode.select();
return setAnchorPoint(point);
return $setAnchorPoint(point);
}

if ($isNodeSelection(selection)) {
Expand All @@ -897,16 +897,18 @@ export function setAnchorPoint(
anchor.offset = point.offset;
anchor.key = point.key;
}
/** @deprecated renamed to $setAnchorPoint by @lexical/eslint-plugin rules-of-lexical */
export const setAnchorPoint = $setAnchorPoint;

export function setFocusPoint(
export function $setFocusPoint(
point: Pick<PointType, 'type' | 'offset' | 'key'>,
) {
const selection = $getSelection();

if (!$isRangeSelection(selection)) {
const dummyTextNode = $createTextNode();
dummyTextNode.select();
return setFocusPoint(point);
return $setFocusPoint(point);
}

if ($isNodeSelection(selection)) {
Expand All @@ -918,3 +920,5 @@ export function setFocusPoint(
focus.offset = point.offset;
focus.key = point.key;
}
/** @deprecated renamed to $setFocusPoint by @lexical/eslint-plugin rules-of-lexical */
export const setFocusPoint = $setFocusPoint;
74 changes: 37 additions & 37 deletions packages/lexical/src/LexicalEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ if (CAN_USE_BEFORE_INPUT) {
}

let lastKeyDownTimeStamp = 0;
let lastKeyCode = 0;
let lastKeyCode: null | string = null;
let lastBeforeInputInsertTextTimeStamp = 0;
let unprocessedBeforeInputData: null | string = null;
const rootElementsRegistered = new WeakMap<Document, number>();
Expand Down Expand Up @@ -513,7 +513,7 @@ function $canRemoveText(

function isPossiblyAndroidKeyPress(timeStamp: number): boolean {
return (
lastKeyCode === 229 &&
lastKeyCode === 'Delete' &&
timeStamp < lastKeyDownTimeStamp + ANDROID_COMPOSITION_LATENCY
);
}
Expand Down Expand Up @@ -817,7 +817,7 @@ function onInput(event: InputEvent, editor: LexicalEditor): void {
// to ensure to disable composition before dispatching the
// insertText command for when changing the sequence for FF.
if (isFirefoxEndingComposition) {
onCompositionEndImpl(editor, data);
$onCompositionEndImpl(editor, data);
isFirefoxEndingComposition = false;
}
const anchor = selection.anchor;
Expand Down Expand Up @@ -873,7 +873,7 @@ function onInput(event: InputEvent, editor: LexicalEditor): void {

// onInput always fires after onCompositionEnd for FF.
if (isFirefoxEndingComposition) {
onCompositionEndImpl(editor, data || undefined);
$onCompositionEndImpl(editor, data || undefined);
isFirefoxEndingComposition = false;
}
}
Expand Down Expand Up @@ -923,7 +923,7 @@ function onCompositionStart(
});
}

function onCompositionEndImpl(editor: LexicalEditor, data?: string): void {
function $onCompositionEndImpl(editor: LexicalEditor, data?: string): void {
const compositionKey = editor._compositionKey;
$setCompositionKey(null);

Expand Down Expand Up @@ -984,108 +984,108 @@ function onCompositionEnd(
isFirefoxEndingComposition = true;
} else {
updateEditor(editor, () => {
onCompositionEndImpl(editor, event.data);
$onCompositionEndImpl(editor, event.data);
});
}
}

function onKeyDown(event: KeyboardEvent, editor: LexicalEditor): void {
lastKeyDownTimeStamp = event.timeStamp;
lastKeyCode = event.keyCode;
lastKeyCode = event.code;
if (editor.isComposing()) {
return;
}

const {keyCode, shiftKey, ctrlKey, metaKey, altKey} = event;
const {code, shiftKey, ctrlKey, metaKey, altKey} = event;

if (dispatchCommand(editor, KEY_DOWN_COMMAND, event)) {
return;
}

if (isMoveForward(keyCode, ctrlKey, altKey, metaKey)) {
if (isMoveForward(code, ctrlKey, altKey, metaKey)) {
dispatchCommand(editor, KEY_ARROW_RIGHT_COMMAND, event);
} else if (isMoveToEnd(keyCode, ctrlKey, shiftKey, altKey, metaKey)) {
} else if (isMoveToEnd(code, ctrlKey, shiftKey, altKey, metaKey)) {
dispatchCommand(editor, MOVE_TO_END, event);
} else if (isMoveBackward(keyCode, ctrlKey, altKey, metaKey)) {
} else if (isMoveBackward(code, ctrlKey, altKey, metaKey)) {
dispatchCommand(editor, KEY_ARROW_LEFT_COMMAND, event);
} else if (isMoveToStart(keyCode, ctrlKey, shiftKey, altKey, metaKey)) {
} else if (isMoveToStart(code, ctrlKey, shiftKey, altKey, metaKey)) {
dispatchCommand(editor, MOVE_TO_START, event);
} else if (isMoveUp(keyCode, ctrlKey, metaKey)) {
} else if (isMoveUp(code, ctrlKey, metaKey)) {
dispatchCommand(editor, KEY_ARROW_UP_COMMAND, event);
} else if (isMoveDown(keyCode, ctrlKey, metaKey)) {
} else if (isMoveDown(code, ctrlKey, metaKey)) {
dispatchCommand(editor, KEY_ARROW_DOWN_COMMAND, event);
} else if (isLineBreak(keyCode, shiftKey)) {
} else if (isLineBreak(code, shiftKey)) {
isInsertLineBreak = true;
dispatchCommand(editor, KEY_ENTER_COMMAND, event);
} else if (isSpace(keyCode)) {
} else if (isSpace(code)) {
dispatchCommand(editor, KEY_SPACE_COMMAND, event);
} else if (isOpenLineBreak(keyCode, ctrlKey)) {
} else if (isOpenLineBreak(code, ctrlKey)) {
event.preventDefault();
isInsertLineBreak = true;
dispatchCommand(editor, INSERT_LINE_BREAK_COMMAND, true);
} else if (isParagraph(keyCode, shiftKey)) {
} else if (isParagraph(code, shiftKey)) {
isInsertLineBreak = false;
dispatchCommand(editor, KEY_ENTER_COMMAND, event);
} else if (isDeleteBackward(keyCode, altKey, metaKey, ctrlKey)) {
if (isBackspace(keyCode)) {
} else if (isDeleteBackward(code, altKey, metaKey, ctrlKey)) {
if (isBackspace(code)) {
dispatchCommand(editor, KEY_BACKSPACE_COMMAND, event);
} else {
event.preventDefault();
dispatchCommand(editor, DELETE_CHARACTER_COMMAND, true);
}
} else if (isEscape(keyCode)) {
} else if (isEscape(code)) {
dispatchCommand(editor, KEY_ESCAPE_COMMAND, event);
} else if (isDeleteForward(keyCode, ctrlKey, shiftKey, altKey, metaKey)) {
if (isDelete(keyCode)) {
} else if (isDeleteForward(code, ctrlKey, shiftKey, altKey, metaKey)) {
if (isDelete(code)) {
dispatchCommand(editor, KEY_DELETE_COMMAND, event);
} else {
event.preventDefault();
dispatchCommand(editor, DELETE_CHARACTER_COMMAND, false);
}
} else if (isDeleteWordBackward(keyCode, altKey, ctrlKey)) {
} else if (isDeleteWordBackward(code, altKey, ctrlKey)) {
event.preventDefault();
dispatchCommand(editor, DELETE_WORD_COMMAND, true);
} else if (isDeleteWordForward(keyCode, altKey, ctrlKey)) {
} else if (isDeleteWordForward(code, altKey, ctrlKey)) {
event.preventDefault();
dispatchCommand(editor, DELETE_WORD_COMMAND, false);
} else if (isDeleteLineBackward(keyCode, metaKey)) {
} else if (isDeleteLineBackward(code, metaKey)) {
event.preventDefault();
dispatchCommand(editor, DELETE_LINE_COMMAND, true);
} else if (isDeleteLineForward(keyCode, metaKey)) {
} else if (isDeleteLineForward(code, metaKey)) {
event.preventDefault();
dispatchCommand(editor, DELETE_LINE_COMMAND, false);
} else if (isBold(keyCode, altKey, metaKey, ctrlKey)) {
} else if (isBold(code, altKey, metaKey, ctrlKey)) {
event.preventDefault();
dispatchCommand(editor, FORMAT_TEXT_COMMAND, 'bold');
} else if (isUnderline(keyCode, altKey, metaKey, ctrlKey)) {
} else if (isUnderline(code, altKey, metaKey, ctrlKey)) {
event.preventDefault();
dispatchCommand(editor, FORMAT_TEXT_COMMAND, 'underline');
} else if (isItalic(keyCode, altKey, metaKey, ctrlKey)) {
} else if (isItalic(code, altKey, metaKey, ctrlKey)) {
event.preventDefault();
dispatchCommand(editor, FORMAT_TEXT_COMMAND, 'italic');
} else if (isTab(keyCode, altKey, ctrlKey, metaKey)) {
} else if (isTab(code, altKey, ctrlKey, metaKey)) {
dispatchCommand(editor, KEY_TAB_COMMAND, event);
} else if (isUndo(keyCode, shiftKey, metaKey, ctrlKey)) {
} else if (isUndo(code, shiftKey, metaKey, ctrlKey)) {
event.preventDefault();
dispatchCommand(editor, UNDO_COMMAND, undefined);
} else if (isRedo(keyCode, shiftKey, metaKey, ctrlKey)) {
} else if (isRedo(code, shiftKey, metaKey, ctrlKey)) {
event.preventDefault();
dispatchCommand(editor, REDO_COMMAND, undefined);
} else {
const prevSelection = editor._editorState._selection;
if ($isNodeSelection(prevSelection)) {
if (isCopy(keyCode, shiftKey, metaKey, ctrlKey)) {
if (isCopy(code, shiftKey, metaKey, ctrlKey)) {
event.preventDefault();
dispatchCommand(editor, COPY_COMMAND, event);
} else if (isCut(keyCode, shiftKey, metaKey, ctrlKey)) {
} else if (isCut(code, shiftKey, metaKey, ctrlKey)) {
event.preventDefault();
dispatchCommand(editor, CUT_COMMAND, event);
} else if (isSelectAll(keyCode, metaKey, ctrlKey)) {
} else if (isSelectAll(code, metaKey, ctrlKey)) {
event.preventDefault();
dispatchCommand(editor, SELECT_ALL_COMMAND, event);
}
// FF does it well (no need to override behavior)
} else if (!IS_FIREFOX && isSelectAll(keyCode, metaKey, ctrlKey)) {
} else if (!IS_FIREFOX && isSelectAll(code, metaKey, ctrlKey)) {
event.preventDefault();
dispatchCommand(editor, SELECT_ALL_COMMAND, event);
}
Expand Down
Loading
Loading