@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
|
@@ -6,483 +6,636 @@ import { docPreviewFormat } from "./resource-format.js";
|
|
|
6
6
|
import { copyTextToClipboard } from "./clipboard.js";
|
|
7
7
|
import { enhanceCodeElement } from "./shiki.js";
|
|
8
8
|
import { appendFootnotes, decorateLinks, prepareFootnotes, replaceFootnoteTokens } from "./markdown-footnotes.js";
|
|
9
|
-
|
|
10
9
|
export { copyTextToClipboard };
|
|
11
|
-
|
|
12
10
|
const mentionPattern = /<at>@([^<]+)<\/at>/g;
|
|
13
11
|
const resourcePattern = /\[\[resource:([\da-f]{8}-[\da-f]{4}-[\da-f]{4}-[\da-f]{4}-[\da-f]{12})\]\]/gi;
|
|
14
12
|
const previewUrls = new Map();
|
|
15
13
|
const sanitizerTags = new Map();
|
|
16
|
-
let previewObserver;
|
|
17
|
-
|
|
14
|
+
let previewObserver = null;
|
|
18
15
|
// Raw HTML is not a formatting surface here: unknown tags must stay visible as
|
|
19
16
|
// text instead of disappearing inside the sanitizer. Known safe tags still pass
|
|
20
17
|
// through, and DOMPurify remains the final gate.
|
|
21
18
|
function preserveRawHtml(value) {
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
19
|
+
return String(value).replace(/<[^>]*>/g, (tag) => {
|
|
20
|
+
const name = /^<\/?([A-Za-z][\w:-]*)/.exec(tag)?.[1];
|
|
21
|
+
return name && sanitizerKeepsTag(name) ? tag : escapeHtmlText(tag);
|
|
22
|
+
});
|
|
26
23
|
}
|
|
27
|
-
|
|
28
24
|
const SANITIZER_TAG_CACHE_MAX = 256;
|
|
29
25
|
function sanitizerKeepsTag(name) {
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
26
|
+
const key = name.toLowerCase();
|
|
27
|
+
const kept = sanitizerTags.get(key);
|
|
28
|
+
if (kept !== undefined)
|
|
29
|
+
return kept;
|
|
30
|
+
// A message with endless distinct tag names must not probe DOMPurify forever;
|
|
31
|
+
// past the cap, unknown tags simply stay literal text.
|
|
32
|
+
if (sanitizerTags.size >= SANITIZER_TAG_CACHE_MAX)
|
|
33
|
+
return false;
|
|
34
|
+
const probe = `<${key}></${key}>`;
|
|
35
|
+
const keeps = window.DOMPurify.sanitize(probe).toLowerCase().includes(`<${key}`);
|
|
36
|
+
sanitizerTags.set(key, keeps);
|
|
37
|
+
return keeps;
|
|
39
38
|
}
|
|
40
|
-
|
|
41
39
|
function escapeHtmlText(value) {
|
|
42
|
-
|
|
40
|
+
return String(value).replace(/[&<>"']/g, (char) => ({ "&": "&", "<": "<", ">": ">", '"': """, "'": "'" }[char]));
|
|
43
41
|
}
|
|
44
|
-
|
|
45
42
|
/**
|
|
46
43
|
* Render plain text that may contain resource markers into attachment cards.
|
|
47
44
|
* System relay rows use this instead of textContent so inbound files stay openable.
|
|
48
45
|
*/
|
|
49
46
|
export function renderResourceText(root, text, app) {
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
47
|
+
root.textContent = text;
|
|
48
|
+
for (const resource of replaceResourceMarkers(root, app)) {
|
|
49
|
+
void loadResourceCard(resource.card, resource.roomId ?? "", app);
|
|
50
|
+
}
|
|
54
51
|
}
|
|
55
|
-
|
|
56
52
|
export function renderMarkdown(root, text, app) {
|
|
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
|
-
const
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
53
|
+
const mentions = [], diagrams = new Map();
|
|
54
|
+
const nonce = createRandomId();
|
|
55
|
+
const notes = prepareFootnotes(text, nonce);
|
|
56
|
+
const source = notes.source.replace(mentionPattern, (_, name) => {
|
|
57
|
+
const token = `CHATMENTION${nonce}${mentions.length}END`;
|
|
58
|
+
mentions.push({ token, name, literal: `<at>@${name}</at>` });
|
|
59
|
+
return token;
|
|
60
|
+
});
|
|
61
|
+
const renderer = new window.marked.Renderer(), renderCode = renderer.code.bind(renderer);
|
|
62
|
+
renderer.html = ({ text }) => preserveRawHtml(text);
|
|
63
|
+
renderer.code = (token) => {
|
|
64
|
+
if ((token.lang || "").trim().toLowerCase().split(/\s+/)[0] !== "mermaid")
|
|
65
|
+
return renderCode(token);
|
|
66
|
+
const key = `CHATDIAGRAM${nonce}${diagrams.size}END`;
|
|
67
|
+
const literalToken = new RegExp(`CHATMENTION${nonce}\\d+END`, "g");
|
|
68
|
+
diagrams.set(key, token.text.replace(literalToken, (value) => mentions.find((mention) => mention.token === value)?.literal || value));
|
|
69
|
+
return `<div data-chat-mermaid="${key}"></div>`;
|
|
70
|
+
};
|
|
71
|
+
const parsed = window.marked.parse(source, { gfm: true, breaks: true, async: false, renderer });
|
|
72
|
+
const fragment = window.DOMPurify.sanitize(parsed, { RETURN_DOM_FRAGMENT: true });
|
|
73
|
+
replaceMentionTokens(fragment, mentions, app);
|
|
74
|
+
replaceFootnoteTokens(fragment, notes);
|
|
75
|
+
appendFootnotes(fragment, notes);
|
|
76
|
+
const resources = replaceResourceMarkers(fragment, app);
|
|
77
|
+
const diagramNodes = [...fragment.querySelectorAll("[data-chat-mermaid]")];
|
|
78
|
+
for (const node of diagramNodes) {
|
|
79
|
+
const source = diagrams.get(node.dataset.chatMermaid ?? "");
|
|
80
|
+
node.removeAttribute("data-chat-mermaid");
|
|
81
|
+
node.className = "mermaid-diagram";
|
|
82
|
+
if (source !== undefined) {
|
|
83
|
+
const unsafe = isUnsafeDiagram(source);
|
|
84
|
+
showDiagramFallback(node, source, app, !unsafe);
|
|
85
|
+
if (!unsafe)
|
|
86
|
+
activateDiagram(node, source, app);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
decorateLinks(fragment);
|
|
90
|
+
wrapTables(fragment);
|
|
91
|
+
fragment.querySelectorAll('input[type="checkbox"]').forEach((input) => { input.disabled = true; });
|
|
92
|
+
const codeBlocks = [...fragment.querySelectorAll("pre > code")];
|
|
93
|
+
for (const code of codeBlocks)
|
|
94
|
+
decorateCodeBlock(code, app);
|
|
95
|
+
root.appendChild(fragment);
|
|
96
|
+
// Highlight after insertion: the escaped text is readable while the bundle loads.
|
|
97
|
+
for (const code of codeBlocks)
|
|
98
|
+
void enhanceCodeElement(code);
|
|
99
|
+
for (let i = 0; i < diagramNodes.length; i++) {
|
|
100
|
+
const source = diagrams.get(`CHATDIAGRAM${nonce}${i}END`);
|
|
101
|
+
if (!isUnsafeDiagram(source))
|
|
102
|
+
void renderDiagram(diagramNodes[i], source, app);
|
|
88
103
|
}
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
wrapTables(fragment);
|
|
92
|
-
fragment.querySelectorAll('input[type="checkbox"]').forEach((input) => { input.disabled = true; });
|
|
93
|
-
const codeBlocks = [...fragment.querySelectorAll("pre > code")];
|
|
94
|
-
for (const code of codeBlocks) decorateCodeBlock(code, app);
|
|
95
|
-
root.appendChild(fragment);
|
|
96
|
-
// Highlight after insertion: the escaped text is readable while the bundle loads.
|
|
97
|
-
for (const code of codeBlocks) void enhanceCodeElement(code);
|
|
98
|
-
for (let i = 0; i < diagramNodes.length; i++) {
|
|
99
|
-
const source = diagrams.get(`CHATDIAGRAM${nonce}${i}END`);
|
|
100
|
-
if (!isUnsafeDiagram(source)) void renderDiagram(diagramNodes[i], source, app);
|
|
101
|
-
}
|
|
102
|
-
for (const resource of resources) void loadResourceCard(resource.card, resource.roomId, app);
|
|
104
|
+
for (const resource of resources)
|
|
105
|
+
void loadResourceCard(resource.card, resource.roomId ?? "", app);
|
|
103
106
|
}
|
|
104
|
-
|
|
105
107
|
// A narrow screen scrolls the table inside a frame instead of widening the message.
|
|
106
108
|
function wrapTables(fragment) {
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
109
|
+
for (const table of fragment.querySelectorAll("table")) {
|
|
110
|
+
const wrapper = document.createElement("div");
|
|
111
|
+
wrapper.className = "markdown-table";
|
|
112
|
+
table.replaceWith(wrapper);
|
|
113
|
+
wrapper.append(table);
|
|
114
|
+
}
|
|
113
115
|
}
|
|
114
|
-
|
|
115
116
|
function decorateCodeBlock(code, app) {
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
117
|
+
const pre = code.parentElement;
|
|
118
|
+
if (!pre || pre.closest(".mermaid-diagram") || pre.parentElement?.classList.contains("code-block"))
|
|
119
|
+
return;
|
|
120
|
+
const block = document.createElement("div"), head = document.createElement("div");
|
|
121
|
+
block.className = "code-block";
|
|
122
|
+
head.className = "code-block-head";
|
|
123
|
+
pre.replaceWith(block);
|
|
124
|
+
const language = (/language-(\S+)/.exec(code.className) || [])[1];
|
|
125
|
+
if (language) {
|
|
126
|
+
const label = document.createElement("span");
|
|
127
|
+
label.className = "code-language";
|
|
128
|
+
label.textContent = language.toLowerCase();
|
|
129
|
+
head.append(label);
|
|
130
|
+
}
|
|
131
|
+
head.append(codeToolbar(code, pre, block, app));
|
|
132
|
+
// The floating toolbar sits over the code's top-right corner. Hide it while a
|
|
133
|
+
// pointer drag starts inside the code so pointerup selects code, not the toolbar.
|
|
134
|
+
pre.addEventListener("pointerdown", () => {
|
|
135
|
+
head.classList.add("is-selecting");
|
|
136
|
+
const done = () => {
|
|
137
|
+
head.classList.remove("is-selecting");
|
|
138
|
+
window.removeEventListener("pointerup", done, true);
|
|
139
|
+
window.removeEventListener("pointercancel", done, true);
|
|
140
|
+
};
|
|
141
|
+
window.addEventListener("pointerup", done, true);
|
|
142
|
+
window.addEventListener("pointercancel", done, true);
|
|
143
|
+
});
|
|
144
|
+
block.append(head, pre);
|
|
143
145
|
}
|
|
144
|
-
|
|
145
146
|
function codeToolbar(code, pre, block, app) {
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
147
|
+
const toolbar = document.createElement("div");
|
|
148
|
+
toolbar.className = "code-toolbar";
|
|
149
|
+
const wrap = codeToolButton("code-wrap-toggle", "wrapCode", app, wrapIcon());
|
|
150
|
+
wrap.setAttribute("aria-pressed", "false");
|
|
151
|
+
wrap.addEventListener("click", (event) => {
|
|
152
|
+
event.stopPropagation();
|
|
153
|
+
// A pointer click should not pin the toolbar open; hovering is enough on desktop, focus on touch.
|
|
154
|
+
if (event.detail > 0 && !matchMedia("(pointer: coarse)").matches)
|
|
155
|
+
wrap.blur();
|
|
156
|
+
const wrapped = !block.classList.contains("is-wrapped");
|
|
157
|
+
if (wrapped)
|
|
158
|
+
block._codeScrollLeft = pre.scrollLeft;
|
|
159
|
+
block.classList.toggle("is-wrapped", wrapped);
|
|
160
|
+
wrap.setAttribute("aria-pressed", String(wrapped));
|
|
161
|
+
pre.scrollLeft = wrapped ? 0 : block._codeScrollLeft || 0;
|
|
162
|
+
});
|
|
163
|
+
const copy = codeToolButton("code-copy-button", "copyMessage", app, copyIcon(), checkIcon());
|
|
164
|
+
copy.addEventListener("click", (event) => {
|
|
165
|
+
event.stopPropagation();
|
|
166
|
+
if (event.detail > 0 && !matchMedia("(pointer: coarse)").matches)
|
|
167
|
+
copy.blur();
|
|
168
|
+
void copyCode(copy, code, app);
|
|
169
|
+
});
|
|
170
|
+
toolbar.append(wrap, copy);
|
|
171
|
+
return toolbar;
|
|
168
172
|
}
|
|
169
|
-
|
|
170
173
|
function codeToolButton(className, key, app, ...icons) {
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
174
|
+
const button = document.createElement("button");
|
|
175
|
+
button.type = "button";
|
|
176
|
+
button.className = `code-tool-button ${className}`;
|
|
177
|
+
button.setAttribute("aria-label", app.t(key));
|
|
178
|
+
button.title = app.t(key);
|
|
179
|
+
button.dataset.i18nAriaLabel = key;
|
|
180
|
+
button.dataset.i18nTitle = key;
|
|
181
|
+
button.append(...icons);
|
|
182
|
+
return button;
|
|
180
183
|
}
|
|
181
|
-
|
|
182
184
|
const wrapIcon = () => iconNode("text_wrap", "code-tool-icon code-wrap-icon");
|
|
183
185
|
const copyIcon = () => iconNode("copy", "code-tool-icon code-copy-icon");
|
|
184
186
|
const checkIcon = () => iconNode("checkmark", "code-tool-icon code-check-icon");
|
|
185
|
-
|
|
186
187
|
async function copyCode(button, code, app) {
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
188
|
+
const copied = await copyTextToClipboard(code.textContent ?? "");
|
|
189
|
+
clearTimeout(button._feedbackTimer);
|
|
190
|
+
button.classList.toggle("is-copied", copied);
|
|
191
|
+
button.classList.toggle("is-copy-failed", !copied);
|
|
192
|
+
const key = copied ? "copiedMessage" : "copyMessageFailed";
|
|
193
|
+
button.setAttribute("aria-label", app.t(key));
|
|
194
|
+
button.title = app.t(key);
|
|
195
|
+
button._feedbackTimer = setTimeout(() => {
|
|
196
|
+
button.classList.remove("is-copied", "is-copy-failed");
|
|
197
|
+
button.setAttribute("aria-label", app.t("copyMessage"));
|
|
198
|
+
button.title = app.t("copyMessage");
|
|
199
|
+
}, 1600);
|
|
199
200
|
}
|
|
200
|
-
|
|
201
201
|
// A rendered diagram is a button: click, Enter, or Space opens the readable overlay.
|
|
202
202
|
function activateDiagram(node, source, app) {
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
203
|
+
node.tabIndex = 0;
|
|
204
|
+
node.setAttribute("role", "button");
|
|
205
|
+
node.dataset.i18nAriaLabel = "openDiagram";
|
|
206
|
+
node.setAttribute("aria-label", app.t("openDiagram"));
|
|
207
|
+
rememberDiagramSource(node, source);
|
|
208
|
+
const open = (event) => {
|
|
209
|
+
const svg = node.querySelector("svg");
|
|
210
|
+
if (!svg || !app.openDiagram)
|
|
211
|
+
return;
|
|
212
|
+
event.preventDefault();
|
|
213
|
+
event.stopPropagation();
|
|
214
|
+
app.openDiagram(svg, source);
|
|
215
|
+
};
|
|
216
|
+
// Focus belongs in the viewer; closing returns it here.
|
|
217
|
+
node.addEventListener("mousedown", (event) => { if (node.querySelector("svg"))
|
|
218
|
+
event.preventDefault(); });
|
|
219
|
+
node.addEventListener("click", open);
|
|
220
|
+
node.addEventListener("keydown", (event) => {
|
|
221
|
+
if (event.key === "Enter" || event.key === " ")
|
|
222
|
+
open(event);
|
|
223
|
+
});
|
|
221
224
|
}
|
|
222
|
-
|
|
223
225
|
function replaceResourceMarkers(fragment, app) {
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
226
|
+
const walker = document.createTreeWalker(fragment, NodeFilter.SHOW_TEXT), nodes = [], cards = [], roomId = app.state.room?.id;
|
|
227
|
+
while (walker.nextNode())
|
|
228
|
+
nodes.push(walker.currentNode);
|
|
229
|
+
for (const node of nodes) {
|
|
230
|
+
if (node.parentElement?.closest("code, pre, a"))
|
|
231
|
+
continue;
|
|
232
|
+
const value = node.nodeValue || "", replacement = document.createDocumentFragment();
|
|
233
|
+
let last = 0, match;
|
|
234
|
+
resourcePattern.lastIndex = 0;
|
|
235
|
+
while ((match = resourcePattern.exec(value))) {
|
|
236
|
+
replacement.append(document.createTextNode(value.slice(last, match.index)));
|
|
237
|
+
const card = document.createElement("span"), status = document.createElement("span");
|
|
238
|
+
card.className = "chat-resource";
|
|
239
|
+
card.dataset.resourceId = match[1];
|
|
240
|
+
card.setAttribute("role", "group");
|
|
241
|
+
status.className = "resource-status";
|
|
242
|
+
status.setAttribute("role", "status");
|
|
243
|
+
status.textContent = app.t("resourceLoading");
|
|
244
|
+
card.append(status);
|
|
245
|
+
replacement.append(card);
|
|
246
|
+
cards.push({ card, roomId });
|
|
247
|
+
last = resourcePattern.lastIndex;
|
|
248
|
+
}
|
|
249
|
+
if (last) {
|
|
250
|
+
replacement.append(document.createTextNode(value.slice(last)));
|
|
251
|
+
node.parentNode?.replaceChild(replacement, node);
|
|
252
|
+
}
|
|
238
253
|
}
|
|
239
|
-
|
|
240
|
-
}
|
|
241
|
-
return cards;
|
|
254
|
+
return cards;
|
|
242
255
|
}
|
|
243
256
|
async function loadResourceCard(card, roomId, app) {
|
|
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
|
-
|
|
257
|
+
try {
|
|
258
|
+
const id = card.dataset.resourceId ?? "";
|
|
259
|
+
const result = await app.api(`/rooms/${encodeURIComponent(roomId)}/resources/${encodeURIComponent(id)}`);
|
|
260
|
+
if (!card.isConnected)
|
|
261
|
+
return;
|
|
262
|
+
const resource = result.resource, type = String(resource.mimeType).split(";")[0].toLowerCase();
|
|
263
|
+
card.setAttribute("aria-label", resource.name);
|
|
264
|
+
const info = document.createElement("span"), name = document.createElement("span"), size = document.createElement("span"), actions = document.createElement("span"), status = document.createElement("span");
|
|
265
|
+
info.className = "resource-info";
|
|
266
|
+
name.className = "resource-name";
|
|
267
|
+
name.textContent = resource.name;
|
|
268
|
+
size.className = "resource-size";
|
|
269
|
+
size.textContent = formatBytes(resource.size);
|
|
270
|
+
info.append(name, size);
|
|
271
|
+
actions.className = "resource-actions";
|
|
272
|
+
status.className = "resource-status";
|
|
273
|
+
status.setAttribute("role", "status");
|
|
274
|
+
card.replaceChildren(info, actions, status);
|
|
275
|
+
const download = resourceButton("resourceDownload", resource.name, () => downloadResource(card, roomId, id, resource, app, status, download), app);
|
|
276
|
+
download.classList.add("resource-download");
|
|
277
|
+
if (isRoomAdmin(app)) {
|
|
278
|
+
let remove;
|
|
279
|
+
remove = resourceMenuItem("resourceDelete", () => deleteResource(card, roomId, id, resource, app, status, remove), app);
|
|
280
|
+
actions.append(resourceSplit(app, resource, download, remove));
|
|
281
|
+
}
|
|
282
|
+
else
|
|
283
|
+
actions.append(download);
|
|
284
|
+
const docFormat = docPreviewFormat(resource);
|
|
285
|
+
if (previewKind(type) || docFormat) {
|
|
286
|
+
const preview = resourceButton("resourcePreview", resource.name, () => docFormat
|
|
287
|
+
? app.openDocPreview(roomId, resource, preview)
|
|
288
|
+
: previewResource(card, roomId, id, resource, type, preview, app, status), app);
|
|
289
|
+
preview.dataset.previewResource = resource.id;
|
|
290
|
+
actions.append(preview);
|
|
291
|
+
// Media you sent yourself is shown inline; other people's media stays collapsed.
|
|
292
|
+
if (!docFormat && card.closest(".msg.own"))
|
|
293
|
+
void previewResource(card, roomId, id, resource, type, preview, app, status);
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
catch (error) {
|
|
297
|
+
if (card.isConnected)
|
|
298
|
+
setResourceError(card, app, error instanceof Error && error.status === 404 ? "resourceDeleted" : "resourceFailed");
|
|
271
299
|
}
|
|
272
|
-
} catch (error) { if (card.isConnected) setResourceError(card, app, error?.status === 404 ? "resourceDeleted" : "resourceFailed"); }
|
|
273
300
|
}
|
|
274
301
|
function setResourceButton(button, key, name, app) {
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
302
|
+
button.dataset.i18n = key;
|
|
303
|
+
const label = button.querySelector(".i18n-label");
|
|
304
|
+
if (label)
|
|
305
|
+
label.textContent = app.t(key);
|
|
306
|
+
else
|
|
307
|
+
button.textContent = app.t(key);
|
|
308
|
+
button.setAttribute("aria-label", `${app.t(key)}: ${name}`);
|
|
279
309
|
}
|
|
280
310
|
function resourceButton(key, name, action, app) {
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
311
|
+
const button = document.createElement("button");
|
|
312
|
+
button.type = "button";
|
|
313
|
+
button.className = "btn ghost small resource-button";
|
|
314
|
+
const label = document.createElement("span");
|
|
315
|
+
label.className = "i18n-label";
|
|
316
|
+
label.textContent = app.t(key);
|
|
317
|
+
button.append(iconNode(key === "resourceDownload" ? "arrow_download" : "eye"), label);
|
|
318
|
+
button.addEventListener("click", action);
|
|
319
|
+
setResourceButton(button, key, name, app);
|
|
320
|
+
return button;
|
|
289
321
|
}
|
|
290
322
|
function resourceMenuItem(key, action, app) {
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
323
|
+
const button = document.createElement("button");
|
|
324
|
+
button.type = "button";
|
|
325
|
+
button.className = "menu-action danger-text";
|
|
326
|
+
button.setAttribute("role", "menuitem");
|
|
327
|
+
button.dataset.i18n = key;
|
|
328
|
+
const label = document.createElement("span");
|
|
329
|
+
label.className = "i18n-label";
|
|
330
|
+
label.textContent = app.t(key);
|
|
331
|
+
button.append(iconNode("delete"), label);
|
|
332
|
+
button.addEventListener("click", action);
|
|
333
|
+
return button;
|
|
298
334
|
}
|
|
299
335
|
function resourceSplit(app, resource, download, item) {
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
menu.
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
336
|
+
const group = document.createElement("span"), toggle = document.createElement("button"), menu = document.createElement("span");
|
|
337
|
+
group.className = "resource-split";
|
|
338
|
+
toggle.type = "button";
|
|
339
|
+
toggle.className = "btn ghost small resource-button resource-split-toggle";
|
|
340
|
+
toggle.setAttribute("aria-haspopup", "menu");
|
|
341
|
+
toggle.setAttribute("aria-expanded", "false");
|
|
342
|
+
toggle.setAttribute("data-i18n-aria-label", "resourceMoreActions");
|
|
343
|
+
toggle._i18nValues = { name: resource.name };
|
|
344
|
+
toggle.setAttribute("aria-label", app.t("resourceMoreActions", { name: resource.name }));
|
|
345
|
+
toggle.append(iconNode("chevron_down"));
|
|
346
|
+
menu.className = "resource-split-menu";
|
|
347
|
+
menu.hidden = true;
|
|
348
|
+
menu.setAttribute("role", "menu");
|
|
349
|
+
menu.append(item);
|
|
350
|
+
group.append(download, toggle, menu);
|
|
351
|
+
function close(focusToggle = false) {
|
|
352
|
+
if (menu.hidden)
|
|
353
|
+
return;
|
|
354
|
+
menu.hidden = true;
|
|
355
|
+
toggle.setAttribute("aria-expanded", "false");
|
|
356
|
+
document.removeEventListener("click", outside);
|
|
357
|
+
if (focusToggle)
|
|
358
|
+
toggle.focus();
|
|
359
|
+
}
|
|
360
|
+
function open() {
|
|
361
|
+
if (!menu.hidden)
|
|
362
|
+
return;
|
|
363
|
+
menu.hidden = false;
|
|
364
|
+
toggle.setAttribute("aria-expanded", "true");
|
|
365
|
+
document.addEventListener("click", outside);
|
|
366
|
+
item.focus();
|
|
367
|
+
}
|
|
368
|
+
function outside(event) { if (!group.isConnected || !group.contains(event.target))
|
|
369
|
+
close(); }
|
|
370
|
+
item.addEventListener("click", () => close(), { capture: true });
|
|
371
|
+
toggle.addEventListener("click", () => { if (menu.hidden)
|
|
372
|
+
open();
|
|
373
|
+
else
|
|
374
|
+
close(true); });
|
|
375
|
+
toggle.addEventListener("keydown", (event) => {
|
|
376
|
+
if (event.key === "ArrowDown" || event.key === "ArrowUp") {
|
|
377
|
+
event.preventDefault();
|
|
378
|
+
open();
|
|
379
|
+
}
|
|
380
|
+
});
|
|
381
|
+
menu.addEventListener("keydown", (event) => {
|
|
382
|
+
const items = [...menu.querySelectorAll('[role="menuitem"]:not(:disabled)')], index = items.indexOf(document.activeElement);
|
|
383
|
+
if (event.key === "Escape") {
|
|
384
|
+
event.preventDefault();
|
|
385
|
+
close(true);
|
|
386
|
+
}
|
|
387
|
+
else if (event.key === "ArrowDown" || event.key === "ArrowUp") {
|
|
388
|
+
event.preventDefault();
|
|
389
|
+
items[(index + (event.key === "ArrowDown" ? 1 : -1) + items.length) % items.length]?.focus();
|
|
390
|
+
}
|
|
391
|
+
else if (event.key === "Home" || event.key === "End") {
|
|
392
|
+
event.preventDefault();
|
|
393
|
+
(event.key === "Home" ? items[0] : items.at(-1))?.focus();
|
|
394
|
+
}
|
|
395
|
+
});
|
|
396
|
+
group.addEventListener("keydown", (event) => {
|
|
397
|
+
if (event.key === "Escape" && !menu.hidden) {
|
|
398
|
+
event.preventDefault();
|
|
399
|
+
close(true);
|
|
400
|
+
}
|
|
401
|
+
});
|
|
402
|
+
group.addEventListener("focusout", (event) => { if (!menu.hidden && !group.contains(event.relatedTarget))
|
|
403
|
+
close(); });
|
|
404
|
+
return group;
|
|
339
405
|
}
|
|
340
406
|
async function deleteResource(card, roomId, id, resource, app, status, button) {
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
407
|
+
if (!(await app.confirmAction(app.t("resourceDeleteConfirm", { name: resource.name }))))
|
|
408
|
+
return;
|
|
409
|
+
button.disabled = true;
|
|
410
|
+
status.textContent = app.t("resourceDeleting");
|
|
411
|
+
try {
|
|
412
|
+
await app.api(`/rooms/${encodeURIComponent(roomId)}/resources/${encodeURIComponent(id)}`, { method: "DELETE", headers: { "x-session-id": LS.sessionID }, body: "{}" });
|
|
413
|
+
if (!card.isConnected)
|
|
414
|
+
return;
|
|
415
|
+
card._previewElement?.remove();
|
|
416
|
+
if (card._resourceUrl)
|
|
417
|
+
URL.revokeObjectURL(card._resourceUrl);
|
|
418
|
+
previewUrls.delete(card);
|
|
419
|
+
delete card._resourceUrl;
|
|
420
|
+
delete card._previewElement;
|
|
421
|
+
card.querySelector(".resource-actions")?.remove();
|
|
422
|
+
status.textContent = app.t("resourceDeleted");
|
|
423
|
+
// The same file can appear in quoted or repeated messages; mark those copies too.
|
|
424
|
+
for (const other of document.querySelectorAll(".chat-resource")) {
|
|
425
|
+
if (other === card || other.dataset.resourceId !== id)
|
|
426
|
+
continue;
|
|
427
|
+
other.querySelector(".resource-actions")?.remove();
|
|
428
|
+
const otherStatus = other.querySelector(".resource-status");
|
|
429
|
+
if (otherStatus)
|
|
430
|
+
otherStatus.textContent = app.t("resourceDeleted");
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
catch {
|
|
434
|
+
if (card.isConnected) {
|
|
435
|
+
status.textContent = app.t("resourceDeleteFailed");
|
|
436
|
+
button.disabled = false;
|
|
437
|
+
}
|
|
358
438
|
}
|
|
359
|
-
} catch {
|
|
360
|
-
if (card.isConnected) { status.textContent = app.t("resourceDeleteFailed"); button.disabled = false; }
|
|
361
|
-
}
|
|
362
439
|
}
|
|
363
440
|
async function downloadResource(card, roomId, id, resource, app, status, button) {
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
441
|
+
if (!card.isConnected || button.disabled)
|
|
442
|
+
return;
|
|
443
|
+
button.disabled = true;
|
|
444
|
+
status.textContent = app.t("resourceLoading");
|
|
445
|
+
try {
|
|
446
|
+
const blob = await app.apiBlob(`/rooms/${encodeURIComponent(roomId)}/resources/${encodeURIComponent(id)}/content`);
|
|
447
|
+
if (!card.isConnected)
|
|
448
|
+
return;
|
|
449
|
+
const url = URL.createObjectURL(blob), link = document.createElement("a");
|
|
450
|
+
link.href = url;
|
|
451
|
+
link.download = String(resource.name).replace(/[\\/\u0000-\u001f\u007f]/g, "_") || "download";
|
|
452
|
+
document.body.append(link);
|
|
453
|
+
link.click();
|
|
454
|
+
link.remove();
|
|
455
|
+
setTimeout(() => URL.revokeObjectURL(url), 1000);
|
|
456
|
+
status.textContent = "";
|
|
457
|
+
}
|
|
458
|
+
catch {
|
|
459
|
+
if (card.isConnected)
|
|
460
|
+
status.textContent = app.t("resourceFailed");
|
|
461
|
+
}
|
|
462
|
+
finally {
|
|
463
|
+
if (card.isConnected)
|
|
464
|
+
button.disabled = false;
|
|
465
|
+
}
|
|
376
466
|
}
|
|
377
467
|
async function previewResource(card, roomId, id, resource, type, button, app, status) {
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
468
|
+
if (!card.isConnected || button.disabled)
|
|
469
|
+
return;
|
|
470
|
+
button.disabled = true;
|
|
471
|
+
const current = card._previewElement;
|
|
472
|
+
if (current) {
|
|
473
|
+
try {
|
|
474
|
+
await app.preserveReadingLayout(() => {
|
|
475
|
+
current.remove();
|
|
476
|
+
setResourceButton(button, "resourcePreview", resource.name, app);
|
|
477
|
+
button.disabled = false;
|
|
478
|
+
});
|
|
479
|
+
if (card._resourceUrl)
|
|
480
|
+
URL.revokeObjectURL(card._resourceUrl);
|
|
481
|
+
previewUrls.delete(card);
|
|
482
|
+
delete card._resourceUrl;
|
|
483
|
+
delete card._previewElement;
|
|
484
|
+
}
|
|
485
|
+
catch {
|
|
486
|
+
if (!card.isConnected)
|
|
487
|
+
return;
|
|
488
|
+
}
|
|
489
|
+
finally {
|
|
490
|
+
if (card.isConnected)
|
|
491
|
+
button.disabled = false;
|
|
492
|
+
}
|
|
493
|
+
return;
|
|
494
|
+
}
|
|
495
|
+
status.textContent = app.t("resourceLoading");
|
|
496
|
+
let url;
|
|
497
|
+
let media;
|
|
498
|
+
const fail = () => {
|
|
499
|
+
media?.remove();
|
|
500
|
+
media = undefined;
|
|
501
|
+
if (url) {
|
|
502
|
+
URL.revokeObjectURL(url);
|
|
503
|
+
url = undefined;
|
|
504
|
+
}
|
|
505
|
+
setResourceButton(button, "resourcePreview", resource.name, app);
|
|
506
|
+
button.disabled = false;
|
|
507
|
+
if (card.isConnected)
|
|
508
|
+
status.textContent = app.t("resourceFailed");
|
|
509
|
+
};
|
|
381
510
|
try {
|
|
382
|
-
|
|
383
|
-
card.
|
|
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
|
-
if (
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
511
|
+
const blob = await app.apiBlob(`/rooms/${encodeURIComponent(roomId)}/resources/${encodeURIComponent(id)}/content`);
|
|
512
|
+
if (!card.isConnected)
|
|
513
|
+
return;
|
|
514
|
+
const objectUrl = URL.createObjectURL(blob), kind = previewKind(type), element = createPreviewMedia(kind, resource.name);
|
|
515
|
+
url = objectUrl;
|
|
516
|
+
media = element;
|
|
517
|
+
const ready = waitForMediaSize(element, kind);
|
|
518
|
+
element.addEventListener("error", fail, { once: true });
|
|
519
|
+
element.src = objectUrl;
|
|
520
|
+
// Update the control inside the layout step: a stalled animation frame must not
|
|
521
|
+
// leave the preview open with a disabled button.
|
|
522
|
+
await app.preserveReadingLayout(async () => {
|
|
523
|
+
status.textContent = "";
|
|
524
|
+
card.append(element);
|
|
525
|
+
setResourceButton(button, "resourceHide", resource.name, app);
|
|
526
|
+
button.disabled = false;
|
|
527
|
+
await ready;
|
|
528
|
+
});
|
|
529
|
+
if (!card.isConnected) {
|
|
530
|
+
fail();
|
|
531
|
+
return;
|
|
532
|
+
}
|
|
533
|
+
card._resourceUrl = objectUrl;
|
|
534
|
+
card._previewElement = element;
|
|
535
|
+
previewUrls.set(card, objectUrl);
|
|
536
|
+
watchPreviewRemoval();
|
|
537
|
+
}
|
|
538
|
+
catch {
|
|
539
|
+
fail();
|
|
540
|
+
}
|
|
541
|
+
finally {
|
|
542
|
+
if (card.isConnected)
|
|
543
|
+
button.disabled = false;
|
|
544
|
+
}
|
|
545
|
+
}
|
|
546
|
+
function createPreviewMedia(kind, name) {
|
|
547
|
+
if (kind === "img") {
|
|
548
|
+
const image = document.createElement("img");
|
|
549
|
+
image.alt = name;
|
|
550
|
+
return image;
|
|
551
|
+
}
|
|
552
|
+
const element = document.createElement(kind === "audio" ? "audio" : "video");
|
|
553
|
+
element.controls = true;
|
|
554
|
+
element.preload = "metadata";
|
|
555
|
+
return element;
|
|
422
556
|
}
|
|
423
557
|
function waitForMediaSize(media, kind) {
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
558
|
+
const event = kind === "img" ? "load" : "loadedmetadata";
|
|
559
|
+
if (media instanceof HTMLImageElement && media.complete && media.naturalWidth)
|
|
560
|
+
return Promise.resolve();
|
|
561
|
+
if (media instanceof HTMLMediaElement && media.readyState >= 1)
|
|
562
|
+
return Promise.resolve();
|
|
563
|
+
return new Promise((resolve, reject) => {
|
|
564
|
+
media.addEventListener(event, resolve, { once: true });
|
|
565
|
+
media.addEventListener("error", reject, { once: true });
|
|
566
|
+
});
|
|
431
567
|
}
|
|
432
568
|
function previewKind(type) {
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
569
|
+
if (["image/png", "image/jpeg", "image/gif", "image/webp", "image/avif"].includes(type))
|
|
570
|
+
return "img";
|
|
571
|
+
if (["audio/mpeg", "audio/ogg", "audio/wav", "audio/webm", "audio/mp4"].includes(type))
|
|
572
|
+
return "audio";
|
|
573
|
+
if (["video/mp4", "video/webm", "video/ogg"].includes(type))
|
|
574
|
+
return "video";
|
|
575
|
+
return "";
|
|
437
576
|
}
|
|
438
577
|
export function formatBytes(value) {
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
578
|
+
const bytes = Math.max(0, Number(value) || 0);
|
|
579
|
+
if (bytes < 1024)
|
|
580
|
+
return `${bytes} B`;
|
|
581
|
+
return bytes < 1048576 ? `${(bytes / 1024).toFixed(1)} KB` : `${(bytes / 1048576).toFixed(1)} MB`;
|
|
442
582
|
}
|
|
443
583
|
function setResourceError(card, app, key = "resourceFailed") {
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
584
|
+
card.textContent = "";
|
|
585
|
+
const status = document.createElement("span");
|
|
586
|
+
status.className = "resource-status";
|
|
587
|
+
status.setAttribute("role", "status");
|
|
588
|
+
status.textContent = app.t(key);
|
|
589
|
+
card.append(status);
|
|
447
590
|
}
|
|
448
591
|
function watchPreviewRemoval() {
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
592
|
+
const list = document.querySelector("#msgList");
|
|
593
|
+
if (previewObserver || !list)
|
|
594
|
+
return;
|
|
595
|
+
previewObserver = new MutationObserver((records) => {
|
|
596
|
+
for (const node of records.flatMap((record) => [...record.removedNodes])) {
|
|
597
|
+
if (!(node instanceof Element))
|
|
598
|
+
continue;
|
|
599
|
+
for (const card of [node, ...node.querySelectorAll(".chat-resource")]) {
|
|
600
|
+
const url = previewUrls.get(card);
|
|
601
|
+
if (url && !card.isConnected) {
|
|
602
|
+
URL.revokeObjectURL(url);
|
|
603
|
+
previewUrls.delete(card);
|
|
604
|
+
}
|
|
605
|
+
}
|
|
606
|
+
}
|
|
607
|
+
});
|
|
608
|
+
previewObserver.observe(list, { childList: true, subtree: true });
|
|
461
609
|
}
|
|
462
|
-
|
|
463
610
|
function replaceMentionTokens(fragment, mentions, app) {
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
const
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
611
|
+
const names = new Map(mentions.map((item) => [item.token, item]));
|
|
612
|
+
const walker = document.createTreeWalker(fragment, NodeFilter.SHOW_TEXT);
|
|
613
|
+
const nodes = [];
|
|
614
|
+
while (walker.nextNode())
|
|
615
|
+
nodes.push(walker.currentNode);
|
|
616
|
+
for (const node of nodes) {
|
|
617
|
+
const pieces = (node.nodeValue || "").split(/(CHATMENTION[\da-f-]+\d+END)/g);
|
|
618
|
+
if (pieces.length === 1)
|
|
619
|
+
continue;
|
|
620
|
+
const replacement = document.createDocumentFragment();
|
|
621
|
+
for (const piece of pieces) {
|
|
622
|
+
const mention = names.get(piece);
|
|
623
|
+
if (!mention) {
|
|
624
|
+
replacement.appendChild(document.createTextNode(piece));
|
|
625
|
+
continue;
|
|
626
|
+
}
|
|
627
|
+
if (node.parentElement?.closest("code, pre")) {
|
|
628
|
+
replacement.appendChild(document.createTextNode(mention.literal));
|
|
629
|
+
continue;
|
|
630
|
+
}
|
|
631
|
+
const chip = document.createElement("span");
|
|
632
|
+
chip.className = "mention-chip";
|
|
633
|
+
if (app.mentionNames().has(mention.name))
|
|
634
|
+
chip.classList.add("member");
|
|
635
|
+
chip.dataset.mention = mention.name;
|
|
636
|
+
chip.textContent = `@${mention.name === "all" ? app.t("allMembers") : mention.name}`;
|
|
637
|
+
replacement.appendChild(chip);
|
|
638
|
+
}
|
|
639
|
+
node.parentNode?.replaceChild(replacement, node);
|
|
485
640
|
}
|
|
486
|
-
node.replaceWith(replacement);
|
|
487
|
-
}
|
|
488
641
|
}
|