buster/apps/web/src/components/ui/report/Editor.tsx

74 lines
1.9 KiB
TypeScript
Raw Normal View History

2025-07-30 05:44:10 +08:00
import {
PlateContent,
PlateView,
type PlateContentProps,
type PlateViewProps
} from 'platejs/react';
import { cn } from '@/lib/utils';
import { cva, type VariantProps } from 'class-variance-authority';
import React from 'react';
const editorVariants = cva(
cn(
'group/editor',
2025-08-08 03:25:08 +08:00
'relative w-full cursor-text overflow-x-visible break-words whitespace-pre-wrap select-text',
2025-08-22 06:18:27 +08:00
'ring-offset-background focus-visible:outline-none',
2025-07-30 05:44:10 +08:00
'placeholder:text-muted-foreground/80 **:data-slate-placeholder:!top-1/2 **:data-slate-placeholder:-translate-y-1/2 **:data-slate-placeholder:text-muted-foreground/80 **:data-slate-placeholder:opacity-100!',
'[&_strong]:font-bold'
),
{
defaultVariants: {
variant: 'default'
},
variants: {
2025-08-22 11:37:28 +08:00
readOnly: {
true: ''
2025-07-30 05:44:10 +08:00
},
focused: {
true: 'ring-2 ring-ring ring-offset-2'
},
variant: {
comment: cn('rounded-none border-none bg-transparent text-sm'),
2025-08-22 06:18:27 +08:00
default: 'px-16 pt-4 pb-72 text-base sm:px-[max(64px,calc(50%-350px))]',
fullWidth: 'px-16 pt-4 pb-72 text-base sm:px-24',
2025-08-02 07:25:36 +08:00
none: ''
2025-07-30 05:44:10 +08:00
}
}
}
);
export type EditorProps = PlateContentProps & VariantProps<typeof editorVariants>;
export const Editor = React.forwardRef<HTMLDivElement, EditorProps>(
2025-08-22 11:37:28 +08:00
({ className, readOnly, focused, variant, ...props }, ref) => {
2025-07-30 05:44:10 +08:00
return (
<PlateContent
ref={ref}
className={cn(
editorVariants({
2025-08-22 11:37:28 +08:00
readOnly,
2025-07-30 05:44:10 +08:00
focused,
variant
}),
className
)}
2025-08-22 11:37:28 +08:00
readOnly={readOnly}
disableDefaultStyles={true}
2025-07-30 05:44:10 +08:00
{...props}
/>
);
}
);
Editor.displayName = 'Editor';
export function EditorView({
className,
variant,
...props
}: PlateViewProps & VariantProps<typeof editorVariants>) {
return <PlateView {...props} className={cn(editorVariants({ variant }), className)} />;
}
EditorView.displayName = 'EditorView';