annotate libervia/web/pages/chat/_browser/__init__.py @ 1574:4ab4bc3ae462

browser: new `tools` module with a first method to detect touch devices
author Goffi <goffi@goffi.org>
date Wed, 22 Nov 2023 16:31:36 +0100
parents fb31d3dba0c3
children 9ba532041a8e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1536
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
1 import json
1541
3c384b51b2a1 browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents: 1540
diff changeset
2 import re
1536
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
3
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
4 from bridge import AsyncBridge as Bridge
1541
3c384b51b2a1 browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents: 1540
diff changeset
5 from browser import DOMNode, aio, bind, console as log, document, window, html
3c384b51b2a1 browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents: 1540
diff changeset
6 from cache import cache, identities
3c384b51b2a1 browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents: 1540
diff changeset
7 import dialog
3c384b51b2a1 browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents: 1540
diff changeset
8 from file_uploader import FileUploader
3c384b51b2a1 browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents: 1540
diff changeset
9 import jid
1536
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
10 from template import Template, safe
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
11
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
12 log.warning = log.warn
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
13 profile = window.profile or ""
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
14 own_jid = jid.JID(window.own_jid)
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
15 target_jid = jid.JID(window.target_jid)
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
16 bridge = Bridge()
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
17
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
18 # Sensible value to consider that user is at the bottom
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
19 SCROLL_SENSITIVITY = 200
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
20
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
21
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
22 class LiberviaWebChat:
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
23 def __init__(self):
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
24 self.message_tpl = Template("chat/message.html")
1541
3c384b51b2a1 browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents: 1540
diff changeset
25 self.url_preview_control_tpl = Template("components/url_preview_control.html")
3c384b51b2a1 browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents: 1540
diff changeset
26 self.url_preview_tpl = Template("components/url_preview.html")
1542
fb31d3dba0c3 browser (chat): add marker for new messages when page is not visible
Goffi <goffi@goffi.org>
parents: 1541
diff changeset
27 self.new_messages_marker_elt = Template("chat/new_messages_marker.html").get_elt()
1541
3c384b51b2a1 browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents: 1540
diff changeset
28
1536
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
29 self.messages_elt = document["messages"]
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
30
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
31 # attachments
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
32 self.file_uploader = FileUploader(
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
33 "", "chat/attachment_preview.html", on_delete_cb=self.on_attachment_delete
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
34 )
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
35 self.attachments_elt = document["attachments"]
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
36 self.message_input = document["message_input"]
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
37
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
38 # hide/show attachments
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
39 MutationObserver = window.MutationObserver
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
40 observer = MutationObserver.new(lambda *__: self.update_attachments_visibility())
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
41 observer.observe(self.attachments_elt, {"childList": True})
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
42
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
43 # we want the message scroll to be initially at the bottom
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
44 self.messages_elt.scrollTop = self.messages_elt.scrollHeight
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
45
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
46 @property
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
47 def is_at_bottom(self):
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
48 return (
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
49 self.messages_elt.scrollHeight
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
50 - self.messages_elt.scrollTop
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
51 - self.messages_elt.clientHeight
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
52 <= SCROLL_SENSITIVITY
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
53 )
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
54
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
55 def send_message(self):
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
56 """Send message currently in input area
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
57
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
58 The message and corresponding attachment will be sent
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
59 """
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
60 message = self.message_input.value.rstrip()
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
61 log.info(f"{message=}")
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
62
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
63 # attachments
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
64 attachments = []
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
65 for attachment_elt in self.attachments_elt.children:
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
66 file_data = json.loads(attachment_elt.getAttribute("data-file"))
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
67 attachments.append(file_data)
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
68
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
69 if message or attachments:
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
70 extra = {}
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
71
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
72 if attachments:
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
73 extra["attachments"] = attachments
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
74
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
75 # now we send the message
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
76 try:
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
77 aio.run(
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
78 bridge.message_send(
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
79 str(target_jid), {"": message}, {}, "auto", json.dumps(extra)
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
80 )
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
81 )
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
82 except Exception as e:
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
83 dialog.notification.show(f"Can't send message: {e}", "error")
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
84 else:
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
85 self.message_input.value = ""
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
86 self.attachments_elt.clear()
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
87
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
88 def _on_message_new(
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
89 self,
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
90 uid: str,
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
91 timestamp: float,
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
92 from_jid: str,
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
93 to_jid: str,
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
94 message: dict,
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
95 subject: dict,
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
96 mess_type: str,
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
97 extra_s: str,
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
98 profile: str,
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
99 ) -> None:
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
100 if (
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
101 jid.JID(from_jid).bare == window.target_jid
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
102 or jid.JID(to_jid).bare == window.target_jid
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
103 ):
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
104 aio.run(
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
105 self.on_message_new(
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
106 uid,
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
107 timestamp,
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
108 from_jid,
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
109 to_jid,
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
110 message,
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
111 subject,
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
112 mess_type,
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
113 json.loads(extra_s),
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
114 profile,
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
115 )
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
116 )
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
117
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
118 async def on_message_new(
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
119 self,
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
120 uid: str,
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
121 timestamp: float,
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
122 from_jid: str,
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
123 to_jid: str,
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
124 message_data: dict,
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
125 subject_data: dict,
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
126 mess_type: str,
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
127 extra: dict,
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
128 profile: str,
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
129 ) -> None:
1542
fb31d3dba0c3 browser (chat): add marker for new messages when page is not visible
Goffi <goffi@goffi.org>
parents: 1541
diff changeset
130 # FIXME: visibilityState doesn't detect OS events such as `Alt + Tab`, using focus
fb31d3dba0c3 browser (chat): add marker for new messages when page is not visible
Goffi <goffi@goffi.org>
parents: 1541
diff changeset
131 # event may help to get those use cases, but it gives false positives.
fb31d3dba0c3 browser (chat): add marker for new messages when page is not visible
Goffi <goffi@goffi.org>
parents: 1541
diff changeset
132 if document.visibilityState == "hidden" and self.new_messages_marker_elt.parent is None:
fb31d3dba0c3 browser (chat): add marker for new messages when page is not visible
Goffi <goffi@goffi.org>
parents: 1541
diff changeset
133 # the page is not visible, and we have no new messages marker yet, so we add
fb31d3dba0c3 browser (chat): add marker for new messages when page is not visible
Goffi <goffi@goffi.org>
parents: 1541
diff changeset
134 # it
fb31d3dba0c3 browser (chat): add marker for new messages when page is not visible
Goffi <goffi@goffi.org>
parents: 1541
diff changeset
135 self.messages_elt <= self.new_messages_marker_elt
1536
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
136 xhtml_data = extra.get("xhtml")
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
137 if not xhtml_data:
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
138 xhtml = None
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
139 else:
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
140 try:
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
141 xhtml = xhtml_data[""]
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
142 except KeyError:
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
143 xhtml = next(iter(xhtml_data.values()))
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
144
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
145 await cache.fill_identities([from_jid])
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
146
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
147 msg_data = {
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
148 "id": uid,
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
149 "timestamp": timestamp,
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
150 "type": mess_type,
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
151 "from_": from_jid,
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
152 "text": message_data.get("") or next(iter(message_data.values()), ""),
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
153 "subject": subject_data.get("") or next(iter(subject_data.values()), ""),
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
154 "type": mess_type,
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
155 "thread": extra.get("thread"),
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
156 "thread_parent": extra.get("thread_parent"),
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
157 "reeceived": extra.get("received_timestamp") or timestamp,
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
158 "delay_sender": extra.get("delay_sender"),
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
159 "info_type": extra.get("info_type"),
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
160 "html": safe(xhtml) if xhtml else None,
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
161 "encrypted": extra.get("encrypted", False),
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
162 "received": extra.get("received", False),
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
163 "edited": extra.get("edited", False),
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
164 "attachments": extra.get("attachments", []),
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
165 }
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
166 message_elt = self.message_tpl.get_elt(
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
167 {
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
168 "own_jid": own_jid,
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
169 "msg": msg_data,
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
170 "identities": identities,
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
171 }
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
172 )
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
173
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
174 # Check if user is viewing older messages or is at the bottom
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
175 is_at_bottom = self.is_at_bottom
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
176
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
177 self.messages_elt <= message_elt
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
178 self.make_attachments_dynamic(message_elt)
1541
3c384b51b2a1 browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents: 1540
diff changeset
179 # we add preview in parallel on purpose, as they can be slow to get
3c384b51b2a1 browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents: 1540
diff changeset
180 self.handle_url_previews(message_elt)
1536
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
181
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
182 # If user was at the bottom, keep the scroll at the bottom
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
183 if is_at_bottom:
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
184 self.messages_elt.scrollTop = self.messages_elt.scrollHeight
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
185
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
186 def auto_resize_message_input(self):
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
187 """Resize the message input field according to content."""
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
188
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
189 is_at_bottom = self.is_at_bottom
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
190
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
191 # The textarea's height is first reset to 'auto' to ensure it's not influenced by
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
192 # the previous content.
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
193 self.message_input.style.height = "auto"
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
194
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
195 # Then the height is set to the scrollHeight of the textarea (which is the height
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
196 # of the content), plus the vertical border, resulting in a textarea that grows as
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
197 # more lines of text are added.
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
198 self.message_input.style.height = f"{self.message_input.scrollHeight + 2}px"
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
199
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
200 if is_at_bottom:
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
201 # we want the message are to still display the last message
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
202 self.messages_elt.scrollTop = self.messages_elt.scrollHeight
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
203
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
204 def on_message_keydown(self, evt):
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
205 """Handle the 'keydown' event of the message input field
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
206
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
207 @param evt: The event object. 'target' refers to the textarea element.
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
208 """
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
209 if evt.keyCode == 13: # <Enter> key
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
210 if not window.navigator.maxTouchPoints:
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
211 # we have a non touch device, we send message on <Enter>
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
212 if not evt.shiftKey:
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
213 evt.preventDefault() # Prevents line break
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
214 self.send_message()
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
215
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
216 def update_attachments_visibility(self):
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
217 if len(self.attachments_elt.children):
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
218 self.attachments_elt.classList.remove("is-contracted")
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
219 else:
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
220 self.attachments_elt.classList.add("is-contracted")
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
221
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
222 def on_file_selected(self, evt):
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
223 """Handle file selection"""
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
224 log.info("file selected")
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
225 files = evt.currentTarget.files
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
226 self.file_uploader.upload_files(files, self.attachments_elt)
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
227 self.message_input.focus()
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
228
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
229 def on_attachment_delete(self, evt):
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
230 evt.stopPropagation()
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
231 target = evt.currentTarget
1540
b4342176fa0a browser (chat): minor reformatting
Goffi <goffi@goffi.org>
parents: 1536
diff changeset
232 item_elt = DOMNode(target.closest(".attachment-preview"))
1536
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
233 item_elt.remove()
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
234
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
235 def on_attach_button_click(self, evt):
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
236 document["file_input"].click()
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
237
1540
b4342176fa0a browser (chat): minor reformatting
Goffi <goffi@goffi.org>
parents: 1536
diff changeset
238 @bind(document["attachments"], "wheel")
1536
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
239 def wheel_event(evt):
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
240 """Make the mouse wheel to act on horizontal scrolling for attachments
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
241
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
242 Attachments don't have vertical scrolling, thus is makes sense to use the wheel
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
243 for horizontal scrolling
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
244 """
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
245 if evt.deltaY != 0:
1540
b4342176fa0a browser (chat): minor reformatting
Goffi <goffi@goffi.org>
parents: 1536
diff changeset
246 document["attachments"].scrollLeft += evt.deltaY * 0.8
1536
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
247 evt.preventDefault()
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
248
1540
b4342176fa0a browser (chat): minor reformatting
Goffi <goffi@goffi.org>
parents: 1536
diff changeset
249 def make_attachments_dynamic(self, parent_elt=None):
1536
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
250 """Make attachments dynamically clickable"""
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
251 # FIXME: only handle images for now, and display them in a modal
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
252 if parent_elt is None:
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
253 parent_elt = document
1540
b4342176fa0a browser (chat): minor reformatting
Goffi <goffi@goffi.org>
parents: 1536
diff changeset
254 img_elts = parent_elt.select(".message-attachment img")
1536
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
255 for img_elt in img_elts:
1540
b4342176fa0a browser (chat): minor reformatting
Goffi <goffi@goffi.org>
parents: 1536
diff changeset
256 img_elt.bind("click", self.open_modal)
b4342176fa0a browser (chat): minor reformatting
Goffi <goffi@goffi.org>
parents: 1536
diff changeset
257 img_elt.style.cursor = "pointer"
1536
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
258
1540
b4342176fa0a browser (chat): minor reformatting
Goffi <goffi@goffi.org>
parents: 1536
diff changeset
259 close_button = document.select_one(".modal-close")
b4342176fa0a browser (chat): minor reformatting
Goffi <goffi@goffi.org>
parents: 1536
diff changeset
260 close_button.bind("click", self.close_modal)
1536
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
261
1541
3c384b51b2a1 browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents: 1540
diff changeset
262 def find_links(self, message_elt):
3c384b51b2a1 browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents: 1540
diff changeset
263 """Find all http and https links within the body of a message."""
3c384b51b2a1 browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents: 1540
diff changeset
264 msg_body_elt = message_elt.select_one(".msg_body")
3c384b51b2a1 browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents: 1540
diff changeset
265 if not msg_body_elt:
3c384b51b2a1 browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents: 1540
diff changeset
266 return
3c384b51b2a1 browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents: 1540
diff changeset
267
3c384b51b2a1 browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents: 1540
diff changeset
268 # Extracting links from text content
3c384b51b2a1 browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents: 1540
diff changeset
269 text = msg_body_elt.text
3c384b51b2a1 browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents: 1540
diff changeset
270 raw_urls = re.findall(
3c384b51b2a1 browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents: 1540
diff changeset
271 r"http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\\(\\),]|(?:%[0-9a-fA-F][0-9a-fA-F])|#)+",
3c384b51b2a1 browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents: 1540
diff changeset
272 text,
3c384b51b2a1 browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents: 1540
diff changeset
273 )
3c384b51b2a1 browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents: 1540
diff changeset
274
3c384b51b2a1 browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents: 1540
diff changeset
275 # Extracting links from <a> elements
3c384b51b2a1 browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents: 1540
diff changeset
276 a_elements = msg_body_elt.select("a")
3c384b51b2a1 browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents: 1540
diff changeset
277 for a_elt in a_elements:
3c384b51b2a1 browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents: 1540
diff changeset
278 href = a_elt.attrs.get("href", "")
3c384b51b2a1 browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents: 1540
diff changeset
279 if href.startswith("http://") or href.startswith("https://"):
3c384b51b2a1 browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents: 1540
diff changeset
280 raw_urls.append(href)
3c384b51b2a1 browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents: 1540
diff changeset
281
3c384b51b2a1 browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents: 1540
diff changeset
282 # we remove duplicates
3c384b51b2a1 browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents: 1540
diff changeset
283 urls = list(dict.fromkeys(raw_urls))
3c384b51b2a1 browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents: 1540
diff changeset
284
3c384b51b2a1 browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents: 1540
diff changeset
285 return urls
3c384b51b2a1 browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents: 1540
diff changeset
286
3c384b51b2a1 browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents: 1540
diff changeset
287 async def add_url_previews(self, url_previews_elt, urls) -> None:
3c384b51b2a1 browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents: 1540
diff changeset
288 """Add URL previews to the .url-previews element of a message."""
3c384b51b2a1 browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents: 1540
diff changeset
289 url_previews_elt.clear()
3c384b51b2a1 browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents: 1540
diff changeset
290 for url in urls:
3c384b51b2a1 browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents: 1540
diff changeset
291 try:
3c384b51b2a1 browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents: 1540
diff changeset
292 url_preview_data_s = await bridge.url_preview_get(url, "")
3c384b51b2a1 browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents: 1540
diff changeset
293 except Exception as e:
3c384b51b2a1 browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents: 1540
diff changeset
294 log.warning(f"Couldn't get URL preview for {url}: {e}")
3c384b51b2a1 browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents: 1540
diff changeset
295 continue
3c384b51b2a1 browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents: 1540
diff changeset
296
3c384b51b2a1 browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents: 1540
diff changeset
297 if not url_preview_data_s:
3c384b51b2a1 browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents: 1540
diff changeset
298 log.warning(f"No preview could be found for URL: {url}")
3c384b51b2a1 browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents: 1540
diff changeset
299 continue
3c384b51b2a1 browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents: 1540
diff changeset
300
3c384b51b2a1 browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents: 1540
diff changeset
301 url_preview_data = json.loads(url_preview_data_s)
3c384b51b2a1 browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents: 1540
diff changeset
302
3c384b51b2a1 browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents: 1540
diff changeset
303 url_preview_elt = self.url_preview_tpl.get_elt(
3c384b51b2a1 browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents: 1540
diff changeset
304 {"url_preview": url_preview_data}
3c384b51b2a1 browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents: 1540
diff changeset
305 )
3c384b51b2a1 browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents: 1540
diff changeset
306 url_previews_elt <= url_preview_elt
3c384b51b2a1 browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents: 1540
diff changeset
307
3c384b51b2a1 browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents: 1540
diff changeset
308
3c384b51b2a1 browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents: 1540
diff changeset
309 def handle_url_previews(self, parent_elt=None):
3c384b51b2a1 browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents: 1540
diff changeset
310 """Check if URL are presents in a message and show appropriate element
3c384b51b2a1 browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents: 1540
diff changeset
311
3c384b51b2a1 browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents: 1540
diff changeset
312 According to settings, either a preview control panel will be shown to wait for
3c384b51b2a1 browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents: 1540
diff changeset
313 user click, or directly the previews, or nothing at all.
3c384b51b2a1 browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents: 1540
diff changeset
314 """
3c384b51b2a1 browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents: 1540
diff changeset
315
3c384b51b2a1 browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents: 1540
diff changeset
316 if parent_elt is None:
3c384b51b2a1 browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents: 1540
diff changeset
317 parent_elt = document
3c384b51b2a1 browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents: 1540
diff changeset
318 chat_message_elts = parent_elt.select(".is-chat-message")
3c384b51b2a1 browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents: 1540
diff changeset
319 else:
3c384b51b2a1 browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents: 1540
diff changeset
320 chat_message_elts = [parent_elt]
3c384b51b2a1 browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents: 1540
diff changeset
321 for message_elt in chat_message_elts:
3c384b51b2a1 browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents: 1540
diff changeset
322 urls = self.find_links(message_elt)
3c384b51b2a1 browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents: 1540
diff changeset
323 if urls:
3c384b51b2a1 browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents: 1540
diff changeset
324 url_previews_elt = message_elt.select_one(".url-previews")
3c384b51b2a1 browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents: 1540
diff changeset
325 url_previews_elt.classList.remove("is-hidden")
3c384b51b2a1 browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents: 1540
diff changeset
326 preview_control_elt = self.url_preview_control_tpl.get_elt()
3c384b51b2a1 browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents: 1540
diff changeset
327 fetch_preview_btn = preview_control_elt.select_one(".action_fetch_preview")
3c384b51b2a1 browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents: 1540
diff changeset
328 fetch_preview_btn.bind(
3c384b51b2a1 browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents: 1540
diff changeset
329 "click",
3c384b51b2a1 browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents: 1540
diff changeset
330 lambda __, previews_elt=url_previews_elt, preview_urls=urls: aio.run(
3c384b51b2a1 browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents: 1540
diff changeset
331 self.add_url_previews(previews_elt, preview_urls)
3c384b51b2a1 browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents: 1540
diff changeset
332 )
3c384b51b2a1 browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents: 1540
diff changeset
333 )
3c384b51b2a1 browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents: 1540
diff changeset
334 url_previews_elt <= preview_control_elt
3c384b51b2a1 browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents: 1540
diff changeset
335
1536
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
336 def open_modal(self, evt):
1540
b4342176fa0a browser (chat): minor reformatting
Goffi <goffi@goffi.org>
parents: 1536
diff changeset
337 modal_image = document.select_one("#modal-image")
1536
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
338 modal_image.src = evt.target.src
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
339 modal_image.alt = evt.target.alt
1540
b4342176fa0a browser (chat): minor reformatting
Goffi <goffi@goffi.org>
parents: 1536
diff changeset
340 modal = document.select_one("#modal")
b4342176fa0a browser (chat): minor reformatting
Goffi <goffi@goffi.org>
parents: 1536
diff changeset
341 modal.classList.add("is-active")
1536
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
342
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
343 def close_modal(self, evt):
1540
b4342176fa0a browser (chat): minor reformatting
Goffi <goffi@goffi.org>
parents: 1536
diff changeset
344 modal = document.select_one("#modal")
b4342176fa0a browser (chat): minor reformatting
Goffi <goffi@goffi.org>
parents: 1536
diff changeset
345 modal.classList.remove("is-active")
1536
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
346
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
347
1542
fb31d3dba0c3 browser (chat): add marker for new messages when page is not visible
Goffi <goffi@goffi.org>
parents: 1541
diff changeset
348 def handle_visibility_change(self, evt):
fb31d3dba0c3 browser (chat): add marker for new messages when page is not visible
Goffi <goffi@goffi.org>
parents: 1541
diff changeset
349 if (
fb31d3dba0c3 browser (chat): add marker for new messages when page is not visible
Goffi <goffi@goffi.org>
parents: 1541
diff changeset
350 document.visibilityState == "hidden"
fb31d3dba0c3 browser (chat): add marker for new messages when page is not visible
Goffi <goffi@goffi.org>
parents: 1541
diff changeset
351 and self.new_messages_marker_elt.parent is not None
fb31d3dba0c3 browser (chat): add marker for new messages when page is not visible
Goffi <goffi@goffi.org>
parents: 1541
diff changeset
352 ):
fb31d3dba0c3 browser (chat): add marker for new messages when page is not visible
Goffi <goffi@goffi.org>
parents: 1541
diff changeset
353 # if there is a new messages marker, we remove it
fb31d3dba0c3 browser (chat): add marker for new messages when page is not visible
Goffi <goffi@goffi.org>
parents: 1541
diff changeset
354 self.new_messages_marker_elt.remove()
fb31d3dba0c3 browser (chat): add marker for new messages when page is not visible
Goffi <goffi@goffi.org>
parents: 1541
diff changeset
355
fb31d3dba0c3 browser (chat): add marker for new messages when page is not visible
Goffi <goffi@goffi.org>
parents: 1541
diff changeset
356
1536
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
357 libervia_web_chat = LiberviaWebChat()
1540
b4342176fa0a browser (chat): minor reformatting
Goffi <goffi@goffi.org>
parents: 1536
diff changeset
358
1536
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
359 document["message_input"].bind(
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
360 "input", lambda __: libervia_web_chat.auto_resize_message_input()
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
361 )
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
362 document["message_input"].bind("keydown", libervia_web_chat.on_message_keydown)
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
363 document["send_button"].bind("click", lambda __: libervia_web_chat.send_message())
1540
b4342176fa0a browser (chat): minor reformatting
Goffi <goffi@goffi.org>
parents: 1536
diff changeset
364 document["attach_button"].bind("click", libervia_web_chat.on_attach_button_click)
1536
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
365 document["file_input"].bind("change", libervia_web_chat.on_file_selected)
1542
fb31d3dba0c3 browser (chat): add marker for new messages when page is not visible
Goffi <goffi@goffi.org>
parents: 1541
diff changeset
366
fb31d3dba0c3 browser (chat): add marker for new messages when page is not visible
Goffi <goffi@goffi.org>
parents: 1541
diff changeset
367 document.bind("visibilitychange", libervia_web_chat.handle_visibility_change)
fb31d3dba0c3 browser (chat): add marker for new messages when page is not visible
Goffi <goffi@goffi.org>
parents: 1541
diff changeset
368
1536
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
369 bridge.register_signal("message_new", libervia_web_chat._on_message_new)
1540
b4342176fa0a browser (chat): minor reformatting
Goffi <goffi@goffi.org>
parents: 1536
diff changeset
370
1536
dc81403a5b2f browser: chat page:
Goffi <goffi@goffi.org>
parents:
diff changeset
371 libervia_web_chat.make_attachments_dynamic()
1541
3c384b51b2a1 browser (chat): URL previews implementation
Goffi <goffi@goffi.org>
parents: 1540
diff changeset
372 libervia_web_chat.handle_url_previews()