33from __future__ import annotations
44
55import enum
6- from typing import Dict , List , Literal , Optional , Union
6+ from typing import Literal
77
88from reflex .base import Base
99from reflex .components .component import Component , NoSSRComponent
10- from reflex .event import EventHandler
10+ from reflex .event import EventHandler , no_args_event_spec , passthrough_event_spec
1111from reflex .utils .format import to_camel_case
1212from reflex .utils .imports import ImportDict , ImportVar
1313from reflex .vars .base import Var
@@ -48,70 +48,99 @@ class EditorButtonList(list, enum.Enum):
4848
4949class EditorOptions (Base ):
5050 """Some of the additional options to configure the Editor.
51+
5152 Complete list of options found here:
5253 https://github.com/JiHong88/SunEditor/blob/master/README.md#options.
5354 """
5455
5556 # Specifies default tag name of the editor.
5657 # default: 'p' {String}
57- default_tag : Optional [ str ] = None
58+ default_tag : str | None = None
5859
5960 # The mode of the editor ('classic', 'inline', 'balloon', 'balloon-always').
6061 # default: 'classic' {String}
61- mode : Optional [ str ] = None
62+ mode : str | None = None
6263
6364 # If true, the editor is set to RTL(Right To Left) mode.
6465 # default: false {Boolean}
65- rtl : Optional [ bool ] = None
66+ rtl : bool | None = None
6667
6768 # List of buttons to use in the toolbar.
68- button_list : Optional [List [Union [List [str ], str ]]]
69+ button_list : list [list [str ] | str ] | None
70+
71+
72+ def on_blur_spec (_e : Var , content : Var [str ]) -> tuple [Var [str ]]:
73+ """A helper function to specify the on_blur event handler.
74+
75+ Args:
76+ _e: The event.
77+ content: The content of the editor.
78+
79+ Returns:
80+ A tuple containing the content of the editor.
81+ """
82+ return (content ,)
83+
84+
85+ def on_paste_spec (
86+ _e : Var , clean_data : Var [str ], max_char_count : Var [bool ]
87+ ) -> tuple [Var [str ], Var [bool ]]:
88+ """A helper function to specify the on_paste event handler.
89+
90+ Args:
91+ _e: The event.
92+ clean_data: The clean data.
93+ max_char_count: The maximum character count.
94+
95+ Returns:
96+ A tuple containing the clean data and the maximum character count.
97+ """
98+ return (clean_data , max_char_count )
6999
70100
71101class Editor (NoSSRComponent ):
72102 """A Rich Text Editor component based on SunEditor.
103+
73104 Not every JS prop is listed here (some are not easily usable from python),
74105 refer to the library docs for a complete list.
75106 """
76107
77- library = "suneditor-react"
108+ library = "suneditor-react@3.6.1 "
78109
79110 tag = "SunEditor"
80111
81112 is_default = True
82113
83- lib_dependencies : List [str ] = ["suneditor" ]
114+ lib_dependencies : list [str ] = ["suneditor" ]
84115
85116 # Language of the editor.
86117 # Alternatively to a string, a dict of your language can be passed to this prop.
87118 # Please refer to the library docs for this.
88119 # options: "en" | "da" | "de" | "es" | "fr" | "ja" | "ko" | "pt_br" |
89- # "ru" | "zh_cn" | "ro" | "pl" | "ckb" | "lv" | "se" | "ua" | "he" | "it"
90- # default : "en"
120+ # "ru" | "zh_cn" | "ro" | "pl" | "ckb" | "lv" | "se" | "ua" | "he" | "it"
121+ # default: "en".
91122 lang : Var [
92- Union [
93- Literal [
94- "en" ,
95- "da" ,
96- "de" ,
97- "es" ,
98- "fr" ,
99- "ja" ,
100- "ko" ,
101- "pt_br" ,
102- "ru" ,
103- "zh_cn" ,
104- "ro" ,
105- "pl" ,
106- "ckb" ,
107- "lv" ,
108- "se" ,
109- "ua" ,
110- "he" ,
111- "it" ,
112- ],
113- dict ,
123+ Literal [
124+ "en" ,
125+ "da" ,
126+ "de" ,
127+ "es" ,
128+ "fr" ,
129+ "ja" ,
130+ "ko" ,
131+ "pt_br" ,
132+ "ru" ,
133+ "zh_cn" ,
134+ "ro" ,
135+ "pl" ,
136+ "ckb" ,
137+ "lv" ,
138+ "se" ,
139+ "ua" ,
140+ "he" ,
141+ "it" ,
114142 ]
143+ | dict
115144 ]
116145
117146 # This is used to set the HTML form name of the editor.
@@ -121,7 +150,7 @@ class Editor(NoSSRComponent):
121150
122151 # Sets the default value of the editor.
123152 # This is useful if you don't want the on_change method to be called on render.
124- # If you want the on_change method to be called on render please use the set_contents prop
153+ # If you want the on_change method to be called on render use the set_contents prop
125154 default_value : Var [str ]
126155
127156 # Sets the width of the editor.
@@ -140,10 +169,10 @@ class Editor(NoSSRComponent):
140169 auto_focus : Var [bool ]
141170
142171 # Pass an EditorOptions instance to modify the behaviour of Editor even more.
143- set_options : Var [Dict ]
172+ set_options : Var [dict ]
144173
145174 # Whether all SunEditor plugins should be loaded.
146- # default: True
175+ # default: True.
147176 set_all_plugins : Var [bool ]
148177
149178 # Set the content of the editor.
@@ -162,52 +191,47 @@ class Editor(NoSSRComponent):
162191 set_default_style : Var [str ]
163192
164193 # Disable the editor
165- # default: False
194+ # default: False.
166195 disable : Var [bool ]
167196
168197 # Hide the editor
169- # default: False
198+ # default: False.
170199 hide : Var [bool ]
171200
172201 # Hide the editor toolbar
173- # default: False
202+ # default: False.
174203 hide_toolbar : Var [bool ]
175204
176205 # Disable the editor toolbar
177- # default: False
206+ # default: False.
178207 disable_toolbar : Var [bool ]
179208
180209 # Fired when the editor content changes.
181- on_change : EventHandler [lambda content : [ content ] ]
210+ on_change : EventHandler [passthrough_event_spec ( str ) ]
182211
183212 # Fired when the something is inputted in the editor.
184- on_input : EventHandler [lambda e : [ e ] ]
213+ on_input : EventHandler [no_args_event_spec ]
185214
186215 # Fired when the editor loses focus.
187- on_blur : EventHandler [lambda e , content : [ content ] ]
216+ on_blur : EventHandler [on_blur_spec ]
188217
189218 # Fired when the editor is loaded.
190- on_load : EventHandler [lambda reload : [reload ]]
191-
192- # Fired when the editor is resized.
193- on_resize_editor : EventHandler [lambda height , prev_height : [height , prev_height ]]
219+ on_load : EventHandler [passthrough_event_spec (bool )]
194220
195221 # Fired when the editor content is copied.
196- on_copy : EventHandler [lambda e , clipboard_data : [ clipboard_data ] ]
222+ on_copy : EventHandler [no_args_event_spec ]
197223
198224 # Fired when the editor content is cut.
199- on_cut : EventHandler [lambda e , clipboard_data : [ clipboard_data ] ]
225+ on_cut : EventHandler [no_args_event_spec ]
200226
201227 # Fired when the editor content is pasted.
202- on_paste : EventHandler [
203- lambda e , clean_data , max_char_count : [clean_data , max_char_count ]
204- ]
228+ on_paste : EventHandler [on_paste_spec ]
205229
206230 # Fired when the code view is toggled.
207- toggle_code_view : EventHandler [lambda is_code_view : [ is_code_view ] ]
231+ toggle_code_view : EventHandler [passthrough_event_spec ( bool ) ]
208232
209233 # Fired when the full screen mode is toggled.
210- toggle_full_screen : EventHandler [lambda is_full_screen : [ is_full_screen ] ]
234+ toggle_full_screen : EventHandler [passthrough_event_spec ( bool ) ]
211235
212236 def add_imports (self ) -> ImportDict :
213237 """Add imports for the Editor component.
@@ -220,11 +244,13 @@ def add_imports(self) -> ImportDict:
220244 }
221245
222246 @classmethod
223- def create (cls , set_options : Optional [EditorOptions ] = None , ** props ) -> Component :
247+ def create (
248+ cls , set_options : EditorOptions | None = None , ** props : object
249+ ) -> Component :
224250 """Create an instance of Editor. No children allowed.
225251
226252 Args:
227- set_options(Optional[EditorOptions]) : Configuration object to further configure the instance.
253+ set_options: Configuration object to further configure the instance.
228254 **props: Any properties to be passed to the Editor
229255
230256 Returns:
@@ -235,10 +261,11 @@ def create(cls, set_options: Optional[EditorOptions] = None, **props) -> Compone
235261 """
236262 if set_options is not None :
237263 if isinstance (set_options , Var ):
238- raise ValueError ("EditorOptions cannot be a state Var" )
264+ msg = "EditorOptions cannot be a state Var"
265+ raise ValueError (msg )
239266 props ["set_options" ] = {
240267 to_camel_case (k ): v
241268 for k , v in set_options .dict ().items ()
242269 if v is not None
243270 }
244- return super ().create (* [], ** props )
271+ return super ().create (* [], ** props )
0 commit comments