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

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.
@@ -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,146 @@
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.updateJump();
68
+ });
69
+ document.addEventListener("visibilitychange", () => {
70
+ if (!document.hidden) void app.markActiveRoomRead();
71
+ });
72
+ window.addEventListener("focus", () => void app.markActiveRoomRead());
73
+ $("#msgList").addEventListener("click", (event) => {
74
+ const button = event.target.closest("[data-copy-code]");
75
+ if (button) void copyCodeFromButton(button, app);
76
+ });
77
+ ["#inRoomId", "#inName", "#inRoomName"].forEach((selector) => {
78
+ $(selector).addEventListener("input", app.hideJoinError);
79
+ });
80
+ ["#inToken", "#inTokenChat"].forEach((selector) => {
81
+ $(selector).addEventListener("keydown", (event) => {
82
+ if (event.key === "Enter") {
83
+ event.preventDefault();
84
+ document.querySelector(`[data-token-save="${selector.slice(1)}"]`).click();
85
+ }
86
+ });
87
+ });
88
+
89
+ document.addEventListener("click", (event) => {
90
+ const save = event.target.closest("[data-token-save]");
91
+ if (save) {
92
+ saveAccessToken(save, app);
93
+ return;
94
+ }
95
+ if (app.mentionActive && !event.target.closest("#mentionPicker")) app.closeMention();
96
+ if (!event.target.closest(".anchor")) app.closePanels();
97
+ if (!event.target.closest("#railRoomForm, #btnNewRoom")) $("#railRoomForm").hidden = true;
98
+ });
99
+ document.addEventListener("keydown", (event) => {
100
+ if (event.key === "Escape" && app.mentionActive) app.closeMention();
101
+ if (event.key === "Escape" && !$("#railRoomForm").hidden) $("#railRoomForm").hidden = true;
102
+ if (event.key === "Escape" && (!$("#memberPanel").hidden || !$("#chatSettings").hidden)) {
103
+ app.closePanels();
104
+ app.lastFocus?.focus();
105
+ }
106
+ });
107
+
108
+ app.applyLocale();
109
+ $("#inToken").value = LS.token;
110
+ $("#inTokenChat").value = LS.token;
111
+ void app.refreshRoomList();
112
+ $("#inRoomId").focus();
113
+ }
114
+
115
+ function saveAccessToken(button, app) {
116
+ const input = document.getElementById(button.dataset.tokenSave);
117
+ input.value = app.setToken(input.value);
118
+ document.querySelector("#inToken").value = LS.token;
119
+ document.querySelector("#inTokenChat").value = LS.token;
120
+ let persistent = false;
121
+ try { persistent = localStorage.getItem("chat.token") === LS.token; } catch { /* Memory fallback is tab-scoped. */ }
122
+ app.flash(button, persistent ? "saved" : "savedMemory");
123
+ void app.refreshRoomList();
124
+ app.retryPendingAction();
125
+ }
126
+
127
+ function handleMessageKey(event, app) {
128
+ if (app.mentionActive && !event.isComposing) {
129
+ if (event.key === "ArrowDown") { event.preventDefault(); app.moveMention(1); return; }
130
+ if (event.key === "ArrowUp") { event.preventDefault(); app.moveMention(-1); return; }
131
+ if (event.key === "Escape") { event.preventDefault(); app.closeMention(); return; }
132
+ if (event.key === "Enter" || event.key === "Tab") {
133
+ if (app.mention?.items.length) {
134
+ event.preventDefault();
135
+ app.insertMention(app.mention.items[app.mention.index]);
136
+ return;
137
+ }
138
+ app.closeMention();
139
+ if (event.key === "Tab") return;
140
+ }
141
+ }
142
+ if (event.key === "Enter" && !event.isComposing && (event.ctrlKey || !event.shiftKey)) {
143
+ event.preventDefault();
144
+ void app.send();
145
+ }
146
+ }
@@ -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
+ }
@@ -0,0 +1,95 @@
1
+ import { LS } from "./state.js";
2
+
3
+ export function initMembers(app) {
4
+ const $ = app.$;
5
+
6
+ app.mentionNames = () => {
7
+ const room = app.state.room, members = room && app.state.memberSessions.get(room.id);
8
+ return new Set((members || room?.participants || [])
9
+ .filter((person) => members ? person.sessionID !== LS.sessionID : person.name !== app.state.myName)
10
+ .map((person) => person.name));
11
+ };
12
+
13
+ app.renderMembers = () => {
14
+ const people = app.state.room?.participants || [];
15
+ app.setText($("#btnMembers"), "membersCount", { count: people.length });
16
+ const list = $("#memberList");
17
+ list.textContent = "";
18
+ for (const person of people) {
19
+ const row = document.createElement("li"), name = document.createElement("span"), time = document.createElement("span");
20
+ name.textContent = person.name;
21
+ time.className = "m-time";
22
+ time.textContent = app.fmtTime(person.joinedAt);
23
+ row.append(name, time);
24
+ list.append(row);
25
+ }
26
+ $("#memberEmpty").hidden = people.length > 0;
27
+ if (app.mentionActive) app.updateMentionList();
28
+ void app.loadManageableMembers();
29
+ };
30
+
31
+ app.loadManageableMembers = async () => {
32
+ const room = app.state.room;
33
+ if (!room) return;
34
+ try {
35
+ const members = await app.api(`/rooms/${encodeURIComponent(room.id)}/members`);
36
+ if (app.state.room?.id !== room.id) return;
37
+ const previous = app.state.memberSessions.get(room.id) || [];
38
+ const signature = (items) => items.map((member) => `${member.sessionID}\0${member.name}`).sort().join("\u0001");
39
+ const changed = signature(previous) !== signature(members);
40
+ app.state.memberSessions.set(room.id, members);
41
+ if (changed && app.mentionActive) app.updateMentionList();
42
+ if (changed && room.kind !== "direct" && room.messages.some((message) => !message.senderSessionID && message.senderId === app.state.myName)) app.rerenderMessages();
43
+ const list = $("#memberList");
44
+ list.textContent = "";
45
+ for (const member of members) {
46
+ const row = document.createElement("li"), name = document.createElement("span"), actions = document.createElement("span");
47
+ name.textContent = member.name;
48
+ actions.className = "member-actions";
49
+ if (member.sessionID !== LS.sessionID) {
50
+ actions.append(actionButton(member, "messagePrivately", app.startDirect));
51
+ if (room.kind !== "direct") actions.append(actionButton(member, "removeMember", app.kickMember));
52
+ }
53
+ row.append(name, actions);
54
+ list.append(row);
55
+ }
56
+ $("#memberEmpty").hidden = members.length > 0;
57
+ } catch (error) {
58
+ if (error.status === 401) app.openChatSettings();
59
+ }
60
+ };
61
+
62
+ app.startDirect = async (member) => {
63
+ const source = app.state.room;
64
+ if (!source) return;
65
+ const name = app.state.myName, intent = ++app.state.navigationGeneration;
66
+ try {
67
+ const room = await app.api("/direct", {
68
+ method: "POST",
69
+ body: JSON.stringify({ roomId: source.id, sessionID: LS.sessionID, targetSessionID: member.sessionID }),
70
+ });
71
+ app.enterRoom(room, name, intent);
72
+ } catch (error) { app.showSendError("sendFailed", { message: error.message }); }
73
+ };
74
+
75
+ app.kickMember = async (member) => {
76
+ const id = member.sessionID.slice(0, 8);
77
+ if (!confirm(app.t("kickConfirm", { name: member.name, id }))) return;
78
+ try {
79
+ await app.api(`/rooms/${encodeURIComponent(app.state.room.id)}/kick`, {
80
+ method: "POST",
81
+ body: JSON.stringify({ sessionID: LS.sessionID, targetSessionID: member.sessionID }),
82
+ });
83
+ await app.refreshRoomMeta();
84
+ } catch (error) { app.showSendError("sendFailed", { message: error.message }); }
85
+ };
86
+
87
+ function actionButton(member, label, action) {
88
+ const button = document.createElement("button");
89
+ button.type = "button";
90
+ button.className = "member-action";
91
+ button.textContent = app.t(label);
92
+ button.addEventListener("click", () => action(member));
93
+ return button;
94
+ }
95
+ }
@@ -0,0 +1,131 @@
1
+ export function initMentions(app) {
2
+ const $ = app.$;
3
+ const mention = { active: false, start: 0, query: "", items: [], index: 0 };
4
+ app.mention = mention;
5
+ app.mentionActive = false;
6
+ app.updateMentionList = (query = mention.query) => {
7
+ mention.query = query;
8
+ mention.items = filterNames(query, app.mentionNames());
9
+ mention.index = mention.items.length ? 0 : -1;
10
+ const picker = $("#mentionPicker");
11
+ picker.textContent = "";
12
+ const list = document.createElement("ul");
13
+ mention.items.forEach((name, index) => list.append(createOption(name, index, query, app)));
14
+ picker.append(list);
15
+ if (!mention.items.length) {
16
+ const empty = document.createElement("p");
17
+ empty.className = "mention-empty";
18
+ empty.textContent = app.t("mentionEmpty");
19
+ picker.append(empty);
20
+ }
21
+ const hint = document.createElement("p");
22
+ hint.className = "mention-hint";
23
+ hint.textContent = app.t("mentionHint");
24
+ picker.append(hint);
25
+ highlightMention(app, mention);
26
+ };
27
+
28
+ app.openMention = (start, query) => {
29
+ mention.active = app.mentionActive = true;
30
+ mention.start = start;
31
+ app.updateMentionList(query);
32
+ $("#mentionPicker").hidden = false;
33
+ $("#msgInput").setAttribute("aria-expanded", "true");
34
+ };
35
+ app.closeMention = () => {
36
+ mention.active = app.mentionActive = false;
37
+ mention.items = [];
38
+ $("#mentionPicker").hidden = true;
39
+ $("#msgInput").setAttribute("aria-expanded", "false");
40
+ $("#msgInput").removeAttribute("aria-activedescendant");
41
+ };
42
+
43
+ app.recomputeMention = () => {
44
+ const input = $("#msgInput"), match = currentMention(input);
45
+ if (match) {
46
+ mention.active ? app.updateMentionList(match.query) : app.openMention(match.start, match.query);
47
+ } else if (mention.active) {
48
+ const at = input.value.lastIndexOf("@");
49
+ if (at < 0) return app.closeMention();
50
+ mention.start = at;
51
+ app.updateMentionList(input.value.slice(at + 1, input.selectionStart));
52
+ }
53
+ };
54
+ app.moveMention = (direction) => {
55
+ if (!mention.items.length) return;
56
+ mention.index = (mention.index + direction + mention.items.length) % mention.items.length;
57
+ highlightMention(app, mention);
58
+ };
59
+ app.insertMention = (name) => {
60
+ const input = $("#msgInput"), end = mention.start + 1 + mention.query.length;
61
+ if (input.value.slice(mention.start, end) !== `@${mention.query}`) return app.closeMention();
62
+ input.setRangeText(`<at>@${name}</at>`, mention.start, end, "end");
63
+ app.closeMention();
64
+ app.autoGrow(input);
65
+ app.updateSendState();
66
+ app.updateDraftCount();
67
+ if (app.state.room) {
68
+ const id = app.state.room.id;
69
+ app.state.draftRevision.set(id, (app.state.draftRevision.get(id) || 0) + 1);
70
+ app.saveDraft(id, input.value);
71
+ }
72
+ input.focus({ preventScroll: true });
73
+ };
74
+ }
75
+
76
+ function filterNames(query, names) {
77
+ const normalized = query.toLocaleLowerCase();
78
+ const starts = [], contains = [];
79
+ for (const name of names) {
80
+ if (!name) continue;
81
+ if (name.toLocaleLowerCase().startsWith(normalized)) starts.push(name);
82
+ else if (name.toLocaleLowerCase().includes(normalized)) contains.push(name);
83
+ }
84
+ const allMatches = !normalized || "all".includes(normalized) || "所有人".includes(normalized);
85
+ return (allMatches ? ["all"] : []).concat(starts, contains);
86
+ }
87
+
88
+ function createOption(name, index, query, app) {
89
+ const option = document.createElement("li");
90
+ option.className = "mention-opt";
91
+ option.id = `mention-opt-${index}`;
92
+ option.dataset.name = name;
93
+ option.setAttribute("role", "option");
94
+ const at = document.createElement("span");
95
+ at.className = "mention-at";
96
+ at.textContent = "@";
97
+ option.append(at);
98
+ const label = name === "all" ? app.t("allMembers") : name;
99
+ const offset = label.toLocaleLowerCase().indexOf(query.toLocaleLowerCase());
100
+ if (!query || offset < 0) option.append(document.createTextNode(label));
101
+ else {
102
+ option.append(document.createTextNode(label.slice(0, offset)));
103
+ const hit = document.createElement("span");
104
+ hit.className = "mention-hit";
105
+ hit.textContent = label.slice(offset, offset + query.length);
106
+ option.append(hit, document.createTextNode(label.slice(offset + query.length)));
107
+ }
108
+ return option;
109
+ }
110
+
111
+ function currentMention(input) {
112
+ const position = input.selectionStart, value = input.value;
113
+ let start = position;
114
+ while (start > 0 && /[\p{L}\p{N}_]/u.test(value[start - 1])) start--;
115
+ const at = start - 1;
116
+ if (at >= 0 && value[at] === "@" && (at === 0 || !/[A-Za-z0-9_]/.test(value[at - 1]))) {
117
+ return { start: at, query: value.slice(start, position) };
118
+ }
119
+ return null;
120
+ }
121
+
122
+ function highlightMention(app, mention) {
123
+ const input = app.$("#msgInput"), options = app.$("#mentionPicker").querySelectorAll(".mention-opt");
124
+ options.forEach((option, index) => {
125
+ option.classList.toggle("active", index === mention.index);
126
+ option.setAttribute("aria-selected", String(index === mention.index));
127
+ });
128
+ const active = options[mention.index];
129
+ if (!active) { input.removeAttribute("aria-activedescendant"); return; }
130
+ input.setAttribute("aria-activedescendant", active.id);
131
+ }