1
1
import * as React from 'react' ;
2
- import { MouseEvent } from 'react' ;
2
+ import { MouseEvent , useEffect , useState } from 'react' ;
3
3
4
4
import { Editor } from '@tiptap/react' ;
5
5
import {
@@ -18,6 +18,7 @@ import { useTiptapEditor } from '../useTiptapEditor';
18
18
export const FormatButtons = ( props : ToggleButtonGroupProps ) => {
19
19
const editor = useTiptapEditor ( ) ;
20
20
const translate = useTranslate ( ) ;
21
+ const [ values , setValues ] = useState < string [ ] > ( [ ] ) ;
21
22
22
23
const boldLabel = translate ( 'ra.tiptap.bold' , {
23
24
_ : 'Bold' ,
@@ -39,6 +40,31 @@ export const FormatButtons = (props: ToggleButtonGroupProps) => {
39
40
_ : 'Code' ,
40
41
} ) ;
41
42
43
+ useEffect ( ( ) => {
44
+ const handleUpdate = ( ) => {
45
+ setValues ( ( ) =>
46
+ FormatValues . reduce ( ( acc , value ) => {
47
+ if ( editor && editor . isActive ( value ) ) {
48
+ acc . push ( value ) ;
49
+ }
50
+ return acc ;
51
+ } , [ ] )
52
+ ) ;
53
+ } ;
54
+
55
+ if ( editor ) {
56
+ editor . on ( 'update' , handleUpdate ) ;
57
+ editor . on ( 'selectionUpdate' , handleUpdate ) ;
58
+ }
59
+
60
+ return ( ) => {
61
+ if ( editor ) {
62
+ editor . off ( 'update' , handleUpdate ) ;
63
+ editor . off ( 'selectionUpdate' , handleUpdate ) ;
64
+ }
65
+ } ;
66
+ } , [ editor ] ) ;
67
+
42
68
const handleChange = (
43
69
event : MouseEvent < HTMLElement > ,
44
70
newFormats : string [ ]
@@ -59,58 +85,38 @@ export const FormatButtons = (props: ToggleButtonGroupProps) => {
59
85
} ) ;
60
86
} ;
61
87
62
- const value = FormatValues . reduce ( ( acc , value ) => {
63
- if ( editor && editor . isActive ( value ) ) {
64
- acc . push ( value ) ;
65
- }
66
- return acc ;
67
- } , [ ] ) ;
68
-
69
88
return (
70
89
< ToggleButtonGroup
71
90
{ ...props }
72
91
disabled = { ! editor ?. isEditable }
73
92
onChange = { handleChange }
74
- value = { value }
93
+ value = { values }
75
94
>
76
- < ToggleButton
77
- value = "bold"
78
- aria-label = { boldLabel }
79
- title = { boldLabel }
80
- selected = { editor && editor . isActive ( 'bold' ) }
81
- >
95
+ < ToggleButton value = "bold" aria-label = { boldLabel } title = { boldLabel } >
82
96
< FormatBold fontSize = "inherit" />
83
97
</ ToggleButton >
84
98
< ToggleButton
85
99
value = "italic"
86
100
aria-label = { italicLabel }
87
101
title = { italicLabel }
88
- selected = { editor && editor . isActive ( 'italic' ) }
89
102
>
90
103
< FormatItalic fontSize = "inherit" />
91
104
</ ToggleButton >
92
105
< ToggleButton
93
106
value = "underline"
94
107
aria-label = { underlineLabel }
95
108
title = { underlineLabel }
96
- selected = { editor && editor . isActive ( 'underline' ) }
97
109
>
98
110
< FormatUnderlined fontSize = "inherit" />
99
111
</ ToggleButton >
100
112
< ToggleButton
101
113
value = "strike"
102
114
aria-label = { strikeLabel }
103
115
title = { strikeLabel }
104
- selected = { editor && editor . isActive ( 'strike' ) }
105
116
>
106
117
< FormatStrikethrough fontSize = "inherit" />
107
118
</ ToggleButton >
108
- < ToggleButton
109
- value = "code"
110
- aria-label = { codeLabel }
111
- title = { codeLabel }
112
- selected = { editor && editor . isActive ( 'code' ) }
113
- >
119
+ < ToggleButton value = "code" aria-label = { codeLabel } title = { codeLabel } >
114
120
< Code fontSize = "inherit" />
115
121
</ ToggleButton >
116
122
</ ToggleButtonGroup >
0 commit comments