-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3923 from wststone/fix/equation-input-undo
fix: equation input undo regession
- Loading branch information
Showing
20 changed files
with
829 additions
and
346 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { | ||
EquationPlugin, | ||
InlineEquationPlugin, | ||
} from '@udecode/plate-math/react'; | ||
|
||
export const equationPlugins = [InlineEquationPlugin, EquationPlugin]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
'use client'; | ||
|
||
import React, { useRef, useState } from 'react'; | ||
|
||
import type { TEquationElement } from '@udecode/plate-math'; | ||
|
||
import { cn, withRef } from '@udecode/cn'; | ||
import { useElement } from '@udecode/plate-common/react'; | ||
import { useEquationElement } from '@udecode/plate-math/react'; | ||
import { RadicalIcon } from 'lucide-react'; | ||
import { useSelected } from 'slate-react'; | ||
|
||
import { EquationPopoverContent } from './equation-popover'; | ||
import { PlateElement } from './plate-element'; | ||
import { Popover, PopoverTrigger } from './popover'; | ||
|
||
export const EquationElement = withRef<typeof PlateElement>( | ||
({ children, className, ...props }, ref) => { | ||
const element = useElement<TEquationElement>(); | ||
|
||
const selected = useSelected(); | ||
const [open, setOpen] = useState(selected); | ||
const katexRef = useRef<HTMLDivElement | null>(null); | ||
|
||
useEquationElement({ | ||
element, | ||
katexRef: katexRef, | ||
options: { | ||
displayMode: true, | ||
errorColor: '#cc0000', | ||
fleqn: false, | ||
leqno: false, | ||
macros: { '\\f': '#1f(#2)' }, | ||
output: 'htmlAndMathml', | ||
strict: 'warn', | ||
throwOnError: false, | ||
trust: false, | ||
}, | ||
}); | ||
|
||
return ( | ||
<PlateElement | ||
ref={ref} | ||
className={cn('relative my-1', className)} | ||
{...props} | ||
> | ||
<Popover open={open} onOpenChange={setOpen} modal={false}> | ||
<PopoverTrigger asChild> | ||
<div | ||
className={cn( | ||
'group flex cursor-pointer select-none items-center justify-center rounded-sm hover:bg-primary/10 data-[selected=true]:bg-primary/10', | ||
element.texExpression.length === 0 | ||
? 'bg-muted p-3 pr-9' | ||
: 'px-2 py-1' | ||
)} | ||
data-selected={selected} | ||
contentEditable={false} | ||
role="button" | ||
> | ||
{element.texExpression.length > 0 ? ( | ||
<span ref={katexRef} /> | ||
) : ( | ||
<div className="flex h-7 w-full items-center gap-2 whitespace-nowrap text-sm text-muted-foreground"> | ||
<RadicalIcon className="size-6 text-muted-foreground/80" /> | ||
<div>Add a Tex equation</div> | ||
</div> | ||
)} | ||
</div> | ||
</PopoverTrigger> | ||
|
||
<EquationPopoverContent | ||
open={open} | ||
placeholder={`f(x) = \\begin{cases}\n x^2, &\\quad x > 0 \\\\\n 0, &\\quad x = 0 \\\\\n -x^2, &\\quad x < 0\n\\end{cases}`} | ||
isInline={false} | ||
setOpen={setOpen} | ||
/> | ||
</Popover> | ||
|
||
{children} | ||
</PlateElement> | ||
); | ||
} | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
'use client'; | ||
|
||
import React, { useEffect } from 'react'; | ||
import TextareaAutosize, { | ||
type TextareaAutosizeProps, | ||
} from 'react-textarea-autosize'; | ||
|
||
import type { TEquationElement } from '@udecode/plate-math'; | ||
|
||
import { cn } from '@udecode/cn'; | ||
import { | ||
createPrimitiveComponent, | ||
selectSiblingNodePoint, | ||
useEditorRef, | ||
useElement, | ||
} from '@udecode/plate-common/react'; | ||
import { useEquationInput } from '@udecode/plate-math/react'; | ||
import { BlockSelectionPlugin } from '@udecode/plate-selection/react'; | ||
import { CornerDownLeftIcon } from 'lucide-react'; | ||
import { useReadOnly, useSelected } from 'slate-react'; | ||
|
||
import { Button } from './button'; | ||
import { PopoverContent } from './popover'; | ||
|
||
const EquationInput = createPrimitiveComponent(TextareaAutosize)({ | ||
propsHook: useEquationInput, | ||
}); | ||
|
||
const EquationPopoverContent = ({ | ||
className, | ||
isInline, | ||
open, | ||
setOpen, | ||
...props | ||
}: { | ||
isInline: boolean; | ||
open: boolean; | ||
setOpen: (open: boolean) => void; | ||
} & TextareaAutosizeProps) => { | ||
const editor = useEditorRef(); | ||
const readOnly = useReadOnly(); | ||
const element = useElement<TEquationElement>(); | ||
const selected = useSelected(); | ||
|
||
useEffect(() => { | ||
if (isInline && selected) { | ||
setOpen(true); | ||
} | ||
}, [selected, isInline, setOpen]); | ||
|
||
if (readOnly) return null; | ||
|
||
const onClose = () => { | ||
setOpen(false); | ||
|
||
if (isInline) { | ||
selectSiblingNodePoint(editor, { node: element }); | ||
} else { | ||
editor | ||
.getApi(BlockSelectionPlugin) | ||
.blockSelection.addSelectedRow(element.id as string); | ||
} | ||
}; | ||
|
||
return ( | ||
<PopoverContent | ||
className="flex gap-2" | ||
onEscapeKeyDown={(e) => { | ||
e.preventDefault(); | ||
}} | ||
contentEditable={false} | ||
> | ||
<EquationInput | ||
className={cn('max-h-[50vh] grow resize-none p-2 text-sm', className)} | ||
state={{ isInline, open, onClose }} | ||
autoFocus | ||
{...props} | ||
/> | ||
|
||
<Button variant="secondary" className="px-3" onClick={onClose}> | ||
Done <CornerDownLeftIcon className="size-3.5" /> | ||
</Button> | ||
</PopoverContent> | ||
); | ||
}; | ||
|
||
export { EquationPopoverContent }; |
Oops, something went wrong.