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