Skip to content

Commit

Permalink
Bug: Changing the font size when inputting does not take effect faceb…
Browse files Browse the repository at this point in the history
…ook#6000. This will also handle the blur event
  • Loading branch information
pixelbyaj committed May 14, 2024
1 parent 92f6523 commit 57a5d25
Showing 1 changed file with 24 additions and 9 deletions.
33 changes: 24 additions & 9 deletions packages/lexical-playground/src/plugins/ToolbarPlugin/fontSize.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export default function FontSize({
editor: LexicalEditor;
}) {
const [inputValue, setInputValue] = React.useState<string>(selectionFontSize);
const [inputChangeFlag, setInputChangeFlag] = React.useState<boolean>(false);

/**
* Calculates the new font size based on the update type.
Expand Down Expand Up @@ -140,18 +141,18 @@ export default function FontSize({
setInputValue('');
return;
}

setInputChangeFlag(true);
if (e.key === 'Enter' || e.key === 'Tab' || e.key === 'Escape') {
e.preventDefault();

let updatedFontSize = inputValueNumber;
if (inputValueNumber > MAX_ALLOWED_FONT_SIZE) {
updatedFontSize = MAX_ALLOWED_FONT_SIZE;
} else if (inputValueNumber < MIN_ALLOWED_FONT_SIZE) {
updatedFontSize = MIN_ALLOWED_FONT_SIZE;
}
setInputValue(String(updatedFontSize));
updateFontSizeInSelection(String(updatedFontSize) + 'px', null);
updateFontSizeByInputValue(inputValueNumber);
}
};

const handleInputBlur = () => {
if (inputValue !== '' && inputChangeFlag) {
const inputValueNumber = Number(inputValue);
updateFontSizeByInputValue(inputValueNumber);
}
};

Expand All @@ -167,6 +168,19 @@ export default function FontSize({
}
};

const updateFontSizeByInputValue = (inputValueNumber: number) => {
let updatedFontSize = inputValueNumber;
if (inputValueNumber > MAX_ALLOWED_FONT_SIZE) {
updatedFontSize = MAX_ALLOWED_FONT_SIZE;
} else if (inputValueNumber < MIN_ALLOWED_FONT_SIZE) {
updatedFontSize = MIN_ALLOWED_FONT_SIZE;
}

setInputValue(String(updatedFontSize));
updateFontSizeInSelection(String(updatedFontSize) + 'px', null);
setInputChangeFlag(false);
};

React.useEffect(() => {
setInputValue(selectionFontSize);
}, [selectionFontSize]);
Expand Down Expand Up @@ -194,6 +208,7 @@ export default function FontSize({
max={MAX_ALLOWED_FONT_SIZE}
onChange={(e) => setInputValue(e.target.value)}
onKeyDown={handleKeyPress}
onBlur={handleInputBlur}
/>

<button
Expand Down

0 comments on commit 57a5d25

Please sign in to comment.