Skip to content

Commit

Permalink
Merge pull request #3852 from wststone/fix/equation-input-caret
Browse files Browse the repository at this point in the history
fix: correct setvalue in equation input behavior
  • Loading branch information
zbeyens authored Dec 12, 2024
2 parents b48eb17 + df35d66 commit f3bdf7b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .changeset/pink-pillows-end.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@udecode/plate-math": patch
---

Fixes #3851
25 changes: 16 additions & 9 deletions packages/math/src/react/hooks/useEquationInput.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@ export const useEquationInput = ({
const element = useElement<TEquationElement>();
const inputRef = useRef<HTMLTextAreaElement>(null);

const [initialExpression, setInitialExpression] = React.useState<
string | null
>(null);
const [expressionInput, setExpressionInput] = React.useState<string>(
element.texExpression
);

const initialExpressionRef = useRef<string>(element.texExpression);

useEffect(() => {
if (open) {
Expand All @@ -35,22 +37,29 @@ export const useEquationInput = ({
inputRef.current.select();

if (isInline) {
setInitialExpression(element.texExpression);
initialExpressionRef.current = element.texExpression;
}
}
}, 0);
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [open]);

useEffect(() => {
setNode<TEquationElement>(editor, element, {
texExpression: expressionInput || '',
});
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [expressionInput]);

const onSubmit = () => {
onClose?.();
};

const onDismiss = () => {
if (isInline) {
setNode(editor, element, {
texExpression: initialExpression ?? '',
texExpression: initialExpressionRef.current,
});
}

Expand All @@ -59,11 +68,9 @@ export const useEquationInput = ({

return {
props: {
value: element.texExpression,
value: expressionInput,
onChange: (e: React.ChangeEvent<HTMLTextAreaElement>) => {
setNode<TEquationElement>(editor, element, {
texExpression: e.target.value,
});
setExpressionInput(e.target.value);
},
onKeyDown: (e: React.KeyboardEvent<HTMLTextAreaElement>) => {
if (isHotkey('enter')(e)) {
Expand Down

0 comments on commit f3bdf7b

Please sign in to comment.