@@ -29,64 +29,83 @@ interface Props extends Partial<MDXEditorProps> {
2929 value : string ;
3030 setValue : ( value ?: string ) => void ;
3131 placeholder ?: string ;
32+ maxLength ?: number ;
3233}
3334
3435const MarkdownEditor : FunctionComponent < Props > = ( {
3536 value,
3637 setValue,
3738 placeholder,
39+ maxLength,
3840 ...rest
3941} ) => {
4042 const markdownRef = useRef < MDXEditorMethods > ( null ) ;
4143
4244 useEffect ( ( ) => {
4345 markdownRef . current ?. setMarkdown ( value ) ;
4446 } , [ value ] ) ;
47+
48+ const charCount = value . length ;
49+ const isOverLimit = maxLength !== undefined && charCount > maxLength ;
50+
4551 return (
46- < MDXEditor
47- ref = { markdownRef }
48- className = { classNames (
49- "ring-primary focus:ring-2 focus-within:ring-2 rounded border" ,
50- styles . mdxeditor ,
51- rest . className || "" ,
52- ) }
53- onChange = { ( value ) => setValue ( value ) }
54- placeholder = { placeholder }
55- markdown = { value }
56- contentEditableClassName = "mdx-editor-content"
57- plugins = { [
58- // toolbarPlugin({ toolbarContents: () => <KitchenSinkToolbar /> }),
59- toolbarPlugin ( {
60- toolbarContents : ( ) => (
61- < >
62- < BoldItalicUnderlineToggles />
63- < CodeToggle />
64- < ListsToggle />
65- </ >
66- ) ,
67- } ) ,
68- listsPlugin ( ) ,
69- quotePlugin ( ) ,
70- headingsPlugin ( ) ,
71- linkPlugin ( ) ,
72- imagePlugin ( ) ,
73- tablePlugin ( ) ,
74- thematicBreakPlugin ( ) ,
75- frontmatterPlugin ( ) ,
76- codeBlockPlugin ( { defaultCodeBlockLanguage : "go" } ) ,
77- codeMirrorPlugin ( {
78- codeBlockLanguages : {
79- js : "JavaScript" ,
80- css : "CSS" ,
81- txt : "text" ,
82- tsx : "TypeScript" ,
83- go : "Go" ,
84- } ,
85- } ) ,
52+ < div >
53+ < MDXEditor
54+ ref = { markdownRef }
55+ className = { classNames (
56+ "ring-primary focus:ring-2 focus-within:ring-2 rounded border" ,
57+ isOverLimit ? "border-destructive" : "" ,
58+ styles . mdxeditor ,
59+ rest . className || "" ,
60+ ) }
61+ onChange = { ( value ) => setValue ( value ) }
62+ placeholder = { placeholder }
63+ markdown = { value }
64+ contentEditableClassName = "mdx-editor-content"
65+ plugins = { [
66+ // toolbarPlugin({ toolbarContents: () => <KitchenSinkToolbar /> }),
67+ toolbarPlugin ( {
68+ toolbarContents : ( ) => (
69+ < >
70+ < BoldItalicUnderlineToggles />
71+ < CodeToggle />
72+ < ListsToggle />
73+ </ >
74+ ) ,
75+ } ) ,
76+ listsPlugin ( ) ,
77+ quotePlugin ( ) ,
78+ headingsPlugin ( ) ,
79+ linkPlugin ( ) ,
80+ imagePlugin ( ) ,
81+ tablePlugin ( ) ,
82+ thematicBreakPlugin ( ) ,
83+ frontmatterPlugin ( ) ,
84+ codeBlockPlugin ( { defaultCodeBlockLanguage : "go" } ) ,
85+ codeMirrorPlugin ( {
86+ codeBlockLanguages : {
87+ js : "JavaScript" ,
88+ css : "CSS" ,
89+ txt : "text" ,
90+ tsx : "TypeScript" ,
91+ go : "Go" ,
92+ } ,
93+ } ) ,
8694
87- markdownShortcutPlugin ( ) ,
88- ] }
89- />
95+ markdownShortcutPlugin ( ) ,
96+ ] }
97+ />
98+ { maxLength !== undefined && (
99+ < p
100+ className = { classNames (
101+ "mt-1 text-right text-xs" ,
102+ isOverLimit ? "text-destructive" : "text-muted-foreground" ,
103+ ) }
104+ >
105+ { charCount } / { maxLength }
106+ </ p >
107+ ) }
108+ </ div >
90109 ) ;
91110} ;
92111
0 commit comments