@alexsun-top/opencode-chat-room 0.8.1 → 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 +33 -0
- package/README.md +18 -62
- package/README.zh-CN.md +25 -69
- package/dist/cli.js +1008 -274
- package/package.json +10 -7
- package/packages/chat-room-channeld/README.md +42 -15
- package/packages/chat-room-channeld/dist/mock-adapter.js +5 -5
- package/packages/chat-room-channeld/src/adapter.ts +150 -7
- package/packages/chat-room-channeld/src/cli.ts +72 -5
- package/packages/chat-room-channeld/src/host.ts +41 -11
- package/packages/chat-room-channeld/src/inbound.ts +13 -4
- 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 +53 -1
- package/packages/chat-room-channeld/src/rules.ts +7 -2
- package/packages/chat-room-channeld/src/state.ts +73 -97
- package/packages/chat-room-channeld/src/stats.ts +140 -109
- package/plugin.ts +1 -1
- package/server/assets/base.css +11 -3
- package/server/assets/chat/admin-badge.js +32 -30
- package/server/assets/chat/admin.js +198 -163
- package/server/assets/chat/announcements.js +295 -210
- package/server/assets/chat/attachment-store.js +106 -75
- package/server/assets/chat/attachments.js +686 -441
- package/server/assets/chat/chat-background.js +206 -0
- package/server/assets/chat/clipboard.js +44 -25
- package/server/assets/chat/confirm-dialog.js +36 -34
- package/server/assets/chat/copy-action.js +40 -40
- package/server/assets/chat/create-room-dialog.js +50 -48
- package/server/assets/chat/diagram-viewer.js +293 -228
- package/server/assets/chat/doc-preview.js +510 -388
- package/server/assets/chat/editor.js +423 -335
- package/server/assets/chat/events.js +637 -502
- package/server/assets/chat/files.js +476 -426
- 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 -169
- package/server/assets/chat/index.js +6 -5
- package/server/assets/chat/install-prompt.js +86 -66
- package/server/assets/chat/integration-account-schema.js +101 -0
- package/server/assets/chat/integration-editor.js +162 -153
- package/server/assets/chat/integration-values.js +109 -100
- package/server/assets/chat/integrations.js +696 -479
- 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 +553 -386
- 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 -479
- package/server/assets/chat/office-preview.js +46 -45
- package/server/assets/chat/palette.js +450 -412
- package/server/assets/chat/panels.js +94 -58
- 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 -122
- package/server/assets/chat/reminders.js +331 -216
- package/server/assets/chat/replies.js +165 -136
- 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 -505
- package/server/assets/chat/search.js +136 -114
- package/server/assets/chat/settings.js +229 -140
- package/server/assets/chat/shiki.js +61 -62
- package/server/assets/chat/shortcuts.js +212 -190
- package/server/assets/chat/side-pane.js +166 -131
- package/server/assets/chat/state.js +851 -707
- package/server/assets/chat/system-events.js +55 -27
- package/server/assets/chat/system-messages.js +59 -49
- 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 -593
- package/server/assets/chat/viewport.js +32 -29
- package/server/assets/chat/window-controls.js +37 -0
- package/server/assets/code-highlight.css +22 -6
- package/server/assets/conversation.css +65 -16
- package/server/assets/doc-preview.css +6 -1
- package/server/assets/files.css +3 -0
- package/server/assets/monaco/integration-rules.css +1 -1
- package/server/assets/monaco/integration-rules.js +841 -533
- package/server/assets/responsive.css +32 -2
- package/server/assets/settings.css +117 -13
- package/server/assets/window-controls.css +36 -0
- package/server/channel-attachments.ts +133 -0
- package/server/channel-integration.ts +79 -26
- package/server/channels.ts +15 -5
- package/server/chat-server.ts +9 -1
- package/server/chat.html +73 -38
- package/server/manifest.webmanifest +1 -0
- package/server/routes/pages.ts +7 -5
- package/server/sw.js +346 -267
- package/src/center/center-client.ts +1 -1
- package/src/state/pg-database.ts +5 -5
- package/src/state/pg-migrate.ts +7 -7
- package/src/state/pg-worker.mjs +2 -17
- package/src/state/sqlite-database.ts +14 -5
- package/src/state/sqlite-rooms.ts +130 -23
- package/src/state/sqlite-state-store.ts +6 -5
- package/src/state/sqlite.ts +3 -3
- package/src/web/integration-rules-editor.ts +141 -5
- package/src/web/integration-rules.ts +6 -2
- package/src/web/integration-schema.ts +7 -6
- package/packages/chat-room-channeld/dist/feishu-adapter.js +0 -1262
- package/packages/chat-room-channeld/src/feishu-adapter.ts +0 -303
- package/packages/chat-room-channeld/src/feishu-card.ts +0 -27
- package/packages/chat-room-channeld/src/feishu-inbound.ts +0 -158
- package/packages/chat-room-channeld/src/feishu-live.ts +0 -355
- package/packages/chat-room-channeld/src/feishu-send.ts +0 -342
- package/packages/chat-room-channeld/src/feishu-ws-frames.ts +0 -173
- package/server/assets/chat/forward.js +0 -69
|
@@ -6,460 +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]);
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Render plain text that may contain resource markers into attachment cards.
|
|
43
|
+
* System relay rows use this instead of textContent so inbound files stay openable.
|
|
44
|
+
*/
|
|
45
|
+
export function renderResourceText(root, text, app) {
|
|
46
|
+
root.textContent = text;
|
|
47
|
+
for (const resource of replaceResourceMarkers(root, app)) {
|
|
48
|
+
void loadResourceCard(resource.card, resource.roomId, app);
|
|
49
|
+
}
|
|
43
50
|
}
|
|
44
|
-
|
|
45
51
|
export function renderMarkdown(root, text, app) {
|
|
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
|
-
const
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
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);
|
|
77
102
|
}
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
wrapTables(fragment);
|
|
81
|
-
fragment.querySelectorAll('input[type="checkbox"]').forEach((input) => { input.disabled = true; });
|
|
82
|
-
const codeBlocks = [...fragment.querySelectorAll("pre > code")];
|
|
83
|
-
for (const code of codeBlocks) decorateCodeBlock(code, app);
|
|
84
|
-
root.appendChild(fragment);
|
|
85
|
-
// Highlight after insertion: the escaped text is readable while the bundle loads.
|
|
86
|
-
for (const code of codeBlocks) void enhanceCodeElement(code);
|
|
87
|
-
for (let i = 0; i < diagramNodes.length; i++) {
|
|
88
|
-
const source = diagrams.get(`CHATDIAGRAM${nonce}${i}END`);
|
|
89
|
-
if (!isUnsafeDiagram(source)) void renderDiagram(diagramNodes[i], source, app);
|
|
90
|
-
}
|
|
91
|
-
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);
|
|
92
105
|
}
|
|
93
|
-
|
|
94
106
|
// A narrow screen scrolls the table inside a frame instead of widening the message.
|
|
95
107
|
function wrapTables(fragment) {
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
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
|
+
}
|
|
102
114
|
}
|
|
103
|
-
|
|
104
115
|
function decorateCodeBlock(code, app) {
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
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);
|
|
120
144
|
}
|
|
121
|
-
|
|
122
145
|
function codeToolbar(code, pre, block, app) {
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
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;
|
|
145
171
|
}
|
|
146
|
-
|
|
147
172
|
function codeToolButton(className, key, app, ...icons) {
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
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;
|
|
157
182
|
}
|
|
158
|
-
|
|
159
183
|
const wrapIcon = () => iconNode("text_wrap", "code-tool-icon code-wrap-icon");
|
|
160
184
|
const copyIcon = () => iconNode("copy", "code-tool-icon code-copy-icon");
|
|
161
185
|
const checkIcon = () => iconNode("checkmark", "code-tool-icon code-check-icon");
|
|
162
|
-
|
|
163
186
|
async function copyCode(button, code, app) {
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
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);
|
|
176
199
|
}
|
|
177
|
-
|
|
178
200
|
// A rendered diagram is a button: click, Enter, or Space opens the readable overlay.
|
|
179
201
|
function activateDiagram(node, source, app) {
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
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
|
+
});
|
|
198
223
|
}
|
|
199
|
-
|
|
200
224
|
function replaceResourceMarkers(fragment, app) {
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
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
|
+
}
|
|
215
252
|
}
|
|
216
|
-
|
|
217
|
-
}
|
|
218
|
-
return cards;
|
|
253
|
+
return cards;
|
|
219
254
|
}
|
|
220
255
|
async function loadResourceCard(card, roomId, app) {
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
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");
|
|
248
298
|
}
|
|
249
|
-
} catch (error) { if (card.isConnected) setResourceError(card, app, error?.status === 404 ? "resourceDeleted" : "resourceFailed"); }
|
|
250
299
|
}
|
|
251
300
|
function setResourceButton(button, key, name, app) {
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
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}`);
|
|
256
308
|
}
|
|
257
309
|
function resourceButton(key, name, action, app) {
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
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;
|
|
266
320
|
}
|
|
267
321
|
function resourceMenuItem(key, action, app) {
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
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;
|
|
275
333
|
}
|
|
276
334
|
function resourceSplit(app, resource, download, item) {
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
menu.
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
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;
|
|
316
404
|
}
|
|
317
405
|
async function deleteResource(card, roomId, id, resource, app, status, button) {
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
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
|
+
}
|
|
335
437
|
}
|
|
336
|
-
} catch {
|
|
337
|
-
if (card.isConnected) { status.textContent = app.t("resourceDeleteFailed"); button.disabled = false; }
|
|
338
|
-
}
|
|
339
438
|
}
|
|
340
439
|
async function downloadResource(card, roomId, id, resource, app, status, button) {
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
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
|
+
}
|
|
353
465
|
}
|
|
354
466
|
async function previewResource(card, roomId, id, resource, type, button, app, status) {
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
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
|
+
};
|
|
358
506
|
try {
|
|
359
|
-
|
|
360
|
-
card.
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
}
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
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
|
+
}
|
|
399
547
|
}
|
|
400
548
|
function waitForMediaSize(media, kind) {
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
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
|
+
});
|
|
408
558
|
}
|
|
409
559
|
function previewKind(type) {
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
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 "";
|
|
414
567
|
}
|
|
415
568
|
export function formatBytes(value) {
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
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`;
|
|
419
573
|
}
|
|
420
574
|
function setResourceError(card, app, key = "resourceFailed") {
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
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);
|
|
424
581
|
}
|
|
425
582
|
function watchPreviewRemoval() {
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
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 });
|
|
438
600
|
}
|
|
439
|
-
|
|
440
601
|
function replaceMentionTokens(fragment, mentions, app) {
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
const
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
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);
|
|
462
631
|
}
|
|
463
|
-
node.replaceWith(replacement);
|
|
464
|
-
}
|
|
465
632
|
}
|