-
-
Notifications
You must be signed in to change notification settings - Fork 756
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1. fix: equation input undo not working
2. feat: add equation and inline-equation elements in editor and slashmenu
- Loading branch information
Showing
6 changed files
with
273 additions
and
4 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
79 changes: 79 additions & 0 deletions
79
apps/www/src/registry/default/plate-ui/equation-element.tsx
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,79 @@ | ||
'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 { 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 [open, setOpen] = useState(false); | ||
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', | ||
element.texExpression.length === 0 | ||
? 'bg-muted p-3 pr-9' | ||
: 'px-2 py-1' | ||
)} | ||
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 | ||
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> | ||
); | ||
} | ||
); |
83 changes: 83 additions & 0 deletions
83
apps/www/src/registry/default/plate-ui/equation-popover.tsx
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, { 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, | ||
setOpen, | ||
...props | ||
}: { | ||
isInline: boolean; | ||
setOpen: (open: boolean) => void; | ||
} & TextareaAutosizeProps) => { | ||
const editor = useEditorRef(); | ||
const readOnly = useReadOnly(); | ||
const element = useElement<TEquationElement>(); | ||
const selected = useSelected(); | ||
|
||
useEffect(() => { | ||
setOpen(selected); | ||
}, [selected, 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: true, onClose }} | ||
autoFocus | ||
{...props} | ||
/> | ||
|
||
<Button variant="secondary" className="px-3" onClick={onClose}> | ||
Done <CornerDownLeftIcon className="size-3.5" /> | ||
</Button> | ||
</PopoverContent> | ||
); | ||
}; | ||
|
||
export { EquationPopoverContent }; |
88 changes: 88 additions & 0 deletions
88
apps/www/src/registry/default/plate-ui/inline-equation-element.tsx
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,88 @@ | ||
'use client'; | ||
|
||
import { 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 { EquationPopoverContent } from './equation-popover'; | ||
import { PlateElement } from './plate-element'; | ||
import { Popover, PopoverTrigger } from './popover'; | ||
|
||
export const InlineEquationElement = withRef<typeof PlateElement>( | ||
({ children, className, ...props }, ref) => { | ||
const element = useElement<TEquationElement>(); | ||
const katexRef = useRef<HTMLDivElement | null>(null); | ||
const [open, setOpen] = useState(false); | ||
|
||
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( | ||
'inline-block select-none rounded-sm [&_.katex-display]:my-0', | ||
className | ||
)} | ||
{...props} | ||
> | ||
<Popover open={open} onOpenChange={setOpen} modal={false}> | ||
<PopoverTrigger asChild> | ||
<div | ||
className={cn( | ||
'after:absolute after:inset-0 after:-left-1 after:-top-0.5 after:z-[1] after:h-[calc(100%)+4px] after:w-[calc(100%+8px)] after:rounded-sm after:content-[""]', | ||
'h-6', | ||
element.texExpression.length > 0 && open && 'after:bg-brand/15', | ||
element.texExpression.length === 0 && | ||
'text-muted-foreground after:bg-neutral-500/10', | ||
className | ||
)} | ||
contentEditable={false} | ||
> | ||
<span | ||
ref={katexRef} | ||
className={cn( | ||
element.texExpression.length === 0 && 'hidden', | ||
'font-mono leading-none' | ||
)} | ||
/> | ||
{element.texExpression.length === 0 && ( | ||
<span> | ||
<RadicalIcon className="mr-1 inline-block h-[19px] w-4 py-[1.5px] align-text-bottom" /> | ||
New equation | ||
</span> | ||
)} | ||
</div> | ||
</PopoverTrigger> | ||
|
||
<EquationPopoverContent | ||
className="my-auto" | ||
placeholder="E = mc^2" | ||
setOpen={setOpen} | ||
isInline | ||
/> | ||
</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
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