reflex/reflex/components/core/clipboard.py
Khaleel Al-Adhami b2d2719f90
add type hinting to events (#4145)
* add type hinting to events

* fix pyi

* make it a list

* add on change

* dang it darglintz

* add future annotations

* add try except becuse i hate this

* add check for class

* aaaa

* sometimes you need to make hard decisions

* ono

* i hate unions

* add rx event

* move stuff around

* maybe

* special case osmething

* i don't need no test

* remove stray print

Co-authored-by: Masen Furer <m_github@0x26.net>

* remove another stray print

Co-authored-by: Masen Furer <m_github@0x26.net>

* add rx event

---------

Co-authored-by: Masen Furer <m_github@0x26.net>
2024-10-14 08:44:31 -07:00

97 lines
3.1 KiB
Python

"""Global on_paste handling for Reflex app."""
from __future__ import annotations
from typing import Dict, List, Tuple, Union
from reflex.components.base.fragment import Fragment
from reflex.components.tags.tag import Tag
from reflex.event import EventChain, EventHandler, identity_event
from reflex.utils.format import format_prop, wrap
from reflex.utils.imports import ImportVar
from reflex.vars import get_unique_variable_name
from reflex.vars.base import Var
class Clipboard(Fragment):
"""Clipboard component."""
# The element ids to attach the event listener to. Defaults to all child components or the document.
targets: Var[List[str]]
# Called when the user pastes data into the document. Data is a list of tuples of (mime_type, data). Binary types will be base64 encoded as a data uri.
on_paste: EventHandler[identity_event(List[Tuple[str, str]])]
# Save the original event actions for the on_paste event.
on_paste_event_actions: Var[Dict[str, Union[bool, int]]]
@classmethod
def create(cls, *children, **props):
"""Create a Clipboard component.
Args:
*children: The children of the component.
**props: The properties of the component.
Returns:
The Clipboard Component.
"""
if "targets" not in props:
# Add all children as targets if not specified.
targets = props.setdefault("targets", [])
for c in children:
if c.id is None:
c.id = f"clipboard_{get_unique_variable_name()}"
targets.append(c.id)
if "on_paste" in props:
# Capture the event actions for the on_paste handler if not specified.
props.setdefault("on_paste_event_actions", props["on_paste"].event_actions)
return super().create(*children, **props)
def _exclude_props(self) -> list[str]:
return super()._exclude_props() + ["on_paste", "on_paste_event_actions"]
def _render(self) -> Tag:
tag = super()._render()
tag.remove_props("targets")
# Ensure a different Fragment component is created whenever targets differ
tag.add_props(key=self.targets)
return tag
def add_imports(self) -> dict[str, ImportVar]:
"""Add the imports for the Clipboard component.
Returns:
The import dict for the component.
"""
return {
"/utils/helpers/paste.js": ImportVar(
tag="usePasteHandler", is_default=True
),
}
def add_hooks(self) -> list[str]:
"""Add hook to register paste event listener.
Returns:
The hooks to add to the component.
"""
on_paste = self.event_triggers["on_paste"]
if on_paste is None:
return []
if isinstance(on_paste, EventChain):
on_paste = wrap(str(format_prop(on_paste)).strip("{}"), "(")
return [
"usePasteHandler(%s, %s, %s)"
% (
str(self.targets),
str(self.on_paste_event_actions),
on_paste,
)
]
clipboard = Clipboard.create