-
Notifications
You must be signed in to change notification settings - Fork 808
/
Copy patheditor.tsx
116 lines (105 loc) · 3.06 KB
/
editor.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
'use client';
import React from 'react';
import type { PlateContentProps } from '@udecode/plate-common/react';
import type { VariantProps } from 'class-variance-authority';
import { cn } from '@udecode/cn';
import {
PlateContent,
useEditorContainerRef,
useEditorRef,
} from '@udecode/plate-common/react';
import { cva } from 'class-variance-authority';
const editorContainerVariants = cva(
'relative w-full cursor-text overflow-y-auto caret-primary selection:bg-brand/25 [&_.slate-selection-area]:border [&_.slate-selection-area]:border-brand/25 [&_.slate-selection-area]:bg-brand/15',
{
defaultVariants: {
variant: 'default',
},
variants: {
variant: {
default: 'h-full',
demo: 'h-[650px]',
},
},
}
);
export const EditorContainer = ({
className,
variant,
...props
}: React.HTMLAttributes<HTMLDivElement> &
VariantProps<typeof editorContainerVariants>) => {
const editor = useEditorRef();
const containerRef = useEditorContainerRef();
return (
<div
id={editor.uid}
ref={containerRef}
className={cn(
'ignore-click-outside/toolbar',
editorContainerVariants({ variant }),
className
)}
// Adding this role attribute could cause the content captured by html2canvas to be incorrectly centered.
// role="button"
{...props}
/>
);
};
EditorContainer.displayName = 'EditorContainer';
const editorVariants = cva(
cn(
'group/editor',
'relative w-full overflow-x-hidden whitespace-pre-wrap break-words',
'rounded-md ring-offset-background placeholder:text-muted-foreground/80 focus-visible:outline-none',
'[&_[data-slate-placeholder]]:text-muted-foreground/80 [&_[data-slate-placeholder]]:!opacity-100',
'[&_[data-slate-placeholder]]:top-[auto_!important]',
'[&_strong]:font-bold'
),
{
defaultVariants: {
variant: 'default',
},
variants: {
disabled: {
true: 'cursor-not-allowed opacity-50',
},
focused: {
true: 'ring-2 ring-ring ring-offset-2',
},
variant: {
ai: 'w-full px-0 text-base md:text-sm',
aiChat:
'max-h-[min(70vh,320px)] w-full max-w-[700px] overflow-y-auto px-3 py-2 text-base md:text-sm',
default:
'size-full px-16 pb-72 pt-4 text-base sm:px-[max(64px,calc(50%-350px))]',
demo: 'size-full px-16 pb-72 pt-4 text-base sm:px-[max(64px,calc(50%-350px))]',
fullWidth: 'size-full px-16 pb-72 pt-4 text-base sm:px-24',
none: '',
},
},
}
);
export type EditorProps = PlateContentProps &
VariantProps<typeof editorVariants>;
export const Editor = React.forwardRef<HTMLDivElement, EditorProps>(
({ className, disabled, focused, variant, ...props }, ref) => {
return (
<PlateContent
ref={ref}
className={cn(
editorVariants({
disabled,
focused,
variant,
}),
className
)}
disabled={disabled}
disableDefaultStyles
{...props}
/>
);
}
);
Editor.displayName = 'Editor';