11import RichTextEditor from '@/components/commonUI/RichText/RichTextEditor'
2+ import useTextCounter from '@/hooks/useTextCounter'
23import { Box } from '@chakra-ui/react'
34import type { Editor } from '@tiptap/react'
5+ import { useCallback , useMemo , useState } from 'react'
6+ import TextCounter from '../commonUI/TextCounter'
47
58export interface CardEditorProps {
69 side : 'front' | 'back'
@@ -10,16 +13,40 @@ export interface CardEditorProps {
1013}
1114
1215export default function CardEditor ( { side, isFlipped, frontEditor, backEditor } : CardEditorProps ) {
13- const commonBoxStyles = {
14- position : 'absolute' as const ,
15- width : '100%' ,
16- height : '100%' ,
17- backfaceVisibility : 'hidden' as const ,
18- borderRadius : 'lg' ,
19- borderWidth : '1px' ,
20- borderColor : 'bg.200' ,
21- cursor : 'text' ,
22- }
16+ const commonBoxStyles = useMemo (
17+ ( ) => ( {
18+ position : 'absolute' as const ,
19+ width : '100%' ,
20+ height : '100%' ,
21+ backfaceVisibility : 'hidden' as const ,
22+ borderRadius : 'lg' ,
23+ borderWidth : '1px' ,
24+ borderColor : 'bg.200' ,
25+ cursor : 'text' ,
26+ } ) ,
27+ [ ] ,
28+ )
29+
30+ const [ frontTextLength , setFrontTextLength ] = useState ( 0 )
31+ const [ backTextLength , setBackTextLength ] = useState ( 0 )
32+
33+ const memoizedSetFrontTextLength = useCallback ( setFrontTextLength , [ ] )
34+ const memoizedSetBackTextLength = useCallback ( setBackTextLength , [ ] )
35+ const noopSetter = useCallback ( ( ) => { } , [ ] )
36+
37+ useTextCounter (
38+ side === 'front' ? frontEditor : null ,
39+ side === 'front' ? memoizedSetFrontTextLength : noopSetter ,
40+ )
41+ useTextCounter (
42+ side === 'back' ? backEditor : null ,
43+ side === 'back' ? memoizedSetBackTextLength : noopSetter ,
44+ )
45+
46+ const currentTextLength = useMemo (
47+ ( ) => ( side === 'front' ? frontTextLength : backTextLength ) ,
48+ [ side , frontTextLength , backTextLength ] ,
49+ )
2350
2451 return (
2552 < Box
@@ -33,6 +60,7 @@ export default function CardEditor({ side, isFlipped, frontEditor, backEditor }:
3360 >
3461 < Box { ...commonBoxStyles } bg = "bg.50" >
3562 { side === 'front' && frontEditor && < RichTextEditor editor = { frontEditor } /> }
63+ { side === 'front' && < TextCounter textLength = { currentTextLength } /> }
3664 </ Box >
3765
3866 < Box
@@ -42,6 +70,7 @@ export default function CardEditor({ side, isFlipped, frontEditor, backEditor }:
4270 visibility = { isFlipped ? 'visible' : 'hidden' }
4371 >
4472 { side === 'back' && backEditor && < RichTextEditor editor = { backEditor } /> }
73+ { side === 'back' && < TextCounter textLength = { currentTextLength } /> }
4574 </ Box >
4675 </ Box >
4776 )
0 commit comments