@alexsun-top/opencode-chat-room 0.8.2 → 0.8.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +10 -0
- package/README.md +18 -62
- package/README.zh-CN.md +25 -69
- package/dist/cli.js +466 -188
- package/package.json +8 -5
- package/packages/chat-room-channeld/README.md +25 -3
- package/packages/chat-room-channeld/dist/mock-adapter.js +5 -5
- package/packages/chat-room-channeld/src/adapter.ts +5 -0
- package/packages/chat-room-channeld/src/cli.ts +72 -5
- package/packages/chat-room-channeld/src/host.ts +17 -2
- package/packages/chat-room-channeld/src/pg/database.ts +187 -0
- package/packages/chat-room-channeld/src/pg/migrate.ts +150 -0
- package/packages/chat-room-channeld/src/pg/schema.ts +60 -0
- package/packages/chat-room-channeld/src/pg/sql.mjs +175 -0
- package/packages/chat-room-channeld/src/pg/sqlite.ts +100 -0
- package/packages/chat-room-channeld/src/pg/worker.mjs +133 -0
- package/packages/chat-room-channeld/src/protocol.ts +1 -0
- package/packages/chat-room-channeld/src/state.ts +73 -97
- package/packages/chat-room-channeld/src/stats.ts +140 -109
- package/server/assets/base.css +3 -1
- package/server/assets/chat/admin-badge.js +32 -30
- package/server/assets/chat/admin.js +198 -161
- package/server/assets/chat/announcements.js +295 -208
- package/server/assets/chat/attachment-store.js +106 -75
- package/server/assets/chat/attachments.js +686 -441
- package/server/assets/chat/chat-background.js +185 -156
- package/server/assets/chat/clipboard.js +44 -25
- package/server/assets/chat/confirm-dialog.js +36 -33
- package/server/assets/chat/copy-action.js +40 -40
- package/server/assets/chat/create-room-dialog.js +50 -47
- package/server/assets/chat/diagram-viewer.js +293 -234
- package/server/assets/chat/doc-preview.js +507 -441
- package/server/assets/chat/editor.js +423 -335
- package/server/assets/chat/events.js +637 -510
- package/server/assets/chat/files.js +476 -430
- package/server/assets/chat/history-loader.js +79 -73
- package/server/assets/chat/icons.js +9 -11
- package/server/assets/chat/image-zoom.js +239 -176
- package/server/assets/chat/index.js +2 -3
- package/server/assets/chat/install-prompt.js +86 -66
- package/server/assets/chat/integration-account-schema.js +88 -86
- package/server/assets/chat/integration-editor.js +162 -153
- package/server/assets/chat/integration-values.js +109 -100
- package/server/assets/chat/integrations.js +689 -622
- package/server/assets/chat/join-requests.js +408 -358
- package/server/assets/chat/locales/de.js +193 -194
- package/server/assets/chat/locales/es.js +193 -194
- package/server/assets/chat/locales/fr.js +193 -194
- package/server/assets/chat/locales/index.js +1 -2
- package/server/assets/chat/locales/it.js +193 -194
- package/server/assets/chat/locales/ja.js +193 -193
- package/server/assets/chat/locales/ko.js +193 -193
- package/server/assets/chat/markdown-footnotes.js +134 -102
- package/server/assets/chat/markdown.js +547 -403
- package/server/assets/chat/members.js +236 -197
- package/server/assets/chat/mentions.js +157 -133
- package/server/assets/chat/message-cache.js +40 -33
- package/server/assets/chat/message-polling.js +280 -227
- package/server/assets/chat/message-window.js +74 -55
- package/server/assets/chat/messages.js +552 -480
- package/server/assets/chat/office-preview.js +46 -45
- package/server/assets/chat/palette.js +450 -417
- package/server/assets/chat/panels.js +94 -78
- package/server/assets/chat/pwa.js +144 -73
- package/server/assets/chat/questions.js +372 -302
- package/server/assets/chat/recall.js +320 -273
- package/server/assets/chat/receipts.js +149 -121
- package/server/assets/chat/reminders.js +331 -214
- package/server/assets/chat/replies.js +165 -130
- package/server/assets/chat/resource-format.js +59 -62
- package/server/assets/chat/room-list.js +58 -55
- package/server/assets/chat/room-watcher.js +72 -50
- package/server/assets/chat/rooms.js +623 -500
- package/server/assets/chat/search.js +136 -114
- package/server/assets/chat/settings.js +223 -200
- package/server/assets/chat/shiki.js +61 -62
- package/server/assets/chat/shortcuts.js +212 -187
- package/server/assets/chat/side-pane.js +160 -150
- package/server/assets/chat/state.js +849 -735
- package/server/assets/chat/system-events.js +45 -43
- package/server/assets/chat/system-messages.js +57 -56
- package/server/assets/chat/task-form.js +178 -182
- package/server/assets/chat/task-row.js +158 -155
- package/server/assets/chat/tasks.js +653 -587
- package/server/assets/chat/viewport.js +32 -29
- package/server/assets/chat/window-controls.js +32 -32
- package/server/assets/conversation.css +10 -4
- package/server/chat-server.ts +9 -1
- package/server/chat.html +2 -1
- package/server/routes/pages.ts +7 -5
- package/server/sw.js +346 -268
- package/src/web/integration-rules-editor.ts +2 -0
|
@@ -1,538 +1,665 @@
|
|
|
1
1
|
import { LS } from "./state.js";
|
|
2
2
|
import { copyTextToClipboard } from "./markdown.js";
|
|
3
3
|
import { followsBottom, pointerOrSelectionActive } from "./message-window.js";
|
|
4
|
-
|
|
5
4
|
export function initEvents(app) {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
// Mobile panels sit below the header, whose height depends on the title and locale.
|
|
29
|
-
const chatHead = document.querySelector("#chatView > .chat-head");
|
|
30
|
-
if (chatHead) {
|
|
31
|
-
const syncHeadBottom = () => document.documentElement.style.setProperty("--chat-head-bottom", `${Math.round(chatHead.getBoundingClientRect().bottom + 8)}px`);
|
|
32
|
-
new ResizeObserver(syncHeadBottom).observe(chatHead);
|
|
33
|
-
syncHeadBottom();
|
|
34
|
-
}
|
|
35
|
-
// The rail footer divider sits above the identity row. Keep it on the composer's
|
|
36
|
-
// divider while the composer is idle; staged files or wrapped lines grow only the
|
|
37
|
-
// composer, so the footer keeps its idle height instead of stretching with it.
|
|
38
|
-
const composer = document.querySelector("#chatView > .composer");
|
|
39
|
-
if (composer) {
|
|
40
|
-
const root = document.documentElement, editor = app.$("#msgInput");
|
|
41
|
-
// A restored draft or staged files must not become the idle height people see on load.
|
|
42
|
-
// Newline-only drafts are real drafts too: `trim()` would hide them and let the
|
|
43
|
-
// footer stretch with the composer.
|
|
44
|
-
const idleComposer = () => editor?.dataset.empty === "true"
|
|
45
|
-
&& !["#joinNotice", "#sendError", "#replyPreview", "#mentionPicker", "#attachmentTray"].some((selector) => { const node = app.$(selector); return node && !node.hidden; })
|
|
46
|
-
&& !composer.querySelector(".failed-messages:not([hidden])");
|
|
47
|
-
const syncComposerBlock = () => {
|
|
48
|
-
const height = composer.getBoundingClientRect().height;
|
|
49
|
-
if (!height || !idleComposer()) return;
|
|
50
|
-
root.style.setProperty("--composer-block", `${Math.round(height)}px`);
|
|
5
|
+
const $ = app.$;
|
|
6
|
+
// Thread clicks land on non-focusable nodes, so the browser drops focus to BODY
|
|
7
|
+
// and the next keystroke is lost. Returning it to the composer matches chat UX.
|
|
8
|
+
app.focusChatComposer = ({ force = false } = {}) => {
|
|
9
|
+
const editor = $("#msgInput");
|
|
10
|
+
if (matchMedia("(max-width: 760px)").matches || document.querySelector("dialog[open]"))
|
|
11
|
+
return false;
|
|
12
|
+
if ($("#chatView").hidden || editor.contentEditable !== "true" || !editor.getClientRects().length)
|
|
13
|
+
return false;
|
|
14
|
+
if (roomRail.classList.contains("drawer-open"))
|
|
15
|
+
return false;
|
|
16
|
+
if (!force && (document.activeElement !== document.body || window.getSelection()?.toString()))
|
|
17
|
+
return false;
|
|
18
|
+
const selection = window.getSelection();
|
|
19
|
+
const selectionInside = Boolean(selection?.anchorNode && editor.contains(selection.anchorNode));
|
|
20
|
+
editor.focus({ preventScroll: true });
|
|
21
|
+
// A closed modal can leave the DOM selection inside its removed input even
|
|
22
|
+
// after the composer looks focused. Re-seat the caret so the next keystroke
|
|
23
|
+
// lands in the editor instead of disappearing.
|
|
24
|
+
if (force && !selectionInside)
|
|
25
|
+
editor.setCaret?.(editor.value.length);
|
|
26
|
+
return true;
|
|
51
27
|
};
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
28
|
+
app.focusComposer = () => {
|
|
29
|
+
app.focusChatComposer();
|
|
30
|
+
};
|
|
31
|
+
document.addEventListener("click", () => requestAnimationFrame(() => app.focusComposer?.()));
|
|
32
|
+
// Mobile panels sit below the header, whose height depends on the title and locale.
|
|
33
|
+
const chatHead = document.querySelector("#chatView > .chat-head");
|
|
34
|
+
if (chatHead) {
|
|
35
|
+
const syncHeadBottom = () => document.documentElement.style.setProperty("--chat-head-bottom", `${Math.round(chatHead.getBoundingClientRect().bottom + 8)}px`);
|
|
36
|
+
new ResizeObserver(syncHeadBottom).observe(chatHead);
|
|
37
|
+
syncHeadBottom();
|
|
38
|
+
}
|
|
39
|
+
// The rail footer divider sits above the identity row. Keep it on the composer's
|
|
40
|
+
// divider while the composer is idle; staged files or wrapped lines grow only the
|
|
41
|
+
// composer, so the footer keeps its idle height instead of stretching with it.
|
|
42
|
+
const composer = document.querySelector("#chatView > .composer");
|
|
43
|
+
if (composer) {
|
|
44
|
+
const root = document.documentElement, editor = app.$("#msgInput");
|
|
45
|
+
// A restored draft or staged files must not become the idle height people see on load.
|
|
46
|
+
// Newline-only drafts are real drafts too: `trim()` would hide them and let the
|
|
47
|
+
// footer stretch with the composer.
|
|
48
|
+
const idleComposer = () => editor?.dataset.empty === "true"
|
|
49
|
+
&& !["#joinNotice", "#sendError", "#replyPreview", "#mentionPicker", "#attachmentTray"].some((selector) => { const node = app.$(selector); return node && !node.hidden; })
|
|
50
|
+
&& !composer.querySelector(".failed-messages:not([hidden])");
|
|
51
|
+
const syncComposerBlock = () => {
|
|
52
|
+
const height = composer.getBoundingClientRect().height;
|
|
53
|
+
if (!height || !idleComposer())
|
|
54
|
+
return;
|
|
55
|
+
root.style.setProperty("--composer-block", `${Math.round(height)}px`);
|
|
56
|
+
};
|
|
57
|
+
new ResizeObserver(syncComposerBlock).observe(composer);
|
|
58
|
+
syncComposerBlock();
|
|
59
|
+
window.addEventListener("resize", syncComposerBlock);
|
|
60
|
+
let lastComposerHeight = composer.getBoundingClientRect().height;
|
|
61
|
+
new ResizeObserver(() => {
|
|
62
|
+
const height = composer.getBoundingClientRect().height;
|
|
63
|
+
if (height > lastComposerHeight + 1 && followsBottom(app)) {
|
|
64
|
+
const thread = $("#thread");
|
|
65
|
+
thread.scrollTop = thread.scrollHeight;
|
|
66
|
+
}
|
|
67
|
+
lastComposerHeight = height;
|
|
68
|
+
}).observe(composer);
|
|
69
|
+
}
|
|
70
|
+
// Images, diagrams and attachments grow the list after the first paint and the browser
|
|
71
|
+
// answers with a scroll that looks like the reader moving. Subtract the growth: only a
|
|
72
|
+
// reader who was already at the bottom gets pinned back.
|
|
73
|
+
const messageList = $("#msgList");
|
|
74
|
+
const rememberListMetrics = () => {
|
|
75
|
+
const thread = $("#thread");
|
|
76
|
+
app.state.listMetrics = { height: messageList.offsetHeight, top: thread.scrollTop, client: thread.clientHeight };
|
|
77
|
+
};
|
|
78
|
+
const scrollThread = $("#thread");
|
|
79
|
+
if (scrollThread) {
|
|
80
|
+
new ResizeObserver(() => {
|
|
81
|
+
if (followsBottom(app) && scrollThread.scrollHeight - scrollThread.scrollTop - scrollThread.clientHeight > 1)
|
|
82
|
+
scrollThread.scrollTop = scrollThread.scrollHeight;
|
|
83
|
+
}).observe(scrollThread);
|
|
84
|
+
}
|
|
56
85
|
new ResizeObserver(() => {
|
|
57
|
-
|
|
58
|
-
|
|
86
|
+
const thread = $("#thread"), previous = app.state.listMetrics;
|
|
87
|
+
const grew = messageList.offsetHeight > previous.height, sameBox = thread.clientHeight === previous.client;
|
|
88
|
+
// Rows arrive in bursts, so judging each burst on its own loses the reader after the
|
|
89
|
+
// first one. Follow while the reader has not scrolled away: then every growth pins.
|
|
90
|
+
// A changed viewport height (composer, window) is not list growth and keeps its anchor.
|
|
91
|
+
// A pointer or selection is mid-gesture: moving the content under it loses a drag,
|
|
92
|
+
// a click, or the reader's own position.
|
|
93
|
+
if (grew && sameBox && followsBottom(app)
|
|
94
|
+
&& thread.scrollHeight - thread.scrollTop - thread.clientHeight > 1) {
|
|
95
|
+
thread.scrollTop = thread.scrollHeight;
|
|
96
|
+
}
|
|
97
|
+
rememberListMetrics();
|
|
98
|
+
}).observe(messageList);
|
|
99
|
+
app.showRestoreFailure = (message) => {
|
|
100
|
+
document.documentElement.classList.add("session-restore");
|
|
101
|
+
$("#joinView").hidden = $("#chatView").hidden = true;
|
|
102
|
+
$("#restoreView").hidden = false;
|
|
103
|
+
$("#restoreError").textContent = message;
|
|
104
|
+
$("#restoreError").hidden = false;
|
|
105
|
+
$("#btnRestoreRetry").hidden = false;
|
|
106
|
+
};
|
|
107
|
+
const roomRail = $("#roomRail"), drawerTrigger = $("#btnRoomsDrawer"), backdrop = $("#roomDrawerBackdrop");
|
|
108
|
+
const smallScreen = () => matchMedia("(max-width: 760px)").matches;
|
|
109
|
+
let drawerWasSmall = smallScreen(), drawerAnimationTimer = 0;
|
|
110
|
+
let readingPosition = null, resizeFrame = 0, resizeGeneration = 0, scrollGeneration = 0, resizeResetTimer = 0, backdropTimer = 0;
|
|
111
|
+
function captureReadingPosition() {
|
|
112
|
+
const thread = $("#thread"), edge = thread.getBoundingClientRect(), top = edge.top + thread.clientTop;
|
|
113
|
+
const anchor = [...$("#msgList").querySelectorAll("[data-msg-id]")].find((row) => row.getBoundingClientRect().bottom > top);
|
|
114
|
+
readingPosition = { roomId: app.state.room?.id, messageId: anchor?.dataset.msgId, offset: anchor ? anchor.getBoundingClientRect().top - top : 0,
|
|
115
|
+
atBottom: thread.scrollHeight - thread.scrollTop - thread.clientHeight <= 80 };
|
|
116
|
+
const guard = app.state.readAnchorGuard;
|
|
117
|
+
let missing = Boolean(guard) && guard.roomId === readingPosition.roomId && Boolean(guard.missing);
|
|
118
|
+
// A failed restore keeps its anchor while the landing stays put; scrolling to a
|
|
119
|
+
// different message means the reader chose a new position and the old one is stale.
|
|
120
|
+
if (missing) {
|
|
121
|
+
if (!guard.landingId)
|
|
122
|
+
guard.landingId = readingPosition.messageId;
|
|
123
|
+
else if (readingPosition.messageId !== guard.landingId)
|
|
124
|
+
missing = guard.missing = false;
|
|
125
|
+
}
|
|
126
|
+
if (readingPosition.roomId && anchor && (!missing || performance.now() <= app.state.userScrollIntentUntil)) {
|
|
127
|
+
app.savePosition(readingPosition.roomId, { messageId: readingPosition.messageId, offset: readingPosition.offset, atBottom: readingPosition.atBottom });
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
app.captureReadingPosition = captureReadingPosition;
|
|
131
|
+
let layoutQueue = Promise.resolve();
|
|
132
|
+
app.preserveReadingLayout = (change) => {
|
|
133
|
+
const task = layoutQueue.then(async () => {
|
|
134
|
+
captureReadingPosition();
|
|
135
|
+
const saved = readingPosition, roomId = app.state.room?.id, generation = scrollGeneration;
|
|
136
|
+
app.state.layoutScrollAdjusting = true;
|
|
137
|
+
try {
|
|
138
|
+
await change();
|
|
139
|
+
await new Promise((resolve) => requestAnimationFrame(resolve));
|
|
140
|
+
if (roomId && app.state.room?.id === roomId && generation === scrollGeneration) {
|
|
141
|
+
const thread = $("#thread");
|
|
142
|
+
if (saved.atBottom && followsBottom(app))
|
|
143
|
+
thread.scrollTop = thread.scrollHeight;
|
|
144
|
+
else if (saved.messageId) {
|
|
145
|
+
const row = [...$("#msgList").querySelectorAll("[data-msg-id]")].find((item) => item.dataset.msgId === saved.messageId);
|
|
146
|
+
if (row)
|
|
147
|
+
thread.scrollTop += row.getBoundingClientRect().top - (thread.getBoundingClientRect().top + thread.clientTop + saved.offset);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
captureReadingPosition();
|
|
151
|
+
}
|
|
152
|
+
finally {
|
|
153
|
+
requestAnimationFrame(() => { app.state.layoutScrollAdjusting = false; });
|
|
154
|
+
}
|
|
155
|
+
});
|
|
156
|
+
layoutQueue = task.catch(() => { });
|
|
157
|
+
return task;
|
|
158
|
+
};
|
|
159
|
+
app.restoreReadingPosition = () => {
|
|
59
160
|
const thread = $("#thread");
|
|
161
|
+
// Entering a room always shows the newest messages. Restoring an old anchor
|
|
162
|
+
// leaves the reader above messages that already arrived or grew afterwards.
|
|
60
163
|
thread.scrollTop = thread.scrollHeight;
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
const scrollThread = $("#thread");
|
|
74
|
-
if (scrollThread) {
|
|
75
|
-
new ResizeObserver(() => {
|
|
76
|
-
if (followsBottom(app) && scrollThread.scrollHeight - scrollThread.scrollTop - scrollThread.clientHeight > 1) scrollThread.scrollTop = scrollThread.scrollHeight;
|
|
77
|
-
}).observe(scrollThread);
|
|
78
|
-
}
|
|
79
|
-
new ResizeObserver(() => {
|
|
80
|
-
const thread = $("#thread"), previous = app.state.listMetrics;
|
|
81
|
-
const grew = messageList.offsetHeight > previous.height, sameBox = thread.clientHeight === previous.client;
|
|
82
|
-
// Rows arrive in bursts, so judging each burst on its own loses the reader after the
|
|
83
|
-
// first one. Follow while the reader has not scrolled away: then every growth pins.
|
|
84
|
-
// A changed viewport height (composer, window) is not list growth and keeps its anchor.
|
|
85
|
-
// A pointer or selection is mid-gesture: moving the content under it loses a drag,
|
|
86
|
-
// a click, or the reader's own position.
|
|
87
|
-
if (grew && sameBox && followsBottom(app)
|
|
88
|
-
&& thread.scrollHeight - thread.scrollTop - thread.clientHeight > 1) {
|
|
89
|
-
thread.scrollTop = thread.scrollHeight;
|
|
164
|
+
app.state.followBottom = true;
|
|
165
|
+
captureReadingPosition();
|
|
166
|
+
return { messageId: readingPosition?.messageId, atBottom: true };
|
|
167
|
+
};
|
|
168
|
+
function closeRoomDrawer(restore = true) {
|
|
169
|
+
roomRail.classList.remove("drawer-open");
|
|
170
|
+
roomRail.inert = true;
|
|
171
|
+
roomRail.setAttribute("aria-hidden", "true");
|
|
172
|
+
hideBackdrop();
|
|
173
|
+
drawerTrigger.setAttribute("aria-expanded", "false");
|
|
174
|
+
if (restore && smallScreen())
|
|
175
|
+
drawerTrigger.focus({ preventScroll: true });
|
|
90
176
|
}
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
$("#joinView").hidden = $("#chatView").hidden = true;
|
|
96
|
-
$("#restoreView").hidden = false;
|
|
97
|
-
$("#restoreError").textContent = message;
|
|
98
|
-
$("#restoreError").hidden = false;
|
|
99
|
-
$("#btnRestoreRetry").hidden = false;
|
|
100
|
-
};
|
|
101
|
-
const roomRail = $("#roomRail"), drawerTrigger = $("#btnRoomsDrawer"), backdrop = $("#roomDrawerBackdrop");
|
|
102
|
-
const smallScreen = () => matchMedia("(max-width: 760px)").matches;
|
|
103
|
-
let drawerWasSmall = smallScreen(), drawerAnimationTimer = 0;
|
|
104
|
-
let readingPosition = null, resizeFrame = 0, resizeGeneration = 0, scrollGeneration = 0, resizeResetTimer = 0, backdropTimer = 0;
|
|
105
|
-
function captureReadingPosition() {
|
|
106
|
-
const thread = $("#thread"), edge = thread.getBoundingClientRect(), top = edge.top + thread.clientTop;
|
|
107
|
-
const anchor = [...$("#msgList").querySelectorAll("[data-msg-id]")].find((row) => row.getBoundingClientRect().bottom > top);
|
|
108
|
-
readingPosition = { roomId: app.state.room?.id, messageId: anchor?.dataset.msgId, offset: anchor ? anchor.getBoundingClientRect().top - top : 0,
|
|
109
|
-
atBottom: thread.scrollHeight - thread.scrollTop - thread.clientHeight <= 80 };
|
|
110
|
-
const guard = app.state.readAnchorGuard;
|
|
111
|
-
let missing = Boolean(guard) && guard.roomId === readingPosition.roomId && Boolean(guard.missing);
|
|
112
|
-
// A failed restore keeps its anchor while the landing stays put; scrolling to a
|
|
113
|
-
// different message means the reader chose a new position and the old one is stale.
|
|
114
|
-
if (missing) {
|
|
115
|
-
if (!guard.landingId) guard.landingId = readingPosition.messageId;
|
|
116
|
-
else if (readingPosition.messageId !== guard.landingId) missing = guard.missing = false;
|
|
177
|
+
function showBackdrop() {
|
|
178
|
+
clearTimeout(backdropTimer);
|
|
179
|
+
backdrop.hidden = false;
|
|
180
|
+
requestAnimationFrame(() => backdrop.classList.add("is-open"));
|
|
117
181
|
}
|
|
118
|
-
|
|
119
|
-
|
|
182
|
+
function hideBackdrop(immediate = false) {
|
|
183
|
+
clearTimeout(backdropTimer);
|
|
184
|
+
backdrop.classList.remove("is-open");
|
|
185
|
+
if (immediate) {
|
|
186
|
+
backdrop.hidden = true;
|
|
187
|
+
return;
|
|
188
|
+
}
|
|
189
|
+
backdropTimer = setTimeout(() => { backdrop.hidden = true; }, 200);
|
|
120
190
|
}
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
191
|
+
function syncRoomDrawer() {
|
|
192
|
+
// Crossing the drawer breakpoint jumps straight to the target layout instead
|
|
193
|
+
// of animating a rail that is no longer a drawer.
|
|
194
|
+
const small = smallScreen();
|
|
195
|
+
if (small !== drawerWasSmall) {
|
|
196
|
+
drawerWasSmall = small;
|
|
197
|
+
roomRail.classList.add("no-drawer-animation");
|
|
198
|
+
clearTimeout(drawerAnimationTimer);
|
|
199
|
+
drawerAnimationTimer = setTimeout(() => roomRail.classList.remove("no-drawer-animation"), 260);
|
|
200
|
+
}
|
|
201
|
+
if (!small) {
|
|
202
|
+
roomRail.classList.remove("drawer-open");
|
|
203
|
+
roomRail.inert = false;
|
|
204
|
+
roomRail.removeAttribute("aria-hidden");
|
|
205
|
+
hideBackdrop(true);
|
|
206
|
+
drawerTrigger.setAttribute("aria-expanded", "false");
|
|
207
|
+
}
|
|
208
|
+
else if (!roomRail.classList.contains("drawer-open")) {
|
|
209
|
+
if (roomRail.contains(document.activeElement))
|
|
210
|
+
drawerTrigger.focus({ preventScroll: true });
|
|
211
|
+
roomRail.inert = true;
|
|
212
|
+
roomRail.setAttribute("aria-hidden", "true");
|
|
139
213
|
}
|
|
140
|
-
captureReadingPosition();
|
|
141
|
-
} finally { requestAnimationFrame(() => { app.state.layoutScrollAdjusting = false; }); }
|
|
142
|
-
});
|
|
143
|
-
layoutQueue = task.catch(() => {});
|
|
144
|
-
return task;
|
|
145
|
-
};
|
|
146
|
-
app.restoreReadingPosition = () => {
|
|
147
|
-
const thread = $("#thread");
|
|
148
|
-
// Entering a room always shows the newest messages. Restoring an old anchor
|
|
149
|
-
// leaves the reader above messages that already arrived or grew afterwards.
|
|
150
|
-
thread.scrollTop = thread.scrollHeight;
|
|
151
|
-
app.state.followBottom = true;
|
|
152
|
-
captureReadingPosition();
|
|
153
|
-
return { messageId: readingPosition?.messageId, atBottom: true };
|
|
154
|
-
};
|
|
155
|
-
function closeRoomDrawer(restore = true) {
|
|
156
|
-
roomRail.classList.remove("drawer-open"); roomRail.inert = true; roomRail.setAttribute("aria-hidden", "true");
|
|
157
|
-
hideBackdrop(); drawerTrigger.setAttribute("aria-expanded", "false");
|
|
158
|
-
if (restore && smallScreen()) drawerTrigger.focus({ preventScroll: true });
|
|
159
|
-
}
|
|
160
|
-
function showBackdrop() {
|
|
161
|
-
clearTimeout(backdropTimer);
|
|
162
|
-
backdrop.hidden = false;
|
|
163
|
-
requestAnimationFrame(() => backdrop.classList.add("is-open"));
|
|
164
|
-
}
|
|
165
|
-
function hideBackdrop(immediate = false) {
|
|
166
|
-
clearTimeout(backdropTimer);
|
|
167
|
-
backdrop.classList.remove("is-open");
|
|
168
|
-
if (immediate) { backdrop.hidden = true; return; }
|
|
169
|
-
backdropTimer = setTimeout(() => { backdrop.hidden = true; }, 200);
|
|
170
|
-
}
|
|
171
|
-
function syncRoomDrawer() {
|
|
172
|
-
// Crossing the drawer breakpoint jumps straight to the target layout instead
|
|
173
|
-
// of animating a rail that is no longer a drawer.
|
|
174
|
-
const small = smallScreen();
|
|
175
|
-
if (small !== drawerWasSmall) {
|
|
176
|
-
drawerWasSmall = small;
|
|
177
|
-
roomRail.classList.add("no-drawer-animation");
|
|
178
|
-
clearTimeout(drawerAnimationTimer);
|
|
179
|
-
drawerAnimationTimer = setTimeout(() => roomRail.classList.remove("no-drawer-animation"), 260);
|
|
180
214
|
}
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
215
|
+
function preserveReadingPosition() {
|
|
216
|
+
syncRoomDrawer();
|
|
217
|
+
if (!app.state.resizeScrollAdjusting)
|
|
218
|
+
app.state.userScrollIntentUntil = 0;
|
|
219
|
+
app.state.resizeScrollAdjusting = true;
|
|
220
|
+
const generation = ++resizeGeneration;
|
|
221
|
+
if (app.state.receiptObservationFrame)
|
|
222
|
+
cancelAnimationFrame(app.state.receiptObservationFrame);
|
|
223
|
+
app.state.receiptObservationFrame = 0;
|
|
224
|
+
app.state.receiptObservation = null;
|
|
225
|
+
clearTimeout(resizeResetTimer);
|
|
226
|
+
resizeResetTimer = setTimeout(() => { if (generation === resizeGeneration)
|
|
227
|
+
app.state.resizeScrollAdjusting = false; }, 500);
|
|
228
|
+
if (resizeFrame)
|
|
229
|
+
cancelAnimationFrame(resizeFrame);
|
|
230
|
+
resizeFrame = requestAnimationFrame(() => {
|
|
231
|
+
resizeFrame = 0;
|
|
232
|
+
const position = readingPosition, thread = $("#thread");
|
|
233
|
+
if (!position?.roomId || position.roomId !== app.state.room?.id)
|
|
234
|
+
return captureReadingPosition();
|
|
235
|
+
if (position.atBottom && !pointerOrSelectionActive(app))
|
|
236
|
+
thread.scrollTop = thread.scrollHeight;
|
|
237
|
+
else if (position.messageId) {
|
|
238
|
+
const row = [...$("#msgList").querySelectorAll("[data-msg-id]")].find((item) => item.dataset.msgId === position.messageId);
|
|
239
|
+
if (row)
|
|
240
|
+
thread.scrollTop += row.getBoundingClientRect().top - (thread.getBoundingClientRect().top + thread.clientTop + position.offset);
|
|
241
|
+
}
|
|
242
|
+
captureReadingPosition();
|
|
243
|
+
});
|
|
185
244
|
}
|
|
186
|
-
}
|
|
187
|
-
function preserveReadingPosition() {
|
|
188
245
|
syncRoomDrawer();
|
|
189
|
-
if (
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
246
|
+
window.addEventListener("pageshow", () => { if (app.state.followBottom)
|
|
247
|
+
$("#thread").scrollTop = $("#thread").scrollHeight; });
|
|
248
|
+
window.addEventListener("resize", preserveReadingPosition);
|
|
249
|
+
window.addEventListener("pagehide", () => { captureReadingPosition(); app.flushReadOnHide?.(); });
|
|
250
|
+
document.addEventListener("visibilitychange", () => { if (document.hidden) {
|
|
251
|
+
captureReadingPosition();
|
|
252
|
+
app.flushReadOnHide?.();
|
|
253
|
+
} });
|
|
254
|
+
drawerTrigger.addEventListener("click", () => {
|
|
255
|
+
if (roomRail.classList.contains("drawer-open"))
|
|
256
|
+
return closeRoomDrawer();
|
|
257
|
+
app.closePanels();
|
|
258
|
+
roomRail.inert = false;
|
|
259
|
+
roomRail.removeAttribute("aria-hidden");
|
|
260
|
+
roomRail.classList.add("drawer-open");
|
|
261
|
+
showBackdrop();
|
|
262
|
+
drawerTrigger.setAttribute("aria-expanded", "true");
|
|
263
|
+
(roomRail.querySelector('.room-row[aria-current="page"]') || $("#btnRefreshRooms")).focus();
|
|
207
264
|
});
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
if (roomRail.classList.contains("drawer-open")) closeRoomDrawer();
|
|
227
|
-
};
|
|
228
|
-
document.querySelectorAll(".locale-select").forEach((select) => {
|
|
229
|
-
select.addEventListener("change", () => {
|
|
230
|
-
app.setLocale(select.value);
|
|
231
|
-
if (!app.state.availableRooms) return;
|
|
232
|
-
app.renderRoomOptions($("#roomList"), app.state.availableRooms || []);
|
|
233
|
-
app.renderRoomOptions($("#roomListChat"), app.state.availableRooms || []);
|
|
265
|
+
backdrop.addEventListener("click", () => closeRoomDrawer());
|
|
266
|
+
$("#roomListChat").addEventListener("click", (event) => { if (roomRail.classList.contains("drawer-open") && event.target.closest("[data-room-id]"))
|
|
267
|
+
closeRoomDrawer(); });
|
|
268
|
+
const activateRoom = app.activateRoom;
|
|
269
|
+
app.activateRoom = (id, preserveRoomListGeneration = false) => {
|
|
270
|
+
activateRoom(id, preserveRoomListGeneration);
|
|
271
|
+
captureReadingPosition();
|
|
272
|
+
if (roomRail.classList.contains("drawer-open"))
|
|
273
|
+
closeRoomDrawer();
|
|
274
|
+
};
|
|
275
|
+
document.querySelectorAll(".locale-select").forEach((select) => {
|
|
276
|
+
select.addEventListener("change", () => {
|
|
277
|
+
app.setLocale(select.value);
|
|
278
|
+
if (!app.state.availableRooms)
|
|
279
|
+
return;
|
|
280
|
+
app.renderRoomOptions($("#roomList"), app.state.availableRooms || []);
|
|
281
|
+
app.renderRoomOptions($("#roomListChat"), app.state.availableRooms || []);
|
|
282
|
+
});
|
|
234
283
|
});
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
$("#
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
input.addEventListener("compositionend", () => {
|
|
289
|
-
// An ESC-cancelled composition next to a chip can leave the caret inside the
|
|
290
|
-
// non-editable chip, which blocks typing; re-seat it in a real text node.
|
|
291
|
-
const anchor = getSelection()?.anchorNode;
|
|
292
|
-
const element = anchor?.nodeType === Node.ELEMENT_NODE ? anchor : anchor?.parentElement;
|
|
293
|
-
if (element?.closest(".editor-mention")) input.setCaret(input.selectionStart);
|
|
294
|
-
app.recomputeMention(input);
|
|
295
|
-
});
|
|
296
|
-
input.addEventListener("keydown", (event) => {
|
|
297
|
-
if ((event.key === "Home" || event.key === "End")
|
|
298
|
-
&& !event.altKey && !event.ctrlKey && !event.metaKey && !event.shiftKey && !event.isComposing) {
|
|
299
|
-
event.preventDefault();
|
|
300
|
-
input.moveCaretToLineEdge?.(event.key === "End");
|
|
301
|
-
return;
|
|
302
|
-
}
|
|
303
|
-
if ((event.key === "PageUp" || event.key === "PageDown")
|
|
304
|
-
&& !event.ctrlKey && !event.metaKey && !event.shiftKey && !event.isComposing) {
|
|
305
|
-
event.preventDefault();
|
|
306
|
-
markScrollIntent();
|
|
307
|
-
const step = Number(app.LS.pageStep) || 90;
|
|
308
|
-
const delta = (event.key === "PageDown" ? 1 : -1) * Math.max(40, thread.clientHeight * step / 100);
|
|
309
|
-
// Repeated Page keys extend the same target, so holding them scrolls
|
|
310
|
-
// steadily instead of restarting the animation from zero each time.
|
|
311
|
-
if (app.animateThreadScrollBy) app.animateThreadScrollBy(delta);
|
|
312
|
-
else thread.scrollTop += delta;
|
|
313
|
-
return;
|
|
314
|
-
}
|
|
315
|
-
handleMessageKey(event, app);
|
|
316
|
-
});
|
|
317
|
-
input.addEventListener("keyup", (event) => {
|
|
318
|
-
if (app.mentionActive && /^(ArrowLeft|ArrowRight|Home|End)$/.test(event.key)) app.recomputeMention();
|
|
319
|
-
});
|
|
320
|
-
input.addEventListener("click", app.recomputeMention);
|
|
321
|
-
input.addEventListener("blur", () => app.closeMention());
|
|
322
|
-
$("#mentionPicker").addEventListener("mousedown", (event) => {
|
|
323
|
-
const option = event.target.closest(".mention-opt");
|
|
324
|
-
if (!option) return;
|
|
325
|
-
event.preventDefault();
|
|
326
|
-
app.insertMention(option.dataset.name);
|
|
327
|
-
});
|
|
328
|
-
// Stop wheel overscroll at the ends. Without this, some layouts let the
|
|
329
|
-
// document absorb the leftover delta and the view jumps a few pixels.
|
|
330
|
-
thread.addEventListener("wheel", (event) => {
|
|
331
|
-
// Trackpad pinch arrives as Ctrl/Cmd + wheel. Blocking it at a scroll
|
|
332
|
-
// edge would also block browser zoom-out.
|
|
333
|
-
if (event.ctrlKey || event.metaKey) return;
|
|
334
|
-
// While pinch-zoomed the default wheel action pans the visual viewport.
|
|
335
|
-
// The document itself never overflows, so preventing it would freeze the page.
|
|
336
|
-
if (Math.abs((window.visualViewport?.scale ?? 1) - 1) > 0.01) return;
|
|
337
|
-
markScrollIntent();
|
|
338
|
-
const gap = thread.scrollHeight - thread.clientHeight - thread.scrollTop;
|
|
339
|
-
if (event.deltaY < 0 && thread.scrollTop <= 0 || event.deltaY > 0 && gap <= 2) event.preventDefault();
|
|
340
|
-
}, { passive: false });
|
|
341
|
-
thread.addEventListener("touchmove", markScrollIntent, { passive: true });
|
|
342
|
-
thread.addEventListener("keydown", (event) => {
|
|
343
|
-
if (/^(PageDown|PageUp|End|Home|ArrowDown|ArrowUp| )$/.test(event.key)) markScrollIntent();
|
|
344
|
-
});
|
|
345
|
-
thread.addEventListener("pointerdown", (event) => {
|
|
346
|
-
if (event.clientX >= thread.getBoundingClientRect().right - 18) markScrollIntent();
|
|
347
|
-
app.state.threadPointerDown = true;
|
|
348
|
-
});
|
|
349
|
-
const releasePointer = () => { app.state.threadPointerDown = false; };
|
|
350
|
-
window.addEventListener("pointerup", releasePointer);
|
|
351
|
-
window.addEventListener("pointercancel", releasePointer);
|
|
352
|
-
window.addEventListener("blur", releasePointer);
|
|
353
|
-
thread.addEventListener("scroll", () => {
|
|
354
|
-
const adjusting = (app.state.resizeScrollAdjusting || app.state.layoutScrollAdjusting)
|
|
355
|
-
&& performance.now() > app.state.userScrollIntentUntil;
|
|
356
|
-
if (!adjusting) {
|
|
357
|
-
// The growth baseline belongs to the observer alone. A scroll caused by the growth
|
|
358
|
-
// itself arrives before the observer sees the new height, and refreshing the
|
|
359
|
-
// baseline here would make that growth look like no growth at all.
|
|
360
|
-
// Only a scroll the reader started ends following; layout shifts do not.
|
|
361
|
-
const gap = thread.scrollHeight - thread.scrollTop - thread.clientHeight;
|
|
362
|
-
if (gap <= 80) {
|
|
363
|
-
app.state.followBottom = true;
|
|
364
|
-
if (app.state.focusMessageId && performance.now() > (app.state.focusHoldUntil || 0)) {
|
|
365
|
-
app.state.focusMessageId = null;
|
|
366
|
-
app.rerenderMessages?.();
|
|
284
|
+
$("#joinForm").addEventListener("submit", (event) => {
|
|
285
|
+
event.preventDefault();
|
|
286
|
+
const id = $("#inRoomId").value.trim(), name = $("#inName").value.trim();
|
|
287
|
+
if (!id)
|
|
288
|
+
return app.showJoinError("roomIdRequired");
|
|
289
|
+
if (!name)
|
|
290
|
+
return app.showJoinError("nameRequired");
|
|
291
|
+
void app.joinRoom(id, name);
|
|
292
|
+
});
|
|
293
|
+
$("#createForm").addEventListener("submit", (event) => { event.preventDefault(); void app.createRoom(); });
|
|
294
|
+
$("#btnSend").addEventListener("click", () => void app.send());
|
|
295
|
+
$("#btnJump").addEventListener("click", app.jumpToBottom);
|
|
296
|
+
$("#btnCopyId").addEventListener("click", () => void app.copyRoomId());
|
|
297
|
+
$("#btnLeave").addEventListener("click", () => { app.closePanels(); void app.leaveRoom(false); });
|
|
298
|
+
$("#btnDeleteRoom").addEventListener("click", () => void app.deleteRoom());
|
|
299
|
+
$("#btnMembers").addEventListener("click", () => app.togglePanel("#memberPanel"));
|
|
300
|
+
$("#btnRoomOptions").addEventListener("click", () => {
|
|
301
|
+
const panel = $("#roomOptions"), open = panel.hidden;
|
|
302
|
+
app.closePanels();
|
|
303
|
+
panel.hidden = !open;
|
|
304
|
+
$("#btnRoomOptions").setAttribute("aria-expanded", String(open));
|
|
305
|
+
});
|
|
306
|
+
$("#btnRestoreRetry").addEventListener("click", () => {
|
|
307
|
+
$("#btnRestoreRetry").hidden = true;
|
|
308
|
+
$("#restoreError").hidden = true;
|
|
309
|
+
void app.refreshRoomList();
|
|
310
|
+
});
|
|
311
|
+
$("#btnCancelToken").addEventListener("click", () => $("#tokenBox").close());
|
|
312
|
+
$("#btnRefreshRooms").addEventListener("click", () => void app.refreshRoomList());
|
|
313
|
+
$("#btnNewRoom").addEventListener("click", () => app.openCreateRoomDialog());
|
|
314
|
+
$("#railRoomForm").addEventListener("submit", (event) => { event.preventDefault(); void app.createRoom(); });
|
|
315
|
+
$("#inRoomNameChat").addEventListener("input", app.hideSendError);
|
|
316
|
+
const input = $("#msgInput"), thread = $("#thread");
|
|
317
|
+
const markScrollIntent = () => { scrollGeneration++; app.state.userScrollIntentUntil = performance.now() + 700; };
|
|
318
|
+
window.addEventListener("resize", () => app.autoGrow(input));
|
|
319
|
+
$(".composer").addEventListener("click", (event) => {
|
|
320
|
+
const button = event.target.closest?.("[data-failed-action]");
|
|
321
|
+
if (!button)
|
|
322
|
+
return;
|
|
323
|
+
const { failedAction, failedRoom, failedId } = button.dataset;
|
|
324
|
+
if (failedAction === "retry")
|
|
325
|
+
void app.retryFailedMessage(failedRoom, failedId);
|
|
326
|
+
else
|
|
327
|
+
app.discardFailedMessage(failedRoom, failedId);
|
|
328
|
+
});
|
|
329
|
+
input.addEventListener("input", (event) => {
|
|
330
|
+
app.autoGrow(input);
|
|
331
|
+
app.updateSendState();
|
|
332
|
+
app.updateDraftCount();
|
|
333
|
+
if (app.state.followBottom) {
|
|
334
|
+
const thread = $("#thread");
|
|
335
|
+
if (thread)
|
|
336
|
+
thread.scrollTop = thread.scrollHeight;
|
|
367
337
|
}
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
});
|
|
419
|
-
["#inToken", "#inTokenChat"].forEach((selector) => {
|
|
420
|
-
$(selector).addEventListener("keydown", (event) => {
|
|
421
|
-
if (event.key === "Enter") {
|
|
338
|
+
if (app.state.room) {
|
|
339
|
+
const id = app.state.room.id;
|
|
340
|
+
app.state.draftRevision.set(id, (app.state.draftRevision.get(id) || 0) + 1);
|
|
341
|
+
app.saveDraft(id, input.draftValue);
|
|
342
|
+
}
|
|
343
|
+
if (!event.isComposing)
|
|
344
|
+
app.recomputeMention();
|
|
345
|
+
});
|
|
346
|
+
input.addEventListener("compositionend", () => {
|
|
347
|
+
// An ESC-cancelled composition next to a chip can leave the caret inside the
|
|
348
|
+
// non-editable chip, which blocks typing; re-seat it in a real text node.
|
|
349
|
+
const anchor = getSelection()?.anchorNode;
|
|
350
|
+
const element = anchor?.nodeType === Node.ELEMENT_NODE ? anchor : anchor?.parentElement;
|
|
351
|
+
if (element?.closest(".editor-mention"))
|
|
352
|
+
input.setCaret(input.selectionStart);
|
|
353
|
+
app.recomputeMention(input);
|
|
354
|
+
});
|
|
355
|
+
input.addEventListener("keydown", (event) => {
|
|
356
|
+
if ((event.key === "Home" || event.key === "End")
|
|
357
|
+
&& !event.altKey && !event.ctrlKey && !event.metaKey && !event.shiftKey && !event.isComposing) {
|
|
358
|
+
event.preventDefault();
|
|
359
|
+
input.moveCaretToLineEdge?.(event.key === "End");
|
|
360
|
+
return;
|
|
361
|
+
}
|
|
362
|
+
if ((event.key === "PageUp" || event.key === "PageDown")
|
|
363
|
+
&& !event.ctrlKey && !event.metaKey && !event.shiftKey && !event.isComposing) {
|
|
364
|
+
event.preventDefault();
|
|
365
|
+
markScrollIntent();
|
|
366
|
+
const step = Number(app.LS.pageStep) || 90;
|
|
367
|
+
const delta = (event.key === "PageDown" ? 1 : -1) * Math.max(40, thread.clientHeight * step / 100);
|
|
368
|
+
// Repeated Page keys extend the same target, so holding them scrolls
|
|
369
|
+
// steadily instead of restarting the animation from zero each time.
|
|
370
|
+
if (app.animateThreadScrollBy)
|
|
371
|
+
app.animateThreadScrollBy(delta);
|
|
372
|
+
else
|
|
373
|
+
thread.scrollTop += delta;
|
|
374
|
+
return;
|
|
375
|
+
}
|
|
376
|
+
handleMessageKey(event, app);
|
|
377
|
+
});
|
|
378
|
+
input.addEventListener("keyup", (event) => {
|
|
379
|
+
if (app.mentionActive && /^(ArrowLeft|ArrowRight|Home|End)$/.test(event.key))
|
|
380
|
+
app.recomputeMention();
|
|
381
|
+
});
|
|
382
|
+
input.addEventListener("click", app.recomputeMention);
|
|
383
|
+
input.addEventListener("blur", () => app.closeMention());
|
|
384
|
+
$("#mentionPicker").addEventListener("mousedown", (event) => {
|
|
385
|
+
const option = event.target.closest(".mention-opt");
|
|
386
|
+
if (!option)
|
|
387
|
+
return;
|
|
422
388
|
event.preventDefault();
|
|
423
|
-
|
|
424
|
-
}
|
|
389
|
+
app.insertMention(option.dataset.name);
|
|
425
390
|
});
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
391
|
+
// Stop wheel overscroll at the ends. Without this, some layouts let the
|
|
392
|
+
// document absorb the leftover delta and the view jumps a few pixels.
|
|
393
|
+
thread.addEventListener("wheel", (event) => {
|
|
394
|
+
// Trackpad pinch arrives as Ctrl/Cmd + wheel. Blocking it at a scroll
|
|
395
|
+
// edge would also block browser zoom-out.
|
|
396
|
+
if (event.ctrlKey || event.metaKey)
|
|
397
|
+
return;
|
|
398
|
+
// While pinch-zoomed the default wheel action pans the visual viewport.
|
|
399
|
+
// The document itself never overflows, so preventing it would freeze the page.
|
|
400
|
+
if (Math.abs((window.visualViewport?.scale ?? 1) - 1) > 0.01)
|
|
401
|
+
return;
|
|
402
|
+
markScrollIntent();
|
|
403
|
+
const gap = thread.scrollHeight - thread.clientHeight - thread.scrollTop;
|
|
404
|
+
if (event.deltaY < 0 && thread.scrollTop <= 0 || event.deltaY > 0 && gap <= 2)
|
|
405
|
+
event.preventDefault();
|
|
406
|
+
}, { passive: false });
|
|
407
|
+
thread.addEventListener("touchmove", markScrollIntent, { passive: true });
|
|
408
|
+
thread.addEventListener("keydown", (event) => {
|
|
409
|
+
if (/^(PageDown|PageUp|End|Home|ArrowDown|ArrowUp| )$/.test(event.key))
|
|
410
|
+
markScrollIntent();
|
|
429
411
|
});
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
412
|
+
thread.addEventListener("pointerdown", (event) => {
|
|
413
|
+
if (event.clientX >= thread.getBoundingClientRect().right - 18)
|
|
414
|
+
markScrollIntent();
|
|
415
|
+
app.state.threadPointerDown = true;
|
|
416
|
+
});
|
|
417
|
+
const releasePointer = () => { app.state.threadPointerDown = false; };
|
|
418
|
+
window.addEventListener("pointerup", releasePointer);
|
|
419
|
+
window.addEventListener("pointercancel", releasePointer);
|
|
420
|
+
window.addEventListener("blur", releasePointer);
|
|
421
|
+
thread.addEventListener("scroll", () => {
|
|
422
|
+
const adjusting = (app.state.resizeScrollAdjusting || app.state.layoutScrollAdjusting)
|
|
423
|
+
&& performance.now() > app.state.userScrollIntentUntil;
|
|
424
|
+
if (!adjusting) {
|
|
425
|
+
// The growth baseline belongs to the observer alone. A scroll caused by the growth
|
|
426
|
+
// itself arrives before the observer sees the new height, and refreshing the
|
|
427
|
+
// baseline here would make that growth look like no growth at all.
|
|
428
|
+
// Only a scroll the reader started ends following; layout shifts do not.
|
|
429
|
+
const gap = thread.scrollHeight - thread.scrollTop - thread.clientHeight;
|
|
430
|
+
if (gap <= 80) {
|
|
431
|
+
app.state.followBottom = true;
|
|
432
|
+
if (app.state.focusMessageId && performance.now() > (app.state.focusHoldUntil || 0)) {
|
|
433
|
+
app.state.focusMessageId = null;
|
|
434
|
+
app.rerenderMessages?.();
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
else if (performance.now() <= app.state.userScrollIntentUntil)
|
|
438
|
+
app.state.followBottom = false;
|
|
439
|
+
}
|
|
440
|
+
if (!adjusting || performance.now() <= app.state.userScrollIntentUntil)
|
|
441
|
+
captureReadingPosition();
|
|
442
|
+
if (!adjusting && thread.scrollHeight - thread.scrollTop - thread.clientHeight < 80)
|
|
443
|
+
void app.markActiveRoomRead(performance.now() <= app.state.userScrollIntentUntil);
|
|
444
|
+
app.queueReceiptRefresh();
|
|
445
|
+
if (!adjusting)
|
|
446
|
+
app.queueReceiptObservation();
|
|
447
|
+
app.updateJump();
|
|
448
|
+
if (!adjusting && !app.state.jumpToLoadedTop && thread.scrollTop < 160)
|
|
449
|
+
void app.loadOlderHistory?.();
|
|
450
|
+
});
|
|
451
|
+
document.addEventListener("visibilitychange", () => {
|
|
452
|
+
if (!document.hidden) {
|
|
453
|
+
if (!app.state.resizeScrollAdjusting && !app.state.layoutScrollAdjusting)
|
|
454
|
+
void app.markActiveRoomRead();
|
|
455
|
+
void app.loadManageableMembers();
|
|
456
|
+
}
|
|
457
|
+
});
|
|
458
|
+
window.addEventListener("focus", () => {
|
|
459
|
+
if (!app.state.resizeScrollAdjusting && !app.state.layoutScrollAdjusting) {
|
|
460
|
+
void app.markActiveRoomRead();
|
|
461
|
+
app.queueReceiptObservation();
|
|
462
|
+
}
|
|
463
|
+
app.queueReceiptRefresh();
|
|
464
|
+
});
|
|
465
|
+
$("#msgList").addEventListener("click", (event) => {
|
|
466
|
+
const messageButton = event.target.closest("[data-copy-message]");
|
|
467
|
+
if (messageButton)
|
|
468
|
+
void copyTextToClipboard(messageButton._copyText, true).then((copied) => app.flash(messageButton, copied ? "copiedMessage" : "copyMessageFailed"));
|
|
469
|
+
});
|
|
470
|
+
document.addEventListener("copy", (event) => {
|
|
471
|
+
// A plain browser copy of rendered mentions loses their tokens. Rebuild them
|
|
472
|
+
// for any mention chip in the selection, including a double-clicked chip.
|
|
473
|
+
if (event.defaultPrevented)
|
|
474
|
+
return;
|
|
475
|
+
const selection = getSelection();
|
|
476
|
+
if (!selection || selection.isCollapsed || !selection.rangeCount)
|
|
477
|
+
return;
|
|
478
|
+
const range = selection.getRangeAt(0);
|
|
479
|
+
const chips = [...document.querySelectorAll(".mention-chip")].filter((chip) => range.intersectsNode(chip));
|
|
480
|
+
if (!chips.length)
|
|
481
|
+
return;
|
|
482
|
+
// Extend a partial chip selection to its whole chip so the token can be rebuilt.
|
|
483
|
+
const copyRange = range.cloneRange();
|
|
484
|
+
const startChip = chips.find((chip) => chip.contains(copyRange.startContainer));
|
|
485
|
+
const endChip = chips.find((chip) => chip.contains(copyRange.endContainer));
|
|
486
|
+
if (startChip)
|
|
487
|
+
copyRange.setStartBefore(startChip);
|
|
488
|
+
if (endChip)
|
|
489
|
+
copyRange.setEndAfter(endChip);
|
|
490
|
+
const fragment = copyRange.cloneContents();
|
|
491
|
+
const selectedChips = fragment.querySelectorAll(".mention-chip");
|
|
492
|
+
if (!selectedChips.length)
|
|
493
|
+
return;
|
|
494
|
+
selectedChips.forEach((chip) => chip.replaceWith(document.createTextNode(`<at>@${chip.dataset.mention ?? chip.textContent?.slice(1) ?? ""}</at>`)));
|
|
495
|
+
const text = fragment.textContent ?? "";
|
|
496
|
+
const html = `<div data-chatroom-mentions>${text.replace(/[&<>"]/g, (char) => ({ "&": "&", "<": "<", ">": ">", '"': """ })[char])}</div>`;
|
|
497
|
+
event.clipboardData?.setData("text/plain", text);
|
|
498
|
+
event.clipboardData?.setData("text/html", html);
|
|
499
|
+
event.preventDefault();
|
|
500
|
+
});
|
|
501
|
+
["#inRoomId", "#inName", "#inRoomName"].forEach((selector) => {
|
|
502
|
+
$(selector).addEventListener("input", app.hideJoinError);
|
|
503
|
+
});
|
|
504
|
+
["#inToken", "#inTokenChat"].forEach((selector) => {
|
|
505
|
+
$(selector).addEventListener("keydown", (event) => {
|
|
506
|
+
if (event.key === "Enter") {
|
|
507
|
+
event.preventDefault();
|
|
508
|
+
document.querySelector(`[data-token-save="${selector.slice(1)}"]`).click();
|
|
509
|
+
}
|
|
510
|
+
});
|
|
511
|
+
$(selector).addEventListener("input", () => {
|
|
512
|
+
app.hideJoinError();
|
|
513
|
+
app.hideSendError();
|
|
514
|
+
});
|
|
515
|
+
});
|
|
516
|
+
// A press inside a dialog released over the backdrop dispatches click to the
|
|
517
|
+
// dialog's common ancestor (body), so it used to read as an outside click and
|
|
518
|
+
// close the dialog through closePanels. Only a press that started outside may.
|
|
519
|
+
let pressInDialog = false;
|
|
520
|
+
document.addEventListener("pointerdown", (event) => {
|
|
521
|
+
pressInDialog = Boolean(event.target.closest?.("dialog[open]"));
|
|
522
|
+
}, true);
|
|
523
|
+
document.addEventListener("click", (event) => {
|
|
524
|
+
const releasedFromDialog = pressInDialog && !event.target.closest?.("dialog");
|
|
525
|
+
pressInDialog = false;
|
|
526
|
+
const save = event.target.closest("[data-token-save]");
|
|
527
|
+
if (save) {
|
|
528
|
+
saveAccessToken(save, app);
|
|
529
|
+
return;
|
|
530
|
+
}
|
|
531
|
+
if (app.mentionActive && !event.target.closest("#mentionPicker"))
|
|
532
|
+
app.closeMention();
|
|
533
|
+
if (!releasedFromDialog && !event.target.closest(".anchor, dialog"))
|
|
534
|
+
app.closePanels();
|
|
535
|
+
});
|
|
536
|
+
let lastEscapeAt = -Infinity;
|
|
537
|
+
document.addEventListener("keydown", (event) => {
|
|
538
|
+
if (event.key === "Escape" && !event.repeat && !event.defaultPrevented
|
|
539
|
+
&& !event.altKey && !event.ctrlKey && !event.metaKey && !event.shiftKey) {
|
|
540
|
+
const overlay = Boolean(document.querySelector("dialog[open], .panel:not([hidden]), #mentionPicker:not([hidden])"))
|
|
541
|
+
|| roomRail.classList.contains("drawer-open") || !$("#readerPanel").hidden;
|
|
542
|
+
const now = performance.now();
|
|
543
|
+
if (overlay)
|
|
544
|
+
lastEscapeAt = -Infinity;
|
|
545
|
+
else if (now - lastEscapeAt <= 500) {
|
|
546
|
+
lastEscapeAt = -Infinity;
|
|
547
|
+
app.focusChatComposer({ force: true });
|
|
548
|
+
}
|
|
549
|
+
else
|
|
550
|
+
lastEscapeAt = now;
|
|
551
|
+
}
|
|
552
|
+
if (event.key === "Tab" && roomRail.classList.contains("drawer-open") && !document.querySelector("dialog[open]")) {
|
|
553
|
+
const stops = [...roomRail.querySelectorAll("button:not(:disabled), input:not(:disabled)")].filter((node) => node.getClientRects().length);
|
|
554
|
+
const first = stops[0], last = stops.at(-1);
|
|
555
|
+
if (!roomRail.contains(document.activeElement)) {
|
|
556
|
+
event.preventDefault();
|
|
557
|
+
first?.focus();
|
|
558
|
+
}
|
|
559
|
+
else if (event.shiftKey && document.activeElement === first) {
|
|
560
|
+
event.preventDefault();
|
|
561
|
+
last?.focus();
|
|
562
|
+
}
|
|
563
|
+
else if (!event.shiftKey && document.activeElement === last) {
|
|
564
|
+
event.preventDefault();
|
|
565
|
+
first?.focus();
|
|
566
|
+
}
|
|
567
|
+
}
|
|
568
|
+
if (event.key === "Escape" && roomRail.classList.contains("drawer-open") && !document.querySelector("dialog[open]")) {
|
|
569
|
+
event.preventDefault();
|
|
570
|
+
closeRoomDrawer();
|
|
571
|
+
return;
|
|
572
|
+
}
|
|
573
|
+
if (event.key === "Escape" && app.mentionActive)
|
|
574
|
+
app.closeMention();
|
|
575
|
+
if (event.key === "Escape" && !$("#memberPanel").hidden) {
|
|
576
|
+
app.closePanels();
|
|
577
|
+
app.lastFocus?.focus();
|
|
578
|
+
}
|
|
579
|
+
if (event.key === "Escape" && !$("#roomOptions").hidden) {
|
|
580
|
+
app.closePanels();
|
|
581
|
+
$("#btnRoomOptions").focus();
|
|
582
|
+
}
|
|
583
|
+
if (event.altKey && !event.ctrlKey && !event.metaKey && !event.shiftKey && (event.key === "ArrowUp" || event.key === "ArrowDown")) {
|
|
584
|
+
// Same switch as a rail row click on a joined room, in the order the rail renders.
|
|
585
|
+
const joined = (app.state.availableRooms || []).filter((room) => app.state.rooms.has(room.id));
|
|
586
|
+
const index = joined.findIndex((room) => room.id === app.state.room?.id);
|
|
587
|
+
if (event.defaultPrevented || joined.length < 2 || index < 0 || document.querySelector("dialog[open]"))
|
|
588
|
+
return;
|
|
589
|
+
event.preventDefault();
|
|
590
|
+
const step = event.key === "ArrowDown" ? 1 : -1;
|
|
591
|
+
const nextRoom = joined[(index + step + joined.length) % joined.length];
|
|
592
|
+
if (app.switchRoom)
|
|
593
|
+
app.switchRoom(nextRoom.id, step);
|
|
594
|
+
else {
|
|
595
|
+
app.activateRoom(nextRoom.id);
|
|
596
|
+
app.animateRoomSwitch?.(step);
|
|
597
|
+
}
|
|
598
|
+
}
|
|
599
|
+
});
|
|
600
|
+
app.applyLocale();
|
|
601
|
+
$("#inToken").value = LS.token;
|
|
602
|
+
$("#inTokenChat").value = LS.token;
|
|
603
|
+
void app.seedFromCache?.().finally(() => app.refreshRoomList());
|
|
604
|
+
if (!$("#joinView").hidden)
|
|
605
|
+
$("#inRoomId").focus();
|
|
496
606
|
}
|
|
497
|
-
|
|
498
607
|
function saveAccessToken(button, app) {
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
app.
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
608
|
+
const input = document.getElementById(button.dataset.tokenSave);
|
|
609
|
+
input.value = app.setToken(input.value);
|
|
610
|
+
document.querySelector("#inToken").value = LS.token;
|
|
611
|
+
document.querySelector("#inTokenChat").value = LS.token;
|
|
612
|
+
let persistent = false;
|
|
613
|
+
try {
|
|
614
|
+
persistent = localStorage.getItem("chat.token") === LS.token;
|
|
615
|
+
}
|
|
616
|
+
catch { /* Memory fallback is tab-scoped. */ }
|
|
617
|
+
app.flash(button, persistent ? "saved" : "savedMemory");
|
|
618
|
+
if (LS.token) {
|
|
619
|
+
app.hideJoinError();
|
|
620
|
+
app.hideSendError();
|
|
621
|
+
const tokenHint = document.getElementById("tokenHint");
|
|
622
|
+
if (tokenHint)
|
|
623
|
+
tokenHint.hidden = true;
|
|
624
|
+
}
|
|
625
|
+
// The key prompt is a one-time visit: a stored key gets out of the way again.
|
|
626
|
+
if (LS.token)
|
|
627
|
+
document.getElementById("tokenBox")?.close();
|
|
628
|
+
void app.refreshRoomList();
|
|
629
|
+
app.retryPendingAction();
|
|
516
630
|
}
|
|
517
|
-
|
|
518
631
|
function handleMessageKey(event, app) {
|
|
519
|
-
|
|
520
|
-
if (app.mentionActive && !event.isComposing && event.keyCode !== 229) {
|
|
521
|
-
if (event.key === "ArrowDown") { event.preventDefault(); app.moveMention(1); return; }
|
|
522
|
-
if (event.key === "ArrowUp") { event.preventDefault(); app.moveMention(-1); return; }
|
|
523
|
-
if (event.key === "Escape") { event.preventDefault(); app.closeMention(); return; }
|
|
524
|
-
if (event.key === "Enter" || event.key === "Tab") {
|
|
525
|
-
if (app.mention?.items.length) {
|
|
526
|
-
event.preventDefault();
|
|
527
|
-
app.insertMention(app.mention.items[app.mention.index]);
|
|
632
|
+
if (app.handleMentionKey(event, event.currentTarget))
|
|
528
633
|
return;
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
634
|
+
if (app.mentionActive && !event.isComposing && event.keyCode !== 229) {
|
|
635
|
+
if (event.key === "ArrowDown") {
|
|
636
|
+
event.preventDefault();
|
|
637
|
+
app.moveMention(1);
|
|
638
|
+
return;
|
|
639
|
+
}
|
|
640
|
+
if (event.key === "ArrowUp") {
|
|
641
|
+
event.preventDefault();
|
|
642
|
+
app.moveMention(-1);
|
|
643
|
+
return;
|
|
644
|
+
}
|
|
645
|
+
if (event.key === "Escape") {
|
|
646
|
+
event.preventDefault();
|
|
647
|
+
app.closeMention();
|
|
648
|
+
return;
|
|
649
|
+
}
|
|
650
|
+
if (event.key === "Enter" || event.key === "Tab") {
|
|
651
|
+
if (app.mention?.items.length) {
|
|
652
|
+
event.preventDefault();
|
|
653
|
+
app.insertMention(app.mention.items[app.mention.index]);
|
|
654
|
+
return;
|
|
655
|
+
}
|
|
656
|
+
app.closeMention();
|
|
657
|
+
if (event.key === "Tab")
|
|
658
|
+
return;
|
|
659
|
+
}
|
|
660
|
+
}
|
|
661
|
+
if (event.key === "Enter" && !event.isComposing && event.keyCode !== 229 && (event.ctrlKey || (!event.shiftKey && !matchMedia("(pointer: coarse)").matches))) {
|
|
662
|
+
event.preventDefault();
|
|
663
|
+
void app.send();
|
|
532
664
|
}
|
|
533
|
-
}
|
|
534
|
-
if (event.key === "Enter" && !event.isComposing && event.keyCode !== 229 && (event.ctrlKey || (!event.shiftKey && !matchMedia("(pointer: coarse)").matches))) {
|
|
535
|
-
event.preventDefault();
|
|
536
|
-
void app.send();
|
|
537
|
-
}
|
|
538
665
|
}
|