From 90dd330f6e17f5e251cfda98ea093da4f7256ec5 Mon Sep 17 00:00:00 2001 From: bedre7 Date: Mon, 2 Dec 2024 21:30:56 +0300 Subject: [PATCH] refactor --- .../src/plugins/ShortcutsPlugin/shortcuts.ts | 10 +++++++-- packages/lexical-rich-text/src/index.ts | 22 +++++++++---------- .../__tests__/unit/LexicalTextNode.test.tsx | 8 ++++--- 3 files changed, 24 insertions(+), 16 deletions(-) diff --git a/packages/lexical-playground/src/plugins/ShortcutsPlugin/shortcuts.ts b/packages/lexical-playground/src/plugins/ShortcutsPlugin/shortcuts.ts index 3a9caab6270..10131c8875f 100644 --- a/packages/lexical-playground/src/plugins/ShortcutsPlugin/shortcuts.ts +++ b/packages/lexical-playground/src/plugins/ShortcutsPlugin/shortcuts.ts @@ -122,14 +122,20 @@ export function isFormatQuote(event: KeyboardEvent): boolean { export function isLowercase(event: KeyboardEvent): boolean { const {code, shiftKey, altKey, metaKey, ctrlKey} = event; return ( - code === 'Digit1' && shiftKey && !altKey && controlOrMeta(metaKey, ctrlKey) + (code === 'Numpad1' || code === 'Digit1') && + shiftKey && + !altKey && + controlOrMeta(metaKey, ctrlKey) ); } export function isUppercase(event: KeyboardEvent): boolean { const {code, shiftKey, altKey, metaKey, ctrlKey} = event; return ( - code === 'Digit2' && shiftKey && !altKey && controlOrMeta(metaKey, ctrlKey) + (code === 'Numpad2' || code === 'Digit2') && + shiftKey && + !altKey && + controlOrMeta(metaKey, ctrlKey) ); } diff --git a/packages/lexical-rich-text/src/index.ts b/packages/lexical-rich-text/src/index.ts index 426666daafc..3e18bcef723 100644 --- a/packages/lexical-rich-text/src/index.ts +++ b/packages/lexical-rich-text/src/index.ts @@ -552,9 +552,9 @@ function $isSelectionAtEndOfRoot(selection: RangeSelection) { } function $resetCapitalization(selection: RangeSelection): void { - for (const type of ['lowercase', 'uppercase'] as const) { - if (selection.hasFormat(type)) { - selection.toggleFormat(type); + for (const format of ['lowercase', 'uppercase'] as const) { + if (selection.hasFormat(format)) { + selection.toggleFormat(format); } } } @@ -919,7 +919,9 @@ export function registerRichText(editor: LexicalEditor): () => void { if (!$isRangeSelection(selection)) { return false; } + $resetCapitalization(selection); + if (event !== null) { // If we have beforeinput, then we can avoid blocking // the default behavior. This ensures that the iOS can @@ -1089,11 +1091,10 @@ export function registerRichText(editor: LexicalEditor): () => void { KEY_SPACE_COMMAND, (_) => { const selection = $getSelection(); - if (!$isRangeSelection(selection)) { - return false; - } - $resetCapitalization(selection); + if ($isRangeSelection(selection)) { + $resetCapitalization(selection); + } return false; }, @@ -1103,11 +1104,10 @@ export function registerRichText(editor: LexicalEditor): () => void { KEY_TAB_COMMAND, (_) => { const selection = $getSelection(); - if (!$isRangeSelection(selection)) { - return false; - } - $resetCapitalization(selection); + if ($isRangeSelection(selection)) { + $resetCapitalization(selection); + } return false; }, diff --git a/packages/lexical/src/nodes/__tests__/unit/LexicalTextNode.test.tsx b/packages/lexical/src/nodes/__tests__/unit/LexicalTextNode.test.tsx index db427e18afb..44c8e4729b6 100644 --- a/packages/lexical/src/nodes/__tests__/unit/LexicalTextNode.test.tsx +++ b/packages/lexical/src/nodes/__tests__/unit/LexicalTextNode.test.tsx @@ -277,17 +277,18 @@ describe('LexicalTextNode tests', () => { ['superscript', 'subscript'], ['lowercase', 'uppercase'], ['uppercase', 'lowercase'], - ])('setting %s clears %s', async (newFormat, previousFormat) => { + ])('setting %s clears %s', async (newFormat, otherFormat) => { await update(() => { const paragraphNode = $createParagraphNode(); const textNode = $createTextNode('Hello World'); paragraphNode.append(textNode); $getRoot().append(paragraphNode); - textNode.toggleFormat(previousFormat as TextFormatType); + textNode.toggleFormat(otherFormat as TextFormatType); textNode.toggleFormat(newFormat as TextFormatType); + expect(textNode.hasFormat(newFormat as TextFormatType)).toBe(true); - expect(textNode.hasFormat(previousFormat as TextFormatType)).toBe(false); + expect(textNode.hasFormat(otherFormat as TextFormatType)).toBe(false); }); }); @@ -305,6 +306,7 @@ describe('LexicalTextNode tests', () => { textNode.toggleFormat(formatToClear as TextFormatType); textNode.toggleFormat(formatToClear as TextFormatType); + expect(textNode.hasFormat(formatToClear as TextFormatType)).toBe(false); expect(textNode.hasFormat(otherFormat as TextFormatType)).toBe(false); });