chat_message.py 1.9 KB

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