@alexsun-top/opencode-chat-room 0.8.2 → 0.8.4
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 +12 -0
- package/README.md +18 -62
- package/README.zh-CN.md +25 -69
- package/dist/cli.js +466 -188
- package/package.json +11 -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 +201 -161
- package/server/assets/chat/announcements.js +302 -208
- package/server/assets/chat/attachment-store.js +116 -76
- package/server/assets/chat/attachments.js +701 -441
- package/server/assets/chat/chat-background.js +179 -156
- package/server/assets/chat/clipboard.js +45 -25
- package/server/assets/chat/confirm-dialog.js +38 -35
- package/server/assets/chat/copy-action.js +40 -41
- package/server/assets/chat/create-room-dialog.js +50 -47
- package/server/assets/chat/diagram-viewer.js +293 -236
- package/server/assets/chat/doc-preview.js +516 -443
- package/server/assets/chat/editor.js +446 -336
- package/server/assets/chat/events.js +650 -510
- package/server/assets/chat/files.js +505 -431
- package/server/assets/chat/history-loader.js +81 -73
- package/server/assets/chat/icons.js +9 -11
- package/server/assets/chat/image-zoom.js +246 -179
- package/server/assets/chat/index.js +2 -3
- package/server/assets/chat/install-prompt.js +90 -69
- package/server/assets/chat/integration-account-schema.js +91 -87
- package/server/assets/chat/integration-editor.js +162 -155
- package/server/assets/chat/integration-values.js +116 -101
- package/server/assets/chat/integrations.js +750 -626
- package/server/assets/chat/join-requests.js +415 -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 -103
- package/server/assets/chat/markdown.js +557 -404
- package/server/assets/chat/members.js +259 -197
- package/server/assets/chat/mentions.js +181 -133
- package/server/assets/chat/message-cache.js +40 -33
- package/server/assets/chat/message-polling.js +299 -227
- package/server/assets/chat/message-window.js +78 -55
- package/server/assets/chat/messages.js +562 -480
- package/server/assets/chat/office-preview.js +46 -45
- package/server/assets/chat/palette.js +464 -417
- package/server/assets/chat/panels.js +94 -79
- package/server/assets/chat/pwa.js +147 -75
- package/server/assets/chat/questions.js +374 -302
- package/server/assets/chat/recall.js +338 -273
- package/server/assets/chat/receipts.js +151 -121
- package/server/assets/chat/reminders.js +345 -214
- package/server/assets/chat/replies.js +181 -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 +76 -50
- package/server/assets/chat/rooms.js +631 -500
- package/server/assets/chat/search.js +138 -114
- package/server/assets/chat/settings.js +225 -201
- package/server/assets/chat/shiki.js +62 -63
- package/server/assets/chat/shortcuts.js +212 -189
- package/server/assets/chat/side-pane.js +161 -150
- package/server/assets/chat/state.js +860 -737
- package/server/assets/chat/system-events.js +46 -44
- package/server/assets/chat/system-messages.js +57 -56
- package/server/assets/chat/task-form.js +179 -183
- package/server/assets/chat/task-row.js +158 -155
- package/server/assets/chat/tasks.js +665 -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 +3 -1
- package/server/routes/pages.ts +7 -5
- package/server/sw.js +346 -268
- package/src/web/integration-rules-editor.ts +2 -0
|
@@ -1,78 +1,86 @@
|
|
|
1
1
|
import { LOADED_MESSAGE_LIMIT } from "./state.js";
|
|
2
|
-
|
|
3
2
|
// IM-style upward pagination: near the top of the thread, fetch the previous
|
|
4
3
|
// history page and prepend it without moving the reader's viewport.
|
|
5
4
|
export function initHistoryLoader(app) {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
5
|
+
const $ = app.$;
|
|
6
|
+
let loading = false;
|
|
7
|
+
const spinner = document.createElement("li");
|
|
8
|
+
spinner.className = "history-loading";
|
|
9
|
+
spinner.setAttribute("role", "status");
|
|
10
|
+
const wheel = document.createElement("span");
|
|
11
|
+
wheel.className = "history-spinner";
|
|
12
|
+
wheel.setAttribute("aria-hidden", "true");
|
|
13
|
+
const label = document.createElement("span");
|
|
14
|
+
label.dataset.i18n = "loadingHistory";
|
|
15
|
+
label.textContent = app.t("loadingHistory");
|
|
16
|
+
spinner.append(wheel, label);
|
|
17
|
+
const prependOlder = (older, thread, list) => {
|
|
18
|
+
// The loading spinner can be the first child; only a day separator at the
|
|
19
|
+
// boundary is a duplicate to drop.
|
|
20
|
+
const first = list.firstElementChild;
|
|
21
|
+
const tailSep = first instanceof HTMLElement && first.classList.contains("day-sep") ? first : null;
|
|
22
|
+
const fragment = document.createDocumentFragment();
|
|
23
|
+
let day = null;
|
|
24
|
+
for (const message of older) {
|
|
25
|
+
const messageDay = new Date(message.createdAt).toDateString();
|
|
26
|
+
if (messageDay !== day) {
|
|
27
|
+
const separator = document.createElement("li");
|
|
28
|
+
separator.className = "day-sep";
|
|
29
|
+
separator.dataset.day = messageDay;
|
|
30
|
+
separator.textContent = app.dayText(message.createdAt);
|
|
31
|
+
fragment.append(separator);
|
|
32
|
+
day = messageDay;
|
|
33
|
+
}
|
|
34
|
+
const row = app.renderMessage(message, false);
|
|
35
|
+
row.dataset.day = messageDay;
|
|
36
|
+
fragment.append(row);
|
|
37
|
+
}
|
|
38
|
+
const beforeHeight = thread.scrollHeight, beforeTop = thread.scrollTop;
|
|
39
|
+
list.prepend(fragment);
|
|
40
|
+
const last = older.at(-1), tailDay = last && new Date(last.createdAt).toDateString();
|
|
41
|
+
if (tailSep?.classList.contains("day-sep") && tailDay === tailSep.dataset.day)
|
|
42
|
+
tailSep.remove();
|
|
43
|
+
app.cancelThreadScrollAnimation?.();
|
|
44
|
+
thread.scrollTop = beforeTop + (thread.scrollHeight - beforeHeight);
|
|
45
|
+
app.queueReceiptRefresh?.();
|
|
46
|
+
};
|
|
47
|
+
app.loadOlderHistory = async () => {
|
|
48
|
+
const room = app.state.room, entry = room && app.state.rooms.get(room.id);
|
|
49
|
+
const thread = $("#thread"), list = $("#msgList");
|
|
50
|
+
if (!room || !entry || !thread || !list || loading)
|
|
51
|
+
return;
|
|
52
|
+
loading = true;
|
|
53
|
+
const generation = app.state.pollGeneration;
|
|
54
|
+
try {
|
|
55
|
+
const firstRow = list.querySelector("[data-msg-id]");
|
|
56
|
+
const cachedIndex = firstRow ? entry.room.messages.findIndex((message) => message.id === firstRow.dataset.msgId) : -1;
|
|
57
|
+
if (cachedIndex > 0) {
|
|
58
|
+
prependOlder(entry.room.messages.slice(Math.max(0, cachedIndex - 100), cachedIndex), thread, list);
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
if (entry.historyComplete || entry.historyBeforeSeq == null)
|
|
62
|
+
return;
|
|
63
|
+
list.prepend(spinner);
|
|
64
|
+
const page = await app.api(`/rooms/${encodeURIComponent(room.id)}/history?beforeSeq=${entry.historyBeforeSeq}&limit=100`);
|
|
65
|
+
if (app.state.room?.id !== room.id || app.state.pollGeneration !== generation)
|
|
66
|
+
return;
|
|
67
|
+
const known = new Set(entry.room.messages.map((message) => message.id));
|
|
68
|
+
const older = (page.messages || []).filter((message) => !known.has(message.id));
|
|
69
|
+
entry.historyComplete = !page.hasMore;
|
|
70
|
+
spinner.remove();
|
|
71
|
+
if (older.length) {
|
|
72
|
+
prependOlder(older, thread, list);
|
|
73
|
+
entry.room.messages = [...older, ...entry.room.messages].sort((left, right) => left.createdAt - right.createdAt);
|
|
74
|
+
if (entry.room.messages.length > LOADED_MESSAGE_LIMIT)
|
|
75
|
+
entry.room.messages.splice(0, entry.room.messages.length - LOADED_MESSAGE_LIMIT);
|
|
76
|
+
app.cacheRoomSoon?.(room.id);
|
|
77
|
+
}
|
|
78
|
+
entry.historyBeforeSeq = page.hasMore ? (page.nextBeforeSeq ?? entry.room.messages[0]?.seq ?? null) : null;
|
|
79
|
+
}
|
|
80
|
+
catch { /* the next scroll toward the top retries */ }
|
|
81
|
+
finally {
|
|
82
|
+
loading = false;
|
|
83
|
+
spinner.remove();
|
|
84
|
+
}
|
|
85
|
+
};
|
|
78
86
|
}
|
|
@@ -1,15 +1,13 @@
|
|
|
1
1
|
const SPRITE = "/assets/fluent-icons.svg";
|
|
2
|
-
|
|
3
2
|
// Fluent icons are one local sprite; the class carries size and currentColor fill.
|
|
4
3
|
export function iconNode(name, className = "") {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
4
|
+
const svg = document.createElementNS("http://www.w3.org/2000/svg", "svg");
|
|
5
|
+
svg.setAttribute("class", `fluent-icon${className ? ` ${className}` : ""}`);
|
|
6
|
+
svg.setAttribute("aria-hidden", "true");
|
|
7
|
+
svg.setAttribute("viewBox", "0 0 24 24");
|
|
8
|
+
const use = document.createElementNS("http://www.w3.org/2000/svg", "use");
|
|
9
|
+
use.setAttribute("href", `${SPRITE}#fluent-${name}-24`);
|
|
10
|
+
svg.append(use);
|
|
11
|
+
return svg;
|
|
13
12
|
}
|
|
14
|
-
|
|
15
|
-
export const icon = (name, className = "") => iconNode(name, className).outerHTML;
|
|
13
|
+
export const icon = (name, className = "") => iconNode(name, className).outerHTML;
|
|
@@ -1,181 +1,248 @@
|
|
|
1
|
-
// An image in a message opens full-screen in a native dialog. Nothing in the
|
|
2
|
-
// message list moves: the overlay is a top-layer dialog, not a row change.
|
|
3
1
|
export function initImageZoom(app) {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
const
|
|
12
|
-
const
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
2
|
+
const dialog = app.$("#imageDialog"), preview = app.$("#imageDialogImage");
|
|
3
|
+
const zoomLevel = app.$("#zoomLevel"), zoomOut = app.$("#btnZoomOut"), zoomIn = app.$("#btnZoomIn"), zoomReset = app.$("#btnZoomReset");
|
|
4
|
+
if (!dialog || !preview)
|
|
5
|
+
return;
|
|
6
|
+
const MAX = 4, STEP = 1.25;
|
|
7
|
+
let trigger = null, scale = 1, fitScale = 1, pendingFit = false, panX = 0, panY = 0;
|
|
8
|
+
let drag = null;
|
|
9
|
+
const clamp = (value, min, max) => Math.min(max, Math.max(min, value));
|
|
10
|
+
const imageOf = (node) => {
|
|
11
|
+
const image = node instanceof Element ? node.closest("#msgList img, #attachmentList img") : null;
|
|
12
|
+
return image instanceof HTMLImageElement ? image : null;
|
|
13
|
+
};
|
|
14
|
+
const fitLimit = () => {
|
|
15
|
+
const style = getComputedStyle(dialog);
|
|
16
|
+
const width = dialog.clientWidth - parseFloat(style.paddingLeft) - parseFloat(style.paddingRight);
|
|
17
|
+
const height = dialog.clientHeight - parseFloat(style.paddingTop) - parseFloat(style.paddingBottom);
|
|
18
|
+
if (!preview.naturalWidth || !preview.naturalHeight)
|
|
19
|
+
return 1;
|
|
20
|
+
return Math.min(1, width / preview.naturalWidth, height / preview.naturalHeight);
|
|
21
|
+
};
|
|
22
|
+
const limits = () => {
|
|
23
|
+
const style = getComputedStyle(dialog);
|
|
24
|
+
const box = dialog.getBoundingClientRect();
|
|
25
|
+
const left = box.left + parseFloat(style.paddingLeft), right = box.right - parseFloat(style.paddingRight);
|
|
26
|
+
const top = box.top + parseFloat(style.paddingTop), bottom = box.bottom - parseFloat(style.paddingBottom);
|
|
27
|
+
// Flex centering keeps an oversized image centered, so its untransformed
|
|
28
|
+
// layout box may start outside the dialog; offsetLeft still gives that
|
|
29
|
+
// position, while the live rect reflects the current scale and pan.
|
|
30
|
+
const layoutLeft = box.left + parseFloat(style.borderLeftWidth) + preview.offsetLeft;
|
|
31
|
+
const layoutTop = box.top + parseFloat(style.borderTopWidth) + preview.offsetTop;
|
|
32
|
+
const scaledWidth = preview.offsetWidth * scale, scaledHeight = preview.offsetHeight * scale;
|
|
33
|
+
const baseLeft = layoutLeft + preview.offsetWidth / 2 - scaledWidth / 2;
|
|
34
|
+
const baseTop = layoutTop + preview.offsetHeight / 2 - scaledHeight / 2;
|
|
35
|
+
const fitsX = scaledWidth <= right - left, fitsY = scaledHeight <= bottom - top;
|
|
36
|
+
const minX = fitsX ? 0 : right - baseLeft - scaledWidth;
|
|
37
|
+
const maxX = fitsX ? 0 : left - baseLeft;
|
|
38
|
+
const minY = fitsY ? 0 : bottom - baseTop - scaledHeight;
|
|
39
|
+
const maxY = fitsY ? 0 : top - baseTop;
|
|
40
|
+
return { minX, maxX, minY, maxY, x: maxX - minX, y: maxY - minY };
|
|
41
|
+
};
|
|
42
|
+
const apply = () => {
|
|
43
|
+
const limit = limits();
|
|
44
|
+
panX = clamp(panX, limit.minX, limit.maxX);
|
|
45
|
+
panY = clamp(panY, limit.minY, limit.maxY);
|
|
46
|
+
preview.style.transform = `translate3d(${panX}px, ${panY}px, 0) scale(${scale})`;
|
|
47
|
+
preview.classList.toggle("is-zoomed", limit.x > 0 || limit.y > 0);
|
|
48
|
+
if (zoomLevel)
|
|
49
|
+
zoomLevel.textContent = `${Math.round(scale * 100)}%`;
|
|
50
|
+
if (zoomOut)
|
|
51
|
+
zoomOut.disabled = scale <= fitScale + .001;
|
|
52
|
+
if (zoomIn)
|
|
53
|
+
zoomIn.disabled = scale >= MAX - .001;
|
|
54
|
+
};
|
|
55
|
+
const zoomTo = (next, x = innerWidth / 2, y = innerHeight / 2) => {
|
|
56
|
+
const target = clamp(next, fitScale, MAX);
|
|
57
|
+
if (Math.abs(target - scale) < .001)
|
|
58
|
+
return apply();
|
|
59
|
+
// Keep the point under the pointer fixed while scaling.
|
|
60
|
+
const dx = x - innerWidth / 2, dy = y - innerHeight / 2, ratio = target / scale;
|
|
61
|
+
panX = dx - ratio * (dx - panX);
|
|
62
|
+
panY = dy - ratio * (dy - panY);
|
|
63
|
+
scale = target;
|
|
64
|
+
if (scale <= fitScale + .001) {
|
|
65
|
+
scale = fitScale;
|
|
66
|
+
panX = 0;
|
|
67
|
+
panY = 0;
|
|
68
|
+
}
|
|
69
|
+
preview.style.animation = "none";
|
|
70
|
+
apply();
|
|
71
|
+
};
|
|
72
|
+
const zoomBy = (factor) => zoomTo(scale * factor);
|
|
73
|
+
const resetZoom = () => {
|
|
74
|
+
fitScale = fitLimit();
|
|
75
|
+
scale = fitScale;
|
|
76
|
+
pendingFit = false;
|
|
77
|
+
panX = 0;
|
|
78
|
+
panY = 0;
|
|
79
|
+
apply();
|
|
80
|
+
};
|
|
81
|
+
const open = (image) => {
|
|
82
|
+
if (!image || !dialog)
|
|
83
|
+
return;
|
|
84
|
+
trigger = image;
|
|
85
|
+
dialog.showModal();
|
|
86
|
+
// Reuse the URL the list already loaded, so the overlay costs no second download.
|
|
87
|
+
preview.src = image.currentSrc || image.src;
|
|
88
|
+
preview.alt = image.alt || "";
|
|
89
|
+
preview.style.animation = "";
|
|
90
|
+
scale = 1;
|
|
91
|
+
panX = 0;
|
|
92
|
+
panY = 0;
|
|
93
|
+
fitScale = fitLimit();
|
|
94
|
+
pendingFit = !(preview.complete && preview.naturalWidth);
|
|
95
|
+
if (pendingFit)
|
|
96
|
+
apply();
|
|
97
|
+
else
|
|
98
|
+
resetZoom();
|
|
99
|
+
};
|
|
100
|
+
const close = () => { if (dialog.open)
|
|
101
|
+
dialog.close(); };
|
|
102
|
+
// A click that focuses the image makes the browser scroll it into view; the
|
|
103
|
+
// overlay must leave the thread exactly where the reader left it.
|
|
104
|
+
for (const selector of ["#msgList", "#attachmentList"])
|
|
105
|
+
app.$(selector)?.addEventListener("mousedown", (event) => {
|
|
106
|
+
if (imageOf(event.target))
|
|
107
|
+
event.preventDefault();
|
|
108
|
+
});
|
|
109
|
+
for (const selector of ["#msgList", "#attachmentList"])
|
|
110
|
+
app.$(selector)?.addEventListener("click", (event) => {
|
|
111
|
+
const image = imageOf(event.target);
|
|
112
|
+
if (!image)
|
|
113
|
+
return;
|
|
114
|
+
// An image wrapped in a link is zoomed, not followed.
|
|
115
|
+
event.preventDefault();
|
|
116
|
+
open(image);
|
|
117
|
+
});
|
|
118
|
+
document.addEventListener("keydown", (event) => {
|
|
119
|
+
if (event.key !== "Enter" && event.key !== " ")
|
|
120
|
+
return;
|
|
121
|
+
const image = imageOf(event.target);
|
|
122
|
+
if (!image)
|
|
123
|
+
return;
|
|
124
|
+
event.preventDefault();
|
|
125
|
+
open(image);
|
|
126
|
+
});
|
|
127
|
+
app.$("#btnCloseImage")?.addEventListener("click", close);
|
|
128
|
+
// A backdrop click closes, but a drag that started inside the viewer and was
|
|
129
|
+
// released on the backdrop does not: the click target would be the dialog then.
|
|
130
|
+
let dismissPress = false;
|
|
131
|
+
dialog.addEventListener("pointerdown", (event) => { dismissPress = event.target === dialog; });
|
|
132
|
+
dialog.addEventListener("click", (event) => {
|
|
133
|
+
if (event.target === dialog && dismissPress)
|
|
134
|
+
close();
|
|
135
|
+
dismissPress = false;
|
|
136
|
+
});
|
|
137
|
+
// The viewer owns the wheel while it is open so the thread behind it cannot scroll.
|
|
138
|
+
dialog.addEventListener("wheel", (event) => {
|
|
139
|
+
if (!dialog.open)
|
|
140
|
+
return;
|
|
141
|
+
event.preventDefault();
|
|
142
|
+
if (event.target instanceof Element && event.target.closest(".image-dialog-tools, .image-dialog-close"))
|
|
143
|
+
return;
|
|
144
|
+
const delta = event.deltaMode === 1 ? event.deltaY * 16 : event.deltaY;
|
|
145
|
+
zoomTo(scale * Math.exp(-delta * .0015), event.clientX, event.clientY);
|
|
146
|
+
}, { passive: false });
|
|
147
|
+
dialog.addEventListener("keydown", (event) => {
|
|
148
|
+
if (!dialog.open)
|
|
149
|
+
return;
|
|
150
|
+
if (event.key === "+" || event.key === "=") {
|
|
151
|
+
event.preventDefault();
|
|
152
|
+
zoomBy(STEP);
|
|
153
|
+
}
|
|
154
|
+
else if (event.key === "-" || event.key === "_") {
|
|
155
|
+
event.preventDefault();
|
|
156
|
+
zoomBy(1 / STEP);
|
|
157
|
+
}
|
|
158
|
+
else if (event.key === "0") {
|
|
159
|
+
event.preventDefault();
|
|
160
|
+
resetZoom();
|
|
161
|
+
}
|
|
162
|
+
else if ((limits().x > 0 || limits().y > 0) && event.key.startsWith("Arrow")) {
|
|
163
|
+
event.preventDefault();
|
|
164
|
+
const step = event.shiftKey ? 120 : 40;
|
|
165
|
+
if (event.key === "ArrowLeft")
|
|
166
|
+
panX += step;
|
|
167
|
+
else if (event.key === "ArrowRight")
|
|
168
|
+
panX -= step;
|
|
169
|
+
else if (event.key === "ArrowUp")
|
|
170
|
+
panY += step;
|
|
171
|
+
else
|
|
172
|
+
panY -= step;
|
|
173
|
+
apply();
|
|
174
|
+
}
|
|
175
|
+
});
|
|
176
|
+
zoomIn?.addEventListener("click", () => zoomBy(STEP));
|
|
177
|
+
zoomOut?.addEventListener("click", () => zoomBy(1 / STEP));
|
|
178
|
+
zoomReset?.addEventListener("click", resetZoom);
|
|
179
|
+
window.addEventListener("resize", () => {
|
|
180
|
+
if (!dialog.open)
|
|
181
|
+
return;
|
|
182
|
+
const wasFit = Math.abs(scale - fitScale) < .001;
|
|
183
|
+
fitScale = fitLimit();
|
|
184
|
+
if (wasFit) {
|
|
185
|
+
scale = fitScale;
|
|
186
|
+
panX = 0;
|
|
187
|
+
panY = 0;
|
|
188
|
+
}
|
|
189
|
+
else
|
|
190
|
+
scale = Math.max(scale, fitScale);
|
|
191
|
+
apply();
|
|
192
|
+
});
|
|
193
|
+
preview.addEventListener("load", () => { if (dialog.open && pendingFit)
|
|
194
|
+
resetZoom(); });
|
|
195
|
+
// Pointer dragging pans the zoomed image; the tool buttons are the touch and
|
|
196
|
+
// keyboard alternative for changing the scale, and arrow keys pan to the edges.
|
|
197
|
+
preview.draggable = false;
|
|
198
|
+
preview.addEventListener("pointerdown", (event) => {
|
|
199
|
+
if ((limits().x <= 0 && limits().y <= 0) || event.button !== 0)
|
|
200
|
+
return;
|
|
201
|
+
drag = { id: event.pointerId, x: event.clientX, y: event.clientY, panX, panY };
|
|
202
|
+
preview.setPointerCapture(event.pointerId);
|
|
203
|
+
preview.classList.add("is-dragging");
|
|
204
|
+
event.preventDefault();
|
|
205
|
+
});
|
|
206
|
+
preview.addEventListener("pointermove", (event) => {
|
|
207
|
+
if (!drag || event.pointerId !== drag.id)
|
|
208
|
+
return;
|
|
209
|
+
panX = drag.panX + event.clientX - drag.x;
|
|
210
|
+
panY = drag.panY + event.clientY - drag.y;
|
|
211
|
+
apply();
|
|
212
|
+
});
|
|
213
|
+
const endDrag = (event) => {
|
|
214
|
+
if (!drag || event.pointerId !== drag.id)
|
|
215
|
+
return;
|
|
216
|
+
drag = null;
|
|
217
|
+
preview.classList.remove("is-dragging");
|
|
218
|
+
if (preview.hasPointerCapture(event.pointerId))
|
|
219
|
+
preview.releasePointerCapture(event.pointerId);
|
|
220
|
+
};
|
|
221
|
+
preview.addEventListener("pointerup", endDrag);
|
|
222
|
+
preview.addEventListener("pointercancel", endDrag);
|
|
223
|
+
dialog.addEventListener("close", () => {
|
|
224
|
+
preview.removeAttribute("src");
|
|
225
|
+
preview.style.animation = "";
|
|
226
|
+
resetZoom();
|
|
227
|
+
if (trigger?.isConnected)
|
|
228
|
+
trigger.focus({ preventScroll: true });
|
|
229
|
+
trigger = null;
|
|
230
|
+
});
|
|
231
|
+
// Images are in the tab order so the overlay can hand focus back to them.
|
|
232
|
+
const enhance = () => {
|
|
233
|
+
for (const image of document.querySelectorAll("#msgList img, #attachmentList img")) {
|
|
234
|
+
if (image.hasAttribute("tabindex"))
|
|
235
|
+
continue;
|
|
236
|
+
image.tabIndex = 0;
|
|
237
|
+
image.setAttribute("role", "button");
|
|
238
|
+
image.dataset.i18nAriaLabel = "zoomImage";
|
|
239
|
+
image.setAttribute("aria-label", app.t("zoomImage"));
|
|
240
|
+
}
|
|
241
|
+
};
|
|
242
|
+
for (const selector of ["#msgList", "#attachmentList"]) {
|
|
243
|
+
const list = app.$(selector);
|
|
244
|
+
if (list)
|
|
245
|
+
new MutationObserver(enhance).observe(list, { childList: true, subtree: true });
|
|
123
246
|
}
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
zoomOut?.addEventListener("click", () => zoomBy(1 / STEP));
|
|
127
|
-
zoomReset?.addEventListener("click", resetZoom);
|
|
128
|
-
window.addEventListener("resize", () => {
|
|
129
|
-
if (!dialog.open) return;
|
|
130
|
-
const wasFit = Math.abs(scale - fitScale) < .001;
|
|
131
|
-
fitScale = fitLimit();
|
|
132
|
-
if (wasFit) { scale = fitScale; panX = 0; panY = 0; }
|
|
133
|
-
else scale = Math.max(scale, fitScale);
|
|
134
|
-
apply();
|
|
135
|
-
});
|
|
136
|
-
preview.addEventListener("load", () => { if (dialog.open && pendingFit) resetZoom(); });
|
|
137
|
-
// Pointer dragging pans the zoomed image; the tool buttons are the touch and
|
|
138
|
-
// keyboard alternative for changing the scale, and arrow keys pan to the edges.
|
|
139
|
-
preview.draggable = false;
|
|
140
|
-
preview.addEventListener("pointerdown", (event) => {
|
|
141
|
-
if ((limits().x <= 0 && limits().y <= 0) || event.button !== 0) return;
|
|
142
|
-
drag = { id: event.pointerId, x: event.clientX, y: event.clientY, panX, panY };
|
|
143
|
-
preview.setPointerCapture(event.pointerId);
|
|
144
|
-
preview.classList.add("is-dragging");
|
|
145
|
-
event.preventDefault();
|
|
146
|
-
});
|
|
147
|
-
preview.addEventListener("pointermove", (event) => {
|
|
148
|
-
if (!drag || event.pointerId !== drag.id) return;
|
|
149
|
-
panX = drag.panX + event.clientX - drag.x; panY = drag.panY + event.clientY - drag.y;
|
|
150
|
-
apply();
|
|
151
|
-
});
|
|
152
|
-
const endDrag = (event) => {
|
|
153
|
-
if (!drag || event.pointerId !== drag.id) return;
|
|
154
|
-
drag = null; preview.classList.remove("is-dragging");
|
|
155
|
-
if (preview.hasPointerCapture(event.pointerId)) preview.releasePointerCapture(event.pointerId);
|
|
156
|
-
};
|
|
157
|
-
preview.addEventListener("pointerup", endDrag);
|
|
158
|
-
preview.addEventListener("pointercancel", endDrag);
|
|
159
|
-
dialog.addEventListener("close", () => {
|
|
160
|
-
preview.removeAttribute("src");
|
|
161
|
-
preview.style.animation = "";
|
|
162
|
-
resetZoom();
|
|
163
|
-
if (trigger?.isConnected) trigger.focus({ preventScroll: true });
|
|
164
|
-
trigger = null;
|
|
165
|
-
});
|
|
166
|
-
// Images are in the tab order so the overlay can hand focus back to them.
|
|
167
|
-
const enhance = () => {
|
|
168
|
-
for (const image of document.querySelectorAll("#msgList img, #attachmentList img")) {
|
|
169
|
-
if (image.hasAttribute("tabindex")) continue;
|
|
170
|
-
image.tabIndex = 0;
|
|
171
|
-
image.setAttribute("role", "button");
|
|
172
|
-
image.dataset.i18nAriaLabel = "zoomImage";
|
|
173
|
-
image.setAttribute("aria-label", app.t("zoomImage"));
|
|
174
|
-
}
|
|
175
|
-
};
|
|
176
|
-
for (const selector of ["#msgList", "#attachmentList"]) {
|
|
177
|
-
const list = app.$(selector);
|
|
178
|
-
if (list) new MutationObserver(enhance).observe(list, { childList: true, subtree: true });
|
|
179
|
-
}
|
|
180
|
-
enhance();
|
|
181
|
-
}
|
|
247
|
+
enhance();
|
|
248
|
+
}
|