Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
bedre7 committed Dec 2, 2024
1 parent 5339161 commit 90dd330
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
);
}

Expand Down
22 changes: 11 additions & 11 deletions packages/lexical-rich-text/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
},
Expand All @@ -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;
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});

Expand All @@ -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);
});
Expand Down

0 comments on commit 90dd330

Please sign in to comment.