chat_message.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import html
  2. from typing import List, Optional, Union
  3. from ..dependencies import register_component
  4. from ..element import Element
  5. register_component('chat_message', __file__, 'chat_message.js')
  6. class ChatMessage(Element):
  7. def __init__(self,
  8. text: Union[str, List[str]], *,
  9. name: Optional[str] = None,
  10. label: Optional[str] = None,
  11. stamp: Optional[str] = None,
  12. avatar: Optional[str] = None,
  13. sent: bool = False,
  14. text_html: bool = False,
  15. ) -> None:
  16. """Chat Message
  17. Based on Quasar's `Chat Message <https://quasar.dev/vue-components/chat/>`_ component.
  18. :param text: the message body (can be a list of strings for multiple message parts)
  19. :param name: the name of the message author
  20. :param label: renders a label header/section only
  21. :param stamp: timestamp of the message
  22. :param avatar: URL to an avatar
  23. :param sent: render as a sent message (so from current user) (default: False)
  24. :param text_html: render text as HTML (default: False)
  25. """
  26. super().__init__('chat_message')
  27. if isinstance(text, str):
  28. text = [text]
  29. if not text_html:
  30. text = [html.escape(part) for part in text]
  31. text = [part.replace('\n', '<br />') for part in text]
  32. self._props['text'] = text
  33. self._props['text-html'] = True
  34. if name is not None:
  35. self._props['name'] = name
  36. if label is not None:
  37. self._props['label'] = label
  38. if stamp is not None:
  39. self._props['stamp'] = stamp
  40. if avatar is not None:
  41. self._props['avatar'] = avatar
  42. self._props['sent'] = sent