@alexsun-top/opencode-chat-room 0.2.3 → 0.3.1

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.
Files changed (46) hide show
  1. package/CHANGELOG.md +21 -0
  2. package/README.md +44 -7
  3. package/README.zh-CN.md +44 -7
  4. package/package.json +6 -2
  5. package/plugin.ts +21 -6
  6. package/server/assets/DOMPURIFY-LICENSE +202 -0
  7. package/server/assets/MARKED-LICENSE +44 -0
  8. package/server/assets/NOTICE.txt +6 -0
  9. package/server/assets/base.css +225 -0
  10. package/server/assets/chat/events.js +148 -0
  11. package/server/assets/chat/index.js +12 -0
  12. package/server/assets/chat/markdown.js +65 -0
  13. package/server/assets/chat/members.js +201 -0
  14. package/server/assets/chat/mentions.js +131 -0
  15. package/server/assets/chat/messages.js +489 -0
  16. package/server/assets/chat/rooms.js +443 -0
  17. package/server/assets/chat/state.js +327 -0
  18. package/server/assets/conversation.css +364 -0
  19. package/server/assets/dompurify-3.4.7.js +3 -0
  20. package/server/assets/marked-18.0.4.js +79 -0
  21. package/server/assets/responsive.css +119 -0
  22. package/server/chat-server.ts +39 -43
  23. package/server/chat.html +27 -1984
  24. package/server/history-route.ts +2 -1
  25. package/server/inbox.ts +1 -1
  26. package/server/member-routes.ts +117 -0
  27. package/server/newrooms.ts +86 -0
  28. package/server/wake.ts +13 -14
  29. package/src/agent-presence.ts +60 -0
  30. package/src/agent-receipts.ts +107 -0
  31. package/src/background.ts +70 -25
  32. package/src/center-client.ts +25 -6
  33. package/src/center-inbox.ts +77 -37
  34. package/src/center-wake.ts +43 -9
  35. package/src/member-state.ts +25 -0
  36. package/src/mentions.ts +7 -0
  37. package/src/plugin-notifications.ts +75 -23
  38. package/src/queue-transport.ts +17 -1
  39. package/src/queue.ts +36 -2
  40. package/src/receiver-state.ts +108 -6
  41. package/src/room-service.ts +19 -1
  42. package/src/sqlite-state.ts +123 -16
  43. package/src/store.ts +28 -1
  44. package/src/v2-plugin.ts +10 -2
  45. package/src/v2-receiver.ts +67 -45
  46. package/src/watch.ts +17 -3
@@ -0,0 +1,44 @@
1
+ # License information
2
+
3
+ ## Contribution License Agreement
4
+
5
+ If you contribute code to this project, you are implicitly allowing your code
6
+ to be distributed under the MIT license. You are also implicitly verifying that
7
+ all code is your original work. `</legalese>`
8
+
9
+ ## Marked
10
+
11
+ Copyright (c) 2018+, MarkedJS (https://github.com/markedjs/)
12
+ Copyright (c) 2011-2018, Christopher Jeffrey (https://github.com/chjj/)
13
+
14
+ Permission is hereby granted, free of charge, to any person obtaining a copy
15
+ of this software and associated documentation files (the "Software"), to deal
16
+ in the Software without restriction, including without limitation the rights
17
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
18
+ copies of the Software, and to permit persons to whom the Software is
19
+ furnished to do so, subject to the following conditions:
20
+
21
+ The above copyright notice and this permission notice shall be included in
22
+ all copies or substantial portions of the Software.
23
+
24
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
27
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
29
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
30
+ THE SOFTWARE.
31
+
32
+ ## Markdown
33
+
34
+ Copyright © 2004, John Gruber
35
+ http://daringfireball.net/
36
+ All rights reserved.
37
+
38
+ Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
39
+
40
+ * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
41
+ * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
42
+ * Neither the name “Markdown” nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
43
+
44
+ This software is provided by the copyright holders and contributors “as is” and any express or implied warranties, including, but not limited to, the implied warranties of merchantability and fitness for a particular purpose are disclaimed. In no event shall the copyright owner or contributors be liable for any direct, indirect, incidental, special, exemplary, or consequential damages (including, but not limited to, procurement of substitute goods or services; loss of use, data, or profits; or business interruption) however caused and on any theory of liability, whether in contract, strict liability, or tort (including negligence or otherwise) arising in any way out of the use of this software, even if advised of the possibility of such damage.
@@ -0,0 +1,6 @@
1
+ Browser vendors bundled locally; no external requests are made at runtime.
2
+
3
+ marked 18.0.4 — MIT License, see MARKED-LICENSE.
4
+ DOMPurify 3.4.7 — MPL 2.0 or Apache License 2.0, see DOMPURIFY-LICENSE.
5
+
6
+ Sources: https://www.npmjs.com/package/marked and https://www.npmjs.com/package/dompurify
@@ -0,0 +1,225 @@
1
+
2
+ * { box-sizing: border-box; }
3
+ [hidden] { display: none !important; }
4
+
5
+ :root {
6
+ --font-sans: "PingFang SC","Hiragino Sans GB","Noto Sans SC","Noto Sans CJK SC","Source Han Sans SC","Microsoft YaHei",system-ui,-apple-system,sans-serif;
7
+ --font-serif: "Songti SC","STSong","Noto Serif SC","Noto Serif CJK SC","Source Han Serif SC","SimSun",serif;
8
+ --font-mono: ui-monospace,"SF Mono","Cascadia Mono","JetBrains Mono",Consolas,monospace;
9
+ --bg: #f4f1ea;
10
+ --surface: #fdfcf8;
11
+ --surface-2: #ece9df;
12
+ --ink: #2a2721;
13
+ --ink-2: #777162;
14
+ --ink-3: #847c69;
15
+ --line: #e2ddd0;
16
+ --accent: #b85a35;
17
+ --accent-ink: #fdf6ef;
18
+ --accent-soft: #f6e3d6;
19
+ --danger: #b23a2e;
20
+ --danger-soft: #f9e7e2;
21
+ --mention-blue: #2e6cc4;
22
+ --shadow: 0 1px 2px rgb(48 38 25 / .05), 0 12px 32px rgb(48 38 25 / .08);
23
+ }
24
+
25
+ @media (prefers-color-scheme: dark) {
26
+ :root {
27
+ --bg: #17150f;
28
+ --surface: #211e18;
29
+ --surface-2: #2c2820;
30
+ --ink: #eae5d8;
31
+ --ink-2: #a19a88;
32
+ --ink-3: #8a8372;
33
+ --line: #322e25;
34
+ --accent: #dd8761;
35
+ --accent-ink: #241208;
36
+ --accent-soft: #3b2418;
37
+ --danger: #e27a6c;
38
+ --danger-soft: #3a201b;
39
+ --mention-blue: #86a9ee;
40
+ --shadow: 0 1px 2px rgb(0 0 0 / .4), 0 14px 36px rgb(0 0 0 / .45);
41
+ }
42
+ }
43
+
44
+ html, body { height: 100%; }
45
+ body {
46
+ margin: 0;
47
+ font-family: var(--font-sans);
48
+ font-size: 15px;
49
+ line-height: 1.6;
50
+ color: var(--ink);
51
+ background:
52
+ radial-gradient(1100px 520px at 50% -8%, color-mix(in srgb, var(--accent) 7%, transparent), transparent 65%),
53
+ var(--bg);
54
+ -webkit-tap-highlight-color: transparent;
55
+ }
56
+ ::selection { background: var(--accent-soft); }
57
+
58
+ #app {
59
+ max-width: 760px;
60
+ height: 100vh;
61
+ height: 100dvh;
62
+ margin: 0 auto;
63
+ display: flex;
64
+ flex-direction: column;
65
+ }
66
+
67
+ /* View layout */
68
+ .view { flex: 1; min-height: 0; }
69
+ #joinView {
70
+ display: flex;
71
+ overflow-y: auto;
72
+ padding: 40px 20px;
73
+ animation: rise .45s cubic-bezier(.2,.7,.3,1) both;
74
+ }
75
+ #chatView { display: flex; flex-direction: column; animation: view-in .3s ease both; }
76
+
77
+ @keyframes rise { from { opacity: 0; transform: translateY(12px); } }
78
+ @keyframes view-in { from { opacity: 0; } }
79
+ @keyframes msg-in { from { opacity: 0; transform: translateY(5px); } }
80
+ @keyframes pulse { 50% { opacity: .35; } }
81
+
82
+ @media (prefers-reduced-motion: reduce) {
83
+ *, *::before, *::after { animation: none !important; transition: none !important; }
84
+ }
85
+
86
+ /* Join and create */
87
+ .join-card {
88
+ width: 100%;
89
+ max-width: 424px;
90
+ margin: auto;
91
+ padding: 36px 32px 30px;
92
+ background: var(--surface);
93
+ border: 1px solid var(--line);
94
+ border-radius: 18px;
95
+ box-shadow: var(--shadow);
96
+ }
97
+ .locale-control { display: flex; align-items: center; justify-content: flex-end; gap: 8px; margin-bottom: 20px; color: var(--ink-2); font-size: 12.5px; }
98
+ .locale-control label { font-weight: 600; }
99
+ .locale-select { appearance: auto; font: inherit; font-size: 12.5px; color: var(--ink); background: var(--surface); border: 1px solid var(--line); border-radius: 8px; padding: 5px 9px; cursor: pointer; }
100
+ .chat-head-actions .locale-control { margin: 0; }
101
+ .wordmark {
102
+ margin: 0;
103
+ font-family: var(--font-serif);
104
+ font-size: 30px;
105
+ letter-spacing: .14em;
106
+ color: var(--ink);
107
+ }
108
+ .tagline { margin: 8px 0 26px; font-size: 13.5px; color: var(--ink-2); }
109
+
110
+ .field { display: block; margin-bottom: 14px; }
111
+ .field-label {
112
+ display: block;
113
+ margin-bottom: 6px;
114
+ font-size: 12.5px;
115
+ font-weight: 600;
116
+ letter-spacing: .02em;
117
+ color: var(--ink-2);
118
+ }
119
+
120
+ .input {
121
+ width: 100%;
122
+ font: inherit;
123
+ font-size: 16px; /* Keep form text at the mobile browser minimum. */
124
+ color: var(--ink);
125
+ background: var(--surface);
126
+ border: 1px solid var(--line);
127
+ border-radius: 10px;
128
+ padding: 11px 13px;
129
+ transition: border-color .15s, box-shadow .15s;
130
+ }
131
+ .input::placeholder { color: var(--ink-3); }
132
+ .input:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 20%, transparent); }
133
+
134
+ .btn {
135
+ appearance: none;
136
+ border: none;
137
+ cursor: pointer;
138
+ font: inherit;
139
+ font-weight: 600;
140
+ border-radius: 10px;
141
+ padding: 11px 18px;
142
+ transition: filter .15s, transform .1s, border-color .15s, color .15s;
143
+ }
144
+ .btn:active { transform: translateY(1px); }
145
+ .btn:disabled { opacity: .5; cursor: default; }
146
+ .btn.primary { background: var(--accent); color: var(--accent-ink); }
147
+ .btn.primary:not(:disabled):hover { filter: brightness(1.07); }
148
+ .btn.ghost { background: transparent; color: var(--ink-2); border: 1px solid var(--line); }
149
+ .btn.ghost:not(:disabled):hover { color: var(--ink); border-color: var(--ink-3); }
150
+ .btn.small { padding: 8px 14px; font-size: 13.5px; }
151
+ .btn:focus-visible, .chip:focus-visible, .room-row:focus-visible, .input:focus-visible,
152
+ .jump:focus-visible, .copy-id:focus-visible, .locale-select:focus-visible {
153
+ outline: 2px solid var(--accent);
154
+ outline-offset: 2px;
155
+ }
156
+
157
+ #joinForm .btn.primary, #createForm .btn.ghost { width: 100%; margin-top: 4px; }
158
+
159
+ .or { display: flex; align-items: center; gap: 12px; margin: 18px 0; font-size: 12.5px; color: var(--ink-3); }
160
+ .or::before, .or::after { content: ""; flex: 1; height: 1px; background: var(--line); }
161
+
162
+ .alert {
163
+ margin-top: 16px;
164
+ padding: 10px 13px;
165
+ border-radius: 10px;
166
+ background: var(--danger-soft);
167
+ color: var(--danger);
168
+ font-size: 13.5px;
169
+ word-break: break-word;
170
+ }
171
+
172
+ .hint { margin: 8px 0 0; font-size: 12.5px; color: var(--ink-3); }
173
+
174
+ .token-box { margin-top: 18px; border: 1px solid var(--line); border-radius: 12px; background: var(--surface); }
175
+ .token-box summary {
176
+ cursor: pointer;
177
+ padding: 11px 14px;
178
+ font-size: 13.5px;
179
+ font-weight: 600;
180
+ color: var(--ink-2);
181
+ list-style: none;
182
+ }
183
+ .token-box summary::-webkit-details-marker { display: none; }
184
+ .token-box summary::marker { content: none; }
185
+ .token-box summary::after { content: "▾"; margin-left: 6px; font-size: 11px; color: var(--ink-3); }
186
+ .token-box[open] summary::after { content: "▴"; }
187
+ .token-body { padding: 0 14px 14px; }
188
+ .token-body .btn { margin-top: 10px; }
189
+
190
+ .room-list { margin-top: 22px; }
191
+ .room-list h2 { margin: 0 0 8px; font-size: 13px; font-weight: 600; letter-spacing: .06em; color: var(--ink-2); }
192
+ #roomList { list-style: none; margin: 0; padding: 0; max-height: 240px; overflow-y: auto; }
193
+ .room-row {
194
+ position: relative;
195
+ display: flex;
196
+ flex-direction: column;
197
+ gap: 2px;
198
+ width: 100%;
199
+ padding: 9px 44px 9px 11px;
200
+ font: inherit;
201
+ text-align: left;
202
+ color: var(--ink);
203
+ background: none;
204
+ border: 1px solid transparent;
205
+ border-radius: 10px;
206
+ cursor: pointer;
207
+ transition: background .12s, border-color .12s;
208
+ }
209
+ .room-row:hover { background: var(--surface-2); border-color: var(--line); }
210
+ .r-name { font-weight: 600; font-size: 14px; }
211
+ .r-meta { font-size: 12px; color: var(--ink-3); font-variant-numeric: tabular-nums; }
212
+ .r-go {
213
+ position: absolute;
214
+ right: 12px;
215
+ top: 50%;
216
+ transform: translateY(-50%);
217
+ font-size: 12px;
218
+ font-weight: 600;
219
+ color: var(--accent);
220
+ opacity: 0;
221
+ transition: opacity .12s;
222
+ }
223
+ .room-row:hover .r-go, .room-row:focus-visible .r-go { opacity: 1; }
224
+
225
+ /* Conversation layout */
@@ -0,0 +1,148 @@
1
+ import { LS } from "./state.js";
2
+ import { copyCodeFromButton } from "./markdown.js";
3
+
4
+ export function initEvents(app) {
5
+ const $ = app.$;
6
+ document.querySelectorAll(".locale-select").forEach((select) => {
7
+ select.addEventListener("change", () => app.setLocale(select.value));
8
+ });
9
+ $("#joinForm").addEventListener("submit", (event) => {
10
+ event.preventDefault();
11
+ const id = $("#inRoomId").value.trim(), name = $("#inName").value.trim();
12
+ if (!id) return app.showJoinError("roomIdRequired");
13
+ if (!name) return app.showJoinError("nameRequired");
14
+ void app.joinRoom(id, name);
15
+ });
16
+ $("#createForm").addEventListener("submit", (event) => { event.preventDefault(); void app.createRoom(); });
17
+ $("#btnSend").addEventListener("click", () => void app.send());
18
+ $("#btnJump").addEventListener("click", app.jumpToBottom);
19
+ $("#btnCopyId").addEventListener("click", () => void app.copyRoomId());
20
+ $("#btnLeave").addEventListener("click", () => app.leaveRoom(false));
21
+ $("#btnDeleteRoom").addEventListener("click", () => void app.deleteRoom());
22
+ $("#btnMembers").addEventListener("click", () => app.togglePanel("#memberPanel"));
23
+ $("#btnSettings").addEventListener("click", () => app.togglePanel("#chatSettings"));
24
+ $("#btnRefreshRooms").addEventListener("click", () => void app.refreshRoomList());
25
+ $("#btnNewRoom").addEventListener("click", () => {
26
+ const form = $("#railRoomForm");
27
+ form.hidden = !form.hidden;
28
+ if (!form.hidden) $("#inRoomNameChat").focus();
29
+ });
30
+ $("#railRoomForm").addEventListener("submit", (event) => { event.preventDefault(); void app.createRoom(); });
31
+ $("#inRoomNameChat").addEventListener("input", app.hideSendError);
32
+
33
+ const input = $("#msgInput");
34
+ $(".composer").addEventListener("click", (event) => {
35
+ const button = event.target.closest?.("[data-failed-action]");
36
+ if (!button) return;
37
+ const { failedAction, failedRoom, failedId } = button.dataset;
38
+ if (failedAction === "retry") void app.retryFailedMessage(failedRoom, failedId);
39
+ else app.discardFailedMessage(failedRoom, failedId);
40
+ });
41
+ input.addEventListener("input", () => {
42
+ app.autoGrow(input);
43
+ app.updateSendState();
44
+ app.updateDraftCount();
45
+ if (app.state.room) {
46
+ const id = app.state.room.id;
47
+ app.state.draftRevision.set(id, (app.state.draftRevision.get(id) || 0) + 1);
48
+ app.saveDraft(id, input.value);
49
+ }
50
+ app.recomputeMention();
51
+ });
52
+ input.addEventListener("keydown", (event) => handleMessageKey(event, app));
53
+ input.addEventListener("keyup", (event) => {
54
+ if (app.mentionActive && /^(ArrowLeft|ArrowRight|Home|End)$/.test(event.key)) app.recomputeMention();
55
+ });
56
+ input.addEventListener("click", app.recomputeMention);
57
+ input.addEventListener("blur", () => app.closeMention());
58
+ $("#mentionPicker").addEventListener("mousedown", (event) => {
59
+ const option = event.target.closest(".mention-opt");
60
+ if (!option) return;
61
+ event.preventDefault();
62
+ app.insertMention(option.dataset.name);
63
+ });
64
+ $("#thread").addEventListener("scroll", () => {
65
+ const thread = $("#thread");
66
+ if (thread.scrollHeight - thread.scrollTop - thread.clientHeight < 80) void app.markActiveRoomRead();
67
+ app.queueReceiptRefresh();
68
+ app.queueReceiptObservation();
69
+ app.updateJump();
70
+ });
71
+ document.addEventListener("visibilitychange", () => {
72
+ if (!document.hidden) { void app.markActiveRoomRead(); void app.loadManageableMembers(); }
73
+ });
74
+ window.addEventListener("focus", () => { void app.markActiveRoomRead(); app.queueReceiptRefresh(); app.queueReceiptObservation(); });
75
+ $("#msgList").addEventListener("click", (event) => {
76
+ const button = event.target.closest("[data-copy-code]");
77
+ if (button) void copyCodeFromButton(button, app);
78
+ });
79
+ ["#inRoomId", "#inName", "#inRoomName"].forEach((selector) => {
80
+ $(selector).addEventListener("input", app.hideJoinError);
81
+ });
82
+ ["#inToken", "#inTokenChat"].forEach((selector) => {
83
+ $(selector).addEventListener("keydown", (event) => {
84
+ if (event.key === "Enter") {
85
+ event.preventDefault();
86
+ document.querySelector(`[data-token-save="${selector.slice(1)}"]`).click();
87
+ }
88
+ });
89
+ });
90
+
91
+ document.addEventListener("click", (event) => {
92
+ const save = event.target.closest("[data-token-save]");
93
+ if (save) {
94
+ saveAccessToken(save, app);
95
+ return;
96
+ }
97
+ if (app.mentionActive && !event.target.closest("#mentionPicker")) app.closeMention();
98
+ if (!event.target.closest(".anchor")) app.closePanels();
99
+ if (!event.target.closest("#railRoomForm, #btnNewRoom")) $("#railRoomForm").hidden = true;
100
+ });
101
+ document.addEventListener("keydown", (event) => {
102
+ if (event.key === "Escape" && app.mentionActive) app.closeMention();
103
+ if (event.key === "Escape" && !$("#railRoomForm").hidden) $("#railRoomForm").hidden = true;
104
+ if (event.key === "Escape" && (!$("#memberPanel").hidden || !$("#chatSettings").hidden)) {
105
+ app.closePanels();
106
+ app.lastFocus?.focus();
107
+ }
108
+ });
109
+
110
+ app.applyLocale();
111
+ $("#inToken").value = LS.token;
112
+ $("#inTokenChat").value = LS.token;
113
+ void app.refreshRoomList();
114
+ $("#inRoomId").focus();
115
+ }
116
+
117
+ function saveAccessToken(button, app) {
118
+ const input = document.getElementById(button.dataset.tokenSave);
119
+ input.value = app.setToken(input.value);
120
+ document.querySelector("#inToken").value = LS.token;
121
+ document.querySelector("#inTokenChat").value = LS.token;
122
+ let persistent = false;
123
+ try { persistent = localStorage.getItem("chat.token") === LS.token; } catch { /* Memory fallback is tab-scoped. */ }
124
+ app.flash(button, persistent ? "saved" : "savedMemory");
125
+ void app.refreshRoomList();
126
+ app.retryPendingAction();
127
+ }
128
+
129
+ function handleMessageKey(event, app) {
130
+ if (app.mentionActive && !event.isComposing) {
131
+ if (event.key === "ArrowDown") { event.preventDefault(); app.moveMention(1); return; }
132
+ if (event.key === "ArrowUp") { event.preventDefault(); app.moveMention(-1); return; }
133
+ if (event.key === "Escape") { event.preventDefault(); app.closeMention(); return; }
134
+ if (event.key === "Enter" || event.key === "Tab") {
135
+ if (app.mention?.items.length) {
136
+ event.preventDefault();
137
+ app.insertMention(app.mention.items[app.mention.index]);
138
+ return;
139
+ }
140
+ app.closeMention();
141
+ if (event.key === "Tab") return;
142
+ }
143
+ }
144
+ if (event.key === "Enter" && !event.isComposing && (event.ctrlKey || !event.shiftKey)) {
145
+ event.preventDefault();
146
+ void app.send();
147
+ }
148
+ }
@@ -0,0 +1,12 @@
1
+ import { app } from "./state.js";
2
+ import { initMentions } from "./mentions.js";
3
+ import { initMessages } from "./messages.js";
4
+ import { initMembers } from "./members.js";
5
+ import { initRooms } from "./rooms.js";
6
+ import { initEvents } from "./events.js";
7
+
8
+ initMentions(app);
9
+ initMessages(app);
10
+ initMembers(app);
11
+ initRooms(app);
12
+ initEvents(app);
@@ -0,0 +1,65 @@
1
+ import { createRandomId } from "./state.js";
2
+
3
+ const mentionPattern = /<at>@([^<]+)<\/at>/g;
4
+
5
+ export function renderMarkdown(root, text, app) {
6
+ const mentions = [];
7
+ const nonce = createRandomId();
8
+ const source = String(text).replace(mentionPattern, (_, name) => {
9
+ const token = `CHATMENTION${nonce}${mentions.length}END`;
10
+ mentions.push({ token, name, literal: `<at>@${name}</at>` });
11
+ return token;
12
+ });
13
+ const parsed = window.marked.parse(source, { gfm: true, breaks: true, async: false });
14
+ const fragment = window.DOMPurify.sanitize(parsed, { RETURN_DOM_FRAGMENT: true });
15
+ replaceMentionTokens(fragment, mentions, app);
16
+ fragment.querySelectorAll("a").forEach((link) => {
17
+ link.target = "_blank";
18
+ link.rel = "noopener noreferrer";
19
+ });
20
+ fragment.querySelectorAll('input[type="checkbox"]').forEach((input) => { input.disabled = true; });
21
+ fragment.querySelectorAll("pre").forEach((pre) => {
22
+ const button = document.createElement("button");
23
+ button.type = "button";
24
+ button.className = "code-copy";
25
+ button.dataset.copyCode = "";
26
+ button.dataset.i18n = "copyCode";
27
+ button.textContent = app.t("copyCode");
28
+ pre.appendChild(button);
29
+ });
30
+ root.appendChild(fragment);
31
+ }
32
+
33
+ function replaceMentionTokens(fragment, mentions, app) {
34
+ const names = new Map(mentions.map((item) => [item.token, item]));
35
+ const walker = document.createTreeWalker(fragment, NodeFilter.SHOW_TEXT);
36
+ const nodes = [];
37
+ while (walker.nextNode()) nodes.push(walker.currentNode);
38
+ for (const node of nodes) {
39
+ const pieces = (node.nodeValue || "").split(/(CHATMENTION[\da-f-]+\d+END)/g);
40
+ if (pieces.length === 1) continue;
41
+ const replacement = document.createDocumentFragment();
42
+ for (const piece of pieces) {
43
+ const mention = names.get(piece);
44
+ if (!mention) { replacement.appendChild(document.createTextNode(piece)); continue; }
45
+ if (node.parentElement?.closest("code, pre")) {
46
+ replacement.appendChild(document.createTextNode(mention.literal));
47
+ continue;
48
+ }
49
+ const chip = document.createElement("span");
50
+ chip.className = "mention-chip";
51
+ if (app.mentionNames().has(mention.name)) chip.classList.add("member");
52
+ chip.textContent = `@${mention.name}`;
53
+ replacement.appendChild(chip);
54
+ }
55
+ node.replaceWith(replacement);
56
+ }
57
+ }
58
+
59
+ export function copyCodeFromButton(button, app) {
60
+ const code = button.parentElement.querySelector("code")?.textContent || "";
61
+ return navigator.clipboard?.writeText(code).then(
62
+ () => app.flash(button, "copiedCode"),
63
+ () => app.showSendError("copyCodeFailed"),
64
+ ) ?? app.showSendError("copyCodeFailed");
65
+ }