Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add event types to suneditor #4209

Merged
merged 1 commit into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 40 additions & 16 deletions reflex/components/suneditor/editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
from __future__ import annotations

import enum
from typing import Dict, List, Literal, Optional, Union
from typing import Dict, List, Literal, Optional, Tuple, Union

from reflex.base import Base
from reflex.components.component import Component, NoSSRComponent
from reflex.event import EventHandler
from reflex.event import EventHandler, empty_event, identity_event
from reflex.utils.format import to_camel_case
from reflex.utils.imports import ImportDict, ImportVar
from reflex.vars.base import Var
Expand Down Expand Up @@ -68,6 +68,35 @@ class EditorOptions(Base):
button_list: Optional[List[Union[List[str], str]]]


def on_blur_spec(e: Var, content: Var[str]) -> Tuple[Var[str]]:
"""A helper function to specify the on_blur event handler.

Args:
e: The event.
content: The content of the editor.

Returns:
A tuple containing the content of the editor.
"""
return (content,)


def on_paste_spec(
e: Var, clean_data: Var[str], max_char_count: Var[bool]
) -> Tuple[Var[str], Var[bool]]:
"""A helper function to specify the on_paste event handler.

Args:
e: The event.
clean_data: The clean data.
max_char_count: The maximum character count.

Returns:
A tuple containing the clean data and the maximum character count.
"""
return (clean_data, max_char_count)


class Editor(NoSSRComponent):
"""A Rich Text Editor component based on SunEditor.
Not every JS prop is listed here (some are not easily usable from python),
Expand Down Expand Up @@ -178,36 +207,31 @@ class Editor(NoSSRComponent):
disable_toolbar: Var[bool]

# Fired when the editor content changes.
on_change: EventHandler[lambda content: [content]]
on_change: EventHandler[identity_event(str)]

# Fired when the something is inputted in the editor.
on_input: EventHandler[lambda e: [e]]
on_input: EventHandler[empty_event]

# Fired when the editor loses focus.
on_blur: EventHandler[lambda e, content: [content]]
on_blur: EventHandler[on_blur_spec]

# Fired when the editor is loaded.
on_load: EventHandler[lambda reload: [reload]]

# Fired when the editor is resized.
on_resize_editor: EventHandler[lambda height, prev_height: [height, prev_height]]
on_load: EventHandler[identity_event(bool)]

# Fired when the editor content is copied.
on_copy: EventHandler[lambda e, clipboard_data: [clipboard_data]]
on_copy: EventHandler[empty_event]

# Fired when the editor content is cut.
on_cut: EventHandler[lambda e, clipboard_data: [clipboard_data]]
on_cut: EventHandler[empty_event]

# Fired when the editor content is pasted.
on_paste: EventHandler[
lambda e, clean_data, max_char_count: [clean_data, max_char_count]
]
on_paste: EventHandler[on_paste_spec]

# Fired when the code view is toggled.
toggle_code_view: EventHandler[lambda is_code_view: [is_code_view]]
toggle_code_view: EventHandler[identity_event(bool)]

# Fired when the full screen mode is toggled.
toggle_full_screen: EventHandler[lambda is_full_screen: [is_full_screen]]
toggle_full_screen: EventHandler[identity_event(bool)]

def add_imports(self) -> ImportDict:
"""Add imports for the Editor component.
Expand Down
18 changes: 11 additions & 7 deletions reflex/components/suneditor/editor.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# This file was generated by `reflex/utils/pyi_generator.py`!
# ------------------------------------------------------
import enum
from typing import Any, Dict, List, Literal, Optional, Union, overload
from typing import Any, Dict, List, Literal, Optional, Tuple, Union, overload

from reflex.base import Base
from reflex.components.component import NoSSRComponent
Expand Down Expand Up @@ -44,6 +44,11 @@ class EditorOptions(Base):
rtl: Optional[bool]
button_list: Optional[List[Union[List[str], str]]]

def on_blur_spec(e: Var, content: Var[str]) -> Tuple[Var[str]]: ...
def on_paste_spec(
e: Var, clean_data: Var[str], max_char_count: Var[bool]
) -> Tuple[Var[str], Var[bool]]: ...

class Editor(NoSSRComponent):
def add_imports(self) -> ImportDict: ...
@overload
Expand Down Expand Up @@ -122,15 +127,15 @@ class Editor(NoSSRComponent):
class_name: Optional[Any] = None,
autofocus: Optional[bool] = None,
custom_attrs: Optional[Dict[str, Union[Var, str]]] = None,
on_blur: Optional[EventType] = None,
on_blur: Optional[EventType[str]] = None,
on_change: Optional[EventType] = None,
on_click: Optional[EventType[[]]] = None,
on_context_menu: Optional[EventType[[]]] = None,
on_copy: Optional[EventType] = None,
on_cut: Optional[EventType] = None,
on_copy: Optional[EventType[[]]] = None,
on_cut: Optional[EventType[[]]] = None,
on_double_click: Optional[EventType[[]]] = None,
on_focus: Optional[EventType[[]]] = None,
on_input: Optional[EventType] = None,
on_input: Optional[EventType[[]]] = None,
on_load: Optional[EventType] = None,
on_mount: Optional[EventType[[]]] = None,
on_mouse_down: Optional[EventType[[]]] = None,
Expand All @@ -140,8 +145,7 @@ class Editor(NoSSRComponent):
on_mouse_out: Optional[EventType[[]]] = None,
on_mouse_over: Optional[EventType[[]]] = None,
on_mouse_up: Optional[EventType[[]]] = None,
on_paste: Optional[EventType] = None,
on_resize_editor: Optional[EventType] = None,
on_paste: Optional[EventType[str, bool]] = None,
on_scroll: Optional[EventType[[]]] = None,
on_unmount: Optional[EventType[[]]] = None,
toggle_code_view: Optional[EventType] = None,
Expand Down
Loading